view FrmNewUnitEquipment.cs @ 153:fcd90498d9a2

Re #269: Handle multiple stat lines * Improve rendering of stats * Fix column widths * Set width and height based on content Also: * Make FrmUnit title contain unit type as we're using it more now Still needs more testing and fixing
author IBBoard <dev@ibboard.co.uk>
date Wed, 26 May 2010 20:05:20 +0000
parents c1a3993297b1
children 7243e6646926
line wrap: on
line source

// This file (FrmNewUnitEquipment.cs) is a part of the IBBoard.WarFoundry.GUI.WinForms 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 System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
using IBBoard;
using IBBoard.Commands;
using IBBoard.Lang;
using IBBoard.WarFoundry.API;
using IBBoard.WarFoundry.API.Commands;
using IBBoard.WarFoundry.API.Objects;
using IBBoard.WarFoundry.API.Util;
using IBBoard.WarFoundry.GUI.WinForms.UI;
using IBBoard.WarFoundry.GUI.WinForms.Util;
using IBBoard.Windows.Forms;
using IBBoard.Windows.Forms.I18N;

namespace IBBoard.WarFoundry.GUI.WinForms
{
	/// <summary>
	/// Summary description for FrmNewUnitEquipment.
	/// </summary>
	public class FrmNewUnitEquipment : IBBForm
	{
		private Unit unit;
		private CommandStack commandStack;
		private IBBLabel lblAmount;
		private IBBButton bttnCancel;
		private IBBButton bttnOkay;
		private IBBLabel lblItem;
		private System.Windows.Forms.ListBox listItems;
		private EquipmentAmountControl equipmentAmount;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public FrmNewUnitEquipment(Unit unit, CommandStack stack)
		{
			commandStack = stack;
			this.unit = unit;
			InitializeComponent();
			ControlTranslator.TranslateControl(this);
			this.Text = Translation.GetTranslation("FrmNewUnitEquipment", "new equipment for {0}", unit.Name);
			object[] items = Arrays.Subtract(UnitEquipmentUtil.GetAllowedEquipmentItems(unit), unit.GetEquipment());
			listItems.Items.AddRange(items);
			equipmentAmount.SetUnit(unit);
		}

		/// <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.lblAmount = new IBBoard.Windows.Forms.IBBLabel();
			this.bttnCancel = new IBBoard.Windows.Forms.IBBButton();
			this.bttnOkay = new IBBoard.Windows.Forms.IBBButton();
			this.lblItem = new IBBoard.Windows.Forms.IBBLabel();
			this.listItems = new System.Windows.Forms.ListBox();
			this.equipmentAmount = new IBBoard.WarFoundry.GUI.WinForms.UI.EquipmentAmountControl();
			this.SuspendLayout();
			// 
			// lblAmount
			// 
			this.lblAmount.Location = new System.Drawing.Point(12, 80);
			this.lblAmount.Name = "lblAmount";
			this.lblAmount.Size = new System.Drawing.Size(68, 77);
			this.lblAmount.TabIndex = 2;
			this.lblAmount.Text = "amount";
			this.lblAmount.TextAlign = System.Drawing.ContentAlignment.TopRight;
			// 
			// bttnCancel
			// 
			this.bttnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.bttnCancel.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.bttnCancel.Location = new System.Drawing.Point(158, 162);
			this.bttnCancel.Name = "bttnCancel";
			this.bttnCancel.Size = new System.Drawing.Size(75, 23);
			this.bttnCancel.TabIndex = 3;
			this.bttnCancel.Text = "cancel";
			this.bttnCancel.Click += new System.EventHandler(this.bttnCancel_Click);
			// 
			// bttnOkay
			// 
			this.bttnOkay.Enabled = false;
			this.bttnOkay.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.bttnOkay.Location = new System.Drawing.Point(12, 162);
			this.bttnOkay.Name = "bttnOkay";
			this.bttnOkay.Size = new System.Drawing.Size(75, 23);
			this.bttnOkay.TabIndex = 4;
			this.bttnOkay.Text = "okay";
			this.bttnOkay.Click += new System.EventHandler(this.bttnOkay_Click);
			// 
			// lblItem
			// 
			this.lblItem.Location = new System.Drawing.Point(12, 8);
			this.lblItem.Name = "lblItem";
			this.lblItem.Size = new System.Drawing.Size(68, 69);
			this.lblItem.TabIndex = 6;
			this.lblItem.Text = "item";
			this.lblItem.TextAlign = System.Drawing.ContentAlignment.TopRight;
			// 
			// listItems
			// 
			this.listItems.Location = new System.Drawing.Point(88, 8);
			this.listItems.Name = "listItems";
			this.listItems.Size = new System.Drawing.Size(145, 69);
			this.listItems.TabIndex = 7;
			this.listItems.SelectedIndexChanged += new System.EventHandler(this.listItems_SelectedIndexChanged);
			// 
			// equipmentAmount
			// 
			this.equipmentAmount.Location = new System.Drawing.Point(88, 80);
			this.equipmentAmount.Name = "equipmentAmount";
			this.equipmentAmount.Size = new System.Drawing.Size(155, 77);
			this.equipmentAmount.TabIndex = 8;
			this.equipmentAmount.ValueChanged += new System.EventHandler(this.equipmentAmount_ValueChanged);
			// 
			// FrmNewUnitEquipment
			// 
			this.AcceptButton = this.bttnOkay;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.CancelButton = this.bttnCancel;
			this.ClientSize = new System.Drawing.Size(245, 190);
			this.ControlBox = false;
			this.Controls.Add(this.equipmentAmount);
			this.Controls.Add(this.listItems);
			this.Controls.Add(this.lblItem);
			this.Controls.Add(this.bttnOkay);
			this.Controls.Add(this.bttnCancel);
			this.Controls.Add(this.lblAmount);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.Name = "FrmNewUnitEquipment";
			this.ShowInTaskbar = false;
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
			this.Text = "FrmEditUnitEquipment";
			this.ResumeLayout(false);

		}
		#endregion

		private void bttnOkay_Click(object sender, System.EventArgs e)
		{
			SetValue();
			this.Close();
		}

		private void SetValue()
		{
			UnitEquipmentItem equipItem = (UnitEquipmentItem)listItems.SelectedItem;

			if (equipmentAmount.IsRatioEquipmentAmount)
			{
				commandStack.Execute(new SetUnitEquipmentRatioAmountCommand(unit, equipItem, equipmentAmount.EquipmentAmount));
			}
			else
			{
				//Just cast here - we should be returned a whole number anyway
				commandStack.Execute(new SetUnitEquipmentNumericAmountCommand(unit, equipItem, (int) equipmentAmount.EquipmentAmount));
			}
		}

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

		private void listItems_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			if (listItems.SelectedIndex > -1)
			{
				equipmentAmount.SetUnitEquipmentItem((UnitEquipmentItem)listItems.SelectedItem);
			}
			else
			{
				equipmentAmount.SetUnitEquipmentItem(null);
			}

			setOkayButton();
		}

		private void setOkayButton()
		{
			double equipAmount = equipmentAmount.EquipmentAmount;
			bttnOkay.Enabled = listItems.SelectedIndex > -1 && (equipAmount > 0 || equipAmount == WarFoundryCore.INFINITY);
		}

		private void equipmentAmount_ValueChanged(object sender, EventArgs e)
		{
			setOkayButton();
		}
	}
}