The Battle for Wesnoth  1.15.0-dev
mouse_action_item.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2018 by Fabian Mueller <fabianmueller5@gmx.de>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #define GETTEXT_DOMAIN "wesnoth-editor"
16 
19 
21 //#include "gui/dialogs/item_create.hpp"
22 #include "tooltips.hpp"
23 #include "gettext.hpp"
24 
25 #include "map/location.hpp"
26 
27 namespace editor {
28 
29 
31 {
32  if (hex != previous_move_hex_) {
33 
34  update_brush_highlights(disp, hex);
35 
36  previous_move_hex_ = hex;
37 
38  // const item_map& items = disp.get_items();
39  // const item_map::const_item_iterator item_it = items.find(hex);
40 // if (item_it != items.end()) {
41 //
42 // disp.set_mouseover_hex_overlay(nullptr);
43 //
44 // SDL_Rect rect;
45 // rect.x = disp.get_location_x(hex);
46 // rect.y = disp.get_location_y(hex);
47 // rect.h = disp.hex_size();
48 // rect.w = disp.hex_size();
49 // std::stringstream str;
50 // str << N_("ID: ") << item_it->id() << "\n"
51 // << N_("Name: ") << item_it->name() << "\n"
52 // << N_("Type: ") << item_it->type_name();
53 // tooltips::clear_tooltips();
54 // tooltips::add_tooltip(rect, str.str());
55 // }
56 // else {
57 // set_mouse_overlay(disp);
58 // }
59  }
60 }
61 
63 {
64  start_hex_ = disp.hex_clicked_on(x, y);
65  if (!disp.get_map().on_board(start_hex_)) {
66  return nullptr;
67  }
68 
69  const overlay& item = item_palette_.selected_fg_item();
70  disp.add_overlay(start_hex_, item.image, item.halo, "", "", true);
71 
72 
73 
74 // const item_map::const_item_iterator item_it = items.find(start_hex_);
75 // if (item_it != items.end())
76 // set_item_mouse_overlay(disp, item_it->type());
77 
78  click_ = true;
79  return nullptr;
80 }
81 
82 editor_action* mouse_action_item::drag_left(editor_display& disp, int x, int y, bool& /*partial*/, editor_action* /*last_undo*/)
83 {
84  map_location hex = disp.hex_clicked_on(x, y);
85  click_ = (hex == start_hex_);
86  return nullptr;
87 }
88 
90 {
91  if (!click_) return nullptr;
92  click_ = false;
93  map_location hex = disp.hex_clicked_on(x, y);
94  if (!disp.get_map().on_board(hex)) {
95  return nullptr;
96  }
97 
98 // item_type type = item_palette_.selected_fg_item();
99 //
100 // // Does this serve a purpose other than making sure the type is built?
101 // // (Calling item_types.build_item_type(type) would now accomplish that
102 // // with less overhead.)
103 // const std::string& type_id = type.id();
104 // const item_type *new_item_type = item_types.find(type_id);
105 // if (!new_item_type) {
106 // //TODO rewrite the error message.
107 // ERR_ED << "create item dialog returned inexistent or unusable item_type id '" << type_id << "'" << std::endl;
108 // return nullptr;
109 // }
110 //
111 // const item_type &ut = *new_item_type;
112 // item_race::GENDER gender = ut.genders().front();
113 //
114 // item new_item(ut, disp.viewing_side(), true, gender);
115 // editor_action* action = new editor_action_item(hex, new_item);
116 // return action;
117 
118  return nullptr;
119 }
120 
122 {
123  if (click_) return nullptr;
124  editor_action* action = nullptr;
125 
126  map_location hex = disp.hex_clicked_on(x, y);
127  if (!disp.get_map().on_board(hex))
128  return nullptr;
129 
130 // const item_map& items = disp.get_items();
131 // const item_map::const_item_iterator item_it = items.find(start_hex_);
132 // if (item_it == items.end())
133 // return nullptr;
134 
135  action = new editor_action_item_replace(start_hex_, hex);
136  return action;
137 }
138 
139 /*
140 editor_action* mouse_action_item::click_right(editor_display& disp, int x, int y)
141 {
142  map_location hex = disp.hex_clicked_on(x, y);
143  start_hex_ = hex;
144  previous_move_hex_ = hex;
145 
146  const item_map& items = disp.get_items();
147  const item_map::const_item_iterator item_it = items.find(start_hex_);
148 
149  if (item_it != items.end()) {
150  old_direction_ = item_it->facing();
151  }
152 
153  click_ = true;
154  return nullptr;
155 }
156 */
157 
158 //editor_action* mouse_action_item::drag_right(editor_display& disp, int x, int y, bool& /*partial*/, editor_action* /*last_undo*/)
159 //{
160 // map_location hex = disp.hex_clicked_on(x, y);
161 // if (previous_move_hex_ == hex)
162 // return nullptr;
163 //
164 // click_ = (start_hex_ == hex);
165 // previous_move_hex_ = hex;
166 //
167 // const item_map& items = disp.get_items();
168 //
169 // const item_map::const_item_iterator item_it = items.find(start_hex_);
170 // if (item_it != items.end()) {
171 // for (map_location::DIRECTION new_direction = map_location::NORTH;
172 // new_direction <= map_location::NORTH_WEST;
173 // new_direction = map_location::DIRECTION(new_direction +1)){
174 // if (item_it->get_location().get_direction(new_direction, 1) == hex) {
175 // return new editor_action_item_facing(start_hex_, new_direction, old_direction_);
176 // }
177 // }
178 // }
179 //
180 // return nullptr;
181 //}
182 
183 //editor_action* mouse_action_item::up_right(editor_display& disp, int /*x*/, int /*y*/)
184 //{
185 // if (!click_) return nullptr;
186 // click_ = false;
187 //
188 // const item_map& items = disp.get_items();
189 // const item_map::const_item_iterator item_it = items.find(start_hex_);
190 // if (item_it != items.end()) {
191 // return new editor_action_item_delete(start_hex_);
192 // }
193 //
194 // return nullptr;
195 //}
196 
197 //editor_action* mouse_action_item::drag_end_right(editor_display& disp, int x, int y)
198 //{
199 // if (click_) return nullptr;
200 //
201 // map_location hex = disp.hex_clicked_on(x, y);
202 // if (!disp.get_map().on_board(hex))
203 // return nullptr;
204 //
205 // if(new_direction_ != old_direction_) {
206 //
207 // const item_map& items = disp.get_items();
208 // const item_map::const_item_iterator item_it = items.find(start_hex_);
209 // if (item_it != items.end()) {
210 // return new editor_action_item_facing(start_hex_, new_direction_, old_direction_);
211 // }
212 // }
213 //
214 // return nullptr;
215 //}
216 
217 
219 {
220  const overlay& item = item_palette_.selected_fg_item();
221  set_item_mouse_overlay(disp, item);
222 }
223 
225 {
226 
227  std::stringstream filename;
228  filename << u.image; // << "~RC(" << u.flag_rgb() << '>'
229  // << team::get_side_color_id(disp.viewing_side()) << ')';
230 
231  surface image(image::get_image(filename.str()));
232  uint8_t alpha = 196;
233  //TODO don't hardcode
234  int size = 72;
235  //int size = image->w;
236  int zoom = static_cast<int>(size * disp.get_zoom_factor());
237 
238  // Add the alpha factor and scale the image
239  adjust_surface_alpha(image, alpha);
240  image = scale_surface(image, zoom, zoom);
241  disp.set_mouseover_hex_overlay(image);
242 }
243 
244 
245 } //end namespace editor
surface get_image(const image::locator &i_locator, TYPE type)
function to get the surface corresponding to an image.
Definition: image.cpp:947
map_location previous_move_hex_
The hex previously used in move operations.
surface scale_surface(const surface &surf, int w, int h)
Scale a surface using alpha-weighted modified bilinear filtering Note: causes artifacts with alpha gr...
Definition: utils.cpp:275
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:488
editor_action * click_left(editor_display &disp, int x, int y)
TODO.
bool on_board(const map_location &loc) const
Tell if a location is on the map.
Definition: map.cpp:360
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:86
editor_action * up_left(editor_display &disp, int x, int y)
TODO.
editor_action * drag_left(editor_display &disp, int x, int y, bool &partial, editor_action *last_undo)
Drag operation.
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...
void set_mouseover_hex_overlay(const surface &image)
mouseover_hex_overlay_ require a prerendered surface and is drawn underneath the mouse&#39;s location ...
Definition: display.hpp:457
Manage the empty-palette in the editor.
Definition: action.cpp:29
virtual void set_mouse_overlay(editor_display &disp)
Set the mouse overlay for this action.
Encapsulates the map of the game.
Definition: location.hpp:42
void set_item_mouse_overlay(editor_display &disp, const overlay &u)
Base class for all editor actions.
Definition: action_base.hpp:40
double get_zoom_factor() const
Returns the current zoom factor.
Definition: display.hpp:327
std::string halo
Definition: overlay.hpp:37
void add_overlay(const map_location &loc, const std::string &image, const std::string &halo="", const std::string &team_name="", const std::string &item_id="", bool visible_under_fog=true)
Functions to add and remove overlays from locations.
Definition: display.cpp:275
const gamemap & get_map() const
Definition: display.hpp:109
const Item & selected_fg_item() const
Return the currently selected foreground/background item.
void adjust_surface_alpha(surface &surf, fixed_t amount)
Definition: utils.cpp:1175
this module manages the cache of images.
Definition: image.cpp:102
void move(editor_display &disp, const map_location &hex)
Mouse move (not a drag).
Editor action classes.
editor_action * drag_end_left(editor_display &disp, int x, int y)
Drag end replaces the item when clicked left, or adjusts the facing when clicked right.
std::string image
Definition: overlay.hpp:36