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