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