Debian Codegraph @

modules/gridmap/editor/grid_map_editor_plugin.cpp

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