view FrmAddEquipment.cs @ 60:04c0f6a7625c

Re #60: Add UI to add/remove/edit weapons in GTK * Implement Remove button * Hack "code-behind" file again to make things work, until MD bug is fixed
author IBBoard <dev@ibboard.co.uk>
date Fri, 27 Aug 2010 14:44:48 +0000
parents 0c5fbb54bfb0
children e7ad676a7344
line source
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 using System;
5 using IBBoard.WarFoundry.GUI.GTK.UIControl.Interfaces;
6 using IBBoard.WarFoundry.API.Objects;
7 using Gtk;
8 using IBBoard.WarFoundry.GUI.GTK.Util;
9 using IBBoard.GtkSharp;
10 using log4net.Repository.Hierarchy;
11 using log4net;
12 namespace IBBoard.WarFoundry.GUI.GTK
13 {
14 public partial class FrmAddEquipment : Dialog, IAddEquipmentUI
15 {
16 private static ILog log = LogManager.GetLogger(typeof(FrmAddEquipment));
18 public event SingleArgMethodInvoker<UnitEquipmentItem> UnitEquipmentItemChoiceChanged;
20 private bool limitsEnabled = false;
21 private bool ratioLimited = false;
23 public FrmAddEquipment()
24 {
25 this.Build();
26 lstEquipment.Selection.Changed += OnSelectionChanged;
27 TreeViewColumn equipColumn = new TreeViewColumn();
28 equipColumn.Title = "Equipment";
29 CellRendererText equipCell = new CellRendererText();
30 equipColumn.PackStart(equipCell, true);
31 equipColumn.SetCellDataFunc(equipCell, GtkWarFoundryUtil.RenderWarFoundryObjectName);
32 lstEquipment.AppendColumn(equipColumn);
33 }
35 protected virtual void OnSelectionChanged(object o, EventArgs e)
36 {
37 if (UnitEquipmentItemChoiceChanged!=null)
38 {
39 UnitEquipmentItemChoiceChanged(SelectedUnitEquipmentItem);
40 }
41 }
43 public void SetUnitEquipmentItems(UnitEquipmentItem[] items)
44 {
45 ListStore store = new ListStore(typeof(UnitEquipmentItem));
47 foreach (UnitEquipmentItem equipItem in items)
48 {
49 store.AppendValues(equipItem);
50 }
52 lstEquipment.Model = store;
53 }
55 public void SetUnitEquipmentLimits(bool isRatioLimit, double minPercent, double maxPercent, int minNumber, int maxNumber)
56 {
57 log.DebugFormat("IsRatio? {0}. Limits: {1}->{2}, {3}%->{4}%", isRatioLimit, minNumber, maxNumber, minPercent, maxPercent);
58 ratioLimited = isRatioLimit;
59 SetEnabledState();
60 numericAmount.SetRange(minNumber, maxNumber);
61 percentageAmount.SetRange(minPercent, maxPercent);
62 }
64 public void SetUnitEquipmentLimitsEnabled(bool isEnabled)
65 {
66 limitsEnabled = isEnabled;
67 SetEnabledState();
68 }
70 private void SetEnabledState()
71 {
72 rbEquipNumeric.Sensitive = (limitsEnabled && !ratioLimited);
73 numericAmount.Sensitive = (limitsEnabled && !ratioLimited);
74 double minPercentage = GetMinPercentage();
75 rbEquipPercent.Sensitive = limitsEnabled && minPercentage != 100;
76 percentageAmount.Sensitive = limitsEnabled && minPercentage != 100;
77 double maxPercentage = GetMaxPercentage();
78 rbEquipAll.Sensitive = limitsEnabled && maxPercentage == 100;
79 lblEquipAll.Sensitive = limitsEnabled && maxPercentage == 100;
80 }
82 private double GetMaxPercentage()
83 {
84 double min, max;
85 percentageAmount.GetRange(out min, out max);
86 return max;
87 }
89 private double GetMinPercentage()
90 {
91 double min, max;
92 percentageAmount.GetRange(out min, out max);
93 return min;
94 }
96 public bool ShowControl()
97 {
98 int result = Run();
99 bool okayClicked = (result == (int)ResponseType.Ok);
100 this.Hide();
101 return okayClicked;
102 }
104 protected virtual void CancelButtonClicked(object sender, System.EventArgs e)
105 {
106 log.Debug("Cancel clicked");
107 Respond(ResponseType.Cancel);
108 }
110 protected virtual void OkayButtonClicked(object sender, System.EventArgs e)
111 {
112 log.Debug("Okay clicked");
113 Respond(ResponseType.Ok);
114 }
116 public UnitEquipmentItem SelectedUnitEquipmentItem
117 {
118 get
119 {
120 return (UnitEquipmentItem)TreeUtils.GetSelectedItem(lstEquipment);
121 }
122 }
125 public bool IsRatioEquipmentAmount
126 {
127 get
128 {
129 return !rbEquipNumeric.Active;
130 }
131 }
134 public int EquipmentNumericAmount
135 {
136 get
137 {
138 return (int)numericAmount.Value;
139 }
140 }
143 public double EquipmentPercentageAmount
144 {
145 get
146 {
147 double percent;
149 if (rbEquipAll.Active)
150 {
151 percent = 100;
152 }
153 else
154 {
155 percent = percentageAmount.Value;
156 }
158 return percent;
159 }
160 }
161 }
162 }