view FrmEditEquipment.cs @ 112:9a93ae9ad956

* Add simple shell script to make sure that we run through Mono and not Wine (Not necessary, but useful to save confusion) no-open-ticket
author IBBoard <dev@ibboard.co.uk>
date Sun, 07 Aug 2011 19:36:54 +0000
parents b4416ca69153
children 3ec818926241
line source
1 // This file (FrmEditEquipment.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.
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 using IBBoard.GtkSharp.Translatable;
14 using IBBoard.Lang;
16 namespace IBBoard.WarFoundry.GUI.GTK
17 {
18 public partial class FrmEditEquipment : TranslatableDialog, IEditEquipmentUI
19 {
20 private static ILog log = LogManager.GetLogger(typeof(FrmAddEquipment));
22 public event SingleArgMethodInvoker<UnitEquipmentItem> UnitEquipmentItemChoiceChanged;
23 public event MethodInvoker UnitEquipmentAmountTypeChanged;
24 public event MethodInvoker UnitEquipmentAmountChanged;
26 private bool isRatioLimited;
28 public FrmEditEquipment()
29 {
30 this.Build();
31 Translation.TranslationChanged += Retranslate;
32 Translate();
33 }
35 private void Retranslate()
36 {
37 Translate();
38 }
40 public override void Dispose()
41 {
42 Translation.TranslationChanged -= Retranslate;
43 base.Dispose();
44 }
46 public void ListenToWidgets()
47 {
48 rbEquipAll.Clicked += RadioButtonClicked;
49 rbEquipNumeric.Clicked += RadioButtonClicked;
50 rbEquipPercent.Clicked += RadioButtonClicked;
51 numericAmount.ValueChanged += SpinButtonValueChanged;
52 percentageAmount.ValueChanged += SpinButtonValueChanged;
53 }
55 public void IgnoreWidgets()
56 {
57 rbEquipAll.Clicked -= RadioButtonClicked;
58 rbEquipNumeric.Clicked -= RadioButtonClicked;
59 rbEquipPercent.Clicked -= RadioButtonClicked;
60 numericAmount.ValueChanged -= SpinButtonValueChanged;
61 percentageAmount.ValueChanged -= SpinButtonValueChanged;
62 }
64 private void OnUnitEquipmentAmountChanged()
65 {
66 if (UnitEquipmentAmountChanged != null)
67 {
68 UnitEquipmentAmountChanged();
69 }
70 }
72 private void OnUnitEquipmentAmountTypeChanged()
73 {
74 if (UnitEquipmentAmountChanged != null)
75 {
76 UnitEquipmentAmountTypeChanged();
77 }
78 }
80 public void SetUnitEquipmentLimits(bool isRatioLimit, double minPercent, double maxPercent, int minNumber, int maxNumber)
81 {
82 log.DebugFormat("IsRatio? {0}. Limits: {1}->{2}, {3}%->{4}%", isRatioLimit, minNumber, maxNumber, minPercent, maxPercent);
83 numericAmount.SetRange(minNumber, maxNumber);
84 percentageAmount.SetRange(minPercent, maxPercent);
85 SetEquipmentAmountType(isRatioLimit);
86 isRatioLimited = isRatioLimit;
87 }
89 public void SetEquipmentAmountType(bool isRatioAmount)
90 {
91 if (isRatioAmount)
92 {
93 if (percentageAmount.Value == 100)
94 {
95 rbEquipAll.Active = true;
96 }
97 else
98 {
99 rbEquipPercent.Active = true;
100 }
101 }
102 else
103 {
104 rbEquipNumeric.Active = true;
105 }
106 }
108 public void SetUnitEquipmentLimitsEnabled(bool isEnabled)
109 {
110 SetNumericAmountEnabledState(isEnabled);
111 SetPercentageAmountEnabledState(isEnabled);
112 }
114 public bool ShowControl()
115 {
116 int result = Run();
117 bool okayClicked = (result == (int)ResponseType.Ok);
118 this.Hide();
119 return okayClicked;
120 }
122 protected virtual void CancelButtonClicked(object sender, System.EventArgs e)
123 {
124 log.Debug("Cancel clicked");
125 Respond(ResponseType.Cancel);
126 }
128 protected virtual void OkayButtonClicked(object sender, System.EventArgs e)
129 {
130 log.Debug("Okay clicked");
131 Respond(ResponseType.Ok);
132 }
134 public void SetOkayEnabledState(bool enabled)
135 {
136 bttnOkay.Sensitive = enabled;
137 }
139 protected virtual void SpinButtonValueChanged(object sender, System.EventArgs e)
140 {
141 OnUnitEquipmentAmountChanged();
142 }
144 protected virtual void RadioButtonClicked(object sender, System.EventArgs e)
145 {
146 OnUnitEquipmentAmountTypeChanged();
147 }
149 public void SetNumericAmountEnabledState(bool enabled)
150 {
151 double minPercent = GetMinPercentage();
152 rbEquipNumeric.Sensitive = enabled && !(isRatioLimited && minPercent == 100);
153 numericAmount.Sensitive = rbEquipNumeric.Sensitive;
154 }
156 public void SetPercentageAmountEnabledState(bool enabled)
157 {
158 if (enabled)
159 {
160 double minPercentage = GetMinPercentage();
161 rbEquipPercent.Sensitive = isRatioLimited && minPercentage != 100;
162 percentageAmount.Sensitive = rbEquipPercent.Sensitive;
163 double maxPercentage = GetMaxPercentage();
164 rbEquipAll.Sensitive = isRatioLimited && maxPercentage == 100;
165 lblEquipAll.Sensitive = rbEquipAll.Sensitive;
166 }
167 else
168 {
169 rbEquipPercent.Sensitive = false;
170 percentageAmount.Sensitive = false;
171 rbEquipAll.Sensitive = false;
172 lblEquipAll.Sensitive = false;
173 }
174 }
176 private double GetMaxPercentage()
177 {
178 double min, max;
179 percentageAmount.GetRange(out min, out max);
180 return max;
181 }
183 private double GetMinPercentage()
184 {
185 double min, max;
186 percentageAmount.GetRange(out min, out max);
187 return min;
188 }
190 public bool IsRatioEquipmentAmount
191 {
192 get
193 {
194 return !rbEquipNumeric.Active;
195 }
196 }
198 public int EquipmentNumericAmount
199 {
200 get
201 {
202 return (int)numericAmount.Value;
203 }
205 set
206 {
207 numericAmount.Value = value;
208 }
209 }
211 public double EquipmentPercentageAmount
212 {
213 get
214 {
215 double percent;
217 if (rbEquipAll.Active)
218 {
219 percent = 100;
220 }
221 else
222 {
223 percent = percentageAmount.Value;
224 }
226 return percent;
227 }
229 set
230 {
231 percentageAmount.Value = value;
232 }
233 }
234 }
235 }