The Battle for Wesnoth  1.19.13+dev
editor_toolkit.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2012 - 2025
3  by Fabian Mueller <fabianmueller5@gmx.de>
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 
17 #include "config.hpp"
18 
25 
26 #include "game_config_view.hpp"
27 #include "sdl/input.hpp" // get_mouse_state
28 
29 namespace editor {
30 
32  : gui_(gui)
33  , key_()
34  , palette_manager_()
35  , mouse_action_(nullptr) // Will be set before this constructor ends.
36  , mouse_actions_()
37  , brush_(nullptr)
38  , brushes_()
39 {
42  init_mouse_actions(c_manager);
43 }
44 
46 
48 {
49  for (const config &i : game_config.child_range("brush")) {
50  brushes_.emplace_back(i);
51  }
52  if (brushes_.empty()) {
53  ERR_ED << "No brushes defined!";
54  brushes_.emplace_back();
55  brushes_[0].add_relative_location(0, 0);
56  }
57  brush_ = &brushes_[0];
58 }
59 
61 {
63 }
64 
66 {
68  std::make_shared<mouse_action_paint>(&brush_, key_, *palette_manager_->terrain_palette_.get()));
70  std::make_shared<mouse_action_fill>(key_, *palette_manager_->terrain_palette_.get()));
72  std::make_shared<mouse_action_select>(&brush_, key_, *palette_manager_->empty_palette_.get()));
74  std::make_shared<mouse_action_starting_position>(key_, *palette_manager_->location_palette_.get()));
76  std::make_shared<mouse_action_map_label>(key_, *palette_manager_->empty_palette_.get()));
78  std::make_shared<mouse_action_unit>(key_, *palette_manager_->unit_palette_.get()));
80  std::make_shared<mouse_action_village>(key_, *palette_manager_->empty_palette_.get()));
82  std::make_shared<mouse_action_paste>(cmanager.get_clipboard(), key_, *palette_manager_->empty_palette_.get()));
84  std::make_shared<mouse_action_item>(key_, *palette_manager_->item_palette_.get()));
85 
86  for (const theme::menu& menu : gui_.get_theme().menus()) {
87  if (menu.items().size() == 1) {
88  hotkey::HOTKEY_COMMAND hk = hotkey::get_hotkey_command(menu.items().front()["id"].str()).command;
90  if (i != mouse_actions_.end()) {
91  i->second->set_toolbar_button(&menu);
92  }
93  }
94  }
95 
98 }
99 
100 
102 {
104  if (i != mouse_actions_.end()) {
105  palette_manager_->active_palette().hide(true);
106  mouse_action_ = i->second;
107  palette_manager_->adjust_size();
108 
111  palette_manager_->active_palette().hide(false);
112  } else {
113  ERR_ED << "Invalid hotkey command ("
114  << static_cast<int>(command) << ") passed to set_mouse_action";
115  }
116 
117 }
118 
120 {
121  mouse_action_map::const_iterator i = mouse_actions_.find(command);
122  return (i != mouse_actions_.end()) && (i->second == mouse_action_);
123 }
124 
126 {
127  return get_mouse_action().get_palette();
128 }
129 
131 {
132  DBG_ED << __func__;
133  auto [x, y] = sdl::get_mouse_location();
134  map_location hex_clicked = gui_.hex_clicked_on(x,y);
136 }
137 
139 {
141 }
142 
144 {
146 }
147 
148 void editor_toolkit::set_brush(const std::string& id) {
149 
150  for (brush& i : brushes_) {
151  if (i.id() == id) {
152  brush_ = &i;
153  }
154  }
155 }
156 
158 {
159  if (brush_ == &brushes_.back()) {
160  brush_ = &brushes_.front();
161  } else {
162  ++brush_;
163  }
164 
166 }
167 
169 {
170  palette_manager_->adjust_size();
171 }
172 
173 
174 } //Namespace editor
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:158
void invalidate_game_status()
Function to invalidate the game status displayed on the sidebar.
Definition: display.hpp:307
theme & get_theme()
Definition: display.hpp:381
map_location hex_clicked_on(int x, int y) const
given x,y co-ordinates of an onscreen pixel, will return the location of the hex that this pixel corr...
Definition: display.cpp:527
The brush class represents a single brush – a set of relative locations around a "hotspot",...
Definition: brush.hpp:27
map_fragment & get_clipboard()
brush * brush_
The current brush.
void cycle_brush()
Cycle to the next brush.
mouse_action & get_mouse_action()
Get the current mouse action.
std::vector< brush > brushes_
All available brushes.
editor_toolkit(editor_display &gui, const game_config_view &game_config, context_manager &c_manager)
bool is_mouse_action_set(hotkey::HOTKEY_COMMAND command) const
std::shared_ptr< mouse_action > mouse_action_
The current mouse action.
void init_sidebar(const game_config_view &game_config)
init the sidebar objects
std::unique_ptr< palette_manager > palette_manager_
editor_display & gui_
mouse_action_map mouse_actions_
void hotkey_set_mouse_action(hotkey::HOTKEY_COMMAND command)
Set the current mouse action based on a hotkey id.
void init_brushes(const game_config_view &game_config)
init the brushes
common_palette & get_palette()
Get the current palette.
void init_mouse_actions(context_manager &c_manager)
init the mouse actions (tools)
void set_brush(const std::string &id)
void update_brush_highlights(editor_display &disp, const map_location &hex)
Unconditionally update the brush highlights for the current tool when hex is the center location.
common_palette & get_palette()
Getter for the associated palette.
virtual void set_mouse_overlay(editor_display &disp)
Set the mouse overlay for this action.
A class grating read only view to a vector of config objects, viewed as one config with all children ...
const std::vector< menu > & menus() const
Definition: theme.hpp:256
Definitions for the interface to Wesnoth Markup Language (WML).
#define ERR_ED
#define DBG_ED
std::size_t i
Definition: function.cpp:1032
Contains functions for cleanly handling SDL input.
Manage the empty-palette in the editor.
Definition: action.cpp:31
Game configuration data as global variables.
Definition: build_info.cpp:61
General purpose widgets.
const hotkey_command & get_hotkey_command(std::string_view command)
Returns the hotkey_command with the given id.
@ HOTKEY_EDITOR_TOOL_VILLAGE
@ HOTKEY_EDITOR_TOOL_LABEL
@ HOTKEY_EDITOR_CLIPBOARD_PASTE
@ HOTKEY_EDITOR_TOOL_PAINT
@ HOTKEY_EDITOR_TOOL_FILL
@ HOTKEY_EDITOR_TOOL_UNIT
@ HOTKEY_EDITOR_TOOL_STARTING_POSITION
@ HOTKEY_EDITOR_TOOL_SELECT
@ HOTKEY_EDITOR_TOOL_ITEM
point get_mouse_location()
Returns the current mouse location in draw space.
Definition: input.cpp:54
std::string::const_iterator iterator
Definition: tokenizer.hpp:25
HOTKEY_COMMAND command
The command associated with this hotkey.
Encapsulates the map of the game.
Definition: location.hpp:45