diff FrmMainWindow.cs @ 140:2b9fabd65309

Re #344: Show unit requirement failures * Use status bar properly * Add initial validation label * Make use of new event from Army to see when things changed Also: * Commit some automated changes
author IBBoard <dev@ibboard.co.uk>
date Sat, 15 Oct 2011 16:07:13 +0100
parents 33962c2ef550
children 1ce4fe9ae3c1
line wrap: on
line diff
--- a/FrmMainWindow.cs	Wed Oct 12 20:36:02 2011 +0100
+++ b/FrmMainWindow.cs	Sat Oct 15 16:07:13 2011 +0100
@@ -29,6 +29,7 @@
 using log4net;
 using WFObjects = IBBoard.WarFoundry.API.Objects;
 using IBBoard.WarFoundry.API.Savers.Xml;
+using IBBoard.WarFoundry.API.Objects.Requirement;
 
 namespace IBBoard.WarFoundry.GUI.GTK
 {
@@ -396,7 +397,7 @@
 
 		private void OnPointsValueChanged(WarFoundryObject obj, double before, double after)
 		{
-			SetPointsPanelText();
+			SetStatusBarText();
 		}
 
 		public Preferences Preferences
@@ -522,6 +523,7 @@
 				oldArmy.PointsValueChanged -= PointsValueChangedMethod;
 				oldArmy.NameChanged -= OnArmyNameChanged;
 				oldArmy.MaxPointsValueChanged -= OnMaxPointsValueChanged;
+				oldArmy.ArmyCompositionChanged -= HandleArmyCompositionChanged;
 			}
 
 			unitToWidgetMap.Clear();
@@ -542,6 +544,7 @@
 				newArmy.PointsValueChanged += PointsValueChangedMethod;
 				newArmy.NameChanged += OnArmyNameChanged;
 				newArmy.MaxPointsValueChanged += OnMaxPointsValueChanged;
+				newArmy.ArmyCompositionChanged += HandleArmyCompositionChanged;
 				//TODO: Clear all buttons
 				EnableCategoryButtons();
 
@@ -557,14 +560,14 @@
 			miSaveArmyAs.Sensitive = nonNullNewArmy;
 			miExportArmyAs.Sensitive = nonNullNewArmy;
 			miEditArmy.Sensitive = nonNullNewArmy;
-			hpaned2.Visible = nonNullNewArmy;
+			treeUnits.Visible = nonNullNewArmy;
 			loadedArmyPath = null;
 			//New army has no changes, so we can't save it
 			miSaveArmy.Sensitive = false;
 			bttnSaveArmy.Sensitive = false;
 
 			CommandStack.Reset();
-			SetPointsPanelText();
+			SetStatusBarText();
 		}
 
 		private void OnArmyNameChanged (WarFoundryObject obj, string oldValue, string newValue)
@@ -574,7 +577,12 @@
 
 		private void OnMaxPointsValueChanged (WarFoundryObject obj, int oldValue, int newValue)
 		{
-			SetPointsPanelText();
+			SetStatusBarText();
+		}
+
+		private void HandleArmyCompositionChanged()
+		{
+			SetStatusBarText();
 		}
 
 		private void SetArmyTree(Army army)
@@ -657,15 +665,57 @@
 			toolbar.ShowAll();
 		}
 
-		private void SetPointsPanelText()
+		private void SetStatusBarText()
 		{
 			if (WarFoundryCore.CurrentArmy != null)
 			{
-				lblTotalPoints.Text = Translation.GetTranslation("statusPanelPoints", "{0}{2} of {1}{3}", WarFoundryCore.CurrentArmy.Points, WarFoundryCore.CurrentArmy.MaxPoints, WarFoundryCore.CurrentGameSystem.GetPointsAbbrev(WarFoundryCore.CurrentArmy.Points), WarFoundryCore.CurrentGameSystem.GetPointsAbbrev(WarFoundryCore.CurrentArmy.MaxPoints));
+				statusbar.Push(1, GetPointsText());
+				SetValidationText();
 			}
 			else
 			{
-				lblTotalPoints.Text = "";
+				statusbar.Push(1, "");
+				lblValidationWarning.Text = "";
+			}
+		}
+
+		private string GetPointsText()
+		{
+			int maxPts = WarFoundryCore.CurrentArmy.MaxPoints;
+			string maxPtsAbbrev = WarFoundryCore.CurrentGameSystem.GetPointsAbbrev(WarFoundryCore.CurrentArmy.MaxPoints);
+			double points = WarFoundryCore.CurrentArmy.Points;
+			string ptsAbbrev = WarFoundryCore.CurrentGameSystem.GetPointsAbbrev(WarFoundryCore.CurrentArmy.Points);
+			return Translation.GetTranslation("statusPanelPoints", "{0}{2} of {1}{3}", points, maxPts, ptsAbbrev, maxPtsAbbrev);
+		}
+
+		private void SetValidationText()
+		{
+			if (WarFoundryCore.CurrentGameSystem != null && WarFoundryCore.CurrentGameSystem.WarnOnError)
+			{
+				SetValidationTextAndColour();
+			}
+			else
+			{
+				lblValidationWarning.Text = "";
+			}
+		}
+
+		private void SetValidationTextAndColour()
+		{
+			ICollection<string> failureMessages;
+			Validation result = RequirementHandler.ValidateArmy(WarFoundryCore.CurrentArmy, out failureMessages);
+			string pluralHack = (failureMessages.Count == 1 ? "" : "s");
+			lblValidationWarning.Text = String.Format("{0} validation warning{1}", failureMessages.Count, pluralHack);
+			
+			if (Validates.AsOkay(result))
+			{
+				lblValidationWarning.ModifyFg(StateType.Normal, Gdk.Color.Zero);
+			}
+			else
+			{
+				Gdk.Color red;
+				Gdk.Color.Parse("#cc0000", ref red);
+				lblValidationWarning.ModifyFg(StateType.Normal, red);
 			}
 		}