The Battle for Wesnoth  1.13.10+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
mouse_action_item.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2017 by Fabian Mueller <fabianmueller5@gmx.de>
3  Part of the Battle for Wesnoth Project http://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  std::set<map_location> adjacent_set;
37  map_location adjacent[6];
39 
40  for (int i = 0; i < 6; i++)
41  adjacent_set.insert(adjacent[i]);
42 
43  disp.invalidate(adjacent_set);
44  previous_move_hex_ = hex;
45 
46  // const item_map& items = disp.get_items();
47  // const item_map::const_item_iterator item_it = items.find(hex);
48 // if (item_it != items.end()) {
49 //
50 // disp.set_mouseover_hex_overlay(nullptr);
51 //
52 // SDL_Rect rect;
53 // rect.x = disp.get_location_x(hex);
54 // rect.y = disp.get_location_y(hex);
55 // rect.h = disp.hex_size();
56 // rect.w = disp.hex_size();
57 // std::stringstream str;
58 // str << N_("ID: ") << item_it->id() << "\n"
59 // << N_("Name: ") << item_it->name() << "\n"
60 // << N_("Type: ") << item_it->type_name();
61 // tooltips::clear_tooltips();
62 // tooltips::add_tooltip(rect, str.str());
63 // }
64 // else {
65 // set_mouse_overlay(disp);
66 // }
67  }
68 }
69 
71 {
72  start_hex_ = disp.hex_clicked_on(x, y);
73  if (!disp.get_map().on_board(start_hex_)) {
74  return nullptr;
75  }
76 
77  const overlay& item = item_palette_.selected_fg_item();
78  disp.add_overlay(start_hex_, item.image, item.halo, "", "", true);
79 
80 
81 
82 // const item_map::const_item_iterator item_it = items.find(start_hex_);
83 // if (item_it != items.end())
84 // set_item_mouse_overlay(disp, item_it->type());
85 
86  click_ = true;
87  return nullptr;
88 }
89 
90 editor_action* mouse_action_item::drag_left(editor_display& disp, int x, int y, bool& /*partial*/, editor_action* /*last_undo*/)
91 {
92  map_location hex = disp.hex_clicked_on(x, y);
93  click_ = (hex == start_hex_);
94  return nullptr;
95 }
96 
98 {
99  if (!click_) return nullptr;
100  click_ = false;
101  map_location hex = disp.hex_clicked_on(x, y);
102  if (!disp.get_map().on_board(hex)) {
103  return nullptr;
104  }
105 
106 // item_type type = item_palette_.selected_fg_item();
107 //
108 // // Does this serve a purpose other than making sure the type is built?
109 // // (Calling item_types.build_item_type(type) would now accomplish that
110 // // with less overhead.)
111 // const std::string& type_id = type.id();
112 // const item_type *new_item_type = item_types.find(type_id);
113 // if (!new_item_type) {
114 // //TODO rewrite the error message.
115 // ERR_ED << "create item dialog returned inexistent or unusable item_type id '" << type_id << "'" << std::endl;
116 // return nullptr;
117 // }
118 //
119 // const item_type &ut = *new_item_type;
120 // item_race::GENDER gender = ut.genders().front();
121 //
122 // item new_item(ut, disp.viewing_side(), true, gender);
123 // editor_action* action = new editor_action_item(hex, new_item);
124 // return action;
125 
126  return nullptr;
127 }
128 
130 {
131  if (click_) return nullptr;
132  editor_action* action = nullptr;
133 
134  map_location hex = disp.hex_clicked_on(x, y);
135  if (!disp.get_map().on_board(hex))
136  return nullptr;
137 
138 // const item_map& items = disp.get_items();
139 // const item_map::const_item_iterator item_it = items.find(start_hex_);
140 // if (item_it == items.end())
141 // return nullptr;
142 
143  action = new editor_action_item_replace(start_hex_, hex);
144  return action;
145 }
146 
147 /*
148 editor_action* mouse_action_item::click_right(editor_display& disp, int x, int y)
149 {
150  map_location hex = disp.hex_clicked_on(x, y);
151  start_hex_ = hex;
152  previous_move_hex_ = hex;
153 
154  const item_map& items = disp.get_items();
155  const item_map::const_item_iterator item_it = items.find(start_hex_);
156 
157  if (item_it != items.end()) {
158  old_direction_ = item_it->facing();
159  }
160 
161  click_ = true;
162  return nullptr;
163 }
164 */
165 
166 //editor_action* mouse_action_item::drag_right(editor_display& disp, int x, int y, bool& /*partial*/, editor_action* /*last_undo*/)
167 //{
168 // map_location hex = disp.hex_clicked_on(x, y);
169 // if (previous_move_hex_ == hex)
170 // return nullptr;
171 //
172 // click_ = (start_hex_ == hex);
173 // previous_move_hex_ = hex;
174 //
175 // const item_map& items = disp.get_items();
176 //
177 // const item_map::const_item_iterator item_it = items.find(start_hex_);
178 // if (item_it != items.end()) {
179 // for (map_location::DIRECTION new_direction = map_location::NORTH;
180 // new_direction <= map_location::NORTH_WEST;
181 // new_direction = map_location::DIRECTION(new_direction +1)){
182 // if (item_it->get_location().get_direction(new_direction, 1) == hex) {
183 // return new editor_action_item_facing(start_hex_, new_direction, old_direction_);
184 // }
185 // }
186 // }
187 //
188 // return nullptr;
189 //}
190 
191 //editor_action* mouse_action_item::up_right(editor_display& disp, int /*x*/, int /*y*/)
192 //{
193 // if (!click_) return nullptr;
194 // click_ = false;
195 //
196 // const item_map& items = disp.get_items();
197 // const item_map::const_item_iterator item_it = items.find(start_hex_);
198 // if (item_it != items.end()) {
199 // return new editor_action_item_delete(start_hex_);
200 // }
201 //
202 // return nullptr;
203 //}
204 
205 //editor_action* mouse_action_item::drag_end_right(editor_display& disp, int x, int y)
206 //{
207 // if (click_) return nullptr;
208 //
209 // map_location hex = disp.hex_clicked_on(x, y);
210 // if (!disp.get_map().on_board(hex))
211 // return nullptr;
212 //
213 // if(new_direction_ != old_direction_) {
214 //
215 // const item_map& items = disp.get_items();
216 // const item_map::const_item_iterator item_it = items.find(start_hex_);
217 // if (item_it != items.end()) {
218 // return new editor_action_item_facing(start_hex_, new_direction_, old_direction_);
219 // }
220 // }
221 //
222 // return nullptr;
223 //}
224 
225 
227 {
228  const overlay& item = item_palette_.selected_fg_item();
229  set_item_mouse_overlay(disp, item);
230 }
231 
233 {
234 
235  std::stringstream filename;
236  filename << u.image; // << "~RC(" << u.flag_rgb() << '>'
237  // << team::get_side_color_index(disp.viewing_side()) << ')';
238 
239  surface image(image::get_image(filename.str()));
240  Uint8 alpha = 196;
241  //TODO don't hardcode
242  int size = 72;
243  //int size = image->w;
244  int zoom = static_cast<int>(size * disp.get_zoom_factor());
245 
246  // Add the alpha factor and scale the image
247  adjust_surface_alpha(image, alpha);
248  image = scale_surface(image, zoom, zoom);
249  disp.set_mouseover_hex_overlay(image);
250 }
251 
252 
253 } //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:926
const Item & selected_fg_item() const
Return the currently selected foreground/background item.
map_location previous_move_hex_
The hex previously used in move operations.
bool invalidate(const map_location &loc)
Function to invalidate a specific tile for redrawing.
Definition: display.cpp:2981
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.hpp:299
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:585
editor_action * click_left(editor_display &disp, int x, int y)
TODO.
double get_zoom_factor() const
Returns the current zoom factor.
Definition: display.hpp:262
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:262
void adjust_surface_alpha(surface &surf, fixed_t amount)
Definition: utils.cpp:1162
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's location ...
Definition: display.hpp:442
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:40
void set_item_mouse_overlay(editor_display &disp, const overlay &u)
size_t size(const utf8::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:86
Base class for all editor actions.
Definition: action_base.hpp:40
#define i
std::string halo
Definition: overlay.hpp:36
bool on_board(const map_location &loc) const
Tell if a location is on the map.
Definition: map.cpp:369
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:107
const gamemap & get_map() const
Definition: display.hpp:92
this module manages the cache of images.
Definition: image.cpp:109
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:35