Mercurial > repos > RelicTools > PatternSwitcher
view FrmConfig.cs @ 0:a70d6ce73a0e default tip
Initial commit under GPLv3
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sat, 06 Oct 2018 20:07:03 +0100 |
parents | |
children |
line wrap: on
line source
// This file is a part of the Pattern Switcher app and is copyright 2006-2018 IBBoard. // // The file and the library/program it is in are licensed under the GNU GPL license, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license. using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.IO; using IBBoard; namespace IBBoard.Relic.PatternSwitcher { /// <summary> /// Summary description for FrmConfig. /// </summary> public class FrmConfig : System.Windows.Forms.Form { private System.Windows.Forms.Label lblDoWPath; private System.Windows.Forms.Label lblPatternPath; private System.Windows.Forms.TextBox txtDoWPath; private System.Windows.Forms.TextBox txtPatternPath; private System.Windows.Forms.Button bttnDoWPath; private System.Windows.Forms.Button bttnPatternPath; private System.Windows.Forms.Button bttnOK; private System.Windows.Forms.Button bttnCancel; private System.Windows.Forms.FolderBrowserDialog fbdPath; /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null; private System.Windows.Forms.Button bttnSave; private ArrayList changed; private System.Windows.Forms.Button bttnSaveAs; private System.Windows.Forms.Button bttnLoad; private System.Windows.Forms.SaveFileDialog saveFileDialog; private System.Windows.Forms.OpenFileDialog openFileDialog; private System.Windows.Forms.ListBox lstMappings; private System.Windows.Forms.Label label1; private System.Windows.Forms.Button bttnAddMapping; private System.Windows.Forms.Button bttnEditMapping; private System.Windows.Forms.Button bttnRemoveMapping; private ArrayList mappingsList = new ArrayList(); Preferences pref; public FrmConfig(Preferences prefs):this() { pref = prefs; changed = new ArrayList(); } private FrmConfig() { // // Required for Windows Form Designer support // InitializeComponent(); } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.lblDoWPath = new System.Windows.Forms.Label(); this.lblPatternPath = new System.Windows.Forms.Label(); this.fbdPath = new System.Windows.Forms.FolderBrowserDialog(); this.txtDoWPath = new System.Windows.Forms.TextBox(); this.txtPatternPath = new System.Windows.Forms.TextBox(); this.bttnDoWPath = new System.Windows.Forms.Button(); this.bttnPatternPath = new System.Windows.Forms.Button(); this.bttnOK = new System.Windows.Forms.Button(); this.bttnCancel = new System.Windows.Forms.Button(); this.bttnSave = new System.Windows.Forms.Button(); this.bttnSaveAs = new System.Windows.Forms.Button(); this.bttnLoad = new System.Windows.Forms.Button(); this.saveFileDialog = new System.Windows.Forms.SaveFileDialog(); this.openFileDialog = new System.Windows.Forms.OpenFileDialog(); this.lstMappings = new System.Windows.Forms.ListBox(); this.label1 = new System.Windows.Forms.Label(); this.bttnAddMapping = new System.Windows.Forms.Button(); this.bttnEditMapping = new System.Windows.Forms.Button(); this.bttnRemoveMapping = new System.Windows.Forms.Button(); this.SuspendLayout(); // // lblDoWPath // this.lblDoWPath.Location = new System.Drawing.Point(8, 8); this.lblDoWPath.Name = "lblDoWPath"; this.lblDoWPath.Size = new System.Drawing.Size(128, 23); this.lblDoWPath.TabIndex = 0; this.lblDoWPath.Text = "Game Path:"; this.lblDoWPath.TextAlign = System.Drawing.ContentAlignment.TopRight; // // lblPatternPath // this.lblPatternPath.Location = new System.Drawing.Point(8, 40); this.lblPatternPath.Name = "lblPatternPath"; this.lblPatternPath.Size = new System.Drawing.Size(128, 23); this.lblPatternPath.TabIndex = 1; this.lblPatternPath.Text = "Pattern Path:"; this.lblPatternPath.TextAlign = System.Drawing.ContentAlignment.TopRight; // // txtDoWPath // this.txtDoWPath.Location = new System.Drawing.Point(136, 8); this.txtDoWPath.Name = "txtDoWPath"; this.txtDoWPath.Size = new System.Drawing.Size(320, 20); this.txtDoWPath.TabIndex = 2; this.txtDoWPath.Tag = "DoWPath"; this.txtDoWPath.Text = ""; this.txtDoWPath.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textbox_KeyDown); this.txtDoWPath.TextChanged += new System.EventHandler(this.textbox_TextChanged); this.txtDoWPath.Leave += new System.EventHandler(this.textbox_Leave); // // txtPatternPath // this.txtPatternPath.Location = new System.Drawing.Point(136, 40); this.txtPatternPath.Name = "txtPatternPath"; this.txtPatternPath.Size = new System.Drawing.Size(320, 20); this.txtPatternPath.TabIndex = 4; this.txtPatternPath.Tag = "DoWTexturePath"; this.txtPatternPath.Text = ""; this.txtPatternPath.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textbox_KeyDown); this.txtPatternPath.TextChanged += new System.EventHandler(this.textbox_TextChanged); this.txtPatternPath.Leave += new System.EventHandler(this.textbox_Leave); // // bttnDoWPath // this.bttnDoWPath.FlatStyle = System.Windows.Forms.FlatStyle.System; this.bttnDoWPath.Location = new System.Drawing.Point(464, 8); this.bttnDoWPath.Name = "bttnDoWPath"; this.bttnDoWPath.TabIndex = 11; this.bttnDoWPath.Text = "Browse"; this.bttnDoWPath.Click += new System.EventHandler(this.bttnDoWPath_Click); // // bttnPatternPath // this.bttnPatternPath.FlatStyle = System.Windows.Forms.FlatStyle.System; this.bttnPatternPath.Location = new System.Drawing.Point(464, 40); this.bttnPatternPath.Name = "bttnPatternPath"; this.bttnPatternPath.TabIndex = 13; this.bttnPatternPath.Text = "Browse"; this.bttnPatternPath.Click += new System.EventHandler(this.bttnPatternPath_Click); // // bttnOK // this.bttnOK.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.bttnOK.FlatStyle = System.Windows.Forms.FlatStyle.System; this.bttnOK.Location = new System.Drawing.Point(384, 192); this.bttnOK.Name = "bttnOK"; this.bttnOK.TabIndex = 8; this.bttnOK.Text = "OK"; this.bttnOK.Click += new System.EventHandler(this.bttnOK_Click); // // bttnCancel // this.bttnCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.bttnCancel.FlatStyle = System.Windows.Forms.FlatStyle.System; this.bttnCancel.Location = new System.Drawing.Point(464, 192); this.bttnCancel.Name = "bttnCancel"; this.bttnCancel.TabIndex = 9; this.bttnCancel.Text = "Cancel"; this.bttnCancel.Click += new System.EventHandler(this.bttnCancel_Click); // // bttnSave // this.bttnSave.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.bttnSave.FlatStyle = System.Windows.Forms.FlatStyle.System; this.bttnSave.Location = new System.Drawing.Point(8, 192); this.bttnSave.Name = "bttnSave"; this.bttnSave.TabIndex = 10; this.bttnSave.Text = "Save"; this.bttnSave.Click += new System.EventHandler(this.bttnSave_Click); // // bttnSaveAs // this.bttnSaveAs.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.bttnSaveAs.FlatStyle = System.Windows.Forms.FlatStyle.System; this.bttnSaveAs.Location = new System.Drawing.Point(88, 192); this.bttnSaveAs.Name = "bttnSaveAs"; this.bttnSaveAs.TabIndex = 15; this.bttnSaveAs.Text = "Save As"; this.bttnSaveAs.Click += new System.EventHandler(this.bttnSaveAs_Click); // // bttnLoad // this.bttnLoad.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.bttnLoad.FlatStyle = System.Windows.Forms.FlatStyle.System; this.bttnLoad.Location = new System.Drawing.Point(168, 192); this.bttnLoad.Name = "bttnLoad"; this.bttnLoad.TabIndex = 16; this.bttnLoad.Text = "Load"; this.bttnLoad.Click += new System.EventHandler(this.bttnLoad_Click); // // saveFileDialog // this.saveFileDialog.DefaultExt = "xml"; this.saveFileDialog.Filter = "Preferences|*.xml"; this.saveFileDialog.Title = "Save Preferences"; // // openFileDialog // this.openFileDialog.DefaultExt = "xml"; this.openFileDialog.Filter = "Prefereces|*.xml"; this.openFileDialog.Title = "Load Preferences"; // // lstMappings // this.lstMappings.Location = new System.Drawing.Point(136, 72); this.lstMappings.Name = "lstMappings"; this.lstMappings.Size = new System.Drawing.Size(320, 95); this.lstMappings.TabIndex = 17; this.lstMappings.SelectedIndexChanged += new System.EventHandler(this.lstMappings_SelectedIndexChanged); // // label1 // this.label1.Location = new System.Drawing.Point(8, 72); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(128, 23); this.label1.TabIndex = 18; this.label1.Text = "Race Folder Mappings:"; this.label1.TextAlign = System.Drawing.ContentAlignment.TopRight; // // bttnAddMapping // this.bttnAddMapping.FlatStyle = System.Windows.Forms.FlatStyle.System; this.bttnAddMapping.Location = new System.Drawing.Point(464, 72); this.bttnAddMapping.Name = "bttnAddMapping"; this.bttnAddMapping.TabIndex = 19; this.bttnAddMapping.Text = "Add"; this.bttnAddMapping.Click += new System.EventHandler(this.bttnAddMapping_Click); // // bttnEditMapping // this.bttnEditMapping.Enabled = false; this.bttnEditMapping.FlatStyle = System.Windows.Forms.FlatStyle.System; this.bttnEditMapping.Location = new System.Drawing.Point(464, 104); this.bttnEditMapping.Name = "bttnEditMapping"; this.bttnEditMapping.TabIndex = 20; this.bttnEditMapping.Text = "Edit"; this.bttnEditMapping.Click += new System.EventHandler(this.bttnEditMapping_Click); // // bttnRemoveMapping // this.bttnRemoveMapping.Enabled = false; this.bttnRemoveMapping.FlatStyle = System.Windows.Forms.FlatStyle.System; this.bttnRemoveMapping.Location = new System.Drawing.Point(464, 136); this.bttnRemoveMapping.Name = "bttnRemoveMapping"; this.bttnRemoveMapping.TabIndex = 21; this.bttnRemoveMapping.Text = "Remove"; this.bttnRemoveMapping.Click += new System.EventHandler(this.bttnRemoveMapping_Click); // // FrmConfig // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(546, 220); this.ControlBox = false; this.Controls.Add(this.bttnRemoveMapping); this.Controls.Add(this.bttnEditMapping); this.Controls.Add(this.bttnAddMapping); this.Controls.Add(this.label1); this.Controls.Add(this.lstMappings); this.Controls.Add(this.bttnLoad); this.Controls.Add(this.bttnSaveAs); this.Controls.Add(this.txtPatternPath); this.Controls.Add(this.txtDoWPath); this.Controls.Add(this.bttnSave); this.Controls.Add(this.bttnCancel); this.Controls.Add(this.bttnOK); this.Controls.Add(this.bttnPatternPath); this.Controls.Add(this.bttnDoWPath); this.Controls.Add(this.lblPatternPath); this.Controls.Add(this.lblDoWPath); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog; this.MaximizeBox = false; this.MinimizeBox = false; this.Name = "FrmConfig"; this.ShowInTaskbar = false; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent; this.Text = "Configuration"; this.Load += new System.EventHandler(this.FrmConfig_Load); this.ResumeLayout(false); } #endregion private void bttnCancel_Click(object sender, System.EventArgs e) { this.Close(); } private void bttnOK_Click(object sender, System.EventArgs e) { if (setValues()) { this.DialogResult = DialogResult.OK; this.Close(); } } private void bttnPatternPath_Click(object sender, System.EventArgs e) { fbdPath.ShowNewFolderButton = true; fbdPath.SelectedPath = txtPatternPath.Text; DialogResult dr = fbdPath.ShowDialog(); if (dr == DialogResult.OK) { txtPatternPath.Text = fbdPath.SelectedPath.TrimEnd(IBBoard.Constants.DirectoryChar)+IBBoard.Constants.DirectoryChar; } } private void FrmConfig_Load(object sender, System.EventArgs e) { setFormValues(); } private void setFormValues() { txtDoWPath.Text = pref["DoWPath"].ToString(); txtPatternPath.Text = pref["DoWTexturePath"].ToString(); lstMappings.Items.Clear(); mappingsList.Clear(); foreach(FolderToArmyMapping mapping in FolderToArmyMapping.CreateCollectionFromString((string)pref["ArmyMappings"])) { addMappingToList(mapping); } bttnSave.Enabled = false; } private void addMappingToList(FolderToArmyMapping mapping) { mappingsList.Add(mapping); lstMappings.Items.Add(formatMappingName(mapping)); } private string formatMappingName(FolderToArmyMapping mapping) { return mapping.FolderName + " = " + mapping.ArmyName; } private void bttnDoWPath_Click(object sender, System.EventArgs e) { fbdPath.ShowNewFolderButton = false; fbdPath.SelectedPath = txtDoWPath.Text; DialogResult dr = fbdPath.ShowDialog(); if (dr == DialogResult.OK) { txtDoWPath.Text = fbdPath.SelectedPath.TrimEnd(IBBoard.Constants.DirectoryChar)+IBBoard.Constants.DirectoryChar; } } private void bttnSave_Click(object sender, System.EventArgs e) { setValues(); pref.Save(); } private void bttnSaveAs_Click(object sender, System.EventArgs e) { //saveFileDialog.InitialDirectory = pref.PreferencesFolderPath; //DialogResult dr = saveFileDialog.ShowDialog(); //if (dr==DialogResult.OK) //{ // setValues(); // pref.SaveAs(saveFileDialog.FileName); //} } private void bttnLoad_Click(object sender, System.EventArgs e) { //openFileDialog.InitialDirectory = pref.PreferencesFolderPath; //DialogResult dr = openFileDialog.ShowDialog(); //if (dr==DialogResult.OK) //{ // pref.ReloadPreferencesFromFilePath(openFileDialog.FileName); // setFormValues(); //} } private void textbox_TextChanged(object sender, System.EventArgs e) { if (sender is TextBox) { TextBox box = (TextBox)sender; string prefString = pref[box.Tag.ToString()].ToString(); if (Directory.Exists(box.Text)) { if (box.Text != prefString && box.Text+"\\" != prefString) { if (!changed.Contains(box)) { changed.Add(box); } } else if (changed.Contains(box)) { changed.Remove(box); } box.ForeColor = SystemColors.WindowText; } else { changed.Remove(box); box.ForeColor = Color.Red; } setSaveEnabled(); } } private void setSaveEnabled() { bttnSave.Enabled = (changed.Count > 0); } private void textbox_Leave(object sender, System.EventArgs e) { if (sender is TextBox) { TextBox txt = (TextBox)sender; if (txt.Text!="" && !txt.Text.EndsWith("\\")) { txt.Text = txt.Text + "\\"; } } } private void textbox_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { if (sender is TextBox) { if(e.KeyData == (Keys.Control|Keys.A) && e.Control) { ((TextBox)sender).SelectAll(); } } } private bool canFindDoWExeInPath(string path) { path = path + Constants.DirectoryString; string[] exeList = ((string)pref["ExeList"]).Split(','); foreach (string exe in exeList) { if (File.Exists(path+exe)) { return true; } } return false; } private bool setValues() { if (txtDoWPath.Text!="" && (!Directory.Exists(txtDoWPath.Text) || !canFindDoWExeInPath(txtDoWPath.Text))) { MessageBox.Show(this, "Could not find a valid Dawn of War exe in the 'Game Path' specified. Please make sure you specified the correct folder.","Dawn of War Missing", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } try { if (txtPatternPath.Text=="") { if (txtDoWPath.Text!="") { if (!Directory.Exists(txtDoWPath.Text+Constants.DirectoryString+ "Patterns")) { DialogResult dr = MessageBox.Show(this, "Do you want to create a default pattern folder for Dawn of War?", "Pattern Folder Missing", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (dr == DialogResult.Yes) { try { Directory.CreateDirectory(txtDoWPath.Text+Constants.DirectoryString+"Patterns"); txtPatternPath.Text = txtDoWPath.Text+Constants.DirectoryString+"Patterns"; } catch { MessageBox.Show(this, "Pattern Switcher could not create the Dawn of War pattern path and so may not function correctly.","Folder creation failed", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { MessageBox.Show(this, "Pattern Switcher did not create the Dawn of War pattern path and so may not function correctly.","Folder not created", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { txtPatternPath.Text = txtDoWPath.Text+Constants.DirectoryString+"Patterns"; } } else { MessageBox.Show(this, "Pattern Switcher does not have a valid pattern path and so may not function correctly.","Folder not created", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else if (!Directory.Exists(txtPatternPath.Text)) { DialogResult dr = MessageBox.Show(this, "Do you want to create a pattern folder for Dawn of War?", "Pattern Folder Missing", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (dr == DialogResult.Yes) { try { Directory.CreateDirectory(txtPatternPath.Text); } catch { MessageBox.Show(this, "Pattern Switcher could not create the Dawn of War pattern path and so may not function correctly.","Folder creation failed", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { MessageBox.Show(this, "Pattern Switcher did not create the Dawn of War pattern path and so may not function correctly.","Folder not created", MessageBoxButtons.OK, MessageBoxIcon.Error); } } pref["DoWPath"] = txtDoWPath.Text; pref["DoWTexturePath"] = txtPatternPath.Text; pref["ArmyMappings"] = getNewMappingString(); changed.Clear(); setSaveEnabled(); return true; } catch (Exception) { MessageBox.Show(this, "An error occured while saving the preferences. Any changes will not be saved when the program is restarted.", "Preference save failed", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } } private void lstMappings_SelectedIndexChanged(object sender, System.EventArgs e) { bttnRemoveMapping.Enabled = lstMappings.SelectedIndex > -1; bttnEditMapping.Enabled = lstMappings.SelectedIndex > -1; } private string getNewMappingString() { return FolderToArmyMapping.CreateMappingString(getNewMappingListArray()); } private FolderToArmyMapping[] getNewMappingListArray() { return (FolderToArmyMapping[])mappingsList.ToArray(typeof(FolderToArmyMapping)); } private string getMappingString() { return (string)pref["ArmyMappings"]; } private void bttnAddMapping_Click(object sender, System.EventArgs e) { FrmAddMapping addMapping = new FrmAddMapping(getNewMappingListArray()); DialogResult dr = addMapping.ShowDialog(); if (dr==DialogResult.OK) { addMappingToList(addMapping.FolderMapping); setMappingListChanged(); } } private void bttnEditMapping_Click(object sender, System.EventArgs e) { int selected = lstMappings.SelectedIndex; FrmEditMapping editMapping = new FrmEditMapping((FolderToArmyMapping)mappingsList[selected], getNewMappingListArray()); DialogResult dr = editMapping.ShowDialog(); if (dr==DialogResult.OK) { lstMappings.Items.RemoveAt(selected); lstMappings.Items.Insert(selected, formatMappingName(editMapping.FolderMapping)); setMappingListChanged(); } } private void bttnRemoveMapping_Click(object sender, System.EventArgs e) { mappingsList.RemoveAt(lstMappings.SelectedIndex); lstMappings.Items.RemoveAt(lstMappings.SelectedIndex); setMappingListChanged(); } private void setMappingListChanged() { if (!changed.Contains(lstMappings)) { if (!getMappingString().Equals(getNewMappingString())) { changed.Add(lstMappings); } } else { if (getMappingString().Equals(getNewMappingString())) { changed.Remove(lstMappings); } } setSaveEnabled(); } } }