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, addItemRightCentered, void(entity, entity, vector, float));
18 METHOD(Container, moveItemAfter, void(entity, entity, entity));
19 METHOD(Container, removeItem, void(entity, entity));
20 METHOD(Container, setFocus, void(entity, entity));
21 METHOD(Container, saveFocus, void(entity));
22 METHOD(Container, setAlphaOf, void(entity, entity, float));
23 METHOD(Container, itemFromPoint, entity(entity, vector));
24 METHOD(Container, showNotify, void(entity));
25 METHOD(Container, hideNotify, void(entity));
26 METHOD(Container, preferredFocusedGrandChild, entity(entity));
27 ATTRIB(Container, focusable, float, 0)
28 ATTRIB(Container, firstChild, entity, NULL)
29 ATTRIB(Container, lastChild, entity, NULL)
30 ATTRIB(Container, focusedChild, entity, NULL)
31 ATTRIB(Container, savedFocus, entity, NULL)
32 ATTRIB(Container, shown, float, 0)
34 METHOD(Container, enterSubitem, void(entity, entity));
35 METHOD(Container, enterLieSubitem, void(entity, vector, vector, vector, float));
36 METHOD(Container, leaveSubitem, void(entity));
41 .vector Container_origin;
42 .vector Container_size;
43 .vector Container_fontscale;
44 .float Container_alpha;
45 .vector Container_save_shift;
46 .vector Container_save_scale;
47 .vector Container_save_fontscale;
48 .float Container_save_alpha;
52 void Container_enterSubitem(entity me, entity sub)
54 me.enterLieSubitem(me, sub.Container_origin, sub.Container_size, sub.Container_fontscale, sub.Container_alpha);
57 void Container_enterLieSubitem(entity me, vector o, vector s, vector f, float a)
59 me.Container_save_shift = draw_shift;
60 me.Container_save_scale = draw_scale;
61 me.Container_save_alpha = draw_alpha;
62 me.Container_save_fontscale = draw_fontscale;
64 draw_shift = boxToGlobal(o, draw_shift, draw_scale);
65 draw_scale = boxToGlobalSize(s, draw_scale);
67 draw_fontscale = boxToGlobalSize(f, draw_fontscale);
71 void Container_leaveSubitem(entity me)
73 draw_shift = me.Container_save_shift;
74 draw_scale = me.Container_save_scale;
75 draw_alpha = me.Container_save_alpha;
76 draw_fontscale = me.Container_save_fontscale;
79 void Container_showNotify(entity me)
85 for(e = me.firstChild; e; e = e.nextSibling)
86 if(e.Container_alpha > 0)
90 void Container_hideNotify(entity me)
96 for(e = me.firstChild; e; e = e.nextSibling)
97 if(e.Container_alpha > 0)
101 void Container_setAlphaOf(entity me, entity other, float theAlpha)
105 if(other.Container_alpha > 0)
106 other.hideNotify(other);
110 if(other.Container_alpha <= 0)
111 other.showNotify(other);
113 other.Container_alpha = theAlpha;
116 void Container_resizeNotifyLie(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize, .vector originField, .vector sizeField, .vector fontScaleField)
121 for(e = me.firstChild; e; e = e.nextSibling)
125 me.enterLieSubitem(me, o, s, e.(fontScaleField), e.Container_alpha);
126 e.resizeNotify(e, o, s, boxToGlobal(o, absOrigin, absSize), boxToGlobalSize(s, absSize));
132 for(e = me.firstChild; e; e = e.nextSibling)
139 me.enterLieSubitem(me, o, s, e.(fontScaleField), e.Container_alpha);
140 e.resizeNotify(e, o, s, boxToGlobal(o, absOrigin, absSize), boxToGlobalSize(s, absSize));
145 SUPER(Container).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
148 void Container_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
150 me.resizeNotifyLie(me, relOrigin, relSize, absOrigin, absSize, Container_origin, Container_size, Container_fontscale);
153 entity Container_itemFromPoint(entity me, vector pos)
157 for(e = me.lastChild; e; e = e.prevSibling)
159 o = e.Container_origin;
160 s = e.Container_size;
161 if(pos.x < o.x) continue;
162 if(pos.y < o.y) continue;
163 if(pos.x >= o.x + s.x) continue;
164 if(pos.y >= o.y + s.y) continue;
170 void Container_draw(entity me)
175 for(e = me.firstChild; e; e = e.nextSibling)
179 if(e.Container_alpha < 0.003) // can't change color values anyway
181 me.enterSubitem(me, e);
186 SUPER(Container).draw(me);
189 void Container_focusLeave(entity me)
191 me.setFocus(me, NULL);
194 float Container_keyUp(entity me, float scan, float ascii, float shift)
201 me.enterSubitem(me, f);
202 r = f.keyUp(f, scan, ascii, shift);
209 float Container_keyDown(entity me, float scan, float ascii, float shift)
216 me.enterSubitem(me, f);
217 r = f.keyDown(f, scan, ascii, shift);
224 float Container_mouseMove(entity me, vector pos)
231 me.enterSubitem(me, f);
232 r = f.mouseMove(f, globalToBox(pos, f.Container_origin, f.Container_size));
238 float Container_mousePress(entity me, vector pos)
245 me.enterSubitem(me, f);
246 r = f.mousePress(f, globalToBox(pos, f.Container_origin, f.Container_size));
252 float Container_mouseDrag(entity me, vector pos)
259 me.enterSubitem(me, f);
260 r = f.mouseDrag(f, globalToBox(pos, f.Container_origin, f.Container_size));
266 float Container_mouseRelease(entity me, vector pos)
273 me.enterSubitem(me, f);
274 r = f.mouseRelease(f, globalToBox(pos, f.Container_origin, f.Container_size));
281 void Container_addItemCentered(entity me, entity other, vector theSize, float theAlpha)
283 me.addItem(me, other, '0.5 0.5 0' - 0.5 * theSize, theSize, theAlpha);
286 void Container_addItemRightCentered(entity me, entity other, vector theSize, float theAlpha)
288 me.addItem(me, other, '1 0.5 0' - 0.5 * theSize, theSize, theAlpha);
291 void Container_addItem(entity me, entity other, vector theOrigin, vector theSize, float theAlpha)
294 error("Can't add already added item!");
301 theOrigin.x -= 0.5 * (theSize.x - 1);
306 theOrigin.y -= 0.5 * (theSize.y - 1);
309 theOrigin.x = bound(0, theOrigin.x, 1 - theSize.x);
310 theOrigin.y = bound(0, theOrigin.y, 1 - theSize.y);
313 other.Container_origin = theOrigin;
314 other.Container_size = theSize;
315 me.setAlphaOf(me, other, theAlpha);
321 l.nextSibling = other;
323 me.firstChild = other;
325 other.prevSibling = l;
326 other.nextSibling = NULL;
327 me.lastChild = other;
330 void Container_removeItem(entity me, entity other)
332 if(other.parent != me)
333 error("Can't remove from wrong container!");
341 n = other.nextSibling;
342 p = other.prevSibling;
355 void Container_setFocus(entity me, entity other)
357 if(me.focusedChild == other)
362 me.focusedChild.focused = 0;
363 me.focusedChild.focusLeave(me.focusedChild);
364 me.focusedChild = NULL;
370 error("Trying to set focus in a non-focused control!");
374 me.focusedChild = me.savedFocus;
375 me.savedFocus = NULL;
376 me.focusedChild.focused = 1;
377 me.focusedChild.focusEnter(me.focusedChild);
379 if(me.focusedChild.instanceOfContainer)
380 me.focusedChild.setFocus(me.focusedChild, me.focusedChild.savedFocus);
384 me.focusedChild = other;
385 me.focusedChild.focused = 1;
386 me.focusedChild.focusEnter(me.focusedChild);
391 void Container_saveFocus(entity me)
393 me.savedFocus = me.focusedChild;
395 if(me.focusedChild.instanceOfContainer)
396 me.focusedChild.saveFocus(me.focusedChild);
399 void Container_moveItemAfter(entity me, entity other, entity dest)
401 // first: remove other from the chain
404 if(other.parent != me)
405 error("Can't move in wrong container!");
407 n = other.nextSibling;
408 p = other.prevSibling;
420 // now other got removed. Insert it behind dest now.
421 other.prevSibling = dest;
423 other.nextSibling = dest.nextSibling;
425 other.nextSibling = me.firstChild;
428 dest.nextSibling = other;
430 me.firstChild = other;
432 if(other.nextSibling)
433 other.nextSibling.prevSibling = other;
435 me.lastChild = other;
438 entity Container_preferredFocusedGrandChild(entity me)
445 for(e = me.firstChild; e; e = e.nextSibling)
447 if(e.instanceOfContainer)
449 e2 = e.preferredFocusedGrandChild(e);
451 if(!best || best.preferredFocusPriority < e2.preferredFocusPriority)
455 if(!best || best.preferredFocusPriority < e.preferredFocusPriority)