Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/gridmap/editor/grid_map_editor_plugin.cpp
10278 views
1
/**************************************************************************/
2
/* grid_map_editor_plugin.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "grid_map_editor_plugin.h"
32
33
#include "core/os/keyboard.h"
34
#include "editor/editor_main_screen.h"
35
#include "editor/editor_node.h"
36
#include "editor/editor_string_names.h"
37
#include "editor/editor_undo_redo_manager.h"
38
#include "editor/gui/editor_bottom_panel.h"
39
#include "editor/gui/editor_zoom_widget.h"
40
#include "editor/scene/3d/node_3d_editor_plugin.h"
41
#include "editor/settings/editor_command_palette.h"
42
#include "editor/settings/editor_settings.h"
43
#include "editor/themes/editor_scale.h"
44
#include "scene/3d/camera_3d.h"
45
#include "scene/gui/dialogs.h"
46
#include "scene/gui/label.h"
47
#include "scene/gui/menu_button.h"
48
#include "scene/gui/separator.h"
49
#include "scene/main/window.h"
50
51
void GridMapEditor::_configure() {
52
if (!node) {
53
return;
54
}
55
56
update_grid();
57
}
58
59
void GridMapEditor::_menu_option(int p_option) {
60
switch (p_option) {
61
case MENU_OPTION_PREV_LEVEL: {
62
floor->set_value(floor->get_value() - 1);
63
if (selection.active && input_action == INPUT_SELECT) {
64
selection.current[edit_axis]--;
65
_validate_selection();
66
}
67
} break;
68
69
case MENU_OPTION_NEXT_LEVEL: {
70
floor->set_value(floor->get_value() + 1);
71
if (selection.active && input_action == INPUT_SELECT) {
72
selection.current[edit_axis]++;
73
_validate_selection();
74
}
75
} break;
76
77
case MENU_OPTION_X_AXIS:
78
case MENU_OPTION_Y_AXIS:
79
case MENU_OPTION_Z_AXIS: {
80
int new_axis = p_option - MENU_OPTION_X_AXIS;
81
for (int i = 0; i < 3; i++) {
82
int idx = options->get_popup()->get_item_index(MENU_OPTION_X_AXIS + i);
83
options->get_popup()->set_item_checked(idx, i == new_axis);
84
}
85
86
if (edit_axis != new_axis) {
87
if (edit_axis == Vector3::AXIS_Y) {
88
floor->set_tooltip_text("Change Grid Plane");
89
} else if (new_axis == Vector3::AXIS_Y) {
90
floor->set_tooltip_text("Change Grid Floor");
91
}
92
}
93
edit_axis = Vector3::Axis(new_axis);
94
update_grid();
95
96
} break;
97
98
case MENU_OPTION_CURSOR_ROTATE_X:
99
case MENU_OPTION_CURSOR_ROTATE_Y:
100
case MENU_OPTION_CURSOR_ROTATE_Z:
101
case MENU_OPTION_CURSOR_BACK_ROTATE_X:
102
case MENU_OPTION_CURSOR_BACK_ROTATE_Y:
103
case MENU_OPTION_CURSOR_BACK_ROTATE_Z: {
104
Vector3 rotation_axis;
105
float rotation_angle = -Math::PI / 2.0;
106
if (p_option == MENU_OPTION_CURSOR_ROTATE_X || p_option == MENU_OPTION_CURSOR_BACK_ROTATE_X) {
107
rotation_axis.x = (p_option == MENU_OPTION_CURSOR_ROTATE_X) ? 1 : -1;
108
} else if (p_option == MENU_OPTION_CURSOR_ROTATE_Y || p_option == MENU_OPTION_CURSOR_BACK_ROTATE_Y) {
109
rotation_axis.y = (p_option == MENU_OPTION_CURSOR_ROTATE_Y) ? 1 : -1;
110
} else if (p_option == MENU_OPTION_CURSOR_ROTATE_Z || p_option == MENU_OPTION_CURSOR_BACK_ROTATE_Z) {
111
rotation_axis.z = (p_option == MENU_OPTION_CURSOR_ROTATE_Z) ? 1 : -1;
112
}
113
114
Basis r;
115
if (input_action == INPUT_PASTE) {
116
r = node->get_basis_with_orthogonal_index(paste_indicator.orientation);
117
r.rotate(rotation_axis, rotation_angle);
118
paste_indicator.orientation = node->get_orthogonal_index_from_basis(r);
119
_update_paste_indicator();
120
} else if (_has_selection()) {
121
Array cells = _get_selected_cells();
122
for (int i = 0; i < cells.size(); i++) {
123
Vector3i cell = cells[i];
124
r = node->get_basis_with_orthogonal_index(node->get_cell_item_orientation(cell));
125
r.rotate(rotation_axis, rotation_angle);
126
node->set_cell_item(cell, node->get_cell_item(cell), node->get_orthogonal_index_from_basis(r));
127
}
128
} else {
129
r = node->get_basis_with_orthogonal_index(cursor_rot);
130
r.rotate(rotation_axis, rotation_angle);
131
cursor_rot = node->get_orthogonal_index_from_basis(r);
132
_update_cursor_transform();
133
}
134
} break;
135
136
case MENU_OPTION_CURSOR_CLEAR_ROTATION: {
137
if (input_action == INPUT_PASTE) {
138
paste_indicator.orientation = 0;
139
_update_paste_indicator();
140
break;
141
}
142
143
cursor_rot = 0;
144
_update_cursor_transform();
145
} break;
146
147
case MENU_OPTION_PASTE_SELECTS: {
148
int idx = options->get_popup()->get_item_index(MENU_OPTION_PASTE_SELECTS);
149
options->get_popup()->set_item_checked(idx, !options->get_popup()->is_item_checked(idx));
150
} break;
151
152
case MENU_OPTION_SELECTION_DUPLICATE:
153
case MENU_OPTION_SELECTION_CUT: {
154
if (!(selection.active && input_action == INPUT_NONE)) {
155
break;
156
}
157
158
_set_clipboard_data();
159
160
if (p_option == MENU_OPTION_SELECTION_CUT) {
161
_delete_selection();
162
}
163
164
input_action = INPUT_PASTE;
165
paste_indicator.click = selection.begin;
166
paste_indicator.current = selection.begin;
167
paste_indicator.begin = selection.begin;
168
paste_indicator.end = selection.end;
169
paste_indicator.orientation = 0;
170
_update_paste_indicator();
171
} break;
172
case MENU_OPTION_SELECTION_CLEAR: {
173
if (!selection.active) {
174
break;
175
}
176
177
_delete_selection();
178
179
} break;
180
case MENU_OPTION_SELECTION_FILL: {
181
if (!selection.active) {
182
return;
183
}
184
185
_fill_selection();
186
187
} break;
188
case MENU_OPTION_GRIDMAP_SETTINGS: {
189
settings_dialog->popup_centered(settings_vbc->get_combined_minimum_size() + Size2(50, 50) * EDSCALE);
190
} break;
191
}
192
}
193
194
void GridMapEditor::_update_cursor_transform() {
195
cursor_transform = Transform3D();
196
cursor_transform.origin = cursor_origin;
197
cursor_transform.basis *= node->get_cell_scale();
198
cursor_transform = node->get_global_transform() * cursor_transform;
199
200
if (mode_buttons_group->get_pressed_button() == paint_mode_button) {
201
// Auto-deselect the selection when painting.
202
if (selection.active) {
203
_set_selection(false);
204
}
205
// Rotation is only applied in paint mode, we don't want the cursor box to rotate otherwise.
206
cursor_transform.basis = node->get_basis_with_orthogonal_index(cursor_rot);
207
if (selected_palette >= 0 && node && node->get_mesh_library().is_valid()) {
208
cursor_transform *= node->get_mesh_library()->get_item_mesh_transform(selected_palette);
209
}
210
} else {
211
Transform3D xf;
212
xf.scale(node->get_cell_size());
213
xf.origin.x = node->get_center_x() ? -node->get_cell_size().x / 2 : 0;
214
xf.origin.y = node->get_center_y() ? -node->get_cell_size().y / 2 : 0;
215
xf.origin.z = node->get_center_z() ? -node->get_cell_size().z / 2 : 0;
216
cursor_transform *= xf;
217
}
218
219
if (cursor_instance.is_valid()) {
220
RenderingServer::get_singleton()->instance_set_transform(cursor_instance, cursor_transform);
221
RenderingServer::get_singleton()->instance_set_visible(cursor_instance, cursor_visible);
222
}
223
}
224
225
void GridMapEditor::_update_selection_transform() {
226
Transform3D xf_zero;
227
xf_zero.basis.set_zero();
228
229
if (!selection.active) {
230
RenderingServer::get_singleton()->instance_set_transform(selection_instance, xf_zero);
231
for (int i = 0; i < 3; i++) {
232
RenderingServer::get_singleton()->instance_set_transform(selection_level_instance[i], xf_zero);
233
}
234
return;
235
}
236
237
Transform3D xf;
238
xf.scale((Vector3(1, 1, 1) + (selection.end - selection.begin)) * node->get_cell_size());
239
xf.origin = selection.begin * node->get_cell_size();
240
241
RenderingServer::get_singleton()->instance_set_transform(selection_instance, node->get_global_transform() * xf);
242
243
for (int i = 0; i < 3; i++) {
244
if (i != edit_axis || (edit_floor[edit_axis] < selection.begin[edit_axis]) || (edit_floor[edit_axis] > selection.end[edit_axis] + 1)) {
245
RenderingServer::get_singleton()->instance_set_transform(selection_level_instance[i], xf_zero);
246
} else {
247
Vector3 scale = (selection.end - selection.begin + Vector3(1, 1, 1));
248
scale[edit_axis] = 1.0;
249
Vector3 position = selection.begin;
250
position[edit_axis] = edit_floor[edit_axis];
251
252
scale *= node->get_cell_size();
253
position *= node->get_cell_size();
254
255
Transform3D xf2;
256
xf2.basis.scale(scale);
257
xf2.origin = position;
258
259
RenderingServer::get_singleton()->instance_set_transform(selection_level_instance[i], node->get_global_transform() * xf2);
260
}
261
}
262
}
263
264
void GridMapEditor::_validate_selection() {
265
if (!selection.active) {
266
return;
267
}
268
selection.begin = selection.click;
269
selection.end = selection.current;
270
271
if (selection.begin.x > selection.end.x) {
272
SWAP(selection.begin.x, selection.end.x);
273
}
274
if (selection.begin.y > selection.end.y) {
275
SWAP(selection.begin.y, selection.end.y);
276
}
277
if (selection.begin.z > selection.end.z) {
278
SWAP(selection.begin.z, selection.end.z);
279
}
280
281
_update_selection_transform();
282
}
283
284
void GridMapEditor::_set_selection(bool p_active, const Vector3 &p_begin, const Vector3 &p_end) {
285
selection.active = p_active;
286
selection.begin = p_begin;
287
selection.end = p_end;
288
selection.click = p_begin;
289
selection.current = p_end;
290
291
if (is_visible_in_tree()) {
292
_update_selection_transform();
293
}
294
}
295
296
AABB GridMapEditor::_get_selection() const {
297
AABB ret;
298
if (selection.active) {
299
ret.position = selection.begin;
300
ret.size = selection.end - selection.begin;
301
} else {
302
ret.position.zero();
303
ret.size.zero();
304
}
305
return ret;
306
}
307
308
bool GridMapEditor::_has_selection() const {
309
return node != nullptr && selection.active;
310
}
311
312
Array GridMapEditor::_get_selected_cells() const {
313
Array ret;
314
if (node != nullptr && selection.active) {
315
for (int i = selection.begin.x; i <= selection.end.x; i++) {
316
for (int j = selection.begin.y; j <= selection.end.y; j++) {
317
for (int k = selection.begin.z; k <= selection.end.z; k++) {
318
Vector3i selected = Vector3i(i, j, k);
319
int itm = node->get_cell_item(selected);
320
if (itm == GridMap::INVALID_CELL_ITEM) {
321
continue;
322
}
323
ret.append(selected);
324
}
325
}
326
}
327
}
328
return ret;
329
}
330
331
bool GridMapEditor::do_input_action(Camera3D *p_camera, const Point2 &p_point, bool p_click) {
332
if (!spatial_editor) {
333
return false;
334
}
335
if (input_action == INPUT_TRANSFORM) {
336
return false;
337
}
338
if (selected_palette < 0 && input_action != INPUT_NONE && input_action != INPUT_PICK && input_action != INPUT_SELECT && input_action != INPUT_PASTE) {
339
return false;
340
}
341
if (mesh_library.is_null()) {
342
return false;
343
}
344
if (input_action != INPUT_NONE && input_action != INPUT_PICK && input_action != INPUT_SELECT && input_action != INPUT_PASTE && !mesh_library->has_item(selected_palette)) {
345
return false;
346
}
347
348
Camera3D *camera = p_camera;
349
Vector3 from = camera->project_ray_origin(p_point);
350
Vector3 normal = camera->project_ray_normal(p_point);
351
Transform3D local_xform = node->get_global_transform().affine_inverse();
352
Vector<Plane> planes = camera->get_frustum();
353
from = local_xform.xform(from);
354
normal = local_xform.basis.xform(normal).normalized();
355
356
Plane p;
357
p.normal[edit_axis] = 1.0;
358
p.d = edit_floor[edit_axis] * node->get_cell_size()[edit_axis];
359
360
Vector3 inters;
361
if (!p.intersects_segment(from, from + normal * settings_pick_distance->get_value(), &inters)) {
362
return false;
363
}
364
365
// Make sure the intersection is inside the frustum planes, to avoid
366
// Painting on invisible regions.
367
for (int i = 0; i < planes.size(); i++) {
368
Plane fp = local_xform.xform(planes[i]);
369
if (fp.is_point_over(inters)) {
370
return false;
371
}
372
}
373
374
int cell[3];
375
Vector3 cell_size = node->get_cell_size();
376
377
for (int i = 0; i < 3; i++) {
378
if (i == edit_axis) {
379
cell[i] = edit_floor[i];
380
} else {
381
cell[i] = inters[i] / cell_size[i];
382
if (inters[i] < 0) {
383
cell[i] -= 1; // Compensate negative.
384
}
385
grid_ofs[i] = cell[i] * cell_size[i];
386
}
387
}
388
389
RS::get_singleton()->instance_set_transform(grid_instance[edit_axis], node->get_global_transform() * edit_grid_xform);
390
391
if (cursor_instance.is_valid()) {
392
cursor_origin = (Vector3(cell[0], cell[1], cell[2]) + Vector3(0.5 * node->get_center_x(), 0.5 * node->get_center_y(), 0.5 * node->get_center_z())) * node->get_cell_size();
393
cursor_visible = true;
394
395
if (input_action == INPUT_PASTE) {
396
cursor_visible = false;
397
}
398
399
_update_cursor_transform();
400
}
401
402
if (input_action == INPUT_NONE) {
403
return false;
404
}
405
406
if (input_action == INPUT_PASTE) {
407
paste_indicator.current = Vector3i(cell[0], cell[1], cell[2]);
408
_update_paste_indicator();
409
410
} else if (input_action == INPUT_SELECT) {
411
selection.current = Vector3i(cell[0], cell[1], cell[2]);
412
if (p_click) {
413
selection.click = selection.current;
414
}
415
selection.active = true;
416
_validate_selection();
417
418
return true;
419
} else if (input_action == INPUT_PICK) {
420
int item = node->get_cell_item(Vector3i(cell[0], cell[1], cell[2]));
421
if (item >= 0) {
422
selected_palette = item;
423
424
// Clear the filter if picked an item that's filtered out.
425
int index = mesh_library_palette->find_metadata(item);
426
if (index == -1) {
427
search_box->clear();
428
}
429
430
// This will select `selected_palette` in the ItemList when possible.
431
update_palette();
432
433
_update_cursor_instance();
434
}
435
return true;
436
}
437
438
if (input_action == INPUT_PAINT) {
439
SetItem si;
440
si.position = Vector3i(cell[0], cell[1], cell[2]);
441
si.new_value = selected_palette;
442
si.new_orientation = cursor_rot;
443
si.old_value = node->get_cell_item(Vector3i(cell[0], cell[1], cell[2]));
444
si.old_orientation = node->get_cell_item_orientation(Vector3i(cell[0], cell[1], cell[2]));
445
set_items.push_back(si);
446
node->set_cell_item(Vector3i(cell[0], cell[1], cell[2]), selected_palette, cursor_rot);
447
return true;
448
} else if (input_action == INPUT_ERASE) {
449
SetItem si;
450
si.position = Vector3i(cell[0], cell[1], cell[2]);
451
si.new_value = -1;
452
si.new_orientation = 0;
453
si.old_value = node->get_cell_item(Vector3i(cell[0], cell[1], cell[2]));
454
si.old_orientation = node->get_cell_item_orientation(Vector3i(cell[0], cell[1], cell[2]));
455
set_items.push_back(si);
456
node->set_cell_item(Vector3i(cell[0], cell[1], cell[2]), -1);
457
return true;
458
}
459
460
return false;
461
}
462
463
void GridMapEditor::_delete_selection() {
464
if (!selection.active) {
465
return;
466
}
467
468
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
469
undo_redo->create_action(TTR("GridMap Delete Selection"));
470
for (int i = selection.begin.x; i <= selection.end.x; i++) {
471
for (int j = selection.begin.y; j <= selection.end.y; j++) {
472
for (int k = selection.begin.z; k <= selection.end.z; k++) {
473
Vector3i selected = Vector3i(i, j, k);
474
undo_redo->add_do_method(node, "set_cell_item", selected, GridMap::INVALID_CELL_ITEM);
475
undo_redo->add_undo_method(node, "set_cell_item", selected, node->get_cell_item(selected), node->get_cell_item_orientation(selected));
476
}
477
}
478
}
479
undo_redo->add_do_method(this, "_set_selection", !selection.active, selection.begin, selection.end);
480
undo_redo->add_undo_method(this, "_set_selection", selection.active, selection.begin, selection.end);
481
undo_redo->commit_action();
482
}
483
484
void GridMapEditor::_fill_selection() {
485
if (!selection.active) {
486
return;
487
}
488
489
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
490
undo_redo->create_action(TTR("GridMap Fill Selection"));
491
for (int i = selection.begin.x; i <= selection.end.x; i++) {
492
for (int j = selection.begin.y; j <= selection.end.y; j++) {
493
for (int k = selection.begin.z; k <= selection.end.z; k++) {
494
Vector3i selected = Vector3i(i, j, k);
495
undo_redo->add_do_method(node, "set_cell_item", selected, selected_palette, cursor_rot);
496
undo_redo->add_undo_method(node, "set_cell_item", selected, node->get_cell_item(selected), node->get_cell_item_orientation(selected));
497
}
498
}
499
}
500
undo_redo->add_do_method(this, "_set_selection", !selection.active, selection.begin, selection.end);
501
undo_redo->add_undo_method(this, "_set_selection", selection.active, selection.begin, selection.end);
502
undo_redo->commit_action();
503
}
504
505
void GridMapEditor::_clear_clipboard_data() {
506
for (const ClipboardItem &E : clipboard_items) {
507
if (E.instance.is_null()) {
508
continue;
509
}
510
RenderingServer::get_singleton()->free(E.instance);
511
}
512
513
clipboard_items.clear();
514
}
515
516
void GridMapEditor::_set_clipboard_data() {
517
_clear_clipboard_data();
518
519
Ref<MeshLibrary> meshLibrary = node->get_mesh_library();
520
521
const RID scenario = get_tree()->get_root()->get_world_3d()->get_scenario();
522
523
for (int i = selection.begin.x; i <= selection.end.x; i++) {
524
for (int j = selection.begin.y; j <= selection.end.y; j++) {
525
for (int k = selection.begin.z; k <= selection.end.z; k++) {
526
Vector3i selected = Vector3i(i, j, k);
527
int itm = node->get_cell_item(selected);
528
if (itm == GridMap::INVALID_CELL_ITEM) {
529
continue;
530
}
531
532
Ref<Mesh> mesh = meshLibrary->get_item_mesh(itm);
533
534
ClipboardItem item;
535
item.cell_item = itm;
536
item.grid_offset = Vector3(selected) - selection.begin;
537
item.orientation = node->get_cell_item_orientation(selected);
538
539
if (mesh.is_valid()) {
540
item.instance = RenderingServer::get_singleton()->instance_create2(mesh->get_rid(), scenario);
541
}
542
543
clipboard_items.push_back(item);
544
}
545
}
546
}
547
}
548
549
void GridMapEditor::_update_paste_indicator() {
550
if (input_action != INPUT_PASTE) {
551
Transform3D xf;
552
xf.basis.set_zero();
553
RenderingServer::get_singleton()->instance_set_transform(paste_instance, xf);
554
return;
555
}
556
557
Vector3 center = 0.5 * Vector3(real_t(node->get_center_x()), real_t(node->get_center_y()), real_t(node->get_center_z()));
558
Vector3 scale = (Vector3(1, 1, 1) + (paste_indicator.end - paste_indicator.begin)) * node->get_cell_size();
559
Transform3D xf;
560
xf.scale(scale);
561
xf.origin = (paste_indicator.begin + (paste_indicator.current - paste_indicator.click) + center) * node->get_cell_size();
562
Basis rot;
563
rot = node->get_basis_with_orthogonal_index(paste_indicator.orientation);
564
xf.basis = rot * xf.basis;
565
xf.translate_local((-center * node->get_cell_size()) / scale);
566
567
RenderingServer::get_singleton()->instance_set_transform(paste_instance, node->get_global_transform() * xf);
568
569
for (const ClipboardItem &item : clipboard_items) {
570
if (item.instance.is_null()) {
571
continue;
572
}
573
xf = Transform3D();
574
xf.origin = (paste_indicator.begin + (paste_indicator.current - paste_indicator.click) + center) * node->get_cell_size();
575
xf.basis = rot * xf.basis;
576
xf.translate_local(item.grid_offset * node->get_cell_size());
577
578
Basis item_rot;
579
item_rot = node->get_basis_with_orthogonal_index(item.orientation);
580
xf.basis = item_rot * xf.basis * node->get_cell_scale();
581
582
RenderingServer::get_singleton()->instance_set_transform(item.instance, node->get_global_transform() * xf);
583
}
584
}
585
586
void GridMapEditor::_do_paste() {
587
int idx = options->get_popup()->get_item_index(MENU_OPTION_PASTE_SELECTS);
588
bool reselect = options->get_popup()->is_item_checked(idx);
589
590
Basis rot;
591
rot = node->get_basis_with_orthogonal_index(paste_indicator.orientation);
592
593
Vector3 ofs = paste_indicator.current - paste_indicator.click;
594
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
595
undo_redo->create_action(TTR("GridMap Paste Selection"));
596
597
for (const ClipboardItem &item : clipboard_items) {
598
Vector3 position = rot.xform(item.grid_offset) + paste_indicator.begin + ofs;
599
600
Basis orm;
601
orm = node->get_basis_with_orthogonal_index(item.orientation);
602
orm = rot * orm;
603
604
undo_redo->add_do_method(node, "set_cell_item", position, item.cell_item, node->get_orthogonal_index_from_basis(orm));
605
undo_redo->add_undo_method(node, "set_cell_item", position, node->get_cell_item(position), node->get_cell_item_orientation(position));
606
}
607
608
if (reselect) {
609
// We need to rotate the paste_indicator to find the selection begin and end:
610
Vector3 temp_end = rot.xform(paste_indicator.end - paste_indicator.begin) + paste_indicator.begin + ofs;
611
Vector3 temp_begin = paste_indicator.begin + ofs;
612
// _set_selection expects that selection_begin is the corner closer to the origin:
613
for (int i = 0; i < 3; ++i) {
614
if (temp_begin[i] > temp_end[i]) {
615
float p = temp_begin[i];
616
temp_begin[i] = temp_end[i];
617
temp_end[i] = p;
618
}
619
}
620
undo_redo->add_do_method(this, "_set_selection", true, temp_begin, temp_end);
621
undo_redo->add_undo_method(this, "_set_selection", selection.active, selection.begin, selection.end);
622
}
623
624
undo_redo->commit_action();
625
626
_clear_clipboard_data();
627
}
628
629
void GridMapEditor::_show_viewports_transform_gizmo(bool p_value) {
630
Dictionary new_state;
631
new_state["transform_gizmo"] = p_value;
632
for (uint32_t i = 0; i < Node3DEditor::VIEWPORTS_COUNT; i++) {
633
Node3DEditorViewport *viewport = Node3DEditor::get_singleton()->get_editor_viewport(i);
634
viewport->set_state(new_state);
635
}
636
}
637
638
EditorPlugin::AfterGUIInput GridMapEditor::forward_spatial_input_event(Camera3D *p_camera, const Ref<InputEvent> &p_event) {
639
// If the mouse is currently captured, we are most likely in freelook mode.
640
// In this case, disable shortcuts to avoid conflicts with freelook navigation.
641
if (!node || Input::get_singleton()->get_mouse_mode() == Input::MOUSE_MODE_CAPTURED) {
642
return EditorPlugin::AFTER_GUI_INPUT_PASS;
643
}
644
645
Ref<InputEventKey> k = p_event;
646
if (k.is_valid() && k->is_pressed() && !k->is_echo()) {
647
// Transform mode (toggle button):
648
// If we are in Transform mode we pass the events to the 3D editor,
649
// but if the Transform mode shortcut is pressed again, we go back to Selection mode.
650
if (mode_buttons_group->get_pressed_button() == transform_mode_button) {
651
if (transform_mode_button->get_shortcut().is_valid() && transform_mode_button->get_shortcut()->matches_event(p_event)) {
652
select_mode_button->set_pressed(true);
653
accept_event();
654
return EditorPlugin::AFTER_GUI_INPUT_STOP;
655
}
656
return EditorPlugin::AFTER_GUI_INPUT_PASS;
657
}
658
// Tool modes and tool actions:
659
for (BaseButton *b : viewport_shortcut_buttons) {
660
if (b->is_disabled()) {
661
continue;
662
}
663
664
if (b->get_shortcut().is_valid() && b->get_shortcut()->matches_event(p_event)) {
665
if (b->is_toggle_mode()) {
666
b->set_pressed(b->get_button_group().is_valid() || !b->is_pressed());
667
} else {
668
// Can't press a button without toggle mode, so just emit the signal directly.
669
b->emit_signal(SceneStringName(pressed));
670
}
671
accept_event();
672
return EditorPlugin::AFTER_GUI_INPUT_STOP;
673
}
674
}
675
// Hard key actions:
676
if (k->get_keycode() == Key::ESCAPE) {
677
if (input_action == INPUT_PASTE) {
678
_clear_clipboard_data();
679
input_action = INPUT_NONE;
680
_update_paste_indicator();
681
return EditorPlugin::AFTER_GUI_INPUT_STOP;
682
} else if (selection.active) {
683
_set_selection(false);
684
return EditorPlugin::AFTER_GUI_INPUT_STOP;
685
} else {
686
input_action = INPUT_NONE;
687
update_palette();
688
_update_cursor_instance();
689
return EditorPlugin::AFTER_GUI_INPUT_STOP;
690
}
691
}
692
// Options menu shortcuts:
693
Ref<Shortcut> ed_shortcut = ED_GET_SHORTCUT("grid_map/previous_floor");
694
if (ed_shortcut.is_valid() && ed_shortcut->matches_event(p_event)) {
695
accept_event();
696
_menu_option(MENU_OPTION_PREV_LEVEL);
697
return EditorPlugin::AFTER_GUI_INPUT_STOP;
698
}
699
ed_shortcut = ED_GET_SHORTCUT("grid_map/next_floor");
700
if (ed_shortcut.is_valid() && ed_shortcut->matches_event(p_event)) {
701
accept_event();
702
_menu_option(MENU_OPTION_NEXT_LEVEL);
703
return EditorPlugin::AFTER_GUI_INPUT_STOP;
704
}
705
for (int i = 0; i < options->get_popup()->get_item_count(); ++i) {
706
const Ref<Shortcut> &shortcut = options->get_popup()->get_item_shortcut(i);
707
if (shortcut.is_valid() && shortcut->matches_event(p_event)) {
708
// Consume input to avoid conflicts with other plugins.
709
accept_event();
710
_menu_option(options->get_popup()->get_item_id(i));
711
return EditorPlugin::AFTER_GUI_INPUT_STOP;
712
}
713
}
714
}
715
716
Ref<InputEventMouseButton> mb = p_event;
717
if (mb.is_valid()) {
718
if (mb->get_button_index() == MouseButton::WHEEL_UP && (mb->is_command_or_control_pressed())) {
719
if (mb->is_pressed()) {
720
floor->set_value(floor->get_value() + mb->get_factor());
721
}
722
723
return EditorPlugin::AFTER_GUI_INPUT_STOP; // Eaten.
724
} else if (mb->get_button_index() == MouseButton::WHEEL_DOWN && (mb->is_command_or_control_pressed())) {
725
if (mb->is_pressed()) {
726
floor->set_value(floor->get_value() - mb->get_factor());
727
}
728
return EditorPlugin::AFTER_GUI_INPUT_STOP;
729
}
730
731
if (mb->is_pressed()) {
732
Node3DEditorViewport::NavigationScheme nav_scheme = (Node3DEditorViewport::NavigationScheme)EDITOR_GET("editors/3d/navigation/navigation_scheme").operator int();
733
if ((nav_scheme == Node3DEditorViewport::NAVIGATION_MAYA || nav_scheme == Node3DEditorViewport::NAVIGATION_MODO) && mb->is_alt_pressed()) {
734
input_action = INPUT_NONE;
735
} else if (mb->get_button_index() == MouseButton::LEFT) {
736
bool can_edit = (node && node->get_mesh_library().is_valid());
737
if (input_action == INPUT_PASTE) {
738
_do_paste();
739
input_action = INPUT_NONE;
740
_update_paste_indicator();
741
return EditorPlugin::AFTER_GUI_INPUT_STOP;
742
} else if (mode_buttons_group->get_pressed_button() == select_mode_button && can_edit) {
743
input_action = INPUT_SELECT;
744
last_selection = selection;
745
} else if (mode_buttons_group->get_pressed_button() == pick_mode_button && can_edit) {
746
input_action = INPUT_PICK;
747
} else if (mode_buttons_group->get_pressed_button() == paint_mode_button && can_edit) {
748
input_action = INPUT_PAINT;
749
set_items.clear();
750
} else if (mode_buttons_group->get_pressed_button() == erase_mode_button && can_edit) {
751
input_action = INPUT_ERASE;
752
set_items.clear();
753
}
754
} else if (mb->get_button_index() == MouseButton::RIGHT) {
755
if (input_action == INPUT_PASTE) {
756
_clear_clipboard_data();
757
input_action = INPUT_NONE;
758
_update_paste_indicator();
759
return EditorPlugin::AFTER_GUI_INPUT_STOP;
760
} else if (selection.active) {
761
_set_selection(false);
762
return EditorPlugin::AFTER_GUI_INPUT_STOP;
763
}
764
} else {
765
return EditorPlugin::AFTER_GUI_INPUT_PASS;
766
}
767
768
if (do_input_action(p_camera, Point2(mb->get_position().x, mb->get_position().y), true)) {
769
return EditorPlugin::AFTER_GUI_INPUT_STOP;
770
}
771
return EditorPlugin::AFTER_GUI_INPUT_PASS;
772
} else {
773
if ((mb->get_button_index() == MouseButton::LEFT && input_action == INPUT_ERASE) || (mb->get_button_index() == MouseButton::LEFT && input_action == INPUT_PAINT)) {
774
if (set_items.size()) {
775
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
776
undo_redo->create_action(TTR("GridMap Paint"));
777
for (const SetItem &si : set_items) {
778
undo_redo->add_do_method(node, "set_cell_item", si.position, si.new_value, si.new_orientation);
779
}
780
for (uint32_t i = set_items.size(); i > 0; i--) {
781
const SetItem &si = set_items[i - 1];
782
undo_redo->add_undo_method(node, "set_cell_item", si.position, si.old_value, si.old_orientation);
783
}
784
785
undo_redo->commit_action();
786
}
787
set_items.clear();
788
input_action = INPUT_NONE;
789
790
if (set_items.size() > 0) {
791
return EditorPlugin::AFTER_GUI_INPUT_STOP;
792
}
793
return EditorPlugin::AFTER_GUI_INPUT_PASS;
794
}
795
796
if (mb->get_button_index() == MouseButton::LEFT && input_action == INPUT_SELECT) {
797
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
798
undo_redo->create_action(TTR("GridMap Selection"));
799
undo_redo->add_do_method(this, "_set_selection", selection.active, selection.begin, selection.end);
800
undo_redo->add_undo_method(this, "_set_selection", last_selection.active, last_selection.begin, last_selection.end);
801
undo_redo->commit_action();
802
}
803
804
if (mb->get_button_index() == MouseButton::LEFT && input_action != INPUT_NONE) {
805
set_items.clear();
806
input_action = INPUT_NONE;
807
return EditorPlugin::AFTER_GUI_INPUT_STOP;
808
}
809
if (mb->get_button_index() == MouseButton::RIGHT && (input_action == INPUT_ERASE || input_action == INPUT_PASTE)) {
810
input_action = INPUT_NONE;
811
return EditorPlugin::AFTER_GUI_INPUT_STOP;
812
}
813
}
814
}
815
816
Ref<InputEventMouseMotion> mm = p_event;
817
818
if (mm.is_valid()) {
819
// Update the grid, to check if the grid needs to be moved to a tile cursor.
820
update_grid();
821
822
if (do_input_action(p_camera, mm->get_position(), false)) {
823
return EditorPlugin::AFTER_GUI_INPUT_STOP;
824
}
825
return EditorPlugin::AFTER_GUI_INPUT_PASS;
826
}
827
828
Ref<InputEventPanGesture> pan_gesture = p_event;
829
if (pan_gesture.is_valid()) {
830
if (pan_gesture->is_alt_pressed() && pan_gesture->is_command_or_control_pressed()) {
831
const real_t delta = pan_gesture->get_delta().y * 0.5;
832
accumulated_floor_delta += delta;
833
int step = 0;
834
if (Math::abs(accumulated_floor_delta) > 1.0) {
835
step = SIGN(accumulated_floor_delta);
836
accumulated_floor_delta -= step;
837
}
838
if (step) {
839
floor->set_value(floor->get_value() + step);
840
}
841
return EditorPlugin::AFTER_GUI_INPUT_STOP;
842
}
843
}
844
accumulated_floor_delta = 0.0;
845
846
return EditorPlugin::AFTER_GUI_INPUT_PASS;
847
}
848
849
struct _CGMEItemSort {
850
String name;
851
int id = 0;
852
_FORCE_INLINE_ bool operator<(const _CGMEItemSort &r_it) const { return name < r_it.name; }
853
};
854
855
void GridMapEditor::_set_display_mode(int p_mode) {
856
if (display_mode == p_mode) {
857
return;
858
}
859
860
if (p_mode == DISPLAY_LIST) {
861
mode_list->set_pressed(true);
862
mode_thumbnail->set_pressed(false);
863
} else if (p_mode == DISPLAY_THUMBNAIL) {
864
mode_list->set_pressed(false);
865
mode_thumbnail->set_pressed(true);
866
}
867
868
display_mode = p_mode;
869
870
update_palette();
871
}
872
873
void GridMapEditor::_text_changed(const String &p_text) {
874
update_palette();
875
}
876
877
void GridMapEditor::_sbox_input(const Ref<InputEvent> &p_event) {
878
// Redirect navigational key events to the item list.
879
Ref<InputEventKey> key = p_event;
880
if (key.is_valid()) {
881
if (key->is_action("ui_up", true) || key->is_action("ui_down", true) || key->is_action("ui_page_up") || key->is_action("ui_page_down")) {
882
mesh_library_palette->gui_input(key);
883
search_box->accept_event();
884
}
885
}
886
}
887
888
void GridMapEditor::_mesh_library_palette_input(const Ref<InputEvent> &p_ie) {
889
const Ref<InputEventMouseButton> mb = p_ie;
890
891
// Zoom in/out using Ctrl + mouse wheel
892
if (mb.is_valid() && mb->is_pressed() && mb->is_command_or_control_pressed()) {
893
if (mb->is_pressed() && mb->get_button_index() == MouseButton::WHEEL_UP) {
894
zoom_widget->set_zoom(zoom_widget->get_zoom() + 0.2);
895
zoom_widget->emit_signal(SNAME("zoom_changed"), zoom_widget->get_zoom());
896
}
897
898
if (mb->is_pressed() && mb->get_button_index() == MouseButton::WHEEL_DOWN) {
899
zoom_widget->set_zoom(zoom_widget->get_zoom() - 0.2);
900
zoom_widget->emit_signal(SNAME("zoom_changed"), zoom_widget->get_zoom());
901
}
902
}
903
}
904
905
void GridMapEditor::_icon_size_changed(float p_value) {
906
mesh_library_palette->set_icon_scale(p_value);
907
update_palette();
908
}
909
910
void GridMapEditor::update_palette() {
911
float min_size = EDITOR_GET("editors/grid_map/preview_size");
912
min_size *= EDSCALE;
913
914
mesh_library_palette->clear();
915
if (display_mode == DISPLAY_THUMBNAIL) {
916
mesh_library_palette->set_max_columns(0);
917
mesh_library_palette->set_icon_mode(ItemList::ICON_MODE_TOP);
918
mesh_library_palette->set_fixed_column_width(min_size * MAX(zoom_widget->get_zoom(), 1.5));
919
} else if (display_mode == DISPLAY_LIST) {
920
mesh_library_palette->set_max_columns(0);
921
mesh_library_palette->set_icon_mode(ItemList::ICON_MODE_LEFT);
922
mesh_library_palette->set_fixed_column_width(0);
923
}
924
925
mesh_library_palette->set_fixed_icon_size(Size2(min_size, min_size));
926
mesh_library_palette->set_max_text_lines(2);
927
928
if (mesh_library.is_null()) {
929
search_box->set_text("");
930
search_box->set_editable(false);
931
info_message->show();
932
return;
933
}
934
935
search_box->set_editable(true);
936
info_message->hide();
937
938
Vector<int> ids;
939
ids = mesh_library->get_item_list();
940
941
List<_CGMEItemSort> il;
942
for (int i = 0; i < ids.size(); i++) {
943
_CGMEItemSort is;
944
is.id = ids[i];
945
is.name = mesh_library->get_item_name(ids[i]);
946
il.push_back(is);
947
}
948
il.sort();
949
950
String filter = search_box->get_text().strip_edges();
951
952
int item = 0;
953
954
for (_CGMEItemSort &E : il) {
955
int id = E.id;
956
String name = mesh_library->get_item_name(id);
957
Ref<Texture2D> preview = mesh_library->get_item_preview(id);
958
959
if (name.is_empty()) {
960
name = "#" + itos(id);
961
}
962
963
if (!filter.is_empty() && !filter.is_subsequence_ofn(name)) {
964
continue;
965
}
966
967
mesh_library_palette->add_item("");
968
if (preview.is_valid()) {
969
mesh_library_palette->set_item_icon(item, preview);
970
mesh_library_palette->set_item_tooltip(item, name);
971
}
972
mesh_library_palette->set_item_text(item, name);
973
mesh_library_palette->set_item_metadata(item, id);
974
975
if (selected_palette == id) {
976
mesh_library_palette->select(item);
977
}
978
979
item++;
980
}
981
}
982
983
void GridMapEditor::_update_mesh_library() {
984
ERR_FAIL_NULL(node);
985
986
Ref<MeshLibrary> new_mesh_library = node->get_mesh_library();
987
if (new_mesh_library != mesh_library) {
988
if (mesh_library.is_valid()) {
989
mesh_library->disconnect_changed(callable_mp(this, &GridMapEditor::update_palette));
990
}
991
mesh_library = new_mesh_library;
992
} else {
993
return;
994
}
995
996
if (mesh_library.is_valid()) {
997
mesh_library->connect_changed(callable_mp(this, &GridMapEditor::update_palette));
998
}
999
1000
update_palette();
1001
// Make sure we select the first tile as default possible.
1002
if (mesh_library_palette->get_current() == -1 && mesh_library_palette->get_item_count() > 0) {
1003
mesh_library_palette->set_current(0);
1004
selected_palette = mesh_library_palette->get_item_metadata(0);
1005
}
1006
// Update the cursor and grid in case the library is changed or removed.
1007
_update_cursor_instance();
1008
update_grid();
1009
}
1010
1011
void GridMapEditor::edit(GridMap *p_gridmap) {
1012
if (node) {
1013
node->disconnect(SNAME("cell_size_changed"), callable_mp(this, &GridMapEditor::_draw_grids));
1014
node->disconnect(CoreStringName(changed), callable_mp(this, &GridMapEditor::_update_mesh_library));
1015
if (mesh_library.is_valid()) {
1016
mesh_library->disconnect_changed(callable_mp(this, &GridMapEditor::update_palette));
1017
mesh_library = Ref<MeshLibrary>();
1018
}
1019
}
1020
1021
node = p_gridmap;
1022
1023
input_action = INPUT_NONE;
1024
selection.active = false;
1025
_update_selection_transform();
1026
_update_paste_indicator();
1027
1028
spatial_editor = Object::cast_to<Node3DEditorPlugin>(EditorNode::get_singleton()->get_editor_main_screen()->get_selected_plugin());
1029
1030
if (!node) {
1031
set_process(false);
1032
for (int i = 0; i < 3; i++) {
1033
RenderingServer::get_singleton()->instance_set_visible(grid_instance[i], false);
1034
}
1035
1036
if (cursor_instance.is_valid()) {
1037
RenderingServer::get_singleton()->instance_set_visible(cursor_instance, false);
1038
}
1039
1040
return;
1041
}
1042
1043
update_palette();
1044
_update_cursor_instance();
1045
1046
set_process(true);
1047
1048
_draw_grids(node->get_cell_size());
1049
update_grid();
1050
1051
node->connect(SNAME("cell_size_changed"), callable_mp(this, &GridMapEditor::_draw_grids));
1052
node->connect(CoreStringName(changed), callable_mp(this, &GridMapEditor::_update_mesh_library));
1053
_update_mesh_library();
1054
}
1055
1056
void GridMapEditor::update_grid() {
1057
grid_xform.origin.x -= 1; // Force update in hackish way.
1058
1059
grid_ofs[edit_axis] = edit_floor[edit_axis] * node->get_cell_size()[edit_axis];
1060
1061
// If there's a valid tile cursor, offset the grid, otherwise move it back to the node.
1062
edit_grid_xform.origin = cursor_instance.is_valid() ? grid_ofs : Vector3();
1063
edit_grid_xform.basis = Basis();
1064
1065
for (int i = 0; i < 3; i++) {
1066
RenderingServer::get_singleton()->instance_set_visible(grid_instance[i], i == edit_axis);
1067
}
1068
1069
updating = true;
1070
floor->set_value(edit_floor[edit_axis]);
1071
updating = false;
1072
}
1073
1074
void GridMapEditor::_draw_grids(const Vector3 &cell_size) {
1075
Vector3 edited_floor = node->get_meta("_editor_floor_", Vector3());
1076
1077
for (int i = 0; i < 3; i++) {
1078
RS::get_singleton()->mesh_clear(grid[i]);
1079
edit_floor[i] = edited_floor[i];
1080
}
1081
1082
Vector<Vector3> grid_points[3];
1083
Vector<Color> grid_colors[3];
1084
1085
for (int i = 0; i < 3; i++) {
1086
Vector3 axis;
1087
axis[i] = 1;
1088
Vector3 axis_n1;
1089
axis_n1[(i + 1) % 3] = cell_size[(i + 1) % 3];
1090
Vector3 axis_n2;
1091
axis_n2[(i + 2) % 3] = cell_size[(i + 2) % 3];
1092
1093
for (int j = -GRID_CURSOR_SIZE; j <= GRID_CURSOR_SIZE; j++) {
1094
for (int k = -GRID_CURSOR_SIZE; k <= GRID_CURSOR_SIZE; k++) {
1095
Vector3 p = axis_n1 * j + axis_n2 * k;
1096
float trans = Math::pow(MAX(0, 1.0 - (Vector2(j, k).length() / GRID_CURSOR_SIZE)), 2);
1097
1098
Vector3 pj = axis_n1 * (j + 1) + axis_n2 * k;
1099
float transj = Math::pow(MAX(0, 1.0 - (Vector2(j + 1, k).length() / GRID_CURSOR_SIZE)), 2);
1100
1101
Vector3 pk = axis_n1 * j + axis_n2 * (k + 1);
1102
float transk = Math::pow(MAX(0, 1.0 - (Vector2(j, k + 1).length() / GRID_CURSOR_SIZE)), 2);
1103
1104
grid_points[i].push_back(p);
1105
grid_points[i].push_back(pk);
1106
grid_colors[i].push_back(Color(1, 1, 1, trans));
1107
grid_colors[i].push_back(Color(1, 1, 1, transk));
1108
1109
grid_points[i].push_back(p);
1110
grid_points[i].push_back(pj);
1111
grid_colors[i].push_back(Color(1, 1, 1, trans));
1112
grid_colors[i].push_back(Color(1, 1, 1, transj));
1113
}
1114
}
1115
1116
Array d;
1117
d.resize(RS::ARRAY_MAX);
1118
d[RS::ARRAY_VERTEX] = grid_points[i];
1119
d[RS::ARRAY_COLOR] = grid_colors[i];
1120
RenderingServer::get_singleton()->mesh_add_surface_from_arrays(grid[i], RenderingServer::PRIMITIVE_LINES, d);
1121
RenderingServer::get_singleton()->mesh_surface_set_material(grid[i], 0, indicator_mat->get_rid());
1122
}
1123
}
1124
1125
void GridMapEditor::_update_theme() {
1126
transform_mode_button->set_button_icon(get_theme_icon(SNAME("ToolMove"), EditorStringName(EditorIcons)));
1127
select_mode_button->set_button_icon(get_theme_icon(SNAME("ToolSelect"), EditorStringName(EditorIcons)));
1128
erase_mode_button->set_button_icon(get_theme_icon(SNAME("Eraser"), EditorStringName(EditorIcons)));
1129
paint_mode_button->set_button_icon(get_theme_icon(SNAME("Paint"), EditorStringName(EditorIcons)));
1130
pick_mode_button->set_button_icon(get_theme_icon(SNAME("ColorPick"), EditorStringName(EditorIcons)));
1131
fill_action_button->set_button_icon(get_theme_icon(SNAME("Bucket"), EditorStringName(EditorIcons)));
1132
move_action_button->set_button_icon(get_theme_icon(SNAME("ActionCut"), EditorStringName(EditorIcons)));
1133
duplicate_action_button->set_button_icon(get_theme_icon(SNAME("ActionCopy"), EditorStringName(EditorIcons)));
1134
delete_action_button->set_button_icon(get_theme_icon(SNAME("Clear"), EditorStringName(EditorIcons)));
1135
rotate_x_button->set_button_icon(get_theme_icon(SNAME("RotateLeft"), EditorStringName(EditorIcons)));
1136
rotate_y_button->set_button_icon(get_theme_icon(SNAME("ToolRotate"), EditorStringName(EditorIcons)));
1137
rotate_z_button->set_button_icon(get_theme_icon(SNAME("RotateRight"), EditorStringName(EditorIcons)));
1138
search_box->set_right_icon(get_theme_icon(SNAME("Search"), EditorStringName(EditorIcons)));
1139
mode_thumbnail->set_button_icon(get_theme_icon(SNAME("FileThumbnail"), EditorStringName(EditorIcons)));
1140
mode_list->set_button_icon(get_theme_icon(SNAME("FileList"), EditorStringName(EditorIcons)));
1141
options->set_button_icon(get_theme_icon(SNAME("Tools"), EditorStringName(EditorIcons)));
1142
}
1143
1144
void GridMapEditor::_notification(int p_what) {
1145
switch (p_what) {
1146
case NOTIFICATION_ENTER_TREE: {
1147
mesh_library_palette->connect(SceneStringName(item_selected), callable_mp(this, &GridMapEditor::_item_selected_cbk));
1148
1149
const RID scenario = get_tree()->get_root()->get_world_3d()->get_scenario();
1150
1151
for (int i = 0; i < 3; i++) {
1152
grid[i] = RS::get_singleton()->mesh_create();
1153
grid_instance[i] = RS::get_singleton()->instance_create2(grid[i], scenario);
1154
RenderingServer::get_singleton()->instance_set_layer_mask(grid_instance[i], 1 << Node3DEditorViewport::MISC_TOOL_LAYER);
1155
selection_level_instance[i] = RenderingServer::get_singleton()->instance_create2(selection_level_mesh[i], scenario);
1156
RenderingServer::get_singleton()->instance_set_layer_mask(selection_level_instance[i], 1 << Node3DEditorViewport::MISC_TOOL_LAYER);
1157
}
1158
1159
cursor_instance = RenderingServer::get_singleton()->instance_create2(cursor_mesh, scenario);
1160
RenderingServer::get_singleton()->instance_set_layer_mask(cursor_instance, 1 << Node3DEditorViewport::MISC_TOOL_LAYER);
1161
RenderingServer::get_singleton()->instance_set_visible(cursor_instance, false);
1162
selection_instance = RenderingServer::get_singleton()->instance_create2(selection_mesh, scenario);
1163
RenderingServer::get_singleton()->instance_set_layer_mask(selection_instance, 1 << Node3DEditorViewport::MISC_TOOL_LAYER);
1164
paste_instance = RenderingServer::get_singleton()->instance_create2(paste_mesh, scenario);
1165
RenderingServer::get_singleton()->instance_set_layer_mask(paste_instance, 1 << Node3DEditorViewport::MISC_TOOL_LAYER);
1166
1167
_update_selection_transform();
1168
_update_paste_indicator();
1169
_update_theme();
1170
} break;
1171
1172
case NOTIFICATION_EXIT_TREE: {
1173
_clear_clipboard_data();
1174
1175
for (int i = 0; i < 3; i++) {
1176
RS::get_singleton()->free(grid_instance[i]);
1177
RS::get_singleton()->free(grid[i]);
1178
grid_instance[i] = RID();
1179
grid[i] = RID();
1180
RenderingServer::get_singleton()->free(selection_level_instance[i]);
1181
}
1182
1183
RenderingServer::get_singleton()->free(cursor_instance);
1184
RenderingServer::get_singleton()->free(selection_instance);
1185
RenderingServer::get_singleton()->free(paste_instance);
1186
cursor_instance = RID();
1187
selection_instance = RID();
1188
paste_instance = RID();
1189
} break;
1190
1191
case NOTIFICATION_PROCESS: {
1192
if (!node) {
1193
return;
1194
}
1195
1196
Transform3D xf = node->get_global_transform();
1197
1198
if (xf != grid_xform) {
1199
for (int i = 0; i < 3; i++) {
1200
RS::get_singleton()->instance_set_transform(grid_instance[i], xf * edit_grid_xform);
1201
}
1202
grid_xform = xf;
1203
_update_cursor_transform();
1204
_update_selection_transform();
1205
}
1206
} break;
1207
1208
case NOTIFICATION_THEME_CHANGED: {
1209
_update_theme();
1210
} break;
1211
1212
case NOTIFICATION_APPLICATION_FOCUS_OUT: {
1213
if (input_action == INPUT_PAINT) {
1214
// Simulate mouse released event to stop drawing when editor focus exists.
1215
Ref<InputEventMouseButton> release;
1216
release.instantiate();
1217
release->set_button_index(MouseButton::LEFT);
1218
forward_spatial_input_event(nullptr, release);
1219
}
1220
} break;
1221
1222
case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
1223
indicator_mat->set_albedo(EDITOR_GET("editors/3d_gizmos/gizmo_colors/gridmap_grid"));
1224
1225
// Take Preview Size changes into account.
1226
update_palette();
1227
} break;
1228
}
1229
}
1230
1231
void GridMapEditor::_update_cursor_instance() {
1232
if (!node) {
1233
return;
1234
}
1235
1236
if (cursor_instance.is_valid()) {
1237
RenderingServer::get_singleton()->free(cursor_instance);
1238
}
1239
cursor_instance = RID();
1240
1241
const RID scenario = get_tree()->get_root()->get_world_3d()->get_scenario();
1242
1243
if (mode_buttons_group->get_pressed_button() == paint_mode_button) {
1244
if (selected_palette >= 0 && node && node->get_mesh_library().is_valid()) {
1245
Ref<Mesh> mesh = node->get_mesh_library()->get_item_mesh(selected_palette);
1246
if (mesh.is_valid() && mesh->get_rid().is_valid()) {
1247
cursor_instance = RenderingServer::get_singleton()->instance_create2(mesh->get_rid(), scenario);
1248
RS::ShadowCastingSetting cast_shadows = (RS::ShadowCastingSetting)node->get_mesh_library()->get_item_mesh_cast_shadow(selected_palette);
1249
RS::get_singleton()->instance_geometry_set_cast_shadows_setting(cursor_instance, cast_shadows);
1250
}
1251
}
1252
} else if (mode_buttons_group->get_pressed_button() == select_mode_button) {
1253
cursor_inner_mat->set_albedo(Color(default_color, 0.2));
1254
cursor_outer_mat->set_albedo(Color(default_color, 0.8));
1255
cursor_instance = RenderingServer::get_singleton()->instance_create2(cursor_mesh, scenario);
1256
} else if (mode_buttons_group->get_pressed_button() == erase_mode_button) {
1257
cursor_inner_mat->set_albedo(Color(erase_color, 0.2));
1258
cursor_outer_mat->set_albedo(Color(erase_color, 0.8));
1259
cursor_instance = RenderingServer::get_singleton()->instance_create2(cursor_mesh, scenario);
1260
} else if (mode_buttons_group->get_pressed_button() == pick_mode_button) {
1261
cursor_inner_mat->set_albedo(Color(pick_color, 0.2));
1262
cursor_outer_mat->set_albedo(Color(pick_color, 0.8));
1263
cursor_instance = RenderingServer::get_singleton()->instance_create2(cursor_mesh, scenario);
1264
}
1265
1266
// Make the cursor translucent so that it can be distinguished from already-placed tiles.
1267
RenderingServer::get_singleton()->instance_geometry_set_transparency(cursor_instance, 0.5);
1268
1269
_update_cursor_transform();
1270
}
1271
1272
void GridMapEditor::_on_tool_mode_changed() {
1273
_show_viewports_transform_gizmo(mode_buttons_group->get_pressed_button() == transform_mode_button);
1274
_update_cursor_instance();
1275
}
1276
1277
void GridMapEditor::_item_selected_cbk(int idx) {
1278
selected_palette = mesh_library_palette->get_item_metadata(idx);
1279
1280
_update_cursor_instance();
1281
}
1282
1283
void GridMapEditor::_floor_changed(float p_value) {
1284
if (updating) {
1285
return;
1286
}
1287
1288
edit_floor[edit_axis] = p_value;
1289
node->set_meta("_editor_floor_", Vector3(edit_floor[0], edit_floor[1], edit_floor[2]));
1290
update_grid();
1291
_update_selection_transform();
1292
}
1293
1294
void GridMapEditor::_floor_mouse_exited() {
1295
floor->get_line_edit()->release_focus();
1296
}
1297
1298
void GridMapEditor::_bind_methods() {
1299
ClassDB::bind_method("_configure", &GridMapEditor::_configure);
1300
ClassDB::bind_method("_set_selection", &GridMapEditor::_set_selection);
1301
}
1302
1303
GridMapEditor::GridMapEditor() {
1304
ED_SHORTCUT("grid_map/previous_floor", TTRC("Previous Floor"), Key::KEY_1, true);
1305
ED_SHORTCUT("grid_map/next_floor", TTRC("Next Floor"), Key::KEY_3, true);
1306
ED_SHORTCUT("grid_map/edit_x_axis", TTRC("Edit X Axis"), KeyModifierMask::SHIFT + Key::Z, true);
1307
ED_SHORTCUT("grid_map/edit_y_axis", TTRC("Edit Y Axis"), KeyModifierMask::SHIFT + Key::X, true);
1308
ED_SHORTCUT("grid_map/edit_z_axis", TTRC("Edit Z Axis"), KeyModifierMask::SHIFT + Key::C, true);
1309
ED_SHORTCUT("grid_map/keep_selected", TTRC("Keep Selection"));
1310
ED_SHORTCUT("grid_map/clear_rotation", TTRC("Clear Rotation"));
1311
1312
options = memnew(MenuButton);
1313
options->set_theme_type_variation(SceneStringName(FlatButton));
1314
options->get_popup()->add_separator();
1315
options->get_popup()->add_radio_check_shortcut(ED_GET_SHORTCUT("grid_map/edit_x_axis"), MENU_OPTION_X_AXIS);
1316
options->get_popup()->add_radio_check_shortcut(ED_GET_SHORTCUT("grid_map/edit_y_axis"), MENU_OPTION_Y_AXIS);
1317
options->get_popup()->add_radio_check_shortcut(ED_GET_SHORTCUT("grid_map/edit_z_axis"), MENU_OPTION_Z_AXIS);
1318
options->get_popup()->set_item_checked(options->get_popup()->get_item_index(MENU_OPTION_Y_AXIS), true);
1319
options->get_popup()->add_separator();
1320
// TRANSLATORS: This is a toggle to select after pasting the new content.
1321
options->get_popup()->add_shortcut(ED_GET_SHORTCUT("grid_map/clear_rotation"), MENU_OPTION_CURSOR_CLEAR_ROTATION);
1322
options->get_popup()->add_check_shortcut(ED_GET_SHORTCUT("grid_map/keep_selected"), MENU_OPTION_PASTE_SELECTS);
1323
options->get_popup()->set_item_checked(options->get_popup()->get_item_index(MENU_OPTION_PASTE_SELECTS), true);
1324
options->get_popup()->add_separator();
1325
options->get_popup()->add_item(TTR("Settings..."), MENU_OPTION_GRIDMAP_SETTINGS);
1326
1327
settings_dialog = memnew(ConfirmationDialog);
1328
settings_dialog->set_title(TTR("GridMap Settings"));
1329
add_child(settings_dialog);
1330
settings_vbc = memnew(VBoxContainer);
1331
settings_vbc->set_custom_minimum_size(Size2(200, 0) * EDSCALE);
1332
settings_dialog->add_child(settings_vbc);
1333
1334
settings_pick_distance = memnew(SpinBox);
1335
settings_pick_distance->set_max(10000.0f);
1336
settings_pick_distance->set_min(500.0f);
1337
settings_pick_distance->set_step(1.0f);
1338
settings_pick_distance->set_value(EDITOR_GET("editors/grid_map/pick_distance"));
1339
settings_pick_distance->set_accessibility_name(TTRC("Pick Distance:"));
1340
settings_vbc->add_margin_child(TTR("Pick Distance:"), settings_pick_distance);
1341
1342
options->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &GridMapEditor::_menu_option));
1343
1344
toolbar = memnew(HBoxContainer);
1345
add_child(toolbar);
1346
toolbar->set_h_size_flags(SIZE_EXPAND_FILL);
1347
1348
HBoxContainer *mode_buttons = memnew(HBoxContainer);
1349
toolbar->add_child(mode_buttons);
1350
mode_buttons_group.instantiate();
1351
1352
viewport_shortcut_buttons.reserve(12);
1353
1354
transform_mode_button = memnew(Button);
1355
transform_mode_button->set_theme_type_variation(SceneStringName(FlatButton));
1356
transform_mode_button->set_toggle_mode(true);
1357
transform_mode_button->set_button_group(mode_buttons_group);
1358
transform_mode_button->set_shortcut(ED_SHORTCUT("grid_map/transform_tool", TTRC("Transform"), Key::T, true));
1359
transform_mode_button->set_accessibility_name(TTRC("Transform"));
1360
transform_mode_button->connect(SceneStringName(toggled),
1361
callable_mp(this, &GridMapEditor::_on_tool_mode_changed).unbind(1));
1362
mode_buttons->add_child(transform_mode_button);
1363
viewport_shortcut_buttons.push_back(transform_mode_button);
1364
VSeparator *vsep = memnew(VSeparator);
1365
mode_buttons->add_child(vsep);
1366
1367
select_mode_button = memnew(Button);
1368
select_mode_button->set_theme_type_variation(SceneStringName(FlatButton));
1369
select_mode_button->set_toggle_mode(true);
1370
select_mode_button->set_button_group(mode_buttons_group);
1371
select_mode_button->set_shortcut(ED_SHORTCUT("grid_map/selection_tool", TTRC("Selection"), Key::Q, true));
1372
select_mode_button->set_accessibility_name(TTRC("Selection"));
1373
select_mode_button->connect(SceneStringName(toggled),
1374
callable_mp(this, &GridMapEditor::_on_tool_mode_changed).unbind(1));
1375
mode_buttons->add_child(select_mode_button);
1376
viewport_shortcut_buttons.push_back(select_mode_button);
1377
1378
erase_mode_button = memnew(Button);
1379
erase_mode_button->set_theme_type_variation(SceneStringName(FlatButton));
1380
erase_mode_button->set_toggle_mode(true);
1381
erase_mode_button->set_button_group(mode_buttons_group);
1382
erase_mode_button->set_shortcut(ED_SHORTCUT("grid_map/erase_tool", TTRC("Erase"), Key::W, true));
1383
erase_mode_button->set_accessibility_name(TTRC("Erase"));
1384
mode_buttons->add_child(erase_mode_button);
1385
erase_mode_button->connect(SceneStringName(toggled),
1386
callable_mp(this, &GridMapEditor::_on_tool_mode_changed).unbind(1));
1387
viewport_shortcut_buttons.push_back(erase_mode_button);
1388
1389
paint_mode_button = memnew(Button);
1390
paint_mode_button->set_theme_type_variation(SceneStringName(FlatButton));
1391
paint_mode_button->set_toggle_mode(true);
1392
paint_mode_button->set_button_group(mode_buttons_group);
1393
paint_mode_button->set_shortcut(ED_SHORTCUT("grid_map/paint_tool", TTRC("Paint"), Key::E, true));
1394
paint_mode_button->set_accessibility_name(TTRC("Paint"));
1395
paint_mode_button->connect(SceneStringName(toggled),
1396
callable_mp(this, &GridMapEditor::_on_tool_mode_changed).unbind(1));
1397
mode_buttons->add_child(paint_mode_button);
1398
viewport_shortcut_buttons.push_back(paint_mode_button);
1399
1400
pick_mode_button = memnew(Button);
1401
pick_mode_button->set_theme_type_variation(SceneStringName(FlatButton));
1402
pick_mode_button->set_toggle_mode(true);
1403
pick_mode_button->set_button_group(mode_buttons_group);
1404
pick_mode_button->set_shortcut(ED_SHORTCUT("grid_map/pick_tool", TTRC("Pick"), Key::R, true));
1405
pick_mode_button->set_accessibility_name(TTRC("Pick"));
1406
pick_mode_button->connect(SceneStringName(toggled),
1407
callable_mp(this, &GridMapEditor::_on_tool_mode_changed).unbind(1));
1408
mode_buttons->add_child(pick_mode_button);
1409
viewport_shortcut_buttons.push_back(pick_mode_button);
1410
1411
vsep = memnew(VSeparator);
1412
toolbar->add_child(vsep);
1413
1414
HBoxContainer *action_buttons = memnew(HBoxContainer);
1415
toolbar->add_child(action_buttons);
1416
1417
fill_action_button = memnew(Button);
1418
fill_action_button->set_theme_type_variation(SceneStringName(FlatButton));
1419
fill_action_button->set_shortcut(ED_SHORTCUT("grid_map/fill_tool", TTRC("Fill"), Key::Z, true));
1420
fill_action_button->set_accessibility_name(TTRC("Fill"));
1421
fill_action_button->connect(SceneStringName(pressed),
1422
callable_mp(this, &GridMapEditor::_menu_option).bind(MENU_OPTION_SELECTION_FILL));
1423
action_buttons->add_child(fill_action_button);
1424
viewport_shortcut_buttons.push_back(fill_action_button);
1425
1426
move_action_button = memnew(Button);
1427
move_action_button->set_theme_type_variation(SceneStringName(FlatButton));
1428
move_action_button->set_shortcut(ED_SHORTCUT("grid_map/move_tool", TTRC("Move"), Key::X, true));
1429
fill_action_button->set_accessibility_name(TTRC("Move"));
1430
move_action_button->connect(SceneStringName(pressed),
1431
callable_mp(this, &GridMapEditor::_menu_option).bind(MENU_OPTION_SELECTION_CUT));
1432
action_buttons->add_child(move_action_button);
1433
viewport_shortcut_buttons.push_back(move_action_button);
1434
1435
duplicate_action_button = memnew(Button);
1436
duplicate_action_button->set_theme_type_variation(SceneStringName(FlatButton));
1437
duplicate_action_button->set_shortcut(ED_SHORTCUT("grid_map/duplicate_tool", TTRC("Duplicate"), Key::C, true));
1438
duplicate_action_button->set_accessibility_name(TTRC("Duplicate"));
1439
duplicate_action_button->connect(SceneStringName(pressed),
1440
callable_mp(this, &GridMapEditor::_menu_option).bind(MENU_OPTION_SELECTION_DUPLICATE));
1441
action_buttons->add_child(duplicate_action_button);
1442
viewport_shortcut_buttons.push_back(duplicate_action_button);
1443
1444
delete_action_button = memnew(Button);
1445
delete_action_button->set_theme_type_variation(SceneStringName(FlatButton));
1446
delete_action_button->set_shortcut(ED_SHORTCUT("grid_map/delete_tool", TTRC("Delete"), Key::V, true));
1447
delete_action_button->set_accessibility_name(TTRC("Delete"));
1448
delete_action_button->connect(SceneStringName(pressed),
1449
callable_mp(this, &GridMapEditor::_menu_option).bind(MENU_OPTION_SELECTION_CLEAR));
1450
action_buttons->add_child(delete_action_button);
1451
viewport_shortcut_buttons.push_back(delete_action_button);
1452
1453
vsep = memnew(VSeparator);
1454
toolbar->add_child(vsep);
1455
1456
HBoxContainer *rotation_buttons = memnew(HBoxContainer);
1457
toolbar->add_child(rotation_buttons);
1458
1459
rotate_x_button = memnew(Button);
1460
rotate_x_button->set_theme_type_variation(SceneStringName(FlatButton));
1461
rotate_x_button->set_shortcut(ED_SHORTCUT("grid_map/cursor_rotate_x", TTRC("Cursor Rotate X"), Key::A, true));
1462
rotate_x_button->set_accessibility_name(TTRC("Cursor Rotate X"));
1463
rotate_x_button->connect(SceneStringName(pressed),
1464
callable_mp(this, &GridMapEditor::_menu_option).bind(MENU_OPTION_CURSOR_ROTATE_X));
1465
rotation_buttons->add_child(rotate_x_button);
1466
viewport_shortcut_buttons.push_back(rotate_x_button);
1467
1468
rotate_y_button = memnew(Button);
1469
rotate_y_button->set_theme_type_variation(SceneStringName(FlatButton));
1470
rotate_y_button->set_shortcut(ED_SHORTCUT("grid_map/cursor_rotate_y", TTRC("Cursor Rotate Y"), Key::S, true));
1471
rotate_y_button->set_accessibility_name(TTRC("Cursor Rotate Y"));
1472
rotate_y_button->connect(SceneStringName(pressed),
1473
callable_mp(this, &GridMapEditor::_menu_option).bind(MENU_OPTION_CURSOR_ROTATE_Y));
1474
rotation_buttons->add_child(rotate_y_button);
1475
viewport_shortcut_buttons.push_back(rotate_y_button);
1476
1477
rotate_z_button = memnew(Button);
1478
rotate_z_button->set_theme_type_variation(SceneStringName(FlatButton));
1479
rotate_z_button->set_shortcut(ED_SHORTCUT("grid_map/cursor_rotate_z", TTRC("Cursor Rotate Z"), Key::D, true));
1480
rotate_z_button->set_accessibility_name(TTRC("Cursor Rotate Z"));
1481
rotate_z_button->connect(SceneStringName(pressed),
1482
callable_mp(this, &GridMapEditor::_menu_option).bind(MENU_OPTION_CURSOR_ROTATE_Z));
1483
rotation_buttons->add_child(rotate_z_button);
1484
viewport_shortcut_buttons.push_back(rotate_z_button);
1485
1486
// Wide empty separation control. (like BoxContainer::add_spacer())
1487
Control *c = memnew(Control);
1488
c->set_mouse_filter(MOUSE_FILTER_PASS);
1489
c->set_h_size_flags(SIZE_EXPAND_FILL);
1490
toolbar->add_child(c);
1491
1492
floor = memnew(SpinBox);
1493
floor->set_min(-32767);
1494
floor->set_max(32767);
1495
floor->set_step(1);
1496
floor->set_accessibility_name(TTRC("Change Grid Floor:"));
1497
floor->set_tooltip_text(
1498
vformat(TTR("Change Grid Floor:\nPrevious Plane (%s)\nNext Plane (%s)"),
1499
ED_GET_SHORTCUT("grid_map/previous_floor")->get_as_text(),
1500
ED_GET_SHORTCUT("grid_map/next_floor")->get_as_text()));
1501
toolbar->add_child(floor);
1502
floor->get_line_edit()->add_theme_constant_override("minimum_character_width", 2);
1503
floor->get_line_edit()->set_context_menu_enabled(false);
1504
floor->connect(SceneStringName(value_changed), callable_mp(this, &GridMapEditor::_floor_changed));
1505
floor->connect(SceneStringName(mouse_exited), callable_mp(this, &GridMapEditor::_floor_mouse_exited));
1506
floor->get_line_edit()->connect(SceneStringName(mouse_exited), callable_mp(this, &GridMapEditor::_floor_mouse_exited));
1507
1508
search_box = memnew(LineEdit);
1509
search_box->add_theme_constant_override("minimum_character_width", 10);
1510
search_box->set_placeholder(TTR("Filter Meshes"));
1511
search_box->set_accessibility_name(TTRC("Filter Meshes"));
1512
search_box->set_clear_button_enabled(true);
1513
toolbar->add_child(search_box);
1514
search_box->connect(SceneStringName(text_changed), callable_mp(this, &GridMapEditor::_text_changed));
1515
search_box->connect(SceneStringName(gui_input), callable_mp(this, &GridMapEditor::_sbox_input));
1516
1517
zoom_widget = memnew(EditorZoomWidget);
1518
toolbar->add_child(zoom_widget);
1519
zoom_widget->setup_zoom_limits(0.2, 4);
1520
zoom_widget->set_zoom(1.0);
1521
zoom_widget->set_anchors_and_offsets_preset(Control::PRESET_TOP_LEFT, Control::PRESET_MODE_MINSIZE, 2 * EDSCALE);
1522
zoom_widget->connect("zoom_changed", callable_mp(this, &GridMapEditor::_icon_size_changed));
1523
zoom_widget->set_shortcut_context(this);
1524
1525
mode_thumbnail = memnew(Button);
1526
mode_thumbnail->set_theme_type_variation(SceneStringName(FlatButton));
1527
mode_thumbnail->set_toggle_mode(true);
1528
mode_thumbnail->set_accessibility_name(TTRC("View as Thumbnails"));
1529
mode_thumbnail->set_pressed(true);
1530
toolbar->add_child(mode_thumbnail);
1531
mode_thumbnail->connect(SceneStringName(pressed), callable_mp(this, &GridMapEditor::_set_display_mode).bind(DISPLAY_THUMBNAIL));
1532
1533
mode_list = memnew(Button);
1534
mode_list->set_theme_type_variation(SceneStringName(FlatButton));
1535
mode_list->set_toggle_mode(true);
1536
mode_list->set_accessibility_name(TTRC("View as List"));
1537
mode_list->set_pressed(false);
1538
toolbar->add_child(mode_list);
1539
mode_list->connect(SceneStringName(pressed), callable_mp(this, &GridMapEditor::_set_display_mode).bind(DISPLAY_LIST));
1540
1541
toolbar->add_child(options);
1542
1543
mesh_library_palette = memnew(ItemList);
1544
mesh_library_palette->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
1545
add_child(mesh_library_palette);
1546
mesh_library_palette->set_v_size_flags(SIZE_EXPAND_FILL);
1547
mesh_library_palette->connect(SceneStringName(gui_input), callable_mp(this, &GridMapEditor::_mesh_library_palette_input));
1548
1549
info_message = memnew(Label);
1550
info_message->set_focus_mode(FOCUS_ACCESSIBILITY);
1551
info_message->set_text(TTR("Give a MeshLibrary resource to this GridMap to use its meshes."));
1552
info_message->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
1553
info_message->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
1554
info_message->set_autowrap_mode(TextServer::AUTOWRAP_WORD_SMART);
1555
info_message->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
1556
info_message->set_anchors_and_offsets_preset(PRESET_FULL_RECT, PRESET_MODE_KEEP_SIZE, 8 * EDSCALE);
1557
mesh_library_palette->add_child(info_message);
1558
1559
edit_axis = Vector3::AXIS_Y;
1560
edit_floor[0] = -1;
1561
edit_floor[1] = -1;
1562
edit_floor[2] = -1;
1563
1564
cursor_mesh = RenderingServer::get_singleton()->mesh_create();
1565
selection_mesh = RenderingServer::get_singleton()->mesh_create();
1566
paste_mesh = RenderingServer::get_singleton()->mesh_create();
1567
1568
{
1569
// Selection mesh create.
1570
1571
Vector<Vector3> lines;
1572
Vector<Vector3> triangles;
1573
Vector<Vector3> square[3];
1574
1575
for (int i = 0; i < 6; i++) {
1576
Vector3 face_points[4];
1577
1578
for (int j = 0; j < 4; j++) {
1579
float v[3];
1580
v[0] = 1.0;
1581
v[1] = 1 - 2 * ((j >> 1) & 1);
1582
v[2] = v[1] * (1 - 2 * (j & 1));
1583
1584
for (int k = 0; k < 3; k++) {
1585
if (i < 3) {
1586
face_points[j][(i + k) % 3] = v[k];
1587
} else {
1588
face_points[3 - j][(i + k) % 3] = -v[k];
1589
}
1590
}
1591
}
1592
1593
triangles.push_back(face_points[0] * 0.5 + Vector3(0.5, 0.5, 0.5));
1594
triangles.push_back(face_points[1] * 0.5 + Vector3(0.5, 0.5, 0.5));
1595
triangles.push_back(face_points[2] * 0.5 + Vector3(0.5, 0.5, 0.5));
1596
1597
triangles.push_back(face_points[2] * 0.5 + Vector3(0.5, 0.5, 0.5));
1598
triangles.push_back(face_points[3] * 0.5 + Vector3(0.5, 0.5, 0.5));
1599
triangles.push_back(face_points[0] * 0.5 + Vector3(0.5, 0.5, 0.5));
1600
}
1601
1602
for (int i = 0; i < 12; i++) {
1603
AABB base(Vector3(0, 0, 0), Vector3(1, 1, 1));
1604
Vector3 a, b;
1605
base.get_edge(i, a, b);
1606
lines.push_back(a);
1607
lines.push_back(b);
1608
}
1609
1610
for (int i = 0; i < 3; i++) {
1611
Vector3 points[4];
1612
for (int j = 0; j < 4; j++) {
1613
static const bool orderx[4] = { false, true, true, false };
1614
static const bool ordery[4] = { false, false, true, true };
1615
1616
Vector3 sp;
1617
if (orderx[j]) {
1618
sp[(i + 1) % 3] = 1.0;
1619
}
1620
if (ordery[j]) {
1621
sp[(i + 2) % 3] = 1.0;
1622
}
1623
1624
points[j] = sp;
1625
}
1626
1627
for (int j = 0; j < 4; j++) {
1628
Vector3 ofs;
1629
ofs[i] += 0.01;
1630
square[i].push_back(points[j] - ofs);
1631
square[i].push_back(points[(j + 1) % 4] - ofs);
1632
square[i].push_back(points[j] + ofs);
1633
square[i].push_back(points[(j + 1) % 4] + ofs);
1634
}
1635
}
1636
1637
Array d;
1638
d.resize(RS::ARRAY_MAX);
1639
1640
default_color = Color(0.0, 0.565, 1.0); // blue 0.7, 0.7, 1.0
1641
erase_color = Color(1.0, 0.2, 0.2); // red
1642
pick_color = Color(1, 0.7, 0); // orange/yellow
1643
1644
cursor_inner_mat.instantiate();
1645
cursor_inner_mat->set_albedo(Color(default_color, 0.2));
1646
cursor_inner_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
1647
cursor_inner_mat->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
1648
cursor_inner_mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
1649
1650
cursor_outer_mat.instantiate();
1651
cursor_outer_mat->set_albedo(Color(default_color, 0.8));
1652
cursor_outer_mat->set_on_top_of_alpha();
1653
cursor_outer_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
1654
cursor_outer_mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
1655
cursor_outer_mat->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
1656
1657
inner_mat.instantiate();
1658
inner_mat->set_albedo(Color(default_color, 0.2));
1659
inner_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
1660
inner_mat->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
1661
inner_mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
1662
1663
outer_mat.instantiate();
1664
outer_mat->set_albedo(Color(default_color, 0.8));
1665
outer_mat->set_on_top_of_alpha();
1666
outer_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
1667
outer_mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
1668
outer_mat->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
1669
1670
selection_floor_mat.instantiate();
1671
selection_floor_mat->set_albedo(Color(0.80, 0.80, 1.0, 1));
1672
selection_floor_mat->set_on_top_of_alpha();
1673
selection_floor_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
1674
selection_floor_mat->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
1675
1676
d[RS::ARRAY_VERTEX] = triangles;
1677
RenderingServer::get_singleton()->mesh_add_surface_from_arrays(cursor_mesh, RS::PRIMITIVE_TRIANGLES, d);
1678
RenderingServer::get_singleton()->mesh_surface_set_material(cursor_mesh, 0, cursor_inner_mat->get_rid());
1679
1680
d[RS::ARRAY_VERTEX] = lines;
1681
RenderingServer::get_singleton()->mesh_add_surface_from_arrays(cursor_mesh, RS::PRIMITIVE_LINES, d);
1682
RenderingServer::get_singleton()->mesh_surface_set_material(cursor_mesh, 1, cursor_outer_mat->get_rid());
1683
1684
d[RS::ARRAY_VERTEX] = triangles;
1685
RenderingServer::get_singleton()->mesh_add_surface_from_arrays(selection_mesh, RS::PRIMITIVE_TRIANGLES, d);
1686
RenderingServer::get_singleton()->mesh_surface_set_material(selection_mesh, 0, inner_mat->get_rid());
1687
1688
d[RS::ARRAY_VERTEX] = lines;
1689
RenderingServer::get_singleton()->mesh_add_surface_from_arrays(selection_mesh, RS::PRIMITIVE_LINES, d);
1690
RenderingServer::get_singleton()->mesh_surface_set_material(selection_mesh, 1, outer_mat->get_rid());
1691
1692
d[RS::ARRAY_VERTEX] = triangles;
1693
RenderingServer::get_singleton()->mesh_add_surface_from_arrays(paste_mesh, RS::PRIMITIVE_TRIANGLES, d);
1694
RenderingServer::get_singleton()->mesh_surface_set_material(paste_mesh, 0, inner_mat->get_rid());
1695
1696
d[RS::ARRAY_VERTEX] = lines;
1697
RenderingServer::get_singleton()->mesh_add_surface_from_arrays(paste_mesh, RS::PRIMITIVE_LINES, d);
1698
RenderingServer::get_singleton()->mesh_surface_set_material(paste_mesh, 1, outer_mat->get_rid());
1699
1700
for (int i = 0; i < 3; i++) {
1701
d[RS::ARRAY_VERTEX] = square[i];
1702
selection_level_mesh[i] = RS::get_singleton()->mesh_create();
1703
RenderingServer::get_singleton()->mesh_add_surface_from_arrays(selection_level_mesh[i], RS::PRIMITIVE_LINES, d);
1704
RenderingServer::get_singleton()->mesh_surface_set_material(selection_level_mesh[i], 0, selection_floor_mat->get_rid());
1705
}
1706
}
1707
1708
_set_selection(false);
1709
1710
indicator_mat.instantiate();
1711
indicator_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
1712
indicator_mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
1713
indicator_mat->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
1714
indicator_mat->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
1715
indicator_mat->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
1716
indicator_mat->set_albedo(EDITOR_GET("editors/3d_gizmos/gizmo_colors/gridmap_grid"));
1717
}
1718
1719
GridMapEditor::~GridMapEditor() {
1720
ERR_FAIL_NULL(RenderingServer::get_singleton());
1721
_clear_clipboard_data();
1722
1723
for (int i = 0; i < 3; i++) {
1724
if (grid[i].is_valid()) {
1725
RenderingServer::get_singleton()->free(grid[i]);
1726
}
1727
if (grid_instance[i].is_valid()) {
1728
RenderingServer::get_singleton()->free(grid_instance[i]);
1729
}
1730
if (selection_level_instance[i].is_valid()) {
1731
RenderingServer::get_singleton()->free(selection_level_instance[i]);
1732
}
1733
if (selection_level_mesh[i].is_valid()) {
1734
RenderingServer::get_singleton()->free(selection_level_mesh[i]);
1735
}
1736
}
1737
1738
RenderingServer::get_singleton()->free(cursor_mesh);
1739
if (cursor_instance.is_valid()) {
1740
RenderingServer::get_singleton()->free(cursor_instance);
1741
}
1742
1743
RenderingServer::get_singleton()->free(selection_mesh);
1744
if (selection_instance.is_valid()) {
1745
RenderingServer::get_singleton()->free(selection_instance);
1746
}
1747
1748
RenderingServer::get_singleton()->free(paste_mesh);
1749
if (paste_instance.is_valid()) {
1750
RenderingServer::get_singleton()->free(paste_instance);
1751
}
1752
}
1753
1754
void GridMapEditorPlugin::_notification(int p_what) {
1755
switch (p_what) {
1756
case NOTIFICATION_ENTER_TREE: {
1757
grid_map_editor = memnew(GridMapEditor);
1758
grid_map_editor->set_h_size_flags(Control::SIZE_EXPAND_FILL);
1759
grid_map_editor->set_v_size_flags(Control::SIZE_EXPAND_FILL);
1760
grid_map_editor->set_custom_minimum_size(Size2(0, 200) * EDSCALE);
1761
grid_map_editor->hide();
1762
1763
panel_button = EditorNode::get_bottom_panel()->add_item(TTRC("GridMap"), grid_map_editor, ED_SHORTCUT_AND_COMMAND("bottom_panels/toggle_grid_map_bottom_panel", TTRC("Toggle GridMap Bottom Panel")));
1764
panel_button->hide();
1765
} break;
1766
case NOTIFICATION_EXIT_TREE: {
1767
EditorNode::get_bottom_panel()->remove_item(grid_map_editor);
1768
memdelete_notnull(grid_map_editor);
1769
grid_map_editor = nullptr;
1770
panel_button = nullptr;
1771
} break;
1772
}
1773
}
1774
1775
void GridMapEditorPlugin::_bind_methods() {
1776
ClassDB::bind_method(D_METHOD("get_current_grid_map"), &GridMapEditorPlugin::get_current_grid_map);
1777
ClassDB::bind_method(D_METHOD("set_selection", "begin", "end"), &GridMapEditorPlugin::set_selection);
1778
ClassDB::bind_method(D_METHOD("clear_selection"), &GridMapEditorPlugin::clear_selection);
1779
ClassDB::bind_method(D_METHOD("get_selection"), &GridMapEditorPlugin::get_selection);
1780
ClassDB::bind_method(D_METHOD("has_selection"), &GridMapEditorPlugin::has_selection);
1781
ClassDB::bind_method(D_METHOD("get_selected_cells"), &GridMapEditorPlugin::get_selected_cells);
1782
ClassDB::bind_method(D_METHOD("set_selected_palette_item", "item"), &GridMapEditorPlugin::set_selected_palette_item);
1783
ClassDB::bind_method(D_METHOD("get_selected_palette_item"), &GridMapEditorPlugin::get_selected_palette_item);
1784
}
1785
1786
void GridMapEditorPlugin::edit(Object *p_object) {
1787
ERR_FAIL_NULL(grid_map_editor);
1788
grid_map_editor->edit(Object::cast_to<GridMap>(p_object));
1789
}
1790
1791
bool GridMapEditorPlugin::handles(Object *p_object) const {
1792
return p_object->is_class("GridMap");
1793
}
1794
1795
void GridMapEditorPlugin::make_visible(bool p_visible) {
1796
ERR_FAIL_NULL(grid_map_editor);
1797
if (p_visible) {
1798
BaseButton *button = grid_map_editor->mode_buttons_group->get_pressed_button();
1799
if (button == nullptr) {
1800
grid_map_editor->select_mode_button->set_pressed(true);
1801
}
1802
grid_map_editor->_on_tool_mode_changed();
1803
panel_button->show();
1804
EditorNode::get_bottom_panel()->make_item_visible(grid_map_editor);
1805
grid_map_editor->set_process(true);
1806
} else {
1807
grid_map_editor->_show_viewports_transform_gizmo(true);
1808
panel_button->hide();
1809
if (grid_map_editor->is_visible_in_tree()) {
1810
EditorNode::get_bottom_panel()->hide_bottom_panel();
1811
}
1812
grid_map_editor->set_process(false);
1813
}
1814
}
1815
1816
GridMap *GridMapEditorPlugin::get_current_grid_map() const {
1817
ERR_FAIL_NULL_V(grid_map_editor, nullptr);
1818
return grid_map_editor->node;
1819
}
1820
1821
void GridMapEditorPlugin::set_selection(const Vector3i &p_begin, const Vector3i &p_end) {
1822
ERR_FAIL_NULL(grid_map_editor);
1823
grid_map_editor->_set_selection(true, p_begin, p_end);
1824
}
1825
1826
void GridMapEditorPlugin::clear_selection() {
1827
ERR_FAIL_NULL(grid_map_editor);
1828
grid_map_editor->_set_selection(false);
1829
}
1830
1831
AABB GridMapEditorPlugin::get_selection() const {
1832
ERR_FAIL_NULL_V(grid_map_editor, AABB());
1833
return grid_map_editor->_get_selection();
1834
}
1835
1836
bool GridMapEditorPlugin::has_selection() const {
1837
ERR_FAIL_NULL_V(grid_map_editor, false);
1838
return grid_map_editor->_has_selection();
1839
}
1840
1841
Array GridMapEditorPlugin::get_selected_cells() const {
1842
ERR_FAIL_NULL_V(grid_map_editor, Array());
1843
return grid_map_editor->_get_selected_cells();
1844
}
1845
1846
void GridMapEditorPlugin::set_selected_palette_item(int p_item) const {
1847
ERR_FAIL_NULL(grid_map_editor);
1848
if (grid_map_editor->node && grid_map_editor->node->get_mesh_library().is_valid()) {
1849
if (p_item < -1) {
1850
p_item = -1;
1851
} else if (p_item >= grid_map_editor->node->get_mesh_library()->get_item_list().size()) {
1852
p_item = grid_map_editor->node->get_mesh_library()->get_item_list().size() - 1;
1853
}
1854
if (p_item != grid_map_editor->selected_palette) {
1855
grid_map_editor->selected_palette = p_item;
1856
grid_map_editor->_update_cursor_instance();
1857
grid_map_editor->update_palette();
1858
}
1859
}
1860
}
1861
1862
int GridMapEditorPlugin::get_selected_palette_item() const {
1863
ERR_FAIL_NULL_V(grid_map_editor, 0);
1864
if (grid_map_editor->selected_palette >= 0 && grid_map_editor->node && grid_map_editor->node->get_mesh_library().is_valid()) {
1865
return grid_map_editor->selected_palette;
1866
} else {
1867
return -1;
1868
}
1869
}
1870
1871