view FrmNewArmy.cs @ 70:a8667ab60362

Re #180: Add multiple mutex groups * Make WinForms UI handle multiple groups instead of assuming one
author IBBoard <dev@ibboard.co.uk>
date Sat, 26 Sep 2009 19:55:13 +0000
parents 11e81ba85684
children c243b043aa62
line wrap: on
line source

// This file (FrmNewArmy.cs) is a part of the IBBoard.WarFoundry.GUI.WinForms project and is copyright 2007, 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 System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
using IBBoard;
using IBBoard.Lang;
using IBBoard.Windows.Forms;
using IBBoard.WarFoundry.API;
using IBBoard.WarFoundry.API.Objects;
using IBBoard.Windows.Forms.I18N;

namespace IBBoard.WarFoundry.GUI.WinForms
{
	/// <summary>
	/// Summary description for FrmNewArmy.
	/// </summary>
	public class FrmNewArmy : IBBForm
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private IBBoard.Windows.Forms.IBBButton bttnCancel;
		private IBBoard.Windows.Forms.IBBLabel lblRaceList;
		private IBBoard.Windows.Forms.IBBButton bttnSelectRace;
		private System.Windows.Forms.ListBox lstRaces;
		private IBBoard.Windows.Forms.IBBLabel lblArmyName;
		private IBBoard.Windows.Forms.IBBLabel lblArmySize;
		private System.Windows.Forms.NumericUpDown armySize;
		private Label lblGameSystem;
		private ComboBox gameSystems;
		private System.Windows.Forms.TextBox txtArmyName;

		private GameSystem system;
		private Race race;
		private string armyName;
		private int maxPoints;

		public FrmNewArmy(GameSystem system)
		{
			InitializeComponent();

			ControlTranslator.TranslateControl(this);

			foreach (GameSystem gameSystem in WarFoundryLoader.GetDefault().GetGameSystems())
			{
				gameSystems.Items.Add(gameSystem);
			}

			if (WarFoundryCore.CurrentGameSystem != null)
			{
				gameSystems.SelectedItem = FrmMain.CurrentGameSystem;
			}
		}

		private void SetRaces(GameSystem system)
		{
			foreach (Race race in WarFoundryLoader.GetDefault().GetRaces(system))
			{
				lstRaces.Items.Add(race);
			}
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FrmNewArmy));
			this.lstRaces = new System.Windows.Forms.ListBox();
			this.bttnCancel = new IBBoard.Windows.Forms.IBBButton();
			this.lblRaceList = new IBBoard.Windows.Forms.IBBLabel();
			this.bttnSelectRace = new IBBoard.Windows.Forms.IBBButton();
			this.lblArmyName = new IBBoard.Windows.Forms.IBBLabel();
			this.txtArmyName = new System.Windows.Forms.TextBox();
			this.lblArmySize = new IBBoard.Windows.Forms.IBBLabel();
			this.armySize = new System.Windows.Forms.NumericUpDown();
			this.lblGameSystem = new System.Windows.Forms.Label();
			this.gameSystems = new System.Windows.Forms.ComboBox();
			((System.ComponentModel.ISupportInitialize) (this.armySize)).BeginInit();
			this.SuspendLayout();
			// 
			// lstRaces
			// 
			this.lstRaces.DisplayMember = "Name";
			this.lstRaces.Location = new System.Drawing.Point(89, 33);
			this.lstRaces.Name = "lstRaces";
			this.lstRaces.Size = new System.Drawing.Size(269, 121);
			this.lstRaces.TabIndex = 7;
			this.lstRaces.SelectedIndexChanged += new System.EventHandler(this.lstRaces_SelectedIndexChanged);
			// 
			// bttnCancel
			// 
			this.bttnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.bttnCancel.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.bttnCancel.Location = new System.Drawing.Point(12, 231);
			this.bttnCancel.Name = "bttnCancel";
			this.bttnCancel.Size = new System.Drawing.Size(80, 24);
			this.bttnCancel.TabIndex = 6;
			this.bttnCancel.Text = "Cancel";
			this.bttnCancel.Click += new System.EventHandler(this.bttnCancel_Click);
			// 
			// lblRaceList
			// 
			this.lblRaceList.Location = new System.Drawing.Point(12, 33);
			this.lblRaceList.Name = "lblRaceList";
			this.lblRaceList.Size = new System.Drawing.Size(71, 80);
			this.lblRaceList.TabIndex = 5;
			this.lblRaceList.Text = "race list";
			this.lblRaceList.TextAlign = System.Drawing.ContentAlignment.TopRight;
			// 
			// bttnSelectRace
			// 
			this.bttnSelectRace.Enabled = false;
			this.bttnSelectRace.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.bttnSelectRace.Location = new System.Drawing.Point(254, 231);
			this.bttnSelectRace.Name = "bttnSelectRace";
			this.bttnSelectRace.Size = new System.Drawing.Size(104, 24);
			this.bttnSelectRace.TabIndex = 4;
			this.bttnSelectRace.Text = "create race";
			this.bttnSelectRace.Click += new System.EventHandler(this.bttnSelectRace_Click);
			// 
			// lblArmyName
			// 
			this.lblArmyName.Location = new System.Drawing.Point(12, 163);
			this.lblArmyName.Name = "lblArmyName";
			this.lblArmyName.Size = new System.Drawing.Size(71, 17);
			this.lblArmyName.TabIndex = 8;
			this.lblArmyName.Text = "army name";
			this.lblArmyName.TextAlign = System.Drawing.ContentAlignment.TopRight;
			// 
			// txtArmyName
			// 
			this.txtArmyName.Location = new System.Drawing.Point(89, 160);
			this.txtArmyName.Name = "txtArmyName";
			this.txtArmyName.Size = new System.Drawing.Size(269, 20);
			this.txtArmyName.TabIndex = 9;
			this.txtArmyName.TextChanged += new System.EventHandler(this.txtArmyName_TextChanged);
			// 
			// lblArmySize
			// 
			this.lblArmySize.Location = new System.Drawing.Point(12, 188);
			this.lblArmySize.Name = "lblArmySize";
			this.lblArmySize.Size = new System.Drawing.Size(71, 18);
			this.lblArmySize.TabIndex = 10;
			this.lblArmySize.Text = "army size";
			this.lblArmySize.TextAlign = System.Drawing.ContentAlignment.TopRight;
			// 
			// armySize
			// 
			this.armySize.Increment = new decimal(new int[] {
            50,
            0,
            0,
            0});
			this.armySize.Location = new System.Drawing.Point(89, 186);
			this.armySize.Maximum = new decimal(new int[] {
            1000000,
            0,
            0,
            0});
			this.armySize.Name = "armySize";
			this.armySize.Size = new System.Drawing.Size(120, 20);
			this.armySize.TabIndex = 11;
			this.armySize.ThousandsSeparator = true;
			this.armySize.Value = new decimal(new int[] {
            2000,
            0,
            0,
            0});
			// 
			// lblGameSystem
			// 
			this.lblGameSystem.Location = new System.Drawing.Point(12, 9);
			this.lblGameSystem.Name = "lblGameSystem";
			this.lblGameSystem.Size = new System.Drawing.Size(71, 18);
			this.lblGameSystem.TabIndex = 12;
			this.lblGameSystem.Text = "game system";
			this.lblGameSystem.TextAlign = System.Drawing.ContentAlignment.TopRight;
			// 
			// gameSystems
			// 
			this.gameSystems.DisplayMember = "Name";
			this.gameSystems.FormattingEnabled = true;
			this.gameSystems.Location = new System.Drawing.Point(89, 6);
			this.gameSystems.Name = "gameSystems";
			this.gameSystems.Size = new System.Drawing.Size(269, 21);
			this.gameSystems.TabIndex = 13;
			this.gameSystems.SelectedValueChanged += new System.EventHandler(this.gameSystems_SelectedValueChanged);
			// 
			// FrmNewArmy
			// 
			this.AcceptButton = this.bttnSelectRace;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.CancelButton = this.bttnCancel;
			this.ClientSize = new System.Drawing.Size(370, 267);
			this.Controls.Add(this.gameSystems);
			this.Controls.Add(this.lblGameSystem);
			this.Controls.Add(this.armySize);
			this.Controls.Add(this.lblArmySize);
			this.Controls.Add(this.txtArmyName);
			this.Controls.Add(this.lblArmyName);
			this.Controls.Add(this.bttnCancel);
			this.Controls.Add(this.lblRaceList);
			this.Controls.Add(this.bttnSelectRace);
			this.Controls.Add(this.lstRaces);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.Icon = ((System.Drawing.Icon) (resources.GetObject("$this.Icon")));
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "FrmNewArmy";
			this.ShowInTaskbar = false;
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
			this.Text = "FrmNewArmy";
			((System.ComponentModel.ISupportInitialize) (this.armySize)).EndInit();
			this.ResumeLayout(false);
			this.PerformLayout();

		}
		#endregion

		public GameSystem SelectedSystem
		{
			get { return system; }
		}

		public Race SelectedRace
		{
			get { return race; }
		}

		public string ArmyName
		{
			get { return armyName; }
		}

		public int ArmySize
		{
			get { return maxPoints; }
		}

		private void bttnCancel_Click(object sender, System.EventArgs e)
		{
			DialogResult = DialogResult.Cancel;
			this.Close();
		}

		private void bttnSelectRace_Click(object sender, System.EventArgs e)
		{
			DialogResult = DialogResult.OK;
			SetValues();
			this.Close();
		}

		private void SetValues()
		{
			system = GetSelectedGameSystem();
			race = (Race) lstRaces.SelectedItem;
			armyName = txtArmyName.Text;
			maxPoints = (int) armySize.Value;
		}

		private GameSystem GetSelectedGameSystem()
		{
			return (GameSystem) gameSystems.SelectedItem;
		}

		private void txtArmyName_TextChanged(object sender, System.EventArgs e)
		{
			setSelectRaceEnabledVal();
		}

		private void setSelectRaceEnabledVal()
		{
			bttnSelectRace.Enabled = (lstRaces.SelectedIndex>-1 && txtArmyName.Text.Trim()!="" && armySize.Value > 0);
		}

		private void lstRaces_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			setSelectRaceEnabledVal();
		}

		private void gameSystems_SelectedValueChanged(object sender, EventArgs e)
		{
			GameSystem selected = GetSelectedGameSystem();
			lstRaces.Items.Clear();

			if (selected != null)
			{
				SetRaces(selected);
			}
		}
	}
}