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