-
-
Notifications
You must be signed in to change notification settings - Fork 992
/
location_palette.cpp
392 lines (345 loc) · 9.59 KB
/
location_palette.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
/*
Copyright (C) 2003 - 2016 by David White <dave@whitevine.net>
Part of the Battle for Wesnoth Project http://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 "location_palette.hpp"
#include "gettext.hpp"
#include "marked-up_text.hpp"
#include "tooltips.hpp"
#include "editor/action/mouse/mouse_action.hpp"
#include "gui/dialogs/edit_text.hpp"
#include "wml_separators.hpp"
#include "formula/string_utils.hpp"
class location_palette_item : public gui::widget
{
public:
struct tstate {
tstate()
: selected(false)
, mouseover(false)
{}
bool selected;
bool mouseover;
friend bool operator==(tstate r, tstate l)
{
return r.selected == l.selected && r.mouseover == l.mouseover;
}
};
location_palette_item(CVideo& video, editor::location_palette& parent)
: gui::widget(video, true)
, parent_(parent)
{
}
void draw_contents() override
{
if (state_.mouseover) {
sdl::draw_solid_tinted_rectangle(location().x, location().y, location().w, location().h, 200, 200, 200, 0.1, video().getSurface());
}
if (state_.selected) {
sdl::draw_rectangle(location().x, location().y, location().w, location().h, -1, video().getSurface());
}
font::draw_text(&video(), location(), 16, font::NORMAL_COLOR, desc_.empty() ? id_ : desc_, location().x + 2, location().y, 0);
}
//TODO move to widget
bool hit(int x, int y) const
{
return sdl::point_in_rect(x, y, location());
}
void mouse_up(SDL_MouseButtonEvent const &e)
{
if (!(hit(e.x, e.y)))
return;
if (e.button == SDL_BUTTON_LEFT) {
parent_.select_item(id_);
}
if (e.button == SDL_BUTTON_RIGHT) {
//TODO: add a context menu with the follwing options:
// 1) 'copy it to clipboard'
// 2) 'jump to item'
// 3) 'delete item'.
}
}
void handle_event(const SDL_Event& e) override
{
gui::widget::handle_event(e);
if (hidden() || !enabled() || mouse_locked())
return;
tstate start_state = state_;
switch (e.type) {
case SDL_MOUSEBUTTONUP:
mouse_up(e.button);
break;
case SDL_MOUSEMOTION:
state_.mouseover = hit(e.motion.x, e.motion.y);
break;
default:
return;
}
if (!(start_state == state_))
set_dirty(true);
}
void set_item_id(const std::string& id)
{
id_ = id;
bool is_number = std::find_if(id.begin(), id.end(), [](char c) { return !std::isdigit(c); }) == id.end();
if (is_number) {
desc_ = vgettext("Player $side_num", utils::string_map{ {"side_num", id} });
}
else {
desc_ = "";
}
}
void set_selected(bool selected)
{
state_.selected = selected;
}
void draw() { gui::widget::draw(); }
private:
std::string id_;
std::string desc_;
tstate state_;
editor::location_palette& parent_;
};
class location_palette_button : public gui::button
{
public:
location_palette_button(CVideo& video, const SDL_Rect& location, const std::string& text, const std::function<void (void)>& callback)
: gui::button(video, text)
, callback_(callback)
{
this->set_location(location.x, location.y);
this->hide(false);
}
protected:
virtual void mouse_up(const SDL_MouseButtonEvent& e) override
{
gui::button::mouse_up(e);
if (callback_) {
if (this->pressed()) {
callback_();
}
}
}
std::function<void (void)> callback_;
};
namespace editor {
location_palette::location_palette(editor_display &gui, const config& /*cfg*/, mouse_action** active_mouse_action)
: common_palette(gui)
, gui_(gui)
, item_size_(20)
//TODO avoid magic number
, item_space_(20 + 3)
, palette_y_(0)
, palette_x_(0)
, items_start_(0)
, selected_item_()
, items_()
, active_mouse_action_(active_mouse_action)
, buttons_()
, button_add_()
, button_delete_()
, button_goto_()
, help_handle_(-1)
, disp_(gui)
{
for (int i = 1; i < 10; ++i) {
items_.push_back(std::to_string(i));
}
selected_item_ = items_[0];
}
sdl_handler_vector location_palette::handler_members()
{
sdl_handler_vector h;
for (gui::widget& b : buttons_) {
h.push_back(&b);
}
if (button_add_) { h.push_back(button_add_.get()); }
if (button_delete_) { h.push_back(button_delete_.get()); }
if (button_goto_) { h.push_back(button_goto_.get()); }
return h;
}
void location_palette::hide(bool hidden) {
widget::hide(hidden);
if (!hidden) {
help_handle_ = gui_.video().set_help_string(get_help_string());
}
else {
gui_.video().clear_help_string(help_handle_);
}
for (auto& w : handler_members()) {
static_cast<gui::widget&>(*w).hide(hidden);
}
}
bool location_palette::scroll_up()
{
int decrement = 1;
if(items_start_ >= decrement) {
items_start_ -= decrement;
draw();
return true;
}
return false;
}
bool location_palette::can_scroll_up()
{
return (items_start_ != 0);
}
bool location_palette::can_scroll_down()
{
return (items_start_ + num_visible_items() + 1 <= num_items());
}
bool location_palette::scroll_down()
{
bool end_reached = (!(items_start_ + num_visible_items() + 1 <= num_items()));
bool scrolled = false;
// move downwards
if(!end_reached) {
items_start_ += 1;
scrolled = true;
set_dirty(true);
}
draw();
return scrolled;
}
void location_palette::adjust_size(const SDL_Rect& target)
{
palette_x_ = target.x;
palette_y_ = target.y;
const int button_height = 30;
int bottom = target.y + target.h;
button_add_.reset();
button_delete_.reset();
button_goto_.reset();
button_goto_.reset(new location_palette_button(video(), SDL_Rect{ target.x , bottom -= button_height, target.w - 10, button_height }, _("Go To"), [this]() {
//static_cast<mouse_action_starting_position&>(**active_mouse_action_).
map_location pos = disp_.get_map().special_location(selected_item_);
if (pos.valid()) {
disp_.scroll_to_tile(pos, display::WARP);
}
}));
button_add_.reset(new location_palette_button(video(), SDL_Rect{ target.x , bottom -= button_height, target.w - 10, button_height }, _("Add"), [this]() {
std::string newid;
if (gui2::tedit_text::execute(_("New Location Identifer"), "", newid, video())) {
add_item(newid);
}
}));
button_delete_.reset(new location_palette_button(video(), SDL_Rect{ target.x , bottom -= button_height, target.w - 10, button_height }, _("Delete"), nullptr));
const int space_for_items = bottom - target.y;
const int items_fitting = space_for_items / item_space_;
if (num_visible_items() != items_fitting) {
location_palette_item lpi(gui_.video(), *this);
//Why does this need a pointer to a non-const as second paraeter?
//TODO: we should write our own ptr_vector class, boost::ptr_vector has a lot of flaws.
buttons_.resize(items_fitting, &lpi);
}
set_location(target);
set_dirty(true);
gui_.video().clear_help_string(help_handle_);
help_handle_ = gui_.video().set_help_string(get_help_string());
}
void location_palette::select_item(const std::string& item_id)
{
if (selected_item_ != item_id) {
selected_item_ = item_id;
set_dirty();
}
gui_.video().clear_help_string(help_handle_);
help_handle_ = gui_.video().set_help_string(get_help_string());
}
int location_palette::num_items()
{
return items_.size();
}
int location_palette::num_visible_items()
{
return buttons_.size();
}
bool location_palette::is_selected_item(const std::string& id)
{
return selected_item_ == id;
}
void location_palette::draw_contents()
{
if (*active_mouse_action_)
(*active_mouse_action_)->set_mouse_overlay(gui_);
int y = palette_y_;
const int x = palette_x_;
const int starting = items_start_;
int ending = std::min<int>(starting + num_visible_items(), num_items());
gui::button* upscroll_button = gui_.find_action_button("upscroll-button-editor");
if (upscroll_button)
upscroll_button->enable(starting != 0);
gui::button* downscroll_button = gui_.find_action_button("downscroll-button-editor");
if (downscroll_button)
downscroll_button->enable(ending != num_items());
if (button_goto_) {
button_goto_->set_dirty(true);
}
if (button_add_) {
button_add_->set_dirty(true);
}
if (button_delete_) {
button_delete_->set_dirty(true);
}
for (int i = 0, size = num_visible_items(); i < size; i++) {
location_palette_item & tile = buttons_[i];
tile.hide(true);
if (i >= ending) {
//We want to hide all follwing buttons to we cannot use break here.
continue;
}
const std::string item_id = items_[starting + i];
std::stringstream tooltip_text;
SDL_Rect dstrect;
dstrect.x = x;
dstrect.y = y;
dstrect.w = location().w - 10;
dstrect.h = item_size_ + 2;
tile.set_location(dstrect);
tile.set_tooltip_string(tooltip_text.str());
tile.set_item_id(item_id);
tile.set_selected(is_selected_item(item_id));
tile.set_dirty(true);
tile.hide(false);
tile.draw();
// Adjust location
y += item_space_;
}
update_rect(location());
}
std::vector<std::string> location_palette::action_pressed() const
{
std::vector<std::string> res;
if (button_delete_ && button_delete_->pressed()) {
res.push_back("editor-remove-location");
}
return res;
}
location_palette::~location_palette()
{
}
void location_palette::add_item(const std::string& id)
{
int pos;
const auto itor = std::find(items_.begin(), items_.end(), id);
if (itor == items_.end()) {
items_.push_back(id);
pos = items_.size() - 1;
}
else {
pos = itor - items_.begin();
}
selected_item_ = id;
items_start_ = std::max(pos - num_visible_items() + 1, items_start_);
items_start_ = std::min(pos, items_start_);
adjust_size(location());
}
} // end namespace editor