view Widgets/UnitDisplayWidget.cs @ 64:e3fe48c4d794

Re #60: Add UI to add/remove/edit weapons in GTK * Add most of basic "edit" interface, based on "add" interface TODO: * Set initial values * Warn when setting to 0
author IBBoard <dev@ibboard.co.uk>
date Thu, 02 Sep 2010 20:12:21 +0000
parents 04c0f6a7625c
children 77448375d2f9
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 WFObjects = IBBoard.WarFoundry.API.Objects;
using IBBoard.WarFoundry.API.Commands;
using IBBoard.WarFoundry.GUI.GTK.UIControl;
using IBBoard.WarFoundry.API.Objects;
using IBBoard.WarFoundry.API.Util;
using IBBoard.GtkSharp;
using log4net;

namespace IBBoard.WarFoundry.GTK.Widgets
{
	[System.ComponentModel.Category("WarFoundry GTK# GUI")]
	[System.ComponentModel.ToolboxItem(true)]
	public partial class UnitDisplayWidget : Gtk.Bin
	{
		private static ILog log = LogManager.GetLogger(typeof(UnitDisplayWidget));
		
		private WFObjects.Unit unit;
		private CommandStack stack;
		
		public UnitDisplayWidget(WFObjects.Unit sourceUnit, CommandStack commandStack)
		{
			this.Build();
			stack = commandStack;
			unit = sourceUnit;
			unitName.Text = unit.Name;
			unitSize.Value = unit.Size;
			int maxSize = (unit.UnitType.MaxSize == WarFoundryCore.INFINITY ? int.MaxValue : unit.UnitType.MaxSize);
			int minSize = unit.UnitType.MinSize;
			unitSize.SetRange(minSize, maxSize);
			unitSize.Sensitive = (maxSize != minSize);
			notesView.Buffer.Text = unit.UnitType.Notes;
			unit.NameChanged += UnitNameChanged;
			unit.UnitSizeChanged += UnitSizeChanged;
			unit.UnitEquipmentAmountChanged += HandleUnitUnitEquipmentAmountChanged;
			equipmentList.Selection.Changed += HandleEquipmentListSelectionChanged;
			SetStats();
			SetWeapons();
		}

		private void HandleEquipmentListSelectionChanged (object sender, EventArgs e)
		{
			SetButtonsEnabledState();	
		}
		private void SetButtonsEnabledState()
		{			
			UnitEquipmentItem equipItem = GetSelectedEquipmentItem();
			bttnReplaceEquipment.Sensitive = (equipItem != null && equipItem.HasAlternatives());
			bttnEditEquipment.Sensitive = (equipItem != null);
			bttnRemoveEquipment.Sensitive = (equipItem != null && !equipItem.IsRequired);
		}
		private UnitEquipmentItem GetSelectedEquipmentItem()
		{
			return (UnitEquipmentItem)TreeUtils.GetSelectedItem(equipmentList);
		}

		private void SetStats()
		{
            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(WFObjects.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 WFObjects.Unit)
			{
				WFObjects.Unit u = (WFObjects.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 WFObjects.Unit)
			{
				WFObjects.Unit u = (WFObjects.Unit)o;
				(cell as CellRendererText).Text = u.GetStatValue(column.Title);
			}
		}

		private void SetWeapons()
		{
			CellRendererText renderer = new CellRendererText();
			equipmentList.AppendColumn("", renderer, new TreeCellDataFunc(RenderEquipmentLine));
			
			ListStore model = new ListStore(typeof(UnitEquipmentItem));
			
			foreach (UnitEquipmentItem item in unit.GetEquipment())
			{
				model.AppendValues(item);
			}
			
			equipmentList.Model = model;
		}
		
		public void RenderEquipmentLine(TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter)
		{
			object o = model.GetValue(iter, 0);
			
			if (o is UnitEquipmentItem)
			{
				UnitEquipmentItem item = (UnitEquipmentItem)o;
				(cell as CellRendererText).Text = GetUnitEquipmentText(item);
			}			
						
		}
		
		private string GetUnitEquipmentText(UnitEquipmentItem item)
		{
			string translation = "";

			if (item.Cost == 0)
			{
				translation = Translation.GetTranslation("equipmentAmountWithZeroCost", "{0} ({1} - free)", item.Name, GetAmountString(item));
			}
			else
			{
				translation = Translation.GetTranslation("equipmentAmountWithCost", "{0} ({1} at {2}pts each)", item.Name, GetAmountString(item), item.Cost);
			}

			return translation;
		}

		private string GetAmountString(UnitEquipmentItem item)
		{
			double amount = UnitEquipmentUtil.GetEquipmentAmount(unit, item);
			string amountString = "";
			
			if (UnitEquipmentUtil.GetEquipmentAmountIsRatio(unit, item))
			{
				int number = UnitEquipmentUtil.GetEquipmentAmountTaken(unit, item);
				
				if (amount == 100)
				{
					amountString = Translation.GetTranslation("equipmentChoiceAmountAll", "all ({1})", amount, number);
				}

				else
				{
					amountString = Translation.GetTranslation("equipmentChoiceAmountPercentage", "{0}% ({1})", amount, number);
				}
			}
			else
			{
				amountString = Translation.GetTranslation("equipmentChoiceAmountNumber", "{0}", amount);
			}
			
			return amountString;
		}

		public WFObjects.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;
		}

		private void HandleUnitUnitEquipmentAmountChanged(WarFoundryObject obj, double oldValue, double newValue)
		{
			if (oldValue == 0)
			{
				((ListStore)equipmentList.Model).AppendValues(obj);
			}
			else if (newValue == 0)
			{
				TreeIter treeIter = TreeUtils.GetItemIter(equipmentList, obj);
				((ListStore)equipmentList.Model).Remove(ref treeIter);
			}
			
			equipmentList.QueueDraw();
		}

		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();
		}
		
		protected virtual void HandleRemoveButtonActivated(object sender, System.EventArgs e)
		{
			UnitEquipmentItem item = GetSelectedEquipmentItem();
			log.Debug("Remove "+item);
				
			if (item != null)
			{
				SetUnitEquipmentNumericAmountCommand cmd = new SetUnitEquipmentNumericAmountCommand(unit, item, 0);
				stack.Execute(cmd);
			}
		}		
		
		protected virtual void HandleEditButtonClicked(object sender, System.EventArgs e)
		{
			UnitEquipmentItem item = GetSelectedEquipmentItem();
			log.Debug("Edit " + item);
			
			if (item != null)
			{
				EditEquipmentUIControl editEquipment = new EditEquipmentUIControl(unit, item, stack);
				editEquipment.Show();
			}
		}
		
		
	}
}