view FrmNewUnit.cs @ 243:55c94d438a4b

Re #58: Remove LogNotifier from API * Add logging moved from API
author IBBoard <dev@ibboard.co.uk>
date Sat, 17 Mar 2012 21:03:33 +0000
parents 8021cc097ec8
children 2c3474d652e1
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.Collections.Generic;
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.WarFoundry.API.Objects.Requirement;
using IBBoard.Windows.Forms.I18N;

namespace IBBoard.WarFoundry
{
	/// <summary>
	/// Summary description for FrmNewUnit.
	/// </summary>
	public class FrmNewUnit : IBBForm
	{
		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.Label lblNewUnitWarning;
		private System.Windows.Forms.Label lblWarningIcon;

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

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

		#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(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.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.SelectedIndexChanged += new System.EventHandler(this.lstUnits_SelectedIndexChanged);
			this.lstUnits.DoubleClick += new System.EventHandler(this.lstUnits_DoubleClick);
			this.lstUnits.MouseDown += new System.Windows.Forms.MouseEventHandler(this.lstUnits_MouseDown);
			// 
			// 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.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 = "One or more unit requirements are not satisfied.";
			this.lblNewUnitWarning.TextAlign = System.Drawing.ContentAlignment.TopRight;
			this.lblNewUnitWarning.Visible = false;
			// 
			// lblWarningIcon
			// 
			this.lblWarningIcon.Image = global::IBBoard.WarFoundry.GUI.WinForms.Properties.Resources.dialog_warning;
			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;
                ICollection<string> failureMessages;
				lblNewUnitWarning.Visible = !Validates.AsOkay(RequirementHandler.AllowsAdding(SelectedUnit, army, out failureMessages));
				lblWarningIcon.Visible = lblNewUnitWarning.Visible;

				if (lblNewUnitWarning.Visible)
				{
					lblNewUnitWarning.Text = String.Join("\n", new List<string>(failureMessages).ToArray());
				}
			}
			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;
		}
	}
}