view FrmAddEquipment.cs @ 58:7bba99c368c8

Re #60: Add UI to add/remove/edit weapons in GTK * Fix GTK error about tree model columns by populating the store correctly
author IBBoard <dev@ibboard.co.uk>
date Thu, 26 Aug 2010 19:34:57 +0000
parents 293d204e40db
children e7ad676a7344
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;
		
		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);
		}
		
		protected virtual 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;
			SetEnabledState();
			numericAmount.SetRange(minNumber, maxNumber);
			percentageAmount.SetRange(minPercent, maxPercent);
		}

		public void SetUnitEquipmentLimitsEnabled(bool isEnabled)
		{
			limitsEnabled = isEnabled;
			SetEnabledState();
		}
		
		private void SetEnabledState()
		{
			rbEquipNumeric.Sensitive = (limitsEnabled && !ratioLimited);
			numericAmount.Sensitive = (limitsEnabled && !ratioLimited);
			double minPercentage = GetMinPercentage();
			rbEquipPercent.Sensitive = limitsEnabled && minPercentage != 100;
			percentageAmount.Sensitive = limitsEnabled && minPercentage != 100;
			double maxPercentage = GetMaxPercentage();
			rbEquipAll.Sensitive = limitsEnabled && maxPercentage == 100;
			lblEquipAll.Sensitive = limitsEnabled && maxPercentage == 100;
		}
		
		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 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 UnitEquipmentItem SelectedUnitEquipmentItem
		{
			get
			{
				return (UnitEquipmentItem)TreeUtils.GetSelectedItem(lstEquipment);
			}
		}
		
		
		public bool IsRatioEquipmentAmount
		{
			get
			{
				return !rbEquipNumeric.Active;
			}
		}
		
		
		public int EquipmentNumericAmount
		{
			get
			{
				return (int)numericAmount.Value;
			}
		}
		
		
		public double EquipmentPercentageAmount
		{
			get
			{
				double percent;
				
				if (rbEquipAll.Active)
				{
					percent = 100;
				}
				else
				{
					percent = percentageAmount.Value;
				}
				
				return percent;
			}
		}
	}
}