The Battle for Wesnoth  1.17.0-dev
mouse_action_item.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2021
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 "tooltips.hpp"
23 #include "gettext.hpp"
24 
25 #include "map/location.hpp"
26 
27 namespace editor {
28 
29 
31 {
32  if (hex != previous_move_hex_) {
33 
34  update_brush_highlights(disp, hex);
35 
36  std::set<map_location> adjacent_set;
38  adjacent_set.insert(adj);
39  }
40 
41  disp.invalidate(adjacent_set);
42  previous_move_hex_ = hex;
43  }
44 }
45 
46 std::unique_ptr<editor_action> mouse_action_item::click_left(editor_display& disp, int x, int y)
47 {
48  start_hex_ = disp.hex_clicked_on(x, y);
49  if (!disp.get_map().on_board(start_hex_)) {
50  return nullptr;
51  }
52 
54  disp.add_overlay(start_hex_, item.image, item.halo, "", "", true);
55 
56  click_ = true;
57  return nullptr;
58 }
59 
60 std::unique_ptr<editor_action> mouse_action_item::drag_left(editor_display& disp, int x, int y, bool& /*partial*/, editor_action* /*last_undo*/)
61 {
62  map_location hex = disp.hex_clicked_on(x, y);
63  click_ = (hex == start_hex_);
64  return nullptr;
65 }
66 
67 std::unique_ptr<editor_action> mouse_action_item::up_left(editor_display& disp, int x, int y)
68 {
69  if (!click_) return nullptr;
70  click_ = false;
71  map_location hex = disp.hex_clicked_on(x, y);
72  if (!disp.get_map().on_board(hex)) {
73  return nullptr;
74  }
75 
76 // item_type type = item_palette_.selected_fg_item();
77 //
78 // // Does this serve a purpose other than making sure the type is built?
79 // // (Calling item_types.build_item_type(type) would now accomplish that
80 // // with less overhead.)
81 // const std::string& type_id = type.id();
82 // const item_type *new_item_type = item_types.find(type_id);
83 // if (!new_item_type) {
84 // //TODO rewrite the error message.
85 // ERR_ED << "create item dialog returned inexistent or unusable item_type id '" << type_id << "'" << std::endl;
86 // return nullptr;
87 // }
88 //
89 // const item_type &ut = *new_item_type;
90 // item_race::GENDER gender = ut.genders().front();
91 //
92 // item new_item(ut, disp.viewing_side(), true, gender);
93 // editor_action* action = new editor_action_item(hex, new_item);
94 // return action;
95 
96 // \todo in #5070: there's a load of commented-out code in this file, it should probably
97 // all be deleted. For the function that this comment is in, I've left the commented-out
98 // code in because it seems the not-commented code should also be reviewed. AFAICS, the
99 // entire function (including the not-commented code) could be deleted, and fall back to
100 // the parent class' implementation of just returning nullptr.
101 
102  return nullptr;
103 }
104 
105 std::unique_ptr<editor_action> mouse_action_item::drag_end_left(editor_display& disp, int x, int y)
106 {
107  if (click_) return nullptr;
108 
109  map_location hex = disp.hex_clicked_on(x, y);
110  if (!disp.get_map().on_board(hex))
111  return nullptr;
112 
113 // const item_map& items = disp.get_items();
114 // const item_map::const_item_iterator item_it = items.find(start_hex_);
115 // if (item_it == items.end())
116 // return nullptr;
117 
118  return std::make_unique<editor_action_item_replace>(start_hex_, hex);
119 }
120 
122 {
124  set_item_mouse_overlay(disp, item);
125 }
126 
128 {
129 
130  std::stringstream filename;
131  filename << u.image; // << "~RC(" << u.flag_rgb() << '>'
132  // << team::get_side_color_id(disp.viewing_side()) << ')';
133 
134  surface image(image::get_image(filename.str()));
135  uint8_t alpha = 196;
136  //TODO don't hardcode
137  int size = 72;
138  //int size = image->w;
139  int zoom = static_cast<int>(size * disp.get_zoom_factor());
140 
141  // Add the alpha factor and scale the image
142  adjust_surface_alpha(image, alpha);
143  image = scale_surface(image, zoom, zoom);
144  disp.set_mouseover_hex_overlay(image);
145 }
146 
147 
148 } //end namespace editor
surface get_image(const image::locator &i_locator, TYPE type)
Caches and returns an image.
Definition: picture.cpp:816
map_location previous_move_hex_
The hex previously used in move operations.
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
bool invalidate(const map_location &loc)
Function to invalidate a specific tile for redrawing.
Definition: display.cpp:3026
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:599
static double get_zoom_factor()
Returns the current zoom factor.
Definition: display.hpp:263
void move(editor_display &disp, const map_location &hex) override
Mouse move (not a drag).
std::unique_ptr< editor_action > up_left(editor_display &disp, int x, int y) override
TODO.
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:197
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:87
void adjust_surface_alpha(surface &surf, fixed_t amount)
Definition: utils.cpp:1087
std::unique_ptr< editor_action > drag_end_left(editor_display &disp, int x, int y) override
Drag end replaces the item when clicked left, or adjusts the facing when clicked right.
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...
std::unique_ptr< editor_action > drag_left(editor_display &disp, int x, int y, bool &partial, editor_action *last_undo) override
Drag operation.
void add_overlay(const map_location &loc, const std::string &image, const std::string &halo="", const std::string &team_name="", const std::string &item_id="", bool visible_under_fog=true, float z_order=0)
Functions to add and remove overlays from locations.
Definition: display.cpp:136
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:448
Manage the empty-palette in the editor.
Definition: action.cpp:30
Encapsulates the map of the game.
Definition: location.hpp:38
void set_item_mouse_overlay(editor_display &disp, const overlay &u)
Base class for all editor actions.
Definition: action_base.hpp:41
bool on_board(const map_location &loc) const
Tell if a location is on the map.
Definition: map.cpp:385
std::string halo
Definition: overlay.hpp:53
const gamemap & get_map() const
Definition: display.hpp:96
const Item & selected_fg_item() const
Return the currently selected foreground/background item.
Functions to load and save images from/to disk.
virtual void set_mouse_overlay(editor_display &disp) override
Set the mouse overlay for this action.
Editor action classes.
std::unique_ptr< editor_action > click_left(editor_display &disp, int x, int y) override
TODO.
std::pair< std::string, unsigned > item
Definition: help_impl.hpp:410
std::string image
Definition: overlay.hpp:52