The Battle for Wesnoth  1.17.4+dev
editor_toolkit.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2012 - 2022
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  const game_config_view& game_config, context_manager& c_manager)
33  : gui_(gui)
34  , key_(key)
35  , palette_manager_()
36  , mouse_action_(nullptr) // Will be set before this constructor ends.
37  , mouse_actions_()
38  , brush_(nullptr)
39  , brushes_()
40 {
41  init_brushes(game_config);
42  init_sidebar(game_config);
43  init_mouse_actions(c_manager);
44 }
45 
47 
49 {
50  for (const config &i : game_config.child_range("brush")) {
51  brushes_.emplace_back(i);
52  }
53  if (brushes_.empty()) {
54  ERR_ED << "No brushes defined!";
55  brushes_.emplace_back();
56  brushes_[0].add_relative_location(0, 0);
57  }
58  brush_ = &brushes_[0];
59 }
60 
62 {
63  palette_manager_.reset(new palette_manager(gui_, game_config, *this));
64 }
65 
67 {
69  std::make_shared<mouse_action_paint>(&brush_, key_, *palette_manager_->terrain_palette_.get()));
71  std::make_shared<mouse_action_fill>(key_, *palette_manager_->terrain_palette_.get()));
73  std::make_shared<mouse_action_select>(&brush_, key_, *palette_manager_->empty_palette_.get()));
75  std::make_shared<mouse_action_starting_position>(key_, *palette_manager_->location_palette_.get()));
77  std::make_shared<mouse_action_map_label>(key_, *palette_manager_->empty_palette_.get()));
79  std::make_shared<mouse_action_unit>(key_, *palette_manager_->unit_palette_.get()));
81  std::make_shared<mouse_action_village>(key_, *palette_manager_->empty_palette_.get()));
83  std::make_shared<mouse_action_paste>(cmanager.get_clipboard(), key_, *palette_manager_->empty_palette_.get()));
85  std::make_shared<mouse_action_item>(key_, *palette_manager_->item_palette_.get()));
86 
87  for (const theme::menu& menu : gui_.get_theme().menus()) {
88  if (menu.items().size() == 1) {
89  hotkey::HOTKEY_COMMAND hk = hotkey::get_id(menu.items().front()["id"]);
91  if (i != mouse_actions_.end()) {
92  i->second->set_toolbar_button(&menu);
93  }
94  }
95  }
96 
99 }
100 
101 
103 {
105  if (i != mouse_actions_.end()) {
106  palette_manager_->active_palette().hide(true);
107  mouse_action_ = i->second;
108  palette_manager_->adjust_size();
109 
112  palette_manager_->active_palette().hide(false);
113  } else {
114  ERR_ED << "Invalid hotkey command ("
115  << static_cast<int>(command) << ") passed to set_mouse_action\n";
116  }
117 
118 }
119 
121 {
122  mouse_action_map::const_iterator i = mouse_actions_.find(command);
123  return (i != mouse_actions_.end()) && (i->second == mouse_action_);
124 }
125 
127 {
128  return get_mouse_action().get_palette();
129 }
130 
132 {
133  DBG_ED << __func__ << "\n";
134  int x, y;
135  sdl::get_mouse_state(&x, &y);
136  map_location hex_clicked = gui_.hex_clicked_on(x,y);
138 }
139 
141 {
143 }
144 
146 {
148 }
149 
150 void editor_toolkit::set_brush(std::string id) {
151 
152  for (brush& i : brushes_) {
153  if (i.id() == id) {
154  brush_ = &i;
155  }
156  }
157 }
158 
160 {
161  if (brush_ == &brushes_.back()) {
162  brush_ = &brushes_.front();
163  } else {
164  ++brush_;
165  }
166 
168 }
169 
171 {
172  palette_manager_->adjust_size();
173 }
174 
175 
176 } //Namespace editor
editor_display & gui_
void invalidate_game_status()
Function to invalidate the game status displayed on the sidebar.
Definition: display.hpp:297
theme & get_theme()
Definition: display.hpp:379
void hotkey_set_mouse_action(hotkey::HOTKEY_COMMAND command)
Set the current mouse action based on a hotkey id.
const 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:588
mouse_action_map mouse_actions_
void cycle_brush()
Cycle to the next brush.
config_array_view child_range(config_key_type key) const
std::shared_ptr< mouse_action > mouse_action_
The current mouse action.
General purpose widgets.
editor_toolkit(editor_display &gui, const CKey &key, const game_config_view &game_config, context_manager &c_manager)
void clear_mouseover_hex_overlay()
Definition: display.hpp:445
Definitions for the interface to Wesnoth Markup Language (WML).
brush * brush_
The current brush.
Contains functions for cleanly handling SDL input.
void set_brush(std::string id)
TODO.
void init_sidebar(const game_config_view &game_config)
init the sidebar objects
common_palette & get_palette()
Get the current palette.
std::vector< brush > brushes_
All available brushes.
common_palette & get_palette()
Getter for the associated palette.
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...
The brush class represents a single brush – a set of relative locations around a "hotspot"...
Definition: brush.hpp:26
void init_brushes(const game_config_view &game_config)
init the brushes
std::unique_ptr< palette_manager > palette_manager_
Manage the empty-palette in the editor.
Definition: action.cpp:30
map_fragment & get_clipboard()
Encapsulates the map of the game.
Definition: location.hpp:38
void init_mouse_actions(context_manager &c_manager)
init the mouse actions (tools)
std::string id
Text to match against addon_info.tags()
Definition: manager.cpp:215
std::size_t i
Definition: function.cpp:967
Game configuration data as global variables.
Definition: build_info.cpp:60
mouse_action & get_mouse_action()
Get the current mouse action.
virtual void set_mouse_overlay(editor_display &disp)
Set the mouse overlay for this action.
#define ERR_ED
uint32_t get_mouse_state(int *x, int *y)
A wrapper for SDL_GetMouseState that gives coordinates in draw space.
Definition: input.cpp:29
bool is_mouse_action_set(hotkey::HOTKEY_COMMAND command) const
const std::vector< menu > & menus() const
Definition: theme.hpp:255
#define DBG_ED
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:60
Class that keeps track of all the keys on the keyboard.
Definition: key.hpp:28
std::string::const_iterator iterator
Definition: tokenizer.hpp:25
HOTKEY_COMMAND get_id(const std::string &command)
returns get_hotkey_command(command).id