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