view FrmNewUnit.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 1576f669b3eb
children 1bcd7ea857c4
line wrap: on
line source

// This file (FrmNewUnit.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
{
	/// <summary>
	/// Summary description for FrmNewUnit.
	/// </summary>
	public class FrmNewUnit : IBBForm
	{
		private System.ComponentModel.IContainer components;
		private IBBoard.Windows.Forms.IBBButton bttnCancel;
		private IBBoard.Windows.Forms.IBBLabel lblUnitList;
		private IBBoard.Windows.Forms.IBBButton bttnSelectUnit;
		private System.Windows.Forms.ListBox lstUnits;
		private UnitType[] units;
		private Army army;
		private System.Windows.Forms.ImageList imageList;
		private System.Windows.Forms.Label lblNewUnitWarning;
		private System.Windows.Forms.Label lblWarningIcon;
		private bool[] allowed;

		public FrmNewUnit(Race race, Category cat, Army army)
		{
			InitializeComponent();

			ControlTranslator.TranslateControl(this, cat.Name);
			units = race.GetUnitTypes(cat);
			allowed = new bool[units.Length];
			this.army = army;
			
			for (int i = 0; i<units.Length; i++)
			{
				allowed[i] = army.CanAddUnitType(units[i]).Count == 0;
				lstUnits.Items.Add(units[i].Name);
			}
		}

		/// <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.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(FrmNewUnit));
			this.lstUnits = new System.Windows.Forms.ListBox();
			this.bttnCancel = new IBBoard.Windows.Forms.IBBButton();
			this.lblUnitList = new IBBoard.Windows.Forms.IBBLabel();
			this.bttnSelectUnit = new IBBoard.Windows.Forms.IBBButton();
			this.lblNewUnitWarning = new System.Windows.Forms.Label();
			this.imageList = new System.Windows.Forms.ImageList(this.components);
			this.lblWarningIcon = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// lstUnits
			// 
			this.lstUnits.Location = new System.Drawing.Point(88, 8);
			this.lstUnits.Name = "lstUnits";
			this.lstUnits.Size = new System.Drawing.Size(272, 121);
			this.lstUnits.TabIndex = 7;
			this.lstUnits.MouseDown += new System.Windows.Forms.MouseEventHandler(this.lstUnits_MouseDown);
			this.lstUnits.DoubleClick += new System.EventHandler(this.lstUnits_DoubleClick);
			this.lstUnits.SelectedIndexChanged += new System.EventHandler(this.lstUnits_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(8, 168);
			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);
			// 
			// lblUnitList
			// 
			this.lblUnitList.Location = new System.Drawing.Point(0, 8);
			this.lblUnitList.Name = "lblUnitList";
			this.lblUnitList.Size = new System.Drawing.Size(88, 80);
			this.lblUnitList.TabIndex = 5;
			this.lblUnitList.Text = "unit list";
			this.lblUnitList.TextAlign = System.Drawing.ContentAlignment.TopRight;
			// 
			// bttnSelectUnit
			// 
			this.bttnSelectUnit.Enabled = false;
			this.bttnSelectUnit.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.bttnSelectUnit.Location = new System.Drawing.Point(256, 168);
			this.bttnSelectUnit.Name = "bttnSelectUnit";
			this.bttnSelectUnit.Size = new System.Drawing.Size(104, 24);
			this.bttnSelectUnit.TabIndex = 4;
			this.bttnSelectUnit.Text = "create unit";
			this.bttnSelectUnit.Click += new System.EventHandler(this.bttnSelectUnit_Click);
			// 
			// lblNewUnitWarning
			// 
			this.lblNewUnitWarning.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.lblNewUnitWarning.ImageList = this.imageList;
			this.lblNewUnitWarning.Location = new System.Drawing.Point(48, 136);
			this.lblNewUnitWarning.Name = "lblNewUnitWarning";
			this.lblNewUnitWarning.Size = new System.Drawing.Size(312, 32);
			this.lblNewUnitWarning.TabIndex = 9;
			this.lblNewUnitWarning.Text = "label1";
			this.lblNewUnitWarning.TextAlign = System.Drawing.ContentAlignment.TopRight;
			this.lblNewUnitWarning.Visible = false;
			// 
			// imageList
			// 
			this.imageList.ColorDepth = System.Windows.Forms.ColorDepth.Depth32Bit;
			this.imageList.ImageSize = new System.Drawing.Size(32, 32);
			this.imageList.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList.ImageStream")));
			this.imageList.TransparentColor = System.Drawing.Color.Transparent;
			// 
			// lblWarningIcon
			// 
			this.lblWarningIcon.ImageIndex = 0;
			this.lblWarningIcon.ImageList = this.imageList;
			this.lblWarningIcon.Location = new System.Drawing.Point(8, 136);
			this.lblWarningIcon.Name = "lblWarningIcon";
			this.lblWarningIcon.Size = new System.Drawing.Size(40, 32);
			this.lblWarningIcon.TabIndex = 10;
			this.lblWarningIcon.Visible = false;
			// 
			// FrmNewUnit
			// 
			this.AcceptButton = this.bttnSelectUnit;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.CancelButton = this.bttnCancel;
			this.ClientSize = new System.Drawing.Size(370, 196);
			this.Controls.Add(this.lblWarningIcon);
			this.Controls.Add(this.lblNewUnitWarning);
			this.Controls.Add(this.bttnCancel);
			this.Controls.Add(this.lblUnitList);
			this.Controls.Add(this.bttnSelectUnit);
			this.Controls.Add(this.lstUnits);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "FrmNewUnit";
			this.ShowInTaskbar = false;
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
			this.Text = "FrmNewUni";
			this.ResumeLayout(false);

		}
		#endregion

		public UnitType SelectedUnit
		{
			get { return units[lstUnits.SelectedIndex]; }
		}

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

		private void bttnSelectUnit_Click(object sender, System.EventArgs e)
		{
			selectUnit();
		}

		private void selectUnit()
		{
			DialogResult = DialogResult.OK;
			this.Close();
		}

		private void setSelectUnitEnabledVal()
		{
			if (lstUnits.SelectedIndex>-1)
			{
				bttnSelectUnit.Enabled = true;
				lblNewUnitWarning.Visible = !allowed[lstUnits.SelectedIndex];
				lblWarningIcon.Visible = lblNewUnitWarning.Visible;

				if (lblNewUnitWarning.Visible)
				{
					//lblNewUnitWarning.Text = army.GetFailedAddRequirement(units[lstUnits.SelectedIndex]).Description;
                    //FIXME: Add failed requirement text
				}
			}
			else
			{
				bttnSelectUnit.Enabled = false;
			}
		}

		private void lstUnits_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			setSelectUnitEnabledVal();
		}

		private void lstUnits_DoubleClick(object sender, EventArgs e)
		{
			if (lstUnits.SelectedIndex>-1)
			{
				selectUnit();
			}
		}

		private void lstUnits_MouseDown(object sender, MouseEventArgs e)
		{
			//make sure no item is selected when a click is made outside a unit
			int index = lstUnits.IndexFromPoint(e.X, e.Y);
			lstUnits.SelectedIndex = index;
		}
	}
}