The Battle for Wesnoth  1.17.0-dev
controller_base.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2021
3  by Joerg Hinrichs <joerg.hinrichs@alice-dsl.de>
4  Copyright (C) 2003 by David White <dave@whitevine.net>
5  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY.
13 
14  See the COPYING file for more details.
15 */
16 
17 #include "controller_base.hpp"
18 
19 #include "display.hpp"
20 #include "events.hpp"
21 #include "game_config_manager.hpp"
23 #include "log.hpp"
24 #include "map/map.hpp"
25 #include "mouse_handler_base.hpp"
26 #include "preferences/game.hpp"
28 #include "show_dialog.hpp" //gui::in_dialog
29 #include "gui/core/event/handler.hpp" // gui2::is_in_dialog
30 #include "soundsource.hpp"
31 #include "gui/core/timer.hpp"
32 
33 static lg::log_domain log_display("display");
34 #define ERR_DP LOG_STREAM(err, log_display)
35 
36 static const int long_touch_duration_ms = 800;
37 
39  : game_config_(game_config_manager::get()->game_config())
40  , key_()
41  , scrolling_(false)
42  , scroll_up_(false)
43  , scroll_down_(false)
44  , scroll_left_(false)
45  , scroll_right_(false)
46  , key_release_listener_(*this)
47  , last_mouse_is_touch_(false)
48  , long_touch_timer_(0)
49 {
50 }
51 
53 {
54  if(long_touch_timer_ != 0) {
57  }
58 }
59 
61 {
62  if(long_touch_timer_ != 0 && !get_mouse_handler_base().dragging_started()) {
63  int x_now;
64  int y_now;
65  uint32_t mouse_state = SDL_GetMouseState(&x_now, &y_now);
66 
67 #ifdef MOUSE_TOUCH_EMULATION
68  if(mouse_state & SDL_BUTTON(SDL_BUTTON_RIGHT)) {
69  // Monkey-patch touch controls again to make them look like left button.
70  mouse_state = SDL_BUTTON(SDL_BUTTON_LEFT);
71  }
72 #endif
73 
74  // Workaround for double-menu b/c of slow events processing, or I don't know.
75  int dx = x - x_now;
76  int dy = y - y_now;
77  int threshold = get_mouse_handler_base().drag_threshold();
78  bool yes_actually_dragging = dx * dx + dy * dy >= threshold * threshold;
79 
80  if(!yes_actually_dragging
81  && (mouse_state & SDL_BUTTON(SDL_BUTTON_LEFT)) != 0
82  && sdl::point_in_rect(x_now, y_now, get_display().map_area()))
83  {
85  if(m != nullptr) {
86  show_menu(get_display().get_theme().context_menu()->items(), x_now, y_now, true, get_display());
87  }
88  }
89  }
90 
92 }
93 
94 void controller_base::handle_event(const SDL_Event& event)
95 {
96  if(gui::in_dialog()) {
97  return;
98  }
99 
101 
102  SDL_Event new_event = {};
103 
104  switch(event.type) {
105  case SDL_TEXTINPUT:
106  if(have_keyboard_focus()) {
108  }
109  break;
110 
111  case SDL_TEXTEDITING:
112  if(have_keyboard_focus()) {
113  SDL_Event evt = event;
114  evt.type = SDL_TEXTINPUT;
116  SDL_StopTextInput();
117  SDL_StartTextInput();
118  }
119  break;
120 
121  case SDL_KEYDOWN:
122  // Detect key press events, unless there something that has keyboard focus
123  // in which case the key press events should go only to it.
124  if(have_keyboard_focus()) {
125  if(event.key.keysym.sym == SDLK_ESCAPE) {
127  break;
128  }
129 
130  process_keydown_event(event);
132  process_keyup_event(event);
133  } else {
135  }
136  break;
137 
138  case SDL_KEYUP:
139  process_keyup_event(event);
141  break;
142 
143  case SDL_JOYBUTTONDOWN:
145  break;
146 
147  case SDL_JOYHATMOTION:
149  break;
150 
151  case SDL_MOUSEMOTION:
152  // Ignore old mouse motion events in the event queue
153  if(SDL_PeepEvents(&new_event, 1, SDL_GETEVENT, SDL_MOUSEMOTION, SDL_MOUSEMOTION) > 0) {
154  while(SDL_PeepEvents(&new_event, 1, SDL_GETEVENT, SDL_MOUSEMOTION, SDL_MOUSEMOTION) > 0) {
155  };
156  if(new_event.motion.which != SDL_TOUCH_MOUSEID) {
157  mh_base.mouse_motion_event(new_event.motion, is_browsing());
158  }
159  } else {
160  if(new_event.motion.which != SDL_TOUCH_MOUSEID) {
161  mh_base.mouse_motion_event(event.motion, is_browsing());
162  }
163  }
164  break;
165 
166  case SDL_FINGERMOTION:
167  if(SDL_PeepEvents(&new_event, 1, SDL_GETEVENT, SDL_FINGERMOTION, SDL_FINGERMOTION) > 0) {
168  while(SDL_PeepEvents(&new_event, 1, SDL_GETEVENT, SDL_FINGERMOTION, SDL_FINGERMOTION) > 0) {
169  };
170  mh_base.touch_motion_event(new_event.tfinger, is_browsing());
171  } else {
172  mh_base.touch_motion_event(event.tfinger, is_browsing());
173  }
174  break;
175 
176  case SDL_MOUSEBUTTONDOWN:
177  last_mouse_is_touch_ = event.button.which == SDL_TOUCH_MOUSEID;
178 
182  std::bind(&controller_base::long_touch_callback, this, event.button.x, event.button.y));
183  }
184 
185  mh_base.mouse_press(event.button, is_browsing());
187  break;
188 
189  case SDL_FINGERDOWN:
190  // handled by mouse case
191  break;
192 
193  case SDL_MOUSEBUTTONUP:
194  if(long_touch_timer_ != 0) {
196  long_touch_timer_ = 0;
197  }
198 
199  last_mouse_is_touch_ = event.button.which == SDL_TOUCH_MOUSEID;
200 
201  mh_base.mouse_press(event.button, is_browsing());
202  if(mh_base.get_show_menu()) {
203  show_menu(get_display().get_theme().context_menu()->items(), event.button.x, event.button.y, true,
204  get_display());
205  }
206  break;
207  case DOUBLE_CLICK_EVENT:
208  {
209  int x = static_cast<int>(reinterpret_cast<std::intptr_t>(event.user.data1));
210  int y = static_cast<int>(reinterpret_cast<std::intptr_t>(event.user.data2));
211  if(event.user.code == static_cast<int>(SDL_TOUCH_MOUSEID)
212  // TODO: Move to right_click_show_menu?
213  && sdl::point_in_rect(x, y, get_display().map_area())
214  // TODO: This chain repeats in several places, move to a method.
215  && get_display().get_theme().context_menu() != nullptr) {
216  show_menu(get_display().get_theme().context_menu()->items(),
217  x,
218  y,
219  true,
220  get_display());
221  }
222  }
223  break;
224 
225  case SDL_FINGERUP:
226  // handled by mouse case
227  break;
228 
229  case SDL_MOUSEWHEEL:
230 #if defined(_WIN32) || defined(__APPLE__)
231  mh_base.mouse_wheel(-event.wheel.x, event.wheel.y, is_browsing());
232 #else
233  mh_base.mouse_wheel(event.wheel.x, event.wheel.y, is_browsing());
234 #endif
235  break;
236 
237  case TIMER_EVENT:
238  gui2::execute_timer(reinterpret_cast<size_t>(event.user.data1));
239  break;
240 
241  // TODO: Support finger specifically, like pan the map. For now, SDL's "shadow mouse" events will do.
242  case SDL_MULTIGESTURE:
243  default:
244  break;
245  }
246 }
247 
249 {
250  if(gui2::is_in_dialog()) {
251  return;
252  }
253 
255 }
256 
258 {
259  if(event.type == SDL_KEYUP) {
260  hotkey::keyup_event(event, controller_.get_hotkey_command_executor());
261  }
262 }
263 
265 {
266  return true;
267 }
268 
269 bool controller_base::handle_scroll(int mousex, int mousey, int mouse_flags)
270 {
271  const bool mouse_in_window =
272  CVideo::get_singleton().window_has_flags(SDL_WINDOW_MOUSE_FOCUS)
273  || preferences::get("scroll_when_mouse_outside", true);
274 
276  int dx = 0, dy = 0;
277 
278  int scroll_threshold = preferences::mouse_scroll_enabled()
280  : 0;
281 
282  for(const theme::menu& m : get_display().get_theme().menus()) {
283  if(sdl::point_in_rect(mousex, mousey, m.get_location())) {
284  scroll_threshold = 0;
285  }
286  }
287 
288  // Apply keyboard scrolling
289  dy -= scroll_up_ * scroll_speed;
290  dy += scroll_down_ * scroll_speed;
291  dx -= scroll_left_ * scroll_speed;
292  dx += scroll_right_ * scroll_speed;
293 
294  // Scroll if mouse is placed near the edge of the screen
295  if(mouse_in_window) {
296  if(mousey < scroll_threshold) {
297  dy -= scroll_speed;
298  }
299 
300  if(mousey > get_display().video().get_height() - scroll_threshold) {
301  dy += scroll_speed;
302  }
303 
304  if(mousex < scroll_threshold) {
305  dx -= scroll_speed;
306  }
307 
308  if(mousex > get_display().video().get_width() - scroll_threshold) {
309  dx += scroll_speed;
310  }
311  }
312 
314 
315  // Scroll with middle-mouse if enabled
316  if((mouse_flags & SDL_BUTTON_MMASK) != 0 && preferences::middle_click_scrolls()) {
317  const SDL_Point original_loc = mh_base.get_scroll_start();
318 
319  if(mh_base.scroll_started()) {
320  const SDL_Rect& rect = get_display().map_outside_area();
321 
322  if(sdl::point_in_rect(mousex, mousey, rect) && mh_base.scroll_started()) {
323  // Scroll speed is proportional from the distance from the first
324  // middle click and scrolling speed preference.
325  const double speed = 0.04 * std::sqrt(static_cast<double>(scroll_speed));
326  const double snap_dist = 16; // Snap to horizontal/vertical scrolling
327  const double x_diff = (mousex - original_loc.x);
328  const double y_diff = (mousey - original_loc.y);
329 
330  if(std::fabs(x_diff) > snap_dist || std::fabs(y_diff) <= snap_dist) {
331  dx += speed * x_diff;
332  }
333 
334  if(std::fabs(y_diff) > snap_dist || std::fabs(x_diff) <= snap_dist) {
335  dy += speed * y_diff;
336  }
337  }
338  } else { // Event may fire mouse down out of order with respect to initial click
339  mh_base.set_scroll_start(mousex, mousey);
340  }
341  }
342 
343  return get_display().scroll(dx, dy);
344 }
345 
346 void controller_base::play_slice(bool is_delay_enabled)
347 {
348  CKey key;
349 
351  l->play_slice();
352  }
353 
354  events::pump();
357 
358  // Update sound sources before scrolling
360  l->update();
361  }
362 
363  const theme::menu* const m = get_display().menu_pressed();
364  if(m != nullptr) {
365  const SDL_Rect& menu_loc = m->location(get_display().video().screen_area());
366  show_menu(m->items(), menu_loc.x + 1, menu_loc.y + menu_loc.h + 1, false, get_display());
367 
368  return;
369  }
370 
371  const theme::action* const a = get_display().action_pressed();
372  if(a != nullptr) {
373  const SDL_Rect& action_loc = a->location(get_display().video().screen_area());
374  execute_action(a->items(), action_loc.x + 1, action_loc.y + action_loc.h + 1, false);
375 
376  return;
377  }
378 
379  auto str_vec = additional_actions_pressed();
380  if(!str_vec.empty()) {
381  execute_action(str_vec, 0, 0, false);
382  return;
383  }
384 
385  bool was_scrolling = scrolling_;
386 
387  int mousex, mousey;
388  uint8_t mouse_flags = SDL_GetMouseState(&mousex, &mousey);
389 
390  scrolling_ = handle_scroll(mousex, mousey, mouse_flags);
391 
392  map_location highlighted_hex = get_display().mouseover_hex();
393 
394  // be nice when window is not visible // NOTE should be handled by display instead, to only disable drawing
395  if(is_delay_enabled && !CVideo::get_singleton().window_has_flags(SDL_WINDOW_SHOWN)) {
396  CVideo::delay(200);
397  }
398 
399  // Scrolling ended, update the cursor and the brightened hex
400  if(!scrolling_ && was_scrolling) {
401  get_mouse_handler_base().mouse_update(is_browsing(), highlighted_hex);
402  }
403 }
404 
406  const std::vector<config>& items_arg, int xloc, int yloc, bool context_menu, display& disp)
407 {
409  if(!cmd_exec) {
410  return;
411  }
412 
413  std::vector<config> items;
414  for(const config& c : items_arg) {
415  const std::string& id = c["id"];
417 
418  if(cmd_exec->can_execute_command(command) && (!context_menu || in_context_menu(command.id))) {
419  items.emplace_back("id", id);
420  }
421  }
422 
423  if(items.empty()) {
424  return;
425  }
426 
427  cmd_exec->show_menu(items, xloc, yloc, context_menu, disp);
428 }
429 
430 void controller_base::execute_action(const std::vector<std::string>& items_arg, int xloc, int yloc, bool context_menu)
431 {
433  if(!cmd_exec) {
434  return;
435  }
436 
437  std::vector<std::string> items;
438  for(const std::string& item : items_arg) {
440  if(cmd_exec->can_execute_command(command)) {
441  items.push_back(item);
442  }
443  }
444 
445  if(items.empty()) {
446  return;
447  }
448 
449  cmd_exec->execute_action(items, xloc, yloc, context_menu, get_display());
450 }
451 
453 {
454  return true;
455 }
456 
457 const config& controller_base::get_theme(const game_config_view& game_config, std::string theme_name)
458 {
459  if(theme_name.empty()) {
460  theme_name = preferences::theme();
461  }
462 
463  if(const config& c = game_config.find_child("theme", "id", theme_name)) {
464  return c;
465  }
466 
467  ERR_DP << "Theme '" << theme_name << "' not found. Trying the default theme." << std::endl;
468 
469  if(const config& c = game_config.find_child("theme", "id", "Default")) {
470  return c;
471  }
472 
473  ERR_DP << "Default theme not found." << std::endl;
474 
475  static config empty;
476  return empty;
477 }
virtual bool in_context_menu(hotkey::HOTKEY_COMMAND command) const
const map_location & mouseover_hex() const
Definition: display.hpp:290
virtual plugins_context * get_plugins_context()
Get (optionally) a plugins context a derived class uses.
virtual SDL_Rect & location(const SDL_Rect &screen) const
Definition: theme.cpp:318
const SDL_Point get_scroll_start() const
virtual void process_keyup_event(const SDL_Event &)
Process keyup (always).
static const int long_touch_duration_ms
HOTKEY_COMMAND id
the names are strange: the "hotkey::HOTKEY_COMMAND" is named id, and the string to identify the objec...
theme & get_theme()
Definition: display.hpp:384
void set_scroll_start(int x, int y)
Called when the middle click scrolling.
int mouse_scroll_threshold()
Gets the threshold for when to scroll.
Definition: general.cpp:780
static lg::log_domain log_display("display")
virtual int drag_threshold() const
Minimum dragging distance to fire the drag&drop.
virtual void show_menu(const std::vector< config > &items_arg, int xloc, int yloc, bool context_menu, display &disp)
#define a
bool in_dialog()
Definition: show_dialog.cpp:53
int scroll_speed()
Definition: general.cpp:751
bool is_in_dialog()
Is a dialog open?
Definition: handler.cpp:1116
static const config & get_theme(const game_config_view &game_config, std::string theme_name)
#define TIMER_EVENT
Definition: events.hpp:25
Stores all information related to functions that can be bound to hotkeys.
void touch_motion_event(const SDL_TouchFingerEvent &event, const bool browse)
virtual void play_slice(bool is_delay_enabled=true)
static CVideo & get_singleton()
Definition: video.hpp:49
void mbutton_event(const SDL_Event &event, command_executor *executor)
virtual display & get_display()=0
Get a reference to a display member a derived class uses.
const std::vector< std::string > items
void mouse_motion_event(const SDL_MouseMotionEvent &event, const bool browse)
controller_base framework: controller_base is roughly analogous to a "dialog" class in a GUI toolkit ...
virtual bool have_keyboard_focus()
Derived classes should override this to return false when arrow keys should not scroll the map...
virtual bool can_execute_command(const hotkey_command &command, int index=-1) const =0
virtual std::vector< std::string > additional_actions_pressed()
const theme::action * action_pressed()
Definition: display.cpp:1730
void run_events(command_executor *executor)
virtual void mouse_press(const SDL_MouseButtonEvent &event, const bool browse)
virtual soundsource::manager * get_soundsource_man()
Get (optionally) a soundsources manager a derived class uses.
virtual bool is_browsing() const
std::string get(const std::string &key)
Definition: general.cpp:209
void jbutton_event(const SDL_Event &event, command_executor *executor)
const theme::menu * menu_pressed()
Definition: display.cpp:1746
std::string theme()
Definition: game.cpp:815
void handle_event(const SDL_Event &event) override
virtual void show_menu(const std::vector< config > &items_arg, int xloc, int yloc, bool context_menu, display &gui)
void keyup_event(const SDL_Event &, command_executor *executor)
void handle_event(const SDL_Event &event) override
Process mouse- and keypress-events from SDL.
map_display and display: classes which take care of displaying the map and game-data on the screen...
int get_width(bool as_pixels=true) const
Returns the window renderer width in pixels or screen coordinates.
Definition: video.cpp:291
const std::vector< std::string > & items() const
Definition: theme.hpp:182
virtual ~controller_base()
void raise_draw_event()
Definition: events.cpp:774
void jhat_event(const SDL_Event &event, command_executor *executor)
virtual events::mouse_handler_base & get_mouse_handler_base()=0
Get a reference to a mouse handler member a derived class uses.
void pump()
Definition: events.cpp:473
bool point_in_rect(int x, int y, const SDL_Rect &rect)
Tests whether a point is inside a rectangle.
Definition: rect.cpp:23
virtual void process(events::pump_info &) override
virtual void execute_action(const std::vector< std::string > &items_arg, int xloc, int yloc, bool context_menu)
Encapsulates the map of the game.
Definition: location.hpp:38
virtual void process_keydown_event(const SDL_Event &)
Process keydown (always).
#define DOUBLE_CLICK_EVENT
Definition: events.hpp:24
void raise_process_event()
Definition: events.cpp:751
virtual void process_focus_keydown_event(const SDL_Event &)
Process keydown (only when the general map display does not have focus).
const std::vector< config > & items() const
Definition: theme.hpp:235
Game configuration data as global variables.
Definition: build_info.cpp:59
bool middle_click_scrolls()
Definition: general.cpp:765
void execute_action(const std::vector< std::string > &items_arg, int xloc, int yloc, bool context_menu, display &gui)
const SDL_Rect & map_outside_area() const
Returns the available area for a map, this may differ from the above.
Definition: display.hpp:242
Contains the gui2 timer routines.
CURSOR_TYPE get()
Definition: cursor.cpp:216
virtual void mouse_wheel(int xscroll, int yscroll, bool browse)
Called when scrolling with the mouse wheel.
std::size_t add_timer(const uint32_t interval, const std::function< void(std::size_t id)> &callback, const bool repeat)
Adds a new timer.
Definition: timer.cpp:127
int get_height(bool as_pixels=true) const
Returns the window renderer height in pixels or in screen coordinates.
Definition: video.cpp:296
size_t long_touch_timer_
Context menu timer.
void key_event(const SDL_Event &event, command_executor *executor)
bool handle_scroll(int mousex, int mousey, int mouse_flags)
Handle scrolling by keyboard, joystick and moving mouse near map edges.
const config & find_child(config_key_type key, const std::string &name, const std::string &value) const
void long_touch_callback(int x, int y)
virtual hotkey::command_executor * get_hotkey_command_executor()
Optionally get a command executor to handle context menu events.
bool mouse_scroll_enabled()
Definition: general.cpp:770
virtual display & gui()=0
Reference to the used display objects.
Standard logging facilities (interface).
CVideo & video()
Gets the underlying screen object.
Definition: display.hpp:201
const menu * context_menu() const
Definition: theme.hpp:259
static void delay(unsigned int milliseconds)
Waits a given number of milliseconds before returning.
Definition: video.cpp:301
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:61
Class that keeps track of all the keys on the keyboard.
Definition: key.hpp:28
mock_char c
const hotkey_command & get_hotkey_command(const std::string &command)
returns the hotkey_command with the given name
void mouse_update(const bool browse, map_location loc)
Update the mouse with a fake mouse motion.
bool scroll(int xmov, int ymov, bool force=false)
Scrolls the display by xmov,ymov pixels.
Definition: display.cpp:1926
#define ERR_DP
bool window_has_flags(uint32_t flags) const
Tests whether the given flags are currently set on the SDL window.
Definition: video.cpp:378
bool execute_timer(const std::size_t id)
Executes a timer.
Definition: timer.cpp:201
std::pair< std::string, unsigned > item
Definition: help_impl.hpp:410
bool remove_timer(const std::size_t id)
Removes a timer.
Definition: timer.cpp:168