The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
controller_base.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2003 - 2025
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 "map/location.hpp"
43#include "quit_confirmation.hpp"
44#include "theme.hpp"
45
46#include <chrono>
47
49class display;
50class plugins_context;
51
52namespace events
53{
54class mouse_handler_base;
55}
56
57namespace hotkey
58{
60struct ui_command;
61}
62
63namespace soundsource
64{
65class manager;
66}
67
69{
70public:
72 virtual ~controller_base();
73
74 virtual void play_slice();
75
76 void set_scroll_up(bool on)
77 {
78 scroll_up_ = on;
79 }
80
81 void set_scroll_down(bool on)
82 {
83 scroll_down_ = on;
84 }
85
86 void set_scroll_left(bool on)
87 {
88 scroll_left_ = on;
89 }
90
91 void set_scroll_right(bool on)
92 {
93 scroll_right_ = on;
94 }
95
96 /** Optionally get a command executor to handle context menu events. */
98 {
99 return nullptr;
100 }
101
102protected:
103 virtual bool is_browsing() const
104 {
105 return false;
106 }
107
108 /** Get a reference to a mouse handler member a derived class uses. */
110
111 /** Get a reference to a display member a derived class uses. */
112 virtual display& get_display() = 0;
113
114 /** Get (optionally) a soundsources manager a derived class uses. */
116 {
117 return nullptr;
118 }
119
120 /** Get (optionally) a plugins context a derived class uses. */
122 {
123 return nullptr;
124 }
125
126 /**
127 * Derived classes should override this to return false when arrow keys
128 * should not scroll the map, hotkeys not processed etc, for example
129 * when a textbox is active
130 * @returns true when arrow keys should scroll the map, false otherwise
131 */
132 virtual bool have_keyboard_focus();
133
134 virtual std::vector<std::string> additional_actions_pressed()
135 {
136 return std::vector<std::string>();
137 }
138
139 /**
140 * Handle scrolling by keyboard, joystick and moving mouse near map edges
141 * @see scrolling_, which is set if the display is being scrolled
142 * @return true when there was any scrolling, false otherwise
143 */
144 bool handle_scroll(int mousex, int mousey, int mouse_flags);
145
146 /**
147 * Process mouse- and keypress-events from SDL.
148 * Calls various virtual function to allow specialized
149 * behavior of derived classes.
150 */
151 void handle_event(const SDL_Event& event) override;
152
153 /** Process keydown (only when the general map display does not have focus). */
154 virtual void process_focus_keydown_event(const SDL_Event& /*event*/)
155 {
156 // No action by default
157 }
158
159 virtual void process() override;
160
161 /** Process keydown (always). Overridden in derived classes */
162 virtual void process_keydown_event(const SDL_Event& /*event*/)
163 {
164 // No action by default
165 }
166
167 /** Process keyup (always). * Overridden in derived classes */
168 virtual void process_keyup_event(const SDL_Event& /*event*/)
169 {
170 // No action by default
171 }
172
173 bool show_menu(const theme::menu* menu, const point& loc, bool context_menu);
174 virtual void execute_action(const std::vector<std::string>& items_arg);
175
176 void long_touch_callback(int x, int y);
177
179
185 /* When the last scroll tick was processed */
186 std::chrono::steady_clock::time_point last_scroll_tick_;
187 /* Sub-pixel movement left over from a previous scroll tick.
188 * This is added to the next scroll tick, if scrolling continues. */
191
192private:
193 /* A separate class for listening key-up events.
194 It's needed because otherwise such events might be consumed by a different event context
195 and the input system would believe that the player is still holding a key (bug #2573) */
197 {
198 public:
200 : events::sdl_handler(false)
201 , controller_(controller)
202 {
203 join_global();
204 }
205
206 void handle_event(const SDL_Event& event) override;
207
208 private:
210 };
211
213
214 /** Context menu timer */
215 std::size_t long_touch_timer_;
216};
map_location loc
Definition move.cpp:172
void handle_event(const SDL_Event &event) override
keyup_listener(controller_base &controller)
virtual display & get_display()=0
Get a reference to a display member a derived class uses.
virtual plugins_context * get_plugins_context()
Get (optionally) a plugins context a derived class uses.
void set_scroll_up(bool on)
bool handle_scroll(int mousex, int mousey, int mouse_flags)
Handle scrolling by keyboard, joystick and moving mouse near map edges.
virtual ~controller_base()
void set_scroll_left(bool on)
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 soundsource::manager * get_soundsource_man()
Get (optionally) a soundsources manager a derived class uses.
virtual hotkey::command_executor * get_hotkey_command_executor()
Optionally get a command executor to handle context menu events.
virtual void process() override
void set_scroll_right(bool on)
virtual events::mouse_handler_base & get_mouse_handler_base()=0
Get a reference to a mouse handler member a derived class uses.
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).
const game_config_view & game_config_
void long_touch_callback(int x, int y)
keyup_listener key_release_listener_
std::size_t long_touch_timer_
Context menu timer.
void set_scroll_down(bool on)
virtual void play_slice()
std::chrono::steady_clock::time_point last_scroll_tick_
virtual std::vector< std::string > additional_actions_pressed()
virtual bool is_browsing() const
Sort-of-Singleton that many classes, both GUI and non-GUI, use to access the game data.
Definition display.hpp:88
virtual void join_global()
Definition events.cpp:371
sdl_handler(sdl_handler &&)=delete
A class grating read only view to a vector of config objects, viewed as one config with all children ...
Handling of system events.
Keyboard shortcuts for game actions.
This file contains object "key", which is used to store information about keys while annotation parsi...
Used as the main parameter for can_execute_command/do_execute_command These functions are used to exe...
Holds a 2D point.
Definition point.hpp:25
Definitions related to theme-support.