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