view FrmAddEquipment.cs @ 65:77448375d2f9

Re #60: Add UI to add/remove/edit weapons in GTK * Add titles and make dialogs not appear on task list * Capture ratio amounts in Control object's local variable, otherwise we get 0s * Set initial values when editing equipment * Only enable the Edit button when we can edit
author IBBoard <dev@ibboard.co.uk>
date Sat, 04 Sep 2010 10:25:39 +0000
parents c2d79b4209e3
children 4b82515586ac
line wrap: on
line source

//  This file (FrmAddEquipment.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.WarFoundry.GUI.GTK.UIControl.Interfaces;
using IBBoard.WarFoundry.API.Objects;
using Gtk;
using IBBoard.WarFoundry.GUI.GTK.Util;
using IBBoard.GtkSharp;
using log4net.Repository.Hierarchy;
using log4net;
namespace IBBoard.WarFoundry.GUI.GTK
{
	public partial class FrmAddEquipment : Dialog, IAddEquipmentUI
	{
		private static ILog log = LogManager.GetLogger(typeof(FrmAddEquipment));
		
		public event SingleArgMethodInvoker<UnitEquipmentItem> UnitEquipmentItemChoiceChanged;
		public event MethodInvoker UnitEquipmentAmountTypeChanged;
		public event MethodInvoker UnitEquipmentAmountChanged;
		
		private bool limitsEnabled = false;
		private bool ratioLimited = false;
		
		public FrmAddEquipment()
		{
			this.Build();
			lstEquipment.Selection.Changed += OnSelectionChanged;
			TreeViewColumn equipColumn = new TreeViewColumn();
			equipColumn.Title = "Equipment";
			CellRendererText equipCell = new CellRendererText();
			equipColumn.PackStart(equipCell, true);
			equipColumn.SetCellDataFunc(equipCell, GtkWarFoundryUtil.RenderWarFoundryObjectName);
			lstEquipment.AppendColumn(equipColumn);			
		}
		
		private void OnUnitEquipmentAmountChanged()
		{
			if (UnitEquipmentAmountChanged != null)
			{
				UnitEquipmentAmountChanged();
			}
		}
		
		private void OnUnitEquipmentAmountTypeChanged()
		{
			if (UnitEquipmentAmountChanged != null)
			{
				UnitEquipmentAmountTypeChanged();
			}
		}
		
		protected void OnSelectionChanged(object o, EventArgs e)
		{
			if (UnitEquipmentItemChoiceChanged!=null)
			{
				UnitEquipmentItemChoiceChanged(SelectedUnitEquipmentItem);
			}
		}

		public void SetUnitEquipmentItems(UnitEquipmentItem[] items)
		{
			ListStore store = new ListStore(typeof(UnitEquipmentItem));
			
			foreach (UnitEquipmentItem equipItem in items)
			{
				store.AppendValues(equipItem);
			}
			
			lstEquipment.Model = store;
		}

		public void SetUnitEquipmentLimits(bool isRatioLimit, double minPercent, double maxPercent, int minNumber, int maxNumber)
		{
			log.DebugFormat("IsRatio? {0}. Limits: {1}->{2}, {3}%->{4}%", isRatioLimit, minNumber, maxNumber, minPercent, maxPercent);
			ratioLimited = isRatioLimit;
			numericAmount.SetRange(minNumber, maxNumber);
			percentageAmount.SetRange(minPercent, maxPercent);
			
			if (isRatioLimit)
			{
				if (minPercent == 100)
				{
					rbEquipAll.Active = true;
				}
				else
				{
					rbEquipPercent.Active = true;
				}
			}
			else
			{
				rbEquipNumeric.Active = true;
			}
		}

		public void SetUnitEquipmentLimitsEnabled(bool isEnabled)
		{
			SetNumericAmountEnabledState(isEnabled);
			SetPercentageAmountEnabledState(isEnabled);
		}

		public bool ShowControl()
		{
			int result = Run();
			bool okayClicked = (result == (int)ResponseType.Ok);
			this.Hide();
			return okayClicked;
		}
		
		protected virtual void CancelButtonClicked(object sender, System.EventArgs e)
		{
			log.Debug("Cancel clicked");
			Respond(ResponseType.Cancel);
		}	
		
		protected virtual void OkayButtonClicked(object sender, System.EventArgs e)
		{
			log.Debug("Okay clicked");
			Respond(ResponseType.Ok);
		}		
		
		public void SetOkayEnabledState (bool enabled)
		{
			buttonOk.Sensitive = enabled;
		}
				
		protected virtual void SpinButtonValueChanged (object sender, System.EventArgs e)
		{
			OnUnitEquipmentAmountChanged();
		}		
		
		protected virtual void RadioButtonClicked(object sender, System.EventArgs e)
		{
			OnUnitEquipmentAmountTypeChanged();
		}
		
		public void SetNumericAmountEnabledState (bool enabled)
		{
			rbEquipNumeric.Sensitive = enabled;
			numericAmount.Sensitive = enabled;
		}		
		
		public void SetPercentageAmountEnabledState(bool enabled)
		{
			if (enabled)
			{
				double minPercentage = GetMinPercentage();
				rbEquipPercent.Sensitive = minPercentage != 100;
				percentageAmount.Sensitive = minPercentage != 100;
				double maxPercentage = GetMaxPercentage();
				rbEquipAll.Sensitive = ratioLimited && maxPercentage == 100;
				lblEquipAll.Sensitive = ratioLimited && maxPercentage == 100;
			}
			else
			{
				rbEquipPercent.Sensitive = false;
				percentageAmount.Sensitive = false;
				rbEquipAll.Sensitive = false;
				lblEquipAll.Sensitive = false;
			}
		}

		private double GetMaxPercentage()
		{
			double min, max;
			percentageAmount.GetRange(out min, out max);
			return max;
		}

		private double GetMinPercentage()
		{
			double min, max;
			percentageAmount.GetRange(out min, out max);
			return min;
		}
		
		public UnitEquipmentItem SelectedUnitEquipmentItem
		{
			get
			{
				return (UnitEquipmentItem)TreeUtils.GetSelectedItem(lstEquipment);
			}
		}
		
		
		public bool IsRatioEquipmentAmount
		{
			get
			{
				return !rbEquipNumeric.Active;
			}
		}
		
		
		public int EquipmentNumericAmount
		{
			get
			{
				return (int)numericAmount.Value;
			}
			
			set
			{
				numericAmount.Value = value;
			}
		}
		
		
		public double EquipmentPercentageAmount
		{
			get
			{
				double percent;
				
				if (rbEquipAll.Active)
				{
					percent = 100;
				}
				else
				{
					percent = percentageAmount.Value;
				}
				
				return percent;
			}
			
			set
			{
				percentageAmount.Value = value;
			}
		}
	}
}