diff Widgets/UnitDisplayWidget.cs @ 0:1bb28f84d567

Initial commit of WarFoundry code
author IBBoard <dev@ibboard.co.uk>
date Fri, 19 Dec 2008 15:57:51 +0000
parents
children 65279b85446f
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Widgets/UnitDisplayWidget.cs	Fri Dec 19 15:57:51 2008 +0000
@@ -0,0 +1,157 @@
+// UnitDisplayWidget.cs
+//
+//  Copyright (C) 2008 IBBoard
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License version 2.1 of the License as published by the Free
+// Software Foundation.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
+//
+//
+
+using System;
+using Gtk;
+using IBBoard.Commands;
+using IBBoard.Lang;
+using IBBoard.WarFoundry.API;
+using IBBoard.WarFoundry.API.Objects;
+using IBBoard.WarFoundry.API.Commands;
+
+namespace IBBoard.WarFoundry.Widgets
+{
+	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;
+			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;
+            Stats stats = unit.UnitType.UnitStats;
+            CellRendererText renderer = new CellRendererText();
+            unitStats.AppendColumn(Translation.GetTranslation("UnitNameColumn", "Unit Type", null), renderer, new TreeCellDataFunc(RenderUnitName));
+            
+            TreeCellDataFunc statFunc = new TreeCellDataFunc(RenderUnitStat);
+			
+			int length = stats.StatCount;
+
+			for (int i = 0; i < length; i++)
+			{
+	            unitStats.AppendColumn(stats[i].ParentSlot.Name, 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;
+				//TODO Check for correct value based on the column
+				(cell as CellRendererText).Text = u.UnitStats[column.Title].SlotValueString;
+			}
+		}
+		
+		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)
+			{
+				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)
+			{
+				SetNewUnitName();
+			}
+		}
+		
+		private void SetNewUnitName()
+		{
+			if (unitName.Text!=unit.Name)
+			{
+				SetNameCommand cmd = new SetNameCommand(unit, unitName.Text);
+				stack.Execute(cmd);
+			}
+		}
+	}
+}