// Returns 1 if moving int UI_GADGET::check_move() { #if 0 if ( parent != NULL ) return base_dragging; if ( !base_dragging ) { if ( B2_JUST_PRESSED ) { if ( is_mouse_on() || is_mouse_on_children() ) { start_drag_with_children(); base_drag_x = ui_mouse.x; base_drag_y = ui_mouse.y; return 1; } else { return 0; } } else return 0; } else { drag_with_children(ui_mouse.x - base_drag_x,ui_mouse.y - base_drag_y); nprintf(( "UI", "UI: X=%d, Y=%d, Delta=(%d,%d)\n", x, y, (ui_mouse.x - base_drag_x), (ui_mouse.y - base_drag_y) )); if (B2_RELEASED) { stop_drag_with_children(); } return 1; } #endif return 0; }
// ----------------------------------------------------------------------- // process() // void UI_ICON::process(int focus) { int OnMe; if (disabled_flag) { return; } OnMe = is_mouse_on(); if (!OnMe) { m_flags |= ICON_NOT_HIGHLIGHTED; } else { if (m_flags & ICON_NOT_HIGHLIGHTED) { m_flags |= ICON_JUST_HIGHLIGHTED; // if a callback exists, call it m_flags &= ~ICON_NOT_HIGHLIGHTED; } } }
void UI_CHECKBOX::process(int focus) { int OnMe, oldposition; if (disabled_flag) { position = 0; return; } if (my_wnd->selected_gadget == this) focus = 1; OnMe = is_mouse_on(); oldposition = position; if ( B1_PRESSED && OnMe ) { position = 1; } else { position = 0; } if (my_wnd->keypress == hotkey ) { position = 2; my_wnd->last_keypress = 0; } if ( focus && ((my_wnd->keypress == KEY_SPACEBAR) || (my_wnd->keypress == KEY_ENTER)) ) position = 2; if (focus) if ( (oldposition == 2) && (keyd_pressed[KEY_SPACEBAR] || keyd_pressed[KEY_ENTER]) ) position = 2; pressed_down = 0; if (position == 0) { if ( (oldposition == 1) && OnMe ){ pressed_down = 1; } if ( (oldposition == 2) && focus ){ pressed_down = 1; } } if (pressed_down && user_function ) { user_function(); } if (pressed_down) flag = !flag; }
void UI_BUTTON::maybe_show_custom_cursor() { if (disabled_flag) return; // set the mouseover cursor if (is_mouse_on()) { if ((custom_cursor_bmap >= 0) && (previous_cursor_bmap < 0)) { previous_cursor_bmap = gr_get_cursor_bitmap(); gr_set_cursor_bitmap(custom_cursor_bmap, GR_CURSOR_LOCK); // set and lock } } }
// process() is called to process the button, which amounts to: // If mouse is over button, hilight it // If highlighted and mouse button down, flag button as down // If hotkey pressed, flag button as down // If hotkey_if_focus pressed, and button has focus, flag button as down // Set various BF_JUST_* flags if events changed from last frame // void UI_BUTTON::process(int focus) { int mouse_on_me, old_flags; old_flags = m_flags; frame_reset(); // check mouse over control and handle hilighting state mouse_on_me = is_mouse_on(); // if gadget is disabled, force button up and return if (disabled_flag) { if (old_flags & BF_DOWN){ m_flags |= BF_JUST_RELEASED; } if (!hidden && !my_wnd->use_hack_to_get_around_stupid_problem_flag) { if (mouse_on_me && B1_JUST_PRESSED){ gamesnd_play_iface(SND_GENERAL_FAIL); } if ( (hotkey >= 0) && (my_wnd->keypress == hotkey) ){ gamesnd_play_iface(SND_GENERAL_FAIL); } } // do callback if the button is disabled if (mouse_on_me && B1_JUST_PRESSED){ if (m_disabled_function != NULL) { m_disabled_function(); } } return; } // check focus and derived focus with one variable if (my_wnd->selected_gadget == this) { focus = 1; } // show alternate cursor, perhaps? maybe_show_custom_cursor(); if ( !mouse_on_me ) { next_repeat = 0; } else { m_flags |= BF_HIGHLIGHTED; if ( !(old_flags & BF_HIGHLIGHTED) ) { int do_callback = 1; m_flags |= BF_JUST_HIGHLIGHTED; // if a callback exists, call it if (m_just_highlighted_function) { if ( m_flags & BF_SKIP_FIRST_HIGHLIGHT_CALLBACK ) { if ( first_callback ) { do_callback = 0; } } first_callback = 0; if ( do_callback ) { m_just_highlighted_function(); } } } } // check if mouse is pressed if ( B1_PRESSED && mouse_on_me ) { m_flags |= BF_DOWN; capture_mouse(); } // check if hotkey is down or not if ( (hotkey >= 0) && (my_wnd->keypress == hotkey) ) { m_flags |= BF_DOWN | BF_CLICKED; } // only check for space/enter keystrokes if we are not ignoring the focus (this is the // default behavior) if ( !(m_flags & BF_IGNORE_FOCUS) ) { if ( focus && (hotkey_if_focus >= 0) ) { if (my_wnd->keypress == hotkey_if_focus) m_flags |= BF_DOWN | BF_CLICKED; if ( (hotkey_if_focus == KEY_SPACEBAR) && (my_wnd->keypress == KEY_ENTER) ) m_flags |= BF_DOWN | BF_CLICKED; } } // handler for button not down if ( !(m_flags & BF_DOWN) ) { next_repeat = 0; if ( (old_flags & BF_DOWN) && !(old_flags & BF_CLICKED) ) // check for release of mouse, not hotkey m_flags |= BF_JUST_RELEASED; // non-repeating buttons behave sort of uniquely.. They activate when released over button if (!(m_flags & BF_REPEATS)) { if ( (m_flags & BF_JUST_RELEASED) && (m_flags & BF_HIGHLIGHTED) ) m_flags |= BF_CLICKED; } return; } // check if button just went down this frame if ( !(old_flags & BF_DOWN) ) { m_flags |= BF_JUST_PRESSED; m_press_linger = timestamp(100); if (user_function) user_function(); if (m_flags & BF_REPEATS) { next_repeat = timestamp(B_REPEAT_TIME * 3); m_flags |= BF_CLICKED; } } // check if a repeat event should occur if ( timestamp_elapsed(next_repeat) && (m_flags & BF_REPEATS) ) { next_repeat = timestamp(B_REPEAT_TIME); m_flags |= BF_CLICKED; m_press_linger = timestamp(100); } // check for double click occurance if (B1_DOUBLE_CLICKED && mouse_on_me) { m_flags |= BF_DOUBLE_CLICKED; m_press_linger = timestamp(100); } }
void UI_SCROLLBAR::process(int focus) { int OnMe, OnSlider; int op; moved = 0; if (disabled_flag) { return; } if (my_wnd->selected_gadget == this) focus = 1; up_button.process(focus); down_button.process(focus); if (start == stop) { position = 0; bar_position = 0; return; } op = position; if (up_button.pressed()) { position--; if (position < start) position = start; bar_position = position - start; bar_position *= h - bar_size; bar_position /= stop - start; set_focus(); } if (down_button.pressed()) { position++; if (position > stop) position = stop; bar_position = position - start; bar_position *= h - bar_size; bar_position /= stop - start; set_focus(); } OnMe = is_mouse_on(); if (!B1_PRESSED) dragging = 0; OnSlider = 0; if ( (ui_mouse.y >= bar_position + y) && (ui_mouse.y < bar_position + y + bar_size) && OnMe ) OnSlider = 1; if (B1_JUST_PRESSED && OnSlider) { dragging = 1; drag_x = ui_mouse.x; drag_y = ui_mouse.y; drag_starting = bar_position; set_focus(); } if ( B1_PRESSED && OnMe && !OnSlider && (timer_get_milliseconds() > last_scrolled + 1000 / (18*4)) ) { last_scrolled = timer_get_milliseconds(); if ( ui_mouse.y < bar_position+y ) { // Page Up position -= window_size; if (position < start) position = start; } else { // Page Down position += window_size; if (position > stop) position = stop; } bar_position = position - start; bar_position *= h - bar_size; bar_position /= stop - start; set_focus(); } if (B1_PRESSED && dragging) { bar_position = drag_starting + ui_mouse.y - drag_y; if (bar_position < 0) { bar_position = 0; } if (bar_position > h - bar_size) { bar_position = h - bar_size; } position = bar_position; position *= stop - start; position /= h - bar_size; position += start; if (position > stop) position = stop; if (position < start) position = start; set_focus(); } if (op != position) moved = 1; else moved = 0; }
void UI_INPUTBOX::process(int focus) { int ascii, clear_lastkey, key, key_used, key_check; // check if mouse is pressed if (B1_PRESSED && is_mouse_on()) { set_focus(); } if (disabled_flag) return; if (my_wnd->selected_gadget == this) focus = 1; key_used = 0; changed_flag = 0; oldposition = position; pressed_down = 0; clear_lastkey = (flags & UI_INPUTBOX_FLAG_KEYTHRU) ? 0 : 1; if (focus) { key = my_wnd->keypress; switch (key) { case 0: break; //case KEY_LEFT: case KEY_BACKSP: if (position > 0) position--; text[position] = 0; if (flags & UI_INPUTBOX_FLAG_PASSWD) { passwd_text[position] = 0; } changed_flag = 1; key_used = 1; break; case KEY_ENTER: pressed_down = 1; locked = 0; changed_flag = 1; key_used = 1; break; case KEY_ESC: if (flags & UI_INPUTBOX_FLAG_ESC_CLR){ if (position > 0) { set_text(""); key_used = 1; } else { key_used = 0; clear_lastkey = 0; } } if (flags & UI_INPUTBOX_FLAG_ESC_FOC) { clear_focus(); } break; default: if (!locked) { // MWA -- determine if alt or ctrl held down on this key and don't process if it is. We // need to be able to pass these keys back to the top level. (And anyway -- ctrl-a shouldn't // print out an A in the input window if ( key & (KEY_ALTED | KEY_CTRLED) ) { clear_lastkey = 0; break; } // get an ascii char from the input if possible key_check = keypad_to_ascii(key); if(key_check == -1){ key_check = key_to_ascii(key); } ascii = validate_input(key_check); if ((ascii > 0) && (ascii < 255)) { if (flags & UI_INPUTBOX_FLAG_LETTER_FIRST) { if ((position == 0) && !is_letter((char) ascii)) break; } key_used = 1; if ( position < length ) { text[position] = (char) ascii; text[position + 1] = 0; if (flags & UI_INPUTBOX_FLAG_PASSWD) { passwd_text[position] = (char) INPUTBOX_PASSWD_CHAR; passwd_text[position + 1] = 0; } position++; // check to see if we should limit by pixel width if (pixel_limit > -1) { int _w; if (flags & UI_INPUTBOX_FLAG_PASSWD) { gr_get_string_size(&_w, NULL, passwd_text); } else { gr_get_string_size(&_w, NULL, text); } if (_w > pixel_limit) { position--; locked = 1; text[position] = 0; if (flags & UI_INPUTBOX_FLAG_PASSWD) { passwd_text[position] = 0; } } } } changed_flag = 1; } } break; } if (clear_lastkey || (key_used && (flags & UI_INPUTBOX_FLAG_EAT_USED)) ) my_wnd->last_keypress=0; } }
void UI_LISTBOX::process(int focus) { int OnMe, mitem, oldbarpos, kf = 0; int i, j; selected_item = -1; toggled_item = -1; if (disabled_flag) return; if (my_wnd->selected_gadget == this) focus = 1; if (has_scrollbar) { scrollbar.process(0); if (my_wnd->selected_gadget == &scrollbar) { set_focus(); focus = 1; } } if (num_items < 1) { current_item = -1; first_item = 0; old_current_item = current_item; old_first_item = first_item; // if (my_wnd->selected_gadget == this) { // my_wnd->selected_gadget == get_next(); // } return; } old_current_item = current_item; old_first_item = first_item; OnMe = is_mouse_on(); if (has_scrollbar) { if (scrollbar.moved) { first_item = scrollbar.position; Assert(first_item >= 0); if (current_item<first_item) current_item = first_item; if (current_item > first_item + num_items_displayed - 1) current_item = first_item + num_items_displayed - 1; } } if (!B1_PRESSED) dragging = 0; if (B1_PRESSED && OnMe) { set_focus(); dragging = 1; } if ( key_buffer_count && (timer_get_milliseconds() > last_typed + KEY_BUFFER_TIMEOUT) ) key_buffer_count = 0; if (focus) { if (my_wnd->keypress) { kf = 0; switch (my_wnd->keypress) { case KEY_ENTER: selected_item = current_item; break; case KEY_SPACEBAR: toggled_item = current_item; break; case KEY_UP: current_item--; kf = 1; break; case KEY_DOWN: current_item++; kf = 1; break; case KEY_HOME: current_item = 0; kf = 1; break; case KEY_END: current_item=num_items - 1; kf = 1; break; case KEY_PAGEUP: current_item -= num_items_displayed; kf = 1; break; case KEY_PAGEDOWN: current_item += num_items_displayed; kf = 1; break; default: // enter the key in the key buffer if (my_wnd->keypress == KEY_BACKSP) { key_buffer_count = 0; } else if (key_buffer_count < MAX_KEY_BUFFER) { key_buffer[key_buffer_count++] = (char) my_wnd->keypress; last_typed = timer_get_milliseconds(); } if (!key_buffer_count) break; for (i=0; i<num_items; i++) { char *current_text; current_text = get_string(i); for (j=0; j<key_buffer_count; j++) if ( (current_text[j] != ascii_table[key_buffer[j]]) && (current_text[j] != shifted_ascii_table[key_buffer[j]]) ) break; if (j == key_buffer_count) { set_first_item(i - num_items_displayed / 2); set_current(i); break; } } } } if (kf == 1) { if (current_item < 0) current_item = 0; if (current_item >= num_items) current_item = num_items - 1; if (current_item < first_item) first_item = current_item; if (current_item >= first_item + num_items_displayed) first_item = current_item - num_items_displayed + 1; if (num_items <= num_items_displayed ) { first_item = 0; } else { if (has_scrollbar) { oldbarpos = scrollbar.position; scrollbar.position = first_item; scrollbar.bar_position = scrollbar.position - scrollbar.start; scrollbar.bar_position *= scrollbar.h - scrollbar.bar_size; scrollbar.bar_position /= scrollbar.stop - scrollbar.start; if (scrollbar.bar_position < 0) { scrollbar.bar_position = 0; } if (scrollbar.bar_position > scrollbar.h - scrollbar.bar_size) { scrollbar.bar_position = scrollbar.h - scrollbar.bar_size; } } } } } if (focus) { if (B1_PRESSED && dragging) { if (ui_mouse.y < y ) mitem = -1; else mitem = (ui_mouse.y - y)/textheight; if ( (mitem < 0) && (timer_get_milliseconds() > last_scrolled + 1000 / 18) ) { current_item--; last_scrolled = timer_get_milliseconds(); } if ( (mitem >= num_items_displayed) && (timer_get_milliseconds() > last_scrolled + 1000 / 18) ) { current_item++; last_scrolled = timer_get_milliseconds(); } if ((mitem >= 0) && (mitem<num_items_displayed)) { current_item = mitem + first_item; } if (current_item < 0) current_item = 0; if (current_item >= num_items) current_item = num_items - 1; if (current_item < first_item) first_item = current_item; if (current_item >= first_item + num_items_displayed) first_item = current_item - num_items_displayed + 1; if (num_items <= num_items_displayed) { first_item = 0; } else if (has_scrollbar) { oldbarpos = scrollbar.position; scrollbar.position = first_item; scrollbar.bar_position = scrollbar.position - scrollbar.start; scrollbar.bar_position *= scrollbar.h - scrollbar.bar_size; scrollbar.bar_position /= scrollbar.stop - scrollbar.start; if (scrollbar.bar_position < 0) { scrollbar.bar_position = 0; } if (scrollbar.bar_position > scrollbar.h - scrollbar.bar_size) { scrollbar.bar_position = scrollbar.h - scrollbar.bar_size; } } } if (check_list) { if (B1_JUST_RELEASED) toggled_item = current_item; } if (B1_DOUBLE_CLICKED) { selected_item = current_item; } } }
void UI_SLIDER2::process(int focus) { int OnMe, mouse_lock_move; if (disabled_flag) { return; } OnMe = is_mouse_on(); if ( OnMe ) { // are we on the button? if ( (ui_mouse.y >= (y+currentPosition)) && (ui_mouse.y <= (y+currentPosition+slider_h)) ) { slider_mode = S2M_ON_ME; if ( B1_PRESSED ) { mouse_locked = 1; } } } else slider_mode = S2M_DEFAULT; if ( !B1_PRESSED) { if (mouse_locked == 1) if (captureCallback != NULL) { captureCallback(); mprintf(("Called captureCallback()!\n")); } mouse_locked = 0; } if (!OnMe && !mouse_locked) return; // could we possibly be moving up? if ((OnMe && B1_PRESSED && ui_mouse.y < (currentPosition+y+slider_half_h-1)) || (mouse_locked && (ui_mouse.y < (currentPosition+y+slider_half_h-1))) ) { // make sure we wait at least 50 ms between events unless mouse locked if ( (timer_get_milliseconds() > last_scrolled+50) || B1_JUST_PRESSED || mouse_locked ) { last_scrolled = timer_get_milliseconds(); if (!mouse_locked) { if (currentItem > 0) { currentItem--; if (upCallback != NULL) { upCallback(); if (captureCallback != NULL) captureCallback(); } } currentPosition = fl2i((((float)currentItem/(float)numberItems) * (float)numberPositions)-.49); } else { mouse_lock_move = fl2i( ((((float)ui_mouse.y - (float)y - (float)slider_half_h)/(float)numberPositions) * (float)numberItems) -.49); mouse_lock_move = currentItem - mouse_lock_move; if (mouse_lock_move > 0) { while (mouse_lock_move > 0) { if (currentItem > 0) { currentItem--; if (upCallback != NULL) upCallback(); } mouse_lock_move--; } } // currentPosition = ui_mouse.y - y - slider_half_h; currentPosition = fl2i((((float)currentItem/(float)numberItems) * (float)numberPositions)-.49); } if (currentPosition < 0) currentPosition = 0; if (currentPosition > numberPositions) currentPosition = numberPositions; slider_mode = S2M_MOVING; } } if ( ( OnMe && B1_PRESSED && ui_mouse.y > (currentPosition+y+slider_half_h+1)) || (mouse_locked && (ui_mouse.y > (currentPosition+y+slider_half_h+1))) ) { // make sure we wait at least 50 ms between events unless mouse locked if ( (timer_get_milliseconds() > last_scrolled+50) || B1_JUST_PRESSED || mouse_locked ) { last_scrolled = timer_get_milliseconds(); if (!mouse_locked) { if (currentItem < numberItems) { currentItem++; if (downCallback != NULL) { downCallback(); if (captureCallback != NULL) captureCallback(); } } currentPosition = fl2i((((float)currentItem/(float)numberItems) * (float)numberPositions)-.49); } else { mouse_lock_move = fl2i( ((((float)ui_mouse.y - (float)y - (float)slider_half_h)/(float)numberPositions) * (float)numberItems) -.49); mouse_lock_move -= currentItem; if (mouse_lock_move > 0) { while (mouse_lock_move > 0) { if (currentItem < numberItems) { currentItem++; if (downCallback != NULL) downCallback(); } mouse_lock_move--; } } // currentPosition = ui_mouse.y - y - slider_half_h; currentPosition = fl2i((((float)currentItem/(float)numberItems) * (float)numberPositions)-.49); } if (currentPosition < 0){ currentPosition = 0; } if (currentPosition > numberPositions){ currentPosition = numberPositions; } slider_mode = S2M_MOVING; } } // if we are centerd on the bitmap and still in mouse lock mode, we need to make sure the MOVING bitmap is still shown // or if mouse is on us and we are pressing the mouse button if (mouse_locked || (OnMe && B1_PRESSED)){ slider_mode = S2M_MOVING; } }
void UI_RADIO::process(int focus) { int OnMe, oldposition; if (disabled_flag) { position = 0; return; } if (my_wnd->selected_gadget == this) focus = 1; OnMe = is_mouse_on(); oldposition = position; if (B1_PRESSED && OnMe) { position = 1; } else { position = 0; } if (my_wnd->keypress == hotkey) { position = 2; my_wnd->last_keypress = 0; } if ( focus && ((my_wnd->keypress == KEY_SPACEBAR) || (my_wnd->keypress == KEY_ENTER)) ) position = 2; if (focus) if ( (oldposition == 2) && (keyd_pressed[KEY_SPACEBAR] || keyd_pressed[KEY_ENTER]) ) position = 2; pressed_down = 0; if (position) { if ( (oldposition == 1) && OnMe ) pressed_down = 1; if ( (oldposition == 2) && focus ) pressed_down = 1; } if (pressed_down && user_function) { user_function(); } if (pressed_down && (flag == 0)) { UI_GADGET *tmp = (UI_GADGET *) next; UI_RADIO *tmpr; while (tmp != this) { if (tmp->kind == UI_KIND_RADIO) { tmpr = (UI_RADIO *) tmp; if ((tmpr->group == group) && tmpr->flag) { tmpr->flag = 0; tmpr->pressed_down = 0; } } tmp = tmp->next; } flag = 1; } }