1 #ifndef ITEM_MODALCONTROLLER_H
2 #define ITEM_MODALCONTROLLER_H
3 #include "container.qc"
4 CLASS(ModalController, Container)
5 METHOD(ModalController, resizeNotify, void(entity, vector, vector, vector, vector))
6 METHOD(ModalController, draw, void(entity))
7 METHOD(ModalController, showChild, void(entity, entity, vector, vector, float))
8 METHOD(ModalController, hideChild, void(entity, entity, float))
9 METHOD(ModalController, hideAll, void(entity, float))
10 METHOD(ModalController, addItem, void(entity, entity, vector, vector, float))
11 METHOD(ModalController, addTab, void(entity, entity, entity))
13 METHOD(ModalController, initializeDialog, void(entity, entity))
15 METHOD(ModalController, switchState, void(entity, entity, float, float))
16 ATTRIB(ModalController, origin, vector, '0 0 0')
17 ATTRIB(ModalController, size, vector, '0 0 0')
18 ATTRIB(ModalController, previousButton, entity, NULL)
19 ATTRIB(ModalController, fadedAlpha, float, 0.3)
20 ENDCLASS(ModalController)
22 .entity tabSelectingButton;
25 void TabButton_Click(entity button, entity tab); // assumes a button has set the above fields to its own absolute origin, its size, and the tab to activate
26 void DialogOpenButton_Click(entity button, entity tab); // assumes a button has set the above fields to its own absolute origin, its size, and the tab to activate
27 void DialogOpenButton_Click_withCoords(entity button, entity tab, vector theOrigin, vector theSize);
28 void DialogCloseButton_Click(entity button, entity tab); // assumes a button has set the above fields to the tab to close
33 // modal dialog controller
34 // handles a stack of dialog elements
35 // each element can have one of the following states:
36 // 0: hidden (fading out)
37 // 1: visible (zooming in)
38 // 2: greyed out (inactive)
39 // While an animation is running, no item has focus. When an animation is done,
40 // the topmost item gets focus.
41 // The items are assumed to be added in overlapping order, that is, the lowest
42 // window must get added first.
45 // - to control a modal dialog:
46 // - show modal dialog: me.showChild(me, childItem, buttonAbsOrigin, buttonAbsSize, 0) // childItem also gets focus
47 // - dismiss modal dialog: me.hideChild(me, childItem, 0) // childItem fades out and relinquishes focus
48 // - show first screen in m_show: me.hideAll(me, 1); me.showChild(me, me.firstChild, '0 0 0', '0 0 0', 1);
49 // - to show a temporary dialog instead of the menu (teamselect): me.hideAll(me, 1); me.showChild(me, teamSelectDialog, '0 0 0', '0 0 0', 1);
50 // - as a tabbed dialog control:
51 // - to initialize: me.hideAll(me, 1); me.showChild(me, me.firstChild, '0 0 0', '0 0 0', 1);
52 // - to show a tab: me.hideChild(me, currentTab, 0); me.showChild(me, newTab, buttonAbsOrigin, buttonAbsSize, 0);
54 .vector ModalController_initialSize;
55 .vector ModalController_initialOrigin;
56 .vector ModalController_initialFontScale;
57 .float ModalController_initialAlpha;
58 .vector ModalController_buttonSize;
59 .vector ModalController_buttonOrigin;
60 .float ModalController_state;
61 .float ModalController_factor;
62 .entity ModalController_controllingButton;
64 void ModalController_initializeDialog(entity me, entity root)
67 me.showChild(me, root, '0 0 0', '0 0 0', 1); // someone else animates for us
70 void TabButton_Click(entity button, entity tab)
72 if(tab.ModalController_state == 1)
74 tab.parent.hideAll(tab.parent, 0);
75 button.forcePressed = 1;
76 tab.ModalController_controllingButton = button;
77 tab.parent.showChild(tab.parent, tab, button.origin, button.size, 0);
80 void DialogOpenButton_Click(entity button, entity tab)
82 DialogOpenButton_Click_withCoords(button, tab, button.origin, button.size);
85 void DialogOpenButton_Click_withCoords(entity button, entity tab, vector theOrigin, vector theSize)
87 if(tab.ModalController_state)
90 button.forcePressed = 1;
91 if(tab.parent.focusedChild)
92 tab.parent.focusedChild.saveFocus(tab.parent.focusedChild);
93 tab.ModalController_controllingButton = button;
94 tab.parent.showChild(tab.parent, tab, theOrigin, theSize, 0);
97 void DialogCloseButton_Click(entity button, entity tab)
99 tab.parent.hideChild(tab.parent, tab, 0);
102 void ModalController_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
104 me.resizeNotifyLie(me, relOrigin, relSize, absOrigin, absSize, ModalController_initialOrigin, ModalController_initialSize, ModalController_initialFontScale);
107 void ModalController_switchState(entity me, entity other, float state, float skipAnimation)
110 previousState = other.ModalController_state;
111 if(state == previousState && !skipAnimation)
113 other.ModalController_state = state;
117 other.ModalController_factor = 1 - other.Container_alpha / other.ModalController_initialAlpha;
121 other.ModalController_factor = other.Container_alpha / other.ModalController_initialAlpha;
122 if(previousState == 0 && !skipAnimation)
124 other.Container_origin = other.ModalController_buttonOrigin;
125 other.Container_size = other.ModalController_buttonSize;
130 other.ModalController_factor = bound(0, (1 - other.Container_alpha / other.ModalController_initialAlpha) / me.fadedAlpha, 1);
131 // fading out halfway
135 other.ModalController_factor = 1;
138 void ModalController_draw(entity me)
143 float f; // animation factor
144 float df; // animation step size
145 float prevFactor, targetFactor;
146 vector targetOrigin, targetSize; float targetAlpha;
151 for(e = me.firstChild; e; e = e.nextSibling)
152 if(e.ModalController_state)
155 me.switchState(me, front, 2, 0);
159 me.switchState(me, front, 1, 0);
161 df = frametime * 3; // animation speed
163 for(e = me.firstChild; e; e = e.nextSibling)
165 if(e.ModalController_state == 2)
167 // fading out partially
168 targetOrigin = e.Container_origin; // stay as is
169 targetSize = e.Container_size; // stay as is
170 targetAlpha = me.fadedAlpha * e.ModalController_initialAlpha;
172 else if(e.ModalController_state == 1)
175 targetOrigin = e.ModalController_initialOrigin;
176 targetSize = e.ModalController_initialSize;
177 targetAlpha = e.ModalController_initialAlpha;
182 targetOrigin = e.Container_origin; // stay as is
183 targetSize = e.Container_size; // stay as is
187 f = (e.ModalController_factor = min(1, e.ModalController_factor + df));
192 e.Container_origin = targetOrigin;
193 e.Container_size = targetSize;
194 me.setAlphaOf(me, e, targetAlpha);
198 prevFactor = (1 - f) / (1 - f + df);
199 if(!e.ModalController_state) // optimize code and avoid precision errors
200 me.setAlphaOf(me, e, e.Container_alpha * prevFactor);
204 targetFactor = df / (1 - f + df);
206 if(e.ModalController_state == 1)
208 e.Container_origin = e.Container_origin * prevFactor + targetOrigin * targetFactor;
209 e.Container_size = e.Container_size * prevFactor + targetSize * targetFactor;
211 me.setAlphaOf(me, e, e.Container_alpha * prevFactor + targetAlpha * targetFactor);
214 // assume: o == to * f_prev + X * (1 - f_prev)
215 // make: o' = to * f + X * (1 - f)
217 // X == (o - to * f_prev) / (1 - f_prev)
218 // o' = to * f + (o - to * f_prev) / (1 - f_prev) * (1 - f)
220 // o' = (to * (f - f_prev) + o * (1 - f)) / (1 - f_prev)
222 if(e.ModalController_state == 1)
224 fs = globalToBoxSize(e.Container_size, e.ModalController_initialSize);
225 e.Container_fontscale_x = fs.x * e.ModalController_initialFontScale.x;
226 e.Container_fontscale_y = fs.y * e.ModalController_initialFontScale.y;
230 if(animating || !me.focused)
231 me.setFocus(me, NULL);
233 me.setFocus(me, front);
234 SUPER(ModalController).draw(me);
237 void ModalController_addTab(entity me, entity other, entity tabButton)
239 me.addItem(me, other, '0 0 0', '1 1 1', 1);
240 tabButton.onClick = TabButton_Click;
241 tabButton.onClickEntity = other;
242 other.tabSelectingButton = tabButton;
243 if(other == me.firstChild)
245 tabButton.forcePressed = 1;
246 other.ModalController_controllingButton = tabButton;
247 me.showChild(me, other, '0 0 0', '0 0 0', 1);
251 void ModalController_addItem(entity me, entity other, vector theOrigin, vector theSize, float theAlpha)
253 SUPER(ModalController).addItem(me, other, theOrigin, theSize, (other == me.firstChild) ? theAlpha : 0);
254 other.ModalController_initialFontScale = other.Container_fontscale;
255 other.ModalController_initialSize = other.Container_size;
256 other.ModalController_initialOrigin = other.Container_origin;
257 other.ModalController_initialAlpha = theAlpha; // hope Container never modifies this
258 if(other.ModalController_initialFontScale == '0 0 0')
259 other.ModalController_initialFontScale = '1 1 0';
262 void ModalController_showChild(entity me, entity other, vector theOrigin, vector theSize, float skipAnimation)
264 if(other.ModalController_state == 0 || skipAnimation)
266 me.setFocus(me, NULL);
269 other.ModalController_buttonOrigin = globalToBox(theOrigin, me.origin, me.size);
270 other.ModalController_buttonSize = globalToBoxSize(theSize, me.size);
272 me.switchState(me, other, 1, skipAnimation);
273 } // zoom in from button (factor increases)
276 void ModalController_hideAll(entity me, float skipAnimation)
279 for(e = me.firstChild; e; e = e.nextSibling)
280 me.hideChild(me, e, skipAnimation);
283 void ModalController_hideChild(entity me, entity other, float skipAnimation)
285 if(other.ModalController_state || skipAnimation)
287 me.setFocus(me, NULL);
288 me.switchState(me, other, 0, skipAnimation);
289 if(other.ModalController_controllingButton)
291 other.ModalController_controllingButton.forcePressed = 0;
292 other.ModalController_controllingButton = NULL;
294 } // just alpha fade out (factor increases and decreases alpha)