view FrmEditUnitEquipment.cs @ 184:d97de6f980b4

Re #88: WinForms UI * Keep last 25 log files to stop log folder bloat
author IBBoard <dev@ibboard.co.uk>
date Sat, 30 Oct 2010 10:13:59 +0000
parents d3f498514122
children
line wrap: on
line source

// This file (FrmEditUnitEquipment.cs) is a part of the IBBoard.WarFoundry.GUI.WinForms project and is copyright 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.Commands;
using IBBoard.Lang;
using IBBoard.Windows.Forms;
using IBBoard.Windows.Forms.I18N;
using IBBoard.WarFoundry.API;
using IBBoard.WarFoundry.API.Commands;
using IBBoard.WarFoundry.API.Objects;
using IBBoard.WarFoundry.API.Util;

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

		public FrmEditUnitEquipment(Unit unit, UnitEquipmentItem equipItem, CommandStack stack)
		{
			commandStack = stack;
			this.unit = unit;
			this.equipItem = equipItem;
			InitializeComponent();
			ControlTranslator.TranslateControl(this);
			this.Text = Translation.GetTranslation("FrmEditUnitEquipment", "edit {0} for {1}", equipItem.Name, unit.Name);
			equipmentAmount.SetUnit(unit);
			equipmentAmount.SetUnitEquipmentItem(equipItem);
		}

		/// <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.equipmentAmount = new IBBoard.WarFoundry.GUI.WinForms.UI.EquipmentAmountControl();
			this.SuspendLayout();
			// 
			// lblAmount
			// 
			this.lblAmount.Location = new System.Drawing.Point(12, 8);
			this.lblAmount.Name = "lblAmount";
			this.lblAmount.Size = new System.Drawing.Size(68, 74);
			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(153, 95);
			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, 95);
			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);
			// 
			// equipmentAmount
			// 
			this.equipmentAmount.Location = new System.Drawing.Point(88, 5);
			this.equipmentAmount.Name = "equipmentAmount";
			this.equipmentAmount.Size = new System.Drawing.Size(155, 77);
			this.equipmentAmount.TabIndex = 5;
			this.equipmentAmount.ValueChanged += new System.EventHandler(this.equipmentAmount_ValueChanged);
			// 
			// FrmEditUnitEquipment
			// 
			this.AcceptButton = this.bttnOkay;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.CancelButton = this.bttnCancel;
			this.ClientSize = new System.Drawing.Size(240, 130);
			this.ControlBox = false;
			this.Controls.Add(this.equipmentAmount);
			this.Controls.Add(this.bttnOkay);
			this.Controls.Add(this.bttnCancel);
			this.Controls.Add(this.lblAmount);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.Name = "FrmEditUnitEquipment";
			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)
		{
			if (SetValue())
			{
				this.Close();
			}
		}

		private bool SetValue()
		{
			bool setValue = true;
			double amount = equipmentAmount.EquipmentAmount;

			if (amount == 0)
			{
				if (equipItem.IsRequired)
				{
					string requiredText = "this item is required and cannot be removed - it must exist or be replaced";
					string requiredTitle = "required item";
					requiredText = Translation.GetTranslation("removeRequiredEquipmentText", requiredText);
					requiredTitle = Translation.GetTranslation("removeRequiredEquipmentTitle", requiredTitle);
					MessageBox.Show(this, requiredText, requiredTitle, MessageBoxButtons.OK, MessageBoxIcon.Warning);
					setValue = false;
				}
				else
				{
					string removeEquipText = "remove equipment item from unit?";
					string removeEquipTitle = "confirm removing equipment";
					removeEquipText = Translation.GetTranslation("removeEquipmentText", removeEquipText);
					removeEquipTitle = Translation.GetTranslation("removeEquipmentTitle", removeEquipTitle);
					DialogResult dr = MessageBox.Show(this, removeEquipText, removeEquipTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);

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

			double oldAmount = UnitEquipmentUtil.GetEquipmentAmount(unit, equipItem);

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

			return setValue;
		}

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

		private void SetOkayButton()
		{
			double equipAmount = equipmentAmount.EquipmentAmount;
			bttnOkay.Enabled = equipAmount > 0 || !equipItem.IsRequired;
		}

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