-
-
Notifications
You must be signed in to change notification settings - Fork 991
/
mouse_action.cpp
474 lines (397 loc) · 14.4 KB
/
mouse_action.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/*
Copyright (C) 2008 - 2018 by Tomasz Sniatowski <kailoran@gmail.com>
Part of the Battle for Wesnoth Project https://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#define GETTEXT_DOMAIN "wesnoth-editor"
#include "editor/action/action.hpp"
#include "editor/toolkit/brush.hpp"
#include "editor/editor_display.hpp"
#include "editor/action/mouse/mouse_action.hpp"
#include "gettext.hpp"
#include "editor/palette/terrain_palettes.hpp"
namespace editor {
bool mouse_action::has_context_menu() const
{
return false;
}
void mouse_action::move(editor_display& disp, const map_location& hex)
{
if (hex != previous_move_hex_) {
update_brush_highlights(disp, hex);
previous_move_hex_ = hex;
}
}
void mouse_action::update_brush_highlights(editor_display& disp, const map_location& hex)
{
disp.set_brush_locs(affected_hexes(disp, hex));
}
std::set<map_location> mouse_action::affected_hexes(
editor_display& /*disp*/, const map_location& hex)
{
std::set<map_location> res;
res.insert(hex);
return res;
}
std::unique_ptr<editor_action> mouse_action::drag_left(editor_display& /*disp*/,
int /*x*/, int /*y*/, bool& /*partial*/, editor_action* /*last_undo*/)
{
return nullptr;
}
std::unique_ptr<editor_action> mouse_action::drag_right(editor_display& /*disp*/,
int /*x*/, int /*y*/, bool& /*partial*/, editor_action* /*last_undo*/)
{
return nullptr;
}
std::unique_ptr<editor_action> mouse_action::drag_end_left(
editor_display& /*disp*/, int /*x*/, int /*y*/)
{
return nullptr;
}
std::unique_ptr<editor_action> mouse_action::drag_end_right(
editor_display& /*disp*/, int /*x*/, int /*y*/)
{
return nullptr;
}
std::unique_ptr<editor_action> mouse_action::up_right(
editor_display& /*disp*/, int /*x*/, int /*y*/)
{
return nullptr;
}
std::unique_ptr<editor_action> mouse_action::up_left(
editor_display& /*disp*/, int /*x*/, int /*y*/)
{
return nullptr;
}
std::unique_ptr<editor_action> mouse_action::key_event(
editor_display& disp, const SDL_Event& event)
{
if (!has_alt_modifier() && (event.key.keysym.sym >= '1' && event.key.keysym.sym <= '9')) {
int side = event.key.keysym.sym - '0';
if (side >= 1 && side <= gamemap::MAX_PLAYERS) {
map_location pos = disp.get_map().starting_position(side);
if (pos.valid()) {
disp.scroll_to_tile(pos, display::WARP);
}
}
return nullptr;
}
if (!disp.map().on_board(previous_move_hex_) || event.type != SDL_KEYUP) {
return nullptr;
}
std::unique_ptr<editor_action> a;
if ((has_alt_modifier() && (event.key.keysym.sym >= '1' && event.key.keysym.sym <= '9'))
|| event.key.keysym.sym == SDLK_DELETE) {
int res = event.key.keysym.sym - '0';
if (res > gamemap::MAX_PLAYERS || event.key.keysym.sym == SDLK_DELETE) res = 0;
const std::string* old_id = disp.map().is_special_location(previous_move_hex_);
if (res == 0 && old_id != nullptr) {
a = std::make_unique<editor_action_starting_position>(map_location(), *old_id);
} else if (res > 0 && (old_id == nullptr || *old_id == std::to_string(res))) {
a = std::make_unique<editor_action_starting_position>(previous_move_hex_, std::to_string(res));
}
}
return a;
}
void mouse_action::set_mouse_overlay(editor_display& disp)
{
disp.set_mouseover_hex_overlay(nullptr);
}
bool mouse_action::has_alt_modifier() const
{
return key_[SDLK_RALT] || key_[SDLK_LALT];
}
bool mouse_action::has_shift_modifier() const
{
return key_[SDLK_RSHIFT] || key_[SDLK_LSHIFT];
}
bool mouse_action::has_ctrl_modifier() const
{
#ifdef __APPLE__
return key_[SDLK_RGUI] || key_[SDLK_LGUI];
#else
return key_[SDLK_RCTRL] || key_[SDLK_LCTRL];
#endif
}
void mouse_action::set_terrain_mouse_overlay(editor_display& disp, const t_translation::terrain_code & fg,
const t_translation::terrain_code & bg)
{
surface image_fg(image::get_image(disp.get_map().get_terrain_info(fg).editor_image()));
surface image_bg(image::get_image(disp.get_map().get_terrain_info(bg).editor_image()));
if (image_fg == nullptr || image_bg == nullptr) {
ERR_ED << "Missing terrain icon" << std::endl;
disp.set_mouseover_hex_overlay(nullptr);
return;
}
// Create a transparent surface of the right size.
surface image(image_fg->w, image_fg->h);
// For efficiency the size of the tile is cached.
// We assume all tiles are of the same size.
// The zoom factor can change, so it's not cached.
// NOTE: when zooming and not moving the mouse, there are glitches.
// Since the optimal alpha factor is unknown, it has to be calculated
// on the fly, and caching the surfaces makes no sense yet.
static const fixed_t alpha = 196;
static const int size = image_fg->w;
static const int half_size = size / 2;
static const int quarter_size = size / 4;
static const int offset = 2;
static const int new_size = half_size - 2;
// Blit left side
image_fg = scale_surface(image_fg, new_size, new_size);
SDL_Rect rcDestLeft {offset, quarter_size, 0, 0};
sdl_blit( image_fg, nullptr, image, &rcDestLeft );
// Blit right side
image_bg = scale_surface(image_bg, new_size, new_size);
SDL_Rect rcDestRight {half_size, quarter_size, 0, 0};
sdl_blit( image_bg, nullptr, image, &rcDestRight );
//apply mask so the overlay is contained within the mouseover hex
image = mask_surface(image, image::get_hexmask());
// Add the alpha factor
adjust_surface_alpha(image, alpha);
// scale the image
const unsigned int zoom = disp.hex_size();
if (zoom != game_config::tile_size) {
image = scale_surface(image, zoom, zoom);
}
// Set as mouseover
disp.set_mouseover_hex_overlay(image);
}
std::set<map_location> brush_drag_mouse_action::affected_hexes(
editor_display& /*disp*/, const map_location& hex)
{
return get_brush().project(hex);
}
std::unique_ptr<editor_action> brush_drag_mouse_action::click_left(editor_display& disp, int x, int y)
{
map_location hex = disp.hex_clicked_on(x, y);
previous_drag_hex_ = hex;
return click_perform_left(disp, affected_hexes(disp, hex));
}
std::unique_ptr<editor_action> brush_drag_mouse_action::click_right(editor_display& disp, int x, int y)
{
map_location hex = disp.hex_clicked_on(x, y);
previous_drag_hex_ = hex;
return click_perform_right(disp, affected_hexes(disp, hex));
}
std::unique_ptr<editor_action> brush_drag_mouse_action::drag_left(editor_display& disp,
int x, int y, bool& partial, editor_action* last_undo)
{
return drag_generic<&brush_drag_mouse_action::click_perform_left>(disp, x, y, partial, last_undo);
}
std::unique_ptr<editor_action> brush_drag_mouse_action::drag_right(editor_display& disp,
int x, int y, bool& partial, editor_action* last_undo)
{
return drag_generic<&brush_drag_mouse_action::click_perform_right>(disp, x, y, partial, last_undo);
}
std::unique_ptr<editor_action> brush_drag_mouse_action::drag_end(
editor_display& /*disp*/, int /*x*/, int /*y*/)
{
return nullptr;
}
template <std::unique_ptr<editor_action> (brush_drag_mouse_action::*perform_func)(editor_display&, const std::set<map_location>&)>
std::unique_ptr<editor_action> brush_drag_mouse_action::drag_generic(editor_display& disp, int x, int y, bool& partial, editor_action* last_undo)
{
map_location hex = disp.hex_clicked_on(x, y);
move(disp, hex);
if (hex != previous_drag_hex_) {
editor_action_extendable* last_undo_x = dynamic_cast<editor_action_extendable*>(last_undo);
LOG_ED << "Last undo is " << last_undo << " and as x " << last_undo_x << "\n";
partial = true;
auto a = (this->*perform_func)(disp, affected_hexes(disp, hex));
previous_drag_hex_ = hex;
return a;
} else {
return nullptr;
}
}
const brush& brush_drag_mouse_action::get_brush()
{
assert(brush_);
assert(*brush_);
return **brush_;
}
std::unique_ptr<editor_action> mouse_action_paint::click_left(editor_display& disp, int x, int y)
{
if (has_ctrl_modifier()) {
map_location hex = disp.hex_clicked_on(x, y);
terrain_palette_.select_fg_item(disp.map().get_terrain(hex));
return nullptr;
} else {
return brush_drag_mouse_action::click_left(disp, x, y);
}
}
std::unique_ptr<editor_action> mouse_action_paint::click_right(editor_display& disp, int x, int y)
{
if (has_ctrl_modifier()) {
map_location hex = disp.hex_clicked_on(x, y);
terrain_palette_.select_bg_item(disp.map().get_terrain(hex));
return nullptr;
} else {
return brush_drag_mouse_action::click_right(disp, x, y);
}
}
std::unique_ptr<editor_action> mouse_action_paint::click_perform_left(
editor_display& /*disp*/, const std::set<map_location>& hexes)
{
if (has_ctrl_modifier()) return nullptr;
// \todo why is this a chain and not an individual action? I guess that mouse drags were meant
// to be optimised with one undo for multiple hexes, but it seems that was never added.
auto chain = std::make_unique<editor_action_chain>();
chain->append_action(std::make_unique<editor_action_paint_area>(
hexes, terrain_palette_.selected_fg_item(), has_shift_modifier()));
return chain;
}
std::unique_ptr<editor_action> mouse_action_paint::click_perform_right(
editor_display& /*disp*/, const std::set<map_location>& hexes)
{
if (has_ctrl_modifier()) return nullptr;
// \todo why is this a chain and not an individual action?
auto chain = std::make_unique<editor_action_chain>();
chain->append_action(std::make_unique<editor_action_paint_area>(
hexes, terrain_palette_.selected_bg_item(), has_shift_modifier()));
return chain;
}
void mouse_action_paint::set_mouse_overlay(editor_display& disp)
{
set_terrain_mouse_overlay(disp, terrain_palette_.selected_fg_item(),
terrain_palette_.selected_bg_item());
}
bool mouse_action_paste::has_context_menu() const
{
return true;
}
std::set<map_location> mouse_action_paste::affected_hexes(
editor_display& /*disp*/, const map_location& hex)
{
return paste_.get_offset_area(hex);
}
std::unique_ptr<editor_action> mouse_action_paste::click_left(editor_display& disp, int x, int y)
{
map_location hex = disp.hex_clicked_on(x, y);
return std::make_unique<editor_action_paste>(paste_, hex);
}
std::unique_ptr<editor_action> mouse_action_paste::click_right(editor_display& /*disp*/, int /*x*/, int /*y*/)
{
return nullptr;
}
void mouse_action_paste::set_mouse_overlay(editor_display& disp)
{
surface image60 = image::get_image("icons/action/editor-paste_60.png");
//TODO avoid hardcoded hex field size
surface image(72,72);
SDL_Rect r {6, 6, 0, 0};
sdl_blit(image60, nullptr, image, &r);
uint8_t alpha = 196;
int size = image->w;
int zoom = static_cast<int>(size * disp.get_zoom_factor());
// Add the alpha factor and scale the image
adjust_surface_alpha(image, alpha);
image = scale_surface(image, zoom, zoom);
disp.set_mouseover_hex_overlay(image);
}
std::set<map_location> mouse_action_fill::affected_hexes(
editor_display& disp, const map_location& hex)
{
return disp.map().get_contiguous_terrain_tiles(hex);
}
std::unique_ptr<editor_action> mouse_action_fill::click_left(editor_display& disp, int x, int y)
{
map_location hex = disp.hex_clicked_on(x, y);
if (has_ctrl_modifier()) {
terrain_palette_.select_fg_item(disp.map().get_terrain(hex));
return nullptr;
} else {
/** @todo only take the base terrain into account when searching for contiguous terrain when painting base only */
//or use a different key modifier for that
return std::make_unique<editor_action_fill>(hex, terrain_palette_.selected_fg_item(),
has_shift_modifier());
}
}
std::unique_ptr<editor_action> mouse_action_fill::click_right(editor_display& disp, int x, int y)
{
map_location hex = disp.hex_clicked_on(x, y);
if (has_ctrl_modifier()) {
terrain_palette_.select_bg_item(disp.map().get_terrain(hex));
return nullptr;
} else {
/** @todo only take the base terrain into account when searching for contiguous terrain when painting base only */
//or use a different key modifier for that
return std::make_unique<editor_action_fill>(hex, terrain_palette_.selected_bg_item(),
has_shift_modifier());
}
}
void mouse_action_fill::set_mouse_overlay(editor_display& disp)
{
set_terrain_mouse_overlay(disp, terrain_palette_.selected_fg_item(),
terrain_palette_.selected_bg_item());
}
std::unique_ptr<editor_action> mouse_action_starting_position::up_left(editor_display& disp, int x, int y)
{
if (!click_) return nullptr;
click_ = false;
map_location hex = disp.hex_clicked_on(x, y);
if (!disp.map().on_board(hex)) {
return nullptr;
}
auto player_starting_at_hex = disp.map().is_special_location(hex);
if (has_ctrl_modifier()) {
if (player_starting_at_hex) {
location_palette_.add_item(*player_starting_at_hex);
}
return nullptr;
}
std::string new_player_at_hex = location_palette_.selected_item();
std::unique_ptr<editor_action> a;
if(!player_starting_at_hex || new_player_at_hex != *player_starting_at_hex) {
// Set a starting position
a = std::make_unique<editor_action_starting_position>(hex, new_player_at_hex);
}
else {
// Erase current starting position
a = std::make_unique<editor_action_starting_position>(map_location(), *player_starting_at_hex);
}
update_brush_highlights(disp, hex);
return a;
}
std::unique_ptr<editor_action> mouse_action_starting_position::click_left(editor_display& /*disp*/, int /*x*/, int /*y*/)
{
click_ = true;
return nullptr;
}
std::unique_ptr<editor_action> mouse_action_starting_position::up_right(editor_display& disp, int x, int y)
{
map_location hex = disp.hex_clicked_on(x, y);
auto player_starting_at_hex = disp.map().is_special_location(hex);
if (player_starting_at_hex != nullptr) {
return std::make_unique<editor_action_starting_position>(map_location(), *player_starting_at_hex);
} else {
return nullptr;
}
}
std::unique_ptr<editor_action> mouse_action_starting_position::click_right(editor_display& /*disp*/, int /*x*/, int /*y*/)
{
return nullptr;
}
void mouse_action_starting_position::set_mouse_overlay(editor_display& disp)
{
surface image60 = image::get_image("icons/action/editor-tool-starting-position_60.png");
//TODO avoid hardcoded hex field size
surface image(72,72);
SDL_Rect r {6, 6, 0, 0};
sdl_blit(image60, nullptr, image, &r);
uint8_t alpha = 196;
int size = image->w;
int zoom = static_cast<int>(size * disp.get_zoom_factor());
// Add the alpha factor and scale the image
adjust_surface_alpha(image, alpha);
image = scale_surface(image, zoom, zoom);
disp.set_mouseover_hex_overlay(image);
}
} //end namespace editor