void slider::draw_contents() { surface image(state_ != NORMAL ? highlightedImage_ : image_); if (image == NULL) return; SDL_Color line_color = font::NORMAL_COLOR; if (!enabled()) { image = greyscale_image(image); line_color = font::DISABLED_COLOR; } SDL_Rect const &loc = location(); if (image->w >= loc.w) return; surface screen = video().getSurface(); SDL_Rect line_rect = create_rect(loc.x + image->w / 2 , loc.y + loc.h / 2 , loc.w - image->w , 1); sdl_fill_rect(screen, &line_rect, SDL_MapRGB(screen->format, line_color.r, line_color.g, line_color.b)); SDL_Rect const &slider = slider_area(); video().blit_surface(slider.x, slider.y, image); }
void slider::mouse_down(const SDL_MouseButtonEvent& event) { bool prev_change = value_change_; if (!sdl::point_in_rect(event.x, event.y, location())) return; if (event.button != SDL_BUTTON_LEFT) return; state_ = CLICKED; set_focus(true); if (sdl::point_in_rect(event.x, event.y, slider_area())) { sound::play_UI_sound(game_config::sounds::button_press); } else { value_change_ = false; set_slider_position(event.x); if(value_change_) { sound::play_UI_sound(game_config::sounds::slider_adjust); } else { value_change_ = prev_change; } } }
void slider::handle_event(const SDL_Event& event) { gui::widget::handle_event(event); if (!enabled() || hidden()) return; STATE start_state = state_; switch(event.type) { case SDL_MOUSEBUTTONUP: if (!mouse_locked()) { bool on = sdl::point_in_rect(event.button.x, event.button.y, slider_area()); state_ = on ? ACTIVE : NORMAL; } break; case SDL_MOUSEBUTTONDOWN: if (!mouse_locked()) mouse_down(event.button); break; case SDL_MOUSEMOTION: if (!mouse_locked()) mouse_motion(event.motion); break; case SDL_KEYDOWN: if(focus(&event) && allow_key_events()) { //allow_key_events is used by zoom_sliders to disable left-right key press, which is buggy for them const SDL_Keysym& key = reinterpret_cast<const SDL_KeyboardEvent&>(event).keysym; const int c = key.sym; if(c == SDLK_LEFT) { sound::play_UI_sound(game_config::sounds::slider_adjust); set_value(value_ - increment_); } else if(c == SDLK_RIGHT) { sound::play_UI_sound(game_config::sounds::slider_adjust); set_value(value_ + increment_); } } break; case SDL_MOUSEWHEEL: if (!mouse_locked()) mouse_wheel(event.wheel); break; default: return; } if (start_state != state_) set_dirty(true); }
void slider::mouse_motion(const SDL_MouseMotionEvent& event) { if (state_ == NORMAL || state_ == ACTIVE) { bool on = point_in_rect(event.x, event.y, slider_area()); state_ = on ? ACTIVE : NORMAL; } else if (state_ == CLICKED || state_ == DRAGGED) { state_ = DRAGGED; bool prev_change = value_change_; value_change_ = false; set_slider_position(event.x); if(value_change_) { sound::play_UI_sound(game_config::sounds::slider_adjust); } else { value_change_ = prev_change; } } }
void slider::draw_contents() { surface image; switch (state_) { case NORMAL: image.assign(image_); break; case ACTIVE: image.assign(activeImage_); break; default: image.assign(pressedImage_); break; } assert(image != nullptr); color_t line_color = line_color_; if (!enabled()) { image.assign(disabledImage_); line_color = font::DISABLED_COLOR; } SDL_Rect const &loc = location(); if (image->w >= loc.w) return; surface& screen = video().getSurface(); SDL_Rect line_rect = sdl::create_rect(loc.x + image->w / 2 , loc.y + loc.h / 2 , loc.w - image->w , 1); sdl::fill_rect(screen, &line_rect, SDL_MapRGB(screen->format, line_color.r, line_color.g, line_color.b)); SDL_Rect const &slider = slider_area(); video().blit_surface(slider.x, slider.y, image); }