The Battle for Wesnoth  1.17.0-dev
mouse_action_map_label.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 
24 
26 
27 #include "font/standard_colors.hpp"
28 #include "color.hpp"
29 
30 namespace editor {
31 
32 std::unique_ptr<editor_action> mouse_action_map_label::click_left(editor_display& disp, int x, int y)
33 {
34  click_ = true;
35  map_location hex = disp.hex_clicked_on(x, y);
36  clicked_on_ = hex;
37  return nullptr;
38 }
39 
40 std::unique_ptr<editor_action> mouse_action_map_label::drag_left(editor_display& disp, int x, int y
41  , bool& /*partial*/, editor_action* /*last_undo*/)
42 {
43  map_location hex = disp.hex_clicked_on(x, y);
44  click_ = (hex == clicked_on_);
45  return nullptr;
46 }
47 
48 std::unique_ptr<editor_action> mouse_action_map_label::up_left(editor_display& disp, int x, int y)
49 {
50  if (!click_) return nullptr;
51  click_ = false;
52 
53  const map_location hex = disp.hex_clicked_on(x, y);
54  if (!disp.get_map().on_board(hex)) {
55  return nullptr;
56  }
57 
58  const terrain_label* old_label = disp.get_controller().get_current_map_context().get_labels().get_label(hex);
59  std::string label = old_label ? old_label->text() : "";
60  std::string team_name = old_label ? old_label->team_name() : "";
61  std::string category = old_label ? old_label->category() : "";
62  bool visible_shroud = old_label ? old_label->visible_in_shroud() : false;
63  bool visible_fog = old_label ? old_label->visible_in_fog() : true;
64  bool immutable = old_label ? old_label->immutable() : true;
65  color_t color = old_label ? old_label->color() : font::NORMAL_COLOR;
66 
67  gui2::dialogs::editor_edit_label d(label, immutable, visible_fog, visible_shroud, color, category);
68 
69  std::unique_ptr<editor_action> a;
70  if(d.show()) {
71  a = std::make_unique<editor_action_label>(hex, label, team_name, color
72  , visible_fog, visible_shroud, immutable, category);
73  update_brush_highlights(disp, hex);
74  }
75  return a;
76 }
77 
78 std::unique_ptr<editor_action> mouse_action_map_label::click_right(editor_display& /*disp*/, int /*x*/, int /*y*/)
79 {
80  return nullptr;
81 }
82 
83 std::unique_ptr<editor_action> mouse_action_map_label::up_right(editor_display& disp, int x, int y)
84 {
85  map_location hex = disp.hex_clicked_on(x, y);
86 
87  //TODO
88 // const terrain_label* clicked_label = disp.map().get_map_labels().get_label(hex);
89  //if (!clicked_label)
90  // return nullptr;
91 
92  return std::make_unique<editor_action_label_delete>(hex);
93 }
94 
95 std::unique_ptr<editor_action> mouse_action_map_label::drag_end_left(editor_display& disp, int x, int y)
96 {
97  if (click_) return nullptr;
98 
99  map_location hex = disp.hex_clicked_on(x, y);
100  if (!disp.get_map().on_board(hex))
101  return nullptr;
102 
103  const terrain_label* dragged_label = disp
104  .get_controller()
106  .get_labels()
108 
109  // Return nullptr if the dragged label doesn't exist
110  if (!dragged_label) {
111  return nullptr;
112  }
113 
114  // Create action chain to:
115  // 1. Delete label in initial hex
116  // 2. Delete label in target hex if it exists, otherwise will no-op
117  // 3. Create label in target hex
118  auto chain = std::make_unique<editor_action_chain>();
119  chain->append_action(std::make_unique<editor_action_label_delete>(clicked_on_));
120  chain->append_action(std::make_unique<editor_action_label_delete>(hex));
121  chain->append_action(
122  std::make_unique<editor_action_label>(
123  hex,
124  dragged_label->text(),
125  dragged_label->team_name(),
126  dragged_label->color(),
127  dragged_label->visible_in_fog(),
128  dragged_label->visible_in_shroud(),
129  dragged_label->immutable(),
130  dragged_label->category()
131  )
132  );
133 
134  return chain;
135 }
136 
138 {
139  surface image60 = image::get_image("icons/action/editor-tool-label_60.png");
140 
141  //TODO avoid hardcoded hex field size
142  surface image(72,72);
143 
144  SDL_Rect r {6, 6, 0, 0};
145  sdl_blit(image60, nullptr, image, &r);
146 
147  uint8_t alpha = 196;
148  int size = image->w;
149  int zoom = static_cast<int>(size * disp.get_zoom_factor());
150 
151  // Add the alpha factor and scale the image
152  adjust_surface_alpha(image, alpha);
153  image = scale_surface(image, zoom, zoom);
154  disp.set_mouseover_hex_overlay(image);
155 }
156 
157 
158 } //end namespace editor
std::unique_ptr< editor_action > drag_end_left(editor_display &disp, int x, int y) override
Replaces the label under the mouse with the dragged label.
virtual void set_mouse_overlay(editor_display &disp) override
Set the mouse overlay for this action.
surface get_image(const image::locator &i_locator, TYPE type)
Caches and returns an image.
Definition: picture.cpp:816
Editor action classes.
const color_t & color() const
Definition: label.hpp:184
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
const std::string & category() const
Definition: label.hpp:159
#define a
std::unique_ptr< editor_action > drag_left(editor_display &disp, int x, int y, bool &partial, editor_action *last_undo) override
Drags a label.
const t_string & text() const
Definition: label.hpp:139
map_labels & get_labels()
const std::string & team_name() const
Definition: label.hpp:154
bool visible_in_shroud() const
Definition: label.hpp:169
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
#define d
bool show(const unsigned auto_close_time=0)
Shows the window.
map_context & get_current_map_context() const
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 > up_right(editor_display &disp, int x, int y) override
Right click erases the label under the mouse.
std::string label
What to show in the filter&#39;s drop-down list.
Definition: manager.cpp:217
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...
Dialog for editing gamemap labels.
Definition: edit_label.hpp:34
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
const terrain_label * get_label(const map_location &loc, const std::string &team_name) const
Definition: label.hpp:48
const color_t NORMAL_COLOR
Encapsulates the map of the game.
Definition: location.hpp:38
bool visible_in_fog() const
Definition: label.hpp:164
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::unique_ptr< editor_action > click_left(editor_display &disp, int x, int y) override
A click, possibly the beginning of a drag.
std::unique_ptr< editor_action > up_left(editor_display &disp, int x, int y) override
Left click displays a dialog that is used for entering the label string.
const gamemap & get_map() const
Definition: display.hpp:96
To store label data Class implements logic for rendering.
Definition: label.hpp:110
editor_controller & get_controller()
Functions to load and save images from/to disk.
void sdl_blit(const surface &src, SDL_Rect *src_rect, surface &dst, SDL_Rect *dst_rect)
Definition: utils.hpp:32
bool immutable() const
Definition: label.hpp:174
std::unique_ptr< editor_action > click_right(editor_display &disp, int x, int y) override
A click, possibly the beginning of a drag.