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);
66 if (f != '0 0 0') 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)
83 for (e = me.firstChild; e; e = e.nextSibling)
84 if (e.Container_alpha > 0) e.showNotify(e);
87 void Container_hideNotify(entity me)
90 if (!me.shown) return;
92 for (e = me.firstChild; e; e = e.nextSibling)
93 if (e.Container_alpha > 0) e.hideNotify(e);
96 void Container_setAlphaOf(entity me, entity other, float theAlpha)
100 if (other.Container_alpha > 0) other.hideNotify(other);
104 if (other.Container_alpha <= 0) other.showNotify(other);
106 other.Container_alpha = theAlpha;
109 void Container_resizeNotifyLie(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize, .vector originField, .vector sizeField, .vector fontScaleField)
114 for (e = me.firstChild; e; e = e.nextSibling)
118 me.enterLieSubitem(me, o, s, e.(fontScaleField), e.Container_alpha);
119 e.resizeNotify(e, o, s, boxToGlobal(o, absOrigin, absSize), boxToGlobalSize(s, absSize));
125 for (e = me.firstChild; e; e = e.nextSibling)
132 me.enterLieSubitem(me, o, s, e.(fontScaleField), e.Container_alpha);
133 e.resizeNotify(e, o, s, boxToGlobal(o, absOrigin, absSize), boxToGlobalSize(s, absSize));
138 SUPER(Container).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
141 void Container_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
143 me.resizeNotifyLie(me, relOrigin, relSize, absOrigin, absSize, Container_origin, Container_size, Container_fontscale);
146 entity Container_itemFromPoint(entity me, vector pos)
150 for (e = me.lastChild; e; e = e.prevSibling)
152 o = e.Container_origin;
153 s = e.Container_size;
154 if (pos.x < o.x) continue;
155 if (pos.y < o.y) continue;
156 if (pos.x >= o.x + s.x) continue;
157 if (pos.y >= o.y + s.y) continue;
163 void Container_draw(entity me)
168 for (e = me.firstChild; e; e = e.nextSibling)
170 if (e.focusable) me.focusable += 1;
171 if (e.Container_alpha < 0.003) // can't change color values anyway
173 me.enterSubitem(me, e);
178 SUPER(Container).draw(me);
181 void Container_focusLeave(entity me)
183 me.setFocus(me, NULL);
186 float Container_keyUp(entity me, float scan, float ascii, float shift)
193 me.enterSubitem(me, f);
194 r = f.keyUp(f, scan, ascii, shift);
201 float Container_keyDown(entity me, float scan, float ascii, float shift)
208 me.enterSubitem(me, f);
209 r = f.keyDown(f, scan, ascii, shift);
216 float Container_mouseMove(entity me, vector pos)
223 me.enterSubitem(me, f);
224 r = f.mouseMove(f, globalToBox(pos, f.Container_origin, f.Container_size));
230 float Container_mousePress(entity me, vector pos)
237 me.enterSubitem(me, f);
238 r = f.mousePress(f, globalToBox(pos, f.Container_origin, f.Container_size));
244 float Container_mouseDrag(entity me, vector pos)
251 me.enterSubitem(me, f);
252 r = f.mouseDrag(f, globalToBox(pos, f.Container_origin, f.Container_size));
258 float Container_mouseRelease(entity me, vector pos)
265 me.enterSubitem(me, f);
266 r = f.mouseRelease(f, globalToBox(pos, f.Container_origin, f.Container_size));
273 void Container_addItemCentered(entity me, entity other, vector theSize, float theAlpha)
275 me.addItem(me, other, '0.5 0.5 0' - 0.5 * theSize, theSize, theAlpha);
278 void Container_addItemRightCentered(entity me, entity other, vector theSize, float theAlpha)
280 me.addItem(me, other, '1 0.5 0' - 0.5 * theSize, theSize, theAlpha);
283 void Container_addItem(entity me, entity other, vector theOrigin, vector theSize, float theAlpha)
285 if (other.parent) error("Can't add already added item!");
287 if (other.focusable) me.focusable += 1;
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;
306 // don't call setAlphaOf, it would uneccessarily trigger showNotify
307 //me.setAlphaOf(me, other, theAlpha);
308 other.Container_alpha = theAlpha;
313 if (l) l.nextSibling = other;
314 else me.firstChild = other;
316 other.prevSibling = l;
317 other.nextSibling = NULL;
318 me.lastChild = other;
321 void Container_removeItem(entity me, entity other)
323 if (other.parent != me) error("Can't remove from wrong container!");
325 if (other.focusable) me.focusable -= 1;
330 n = other.nextSibling;
331 p = other.prevSibling;
333 if (p) p.nextSibling = n;
334 else me.firstChild = n;
336 if (n) n.prevSibling = p;
337 else me.lastChild = p;
340 void Container_setFocus(entity me, entity other)
342 if (me.focusedChild == other) return;
346 me.focusedChild.focused = 0;
347 me.focusedChild.focusLeave(me.focusedChild);
348 me.focusedChild = NULL;
353 if (!me.focused) error("Trying to set focus in a non-focused control!");
357 me.focusedChild = me.savedFocus;
358 me.savedFocus = NULL;
359 me.focusedChild.focused = 1;
360 me.focusedChild.focusEnter(me.focusedChild);
362 if (me.focusedChild.instanceOfContainer) me.focusedChild.setFocus(me.focusedChild, me.focusedChild.savedFocus);
366 me.focusedChild = other;
367 me.focusedChild.focused = 1;
368 me.focusedChild.focusEnter(me.focusedChild);
373 void Container_saveFocus(entity me)
375 me.savedFocus = me.focusedChild;
377 if (me.focusedChild.instanceOfContainer) me.focusedChild.saveFocus(me.focusedChild);
380 void Container_moveItemAfter(entity me, entity other, entity dest)
382 // first: remove other from the chain
385 if (other.parent != me) error("Can't move in wrong container!");
387 n = other.nextSibling;
388 p = other.prevSibling;
390 if (p) p.nextSibling = n;
391 else me.firstChild = n;
393 if (n) n.prevSibling = p;
394 else me.lastChild = p;
396 // now other got removed. Insert it behind dest now.
397 other.prevSibling = dest;
398 if (dest) other.nextSibling = dest.nextSibling;
399 else other.nextSibling = me.firstChild;
401 if (dest) dest.nextSibling = other;
402 else me.firstChild = other;
404 if (other.nextSibling) other.nextSibling.prevSibling = other;
405 else me.lastChild = other;
408 entity Container_preferredFocusedGrandChild(entity me)
415 for (e = me.firstChild; e; e = e.nextSibling)
417 if (e.instanceOfContainer)
419 e2 = e.preferredFocusedGrandChild(e);
421 if (!best || best.preferredFocusPriority < e2.preferredFocusPriority) best = e2;
424 if (!best || best.preferredFocusPriority < e.preferredFocusPriority) best = e;