changeset 117:468a41d79b10

Fixes #237: loading race after choise of race bug * Don't add buttons when game system changes * Add buttons on army change when the categories have changed * Store the game system before we do army stuff so that a change in game system doesn't trigger setting the army to null and resetting our buttons (which is what happened before)
author IBBoard <dev@ibboard.co.uk>
date Thu, 07 Jan 2010 20:42:05 +0000
parents 1e92802391e3
children d2aa50d97377
files FrmMain.cs
diffstat 1 files changed, 44 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/FrmMain.cs	Thu Dec 24 20:31:22 2009 +0000
+++ b/FrmMain.cs	Thu Jan 07 20:42:05 2010 +0000
@@ -832,7 +832,6 @@
 		{
 			SetAppTitle();
 			RemoveCategoryButtons();
-			AddCategoryButtons();
 		}
 
 		private void FrmMain_ArmyChanged(Army oldArmy, Army newArmy)
@@ -856,6 +855,7 @@
 			}
 			else
 			{
+				WarFoundryCore.CurrentGameSystem = newArmy.GameSystem;
 				ListenToArmy(newArmy);
 				SetNonNullArmyState(newArmy);
 			}
@@ -906,12 +906,7 @@
 
 		private void SetNonNullArmyState(Army newArmy)
 		{
-			if (newArmy.Race.HasCategoryOverrides())
-			{
-				RemoveCategoryButtons();
-				AddCategoryButtons(newArmy.Race.Categories);
-			}
-
+			SetCategoryButtons(newArmy.Race.Categories);
 			EnableCategoryButtons();
 			miSaveArmyAs.Enabled = true;
 			miCloseArmy.Enabled = true;
@@ -919,39 +914,33 @@
 			armyTree.Show();
 		}
 
-		private void AddCategoryButtons()
+		private void SetCategoryButtons(Category[] cats)
 		{
-			if (CurrentGameSystem!=null)
+			if (CategoryButtonsHaveChanged(cats))
 			{
-				AddCategoryButtons(CurrentGameSystem.Categories);
+				RemoveCategoryButtons();
+				AddCategoryButtons(cats);
 			}
 		}
 
-		private void AddCategoryButtons(Category[] cats)
+		private bool CategoryButtonsHaveChanged(Category[] cats)
 		{
 			int catCount = cats.Length;
-			Category cat;
-			categoryButtons = new ToolBarButton[catCount+1];
-
-			ToolBarButton sep = new ToolBarButton();
-			sep.Style = ToolBarButtonStyle.Separator;
-			categoryButtons[0] = sep;
-
-			IBBToolBarButton button;
+			bool haveChanged = (categoryButtons == null || catCount != (categoryButtons.Length - 1));
 
-			for (int i = 0; i<catCount; i++)
+			if (!haveChanged)
 			{
-				cat = cats[i];
-				button = new IBBToolBarButton();
-				///button.Name = "bttnAddCategory" + cat.Name[0].ToString();
-				button.Text = cat.Name.ToString();//String.Format(Translation.GetTranslation("bttnAddCategory"), cat.Name);
-				button.Tag = cat;
-				button.ImageIndex = 6;
-				button.Enabled = false;
-				categoryButtons[i+1] = button;
+				for (int i = 0; i < catCount; i++)
+				{
+					if (cats[i].Equals(categoryButtons[i+1].Tag))
+					{
+						haveChanged = true;
+						break;
+					}
+				}
 			}
 
-			this.Invoke(new ToolBarButtonRangeDelegate(toolBar.Buttons.AddRange), new object[]{categoryButtons});
+			return haveChanged;
 		}
 
 		private void RemoveCategoryButtons()
@@ -965,6 +954,32 @@
 			}
 		}
 
+		private void AddCategoryButtons(Category[] cats)
+		{
+			int catCount = cats.Length;
+			Category cat;
+			categoryButtons = new ToolBarButton[catCount + 1];
+
+			ToolBarButton sep = new ToolBarButton();
+			sep.Style = ToolBarButtonStyle.Separator;
+			categoryButtons[0] = sep;
+
+			IBBToolBarButton button;
+
+			for (int i = 0; i < catCount; i++)
+			{
+				cat = cats[i];
+				button = new IBBToolBarButton();
+				button.Text = cat.Name;
+				button.Tag = cat;
+				button.ImageIndex = 6;
+				button.Enabled = false;
+				categoryButtons[i + 1] = button;
+			}
+
+			this.Invoke(new ToolBarButtonRangeDelegate(toolBar.Buttons.AddRange), new object[] { categoryButtons });
+		}
+
 		private void EnableCategoryButtons()
 		{
 			SetCategoryButtonState(true);