2 CLASS(ModalController) EXTENDS(Container)
3 METHOD(ModalController, resizeNotify, void(entity, vector, vector, vector, vector))
4 METHOD(ModalController, draw, void(entity))
5 METHOD(ModalController, addItem, void(entity, entity, vector, vector, float))
6 METHOD(ModalController, showChild, void(entity, entity, vector, vector, float))
7 METHOD(ModalController, hideChild, void(entity, entity, float))
8 METHOD(ModalController, hideAll, void(entity, float))
9 METHOD(ModalController, addItem, void(entity, entity, vector, vector, float))
10 METHOD(ModalController, addTab, void(entity, entity, entity))
12 METHOD(ModalController, initializeDialog, void(entity, entity))
14 METHOD(ModalController, switchState, void(entity, entity, float, float))
15 ATTRIB(ModalController, origin, vector, '0 0 0')
16 ATTRIB(ModalController, size, vector, '0 0 0')
17 ATTRIB(ModalController, previousButton, entity, NULL)
18 ATTRIB(ModalController, fadedAlpha, float, 0.3)
19 ENDCLASS(ModalController)
21 .entity tabSelectingButton;
24 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
25 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
26 void DialogOpenButton_Click_withCoords(entity button, entity tab, vector theOrigin, vector theSize);
27 void DialogCloseButton_Click(entity button, entity tab); // assumes a button has set the above fields to the tab to close
32 // modal dialog controller
33 // handles a stack of dialog elements
34 // each element can have one of the following states:
35 // 0: hidden (fading out)
36 // 1: visible (zooming in)
37 // 2: greyed out (inactive)
38 // While an animation is running, no item has focus. When an animation is done,
39 // the topmost item gets focus.
40 // The items are assumed to be added in overlapping order, that is, the lowest
41 // window must get added first.
44 // - to control a modal dialog:
45 // - show modal dialog: me.showChild(me, childItem, buttonAbsOrigin, buttonAbsSize, 0) // childItem also gets focus
46 // - dismiss modal dialog: me.hideChild(me, childItem, 0) // childItem fades out and relinquishes focus
47 // - show first screen in m_show: me.hideAll(me, 1); me.showChild(me, me.firstChild, '0 0 0', '0 0 0', 1);
48 // - 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);
49 // - as a tabbed dialog control:
50 // - to initialize: me.hideAll(me, 1); me.showChild(me, me.firstChild, '0 0 0', '0 0 0', 1);
51 // - to show a tab: me.hideChild(me, currentTab, 0); me.showChild(me, newTab, buttonAbsOrigin, buttonAbsSize, 0);
53 .vector ModalController_initialSize;
54 .vector ModalController_initialOrigin;
55 .vector ModalController_initialFontScale;
56 .float ModalController_initialAlpha;
57 .vector ModalController_buttonSize;
58 .vector ModalController_buttonOrigin;
59 .float ModalController_state;
60 .float ModalController_factor;
61 .entity ModalController_controllingButton;
63 void ModalController_initializeDialog(entity me, entity root)
66 me.showChild(me, root, '0 0 0', '0 0 0', 1); // someone else animates for us
69 void TabButton_Click(entity button, entity tab)
71 if(tab.ModalController_state == 1)
73 tab.parent.hideAll(tab.parent, 0);
74 button.forcePressed = 1;
75 tab.ModalController_controllingButton = button;
76 tab.parent.showChild(tab.parent, tab, button.origin, button.size, 0);
79 void DialogOpenButton_Click(entity button, entity tab)
81 DialogOpenButton_Click_withCoords(button, tab, button.origin, button.size);
84 void DialogOpenButton_Click_withCoords(entity button, entity tab, vector theOrigin, vector theSize)
86 if(tab.ModalController_state)
89 button.forcePressed = 1;
90 tab.ModalController_controllingButton = button;
91 tab.parent.showChild(tab.parent, tab, theOrigin, theSize, 0);
94 void DialogCloseButton_Click(entity button, entity tab)
96 tab.parent.hideChild(tab.parent, tab, 0);
99 void ModalController_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
101 me.resizeNotifyLie(me, relOrigin, relSize, absOrigin, absSize, ModalController_initialOrigin, ModalController_initialSize, ModalController_initialFontScale);
104 void ModalController_switchState(entity me, entity other, float state, float skipAnimation)
107 previousState = other.ModalController_state;
108 if(state == previousState && !skipAnimation)
110 other.ModalController_state = state;
114 other.ModalController_factor = 1 - other.Container_alpha / other.ModalController_initialAlpha;
118 other.ModalController_factor = other.Container_alpha / other.ModalController_initialAlpha;
119 if(previousState == 0 && !skipAnimation)
121 other.Container_origin = other.ModalController_buttonOrigin;
122 other.Container_size = other.ModalController_buttonSize;
127 other.ModalController_factor = bound(0, (1 - other.Container_alpha / other.ModalController_initialAlpha) / me.fadedAlpha, 1);
128 // fading out halfway
132 other.ModalController_factor = 1;
135 void ModalController_draw(entity me)
140 float f; // animation factor
141 float df; // animation step size
142 float prevFactor, targetFactor;
143 vector targetOrigin, targetSize; float targetAlpha;
148 for(e = me.firstChild; e; e = e.nextSibling)
149 if(e.ModalController_state)
152 me.switchState(me, front, 2, 0);
156 me.switchState(me, front, 1, 0);
158 df = frametime * 3; // animation speed
160 for(e = me.firstChild; e; e = e.nextSibling)
162 f = (e.ModalController_factor = min(1, e.ModalController_factor + df));
163 if(e.ModalController_state)
169 prevFactor = (1 - f) / (1 - f + df);
170 targetFactor = df / (1 - f + df);
178 if(e.ModalController_state == 2)
180 // fading out partially
181 targetOrigin = e.Container_origin; // stay as is
182 targetSize = e.Container_size; // stay as is
183 targetAlpha = me.fadedAlpha * e.ModalController_initialAlpha;
185 else if(e.ModalController_state == 1)
188 targetOrigin = e.ModalController_initialOrigin;
189 targetSize = e.ModalController_initialSize;
190 targetAlpha = e.ModalController_initialAlpha;
197 targetOrigin = e.Container_origin; // stay as is
198 targetSize = e.Container_size; // stay as is
204 e.Container_origin = targetOrigin;
205 e.Container_size = targetSize;
206 me.setAlphaOf(me, e, targetAlpha);
210 e.Container_origin = e.Container_origin * prevFactor + targetOrigin * targetFactor;
211 e.Container_size = e.Container_size * prevFactor + targetSize * targetFactor;
212 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 fs = globalToBoxSize(e.Container_size, e.ModalController_initialSize);
223 e.Container_fontscale_x = fs_x * e.ModalController_initialFontScale_x;
224 e.Container_fontscale_y = fs_y * e.ModalController_initialFontScale_y;
226 if(animating || !me.focused)
227 me.setFocus(me, NULL);
229 me.setFocus(me, front);
230 SUPER(ModalController).draw(me);
233 void ModalController_addTab(entity me, entity other, entity tabButton)
235 me.addItem(me, other, '0 0 0', '1 1 1', 1);
236 tabButton.onClick = TabButton_Click;
237 tabButton.onClickEntity = other;
238 other.tabSelectingButton = tabButton;
239 if(other == me.firstChild)
241 tabButton.forcePressed = 1;
242 other.ModalController_controllingButton = tabButton;
243 me.showChild(me, other, '0 0 0', '0 0 0', 1);
247 void ModalController_addItem(entity me, entity other, vector theOrigin, vector theSize, float theAlpha)
249 SUPER(ModalController).addItem(me, other, theOrigin, theSize, (other == me.firstChild) ? theAlpha : 0);
250 other.ModalController_initialFontScale = other.Container_fontscale;
251 other.ModalController_initialSize = other.Container_size;
252 other.ModalController_initialOrigin = other.Container_origin;
253 other.ModalController_initialAlpha = theAlpha; // hope Container never modifies this
254 if(other.ModalController_initialFontScale == '0 0 0')
255 other.ModalController_initialFontScale = '1 1 0';
258 void ModalController_showChild(entity me, entity other, vector theOrigin, vector theSize, float skipAnimation)
260 if(other.ModalController_state == 0 || skipAnimation)
262 me.setFocus(me, NULL);
265 other.ModalController_buttonOrigin = globalToBox(theOrigin, me.origin, me.size);
266 other.ModalController_buttonSize = globalToBoxSize(theSize, me.size);
268 me.switchState(me, other, 1, skipAnimation);
269 } // zoom in from button (factor increases)
272 void ModalController_hideAll(entity me, float skipAnimation)
275 for(e = me.firstChild; e; e = e.nextSibling)
276 me.hideChild(me, e, skipAnimation);
279 void ModalController_hideChild(entity me, entity other, float skipAnimation)
281 if(other.ModalController_state || skipAnimation)
283 me.setFocus(me, NULL);
284 me.switchState(me, other, 0, skipAnimation);
285 if(other.ModalController_controllingButton)
287 other.ModalController_controllingButton.forcePressed = 0;
288 other.ModalController_controllingButton = NULL;
290 } // just alpha fade out (factor increases and decreases alpha)