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 diff
     1.1 --- a/FrmMainWindow.cs	Tue Jul 17 20:46:44 2012 +0100
     1.2 +++ b/FrmMainWindow.cs	Sun Aug 05 21:01:00 2012 +0100
     1.3 @@ -240,7 +240,7 @@
     1.4  		{
     1.5  			foreach (FileLoadFailure failure in failures)
     1.6  			{
     1.7 -				logger.Warn("Failed to load " + failure.FailedFile.FullName + ": " + failure.Message);
     1.8 +				logger.Warn("Failed to load " + failure.FailedFile.Name + ": " + failure.Message);
     1.9  			}
    1.10  		}
    1.11  
    1.12 @@ -331,8 +331,7 @@
    1.13  						treeUnits.ExpandToPath(path);
    1.14  					}
    1.15  				}
    1.16 -			}
    1.17 -			while (model.IterNext(ref iter));
    1.18 +			} while (model.IterNext(ref iter));
    1.19  		}
    1.20  
    1.21  		private void OnUnitRemoved(WarFoundryObject obj)
    1.22 @@ -373,8 +372,7 @@
    1.23  								removed = true;
    1.24  								break;
    1.25  							}
    1.26 -						}
    1.27 -						while (model.IterNext(ref innerIter));
    1.28 +						} while (model.IterNext(ref innerIter));
    1.29  
    1.30  						if (removed)
    1.31  						{
    1.32 @@ -382,8 +380,7 @@
    1.33  						}
    1.34  					}
    1.35  				}
    1.36 -			}
    1.37 -			while (model.IterNext(ref iter));
    1.38 +			} while (model.IterNext(ref iter));
    1.39  		}
    1.40  
    1.41  		private void RemoveUnitTab(WFObjects.Unit unit)
    1.42 @@ -1177,15 +1174,26 @@
    1.43  		protected virtual void OnTreeUnitsPopupMenu(object o, Gtk.PopupMenuArgs args)
    1.44  		{
    1.45  			object selectedItem = TreeUtils.GetSelectedItem(treeUnits);
    1.46 +			WFObjects.Unit theUnit = selectedItem as WFObjects.Unit;
    1.47  
    1.48 -			if (selectedItem is WFObjects.Unit)
    1.49 +			if (theUnit != null)
    1.50  			{
    1.51  				Menu menu = new Menu();
    1.52  				ImageMenuItem delete = new ImageMenuItem(Translation.GetTranslation("menuRemoveUnit", "remove unit"));
    1.53  				delete.Image = new Gtk.Image(Stock.Delete, IconSize.Menu);
    1.54  				delete.Activated += new EventHandler(OnUnitDelete);
    1.55 -				delete.Data["unit"] = selectedItem;
    1.56 +				delete.Data["unit"] = theUnit;
    1.57  				menu.Append(delete);
    1.58 +
    1.59 +				if (theUnit.UnitType.ContainedUnitTypes.Length > 0)
    1.60 +				{					
    1.61 +					ImageMenuItem addContained = new ImageMenuItem(Translation.GetTranslation("menuAddUnit", "add unit"));
    1.62 +					addContained.Image = new Gtk.Image(Stock.Add, IconSize.Menu);
    1.63 +					addContained.Activated += new EventHandler(OnSubUnitAdd);
    1.64 +					addContained.Data["unit"] = theUnit;
    1.65 +					menu.Append(addContained);
    1.66 +				}
    1.67 +
    1.68  				menu.ShowAll();
    1.69  				menu.Popup();
    1.70  			}
    1.71 @@ -1197,6 +1205,24 @@
    1.72  			commandStack.Execute(command);
    1.73  		}
    1.74  
    1.75 +		void OnSubUnitAdd(object sender, EventArgs e)
    1.76 +		{
    1.77 +			WFObjects.Unit unit = (WFObjects.Unit)((ImageMenuItem)sender).Data["unit"];
    1.78 +			logger.DebugFormat("Show FrmNewUnit for contained units of {0}", unit.Name);
    1.79 +			FrmNewUnit newUnit = new FrmNewUnit(unit, WarFoundryCore.CurrentArmy);
    1.80 +			ResponseType response = (ResponseType)newUnit.Run();
    1.81 +			newUnit.Hide();
    1.82 +			
    1.83 +			if (response == ResponseType.Ok)
    1.84 +			{
    1.85 +				CreateAndAddUnitCommand cmd = new CreateAndAddUnitCommand(newUnit.SelectedUnit, WarFoundryCore.CurrentArmy.GetCategory(newUnit.SelectedUnit.MainCategory), unit);
    1.86 +				commandStack.Execute(cmd);
    1.87 +				ShowUnitWidget(cmd.Unit);
    1.88 +			}
    1.89 +			
    1.90 +			newUnit.Dispose();
    1.91 +		}
    1.92 +
    1.93  		[GLib.ConnectBefore]
    1.94  
    1.95  		protected virtual void UnitTreeButtonPressed(object o, Gtk.ButtonPressEventArgs args)
     2.1 --- a/FrmNewUnit.cs	Tue Jul 17 20:46:44 2012 +0100
     2.2 +++ b/FrmNewUnit.cs	Sun Aug 05 21:01:00 2012 +0100
     2.3 @@ -6,11 +6,13 @@
     2.4  using System.Collections.Generic;
     2.5  using Gtk;
     2.6  using IBBoard.GtkSharp.Translatable;
     2.7 +using WFObjects = IBBoard.WarFoundry.API.Objects;
     2.8  using IBBoard.WarFoundry.API.Objects;
     2.9  using log4net;
    2.10  using IBBoard.Lang;
    2.11  using IBBoard.WarFoundry.API.Objects.Requirement;
    2.12  using System.Text;
    2.13 +using IBBoard.WarFoundry.API.Objects.Requirement.Context;
    2.14  
    2.15  namespace IBBoard.WarFoundry.GUI.GTK
    2.16  {
    2.17 @@ -19,13 +21,25 @@
    2.18  		private ILog logger = LogManager.GetLogger(typeof(FrmNewUnit));
    2.19  		private UnitType unitType;
    2.20  		private Army unitArmy;
    2.21 -		private Category cat;
    2.22 +		private string titlePart;
    2.23 +		private AddingContext context;
    2.24  
    2.25 -		public FrmNewUnit(Race race, Category category, Army army)
    2.26 +		public FrmNewUnit(Race race, Category category, Army army) : this(race.GetUnitTypes(category), army)
    2.27 +		{
    2.28 +			titlePart = category.Name;
    2.29 +			context = new NullAddingContext();
    2.30 +		}
    2.31 +
    2.32 +		public FrmNewUnit(WFObjects.Unit unit, Army army) : this(unit.UnitType.ContainedUnitTypes, army)
    2.33 +		{
    2.34 +			titlePart = unit.Name;
    2.35 +			context = new AddingToParentContext(unit);
    2.36 +		}
    2.37 +
    2.38 +		private FrmNewUnit(UnitType[] unitTypes, Army army)
    2.39  		{
    2.40  			this.Build();
    2.41  			unitArmy = army;
    2.42 -			cat = category;
    2.43  			
    2.44  			TreeViewColumn unitTypeColumn = new TreeViewColumn();
    2.45  			CellRendererText unitTypeCell = new CellRendererText();
    2.46 @@ -33,10 +47,9 @@
    2.47  			lstUnitTypes.AppendColumn(unitTypeColumn);
    2.48  			unitTypeColumn.SetCellDataFunc(unitTypeCell, new TreeCellDataFunc(RenderUnitTypeName));
    2.49  			ListStore store = new ListStore(typeof(UnitType));
    2.50 -			UnitType[] types = race.GetUnitTypes(cat);
    2.51 -			logger.DebugFormat("Listing {0} unit types in {1}", types.Length, cat.Name);
    2.52 +			logger.DebugFormat("Listing {0} unit types", unitTypes.Length);
    2.53  			
    2.54 -			foreach (UnitType type in types)
    2.55 +			foreach (UnitType type in unitTypes)
    2.56  			{
    2.57  				logger.DebugFormat("Adding unit type {0}", type.Name);
    2.58  				store.AppendValues(type);
    2.59 @@ -64,7 +77,7 @@
    2.60  		{
    2.61  			base.Translate();
    2.62  			lstUnitTypes.Columns[0].Title = Translation.GetTranslation("frmNewUnitNewUnitColumn", "unit type");
    2.63 -			Title = Translation.GetTranslation(Name, "Create new unit", cat.Name);
    2.64 +			Title = Translation.GetTranslation(Name, "Create new unit", titlePart);
    2.65  		}
    2.66  
    2.67  		private void RenderUnitTypeName(TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter)
    2.68 @@ -106,7 +119,7 @@
    2.69  			{
    2.70  				bttnCreate.Sensitive = true;
    2.71  				ICollection<string> failureMessages;
    2.72 -				Validation result = RequirementHandler.AllowsAdding(type, unitArmy, out failureMessages);
    2.73 +				Validation result = RequirementHandler.AllowsAdding(type, unitArmy, context, out failureMessages);
    2.74  				bool validationFailed = !Validates.AsOkay(result);
    2.75  				validationWidget.Visible = validationFailed;
    2.76