1 #ifndef ITEM_CONTAINER_H
2 #define ITEM_CONTAINER_H
5 METHOD(Container, draw, void(entity))
6 METHOD(Container, keyUp, float(entity, float, float, float))
7 METHOD(Container, keyDown, float(entity, float, float, float))
8 METHOD(Container, mouseMove, float(entity, vector))
9 METHOD(Container, mousePress, float(entity, vector))
10 METHOD(Container, mouseDrag, float(entity, vector))
11 METHOD(Container, mouseRelease, float(entity, vector))
12 METHOD(Container, focusLeave, void(entity))
13 METHOD(Container, resizeNotify, void(entity, vector, vector, vector, vector))
14 METHOD(Container, resizeNotifyLie, void(entity, vector, vector, vector, vector, .vector, .vector, .vector))
15 METHOD(Container, addItem, void(entity, entity, vector, vector, float))
16 METHOD(Container, addItemCentered, void(entity, entity, vector, float))
17 METHOD(Container, moveItemAfter, void(entity, entity, entity))
18 METHOD(Container, removeItem, void(entity, entity))
19 METHOD(Container, setFocus, void(entity, entity))
20 METHOD(Container, saveFocus, void(entity))
21 METHOD(Container, setAlphaOf, void(entity, entity, float))
22 METHOD(Container, itemFromPoint, entity(entity, vector))
23 METHOD(Container, showNotify, void(entity))
24 METHOD(Container, hideNotify, void(entity))
25 METHOD(Container, preferredFocusedGrandChild, entity(entity))
26 ATTRIB(Container, focusable, float, 0)
27 ATTRIB(Container, firstChild, entity, NULL)
28 ATTRIB(Container, lastChild, entity, NULL)
29 ATTRIB(Container, focusedChild, entity, NULL)
30 ATTRIB(Container, savedFocus, entity, NULL)
31 ATTRIB(Container, shown, float, 0)
33 METHOD(Container, enterSubitem, void(entity, entity))
34 METHOD(Container, enterLieSubitem, void(entity, vector, vector, vector, float))
35 METHOD(Container, leaveSubitem, void(entity))
40 .vector Container_origin;
41 .vector Container_size;
42 .vector Container_fontscale;
43 .float Container_alpha;
44 .vector Container_save_shift;
45 .vector Container_save_scale;
46 .vector Container_save_fontscale;
47 .float Container_save_alpha;
51 void Container_enterSubitem(entity me, entity sub)
53 me.enterLieSubitem(me, sub.Container_origin, sub.Container_size, sub.Container_fontscale, sub.Container_alpha);
56 void Container_enterLieSubitem(entity me, vector o, vector s, vector f, float a)
58 me.Container_save_shift = draw_shift;
59 me.Container_save_scale = draw_scale;
60 me.Container_save_alpha = draw_alpha;
61 me.Container_save_fontscale = draw_fontscale;
63 draw_shift = boxToGlobal(o, draw_shift, draw_scale);
64 draw_scale = boxToGlobalSize(s, draw_scale);
66 draw_fontscale = boxToGlobalSize(f, draw_fontscale);
70 void Container_leaveSubitem(entity me)
72 draw_shift = me.Container_save_shift;
73 draw_scale = me.Container_save_scale;
74 draw_alpha = me.Container_save_alpha;
75 draw_fontscale = me.Container_save_fontscale;
78 void Container_showNotify(entity me)
84 for(e = me.firstChild; e; e = e.nextSibling)
85 if(e.Container_alpha > 0)
89 void Container_hideNotify(entity me)
95 for(e = me.firstChild; e; e = e.nextSibling)
96 if(e.Container_alpha > 0)
100 void Container_setAlphaOf(entity me, entity other, float theAlpha)
104 if(other.Container_alpha > 0)
105 other.hideNotify(other);
109 if(other.Container_alpha <= 0)
110 other.showNotify(other);
112 other.Container_alpha = theAlpha;
115 void Container_resizeNotifyLie(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize, .vector originField, .vector sizeField, .vector fontScaleField)
120 for(e = me.firstChild; e; e = e.nextSibling)
124 me.enterLieSubitem(me, o, s, e.(fontScaleField), e.Container_alpha);
125 e.resizeNotify(e, o, s, boxToGlobal(o, absOrigin, absSize), boxToGlobalSize(s, absSize));
131 for(e = me.firstChild; e; e = e.nextSibling)
138 me.enterLieSubitem(me, o, s, e.(fontScaleField), e.Container_alpha);
139 e.resizeNotify(e, o, s, boxToGlobal(o, absOrigin, absSize), boxToGlobalSize(s, absSize));
144 SUPER(Container).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
147 void Container_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
149 me.resizeNotifyLie(me, relOrigin, relSize, absOrigin, absSize, Container_origin, Container_size, Container_fontscale);
152 entity Container_itemFromPoint(entity me, vector pos)
156 for(e = me.lastChild; e; e = e.prevSibling)
158 o = e.Container_origin;
159 s = e.Container_size;
160 if(pos.x < o.x) continue;
161 if(pos.y < o.y) continue;
162 if(pos.x >= o.x + s.x) continue;
163 if(pos.y >= o.y + s.y) continue;
169 void Container_draw(entity me)
174 for(e = me.firstChild; e; e = e.nextSibling)
178 if(e.Container_alpha < 0.003) // can't change color values anyway
180 me.enterSubitem(me, e);
185 SUPER(Container).draw(me);
188 void Container_focusLeave(entity me)
190 me.setFocus(me, NULL);
193 float Container_keyUp(entity me, float scan, float ascii, float shift)
200 me.enterSubitem(me, f);
201 r = f.keyUp(f, scan, ascii, shift);
208 float Container_keyDown(entity me, float scan, float ascii, float shift)
215 me.enterSubitem(me, f);
216 r = f.keyDown(f, scan, ascii, shift);
223 float Container_mouseMove(entity me, vector pos)
230 me.enterSubitem(me, f);
231 r = f.mouseMove(f, globalToBox(pos, f.Container_origin, f.Container_size));
237 float Container_mousePress(entity me, vector pos)
244 me.enterSubitem(me, f);
245 r = f.mousePress(f, globalToBox(pos, f.Container_origin, f.Container_size));
251 float Container_mouseDrag(entity me, vector pos)
258 me.enterSubitem(me, f);
259 r = f.mouseDrag(f, globalToBox(pos, f.Container_origin, f.Container_size));
265 float Container_mouseRelease(entity me, vector pos)
272 me.enterSubitem(me, f);
273 r = f.mouseRelease(f, globalToBox(pos, f.Container_origin, f.Container_size));
280 void Container_addItemCentered(entity me, entity other, vector theSize, float theAlpha)
282 me.addItem(me, other, '0.5 0.5 0' - 0.5 * theSize, theSize, theAlpha);
285 void Container_addItem(entity me, entity other, vector theOrigin, vector theSize, float theAlpha)
288 error("Can't add already added item!");
295 theOrigin.x -= 0.5 * (theSize.x - 1);
300 theOrigin.y -= 0.5 * (theSize.y - 1);
303 theOrigin.x = bound(0, theOrigin.x, 1 - theSize.x);
304 theOrigin.y = bound(0, theOrigin.y, 1 - theSize.y);
307 other.Container_origin = theOrigin;
308 other.Container_size = theSize;
309 me.setAlphaOf(me, other, theAlpha);
315 l.nextSibling = other;
317 me.firstChild = other;
319 other.prevSibling = l;
320 other.nextSibling = NULL;
321 me.lastChild = other;
324 void Container_removeItem(entity me, entity other)
326 if(other.parent != me)
327 error("Can't remove from wrong container!");
335 n = other.nextSibling;
336 p = other.prevSibling;
349 void Container_setFocus(entity me, entity other)
351 if(me.focusedChild == other)
356 me.focusedChild.focused = 0;
357 me.focusedChild.focusLeave(me.focusedChild);
358 me.focusedChild = NULL;
364 error("Trying to set focus in a non-focused control!");
368 me.focusedChild = me.savedFocus;
369 me.savedFocus = NULL;
370 me.focusedChild.focused = 1;
371 me.focusedChild.focusEnter(me.focusedChild);
373 if(me.focusedChild.instanceOfContainer)
374 me.focusedChild.setFocus(me.focusedChild, me.focusedChild.savedFocus);
378 me.focusedChild = other;
379 me.focusedChild.focused = 1;
380 me.focusedChild.focusEnter(me.focusedChild);
385 void Container_saveFocus(entity me)
387 me.savedFocus = me.focusedChild;
389 if(me.focusedChild.instanceOfContainer)
390 me.focusedChild.saveFocus(me.focusedChild);
393 void Container_moveItemAfter(entity me, entity other, entity dest)
395 // first: remove other from the chain
398 if(other.parent != me)
399 error("Can't move in wrong container!");
401 n = other.nextSibling;
402 p = other.prevSibling;
414 // now other got removed. Insert it behind dest now.
415 other.prevSibling = dest;
417 other.nextSibling = dest.nextSibling;
419 other.nextSibling = me.firstChild;
422 dest.nextSibling = other;
424 me.firstChild = other;
426 if(other.nextSibling)
427 other.nextSibling.prevSibling = other;
429 me.lastChild = other;
432 entity Container_preferredFocusedGrandChild(entity me)
439 for(e = me.firstChild; e; e = e.nextSibling)
441 if(e.instanceOfContainer)
443 e2 = e.preferredFocusedGrandChild(e);
445 if(!best || best.preferredFocusPriority < e2.preferredFocusPriority)
449 if(!best || best.preferredFocusPriority < e.preferredFocusPriority)