view FrmReplaceUnitEquipment.cs @ 64:e04bea5b7b3d WarFoundry_v0.1beta3_Winforms

Fixes #157: Stats show object class name when selected * Do the column style properly and re-implement the parent's abstract methods from scratch rather than from the "textbox" version (as per MSDN page: http://msdn.microsoft.com/en-us/library/ms996453.aspx#datagridcolumnstyle2_topic5)
author IBBoard <dev@ibboard.co.uk>
date Tue, 22 Sep 2009 19:24:00 +0000
parents 2f2e46cd4171
children ff80ed9ac50f
line wrap: on
line source

// This file (FrmReplaceUnitEquipment.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.WarFoundry.API;
using IBBoard.WarFoundry.API.Commands;
using IBBoard.WarFoundry.API.Objects;

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

		public FrmReplaceUnitEquipment(Unit unit, UnitEquipmentItem item, CommandStack stack)
		{
			commandStack = stack;
			this.unit = unit;
			oldItem = item;
			InitializeComponent();
			this.Text = "Replace " + item.EquipmentItem.Name + " with new equipment";
			object[] items = Arrays.Subtract(unit.UnitType.GetEquipmentItemsByExclusionGroup(item.MutexGroup), 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 System.Windows.Forms.Label();
			this.bttnCancel = new System.Windows.Forms.Button();
			this.bttnOkay = new System.Windows.Forms.Button();
			this.lblItem = new System.Windows.Forms.Label();
			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(8, 80);
			this.lblAmount.Name = "lblAmount";
			this.lblAmount.Size = new System.Drawing.Size(72, 23);
			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(159, 184);
			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(11, 184);
			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(0, 8);
			this.lblItem.Name = "lblItem";
			this.lblItem.Size = new System.Drawing.Size(80, 23);
			this.lblItem.TabIndex = 6;
			this.lblItem.Text = "item";
			this.lblItem.TextAlign = System.Drawing.ContentAlignment.TopRight;
			// 
			// listItems
			// 
			this.listItems.Location = new System.Drawing.Point(86, 8);
			this.listItems.Name = "listItems";
			this.listItems.Size = new System.Drawing.Size(144, 69);
			this.listItems.TabIndex = 7;
			this.listItems.SelectedIndexChanged += new System.EventHandler(this.listItems_SelectedIndexChanged);
			// 
			// equipmentAmount
			// 
			this.equipmentAmount.Location = new System.Drawing.Point(86, 83);
			this.equipmentAmount.Name = "equipmentAmount";
			this.equipmentAmount.Size = new System.Drawing.Size(155, 77);
			this.equipmentAmount.TabIndex = 8;
			// 
			// FrmReplaceUnitEquipment
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(246, 219);
			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 = "FrmReplaceUnitEquipment";
			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;
			bool setValue = true;
			double amount = equipmentAmount.EquipmentAmount;

			if (amount == 0)
			{
				if (equipItem.IsRequired)
				{
					MessageBox.Show(this, "This item is required and cannot be removed. It must have a quantity of at least one or be replaced by an alternative.", "Required item", MessageBoxButtons.OK, MessageBoxIcon.Warning);
					setValue = false;
				}
				else
				{
					DialogResult dr = MessageBox.Show(this, "This will remove the item from the unit. Continue?", "Confirm remove", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);

					if (dr != DialogResult.Yes)
					{
						setValue = false;
					}
				}
			}

			double oldAmount = unit.GetEquipmentAmount(equipItem);

			if (setValue && oldAmount != amount)
			{
				if (equipmentAmount.IsRatioEquipmentAmount)
				{
					commandStack.Execute(new ReplaceUnitEquipmentWithRatioAmountItemCommand(unit, oldItem, equipItem, amount));
				}
				else
				{
					commandStack.Execute(new ReplaceUnitEquipmentWithNumericAmountItemCommand(unit, oldItem, equipItem, (int)amount));
				}
			}
		}

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

		private void listItems_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			equipmentAmount.SetUnitEquipmentItem((UnitEquipmentItem)listItems.SelectedItem);
			SetOkayButton();
		}

		private void SetOkayButton()
		{
			double equipAmount = equipmentAmount.EquipmentAmount;
			bttnOkay.Enabled = equipAmount > 0 || equipAmount == WarFoundryCore.INFINITY || !((UnitEquipmentItem)listItems.SelectedItem).IsRequired;
		}

		private void numEquipAmount_ValueChanged(object sender, System.EventArgs e)
		{			
			SetOkayButton();
		}
	}
}