]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/item/modalcontroller.qc
Uncrustify menu/{anim,item}/*
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / item / modalcontroller.qc
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));
12
13                 METHOD(ModalController, initializeDialog, void(entity, entity));
14
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)
21
22         .entity tabSelectingButton;
23         .vector origin;
24         .vector size;
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
29 #endif
30
31 #ifdef IMPLEMENTATION
32
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.
43 //
44 // Possible uses:
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);
53
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;
63
64         void ModalController_initializeDialog(entity me, entity root)
65         {
66                 me.hideAll(me, 1);
67                 me.showChild(me, root, '0 0 0', '0 0 0', 1);  // someone else animates for us
68         }
69
70         void TabButton_Click(entity button, entity tab)
71         {
72                 if (tab.ModalController_state == 1) return;
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);
77         }
78
79         void DialogOpenButton_Click(entity button, entity tab)
80         {
81                 DialogOpenButton_Click_withCoords(button, tab, button.origin, button.size);
82         }
83
84         void DialogOpenButton_Click_withCoords(entity button, entity tab, vector theOrigin, vector theSize)
85         {
86                 if (tab.ModalController_state) return;
87                 if (button) button.forcePressed = 1;
88                 if (tab.parent.focusedChild) tab.parent.focusedChild.saveFocus(tab.parent.focusedChild);
89                 tab.ModalController_controllingButton = button;
90                 tab.parent.showChild(tab.parent, tab, theOrigin, theSize, 0);
91         }
92
93         void DialogCloseButton_Click(entity button, entity tab)
94         {
95                 tab.parent.hideChild(tab.parent, tab, 0);
96         }
97
98         void ModalController_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
99         {
100                 me.resizeNotifyLie(me, relOrigin, relSize, absOrigin, absSize, ModalController_initialOrigin, ModalController_initialSize, ModalController_initialFontScale);
101         }
102
103         void ModalController_switchState(entity me, entity other, float state, float skipAnimation)
104         {
105                 float previousState;
106                 previousState = other.ModalController_state;
107                 if (state == previousState && !skipAnimation) return;
108                 other.ModalController_state = state;
109                 switch (state)
110                 {
111                         case 0:
112                                 other.ModalController_factor = 1 - other.Container_alpha / other.ModalController_initialAlpha;
113                                 // fading out
114                                 break;
115                         case 1:
116                                 other.ModalController_factor = other.Container_alpha / other.ModalController_initialAlpha;
117                                 if (previousState == 0 && !skipAnimation)
118                                 {
119                                         other.Container_origin = other.ModalController_buttonOrigin;
120                                         other.Container_size = other.ModalController_buttonSize;
121                                 }
122                                 // zooming in
123                                 break;
124                         case 2:
125                                 other.ModalController_factor = bound(0, (1 - other.Container_alpha / other.ModalController_initialAlpha) / me.fadedAlpha, 1);
126                                 // fading out halfway
127                                 break;
128                 }
129                 if (skipAnimation) other.ModalController_factor = 1;
130         }
131
132         void ModalController_draw(entity me)
133         {
134                 entity e;
135                 entity front;
136                 float animating;
137                 float f;  // animation factor
138                 float df; // animation step size
139                 float prevFactor, targetFactor;
140                 vector targetOrigin, targetSize;
141                 float targetAlpha;
142                 vector fs;
143                 animating = 0;
144
145                 front = NULL;
146                 for (e = me.firstChild; e; e = e.nextSibling)
147                         if (e.ModalController_state)
148                         {
149                                 if (front) me.switchState(me, front, 2, 0);
150                                 front = e;
151                         }
152                 if (front) me.switchState(me, front, 1, 0);
153
154                 df = frametime * 3;  // animation speed
155
156                 for (e = me.firstChild; e; e = e.nextSibling)
157                 {
158                         if (e.ModalController_state == 2)
159                         {
160                                 // fading out partially
161                                 targetOrigin = e.Container_origin; // stay as is
162                                 targetSize = e.Container_size;     // stay as is
163                                 targetAlpha = me.fadedAlpha * e.ModalController_initialAlpha;
164                         }
165                         else if (e.ModalController_state == 1)
166                         {
167                                 // zooming in
168                                 targetOrigin = e.ModalController_initialOrigin;
169                                 targetSize = e.ModalController_initialSize;
170                                 targetAlpha = e.ModalController_initialAlpha;
171                         }
172                         else
173                         {
174                                 // fading out
175                                 targetOrigin = e.Container_origin; // stay as is
176                                 targetSize = e.Container_size;     // stay as is
177                                 targetAlpha = 0;
178                         }
179
180                         f = (e.ModalController_factor = min(1, e.ModalController_factor + df));
181                         if (f == 1)
182                         {
183                                 prevFactor = 0;
184                                 targetFactor = 1;
185                                 e.Container_origin = targetOrigin;
186                                 e.Container_size = targetSize;
187                                 me.setAlphaOf(me, e, targetAlpha);
188                         }
189                         else
190                         {
191                                 prevFactor = (1 - f) / (1 - f + df);
192                                 if (!e.ModalController_state)  // optimize code and avoid precision errors
193                                 {
194                                         me.setAlphaOf(me, e, e.Container_alpha  * prevFactor);
195                                 }
196                                 else
197                                 {
198                                         animating = 1;
199                                         targetFactor = df / (1 - f + df);
200
201                                         if (e.ModalController_state == 1)
202                                         {
203                                                 e.Container_origin = e.Container_origin * prevFactor + targetOrigin * targetFactor;
204                                                 e.Container_size   = e.Container_size   * prevFactor + targetSize   * targetFactor;
205                                         }
206                                         me.setAlphaOf(me, e, e.Container_alpha  * prevFactor + targetAlpha  * targetFactor);
207                                 }
208                         }
209                         // assume: o == to * f_prev + X * (1 - f_prev)
210                         // make:   o' = to * f  + X * (1 - f)
211                         // -->
212                         // X == (o - to * f_prev) / (1 - f_prev)
213                         // o' = to * f + (o - to * f_prev) / (1 - f_prev) * (1 - f)
214                         // --> (maxima)
215                         // o' = (to * (f - f_prev) + o * (1 - f)) / (1 - f_prev)
216
217                         if (e.ModalController_state == 1)
218                         {
219                                 fs = globalToBoxSize(e.Container_size, e.ModalController_initialSize);
220                                 e.Container_fontscale_x = fs.x * e.ModalController_initialFontScale.x;
221                                 e.Container_fontscale_y = fs.y * e.ModalController_initialFontScale.y;
222                         }
223                 }
224
225                 if (animating || !me.focused) me.setFocus(me, NULL);
226                 else me.setFocus(me, front);
227                 SUPER(ModalController).draw(me);
228         }
229
230         void ModalController_addTab(entity me, entity other, entity tabButton)
231         {
232                 me.addItem(me, other, '0 0 0', '1 1 1', 1);
233                 tabButton.onClick = TabButton_Click;
234                 tabButton.onClickEntity = other;
235                 other.tabSelectingButton = tabButton;
236                 if (other == me.firstChild)
237                 {
238                         tabButton.forcePressed = 1;
239                         other.ModalController_controllingButton = tabButton;
240                         me.showChild(me, other, '0 0 0', '0 0 0', 1);
241                 }
242         }
243
244         void ModalController_addItem(entity me, entity other, vector theOrigin, vector theSize, float theAlpha)
245         {
246                 SUPER(ModalController).addItem(me, other, theOrigin, theSize, (other == me.firstChild) ? theAlpha : 0);
247                 other.ModalController_initialFontScale = other.Container_fontscale;
248                 other.ModalController_initialSize = other.Container_size;
249                 other.ModalController_initialOrigin = other.Container_origin;
250                 other.ModalController_initialAlpha = theAlpha;  // hope Container never modifies this
251                 if (other.ModalController_initialFontScale == '0 0 0') other.ModalController_initialFontScale = '1 1 0';
252         }
253
254         void ModalController_showChild(entity me, entity other, vector theOrigin, vector theSize, float skipAnimation)
255         {
256                 if (other.ModalController_state == 0 || skipAnimation)
257                 {
258                         me.setFocus(me, NULL);
259                         if (!skipAnimation)
260                         {
261                                 other.ModalController_buttonOrigin = globalToBox(theOrigin, me.origin, me.size);
262                                 other.ModalController_buttonSize = globalToBoxSize(theSize, me.size);
263                         }
264                         me.switchState(me, other, 1, skipAnimation);
265                 }  // zoom in from button (factor increases)
266         }
267
268         void ModalController_hideAll(entity me, float skipAnimation)
269         {
270                 entity e;
271                 for (e = me.firstChild; e; e = e.nextSibling)
272                         me.hideChild(me, e, skipAnimation);
273         }
274
275         void ModalController_hideChild(entity me, entity other, float skipAnimation)
276         {
277                 if (other.ModalController_state || skipAnimation)
278                 {
279                         me.setFocus(me, NULL);
280                         me.switchState(me, other, 0, skipAnimation);
281                         if (other.ModalController_controllingButton)
282                         {
283                                 other.ModalController_controllingButton.forcePressed = 0;
284                                 other.ModalController_controllingButton = NULL;
285                         }
286                 }  // just alpha fade out (factor increases and decreases alpha)
287         }
288 #endif