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