comparison mde/gui/widget/miscContent.d @ 115:1b1e2297e2fc

Enums handled more generically now via either a popup list or flat list of BoolContentWidgets. EnumContent is an IContentList with BoolContent sub-contents. Content modules moved around (again). ContentListWidget can now list horizontally. Log-level setting callback.
author Diggory Hardy <diggory.hardy@gmail.com>
date Sat, 20 Dec 2008 17:57:05 +0000
parents b16a534f5302
children 5ee69b3ed9c9
comparison
equal deleted inserted replaced
114:b16a534f5302 115:1b1e2297e2fc
20 20
21 import mde.gui.widget.Widget; 21 import mde.gui.widget.Widget;
22 import mde.gui.exception; 22 import mde.gui.exception;
23 import mde.gui.widget.textContent; 23 import mde.gui.widget.textContent;
24 import mde.gui.widget.layout; 24 import mde.gui.widget.layout;
25 import mde.gui.widget.PopupMenu;
25 26
26 import mde.gui.renderer.IRenderer; 27 import mde.gui.renderer.IRenderer;
27 import mde.content.AStringContent; 28 import mde.content.AStringContent;
29 import mde.content.miscContent;
28 import Items = mde.content.Items; 30 import Items = mde.content.Items;
29 31
30 debug { 32 debug {
31 import tango.util.log.Log : Log, Logger; 33 import tango.util.log.Log : Log, Logger;
32 private Logger logger; 34 private Logger logger;
56 * Widgets which can be returned: BoolContentWidget (toggle button), ValueContentWidget (generic 58 * Widgets which can be returned: BoolContentWidget (toggle button), ValueContentWidget (generic
57 * text-box editor), DisplayContentWidget (generic text label). 59 * text-box editor), DisplayContentWidget (generic text label).
58 *************************************************************************************************/ 60 *************************************************************************************************/
59 IChildWidget editContent (IWidgetManager mgr, widgetID id, WidgetData data, IContent c) { 61 IChildWidget editContent (IWidgetManager mgr, widgetID id, WidgetData data, IContent c) {
60 if (c is null) throw new ContentException; 62 if (c is null) throw new ContentException;
61 if (cast(BoolContent) c)
62 return new BoolContentWidget(mgr,id,data,c);
63 else if (cast(AStringContent) c) { 63 else if (cast(AStringContent) c) {
64 if (cast(EnumContent) c) 64 if (cast(EnumContent) c) // can be PopupMenuWidget or ContentListWidget
65 return new EnumContentWidget(mgr,id,data,c); 65 return new ContentListWidget(mgr,id,data,c);
66 else 66 else if (cast(BoolContent) c)
67 return new BoolContentWidget(mgr,id,data,c);
68 else
67 return new AStringContentWidget(mgr,id,data,c); 69 return new AStringContentWidget(mgr,id,data,c);
68 } else if (cast(ContentList) c) 70 } else if (cast(IContentList) c)
69 return new ContentListWidget(mgr,id,data,c); 71 return new ContentListWidget(mgr,id,data,c);
70 else if (cast(EventContent) c) 72 else if (cast(EventContent) c)
71 return new ButtonContentWidget(mgr,id,data,c); 73 return new ButtonContentWidget(mgr,id,data,c);
72 else // generic uneditable option 74 else // generic uneditable option
73 return new DisplayContentWidget(mgr,id,data,c); 75 return new DisplayContentWidget(mgr,id,data,c);
76 /// Editable boolean widget 78 /// Editable boolean widget
77 class BoolContentWidget : AButtonWidget 79 class BoolContentWidget : AButtonWidget
78 { 80 {
79 this (IWidgetManager mgr, widgetID id, WidgetData data, IContent c) { 81 this (IWidgetManager mgr, widgetID id, WidgetData data, IContent c) {
80 content = cast(BoolContent) c; 82 content = cast(BoolContent) c;
81 WDCCheck(data, 1,0, content); 83 WDCMinCheck(data, 1,0, content);
82 wdimPair s = mgr.renderer.getToggleSize; 84 wdimPair s = mgr.renderer.getToggleSize;
83 w = mw = s.x; 85 w = mw = s.x;
84 h = mh = s.y; 86 h = mh = s.y;
85 super (mgr, id, data); 87 super (mgr, id, data);
86 } 88 }
100 /// A button connected to an EventContent 102 /// A button connected to an EventContent
101 class ButtonContentWidget : AButtonWidget 103 class ButtonContentWidget : AButtonWidget
102 { 104 {
103 this (IWidgetManager mgr, widgetID id, WidgetData data, IContent c) { 105 this (IWidgetManager mgr, widgetID id, WidgetData data, IContent c) {
104 content = cast(EventContent) c; 106 content = cast(EventContent) c;
105 WDCCheck (data, 1,0, content); 107 WDCMinCheck (data, 1,0, content);
106 adapter = mgr.renderer.getAdapter (); 108 adapter = mgr.renderer.getAdapter ();
107 super (mgr, id, data); 109 super (mgr, id, data);
108 } 110 }
109 111
110 override bool setup (uint n, uint flags) { 112 override bool setup (uint n, uint flags) {
130 protected: 132 protected:
131 IRenderer.TextAdapter adapter; 133 IRenderer.TextAdapter adapter;
132 EventContent content; 134 EventContent content;
133 int index; 135 int index;
134 } 136 }
135 /// Pops up a list of selectable values
136 class EnumContentWidget : AButtonWidget
137 {
138 this (IWidgetManager mgr, widgetID id, WidgetData data, IContent c) {
139 content = cast(EnumContent) c;
140 WDCCheck (data, 1,0, content);
141 adapter = mgr.renderer.getAdapter;
142 adapter.text = content.toString (0);
143 adapter.getDimensions (mw, mh);
144 w = mw;
145 h = mh;
146 internalWidg = new EnumContentPopup (mgr, id, data, content);
147 super (mgr, id, data);
148 }
149
150 void changeContent () {
151 adapter.text = content.toString (0);
152 adapter.getDimensions (mw, mh);
153 w = mw;
154 h = mh;
155 }
156
157 override void activated () {
158 mgr.addPopup (this, internalWidg);
159 }
160
161 override void draw () {
162 super.draw;
163 adapter.draw (x,y);
164 }
165
166 protected:
167 EnumContentPopup internalWidg; // setup & saveChanges calls not propegated
168 EnumContent content;
169 IRenderer.TextAdapter adapter;
170
171 /// The widget which gets popped up
172 class EnumContentPopup : AButtonWidget
173 {
174 /// Assumes EnumContent c isn't null
175 this (IWidgetManager mgr, widgetID id, WidgetData data, EnumContent c) {
176 content = c;
177 adapters.length = content.enumSymbols.length;
178 yPos.length = adapters.length;
179 // mh is 0
180 foreach (i, ref adapter; adapters) {
181 adapter = mgr.renderer.getAdapter;
182 adapter.text = content.toString(i+3);
183 wdim aw, ah;
184 adapter.getDimensions (aw,ah);
185 if (mw < aw) mw = aw;
186 yPos[i] = mh;
187 mh += ah;
188 }
189 w = mw;
190 h = mh;
191 super (mgr, id, data);
192 }
193
194 override void draw () {
195 super.draw;
196 foreach (i,yp; yPos) {
197 adapters[i].draw (x,y+yp);
198 }
199 }
200
201 /// Called when a mouse click event occurs while held; handles up-click
202 override bool clickWhilePushed (wdabs cx, wdabs cy, ubyte b, bool state) {
203 if (b == 1 && state == false) {
204 cy -= y;
205 if (cx >= x && cx < x+w && cy >= 0 && cy < h) {
206 uint i = yPos.length-1;
207 for (; i > 0; --i)
208 if (cy >= yPos[i])
209 break;
210 logger.trace ("Setting value: {}", i);
211 content = i;
212 //mgr.removePopup (this);
213 changeContent;
214 }
215
216 pushed = false;
217 mgr.requestRedraw;
218 mgr.removeCallbacks (cast(void*) this);
219
220 return true;
221 }
222 return false;
223 }
224
225 override void activated () {}
226
227 protected:
228 EnumContent content;
229 IRenderer.TextAdapter[] adapters; // NOTE: replace with multi-line adapter
230 wdrel[] yPos; // y position of each adapter
231 }
232 }