The Battle for Wesnoth  1.13.10+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 - 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 
22 #include "tooltips.hpp"
23 #include "gettext.hpp"
24 
25 #include "map/location.hpp"
26 #include "sdl/rect.hpp"
27 #include "team.hpp"
28 #include "units/unit.hpp"
29 #include "units/map.hpp"
30 
31 namespace editor {
32 
33 
35 {
36  if (hex != previous_move_hex_) {
37 
38  update_brush_highlights(disp, hex);
39 
40  std::set<map_location> adjacent_set;
41  map_location adjacent[6];
43 
44  for (int i = 0; i < 6; i++)
45  adjacent_set.insert(adjacent[i]);
46 
47  disp.invalidate(adjacent_set);
48  previous_move_hex_ = hex;
49 
50  const unit_map& units = disp.get_units();
51  const unit_map::const_unit_iterator unit_it = units.find(hex);
52  if (unit_it != units.end()) {
53 
54  disp.set_mouseover_hex_overlay(nullptr);
55 
56  SDL_Rect rect;
57  rect.x = disp.get_location_x(hex);
58  rect.y = disp.get_location_y(hex);
59  rect.h = disp.hex_size();
60  rect.w = disp.hex_size();
61  std::stringstream str;
62  str << N_("Identifier: ") << unit_it->id() << "\n"
63  << N_("Name: ") << unit_it->name() << "\n"
64  << N_("Type: ") << unit_it->type_name() << "\n"
65  << N_("Level: ") << unit_it->level() << "\n"
66  << N_("Cost: ") << unit_it->cost() << "\n"
67  << N_("Recruit: ") << utils::join(unit_it->recruits()) << "\n";
69  tooltips::add_tooltip(rect, str.str());
70  }
71  else {
72  set_mouse_overlay(disp);
73  }
74  }
75 }
76 
78 {
79  start_hex_ = disp.hex_clicked_on(x, y);
80  if (!disp.get_map().on_board(start_hex_)) {
81  return nullptr;
82  }
83 
84  const unit_map& units = disp.get_units();
85  const unit_map::const_unit_iterator unit_it = units.find(start_hex_);
86  if (unit_it != units.end())
87  set_unit_mouse_overlay(disp, unit_it->type());
88 
89  click_ = true;
90  return nullptr;
91 }
92 
93 editor_action* mouse_action_unit::drag_left(editor_display& disp, int x, int y, bool& /*partial*/, editor_action* /*last_undo*/)
94 {
95  map_location hex = disp.hex_clicked_on(x, y);
96  click_ = (hex == start_hex_);
97  return nullptr;
98 }
99 
101 {
102  if (!click_) return nullptr;
103  click_ = false;
104  map_location hex = disp.hex_clicked_on(x, y);
105  if (!disp.get_map().on_board(hex)) {
106  return nullptr;
107  }
108 
110 
111  // Does this serve a purpose other than making sure the type is built?
112  // (Calling unit_types.build_unit_type(type) would now accomplish that
113  // with less overhead.)
114  const std::string& type_id = type.id();
115  const unit_type *new_unit_type = unit_types.find(type_id);
116  if (!new_unit_type) {
117  //TODO rewrite the error message.
118  ERR_ED << "create unit dialog returned inexistent or unusable unit_type id '" << type_id << "'" << std::endl;
119  return nullptr;
120  }
121 
122  const unit_type &ut = *new_unit_type;
123  unit_race::GENDER gender = ut.genders().front();
124 
125  unit new_unit(ut, disp.viewing_side(), true, gender);
126  editor_action* action = new editor_action_unit(hex, new_unit);
127  return action;
128 }
129 
131 {
132  if (click_) return nullptr;
133  editor_action* action = nullptr;
134 
135  map_location hex = disp.hex_clicked_on(x, y);
136  if (!disp.get_map().on_board(hex))
137  return nullptr;
138 
139  const unit_map& units = disp.get_units();
140  const unit_map::const_unit_iterator unit_it = units.find(start_hex_);
141  if (unit_it == units.end())
142  return nullptr;
143 
144  action = new editor_action_unit_replace(start_hex_, hex);
145  return action;
146 }
147 
148 /*
149 editor_action* mouse_action_unit::click_right(editor_display& disp, int x, int y)
150 {
151  map_location hex = disp.hex_clicked_on(x, y);
152  start_hex_ = hex;
153  previous_move_hex_ = hex;
154 
155  const unit_map& units = disp.get_units();
156  const unit_map::const_unit_iterator unit_it = units.find(start_hex_);
157 
158  if (unit_it != units.end()) {
159  old_direction_ = unit_it->facing();
160  }
161 
162  click_ = true;
163  return nullptr;
164 }
165 */
166 
167 //editor_action* mouse_action_unit::drag_right(editor_display& disp, int x, int y, bool& /*partial*/, editor_action* /*last_undo*/)
168 //{
169 // map_location hex = disp.hex_clicked_on(x, y);
170 // if (previous_move_hex_ == hex)
171 // return nullptr;
172 //
173 // click_ = (start_hex_ == hex);
174 // previous_move_hex_ = hex;
175 //
176 // const unit_map& units = disp.get_units();
177 //
178 // const unit_map::const_unit_iterator unit_it = units.find(start_hex_);
179 // if (unit_it != units.end()) {
180 // for (map_location::DIRECTION new_direction = map_location::NORTH;
181 // new_direction <= map_location::NORTH_WEST;
182 // new_direction = map_location::DIRECTION(new_direction +1)){
183 // if (unit_it->get_location().get_direction(new_direction, 1) == hex) {
184 // return new editor_action_unit_facing(start_hex_, new_direction, old_direction_);
185 // }
186 // }
187 // }
188 //
189 // return nullptr;
190 //}
191 
192 //editor_action* mouse_action_unit::up_right(editor_display& disp, int /*x*/, int /*y*/)
193 //{
194 // if (!click_) return nullptr;
195 // click_ = false;
196 //
197 // const unit_map& units = disp.get_units();
198 // const unit_map::const_unit_iterator unit_it = units.find(start_hex_);
199 // if (unit_it != units.end()) {
200 // return new editor_action_unit_delete(start_hex_);
201 // }
202 //
203 // return nullptr;
204 //}
205 
206 //editor_action* mouse_action_unit::drag_end_right(editor_display& disp, int x, int y)
207 //{
208 // if (click_) return nullptr;
209 //
210 // map_location hex = disp.hex_clicked_on(x, y);
211 // if (!disp.get_map().on_board(hex))
212 // return nullptr;
213 //
214 // if(new_direction_ != old_direction_) {
215 //
216 // const unit_map& units = disp.get_units();
217 // const unit_map::const_unit_iterator unit_it = units.find(start_hex_);
218 // if (unit_it != units.end()) {
219 // return new editor_action_unit_facing(start_hex_, new_direction_, old_direction_);
220 // }
221 // }
222 //
223 // return nullptr;
224 //}
225 
226 
228 {
230  set_unit_mouse_overlay(disp, u);
231 }
232 
234 {
235 
236  std::stringstream filename;
237  filename << u.image() << "~RC(" << u.flag_rgb() << '>'
238  << team::get_side_color_index(disp.viewing_side()) << ')';
239 
240  surface image(image::get_image(filename.str()));
241  Uint8 alpha = 196;
242  //TODO don't hardcode
243  int size = 72;
244  //int size = image->w;
245  int zoom = static_cast<int>(size * disp.get_zoom_factor());
246 
247  // Add the alpha factor and scale the image
248  adjust_surface_alpha(image, alpha);
249  image = scale_surface(image, zoom, zoom);
250  disp.set_mouseover_hex_overlay(image);
251 }
252 
253 
254 } //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:926
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:415
std::vector< char_t > string
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
This class represents a single unit of a specific type.
Definition: unit.hpp:101
std::string join(const T &v, const std::string &s=",")
Generates a new string joining container items in a list.
void set_unit_mouse_overlay(editor_display &disp, const unit_type &u)
Editor action classes.
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
unit_type_data unit_types
Definition: types.cpp:1455
const std::string & image() const
Definition: types.hpp:163
double get_zoom_factor() const
Returns the current zoom factor.
Definition: display.hpp:262
int get_location_x(const map_location &loc) const
Functions to get the on-screen positions of hexes.
Definition: display.cpp:736
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
-file sdl_utils.hpp
const std::string & flag_rgb() const
Definition: types.cpp:742
A single unit type that the player may recruit.
Definition: types.hpp:43
int get_location_y(const map_location &loc) const
Definition: display.cpp:741
void clear_tooltips()
Definition: tooltips.cpp:125
void move(editor_display &disp, const map_location &hex)
Mouse move (not a drag).
void adjust_surface_alpha(surface &surf, fixed_t amount)
Definition: utils.cpp:1162
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:221
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...
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:442
Manage the empty-palette in the editor.
Definition: action.cpp:29
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
Encapsulates the map of the game.
Definition: location.hpp:40
size_t size(const utf8::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:86
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:37
Base class for all editor actions.
Definition: action_base.hpp:40
#define i
#define N_(String)
Definition: gettext.hpp:97
bool on_board(const map_location &loc) const
Tell if a location is on the map.
Definition: map.cpp:369
Contains the SDL_Rect helper code.
const gamemap & get_map() const
Definition: display.hpp:92
#define ERR_ED
this module manages the cache of images.
Definition: image.cpp:109
Container associating units to locations.
Definition: map.hpp:99
static std::string get_side_color_index(int side)
Definition: team.cpp:888
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:1273
unit_iterator find(size_t id)
Definition: map.cpp:311
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:259
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
unit_map * units
Definition: resources.cpp:34
const std::string & id() const
The id for this unit_type.
Definition: types.hpp:140