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