]> icculus.org git repositories - taylor/freespace2.git/blob - src/ui/slider2.cpp
Initial revision
[taylor/freespace2.git] / src / ui / slider2.cpp
1 /*
2  * $Logfile: /Freespace2/code/Ui/SLIDER2.cpp $
3  * $Revision$
4  * $Date$
5  * $Author$
6  *
7  * Implements UI_SLIDER2 control
8  *
9  * $Log$
10  * Revision 1.1  2002/05/03 03:28:11  root
11  * Initial revision
12  *
13  * 
14  * 9     8/16/99 4:06p Dave
15  * Big honking checkin.
16  * 
17  * 8     8/11/99 12:18p Jefff
18  * added option to slider2 class to not force slider reset on
19  * set_numberItems
20  * 
21  * 7     8/10/99 6:54p Dave
22  * Mad optimizations. Added paging to the nebula effect.
23  * 
24  * 6     5/04/99 5:20p Dave
25  * Fixed up multiplayer join screen and host options screen. Should both
26  * be at 100% now.
27  * 
28  * 5     5/03/99 11:04p Dave
29  * Most of the way done with the multi join screen.
30  * 
31  * 4     4/29/99 2:15p Neilk
32  * fixed slider so there is an extra callback for mouse locks
33  * 
34  * 3     4/26/99 5:05p Neilk
35  * removed some excess debug output
36  * 
37  * 2     4/16/99 5:22p Neilk
38  * First implementation of UI_SLIDER2
39  *
40  * $NoKeywords: $
41  */
42
43 #include "uidefs.h"
44 #include "ui.h"
45 #include "freespace.h"
46 #include "bmpman.h"
47 #include "timer.h"
48
49 // captureCallback is called when an item is "selected" by mouse release. That is, the user has clicked, dragged and _released_. 
50 // the callback is called when the scrollbar has been released
51 void UI_SLIDER2::create(UI_WINDOW *wnd, int _x, int _y, int _w, int _h, int _numberItems, char *_bitmapSliderControl, void (* _upCallback)(), void (*_downCallback)(),
52                                 void (* _captureCallback)()) {
53
54         int buttonHeight, buttonWidth;
55
56         base_create( wnd, UI_KIND_SLIDER2, _x, _y, _w, _h );
57
58         Assert(_upCallback != NULL);
59         Assert(_downCallback != NULL);
60
61         upCallback = _upCallback;
62         downCallback = _downCallback;
63
64         captureCallback = _captureCallback;     
65
66         Assert(_bitmapSliderControl > 0);
67
68         last_scrolled = 0;
69
70         // set bitmap
71         set_bmaps(_bitmapSliderControl, 3, 0);
72         
73         // determine possible positions
74         bm_get_info(bmap_ids[S2_NORMAL],&buttonWidth, &buttonHeight, NULL, NULL, NULL);
75         slider_w = buttonWidth;
76         slider_h = buttonHeight;
77         Assert(buttonHeight > 5);
78         slider_half_h = (int)(buttonHeight / 2);
79         numberPositions = _h - buttonHeight;
80         
81         Assert(numberPositions >= 0);
82         currentItem = 0;
83         currentPosition = 0;
84
85         numberItems = _numberItems;
86         if (numberItems <= 0) {
87                 disabled_flag = 1;
88         }
89
90         slider_mode = S2M_DEFAULT;
91 }
92
93 void UI_SLIDER2::draw() {
94         Assert((currentPosition >= 0) && (currentPosition <= numberPositions));
95         if (uses_bmaps & !disabled_flag) {
96                 gr_reset_clip();
97                 switch (slider_mode) {
98                 case S2M_ON_ME:
99                         gr_set_bitmap(bmap_ids[S2_HIGHLIGHT]);  // draw slider level
100                         break;
101                 case S2M_MOVING:
102                         gr_set_bitmap(bmap_ids[S2_PRESSED]);
103                         break;
104                 case S2M_DEFAULT:
105                 default:
106                         gr_set_bitmap(bmap_ids[S2_NORMAL]);  // draw slider level
107                         break;
108                 }
109                 gr_bitmap(x, y+currentPosition);
110         }
111 }
112
113 void UI_SLIDER2::process(int focus)
114 {
115         int OnMe, keyfocus, mouse_lock_move;    
116
117         if (disabled_flag) {
118                 return;
119         }
120
121         keyfocus = 0;   
122         if (my_wnd->selected_gadget == this){
123                 keyfocus = 1;
124         }
125
126         OnMe = is_mouse_on();
127         if ( OnMe ) {
128                 // are we on the button?
129                 if ( (ui_mouse.y >= (y+currentPosition)) && (ui_mouse.y <= (y+currentPosition+slider_h)) ) {
130                         slider_mode = S2M_ON_ME;
131                         if ( B1_PRESSED ) {
132                                 mouse_locked = 1;
133                         }
134                 }
135         } else
136                 slider_mode = S2M_DEFAULT;
137
138         if ( !B1_PRESSED) {
139                 if (mouse_locked == 1)
140                         if (captureCallback != NULL) {
141                                 captureCallback();
142                                 mprintf(("Called captureCallback()!\n"));
143                         }
144                 mouse_locked = 0;
145         }                       
146         
147         if (!OnMe && !mouse_locked)
148                 return;
149         
150         // could we possibly be moving up?
151         if ((OnMe && B1_PRESSED && ui_mouse.y < (currentPosition+y+slider_half_h-1)) || (mouse_locked && (ui_mouse.y < (currentPosition+y+slider_half_h-1))) ) {                
152                 // make sure we wait at least 50 ms between events unless mouse locked 
153                 if ( (timer_get_milliseconds() > last_scrolled+50) || B1_JUST_PRESSED || mouse_locked ) {
154                         last_scrolled = timer_get_milliseconds();
155                         if (!mouse_locked) {
156                                 if (currentItem > 0) {
157                                         currentItem--;
158                                         if (upCallback != NULL) {
159                                                 upCallback();
160                                                 if (captureCallback != NULL)
161                                                         captureCallback();
162                                         }
163                                 }
164                                 currentPosition = fl2i((((float)currentItem/(float)numberItems) * (float)numberPositions)-.49);
165                         } else {
166                                 mouse_lock_move = fl2i( ((((float)ui_mouse.y - (float)y - (float)slider_half_h)/(float)numberPositions) * (float)numberItems) -.49);                            
167                                 mouse_lock_move = currentItem - mouse_lock_move;
168                                 if (mouse_lock_move > 0) {
169                                         while (mouse_lock_move >  0) {                                          
170                                                 if (currentItem > 0) {
171                                                         currentItem--;
172                                                         if (upCallback != NULL)
173                                                                 upCallback();
174                                                 }
175                                                 mouse_lock_move--;
176                                         }
177                                 }
178                                 // currentPosition = ui_mouse.y - y - slider_half_h;
179                                 currentPosition = fl2i((((float)currentItem/(float)numberItems) * (float)numberPositions)-.49);
180                         }
181                         if (currentPosition < 0)
182                                 currentPosition = 0;
183                         if (currentPosition > numberPositions)
184                         currentPosition = numberPositions;
185                         slider_mode = S2M_MOVING;       
186                 }
187         }
188
189         if ( ( OnMe && B1_PRESSED && ui_mouse.y > (currentPosition+y+slider_half_h+1)) || (mouse_locked && (ui_mouse.y > (currentPosition+y+slider_half_h+1)))  ) {             
190                 // make sure we wait at least 50 ms between events unless mouse locked 
191                 if ( (timer_get_milliseconds() > last_scrolled+50) || B1_JUST_PRESSED || mouse_locked ) {
192                         last_scrolled = timer_get_milliseconds();
193                         if (!mouse_locked) {
194                                 if (currentItem < numberItems) {
195                                         currentItem++;
196                                         if (downCallback != NULL) {
197                                                 downCallback();
198                                                 if (captureCallback != NULL)
199                                                         captureCallback();
200                                         }
201                                 }
202                                 currentPosition = fl2i((((float)currentItem/(float)numberItems) * (float)numberPositions)-.49);
203                         } else {
204                                 mouse_lock_move = fl2i( ((((float)ui_mouse.y - (float)y - (float)slider_half_h)/(float)numberPositions) * (float)numberItems) -.49);                            
205                                 mouse_lock_move -= currentItem;
206                                 if (mouse_lock_move > 0) {
207                                         while (mouse_lock_move > 0) {                                           
208                                                 if  (currentItem < numberItems) {
209                                                         currentItem++;
210                                                         if (downCallback != NULL)
211                                                                 downCallback();
212                                                 }
213                                                 mouse_lock_move--;
214                                         }
215                                 }
216                                 // currentPosition = ui_mouse.y - y - slider_half_h;
217                                 currentPosition = fl2i((((float)currentItem/(float)numberItems) * (float)numberPositions)-.49);
218                         }       
219                         if (currentPosition < 0){
220                                 currentPosition = 0;
221                         }
222                         if (currentPosition > numberPositions){
223                                 currentPosition = numberPositions;
224                         }
225                         slider_mode = S2M_MOVING;
226                 } 
227         } 
228
229         // if we are centerd on the bitmap and still in mouse lock mode, we need to make sure the MOVING bitmap is still shown
230         // or if mouse is on us and we are pressing the mouse button
231         if (mouse_locked || (OnMe && B1_PRESSED)){
232                 slider_mode = S2M_MOVING;
233         }
234 }
235
236 void UI_SLIDER2::hide()
237 {
238         hidden = 1;
239 }
240
241 void UI_SLIDER2::unhide()
242 {
243         hidden = 0;
244 }
245
246 int UI_SLIDER2::get_hidden()
247 {
248         return hidden;
249 }
250
251
252 // return number of itmes
253 int UI_SLIDER2::get_numberItems() {
254         return numberItems;
255 }
256
257 // return current position
258 int UI_SLIDER2::get_currentPosition() {
259         return currentPosition;
260 }
261
262 // return current item
263 int UI_SLIDER2::get_currentItem() {
264         return currentItem;
265 }
266
267 // change range. reset back to position 0
268 void UI_SLIDER2::set_numberItems(int _numberItems, int reset) {
269         numberItems = _numberItems;
270
271         if (reset) {
272                 currentItem = 0;
273                 currentPosition = 0;
274         } else {
275                 // recalcluate current position
276                 currentPosition = fl2i((((float)currentItem/(float)numberItems) * (float)numberPositions)-.49);
277                 if (currentPosition < 0){
278                         currentPosition = 0;
279                 }
280                 if (currentPosition > numberPositions){
281                         currentPosition = numberPositions;
282                 }
283         }
284         if (numberItems <= 0){
285                 disabled_flag = 1;
286         } else {
287                 disabled_flag = 0;
288         }
289 }
290
291 // force slider to new position manually
292 void UI_SLIDER2::set_currentItem(int _currentItem) {
293         if (_currentItem > numberItems) 
294                 return;
295
296         if (_currentItem == currentItem)
297                 return;
298
299         if (_currentItem < 0)
300                 return;
301
302         if (_currentItem > currentItem) {
303                 while (currentItem != _currentItem) {
304                         currentItem++;
305                         if (downCallback != NULL)
306                                 downCallback();
307                 }
308         } else if (_currentItem < currentItem) {
309                 while (currentItem != _currentItem) {
310                         currentItem--;
311                         if (upCallback != NULL)
312                                 upCallback();
313                 }
314         }       
315         
316         currentPosition = fl2i(((float)currentItem/(float)numberItems) * (float)numberPositions);       
317 }
318
319 void UI_SLIDER2::force_currentItem(int _currentItem) {  
320         currentItem = _currentItem;     
321         if(currentItem < 0){
322                 currentItem = 0;
323         };
324         currentPosition = fl2i(((float)currentItem/(float)numberItems) * (float)numberPositions);       
325 }
326
327 void UI_SLIDER2::forceDown() {
328         if (currentItem < numberItems) {
329                 currentItem++;
330                 currentPosition = fl2i(((float)currentItem/(float)numberItems) * (float)numberPositions);
331         }
332 }
333
334 void UI_SLIDER2::forceUp() {
335         if (currentItem > 0) {
336                 currentItem--;
337                 currentPosition = fl2i(((float)currentItem/(float)numberItems) * (float)numberPositions);
338         }
339 }