The Battle for Wesnoth  1.15.12+dev
mouse_action_map_label.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2018 by Fabian Mueller <fabianmueller5@gmx.de>
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 #define GETTEXT_DOMAIN "wesnoth-editor"
15 
18 
22 
24 
25 #include "font/standard_colors.hpp"
26 #include "color.hpp"
27 
28 namespace editor {
29 
30 std::unique_ptr<editor_action> mouse_action_map_label::click_left(editor_display& disp, int x, int y)
31 {
32  click_ = true;
33  map_location hex = disp.hex_clicked_on(x, y);
34  clicked_on_ = hex;
35  return nullptr;
36 }
37 
38 std::unique_ptr<editor_action> mouse_action_map_label::drag_left(editor_display& disp, int x, int y
39  , bool& /*partial*/, editor_action* /*last_undo*/)
40 {
41  map_location hex = disp.hex_clicked_on(x, y);
42  click_ = (hex == clicked_on_);
43  return nullptr;
44 }
45 
46 std::unique_ptr<editor_action> mouse_action_map_label::up_left(editor_display& disp, int x, int y)
47 {
48  if (!click_) return nullptr;
49  click_ = false;
50 
51  const map_location hex = disp.hex_clicked_on(x, y);
52  if (!disp.get_map().on_board(hex)) {
53  return nullptr;
54  }
55 
56  const terrain_label* old_label = disp.get_controller().get_current_map_context().get_labels().get_label(hex);
57  std::string label = old_label ? old_label->text() : "";
58  std::string team_name = old_label ? old_label->team_name() : "";
59  std::string category = old_label ? old_label->category() : "";
60  bool visible_shroud = old_label ? old_label->visible_in_shroud() : false;
61  bool visible_fog = old_label ? old_label->visible_in_fog() : true;
62  bool immutable = old_label ? old_label->immutable() : true;
63  color_t color = old_label ? old_label->color() : font::NORMAL_COLOR;
64 
65  gui2::dialogs::editor_edit_label d(label, immutable, visible_fog, visible_shroud, color, category);
66 
67  std::unique_ptr<editor_action> a;
68  if(d.show()) {
69  a = std::make_unique<editor_action_label>(hex, label, team_name, color
70  , visible_fog, visible_shroud, immutable, category);
71  update_brush_highlights(disp, hex);
72  }
73  return a;
74 }
75 
76 std::unique_ptr<editor_action> mouse_action_map_label::click_right(editor_display& /*disp*/, int /*x*/, int /*y*/)
77 {
78  return nullptr;
79 }
80 
81 std::unique_ptr<editor_action> mouse_action_map_label::up_right(editor_display& disp, int x, int y)
82 {
83  map_location hex = disp.hex_clicked_on(x, y);
84 
85  //TODO
86 // const terrain_label* clicked_label = disp.map().get_map_labels().get_label(hex);
87  //if (!clicked_label)
88  // return nullptr;
89 
90  return std::make_unique<editor_action_label_delete>(hex);
91 }
92 
93 std::unique_ptr<editor_action> mouse_action_map_label::drag_end_left(editor_display& disp, int x, int y)
94 {
95  if (click_) return nullptr;
96 
97  map_location hex = disp.hex_clicked_on(x, y);
98  if (!disp.get_map().on_board(hex))
99  return nullptr;
100 
101  const terrain_label* dragged_label = disp
102  .get_controller()
104  .get_labels()
106 
107  // Return nullptr if the dragged label doesn't exist
108  if (!dragged_label) {
109  return nullptr;
110  }
111 
112  // Create action chain to:
113  // 1. Delete label in initial hex
114  // 2. Delete label in target hex if it exists, otherwise will no-op
115  // 3. Create label in target hex
116  auto chain = std::make_unique<editor_action_chain>();
117  chain->append_action(std::make_unique<editor_action_label_delete>(clicked_on_));
118  chain->append_action(std::make_unique<editor_action_label_delete>(hex));
119  chain->append_action(
120  std::make_unique<editor_action_label>(
121  hex,
122  dragged_label->text(),
123  dragged_label->team_name(),
124  dragged_label->color(),
125  dragged_label->visible_in_fog(),
126  dragged_label->visible_in_shroud(),
127  dragged_label->immutable(),
128  dragged_label->category()
129  )
130  );
131 
132  return chain;
133 }
134 
136 {
137  surface image60 = image::get_image("icons/action/editor-tool-label_60.png");
138 
139  //TODO avoid hardcoded hex field size
140  surface image(72,72);
141 
142  SDL_Rect r {6, 6, 0, 0};
143  sdl_blit(image60, nullptr, image, &r);
144 
145  uint8_t alpha = 196;
146  int size = image->w;
147  int zoom = static_cast<int>(size * disp.get_zoom_factor());
148 
149  // Add the alpha factor and scale the image
150  adjust_surface_alpha(image, alpha);
151  image = scale_surface(image, zoom, zoom);
152  disp.set_mouseover_hex_overlay(image);
153 }
154 
155 
156 } //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:815
Editor action classes.
const color_t & color() const
Definition: label.hpp:183
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:261
const std::string & category() const
Definition: label.hpp:158
#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:138
map_labels & get_labels()
const std::string & team_name() const
Definition: label.hpp:153
bool visible_in_shroud() const
Definition: label.hpp:168
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
#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:86
void adjust_surface_alpha(surface &surf, fixed_t amount)
Definition: utils.cpp:1086
std::unique_ptr< editor_action > up_right(editor_display &disp, int x, int y) override
Right click erases the label under the mouse.
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:33
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:446
Manage the empty-palette in the editor.
Definition: action.cpp:29
const terrain_label * get_label(const map_location &loc, const std::string &team_name) const
Definition: label.hpp:47
const color_t NORMAL_COLOR
Encapsulates the map of the game.
Definition: location.hpp:37
bool visible_in_fog() const
Definition: label.hpp:163
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:380
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:95
To store label data Class implements logic for rendering.
Definition: label.hpp:109
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:31
bool immutable() const
Definition: label.hpp:173
std::unique_ptr< editor_action > click_right(editor_display &disp, int x, int y) override
A click, possibly the beginning of a drag.