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