The Battle for Wesnoth  1.19.13+dev
editor_controller.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2025
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"
22 
23 #include "controller_base.hpp"
24 #include "help/help.hpp"
26 #include "mouse_handler_base.hpp"
27 #include "tooltips.hpp"
28 
29 #include "sound_music_track.hpp"
30 
31 namespace font {
33 }
34 
35 namespace editor {
36 
37 /**
38  * The editor_controller class contains the mouse and keyboard event handling
39  * routines for the editor. It also serves as the main editor class with the
40  * general logic.
41  */
46 {
47  public:
50 
51  /**
52  * The constructor.
53  */
54  editor_controller(bool clear_id);
55 
57 
58  /** Editor main loop */
60 
61  /** Takes a screenshot **/
62  void do_screenshot(const std::string& screenshot_filename = "map_screenshot.png");
63 
64  /** Show a quit confirmation dialog and returns true if the user pressed 'yes' */
65  bool quit_confirm();
66 
67  /** Show Unit Editor dialog */
68  void unit_editor_dialog();
69 
70  /** Display the settings dialog, used to control e.g. the lighting settings */
71  void custom_tods_dialog();
72 
73  /** Updates schedule and the map display */
74  void update_map_schedule(const std::vector<time_of_day>& schedule);
75 
76  /** Save the map, open dialog if not named yet. */
77  void save_map() override {context_manager_->save_map();}
78 
79  /** command_executor override */
80  bool can_execute_command(const hotkey::ui_command& command) const override;
81 
82  /** command_executor override */
83  hotkey::action_state get_action_state(const hotkey::ui_command& command) const override;
84 
85  /** command_executor override */
86  bool do_execute_command(const hotkey::ui_command& command, bool press = true, bool release = false) override;
87 
88  /** command_executor override */
89  bool keep_menu_open() const override;
90 
91  /** controller_base override */
92  void show_menu(const std::vector<config>& items_arg, int xloc, int yloc, bool context_menu) override;
93 
94  void show_help() override;
95  void status_table() override;
96 
97  /** Show the preferences dialog */
98  void preferences() override;
99 
100  /** Handle hotkeys to scroll map */
101  void scroll_up(bool on) override;
102  void scroll_down(bool on) override;
103  void scroll_left(bool on) override;
104  void scroll_right(bool on) override;
105 
106  /** Grid toggle */
107  void toggle_grid() override;
108 
109  void terrain_description() override;
110  void unit_description() override;
111  void change_unit_id();
112  void rename_unit() override;
113 
114  void unit_list() override;
115 
116  /** Copy the selection on the current map to the clipboard */
117  void copy_selection();
118 
119  /** Cut the selection from the current map to the clipboard */
120  void cut_selection();
121 
122  /** Export the WML-compatible list of selected tiles to the system clipboard */
124 
125  /** Save the current selection to the active area. */
126  void save_area();
127 
128  /** Add a new area to the current context, filled with the selection if any. */
129  void add_area();
130 
131  /* mouse_handler_base overrides */
132  void mouse_motion(int x, int y, const bool browse, bool update, map_location new_loc = map_location::null_location()) override;
133  void touch_motion(int x, int y, const bool browse, bool update=false, map_location new_loc = map_location::null_location()) override;
134  editor_display& gui() override { return *gui_; }
135  const editor_display& gui() const override { return *gui_; }
136  bool allow_mouse_wheel_scroll(int x, int y) override;
137  bool right_click_show_menu(int x, int y, const bool browse) override;
138  bool left_click(int x, int y, const bool browse) override;
139  void left_drag_end(int x, int y, const bool browse) override;
140  void left_mouse_up(int x, int y, const bool browse) override;
141  bool right_click(int x, int y, const bool browse) override;
142  void right_drag_end(int x, int y, const bool browse) override;
143  void right_mouse_up(int x, int y, const bool browse) override;
144 
146 
148  {
149  return context_manager_->get_map_context();
150  }
151 
152  /** Initialize an addon if the addon id is empty
153  * @return If the initialization succeeded.
154  * */
155  bool initialize_addon();
156 
157  protected:
158  /* controller_base overrides */
159  void process_keyup_event(const SDL_Event& event) override;
160  mouse_handler_base& get_mouse_handler_base() override { return *this; }
161  editor_display& get_display() override { return *gui_; }
162 
163  /** Get the current mouse action */
164  const mouse_action& get_mouse_action() const { return toolkit_->get_mouse_action(); }
165  /** Get the current mouse action */
166  mouse_action& get_mouse_action() { return toolkit_->get_mouse_action(); }
167 
168  /**
169  * Perform an action, then delete the action object.
170  * The pointer can be nullptr, in which case nothing will happen.
171  */
172  void perform_delete(std::unique_ptr<editor_action> action);
173 
174  /**
175  * Peform an action on the current map_context, then refresh the display
176  * and delete the pointer. The pointer can be nullptr, in which case nothing will happen.
177  */
178  void perform_refresh_delete(std::unique_ptr<editor_action> action, bool drag_part = false);
179 
180 
181  virtual std::vector<std::string> additional_actions_pressed() override;
182 
183  private:
184  enum class menu_type {
185  map,
186  load_mru,
187  palette,
188  area,
189  addon,
190  side,
191  time,
192  local_time,
193  schedule,
195  music,
196  unit_facing,
197  none
198  };
199 
200  /** init the display object and general set-up */
201  void init_gui();
202 
203  /** init the available time-of-day settings */
205 
206  /** init background music for the editor */
208 
209  /** Reload images */
210  void refresh_image_cache();
211 
212  /**
213  * Callback function passed to display to be called on queue_rerender.
214  * Redraws toolbar, brush bar and related items.
215  */
217 
218  /**
219  * Undos an action in the current map context
220  */
221  void undo() override;
222 
223  /**
224  * Redos an action in the current map context
225  */
226  void redo() override;
227 
228  /** The currently invoked dropdown menu. Outside of show_menu, this will be menu_type::none. */
230 
231  /** Reports object. Must be initialized before the gui_ */
232  const std::unique_ptr<reports> reports_;
233 
234  /** The display object used and owned by the editor. */
235  const std::unique_ptr<editor_display> gui_;
236 
237  /** Pre-defined time of day lighting settings for the settings dialog */
238  typedef std::map<std::string, std::pair<std::string ,std::vector<time_of_day>>> tods_map;
240 
241  /* managers */
242  public:
243  const std::unique_ptr<context_manager> context_manager_;
244 
245  static std::string current_addon_id_;
246  private:
247  std::unique_ptr<editor_toolkit> toolkit_;
249  std::unique_ptr<font::floating_label_context> floating_label_manager_;
250 
251  std::unique_ptr<help::help_manager> help_manager_;
252 
253  /** Quit main loop flag */
254  bool do_quit_;
256 
257  std::vector<sound::music_track> music_tracks_;
258 };
259 
260 } //end namespace editor
Sort-of-Singleton that many classes, both GUI and non-GUI, use to access the game data.
Definition: display.hpp:88
The editor_controller class contains the mouse and keyboard event handling routines for the editor.
const std::unique_ptr< context_manager > context_manager_
void preferences() override
Show the preferences dialog.
void refresh_image_cache()
Reload images.
void right_drag_end(int x, int y, const bool browse) override
Called whenever the right mouse drag has "ended".
void unit_editor_dialog()
Show Unit Editor dialog.
bool can_execute_command(const hotkey::ui_command &command) const override
command_executor override
bool quit_confirm()
Show a quit confirmation dialog and returns true if the user pressed 'yes'.
tooltips::manager tooltip_manager_
void update_map_schedule(const std::vector< time_of_day > &schedule)
Updates schedule and the map display.
void scroll_up(bool on) override
Handle hotkeys to scroll map.
void cut_selection()
Cut the selection from the current map to the clipboard.
bool keep_menu_open() const override
command_executor override
void display_redraw_callback(display &)
Callback function passed to display to be called on queue_rerender.
std::unique_ptr< font::floating_label_context > floating_label_manager_
menu_type active_menu_
The currently invoked dropdown menu.
void undo() override
Undos an action in the current map context.
void left_mouse_up(int x, int y, const bool browse) override
Called when the left mouse button is up.
bool left_click(int x, int y, const bool browse) override
Overridden in derived classes, called on a left click (mousedown).
mouse_handler_base & get_mouse_handler_base() override
Get a reference to a mouse handler member a derived class uses.
bool allow_mouse_wheel_scroll(int x, int y) override
Derived classes can override this to disable mousewheel scrolling under some circumstances,...
void touch_motion(int x, int y, const bool browse, bool update=false, map_location new_loc=map_location::null_location()) override
virtual std::vector< std::string > additional_actions_pressed() override
void perform_refresh_delete(std::unique_ptr< editor_action > action, bool drag_part=false)
Peform an action on the current map_context, then refresh the display and delete the pointer.
void init_tods(const game_config_view &game_config)
init the available time-of-day settings
bool initialize_addon()
Initialize an addon if the addon id is empty.
const mouse_action & get_mouse_action() const
Get the current mouse action.
map_context & get_current_map_context() const
std::unique_ptr< editor_toolkit > toolkit_
void scroll_right(bool on) override
void init_music(const game_config_view &game_config)
init background music for the editor
editor_controller & operator=(const editor_controller &)=delete
const std::unique_ptr< editor_display > gui_
The display object used and owned by the editor.
bool do_execute_command(const hotkey::ui_command &command, bool press=true, bool release=false) override
command_executor override
void export_selection_coords()
Export the WML-compatible list of selected tiles to the system clipboard.
void redo() override
Redos an action in the current map context.
editor_display & gui() override
Reference to the used display objects.
void show_menu(const std::vector< config > &items_arg, int xloc, int yloc, bool context_menu) override
controller_base override
void perform_delete(std::unique_ptr< editor_action > action)
Perform an action, then delete the action object.
void right_mouse_up(int x, int y, const bool browse) override
Called when the right mouse button is up.
virtual hotkey::command_executor * get_hotkey_command_executor() override
Optionally get a command executor to handle context menu events.
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.
void mouse_motion(int x, int y, const bool browse, bool update, map_location new_loc=map_location::null_location()) override
Called when a mouse motion event takes place.
EXIT_STATUS main_loop()
Editor main loop.
editor_controller(const editor_controller &)=delete
void custom_tods_dialog()
Display the settings dialog, used to control e.g.
void do_screenshot(const std::string &screenshot_filename="map_screenshot.png")
Takes a screenshot.
void toggle_grid() override
Grid toggle.
void scroll_left(bool on) override
bool right_click(int x, int y, const bool browse) override
Overridden in derived classes, called on a right click (mousedown).
void save_area()
Save the current selection to the active area.
void process_keyup_event(const SDL_Event &event) override
Process keyup (always).
editor_display & get_display() override
Get a reference to a display member a derived class uses.
void copy_selection()
Copy the selection on the current map to the clipboard.
mouse_action & get_mouse_action()
Get the current mouse action.
void init_gui()
init the display object and general set-up
hotkey::action_state get_action_state(const hotkey::ui_command &command) const override
command_executor override
std::unique_ptr< help::help_manager > help_manager_
std::vector< sound::music_track > music_tracks_
bool right_click_show_menu(int x, int y, const bool browse) override
Called in the default right_click when the context menu is about to be shown, can be used for preproc...
static std::string current_addon_id_
void add_area()
Add a new area to the current context, filled with the selection if any.
const std::unique_ptr< reports > reports_
Reports object.
void scroll_down(bool on) override
void left_drag_end(int x, int y, const bool browse) override
Called whenever the left mouse drag has "ended".
bool do_quit_
Quit main loop flag.
const editor_display & gui() const override
Const version of gui.
void save_map() override
Save the map, open dialog if not named yet.
This class wraps around a map to provide a concise interface for the editor to work with.
Definition: map_context.hpp:63
A mouse action receives events from the controller, and responds to them by creating an appropriate e...
A class grating read only view to a vector of config objects, viewed as one config with all children ...
Implements a quit confirmation dialog.
controller_base framework: controller_base is roughly analogous to a "dialog" class in a GUI toolkit ...
static void update()
Manage the empty-palette in the editor.
Definition: action.cpp:31
Graphical text output.
Game configuration data as global variables.
Definition: build_info.cpp:61
structure which will hide all current floating labels, and cause floating labels instantiated after i...
Used as the main parameter for can_execute_command/do_execute_command These functions are used to exe...
Encapsulates the map of the game.
Definition: location.hpp:45
static const map_location & null_location()
Definition: location.hpp:102