The Battle for Wesnoth  1.17.0-dev
editor_controller.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2021
3  by Tomasz Sniatowski <kailoran@gmail.com>
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 #pragma once
17 
19 #include "editor/editor_main.hpp"
24 
25 #include "controller_base.hpp"
26 #include "help/help.hpp"
28 #include "mouse_handler_base.hpp"
29 #include "tooltips.hpp"
30 
31 #include "sound_music_track.hpp"
32 
33 class map_generator;
34 
35 namespace tooltips {
36 struct manager;
37 }
38 
39 namespace font {
41 }
42 
43 namespace editor {
44 
45 class editor_map;
46 
47 std::string get_left_button_function();
48 
49 enum menu_type {
50  MAP,
61 };
62 
63 /**
64  * The editor_controller class contains the mouse and keyboard event handling
65  * routines for the editor. It also serves as the main editor class with the
66  * general logic.
67  */
72 {
73  public:
74  editor_controller(const editor_controller&) = delete;
75  editor_controller& operator=(const editor_controller&) = delete;
76 
77  /**
78  * The constructor. A initial map context can be specified here, the controller
79  * will assume ownership and delete the pointer during destruction, but changes
80  * to the map can be retrieved between the main loop's end and the controller's
81  * destruction.
82  */
84 
86 
87  /** Editor main loop */
88  EXIT_STATUS main_loop();
89 
90  /** Takes a screenshot **/
91  void do_screenshot(const std::string& screenshot_filename = "map_screenshot.bmp");
92 
93  /** Process a hotkey quit command */
94  void hotkey_quit();
95 
96  /** Show a quit confirmation dialog and returns true if the user pressed 'yes' */
97  bool quit_confirm();
98 
99  /** Display the settings dialog, used to control e.g. the lighting settings */
100  void custom_tods_dialog();
101 
102  /** Save the map, open dialog if not named yet. */
103  void save_map() override {context_manager_->save_map();}
104 
105  /** command_executor override */
106  bool can_execute_command(const hotkey::hotkey_command& command, int index = -1) const override;
107 
108  /** command_executor override */
109  hotkey::ACTION_STATE get_action_state(hotkey::HOTKEY_COMMAND command, int index) const override;
110 
111  /** command_executor override */
112  bool do_execute_command(const hotkey::hotkey_command& command, int index = -1, bool press = true, bool release = false) override;
113 
114  /** controller_base override */
115  void show_menu(const std::vector<config>& items_arg, int xloc, int yloc, bool context_menu, display& disp) override;
116 
117  void show_help() override;
118  void status_table() override;
119 
120  /** Show the preferences dialog */
121  void preferences() override;
122 
123  /** Handle hotkeys to scroll map */
124  void scroll_up(bool on) override;
125  void scroll_down(bool on) override;
126  void scroll_left(bool on) override;
127  void scroll_right(bool on) override;
128 
129  /** Grid toggle */
130  void toggle_grid() override;
131 
132  void terrain_description() override;
133  void unit_description() override;
134  void change_unit_id();
135  void rename_unit() override;
136 
137  void unit_list() override;
138 
139  /** Copy the selection on the current map to the clipboard */
140  void copy_selection();
141 
142  /** Cut the selection from the current map to the clipboard */
143  void cut_selection();
144 
145  /** Export the WML-compatible list of selected tiles to the system clipboard */
146  void export_selection_coords();
147 
148  void update_mouse_action_highlights();
149 
150  /** Save the current selection to the active area. */
151  void save_area();
152 
153  /** Add a new area to the current context, filled with the selection if any. */
154  void add_area();
155 
156  /* mouse_handler_base overrides */
157  void mouse_motion(int x, int y, const bool browse, bool update, map_location new_loc = map_location::null_location()) override;
158  void touch_motion(int x, int y, const bool browse, bool update=false, map_location new_loc = map_location::null_location()) override;
159  editor_display& gui() override { return *gui_; }
160  const editor_display& gui() const override { return *gui_; }
161  bool allow_mouse_wheel_scroll(int x, int y) override;
162  bool right_click_show_menu(int x, int y, const bool browse) override;
163  bool left_click(int x, int y, const bool browse) override;
164  void left_drag_end(int x, int y, const bool browse) override;
165  void left_mouse_up(int x, int y, const bool browse) override;
166  bool right_click(int x, int y, const bool browse) override;
167  void right_drag_end(int x, int y, const bool browse) override;
168  void right_mouse_up(int x, int y, const bool browse) override;
169 
170  virtual hotkey::command_executor * get_hotkey_command_executor() override;
171 
173  {
174  return context_manager_->get_map_context();
175  }
176 
177  protected:
178  /* controller_base overrides */
179  void process_keyup_event(const SDL_Event& event) override;
180  mouse_handler_base& get_mouse_handler_base() override { return *this; }
181  editor_display& get_display() override { return *gui_; }
182 
183  /** Get the current mouse action */
184  const mouse_action& get_mouse_action() const { return toolkit_->get_mouse_action(); }
185  /** Get the current mouse action */
186  mouse_action& get_mouse_action() { return toolkit_->get_mouse_action(); }
187 
188  /**
189  * Perform an action, then delete the action object.
190  * The pointer can be nullptr, in which case nothing will happen.
191  */
192  void perform_delete(std::unique_ptr<editor_action> action);
193 
194  /**
195  * Peform an action on the current map_context, then refresh the display
196  * and delete the pointer. The pointer can be nullptr, in which case nothing will happen.
197  */
198  void perform_refresh_delete(std::unique_ptr<editor_action> action, bool drag_part = false);
199 
200 
201  virtual std::vector<std::string> additional_actions_pressed() override;
202 
203  private:
204 
205  /** init the display object and general set-up */
206  void init_gui();
207 
208  /** init the available time-of-day settings */
209  void init_tods(const game_config_view& game_config);
210 
211  /** init background music for the editor */
212  void init_music(const game_config_view& game_config);
213 
214  /** Load editor-specific tooltips */
215  void load_tooltips();
216 
217  /** Reload images */
218  void refresh_image_cache();
219 
220  /**
221  * Callback function passed to display to be called on each redraw_everything run.
222  * Redraws toolbar, brush bar and related items.
223  */
224  void display_redraw_callback(display&);
225 
226  /**
227  * Undos an action in the current map context
228  */
229  void undo() override;
230 
231  /**
232  * Redos an action in the current map context
233  */
234  void redo() override;
235 
237 
238  /** Reports object. Must be initialized before the gui_ */
239  const std::unique_ptr<reports> reports_;
240 
241  /** The display object used and owned by the editor. */
242  const std::unique_ptr<editor_display> gui_;
243 
244  /** Pre-defined time of day lighting settings for the settings dialog */
245  typedef std::map<std::string, std::pair<std::string ,std::vector<time_of_day>>> tods_map;
246  tods_map tods_;
247 
248  /* managers */
249  public:
250  const std::unique_ptr<context_manager> context_manager_;
251  private:
252  std::unique_ptr<editor_toolkit> toolkit_;
254  std::unique_ptr<font::floating_label_context> floating_label_manager_;
255 
256  std::unique_ptr<help::help_manager> help_manager_;
257 
258  /** Quit main loop flag */
259  bool do_quit_;
261 
262  std::vector<sound::music_track> music_tracks_;
263 };
264 
265 } //end namespace editor
void show_help(const std::string &show_topic, int xloc, int yloc)
Open the help browser, show topic with id show_topic.
Definition: help.cpp:144
std::unique_ptr< font::floating_label_context > floating_label_manager_
tooltips::manager tooltip_manager_
void save_map() override
Save the map, open dialog if not named yet.
Collection of helper functions relating to Pango formatting.
std::string get_left_button_function()
std::unique_ptr< editor_toolkit > toolkit_
editor_display & gui() override
Reference to the used display objects.
Stores all information related to functions that can be bound to hotkeys.
const mouse_action & get_mouse_action() const
Get the current mouse action.
editor_display & get_display() override
Get a reference to a display member a derived class uses.
controller_base framework: controller_base is roughly analogous to a "dialog" class in a GUI toolkit ...
The editor_controller class contains the mouse and keyboard event handling routines for the editor...
const std::unique_ptr< editor_display > gui_
The display object used and owned by the editor.
map_context & get_current_map_context() const
Implements a quit confirmation dialog.
Modify, read and display user preferences.
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
Manage the empty-palette in the editor.
Definition: action.cpp:30
Encapsulates the map of the game.
Definition: location.hpp:38
tooltips.
Game configuration data as global variables.
Definition: build_info.cpp:59
structure which will hide all current floating labels, and cause floating labels instantiated after i...
This class wraps around a map to provide a concise interface for the editor to work with...
Definition: map_context.hpp:60
std::vector< sound::music_track > music_tracks_
std::size_t index(const std::string &str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
Definition: unicode.cpp:72
std::map< std::string, std::pair< std::string,std::vector< time_of_day > > > tods_map
Pre-defined time of day lighting settings for the settings dialog.
mouse_handler_base & get_mouse_handler_base() override
Get a reference to a mouse handler member a derived class uses.
bool do_quit_
Quit main loop flag.
std::unique_ptr< help::help_manager > help_manager_
const editor_display & gui() const override
Const version of gui.
static const map_location & null_location()
Definition: location.hpp:81
A mouse action receives events from the controller, and responds to them by creating an appropriate e...
const std::unique_ptr< context_manager > context_manager_
const std::unique_ptr< reports > reports_
Reports object.
mouse_action & get_mouse_action()
Get the current mouse action.