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/SLIDER2.cpp $
15 * Implements UI_SLIDER2 control
18 * Revision 1.4 2004/09/20 01:31:45 theoddone33
21 * Revision 1.3 2002/06/09 04:41:29 relnev
22 * added copyright header
24 * Revision 1.2 2002/05/07 03:16:53 theoddone33
25 * The Great Newline Fix
27 * Revision 1.1.1.1 2002/05/03 03:28:11 root
31 * 9 8/16/99 4:06p Dave
32 * Big honking checkin.
34 * 8 8/11/99 12:18p Jefff
35 * added option to slider2 class to not force slider reset on
38 * 7 8/10/99 6:54p Dave
39 * Mad optimizations. Added paging to the nebula effect.
41 * 6 5/04/99 5:20p Dave
42 * Fixed up multiplayer join screen and host options screen. Should both
45 * 5 5/03/99 11:04p Dave
46 * Most of the way done with the multi join screen.
48 * 4 4/29/99 2:15p Neilk
49 * fixed slider so there is an extra callback for mouse locks
51 * 3 4/26/99 5:05p Neilk
52 * removed some excess debug output
54 * 2 4/16/99 5:22p Neilk
55 * First implementation of UI_SLIDER2
62 #include "freespace.h"
66 // captureCallback is called when an item is "selected" by mouse release. That is, the user has clicked, dragged and _released_.
67 // the callback is called when the scrollbar has been released
68 void UI_SLIDER2::create(UI_WINDOW *wnd, int _x, int _y, int _w, int _h, int _numberItems, const char *_bitmapSliderControl, void (* _upCallback)(), void (*_downCallback)(),
69 void (* _captureCallback)()) {
71 int buttonHeight, buttonWidth;
73 base_create( wnd, UI_KIND_SLIDER2, _x, _y, _w, _h );
75 SDL_assert(_upCallback != NULL);
76 SDL_assert(_downCallback != NULL);
78 upCallback = _upCallback;
79 downCallback = _downCallback;
81 captureCallback = _captureCallback;
83 SDL_assert(_bitmapSliderControl > 0);
88 set_bmaps(_bitmapSliderControl, 3, 0);
90 // determine possible positions
91 bm_get_info(bmap_ids[S2_NORMAL],&buttonWidth, &buttonHeight, NULL, NULL, NULL);
92 slider_w = buttonWidth;
93 slider_h = buttonHeight;
94 SDL_assert(buttonHeight > 5);
95 slider_half_h = (int)(buttonHeight / 2);
96 numberPositions = _h - buttonHeight;
98 SDL_assert(numberPositions >= 0);
102 numberItems = _numberItems;
103 if (numberItems <= 0) {
107 slider_mode = S2M_DEFAULT;
110 void UI_SLIDER2::draw() {
111 SDL_assert((currentPosition >= 0) && (currentPosition <= numberPositions));
112 if (uses_bmaps & !disabled_flag) {
114 switch (slider_mode) {
116 gr_set_bitmap(bmap_ids[S2_HIGHLIGHT], GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1); // draw slider level
119 gr_set_bitmap(bmap_ids[S2_PRESSED], GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
123 gr_set_bitmap(bmap_ids[S2_NORMAL], GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1); // draw slider level
126 gr_bitmap(x, y+currentPosition);
130 void UI_SLIDER2::process(int focus)
132 int OnMe, mouse_lock_move;
138 OnMe = is_mouse_on();
140 // are we on the button?
141 if ( (ui_mouse.y >= (y+currentPosition)) && (ui_mouse.y <= (y+currentPosition+slider_h)) ) {
142 slider_mode = S2M_ON_ME;
148 slider_mode = S2M_DEFAULT;
151 if (mouse_locked == 1)
152 if (captureCallback != NULL) {
154 mprintf(("Called captureCallback()!\n"));
159 if (!OnMe && !mouse_locked)
162 // could we possibly be moving up?
163 if ((OnMe && B1_PRESSED && ui_mouse.y < (currentPosition+y+slider_half_h-1)) || (mouse_locked && (ui_mouse.y < (currentPosition+y+slider_half_h-1))) ) {
164 // make sure we wait at least 50 ms between events unless mouse locked
165 if ( (timer_get_milliseconds() > last_scrolled+50) || B1_JUST_PRESSED || mouse_locked ) {
166 last_scrolled = timer_get_milliseconds();
168 if (currentItem > 0) {
170 if (upCallback != NULL) {
172 if (captureCallback != NULL)
176 currentPosition = fl2i((((float)currentItem/(float)numberItems) * (float)numberPositions)-.49);
178 mouse_lock_move = fl2i( ((((float)ui_mouse.y - (float)y - (float)slider_half_h)/(float)numberPositions) * (float)numberItems) -.49);
179 mouse_lock_move = currentItem - mouse_lock_move;
180 if (mouse_lock_move > 0) {
181 while (mouse_lock_move > 0) {
182 if (currentItem > 0) {
184 if (upCallback != NULL)
190 // currentPosition = ui_mouse.y - y - slider_half_h;
191 currentPosition = fl2i((((float)currentItem/(float)numberItems) * (float)numberPositions)-.49);
193 if (currentPosition < 0)
195 if (currentPosition > numberPositions)
196 currentPosition = numberPositions;
197 slider_mode = S2M_MOVING;
201 if ( ( OnMe && B1_PRESSED && ui_mouse.y > (currentPosition+y+slider_half_h+1)) || (mouse_locked && (ui_mouse.y > (currentPosition+y+slider_half_h+1))) ) {
202 // make sure we wait at least 50 ms between events unless mouse locked
203 if ( (timer_get_milliseconds() > last_scrolled+50) || B1_JUST_PRESSED || mouse_locked ) {
204 last_scrolled = timer_get_milliseconds();
206 if (currentItem < numberItems) {
208 if (downCallback != NULL) {
210 if (captureCallback != NULL)
214 currentPosition = fl2i((((float)currentItem/(float)numberItems) * (float)numberPositions)-.49);
216 mouse_lock_move = fl2i( ((((float)ui_mouse.y - (float)y - (float)slider_half_h)/(float)numberPositions) * (float)numberItems) -.49);
217 mouse_lock_move -= currentItem;
218 if (mouse_lock_move > 0) {
219 while (mouse_lock_move > 0) {
220 if (currentItem < numberItems) {
222 if (downCallback != NULL)
228 // currentPosition = ui_mouse.y - y - slider_half_h;
229 currentPosition = fl2i((((float)currentItem/(float)numberItems) * (float)numberPositions)-.49);
231 if (currentPosition < 0){
234 if (currentPosition > numberPositions){
235 currentPosition = numberPositions;
237 slider_mode = S2M_MOVING;
241 // if we are centerd on the bitmap and still in mouse lock mode, we need to make sure the MOVING bitmap is still shown
242 // or if mouse is on us and we are pressing the mouse button
243 if (mouse_locked || (OnMe && B1_PRESSED)){
244 slider_mode = S2M_MOVING;
248 void UI_SLIDER2::hide(int n)
253 void UI_SLIDER2::unhide()
258 int UI_SLIDER2::get_hidden()
264 // return number of itmes
265 int UI_SLIDER2::get_numberItems() {
269 // return current position
270 int UI_SLIDER2::get_currentPosition() {
271 return currentPosition;
274 // return current item
275 int UI_SLIDER2::get_currentItem() {
279 // change range. reset back to position 0
280 void UI_SLIDER2::set_numberItems(int _numberItems, int reset) {
281 numberItems = _numberItems;
287 // recalcluate current position
288 currentPosition = fl2i((((float)currentItem/(float)numberItems) * (float)numberPositions)-.49);
289 if (currentPosition < 0){
292 if (currentPosition > numberPositions){
293 currentPosition = numberPositions;
296 if (numberItems <= 0){
303 // force slider to new position manually
304 void UI_SLIDER2::set_currentItem(int _currentItem) {
305 if (_currentItem > numberItems)
308 if (_currentItem == currentItem)
311 if (_currentItem < 0)
314 if (_currentItem > currentItem) {
315 while (currentItem != _currentItem) {
317 if (downCallback != NULL)
320 } else if (_currentItem < currentItem) {
321 while (currentItem != _currentItem) {
323 if (upCallback != NULL)
328 currentPosition = fl2i(((float)currentItem/(float)numberItems) * (float)numberPositions);
331 void UI_SLIDER2::force_currentItem(int _currentItem) {
332 currentItem = _currentItem;
336 currentPosition = fl2i(((float)currentItem/(float)numberItems) * (float)numberPositions);
339 void UI_SLIDER2::forceDown() {
340 if (currentItem < numberItems) {
342 currentPosition = fl2i(((float)currentItem/(float)numberItems) * (float)numberPositions);
346 void UI_SLIDER2::forceUp() {
347 if (currentItem > 0) {
349 currentPosition = fl2i(((float)currentItem/(float)numberItems) * (float)numberPositions);