The Battle for Wesnoth  1.17.17+dev
mouse_action_unit.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2023
3  by Fabian Mueller <fabianmueller5@gmx.de>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #define GETTEXT_DOMAIN "wesnoth-editor"
17 
20 
22 #include "formula/string_utils.hpp"
24 #include "tooltips.hpp"
25 #include "gettext.hpp"
26 
27 #include "map/location.hpp"
28 #include "sdl/rect.hpp"
29 #include "team.hpp"
30 #include "units/unit.hpp"
31 #include "units/map.hpp"
32 
33 namespace editor {
34 
35 
37 {
38  if (hex == previous_move_hex_) {
39  return;
40  }
41 
42  update_brush_highlights(disp, hex);
43 
44  std::set<map_location> adjacent_set;
46  adjacent_set.insert(adj);
47  }
48 
49  disp.invalidate(adjacent_set);
50  previous_move_hex_ = hex;
51 
52  const unit_map& units = disp.get_units();
53  const unit_map::const_unit_iterator unit_it = units.find(hex);
54  if (unit_it != units.end()) {
55 
57 
58  SDL_Rect rect;
59  rect.x = disp.get_location_x(hex);
60  rect.y = disp.get_location_y(hex);
61  rect.h = disp.hex_size();
62  rect.w = disp.hex_size();
63  std::stringstream str;
64  str << _("Identifier: ") << unit_it->id() << "\n";
65  if(unit_it->name() != "") {
66  str << _("Name: ") << unit_it->name() << "\n";
67  }
68  str << _("Type: ") << unit_it->type_name() << "\n"
69  << _("Level: ") << unit_it->level() << "\n"
70  << _("Cost: ") << unit_it->cost() << "\n";
72  tooltips::add_tooltip(rect, str.str());
73  }
74  else {
75  set_mouse_overlay(disp);
76  }
77 }
78 
79 std::unique_ptr<editor_action> mouse_action_unit::click_left(editor_display& disp, int x, int y)
80 {
81  start_hex_ = disp.hex_clicked_on(x, y);
82  if (!disp.get_map().on_board(start_hex_)) {
83  return nullptr;
84  }
85 
86  const unit_map& units = disp.get_units();
87  const unit_map::const_unit_iterator unit_it = units.find(start_hex_);
88  if (unit_it != units.end())
89  set_unit_mouse_overlay(disp, unit_it->type());
90 
91  click_ = true;
92  return nullptr;
93 }
94 
95 std::unique_ptr<editor_action> mouse_action_unit::drag_left(editor_display& disp, int x, int y, bool& /*partial*/, editor_action* /*last_undo*/)
96 {
97  map_location hex = disp.hex_clicked_on(x, y);
98  click_ = (hex == start_hex_);
99  return nullptr;
100 }
101 
102 std::unique_ptr<editor_action> mouse_action_unit::up_left(editor_display& disp, int x, int y)
103 {
104  if (!click_) return nullptr;
105  click_ = false;
106  map_location hex = disp.hex_clicked_on(x, y);
107  if (!disp.get_map().on_board(hex)) {
108  return nullptr;
109  }
110 
112 
113  // Does this serve a purpose other than making sure the type is built?
114  // (Calling unit_types.build_unit_type(type) would now accomplish that
115  // with less overhead.)
116  const std::string& type_id = type.id();
117  const unit_type *new_unit_type = unit_types.find(type_id);
118  if (!new_unit_type) {
119  //TODO rewrite the error message.
120  ERR_ED << "create unit dialog returned inexistent or unusable unit_type id '" << type_id << "'";
121  return nullptr;
122  }
123 
124  const unit_type &ut = *new_unit_type;
125  unit_race::GENDER gender = ut.genders().front();
126 
127  unit_ptr new_unit = unit::create(ut, disp.viewing_side(), true, gender);
128  auto action = std::make_unique<editor_action_unit>(hex, *new_unit);
129  return action;
130 }
131 
132 std::unique_ptr<editor_action> mouse_action_unit::drag_end_left(editor_display& disp, int x, int y)
133 {
134  if (click_) return nullptr;
135 
136  map_location hex = disp.hex_clicked_on(x, y);
137  if (!disp.get_map().on_board(hex))
138  return nullptr;
139 
140  const unit_map& units = disp.get_units();
141  const unit_map::const_unit_iterator unit_it = units.find(start_hex_);
142  if (unit_it == units.end())
143  return nullptr;
144 
145  return std::make_unique<editor_action_unit_replace>(start_hex_, hex);
146 }
147 
149 {
151  set_unit_mouse_overlay(disp, u);
152 }
153 
155 {
156  std::stringstream filename;
157  filename << u.image() << "~RC(" << u.flag_rgb() << '>'
158  << team::get_side_color_id(disp.viewing_side()) << ')';
159 
160  disp.set_mouseover_hex_overlay(image::get_texture(filename.str()));
161 }
162 
163 
164 } //end namespace editor
Editor action classes.
int viewing_side() const
The 1-based equivalent of the 0-based viewing_team() function.
Definition: display.hpp:130
const unit_map & get_units() const
Definition: display.hpp:148
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:562
bool invalidate(const map_location &loc)
Function to invalidate a specific tile for redrawing.
Definition: display.cpp:3122
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:264
int get_location_x(const map_location &loc) const
Functions to get the on-screen positions of hexes.
Definition: display.cpp:712
const gamemap & get_map() const
Definition: display.hpp:105
int get_location_y(const map_location &loc) const
Definition: display.cpp:717
Base class for all editor actions.
Definition: action_base.hpp:42
void set_mouseover_hex_overlay(const texture &image)
Sets texture to be drawn in hex under the mouse's location.
const Item & selected_fg_item() const
Return the currently selected foreground/background item.
virtual void set_mouse_overlay(editor_display &disp) override
Set the mouse overlay for this action.
std::unique_ptr< editor_action > click_left(editor_display &disp, int x, int y) override
TODO.
std::unique_ptr< editor_action > drag_left(editor_display &disp, int x, int y, bool &partial, editor_action *last_undo) override
Drag operation.
std::unique_ptr< editor_action > up_left(editor_display &disp, int x, int y) override
TODO.
std::unique_ptr< editor_action > drag_end_left(editor_display &disp, int x, int y) override
Drag end replaces the unit when clicked left, or adjusts the facing when clicked right.
void set_unit_mouse_overlay(editor_display &disp, const unit_type &u)
void move(editor_display &disp, const map_location &hex) override
Mouse move (not a drag).
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.
map_location previous_move_hex_
The hex previously used in move operations.
bool on_board(const map_location &loc) const
Tell if a location is on the map.
Definition: map.cpp:385
static std::string get_side_color_id(unsigned side)
Definition: team.cpp:969
Container associating units to locations.
Definition: map.hpp:99
unit_iterator end()
Definition: map.hpp:429
unit_iterator find(std::size_t id)
Definition: map.cpp:301
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:1246
A single unit type that the player may recruit.
Definition: types.hpp:46
const std::string & image() const
Definition: types.hpp:179
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:251
const std::string & flag_rgb() const
Definition: types.cpp:720
static unit_ptr create(const config &cfg, bool use_traits=false, const vconfig *vcfg=nullptr)
Initializes a unit from a config.
Definition: unit.hpp:202
#define ERR_ED
static std::string _(const char *str)
Definition: gettext.hpp:93
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.cpp:475
void rect(const SDL_Rect &rect)
Draw a rectangle.
Definition: draw.cpp:141
Manage the empty-palette in the editor.
Definition: action.cpp:31
texture get_texture(const image::locator &i_locator, TYPE type, bool skip_cache)
Returns an image texture suitable for hardware-accelerated rendering.
Definition: picture.cpp:985
int add_tooltip(const SDL_Rect &origin, const std::string &message, const std::string &action)
Definition: tooltips.cpp:237
void clear_tooltips()
Definition: tooltips.cpp:179
std::shared_ptr< unit > unit_ptr
Definition: ptr.hpp:26
Contains the SDL_Rect helper code.
Encapsulates the map of the game.
Definition: location.hpp:38
An abstract description of a rectangle with integer coordinates.
Definition: rect.hpp:47
unit_type_data unit_types
Definition: types.cpp:1463