2 * $Logfile: /Freespace2/code/Ui/SLIDER2.cpp $
7 * Implements UI_SLIDER2 control
10 * Revision 1.2 2002/05/07 03:16:53 theoddone33
11 * The Great Newline Fix
13 * Revision 1.1.1.1 2002/05/03 03:28:11 root
17 * 9 8/16/99 4:06p Dave
18 * Big honking checkin.
20 * 8 8/11/99 12:18p Jefff
21 * added option to slider2 class to not force slider reset on
24 * 7 8/10/99 6:54p Dave
25 * Mad optimizations. Added paging to the nebula effect.
27 * 6 5/04/99 5:20p Dave
28 * Fixed up multiplayer join screen and host options screen. Should both
31 * 5 5/03/99 11:04p Dave
32 * Most of the way done with the multi join screen.
34 * 4 4/29/99 2:15p Neilk
35 * fixed slider so there is an extra callback for mouse locks
37 * 3 4/26/99 5:05p Neilk
38 * removed some excess debug output
40 * 2 4/16/99 5:22p Neilk
41 * First implementation of UI_SLIDER2
48 #include "freespace.h"
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)()) {
57 int buttonHeight, buttonWidth;
59 base_create( wnd, UI_KIND_SLIDER2, _x, _y, _w, _h );
61 Assert(_upCallback != NULL);
62 Assert(_downCallback != NULL);
64 upCallback = _upCallback;
65 downCallback = _downCallback;
67 captureCallback = _captureCallback;
69 Assert(_bitmapSliderControl > 0);
74 set_bmaps(_bitmapSliderControl, 3, 0);
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;
84 Assert(numberPositions >= 0);
88 numberItems = _numberItems;
89 if (numberItems <= 0) {
93 slider_mode = S2M_DEFAULT;
96 void UI_SLIDER2::draw() {
97 Assert((currentPosition >= 0) && (currentPosition <= numberPositions));
98 if (uses_bmaps & !disabled_flag) {
100 switch (slider_mode) {
102 gr_set_bitmap(bmap_ids[S2_HIGHLIGHT]); // draw slider level
105 gr_set_bitmap(bmap_ids[S2_PRESSED]);
109 gr_set_bitmap(bmap_ids[S2_NORMAL]); // draw slider level
112 gr_bitmap(x, y+currentPosition);
116 void UI_SLIDER2::process(int focus)
118 int OnMe, keyfocus, mouse_lock_move;
125 if (my_wnd->selected_gadget == this){
129 OnMe = is_mouse_on();
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;
139 slider_mode = S2M_DEFAULT;
142 if (mouse_locked == 1)
143 if (captureCallback != NULL) {
145 mprintf(("Called captureCallback()!\n"));
150 if (!OnMe && !mouse_locked)
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();
159 if (currentItem > 0) {
161 if (upCallback != NULL) {
163 if (captureCallback != NULL)
167 currentPosition = fl2i((((float)currentItem/(float)numberItems) * (float)numberPositions)-.49);
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) {
175 if (upCallback != NULL)
181 // currentPosition = ui_mouse.y - y - slider_half_h;
182 currentPosition = fl2i((((float)currentItem/(float)numberItems) * (float)numberPositions)-.49);
184 if (currentPosition < 0)
186 if (currentPosition > numberPositions)
187 currentPosition = numberPositions;
188 slider_mode = S2M_MOVING;
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();
197 if (currentItem < numberItems) {
199 if (downCallback != NULL) {
201 if (captureCallback != NULL)
205 currentPosition = fl2i((((float)currentItem/(float)numberItems) * (float)numberPositions)-.49);
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) {
213 if (downCallback != NULL)
219 // currentPosition = ui_mouse.y - y - slider_half_h;
220 currentPosition = fl2i((((float)currentItem/(float)numberItems) * (float)numberPositions)-.49);
222 if (currentPosition < 0){
225 if (currentPosition > numberPositions){
226 currentPosition = numberPositions;
228 slider_mode = S2M_MOVING;
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;
239 void UI_SLIDER2::hide()
244 void UI_SLIDER2::unhide()
249 int UI_SLIDER2::get_hidden()
255 // return number of itmes
256 int UI_SLIDER2::get_numberItems() {
260 // return current position
261 int UI_SLIDER2::get_currentPosition() {
262 return currentPosition;
265 // return current item
266 int UI_SLIDER2::get_currentItem() {
270 // change range. reset back to position 0
271 void UI_SLIDER2::set_numberItems(int _numberItems, int reset) {
272 numberItems = _numberItems;
278 // recalcluate current position
279 currentPosition = fl2i((((float)currentItem/(float)numberItems) * (float)numberPositions)-.49);
280 if (currentPosition < 0){
283 if (currentPosition > numberPositions){
284 currentPosition = numberPositions;
287 if (numberItems <= 0){
294 // force slider to new position manually
295 void UI_SLIDER2::set_currentItem(int _currentItem) {
296 if (_currentItem > numberItems)
299 if (_currentItem == currentItem)
302 if (_currentItem < 0)
305 if (_currentItem > currentItem) {
306 while (currentItem != _currentItem) {
308 if (downCallback != NULL)
311 } else if (_currentItem < currentItem) {
312 while (currentItem != _currentItem) {
314 if (upCallback != NULL)
319 currentPosition = fl2i(((float)currentItem/(float)numberItems) * (float)numberPositions);
322 void UI_SLIDER2::force_currentItem(int _currentItem) {
323 currentItem = _currentItem;
327 currentPosition = fl2i(((float)currentItem/(float)numberItems) * (float)numberPositions);
330 void UI_SLIDER2::forceDown() {
331 if (currentItem < numberItems) {
333 currentPosition = fl2i(((float)currentItem/(float)numberItems) * (float)numberPositions);
337 void UI_SLIDER2::forceUp() {
338 if (currentItem > 0) {
340 currentPosition = fl2i(((float)currentItem/(float)numberItems) * (float)numberPositions);