The Battle for Wesnoth  1.19.7+dev
mouse_action_item.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 #define GETTEXT_DOMAIN "wesnoth-editor"
17 
20 
22 #include "gettext.hpp"
23 
24 #include "map/location.hpp"
25 
26 namespace editor {
27 
28 
30 {
31  if (hex == previous_move_hex_) {
32  return;
33  }
34 
35  update_brush_highlights(disp, hex);
36 
37  std::set<map_location> adjacent_set;
39  adjacent_set.insert(adj);
40  }
41 
42  disp.invalidate(adjacent_set);
43  previous_move_hex_ = hex;
44 }
45 
46 std::unique_ptr<editor_action> mouse_action_item::click_left(editor_display& disp, int x, int y)
47 {
48  start_hex_ = disp.hex_clicked_on(x, y);
49  if (!disp.get_map().on_board(start_hex_)) {
50  return nullptr;
51  }
52 
54  disp.add_overlay(start_hex_, std::move(item));
55 
56  click_ = true;
57  return nullptr;
58 }
59 
60 std::unique_ptr<editor_action> mouse_action_item::click_right(editor_display& disp, int x, int y)
61 {
62  start_hex_ = disp.hex_clicked_on(x, y);
63  if (!disp.get_map().on_board(start_hex_)) {
64  return nullptr;
65  }
66 
68 
69  click_ = true;
70  return nullptr;
71 }
72 
73 std::unique_ptr<editor_action> mouse_action_item::drag_left(editor_display& disp, int x, int y, bool& /*partial*/, editor_action* /*last_undo*/)
74 {
75  map_location hex = disp.hex_clicked_on(x, y);
76  click_ = (hex == start_hex_);
77  return nullptr;
78 }
79 
80 std::unique_ptr<editor_action> mouse_action_item::up_left(editor_display& disp, int x, int y)
81 {
82  if (!click_) return nullptr;
83  click_ = false;
84  map_location hex = disp.hex_clicked_on(x, y);
85  if (!disp.get_map().on_board(hex)) {
86  return nullptr;
87  }
88 
89 // item_type type = item_palette_.selected_fg_item();
90 //
91 // // Does this serve a purpose other than making sure the type is built?
92 // // (Calling item_types.build_item_type(type) would now accomplish that
93 // // with less overhead.)
94 // const std::string& type_id = type.id();
95 // const item_type *new_item_type = item_types.find(type_id);
96 // if (!new_item_type) {
97 // //TODO rewrite the error message.
98 // ERR_ED << "create item dialog returned inexistent or unusable item_type id '" << type_id << "'";
99 // return nullptr;
100 // }
101 //
102 // const item_type &ut = *new_item_type;
103 // item_race::GENDER gender = ut.genders().front();
104 //
105 // item new_item(ut, disp.viewing_side(), true, gender);
106 // editor_action* action = new editor_action_item(hex, new_item);
107 // return action;
108 
109 // \todo in #5070: there's a load of commented-out code in this file, it should probably
110 // all be deleted. For the function that this comment is in, I've left the commented-out
111 // code in because it seems the not-commented code should also be reviewed. AFAICS, the
112 // entire function (including the not-commented code) could be deleted, and fall back to
113 // the parent class' implementation of just returning nullptr.
114 
115  return nullptr;
116 }
117 
118 std::unique_ptr<editor_action> mouse_action_item::drag_end_left(editor_display& disp, int x, int y)
119 {
120  if (click_) return nullptr;
121 
122  map_location hex = disp.hex_clicked_on(x, y);
123  if (!disp.get_map().on_board(hex))
124  return nullptr;
125 
126 // const item_map& items = disp.get_items();
127 // const item_map::const_item_iterator item_it = items.find(start_hex_);
128 // if (item_it == items.end())
129 // return nullptr;
130 
131  return std::make_unique<editor_action_item_replace>(start_hex_, hex);
132 }
133 
135 {
136  const overlay& item = item_palette_.selected_fg_item();
137  set_item_mouse_overlay(disp, item);
138 }
139 
141 {
143 }
144 
145 
146 } //end namespace editor
Editor action classes.
void remove_overlay(const map_location &loc)
remove_overlay will remove all overlays on a tile.
Definition: display.cpp:134
bool invalidate(const map_location &loc)
Function to invalidate a specific tile for redrawing.
Definition: display.cpp:3087
void add_overlay(const map_location &loc, overlay &&ov)
Functions to add and remove overlays from locations.
Definition: display.cpp:121
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:538
Base class for all editor actions.
Definition: action_base.hpp:42
void set_mouseover_hex_overlay(const texture &image)
Sets texture to be drawn in hex under the mouse's location.
const editor_map & get_map() const
const Item & selected_fg_item() const
Return the currently selected foreground/background item.
std::unique_ptr< editor_action > drag_left(editor_display &disp, int x, int y, bool &partial, editor_action *last_undo) override
Drag operation.
void set_item_mouse_overlay(editor_display &disp, const overlay &u)
std::unique_ptr< editor_action > click_left(editor_display &disp, int x, int y) override
Left clicking places the currently selected item on the x,y map hex.
std::unique_ptr< editor_action > drag_end_left(editor_display &disp, int x, int y) override
Drag end replaces the item when clicked left, or adjusts the facing when clicked right.
std::unique_ptr< editor_action > click_right(editor_display &disp, int x, int y) override
Right clicking removes the item on the x,y map hex.
virtual void set_mouse_overlay(editor_display &disp) override
Set the mouse overlay for this action.
std::unique_ptr< editor_action > up_left(editor_display &disp, int x, int y) override
TODO.
void move(editor_display &disp, const map_location &hex) override
Mouse move (not a drag).
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.
map_location previous_move_hex_
The hex previously used in move operations.
bool on_board(const map_location &loc) const
Tell if a location is on the map.
Definition: map.cpp:385
void get_adjacent_tiles(const map_location &a, map_location *res)
Function which, given a location, will place all adjacent locations in res.
Definition: location.cpp:479
Manage the empty-palette in the editor.
Definition: action.cpp:31
texture get_texture(const image::locator &i_locator, TYPE type, bool skip_cache)
Returns an image texture suitable for hardware-accelerated rendering.
Definition: picture.cpp:920
Encapsulates the map of the game.
Definition: location.hpp:45
std::string image
Definition: overlay.hpp:55