view FrmAddEquipment.cs @ 56:0c5fbb54bfb0

Re #60: Add UI to add/remove/edit weapons in GTK * Refactor enabling/disabling of radio buttons * Handle null selections * Move Dispose() to be an interface method and move call out to controller
author IBBoard <dev@ibboard.co.uk>
date Wed, 25 Aug 2010 20:04:27 +0000
parents eb7db8495bb5
children 293d204e40db
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((UnitEquipmentItem)TreeUtils.GetSelectedItem(lstEquipment));
			}
		}

		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);
			rbEquipPercent.Sensitive = limitsEnabled;
			percentageAmount.Sensitive = limitsEnabled;
			rbEquipAll.Sensitive = limitsEnabled;
			lblEquipAll.Sensitive = limitsEnabled;
		}

		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);
		}		
	}
}