diff FrmNewUnit.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/FrmNewUnit.cs	Fri Dec 19 15:57:51 2008 +0000
@@ -0,0 +1,133 @@
+// FrmNewUnit.cs
+//
+//  Copyright (C) 2007 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 System.Collections.Generic;
+using IBBoard.WarFoundry.API.Objects;
+using IBBoard.WarFoundry.API.Requirements;
+using Gtk;
+using log4net;
+
+namespace IBBoard.WarFoundry
+{
+	public partial class FrmNewUnit : Gtk.Dialog
+	{
+		private ILog logger = LogManager.GetLogger(typeof(FrmNewUnit));
+		
+		private UnitType unitType;
+		private Army unitArmy;
+		
+		public FrmNewUnit(Race race, Category cat, Army army)
+		{
+			this.Build();
+			unitArmy = army;
+			
+			TreeViewColumn unitTypeColumn = new TreeViewColumn ();
+			unitTypeColumn.Title = "Unit Type";
+			CellRendererText unitTypeCell = new CellRendererText ();
+			unitTypeColumn.PackStart(unitTypeCell, true);
+			lstUnitTypes.AppendColumn(unitTypeColumn);
+			unitTypeColumn.SetCellDataFunc (unitTypeCell, new TreeCellDataFunc(RenderUnitTypeName));
+			ListStore store = new ListStore(typeof(UnitType));
+			UnitType[] types = race.GetUnitTypes(cat);
+			logger.DebugFormat("Listing {0} unit types in {1}", types.Length, cat.Name);
+			
+			foreach (UnitType type in types)
+			{
+				logger.DebugFormat("Adding unit type {0}", type.Name);
+				store.AppendValues(type);
+			}
+			
+			lstUnitTypes.Model = store;
+			lstUnitTypes.Selection.Changed+= new EventHandler(OnSelectionChanged);
+		}
+		
+		private void RenderUnitTypeName(TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter)
+		{
+			UnitType type = (UnitType)model.GetValue(iter, 0);
+			(cell as CellRendererText).Text = type.Name;
+		}
+		
+		protected virtual void OnSelectionChanged(object o, EventArgs e)
+		{
+			SetSelectUnitEnabledVal();
+		}
+		
+		public UnitType SelectedUnit
+		{
+			get { return unitType; }
+		}
+		
+		private UnitType GetSelectedUnitType()
+		{		
+			TreeModel model;
+			TreeIter iter;
+			lstUnitTypes.Selection.GetSelected (out model, out iter);
+			UnitType toReturn = null;
+			
+			if (model!=null)
+			{
+				toReturn = (UnitType) model.GetValue(iter, 0);
+			}
+			
+			return toReturn;
+		}
+		
+		private void SetSelectUnitEnabledVal()
+		{
+			UnitType type = GetSelectedUnitType();
+			
+			if (type!=null)
+			{
+				buttonOk.Sensitive = true;
+				List<FailedUnitRequirement> fails = unitArmy.CanAddUnitType(type);
+				lblNewUnitWarning.Visible = (fails != null);
+
+				if (fails.Count > 0)
+				{
+					//FIXME: currently only show the first error
+					lblNewUnitWarning.Text = fails[0].Description;
+				}
+			}
+			else
+			{
+				buttonOk.Sensitive = false;
+				lblNewUnitWarning.Visible = false;
+			}
+		}
+
+		protected virtual void OnButtonCancelActivated (object sender, System.EventArgs e)
+		{
+			Respond(ResponseType.Cancel);
+		}
+
+		protected virtual void OnRowActivated (object o, Gtk.RowActivatedArgs args)
+		{
+			unitType = GetSelectedUnitType();
+			Respond(ResponseType.Ok);
+		}
+
+		protected virtual void OnButtonOkClicked (object sender, System.EventArgs e)
+		{
+			unitType = GetSelectedUnitType();
+			Respond(ResponseType.Ok);
+		}
+	}
+}