1 #include "container.qh"
3 void Container_enterSubitem(entity me, entity sub)
5 me.enterLieSubitem(me, sub.Container_origin, sub.Container_size, sub.Container_fontscale, sub.Container_alpha);
8 void Container_enterLieSubitem(entity me, vector o, vector s, vector f, float a)
10 me.Container_save_shift = draw_shift;
11 me.Container_save_scale = draw_scale;
12 me.Container_save_alpha = draw_alpha;
13 me.Container_save_fontscale = draw_fontscale;
15 draw_shift = boxToGlobal(o, draw_shift, draw_scale);
16 draw_scale = boxToGlobalSize(s, draw_scale);
17 if (f != '0 0 0') draw_fontscale = boxToGlobalSize(f, draw_fontscale);
21 void Container_leaveSubitem(entity me)
23 draw_shift = me.Container_save_shift;
24 draw_scale = me.Container_save_scale;
25 draw_alpha = me.Container_save_alpha;
26 draw_fontscale = me.Container_save_fontscale;
29 void Container_showNotify(entity me)
34 for (e = me.firstChild; e; e = e.nextSibling)
35 if (e.Container_alpha > 0) e.showNotify(e);
38 void Container_hideNotify(entity me)
41 if (!me.shown) return;
43 for (e = me.firstChild; e; e = e.nextSibling)
44 if (e.Container_alpha > 0) e.hideNotify(e);
47 void Container_setAlphaOf(entity me, entity other, float theAlpha)
51 if (other.Container_alpha > 0) other.hideNotify(other);
55 if (other.Container_alpha <= 0) other.showNotify(other);
57 other.Container_alpha = theAlpha;
60 void Container_resizeNotifyLie(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize, .vector originField, .vector sizeField, .vector fontScaleField)
65 for (e = me.firstChild; e; e = e.nextSibling)
69 me.enterLieSubitem(me, o, s, e.(fontScaleField), e.Container_alpha);
70 e.resizeNotify(e, o, s, boxToGlobal(o, absOrigin, absSize), boxToGlobalSize(s, absSize));
76 for (e = me.firstChild; e; e = e.nextSibling)
83 me.enterLieSubitem(me, o, s, e.(fontScaleField), e.Container_alpha);
84 e.resizeNotify(e, o, s, boxToGlobal(o, absOrigin, absSize), boxToGlobalSize(s, absSize));
89 SUPER(Container).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
92 void Container_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
94 me.resizeNotifyLie(me, relOrigin, relSize, absOrigin, absSize, Container_origin, Container_size, Container_fontscale);
97 entity Container_itemFromPoint(entity me, vector pos)
101 for (e = me.lastChild; e; e = e.prevSibling)
103 o = e.Container_origin;
104 s = e.Container_size;
105 if (pos.x < o.x) continue;
106 if (pos.y < o.y) continue;
107 if (pos.x >= o.x + s.x) continue;
108 if (pos.y >= o.y + s.y) continue;
114 void Container_draw(entity me)
119 for (e = me.firstChild; e; e = e.nextSibling)
121 if (e.focusable) me.focusable += 1;
122 if (e.Container_alpha < 0.003) // can't change color values anyway
124 me.enterSubitem(me, e);
129 SUPER(Container).draw(me);
132 void Container_focusLeave(entity me)
134 me.setFocus(me, NULL);
137 float Container_keyUp(entity me, float scan, float ascii, float shift)
144 me.enterSubitem(me, f);
145 r = f.keyUp(f, scan, ascii, shift);
152 float Container_keyDown(entity me, float scan, float ascii, float shift)
159 me.enterSubitem(me, f);
160 r = f.keyDown(f, scan, ascii, shift);
167 float Container_mouseMove(entity me, vector pos)
174 me.enterSubitem(me, f);
175 r = f.mouseMove(f, globalToBox(pos, f.Container_origin, f.Container_size));
181 METHOD(Container, mousePress, bool(Container this, vector pos))
183 entity f = this.focusedChild;
186 this.enterSubitem(this, f);
187 bool r = f.mousePress(f, globalToBox(pos, f.Container_origin, f.Container_size));
188 this.leaveSubitem(this);
193 float Container_mouseDrag(entity me, vector pos)
200 me.enterSubitem(me, f);
201 r = f.mouseDrag(f, globalToBox(pos, f.Container_origin, f.Container_size));
207 float Container_mouseRelease(entity me, vector pos)
214 me.enterSubitem(me, f);
215 r = f.mouseRelease(f, globalToBox(pos, f.Container_origin, f.Container_size));
222 void Container_addItemCentered(entity me, entity other, vector theSize, float theAlpha)
224 me.addItem(me, other, '0.5 0.5 0' - 0.5 * theSize, theSize, theAlpha);
227 void Container_addItemRightCentered(entity me, entity other, vector theSize, float theAlpha)
229 me.addItem(me, other, '1 0.5 0' - 0.5 * theSize, theSize, theAlpha);
232 void Container_addItem(entity me, entity other, vector theOrigin, vector theSize, float theAlpha)
234 if (other.parent) error("Can't add already added item!");
236 if (other.focusable) me.focusable += 1;
240 theOrigin.x -= 0.5 * (theSize.x - 1);
245 theOrigin.y -= 0.5 * (theSize.y - 1);
248 theOrigin.x = bound(0, theOrigin.x, 1 - theSize.x);
249 theOrigin.y = bound(0, theOrigin.y, 1 - theSize.y);
252 other.Container_origin = theOrigin;
253 other.Container_size = theSize;
255 // don't call setAlphaOf, it would uneccessarily trigger showNotify
256 //me.setAlphaOf(me, other, theAlpha);
257 other.Container_alpha = theAlpha;
262 if (l) l.nextSibling = other;
263 else me.firstChild = other;
265 other.prevSibling = l;
266 other.nextSibling = NULL;
267 me.lastChild = other;
270 void Container_removeItem(entity me, entity other)
272 if (other.parent != me) error("Can't remove from wrong container!");
274 if (other.focusable) me.focusable -= 1;
279 n = other.nextSibling;
280 p = other.prevSibling;
282 if (p) p.nextSibling = n;
283 else me.firstChild = n;
285 if (n) n.prevSibling = p;
286 else me.lastChild = p;
289 void Container_setFocus(entity me, entity other)
291 if (me.focusedChild == other) return;
295 me.focusedChild.focused = 0;
296 me.focusedChild.focusLeave(me.focusedChild);
297 me.focusedChild = NULL;
302 if (!me.focused) error("Trying to set focus in a non-focused control!");
306 me.focusedChild = me.savedFocus;
307 me.savedFocus = NULL;
308 me.focusedChild.focused = 1;
309 me.focusedChild.focusEnter(me.focusedChild);
311 if (me.focusedChild.instanceOfContainer)
312 me.focusedChild.setFocus(me.focusedChild, me.focusedChild.savedFocus);
316 me.focusedChild = other;
317 me.focusedChild.focused = 1;
318 me.focusedChild.focusEnter(me.focusedChild);
323 void Container_saveFocus(entity me)
325 me.savedFocus = me.focusedChild;
327 if (me.focusedChild.instanceOfContainer) me.focusedChild.saveFocus(me.focusedChild);
330 void Container_moveItemAfter(entity me, entity other, entity dest)
332 // first: remove other from the chain
335 if (other.parent != me) error("Can't move in wrong container!");
337 n = other.nextSibling;
338 p = other.prevSibling;
340 if (p) p.nextSibling = n;
341 else me.firstChild = n;
343 if (n) n.prevSibling = p;
344 else me.lastChild = p;
346 // now other got removed. Insert it behind dest now.
347 other.prevSibling = dest;
348 if (dest) other.nextSibling = dest.nextSibling;
349 else other.nextSibling = me.firstChild;
351 if (dest) dest.nextSibling = other;
352 else me.firstChild = other;
354 if (other.nextSibling) other.nextSibling.prevSibling = other;
355 else me.lastChild = other;
358 entity Container_preferredFocusedGrandChild(entity me)
365 for (e = me.firstChild; e; e = e.nextSibling)
367 if (e.instanceOfContainer)
369 e2 = e.preferredFocusedGrandChild(e);
371 if (!best || best.preferredFocusPriority < e2.preferredFocusPriority) best = e2;
374 if (!best || best.preferredFocusPriority < e.preferredFocusPriority) best = e;