Mercurial > repos > IBBoard.WarFoundry.API
changeset 298:483b491d18f4
Fixes #337: Merge API v0.1.1 changes into v0.2
* Merge translation-related changes from v0.1.1 branch
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Mon, 17 Jan 2011 20:02:41 +0000 |
parents | 349e521785c1 |
children | 25c47b7ecc99 |
files | api/Commands/AbstractReplaceUnitEquipmentCommand.cs api/Commands/AbstractSetUnitEquipmentAmountCommand.cs api/Commands/CreateAndAddUnitCommand.cs api/Commands/RemoveUnitCommand.cs api/Commands/SetNameCommand.cs api/Commands/SetUnitSizeCommand.cs api/Objects/Unit.cs |
diffstat | 7 files changed, 94 insertions(+), 155 deletions(-) [+] |
line wrap: on
line diff
--- a/api/Commands/AbstractReplaceUnitEquipmentCommand.cs Fri Jan 07 03:35:50 2011 +0000 +++ b/api/Commands/AbstractReplaceUnitEquipmentCommand.cs Mon Jan 17 20:02:41 2011 +0000 @@ -16,9 +16,7 @@ { private SetUnitEquipmentNumericAmountCommand removeOldCommand; private AbstractSetUnitEquipmentAmountCommand addNewCommand; - private string description; - private string undoDescription; - + public AbstractReplaceUnitEquipmentCommand(Unit unit, UnitEquipmentItem oldItem, AbstractSetUnitEquipmentAmountCommand addNewEquipmentCommand) { //We can get away with a numeric amount here even if it is a ratio item because we're setting it to 0 @@ -35,12 +33,7 @@ { get { - if (description == null) - { - description = Translation.GetTranslation("replaceUnitEquipmentCommandDescription", "replace {0} with {1} for {2}", removeOldCommand.EquipItem.Name, addNewCommand.EquipItem.Name, removeOldCommand.Unit.Name); - } - - return description; + return Translation.GetTranslation("replaceUnitEquipmentCommandDescription", "replace {0} with {1} for {2}", removeOldCommand.EquipItem.Name, addNewCommand.EquipItem.Name, removeOldCommand.Unit.Name); } } @@ -48,12 +41,7 @@ { get { - if (undoDescription == null) - { - undoDescription = Translation.GetTranslation("replaceUnitEquipmentCommandUndoDescription", "replace {0} with {1} for {2}", addNewCommand.EquipItem.Name, removeOldCommand.EquipItem.Name, removeOldCommand.Unit.Name); - } - - return undoDescription; + return Translation.GetTranslation("replaceUnitEquipmentCommandUndoDescription", "replace {0} with {1} for {2}", addNewCommand.EquipItem.Name, removeOldCommand.EquipItem.Name, removeOldCommand.Unit.Name); } } @@ -75,7 +63,6 @@ removeOldCommand.Undo(); } - public override string Name { get
--- a/api/Commands/AbstractSetUnitEquipmentAmountCommand.cs Fri Jan 07 03:35:50 2011 +0000 +++ b/api/Commands/AbstractSetUnitEquipmentAmountCommand.cs Mon Jan 17 20:02:41 2011 +0000 @@ -19,9 +19,7 @@ private UnitEquipmentItem equip; private double oldAmount; private bool oldAmountWasRatio; - private string description; - private string undoDescription; - + public AbstractSetUnitEquipmentAmountCommand(Unit unit, UnitEquipmentItem item) { this.unit = unit; @@ -32,19 +30,14 @@ public override bool CanExecute() { - return (unit!=null && equip!=null); + return (unit != null && equip != null); } public override string Description { get { - if (description == null) - { - description = Translation.GetTranslation("setEquipmentAmountCommandDescription", "set {0} amount for {1} to {2}", equip.Name, unit.Name, GetNewAmountString()); - } - - return description; + return Translation.GetTranslation("setEquipmentAmountCommandDescription", "set {0} amount for {1} to {2}", equip.Name, unit.Name, GetNewAmountString()); } } @@ -60,12 +53,7 @@ { get { - if (undoDescription == null) - { - undoDescription = Translation.GetTranslation("setEquipmentAmountCommandUndoDescription", "set {0} amount for {1} to {2}", equip.Name, unit.Name, GetOldAmountString()); - } - - return undoDescription; + return Translation.GetTranslation("setEquipmentAmountCommandUndoDescription", "set {0} amount for {1} to {2}", equip.Name, unit.Name, GetOldAmountString()); } } @@ -79,23 +67,23 @@ { return oldAmountWasRatio ? GetRatioAmountString(oldAmount, UnitEquipmentRatioSelection.CalculateNumberTaken(Unit, EquipItem, oldAmount)) : GetNumberAmountString((int)oldAmount); } - + protected string GetNumberAmountString(int number) { - return Translation.GetTranslation ("equipmentAmountNumber", "{0}", number); + return Translation.GetTranslation("equipmentAmountNumber", "{0}", number); } - protected string GetRatioAmountString (double amount, int number) + protected string GetRatioAmountString(double amount, int number) { string amountString; if (amount == 100) { - amountString = Translation.GetTranslation ("equipmentAmountAll", "all ({1})", amount, number); + amountString = Translation.GetTranslation("equipmentAmountAll", "all ({1})", amount, number); } else { - amountString = Translation.GetTranslation ("equipmentAmountPercentage", "{0}% ({1})", amount, number); + amountString = Translation.GetTranslation("equipmentAmountPercentage", "{0}% ({1})", amount, number); } return amountString; @@ -107,7 +95,7 @@ return true; } - public override void Undo () + public override void Undo() { if (oldAmountWasRatio) { @@ -119,7 +107,6 @@ } } - public UnitEquipmentItem EquipItem { get { return equip; }
--- a/api/Commands/CreateAndAddUnitCommand.cs Fri Jan 07 03:35:50 2011 +0000 +++ b/api/Commands/CreateAndAddUnitCommand.cs Mon Jan 17 20:02:41 2011 +0000 @@ -14,9 +14,7 @@ private UnitType addedUnitType; private ArmyCategory armyCat; private Unit addedUnit; - private string description; - private string undoDescription; - + public CreateAndAddUnitCommand(UnitType toAdd, ArmyCategory armyCatTo) { addedUnitType = toAdd; @@ -25,19 +23,14 @@ public override bool CanExecute() { - return (addedUnitType!=null && armyCat!=null); + return (addedUnitType != null && armyCat != null); } public override string Description { get { - if (description == null) - { - description = Translation.GetTranslation("createAndAddUnitCommandDescription", "add unit of {0} to the army", addedUnitType.Name); - } - - return description; + return Translation.GetTranslation("createAndAddUnitCommandDescription", "add unit of {0} to the army", addedUnitType.Name); } } @@ -45,12 +38,7 @@ { get { - if (undoDescription == null) - { - undoDescription = Translation.GetTranslation("createAndAddUnitCommandUndoDescription", "remove unit of {0} from army", addedUnitType.Name); - } - - return undoDescription; + return Translation.GetTranslation("createAndAddUnitCommandUndoDescription", "remove unit of {0} from army", addedUnitType.Name); } }
--- a/api/Commands/RemoveUnitCommand.cs Fri Jan 07 03:35:50 2011 +0000 +++ b/api/Commands/RemoveUnitCommand.cs Mon Jan 17 20:02:41 2011 +0000 @@ -16,8 +16,6 @@ { private Unit unit; private ArmyCategory cat; - private string description; - private string undoDescription; public RemoveUnitCommand(Unit toRemove) { @@ -27,19 +25,14 @@ public override bool CanExecute() { - return (unit!=null); + return (unit != null); } public override string Description { get { - if (description == null) - { - description = Translation.GetTranslation("removeUnitCommandDescription", "remove {0} from the army", unit.Name); - } - - return description; + return Translation.GetTranslation("removeUnitCommandDescription", "remove {0} from the army", unit.Name); } } @@ -47,12 +40,7 @@ { get { - if (undoDescription == null) - { - undoDescription = Translation.GetTranslation("removeUnitCommandUndoDescription", "re-add {0} to the army", unit.Name); - } - - return undoDescription; + return Translation.GetTranslation("removeUnitCommandUndoDescription", "re-add {0} to the army", unit.Name); } }
--- a/api/Commands/SetNameCommand.cs Fri Jan 07 03:35:50 2011 +0000 +++ b/api/Commands/SetNameCommand.cs Mon Jan 17 20:02:41 2011 +0000 @@ -16,8 +16,6 @@ { private WarFoundryObject obj; private string newName, oldName; - private string description; - private string undoDescription; public SetNameCommand(WarFoundryObject toRename, string name) { @@ -28,19 +26,14 @@ public override bool CanExecute() { - return (obj!=null && newName!=null && newName!=""); + return (obj != null && newName != null && newName != ""); } public override string Description { get { - if (description == null) - { - description = Translation.GetTranslation("setUnitNameCommandDescription", "rename \"{0}\" to \"{1}\"", oldName, newName); - } - - return description; + return Translation.GetTranslation("setUnitNameCommandDescription", "rename \"{0}\" to \"{1}\"", oldName, newName); } } @@ -48,12 +41,7 @@ { get { - if (undoDescription == null) - { - undoDescription = Translation.GetTranslation("setUnitNameCommandUndoDescription", "rename \"{0}\" to \"{1}\"", newName, oldName); - } - - return undoDescription; + return Translation.GetTranslation("setUnitNameCommandUndoDescription", "rename \"{0}\" to \"{1}\"", newName, oldName); } }
--- a/api/Commands/SetUnitSizeCommand.cs Fri Jan 07 03:35:50 2011 +0000 +++ b/api/Commands/SetUnitSizeCommand.cs Mon Jan 17 20:02:41 2011 +0000 @@ -16,8 +16,6 @@ { private Unit unit; private int newSize, oldSize; - private string description; - private string undoDescription; public SetUnitSizeCommand(Unit toResize, int size) { @@ -28,19 +26,14 @@ public override bool CanExecute() { - return (unit!=null && newSize >0 && oldSize > 0); + return (unit != null && newSize > 0 && oldSize > 0); } public override string Description { get { - if (description == null) - { - description = Translation.GetTranslation("setUnitSizeCommandDescription", "set size of {0} to {1}", unit.Name, newSize); - } - - return description; + return Translation.GetTranslation("setUnitSizeCommandDescription", "set size of {0} to {1}", unit.Name, newSize); } } @@ -48,12 +41,7 @@ { get { - if (undoDescription == null) - { - undoDescription = Translation.GetTranslation("setUnitSizeCommandUndoDescription", "set size of {0} to {1}", unit.Name, oldSize); - } - - return undoDescription; + return Translation.GetTranslation("setUnitSizeCommandUndoDescription", "set size of {0} to {1}", unit.Name, oldSize); } }
--- a/api/Objects/Unit.cs Fri Jan 07 03:35:50 2011 +0000 +++ b/api/Objects/Unit.cs Mon Jan 17 20:02:41 2011 +0000 @@ -25,33 +25,38 @@ private Dictionary<UnitEquipmentItem, AbstractUnitEquipmentItemSelection> equipment = new Dictionary<UnitEquipmentItem, AbstractUnitEquipmentItemSelection>(); private Dictionary<string, List<AbstractUnitEquipmentItemSelection>> equipmentSlots = new Dictionary<string, List<AbstractUnitEquipmentItemSelection>>(); private List<Unit> containedUnits = new List<Unit>(); + public event DoubleValChangedDelegate PointsValueChanged; public event IntValChangedDelegate UnitSizeChanged; public event DoubleValChangedDelegate UnitEquipmentAmountChanged; - public Unit(UnitType unitType, ArmyCategory parentArmyCat) : this(unitType, unitType.MinSize, parentArmyCat) { } + public Unit(UnitType unitType, ArmyCategory parentArmyCat) : this(unitType, unitType.MinSize, parentArmyCat) + { + //Do nothing extra + } public Unit(UnitType unitType, int startSize, ArmyCategory parentArmyCat) : this("", "", startSize, unitType, parentArmyCat) { - SetInitialEquipment(); + SetInitialEquipment(); UnitSizeChanged += new IntValChangedDelegate(RefreshUnitEquipmentAmounts); } - + public Unit(string id, string name, int startSize, UnitType unitType, ArmyCategory parentArmyCat) : base(id, name) { Category = parentArmyCat; type = unitType; Size = startSize; CalcCost(); - UnitEquipmentAmountChanged+= new DoubleValChangedDelegate(UnitEquipmentAmountChangedHandler); - UnitSizeChanged+= new IntValChangedDelegate(UnitSizeChangedHandler); + UnitEquipmentAmountChanged += new DoubleValChangedDelegate(UnitEquipmentAmountChangedHandler); + UnitSizeChanged += new IntValChangedDelegate(UnitSizeChangedHandler); + Translation.TranslationChanged += HandleTranslationChanged; } - + private void UnitEquipmentAmountChangedHandler(WarFoundryObject obj, double oldVal, double newVal) { CalcCost(); } - + private void UnitSizeChangedHandler(WarFoundryObject obj, int oldVal, int newVal) { CalcCost(); @@ -81,6 +86,14 @@ } } + private void HandleTranslationChanged() + { + if (type != null && HasDefaultName() && size != 1) + { + OnNameChanged(null, DefaultName()); + } + } + private void SetInitialEquipment() { foreach (UnitEquipmentItem unitEquip in UnitType.GetEquipmentItems()) @@ -114,12 +127,12 @@ points += equipSelection.TotalCost; } - if (oldpoints!=points) + if (oldpoints != points) { OnPointsValueChanged(oldpoints, points); } } - + public int AdditionalTroopers { get { return Math.Max(Size - type.BaseSize, 0); } @@ -128,12 +141,12 @@ public int Size { get { return size; } - set + set { - if (value!=size) + if (value != size) { int oldValue = size; - size = (value>0 ? value : 1); + size = (value > 0 ? value : 1); OnUnitSizeChanged(oldValue, size); } } @@ -168,10 +181,10 @@ { get { return Points; } } - + public double Points { - get + get { if (points == 0) { @@ -181,12 +194,12 @@ return points; } } - + public Unit[] ContainedUnits { get { return containedUnits.ToArray(); } } - + public void AddContainedUnit(Unit unit) { if (UnitType.CanContainUnit(unit)) @@ -203,12 +216,12 @@ throw new InvalidContainershipException(this, unit); } } - + public void RemoveContainedUnit(Unit unit) { containedUnits.Remove(unit); } - + public Unit ParentUnit { get { return parentUnit; } @@ -218,7 +231,7 @@ { parentUnit = value; - if (value!=null) + if (value != null) { value.AddContainedUnit(this); } @@ -241,7 +254,7 @@ { List<EquipmentItem> list = new List<EquipmentItem>(); - foreach(UnitEquipmentItem item in GetEquipment()) + foreach (UnitEquipmentItem item in GetEquipment()) { if (item.IsRequired) { @@ -292,10 +305,10 @@ { return ""; } - + public void SetEquipmentAmount(UnitEquipmentItem equip, int amount) { - if (amount <1 && amount != WarFoundryCore.INFINITY) + if (amount < 1 && amount != WarFoundryCore.INFINITY) { amount = 0; } @@ -330,7 +343,7 @@ } } } - + private void AddEquipmentAmount(UnitEquipmentItem equip, int amount) { AbstractUnitEquipmentItemSelection newItem = new UnitEquipmentNumericSelection(this, equip, amount); @@ -345,12 +358,12 @@ selections.Add(newItem); } - + public void SetEquipmentRatio(UnitEquipmentItem equip, double ratio) { if (!equip.IsRatioLimit) { - throw new InvalidOperationException("Equipment with ID "+equip.ID+" for unit of type "+UnitType.ID+" has an absolute limit, not a ratio limit"); + throw new InvalidOperationException("Equipment with ID " + equip.ID + " for unit of type " + UnitType.ID + " has an absolute limit, not a ratio limit"); } if (ratio > 100) @@ -391,10 +404,10 @@ } } } - + private void AddEquipmentRatio(UnitEquipmentItem equip, double ratio) { - UnitEquipmentRatioSelection newItem = new UnitEquipmentRatioSelection (this, equip, ratio); + UnitEquipmentRatioSelection newItem = new UnitEquipmentRatioSelection(this, equip, ratio); equipment[equip] = newItem; List<AbstractUnitEquipmentItemSelection> selections = DictionaryUtils.GetValue(equipmentSlots, equip.SlotName); @@ -406,21 +419,21 @@ selections.Add(newItem); } - + private void RemoveEquipmentItem(UnitEquipmentItem equip) { double oldAmount = UnitEquipmentUtil.GetEquipmentAmount(this, equip); if (oldAmount != 0) { - AbstractUnitEquipmentItemSelection selection = DictionaryUtils.GetValue (equipment, equip); + AbstractUnitEquipmentItemSelection selection = DictionaryUtils.GetValue(equipment, equip); equipment.Remove(equip); - List<AbstractUnitEquipmentItemSelection> slotSelections = DictionaryUtils.GetValue (equipmentSlots, equip.SlotName); + List<AbstractUnitEquipmentItemSelection> slotSelections = DictionaryUtils.GetValue(equipmentSlots, equip.SlotName); slotSelections.Remove(selection); OnUnitEquipmentAmountChanged(equip, oldAmount, 0); } } - + public bool CanEquipWithItem(UnitEquipmentItem item) { string[] mutexes = item.MutexGroups; @@ -451,7 +464,7 @@ private void OnPointsValueChanged(double oldValue, double newValue) { - if (PointsValueChanged!=null) + if (PointsValueChanged != null) { PointsValueChanged(this, oldValue, newValue); } @@ -459,7 +472,7 @@ private void OnUnitSizeChanged(int oldValue, int newValue) { - if (UnitSizeChanged!=null) + if (UnitSizeChanged != null) { UnitSizeChanged(this, oldValue, newValue); } @@ -467,7 +480,7 @@ private void OnUnitEquipmentAmountChanged(UnitEquipmentItem equip, double oldValue, double newValue) { - if (UnitEquipmentAmountChanged!=null) + if (UnitEquipmentAmountChanged != null) { UnitEquipmentAmountChanged(equip, oldValue, newValue); } @@ -478,7 +491,7 @@ { get { return UnitType.UnitStatsArray; } } - + public Stat[][] UnitStatsArrays { get { return UnitType.UnitStatsArrays; } @@ -489,15 +502,15 @@ { get { return UnitType.UnitStatsArrayWithName; } } - + public Stat[][] UnitStatsArraysWithName { get { return UnitType.UnitStatsArraysWithName; } } - + public string[] UnitStatsArrayIDs { - get + get { return UnitType.UnitStatsArrayIDs; } @@ -508,7 +521,7 @@ return UnitType.GetStatValue(statName); } - public int GetEquipmentAmountInSlot (string slotName) + public int GetEquipmentAmountInSlot(string slotName) { int amount = 0; @@ -558,13 +571,13 @@ } } - private int GetSelectionTotal (List<AbstractUnitEquipmentItemSelection> selections) + private int GetSelectionTotal(List<AbstractUnitEquipmentItemSelection> selections) { int amount = 0; foreach (AbstractUnitEquipmentItemSelection selection in selections) { - amount+= selection.NumberTaken; + amount += selection.NumberTaken; } return amount; @@ -578,20 +591,20 @@ get { return UnitType.GetRequiredAbilities(); - } - } - - private void RefreshUnitEquipmentAmounts(WarFoundryObject obj, int oldValue, int newValue) - { - foreach (UnitEquipmentItem item in equipment.Keys) - { - AbstractUnitEquipmentItemSelection selection = equipment[item]; - - if (selection is UnitEquipmentRatioSelection) - { - OnUnitEquipmentAmountChanged(item, selection.AmountTaken, selection.AmountTaken); - } - } + } + } + + private void RefreshUnitEquipmentAmounts(WarFoundryObject obj, int oldValue, int newValue) + { + foreach (UnitEquipmentItem item in equipment.Keys) + { + AbstractUnitEquipmentItemSelection selection = equipment[item]; + + if (selection is UnitEquipmentRatioSelection) + { + OnUnitEquipmentAmountChanged(item, selection.AmountTaken, selection.AmountTaken); + } + } } } }