PageRenderTime 151ms CodeModel.GetById 87ms app.highlight 46ms RepoModel.GetById 1ms app.codeStats 1ms

/WorldView/FormWorldView.cs

#
C# | 1998 lines | 1533 code | 397 blank | 68 comment | 278 complexity | 912b23247c741bd790ccebee9cc73e18 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1using System;
   2using System.Collections.Generic;
   3using System.ComponentModel;
   4using System.Drawing;
   5using System.Linq;
   6using System.Windows.Forms;
   7using System.Windows.Forms.VisualStyles;
   8using System.IO;
   9using System.Timers;
  10using MoreTerra.Properties;
  11using MoreTerra.Structures;
  12using MoreTerra.Utilities;
  13using MoreTerra.Structures.TerraInfo;
  14
  15namespace MoreTerra
  16{
  17	public partial class FormWorldView : Form
  18	{
  19		private delegate void PopulateWorldTreeDelegate();
  20		private delegate void PopulateChestTreeDelegate(TreeNode[] node_array);
  21		private delegate DialogResult MessageBoxShowDelegate(String text);
  22		private delegate DialogResult MessageBoxShowFullDelegate(String text, String caption,
  23			MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton);
  24		private delegate DialogResult MessageBoxWithCheckBoxShowDelegate(FormMessageBoxWithCheckBox box);
  25
  26
  27		#region Variable definitions
  28		private WorldMapper mapper = null;
  29		private BackgroundWorker mapperWorker = null;
  30		private System.Timers.Timer tmrMapperProgress;
  31
  32		// Used to store the images for our TreeView marker list.
  33		private ImageList markerImageList;
  34		private ImageList markerThreeStateList;
  35		private ImageList chestImageList;
  36		private ImageList colorImageList;
  37		private ImageList buttonImageList;
  38
  39		private string worldPath = string.Empty;
  40
  41		// Used to store the real names of each .WLD file in the chosen directory.
  42		private Dictionary<String, String> worldNames = new Dictionary<String, String>();
  43
  44		// This stores the nodes for easy lookup.  Only the subnodes are stored.
  45		private Dictionary<String, TreeNode> markerNodes;
  46
  47		// We use this to see if something new got added to the item list when we are done
  48		// drawing/loading information.
  49		private Int32 filterCount;
  50
  51		// We'll use this to refilter the list on the World Information page if we've
  52		// changed what's in the filter since we switched tabs.
  53		private Boolean filterUpdated;
  54
  55		// This is used to hold the data for the color TreeView item.
  56		private ColorListData colorData;
  57
  58		private FormProgressDialog progForm;
  59		#endregion
  60
  61		public FormWorldView()
  62		{
  63			FormWorldView.Form = this;
  64
  65			InitializeComponent();
  66			RegisterEventHandlers();
  67
  68			this.Icon = Properties.Resources.Cannon;
  69		}
  70
  71		// Done here instead of by the automatic code generator as I'm tired of the code
  72		// Generator removing my event handlers when I try to move items around.
  73		private void RegisterEventHandlers()
  74		{
  75			#region Global Handlers
  76			this.Load += new System.EventHandler(this.WorldViewForm_Load);
  77
  78			// Select World groupbox
  79			this.comboBoxWorldFilePath.SelectedIndexChanged += new System.EventHandler(this.comboBoxWorldFilePath_TextChanged);
  80			this.comboBoxWorldFilePath.TextChanged += new System.EventHandler(this.comboBoxWorldFilePath_TextChanged);
  81			this.buttonBrowseWorld.Click += new System.EventHandler(this.buttonBrowseWorld_Click);
  82
  83			// Settings groupbox
  84			this.comboBoxSettings.SelectedIndexChanged += new System.EventHandler(this.comboBoxSettings_SelectedIndexChanged);
  85			this.buttonSettingsAddNew.Click += new System.EventHandler(this.buttonSettingsAddNew_Click);
  86			this.buttonSettingsDelete.Click += new System.EventHandler(this.buttonSettingsDelete_Click);
  87
  88			// Tab Control
  89			this.tabControlSettings.SelectedIndexChanged += new System.EventHandler(this.tabControlSettings_SelectedIndexChanged);
  90			#endregion
  91
  92			#region Draw World tabPage Handlers
  93			// Output Image groupbox
  94			this.buttonBrowseOutput.Click += new System.EventHandler(this.buttonBrowseOutput_Click);
  95
  96            this.checkBoxOfficialColors.CheckedChanged += new System.EventHandler(this.checkBoxOfficialColors_CheckedChanged);
  97			this.checkBoxDrawWires.CheckedChanged += new System.EventHandler(this.checkBoxDrawWires_CheckedChanged);
  98			this.checkBoxDrawWalls.CheckedChanged += new System.EventHandler(this.checkBoxDrawWalls_CheckedChanged);
  99			this.checkBoxOpenImage.CheckedChanged += new System.EventHandler(this.checkBoxOpenImage_CheckedChanged);
 100
 101			this.comboBoxCropImage.SelectedIndexChanged += new System.EventHandler(this.comboBoxCropImage_SelectedIndexChanged);
 102
 103			this.buttonDrawWorld.Click += new System.EventHandler(this.buttonDrawWorld_Click);
 104
 105			// Unsupported World Version groupbox
 106			#endregion
 107
 108			#region Markers tabPage Handlers
 109			this.checkBoxShowChestTypes.CheckedChanged += new System.EventHandler(this.checkBoxShowChestTypes_CheckedChanged);
 110
 111			this.treeViewMarkerList.AfterCheck += new System.Windows.Forms.TreeViewEventHandler(this.treeViewMarkerList_AfterCheck);
 112			this.treeViewMarkerList.NodeMouseClick += new System.Windows.Forms.TreeNodeMouseClickEventHandler(this.treeViewMarkerList_NodeMouseClick);
 113			this.treeViewMarkerList.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.treeViewMarkerList_KeyPress);
 114
 115			// Use Custom Images groupbox
 116			this.checkBoxCustomMarkers.CheckedChanged += new System.EventHandler(this.checkBoxCustomMarkers_CheckedChanged);
 117			this.buttonResetCustomImages.Click += new System.EventHandler(this.buttonResetCustomImages_Click);
 118			this.buttonCustomResources.Click += new System.EventHandler(this.buttonCustomResources_Click);
 119
 120			// ContextMenu for treeViewmarkerList
 121			this.selectAllToolStripMenuItem.Click += new System.EventHandler(this.selectAllToolStripMenuItem_Click);
 122			this.selectNoneToolStripMenuItem.Click += new System.EventHandler(this.selectNoneToolStripMenuItem_Click);
 123			this.invertSelectionToolStripMenuItem.Click += new System.EventHandler(this.invertSelectionToolStripMenuItem_Click);
 124			#endregion
 125
 126			#region Colors tabPage Handlers
 127			this.treeViewColorList.AfterSelect += new TreeViewEventHandler(treeViewColorList_AfterSelect);
 128
 129			this.radioButtonColorDefault.CheckedChanged += new EventHandler(radioButtonColorDefault_CheckedChanged);
 130			this.radioButtonColorPreset.CheckedChanged += new EventHandler(radioButtonColorName_CheckedChanged);
 131			this.radioButtonColorColor.CheckedChanged += new EventHandler(radioButtonColorColor_CheckedChanged);
 132
 133			this.textBoxColorColor.TextChanged += new EventHandler(textBoxColorColor_TextChanged);
 134			this.comboBoxColorName.SelectedIndexChanged += new EventHandler(comboBoxColorName_SelectedIndexChanged);
 135			this.buttonColorColor.Click += buttonColorColor_Click;
 136			#endregion
 137
 138			#region Chest Finder tabPage Handlers
 139			this.checkBoxFilterChests.CheckedChanged += new System.EventHandler(this.checkBoxFilterChests_CheckedChanged);
 140			this.checkBoxShowChestItems.CheckedChanged +=new EventHandler(checkBoxShowChestItems_CheckedChanged);
 141			this.checkBoxShowNormalItems.CheckedChanged +=new EventHandler(checkBoxShowNormalItems_CheckedChanged);
 142
 143			this.lstAvailableItems.DoubleClick += new System.EventHandler(this.lstAvailableItems_DoubleClick);
 144			this.buttonMoveAllToFiltered.Click += new System.EventHandler(this.buttonMoveAllToFiltered_Click);
 145
 146			this.lstFilteredItems.DoubleClick += new System.EventHandler(this.lstFilteredItems_DoubleClick);
 147			this.lstFilteredItems.KeyDown += new System.Windows.Forms.KeyEventHandler(this.lstFilteredItems_KeyDown);
 148			this.buttonMoveAllToAvailable.Click += new System.EventHandler(this.buttonMoveAllToAvailable_Click);
 149			#endregion
 150
 151			#region World Information tabPage Handlers
 152			this.buttonLoadInformation.Click += new System.EventHandler(this.buttonLoadInformation_Click);
 153
 154			// Sort By groupBox
 155			this.radioButtonSortByNone.CheckedChanged += new System.EventHandler(this.radioButtonSortByNone_CheckedChanged);
 156			this.radioButtonSortByX.CheckedChanged += new System.EventHandler(this.radioButtonSortByX_CheckedChanged);
 157			this.radioButtonSortByY.CheckedChanged += new System.EventHandler(this.radioButtonSortByY_CheckedChanged);
 158	
 159			// ContextMenu for Chest list
 160			this.saveToTextToolStripMenuItem.Click += new System.EventHandler(this.saveToTextToolStripMenuItem_Click);
 161			this.saveToCSVToolStripMenuItem.Click += new System.EventHandler(this.saveToCSVToolStripMenuItem_Click);
 162			this.saveToXMLToolStripMenuItem.Click += new System.EventHandler(this.saveToXMLtToolStripMenuItem_Click);
 163			#endregion
 164
 165			#region About tabPage Handlers
 166			this.linkLabelHomepage.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabelHomepage_LinkClicked);
 167			#endregion
 168
 169		}
 170
 171		void buttonColorColor_Click(object sender, EventArgs e)
 172		{
 173			if (colorDialog.ShowDialog() == DialogResult.OK)
 174			{
 175				var rgb = new byte[]{colorDialog.Color.R,colorDialog.Color.G, colorDialog.Color.B};
 176				textBoxColorColor.Text = string.Format("#{0}", BitConverter.ToString(rgb).Replace("-", string.Empty));
 177			}
 178				
 179		}
 180
 181		private void WorldViewForm_Load(object sender, EventArgs e)
 182		{
 183			Int32 i;
 184
 185			for (i = 0; i < SettingsManager.Instance.SettingsCount; i++)
 186			{
 187				comboBoxSettings.Items.Add(SettingsManager.Instance.SettingsName(i));
 188			}
 189
 190			if (SettingsManager.Instance.SettingsName(SettingsManager.Instance.CurrentSettings) == "Default")
 191				buttonSettingsDelete.Enabled = false;
 192			else
 193				buttonSettingsDelete.Enabled = true;
 194
 195			comboBoxSettings.SelectedIndex = SettingsManager.Instance.CurrentSettings;
 196
 197			labelSpecialThanks.Text = Global.Credits;
 198
 199			lblVersion.Text = "Version: " + GetVersion();
 200
 201			tmrMapperProgress = new System.Timers.Timer();
 202			tmrMapperProgress.Elapsed += new ElapsedEventHandler(tmrMapperProgress_Tick);
 203			tmrMapperProgress.Enabled = false;
 204			tmrMapperProgress.Interval = 333;
 205
 206			// If we've updated the software push it into the settings file.
 207			if (SettingsManager.Instance.TopVersion < Global.CurrentVersion)
 208				SettingsManager.Instance.TopVersion = Global.CurrentVersion;
 209
 210			// These two lines are outside of SetupMainForm as they get called by event
 211			// handlers in there, but the handlers do not fire until the everything is fully
 212			// loaded.  So to keep them from getting double called they are here.
 213			ResourceManager.Instance.Custom = SettingsManager.Instance.CustomMarkers;
 214
 215			SetupColorNames();
 216			SetupColorData();
 217			SetupImageLists();
 218			SetupColorButtons();
 219
 220			SetupMainForm();
 221		}
 222
 223		// This is everything that needs to get reset if the current settings have changed.
 224		private void SetupMainForm()
 225		{
 226			// These event handlers do nothing except ironically set SettingsManager back.
 227            checkBoxOfficialColors.Checked = SettingsManager.Instance.OfficialColors;
 228			checkBoxDrawWires.Checked = SettingsManager.Instance.DrawWires;
 229			checkBoxDrawWalls.Checked = SettingsManager.Instance.DrawWalls;
 230			checkBoxOpenImage.Checked = SettingsManager.Instance.OpenImage;
 231			checkBoxShowChestTypes.Checked = SettingsManager.Instance.ShowChestTypes;
 232			checkBoxFilterChests.Checked = SettingsManager.Instance.FilterChests;
 233			checkBoxShowChestItems.Checked = SettingsManager.Instance.ShowChestItems;
 234			checkBoxShowNormalItems.Checked = SettingsManager.Instance.ShowNormalItems;
 235			comboBoxCropImage.SelectedIndex = SettingsManager.Instance.CropImageUsing;
 236
 237			// This event handler sets both ResourceManager.Custom but also calls
 238			// SetupImageLists.  SetupImageLists always needs to be called before
 239			// SetupMarkerList.
 240			checkBoxCustomMarkers.Checked = SettingsManager.Instance.CustomMarkers;
 241
 242			string folder = string.Empty;
 243
 244			if (Directory.Exists(SettingsManager.Instance.InputWorldDirectory))
 245			{
 246				folder = SettingsManager.Instance.InputWorldDirectory;
 247			}
 248			else if (Directory.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "My Games\\Terraria")))
 249			{
 250				folder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "My Games\\Terraria");
 251				SettingsManager.Instance.InputWorldDirectory = folder;
 252			}
 253
 254			if (folder != string.Empty)
 255				this.worldDirectoryChanged();
 256
 257
 258			// SetupImageLists does all boxes so it needs to come before
 259			// the Marker list and the Chest list.
 260			SetupMarkerListBox();
 261			SetupColorListBox();
 262
 263			if (SettingsManager.Instance.FilterItemStates != null)
 264				ResetFilterLists();
 265
 266			switch (SettingsManager.Instance.SortChestsBy)
 267			{
 268				case 1:
 269					radioButtonSortByX.Checked = true;
 270					break;
 271				case 2:
 272					radioButtonSortByY.Checked = true;
 273					break;
 274				default:
 275					radioButtonSortByNone.Checked = true;
 276					break;
 277			}
 278		}
 279
 280
 281		#region SelectWorld groupBox functions
 282		private void buttonBrowseWorld_Click(object sender, EventArgs e)
 283		{
 284			OpenFileDialog dialog = new OpenFileDialog();
 285			dialog.Filter = "Terraria World (*.wld)|*.wld|Terraria Backup World (*.wld.bak)|*.wld.bak";
 286			dialog.Title = "Select World File";
 287			dialog.InitialDirectory = SettingsManager.Instance.InputWorldDirectory;
 288			string filePath = (dialog.ShowDialog() == DialogResult.OK) ? dialog.FileName : string.Empty;
 289
 290			if (filePath == string.Empty) return;
 291
 292			comboBoxWorldFilePath.Text = filePath;
 293			if (!(SettingsManager.Instance.InputWorldDirectory == Path.GetDirectoryName(filePath)))
 294			{
 295				SettingsManager.Instance.InputWorldDirectory = Path.GetDirectoryName(filePath);
 296				worldDirectoryChanged(filePath);
 297			}
 298		}
 299
 300		private void comboBoxWorldFilePath_TextChanged(object sender, EventArgs e)
 301		{
 302			worldPath = comboBoxWorldFilePath.Text;
 303
 304			if (File.Exists(comboBoxWorldFilePath.Text) && Directory.Exists(SettingsManager.Instance.OutputPreviewDirectory))
 305			{
 306				textBoxOutputFile.Text = Path.Combine(SettingsManager.Instance.OutputPreviewDirectory, Path.GetFileNameWithoutExtension(comboBoxWorldFilePath.Text) + ".png");
 307			}
 308
 309			if (worldNames.ContainsKey(worldPath) == true)
 310			{
 311				labelWorldName.Text = "World Name: " + worldNames[worldPath];
 312			}
 313			else
 314			{
 315				labelWorldName.Text = "World Name: ";
 316			}
 317
 318		}
 319
 320		private void worldDirectoryChanged(String selectedFile = null)
 321		{
 322			worldNames.Clear();
 323			Int32 i = 0;
 324			Int32 selection = 0;
 325			World w;
 326
 327			comboBoxWorldFilePath.Items.Clear();
 328
 329			w = new World();
 330
 331			foreach (String file in Directory.GetFiles(SettingsManager.Instance.InputWorldDirectory, "*.wld"))
 332			{
 333				worldNames.Add(file, w.GetWorldName(file));
 334				comboBoxWorldFilePath.Items.Add(file);
 335
 336				if (String.IsNullOrEmpty(selectedFile) != true && file == selectedFile)
 337				{
 338					selection = i;
 339				}
 340				i++;
 341			}
 342
 343			if (comboBoxWorldFilePath.Items.Count > 0)
 344				comboBoxWorldFilePath.SelectedIndex = selection;
 345		}
 346		#endregion
 347
 348		#region Settings groupBox functions
 349		private void buttonSettingsAddNew_Click(object sender, EventArgs e)
 350		{
 351			Boolean mainLoop = true;
 352			DialogResult result;
 353			FormEntryBox nameEntryBox = new FormEntryBox();
 354
 355			nameEntryBox.FormText = "Add New Settings Preset";
 356			nameEntryBox.LabelText = "Enter the a name for a new settings preset:";
 357
 358			while (mainLoop == true)
 359			{
 360				result = nameEntryBox.ShowDialog(this);
 361
 362				if (result == DialogResult.Cancel)
 363					return;
 364				else
 365				{
 366					if (!SettingsManager.Instance.AddNewSettings(nameEntryBox.EntryItem))
 367					{
 368						MessageBox.Show(String.Format("Preset {0} already exists.", nameEntryBox.EntryItem),
 369							"Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
 370					}
 371					else
 372					{
 373						mainLoop = false;
 374					}
 375				}
 376			}
 377
 378			comboBoxSettings.Items.Add(nameEntryBox.EntryItem);
 379		}
 380
 381		private void buttonSettingsDelete_Click(object sender, EventArgs e)
 382		{
 383			Int32 toDelete = comboBoxSettings.SelectedIndex;
 384			String deleteName = SettingsManager.Instance.SettingsName(toDelete);
 385			if ((deleteName == "Default") || (deleteName == String.Empty))
 386				return;
 387
 388			SettingsManager.Instance.DeleteSettings(toDelete);
 389			comboBoxSettings.Items.RemoveAt(toDelete);
 390
 391			if (toDelete >= comboBoxSettings.Items.Count)
 392				toDelete = comboBoxSettings.Items.Count - 1;
 393
 394			comboBoxSettings.SelectedIndex = toDelete;
 395		}
 396
 397		private void comboBoxSettings_SelectedIndexChanged(object sender, EventArgs e)
 398		{
 399			Int32 index = comboBoxSettings.SelectedIndex;
 400
 401			if ((index == SettingsManager.Instance.CurrentSettings) || (index < 0))
 402				return;
 403
 404			if (SettingsManager.Instance.SettingsName(index) == "Default")
 405				buttonSettingsDelete.Enabled = false;
 406			else
 407				buttonSettingsDelete.Enabled = true;
 408
 409			SettingsManager.Instance.CurrentSettings = comboBoxSettings.SelectedIndex;
 410			SetupMainForm();
 411		}
 412		#endregion
 413
 414		#region Draw World tabPage functions
 415        private void checkBoxOfficialColors_CheckedChanged(object sender, EventArgs e)
 416        {
 417            SettingsManager.Instance.OfficialColors = checkBoxOfficialColors.Checked;
 418        }
 419
 420		private void checkBoxDrawWires_CheckedChanged(object sender, EventArgs e)
 421		{
 422			SettingsManager.Instance.DrawWires = checkBoxDrawWires.Checked;
 423		}
 424
 425		private void checkBoxDrawWalls_CheckedChanged(object sender, EventArgs e)
 426		{
 427			SettingsManager.Instance.DrawWalls = checkBoxDrawWalls.Checked;
 428		}
 429
 430		private void checkBoxOpenImage_CheckedChanged(object sender, EventArgs e)
 431		{
 432			SettingsManager.Instance.OpenImage = checkBoxOpenImage.Checked;
 433		}
 434
 435		private void buttonBrowseOutput_Click(object sender, EventArgs e)
 436		{
 437			SaveFileDialog dialog = new SaveFileDialog();
 438			dialog.Filter = "PNG (*.png)|*.png";
 439			dialog.Title = "Select World File";
 440			dialog.InitialDirectory = SettingsManager.Instance.OutputPreviewDirectory;
 441
 442			if (comboBoxWorldFilePath.Text != string.Empty)
 443			{
 444				dialog.FileName = string.Format("{0}.png", System.IO.Path.GetFileNameWithoutExtension(comboBoxWorldFilePath.Text));
 445			}
 446
 447			if (dialog.ShowDialog() != DialogResult.OK) return;
 448
 449			SettingsManager.Instance.OutputPreviewDirectory = Path.GetDirectoryName(dialog.FileName);
 450			textBoxOutputFile.Text = dialog.FileName;
 451
 452		}
 453
 454		private void buttonDrawWorld_Click(object sender, EventArgs e)
 455		{
 456			if (SettingsManager.Instance.FilterChests && !SettingsManager.Instance.DrawMarker("Chest"))
 457			{
 458				DialogResult markers = MessageBox.Show("You have enabled Chest Filtering but have disabled drawing Chest Markers. " +
 459													   "No Chests will show up, even if they contain the items you are looking for.\r\n\r\n" +
 460													   "Would you like to enable Markers for Chests before continuing?", "Chest Markers disabled",
 461													   MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
 462
 463				if (markers == System.Windows.Forms.DialogResult.Yes)
 464				{
 465					SettingsManager.Instance.MarkerVisible("Chest", true);
 466					markerNodes["Chest"].Checked = true;
 467					treeViewMarkerList_updateParentNode(markerNodes["Chest"]);
 468				}
 469				else if (markers == System.Windows.Forms.DialogResult.Cancel)
 470				{
 471					return;
 472				}
 473			}
 474
 475			if (SettingsManager.Instance.FilterChests && lstFilteredItems.Items.Count == 0)
 476			{
 477				DialogResult markers = MessageBox.Show("You have enabled Chest Filtering but have not selected any items. " +
 478													   "No Chests will show up, even if they are empty.\r\n\r\n" +
 479													   "Would you like to disable Chest Filtering before continuing?", "Chest Finder list is empty",
 480													   MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
 481
 482				if (markers == System.Windows.Forms.DialogResult.Yes)
 483				{
 484					SettingsManager.Instance.FilterChests = false;
 485					checkBoxFilterChests.Checked = false;
 486				}
 487				else if (markers == System.Windows.Forms.DialogResult.Cancel)
 488				{
 489					return;
 490				}
 491			}
 492
 493			if (checkValidPaths(true))
 494			{
 495				if (SettingsManager.Instance.ShowChestTypes)
 496					treeViewChestInformation.ImageList = chestImageList;
 497
 498				buttonDrawWorld.Enabled = false;
 499				filterCount = SettingsManager.Instance.FilterItemStates.Count();
 500
 501				groupBoxSelectWorld.Enabled = false;
 502				groupBoxImageOutput.Enabled = false;
 503				(this.tabPageMarkers as Control).Enabled = false;
 504				(this.tabPageWorldInformation as Control).Enabled = false;
 505
 506				Point pt = this.Location;
 507				pt.X += (this.Size.Width / 2);
 508				pt.Y += (this.Size.Height / 2);
 509
 510				mapper = new WorldMapper();
 511				mapper.Initialize();
 512
 513				if (textBoxOutputFile.Text.Substring(textBoxOutputFile.Text.Length - 4).CompareTo(".png") != 0)
 514				{
 515					textBoxOutputFile.Text += ".png";
 516				}
 517
 518				mapperWorker = new BackgroundWorker();
 519				progForm = new FormProgressDialog("Draw World", false, mapperWorker);
 520
 521				mapperWorker.WorkerReportsProgress = true;
 522				mapperWorker.WorkerSupportsCancellation = true;
 523				mapperWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(progForm.worker_Completed);
 524				mapperWorker.ProgressChanged += new ProgressChangedEventHandler(progForm.worker_ProgressChanged);
 525				mapperWorker.DoWork += new DoWorkEventHandler(worker_GenerateMap);
 526				mapperWorker.RunWorkerAsync(true);
 527
 528				progForm.FormClosed += new FormClosedEventHandler(worker_Completed);
 529				progForm.Show(this);
 530			}
 531		}
 532
 533		private void worker_GenerateMap(object sender, DoWorkEventArgs e)
 534		{
 535			BackgroundWorker bw = (BackgroundWorker)sender;
 536
 537			tmrMapperProgress.Start();
 538
 539#if  (!DEBUG)
 540			try
 541			{
 542#endif
 543			mapper.OpenWorld();
 544
 545			//we're drawing a map
 546			if ((bool)e.Argument == true)
 547				mapper.ProcessWorld(worldPath, bw);
 548			else
 549				mapper.ReadChests(worldPath, bw);
 550
 551			TreeNode[] chests = GetChests();
 552
 553			PopulateWorldTree();
 554			PopulateChestTree(chests);
 555
 556			//we're drawing a map
 557			if ((bool)e.Argument == true)
 558			{
 559			//    pictureBox.BackgroundImageLayout = ImageLayout.
 560				 
 561				pictureBox.Image = mapper.CreatePreviewPNG(textBoxOutputFile.Text, bw);
 562				pictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
 563			  //  pictureBox.Update();
 564								   
 565				if (SettingsManager.Instance.OpenImage)
 566					System.Diagnostics.Process.Start(textBoxOutputFile.Text);
 567			}
 568
 569			tmrMapperProgress.Stop();
 570#if  (!DEBUG)
 571			}
 572			catch (Exception ex)
 573			{
 574				tmrMapperProgress.Stop();
 575				FormWorldView.MessageBoxShow(ex.Message + Environment.NewLine + Environment.NewLine +
 576					"Details: " + ex.ToString() + Environment.NewLine + "Version: " + GetVersion(),
 577					"Error Opening World", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
 578
 579
 580				return;
 581			}
 582#endif
 583		}
 584
 585		private void tmrMapperProgress_Tick(object sender, EventArgs e)
 586		{
 587			mapperWorker.ReportProgress(mapper.progress);
 588		}
 589
 590		private void worker_Completed(object sender, FormClosedEventArgs e)
 591		{
 592			Boolean v = progForm.Success;
 593
 594			mapper.Cleanup();
 595			GC.Collect();
 596
 597			if (filterCount != SettingsManager.Instance.FilterItemStates.Count())
 598				ResetFilterLists();
 599
 600
 601			buttonDrawWorld.Enabled = true;
 602
 603			groupBoxSelectWorld.Enabled = true;
 604			groupBoxImageOutput.Enabled = true;
 605			(this.tabPageMarkers as Control).Enabled = true;
 606			(this.tabPageWorldInformation as Control).Enabled = true;
 607		}
 608
 609		private void PopulateWorldTree()
 610		{
 611			if (worldPropertyGrid.InvokeRequired)
 612			{
 613				PopulateWorldTreeDelegate del = new PopulateWorldTreeDelegate(PopulateWorldTree);
 614				worldPropertyGrid.Invoke(del);
 615				return;
 616			}
 617
 618			worldPropertyGrid.SelectedObject = mapper.World.Header;
 619		}
 620
 621		private TreeNode[] GetChests()
 622		{
 623			List<Chest> chests;
 624			List<TreeNode> chestNodes;
 625
 626			chestNodes = new List<TreeNode>();
 627
 628			if (this.mapper == null)
 629				return chestNodes.ToArray();
 630
 631			if (SettingsManager.Instance.FilterChests == true)
 632			{
 633				chests = applyChestFilter(this.mapper.Chests);
 634			}
 635			else
 636			{
 637				chests = this.mapper.Chests;
 638			}
 639
 640//			chestNodes = new List<TreeNode>(chests.Count);
 641
 642			foreach (Chest c in chests)
 643			{
 644				TreeNode node = new TreeNode(string.Format("Chest at ({0},{1})", c.Coordinates.X, c.Coordinates.Y, c.ChestId));
 645				node.ImageIndex = (Int32) c.Type + 1;
 646				node.SelectedImageIndex = (Int32)c.Type + 1;
 647
 648				foreach (Item i in c.Items)
 649				{
 650					node.Nodes.Add(i.ToString());
 651				}
 652				chestNodes.Add(node);
 653			}
 654
 655			return chestNodes.ToArray();
 656		}
 657
 658		private void PopulateChestTree(TreeNode[] node_array)
 659		{
 660			if (treeViewChestInformation.InvokeRequired)
 661			{
 662				PopulateChestTreeDelegate del = new PopulateChestTreeDelegate(PopulateChestTree);
 663				treeViewChestInformation.Invoke(del, new object[] { node_array });
 664				return;
 665			}
 666
 667			treeViewChestInformation.SuspendLayout();
 668			treeViewChestInformation.Nodes.Clear();
 669
 670			//nodes have to be added in this fairly awkward way, because this
 671			//fixes a bug with the TreeView control that will cause the last node
 672			//to have its lower half cut off at the bottom of the control,
 673			//and it won't allow the user to scroll further down...
 674
 675			treeViewChestInformation.Nodes.AddRange(node_array);
 676
 677			treeViewChestInformation.ResumeLayout(true);
 678		}
 679
 680		private bool checkValidPaths(bool checkOutput)
 681		{
 682			if (comboBoxWorldFilePath.Text == string.Empty || !File.Exists(comboBoxWorldFilePath.Text))
 683			{
 684				MessageBox.Show("The World file could not be found.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
 685				return false;
 686			}
 687			else if (checkOutput && textBoxOutputFile.Text == string.Empty)
 688			{
 689				MessageBox.Show("Please enter desired output image path.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
 690				return false;
 691			}
 692			else
 693			{
 694				return true;
 695			}
 696		}
 697		#endregion
 698
 699		#region Marker tabPage functions
 700		// This handles pressing space to check/uncheck boxes.
 701		private void treeViewMarkerList_KeyPress(object sender, KeyPressEventArgs e)
 702		{
 703			if (e.KeyChar != ' ')
 704				return;
 705
 706			if (treeViewMarkerList.SelectedNode != null)
 707			{
 708				treeViewMarkerList.SelectedNode.Checked = !treeViewMarkerList.SelectedNode.Checked;
 709				this.treeViewMarkerList_AfterCheck(this.treeViewMarkerList,
 710					new TreeViewEventArgs(treeViewMarkerList.SelectedNode, TreeViewAction.ByMouse));
 711			}
 712
 713		}
 714
 715		// This handles clicking on the check box part of the nodes.
 716		private void treeViewMarkerList_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
 717		{
 718			Int32 xSpot = e.X - e.Node.Bounds.Left;
 719
 720			if (treeViewMarkerList.ImageList != null)
 721			{
 722				if (xSpot < -34 || xSpot > -19)
 723					return;
 724			}
 725			else
 726			{
 727				if (xSpot < -16 || xSpot > -1)
 728					return;
 729			}
 730
 731			e.Node.Checked = !e.Node.Checked;
 732
 733			this.treeViewMarkerList_AfterCheck(this.treeViewMarkerList,
 734				new TreeViewEventArgs(e.Node, TreeViewAction.ByMouse));
 735		}
 736
 737		// This takes a node that was clicked and goes down the list to update the parent to
 738		// it's correct state.
 739		private void treeViewMarkerList_updateParentNode(TreeNode e)
 740		{
 741			Int32 count, set;
 742
 743			count = 0;
 744			set = 0;
 745			foreach (TreeNode n in e.Parent.Nodes)
 746			{
 747				count++;
 748				if (n.Checked == true)
 749					set++;
 750			}
 751
 752			if (set == count)
 753				e.Parent.Checked = true;
 754			else
 755				e.Parent.Checked = false;
 756
 757			if (set == 0)
 758				e.Parent.StateImageIndex = (Int32)CheckState.Unchecked;
 759			else if (set != count)
 760				e.Parent.StateImageIndex = (Int32)CheckState.Indeterminate;
 761			else
 762				e.Parent.StateImageIndex = (Int32)CheckState.Checked;
 763
 764		}
 765
 766		// This handles clicking a checkbox and correctly sets the child nodes, if there are any.
 767		private void treeViewMarkerList_AfterCheck(object sender, TreeViewEventArgs e)
 768		{
 769			// Don't do anything unless it was the user who set the checked state.
 770			if (e.Action == TreeViewAction.Unknown)
 771				return;
 772
 773			if (markerNodes.ContainsKey(e.Node.Text))
 774			{
 775				SettingsManager.Instance.MarkerVisible(e.Node.Text, e.Node.Checked);
 776				treeViewMarkerList_updateParentNode(e.Node);
 777			}
 778			else
 779			{
 780				foreach (TreeNode n in e.Node.Nodes)
 781				{
 782					n.Checked = e.Node.Checked;
 783					SettingsManager.Instance.MarkerVisible(n.Text, e.Node.Checked);
 784				}
 785			}
 786		}
 787
 788		private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
 789		{
 790			if (contextMenuStripListOperations.SourceControl is TreeView)
 791			{
 792				foreach (TreeNode p in (contextMenuStripListOperations.SourceControl as TreeView).Nodes)
 793				{
 794					p.Checked = true;
 795
 796					foreach (TreeNode n in p.Nodes)
 797					{
 798						n.Checked = true;
 799						SettingsManager.Instance.MarkerVisible(n.Text, true);
 800					}
 801				}
 802			}
 803		}
 804
 805		private void selectNoneToolStripMenuItem_Click(object sender, EventArgs e)
 806		{
 807			if (contextMenuStripListOperations.SourceControl is TreeView)
 808			{
 809				foreach (TreeNode p in (contextMenuStripListOperations.SourceControl as TreeView).Nodes)
 810				{
 811					p.Checked = false;
 812
 813					foreach (TreeNode n in p.Nodes)
 814					{
 815						n.Checked = false;
 816						SettingsManager.Instance.MarkerVisible(n.Text, false);
 817					}
 818				}
 819			}
 820		}
 821
 822		private void invertSelectionToolStripMenuItem_Click(object sender, EventArgs e)
 823		{
 824			Boolean nodeFlip;
 825			Int32 count, set;
 826
 827			if (contextMenuStripListOperations.SourceControl is TreeView)
 828			{
 829				foreach (TreeNode p in (contextMenuStripListOperations.SourceControl as TreeView).Nodes)
 830				{
 831					count = 0;
 832					set = 0;
 833					foreach (TreeNode n in p.Nodes)
 834					{
 835						count++;
 836						nodeFlip = !n.Checked;
 837						SettingsManager.Instance.MarkerVisible(n.Text, nodeFlip);
 838						n.Checked = nodeFlip;
 839
 840						if (nodeFlip == true)
 841							set++;
 842					}
 843
 844					if (set == count)
 845						p.Checked = true;
 846					else
 847						p.Checked = false;
 848
 849					if (set == 0)
 850						p.StateImageIndex = (Int32)CheckState.Unchecked;
 851					else if (set != count)
 852						p.StateImageIndex = (Int32)CheckState.Indeterminate;
 853					else
 854						p.StateImageIndex = (Int32)CheckState.Checked;
 855				}
 856			}
 857		}
 858
 859		private void buttonCustomResources_Click(object sender, EventArgs e)
 860		{
 861			System.Diagnostics.Process.Start(Global.ApplicationResourceDirectory);
 862		}
 863
 864		private void checkBoxShowChestTypes_CheckedChanged(object sender, EventArgs e)
 865		{
 866			SettingsManager.Instance.ShowChestTypes = checkBoxShowChestTypes.Checked;
 867		}
 868
 869		private void checkBoxCustomMarkers_CheckedChanged(object sender, EventArgs e)
 870		{
 871			SettingsManager.Instance.CustomMarkers = checkBoxCustomMarkers.Checked;
 872			ResourceManager.Instance.Custom = checkBoxCustomMarkers.Checked;
 873			SetupImageLists();
 874		}
 875
 876		private void buttonResetCustomImages_Click(object sender, EventArgs e)
 877		{
 878			DialogResult res = MessageBox.Show("This will overwrite all files in the Resources directory.  If you have made changes to them they will be lost.  Continue?",
 879				"Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
 880
 881			if (res == DialogResult.No)
 882				return;
 883
 884			ResourceManager.Instance.ResetCustomMarkers();
 885			
 886			if (SettingsManager.Instance.CustomMarkers)
 887				SetupImageLists();
 888		}
 889
 890		private void SetupImageLists()
 891		{
 892			Graphics graph;
 893			Bitmap bmp;
 894			if (markerThreeStateList == null)
 895			{
 896				markerThreeStateList = new ImageList();
 897
 898				// Set up the images for the three-state box.
 899				bmp = new Bitmap(16, 16);
 900				graph = Graphics.FromImage(bmp);
 901				CheckBoxRenderer.DrawCheckBox(graph, new Point(1, 1), CheckBoxState.UncheckedNormal);
 902				markerThreeStateList.Images.Add(bmp);
 903
 904				bmp = new Bitmap(16, 16);
 905				graph = Graphics.FromImage(bmp);
 906				CheckBoxRenderer.DrawCheckBox(graph, new Point(1, 1), CheckBoxState.CheckedNormal);
 907				markerThreeStateList.Images.Add(bmp);
 908
 909				bmp = new Bitmap(16, 16);
 910				graph = Graphics.FromImage(bmp);
 911				CheckBoxRenderer.DrawCheckBox(graph, new Point(1, 1), CheckBoxState.MixedNormal);
 912				markerThreeStateList.Images.Add(bmp);
 913			}
 914
 915			if (chestImageList == null)
 916				chestImageList = new ImageList();
 917
 918			if (markerImageList == null)
 919				markerImageList = new ImageList();
 920
 921			if (colorImageList == null)
 922			{
 923				colorImageList = new ImageList();
 924
 925				// This is the empty one we'll use for parent objects.
 926				colorImageList.Images.Add(new Bitmap(16, 16));
 927
 928				bmp = new Bitmap(16, 16);
 929				graph = Graphics.FromImage(bmp);
 930
 931				graph.DrawRectangle(new Pen(Color.Black, 1), 1, 1, 14, 14);
 932
 933				// We filled the image list with as many copies of the empty square bitmap as
 934				// we needed.
 935				for (Int32 i = 0; i < colorData.Count; i++)
 936				{
 937					colorImageList.Images.Add(new Bitmap(bmp));
 938				}
 939			}
 940
 941			if (buttonImageList == null)
 942			{
 943				buttonImageList = new ImageList();
 944				buttonImageList.ImageSize = new Size(51, 23);
 945
 946				bmp = new Bitmap(50, 22);
 947
 948				buttonImageList.Images.Add(bmp);
 949				buttonImageList.Images.Add(new Bitmap(bmp));
 950			}
 951
 952			markerImageList.Images.Clear();
 953			chestImageList.Images.Clear();
 954
 955			// This is our blank image to keep chest list items from having images.
 956			bmp = new Bitmap(16, 16);
 957			chestImageList.Images.Add(bmp);
 958
 959			foreach (KeyValuePair<String, List<MarkerInfo>> kvp in Global.Instance.Info.MarkerSets)
 960			{
 961                bmp = ResourceManager.Instance.GetMarker(kvp.Key);
 962                markerImageList.Images.Add(bmp);
 963
 964                foreach (MarkerInfo mi in kvp.Value)
 965                {
 966                    bmp = ResourceManager.Instance.GetMarker(mi.markerImage);
 967
 968				markerImageList.Images.Add(bmp);
 969
 970                    if (kvp.Key == "Containers")
 971					chestImageList.Images.Add(bmp);
 972			}
 973		}
 974		}
 975
 976		private void SetupMarkerListBox()
 977		{
 978			Dictionary<String, MarkerSettings> markerStates = SettingsManager.Instance.MarkerStates;
 979			Boolean[] expList = null;
 980
 981			// This section sets up the Marker list box.  The first part draws the three
 982			// states of our checkboxes and then it populates the TreeView.
 983			Int32 index = 0;
 984			TreeNode node;
 985
 986			if (treeViewMarkerList.Nodes.Count > 0)
 987			{
 988				expList = new Boolean[treeViewMarkerList.Nodes.Count];
 989				Int32 count = 0;
 990
 991				foreach (TreeNode n in treeViewMarkerList.Nodes)
 992				{
 993					if (n.Parent == null)
 994						expList[count++] = n.IsExpanded;
 995				}
 996			}
 997
 998			treeViewMarkerList.Nodes.Clear();
 999
1000			treeViewMarkerList.StateImageList = markerThreeStateList;
1001			treeViewMarkerList.ImageList = markerImageList;
1002
1003			markerNodes = new Dictionary<String, TreeNode>();
1004
1005			Dictionary<String, TreeNode> parentNodes = new Dictionary<String, TreeNode>();
1006
1007			foreach (KeyValuePair<String, List<MarkerInfo>> kvp in Global.Instance.Info.MarkerSets)
1008			{
1009				node = new TreeNode(kvp.Key);
1010				node.ImageIndex = index;
1011				node.SelectedImageIndex = index;
1012
1013					parentNodes.Add(kvp.Key, node);
1014
1015                foreach (MarkerInfo mi in kvp.Value)
1016				{
1017                    index++;
1018                    node = new TreeNode(mi.name);
1019                    node.ImageIndex = index;
1020                    node.SelectedImageIndex = index;
1021                    markerNodes.Add(mi.name, node);
1022
1023					if (markerStates[mi.name].Drawing)
1024					{
1025						node.Checked = true;
1026						node.StateImageIndex = (Int32)CheckState.Checked;
1027					}
1028					else
1029					{
1030						node.Checked = false;
1031						node.StateImageIndex = (Int32)CheckState.Unchecked;
1032					}
1033				}
1034
1035				index++;
1036			}
1037
1038			// We parse the list again, this time to set the parent/child heirarchy up.
1039			// This makes it so that we do not have to have them in the exact order in the XML file.
1040			foreach(KeyValuePair<Int32, MarkerInfo> kvp in Global.Instance.Info.Markers)
1041			{
1042				parentNodes[kvp.Value.markerSet].Nodes.Add(markerNodes[kvp.Value.name]);
1043			}
1044
1045			// Now that it is set up so all child nodes are in the right spot we add them to the
1046			// treeNode and force it to update the checked state.
1047			foreach(KeyValuePair<String, TreeNode> kvp in parentNodes)
1048			{
1049				treeViewMarkerList.Nodes.Add(kvp.Value);
1050				treeViewMarkerList_updateParentNode(kvp.Value.Nodes[0]);
1051			}
1052
1053			if (expList != null)
1054			{
1055				Int32 count = 0;
1056
1057				foreach (TreeNode n in treeViewMarkerList.Nodes)
1058				{
1059					if ((n.Parent == null) && (expList[count++] == true))
1060						n.Expand();
1061				}
1062			}
1063
1064
1065
1066		}
1067		#endregion
1068
1069		#region Color tabPage functions
1070		private void treeViewColorList_AfterSelect(object sender, TreeViewEventArgs e)
1071		{
1072			Global.Instance.SkipEvents = true;
1073
1074			if ((e.Node == null) || (e.Node.Parent == null))
1075			{
1076				radioButtonColorDefault.Checked = true;
1077				comboBoxColorName.SelectedIndex = -1;
1078				textBoxColorColor.Text = String.Empty;
1079				groupBoxColor.Enabled = false;
1080				Global.Instance.SkipEvents = false;
1081				return;
1082			}
1083
1084			groupBoxColor.Enabled = true;
1085
1086			ColorListDataNode data = colorData.GetNode(e.Node.Text, e.Node.Parent.Text);
1087			if (data == null)
1088			{
1089				Global.Instance.SkipEvents = false;
1090				return;
1091			}
1092
1093			if (data.defaultColor == data.currentColor)
1094				radioButtonColorDefault.Checked = true;
1095			else if (data.currentColorName != String.Empty)
1096				radioButtonColorPreset.Checked = true;
1097			else
1098				radioButtonColorColor.Checked = true;
1099
1100			if (data.currentColorName == String.Empty)
1101				comboBoxColorName.SelectedIndex = -1;
1102			else
1103			{
1104				if (comboBoxColorName.Items.Contains(data.currentColorName))
1105					comboBoxColorName.SelectedItem = data.currentColorName;
1106				else
1107					comboBoxColorName.SelectedIndex = -1;
1108			}
1109
1110			textBoxColorColor.Text = Global.ToColorString(data.currentColor);
1111			Global.Instance.SkipEvents = false;
1112		}
1113
1114		private void radioButtonColorDefault_CheckedChanged(object sender, EventArgs e)
1115		{
1116			if (Global.Instance.SkipEvents)
1117				return;
1118
1119			//MessageBox.Show("radioButtonColorDefault_CheckedChanged");
1120		}
1121
1122		private void radioButtonColorName_CheckedChanged(object sender, EventArgs e)
1123		{
1124			if (Global.Instance.SkipEvents)
1125				return;
1126
1127			//MessageBox.Show("radioButtonColorName_CheckedChanged");
1128		}
1129
1130		private void radioButtonColorColor_CheckedChanged(object sender, EventArgs e)
1131		{
1132			if (Global.Instance.SkipEvents)
1133				return;
1134
1135			//MessageBox.Show("radioButtonColorColor_CheckedChanged");
1136		}
1137
1138		private void comboBoxColorName_SelectedIndexChanged(object sender, EventArgs e)
1139		{
1140			if (Global.Instance.SkipEvents)
1141				return;
1142
1143			MessageBox.Show("comboBoxColorName_SelectedIndexChanged");
1144		}
1145
1146		private void textBoxColorColor_TextChanged(object sender, EventArgs e)
1147		{
1148			if (Global.Instance.SkipEvents)
1149				return;
1150            
1151		}
1152
1153		private void SetupColorButtons()
1154		{
1155			buttonColorColor.ImageList = buttonImageList;
1156			buttonColorColor.ImageIndex = 0;
1157
1158			buttonColorNameColor.ImageList = buttonImageList;
1159			buttonColorNameColor.ImageIndex = 1;
1160		}
1161
1162		private void SetupColorNames()
1163		{
1164			Dictionary<String, ColorInfo> colors = Global.Instance.Info.Colors;
1165
1166			comboBoxColorName.Items.Clear();
1167			comboBoxColorNamesName.Items.Clear();
1168
1169			foreach (KeyValuePair<String, ColorInfo> kvp in colors)
1170			{
1171				comboBoxColorName.Items.Add(kvp.Key);
1172				comboBoxColorNamesName.Items.Add(kvp.Key);
1173			}
1174
1175			comboBoxColorNamesName.SelectedIndex = 0;
1176		}
1177
1178		private void SetupColorData()
1179		{
1180			treeViewColorList.Nodes.Clear();
1181			treeViewColorList.ImageList = colorImageList;
1182
1183			if (colorData == null)
1184				colorData = new ColorListData();
1185			else
1186				colorData.Clear();
1187
1188			String parent = "Tiles";
1189			Int32 id = 1;
1190
1191			foreach (KeyValuePair<Int32, TileInfo> kvp in Global.Instance.Info.Tiles)
1192			{
1193				colorData.AddNewNode(kvp.Value.name, parent, kvp.Value.color,
1194					kvp.Value.colorName, id + kvp.Value.tileImage);
1195			}
1196
1197			id += Global.Instance.Info.Tiles.Count - 1;
1198
1199			parent = "Walls";
1200
1201			foreach (KeyValuePair<Int32, WallInfo> kvp in Global.Instance.Info.Walls)
1202			{
1203				colorData.AddNewNode(kvp.Value.name, parent, kvp.Value.color,
1204					kvp.Value.colorName, id + kvp.Value.wallImage);
1205			}
1206
1207			id += Global.Instance.Info.Walls.Count + 1;
1208
1209			foreach (KeyValuePair<String, List<SpecialObjectInfo>> kvp in Global.Instance.Info.SpecialObjects)
1210			{
1211				parent = kvp.Key + "s";
1212
1213				foreach (SpecialObjectInfo soi in kvp.Value)
1214				{
1215					colorData.AddNewNode(soi.name, parent, soi.color,
1216						soi.colorName, id);
1217
1218					id++;
1219				}
1220			}
1221		}
1222
1223		private void SetupColorListBox()
1224		{
1225			Boolean[] expList = null;
1226			TreeNode node, parent;
1227
1228			if (treeViewColorList.Nodes.Count > 0)
1229			{
1230				expList = new Boolean[treeViewColorList.Nodes.Count];
1231				Int32 count = 0;
1232
1233				foreach (TreeNode n in treeViewColorList.Nodes)
1234				{
1235					if (n.Parent == null)
1236						expList[count++] = n.IsExpanded;
1237				}
1238			}
1239
1240			treeViewColorList.Nodes.Clear();
1241			treeViewColorList.ImageList = colorImageList;
1242
1243			foreach (KeyValuePair<String, Dictionary<String, ColorListDataNode>> kvp in colorData.Data)
1244			{
1245				parent = new TreeNode(kvp.Key);
1246				parent.ImageIndex = -1;
1247				parent.SelectedImageIndex = -1;
1248				treeViewColorList.Nodes.Add(parent);
1249
1250				foreach (KeyValuePair<String, ColorListDataNode> kvp2 in kvp.Value)
1251				{
1252					node = new TreeNode(kvp2.Key);
1253					node.SelectedImageIndex = kvp2.Value.nodeId;
1254					node.ImageIndex = node.SelectedImageIndex;
1255
1256					UpdateColorImage(node.ImageIndex, kvp2.Value.currentColor);
1257
1258					parent.Nodes.Add(node);
1259				}
1260			}
1261
1262			if (expList != null)
1263			{
1264				Int32 count = 0;
1265
1266				foreach (TreeNode n in treeViewColorList.Nodes)
1267				{
1268					if ((n.Parent == null) && (expList[count++] == true))
1269						n.Expand();
1270				}
1271			}
1272		}
1273
1274		private void UpdateColorImage(Int32 useImage, Color newColor)
1275		{
1276			if ((useImage < 0) || (useImage >= colorImageList.Images.Count))
1277				return;
1278
1279			Image image = colorImageList.Images[useImage];
1280			Graphics graph = Graphics.FromImage(image);
1281
1282			graph.FillRectangle(new SolidBrush(newColor), 2, 2, 13, 13);
1283
1284			colorImageList.Images[useImage] = image;
1285		}
1286
1287		private void UpdateColorButton(Int32 useButton, Color newColor)
1288		{
1289			if ((useButton < 0) || (useButton >= buttonImageList.Images.Count))
1290				return;
1291
1292			Image image = buttonImageList.Images[useButton];
1293			Graphics graph = Graphics.FromImage(image);
1294
1295			graph.FillRectangle(new SolidBrush(newColor), 0, 0, image.Width - 1, image.Height - 1);
1296			buttonImageList.Images[useButton] = image;
1297		}
1298		#endregion
1299
1300		#region ChestFinder tabPage functions
1301		private void checkBoxFilterChests_CheckedChanged(object sender, EventArgs e)
1302		{
1303			SettingsManager.Instance.FilterChests = checkBoxFilterChests.Checked;
1304
1305			lstAvailableItems.Enabled = checkBoxFilterChests.Checked;
1306			lstFilteredItems.Enabled = checkBoxFilterChests.Checked;
1307			filterUpdated = true;
1308		}
1309
1310		private void checkBoxShowChestItems_CheckedChanged(object sender, EventArgs e)
1311		{
1312			SettingsManager.Instance.ShowChestItems = checkBoxShowChestItems.Checked;
1313
1314			ResetFilterLists();
1315		}
1316
1317		private void checkBoxShowNormalItems_CheckedChanged(object sender, EventArgs e)
1318		{
1319			SettingsManager.Instance.ShowNormalItems = checkBoxShowNormalItems.Checked;
1320
1321			ResetFilterLists();
1322		}
1323
1324		private void lstFilteredItems_DoubleClick(object sender, EventArgs e)
1325		{
1326			lstFilteredItems_KeyDown(sender, new KeyEventArgs(Keys.Delete));
1327		}
1328
1329		private void lstAvailableItems_DoubleClick(object sender, EventArgs e)
1330		{
1331			String si;
1332			int selected = lstAvailableItems.SelectedIndex;
1333			
1334			if (selected == -1) return;
1335
1336			si = lstAvailableItems.SelectedItem.ToString();
1337
1338			lstFilteredItems.Items.Add(si);
1339
1340			lstAvailableItems.Items.Remove(si);
1341			lstAvailableItems.SelectedIndex = Math.Min(selected, lstAvailableItems.Items.Count - 1);
1342
1343			SettingsManager.Instance.FilterItem(si, true);
1344			filterUpdated = true;
1345
1346			buttonMoveAllToAvailable.Enabled = (lstFilteredItems.Items.Count != 0);
1347			buttonMoveAllToFiltered.Enabled = (lstAvailableItems.Items.Count != 0);
1348		}
1349	
1350		private void lstFilteredItems_KeyDown(object sender, KeyEventArgs e)
1351		{
1352			ItemInfo ii;
1353			String si;
1354			int selected = lstFilteredItems.SelectedIndex;
1355
1356			if (selected == -1) return;
1357
1358			if (e.KeyCode == Keys.Delete)
1359			{
1360				si = lstFilteredItems.SelectedItem.ToString();
1361
1362				ii = Global.Instance.Info.GetItem(si);
1363
1364				if (ii.foundIn == "Chest")
1365				{
1366					if (SettingsManager.Instance.ShowChestItems)
1367						lstAvailableItems.Items.Add(si);
1368				} else if (SettingsManager.Instance.ShowNormalItems)
1369					lstAvailableItems.Items.Add(si);
1370
1371				lstFilteredItems.Items.Remove(lstFilteredItems.SelectedItem);
1372				lstFilteredItems.SelectedIndex = Math.Min(selected, lstFilteredItems.Items.Count - 1);
1373
1374				SettingsManager.Instance.FilterItem(si, false);
1375				filterUpdated = true;
1376
1377				buttonMoveAllToAvailable.Enabled = (lstFilteredItems.Items.Count != 0);
1378				buttonMoveAllToFiltered.Enabled = (lstAvailableItems.Items.Count != 0);
1379			}
1380		}
1381
1382		private void buttonMoveAllToFiltered_Click(object sender, EventArgs e)
1383		{
1384			List<String> filter;
1385
1386			if (lstAvailableItems.Items.Count == 0)
1387				return;
1388
1389			filter = SettingsManager.Instance.FilterItemStates;
1390
1391			foreach (String s in lstAvailableItems.Items)
1392			{
1393				filter.Add(s);
1394			}
1395
1396			ResetFilterLists();
1397		}
1398
1399		private void buttonMoveAllToAvailable_Click(object sender, EventArgs e)
1400		{
1401			if (lstFilteredItems.Items.Count == 0)
1402				return;
1403
1404			SettingsManager.Instance.FilterItemStates.Clear();
1405
1406			ResetFilterLists();
1407		}
1408
1409		private void ResetFilterLists()
1410		{
1411			String lstItem;
1412			List<String> filterList;
1413
1414			Boolean showInChest = SettingsManager.Instance.ShowChestItems;
1415			Boolean showNormal = SettingsManager.Instance.ShowNormalItems;
1416
1417			lstFilteredItems.Items.Clear();
1418			lstAvailableItems.Items.Clear();
1419
1420			filterList = SettingsManager.Instance.FilterItemStates;
1421
1422			foreach (KeyValuePair<String, ItemInfo> kvp in Global.Instance.Info.Items)
1423			{
1424				lstItem = kvp.Key;
1425
1426				if (filterList.Contains(kvp.Key))
1427				{
1428					lstFilteredItems.Items.Add(kvp.Key);
1429				}
1430				else
1431				{
1432					if (kvp.Value.foundIn == "Chest")
1433					{
1434						if (showInChest == false)
1435							continue;
1436					}
1437					else if (showNormal == false)
1438					{
1439						continue;
1440					}
1441
1442					lstAvailableItems.Items.Add(lstItem);
1443				}
1444			}
1445
1446			buttonMoveAllToAvailable.Enabled = (lstFilteredItems.Items.Count != 0);
1447			buttonMoveAllToFiltered.Enabled = (lstAvailableItems.Items.Count != 0);
1448
1449			filterUpdated = true;
1450		}
1451		#endregion
1452
1453		#region World Information tabPage functions
1454		private void buttonLoadInformation_Click(object sender, EventArgs e)
1455		{
1456			if (checkValidPaths(false))
1457			{
1458				treeViewChestInformation.ImageList = null;
1459
1460				filterCount = SettingsManager.Instance.FilterItemStates.Count;
1461
1462				buttonDrawWorld.Enabled = false;
1463
1464				groupBoxSelectWorld.Enabled = false;
1465				groupBoxImageOutput.Enabled = false;
1466				(this.tabPageMarkers as Control).Enabled = false;
1467				(this.tabPageWorldInformation as Control).Enabled = false;
1468
1469				Point pt = this.Location;
1470				pt.X += (this.Size.Width / 2);
1471				pt.Y += (this.Size.Height / 2);
1472
1473				mapper = new WorldMapper();
1474				mapper.Initialize();
1475
1476				mapperWorker = new BackgroundWorker();
1477				progForm = new FormProgressDialog("Load World Information", false, mapperWorker);
1478
1479				mapperWorker.WorkerReportsProgress = true;
1480				mapperWorker.WorkerSupportsCancellation = false;
1481				mapperWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(progForm.worker_Completed);
1482				mapperWorker.ProgressChanged += new ProgressChangedEventHandler(progForm.worker_ProgressChanged);
1483				mapperWorker.DoWork += new DoWorkEventHandler(worker_GenerateMap);
1484				mapperWorker.RunWorkerAsync(false);
1485
1486				progForm.FormClosed += new FormClosedEventHandler(worker_Completed);
1487				progForm.Show(this);
1488			}
1489		}
1490
1491		private void radioButtonSortByNone_CheckedChanged(object sender, EventArgs e)
1492		{
1493			SettingsManager.Instance.SortChestsBy = 0;
1494
1495			treeViewChestInformation.TreeViewNodeSorter = null;
1496			treeViewChestInformation.Sorted = false;
1497
1498			PopulateChestTree(GetChests());
1499		}
1500
1501		private void radioButtonSortByX_CheckedChanged(object sender, EventArgs e)
1502		{
1503			SettingsManager.Instance.SortChestsBy = 1;
1504
1505			treeViewChestInformation.TreeViewNodeSorter = new ChestComparerX();
1506		}
1507
1508		private void radioButtonSortByY_CheckedChanged(object sender, EventArgs e)
1509		{
1510			SettingsManager.Instance.SortChestsBy = 2;
1511
1512			treeViewChestInformation.TreeViewNodeSorter = new ChestComparerY();
1513		}
1514
1515		private List<Chest> applyChestFilter(List<Chest> chests)
1516		{
1517			List<String> itemFilters = SettingsManager.Instance.FilterItemStates;
1518			List<Chest> filteredChests = new List<Chest>();
1519
1520			foreach(Chest c in chests)
1521			{
1522				// If we are checking the chest node itself.
1523				foreach (Item item in c.Items)
1524				{
1525					if (itemFilters.Contains(item.Name))
1526					{
1527						filteredChests.Add(c);
1528						break;
1529					}
1530				}
1531			}
1532
1533			return filteredChests;
1534		}
1535
1536		private void tabControlSettings_SelectedIndexChanged(object sender, EventArgs e)
1537		{
1538			if (tabControlSettings.SelectedTab.Text == "World Information")
1539			{
1540				if (this.mapper == null)
1541					return;
1542
1543				if (filterUpdated == true)
1544				{
1545					PopulateChestTree(GetChests());
1546					filterUpdated = false;
1547				}
1548			}
1549		}
1550
1551		private void saveToTextToolStripMenuItem_Click(object sender, EventArgs e)
1552		{
1553			SaveFileDialog dialog = new SaveFileDialog();
1554			DialogResult result;
1555
1556			if (treeViewChestInformation.Nodes.Count == 0)
1557			{
1558				MessageBox.Show("No chests to save!");
1559				return;
1560			}
1561
1562			dialog.FileName = String.Format("{0}Chests.txt",
1563				Path.GetFileNameWithoutExtension(comboBoxWorldFilePath.SelectedItem.ToString()));
1564			dialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
1565			dialog.FilterIndex = 1;
1566			dialog.RestoreDirectory = true;
1567
1568			result = dialog.ShowDialog();
1569
1570			if (result == DialogResult.Cancel)
1571				return;
1572
1573			SaveChestsAsText(dialog.FileN

Large files files are truncated, but you can click here to view the full file