diff UIControl/EditEquipmentUIControl.cs @ 64:e3fe48c4d794

Re #60: Add UI to add/remove/edit weapons in GTK * Add most of basic "edit" interface, based on "add" interface TODO: * Set initial values * Warn when setting to 0
author IBBoard <dev@ibboard.co.uk>
date Thu, 02 Sep 2010 20:12:21 +0000
parents
children 77448375d2f9
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/UIControl/EditEquipmentUIControl.cs	Thu Sep 02 20:12:21 2010 +0000
@@ -0,0 +1,205 @@
+//  This file (EditEquipmentUIControl.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;
+using IBBoard.WarFoundry.GUI.GTK;
+namespace IBBoard.WarFoundry.GUI.GTK.UIControl
+{
+	public class EditEquipmentUIControl
+	{
+		private CommandStack commandStack;
+		private IEditEquipmentUI ui;
+		private Unit unit;
+		private UnitEquipmentItem equipItem;
+		private double minPercentage, maxPercentage;
+		private int minNumber, maxNumber;
+		private bool isRatioAmount;
+		private double equipmentAmount;
+		
+		
+		public EditEquipmentUIControl(Unit unit, UnitEquipmentItem item, CommandStack commandStack)
+		{
+			this.unit = unit;
+			this.commandStack = commandStack;
+			this.equipItem = item;
+			SetupUI();
+		}
+		
+		private void SetupUI()
+		{
+			CreateEquipmentUI();
+			ui.SetOkayEnabledState(false);
+			isRatioAmount = UnitEquipmentUtil.IsEquipmentRatioLimited(unit, equipItem);
+			maxPercentage = GetMaxPercentageLimit(equipItem);
+			minPercentage = GetMinPercentageLimit(equipItem);
+			maxNumber = GetMaxNumericLimit(equipItem);
+			minNumber = GetMinNumericLimit(equipItem);
+			
+			ui.SetUnitEquipmentLimits(isRatioAmount, minPercentage, maxPercentage, minNumber, maxNumber);
+			ui.SetUnitEquipmentLimitsEnabled(true);
+			ui.SetOkayEnabledState(HasNonZeroEquipmentAmount());
+			SetEquipmentAmountControlEnabledStates();
+			
+			//TODO: Set initial values
+			
+			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 FrmEditEquipment();
+		}
+
+		private void SetEquipmentAmountControlEnabledStates()
+		{
+			ui.SetNumericAmountEnabledState(!isRatioAmount);
+			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();
+		}
+	}
+}
+