The Battle for Wesnoth  1.19.5+dev
mouse_action_unit.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2024
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"
23 #include "tooltips.hpp"
24 #include "gettext.hpp"
25 
26 #include "map/location.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  return;
38  }
39 
40  update_brush_highlights(disp, hex);
41 
42  std::set<map_location> adjacent_set;
44  adjacent_set.insert(adj);
45  }
46 
47  disp.invalidate(adjacent_set);
48  previous_move_hex_ = hex;
49 
50  const unit_map& units = disp.context().units();
51  const unit_map::const_unit_iterator unit_it = units.find(hex);
52  if (unit_it != units.end()) {
53 
55 
56  SDL_Rect rect = disp.get_location_rect(hex);
57  std::stringstream str;
58  str << _("Identifier: ") << unit_it->id() << "\n";
59  if(unit_it->name() != "") {
60  str << _("Name: ") << unit_it->name() << "\n";
61  }
62  str << _("Type: ") << unit_it->type_name() << "\n"
63  << _("Level: ") << unit_it->level() << "\n"
64  << _("Cost: ") << unit_it->cost() << "\n";
66  tooltips::add_tooltip(rect, str.str());
67  }
68  else {
69  set_mouse_overlay(disp);
70  }
71 }
72 
73 std::unique_ptr<editor_action> mouse_action_unit::click_left(editor_display& disp, int x, int y)
74 {
75  start_hex_ = disp.hex_clicked_on(x, y);
76  if (!disp.get_map().on_board(start_hex_)) {
77  return nullptr;
78  }
79 
80  const unit_map& units = disp.context().units();
81  const unit_map::const_unit_iterator unit_it = units.find(start_hex_);
82  if (unit_it != units.end())
83  set_unit_mouse_overlay(disp, unit_it->type());
84 
85  click_ = true;
86  return nullptr;
87 }
88 
89 std::unique_ptr<editor_action> mouse_action_unit::drag_left(editor_display& disp, int x, int y, bool& /*partial*/, editor_action* /*last_undo*/)
90 {
91  map_location hex = disp.hex_clicked_on(x, y);
92  click_ = (hex == start_hex_);
93  return nullptr;
94 }
95 
96 std::unique_ptr<editor_action> mouse_action_unit::up_left(editor_display& disp, int x, int y)
97 {
98  if (!click_) return nullptr;
99  click_ = false;
100  map_location hex = disp.hex_clicked_on(x, y);
101  if (!disp.get_map().on_board(hex)) {
102  return nullptr;
103  }
104 
106 
107  // Does this serve a purpose other than making sure the type is built?
108  // (Calling unit_types.build_unit_type(type) would now accomplish that
109  // with less overhead.)
110  const std::string& type_id = type.id();
111  const unit_type *new_unit_type = unit_types.find(type_id);
112  if (!new_unit_type) {
113  //TODO rewrite the error message.
114  ERR_ED << "create unit dialog returned inexistent or unusable unit_type id '" << type_id << "'";
115  return nullptr;
116  }
117 
118  const unit_type &ut = *new_unit_type;
119  unit_race::GENDER gender = ut.genders().front();
120 
121  unit_ptr new_unit = unit::create(ut, disp.viewing_team().side(), true, gender);
122  auto action = std::make_unique<editor_action_unit>(hex, *new_unit);
123  return action;
124 }
125 
126 std::unique_ptr<editor_action> mouse_action_unit::drag_end_left(editor_display& disp, int x, int y)
127 {
128  if (click_) return nullptr;
129 
130  map_location hex = disp.hex_clicked_on(x, y);
131  if (!disp.get_map().on_board(hex))
132  return nullptr;
133 
134  const unit_map& units = disp.context().units();
135  const unit_map::const_unit_iterator unit_it = units.find(start_hex_);
136  if (unit_it == units.end())
137  return nullptr;
138 
139  return std::make_unique<editor_action_unit_replace>(start_hex_, hex);
140 }
141 
143 {
145  set_unit_mouse_overlay(disp, u);
146 }
147 
149 {
150  std::stringstream filename;
151  filename << u.image() << "~RC(" << u.flag_rgb() << '>'
152  << team::get_side_color_id(disp.viewing_team().side()) << ')';
153 
155 }
156 
157 
158 } //end namespace editor
Editor action classes.
virtual const unit_map & units() const =0
const team & viewing_team() const
Definition: display.cpp:342
bool invalidate(const map_location &loc)
Function to invalidate a specific tile for redrawing.
Definition: display.cpp:3087
const display_context & context() const
Definition: display.hpp:193
rect get_location_rect(const map_location &loc) const
Returns the on-screen rect corresponding to a loc.
Definition: display.cpp:687
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:538
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 editor_map & get_map() const
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
int side() const
Definition: team.hpp:175
static std::string get_side_color_id(unsigned side)
Definition: team.cpp:972
Container associating units to locations.
Definition: map.hpp:98
unit_iterator end()
Definition: map.hpp:428
unit_iterator find(std::size_t id)
Definition: map.cpp:302
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:1265
A single unit type that the player may recruit.
Definition: types.hpp:43
const std::string & image() const
Definition: types.hpp:176
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:249
const std::string & flag_rgb() const
Definition: types.cpp:722
static unit_ptr create(const config &cfg, bool use_traits=false, const vconfig *vcfg=nullptr)
Initializes a unit from a config.
Definition: unit.hpp:201
#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:479
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:920
int add_tooltip(const SDL_Rect &origin, const std::string &message, const std::string &action)
Definition: tooltips.cpp:299
void clear_tooltips()
Definition: tooltips.cpp:241
std::shared_ptr< unit > unit_ptr
Definition: ptr.hpp:26
std::string filename
Filename.
Encapsulates the map of the game.
Definition: location.hpp:45
An abstract description of a rectangle with integer coordinates.
Definition: rect.hpp:47
unit_type_data unit_types
Definition: types.cpp:1500