The Battle for Wesnoth  1.17.12+dev
controller_base.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2022
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 /**
18  * @file
19  * controller_base framework:
20  * controller_base is roughly analogous to a "dialog" class in a GUI toolkit
21  * which is appropriate for deriving wesnoth game modes, e.g. single player
22  * mode, multiplayer mode, replay mode, editor mode.
23  *
24  * It provides implementation details for:
25  * - play_slice, which is essentially one pass of the "main loop" of
26  * the application, pumping and dispatching SDL events, raising draw
27  * events, handling scrolling, sound sources, and some joystick issues
28  * It also handles displaying menus (Menu, Action).
29  *
30  * - showing context menus (much is delegated to command executor though)
31  *
32  * Other than this it functions as an abstract interface, enforcing that
33  * controllers derive from events::sdl_handler, hotkey_command_executor,
34  * and provide some accessors needed for event handling.
35  */
36 
37 #pragma once
38 
39 #include "events.hpp"
41 #include "key.hpp"
42 #include "quit_confirmation.hpp"
43 
44 class game_config_view;
45 class display;
46 class plugins_context;
47 
48 namespace events
49 {
50 class mouse_handler_base;
51 }
52 
53 namespace hotkey
54 {
55 class command_executor;
56 }
57 
58 namespace soundsource
59 {
60 class manager;
61 }
62 
64 {
65 public:
67  virtual ~controller_base();
68 
69  virtual void play_slice(bool is_delay_enabled = true);
70 
71  void apply_keyboard_scroll(int x, int y);
72 
73  void set_scroll_up(bool on)
74  {
75  scroll_up_ = on;
76  }
77 
78  void set_scroll_down(bool on)
79  {
80  scroll_down_ = on;
81  }
82 
83  void set_scroll_left(bool on)
84  {
85  scroll_left_ = on;
86  }
87 
88  void set_scroll_right(bool on)
89  {
90  scroll_right_ = on;
91  }
92 
93  /** Optionally get a command executor to handle context menu events. */
95  {
96  return nullptr;
97  }
98 
99 protected:
100  virtual bool is_browsing() const
101  {
102  return false;
103  }
104 
105  /** Get a reference to a mouse handler member a derived class uses. */
106  virtual events::mouse_handler_base& get_mouse_handler_base() = 0;
107 
108  /** Get a reference to a display member a derived class uses. */
109  virtual display& get_display() = 0;
110 
111  /** Get (optionally) a soundsources manager a derived class uses. */
113  {
114  return nullptr;
115  }
116 
117  /** Get (optionally) a plugins context a derived class uses. */
119  {
120  return nullptr;
121  }
122 
123  /**
124  * Derived classes should override this to return false when arrow keys
125  * should not scroll the map, hotkeys not processed etc, for example
126  * when a textbox is active
127  * @returns true when arrow keys should scroll the map, false otherwise
128  */
129  virtual bool have_keyboard_focus();
130 
131  virtual std::vector<std::string> additional_actions_pressed()
132  {
133  return std::vector<std::string>();
134  }
135 
136  /**
137  * Handle scrolling by keyboard, joystick and moving mouse near map edges
138  * @see scrolling_, which is set if the display is being scrolled
139  * @return true when there was any scrolling, false otherwise
140  */
141  bool handle_scroll(int mousex, int mousey, int mouse_flags);
142 
143  /**
144  * Process mouse- and keypress-events from SDL.
145  * Calls various virtual function to allow specialized
146  * behavior of derived classes.
147  */
148  void handle_event(const SDL_Event& event) override;
149 
150  /** Process keydown (only when the general map display does not have focus). */
151  virtual void process_focus_keydown_event(const SDL_Event& /*event*/)
152  {
153  // No action by default
154  }
155 
156  virtual void process(events::pump_info&) override;
157 
158  /** Process keydown (always). Overridden in derived classes */
159  virtual void process_keydown_event(const SDL_Event& /*event*/)
160  {
161  // No action by default
162  }
163 
164  /** Process keyup (always). * Overridden in derived classes */
165  virtual void process_keyup_event(const SDL_Event& /*event*/)
166  {
167  // No action by default
168  }
169 
170  virtual void show_menu(const std::vector<config>& items_arg, int xloc, int yloc, bool context_menu, display& disp);
171  virtual void execute_action(const std::vector<std::string>& items_arg, int xloc, int yloc, bool context_menu);
172 
173  virtual bool in_context_menu(hotkey::HOTKEY_COMMAND command) const;
174 
175  void long_touch_callback(int x, int y);
176 
178 
180 
186  /* When the last scroll tick was processed */
188  /* Sub-pixel movement left over from a previous scroll tick.
189  * This is added to the next scroll tick, if scrolling continues. */
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 
209  private:
211  };
212 
214 
216  /** Context menu timer */
218 };
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)
uint32_t last_scroll_tick_
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:278
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:185
keyup_listener(controller_base &controller)
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).
void set_scroll_right(bool on)
size_t long_touch_timer_
Context menu timer.
Handling of system events.
Definition: manager.hpp:43
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_
Class that keeps track of all the keys on the keyboard.
Definition: key.hpp:28