comparison FrmReplaceEquipment.cs @ 68:7028e24b67ec

Re #60: Add UI to add/remove/edit weapons in GTK * Add "Replace" dialog Note: Dialog null refs because of a bad assumption in the base class - base constructor calls SetupUI before Replace constructor has set all of its values
author IBBoard <dev@ibboard.co.uk>
date Wed, 03 Nov 2010 21:02:54 +0000
parents
children 4b82515586ac
comparison
equal deleted inserted replaced
67:a7306b5a229e 68:7028e24b67ec
1 // This file (FrmAddEquipment.cs) is a part of the IBBoard.WarFoundry.GUI.GTK project and is copyright 2010 IBBoard
2 //
3 // 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.
4
5 using System;
6 using IBBoard.WarFoundry.GUI.GTK.UIControl.Interfaces;
7 using IBBoard.WarFoundry.API.Objects;
8 using Gtk;
9 using IBBoard.WarFoundry.GUI.GTK.Util;
10 using IBBoard.GtkSharp;
11 using log4net.Repository.Hierarchy;
12 using log4net;
13
14 namespace IBBoard.WarFoundry.GUI.GTK
15 {
16 public partial class FrmReplaceEquipment : Dialog, IReplaceEquipmentUI
17 {
18 private static ILog log = LogManager.GetLogger(typeof(FrmReplaceEquipment));
19
20 public event SingleArgMethodInvoker<UnitEquipmentItem> UnitEquipmentItemChoiceChanged;
21
22 public event MethodInvoker UnitEquipmentAmountTypeChanged;
23
24 public event MethodInvoker UnitEquipmentAmountChanged;
25 private bool limitsEnabled = false;
26 private bool ratioLimited = false;
27
28 public FrmReplaceEquipment()
29 {
30 this.Build();
31 lstEquipment.Selection.Changed += OnSelectionChanged;
32 TreeViewColumn equipColumn = new TreeViewColumn();
33 equipColumn.Title = "Equipment";
34 CellRendererText equipCell = new CellRendererText();
35 equipColumn.PackStart(equipCell, true);
36 equipColumn.SetCellDataFunc(equipCell, GtkWarFoundryUtil.RenderWarFoundryObjectName);
37 lstEquipment.AppendColumn(equipColumn);
38 }
39
40 private void OnUnitEquipmentAmountChanged()
41 {
42 if (UnitEquipmentAmountChanged != null)
43 {
44 UnitEquipmentAmountChanged();
45 }
46 }
47
48 private void OnUnitEquipmentAmountTypeChanged()
49 {
50 if (UnitEquipmentAmountChanged != null)
51 {
52 UnitEquipmentAmountTypeChanged();
53 }
54 }
55
56 protected void OnSelectionChanged(object o, EventArgs e)
57 {
58 if (UnitEquipmentItemChoiceChanged != null)
59 {
60 UnitEquipmentItemChoiceChanged(SelectedUnitEquipmentItem);
61 }
62 }
63
64 public void SetUnitEquipmentItems(UnitEquipmentItem[] items)
65 {
66 ListStore store = new ListStore(typeof(UnitEquipmentItem));
67
68 foreach (UnitEquipmentItem equipItem in items)
69 {
70 store.AppendValues(equipItem);
71 }
72
73 lstEquipment.Model = store;
74 }
75
76 public void SetUnitEquipmentLimits(bool isRatioLimit, double minPercent, double maxPercent, int minNumber, int maxNumber)
77 {
78 log.DebugFormat("IsRatio? {0}. Limits: {1}->{2}, {3}%->{4}%", isRatioLimit, minNumber, maxNumber, minPercent, maxPercent);
79 ratioLimited = isRatioLimit;
80 numericAmount.SetRange(minNumber, maxNumber);
81 percentageAmount.SetRange(minPercent, maxPercent);
82
83 if (isRatioLimit)
84 {
85 if (minPercent == 100)
86 {
87 rbEquipAll.Active = true;
88 }
89 else
90 {
91 rbEquipPercent.Active = true;
92 }
93 }
94 else
95 {
96 rbEquipNumeric.Active = true;
97 }
98 }
99
100 public void SetUnitEquipmentLimitsEnabled(bool isEnabled)
101 {
102 SetNumericAmountEnabledState(isEnabled);
103 SetPercentageAmountEnabledState(isEnabled);
104 }
105
106 public bool ShowControl()
107 {
108 int result = Run();
109 bool okayClicked = (result == (int)ResponseType.Ok);
110 this.Hide();
111 return okayClicked;
112 }
113
114 protected virtual void CancelButtonClicked(object sender, System.EventArgs e)
115 {
116 log.Debug("Cancel clicked");
117 Respond(ResponseType.Cancel);
118 }
119
120 protected virtual void OkayButtonClicked(object sender, System.EventArgs e)
121 {
122 log.Debug("Okay clicked");
123 Respond(ResponseType.Ok);
124 }
125
126 public void SetOkayEnabledState(bool enabled)
127 {
128 buttonOk.Sensitive = enabled;
129 }
130
131 protected virtual void SpinButtonValueChanged(object sender, System.EventArgs e)
132 {
133 OnUnitEquipmentAmountChanged();
134 }
135
136 protected virtual void RadioButtonClicked(object sender, System.EventArgs e)
137 {
138 OnUnitEquipmentAmountTypeChanged();
139 }
140
141 public void SetNumericAmountEnabledState(bool enabled)
142 {
143 rbEquipNumeric.Sensitive = enabled;
144 numericAmount.Sensitive = enabled;
145 }
146
147 public void SetPercentageAmountEnabledState(bool enabled)
148 {
149 if (enabled)
150 {
151 double minPercentage = GetMinPercentage();
152 rbEquipPercent.Sensitive = minPercentage != 100;
153 percentageAmount.Sensitive = minPercentage != 100;
154 double maxPercentage = GetMaxPercentage();
155 rbEquipAll.Sensitive = ratioLimited && maxPercentage == 100;
156 lblEquipAll.Sensitive = ratioLimited && maxPercentage == 100;
157 }
158 else
159 {
160 rbEquipPercent.Sensitive = false;
161 percentageAmount.Sensitive = false;
162 rbEquipAll.Sensitive = false;
163 lblEquipAll.Sensitive = false;
164 }
165 }
166
167 private double GetMaxPercentage()
168 {
169 double min, max;
170 percentageAmount.GetRange(out min, out max);
171 return max;
172 }
173
174 private double GetMinPercentage()
175 {
176 double min, max;
177 percentageAmount.GetRange(out min, out max);
178 return min;
179 }
180
181 public UnitEquipmentItem SelectedUnitEquipmentItem
182 {
183 get
184 {
185 return (UnitEquipmentItem)TreeUtils.GetSelectedItem(lstEquipment);
186 }
187 }
188
189 public bool IsRatioEquipmentAmount
190 {
191 get
192 {
193 return !rbEquipNumeric.Active;
194 }
195 }
196
197 public int EquipmentNumericAmount
198 {
199 get
200 {
201 return (int)numericAmount.Value;
202 }
203
204 set
205 {
206 numericAmount.Value = value;
207 }
208 }
209
210 public double EquipmentPercentageAmount
211 {
212 get
213 {
214 double percent;
215
216 if (rbEquipAll.Active)
217 {
218 percent = 100;
219 }
220 else
221 {
222 percent = percentageAmount.Value;
223 }
224
225 return percent;
226 }
227
228 set
229 {
230 percentageAmount.Value = value;
231 }
232 }
233 }
234 }
235