35 #define ERR_DP LOG_STREAM(err, log_display)
37 using namespace std::chrono_literals;
45 , scroll_right_(false)
47 , scroll_carry_x_(0.0)
48 , scroll_carry_y_(0.0)
49 , key_release_listener_(*this)
50 , long_touch_timer_(0)
69 #ifdef MOUSE_TOUCH_EMULATION
70 if(mouse_state & SDL_BUTTON(SDL_BUTTON_RIGHT)) {
72 mouse_state = SDL_BUTTON(SDL_BUTTON_LEFT);
80 bool yes_actually_dragging = dx * dx + dy * dy >= threshold * threshold;
82 if(!yes_actually_dragging
83 && (mouse_state & SDL_BUTTON(SDL_BUTTON_LEFT)) != 0)
100 SDL_Event new_event = {};
109 case SDL_TEXTEDITING:
111 SDL_Event evt = event;
112 evt.type = SDL_TEXTINPUT;
115 SDL_StartTextInput();
123 if(event.key.keysym.sym == SDLK_ESCAPE) {
141 case SDL_JOYBUTTONDOWN:
145 case SDL_JOYHATMOTION:
149 case SDL_MOUSEMOTION:
151 if(SDL_PeepEvents(&new_event, 1, SDL_GETEVENT, SDL_MOUSEMOTION, SDL_MOUSEMOTION) > 0) {
152 while(SDL_PeepEvents(&new_event, 1, SDL_GETEVENT, SDL_MOUSEMOTION, SDL_MOUSEMOTION) > 0) {};
164 case SDL_FINGERMOTION:
165 if(SDL_PeepEvents(&new_event, 1, SDL_GETEVENT, SDL_FINGERMOTION, SDL_FINGERMOTION) > 0) {
166 while(SDL_PeepEvents(&new_event, 1, SDL_GETEVENT, SDL_FINGERMOTION, SDL_FINGERMOTION) > 0) {
174 case SDL_MOUSEBUTTONDOWN:
176 int x =
event.button.x;
177 int y =
event.button.y;
193 case SDL_MOUSEBUTTONUP:
213 #if defined(_WIN32) || defined(__APPLE__)
226 const char* video_driver = SDL_GetCurrentVideoDriver();
228 SDL_GetVersion(&ver);
229 if(video_driver !=
nullptr && ver.major <= 2 && ver.minor <= 0) {
230 if(std::strcmp(video_driver,
"x11") == 0 && ver.patch < 18) {
232 }
else if(std::strcmp(video_driver,
"wayland") == 0 && ver.patch < 20) {
247 case SDL_MULTIGESTURE:
264 if(event.type == SDL_KEYUP) {
276 const bool mouse_in_window =
281 double dx = 0.0, dy = 0.0;
283 int scroll_threshold =
prefs::get().mouse_scrolling()
288 if(m.get_location().contains(mousex, mousey)) {
289 scroll_threshold = 0;
294 auto tick_now = std::chrono::steady_clock::now();
299 dt = std::chrono::duration_cast<std::chrono::milliseconds>(tick_now -
last_scroll_tick_);
305 double scroll_amount = dt.count() * 0.036 * double(scroll_speed);
315 if(mouse_in_window) {
316 if(mousey < scroll_threshold) {
324 if(mousex < scroll_threshold) {
336 if((mouse_flags & SDL_BUTTON_MMASK) != 0 &&
prefs::get().middle_click_scrolls()) {
344 const double speed = 0.01 * scroll_amount;
345 const double snap_dist = 16;
346 const double x_diff = (mousex - original_loc.x);
347 const double y_diff = (mousey - original_loc.y);
349 if(std::fabs(x_diff) > snap_dist || std::fabs(y_diff) <= snap_dist) {
350 dx += speed * x_diff;
353 if(std::fabs(y_diff) > snap_dist || std::fabs(x_diff) <= snap_dist) {
354 dy += speed * y_diff;
371 point dist{int(dx), int(dy)};
401 show_menu(m, { menu_loc.x + 1, menu_loc.y + menu_loc.h + 1 },
false);
412 if(!str_vec.empty()) {
435 if(!menu || !cmd_exec) {
controller_base & controller_
void handle_event(const SDL_Event &event) override
bool handle_scroll(int mousex, int mousey, int mouse_flags)
Handle scrolling by keyboard, joystick and moving mouse near map edges.
virtual events::mouse_handler_base & get_mouse_handler_base()=0
Get a reference to a mouse handler member a derived class uses.
virtual ~controller_base()
virtual plugins_context * get_plugins_context()
Get (optionally) a plugins context a derived class uses.
virtual soundsource::manager * get_soundsource_man()
Get (optionally) a soundsources manager a derived class uses.
void handle_event(const SDL_Event &event) override
Process mouse- and keypress-events from SDL.
virtual void process_keyup_event(const SDL_Event &)
Process keyup (always).
bool show_menu(const theme::menu *menu, const point &loc, bool context_menu)
virtual void process() override
virtual void process_focus_keydown_event(const SDL_Event &)
Process keydown (only when the general map display does not have focus).
virtual bool have_keyboard_focus()
Derived classes should override this to return false when arrow keys should not scroll the map,...
virtual void execute_action(const std::vector< std::string > &items_arg)
virtual void process_keydown_event(const SDL_Event &)
Process keydown (always).
virtual display & get_display()=0
Get a reference to a display member a derived class uses.
void long_touch_callback(int x, int y)
std::size_t long_touch_timer_
Context menu timer.
virtual std::vector< std::string > additional_actions_pressed()
virtual hotkey::command_executor * get_hotkey_command_executor()
Optionally get a command executor to handle context menu events.
virtual void play_slice()
std::chrono::steady_clock::time_point last_scroll_tick_
virtual bool is_browsing() const
const theme::action * action_pressed()
const theme::menu * menu_pressed()
const map_location & mouseover_hex() const
bool scroll(const point &amount, bool force=false)
Scrolls the display by amount pixels.
virtual int drag_threshold() const
Minimum dragging distance to fire the drag&drop.
void touch_motion_event(const SDL_TouchFingerEvent &event, const bool browse)
virtual void mouse_press(const SDL_MouseButtonEvent &event, const bool browse)
void mouse_update(const bool browse, map_location loc)
Update the mouse with a fake mouse motion.
void mouse_motion_event(const SDL_MouseMotionEvent &event, const bool browse)
bool scroll_started() const
bool get_show_menu() const
virtual void mouse_wheel(int xscroll, int yscroll, bool browse)
Called when scrolling with the mouse wheel.
const point get_scroll_start() const
void set_scroll_start(int x, int y)
Called when the middle click scrolling.
virtual void show_menu(const std::vector< config > &items_arg, const point &menu_loc, bool context_menu)
void execute_quit_command()
void execute_action(const std::vector< std::string > &items_arg)
bool get_scroll_when_mouse_outside(bool def)
int mouse_scroll_threshold()
Gets the threshold for when to scroll.
const std::vector< std::string > & items() const
static lg::log_domain log_display("display")
controller_base framework: controller_base is roughly analogous to a "dialog" class in a GUI toolkit ...
map_display and display: classes which take care of displaying the map and game-data on the screen.
Standard logging facilities (interface).
void draw()
Trigger a draw cycle.
void raise_process_event()
void pump()
Process all events currently in the queue.
bool is_touch(const SDL_MouseButtonEvent &event)
Check if this mouse button event is caused by a touch.
Game configuration data as global variables.
std::chrono::milliseconds popup_show_delay
Delay before a popup shows.
bool is_in_dialog()
Is a dialog open?
std::size_t add_timer(const std::chrono::milliseconds &interval, const std::function< void(std::size_t id)> &callback, const bool repeat)
Adds a new timer.
bool remove_timer(const std::size_t id)
Removes a timer.
bool execute_timer(const std::size_t id)
Executes a timer.
void jhat_event(const SDL_Event &event, command_executor *executor)
void key_event(const SDL_Event &event, command_executor *executor)
void mbutton_event(const SDL_Event &event, command_executor *executor)
void run_events(command_executor *executor)
void jbutton_event(const SDL_Event &event, command_executor *executor)
void keyup_event(const SDL_Event &, command_executor *executor)
uint32_t get_mouse_state(int *x, int *y)
A wrapper for SDL_GetMouseState that gives coordinates in draw space.
bool contains(const Container &container, const Value &value)
Returns true iff value is found in container.
bool window_has_mouse_focus()
True iff the window has mouse focus.
point game_canvas_size()
The size of the game canvas, in drawing coordinates / game pixels.
rect game_canvas()
The game canvas area, in drawing coordinates.
This file contains the settings handling of the widget library.
Encapsulates the map of the game.
An abstract description of a rectangle with integer coordinates.
Contains the gui2 timer routines.