# HG changeset patch # User IBBoard # Date 1249135316 0 # Node ID 526fefefb16bc17bd76a6e714f9dc115f3922a9b # Parent d9aca1dc95d27bfcce7280a8a8d615d122c87f0f Fixes #91: Fix WinForms rendering of unit stats * Add StatColumnStyle (custom data grid style) * Use custom column style and fix positioning of columns Also, moved some forms from IBBoard.WarFoundry namespace to IBBoard.WarFoundry.WinForms diff -r d9aca1dc95d2 -r 526fefefb16b FrmArmyTree.cs --- a/FrmArmyTree.cs Sun Jul 05 09:36:21 2009 +0000 +++ b/FrmArmyTree.cs Sat Aug 01 14:01:56 2009 +0000 @@ -11,9 +11,9 @@ using IBBoard.WarFoundry.API; using IBBoard.WarFoundry.API.Commands; using IBBoard.Windows.Forms; -using IBBoard.WarFoundry.API.Objects; - -namespace IBBoard.WarFoundry +using IBBoard.WarFoundry.API.Objects; + +namespace IBBoard.WarFoundry.WinForms { /// /// Summary description for FrmArmyTree. diff -r d9aca1dc95d2 -r 526fefefb16b FrmMain.cs --- a/FrmMain.cs Sun Jul 05 09:36:21 2009 +0000 +++ b/FrmMain.cs Sat Aug 01 14:01:56 2009 +0000 @@ -27,7 +27,7 @@ using IBBoard.WarFoundry.API.Factories; using IBBoard.WarFoundry.API.Factories.Xml; -namespace IBBoard.WarFoundry +namespace IBBoard.WarFoundry.WinForms { /// /// Summary description for Form1. diff -r d9aca1dc95d2 -r 526fefefb16b FrmUnit.cs --- a/FrmUnit.cs Sun Jul 05 09:36:21 2009 +0000 +++ b/FrmUnit.cs Sat Aug 01 14:01:56 2009 +0000 @@ -1,524 +1,526 @@ -// This file (FrmUnit.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 under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license. - -using System; -using System.Drawing; -using System.Data; -using System.Collections.Generic; -using System.ComponentModel; -using System.Windows.Forms; -using IBBoard.Commands; -using IBBoard.Windows.Forms; -using IBBoard.WarFoundry.API; -using IBBoard.WarFoundry.API.Commands; -using IBBoard.WarFoundry.API.Objects; -using IBBoard.WarFoundry.GUI.WinForms.Util; - -namespace IBBoard.WarFoundry -{ - ///TODO: Separate weapons out into optional and required, where required only has button for replacing - - /// - /// Summary description for FrmUnit. - /// - public class FrmUnit : IBBoard.Windows.Forms.IBBForm - { - private Unit unit; - private Dictionary equipmentChoices = new Dictionary(); - private CommandStack commandStack; - private System.Windows.Forms.DataGrid statsGrid; - private System.Windows.Forms.TextBox tbUnitName; - private System.Windows.Forms.NumericUpDown unitSize; - private System.Windows.Forms.Label lblUnitSize; - private System.Windows.Forms.Button bttnAddWeapon; - private System.Windows.Forms.Button bttnRemoveWeapon; - private System.Windows.Forms.Button bttnEditWeapon; - private System.Windows.Forms.Label lblRequiredEquip; - private System.Windows.Forms.ListBox reqdList; - private System.Windows.Forms.ListBox optList; - private System.Windows.Forms.Label lblOptionalEquip; - private System.Windows.Forms.Button bttnReplaceWeapon; - private System.Windows.Forms.Button bttnEditReqdWeapon; - /// - /// Required designer variable. - /// - private System.ComponentModel.Container components = null; - - public FrmUnit(Unit toDisplay, CommandStack cmdStack) - { - unit = toDisplay; - commandStack = cmdStack; - // - // Required for Windows Form Designer support - // - InitializeComponent(); - - tbUnitName.Text = unit.Name; - Text = unit.Name; - unit.NameChanged+=new StringValChangedDelegate(unit_NameChanged); - unit.UnitSizeChanged+= new IntValChangedDelegate(unit_UnitSizeChanged); - unit.UnitEquipmentAmountChanged+=new DoubleValChangedDelegate(unit_UnitEquipmentAmountChanged); - - if (unit.UnitType.MaxSize==unit.UnitType.MinSize) - { - unitSize.Value = unit.UnitType.MaxSize; - unitSize.Visible = false; - lblUnitSize.Visible = false; - } - else - { - unitSize.Value = unit.Size; - unitSize.Maximum = (unit.UnitType.MaxSize == WarFoundryCore.INFINITY ? int.MaxValue : unit.UnitType.MaxSize); - unitSize.Minimum = unit.UnitType.MinSize; - } - - SetStats(); - SetWeapons(); - } - - private void SetStats() - { - DataTable dt = new DataTable(); - Stat[] stats = unit.UnitStatsArrayWithName; - DataColumn[] dc = new DataColumn[stats.Length+1]; - dc[0] = new DataColumn("name"); - - DataGridTableStyle dgStyle = new DataGridTableStyle(); - dgStyle.RowHeadersVisible = false; - - DataGridTextBoxColumn colStyle = new DataGridTextBoxColumn(); - colStyle.Width = statsGrid.ClientSize.Width - (stats.Length * 40) - 4; - colStyle.MappingName = "name"; - colStyle.HeaderText = "name"; - colStyle.ReadOnly = true; - dgStyle.GridColumnStyles.Add(colStyle); - - DataColumn tempCol; - int i = 1; - - foreach (Stat stat in stats) - { - tempCol = new DataColumn(stat.ParentSlotName); - dc[i] = tempCol; - colStyle = new DataGridTextBoxColumn(); - colStyle.Alignment = HorizontalAlignment.Center; - colStyle.Width = 40; - colStyle.MappingName = stat.ParentSlotName; - colStyle.HeaderText = stat.ParentSlotName; - colStyle.ReadOnly = true; - dgStyle.GridColumnStyles.Add(colStyle); - i++; - } - - dt.Columns.AddRange(dc); - - DataRow dr = dt.NewRow(); - dr.ItemArray = unit.UnitStatsArrayWithName; - dt.Rows.Add(dr); - statsGrid.DataSource = dt; - statsGrid.TableStyles.Add(dgStyle); - } - - private void SetWeapons() - { - foreach(UnitEquipmentItem item in unit.GetEquipment()) - { - if (item.IsRequired) - { - reqdList.Items.Add(GetEquipmentChoice(item)); - } - else - { - optList.Items.Add(GetEquipmentChoice(item)); - } - } - } - - private UnitEquipmentChoice GetEquipmentChoice(UnitEquipmentItem item) - { - UnitEquipmentChoice choice = null; - equipmentChoices.TryGetValue(item, out choice); - - if (choice == null) - { - choice = new UnitEquipmentChoice(Unit, item); - equipmentChoices[item] = choice; - } - - return choice; - } - - /// - /// Clean up any resources being used. - /// - protected override void Dispose( bool disposing ) - { - //remove our leave events so that disposing doesn't trigger them - tbUnitName.Leave-= new System.EventHandler(this.tbUnitName_Leave); - unitSize.Leave-= new System.EventHandler(this.unitSize_Leave); - - if( disposing ) - { - if(components != null) - { - components.Dispose(); - } - } - base.Dispose( disposing ); - } - - #region Windows Form Designer generated code - /// - /// Required method for Designer support - do not modify - /// the contents of this method with the code editor. - /// - private void InitializeComponent() - { - this.statsGrid = new System.Windows.Forms.DataGrid(); - this.tbUnitName = new System.Windows.Forms.TextBox(); - this.unitSize = new System.Windows.Forms.NumericUpDown(); - this.lblUnitSize = new System.Windows.Forms.Label(); - this.lblRequiredEquip = new System.Windows.Forms.Label(); - this.bttnAddWeapon = new System.Windows.Forms.Button(); - this.bttnRemoveWeapon = new System.Windows.Forms.Button(); - this.reqdList = new System.Windows.Forms.ListBox(); - this.bttnEditWeapon = new System.Windows.Forms.Button(); - this.optList = new System.Windows.Forms.ListBox(); - this.lblOptionalEquip = new System.Windows.Forms.Label(); - this.bttnReplaceWeapon = new System.Windows.Forms.Button(); - this.bttnEditReqdWeapon = new System.Windows.Forms.Button(); - ((System.ComponentModel.ISupportInitialize)(this.statsGrid)).BeginInit(); - ((System.ComponentModel.ISupportInitialize)(this.unitSize)).BeginInit(); - this.SuspendLayout(); - // - // statsGrid - // - this.statsGrid.AllowNavigation = false; - this.statsGrid.AllowSorting = false; - this.statsGrid.AlternatingBackColor = System.Drawing.SystemColors.Control; - this.statsGrid.BackgroundColor = System.Drawing.SystemColors.Control; - this.statsGrid.CaptionVisible = false; - this.statsGrid.DataMember = ""; - this.statsGrid.GridLineColor = System.Drawing.SystemColors.ControlDarkDark; - this.statsGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText; - this.statsGrid.Location = new System.Drawing.Point(8, 32); - this.statsGrid.Name = "statsGrid"; - this.statsGrid.PreferredColumnWidth = 40; - this.statsGrid.ReadOnly = true; - this.statsGrid.RowHeadersVisible = false; - this.statsGrid.SelectionBackColor = System.Drawing.SystemColors.Control; - this.statsGrid.SelectionForeColor = System.Drawing.SystemColors.WindowText; - this.statsGrid.Size = new System.Drawing.Size(600, 88); - this.statsGrid.TabIndex = 0; - this.statsGrid.TabStop = false; - // - // tbUnitName - // - this.tbUnitName.Location = new System.Drawing.Point(8, 8); - this.tbUnitName.Name = "tbUnitName"; - this.tbUnitName.Size = new System.Drawing.Size(344, 20); - this.tbUnitName.TabIndex = 1; - this.tbUnitName.Text = ""; - this.tbUnitName.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tbUnitName_KeyDown); - this.tbUnitName.Leave += new System.EventHandler(this.tbUnitName_Leave); - // - // unitSize - // - this.unitSize.Location = new System.Drawing.Point(528, 8); - this.unitSize.Name = "unitSize"; - this.unitSize.Size = new System.Drawing.Size(80, 20); - this.unitSize.TabIndex = 1; - this.unitSize.TextAlign = System.Windows.Forms.HorizontalAlignment.Right; - this.unitSize.Value = new System.Decimal(new int[] { - 1, - 0, - 0, - 0}); - this.unitSize.KeyDown += new System.Windows.Forms.KeyEventHandler(this.unitSize_KeyDown); - this.unitSize.Leave += new System.EventHandler(this.unitSize_Leave); - // - // lblUnitSize - // - this.lblUnitSize.Location = new System.Drawing.Point(424, 8); - this.lblUnitSize.Name = "lblUnitSize"; - this.lblUnitSize.TabIndex = 0; - this.lblUnitSize.Text = "unit size"; - this.lblUnitSize.TextAlign = System.Drawing.ContentAlignment.TopRight; - // - // lblRequiredEquip - // - this.lblRequiredEquip.Location = new System.Drawing.Point(8, 128); - this.lblRequiredEquip.Name = "lblRequiredEquip"; - this.lblRequiredEquip.Size = new System.Drawing.Size(88, 32); - this.lblRequiredEquip.TabIndex = 3; - this.lblRequiredEquip.Text = "reqd equipment"; - this.lblRequiredEquip.TextAlign = System.Drawing.ContentAlignment.TopRight; - // - // bttnAddWeapon - // - this.bttnAddWeapon.FlatStyle = System.Windows.Forms.FlatStyle.System; - this.bttnAddWeapon.Location = new System.Drawing.Point(520, 200); - this.bttnAddWeapon.Name = "bttnAddWeapon"; - this.bttnAddWeapon.Size = new System.Drawing.Size(88, 22); - this.bttnAddWeapon.TabIndex = 4; - this.bttnAddWeapon.Text = "add"; - this.bttnAddWeapon.Click += new System.EventHandler(this.bttnAddWeapon_Click); - // - // bttnRemoveWeapon - // - this.bttnRemoveWeapon.Enabled = false; - this.bttnRemoveWeapon.FlatStyle = System.Windows.Forms.FlatStyle.System; - this.bttnRemoveWeapon.Location = new System.Drawing.Point(520, 248); - this.bttnRemoveWeapon.Name = "bttnRemoveWeapon"; - this.bttnRemoveWeapon.Size = new System.Drawing.Size(88, 22); - this.bttnRemoveWeapon.TabIndex = 5; - this.bttnRemoveWeapon.Text = "remove"; - this.bttnRemoveWeapon.Click += new System.EventHandler(this.bttnRemoveWeapon_Click); - // - // reqdList - // - this.reqdList.Location = new System.Drawing.Point(104, 128); - this.reqdList.Name = "reqdList"; - this.reqdList.Size = new System.Drawing.Size(408, 69); - this.reqdList.TabIndex = 6; - this.reqdList.DoubleClick += new System.EventHandler(this.reqdList_DoubleClick); - this.reqdList.SelectedIndexChanged += new System.EventHandler(this.reqdList_SelectedIndexChanged); - // - // bttnEditWeapon - // - this.bttnEditWeapon.Enabled = false; - this.bttnEditWeapon.FlatStyle = System.Windows.Forms.FlatStyle.System; - this.bttnEditWeapon.Location = new System.Drawing.Point(520, 224); - this.bttnEditWeapon.Name = "bttnEditWeapon"; - this.bttnEditWeapon.Size = new System.Drawing.Size(88, 22); - this.bttnEditWeapon.TabIndex = 7; - this.bttnEditWeapon.Text = "edit"; - this.bttnEditWeapon.Click += new System.EventHandler(this.bttnEditWeapon_Click); - // - // optList - // - this.optList.Location = new System.Drawing.Point(104, 200); - this.optList.Name = "optList"; - this.optList.Size = new System.Drawing.Size(408, 69); - this.optList.TabIndex = 9; - this.optList.DoubleClick += new System.EventHandler(this.optList_DoubleClick); - this.optList.SelectedIndexChanged += new System.EventHandler(this.optList_SelectedIndexChanged); - // - // lblOptionalEquip - // - this.lblOptionalEquip.Location = new System.Drawing.Point(8, 200); - this.lblOptionalEquip.Name = "lblOptionalEquip"; - this.lblOptionalEquip.Size = new System.Drawing.Size(88, 32); - this.lblOptionalEquip.TabIndex = 8; - this.lblOptionalEquip.Text = "opt equipment"; - this.lblOptionalEquip.TextAlign = System.Drawing.ContentAlignment.TopRight; - // - // bttnReplaceWeapon - // - this.bttnReplaceWeapon.Enabled = false; - this.bttnReplaceWeapon.FlatStyle = System.Windows.Forms.FlatStyle.System; - this.bttnReplaceWeapon.Location = new System.Drawing.Point(520, 128); - this.bttnReplaceWeapon.Name = "bttnReplaceWeapon"; - this.bttnReplaceWeapon.Size = new System.Drawing.Size(88, 22); - this.bttnReplaceWeapon.TabIndex = 10; - this.bttnReplaceWeapon.Text = "replace"; - this.bttnReplaceWeapon.Click += new System.EventHandler(this.bttnReplaceWeapon_Click); - // - // bttnEditReqdWeapon - // - this.bttnEditReqdWeapon.Enabled = false; - this.bttnEditReqdWeapon.FlatStyle = System.Windows.Forms.FlatStyle.System; - this.bttnEditReqdWeapon.Location = new System.Drawing.Point(520, 152); - this.bttnEditReqdWeapon.Name = "bttnEditReqdWeapon"; - this.bttnEditReqdWeapon.Size = new System.Drawing.Size(88, 22); - this.bttnEditReqdWeapon.TabIndex = 11; - this.bttnEditReqdWeapon.Text = "edit"; - this.bttnEditReqdWeapon.Click += new System.EventHandler(this.bttnEditReqdWeapon_Click); - // - // FrmUnit - // - this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); - this.ClientSize = new System.Drawing.Size(616, 314); - this.Controls.Add(this.bttnEditReqdWeapon); - this.Controls.Add(this.bttnReplaceWeapon); - this.Controls.Add(this.optList); - this.Controls.Add(this.lblOptionalEquip); - this.Controls.Add(this.bttnEditWeapon); - this.Controls.Add(this.reqdList); - this.Controls.Add(this.bttnRemoveWeapon); - this.Controls.Add(this.bttnAddWeapon); - this.Controls.Add(this.lblRequiredEquip); - this.Controls.Add(this.lblUnitSize); - this.Controls.Add(this.unitSize); - this.Controls.Add(this.tbUnitName); - this.Controls.Add(this.statsGrid); - this.Name = "FrmUnit"; - this.ShowInTaskbar = false; - this.Text = "FrmUnit"; - ((System.ComponentModel.ISupportInitialize)(this.statsGrid)).EndInit(); - ((System.ComponentModel.ISupportInitialize)(this.unitSize)).EndInit(); - this.ResumeLayout(false); - - } - #endregion - - public Unit Unit - { - get { return unit; } - } - - private void tbUnitName_Leave(object sender, System.EventArgs e) - { - updateUnitName(); - } - - private void tbUnitName_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) - { - if (e.KeyCode == Keys.Enter) - { - updateUnitName(); - } - } - - private void updateUnitName() - { - if (unit.Name!=tbUnitName.Text) - { - commandStack.Execute(new SetNameCommand(unit, tbUnitName.Text)); - } - } - - private void unitSize_Leave(object sender, System.EventArgs e) - { - updateUnitSize(); - } - - private void unitSize_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) - { - if (e.KeyCode == Keys.Enter) - { - updateUnitSize(); - } - } - - private void updateUnitSize() - { - if (unit.Size!=unitSize.Value) - { - commandStack.Execute(new SetUnitSizeCommand(unit, (int)unitSize.Value)); - } - } - - private void unit_NameChanged(WarFoundryObject obj, string oldValue, string newValue) - { - if (obj is Unit && obj.Equals(unit)) - { - Unit u = (Unit)obj; - tbUnitName.Text = obj.Name; - Text = obj.Name; - } - } - - private void unit_UnitSizeChanged(WarFoundryObject obj, int oldValue, int newValue) - { - if (obj is Unit && obj.Equals(unit)) - { - unitSize.Value = newValue; - } - } - - private void reqdList_SelectedIndexChanged(object sender, System.EventArgs e) - { - bttnReplaceWeapon.Enabled = (reqdList.SelectedIndex>-1 && ((UnitEquipmentChoice)reqdList.SelectedItem).Item.HasAlternatives()); - bttnEditReqdWeapon.Enabled = (reqdList.SelectedIndex>-1); - } - - private void optList_SelectedIndexChanged(object sender, System.EventArgs e) - { - bttnEditWeapon.Enabled = optList.SelectedIndex>-1; - bttnRemoveWeapon.Enabled = bttnEditWeapon.Enabled; - } - - private void unit_UnitEquipmentAmountChanged(WarFoundryObject obj, double oldValue, double newValue) - { - if (obj is UnitEquipmentItem) - { - UnitEquipmentItem equip = (UnitEquipmentItem)obj; - ListBox weaponList = (equip.IsRequired ? reqdList : optList); - - if (newValue==0) - { - weaponList.Items.Remove(GetEquipmentChoice(equip)); - } - else - { - UnitEquipmentChoice equipObj = GetEquipmentChoice(equip); - int idx = weaponList.Items.IndexOf(equipObj); - - if (idx>-1) - { - weaponList.Items[idx] = equipObj; - } - else - { - weaponList.Items.Add(equipObj); - } - } - } - } - - private void editWeapon(ListBox list) - { - FrmEditUnitEquipment editEquip = new FrmEditUnitEquipment(Unit, ((UnitEquipmentChoice)list.SelectedItem).Item, commandStack); - editEquip.ShowDialog(this); - } - - private void bttnEditWeapon_Click(object sender, System.EventArgs e) - { - editWeapon(optList); - } - - private void optList_DoubleClick(object sender, System.EventArgs e) - { - editWeapon(optList); - } - - private void reqdList_DoubleClick(object sender, System.EventArgs e) - { - editWeapon(reqdList); - } - - private void addWeapon() - { - FrmNewUnitEquipment newEquip = new FrmNewUnitEquipment(Unit, commandStack); - newEquip.ShowDialog(this); - } - - private void bttnAddWeapon_Click(object sender, System.EventArgs e) - { - addWeapon(); - } - - private void removeWeapon() - { - commandStack.Execute(new SetUnitEquipmentAmountCommand(unit, ((UnitEquipmentChoice)optList.SelectedItem).Item, 0)); - } - - private void bttnRemoveWeapon_Click(object sender, System.EventArgs e) - { - removeWeapon(); - } - - private void bttnEditReqdWeapon_Click(object sender, System.EventArgs e) - { - editWeapon(reqdList); - } - - private void bttnReplaceWeapon_Click(object sender, System.EventArgs e) - { - FrmReplaceUnitEquipment replace = new FrmReplaceUnitEquipment(unit, ((UnitEquipmentChoice)reqdList.SelectedItem).Item, commandStack); - replace.ShowDialog(this); - } - } -} +// This file (FrmUnit.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 under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license. + +using System; +using System.Drawing; +using System.Data; +using System.Collections.Generic; +using System.ComponentModel; +using System.Windows.Forms; +using IBBoard.Commands; +using IBBoard.Lang; +using IBBoard.Windows.Forms; +using IBBoard.WarFoundry.API; +using IBBoard.WarFoundry.API.Commands; +using IBBoard.WarFoundry.API.Objects; +using IBBoard.WarFoundry.WinForms.UI; +using IBBoard.WarFoundry.GUI.WinForms.Util; + +namespace IBBoard.WarFoundry.WinForms +{ + ///TODO: Separate weapons out into optional and required, where required only has button for replacing + + /// + /// Summary description for FrmUnit. + /// + public class FrmUnit : IBBoard.Windows.Forms.IBBForm + { + private Unit unit; + private Dictionary equipmentChoices = new Dictionary(); + private CommandStack commandStack; + private System.Windows.Forms.DataGrid statsGrid; + private System.Windows.Forms.TextBox tbUnitName; + private System.Windows.Forms.NumericUpDown unitSize; + private System.Windows.Forms.Label lblUnitSize; + private System.Windows.Forms.Button bttnAddWeapon; + private System.Windows.Forms.Button bttnRemoveWeapon; + private System.Windows.Forms.Button bttnEditWeapon; + private System.Windows.Forms.Label lblRequiredEquip; + private System.Windows.Forms.ListBox reqdList; + private System.Windows.Forms.ListBox optList; + private System.Windows.Forms.Label lblOptionalEquip; + private System.Windows.Forms.Button bttnReplaceWeapon; + private System.Windows.Forms.Button bttnEditReqdWeapon; + /// + /// Required designer variable. + /// + private System.ComponentModel.Container components = null; + + public FrmUnit(Unit toDisplay, CommandStack cmdStack) + { + unit = toDisplay; + commandStack = cmdStack; + // + // Required for Windows Form Designer support + // + InitializeComponent(); + + tbUnitName.Text = unit.Name; + Text = unit.Name; + unit.NameChanged += new StringValChangedDelegate(unit_NameChanged); + unit.UnitSizeChanged += new IntValChangedDelegate(unit_UnitSizeChanged); + unit.UnitEquipmentAmountChanged += new DoubleValChangedDelegate(unit_UnitEquipmentAmountChanged); + + if (unit.UnitType.MaxSize == unit.UnitType.MinSize) + { + unitSize.Value = unit.UnitType.MaxSize; + unitSize.Visible = false; + lblUnitSize.Visible = false; + } + else + { + unitSize.Value = unit.Size; + unitSize.Maximum = (unit.UnitType.MaxSize == WarFoundryCore.INFINITY ? int.MaxValue : unit.UnitType.MaxSize); + unitSize.Minimum = unit.UnitType.MinSize; + } + + SetStats(); + SetWeapons(); + } + + private void SetStats() + { + DataTable dt = new DataTable(); + Stat[] stats = unit.UnitStatsArrayWithName; + int statsCount = stats.Length; + DataColumn[] dc = new DataColumn[statsCount]; + + DataGridTableStyle dgStyle = new DataGridTableStyle(); + dgStyle.RowHeadersVisible = false; + + Stat stat = stats[0]; + DataColumn tempCol = new DataColumn(stat.ParentSlotName); + tempCol.DataType = stat.GetType(); + + for (int i = 0; i < statsCount; i++) + { + stat = stats[i]; + tempCol = new DataColumn(stat.ParentSlotName); + tempCol.DataType = stat.GetType(); + dc[i] = tempCol; + DataGridColumnStyle colStyle = new StatColumnStyle(); + colStyle.Width = 40; + colStyle.MappingName = stat.ParentSlotName; + colStyle.HeaderText = stat.ParentSlotName; + colStyle.Alignment = HorizontalAlignment.Center; + colStyle.ReadOnly = true; + dgStyle.GridColumnStyles.Add(colStyle); + } + + DataGridColumnStyle nameColStyle = dgStyle.GridColumnStyles[0]; + nameColStyle.HeaderText = Translation.GetTranslation("UnitName", "Name"); + nameColStyle.Alignment = HorizontalAlignment.Left; + nameColStyle.Width = statsGrid.ClientSize.Width - ((stats.Length - 1) * 40) - 4; + + dt.Columns.AddRange(dc); + + DataRow dr = dt.NewRow(); + dr.ItemArray = stats; + dt.Rows.Add(dr); + statsGrid.DataSource = dt; + statsGrid.TableStyles.Add(dgStyle); + } + + private void SetWeapons() + { + foreach (UnitEquipmentItem item in unit.GetEquipment()) + { + if (item.IsRequired) + { + reqdList.Items.Add(GetEquipmentChoice(item)); + } + else + { + optList.Items.Add(GetEquipmentChoice(item)); + } + } + } + + private UnitEquipmentChoice GetEquipmentChoice(UnitEquipmentItem item) + { + UnitEquipmentChoice choice = null; + equipmentChoices.TryGetValue(item, out choice); + + if (choice == null) + { + choice = new UnitEquipmentChoice(Unit, item); + equipmentChoices[item] = choice; + } + + return choice; + } + + /// + /// Clean up any resources being used. + /// + protected override void Dispose(bool disposing) + { + //remove our leave events so that disposing doesn't trigger them + tbUnitName.Leave -= new System.EventHandler(this.tbUnitName_Leave); + unitSize.Leave -= new System.EventHandler(this.unitSize_Leave); + + if (disposing) + { + if (components != null) + { + components.Dispose(); + } + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.statsGrid = new System.Windows.Forms.DataGrid(); + this.tbUnitName = new System.Windows.Forms.TextBox(); + this.unitSize = new System.Windows.Forms.NumericUpDown(); + this.lblUnitSize = new System.Windows.Forms.Label(); + this.lblRequiredEquip = new System.Windows.Forms.Label(); + this.bttnAddWeapon = new System.Windows.Forms.Button(); + this.bttnRemoveWeapon = new System.Windows.Forms.Button(); + this.reqdList = new System.Windows.Forms.ListBox(); + this.bttnEditWeapon = new System.Windows.Forms.Button(); + this.optList = new System.Windows.Forms.ListBox(); + this.lblOptionalEquip = new System.Windows.Forms.Label(); + this.bttnReplaceWeapon = new System.Windows.Forms.Button(); + this.bttnEditReqdWeapon = new System.Windows.Forms.Button(); + ((System.ComponentModel.ISupportInitialize) (this.statsGrid)).BeginInit(); + ((System.ComponentModel.ISupportInitialize) (this.unitSize)).BeginInit(); + this.SuspendLayout(); + // + // statsGrid + // + this.statsGrid.AllowNavigation = false; + this.statsGrid.AllowSorting = false; + this.statsGrid.AlternatingBackColor = System.Drawing.SystemColors.Control; + this.statsGrid.BackgroundColor = System.Drawing.SystemColors.Control; + this.statsGrid.CaptionVisible = false; + this.statsGrid.DataMember = ""; + this.statsGrid.GridLineColor = System.Drawing.SystemColors.ControlDarkDark; + this.statsGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText; + this.statsGrid.Location = new System.Drawing.Point(8, 32); + this.statsGrid.Name = "statsGrid"; + this.statsGrid.PreferredColumnWidth = 40; + this.statsGrid.ReadOnly = true; + this.statsGrid.RowHeadersVisible = false; + this.statsGrid.SelectionBackColor = System.Drawing.SystemColors.Control; + this.statsGrid.SelectionForeColor = System.Drawing.SystemColors.WindowText; + this.statsGrid.Size = new System.Drawing.Size(600, 88); + this.statsGrid.TabIndex = 0; + this.statsGrid.TabStop = false; + // + // tbUnitName + // + this.tbUnitName.Location = new System.Drawing.Point(8, 8); + this.tbUnitName.Name = "tbUnitName"; + this.tbUnitName.Size = new System.Drawing.Size(344, 20); + this.tbUnitName.TabIndex = 1; + this.tbUnitName.Text = ""; + this.tbUnitName.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tbUnitName_KeyDown); + this.tbUnitName.Leave += new System.EventHandler(this.tbUnitName_Leave); + // + // unitSize + // + this.unitSize.Location = new System.Drawing.Point(528, 8); + this.unitSize.Name = "unitSize"; + this.unitSize.Size = new System.Drawing.Size(80, 20); + this.unitSize.TabIndex = 1; + this.unitSize.TextAlign = System.Windows.Forms.HorizontalAlignment.Right; + this.unitSize.Value = new System.Decimal(new int[] { + 1, + 0, + 0, + 0}); + this.unitSize.KeyDown += new System.Windows.Forms.KeyEventHandler(this.unitSize_KeyDown); + this.unitSize.Leave += new System.EventHandler(this.unitSize_Leave); + // + // lblUnitSize + // + this.lblUnitSize.Location = new System.Drawing.Point(424, 8); + this.lblUnitSize.Name = "lblUnitSize"; + this.lblUnitSize.TabIndex = 0; + this.lblUnitSize.Text = "unit size"; + this.lblUnitSize.TextAlign = System.Drawing.ContentAlignment.TopRight; + // + // lblRequiredEquip + // + this.lblRequiredEquip.Location = new System.Drawing.Point(8, 128); + this.lblRequiredEquip.Name = "lblRequiredEquip"; + this.lblRequiredEquip.Size = new System.Drawing.Size(88, 32); + this.lblRequiredEquip.TabIndex = 3; + this.lblRequiredEquip.Text = "reqd equipment"; + this.lblRequiredEquip.TextAlign = System.Drawing.ContentAlignment.TopRight; + // + // bttnAddWeapon + // + this.bttnAddWeapon.FlatStyle = System.Windows.Forms.FlatStyle.System; + this.bttnAddWeapon.Location = new System.Drawing.Point(520, 200); + this.bttnAddWeapon.Name = "bttnAddWeapon"; + this.bttnAddWeapon.Size = new System.Drawing.Size(88, 22); + this.bttnAddWeapon.TabIndex = 4; + this.bttnAddWeapon.Text = "add"; + this.bttnAddWeapon.Click += new System.EventHandler(this.bttnAddWeapon_Click); + // + // bttnRemoveWeapon + // + this.bttnRemoveWeapon.Enabled = false; + this.bttnRemoveWeapon.FlatStyle = System.Windows.Forms.FlatStyle.System; + this.bttnRemoveWeapon.Location = new System.Drawing.Point(520, 248); + this.bttnRemoveWeapon.Name = "bttnRemoveWeapon"; + this.bttnRemoveWeapon.Size = new System.Drawing.Size(88, 22); + this.bttnRemoveWeapon.TabIndex = 5; + this.bttnRemoveWeapon.Text = "remove"; + this.bttnRemoveWeapon.Click += new System.EventHandler(this.bttnRemoveWeapon_Click); + // + // reqdList + // + this.reqdList.Location = new System.Drawing.Point(104, 128); + this.reqdList.Name = "reqdList"; + this.reqdList.Size = new System.Drawing.Size(408, 69); + this.reqdList.TabIndex = 6; + this.reqdList.DoubleClick += new System.EventHandler(this.reqdList_DoubleClick); + this.reqdList.SelectedIndexChanged += new System.EventHandler(this.reqdList_SelectedIndexChanged); + // + // bttnEditWeapon + // + this.bttnEditWeapon.Enabled = false; + this.bttnEditWeapon.FlatStyle = System.Windows.Forms.FlatStyle.System; + this.bttnEditWeapon.Location = new System.Drawing.Point(520, 224); + this.bttnEditWeapon.Name = "bttnEditWeapon"; + this.bttnEditWeapon.Size = new System.Drawing.Size(88, 22); + this.bttnEditWeapon.TabIndex = 7; + this.bttnEditWeapon.Text = "edit"; + this.bttnEditWeapon.Click += new System.EventHandler(this.bttnEditWeapon_Click); + // + // optList + // + this.optList.Location = new System.Drawing.Point(104, 200); + this.optList.Name = "optList"; + this.optList.Size = new System.Drawing.Size(408, 69); + this.optList.TabIndex = 9; + this.optList.DoubleClick += new System.EventHandler(this.optList_DoubleClick); + this.optList.SelectedIndexChanged += new System.EventHandler(this.optList_SelectedIndexChanged); + // + // lblOptionalEquip + // + this.lblOptionalEquip.Location = new System.Drawing.Point(8, 200); + this.lblOptionalEquip.Name = "lblOptionalEquip"; + this.lblOptionalEquip.Size = new System.Drawing.Size(88, 32); + this.lblOptionalEquip.TabIndex = 8; + this.lblOptionalEquip.Text = "opt equipment"; + this.lblOptionalEquip.TextAlign = System.Drawing.ContentAlignment.TopRight; + // + // bttnReplaceWeapon + // + this.bttnReplaceWeapon.Enabled = false; + this.bttnReplaceWeapon.FlatStyle = System.Windows.Forms.FlatStyle.System; + this.bttnReplaceWeapon.Location = new System.Drawing.Point(520, 128); + this.bttnReplaceWeapon.Name = "bttnReplaceWeapon"; + this.bttnReplaceWeapon.Size = new System.Drawing.Size(88, 22); + this.bttnReplaceWeapon.TabIndex = 10; + this.bttnReplaceWeapon.Text = "replace"; + this.bttnReplaceWeapon.Click += new System.EventHandler(this.bttnReplaceWeapon_Click); + // + // bttnEditReqdWeapon + // + this.bttnEditReqdWeapon.Enabled = false; + this.bttnEditReqdWeapon.FlatStyle = System.Windows.Forms.FlatStyle.System; + this.bttnEditReqdWeapon.Location = new System.Drawing.Point(520, 152); + this.bttnEditReqdWeapon.Name = "bttnEditReqdWeapon"; + this.bttnEditReqdWeapon.Size = new System.Drawing.Size(88, 22); + this.bttnEditReqdWeapon.TabIndex = 11; + this.bttnEditReqdWeapon.Text = "edit"; + this.bttnEditReqdWeapon.Click += new System.EventHandler(this.bttnEditReqdWeapon_Click); + // + // FrmUnit + // + this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); + this.ClientSize = new System.Drawing.Size(616, 314); + this.Controls.Add(this.bttnEditReqdWeapon); + this.Controls.Add(this.bttnReplaceWeapon); + this.Controls.Add(this.optList); + this.Controls.Add(this.lblOptionalEquip); + this.Controls.Add(this.bttnEditWeapon); + this.Controls.Add(this.reqdList); + this.Controls.Add(this.bttnRemoveWeapon); + this.Controls.Add(this.bttnAddWeapon); + this.Controls.Add(this.lblRequiredEquip); + this.Controls.Add(this.lblUnitSize); + this.Controls.Add(this.unitSize); + this.Controls.Add(this.tbUnitName); + this.Controls.Add(this.statsGrid); + this.Name = "FrmUnit"; + this.ShowInTaskbar = false; + this.Text = "FrmUnit"; + ((System.ComponentModel.ISupportInitialize) (this.statsGrid)).EndInit(); + ((System.ComponentModel.ISupportInitialize) (this.unitSize)).EndInit(); + this.ResumeLayout(false); + + } + #endregion + + public Unit Unit + { + get { return unit; } + } + + private void tbUnitName_Leave(object sender, System.EventArgs e) + { + updateUnitName(); + } + + private void tbUnitName_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) + { + if (e.KeyCode == Keys.Enter) + { + updateUnitName(); + } + } + + private void updateUnitName() + { + if (unit.Name != tbUnitName.Text) + { + commandStack.Execute(new SetNameCommand(unit, tbUnitName.Text)); + } + } + + private void unitSize_Leave(object sender, System.EventArgs e) + { + updateUnitSize(); + } + + private void unitSize_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) + { + if (e.KeyCode == Keys.Enter) + { + updateUnitSize(); + } + } + + private void updateUnitSize() + { + if (unit.Size != unitSize.Value) + { + commandStack.Execute(new SetUnitSizeCommand(unit, (int) unitSize.Value)); + } + } + + private void unit_NameChanged(WarFoundryObject obj, string oldValue, string newValue) + { + if (obj is Unit && obj.Equals(unit)) + { + Unit u = (Unit) obj; + tbUnitName.Text = obj.Name; + Text = obj.Name; + } + } + + private void unit_UnitSizeChanged(WarFoundryObject obj, int oldValue, int newValue) + { + if (obj is Unit && obj.Equals(unit)) + { + unitSize.Value = newValue; + } + } + + private void reqdList_SelectedIndexChanged(object sender, System.EventArgs e) + { + bttnReplaceWeapon.Enabled = (reqdList.SelectedIndex > -1 && ((UnitEquipmentChoice) reqdList.SelectedItem).Item.HasAlternatives()); + bttnEditReqdWeapon.Enabled = (reqdList.SelectedIndex > -1); + } + + private void optList_SelectedIndexChanged(object sender, System.EventArgs e) + { + bttnEditWeapon.Enabled = optList.SelectedIndex > -1; + bttnRemoveWeapon.Enabled = bttnEditWeapon.Enabled; + } + + private void unit_UnitEquipmentAmountChanged(WarFoundryObject obj, double oldValue, double newValue) + { + if (obj is UnitEquipmentItem) + { + UnitEquipmentItem equip = (UnitEquipmentItem) obj; + ListBox weaponList = (equip.IsRequired ? reqdList : optList); + + if (newValue == 0) + { + weaponList.Items.Remove(GetEquipmentChoice(equip)); + } + else + { + UnitEquipmentChoice equipObj = GetEquipmentChoice(equip); + int idx = weaponList.Items.IndexOf(equipObj); + + if (idx > -1) + { + weaponList.Items[idx] = equipObj; + } + else + { + weaponList.Items.Add(equipObj); + } + } + } + } + + private void editWeapon(ListBox list) + { + FrmEditUnitEquipment editEquip = new FrmEditUnitEquipment(Unit, ((UnitEquipmentChoice) list.SelectedItem).Item, commandStack); + editEquip.ShowDialog(this); + } + + private void bttnEditWeapon_Click(object sender, System.EventArgs e) + { + editWeapon(optList); + } + + private void optList_DoubleClick(object sender, System.EventArgs e) + { + editWeapon(optList); + } + + private void reqdList_DoubleClick(object sender, System.EventArgs e) + { + editWeapon(reqdList); + } + + private void addWeapon() + { + FrmNewUnitEquipment newEquip = new FrmNewUnitEquipment(Unit, commandStack); + newEquip.ShowDialog(this); + } + + private void bttnAddWeapon_Click(object sender, System.EventArgs e) + { + addWeapon(); + } + + private void removeWeapon() + { + commandStack.Execute(new SetUnitEquipmentAmountCommand(unit, ((UnitEquipmentChoice) optList.SelectedItem).Item, 0)); + } + + private void bttnRemoveWeapon_Click(object sender, System.EventArgs e) + { + removeWeapon(); + } + + private void bttnEditReqdWeapon_Click(object sender, System.EventArgs e) + { + editWeapon(reqdList); + } + + private void bttnReplaceWeapon_Click(object sender, System.EventArgs e) + { + FrmReplaceUnitEquipment replace = new FrmReplaceUnitEquipment(unit, ((UnitEquipmentChoice) reqdList.SelectedItem).Item, commandStack); + replace.ShowDialog(this); + } + } +} \ No newline at end of file diff -r d9aca1dc95d2 -r 526fefefb16b IBBoard.WarFoundry.GUI.WinForms.csproj --- a/IBBoard.WarFoundry.GUI.WinForms.csproj Sun Jul 05 09:36:21 2009 +0000 +++ b/IBBoard.WarFoundry.GUI.WinForms.csproj Sat Aug 01 14:01:56 2009 +0000 @@ -19,7 +19,7 @@ WinExe IBBoard.WarFoundry OnBuildSuccess - IBBoard.WarFoundry.FrmMain + IBBoard.WarFoundry.WinForms.FrmMain @@ -146,6 +146,9 @@ Form + + Component + FrmArmyTree.cs diff -r d9aca1dc95d2 -r 526fefefb16b UI/StatColumnStyle.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/UI/StatColumnStyle.cs Sat Aug 01 14:01:56 2009 +0000 @@ -0,0 +1,32 @@ +using System; +using System.Drawing; +using System.Text; +using System.Windows.Forms; +using IBBoard.WarFoundry.API.Objects; + +namespace IBBoard.WarFoundry.WinForms.UI +{ + class StatColumnStyle : DataGridTextBoxColumn + { + protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, Brush backBrush, Brush foreBrush, bool alignToRight) + { + Object obj = GetColumnValueAtRow(source, rowNum); + Rectangle rect = bounds; + g.FillRectangle(backBrush, rect); + rect.Offset(0, 2); + rect.Height -= 2; + + if (obj is Stat) + { + Stat stat = (Stat) obj; + StringFormat stringFormat = new StringFormat(); + stringFormat.Alignment = (stat.ParentSlotName == "Name" ? StringAlignment.Near : StringAlignment.Center); + g.DrawString(stat.SlotValueString, this.DataGridTableStyle.DataGrid.Font, foreBrush, rect, stringFormat); + } + else + { + g.DrawString(NullText, this.DataGridTableStyle.DataGrid.Font, foreBrush, rect); + } + } + } +}