view Widgets/UnitDisplayWidget.cs @ 52:4bad8cb3f889

Re #60: Add UI to add/remove/edit weapons in GTK * Remove "not implemented" exceptions and add first implementation of setting equipment list * Add widgets to add equipment form * Subscribe to Clicked instead of Activated to hook on to correct event * Extract tree rendering method into helper class Also: * Improve exception handling of unhandled exceptions (print full stack trace with "caused by" hierarchy)
author IBBoard <dev@ibboard.co.uk>
date Sun, 22 Aug 2010 14:32:16 +0000
parents dafbd432ca23
children 28b242612ad7
line wrap: on
line source

// This file (UnitDisplayWidget.cs) is a part of the IBBoard.WarFoundry.GTK project and is copyright 2008, 2009 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 Gtk;
using IBBoard.Commands;
using IBBoard.Lang;
using IBBoard.WarFoundry.API;
using IBBoard.WarFoundry.API.Objects;
using IBBoard.WarFoundry.API.Commands;
using IBBoard.WarFoundry.GUI.GTK.UIControl;

namespace IBBoard.WarFoundry.GTK.Widgets
{
	[System.ComponentModel.Category("WarFoundry GTK# GUI")]
	[System.ComponentModel.ToolboxItem(true)]
	public partial class UnitDisplayWidget : Gtk.Bin
	{		
		private IBBoard.WarFoundry.API.Objects.Unit unit;
		private CommandStack stack;
		
		public UnitDisplayWidget(IBBoard.WarFoundry.API.Objects.Unit sourceUnit, CommandStack commandStack)
		{
			this.Build();
			stack = commandStack;
			unit = sourceUnit;
			unitName.Text = unit.Name;
			unitSize.Value = unit.Size;
			notesView.Buffer.Text = unit.UnitType.Notes;
			double max = unit.UnitType.MaxSize;
			
			if (max == -1)
			{
				max = double.MaxValue;
			}
			
			unitSize.SetRange(unit.UnitType.MinSize, max);
			unit.NameChanged+= new StringValChangedDelegate(UnitNameChanged);
			unit.UnitSizeChanged+= new IntValChangedDelegate(UnitSizeChanged);
			SetStats();
		}

		private void SetStats()
		{
            //GameSystem system = unit.Army.GameSystem;
            //SystemStats stats = system.StandardSystemStats;
            CellRendererText renderer = new CellRendererText();
            unitStats.AppendColumn(Translation.GetTranslation("UnitNameColumn", "Unit Type", null), renderer, new TreeCellDataFunc(RenderUnitName));
            
            TreeCellDataFunc statFunc = new TreeCellDataFunc(RenderUnitStat);
            Stat[] stats = unit.UnitStatsArray;
			
			int length = stats.Length;

			for (int i = 0; i < length; i++)
			{
	            unitStats.AppendColumn(stats[i].ParentSlotName, renderer, statFunc);
			}

			TreeStore model = new TreeStore(typeof(IBBoard.WarFoundry.API.Objects.Unit));
			model.AppendValues(unit);
			unitStats.Model = model;
		}
		
		private void RenderUnitName(TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter)
		{
			object o = model.GetValue(iter, 0);
			
			if (o is IBBoard.WarFoundry.API.Objects.Unit)
			{
				IBBoard.WarFoundry.API.Objects.Unit u = (IBBoard.WarFoundry.API.Objects.Unit)o;				
				(cell as CellRendererText).Text = u.UnitType.Name;
			}
		}
		
		private void RenderUnitStat(TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter)
		{
			object o = model.GetValue(iter, 0);
			
			if (o is IBBoard.WarFoundry.API.Objects.Unit)
			{
				IBBoard.WarFoundry.API.Objects.Unit u = (IBBoard.WarFoundry.API.Objects.Unit)o;
				(cell as CellRendererText).Text = u.GetStatValue(column.Title);
			}
		}

		public IBBoard.WarFoundry.API.Objects.Unit Unit
		{
			get { return unit; }
		}
		
		private void UnitNameChanged(WarFoundryObject obj, string oldValue, string newValue)
		{
			unitName.Text = newValue;
		}
		
		private void UnitSizeChanged(WarFoundryObject obj, int oldValue, int newValue)
		{
			unitSize.Value = newValue;
		}

		protected virtual void OnUnitSizeFocusOut (object o, Gtk.FocusOutEventArgs args)
		{
			SetNewUnitSize();
		}

		[GLib.ConnectBefore ()]
		protected virtual void OnUnitSizeKeyPress (object o, Gtk.KeyPressEventArgs args)
		{
			if (args.Event.Key == Gdk.Key.Return || args.Event.Key == Gdk.Key.KP_Enter)
			{
				SetNewUnitSize();
			}
		}
		
		private void SetNewUnitSize()
		{
			if (unitSize.Value!=unit.Size)
			{
				SetUnitSizeCommand cmd = new SetUnitSizeCommand(unit, (int)Math.Round(unitSize.Value));
				stack.Execute(cmd);
			}
		}

		protected virtual void OnUnitNameFocusOut (object o, Gtk.FocusOutEventArgs args)
		{
			SetNewUnitName();
		}

		[GLib.ConnectBefore ()]
		protected virtual void OnUnitNameKeyPress (object o, Gtk.KeyPressEventArgs args)
		{
			if (args.Event.Key == Gdk.Key.Return || args.Event.Key == Gdk.Key.KP_Enter)
			{
				SetNewUnitName();
			}
		}
		
		private void SetNewUnitName()
		{
			if (unitName.Text != unit.Name)
			{
				SetNameCommand cmd = new SetNameCommand(unit, unitName.Text);
				stack.Execute(cmd);
			}
		}
		
		private void OnBttnAddEquipmentClicked(object sender, System.EventArgs e)
		{
			AddEquipment();
		}
		
		private void AddEquipment()
		{
			AddEquipmentUIControl addEquipment = new AddEquipmentUIControl(unit, stack);
			addEquipment.Show();
		}
	}
}