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);
182 void Container_focusLeave(entity me)
184 me.setFocus(me, NULL);
187 float Container_keyUp(entity me, float scan, float ascii, float shift)
194 me.enterSubitem(me, f);
195 r = f.keyUp(f, scan, ascii, shift);
202 float Container_keyDown(entity me, float scan, float ascii, float shift)
209 me.enterSubitem(me, f);
210 r = f.keyDown(f, scan, ascii, shift);
217 float Container_mouseMove(entity me, vector pos)
224 me.enterSubitem(me, f);
225 r = f.mouseMove(f, globalToBox(pos, f.Container_origin, f.Container_size));
231 float Container_mousePress(entity me, vector pos)
238 me.enterSubitem(me, f);
239 r = f.mousePress(f, globalToBox(pos, f.Container_origin, f.Container_size));
245 float Container_mouseDrag(entity me, vector pos)
252 me.enterSubitem(me, f);
253 r = f.mouseDrag(f, globalToBox(pos, f.Container_origin, f.Container_size));
259 float Container_mouseRelease(entity me, vector pos)
266 me.enterSubitem(me, f);
267 r = f.mouseRelease(f, globalToBox(pos, f.Container_origin, f.Container_size));
274 void Container_addItemCentered(entity me, entity other, vector theSize, float theAlpha)
276 me.addItem(me, other, '0.5 0.5 0' - 0.5 * theSize, theSize, theAlpha);
279 void Container_addItem(entity me, entity other, vector theOrigin, vector theSize, float theAlpha)
282 error("Can't add already added item!");
289 theOrigin_x -= 0.5 * (theSize_x - 1);
294 theOrigin_y -= 0.5 * (theSize_y - 1);
297 theOrigin_x = bound(0, theOrigin_x, 1 - theSize_x);
298 theOrigin_y = bound(0, theOrigin_y, 1 - theSize_y);
301 other.Container_origin = theOrigin;
302 other.Container_size = theSize;
303 me.setAlphaOf(me, other, theAlpha);
310 l.nextSibling = other;
312 me.firstChild = other;
314 other.prevSibling = l;
315 other.nextSibling = NULL;
316 me.lastChild = other;
318 draw_NeedResizeNotify = 1;
321 void Container_removeItem(entity me, entity other)
323 if(other.parent != me)
324 error("Can't remove from wrong container!");
334 n = other.nextSibling;
335 p = other.prevSibling;
348 void Container_setFocus(entity me, entity other)
352 error("Trying to set focus in a non-focused control!");
353 if(me.focusedChild == other)
355 //print(etos(me), ": focus changes from ", etos(me.focusedChild), " to ", etos(other), "\n");
358 me.focusedChild.focused = 0;
359 me.focusedChild.focusLeave(me.focusedChild);
364 other.focusEnter(other);
366 me.focusedChild = other;
369 void Container_moveItemAfter(entity me, entity other, entity dest)
371 // first: remove other from the chain
374 if(other.parent != me)
375 error("Can't move in wrong container!");
379 n = other.nextSibling;
380 p = other.prevSibling;
392 // now other got removed. Insert it behind dest now.
393 other.prevSibling = dest;
395 other.nextSibling = dest.nextSibling;
397 other.nextSibling = me.firstChild;
400 dest.nextSibling = other;
402 me.firstChild = other;
404 if(other.nextSibling)
405 other.nextSibling.prevSibling = other;
407 me.lastChild = other;
410 entity Container_preferredFocusedGrandChild(entity me)
417 for(e = me.firstChild; e; e = e.nextSibling)
419 if(e.instanceOfContainer)
421 e2 = e.preferredFocusedGrandChild(e);
423 if(!best || best.preferredFocusPriority < e2.preferredFocusPriority)
427 if(!best || best.preferredFocusPriority < e.preferredFocusPriority)