view FrmNewArmy.cs @ 152:b612d7137f4d

Re #417: Improve WarFoundry installation experience * Initial attempt at a script that tries to do some validation and fix Ubuntu problems
author IBBoard <dev@ibboard.co.uk>
date Tue, 08 May 2012 21:00:24 +0100
parents d4b726cec12c
children
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;
using IBBoard.WarFoundry.GUI.GTK.Util;
using IBBoard.GtkSharp.Translatable;

namespace IBBoard.WarFoundry.GUI.GTK
{	
	public partial class FrmNewArmy : TranslatableDialog, ITranslatable
	{
		private ILog logger = LogManager.GetLogger(typeof(FrmNewArmy));
		private Race race;
		private string armyName;
		private int pointsValue;
		private bool isDefaultArmyName = true;

		public FrmNewArmy(GameSystem gameSystem)
		{
			this.Build();
			
			GameSystem[] gameSystems = WarFoundryLoader.GetDefault().GetGameSystems();
			ComboBoxUtils.FillCombo(systemCombo, gameSystems, delegate(GameSystem sys){return sys.Name;});
			
			lstRaces.Selection.Changed += new EventHandler(OnSelectionChanged);
			
			TreeViewColumn raceColumn = new TreeViewColumn();
			raceColumn.Title = Translation.GetTranslation("frmNewArmyRaceColumn", "race");
			CellRendererText raceCell = new CellRendererText();
			raceColumn.PackStart(raceCell, true);
			lstRaces.AppendColumn(raceColumn);
			raceColumn.SetCellDataFunc(raceCell, GtkWarFoundryUtil.RenderWarFoundryObjectName);
			
			if (gameSystem != null)
			{
				ComboBoxUtils.SelectItem(systemCombo, gameSystem);
			}
			else if (gameSystems.Length == 1)
			{
				ComboBoxUtils.SelectIndex(systemCombo, 0);
			}
			
			Translation.TranslationChanged += Retranslate;
			Translate();
		}

		private void Retranslate()
		{
			Translate();
		}

		public override void Dispose()
		{
			Translation.TranslationChanged -= Retranslate;
			base.Dispose();
		}

		protected override void Translate()
		{
			base.Translate();
			ControlTranslator.TranslateButtons(bttnCreate, bttnCancel);
		}

		protected virtual void OnSelectionChanged(object o, EventArgs e)
		{
			logger.Debug("Race selection changed");
			SetArmyName();
			SetOkayButtonState();
		}

		private void SetOkayButtonState()
		{
			bttnCreate.Sensitive = (lstRaces.Selection.CountSelectedRows() == 1 && txtArmyName.Text != "" && sbPointsValue.Value > 0);
		}

		protected virtual void OnCreateClicked(object sender, System.EventArgs e)
		{
			race = GetSelectedRace();
			armyName = txtArmyName.Text;
			pointsValue = (int)sbPointsValue.Value;
			Respond(ResponseType.Ok);
		}

		private Race GetSelectedRace()
		{
			TreeModel model;
			TreeIter iter;
			lstRaces.Selection.GetSelected(out model, out iter);
			return (Race)model.GetValue(iter, 0);
		}

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

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

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

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

		protected virtual void OnSystemComboChanged(object sender, System.EventArgs e)
		{
			GameSystem system = ComboBoxUtils.GetSelectedItem<GameSystem>(systemCombo);
			SetRaces(system);
			sbPointsValue.Value = system.SystemArmyDefaultSize;
			ListStore model = (ListStore)lstRaces.Model;

			if (model.IterNChildren() == 1)
			{
				TreeIter iter;
				model.GetIterFirst(out iter);
				lstRaces.Selection.SelectIter(iter);
				SetArmyName();
			}

			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;
		}

		private void SetArmyName()
        {
			if (isDefaultArmyName || txtArmyName.Text == "")
			{
				if (lstRaces.Selection.CountSelectedRows() == 1)
		        {
	                GameSystem currSystem = SelectedGameSystem;
	                int points = (int)sbPointsValue.Value;
					txtArmyName.Text = Army.GenerateDefaultName(GetSelectedRace(), points, currSystem.GetPointsAbbrev(points));
				}
				else {
					txtArmyName.Text = "";
				}

				isDefaultArmyName = true;
			}
        }

		public Race SelectedRace
		{
			get { return race; }
		}

		public string ArmyName
		{
			get { return armyName; }
		}

		public int ArmySize
		{
			get { return pointsValue; }
		}

		private GameSystem SelectedGameSystem
		{
			get { return ComboBoxUtils.GetSelectedItem<GameSystem>(systemCombo); }
		}
	}
}