Mercurial > repos > IBBoard.WarFoundry.GUI.GTK
view UIControl/AddEquipmentUIControl.cs @ 63:c2d79b4209e3
* FrmAddEquipment.cs: Re #60: Add UI to add/remove/edit weapons in
GTK
* Cache more values in the controller so that we don't rely on the UI
as a the model/data store
* Move enable/disable of amount controls to controller (assumes
minimum numeric and percentage, so implementation has its own custom
implementation to handle "Equip All")
* AddEquipmentUIControl.cs:
* IAddEquipmentUI.cs:
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Mon, 30 Aug 2010 19:44:35 +0000 |
parents | f733073967a2 |
children | e3fe48c4d794 |
line wrap: on
line source
// This file (AddEquipmentUI.cs) is a part of the IBBoard.WarFoundry.GUI.GTK project and is copyright 2010 IBBoard // // // The file and the library/program it is in are licensed and distributed, without warranty, under the GNU Affero GPL license, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license. using System; using IBBoard.Commands; using IBBoard.WarFoundry.API.Objects; using IBBoard.WarFoundry.API.Util; using IBBoard.WarFoundry.GUI.GTK.UIControl.Interfaces; using IBBoard.WarFoundry.API.Commands; using CustomMath = IBBoard.CustomMath; using IBBoard.Lang; namespace IBBoard.WarFoundry.GUI.GTK.UIControl { public class AddEquipmentUIControl { private CommandStack commandStack; private IAddEquipmentUI ui; private Unit unit; private UnitEquipmentItem equipItem; private double minPercentage, maxPercentage; private int minNumber, maxNumber; private bool isRatioAmount; private double equipmentAmount; public AddEquipmentUIControl(Unit unit, CommandStack commandStack) { this.unit = unit; this.commandStack = commandStack; SetupUI(); } private void SetupUI() { CreateEquipmentUI(); ui.SetUnitEquipmentLimitsEnabled(false); UnitEquipmentItem[] items = Arrays.Subtract(UnitEquipmentUtil.GetAllowedEquipmentItems(unit), unit.GetEquipment()); ui.SetUnitEquipmentItems(items); ui.SetOkayEnabledState(false); ui.UnitEquipmentItemChoiceChanged += HandleUiUnitEquipmentItemChoiceChanged; ui.UnitEquipmentAmountChanged += HandleUnitEquipmentAmountChanged; ui.UnitEquipmentAmountTypeChanged += HandleUnitEquipmentAmountChanged; } private void HandleUnitEquipmentAmountChanged() { ui.SetOkayEnabledState(equipItem != null && HasNonZeroEquipmentAmount()); isRatioAmount = ui.IsRatioEquipmentAmount; if (isRatioAmount) { double equipmentAmount = ui.EquipmentPercentageAmount; SetEquipmentAmountsFromPercentage(equipmentAmount); } else { int equipmentIntAmount = ui.EquipmentNumericAmount; equipmentAmount = equipmentIntAmount; SetEquipmentAmountsFromNumber(equipmentIntAmount); } } private void SetEquipmentAmountsFromPercentage(double equipAmount) { if (equipAmount > maxPercentage) { string percentageTooLarge = Translation.GetTranslation("equipPercentageTooLarge", "the current percentage ({0}%) was larger than the maximum for the equipment item ({1}%) - the maximum value will be used instead", equipAmount, maxPercentage); string percentageTooLargeTitle = Translation.GetTranslation("equipPercentageTooLargeTitle", "equipment percentage too large"); // MessageBox.Show(ParentForm, percentageTooLarge, percentageTooLargeTitle); equipAmount = maxPercentage; } else if (equipAmount < minPercentage) { string percentageTooSmall = Translation.GetTranslation("equipPercentageTooSmall", "the current percentage ({0}%) was smaller than the minimum for the equipment item ({1}%) - the minimum value will be used instead", equipAmount, minPercentage); string percentageTooSmallTitle = Translation.GetTranslation("equipPercentageTooSmallTitle", "equipment percentage too small"); // MessageBox.Show(ParentForm, percentageTooSmall, percentageTooSmallTitle); equipAmount = minPercentage; } ui.EquipmentNumericAmount = CalculateNumericValueFromPercentage(equipAmount); ui.EquipmentPercentageAmount = equipAmount; } private int CalculateNumericValueFromPercentage(double percent) { int calcedAmount = (int)CustomMath.IBBMath.Round((unit.Size * (percent / 100.0)), equipItem.RoundNumberUp); return Math.Min(Math.Max(calcedAmount, minNumber), maxNumber); } private void SetEquipmentAmountsFromNumber(int equipAmount) { if (equipAmount > maxNumber) { string amountTooLarge = Translation.GetTranslation("equipNumberTooLarge", "the current amount ({0}) was larger than the maximum for the equipment item ({1}) - the maximum value will be used instead", equipAmount, maxNumber); string amountTooLargeTitle = Translation.GetTranslation("equipNumberTooLargeTitle", "equipment amount too large"); //MessageBox.Show(ParentForm, amountTooLarge, amountTooLargeTitle); equipAmount = maxNumber; } else if (equipAmount < minNumber) { string amountTooSmall = Translation.GetTranslation("equipNumberTooSmall", "the current amount ({0}) was smaller than the minimum for the equipment item ({1}) - the minimum value will be used instead", equipAmount, minNumber); string amountTooSmallTitle = Translation.GetTranslation("equipNumberTooSmallTitle", "equipment amount too small"); //MessageBox.Show(ParentForm, amountTooSmall, amountTooSmallTitle); equipAmount = minNumber; } ui.EquipmentPercentageAmount = CalcualtePercentageValueFromNumber(equipAmount); ui.EquipmentNumericAmount = equipAmount; } private double CalcualtePercentageValueFromNumber(int number) { double calcedAmount = RoundPercentage(CustomMath.IBBMath.Percentage(number, unit.Size)); return Math.Min(Math.Max(calcedAmount, minPercentage), maxPercentage); } //TODO Make abstract protected void CreateEquipmentUI() { ui = new FrmAddEquipment(); } private void HandleUiUnitEquipmentItemChoiceChanged(UnitEquipmentItem equip) { equipItem = equip; if (equip != null) { bool equipIsRatioLimit = UnitEquipmentUtil.IsEquipmentRatioLimited(unit, equip); maxPercentage = GetMaxPercentageLimit(equip); minPercentage = GetMinPercentageLimit(equip); maxNumber = GetMaxNumericLimit(equip); minNumber = GetMinNumericLimit(equip); ui.SetUnitEquipmentLimits(equipIsRatioLimit, minPercentage, maxPercentage, minNumber, maxNumber); ui.SetUnitEquipmentLimitsEnabled(true); ui.SetOkayEnabledState(HasNonZeroEquipmentAmount()); SetEquipmentAmountControlEnabledStates(equipIsRatioLimit); } else { maxPercentage = minPercentage = 0; maxNumber = minNumber = 0; ui.SetUnitEquipmentLimits(false, minPercentage, maxPercentage, minNumber, maxNumber); ui.SetUnitEquipmentLimitsEnabled(false); ui.SetOkayEnabledState(false); } } private void SetEquipmentAmountControlEnabledStates(bool ratioLimited) { ui.SetNumericAmountEnabledState(!ratioLimited); ui.SetPercentageAmountEnabledState(true); } private double GetMaxPercentageLimit(UnitEquipmentItem equip) { double maxPercent = RoundPercentage(UnitEquipmentUtil.GetMaxEquipmentPercentage(unit, equip)); return Math.Max(0, maxPercent); } private double GetMinPercentageLimit(UnitEquipmentItem equip) { double minPercent = RoundPercentage(UnitEquipmentUtil.GetMinEquipmentPercentage(unit, equip)); return Math.Max(0, minPercent); } private int GetMaxNumericLimit(UnitEquipmentItem equip) { int maxNumber = UnitEquipmentUtil.GetMaxEquipmentCount(unit, equip); return Math.Max(0, maxNumber); } private int GetMinNumericLimit(UnitEquipmentItem equip) { int minNumber = UnitEquipmentUtil.GetMinEquipmentCount(unit, equip); return Math.Max(0, minNumber); } private bool HasNonZeroEquipmentAmount() { bool nonZero; if (isRatioAmount) { nonZero = (ui.EquipmentPercentageAmount > 0); } else { nonZero = (ui.EquipmentNumericAmount > 0); } return nonZero; } private double RoundPercentage(double percent) { return Math.Round(percent, 1); } public void Show() { bool okayed = ui.ShowControl(); if (okayed) { if (isRatioAmount) { commandStack.Execute(new SetUnitEquipmentRatioAmountCommand(unit, equipItem, equipmentAmount)); } else { commandStack.Execute(new SetUnitEquipmentNumericAmountCommand(unit, equipItem, (int)equipmentAmount)); } } ui.Dispose(); } } }