The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
mouse_action_unit.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2016 by Fabian Mueller <[email protected]>
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 
17 #include "mouse_action_unit.hpp"
19 
22 #include "tooltips.hpp"
23 #include "gettext.hpp"
24 
25 #include "map/location.hpp"
26 #include "sdl/rect.hpp"
27 #include "units/unit.hpp"
28 #include "units/map.hpp"
29 
30 namespace editor {
31 
32 
34 {
35  if (hex != previous_move_hex_) {
36 
37  update_brush_highlights(disp, hex);
38 
39  std::set<map_location> adjacent_set;
40  map_location adjacent[6];
42 
43  for (int i = 0; i < 6; i++)
44  adjacent_set.insert(adjacent[i]);
45 
46  disp.invalidate(adjacent_set);
47  previous_move_hex_ = hex;
48 
49  const unit_map& units = disp.get_units();
50  const unit_map::const_unit_iterator unit_it = units.find(hex);
51  if (unit_it != units.end()) {
52 
53  disp.set_mouseover_hex_overlay(nullptr);
54 
55  SDL_Rect rect;
56  rect.x = disp.get_location_x(hex);
57  rect.y = disp.get_location_y(hex);
58  rect.h = disp.hex_size();
59  rect.w = disp.hex_size();
60  std::stringstream str;
61  str << N_("Identifier: ") << unit_it->id() << "\n"
62  << N_("Name: ") << unit_it->name() << "\n"
63  << N_("Type: ") << unit_it->type_name() << "\n"
64  << N_("Level: ") << unit_it->level() << "\n"
65  << N_("Cost: ") << unit_it->cost() << "\n"
66  << N_("Recruit: ") << utils::join(unit_it->recruits()) << "\n";
68  tooltips::add_tooltip(rect, str.str());
69  }
70  else {
71  set_mouse_overlay(disp);
72  }
73  }
74 }
75 
77 {
78  start_hex_ = disp.hex_clicked_on(x, y);
79  if (!disp.get_map().on_board(start_hex_)) {
80  return nullptr;
81  }
82 
83  const unit_map& units = disp.get_units();
84  const unit_map::const_unit_iterator unit_it = units.find(start_hex_);
85  if (unit_it != units.end())
86  set_unit_mouse_overlay(disp, unit_it->type());
87 
88  click_ = true;
89  return nullptr;
90 }
91 
92 editor_action* mouse_action_unit::drag_left(editor_display& disp, int x, int y, bool& /*partial*/, editor_action* /*last_undo*/)
93 {
94  map_location hex = disp.hex_clicked_on(x, y);
95  click_ = (hex == start_hex_);
96  return nullptr;
97 }
98 
100 {
101  if (!click_) return nullptr;
102  click_ = false;
103  map_location hex = disp.hex_clicked_on(x, y);
104  if (!disp.get_map().on_board(hex)) {
105  return nullptr;
106  }
107 
109 
110  // Does this serve a purpose other than making sure the type is built?
111  // (Calling unit_types.build_unit_type(type) would now accomplish that
112  // with less overhead.)
113  const std::string& type_id = type.id();
114  const unit_type *new_unit_type = unit_types.find(type_id);
115  if (!new_unit_type) {
116  //TODO rewrite the error message.
117  ERR_ED << "create unit dialog returned inexistent or unusable unit_type id '" << type_id << "'" << std::endl;
118  return nullptr;
119  }
120 
121  const unit_type &ut = *new_unit_type;
122  unit_race::GENDER gender = ut.genders().front();
123 
124  unit new_unit(ut, disp.viewing_side(), true, gender);
125  editor_action* action = new editor_action_unit(hex, new_unit);
126  return action;
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 unit_map& units = disp.get_units();
139  const unit_map::const_unit_iterator unit_it = units.find(start_hex_);
140  if (unit_it == units.end())
141  return nullptr;
142 
143  action = new editor_action_unit_replace(start_hex_, hex);
144  return action;
145 }
146 
147 /*
148 editor_action* mouse_action_unit::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 unit_map& units = disp.get_units();
155  const unit_map::const_unit_iterator unit_it = units.find(start_hex_);
156 
157  if (unit_it != units.end()) {
158  old_direction_ = unit_it->facing();
159  }
160 
161  click_ = true;
162  return nullptr;
163 }
164 */
165 
166 //editor_action* mouse_action_unit::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 unit_map& units = disp.get_units();
176 //
177 // const unit_map::const_unit_iterator unit_it = units.find(start_hex_);
178 // if (unit_it != units.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 (unit_it->get_location().get_direction(new_direction, 1) == hex) {
183 // return new editor_action_unit_facing(start_hex_, new_direction, old_direction_);
184 // }
185 // }
186 // }
187 //
188 // return nullptr;
189 //}
190 
191 //editor_action* mouse_action_unit::up_right(editor_display& disp, int /*x*/, int /*y*/)
192 //{
193 // if (!click_) return nullptr;
194 // click_ = false;
195 //
196 // const unit_map& units = disp.get_units();
197 // const unit_map::const_unit_iterator unit_it = units.find(start_hex_);
198 // if (unit_it != units.end()) {
199 // return new editor_action_unit_delete(start_hex_);
200 // }
201 //
202 // return nullptr;
203 //}
204 
205 //editor_action* mouse_action_unit::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 unit_map& units = disp.get_units();
216 // const unit_map::const_unit_iterator unit_it = units.find(start_hex_);
217 // if (unit_it != units.end()) {
218 // return new editor_action_unit_facing(start_hex_, new_direction_, old_direction_);
219 // }
220 // }
221 //
222 // return nullptr;
223 //}
224 
225 
227 {
229  set_unit_mouse_overlay(disp, u);
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  image = scale_surface(adjust_surface_alpha(image, alpha), zoom, zoom);
248  disp.set_mouseover_hex_overlay(image);
249 }
250 
251 
252 } //end namespace editor
virtual void set_mouse_overlay(editor_display &disp)
Set the mouse overlay for this action.
surface get_image(const image::locator &i_locator, TYPE type)
function to get the surface corresponding to an image.
Definition: image.cpp:878
editor_action * drag_left(editor_display &disp, int x, int y, bool &partial, editor_action *last_undo)
Drag operation.
const Item & selected_fg_item() const
Return the currently selected foreground/background item.
unit_iterator end()
Definition: map.hpp:311
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:3536
Definition: unit.hpp:95
void set_unit_mouse_overlay(editor_display &disp, const unit_type &u)
Editor action classes.
GLuint GLuint GLsizei GLenum type
Definition: glew.h:1221
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:274
GLenum GLsizei GLenum GLenum const GLvoid * image
Definition: glew.h:3783
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:577
unit_type_data unit_types
Definition: types.cpp:1314
const std::string & image() const
Definition: types.hpp:138
double get_zoom_factor() const
Returns the current zoom factor.
Definition: display.hpp:269
int get_location_x(const map_location &loc) const
Functions to get the on-screen positions of hexes.
Definition: display.cpp:713
GLint GLint GLint GLint GLint GLint y
Definition: glew.h:1220
surface scale_surface(const surface &surf, int w, int h)
Definition: utils.cpp:443
int get_location_y(const map_location &loc) const
Definition: display.cpp:718
void clear_tooltips()
Definition: tooltips.cpp:125
void move(editor_display &disp, const map_location &hex)
Mouse move (not a drag).
const std::string & flag_rgb() const
Definition: types.hpp:147
surface adjust_surface_alpha(const surface &surf, fixed_t amount, bool optimize)
Definition: utils.cpp:1202
const std::vector< unit_race::GENDER > & genders() const
The returned vector will not be empty, provided this has been built to the HELP_INDEXED status...
Definition: types.hpp:198
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...
GLclampf GLclampf GLclampf alpha
Definition: glew.h:1488
editor_action * click_left(editor_display &disp, int x, int y)
TODO.
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:458
Manage the empty-palette in the editor.
Definition: action.cpp:28
int viewing_side() const
Definition: display.hpp:103
editor_action * up_left(editor_display &disp, int x, int y)
TODO.
const unit_map & get_units() const
Definition: display.hpp:121
std::string join(T const &v, const std::string &s=",")
Generates a new string joining container items in a list.
Encapsulates the map of the game.
Definition: location.hpp:38
editor_action * drag_end_left(editor_display &disp, int x, int y)
Drag end replaces the unit when clicked left, or adjusts the facing when clicked right.
place a new unit on the map
Definition: action_unit.hpp:39
Base class for all editor actions.
Definition: action_base.hpp:41
size_t i
Definition: function.cpp:1057
GLint GLint GLint GLint GLint x
Definition: glew.h:1220
#define N_(String)
Definition: gettext.hpp:90
bool on_board(const map_location &loc) const
Tell if a location is on the map.
Definition: map.cpp:467
GLsizeiptr size
Definition: glew.h:1649
Contains the SDL_Rect helper code.
const gamemap & get_map() const
Definition: display.hpp:92
#define ERR_ED
Container associating units to locations.
Definition: map.hpp:90
static std::string get_side_color_index(int side)
Definition: team.cpp:840
const unit_type * find(const std::string &key, unit_type::BUILD_STATUS status=unit_type::FULL) const
Finds a unit_type by its id() and makes sure it is built to the specified level.
Definition: types.cpp:1155
unit_iterator find(size_t id)
Definition: map.cpp:285
int hex_size() const
Function which returns the size of a hex in pixels (from top tip to bottom tip or left edge to right ...
Definition: display.hpp:266
int add_tooltip(const SDL_Rect &rect, const std::string &message, const std::string &action, bool use_markup, const surface &foreground)
Definition: tooltips.cpp:180
GLsizei const GLcharARB ** string
Definition: glew.h:4503
unit_map * units
Definition: resources.cpp:35
const std::string & id() const
The id for this unit_type.
Definition: types.hpp:115