view UIControl/AddEquipmentUIControl.cs @ 60:04c0f6a7625c

Re #60: Add UI to add/remove/edit weapons in GTK * Implement Remove button * Hack "code-behind" file again to make things work, until MD bug is fixed
author IBBoard <dev@ibboard.co.uk>
date Fri, 27 Aug 2010 14:44:48 +0000
parents 293d204e40db
children e7ad676a7344
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;
namespace IBBoard.WarFoundry.GUI.GTK.UIControl
{
	public class AddEquipmentUIControl
	{
		private Unit unit;
		private CommandStack commandStack;
		private IAddEquipmentUI ui;
		
		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.UnitEquipmentItemChoiceChanged += HandleUiUnitEquipmentItemChoiceChanged;
		}
		
		//TODO Make abstract
		protected void CreateEquipmentUI()
		{
			ui = new FrmAddEquipment();
		}

		private void HandleUiUnitEquipmentItemChoiceChanged(UnitEquipmentItem equip)
		{
			if (equip != null)
			{
				bool equipIsRatioLimit = UnitEquipmentUtil.IsEquipmentRatioLimited(unit, equip);
				double maxPercent = RoundPercentage(UnitEquipmentUtil.GetMaxEquipmentPercentage(unit, equip));
				maxPercent = Math.Max(0, maxPercent);
				double minPercent = RoundPercentage(UnitEquipmentUtil.GetMinEquipmentPercentage(unit, equip));
				minPercent = Math.Max(0, minPercent);
				int maxNumber = UnitEquipmentUtil.GetMaxEquipmentCount(unit, equip);
				maxNumber = Math.Max(0, maxNumber);
				int minNumber = UnitEquipmentUtil.GetMinEquipmentCount(unit, equip);
				minNumber = Math.Max(0, minNumber);
			
				ui.SetUnitEquipmentLimits(equipIsRatioLimit, minPercent, maxPercent, minNumber, maxNumber);
				ui.SetUnitEquipmentLimitsEnabled(true);
			}
			else
			{
				ui.SetUnitEquipmentLimits(true, 0, 0, 0, 0);
				ui.SetUnitEquipmentLimitsEnabled(false);
			}
		}
		
		private double RoundPercentage(double percent)
		{
			return Math.Round(percent, 1);
		}

		public void Show()
		{
			bool okayed = ui.ShowControl();
			
			if (okayed)
			{
				UnitEquipmentItem equipItem = ui.SelectedUnitEquipmentItem;
			
				if (ui.IsRatioEquipmentAmount)
				{
					commandStack.Execute(new SetUnitEquipmentRatioAmountCommand(unit, equipItem, ui.EquipmentPercentageAmount));
				}
				else
				{
					commandStack.Execute(new SetUnitEquipmentNumericAmountCommand(unit, equipItem, ui.EquipmentNumericAmount));
				}
			}
			
			ui.Dispose();
		}
	}
}