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, setFocus, void(entity, 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)
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 DialogCloseButton_Click(entity button, entity tab); // assumes a button has set the above fields to the tab to close
31 // modal dialog controller
32 // handles a stack of dialog elements
33 // each element can have one of the following states:
34 // 0: hidden (fading out)
35 // 1: visible (zooming in)
36 // 2: greyed out (inactive)
37 // While an animation is running, no item has focus. When an animation is done,
38 // the topmost item gets focus.
39 // The items are assumed to be added in overlapping order, that is, the lowest
40 // window must get added first.
43 // - to control a modal dialog:
44 // - show modal dialog: me.showChild(me, childItem, buttonAbsOrigin, buttonAbsSize, 0) // childItem also gets focus
45 // - dismiss modal dialog: me.hideChild(me, childItem, 0) // childItem fades out and relinquishes focus
46 // - show first screen in m_show: me.hideAll(me, 1); me.showChild(me, me.firstChild, '0 0 0', '0 0 0', 1);
47 // - 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);
48 // - as a tabbed dialog control:
49 // - to initialize: me.hideAll(me, 1); me.showChild(me, me.firstChild, '0 0 0', '0 0 0', 1);
50 // - to show a tab: me.hideChild(me, currentTab, 0); me.showChild(me, newTab, buttonAbsOrigin, buttonAbsSize, 0);
52 .vector ModalController_initialSize;
53 .vector ModalController_initialOrigin;
54 .float ModalController_initialAlpha;
55 .vector ModalController_buttonSize;
56 .vector ModalController_buttonOrigin;
57 .float ModalController_state;
58 .float ModalController_factor;
59 .entity ModalController_controllingButton;
61 void initializeDialogModalController(entity me, entity root)
64 me.showChild(me, root, '0 0 0', '0 0 0', 1); // someone else animates for us
67 void TabButton_Click(entity button, entity tab)
69 if(tab.ModalController_state == 1)
71 tab.parent.hideAll(tab.parent, 0);
72 button.forcePressed = 1;
73 tab.ModalController_controllingButton = button;
74 tab.parent.showChild(tab.parent, tab, button.origin, button.size, 0);
77 void DialogOpenButton_Click(entity button, entity tab)
79 if(tab.ModalController_state)
81 button.forcePressed = 1;
82 tab.ModalController_controllingButton = button;
83 tab.parent.showChild(tab.parent, tab, button.origin, button.size, 0);
86 void DialogCloseButton_Click(entity button, entity tab)
88 tab.parent.hideChild(tab.parent, tab, 0);
91 void resizeNotifyModalController(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
93 me.origin = absOrigin;
95 me.resizeNotifyLie(me, relOrigin, relSize, absOrigin, absSize, ModalController_initialOrigin, ModalController_initialSize);
98 void switchStateModalController(entity me, entity other, float state, float skipAnimation)
101 previousState = other.ModalController_state;
102 if(state == previousState)
104 other.ModalController_state = state;
108 other.ModalController_factor = 1 - other.Container_alpha / other.ModalController_initialAlpha;
112 other.ModalController_factor = other.Container_alpha / other.ModalController_initialAlpha;
113 if(previousState == 0 && !skipAnimation)
115 other.Container_origin = other.ModalController_buttonOrigin;
116 other.Container_size = other.ModalController_buttonSize;
121 other.ModalController_factor = bound(0, (1 - other.Container_alpha / other.ModalController_initialAlpha) / me.fadedAlpha, 1);
122 // fading out halfway
126 other.ModalController_factor = 1;
129 void drawModalController(entity me)
131 // TODO set up alpha, sizes and focus
135 float f; // animation factor
136 float df; // animation step size
137 float prevFactor, targetFactor;
138 vector targetOrigin, targetSize; float targetAlpha;
141 for(e = me.firstChild; e; e = e.Container_nextSibling)
142 if(e.ModalController_state)
145 me.switchState(me, front, 2, 0);
149 me.switchState(me, front, 1, 0);
151 df = frametime * 3; // animation speed
153 for(e = me.firstChild; e; e = e.Container_nextSibling)
155 f = (e.ModalController_factor = min(1, e.ModalController_factor + df));
156 if(e.ModalController_state)
162 prevFactor = (1 - f) / (1 - f + df);
163 targetFactor = df / (1 - f + df);
171 if(e.ModalController_state == 2)
173 // fading out partially
174 targetOrigin = e.Container_origin; // stay as is
175 targetSize = e.Container_size; // stay as is
176 targetAlpha = me.fadedAlpha * e.ModalController_initialAlpha;
178 else if(e.ModalController_state == 1)
181 targetOrigin = e.ModalController_initialOrigin;
182 targetSize = e.ModalController_initialSize;
183 targetAlpha = e.ModalController_initialAlpha;
190 targetOrigin = e.Container_origin; // stay as is
191 targetSize = e.Container_size; // stay as is
197 e.Container_origin = targetOrigin;
198 e.Container_size = targetSize;
199 e.Container_alpha = targetAlpha;
203 e.Container_origin = e.Container_origin * prevFactor + targetOrigin * targetFactor;
204 e.Container_size = e.Container_size * prevFactor + targetSize * targetFactor;
205 e.Container_alpha = e.Container_alpha * prevFactor + targetAlpha * targetFactor;
207 // assume: o == to * f_prev + X * (1 - f_prev)
208 // make: o' = to * f + X * (1 - f)
210 // X == (o - to * f_prev) / (1 - f_prev)
211 // o' = to * f + (o - to * f_prev) / (1 - f_prev) * (1 - f)
213 // o' = (to * (f - f_prev) + o * (1 - f)) / (1 - f_prev)
215 if(animating || !me.focused)
216 me.setFocus(me, NULL);
218 me.setFocus(me, front);
222 void addTabModalController(entity me, entity other, entity tabButton)
224 me.addItem(me, other, '0 0 0', '1 1 1', 1);
225 tabButton.onClick = TabButton_Click;
226 tabButton.onClickEntity = other;
227 if(other == me.firstChild)
229 tabButton.forcePressed = 1;
230 other.ModalController_controllingButton = tabButton;
231 me.showChild(me, other, '0 0 0', '0 0 0', 1);
235 void addItemModalController(entity me, entity other, vector theOrigin, vector theSize, float theAlpha)
237 other.ModalController_initialSize = theSize;
238 other.ModalController_initialOrigin = theOrigin;
239 other.ModalController_initialAlpha = theAlpha;
240 addItemContainer(me, other, theOrigin, theSize, theAlpha);
241 if(other != me.firstChild)
242 other.Container_alpha = 0;
245 void setFocusModalController(entity me, entity other)
247 //print("focus to ", etos(other), "\n");
248 setFocusContainer(me, other);
251 void showChildModalController(entity me, entity other, vector theOrigin, vector theSize, float skipAnimation)
253 if(other.ModalController_state == 0)
255 me.setFocus(me, NULL);
256 other.ModalController_buttonOrigin = globalToBox(theOrigin, me.origin, me.size);
257 other.ModalController_buttonSize = globalToBoxSize(theSize, me.size);
258 me.switchState(me, other, 1, skipAnimation);
259 } // zoom in from button (factor increases)
262 void hideAllModalController(entity me, float skipAnimation)
265 for(e = me.firstChild; e; e = e.Container_nextSibling)
266 me.hideChild(me, e, skipAnimation);
269 void hideChildModalController(entity me, entity other, float skipAnimation)
271 if(other.ModalController_state)
273 me.setFocus(me, NULL);
274 me.switchState(me, other, 0, skipAnimation);
275 if(other.ModalController_controllingButton)
277 other.ModalController_controllingButton.forcePressed = 0;
278 other.ModalController_controllingButton = NULL;
280 } // just alpha fade out (factor increases and decreases alpha)