changeset 240:d0454aae0241

Re #360: Add WinForms UI for adding contained units * Fix army tree for nesting
author IBBoard <dev@ibboard.co.uk>
date Sun, 15 Apr 2012 21:00:51 +0100
parents 2c3474d652e1
children e532d2f51ff0
files FrmArmyTree.cs
diffstat 1 files changed, 38 insertions(+), 15 deletions(-) [+]
line diff
     1.1 --- a/FrmArmyTree.cs	Sat Apr 14 20:48:13 2012 +0100
     1.2 +++ b/FrmArmyTree.cs	Sun Apr 15 21:00:51 2012 +0100
     1.3 @@ -80,23 +80,14 @@
     1.4  			{
     1.5  				ArmyCategory[] cats = army.Categories;
     1.6  				TreeNode[] catNodes = new TreeNode[cats.Length];
     1.7 -				Unit[] units;
     1.8  
     1.9  				for (int i = 0; i < cats.Length; i++)
    1.10  				{
    1.11  					ArmyCategory cat = cats[i];
    1.12 -					units = army.GetUnits(cat);
    1.13  					cat.NameChanged += TreeNameChangedMethod;
    1.14  					cat.PointsValueChanged += NodePointsValueChanged;
    1.15 -					TreeNode[] unitNodes = new TreeNode[units.Length];
    1.16 -					TreeNode temp;
    1.17 -
    1.18 -					for (int j = 0; j < units.Length; j++)
    1.19 -					{
    1.20 -						unitNodes[j] = CreateTreeNode(units[j]);
    1.21 -					}
    1.22 -
    1.23 -					temp = new TreeNode(CreateNodeName(cat), unitNodes);
    1.24 +					TreeNode[] unitNodes = CreateChildNodes(army.GetMainUnits(cat));
    1.25 +					TreeNode temp = new TreeNode(CreateNodeName(cat), unitNodes);
    1.26  					temp.Tag = cat;
    1.27  					catNodes[i] = temp;
    1.28  					nodes[cat.ID] = temp;
    1.29 @@ -136,7 +127,8 @@
    1.30  
    1.31  		private TreeNode CreateTreeNode(Unit unit)
    1.32  		{
    1.33 -			TreeNode temp = new TreeNode(CreateNodeName(unit));
    1.34 +			TreeNode[] unitNodes = CreateChildNodes(unit.ContainedUnits);
    1.35 +			TreeNode temp = new TreeNode(CreateNodeName(unit), unitNodes);
    1.36  			temp.Tag = unit;
    1.37  			unit.NameChanged += UnitNameChangedMethod;
    1.38  			unit.PointsValueChanged += NodePointsValueChanged;
    1.39 @@ -144,6 +136,27 @@
    1.40  			return temp;
    1.41  		}
    1.42  
    1.43 +		private TreeNode[] CreateChildNodes(Unit[] containedUnits)
    1.44 +		{
    1.45 +			TreeNode[] unitNodes;
    1.46 +
    1.47 +			if (containedUnits.Length > 0)
    1.48 +			{
    1.49 +				unitNodes = new TreeNode[containedUnits.Length];
    1.50 +
    1.51 +				for (int j = 0; j < containedUnits.Length; j++)
    1.52 +				{
    1.53 +					unitNodes[j] = CreateTreeNode(containedUnits[j]);
    1.54 +				}
    1.55 +			}
    1.56 +			else
    1.57 +			{
    1.58 +				unitNodes = new TreeNode[0];
    1.59 +			}
    1.60 +
    1.61 +			return unitNodes;
    1.62 +		}
    1.63 +
    1.64  		public void FrmArmyTree_ArmyChanged(Army oldArmy, Army newArmy)
    1.65  		{
    1.66  			if (oldArmy != null)
    1.67 @@ -170,8 +183,18 @@
    1.68  			if (obj is Unit)
    1.69  			{
    1.70  				Unit unit = (Unit)obj;
    1.71 -				ArmyCategory cat = unit.Category;
    1.72 -				TreeNode parent = nodes[cat.ID];
    1.73 +				TreeNode parent;
    1.74 +
    1.75 +				if (unit.ParentUnit == null)
    1.76 +				{
    1.77 +					ArmyCategory cat = unit.Category;
    1.78 +					parent = nodes[cat.ID];
    1.79 +				}
    1.80 +				else
    1.81 +				{
    1.82 +					parent = nodes[unit.ParentUnit.ID];
    1.83 +				}
    1.84 +
    1.85  				TreeNode unitNode = CreateTreeNode(unit);
    1.86  				parent.Nodes.Add(unitNode);
    1.87  				parent.Expand(); //make sure it's expanded
    1.88 @@ -391,7 +414,7 @@
    1.89  				if (dr == DialogResult.OK)
    1.90  				{
    1.91  					UnitType newUnit = newUnitFrm.SelectedUnit;
    1.92 -					CreateAndAddUnitCommand cmd = new CreateAndAddUnitCommand(newUnitFrm.SelectedUnit, WarFoundryCore.CurrentArmy.GetCategory(newUnit.MainCategory));
    1.93 +					CreateAndAddUnitCommand cmd = new CreateAndAddUnitCommand(newUnitFrm.SelectedUnit, WarFoundryCore.CurrentArmy.GetCategory(newUnit.MainCategory), unit);
    1.94  					commandStack.Execute(cmd);
    1.95  					MdiParent.OpenUnitDialog(cmd.Unit);
    1.96  				}