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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
|
/*************************************************************************/
/* dialogs.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* http://www.godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "dialogs.h"
#include "line_edit.h"
#include "print_string.h"
#include "translation.h"
#ifdef TOOLS_ENABLED
#include "editor/editor_node.h"
#endif
void WindowDialog::_post_popup() {
drag_type = DRAG_NONE; // just in case
}
void WindowDialog::_fix_size() {
// Perhaps this should be called when the viewport resizes as well or windows go out of bounds...
// Ensure the whole window is visible.
Point2i pos = get_global_pos();
Size2i size = get_size();
Size2i viewport_size = get_viewport_rect().size;
// Windows require additional padding to keep the window chrome visible.
Ref<StyleBox> panel = get_stylebox("panel", "WindowDialog");
float top = panel->get_margin(MARGIN_TOP);
float left = panel->get_margin(MARGIN_LEFT);
float bottom = panel->get_margin(MARGIN_BOTTOM);
float right = panel->get_margin(MARGIN_RIGHT);
pos.x = MAX(left, MIN(pos.x, viewport_size.x - size.x - right));
pos.y = MAX(top, MIN(pos.y, viewport_size.y - size.y - bottom));
set_global_pos(pos);
// Also resize the window to fit if a resize should be possible at all.
if (resizable) {
size.x = MIN(size.x, viewport_size.x - left - right);
size.y = MIN(size.y, viewport_size.y - top - bottom);
set_size(size);
}
}
bool WindowDialog::has_point(const Point2 &p_point) const {
Rect2 r(Point2(), get_size());
// Enlarge upwards for title bar.
int titlebar_height = get_constant("titlebar_height", "WindowDialog");
r.pos.y -= titlebar_height;
r.size.y += titlebar_height;
// Inflate by the resizable border thickness.
if (resizable) {
int scaleborder_size = get_constant("scaleborder_size", "WindowDialog");
r.pos.x -= scaleborder_size;
r.size.width += scaleborder_size * 2;
r.pos.y -= scaleborder_size;
r.size.height += scaleborder_size * 2;
}
return r.has_point(p_point);
}
void WindowDialog::_gui_input(const InputEvent &p_event) {
if (p_event.type == InputEvent::MOUSE_BUTTON && p_event.mouse_button.button_index == BUTTON_LEFT) {
if (p_event.mouse_button.pressed) {
// Begin a possible dragging operation.
drag_type = _drag_hit_test(Point2(p_event.mouse_button.x, p_event.mouse_button.y));
if (drag_type != DRAG_NONE)
drag_offset = get_global_mouse_pos() - get_pos();
drag_offset_far = get_pos() + get_size() - get_global_mouse_pos();
} else if (drag_type != DRAG_NONE && !p_event.mouse_button.pressed) {
// End a dragging operation.
drag_type = DRAG_NONE;
}
}
if (p_event.type == InputEvent::MOUSE_MOTION) {
if (drag_type == DRAG_NONE) {
// Update the cursor while moving along the borders.
CursorShape cursor = CURSOR_ARROW;
if (resizable) {
int preview_drag_type = _drag_hit_test(Point2(p_event.mouse_button.x, p_event.mouse_button.y));
switch (preview_drag_type) {
case DRAG_RESIZE_TOP:
case DRAG_RESIZE_BOTTOM:
cursor = CURSOR_VSIZE;
break;
case DRAG_RESIZE_LEFT:
case DRAG_RESIZE_RIGHT:
cursor = CURSOR_HSIZE;
break;
case DRAG_RESIZE_TOP + DRAG_RESIZE_LEFT:
case DRAG_RESIZE_BOTTOM + DRAG_RESIZE_RIGHT:
cursor = CURSOR_FDIAGSIZE;
break;
case DRAG_RESIZE_TOP + DRAG_RESIZE_RIGHT:
case DRAG_RESIZE_BOTTOM + DRAG_RESIZE_LEFT:
cursor = CURSOR_BDIAGSIZE;
break;
}
}
if (get_cursor_shape() != cursor)
set_default_cursor_shape(cursor);
} else {
// Update while in a dragging operation.
Point2 global_pos = get_global_mouse_pos();
global_pos.y = MAX(global_pos.y, 0); // Ensure title bar stays visible.
Rect2 rect = get_rect();
Size2 min_size = get_minimum_size();
if (drag_type == DRAG_MOVE) {
rect.pos = global_pos - drag_offset;
} else {
if (drag_type & DRAG_RESIZE_TOP) {
int bottom = rect.pos.y + rect.size.height;
int max_y = bottom - min_size.height;
rect.pos.y = MIN(global_pos.y - drag_offset.y, max_y);
rect.size.height = bottom - rect.pos.y;
} else if (drag_type & DRAG_RESIZE_BOTTOM) {
rect.size.height = global_pos.y - rect.pos.y + drag_offset_far.y;
}
if (drag_type & DRAG_RESIZE_LEFT) {
int right = rect.pos.x + rect.size.width;
int max_x = right - min_size.width;
rect.pos.x = MIN(global_pos.x - drag_offset.x, max_x);
rect.size.width = right - rect.pos.x;
} else if (drag_type & DRAG_RESIZE_RIGHT) {
rect.size.width = global_pos.x - rect.pos.x + drag_offset_far.x;
}
}
set_size(rect.size);
set_pos(rect.pos);
}
}
}
void WindowDialog::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_DRAW: {
RID canvas = get_canvas_item();
Size2 size = get_size();
Ref<StyleBox> panel = get_stylebox("panel", "WindowDialog");
panel->draw(canvas, Rect2(Point2(), size));
int title_height = get_constant("title_height", "WindowDialog");
Color title_color = get_color("title_color", "WindowDialog");
Ref<Font> font = get_font("title_font", "WindowDialog");
int ofs = (size.width - font->get_string_size(title).width) / 2;
draw_string(font, Point2(ofs, -title_height + font->get_ascent()), title, title_color, size.width - panel->get_minimum_size().width);
} break;
case NOTIFICATION_THEME_CHANGED:
case NOTIFICATION_ENTER_TREE: {
close_button->set_normal_texture(get_icon("close", "WindowDialog"));
close_button->set_pressed_texture(get_icon("close", "WindowDialog"));
close_button->set_hover_texture(get_icon("close_hilite", "WindowDialog"));
close_button->set_anchor(MARGIN_LEFT, ANCHOR_END);
close_button->set_begin(Point2(get_constant("close_h_ofs", "WindowDialog"), -get_constant("close_v_ofs", "WindowDialog")));
} break;
case NOTIFICATION_MOUSE_EXIT: {
// Reset the mouse cursor when leaving the resizable window border.
if (resizable && !drag_type) {
if (get_default_cursor_shape() != CURSOR_ARROW)
set_default_cursor_shape(CURSOR_ARROW);
}
} break;
#ifdef TOOLS_ENABLED
case NOTIFICATION_POST_POPUP: {
if (get_tree() && get_tree()->is_editor_hint() && EditorNode::get_singleton())
EditorNode::get_singleton()->dim_editor(true);
} break;
case NOTIFICATION_POPUP_HIDE: {
if (get_tree() && get_tree()->is_editor_hint() && EditorNode::get_singleton())
EditorNode::get_singleton()->dim_editor(false);
} break;
#endif
}
}
void WindowDialog::_closed() {
_close_pressed();
hide();
}
int WindowDialog::_drag_hit_test(const Point2 &pos) const {
int drag_type = DRAG_NONE;
if (resizable) {
int titlebar_height = get_constant("titlebar_height", "WindowDialog");
int scaleborder_size = get_constant("scaleborder_size", "WindowDialog");
Rect2 rect = get_rect();
if (pos.y < (-titlebar_height + scaleborder_size))
drag_type = DRAG_RESIZE_TOP;
else if (pos.y >= (rect.size.height - scaleborder_size))
drag_type = DRAG_RESIZE_BOTTOM;
if (pos.x < scaleborder_size)
drag_type |= DRAG_RESIZE_LEFT;
else if (pos.x >= (rect.size.width - scaleborder_size))
drag_type |= DRAG_RESIZE_RIGHT;
}
if (drag_type == DRAG_NONE && pos.y < 0)
drag_type = DRAG_MOVE;
return drag_type;
}
void WindowDialog::set_title(const String &p_title) {
title = XL_MESSAGE(p_title);
update();
}
String WindowDialog::get_title() const {
return title;
}
void WindowDialog::set_resizable(bool p_resizable) {
resizable = p_resizable;
}
bool WindowDialog::get_resizable() const {
return resizable;
}
Size2 WindowDialog::get_minimum_size() const {
Ref<Font> font = get_font("title_font", "WindowDialog");
int msx = close_button->get_combined_minimum_size().x;
msx += font->get_string_size(title).x;
return Size2(msx, 1);
}
TextureButton *WindowDialog::get_close_button() {
return close_button;
}
void WindowDialog::_bind_methods() {
ClassDB::bind_method(D_METHOD("_gui_input"), &WindowDialog::_gui_input);
ClassDB::bind_method(D_METHOD("set_title", "title"), &WindowDialog::set_title);
ClassDB::bind_method(D_METHOD("get_title"), &WindowDialog::get_title);
ClassDB::bind_method(D_METHOD("set_resizable", "resizable"), &WindowDialog::set_resizable);
ClassDB::bind_method(D_METHOD("get_resizable"), &WindowDialog::get_resizable);
ClassDB::bind_method(D_METHOD("_closed"), &WindowDialog::_closed);
ClassDB::bind_method(D_METHOD("get_close_button:TextureButton"), &WindowDialog::get_close_button);
ADD_PROPERTY(PropertyInfo(Variant::STRING, "window_title", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT_INTL), "set_title", "get_title");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "resizable", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT_INTL), "set_resizable", "get_resizable");
}
WindowDialog::WindowDialog() {
//title="Hello!";
drag_type = DRAG_NONE;
resizable = false;
close_button = memnew(TextureButton);
add_child(close_button);
close_button->connect("pressed", this, "_closed");
}
WindowDialog::~WindowDialog() {
}
void PopupDialog::_notification(int p_what) {
if (p_what == NOTIFICATION_DRAW) {
RID ci = get_canvas_item();
get_stylebox("panel", "PopupMenu")->draw(ci, Rect2(Point2(), get_size()));
}
}
PopupDialog::PopupDialog() {
}
PopupDialog::~PopupDialog() {
}
// AcceptDialog
void AcceptDialog::_post_popup() {
WindowDialog::_post_popup();
get_ok()->grab_focus();
}
void AcceptDialog::_notification(int p_what) {
if (p_what == NOTIFICATION_MODAL_CLOSE) {
cancel_pressed();
} else if (p_what == NOTIFICATION_RESIZED) {
_update_child_rects();
}
}
void AcceptDialog::_builtin_text_entered(const String &p_text) {
_ok_pressed();
}
void AcceptDialog::_ok_pressed() {
if (hide_on_ok)
hide();
ok_pressed();
emit_signal("confirmed");
}
void AcceptDialog::_close_pressed() {
cancel_pressed();
}
String AcceptDialog::get_text() const {
return label->get_text();
}
void AcceptDialog::set_text(String p_text) {
label->set_text(p_text);
minimum_size_changed();
_update_child_rects();
}
void AcceptDialog::set_hide_on_ok(bool p_hide) {
hide_on_ok = p_hide;
}
bool AcceptDialog::get_hide_on_ok() const {
return hide_on_ok;
}
void AcceptDialog::register_text_enter(Node *p_line_edit) {
ERR_FAIL_NULL(p_line_edit);
p_line_edit->connect("text_entered", this, "_builtin_text_entered");
}
void AcceptDialog::_update_child_rects() {
Size2 label_size = label->get_minimum_size();
if (label->get_text().empty()) {
label_size.height = 0;
}
int margin = get_constant("margin", "Dialogs");
Size2 size = get_size();
Size2 hminsize = hbc->get_combined_minimum_size();
Vector2 cpos(margin, margin + label_size.height);
Vector2 csize(size.x - margin * 2, size.y - margin * 3 - hminsize.y - label_size.height);
for (int i = 0; i < get_child_count(); i++) {
Control *c = get_child(i)->cast_to<Control>();
if (!c)
continue;
if (c == hbc || c == label || c == get_close_button() || c->is_set_as_toplevel())
continue;
c->set_pos(cpos);
c->set_size(csize);
}
cpos.y += csize.y + margin;
csize.y = hminsize.y;
hbc->set_pos(cpos);
hbc->set_size(csize);
}
Size2 AcceptDialog::get_minimum_size() const {
int margin = get_constant("margin", "Dialogs");
Size2 minsize = label->get_combined_minimum_size();
for (int i = 0; i < get_child_count(); i++) {
Control *c = get_child(i)->cast_to<Control>();
if (!c)
continue;
if (c == hbc || c == label || c == const_cast<AcceptDialog *>(this)->get_close_button() || c->is_set_as_toplevel())
continue;
Size2 cminsize = c->get_combined_minimum_size();
minsize.x = MAX(cminsize.x, minsize.x);
minsize.y = MAX(cminsize.y, minsize.y);
}
Size2 hminsize = hbc->get_combined_minimum_size();
minsize.x = MAX(hminsize.x, minsize.x);
minsize.y += hminsize.y;
minsize.x += margin * 2;
minsize.y += margin * 3; //one as separation between hbc and child
Size2 wmsize = WindowDialog::get_minimum_size();
minsize.x = MAX(wmsize.x, minsize.x);
return minsize;
}
void AcceptDialog::_custom_action(const String &p_action) {
emit_signal("custom_action", p_action);
custom_action(p_action);
}
Button *AcceptDialog::add_button(const String &p_text, bool p_right, const String &p_action) {
Button *button = memnew(Button);
button->set_text(p_text);
if (p_right) {
hbc->add_child(button);
hbc->add_spacer();
} else {
hbc->add_child(button);
hbc->move_child(button, 0);
hbc->add_spacer(true);
}
if (p_action != "") {
button->connect("pressed", this, "_custom_action", varray(p_action));
}
return button;
}
Button *AcceptDialog::add_cancel(const String &p_cancel) {
String c = p_cancel;
if (p_cancel == "")
c = RTR("Cancel");
Button *b = swap_ok_cancel ? add_button(c, true) : add_button(c);
b->connect("pressed", this, "_closed");
return b;
}
void AcceptDialog::_bind_methods() {
ClassDB::bind_method(D_METHOD("_ok"), &AcceptDialog::_ok_pressed);
ClassDB::bind_method(D_METHOD("get_ok"), &AcceptDialog::get_ok);
ClassDB::bind_method(D_METHOD("get_label"), &AcceptDialog::get_label);
ClassDB::bind_method(D_METHOD("set_hide_on_ok", "enabled"), &AcceptDialog::set_hide_on_ok);
ClassDB::bind_method(D_METHOD("get_hide_on_ok"), &AcceptDialog::get_hide_on_ok);
ClassDB::bind_method(D_METHOD("add_button:Button", "text", "right", "action"), &AcceptDialog::add_button, DEFVAL(false), DEFVAL(""));
ClassDB::bind_method(D_METHOD("add_cancel:Button", "name"), &AcceptDialog::add_cancel);
ClassDB::bind_method(D_METHOD("_builtin_text_entered"), &AcceptDialog::_builtin_text_entered);
ClassDB::bind_method(D_METHOD("register_text_enter:LineEdit", "line_edit"), &AcceptDialog::register_text_enter);
ClassDB::bind_method(D_METHOD("_custom_action"), &AcceptDialog::_custom_action);
ClassDB::bind_method(D_METHOD("set_text", "text"), &AcceptDialog::set_text);
ClassDB::bind_method(D_METHOD("get_text"), &AcceptDialog::get_text);
ADD_SIGNAL(MethodInfo("confirmed"));
ADD_SIGNAL(MethodInfo("custom_action", PropertyInfo(Variant::STRING, "action")));
ADD_GROUP("Dialog", "dialog");
ADD_PROPERTYNZ(PropertyInfo(Variant::STRING, "dialog_text", PROPERTY_HINT_MULTILINE_TEXT, "", PROPERTY_USAGE_DEFAULT_INTL), "set_text", "get_text");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "dialog_hide_on_ok"), "set_hide_on_ok", "get_hide_on_ok");
}
bool AcceptDialog::swap_ok_cancel = false;
void AcceptDialog::set_swap_ok_cancel(bool p_swap) {
swap_ok_cancel = p_swap;
}
AcceptDialog::AcceptDialog() {
int margin = get_constant("margin", "Dialogs");
int button_margin = get_constant("button_margin", "Dialogs");
label = memnew(Label);
label->set_anchor(MARGIN_RIGHT, ANCHOR_END);
label->set_anchor(MARGIN_BOTTOM, ANCHOR_END);
label->set_begin(Point2(margin, margin));
label->set_end(Point2(margin, button_margin + 10));
//label->set_autowrap(true);
add_child(label);
hbc = memnew(HBoxContainer);
add_child(hbc);
hbc->add_spacer();
ok = memnew(Button);
ok->set_text(RTR("OK"));
hbc->add_child(ok);
hbc->add_spacer();
ok->connect("pressed", this, "_ok");
set_as_toplevel(true);
hide_on_ok = true;
set_title(RTR("Alert!"));
}
AcceptDialog::~AcceptDialog() {
}
void ConfirmationDialog::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_cancel:Button"), &ConfirmationDialog::get_cancel);
}
Button *ConfirmationDialog::get_cancel() {
return cancel;
}
ConfirmationDialog::ConfirmationDialog() {
set_title(RTR("Please Confirm..."));
cancel = add_cancel();
}
|