view FrmNewArmy.cs @ 49:d29ad8445461

Re #86: Complete GTK# UI * Make keypad "enter" work for confirming unit name
author IBBoard <dev@ibboard.co.uk>
date Sun, 15 Aug 2010 15:57:25 +0000
parents d68992a831df
children 4bad8cb3f889 4a33b3012100
line wrap: on
line source

// This file (FrmNewArmy.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.Lang;
using IBBoard.WarFoundry.API;
using IBBoard.WarFoundry.API.Objects;
using IBBoard.GtkSharp;
using log4net;

namespace IBBoard.WarFoundry.GTK
{	
	public partial class FrmNewArmy : Dialog, ITranslatable
	{
		private ILog logger = LogManager.GetLogger(typeof(FrmNewArmy));
		private Race race;
		private string armyName;
		private int pointsValue;
		
		public FrmNewArmy(GameSystem gameSystem)
		{
			this.Build();
			
			GameSystem[] gameSystems = WarFoundryLoader.GetDefault().GetGameSystems();
			ComboBoxUtils.FillCombo(systemCombo, gameSystems, delegate(GameSystem sys){return sys.Name;});
			
			if (gameSystem != null)
			{
				ComboBoxUtils.SelectItem(systemCombo, gameSystem);
			}
			else if (gameSystems.Length == 1)
			{
				ComboBoxUtils.SelectIndex(systemCombo, 0);
			}
			
			lstRaces.Selection.Changed+= new EventHandler(OnSelectionChanged);
			
			TreeViewColumn raceColumn = new TreeViewColumn ();
			raceColumn.Title = "Race";
			CellRendererText raceCell = new CellRendererText ();
			raceColumn.PackStart (raceCell, true);
			lstRaces.AppendColumn(raceColumn);
			raceColumn.SetCellDataFunc(raceCell, new TreeCellDataFunc(RenderRaceName));

			
			if (gameSystem!=null)
			{
				SetRaces(gameSystem);
			}
		}
		
		public string Text
		{
			get { return Title; }
			set { Title = value; }
		}
		
		private void RenderRaceName(TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter)
		{
			Race r = (Race)model.GetValue(iter, 0);
			(cell as CellRendererText).Text = r.Name;
		}

		protected virtual void OnSelectionChanged(object o, EventArgs e)
		{
			logger.Debug("Race selection changed");
			SetOkayButtonState();
		}
		
		private void SetOkayButtonState()
		{
			bttnCreate.Sensitive = (lstRaces.Selection.CountSelectedRows() == 1 && txtArmyName.Text!="" && sbPointsValue.Value > 0);
		}

		protected virtual void OnCreateClicked (object sender, System.EventArgs e)
		{
			TreeModel model;
			TreeIter iter;
			lstRaces.Selection.GetSelected (out model, out iter);
			race = (Race) model.GetValue(iter, 0);
			armyName = txtArmyName.Text;
			pointsValue = (int)sbPointsValue.Value;
			Respond(ResponseType.Ok);
		}

		protected virtual void OnCancelClicked (object sender, System.EventArgs e)
		{
			Respond(ResponseType.Cancel);
		}

		protected virtual void OnTextChanged (object sender, System.EventArgs e)
		{
			SetOkayButtonState();
		}

		protected virtual void OnSpinChangeValue (object o, Gtk.ChangeValueArgs args)
		{
			SetOkayButtonState();
		}

		protected virtual void OnSpinValueChanged (object sender, System.EventArgs e)
		{
			SetOkayButtonState();
		}
		
		protected virtual void OnSystemComboChanged (object sender, System.EventArgs e)
		{
			GameSystem system = ComboBoxUtils.GetSelectedItem<GameSystem>(systemCombo);
			SetRaces(system);
			logger.Debug("System selection changed: " + (system == null ? "null" : system.Name));
			SetOkayButtonState();
		}
		
		private void SetRaces(GameSystem system)
		{	
			ListStore store = new ListStore(typeof(Race));
			
			if (system != null)
			{
				foreach (Race race in WarFoundryLoader.GetDefault().GetRaces(system))
				{
					store.AppendValues(race);
				}
			}
			
			lstRaces.Model = store;
		}
		
		public Race SelectedRace
		{
			get { return race; }
		}
		
		public string ArmyName
		{
			get { return armyName; }
		}
		
		public int ArmySize 
		{
			get { return pointsValue; }
		}
	}
}