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.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 * 9 8/16/99 4:06p Dave
29 * Big honking checkin.
31 * 8 8/11/99 12:18p Jefff
32 * added option to slider2 class to not force slider reset on
35 * 7 8/10/99 6:54p Dave
36 * Mad optimizations. Added paging to the nebula effect.
38 * 6 5/04/99 5:20p Dave
39 * Fixed up multiplayer join screen and host options screen. Should both
42 * 5 5/03/99 11:04p Dave
43 * Most of the way done with the multi join screen.
45 * 4 4/29/99 2:15p Neilk
46 * fixed slider so there is an extra callback for mouse locks
48 * 3 4/26/99 5:05p Neilk
49 * removed some excess debug output
51 * 2 4/16/99 5:22p Neilk
52 * First implementation of UI_SLIDER2
59 #include "freespace.h"
63 // captureCallback is called when an item is "selected" by mouse release. That is, the user has clicked, dragged and _released_.
64 // the callback is called when the scrollbar has been released
65 void UI_SLIDER2::create(UI_WINDOW *wnd, int _x, int _y, int _w, int _h, int _numberItems, char *_bitmapSliderControl, void (* _upCallback)(), void (*_downCallback)(),
66 void (* _captureCallback)()) {
68 int buttonHeight, buttonWidth;
70 base_create( wnd, UI_KIND_SLIDER2, _x, _y, _w, _h );
72 Assert(_upCallback != NULL);
73 Assert(_downCallback != NULL);
75 upCallback = _upCallback;
76 downCallback = _downCallback;
78 captureCallback = _captureCallback;
80 Assert(_bitmapSliderControl > 0);
85 set_bmaps(_bitmapSliderControl, 3, 0);
87 // determine possible positions
88 bm_get_info(bmap_ids[S2_NORMAL],&buttonWidth, &buttonHeight, NULL, NULL, NULL);
89 slider_w = buttonWidth;
90 slider_h = buttonHeight;
91 Assert(buttonHeight > 5);
92 slider_half_h = (int)(buttonHeight / 2);
93 numberPositions = _h - buttonHeight;
95 Assert(numberPositions >= 0);
99 numberItems = _numberItems;
100 if (numberItems <= 0) {
104 slider_mode = S2M_DEFAULT;
107 void UI_SLIDER2::draw() {
108 Assert((currentPosition >= 0) && (currentPosition <= numberPositions));
109 if (uses_bmaps & !disabled_flag) {
111 switch (slider_mode) {
113 gr_set_bitmap(bmap_ids[S2_HIGHLIGHT]); // draw slider level
116 gr_set_bitmap(bmap_ids[S2_PRESSED]);
120 gr_set_bitmap(bmap_ids[S2_NORMAL]); // draw slider level
123 gr_bitmap(x, y+currentPosition);
127 void UI_SLIDER2::process(int focus)
129 int OnMe, keyfocus, mouse_lock_move;
136 if (my_wnd->selected_gadget == this){
140 OnMe = is_mouse_on();
142 // are we on the button?
143 if ( (ui_mouse.y >= (y+currentPosition)) && (ui_mouse.y <= (y+currentPosition+slider_h)) ) {
144 slider_mode = S2M_ON_ME;
150 slider_mode = S2M_DEFAULT;
153 if (mouse_locked == 1)
154 if (captureCallback != NULL) {
156 mprintf(("Called captureCallback()!\n"));
161 if (!OnMe && !mouse_locked)
164 // could we possibly be moving up?
165 if ((OnMe && B1_PRESSED && ui_mouse.y < (currentPosition+y+slider_half_h-1)) || (mouse_locked && (ui_mouse.y < (currentPosition+y+slider_half_h-1))) ) {
166 // make sure we wait at least 50 ms between events unless mouse locked
167 if ( (timer_get_milliseconds() > last_scrolled+50) || B1_JUST_PRESSED || mouse_locked ) {
168 last_scrolled = timer_get_milliseconds();
170 if (currentItem > 0) {
172 if (upCallback != NULL) {
174 if (captureCallback != NULL)
178 currentPosition = fl2i((((float)currentItem/(float)numberItems) * (float)numberPositions)-.49);
180 mouse_lock_move = fl2i( ((((float)ui_mouse.y - (float)y - (float)slider_half_h)/(float)numberPositions) * (float)numberItems) -.49);
181 mouse_lock_move = currentItem - mouse_lock_move;
182 if (mouse_lock_move > 0) {
183 while (mouse_lock_move > 0) {
184 if (currentItem > 0) {
186 if (upCallback != NULL)
192 // currentPosition = ui_mouse.y - y - slider_half_h;
193 currentPosition = fl2i((((float)currentItem/(float)numberItems) * (float)numberPositions)-.49);
195 if (currentPosition < 0)
197 if (currentPosition > numberPositions)
198 currentPosition = numberPositions;
199 slider_mode = S2M_MOVING;
203 if ( ( OnMe && B1_PRESSED && ui_mouse.y > (currentPosition+y+slider_half_h+1)) || (mouse_locked && (ui_mouse.y > (currentPosition+y+slider_half_h+1))) ) {
204 // make sure we wait at least 50 ms between events unless mouse locked
205 if ( (timer_get_milliseconds() > last_scrolled+50) || B1_JUST_PRESSED || mouse_locked ) {
206 last_scrolled = timer_get_milliseconds();
208 if (currentItem < numberItems) {
210 if (downCallback != NULL) {
212 if (captureCallback != NULL)
216 currentPosition = fl2i((((float)currentItem/(float)numberItems) * (float)numberPositions)-.49);
218 mouse_lock_move = fl2i( ((((float)ui_mouse.y - (float)y - (float)slider_half_h)/(float)numberPositions) * (float)numberItems) -.49);
219 mouse_lock_move -= currentItem;
220 if (mouse_lock_move > 0) {
221 while (mouse_lock_move > 0) {
222 if (currentItem < numberItems) {
224 if (downCallback != NULL)
230 // currentPosition = ui_mouse.y - y - slider_half_h;
231 currentPosition = fl2i((((float)currentItem/(float)numberItems) * (float)numberPositions)-.49);
233 if (currentPosition < 0){
236 if (currentPosition > numberPositions){
237 currentPosition = numberPositions;
239 slider_mode = S2M_MOVING;
243 // if we are centerd on the bitmap and still in mouse lock mode, we need to make sure the MOVING bitmap is still shown
244 // or if mouse is on us and we are pressing the mouse button
245 if (mouse_locked || (OnMe && B1_PRESSED)){
246 slider_mode = S2M_MOVING;
250 void UI_SLIDER2::hide()
255 void UI_SLIDER2::unhide()
260 int UI_SLIDER2::get_hidden()
266 // return number of itmes
267 int UI_SLIDER2::get_numberItems() {
271 // return current position
272 int UI_SLIDER2::get_currentPosition() {
273 return currentPosition;
276 // return current item
277 int UI_SLIDER2::get_currentItem() {
281 // change range. reset back to position 0
282 void UI_SLIDER2::set_numberItems(int _numberItems, int reset) {
283 numberItems = _numberItems;
289 // recalcluate current position
290 currentPosition = fl2i((((float)currentItem/(float)numberItems) * (float)numberPositions)-.49);
291 if (currentPosition < 0){
294 if (currentPosition > numberPositions){
295 currentPosition = numberPositions;
298 if (numberItems <= 0){
305 // force slider to new position manually
306 void UI_SLIDER2::set_currentItem(int _currentItem) {
307 if (_currentItem > numberItems)
310 if (_currentItem == currentItem)
313 if (_currentItem < 0)
316 if (_currentItem > currentItem) {
317 while (currentItem != _currentItem) {
319 if (downCallback != NULL)
322 } else if (_currentItem < currentItem) {
323 while (currentItem != _currentItem) {
325 if (upCallback != NULL)
330 currentPosition = fl2i(((float)currentItem/(float)numberItems) * (float)numberPositions);
333 void UI_SLIDER2::force_currentItem(int _currentItem) {
334 currentItem = _currentItem;
338 currentPosition = fl2i(((float)currentItem/(float)numberItems) * (float)numberPositions);
341 void UI_SLIDER2::forceDown() {
342 if (currentItem < numberItems) {
344 currentPosition = fl2i(((float)currentItem/(float)numberItems) * (float)numberPositions);
348 void UI_SLIDER2::forceUp() {
349 if (currentItem > 0) {
351 currentPosition = fl2i(((float)currentItem/(float)numberItems) * (float)numberPositions);