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, setAlphaOf, void(entity, entity, float))
19 METHOD(Container, itemFromPoint, entity(entity, vector))
20 METHOD(Container, showNotify, void(entity))
21 METHOD(Container, hideNotify, void(entity))
22 METHOD(Container, preferredFocusedGrandChild, entity(entity))
23 ATTRIB(Container, focusable, float, 0)
24 ATTRIB(Container, firstChild, entity, NULL)
25 ATTRIB(Container, lastChild, entity, NULL)
26 ATTRIB(Container, focusedChild, entity, NULL)
27 ATTRIB(Container, shown, float, 0)
29 METHOD(Container, enterSubitem, void(entity, entity))
30 METHOD(Container, enterLieSubitem, void(entity, vector, vector, vector, float))
31 METHOD(Container, leaveSubitem, void(entity))
36 .vector Container_origin;
37 .vector Container_size;
38 .vector Container_fontscale;
39 .float Container_alpha;
40 .vector Container_save_shift;
41 .vector Container_save_scale;
42 .vector Container_save_fontscale;
43 .float Container_save_alpha;
47 void Container_enterSubitem(entity me, entity sub)
49 me.enterLieSubitem(me, sub.Container_origin, sub.Container_size, sub.Container_fontscale, sub.Container_alpha);
52 void Container_enterLieSubitem(entity me, vector o, vector s, vector f, float a)
54 me.Container_save_shift = draw_shift;
55 me.Container_save_scale = draw_scale;
56 me.Container_save_alpha = draw_alpha;
57 me.Container_save_fontscale = draw_fontscale;
59 draw_shift = boxToGlobal(o, draw_shift, draw_scale);
60 draw_scale = boxToGlobalSize(s, draw_scale);
62 draw_fontscale = boxToGlobalSize(f, draw_fontscale);
66 void Container_leaveSubitem(entity me)
68 draw_shift = me.Container_save_shift;
69 draw_scale = me.Container_save_scale;
70 draw_alpha = me.Container_save_alpha;
71 draw_fontscale = me.Container_save_fontscale;
74 void Container_showNotify(entity me)
80 for(e = me.firstChild; e; e = e.nextSibling)
81 if(e.Container_alpha > 0)
85 void Container_hideNotify(entity me)
91 for(e = me.firstChild; e; e = e.nextSibling)
92 if(e.Container_alpha > 0)
96 void Container_setAlphaOf(entity me, entity other, float theAlpha)
100 if(other.Container_alpha > 0)
101 other.hideNotify(other);
105 if(other.Container_alpha <= 0)
106 other.showNotify(other);
108 other.Container_alpha = theAlpha;
111 void Container_resizeNotifyLie(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize, .vector originField, .vector sizeField, .vector fontScaleField)
116 for(e = me.firstChild; e; e = e.nextSibling)
120 me.enterLieSubitem(me, o, s, e.fontScaleField, e.Container_alpha);
121 e.resizeNotify(e, o, s, boxToGlobal(o, absOrigin, absSize), boxToGlobalSize(s, absSize));
127 for(e = me.firstChild; e; e = e.nextSibling)
134 me.enterLieSubitem(me, o, s, e.fontScaleField, e.Container_alpha);
135 e.resizeNotify(e, o, s, boxToGlobal(o, absOrigin, absSize), boxToGlobalSize(s, absSize));
140 SUPER(Container).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
143 void Container_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
145 me.resizeNotifyLie(me, relOrigin, relSize, absOrigin, absSize, Container_origin, Container_size, Container_fontscale);
148 entity Container_itemFromPoint(entity me, vector pos)
152 for(e = me.lastChild; e; e = e.prevSibling)
154 o = e.Container_origin;
155 s = e.Container_size;
156 if(pos_x < o_x) continue;
157 if(pos_y < o_y) continue;
158 if(pos_x >= o_x + s_x) continue;
159 if(pos_y >= o_y + s_y) continue;
165 void Container_draw(entity me)
170 for(e = me.firstChild; e; e = e.nextSibling)
174 if(e.Container_alpha < 0.003) // can't change color values anyway
176 me.enterSubitem(me, e);
181 SUPER(Container).draw(me);
184 void Container_focusLeave(entity me)
186 me.setFocus(me, NULL);
189 float Container_keyUp(entity me, float scan, float ascii, float shift)
196 me.enterSubitem(me, f);
197 r = f.keyUp(f, scan, ascii, shift);
204 float Container_keyDown(entity me, float scan, float ascii, float shift)
211 me.enterSubitem(me, f);
212 r = f.keyDown(f, scan, ascii, shift);
219 float Container_mouseMove(entity me, vector pos)
226 me.enterSubitem(me, f);
227 r = f.mouseMove(f, globalToBox(pos, f.Container_origin, f.Container_size));
233 float Container_mousePress(entity me, vector pos)
240 me.enterSubitem(me, f);
241 r = f.mousePress(f, globalToBox(pos, f.Container_origin, f.Container_size));
247 float Container_mouseDrag(entity me, vector pos)
254 me.enterSubitem(me, f);
255 r = f.mouseDrag(f, globalToBox(pos, f.Container_origin, f.Container_size));
261 float Container_mouseRelease(entity me, vector pos)
268 me.enterSubitem(me, f);
269 r = f.mouseRelease(f, globalToBox(pos, f.Container_origin, f.Container_size));
276 void Container_addItemCentered(entity me, entity other, vector theSize, float theAlpha)
278 me.addItem(me, other, '0.5 0.5 0' - 0.5 * theSize, theSize, theAlpha);
281 void Container_addItem(entity me, entity other, vector theOrigin, vector theSize, float theAlpha)
284 error("Can't add already added item!");
291 theOrigin_x -= 0.5 * (theSize_x - 1);
296 theOrigin_y -= 0.5 * (theSize_y - 1);
299 theOrigin_x = bound(0, theOrigin_x, 1 - theSize_x);
300 theOrigin_y = bound(0, theOrigin_y, 1 - theSize_y);
303 other.Container_origin = theOrigin;
304 other.Container_size = theSize;
305 me.setAlphaOf(me, other, theAlpha);
311 l.nextSibling = other;
313 me.firstChild = other;
315 other.prevSibling = l;
316 other.nextSibling = NULL;
317 me.lastChild = other;
319 draw_NeedResizeNotify = 1;
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)
351 error("Trying to set focus in a non-focused control!");
352 if(me.focusedChild == other)
354 //print(etos(me), ": focus changes from ", etos(me.focusedChild), " to ", etos(other), "\n");
357 me.focusedChild.focused = 0;
358 me.focusedChild.focusLeave(me.focusedChild);
363 other.focusEnter(other);
365 me.focusedChild = other;
368 void Container_moveItemAfter(entity me, entity other, entity dest)
370 // first: remove other from the chain
373 if(other.parent != me)
374 error("Can't move in wrong container!");
376 n = other.nextSibling;
377 p = other.prevSibling;
389 // now other got removed. Insert it behind dest now.
390 other.prevSibling = dest;
392 other.nextSibling = dest.nextSibling;
394 other.nextSibling = me.firstChild;
397 dest.nextSibling = other;
399 me.firstChild = other;
401 if(other.nextSibling)
402 other.nextSibling.prevSibling = other;
404 me.lastChild = other;
407 entity Container_preferredFocusedGrandChild(entity me)
414 for(e = me.firstChild; e; e = e.nextSibling)
416 if(e.instanceOfContainer)
418 e2 = e.preferredFocusedGrandChild(e);
420 if(!best || best.preferredFocusPriority < e2.preferredFocusPriority)
424 if(!best || best.preferredFocusPriority < e.preferredFocusPriority)