]> icculus.org git repositories - taylor/freespace2.git/blob - src/ui/scroll.cpp
clean up and simplify effects and updating
[taylor/freespace2.git] / src / ui / scroll.cpp
1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell 
5  * or otherwise commercially exploit the source or things you created based on
6  * the source.
7  */
8
9 /*
10  * $Logfile: /Freespace2/code/UI/SCROLL.cpp $
11  * $Revision$
12  * $Date$
13  * $Author$
14  *
15  * Code for vertical scrollbars.
16  *
17  * $Log$
18  * Revision 1.3  2004/09/20 01:31:45  theoddone33
19  * GCC 3.4 fixes.
20  *
21  * Revision 1.2  2002/06/09 04:41:29  relnev
22  * added copyright header
23  *
24  * Revision 1.1.1.1  2002/05/03 03:28:11  root
25  * Initial import.
26  *
27  * 
28  * 4     12/02/98 5:47p Dave
29  * Put in interface xstr code. Converted barracks screen to new format.
30  * 
31  * 3     10/13/98 9:29a Dave
32  * Started neatening up freespace.h. Many variables renamed and
33  * reorganized. Added AlphaColors.[h,cpp]
34  * 
35  * 2     10/07/98 10:54a Dave
36  * Initial checkin.
37  * 
38  * 1     10/07/98 10:51a Dave
39  * 
40  * 15    3/23/98 5:48p Hoffoss
41  * Improved listbox handling.  Most notibly the scrollbar arrows work now.
42  * 
43  * 14    2/03/98 4:21p Hoffoss
44  * Made UI controls draw white text when disabled.
45  * 
46  * 13    1/14/98 6:44p Hoffoss
47  * Massive changes to UI code.  A lot cleaner and better now.  Did all
48  * this to get the new UI_DOT_SLIDER to work properly, which the old code
49  * wasn't flexible enough to handle.
50  * 
51  * 12    8/24/97 5:24p Lawrance
52  * improve drawing of buttons 
53  * 
54  * 11    6/12/97 12:39p John
55  * made ui use freespace colors
56  * 
57  * 10    6/11/97 1:13p John
58  * Started fixing all the text colors in the game.
59  * 
60  * 9     5/26/97 10:26a Lawrance
61  * get slider control working 100%
62  * 
63  * 8     5/22/97 5:36p Lawrance
64  * allowing custom art for scrollbars
65  * 
66  * 7     1/28/97 4:58p Lawrance
67  * allowing hidden UI components
68  * 
69  * 6     12/04/96 3:00p John
70  * Added code to allow adjusting of HUD colors and saved it to the player
71  * config file.
72  * 
73  * 5     12/03/96 11:29a John
74  * Made scroll buttons on listbox scroll once, then delay, then repeat
75  * when the buttons are held down.
76  * 
77  * 4     12/02/96 2:50p John
78  * Made list box not scroll instantly.
79  * 
80  * 3     12/02/96 2:17p John
81  * Made right button drag UI gadgets around and
82  * Ctrl+Shift+Alt+F12 dumps out where they are.
83  * 
84  * 2     11/15/96 11:43a John
85  * 
86  * 1     11/14/96 6:55p John
87  *
88  * $NoKeywords: $
89  */
90
91 #include "uidefs.h"
92 #include "ui.h"
93 #include "timer.h"
94 #include "alphacolors.h"
95 #include "font.h"
96
97
98 // --------------------------------------------------------------------
99 // UI_SCROLLBAR::link_hotspot
100 //
101 //
102 void UI_SCROLLBAR::link_hotspot(int up_button_num, int down_button_num)
103 {
104         up_button.link_hotspot(up_button_num);
105         down_button.link_hotspot(down_button_num);
106 }
107
108 // --------------------------------------------------------------------
109 // UI_SCROLLBAR::set_bmaps
110 //
111 // Call the UI_GADGET::set_bmaps() function for the child components
112 // of a scroll bar (the up and down button).  Set up the bmaps for the
113 // line itself.
114 //
115 // We also need to get the dimensions of the bitmap button so we can update
116 // the dimensions of the scrollbar.
117 //
118 // returns:             -1 ==> error
119 //                                       0 ==> success
120 //
121 int UI_SCROLLBAR::set_bmaps(const char *up_button_fname, const char *down_button_fname, const char *line_fname)
122 {
123         int bx, by, bh, bw;
124
125         up_button.set_bmaps(up_button_fname);
126         down_button.set_bmaps(down_button_fname);
127         up_button.get_dimensions(&bx, &by, &bw, &bh);
128         
129         // set the bitmaps for the rectangle that is the scrollbar itself
130         ((UI_GADGET*)this)->set_bmaps(line_fname);
131         uses_bmaps = 1;
132
133         update_dimensions(x,y+bw,bw,h-bw*2);
134
135         return 0;
136 }
137
138 void UI_SCROLLBAR::hide(int n)
139 {
140         hidden = 1;
141         up_button.hide();
142         down_button.hide();
143 }
144
145 void UI_SCROLLBAR::unhide()
146 {
147         hidden = 0;
148         up_button.unhide();
149         down_button.unhide();
150 }
151
152 int UI_SCROLLBAR::get_hidden()
153 {
154         return hidden;
155 }
156
157 void UI_SCROLLBAR::create(UI_WINDOW *wnd, int _x, int _y, int _h, int _start, int _stop, int _position, int _window_size)
158 {
159         const char *up = "^";
160         const char *down = "v";
161         int bw = 20;
162
163         base_create( wnd, UI_KIND_SCROLLBAR, _x, _y + bw, bw, _h - bw * 2 );
164
165         up_button.create( wnd, up, _x, _y, bw, bw, 1 );
166         up_button.set_parent(this);
167         up_button.set_hotkey_if_focus(SDLK_UP);
168
169         down_button.create( wnd, down, _x, _y + _h - bw, bw, bw, 1 );
170         down_button.set_parent(this);
171         down_button.set_hotkey_if_focus(SDLK_DOWN);
172
173         horz = 0;
174         start = _start;
175         stop = _stop;
176         position = _position;
177         window_size = _window_size;
178         bar_length = h;
179         bar_position =  0;
180
181         SDL_assert( stop >= 0 );
182
183         if (stop != start)
184                 bar_size = (window_size * h) / (stop - start + window_size + 1);
185         else
186                 bar_size = h;
187
188         if (bar_size < 7)
189                 bar_size = 7;
190
191         bar_position = position - start;
192         bar_position *= h - bar_size;
193         bar_position /= stop - start;
194
195         dragging = 0;
196         last_scrolled = 0;
197         moved = 1;
198 };
199
200 void UI_SCROLLBAR::draw()
201 {
202         UI_GADGET::draw();
203
204         if (uses_bmaps) {
205                 gr_reset_clip();
206                 if (disabled_flag) {
207                         if ( bmap_ids[SB_DISABLED] != -1 ) {
208                                 gr_set_bitmap(bmap_ids[SB_DISABLED], GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
209                                 gr_bitmap(x,y);
210                         }
211
212                 } else {
213                         if ( bmap_ids[SB_NORMAL] != -1 ) {
214                                 gr_set_bitmap(bmap_ids[SB_NORMAL], GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
215                                 gr_bitmap(x,y);
216                         }
217                 }
218
219                 gr_set_clip( x, y, w, h );
220                 ui_draw_box_out( 0, bar_position, w - 1, bar_position + bar_size - 1 );
221
222         } else {
223                 gr_set_font(my_wnd->f_id);
224                 gr_set_clip( x, y, w, h );
225
226                 if (my_wnd->selected_gadget == this)
227                         gr_set_color_fast(&CBRIGHT_GREEN);
228                 else
229                         gr_set_color_fast(&CGRAY);
230
231         /*
232                 ui_rect( 0, 0, w-1, bar_position-1 );
233                 ui_rect( 0, bar_position+bar_size, w-1, h-1);
234         */
235                 ui_rect( 0, 0, w-1, h-1 );
236                 ui_draw_box_out( 0, bar_position, w - 1, bar_position + bar_size - 1 );
237         }
238 }
239
240 void UI_SCROLLBAR::process(int focus)
241 {
242         int OnMe, OnSlider;
243         int op;
244
245         moved = 0;
246         if (disabled_flag) {
247                 return;
248         }
249
250         if (my_wnd->selected_gadget == this)
251                 focus = 1;
252
253         up_button.process(focus);
254         down_button.process(focus);
255
256         if (start == stop) {
257                 position = 0;
258                 bar_position = 0;
259                 return;
260         }
261
262         op = position;
263
264         if (up_button.pressed()) {
265                 position--;
266                 if (position < start)
267                         position = start;
268
269                 bar_position = position - start;
270                 bar_position *= h - bar_size;
271                 bar_position /= stop - start;
272                 set_focus();
273         }
274 /*
275         if ( (up_button.position != 0) || (focus && key_pressed(SDLK_UP)) ) {
276                 if ( (timer_get_milliseconds() > last_scrolled + 50) || up_button.just_pressed() ) {
277                         if ( up_button.just_pressed() ) {
278                                 last_scrolled = timer_get_milliseconds() + 300;
279                         } else
280                                 last_scrolled = timer_get_milliseconds();
281
282                         position--;
283                         if (position < start)
284                                 position = start;
285
286                         bar_position = position - start;
287                         bar_position *= h - bar_size;
288                         bar_position /= stop - start;
289                 }
290         }*/
291
292         if (down_button.pressed()) {
293                 position++;
294                 if (position > stop)
295                         position = stop;
296
297                 bar_position = position - start;
298                 bar_position *= h - bar_size;
299                 bar_position /= stop - start;
300                 set_focus();
301         }
302
303 /*      if ( down_button.position || (keyfocus && key_pressed(SDLK_DOWN)) ) {
304                 if ( (timer_get_milliseconds() > last_scrolled + 50) || down_button.just_pressed() ) {
305                         if ( down_button.just_pressed() )
306                                 last_scrolled = timer_get_milliseconds() + 300;
307                         else
308                                 last_scrolled = timer_get_milliseconds();
309
310                         position++;
311                         if (position > stop )
312                                 position = stop;
313
314                         bar_position = position-start;
315                         bar_position *= h-bar_size;
316                         bar_position /= (stop-start);
317                 }
318         }*/
319
320         OnMe = is_mouse_on();
321
322         if (!B1_PRESSED)
323                 dragging = 0;
324
325         OnSlider = 0;
326         if ( (ui_mouse.y >= bar_position + y) && (ui_mouse.y < bar_position + y + bar_size) && OnMe )
327                 OnSlider = 1;
328
329         if (B1_JUST_PRESSED && OnSlider) {
330                 dragging = 1;
331                 drag_x = ui_mouse.x;
332                 drag_y = ui_mouse.y;
333                 drag_starting = bar_position;
334                 set_focus();
335         }
336
337         if ( B1_PRESSED && OnMe && !OnSlider && (timer_get_milliseconds() > last_scrolled + 1000 / (18*4)) ) {
338                 last_scrolled = timer_get_milliseconds();
339
340                 if ( ui_mouse.y < bar_position+y )      {
341                         // Page Up
342                         position -= window_size;
343                         if (position < start)
344                                 position = start;
345
346                 } else {
347                         // Page Down
348                         position += window_size;
349                         if (position > stop)
350                                 position = stop;
351                 }
352
353                 bar_position = position - start;
354                 bar_position *= h - bar_size;
355                 bar_position /= stop - start;
356                 set_focus();
357         }
358
359         if (B1_PRESSED && dragging) {
360                 bar_position = drag_starting + ui_mouse.y - drag_y;
361
362                 if (bar_position < 0) {
363                         bar_position = 0;
364                 }
365
366                 if (bar_position > h - bar_size) {
367                         bar_position = h - bar_size;
368                 }
369
370                 position = bar_position;
371                 position *= stop - start;
372                 position /= h - bar_size;
373                 position += start;
374
375                 if (position > stop)
376                         position = stop;
377
378                 if (position < start)
379                         position = start;
380
381                 set_focus();
382         }
383
384         if (op != position)
385                 moved = 1;
386         else
387                 moved = 0;
388 }
389
390 int UI_SCROLLBAR::getpos()
391 {
392         return position;
393 }
394
395 int UI_SCROLLBAR::changed()
396 {
397         return moved;
398 }
399