Mercurial > repos > IBDev-IBBoard.WarFoundry.GUI.WinForms
changeset 8:755e60be28be
Re #61 - Complete structure of WarFoundry API objects
* Update to using UnitEquipmentItem where properties have moved from EquipmentItem
* Add UnitEquipmentChoice as replacement for UnitEquipmentItemObj
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 05 Apr 2009 13:50:41 +0000 |
parents | 9828ba4f3f36 |
children | ffb49ec86df7 |
files | FrmEditUnitEquipment.cs FrmNewUnitEquipment.cs FrmReplaceUnitEquipment.cs FrmUnit.cs IBBoard.WarFoundry.GUI.WinForms.csproj Util/UnitEquipmentChoice.cs |
diffstat | 6 files changed, 111 insertions(+), 60 deletions(-) [+] |
line wrap: on
line diff
--- a/FrmEditUnitEquipment.cs Sat Mar 07 21:08:31 2009 +0000 +++ b/FrmEditUnitEquipment.cs Sun Apr 05 13:50:41 2009 +0000 @@ -21,8 +21,7 @@ { private Unit unit; private UnitEquipmentItem equipItem; - private EquipmentItem equip; - private float equipAmount; + private double equipAmount; private CommandStack commandStack; private System.Windows.Forms.NumericUpDown numEquipAmount; @@ -41,15 +40,14 @@ commandStack = stack; this.unit = unit; this.equipItem = equipItem; - equip = equipItem.EquipmentItem; InitializeComponent(); - this.Text = equip.Name + " for " + unit.Name; - equipAmount = unit.GetEquipmentAmount(equip.ID); + this.Text = equipItem.Name + " for " + unit.Name; + equipAmount = unit.GetEquipmentAmount(equipItem); - if (equip.IsRatioLimit) + if (equipItem.IsRatioLimit) { - numEquipAmount.Minimum = (decimal)Math.Round(equip.MinNumber * 100, 2); - numEquipAmount.Maximum = (decimal)Math.Round(equip.MaxNumber * 100, 2); + numEquipAmount.Minimum = (decimal)Math.Round(equipItem.MinNumber * 100.0, 2); + numEquipAmount.Maximum = (decimal)Math.Round(equipItem.MaxNumber * 100.0, 2); numEquipAmount.Value = (decimal)Math.Round(equipAmount * 100, 2); cbEquipAll.Enabled = false; lblPercent.Visible = true; @@ -57,12 +55,12 @@ } else { - numEquipAmount.Minimum = (decimal)(equip.MinNumber != -1 ? equip.MinNumber : unit.Size); - numEquipAmount.Maximum = (decimal)(equip.MaxNumber != -1 ? equip.MaxNumber : unit.Size); + numEquipAmount.Minimum = (decimal)(equipItem.MinNumber != -1 ? equipItem.MinNumber : unit.Size); + numEquipAmount.Maximum = (decimal)(equipItem.MaxNumber != -1 ? equipItem.MaxNumber : unit.Size); numEquipAmount.Value = (decimal)(equipAmount == -1 ? unit.Size : equipAmount); cbEquipAll.Checked = (equipAmount == -1); - cbEquipAll.Enabled = (equip.MaxNumber == -1 && equip.MinNumber != -1); - numEquipAmount.Enabled = !cbEquipAll.Checked && equip.MinNumber != -1; + cbEquipAll.Enabled = (equipItem.MaxNumber == -1 && equipItem.MinNumber != -1); + numEquipAmount.Enabled = !cbEquipAll.Checked && equipItem.MinNumber != -1; numEquipAmount.Width = 144; } @@ -189,8 +187,8 @@ } private bool setValue() - { - if ((!numEquipAmount.Enabled || numEquipAmount.Value == 0) && !cbEquipAll.Checked && unit.GetEquipmentAmount(equip.ID) != 0) + { + if ((!numEquipAmount.Enabled || numEquipAmount.Value == 0) && !cbEquipAll.Checked && unit.GetEquipmentAmount(equipItem) != 0) { if (equipItem.IsRequired) { @@ -212,18 +210,18 @@ { if (equipAmount!=-1) { - commandStack.Execute(new SetUnitEquipmentAmountCommand(unit, equip, -1)); + commandStack.Execute(new SetUnitEquipmentAmountCommand(unit, equipItem, -1)); } } else if (numEquipAmount.Value != (decimal)equipAmount) { - if (equip.IsRatioLimit) + if (equipItem.IsRatioLimit) { - commandStack.Execute(new SetUnitEquipmentAmountCommand(unit, equip, (float)((double)numEquipAmount.Value / 100.0))); + commandStack.Execute(new SetUnitEquipmentAmountCommand(unit, equipItem, ((double)numEquipAmount.Value / 100.0))); } else { - commandStack.Execute(new SetUnitEquipmentAmountCommand(unit, equip, (float)numEquipAmount.Value)); + commandStack.Execute(new SetUnitEquipmentAmountCommand(unit, equipItem, (double)numEquipAmount.Value)); } } @@ -237,7 +235,7 @@ private void cbEquipAll_CheckedChanged(object sender, System.EventArgs e) { - numEquipAmount.Enabled = !cbEquipAll.Checked && equip.MinNumber != -1; + numEquipAmount.Enabled = !cbEquipAll.Checked && equipItem.MinNumber != -1; setOkayButton(); }
--- a/FrmNewUnitEquipment.cs Sat Mar 07 21:08:31 2009 +0000 +++ b/FrmNewUnitEquipment.cs Sun Apr 05 13:50:41 2009 +0000 @@ -189,21 +189,20 @@ private void setValue() { UnitEquipmentItem equipItem = (UnitEquipmentItem)listItems.SelectedItem; - EquipmentItem equip = equipItem.EquipmentItem; if (cbEquipAll.Checked) { - commandStack.Execute(new SetUnitEquipmentAmountCommand(unit, equip, -1)); + commandStack.Execute(new SetUnitEquipmentAmountCommand(unit, equipItem, -1)); } else { - if (equip.IsRatioLimit) + if (equipItem.IsRatioLimit) { - commandStack.Execute(new SetUnitEquipmentAmountCommand(unit, equip, (float)((double)numEquipAmount.Value / 100.0))); + commandStack.Execute(new SetUnitEquipmentAmountCommand(unit, equipItem, (double)numEquipAmount.Value / 100.0)); } else { - commandStack.Execute(new SetUnitEquipmentAmountCommand(unit, equip, (float)numEquipAmount.Value)); + commandStack.Execute(new SetUnitEquipmentAmountCommand(unit, equipItem, (double)numEquipAmount.Value)); } } } @@ -215,7 +214,7 @@ private void cbEquipAll_CheckedChanged(object sender, System.EventArgs e) { - numEquipAmount.Enabled = ((UnitEquipmentItem)listItems.SelectedItem).EquipmentItem.MinNumber != -1 && !cbEquipAll.Checked; + numEquipAmount.Enabled = ((UnitEquipmentItem)listItems.SelectedItem).MinNumber != -1 && !cbEquipAll.Checked; setOkayButton(); } @@ -230,13 +229,12 @@ if (listItems.SelectedIndex > -1) { UnitEquipmentItem equipItem = (UnitEquipmentItem)listItems.SelectedItem; - EquipmentItem equip = equipItem.EquipmentItem; required = equipItem.IsRequired; - if (equip.IsRatioLimit) + if (equipItem.IsRatioLimit) { - numEquipAmount.Minimum = (decimal)Math.Round(equip.MinNumber * 100, 2); - numEquipAmount.Maximum = (decimal)Math.Round(equip.MaxNumber * 100, 2); + numEquipAmount.Minimum = (decimal)Math.Round(equipItem.MinNumber * 100.0, 2); + numEquipAmount.Maximum = (decimal)Math.Round(equipItem.MaxNumber * 100.0, 2); numEquipAmount.Value = numEquipAmount.Minimum; cbEquipAll.Enabled = false; lblPercent.Visible = true; @@ -244,11 +242,11 @@ } else { - numEquipAmount.Minimum = (decimal)(equip.MinNumber != -1 ? equip.MinNumber : unit.Size); - numEquipAmount.Maximum = (decimal)(equip.MaxNumber != -1 ? equip.MaxNumber : unit.Size); + numEquipAmount.Minimum = (decimal)(equipItem.MinNumber != -1 ? equipItem.MinNumber : unit.Size); + numEquipAmount.Maximum = (decimal)(equipItem.MaxNumber != -1 ? equipItem.MaxNumber : unit.Size); numEquipAmount.Value = numEquipAmount.Minimum; - cbEquipAll.Enabled = equip.MinNumber != -1; - cbEquipAll.Checked = equip.MinNumber == -1; + cbEquipAll.Enabled = equipItem.MinNumber != -1; + cbEquipAll.Checked = equipItem.MinNumber == -1; numEquipAmount.Width = 144; } }
--- a/FrmReplaceUnitEquipment.cs Sat Mar 07 21:08:31 2009 +0000 +++ b/FrmReplaceUnitEquipment.cs Sun Apr 05 13:50:41 2009 +0000 @@ -21,7 +21,7 @@ public class FrmReplaceUnitEquipment : System.Windows.Forms.Form { private Unit unit; - private EquipmentItem oldItem; + private UnitEquipmentItem oldItem; private bool required; private CommandStack commandStack; @@ -42,7 +42,7 @@ { commandStack = stack; this.unit = unit; - oldItem = item.EquipmentItem; + oldItem = item; InitializeComponent(); this.Text = "Replace " + item.EquipmentItem.Name + " with new equipment"; object[] items = Arrays.Subtract(unit.UnitType.GetEquipmentItemsByExclusionGroup(item.MutexGroup), unit.GetEquipment()); @@ -191,21 +191,20 @@ private void setValue() { UnitEquipmentItem equipItem = (UnitEquipmentItem)listItems.SelectedItem; - EquipmentItem equip = equipItem.EquipmentItem; if (cbEquipAll.Checked) { - commandStack.Execute(new ReplaceUnitEquipmentCommand(unit, oldItem, equip, -1)); + commandStack.Execute(new ReplaceUnitEquipmentCommand(unit, oldItem, equipItem, -1)); } else { - if (equip.IsRatioLimit) + if (equipItem.IsRatioLimit) { - commandStack.Execute(new ReplaceUnitEquipmentCommand(unit, oldItem, equip, (float)((double)numEquipAmount.Value / 100.0))); + commandStack.Execute(new ReplaceUnitEquipmentCommand(unit, oldItem, equipItem, (double)numEquipAmount.Value / 100.0)); } else { - commandStack.Execute(new ReplaceUnitEquipmentCommand(unit, oldItem, equip, (float)numEquipAmount.Value)); + commandStack.Execute(new ReplaceUnitEquipmentCommand(unit, oldItem, equipItem, (double)numEquipAmount.Value)); } } } @@ -217,7 +216,7 @@ private void cbEquipAll_CheckedChanged(object sender, System.EventArgs e) { - numEquipAmount.Enabled = ((UnitEquipmentItem)listItems.SelectedItem).EquipmentItem.MinNumber != -1 && !cbEquipAll.Checked; + numEquipAmount.Enabled = ((UnitEquipmentItem)listItems.SelectedItem).MinNumber != WarFoundryCore.INFINITY && !cbEquipAll.Checked; setOkayButton(); } @@ -232,13 +231,12 @@ if (listItems.SelectedIndex > -1) { UnitEquipmentItem equipItem = (UnitEquipmentItem)listItems.SelectedItem; - EquipmentItem equip = equipItem.EquipmentItem; required = equipItem.IsRequired; - if (equip.IsRatioLimit) + if (equipItem.IsRatioLimit) { - numEquipAmount.Minimum = (decimal)Math.Round(equip.MinNumber * 100, 2); - numEquipAmount.Maximum = (decimal)Math.Round(equip.MaxNumber * 100, 2); + numEquipAmount.Minimum = (decimal)Math.Round(equipItem.MinNumber * 100.0, 2); + numEquipAmount.Maximum = (decimal)Math.Round(equipItem.MaxNumber * 100.0, 2); numEquipAmount.Value = numEquipAmount.Minimum; cbEquipAll.Enabled = false; lblPercent.Visible = true; @@ -246,11 +244,11 @@ } else { - numEquipAmount.Minimum = (decimal)(equip.MinNumber != -1 ? equip.MinNumber : unit.Size); - numEquipAmount.Maximum = (decimal)(equip.MaxNumber != -1 ? equip.MaxNumber : unit.Size); + numEquipAmount.Minimum = (decimal)(equipItem.MinNumber != -1 ? equipItem.MinNumber : unit.Size); + numEquipAmount.Maximum = (decimal)(equipItem.MaxNumber != -1 ? equipItem.MaxNumber : unit.Size); numEquipAmount.Value = numEquipAmount.Minimum; - cbEquipAll.Enabled = (equip.MaxNumber == -1 && equip.MinNumber != -1); - cbEquipAll.Checked = equip.MinNumber == -1; + cbEquipAll.Enabled = (equipItem.MaxNumber == -1 && equipItem.MinNumber != -1); + cbEquipAll.Checked = equipItem.MinNumber == -1; numEquipAmount.Width = 144; } }
--- a/FrmUnit.cs Sat Mar 07 21:08:31 2009 +0000 +++ b/FrmUnit.cs Sun Apr 05 13:50:41 2009 +0000 @@ -5,7 +5,7 @@ using System; using System.Drawing; using System.Data; -using System.Collections; +using System.Collections.Generic; using System.ComponentModel; using System.Windows.Forms; using IBBoard.Commands; @@ -13,6 +13,7 @@ using IBBoard.WarFoundry.API; using IBBoard.WarFoundry.API.Commands; using IBBoard.WarFoundry.API.Objects; +using IBBoard.WarFoundry.GUI.WinForms.Util; namespace IBBoard.WarFoundry { @@ -24,6 +25,7 @@ public class FrmUnit : IBBoard.Windows.Forms.IBBForm { private Unit unit; + private Dictionary<UnitEquipmentItem, UnitEquipmentChoice> equipmentChoices = new Dictionary<UnitEquipmentItem, UnitEquipmentChoice>(); private CommandStack commandStack; private System.Windows.Forms.DataGrid statsGrid; private System.Windows.Forms.TextBox tbUnitName; @@ -56,7 +58,7 @@ Text = unit.Name; unit.NameChanged+=new StringValChangedDelegate(unit_NameChanged); unit.UnitSizeChanged+= new IntValChangedDelegate(unit_UnitSizeChanged); - unit.UnitEquipmentAmountChanged+=new FloatValChangedDelegate(unit_UnitEquipmentAmountChanged); + unit.UnitEquipmentAmountChanged+=new DoubleValChangedDelegate(unit_UnitEquipmentAmountChanged); if (unit.UnitType.MaxSize==unit.UnitType.MinSize) { @@ -126,15 +128,29 @@ { if (item.IsRequired) { - reqdList.Items.Add(UnitEquipmentItemObj.GetEquipObj(Unit, item)); + reqdList.Items.Add(GetEquipmentChoice(item)); } else { - optList.Items.Add(UnitEquipmentItemObj.GetEquipObj(Unit, item)); + optList.Items.Add(GetEquipmentChoice(item)); } } } + private UnitEquipmentChoice GetEquipmentChoice(UnitEquipmentItem item) + { + UnitEquipmentChoice choice = null; + equipmentChoices.TryGetValue(item, out choice); + + if (choice == null) + { + choice = new UnitEquipmentChoice(Unit, item); + equipmentChoices[item] = choice; + } + + return choice; + } + /// <summary> /// Clean up any resources being used. /// </summary> @@ -416,7 +432,7 @@ private void reqdList_SelectedIndexChanged(object sender, System.EventArgs e) { - bttnReplaceWeapon.Enabled = (reqdList.SelectedIndex>-1 && ((UnitEquipmentItemObj)reqdList.SelectedItem).Item.HasAlternatives()); + bttnReplaceWeapon.Enabled = (reqdList.SelectedIndex>-1 && ((UnitEquipmentChoice)reqdList.SelectedItem).Item.HasAlternatives()); bttnEditReqdWeapon.Enabled = (reqdList.SelectedIndex>-1); } @@ -426,7 +442,7 @@ bttnRemoveWeapon.Enabled = bttnEditWeapon.Enabled; } - private void unit_UnitEquipmentAmountChanged(WarFoundryObject obj, float oldValue, float newValue) + private void unit_UnitEquipmentAmountChanged(WarFoundryObject obj, double oldValue, double newValue) { if (obj is UnitEquipmentItem) { @@ -435,11 +451,11 @@ if (newValue==0) { - weaponList.Items.Remove(UnitEquipmentItemObj.GetEquipObj(Unit, equip)); + weaponList.Items.Remove(GetEquipmentChoice(equip)); } else { - UnitEquipmentItemObj equipObj = UnitEquipmentItemObj.GetEquipObj(Unit, equip); + UnitEquipmentChoice equipObj = GetEquipmentChoice(equip); int idx = weaponList.Items.IndexOf(equipObj); if (idx>-1) @@ -456,7 +472,7 @@ private void editWeapon(ListBox list) { - FrmEditUnitEquipment editEquip = new FrmEditUnitEquipment(Unit, ((UnitEquipmentItemObj)list.SelectedItem).Item, commandStack); + FrmEditUnitEquipment editEquip = new FrmEditUnitEquipment(Unit, ((UnitEquipmentChoice)list.SelectedItem).Item, commandStack); editEquip.ShowDialog(this); } @@ -488,7 +504,7 @@ private void removeWeapon() { - commandStack.Execute(new SetUnitEquipmentAmountCommand(unit, unit.UnitType.GetEquipmentItem(((UnitEquipmentItemObj)optList.SelectedItem).Item.ID).EquipmentItem, 0)); + commandStack.Execute(new SetUnitEquipmentAmountCommand(unit, ((UnitEquipmentChoice)optList.SelectedItem).Item, 0)); } private void bttnRemoveWeapon_Click(object sender, System.EventArgs e) @@ -503,7 +519,7 @@ private void bttnReplaceWeapon_Click(object sender, System.EventArgs e) { - FrmReplaceUnitEquipment replace = new FrmReplaceUnitEquipment(unit, ((UnitEquipmentItemObj)reqdList.SelectedItem).Item, commandStack); + FrmReplaceUnitEquipment replace = new FrmReplaceUnitEquipment(unit, ((UnitEquipmentChoice)reqdList.SelectedItem).Item, commandStack); replace.ShowDialog(this); } }
--- a/IBBoard.WarFoundry.GUI.WinForms.csproj Sat Mar 07 21:08:31 2009 +0000 +++ b/IBBoard.WarFoundry.GUI.WinForms.csproj Sun Apr 05 13:50:41 2009 +0000 @@ -151,6 +151,7 @@ <Compile Include="FrmUnit.cs"> <SubType>Form</SubType> </Compile> + <Compile Include="Util\UnitEquipmentChoice.cs" /> <EmbeddedResource Include="FrmArmyTree.resx"> <DependentUpon>FrmArmyTree.cs</DependentUpon> </EmbeddedResource>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Util/UnitEquipmentChoice.cs Sun Apr 05 13:50:41 2009 +0000 @@ -0,0 +1,40 @@ +// This file (UnitEquipmentChoice.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard. +// +// The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license. + +using System; +using System.Collections; +using IBBoard.WarFoundry.API.Objects; + + + +namespace IBBoard.WarFoundry.GUI.WinForms.Util +{ + /// <summary> + /// A helper object that holds an equipment choice for a unit. + /// </summary> + + public class UnitEquipmentChoice + { + private Unit unit; + private UnitEquipmentItem item; + + private static Hashtable equipObjs = new Hashtable(); + + public UnitEquipmentChoice(Unit unit, UnitEquipmentItem unitItem) + { + this.unit = unit; + item = unitItem; + } + + public override string ToString() + { + return String.Format("{0} (For {1} at {2}pts each)", item.Name, UnitEquipmentItem.FormatEquipmentAmount(item, unit.GetEquipmentAmount(item)), item.Cost); + } + + public UnitEquipmentItem Item + { + get { return item; } + } + } +} \ No newline at end of file