2 CLASS(Container) EXTENDS(Item)
3 METHOD(Container, draw, void(entity))
4 METHOD(Container, keyUp, float(entity, float, float, float))
5 METHOD(Container, keyDown, float(entity, float, float, float))
6 METHOD(Container, mouseMove, float(entity, vector))
7 METHOD(Container, mousePress, float(entity, vector))
8 METHOD(Container, mouseDrag, float(entity, vector))
9 METHOD(Container, mouseRelease, float(entity, vector))
10 METHOD(Container, focusLeave, void(entity))
11 METHOD(Container, resizeNotify, void(entity, vector, vector, vector, vector))
12 METHOD(Container, resizeNotifyLie, void(entity, vector, vector, vector, vector, .vector, .vector, .vector))
13 METHOD(Container, addItem, void(entity, entity, vector, vector, float))
14 METHOD(Container, addItemCentered, void(entity, entity, vector, float))
15 METHOD(Container, moveItemAfter, void(entity, entity, entity))
16 METHOD(Container, removeItem, void(entity, entity))
17 METHOD(Container, setFocus, void(entity, entity))
18 METHOD(Container, saveFocus, void(entity))
19 METHOD(Container, setAlphaOf, void(entity, entity, float))
20 METHOD(Container, itemFromPoint, entity(entity, vector))
21 METHOD(Container, showNotify, void(entity))
22 METHOD(Container, hideNotify, void(entity))
23 METHOD(Container, preferredFocusedGrandChild, entity(entity))
24 ATTRIB(Container, focusable, float, 0)
25 ATTRIB(Container, firstChild, entity, NULL)
26 ATTRIB(Container, lastChild, entity, NULL)
27 ATTRIB(Container, focusedChild, entity, NULL)
28 ATTRIB(Container, savedFocus, entity, NULL)
29 ATTRIB(Container, shown, float, 0)
31 METHOD(Container, enterSubitem, void(entity, entity))
32 METHOD(Container, enterLieSubitem, void(entity, vector, vector, vector, float))
33 METHOD(Container, leaveSubitem, void(entity))
38 .vector Container_origin;
39 .vector Container_size;
40 .vector Container_fontscale;
41 .float Container_alpha;
42 .vector Container_save_shift;
43 .vector Container_save_scale;
44 .vector Container_save_fontscale;
45 .float Container_save_alpha;
49 void Container_enterSubitem(entity me, entity sub)
51 me.enterLieSubitem(me, sub.Container_origin, sub.Container_size, sub.Container_fontscale, sub.Container_alpha);
54 void Container_enterLieSubitem(entity me, vector o, vector s, vector f, float a)
56 me.Container_save_shift = draw_shift;
57 me.Container_save_scale = draw_scale;
58 me.Container_save_alpha = draw_alpha;
59 me.Container_save_fontscale = draw_fontscale;
61 draw_shift = boxToGlobal(o, draw_shift, draw_scale);
62 draw_scale = boxToGlobalSize(s, draw_scale);
64 draw_fontscale = boxToGlobalSize(f, draw_fontscale);
68 void Container_leaveSubitem(entity me)
70 draw_shift = me.Container_save_shift;
71 draw_scale = me.Container_save_scale;
72 draw_alpha = me.Container_save_alpha;
73 draw_fontscale = me.Container_save_fontscale;
76 void Container_showNotify(entity me)
82 for(e = me.firstChild; e; e = e.nextSibling)
83 if(e.Container_alpha > 0)
87 void Container_hideNotify(entity me)
93 for(e = me.firstChild; e; e = e.nextSibling)
94 if(e.Container_alpha > 0)
98 void Container_setAlphaOf(entity me, entity other, float theAlpha)
102 if(other.Container_alpha > 0)
103 other.hideNotify(other);
107 if(other.Container_alpha <= 0)
108 other.showNotify(other);
110 other.Container_alpha = theAlpha;
113 void Container_resizeNotifyLie(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize, .vector originField, .vector sizeField, .vector fontScaleField)
118 for(e = me.firstChild; e; e = e.nextSibling)
122 me.enterLieSubitem(me, o, s, e.fontScaleField, e.Container_alpha);
123 e.resizeNotify(e, o, s, boxToGlobal(o, absOrigin, absSize), boxToGlobalSize(s, absSize));
129 for(e = me.firstChild; e; e = e.nextSibling)
136 me.enterLieSubitem(me, o, s, e.fontScaleField, e.Container_alpha);
137 e.resizeNotify(e, o, s, boxToGlobal(o, absOrigin, absSize), boxToGlobalSize(s, absSize));
142 SUPER(Container).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
145 void Container_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
147 me.resizeNotifyLie(me, relOrigin, relSize, absOrigin, absSize, Container_origin, Container_size, Container_fontscale);
150 entity Container_itemFromPoint(entity me, vector pos)
154 for(e = me.lastChild; e; e = e.prevSibling)
156 o = e.Container_origin;
157 s = e.Container_size;
158 if(pos.x < o.x) continue;
159 if(pos.y < o.y) continue;
160 if(pos.x >= o.x + s.x) continue;
161 if(pos.y >= o.y + s.y) continue;
167 void Container_draw(entity me)
172 for(e = me.firstChild; e; e = e.nextSibling)
176 if(e.Container_alpha < 0.003) // can't change color values anyway
178 me.enterSubitem(me, e);
183 SUPER(Container).draw(me);
186 void Container_focusLeave(entity me)
188 me.setFocus(me, NULL);
191 float Container_keyUp(entity me, float scan, float ascii, float shift)
198 me.enterSubitem(me, f);
199 r = f.keyUp(f, scan, ascii, shift);
206 float Container_keyDown(entity me, float scan, float ascii, float shift)
213 me.enterSubitem(me, f);
214 r = f.keyDown(f, scan, ascii, shift);
221 float Container_mouseMove(entity me, vector pos)
228 me.enterSubitem(me, f);
229 r = f.mouseMove(f, globalToBox(pos, f.Container_origin, f.Container_size));
235 float Container_mousePress(entity me, vector pos)
242 me.enterSubitem(me, f);
243 r = f.mousePress(f, globalToBox(pos, f.Container_origin, f.Container_size));
249 float Container_mouseDrag(entity me, vector pos)
256 me.enterSubitem(me, f);
257 r = f.mouseDrag(f, globalToBox(pos, f.Container_origin, f.Container_size));
263 float Container_mouseRelease(entity me, vector pos)
270 me.enterSubitem(me, f);
271 r = f.mouseRelease(f, globalToBox(pos, f.Container_origin, f.Container_size));
278 void Container_addItemCentered(entity me, entity other, vector theSize, float theAlpha)
280 me.addItem(me, other, '0.5 0.5 0' - 0.5 * theSize, theSize, theAlpha);
283 void Container_addItem(entity me, entity other, vector theOrigin, vector theSize, float theAlpha)
286 error("Can't add already added item!");
293 theOrigin.x -= 0.5 * (theSize.x - 1);
298 theOrigin.y -= 0.5 * (theSize.y - 1);
301 theOrigin.x = bound(0, theOrigin.x, 1 - theSize.x);
302 theOrigin.y = bound(0, theOrigin.y, 1 - theSize.y);
305 other.Container_origin = theOrigin;
306 other.Container_size = theSize;
307 me.setAlphaOf(me, other, theAlpha);
313 l.nextSibling = other;
315 me.firstChild = other;
317 other.prevSibling = l;
318 other.nextSibling = NULL;
319 me.lastChild = other;
322 void Container_removeItem(entity me, entity other)
324 if(other.parent != me)
325 error("Can't remove from wrong container!");
333 n = other.nextSibling;
334 p = other.prevSibling;
347 void Container_setFocus(entity me, entity other)
349 if(me.focusedChild == other)
354 me.focusedChild.focused = 0;
355 me.focusedChild.focusLeave(me.focusedChild);
356 me.focusedChild = NULL;
362 error("Trying to set focus in a non-focused control!");
366 me.focusedChild = me.savedFocus;
367 me.savedFocus = NULL;
368 me.focusedChild.focused = 1;
369 me.focusedChild.focusEnter(me.focusedChild);
371 if(me.focusedChild.instanceOfContainer)
372 me.focusedChild.setFocus(me.focusedChild, me.focusedChild.savedFocus);
376 me.focusedChild = other;
377 me.focusedChild.focused = 1;
378 me.focusedChild.focusEnter(me.focusedChild);
383 void Container_saveFocus(entity me)
385 me.savedFocus = me.focusedChild;
387 if(me.focusedChild.instanceOfContainer)
388 me.focusedChild.saveFocus(me.focusedChild);
391 void Container_moveItemAfter(entity me, entity other, entity dest)
393 // first: remove other from the chain
396 if(other.parent != me)
397 error("Can't move in wrong container!");
399 n = other.nextSibling;
400 p = other.prevSibling;
412 // now other got removed. Insert it behind dest now.
413 other.prevSibling = dest;
415 other.nextSibling = dest.nextSibling;
417 other.nextSibling = me.firstChild;
420 dest.nextSibling = other;
422 me.firstChild = other;
424 if(other.nextSibling)
425 other.nextSibling.prevSibling = other;
427 me.lastChild = other;
430 entity Container_preferredFocusedGrandChild(entity me)
437 for(e = me.firstChild; e; e = e.nextSibling)
439 if(e.instanceOfContainer)
441 e2 = e.preferredFocusedGrandChild(e);
443 if(!best || best.preferredFocusPriority < e2.preferredFocusPriority)
447 if(!best || best.preferredFocusPriority < e.preferredFocusPriority)