2 * $Logfile: /Freespace2/code/Ui/SLIDER2.cpp $
7 * Implements UI_SLIDER2 control
10 * Revision 1.1 2002/05/03 03:28:11 root
14 * 9 8/16/99 4:06p Dave
15 * Big honking checkin.
17 * 8 8/11/99 12:18p Jefff
18 * added option to slider2 class to not force slider reset on
21 * 7 8/10/99 6:54p Dave
22 * Mad optimizations. Added paging to the nebula effect.
24 * 6 5/04/99 5:20p Dave
25 * Fixed up multiplayer join screen and host options screen. Should both
28 * 5 5/03/99 11:04p Dave
29 * Most of the way done with the multi join screen.
31 * 4 4/29/99 2:15p Neilk
32 * fixed slider so there is an extra callback for mouse locks
34 * 3 4/26/99 5:05p Neilk
35 * removed some excess debug output
37 * 2 4/16/99 5:22p Neilk
38 * First implementation of UI_SLIDER2
45 #include "freespace.h"
49 // captureCallback is called when an item is "selected" by mouse release. That is, the user has clicked, dragged and _released_.
50 // the callback is called when the scrollbar has been released
51 void UI_SLIDER2::create(UI_WINDOW *wnd, int _x, int _y, int _w, int _h, int _numberItems, char *_bitmapSliderControl, void (* _upCallback)(), void (*_downCallback)(),
52 void (* _captureCallback)()) {
54 int buttonHeight, buttonWidth;
56 base_create( wnd, UI_KIND_SLIDER2, _x, _y, _w, _h );
58 Assert(_upCallback != NULL);
59 Assert(_downCallback != NULL);
61 upCallback = _upCallback;
62 downCallback = _downCallback;
64 captureCallback = _captureCallback;
66 Assert(_bitmapSliderControl > 0);
71 set_bmaps(_bitmapSliderControl, 3, 0);
73 // determine possible positions
74 bm_get_info(bmap_ids[S2_NORMAL],&buttonWidth, &buttonHeight, NULL, NULL, NULL);
75 slider_w = buttonWidth;
76 slider_h = buttonHeight;
77 Assert(buttonHeight > 5);
78 slider_half_h = (int)(buttonHeight / 2);
79 numberPositions = _h - buttonHeight;
81 Assert(numberPositions >= 0);
85 numberItems = _numberItems;
86 if (numberItems <= 0) {
90 slider_mode = S2M_DEFAULT;
93 void UI_SLIDER2::draw() {
94 Assert((currentPosition >= 0) && (currentPosition <= numberPositions));
95 if (uses_bmaps & !disabled_flag) {
97 switch (slider_mode) {
99 gr_set_bitmap(bmap_ids[S2_HIGHLIGHT]); // draw slider level
102 gr_set_bitmap(bmap_ids[S2_PRESSED]);
106 gr_set_bitmap(bmap_ids[S2_NORMAL]); // draw slider level
109 gr_bitmap(x, y+currentPosition);
113 void UI_SLIDER2::process(int focus)
115 int OnMe, keyfocus, mouse_lock_move;
122 if (my_wnd->selected_gadget == this){
126 OnMe = is_mouse_on();
128 // are we on the button?
129 if ( (ui_mouse.y >= (y+currentPosition)) && (ui_mouse.y <= (y+currentPosition+slider_h)) ) {
130 slider_mode = S2M_ON_ME;
136 slider_mode = S2M_DEFAULT;
139 if (mouse_locked == 1)
140 if (captureCallback != NULL) {
142 mprintf(("Called captureCallback()!\n"));
147 if (!OnMe && !mouse_locked)
150 // could we possibly be moving up?
151 if ((OnMe && B1_PRESSED && ui_mouse.y < (currentPosition+y+slider_half_h-1)) || (mouse_locked && (ui_mouse.y < (currentPosition+y+slider_half_h-1))) ) {
152 // make sure we wait at least 50 ms between events unless mouse locked
153 if ( (timer_get_milliseconds() > last_scrolled+50) || B1_JUST_PRESSED || mouse_locked ) {
154 last_scrolled = timer_get_milliseconds();
156 if (currentItem > 0) {
158 if (upCallback != NULL) {
160 if (captureCallback != NULL)
164 currentPosition = fl2i((((float)currentItem/(float)numberItems) * (float)numberPositions)-.49);
166 mouse_lock_move = fl2i( ((((float)ui_mouse.y - (float)y - (float)slider_half_h)/(float)numberPositions) * (float)numberItems) -.49);
167 mouse_lock_move = currentItem - mouse_lock_move;
168 if (mouse_lock_move > 0) {
169 while (mouse_lock_move > 0) {
170 if (currentItem > 0) {
172 if (upCallback != NULL)
178 // currentPosition = ui_mouse.y - y - slider_half_h;
179 currentPosition = fl2i((((float)currentItem/(float)numberItems) * (float)numberPositions)-.49);
181 if (currentPosition < 0)
183 if (currentPosition > numberPositions)
184 currentPosition = numberPositions;
185 slider_mode = S2M_MOVING;
189 if ( ( OnMe && B1_PRESSED && ui_mouse.y > (currentPosition+y+slider_half_h+1)) || (mouse_locked && (ui_mouse.y > (currentPosition+y+slider_half_h+1))) ) {
190 // make sure we wait at least 50 ms between events unless mouse locked
191 if ( (timer_get_milliseconds() > last_scrolled+50) || B1_JUST_PRESSED || mouse_locked ) {
192 last_scrolled = timer_get_milliseconds();
194 if (currentItem < numberItems) {
196 if (downCallback != NULL) {
198 if (captureCallback != NULL)
202 currentPosition = fl2i((((float)currentItem/(float)numberItems) * (float)numberPositions)-.49);
204 mouse_lock_move = fl2i( ((((float)ui_mouse.y - (float)y - (float)slider_half_h)/(float)numberPositions) * (float)numberItems) -.49);
205 mouse_lock_move -= currentItem;
206 if (mouse_lock_move > 0) {
207 while (mouse_lock_move > 0) {
208 if (currentItem < numberItems) {
210 if (downCallback != NULL)
216 // currentPosition = ui_mouse.y - y - slider_half_h;
217 currentPosition = fl2i((((float)currentItem/(float)numberItems) * (float)numberPositions)-.49);
219 if (currentPosition < 0){
222 if (currentPosition > numberPositions){
223 currentPosition = numberPositions;
225 slider_mode = S2M_MOVING;
229 // if we are centerd on the bitmap and still in mouse lock mode, we need to make sure the MOVING bitmap is still shown
230 // or if mouse is on us and we are pressing the mouse button
231 if (mouse_locked || (OnMe && B1_PRESSED)){
232 slider_mode = S2M_MOVING;
236 void UI_SLIDER2::hide()
241 void UI_SLIDER2::unhide()
246 int UI_SLIDER2::get_hidden()
252 // return number of itmes
253 int UI_SLIDER2::get_numberItems() {
257 // return current position
258 int UI_SLIDER2::get_currentPosition() {
259 return currentPosition;
262 // return current item
263 int UI_SLIDER2::get_currentItem() {
267 // change range. reset back to position 0
268 void UI_SLIDER2::set_numberItems(int _numberItems, int reset) {
269 numberItems = _numberItems;
275 // recalcluate current position
276 currentPosition = fl2i((((float)currentItem/(float)numberItems) * (float)numberPositions)-.49);
277 if (currentPosition < 0){
280 if (currentPosition > numberPositions){
281 currentPosition = numberPositions;
284 if (numberItems <= 0){
291 // force slider to new position manually
292 void UI_SLIDER2::set_currentItem(int _currentItem) {
293 if (_currentItem > numberItems)
296 if (_currentItem == currentItem)
299 if (_currentItem < 0)
302 if (_currentItem > currentItem) {
303 while (currentItem != _currentItem) {
305 if (downCallback != NULL)
308 } else if (_currentItem < currentItem) {
309 while (currentItem != _currentItem) {
311 if (upCallback != NULL)
316 currentPosition = fl2i(((float)currentItem/(float)numberItems) * (float)numberPositions);
319 void UI_SLIDER2::force_currentItem(int _currentItem) {
320 currentItem = _currentItem;
324 currentPosition = fl2i(((float)currentItem/(float)numberItems) * (float)numberPositions);
327 void UI_SLIDER2::forceDown() {
328 if (currentItem < numberItems) {
330 currentPosition = fl2i(((float)currentItem/(float)numberItems) * (float)numberPositions);
334 void UI_SLIDER2::forceUp() {
335 if (currentItem > 0) {
337 currentPosition = fl2i(((float)currentItem/(float)numberItems) * (float)numberPositions);