view FrmNewArmy.cs @ 249:a11faa64c333

Re #354: Single-button Add Unit * Add right-click menu (doesn't work yet because of position maths)
author IBBoard <dev@ibboard.co.uk>
date Sat, 20 Aug 2011 17:05:45 +0100
parents 70575ace029b
children b3a47b712a8b
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
	{

		private IContainer components;
		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 IBBLabel lblGameSystem;
		private ComboBox gameSystems;
		private System.Windows.Forms.TextBox txtArmyName;

		private GameSystem system;
		private Race race;
		private string armyName;
        public bool isDefaultArmyName;
		private ToolTip gameSystemToolTip;
		private int maxPoints;

		public FrmNewArmy(GameSystem system)
		{
			InitializeComponent();

			ControlTranslator.TranslateControl(this);

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

			if (gameSystems.Items.Count > 0)
			{
				gameSystems.Enabled = true;
				lblGameSystem.Image = null;
			}
			else
			{
				gameSystems.Enabled = false;
				gameSystemToolTip.SetToolTip(lblGameSystem, Translation.GetTranslation("newArmyNoGameSystems", "could not load data for game systems - please check logs"));
			}

			if (system != null)
			{
				gameSystems.SelectedItem = system;
			}
			else if (gameSystems.Items.Count == 1)
			{
				gameSystems.SelectedIndex = 0;
			}
		}

		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()
		{
			this.components = new System.ComponentModel.Container();
			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.gameSystems = new System.Windows.Forms.ComboBox();
			this.lblGameSystem = new IBBoard.Windows.Forms.IBBLabel();
			this.gameSystemToolTip = new System.Windows.Forms.ToolTip(this.components);
			((System.ComponentModel.ISupportInitialize)(this.armySize)).BeginInit();
			this.SuspendLayout();
			//
			// lstRaces
			//
			this.lstRaces.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
						| System.Windows.Forms.AnchorStyles.Right)));
			this.lstRaces.DisplayMember = "Name";
			this.lstRaces.Enabled = false;
			this.lstRaces.Location = new System.Drawing.Point(141, 33);
			this.lstRaces.Name = "lstRaces";
			this.lstRaces.Size = new System.Drawing.Size(265, 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(123, 80);
			this.lblRaceList.TabIndex = 5;
			this.lblRaceList.Text = "race list";
			this.lblRaceList.TextAlign = System.Drawing.ContentAlignment.TopRight;
			//
			// bttnSelectRace
			//
			this.bttnSelectRace.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
						| System.Windows.Forms.AnchorStyles.Right)));
			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(152, 24);
			this.bttnSelectRace.TabIndex = 4;
			this.bttnSelectRace.Text = "create army";
			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(123, 17);
			this.lblArmyName.TabIndex = 8;
			this.lblArmyName.Text = "army name";
			this.lblArmyName.TextAlign = System.Drawing.ContentAlignment.TopRight;
			//
			// txtArmyName
			//
			this.txtArmyName.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
						| System.Windows.Forms.AnchorStyles.Right)));
			this.txtArmyName.Enabled = false;
			this.txtArmyName.Location = new System.Drawing.Point(141, 160);
			this.txtArmyName.Name = "txtArmyName";
			this.txtArmyName.Size = new System.Drawing.Size(265, 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(123, 18);
			this.lblArmySize.TabIndex = 10;
			this.lblArmySize.Text = "army size";
			this.lblArmySize.TextAlign = System.Drawing.ContentAlignment.TopRight;
			//
			// armySize
			//
			this.armySize.Enabled = false;
			this.armySize.Increment = new decimal(new int[] {
            50,
            0,
            0,
            0});
			this.armySize.Location = new System.Drawing.Point(141, 186);
			this.armySize.Maximum = new decimal(new int[] {
            1000000,
            0,
            0,
            0});
			this.armySize.Name = "armySize";
			this.armySize.Size = new System.Drawing.Size(99, 20);
			this.armySize.TabIndex = 11;
			this.armySize.ThousandsSeparator = true;
			this.armySize.Value = new decimal(new int[] {
            2000,
            0,
            0,
            0});
            this.armySize.ValueChanged += new System.EventHandler(this.armySize_ValueChanged);
            //
			// gameSystems
			//
			this.gameSystems.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
						| System.Windows.Forms.AnchorStyles.Right)));
			this.gameSystems.DisplayMember = "Name";
			this.gameSystems.FormattingEnabled = true;
			this.gameSystems.Location = new System.Drawing.Point(141, 6);
			this.gameSystems.Name = "gameSystems";
			this.gameSystems.Size = new System.Drawing.Size(265, 21);
			this.gameSystems.TabIndex = 13;
			this.gameSystems.SelectedValueChanged += new System.EventHandler(this.gameSystems_SelectedValueChanged);
			//
			// lblGameSystem
			//
			this.lblGameSystem.Image = global::IBBoard.WarFoundry.GUI.WinForms.Properties.Resources.warning_sml;
			this.lblGameSystem.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.lblGameSystem.Location = new System.Drawing.Point(12, 9);
			this.lblGameSystem.Name = "lblGameSystem";
			this.lblGameSystem.Size = new System.Drawing.Size(123, 18);
			this.lblGameSystem.TabIndex = 12;
			this.lblGameSystem.Text = "game system";
			this.lblGameSystem.TextAlign = System.Drawing.ContentAlignment.TopRight;
			//
			// FrmNewArmy
			//
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(418, 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 void armySize_ValueChanged(object sender, System.EventArgs e)
		{
            if (txtArmyName.Enabled && isDefaultArmyName)
            {
                setTxtArmyNameVal();
            }
		}
        
        private GameSystem GetSelectedGameSystem()
		{
			return (GameSystem) gameSystems.SelectedItem;
		}

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

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

        private void setTxtArmyNameVal()
        {
            if (lstRaces.SelectedIndex > -1)
            {
                txtArmyName.Text = String.Format(race.ArmyDefaultName, Translation.GetTranslation("armySizePts", "{0}pt", armySize.Value));
                isDefaultArmyName = true;
            }
        }

        private void lstRaces_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			setSelectRaceEnabledVal();
			bool raceSelected = lstRaces.SelectedItem != null;
			txtArmyName.Enabled = raceSelected;
			armySize.Enabled = raceSelected;
			race = (Race)lstRaces.SelectedItem;
            setTxtArmyNameVal();
		}

		private void gameSystems_SelectedValueChanged(object sender, EventArgs e)
		{
			
            GameSystem selected = GetSelectedGameSystem();
			txtArmyName.Text = "";
            txtArmyName.Enabled = false;
            lstRaces.Items.Clear();
            armySize.Value = selected.SystemArmyDefaultSize;
 
			if (selected != null)
			{
				SetRaces(selected);

				if (lstRaces.Items.Count == 1)
				{
					lstRaces.SelectedIndex = 0;
				}
			}

			lstRaces.Enabled = lstRaces.Items.Count > 0;
		}
	}
}