The Battle for Wesnoth  1.19.0-dev
action_select.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2024
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 
16 /**
17  * @file
18  * Editor label action classes
19  */
20 #define GETTEXT_DOMAIN "wesnoth-editor"
21 
24 
25 namespace editor
26 {
27 IMPLEMENT_ACTION(select)
28 
29 void editor_action_select::extend(const editor_map& /*map*/, const std::set<map_location>& locs)
30 {
31  for(const map_location& loc : locs) {
32  LOG_ED << "Extending by " << loc;
33  area_.insert(loc);
34  }
35 }
36 
37 std::unique_ptr<editor_action> editor_action_select::perform(map_context& mc) const
38 {
39  std::set<map_location> undo_locs;
40  for(const map_location& loc : area_) {
41  undo_locs.insert(loc);
42  mc.add_changed_location(loc);
43  }
44 
46  return std::make_unique<editor_action_select>(undo_locs);
47 }
48 
50 {
51  for(const map_location& loc : area_) {
52  mc.map().add_to_selection(loc);
53  mc.add_changed_location(loc);
54  }
55 }
56 
57 IMPLEMENT_ACTION(deselect)
58 
59 void editor_action_deselect::extend(const editor_map& map, const std::set<map_location>& locs)
60 {
61  for(const map_location& loc : locs) {
62  LOG_ED << "Checking " << loc;
63  if(!map.in_selection(loc)) {
64  LOG_ED << "Extending by " << loc;
65  area_.insert(loc);
66  }
67  }
68 }
69 
70 std::unique_ptr<editor_action> editor_action_deselect::perform(map_context& mc) const
71 {
72  std::set<map_location> undo_locs;
73  for(const map_location& loc : area_) {
74  if(mc.map().in_selection(loc)) {
75  undo_locs.insert(loc);
76  mc.add_changed_location(loc);
77  }
78  }
79 
81  return std::make_unique<editor_action_select>(undo_locs);
82 }
83 
85 {
86  for(const map_location& loc : area_) {
87  mc.map().remove_from_selection(loc);
88  mc.add_changed_location(loc);
89  }
90 }
91 
92 IMPLEMENT_ACTION(select_all)
93 
94 std::unique_ptr<editor_action> editor_action_select_all::perform(map_context& mc) const
95 {
96  std::set<map_location> current = mc.map().selection();
97  mc.map().select_all();
98 
99  std::set<map_location> all = mc.map().selection();
100  std::set<map_location> undo_locs;
101 
102  std::set_difference(
103  all.begin(), all.end(), current.begin(), current.end(), std::inserter(undo_locs, undo_locs.begin()));
104 
105  mc.set_everything_changed();
106  return std::make_unique<editor_action_select>(undo_locs);
107 }
108 
110 {
111  mc.map().select_all();
113 }
114 
115 IMPLEMENT_ACTION(select_none)
116 
117 std::unique_ptr<editor_action> editor_action_select_none::perform(map_context& mc) const
118 {
119  std::set<map_location> current = mc.map().selection();
120  mc.map().clear_selection();
121  mc.set_everything_changed();
122  return std::make_unique<editor_action_select>(current);
123 }
124 
126 {
127  mc.map().clear_selection();
129 }
130 
131 IMPLEMENT_ACTION(select_inverse)
132 
133 std::unique_ptr<editor_action> editor_action_select_inverse::perform(map_context& mc) const
134 {
135  perform_without_undo(mc);
136  return std::make_unique<editor_action_select_inverse>();
137 }
138 
140 {
141  mc.map().invert_selection();
143 }
144 
145 } // end namespace editor
#define IMPLEMENT_ACTION(id)
Helper macro to implement common action methods.
Editor action classes.
std::set< map_location > area_
Definition: action.hpp:236
Deselect the given locations.
std::unique_ptr< editor_action > perform(map_context &mc) const override
Perform the action, returning an undo action that, when performed, shall reverse any effects of this ...
void perform_without_undo(map_context &mc) const override
Perform the action without creating an undo action.
void perform_without_undo(map_context &mc) const override
Perform the action without creating an undo action.
void perform_without_undo(map_context &mc) const override
Perform the action without creating an undo action.
void perform_without_undo(map_context &mc) const override
Perform the action without creating an undo action.
Select the given locations.
std::unique_ptr< editor_action > perform(map_context &mc) const override
Perform the action, returning an undo action that, when performed, shall reverse any effects of this ...
void perform_without_undo(map_context &mc) const override
Perform the action without creating an undo action.
Base class for all editor actions.
Definition: action_base.hpp:42
This class adds extra editor-specific functionality to a normal gamemap.
Definition: editor_map.hpp:70
bool add_to_selection(const map_location &loc)
Add a location to the selection.
Definition: editor_map.cpp:163
void invert_selection()
Invert the selection, i.e.
Definition: editor_map.cpp:188
void select_all()
Select all map hexes.
Definition: editor_map.cpp:201
bool in_selection(const map_location &loc) const
Definition: editor_map.cpp:158
bool remove_from_selection(const map_location &loc)
Remove a location to the selection.
Definition: editor_map.cpp:178
void clear_selection()
Clear the selection.
Definition: editor_map.cpp:183
This class wraps around a map to provide a concise interface for the editor to work with.
Definition: map_context.hpp:63
virtual const editor_map & map() const override
Const map accessor.
void add_changed_location(const map_location &loc)
#define LOG_ED
void set(CURSOR_TYPE type)
Use the default parameter to reset cursors.
Definition: cursor.cpp:176
Manage the empty-palette in the editor.
Definition: action.cpp:31
Encapsulates the map of the game.
Definition: location.hpp:38