The Battle for Wesnoth  1.15.0-dev
mouse_action_unit.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 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  previous_move_hex_ = hex;
41 
42  const unit_map& units = disp.get_units();
43  const unit_map::const_unit_iterator unit_it = units.find(hex);
44  if (unit_it != units.end()) {
45 
46  disp.set_mouseover_hex_overlay(nullptr);
47 
48  SDL_Rect rect;
49  rect.x = disp.get_location_x(hex);
50  rect.y = disp.get_location_y(hex);
51  rect.h = disp.hex_size();
52  rect.w = disp.hex_size();
53  std::stringstream str;
54  str << N_("Identifier: ") << unit_it->id() << "\n"
55  << N_("Name: ") << unit_it->name() << "\n"
56  << N_("Type: ") << unit_it->type_name() << "\n"
57  << N_("Level: ") << unit_it->level() << "\n"
58  << N_("Cost: ") << unit_it->cost() << "\n"
59  << N_("Recruit: ") << utils::join(unit_it->recruits()) << "\n";
61  tooltips::add_tooltip(rect, str.str());
62  }
63  else {
64  set_mouse_overlay(disp);
65  }
66  }
67 }
68 
70 {
71  start_hex_ = disp.hex_clicked_on(x, y);
72  if (!disp.get_map().on_board(start_hex_)) {
73  return nullptr;
74  }
75 
76  const unit_map& units = disp.get_units();
77  const unit_map::const_unit_iterator unit_it = units.find(start_hex_);
78  if (unit_it != units.end())
79  set_unit_mouse_overlay(disp, unit_it->type());
80 
81  click_ = true;
82  return nullptr;
83 }
84 
85 editor_action* mouse_action_unit::drag_left(editor_display& disp, int x, int y, bool& /*partial*/, editor_action* /*last_undo*/)
86 {
87  map_location hex = disp.hex_clicked_on(x, y);
88  click_ = (hex == start_hex_);
89  return nullptr;
90 }
91 
93 {
94  if (!click_) return nullptr;
95  click_ = false;
96  map_location hex = disp.hex_clicked_on(x, y);
97  if (!disp.get_map().on_board(hex)) {
98  return nullptr;
99  }
100 
102 
103  // Does this serve a purpose other than making sure the type is built?
104  // (Calling unit_types.build_unit_type(type) would now accomplish that
105  // with less overhead.)
106  const std::string& type_id = type.id();
107  const unit_type *new_unit_type = unit_types.find(type_id);
108  if (!new_unit_type) {
109  //TODO rewrite the error message.
110  ERR_ED << "create unit dialog returned inexistent or unusable unit_type id '" << type_id << "'" << std::endl;
111  return nullptr;
112  }
113 
114  const unit_type &ut = *new_unit_type;
115  unit_race::GENDER gender = ut.genders().front();
116 
117  unit_ptr new_unit = unit::create(ut, disp.viewing_side(), true, gender);
118  editor_action* action = new editor_action_unit(hex, *new_unit);
119  return action;
120 }
121 
123 {
124  if (click_) return nullptr;
125  editor_action* action = nullptr;
126 
127  map_location hex = disp.hex_clicked_on(x, y);
128  if (!disp.get_map().on_board(hex))
129  return nullptr;
130 
131  const unit_map& units = disp.get_units();
132  const unit_map::const_unit_iterator unit_it = units.find(start_hex_);
133  if (unit_it == units.end())
134  return nullptr;
135 
136  action = new editor_action_unit_replace(start_hex_, hex);
137  return action;
138 }
139 
140 /*
141 editor_action* mouse_action_unit::click_right(editor_display& disp, int x, int y)
142 {
143  map_location hex = disp.hex_clicked_on(x, y);
144  start_hex_ = hex;
145  previous_move_hex_ = hex;
146 
147  const unit_map& units = disp.units();
148  const unit_map::const_unit_iterator unit_it = units.find(start_hex_);
149 
150  if (unit_it != units.end()) {
151  old_direction_ = unit_it->facing();
152  }
153 
154  click_ = true;
155  return nullptr;
156 }
157 */
158 
159 //editor_action* mouse_action_unit::drag_right(editor_display& disp, int x, int y, bool& /*partial*/, editor_action* /*last_undo*/)
160 //{
161 // map_location hex = disp.hex_clicked_on(x, y);
162 // if (previous_move_hex_ == hex)
163 // return nullptr;
164 //
165 // click_ = (start_hex_ == hex);
166 // previous_move_hex_ = hex;
167 //
168 // const unit_map& units = disp.units();
169 //
170 // const unit_map::const_unit_iterator unit_it = units.find(start_hex_);
171 // if (unit_it != units.end()) {
172 // for (map_location::DIRECTION new_direction = map_location::NORTH;
173 // new_direction <= map_location::NORTH_WEST;
174 // new_direction = map_location::DIRECTION(new_direction +1)){
175 // if (unit_it->get_location().get_direction(new_direction, 1) == hex) {
176 // return new editor_action_unit_facing(start_hex_, new_direction, old_direction_);
177 // }
178 // }
179 // }
180 //
181 // return nullptr;
182 //}
183 
184 //editor_action* mouse_action_unit::up_right(editor_display& disp, int /*x*/, int /*y*/)
185 //{
186 // if (!click_) return nullptr;
187 // click_ = false;
188 //
189 // const unit_map& units = disp.units();
190 // const unit_map::const_unit_iterator unit_it = units.find(start_hex_);
191 // if (unit_it != units.end()) {
192 // return new editor_action_unit_delete(start_hex_);
193 // }
194 //
195 // return nullptr;
196 //}
197 
198 //editor_action* mouse_action_unit::drag_end_right(editor_display& disp, int x, int y)
199 //{
200 // if (click_) return nullptr;
201 //
202 // map_location hex = disp.hex_clicked_on(x, y);
203 // if (!disp.get_map().on_board(hex))
204 // return nullptr;
205 //
206 // if(new_direction_ != old_direction_) {
207 //
208 // const unit_map& units = disp.units();
209 // const unit_map::const_unit_iterator unit_it = units.find(start_hex_);
210 // if (unit_it != units.end()) {
211 // return new editor_action_unit_facing(start_hex_, new_direction_, old_direction_);
212 // }
213 // }
214 //
215 // return nullptr;
216 //}
217 
218 
220 {
222  set_unit_mouse_overlay(disp, u);
223 }
224 
226 {
227 
228  std::stringstream filename;
229  filename << u.image() << "~RC(" << u.flag_rgb() << '>'
230  << team::get_side_color_id(disp.viewing_side()) << ')';
231 
232  surface image(image::get_image(filename.str()));
233  uint8_t alpha = 196;
234  //TODO don't hardcode
235  int size = 72;
236  //int size = image->w;
237  int zoom = static_cast<int>(size * disp.get_zoom_factor());
238 
239  // Add the alpha factor and scale the image
240  adjust_surface_alpha(image, alpha);
241  image = scale_surface(image, zoom, zoom);
242  disp.set_mouseover_hex_overlay(image);
243 }
244 
245 
246 } //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:989
editor_action * drag_left(editor_display &disp, int x, int y, bool &partial, editor_action *last_undo)
Drag operation.
unit_iterator end()
Definition: map.hpp:415
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:1275
map_location previous_move_hex_
The hex previously used in move operations.
static int hex_size()
Function which returns the size of a hex in pixels (from top tip to bottom tip or left edge to right ...
Definition: display.hpp:321
std::string join(const T &v, const std::string &s=",")
Generates a new string joining container items in a list.
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
void set_unit_mouse_overlay(editor_display &disp, const unit_type &u)
Editor action classes.
const std::string & flag_rgb() const
Definition: types.cpp:744
const unit_map & get_units() const
Definition: display.hpp:157
int viewing_side() const
Definition: display.hpp:133
unit_type_data unit_types
Definition: types.cpp:1452
bool on_board(const map_location &loc) const
Tell if a location is on the map.
Definition: map.cpp:367
-file sdl_utils.hpp
static unit_ptr create(const config &cfg, bool use_traits=false, const vconfig *vcfg=nullptr)
Initializes a unit from a config.
Definition: unit.hpp:122
A single unit type that the player may recruit.
Definition: types.hpp:42
void clear_tooltips()
Definition: tooltips.cpp:123
void move(editor_display &disp, const map_location &hex)
Mouse move (not a drag).
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:86
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:239
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...
const std::string & id() const
The id for this unit_type.
Definition: types.hpp:139
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&#39;s location ...
Definition: display.hpp:457
Manage the empty-palette in the editor.
Definition: action.cpp:29
editor_action * up_left(editor_display &disp, int x, int y)
TODO.
static std::string get_side_color_id(unsigned side)
Definition: team.cpp:950
SDL_Rect rect
The coordinates of this image on the spritesheet.
Encapsulates the map of the game.
Definition: location.hpp:42
unit_iterator find(std::size_t id)
Definition: map.cpp:311
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
int get_location_y(const map_location &loc) const
Definition: display.cpp:624
double get_zoom_factor() const
Returns the current zoom factor.
Definition: display.hpp:327
#define N_(String)
Definition: gettext.hpp:97
const std::string & image() const
Definition: types.hpp:162
int get_location_x(const map_location &loc) const
Functions to get the on-screen positions of hexes.
Definition: display.cpp:619
const gamemap & get_map() const
Definition: display.hpp:109
boost::intrusive_ptr< unit > unit_ptr
Definition: ptr.hpp:29
Contains the SDL_Rect helper code.
const Item & selected_fg_item() const
Return the currently selected foreground/background item.
#define ERR_ED
void adjust_surface_alpha(surface &surf, fixed_t amount)
Definition: utils.cpp:1175
this module manages the cache of images.
Definition: image.cpp:102
Container associating units to locations.
Definition: map.hpp:99
int add_tooltip(const SDL_Rect &rect, const std::string &message, const std::string &action, bool use_markup, const surface &foreground)
Definition: tooltips.cpp:178