changeset 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 4149dbdf042f
children a8f847c1e305
files FrmMainWindow.cs FrmNewUnit.cs
diffstat 2 files changed, 56 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/FrmMainWindow.cs	Tue Jul 17 20:46:44 2012 +0100
+++ b/FrmMainWindow.cs	Sun Aug 05 21:01:00 2012 +0100
@@ -240,7 +240,7 @@
 		{
 			foreach (FileLoadFailure failure in failures)
 			{
-				logger.Warn("Failed to load " + failure.FailedFile.FullName + ": " + failure.Message);
+				logger.Warn("Failed to load " + failure.FailedFile.Name + ": " + failure.Message);
 			}
 		}
 
@@ -331,8 +331,7 @@
 						treeUnits.ExpandToPath(path);
 					}
 				}
-			}
-			while (model.IterNext(ref iter));
+			} while (model.IterNext(ref iter));
 		}
 
 		private void OnUnitRemoved(WarFoundryObject obj)
@@ -373,8 +372,7 @@
 								removed = true;
 								break;
 							}
-						}
-						while (model.IterNext(ref innerIter));
+						} while (model.IterNext(ref innerIter));
 
 						if (removed)
 						{
@@ -382,8 +380,7 @@
 						}
 					}
 				}
-			}
-			while (model.IterNext(ref iter));
+			} while (model.IterNext(ref iter));
 		}
 
 		private void RemoveUnitTab(WFObjects.Unit unit)
@@ -1177,15 +1174,26 @@
 		protected virtual void OnTreeUnitsPopupMenu(object o, Gtk.PopupMenuArgs args)
 		{
 			object selectedItem = TreeUtils.GetSelectedItem(treeUnits);
+			WFObjects.Unit theUnit = selectedItem as WFObjects.Unit;
 
-			if (selectedItem is WFObjects.Unit)
+			if (theUnit != null)
 			{
 				Menu menu = new Menu();
 				ImageMenuItem delete = new ImageMenuItem(Translation.GetTranslation("menuRemoveUnit", "remove unit"));
 				delete.Image = new Gtk.Image(Stock.Delete, IconSize.Menu);
 				delete.Activated += new EventHandler(OnUnitDelete);
-				delete.Data["unit"] = selectedItem;
+				delete.Data["unit"] = theUnit;
 				menu.Append(delete);
+
+				if (theUnit.UnitType.ContainedUnitTypes.Length > 0)
+				{					
+					ImageMenuItem addContained = new ImageMenuItem(Translation.GetTranslation("menuAddUnit", "add unit"));
+					addContained.Image = new Gtk.Image(Stock.Add, IconSize.Menu);
+					addContained.Activated += new EventHandler(OnSubUnitAdd);
+					addContained.Data["unit"] = theUnit;
+					menu.Append(addContained);
+				}
+
 				menu.ShowAll();
 				menu.Popup();
 			}
@@ -1197,6 +1205,24 @@
 			commandStack.Execute(command);
 		}
 
+		void OnSubUnitAdd(object sender, EventArgs e)
+		{
+			WFObjects.Unit unit = (WFObjects.Unit)((ImageMenuItem)sender).Data["unit"];
+			logger.DebugFormat("Show FrmNewUnit for contained units of {0}", unit.Name);
+			FrmNewUnit newUnit = new FrmNewUnit(unit, WarFoundryCore.CurrentArmy);
+			ResponseType response = (ResponseType)newUnit.Run();
+			newUnit.Hide();
+			
+			if (response == ResponseType.Ok)
+			{
+				CreateAndAddUnitCommand cmd = new CreateAndAddUnitCommand(newUnit.SelectedUnit, WarFoundryCore.CurrentArmy.GetCategory(newUnit.SelectedUnit.MainCategory), unit);
+				commandStack.Execute(cmd);
+				ShowUnitWidget(cmd.Unit);
+			}
+			
+			newUnit.Dispose();
+		}
+
 		[GLib.ConnectBefore]
 
 		protected virtual void UnitTreeButtonPressed(object o, Gtk.ButtonPressEventArgs args)
--- 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;