The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
controller_base.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2006 - 2016 by Joerg Hinrichs <[email protected]>
3  wesnoth playlevel Copyright (C) 2003 by David White <[email protected]>
4  Part of the Battle for Wesnoth Project http://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 #ifndef CONTROLLER_BASE_H_INCLUDED
37 #define CONTROLLER_BASE_H_INCLUDED
38 
39 #include "global.hpp"
40 
41 #include "events.hpp"
43 #include "joystick.hpp"
44 #include "key.hpp"
45 #include "video.hpp"
46 #include "quit_confirmation.hpp"
47 
48 class CVideo;
49 class display;
50 class plugins_context;
51 
52 namespace events { class mouse_handler_base; }
53 
54 namespace hotkey { class command_executor; }
55 
56 namespace soundsource { class manager; }
57 
59 {
60 public:
61  controller_base(const config& game_config, CVideo& video);
62  virtual ~controller_base();
63 
64  void play_slice(bool is_delay_enabled = true);
65 
66  static const config &get_theme(const config& game_config, std::string theme_name);
67 
68  void apply_keyboard_scroll(int x, int y);
69  void set_scroll_up(bool on);
70  void set_scroll_down(bool on);
71  void set_scroll_left(bool on);
72  void set_scroll_right(bool on);
73 protected:
74  virtual bool is_browsing() const
75  { return false; }
76  /**
77  * Get a reference to a mouse handler member a derived class uses
78  */
80  /**
81  * Get a reference to a display member a derived class uses
82  */
83  virtual display& get_display() = 0;
84 
85  /**
86  * Get (optionally) a soundsources manager a derived class uses
87  */
88  virtual soundsource::manager * get_soundsource_man() { return nullptr; }
89 
90  /**
91  * Get (optionally) a plugins context a derived class uses
92  */
93  virtual plugins_context * get_plugins_context() { return nullptr; }
94 
95  /**
96  * Get (optionally) a command executor to handle context menu events
97  */
98  virtual hotkey::command_executor * get_hotkey_command_executor() { return nullptr; }
99 
100  /**
101  * Derived classes should override this to return false when arrow keys
102  * should not scroll the map, hotkeys not processed etc, for example
103  * when a textbox is active
104  * @returns true when arrow keys should scroll the map, false otherwise
105  */
106  virtual bool have_keyboard_focus();
107 
108  virtual std::vector<std::string> additional_actions_pressed() { return std::vector<std::string>(); }
109 
110 
111  /**
112  * Handle scrolling by keyboard, joystick and moving mouse near map edges
113  * @see scrolling_, which is set if the display is being scrolled
114  * @return true when there was any scrolling, false otherwise
115  */
116  bool handle_scroll(int mousex, int mousey, int mouse_flags, double joystickx, double joysticky);
117 
118  /**
119  * Process mouse- and keypress-events from SDL.
120  * Calls various virtual function to allow specialized
121  * behavior of derived classes.
122  */
123  void handle_event(const SDL_Event& event);
124 
125  void handle_window_event(const SDL_Event& ) {}
126 
127  /**
128  * Process keydown (only when the general map display does not have focus).
129  */
130  virtual void process_focus_keydown_event(const SDL_Event& event);
131 
132  /**
133  * Process keydown (always).
134  * Overridden in derived classes
135  */
136  virtual void process_keydown_event(const SDL_Event& event);
137 
138  /**
139  * Process keyup (always).
140  * Overridden in derived classes
141  */
142  virtual void process_keyup_event(const SDL_Event& event);
143 
144  virtual void show_menu(const std::vector<std::string>& items_arg, int xloc, int yloc, bool context_menu, display& disp);
145  virtual void execute_action(const std::vector<std::string>& items_arg, int xloc, int yloc, bool context_menu);
146 
147  virtual bool in_context_menu(hotkey::HOTKEY_COMMAND command) const;
148 
157 };
158 
159 
160 #endif
static const config & get_theme(const config &game_config, std::string theme_name)
joystick_manager joystick_manager_
virtual plugins_context * get_plugins_context()
Get (optionally) a plugins context a derived class uses.
virtual bool is_browsing() const
void set_scroll_up(bool on)
void set_scroll_down(bool on)
virtual void show_menu(const std::vector< std::string > &items_arg, int xloc, int yloc, bool context_menu, display &disp)
void handle_window_event(const SDL_Event &)
virtual void process_focus_keydown_event(const SDL_Event &event)
Process keydown (only when the general map display does not have focus).
Definition: video.hpp:58
void play_slice(bool is_delay_enabled=true)
virtual display & get_display()=0
Get a reference to a display member a derived class uses.
GLint GLint GLint GLint GLint GLint y
Definition: glew.h:1220
virtual void process_keydown_event(const SDL_Event &event)
Process keydown (always).
virtual bool have_keyboard_focus()
Derived classes should override this to return false when arrow keys should not scroll the map...
-file util.hpp
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.
bool handle_scroll(int mousex, int mousey, int mouse_flags, double joystickx, double joysticky)
Handle scrolling by keyboard, joystick and moving mouse near map edges.
void apply_keyboard_scroll(int x, int y)
virtual ~controller_base()
virtual events::mouse_handler_base & get_mouse_handler_base()=0
Get a reference to a mouse handler member a derived class uses.
virtual void execute_action(const std::vector< std::string > &items_arg, int xloc, int yloc, bool context_menu)
controller_base(const config &game_config, CVideo &video)
Game configuration data as global variables.
Definition: build_info.cpp:38
const config & game_config_
void set_scroll_right(bool on)
GLint GLint GLint GLint GLint x
Definition: glew.h:1220
Handling of system events.
Definition: manager.hpp:42
virtual hotkey::command_executor * get_hotkey_command_executor()
Get (optionally) a command executor to handle context menu events.
void set_scroll_left(bool on)
virtual void process_keyup_event(const SDL_Event &event)
Process keyup (always).
cl_event event
Definition: glew.h:3070
void handle_event(const SDL_Event &event)
Process mouse- and keypress-events from SDL.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
Class that keeps track of all the keys on the keyboard.
Definition: key.hpp:27
GLsizei const GLcharARB ** string
Definition: glew.h:4503
virtual bool in_context_menu(hotkey::HOTKEY_COMMAND command) const