diff FrmNewUnit.cs @ 162:354c1d2ad086

Re #423: Support nested units in GTK UI * Add "add unit" context menu to tree * Add quick hacks to unit dialog to work with both nesting and direct category units
author IBBoard <dev@ibboard.co.uk>
date Sun, 05 Aug 2012 21:01:00 +0100
parents f23e5b40ca9d
children
line wrap: on
line diff
--- a/FrmNewUnit.cs	Tue Jul 17 20:46:44 2012 +0100
+++ b/FrmNewUnit.cs	Sun Aug 05 21:01:00 2012 +0100
@@ -6,11 +6,13 @@
 using System.Collections.Generic;
 using Gtk;
 using IBBoard.GtkSharp.Translatable;
+using WFObjects = IBBoard.WarFoundry.API.Objects;
 using IBBoard.WarFoundry.API.Objects;
 using log4net;
 using IBBoard.Lang;
 using IBBoard.WarFoundry.API.Objects.Requirement;
 using System.Text;
+using IBBoard.WarFoundry.API.Objects.Requirement.Context;
 
 namespace IBBoard.WarFoundry.GUI.GTK
 {
@@ -19,13 +21,25 @@
 		private ILog logger = LogManager.GetLogger(typeof(FrmNewUnit));
 		private UnitType unitType;
 		private Army unitArmy;
-		private Category cat;
+		private string titlePart;
+		private AddingContext context;
 
-		public FrmNewUnit(Race race, Category category, Army army)
+		public FrmNewUnit(Race race, Category category, Army army) : this(race.GetUnitTypes(category), army)
+		{
+			titlePart = category.Name;
+			context = new NullAddingContext();
+		}
+
+		public FrmNewUnit(WFObjects.Unit unit, Army army) : this(unit.UnitType.ContainedUnitTypes, army)
+		{
+			titlePart = unit.Name;
+			context = new AddingToParentContext(unit);
+		}
+
+		private FrmNewUnit(UnitType[] unitTypes, Army army)
 		{
 			this.Build();
 			unitArmy = army;
-			cat = category;
 			
 			TreeViewColumn unitTypeColumn = new TreeViewColumn();
 			CellRendererText unitTypeCell = new CellRendererText();
@@ -33,10 +47,9 @@
 			lstUnitTypes.AppendColumn(unitTypeColumn);
 			unitTypeColumn.SetCellDataFunc(unitTypeCell, new TreeCellDataFunc(RenderUnitTypeName));
 			ListStore store = new ListStore(typeof(UnitType));
-			UnitType[] types = race.GetUnitTypes(cat);
-			logger.DebugFormat("Listing {0} unit types in {1}", types.Length, cat.Name);
+			logger.DebugFormat("Listing {0} unit types", unitTypes.Length);
 			
-			foreach (UnitType type in types)
+			foreach (UnitType type in unitTypes)
 			{
 				logger.DebugFormat("Adding unit type {0}", type.Name);
 				store.AppendValues(type);
@@ -64,7 +77,7 @@
 		{
 			base.Translate();
 			lstUnitTypes.Columns[0].Title = Translation.GetTranslation("frmNewUnitNewUnitColumn", "unit type");
-			Title = Translation.GetTranslation(Name, "Create new unit", cat.Name);
+			Title = Translation.GetTranslation(Name, "Create new unit", titlePart);
 		}
 
 		private void RenderUnitTypeName(TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter)
@@ -106,7 +119,7 @@
 			{
 				bttnCreate.Sensitive = true;
 				ICollection<string> failureMessages;
-				Validation result = RequirementHandler.AllowsAdding(type, unitArmy, out failureMessages);
+				Validation result = RequirementHandler.AllowsAdding(type, unitArmy, context, out failureMessages);
 				bool validationFailed = !Validates.AsOkay(result);
 				validationWidget.Visible = validationFailed;