# HG changeset patch # User IBBoard # Date 1282851022 0 # Node ID 293d204e40db5896fe8424b2e2ec4939fefcd6ab # Parent 0c5fbb54bfb0400af8d084cc45c0e53a53be278f Re #60: Add UI to add/remove/edit weapons in GTK * Make enabling/disabling fit the values passed (copied from existing WinForms checks) * Add extra interface methods and properties to get necessary values from UI to use in command * Implement new interface methods * Implement command execution * Make use of IDisposable interface instead of defining Dispose ourselves Also: * Use existing method of printing exception stack traces in main window diff -r 0c5fbb54bfb0 -r 293d204e40db FrmAddEquipment.cs --- a/FrmAddEquipment.cs Wed Aug 25 20:04:27 2010 +0000 +++ b/FrmAddEquipment.cs Thu Aug 26 19:30:22 2010 +0000 @@ -36,7 +36,7 @@ { if (UnitEquipmentItemChoiceChanged!=null) { - UnitEquipmentItemChoiceChanged((UnitEquipmentItem)TreeUtils.GetSelectedItem(lstEquipment)); + UnitEquipmentItemChoiceChanged(SelectedUnitEquipmentItem); } } @@ -69,12 +69,28 @@ private void SetEnabledState() { - rbEquipNumeric.Sensitive = (limitsEnabled && ratioLimited); - numericAmount.Sensitive = (limitsEnabled && ratioLimited); - rbEquipPercent.Sensitive = limitsEnabled; - percentageAmount.Sensitive = limitsEnabled; - rbEquipAll.Sensitive = limitsEnabled; - lblEquipAll.Sensitive = limitsEnabled; + rbEquipNumeric.Sensitive = (limitsEnabled && !ratioLimited); + numericAmount.Sensitive = (limitsEnabled && !ratioLimited); + double minPercentage = GetMinPercentage(); + rbEquipPercent.Sensitive = limitsEnabled && minPercentage != 100; + percentageAmount.Sensitive = limitsEnabled && minPercentage != 100; + double maxPercentage = GetMaxPercentage(); + rbEquipAll.Sensitive = limitsEnabled && maxPercentage == 100; + lblEquipAll.Sensitive = limitsEnabled && maxPercentage == 100; + } + + private double GetMaxPercentage() + { + double min, max; + percentageAmount.GetRange(out min, out max); + return max; + } + + private double GetMinPercentage() + { + double min, max; + percentageAmount.GetRange(out min, out max); + return min; } public bool ShowControl() @@ -96,6 +112,52 @@ log.Debug("Okay clicked"); Respond(ResponseType.Ok); } + + public UnitEquipmentItem SelectedUnitEquipmentItem + { + get + { + return (UnitEquipmentItem)TreeUtils.GetSelectedItem(lstEquipment); + } + } + + + public bool IsRatioEquipmentAmount + { + get + { + return !rbEquipNumeric.Active; + } + } + + + public int EquipmentNumericAmount + { + get + { + return (int)numericAmount.Value; + } + } + + + public double EquipmentPercentageAmount + { + get + { + double percent; + + if (rbEquipAll.Active) + { + percent = 100; + } + else + { + percent = percentageAmount.Value; + } + + return percent; + } + } } } diff -r 0c5fbb54bfb0 -r 293d204e40db FrmMainWindow.cs --- a/FrmMainWindow.cs Wed Aug 25 20:04:27 2010 +0000 +++ b/FrmMainWindow.cs Thu Aug 26 19:30:22 2010 +0000 @@ -90,9 +90,10 @@ private static void HandleUnhandledException(Exception ex) { - LogManager.GetLogger(typeof(FrmMainWindow)).FatalFormat("({0}) {1} {2} {3}", ex.GetType().FullName, ex.Message, Environment.NewLine, GetStackTrace(ex)); + string msg = String.Format("({0}) {1}", ex.GetType().FullName, ex.Message); + LogManager.GetLogger(typeof(FrmMainWindow)).Fatal(msg, ex); MessageDialog dialog = new MessageDialog(null, DialogFlags.Modal, MessageType.Error, ButtonsType.Ok, false, "An unhandled exception occurred. Please check the log for more details."); - dialog.Show(); + dialog.Run(); } private static string GetStackTrace(Exception ex) @@ -101,7 +102,7 @@ if (ex != null) { - message = "Caused by: " + ex.GetType().FullName + Environment.NewLine + ex.StackTrace + Environment.NewLine + GetStackTrace(ex.InnerException); + message = "Caused by: " + ex.GetType().FullName + Environment.NewLine + ex.StackTrace + Environment.NewLine ; } return message; diff -r 0c5fbb54bfb0 -r 293d204e40db UIControl/AddEquipmentUIControl.cs --- a/UIControl/AddEquipmentUIControl.cs Wed Aug 25 20:04:27 2010 +0000 +++ b/UIControl/AddEquipmentUIControl.cs Thu Aug 26 19:30:22 2010 +0000 @@ -6,6 +6,7 @@ using IBBoard.WarFoundry.API.Objects; using IBBoard.WarFoundry.API.Util; using IBBoard.WarFoundry.GUI.GTK.UIControl.Interfaces; +using IBBoard.WarFoundry.API.Commands; namespace IBBoard.WarFoundry.GUI.GTK.UIControl { public class AddEquipmentUIControl @@ -67,8 +68,22 @@ public void Show() { - ui.ShowControl(); - //Do command stuff here + bool okayed = ui.ShowControl(); + + if (okayed) + { + UnitEquipmentItem equipItem = ui.SelectedUnitEquipmentItem; + + if (ui.IsRatioEquipmentAmount) + { + commandStack.Execute(new SetUnitEquipmentRatioAmountCommand(unit, equipItem, ui.EquipmentPercentageAmount)); + } + else + { + commandStack.Execute(new SetUnitEquipmentNumericAmountCommand(unit, equipItem, ui.EquipmentNumericAmount)); + } + } + ui.Dispose(); } } diff -r 0c5fbb54bfb0 -r 293d204e40db UIControl/Interfaces/IAddEquipmentUI.cs --- a/UIControl/Interfaces/IAddEquipmentUI.cs Wed Aug 25 20:04:27 2010 +0000 +++ b/UIControl/Interfaces/IAddEquipmentUI.cs Thu Aug 26 19:30:22 2010 +0000 @@ -8,7 +8,7 @@ /// /// The interface that UI components should implement to represent "Add Equipment" dialogs or system equivalents (e.g. console areas or HTML fragments) /// - public interface IAddEquipmentUI + public interface IAddEquipmentUI : IDisposable { /// /// Should be fired when unit equipment item choice changes. @@ -58,19 +58,38 @@ /// true if the control was closed with "Okay", else false /// bool ShowControl(); - + /// - /// Releases all resource used by the object. + /// Gets the selected equipment item. + /// + /// + /// The selected equipment item. + /// + UnitEquipmentItem SelectedUnitEquipmentItem { get; } + + /// + /// Gets a value indicating whether the equipment amount is a ratio or an absolute number. /// - /// - /// Call when you are finished using the - /// . The method - /// leaves the in an unusable state. - /// After calling , you must release all references to the - /// so the garbage collector can reclaim - /// the memory that the was occupying. - /// - void Dispose(); + /// + /// true if the selected amount is a ratio type (percentage or "all"); otherwise, false. + /// + bool IsRatioEquipmentAmount { get; } + + /// + /// Gets the numeric amount for the current equipment amount. This number is meaningless if is true + /// + /// + /// The absolue number of items taken. + /// + int EquipmentNumericAmount { get; } + + /// + /// Gets the percentage amount for the current equipment amount. This number is meaningless if is false + /// + /// + /// The number of items taken as a percentage of the unit size. + /// + double EquipmentPercentageAmount { get; } } }