# HG changeset patch # User IBBoard # Date 1293984289 0 # Node ID 5c4b3d91795fd71c5b987fcaecf4848c9c4d6ee5 # Parent 3e74bf7b0f725144ae64c268c40d988d53408725 * Add extra notification required by #308 to make sure that unit name changes when translation changes in GTK# UI no-open-ticket diff -r 3e74bf7b0f72 -r 5c4b3d91795f api/Objects/Unit.cs --- a/api/Objects/Unit.cs Sun Jan 02 15:59:13 2011 +0000 +++ b/api/Objects/Unit.cs Sun Jan 02 16:04:49 2011 +0000 @@ -25,33 +25,38 @@ private Dictionary equipment = new Dictionary(); private Dictionary> equipmentSlots = new Dictionary>(); private List containedUnits = new List(); + 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 list = new List(); - 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 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 slotSelections = DictionaryUtils.GetValue (equipmentSlots, equip.SlotName); + List 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 selections) + private int GetSelectionTotal(List 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); + } + } } } }