The Battle for Wesnoth  1.17.0-dev
controller_base.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2021
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 /**
16  * @file
17  * controller_base framework:
18  * controller_base is roughly analogous to a "dialog" class in a GUI toolkit
19  * which is appropriate for deriving wesnoth game modes, e.g. single player
20  * mode, multiplayer mode, replay mode, editor mode.
21  *
22  * It provides implementation details for:
23  * - play_slice, which is essentially one pass of the "main loop" of
24  * the application, pumping and dispatching SDL events, raising draw
25  * events, handling scrolling, sound sources, and some joystick issues
26  * It also handles displaying menus (Menu, Action).
27  *
28  * - showing context menus (much is delegated to command executor though)
29  *
30  * Other than this it functions as an abstract interface, enforcing that
31  * controllers derive from events::sdl_handler, hotkey_command_executor,
32  * and provide some accessors needed for event handling.
33  */
34 
35 #pragma once
36 
37 #include "events.hpp"
39 #include "key.hpp"
40 #include "quit_confirmation.hpp"
41 #include "video.hpp"
42 
43 class game_config_view;
44 class display;
45 class plugins_context;
46 
47 namespace events
48 {
49 class mouse_handler_base;
50 }
51 
52 namespace hotkey
53 {
54 class command_executor;
55 }
56 
57 namespace soundsource
58 {
59 class manager;
60 }
61 
63 {
64 public:
66  virtual ~controller_base();
67 
68  virtual void play_slice(bool is_delay_enabled = true);
69 
70  static const config& get_theme(const game_config_view& game_config, std::string theme_name);
71 
72  void apply_keyboard_scroll(int x, int y);
73 
74  void set_scroll_up(bool on)
75  {
76  scroll_up_ = on;
77  }
78 
79  void set_scroll_down(bool on)
80  {
81  scroll_down_ = on;
82  }
83 
84  void set_scroll_left(bool on)
85  {
86  scroll_left_ = on;
87  }
88 
89  void set_scroll_right(bool on)
90  {
91  scroll_right_ = on;
92  }
93 
94  /** Optionally get a command executor to handle context menu events. */
96  {
97  return nullptr;
98  }
99 
100 protected:
101  virtual bool is_browsing() const
102  {
103  return false;
104  }
105 
106  /** Get a reference to a mouse handler member a derived class uses. */
107  virtual events::mouse_handler_base& get_mouse_handler_base() = 0;
108 
109  /** Get a reference to a display member a derived class uses. */
110  virtual display& get_display() = 0;
111 
112  /** Get (optionally) a soundsources manager a derived class uses. */
114  {
115  return nullptr;
116  }
117 
118  /** Get (optionally) a plugins context a derived class uses. */
120  {
121  return nullptr;
122  }
123 
124  /**
125  * Derived classes should override this to return false when arrow keys
126  * should not scroll the map, hotkeys not processed etc, for example
127  * when a textbox is active
128  * @returns true when arrow keys should scroll the map, false otherwise
129  */
130  virtual bool have_keyboard_focus();
131 
132  virtual std::vector<std::string> additional_actions_pressed()
133  {
134  return std::vector<std::string>();
135  }
136 
137  /**
138  * Handle scrolling by keyboard, joystick and moving mouse near map edges
139  * @see scrolling_, which is set if the display is being scrolled
140  * @return true when there was any scrolling, false otherwise
141  */
142  bool handle_scroll(int mousex, int mousey, int mouse_flags);
143 
144  /**
145  * Process mouse- and keypress-events from SDL.
146  * Calls various virtual function to allow specialized
147  * behavior of derived classes.
148  */
149  void handle_event(const SDL_Event& event) override;
150 
151  void handle_window_event(const SDL_Event& /*event*/) override
152  {
153  // No action by default
154  }
155 
156  /** Process keydown (only when the general map display does not have focus). */
157  virtual void process_focus_keydown_event(const SDL_Event& /*event*/)
158  {
159  // No action by default
160  }
161 
162  virtual void process(events::pump_info&) override;
163 
164  /** Process keydown (always). Overridden in derived classes */
165  virtual void process_keydown_event(const SDL_Event& /*event*/)
166  {
167  // No action by default
168  }
169 
170  /** Process keyup (always). * Overridden in derived classes */
171  virtual void process_keyup_event(const SDL_Event& /*event*/)
172  {
173  // No action by default
174  }
175 
176  virtual void show_menu(const std::vector<config>& items_arg, int xloc, int yloc, bool context_menu, display& disp);
177  virtual void execute_action(const std::vector<std::string>& items_arg, int xloc, int yloc, bool context_menu);
178 
179  virtual bool in_context_menu(hotkey::HOTKEY_COMMAND command) const;
180 
181  void long_touch_callback(int x, int y);
182 
184 
186 
192 
193 private:
194  /* A separate class for listening key-up events.
195  It's needed because otherwise such events might be consumed by a different event context
196  and the input system would believe that the player is still holding a key (bug #2573) */
198  {
199  public:
201  : events::sdl_handler(false)
202  , controller_(controller)
203  {
204  join_global();
205  }
206 
207  void handle_event(const SDL_Event& event) override;
208  void handle_window_event(const SDL_Event&) override {}
209 
210  private:
212  };
213 
215 
217  /** Context menu timer */
219 };
virtual plugins_context * get_plugins_context()
Get (optionally) a plugins context a derived class uses.
void set_scroll_up(bool on)
virtual void process_keyup_event(const SDL_Event &)
Process keyup (always).
void set_scroll_down(bool on)
keyup_listener key_release_listener_
Keyboard shortcuts for game actions.
virtual std::vector< std::string > additional_actions_pressed()
virtual soundsource::manager * get_soundsource_man()
Get (optionally) a soundsources manager a derived class uses.
virtual bool is_browsing() const
void process(int mousex, int mousey)
Definition: tooltips.cpp:193
int show_menu(lua_State *L)
Displays a popup menu at the current mouse position Best used from a [set_menu_item], to show a submenu.
Definition: lua_gui2.cpp:184
keyup_listener(controller_base &controller)
void handle_window_event(const SDL_Event &) override
virtual void process_keydown_event(const SDL_Event &)
Process keydown (always).
virtual void process_focus_keydown_event(const SDL_Event &)
Process keydown (only when the general map display does not have focus).
Game configuration data as global variables.
Definition: build_info.cpp:58
void set_scroll_right(bool on)
size_t long_touch_timer_
Context menu timer.
void handle_window_event(const SDL_Event &) override
Handling of system events.
Definition: manager.hpp:42
virtual hotkey::command_executor * get_hotkey_command_executor()
Optionally get a command executor to handle context menu events.
void set_scroll_left(bool on)
const game_config_view & game_config_
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:59
Class that keeps track of all the keys on the keyboard.
Definition: key.hpp:27