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