2 * Copyright (C) Volition, Inc. 1999. All rights reserved.
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
10 * $Logfile: /Freespace2/code/Ui/slider.cpp $
15 * C++ file for controlling and displaying a horizontal slider
18 * Revision 1.3 2002/06/09 04:41:29 relnev
19 * added copyright header
21 * Revision 1.2 2002/05/07 03:16:53 theoddone33
22 * The Great Newline Fix
24 * Revision 1.1.1.1 2002/05/03 03:28:11 root
28 * 8 8/10/99 6:54p Dave
29 * Mad optimizations. Added paging to the nebula effect.
31 * 7 8/02/99 6:04p Jefff
32 * use_hack_to_get_around_stupid_problem_flag extended to sliders
34 * 6 5/03/99 8:33p Dave
35 * New version of multi host options screen.
37 * 5 2/11/99 3:08p Dave
38 * PXO refresh button. Very preliminary squad war support.
40 * 4 12/02/98 5:47p Dave
41 * Put in interface xstr code. Converted barracks screen to new format.
43 * 3 10/13/98 9:29a Dave
44 * Started neatening up freespace.h. Many variables renamed and
45 * reorganized. Added AlphaColors.[h,cpp]
47 * 2 10/07/98 10:54a Dave
50 * 1 10/07/98 10:51a Dave
52 * 12 4/17/98 10:34a Hoffoss
53 * Fixed dot slider positionings.
55 * 11 3/22/98 10:50p Lawrance
56 * Allow sliders to not have end-buttons.
58 * 10 2/03/98 4:21p Hoffoss
59 * Made UI controls draw white text when disabled.
61 * 9 1/30/98 11:59a Hoffoss
62 * changed offset of slider child button.
64 * 8 1/27/98 7:02p Lawrance
65 * Don't play the "mouse over" sound for the volume circles.
67 * 7 1/15/98 12:00p Hoffoss
68 * Embelished file with nifty comments.
70 * 6 1/14/98 6:44p Hoffoss
71 * Massive changes to UI code. A lot cleaner and better now. Did all
72 * this to get the new UI_DOT_SLIDER to work properly, which the old code
73 * wasn't flexible enough to handle.
75 * 5 8/24/97 5:25p Lawrance
76 * improve drawing of buttons
78 * 4 6/12/97 12:39p John
79 * made ui use freespace colors
81 * 3 6/11/97 1:13p John
82 * Started fixing all the text colors in the game.
84 * 2 5/26/97 10:26a Lawrance
85 * get slider control working 100%
95 #include "missionscreencommon.h"
98 #include "alphacolors.h"
100 /// DOT_SLIDER class down here
101 void UI_DOT_SLIDER_NEW::create(UI_WINDOW *wnd, int _x, int _y, int _num_pos, char *bm_slider, int slider_mask,
102 char *bm_left, int left_mask, int left_x, int left_y,
103 char *bm_right, int right_mask, int right_x, int right_y,
106 // no end buttons yet
109 // if any of the left/right arrow information is specified, make sure its _all_ specified
110 if((bm_left != NULL) || (left_mask != -1) || (bm_right != NULL) || (right_mask != -1)){
111 Assert((bm_left != NULL) && (left_mask >= 0) && (bm_right != NULL) && (right_mask >= 0));
112 if((bm_left == NULL) || (left_mask < 0) || (bm_right == NULL) || (right_mask < 0)){
116 // now we know we have end buttons
122 base_create(wnd, UI_KIND_DOT_SLIDER_NEW, _x, _y, 0, 20);
124 dot_width = _dot_width;
126 // set bitmaps for the slider itself
127 button.create( wnd, "", _x, _y, 0, 0, 0, 1 );
128 button.set_parent(this);
129 button.link_hotspot(slider_mask);
130 button.set_bmaps(bm_slider, num_pos, 0);
133 // maybe setup buttons for the arrows
134 if ( has_end_buttons ) {
135 // Second button is the up (increase) button
136 up_button.create( wnd, "", right_x, right_y, 0, 0, 1, 1 );
137 up_button.set_parent(this);
138 up_button.set_highlight_action(common_play_highlight_sound);
139 up_button.link_hotspot(right_mask);
140 up_button.set_bmaps(bm_right);
142 // Third button is the down (decrease) button
143 down_button.create( wnd, "", left_x, left_y, 0, 0, 1, 1 );
144 down_button.set_parent(this);
145 down_button.set_highlight_action(common_play_highlight_sound);
146 down_button.link_hotspot(left_mask);
147 down_button.set_bmaps(bm_left);
151 void UI_DOT_SLIDER_NEW::draw()
154 if ( has_end_buttons ) {
159 // draw the proper dot
160 Assert((pos >= 0) && (pos <= num_pos));
162 // for position -1, we don't draw (no dots)
165 button.draw_forced(pos);
170 void UI_DOT_SLIDER_NEW::process(int focus)
173 if (!hidden && !my_wnd->use_hack_to_get_around_stupid_problem_flag) {
174 if (button.is_mouse_on() && B1_JUST_PRESSED) {
175 gamesnd_play_iface(SND_GENERAL_FAIL);
176 } else if (has_end_buttons && (up_button.is_mouse_on() || down_button.is_mouse_on())) {
177 gamesnd_play_iface(SND_GENERAL_FAIL);
181 if ( (hotkey >= 0) && (my_wnd->keypress == hotkey) ){
182 gamesnd_play_iface(SND_GENERAL_FAIL);
189 // check focus and derived focus with one variable
190 if (my_wnd->selected_gadget == this){
194 // first check the dot area
195 button.process(focus);
196 if (button.button_down() || button.pressed() || mouse_captured()) {
197 capture_mouse(); // while we are changing level, ignore all other buttons
199 pos = (ui_mouse.x - x) / dot_width;
205 // if we have 10 positions, 0 - 9 are valid
206 if ( pos >= num_pos ) {
213 if ( has_end_buttons ) {
214 up_button.process(focus);
215 if (up_button.pressed()) {
216 if (pos < num_pos-1){
219 gamesnd_play_iface(SND_GENERAL_FAIL);
223 down_button.process(focus);
224 if (down_button.pressed()) {
228 gamesnd_play_iface(SND_GENERAL_FAIL);
235 // OLD DOT SLIDER - TO BE PHASED OUT. IF YOU NEED TO USE A UI_DOT_SLIDER, use a UI_DOT_SLIDER_NEW -------------------
238 /// DOT_SLIDER class down here
239 void UI_DOT_SLIDER::create(UI_WINDOW *wnd, int _x, int _y, char *bm, int id, int end_buttons, int _num_pos)
241 char filename[MAX_PATH_LEN];
242 int bx, by, bw, hotspot;
244 has_end_buttons = end_buttons;
246 if ( has_end_buttons ) {
260 sprintf(filename, "%s%0.2d", bm, hotspot);
261 first_frame = bm_load_animation(filename, &total_frames);
262 if (first_frame < 0) {
263 Error(LOCATION, "Could not load %s.ani\n", filename);
269 base_create(wnd, UI_KIND_DOT_SLIDER, bx, by, bw, 20);
272 // A DOT_SLIDER has up to 3 child buttons..
276 // First button is the region with the dots
277 button.create( wnd, "", bx, by, bw, 20, 0, 1 );
278 button.set_parent(this);
279 button.link_hotspot(hotspot);
282 if ( has_end_buttons ) {
283 // Second button is the up (increase) button
284 sprintf(filename, "%s%0.2d", bm, id + 2);
285 up_button.create( wnd, "", _x + 216, _y, 22, 24, 1, 1 );
286 up_button.set_parent(this);
287 up_button.set_highlight_action(common_play_highlight_sound);
288 up_button.set_bmaps(filename);
289 up_button.link_hotspot(id + 2);
291 // Third button is the down (decrease) button
292 sprintf(filename, "%s%0.2d", bm, id);
293 down_button.create( wnd, "", _x, _y, 22, 24, 1, 1 );
294 down_button.set_parent(this);
295 down_button.set_highlight_action(common_play_highlight_sound);
296 down_button.set_bmaps(filename);
297 down_button.link_hotspot(id);
301 void UI_DOT_SLIDER::destroy()
305 // release ani frames for the dots.
306 for (i=0; i<total_frames; i++){
307 bm_release(first_frame + i);
310 UI_GADGET::destroy();
313 void UI_DOT_SLIDER::draw()
315 if ( has_end_buttons ) {
319 Assert((pos >= 0) && (pos <= num_pos));
320 gr_set_bitmap(first_frame + pos); // draw the dot level
324 void UI_DOT_SLIDER::process(int focus)
329 // check focus and derived focus with one variable
330 if (my_wnd->selected_gadget == this)
333 // first check the dot area
334 button.process(focus);
335 if (button.button_down() || button.pressed() || mouse_captured()) {
336 capture_mouse(); // while we are changing level, ignore all other buttons
338 if ( has_end_buttons ) {
339 pos = (ui_mouse.x - x + 17) / 19;
341 pos = (ui_mouse.x - x) / 19;
348 if ( pos > num_pos ){
355 if ( has_end_buttons ) {
356 up_button.process(focus);
357 if (up_button.pressed()) {
361 gamesnd_play_iface(SND_GENERAL_FAIL);
365 down_button.process(focus);
366 if (down_button.pressed()) {
370 gamesnd_play_iface(SND_GENERAL_FAIL);
377 // --------------------------------------------------------------------
378 // UI_SLIDER::link_hotspot
381 void UI_SLIDER::link_hotspot(int left_button_num, int right_button_num)
383 left_button.link_hotspot(left_button_num);
384 right_button.link_hotspot(right_button_num);
387 // --------------------------------------------------------------------
388 // UI_SLIDER::set_bmaps
390 // Call the UI_GADGET::set_bmaps() function for the child components
391 // of a scroll bar (the up and down button). Set up the bmaps for the
394 // We also need to get the dimensions of the bitmap button so we can update
395 // the dimensions of the scrollbar.
397 // returns: -1 ==> error
400 int UI_SLIDER::set_bmaps(char *left_button_fname, char *right_button_fname, char *bar_fname, char *marker_fname)
404 left_button.set_bmaps(left_button_fname);
405 right_button.set_bmaps(right_button_fname);
407 // set the bitmaps for the rectangle that is the scrollbar itself
408 ((UI_GADGET*)this)->set_bmaps(bar_fname);
409 ((UI_GADGET*)this)->set_bmaps(marker_fname,2); // skip the first two bitmaps
411 bm_get_info( bmap_ids[SLIDER_MARKER_NORMAL], &m_w, &m_h, NULL );
412 // force the slider dimensions based on size of marker bitmap
413 w = n_positions * m_w;
416 pixel_range = w-marker_w;
417 increment = pixel_range / n_positions;
419 right_button.update_dimensions(x+w, y, -1, -1);
426 void UI_SLIDER::hide()
433 void UI_SLIDER::unhide()
436 left_button.unhide();
437 right_button.unhide();
440 int UI_SLIDER::get_hidden()
445 void UI_SLIDER::create(UI_WINDOW *wnd, int _x, int _y, int _w, int _h, float _start, float _stop, float _current, int _n_positions )
451 real_w = _n_positions*bw;
452 base_create( wnd, UI_KIND_SLIDER, _x, _y, real_w, _h);
454 left_button.create( wnd, up, _x-bw, _y, _h, _h );
455 left_button.set_parent(this);
457 right_button.create( wnd, down, _x+real_w, _y, bh, bh );
458 right_button.set_parent(this);
465 Assert( _current >= _start );
466 Assert( _current <= _stop );
469 n_positions = _n_positions;
478 pixel_range = w-marker_w;
479 marker_x = x + fl2i( ( (current - start)/(stop-start) * pixel_range ) );
480 increment = pixel_range / n_positions;
481 Assert(increment >= 1);
485 void UI_SLIDER::draw()
489 if ( disabled_flag ) {
490 if ( bmap_ids[SLIDER_BAR_DISABLED] != -1 ) {
491 gr_set_bitmap(bmap_ids[SLIDER_BAR_DISABLED]);
495 if ( bmap_ids[SLIDER_MARKER_DISABLED] != -1 ) {
496 gr_set_bitmap(bmap_ids[SLIDER_MARKER_DISABLED]);
497 gr_bitmap(marker_x,marker_y);
502 if ( bmap_ids[SLIDER_BAR_NORMAL] != -1 ) {
503 gr_set_bitmap(bmap_ids[SLIDER_BAR_NORMAL]);
507 if ( bmap_ids[SLIDER_MARKER_NORMAL] != -1 ) {
508 gr_set_bitmap(bmap_ids[SLIDER_MARKER_NORMAL]);
509 gr_bitmap(marker_x,marker_y);
514 gr_set_font(my_wnd->f_id);
515 gr_set_clip( x, y, w, h );
517 if (my_wnd->selected_gadget == this)
518 gr_set_color_fast( &CBRIGHT_GREEN );
520 gr_set_color_fast( &CGRAY );
522 ui_rect( 0, 0, w-1, h-1 );
524 gr_set_clip( marker_x, marker_y, w, h );
525 ui_draw_box_out(0, 0, marker_w, marker_h);
529 void UI_SLIDER::process(int focus)
531 int OnMe, OnMarker, keyfocus;
540 if (my_wnd->selected_gadget == this)
543 left_button.process(focus);
544 right_button.process(focus);
555 oldpos = fake_position;
558 OnMe = is_mouse_on();
560 if ( ui_mouse.x >= (marker_x ) && ui_mouse.x <= (marker_x+marker_w) ) {
571 if ( (left_button.position!=0) || (keyfocus && keyd_pressed[KEY_LEFT]) || ( OnMe && B1_PRESSED && ui_mouse.x < marker_x) || (mouse_locked && ui_mouse.x < marker_x ) ) {
572 if ( (timer_get_milliseconds() > last_scrolled+50) || left_button.just_pressed() || B1_JUST_PRESSED || mouse_locked || my_wnd->keypress == KEY_LEFT) {
573 if ( left_button.just_pressed() || B1_JUST_PRESSED || mouse_locked || my_wnd->keypress == KEY_LEFT ) {
574 last_scrolled = timer_get_milliseconds() + 300;
576 last_scrolled = timer_get_milliseconds();
577 marker_x -= increment;
583 if ( (right_button.position!=0) || (keyfocus && keyd_pressed[KEY_RIGHT]) || ( OnMe && B1_PRESSED && ui_mouse.x > (marker_x+marker_w)) || (mouse_locked && ui_mouse.x > marker_x+marker_w) ) {
584 if ( (timer_get_milliseconds() > last_scrolled+50) || right_button.just_pressed() || B1_JUST_PRESSED || mouse_locked || my_wnd->keypress == KEY_RIGHT) {
585 if ( right_button.just_pressed() || B1_JUST_PRESSED || mouse_locked || my_wnd->keypress == KEY_RIGHT)
586 last_scrolled = timer_get_milliseconds() + 300;
588 last_scrolled = timer_get_milliseconds();
589 marker_x += increment;
590 if (marker_x > (x+n_positions*increment) )
591 marker_x = x+n_positions*increment;
595 percent = i2fl(marker_x - x)/i2fl(pixel_range);
596 current = percent * (stop - start);
599 int UI_SLIDER::getpos()
604 float UI_SLIDER::getcurrent()
609 int UI_SLIDER::changed()