comparison mde/gui/widget/miscContent.d @ 112:fe061009029d

EnumContent; log level can be selected from a popup list. New EnumContent, with code to load translations in Items. Editable as an AStringContent. Hacked OptionsMisc to use an EnumContent. Implemented a EnumContentWidget providing a pop-up list to select from (still needs improving). Moved IContent to its own module. ContentExceptions thrown via WDCCheck now. Fixed a small bug with reloading translations.
author Diggory Hardy <diggory.hardy@gmail.com>
date Sat, 13 Dec 2008 12:54:43 +0000
parents 1655693702fc
children 9824bee909fd
comparison
equal deleted inserted replaced
111:1655693702fc 112:fe061009029d
24 import mde.gui.widget.layout; 24 import mde.gui.widget.layout;
25 25
26 import mde.gui.renderer.IRenderer; 26 import mde.gui.renderer.IRenderer;
27 import mde.content.AStringContent; 27 import mde.content.AStringContent;
28 import Items = mde.content.Items; 28 import Items = mde.content.Items;
29
30 debug {
31 import tango.util.log.Log : Log, Logger;
32 private Logger logger;
33 static this () {
34 logger = Log.getLogger ("mde.gui.widget.miscContent");
35 }
36 }
29 37
30 /************************************************************************************************* 38 /*************************************************************************************************
31 * A function which uses Items.get (data.strings[0]) to get a content and creates a widget from 39 * A function which uses Items.get (data.strings[0]) to get a content and creates a widget from
32 * data.ints[1]. The first item in each ints and strings is removed before passing data to the new 40 * data.ints[1]. The first item in each ints and strings is removed before passing data to the new
33 * widget. 41 * widget.
50 *************************************************************************************************/ 58 *************************************************************************************************/
51 IChildWidget editContent (IWidgetManager mgr, widgetID id, WidgetData data, IContent c) { 59 IChildWidget editContent (IWidgetManager mgr, widgetID id, WidgetData data, IContent c) {
52 if (c is null) throw new ContentException; 60 if (c is null) throw new ContentException;
53 if (cast(BoolContent) c) 61 if (cast(BoolContent) c)
54 return new BoolContentWidget(mgr,id,data,c); 62 return new BoolContentWidget(mgr,id,data,c);
55 else if (cast(AStringContent) c) 63 else if (cast(AStringContent) c) {
56 return new AStringContentWidget(mgr,id,data,c); 64 if (cast(EnumContent) c)
57 else if (cast(ContentList) c) 65 return new EnumContentWidget(mgr,id,data,c);
66 else
67 return new AStringContentWidget(mgr,id,data,c);
68 } else if (cast(ContentList) c)
58 return new ContentListWidget(mgr,id,data,c); 69 return new ContentListWidget(mgr,id,data,c);
59 else // generic uneditable option 70 else // generic uneditable option
60 return new DisplayContentWidget(mgr,id,data,c); 71 return new DisplayContentWidget(mgr,id,data,c);
61 } 72 }
62 73
63 /// Editable boolean widget 74 /// Editable boolean widget
64 class BoolContentWidget : AButtonWidget 75 class BoolContentWidget : AButtonWidget
65 { 76 {
66 this (IWidgetManager mgr, widgetID id, WidgetData data, IContent c) { 77 this (IWidgetManager mgr, widgetID id, WidgetData data, IContent c) {
67 WDCheck(data, 1); 78 content = cast(BoolContent) c;
68 content = cast(BoolContent) c; 79 WDCCheck(data, 1,0, content);
69 if (!content) throw new ContentException ();
70 wdimPair s = mgr.renderer.getToggleSize; 80 wdimPair s = mgr.renderer.getToggleSize;
71 w = mw = s.x; 81 w = mw = s.x;
72 h = mh = s.y; 82 h = mh = s.y;
73 super (mgr, id, data); 83 super (mgr, id, data);
74 } 84 }
87 97
88 /// A button connected to an EventContent 98 /// A button connected to an EventContent
89 class ButtonContentWidget : AButtonWidget 99 class ButtonContentWidget : AButtonWidget
90 { 100 {
91 this (IWidgetManager mgr, widgetID id, WidgetData data, IContent c) { 101 this (IWidgetManager mgr, widgetID id, WidgetData data, IContent c) {
92 WDCheck (data, 1);
93 content = cast(EventContent) c; 102 content = cast(EventContent) c;
94 if (!content) throw new ContentException (); 103 WDCCheck (data, 1,0, content);
95 adapter = mgr.renderer.getAdapter (); 104 adapter = mgr.renderer.getAdapter ();
96 super (mgr, id, data); 105 super (mgr, id, data);
97 } 106 }
98 107
99 bool setup (uint n, uint flags) { 108 bool setup (uint n, uint flags) {
119 protected: 128 protected:
120 IRenderer.TextAdapter adapter; 129 IRenderer.TextAdapter adapter;
121 EventContent content; 130 EventContent content;
122 int index; 131 int index;
123 } 132 }
124 133 /// Pops up a list of selectable values
134 class EnumContentWidget : AButtonWidget
135 {
136 this (IWidgetManager mgr, widgetID id, WidgetData data, IContent c) {
137 content = cast(EnumContent) c;
138 WDCCheck (data, 1,0, content);
139 adapter = mgr.renderer.getAdapter;
140 adapter.text = content.toString (0);
141 adapter.getDimensions (mw, mh);
142 w = mw;
143 h = mh;
144 internalWidg = new EnumContentPopup (mgr, id, data, content);
145 super (mgr, id, data);
146 }
147
148 void changeContent () {
149 adapter.text = content.toString (0);
150 adapter.getDimensions (mw, mh);
151 w = mw;
152 h = mh;
153 }
154
155 void activated () {
156 mgr.addPopup (x,y, internalWidg);
157 }
158
159 void draw () {
160 super.draw;
161 adapter.draw (x,y);
162 }
163
164 protected:
165 EnumContentPopup internalWidg; // setup & saveChanges calls not propegated
166 EnumContent content;
167 IRenderer.TextAdapter adapter;
168
169 /// The widget which gets popped up
170 class EnumContentPopup : AButtonWidget
171 {
172 /// Assumes EnumContent c isn't null
173 this (IWidgetManager mgr, widgetID id, WidgetData data, EnumContent c) {
174 content = c;
175 adapters.length = content.enumSymbols.length;
176 yPos.length = adapters.length;
177 // mh is 0
178 foreach (i, ref adapter; adapters) {
179 adapter = mgr.renderer.getAdapter;
180 adapter.text = content.toString(i+3);
181 wdim aw, ah;
182 adapter.getDimensions (aw,ah);
183 if (mw < aw) mw = aw;
184 yPos[i] = mh;
185 mh += ah;
186 }
187 w = mw;
188 h = mh;
189 super (mgr, id, data);
190 }
191
192 void draw () {
193 super.draw;
194 foreach (i,yp; yPos) {
195 adapters[i].draw (x,y+yp);
196 }
197 }
198
199 /// Called when a mouse click event occurs while held; handles up-click
200 bool clickWhilePushed (wdabs cx, wdabs cy, ubyte b, bool state) {
201 if (b == 1 && state == false) {
202 cy -= y;
203 if (cx >= x && cx < x+w && cy >= 0 && cy < h) {
204 uint i = yPos.length-1;
205 for (; i > 0; --i)
206 if (cy >= yPos[i])
207 break;
208 logger.trace ("Setting value: {}", i);
209 content = i;
210 mgr.removePopup (this);
211 changeContent;
212 }
213
214 pushed = false;
215 mgr.requestRedraw;
216 mgr.removeCallbacks (cast(void*) this);
217
218 return true;
219 }
220 return false;
221 }
222
223 void activated () {}
224
225 protected:
226 EnumContent content;
227 IRenderer.TextAdapter[] adapters; // NOTE: replace with multi-line adapter
228 wdrel[] yPos; // y position of each adapter
229 }
230 }