| 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 | #ifdef TOOLS_ENABLED |
| 34 | |
| 35 | #include "core/core_string_names.h" |
| 36 | #include "core/input/input.h" |
| 37 | #include "core/os/keyboard.h" |
| 38 | #include "editor/editor_node.h" |
| 39 | #include "editor/editor_scale.h" |
| 40 | #include "editor/editor_settings.h" |
| 41 | #include "editor/editor_string_names.h" |
| 42 | #include "editor/editor_undo_redo_manager.h" |
| 43 | #include "editor/plugins/node_3d_editor_plugin.h" |
| 44 | #include "scene/3d/camera_3d.h" |
| 45 | #include "scene/gui/dialogs.h" |
| 46 | #include "scene/gui/label.h" |
| 47 | #include "scene/gui/menu_button.h" |
| 48 | #include "scene/gui/separator.h" |
| 49 | #include "scene/main/window.h" |
| 50 | |
| 51 | void GridMapEditor::_configure() { |
| 52 | if (!node) { |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | update_grid(); |
| 57 | } |
| 58 | |
| 59 | void GridMapEditor::(int p_option) { |
| 60 | switch (p_option) { |
| 61 | case MENU_OPTION_PREV_LEVEL: { |
| 62 | floor->set_value(floor->get_value() - 1); |
| 63 | } break; |
| 64 | |
| 65 | case MENU_OPTION_NEXT_LEVEL: { |
| 66 | floor->set_value(floor->get_value() + 1); |
| 67 | } break; |
| 68 | |
| 69 | case MENU_OPTION_X_AXIS: |
| 70 | case MENU_OPTION_Y_AXIS: |
| 71 | case MENU_OPTION_Z_AXIS: { |
| 72 | int new_axis = p_option - MENU_OPTION_X_AXIS; |
| 73 | for (int i = 0; i < 3; i++) { |
| 74 | int idx = options->get_popup()->get_item_index(MENU_OPTION_X_AXIS + i); |
| 75 | options->get_popup()->set_item_checked(idx, i == new_axis); |
| 76 | } |
| 77 | |
| 78 | if (edit_axis != new_axis) { |
| 79 | int item1 = options->get_popup()->get_item_index(MENU_OPTION_NEXT_LEVEL); |
| 80 | int item2 = options->get_popup()->get_item_index(MENU_OPTION_PREV_LEVEL); |
| 81 | if (edit_axis == Vector3::AXIS_Y) { |
| 82 | options->get_popup()->set_item_text(item1, TTR("Next Plane" )); |
| 83 | options->get_popup()->set_item_text(item2, TTR("Previous Plane" )); |
| 84 | spin_box_label->set_text(TTR("Plane:" )); |
| 85 | } else if (new_axis == Vector3::AXIS_Y) { |
| 86 | options->get_popup()->set_item_text(item1, TTR("Next Floor" )); |
| 87 | options->get_popup()->set_item_text(item2, TTR("Previous Floor" )); |
| 88 | spin_box_label->set_text(TTR("Floor:" )); |
| 89 | } |
| 90 | } |
| 91 | edit_axis = Vector3::Axis(new_axis); |
| 92 | update_grid(); |
| 93 | |
| 94 | } break; |
| 95 | case MENU_OPTION_CURSOR_ROTATE_Y: { |
| 96 | Basis r; |
| 97 | if (input_action == INPUT_PASTE) { |
| 98 | r = node->get_basis_with_orthogonal_index(paste_indicator.orientation); |
| 99 | r.rotate(Vector3(0, 1, 0), -Math_PI / 2.0); |
| 100 | paste_indicator.orientation = node->get_orthogonal_index_from_basis(r); |
| 101 | _update_paste_indicator(); |
| 102 | break; |
| 103 | } |
| 104 | |
| 105 | r = node->get_basis_with_orthogonal_index(cursor_rot); |
| 106 | r.rotate(Vector3(0, 1, 0), -Math_PI / 2.0); |
| 107 | cursor_rot = node->get_orthogonal_index_from_basis(r); |
| 108 | _update_cursor_transform(); |
| 109 | } break; |
| 110 | case MENU_OPTION_CURSOR_ROTATE_X: { |
| 111 | Basis r; |
| 112 | if (input_action == INPUT_PASTE) { |
| 113 | r = node->get_basis_with_orthogonal_index(paste_indicator.orientation); |
| 114 | r.rotate(Vector3(1, 0, 0), -Math_PI / 2.0); |
| 115 | paste_indicator.orientation = node->get_orthogonal_index_from_basis(r); |
| 116 | _update_paste_indicator(); |
| 117 | break; |
| 118 | } |
| 119 | |
| 120 | r = node->get_basis_with_orthogonal_index(cursor_rot); |
| 121 | r.rotate(Vector3(1, 0, 0), -Math_PI / 2.0); |
| 122 | cursor_rot = node->get_orthogonal_index_from_basis(r); |
| 123 | _update_cursor_transform(); |
| 124 | } break; |
| 125 | case MENU_OPTION_CURSOR_ROTATE_Z: { |
| 126 | Basis r; |
| 127 | if (input_action == INPUT_PASTE) { |
| 128 | r = node->get_basis_with_orthogonal_index(paste_indicator.orientation); |
| 129 | r.rotate(Vector3(0, 0, 1), -Math_PI / 2.0); |
| 130 | paste_indicator.orientation = node->get_orthogonal_index_from_basis(r); |
| 131 | _update_paste_indicator(); |
| 132 | break; |
| 133 | } |
| 134 | |
| 135 | r = node->get_basis_with_orthogonal_index(cursor_rot); |
| 136 | r.rotate(Vector3(0, 0, 1), -Math_PI / 2.0); |
| 137 | cursor_rot = node->get_orthogonal_index_from_basis(r); |
| 138 | _update_cursor_transform(); |
| 139 | } break; |
| 140 | case MENU_OPTION_CURSOR_BACK_ROTATE_Y: { |
| 141 | Basis r; |
| 142 | if (input_action == INPUT_PASTE) { |
| 143 | r = node->get_basis_with_orthogonal_index(paste_indicator.orientation); |
| 144 | r.rotate(Vector3(0, 1, 0), Math_PI / 2.0); |
| 145 | paste_indicator.orientation = node->get_orthogonal_index_from_basis(r); |
| 146 | _update_paste_indicator(); |
| 147 | break; |
| 148 | } |
| 149 | |
| 150 | r = node->get_basis_with_orthogonal_index(cursor_rot); |
| 151 | r.rotate(Vector3(0, 1, 0), Math_PI / 2.0); |
| 152 | cursor_rot = node->get_orthogonal_index_from_basis(r); |
| 153 | _update_cursor_transform(); |
| 154 | } break; |
| 155 | case MENU_OPTION_CURSOR_BACK_ROTATE_X: { |
| 156 | Basis r; |
| 157 | if (input_action == INPUT_PASTE) { |
| 158 | r = node->get_basis_with_orthogonal_index(paste_indicator.orientation); |
| 159 | r.rotate(Vector3(1, 0, 0), Math_PI / 2.0); |
| 160 | paste_indicator.orientation = node->get_orthogonal_index_from_basis(r); |
| 161 | _update_paste_indicator(); |
| 162 | break; |
| 163 | } |
| 164 | |
| 165 | r = node->get_basis_with_orthogonal_index(cursor_rot); |
| 166 | r.rotate(Vector3(1, 0, 0), Math_PI / 2.0); |
| 167 | cursor_rot = node->get_orthogonal_index_from_basis(r); |
| 168 | _update_cursor_transform(); |
| 169 | } break; |
| 170 | case MENU_OPTION_CURSOR_BACK_ROTATE_Z: { |
| 171 | Basis r; |
| 172 | if (input_action == INPUT_PASTE) { |
| 173 | r = node->get_basis_with_orthogonal_index(paste_indicator.orientation); |
| 174 | r.rotate(Vector3(0, 0, 1), Math_PI / 2.0); |
| 175 | paste_indicator.orientation = node->get_orthogonal_index_from_basis(r); |
| 176 | _update_paste_indicator(); |
| 177 | break; |
| 178 | } |
| 179 | |
| 180 | r = node->get_basis_with_orthogonal_index(cursor_rot); |
| 181 | r.rotate(Vector3(0, 0, 1), Math_PI / 2.0); |
| 182 | cursor_rot = node->get_orthogonal_index_from_basis(r); |
| 183 | _update_cursor_transform(); |
| 184 | } break; |
| 185 | case MENU_OPTION_CURSOR_CLEAR_ROTATION: { |
| 186 | if (input_action == INPUT_PASTE) { |
| 187 | paste_indicator.orientation = 0; |
| 188 | _update_paste_indicator(); |
| 189 | break; |
| 190 | } |
| 191 | |
| 192 | cursor_rot = 0; |
| 193 | _update_cursor_transform(); |
| 194 | } break; |
| 195 | |
| 196 | case MENU_OPTION_PASTE_SELECTS: { |
| 197 | int idx = options->get_popup()->get_item_index(MENU_OPTION_PASTE_SELECTS); |
| 198 | options->get_popup()->set_item_checked(idx, !options->get_popup()->is_item_checked(idx)); |
| 199 | } break; |
| 200 | |
| 201 | case MENU_OPTION_SELECTION_DUPLICATE: |
| 202 | case MENU_OPTION_SELECTION_CUT: { |
| 203 | if (!(selection.active && input_action == INPUT_NONE)) { |
| 204 | break; |
| 205 | } |
| 206 | |
| 207 | _set_clipboard_data(); |
| 208 | |
| 209 | if (p_option == MENU_OPTION_SELECTION_CUT) { |
| 210 | _delete_selection(); |
| 211 | } |
| 212 | |
| 213 | input_action = INPUT_PASTE; |
| 214 | paste_indicator.click = selection.begin; |
| 215 | paste_indicator.current = selection.begin; |
| 216 | paste_indicator.begin = selection.begin; |
| 217 | paste_indicator.end = selection.end; |
| 218 | paste_indicator.orientation = 0; |
| 219 | _update_paste_indicator(); |
| 220 | } break; |
| 221 | case MENU_OPTION_SELECTION_CLEAR: { |
| 222 | if (!selection.active) { |
| 223 | break; |
| 224 | } |
| 225 | |
| 226 | _delete_selection(); |
| 227 | |
| 228 | } break; |
| 229 | case MENU_OPTION_SELECTION_FILL: { |
| 230 | if (!selection.active) { |
| 231 | return; |
| 232 | } |
| 233 | |
| 234 | _fill_selection(); |
| 235 | |
| 236 | } break; |
| 237 | case MENU_OPTION_GRIDMAP_SETTINGS: { |
| 238 | settings_dialog->popup_centered(settings_vbc->get_combined_minimum_size() + Size2(50, 50) * EDSCALE); |
| 239 | } break; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | void GridMapEditor::_update_cursor_transform() { |
| 244 | cursor_transform = Transform3D(); |
| 245 | cursor_transform.origin = cursor_origin; |
| 246 | cursor_transform.basis = node->get_basis_with_orthogonal_index(cursor_rot); |
| 247 | cursor_transform.basis *= node->get_cell_scale(); |
| 248 | cursor_transform = node->get_global_transform() * cursor_transform; |
| 249 | |
| 250 | if (selected_palette >= 0) { |
| 251 | if (node && !node->get_mesh_library().is_null()) { |
| 252 | cursor_transform *= node->get_mesh_library()->get_item_mesh_transform(selected_palette); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | if (cursor_instance.is_valid()) { |
| 257 | RenderingServer::get_singleton()->instance_set_transform(cursor_instance, cursor_transform); |
| 258 | RenderingServer::get_singleton()->instance_set_visible(cursor_instance, cursor_visible); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | void GridMapEditor::_update_selection_transform() { |
| 263 | Transform3D xf_zero; |
| 264 | xf_zero.basis.set_zero(); |
| 265 | |
| 266 | if (!selection.active) { |
| 267 | RenderingServer::get_singleton()->instance_set_transform(selection_instance, xf_zero); |
| 268 | for (int i = 0; i < 3; i++) { |
| 269 | RenderingServer::get_singleton()->instance_set_transform(selection_level_instance[i], xf_zero); |
| 270 | } |
| 271 | return; |
| 272 | } |
| 273 | |
| 274 | Transform3D xf; |
| 275 | xf.scale((Vector3(1, 1, 1) + (selection.end - selection.begin)) * node->get_cell_size()); |
| 276 | xf.origin = selection.begin * node->get_cell_size(); |
| 277 | |
| 278 | RenderingServer::get_singleton()->instance_set_transform(selection_instance, node->get_global_transform() * xf); |
| 279 | |
| 280 | for (int i = 0; i < 3; i++) { |
| 281 | if (i != edit_axis || (edit_floor[edit_axis] < selection.begin[edit_axis]) || (edit_floor[edit_axis] > selection.end[edit_axis] + 1)) { |
| 282 | RenderingServer::get_singleton()->instance_set_transform(selection_level_instance[i], xf_zero); |
| 283 | } else { |
| 284 | Vector3 scale = (selection.end - selection.begin + Vector3(1, 1, 1)); |
| 285 | scale[edit_axis] = 1.0; |
| 286 | Vector3 position = selection.begin; |
| 287 | position[edit_axis] = edit_floor[edit_axis]; |
| 288 | |
| 289 | scale *= node->get_cell_size(); |
| 290 | position *= node->get_cell_size(); |
| 291 | |
| 292 | Transform3D xf2; |
| 293 | xf2.basis.scale(scale); |
| 294 | xf2.origin = position; |
| 295 | |
| 296 | RenderingServer::get_singleton()->instance_set_transform(selection_level_instance[i], xf2); |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | void GridMapEditor::_validate_selection() { |
| 302 | if (!selection.active) { |
| 303 | return; |
| 304 | } |
| 305 | selection.begin = selection.click; |
| 306 | selection.end = selection.current; |
| 307 | |
| 308 | if (selection.begin.x > selection.end.x) { |
| 309 | SWAP(selection.begin.x, selection.end.x); |
| 310 | } |
| 311 | if (selection.begin.y > selection.end.y) { |
| 312 | SWAP(selection.begin.y, selection.end.y); |
| 313 | } |
| 314 | if (selection.begin.z > selection.end.z) { |
| 315 | SWAP(selection.begin.z, selection.end.z); |
| 316 | } |
| 317 | |
| 318 | _update_selection_transform(); |
| 319 | } |
| 320 | |
| 321 | void GridMapEditor::_set_selection(bool p_active, const Vector3 &p_begin, const Vector3 &p_end) { |
| 322 | selection.active = p_active; |
| 323 | selection.begin = p_begin; |
| 324 | selection.end = p_end; |
| 325 | selection.click = p_begin; |
| 326 | selection.current = p_end; |
| 327 | |
| 328 | if (is_visible_in_tree()) { |
| 329 | _update_selection_transform(); |
| 330 | } |
| 331 | |
| 332 | options->get_popup()->set_item_disabled(options->get_popup()->get_item_index(MENU_OPTION_SELECTION_CLEAR), !selection.active); |
| 333 | options->get_popup()->set_item_disabled(options->get_popup()->get_item_index(MENU_OPTION_SELECTION_CUT), !selection.active); |
| 334 | options->get_popup()->set_item_disabled(options->get_popup()->get_item_index(MENU_OPTION_SELECTION_DUPLICATE), !selection.active); |
| 335 | options->get_popup()->set_item_disabled(options->get_popup()->get_item_index(MENU_OPTION_SELECTION_FILL), !selection.active); |
| 336 | } |
| 337 | |
| 338 | bool GridMapEditor::do_input_action(Camera3D *p_camera, const Point2 &p_point, bool p_click) { |
| 339 | if (!spatial_editor) { |
| 340 | return false; |
| 341 | } |
| 342 | |
| 343 | if (selected_palette < 0 && input_action != INPUT_PICK && input_action != INPUT_SELECT && input_action != INPUT_PASTE) { |
| 344 | return false; |
| 345 | } |
| 346 | if (mesh_library.is_null()) { |
| 347 | return false; |
| 348 | } |
| 349 | if (input_action != INPUT_PICK && input_action != INPUT_SELECT && input_action != INPUT_PASTE && !mesh_library->has_item(selected_palette)) { |
| 350 | return false; |
| 351 | } |
| 352 | |
| 353 | Camera3D *camera = p_camera; |
| 354 | Vector3 from = camera->project_ray_origin(p_point); |
| 355 | Vector3 normal = camera->project_ray_normal(p_point); |
| 356 | Transform3D local_xform = node->get_global_transform().affine_inverse(); |
| 357 | Vector<Plane> planes = camera->get_frustum(); |
| 358 | from = local_xform.xform(from); |
| 359 | normal = local_xform.basis.xform(normal).normalized(); |
| 360 | |
| 361 | Plane p; |
| 362 | p.normal[edit_axis] = 1.0; |
| 363 | p.d = edit_floor[edit_axis] * node->get_cell_size()[edit_axis]; |
| 364 | |
| 365 | Vector3 inters; |
| 366 | if (!p.intersects_segment(from, from + normal * settings_pick_distance->get_value(), &inters)) { |
| 367 | return false; |
| 368 | } |
| 369 | |
| 370 | // Make sure the intersection is inside the frustum planes, to avoid |
| 371 | // Painting on invisible regions. |
| 372 | for (int i = 0; i < planes.size(); i++) { |
| 373 | Plane fp = local_xform.xform(planes[i]); |
| 374 | if (fp.is_point_over(inters)) { |
| 375 | return false; |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | int cell[3]; |
| 380 | Vector3 cell_size = node->get_cell_size(); |
| 381 | |
| 382 | for (int i = 0; i < 3; i++) { |
| 383 | if (i == edit_axis) { |
| 384 | cell[i] = edit_floor[i]; |
| 385 | } else { |
| 386 | cell[i] = inters[i] / cell_size[i]; |
| 387 | if (inters[i] < 0) { |
| 388 | cell[i] -= 1; // Compensate negative. |
| 389 | } |
| 390 | grid_ofs[i] = cell[i] * cell_size[i]; |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | RS::get_singleton()->instance_set_transform(grid_instance[edit_axis], node->get_global_transform() * edit_grid_xform); |
| 395 | |
| 396 | if (cursor_instance.is_valid()) { |
| 397 | cursor_origin = (Vector3(cell[0], cell[1], cell[2]) + Vector3(0.5 * node->get_center_x(), 0.5 * node->get_center_y(), 0.5 * node->get_center_z())) * node->get_cell_size(); |
| 398 | cursor_visible = true; |
| 399 | |
| 400 | if (input_action == INPUT_SELECT || input_action == INPUT_PASTE) { |
| 401 | cursor_visible = false; |
| 402 | } |
| 403 | |
| 404 | _update_cursor_transform(); |
| 405 | } |
| 406 | |
| 407 | if (input_action == INPUT_PASTE) { |
| 408 | paste_indicator.current = Vector3i(cell[0], cell[1], cell[2]); |
| 409 | _update_paste_indicator(); |
| 410 | |
| 411 | } else if (input_action == INPUT_SELECT) { |
| 412 | selection.current = Vector3i(cell[0], cell[1], cell[2]); |
| 413 | if (p_click) { |
| 414 | selection.click = selection.current; |
| 415 | } |
| 416 | selection.active = true; |
| 417 | _validate_selection(); |
| 418 | |
| 419 | return true; |
| 420 | } else if (input_action == INPUT_PICK) { |
| 421 | int item = node->get_cell_item(Vector3i(cell[0], cell[1], cell[2])); |
| 422 | if (item >= 0) { |
| 423 | selected_palette = item; |
| 424 | |
| 425 | // Clear the filter if picked an item that's filtered out. |
| 426 | int index = mesh_library_palette->find_metadata(item); |
| 427 | if (index == -1) { |
| 428 | search_box->clear(); |
| 429 | } |
| 430 | |
| 431 | // This will select `selected_palette` in the ItemList when possible. |
| 432 | update_palette(); |
| 433 | |
| 434 | _update_cursor_instance(); |
| 435 | } |
| 436 | return true; |
| 437 | } |
| 438 | |
| 439 | if (input_action == INPUT_PAINT) { |
| 440 | SetItem si; |
| 441 | si.position = Vector3i(cell[0], cell[1], cell[2]); |
| 442 | si.new_value = selected_palette; |
| 443 | si.new_orientation = cursor_rot; |
| 444 | si.old_value = node->get_cell_item(Vector3i(cell[0], cell[1], cell[2])); |
| 445 | si.old_orientation = node->get_cell_item_orientation(Vector3i(cell[0], cell[1], cell[2])); |
| 446 | set_items.push_back(si); |
| 447 | node->set_cell_item(Vector3i(cell[0], cell[1], cell[2]), selected_palette, cursor_rot); |
| 448 | return true; |
| 449 | } else if (input_action == INPUT_ERASE) { |
| 450 | SetItem si; |
| 451 | si.position = Vector3i(cell[0], cell[1], cell[2]); |
| 452 | si.new_value = -1; |
| 453 | si.new_orientation = 0; |
| 454 | si.old_value = node->get_cell_item(Vector3i(cell[0], cell[1], cell[2])); |
| 455 | si.old_orientation = node->get_cell_item_orientation(Vector3i(cell[0], cell[1], cell[2])); |
| 456 | set_items.push_back(si); |
| 457 | node->set_cell_item(Vector3i(cell[0], cell[1], cell[2]), -1); |
| 458 | return true; |
| 459 | } |
| 460 | |
| 461 | return false; |
| 462 | } |
| 463 | |
| 464 | void GridMapEditor::_delete_selection() { |
| 465 | if (!selection.active) { |
| 466 | return; |
| 467 | } |
| 468 | |
| 469 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
| 470 | undo_redo->create_action(TTR("GridMap Delete Selection" )); |
| 471 | for (int i = selection.begin.x; i <= selection.end.x; i++) { |
| 472 | for (int j = selection.begin.y; j <= selection.end.y; j++) { |
| 473 | for (int k = selection.begin.z; k <= selection.end.z; k++) { |
| 474 | Vector3i selected = Vector3i(i, j, k); |
| 475 | undo_redo->add_do_method(node, "set_cell_item" , selected, GridMap::INVALID_CELL_ITEM); |
| 476 | undo_redo->add_undo_method(node, "set_cell_item" , selected, node->get_cell_item(selected), node->get_cell_item_orientation(selected)); |
| 477 | } |
| 478 | } |
| 479 | } |
| 480 | undo_redo->add_do_method(this, "_set_selection" , !selection.active, selection.begin, selection.end); |
| 481 | undo_redo->add_undo_method(this, "_set_selection" , selection.active, selection.begin, selection.end); |
| 482 | undo_redo->commit_action(); |
| 483 | } |
| 484 | |
| 485 | void GridMapEditor::_fill_selection() { |
| 486 | if (!selection.active) { |
| 487 | return; |
| 488 | } |
| 489 | |
| 490 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
| 491 | undo_redo->create_action(TTR("GridMap Fill Selection" )); |
| 492 | for (int i = selection.begin.x; i <= selection.end.x; i++) { |
| 493 | for (int j = selection.begin.y; j <= selection.end.y; j++) { |
| 494 | for (int k = selection.begin.z; k <= selection.end.z; k++) { |
| 495 | Vector3i selected = Vector3i(i, j, k); |
| 496 | undo_redo->add_do_method(node, "set_cell_item" , selected, selected_palette, cursor_rot); |
| 497 | undo_redo->add_undo_method(node, "set_cell_item" , selected, node->get_cell_item(selected), node->get_cell_item_orientation(selected)); |
| 498 | } |
| 499 | } |
| 500 | } |
| 501 | undo_redo->add_do_method(this, "_set_selection" , !selection.active, selection.begin, selection.end); |
| 502 | undo_redo->add_undo_method(this, "_set_selection" , selection.active, selection.begin, selection.end); |
| 503 | undo_redo->commit_action(); |
| 504 | } |
| 505 | |
| 506 | void GridMapEditor::_clear_clipboard_data() { |
| 507 | for (const ClipboardItem &E : clipboard_items) { |
| 508 | RenderingServer::get_singleton()->free(E.instance); |
| 509 | } |
| 510 | |
| 511 | clipboard_items.clear(); |
| 512 | } |
| 513 | |
| 514 | void GridMapEditor::_set_clipboard_data() { |
| 515 | _clear_clipboard_data(); |
| 516 | |
| 517 | Ref<MeshLibrary> meshLibrary = node->get_mesh_library(); |
| 518 | |
| 519 | for (int i = selection.begin.x; i <= selection.end.x; i++) { |
| 520 | for (int j = selection.begin.y; j <= selection.end.y; j++) { |
| 521 | for (int k = selection.begin.z; k <= selection.end.z; k++) { |
| 522 | Vector3i selected = Vector3i(i, j, k); |
| 523 | int itm = node->get_cell_item(selected); |
| 524 | if (itm == GridMap::INVALID_CELL_ITEM) { |
| 525 | continue; |
| 526 | } |
| 527 | |
| 528 | Ref<Mesh> mesh = meshLibrary->get_item_mesh(itm); |
| 529 | |
| 530 | ClipboardItem item; |
| 531 | item.cell_item = itm; |
| 532 | item.grid_offset = Vector3(selected) - selection.begin; |
| 533 | item.orientation = node->get_cell_item_orientation(selected); |
| 534 | item.instance = RenderingServer::get_singleton()->instance_create2(mesh->get_rid(), get_tree()->get_root()->get_world_3d()->get_scenario()); |
| 535 | |
| 536 | clipboard_items.push_back(item); |
| 537 | } |
| 538 | } |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | void GridMapEditor::_update_paste_indicator() { |
| 543 | if (input_action != INPUT_PASTE) { |
| 544 | Transform3D xf; |
| 545 | xf.basis.set_zero(); |
| 546 | RenderingServer::get_singleton()->instance_set_transform(paste_instance, xf); |
| 547 | return; |
| 548 | } |
| 549 | |
| 550 | Vector3 center = 0.5 * Vector3(real_t(node->get_center_x()), real_t(node->get_center_y()), real_t(node->get_center_z())); |
| 551 | Vector3 scale = (Vector3(1, 1, 1) + (paste_indicator.end - paste_indicator.begin)) * node->get_cell_size(); |
| 552 | Transform3D xf; |
| 553 | xf.scale(scale); |
| 554 | xf.origin = (paste_indicator.begin + (paste_indicator.current - paste_indicator.click) + center) * node->get_cell_size(); |
| 555 | Basis rot; |
| 556 | rot = node->get_basis_with_orthogonal_index(paste_indicator.orientation); |
| 557 | xf.basis = rot * xf.basis; |
| 558 | xf.translate_local((-center * node->get_cell_size()) / scale); |
| 559 | |
| 560 | RenderingServer::get_singleton()->instance_set_transform(paste_instance, node->get_global_transform() * xf); |
| 561 | |
| 562 | for (const ClipboardItem &item : clipboard_items) { |
| 563 | xf = Transform3D(); |
| 564 | xf.origin = (paste_indicator.begin + (paste_indicator.current - paste_indicator.click) + center) * node->get_cell_size(); |
| 565 | xf.basis = rot * xf.basis; |
| 566 | xf.translate_local(item.grid_offset * node->get_cell_size()); |
| 567 | |
| 568 | Basis item_rot; |
| 569 | item_rot = node->get_basis_with_orthogonal_index(item.orientation); |
| 570 | xf.basis = item_rot * xf.basis * node->get_cell_scale(); |
| 571 | |
| 572 | RenderingServer::get_singleton()->instance_set_transform(item.instance, node->get_global_transform() * xf); |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | void GridMapEditor::_do_paste() { |
| 577 | int idx = options->get_popup()->get_item_index(MENU_OPTION_PASTE_SELECTS); |
| 578 | bool reselect = options->get_popup()->is_item_checked(idx); |
| 579 | |
| 580 | Basis rot; |
| 581 | rot = node->get_basis_with_orthogonal_index(paste_indicator.orientation); |
| 582 | |
| 583 | Vector3 ofs = paste_indicator.current - paste_indicator.click; |
| 584 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
| 585 | undo_redo->create_action(TTR("GridMap Paste Selection" )); |
| 586 | |
| 587 | for (const ClipboardItem &item : clipboard_items) { |
| 588 | Vector3 position = rot.xform(item.grid_offset) + paste_indicator.begin + ofs; |
| 589 | |
| 590 | Basis orm; |
| 591 | orm = node->get_basis_with_orthogonal_index(item.orientation); |
| 592 | orm = rot * orm; |
| 593 | |
| 594 | undo_redo->add_do_method(node, "set_cell_item" , position, item.cell_item, node->get_orthogonal_index_from_basis(orm)); |
| 595 | undo_redo->add_undo_method(node, "set_cell_item" , position, node->get_cell_item(position), node->get_cell_item_orientation(position)); |
| 596 | } |
| 597 | |
| 598 | if (reselect) { |
| 599 | undo_redo->add_do_method(this, "_set_selection" , true, paste_indicator.begin + ofs, paste_indicator.end + ofs); |
| 600 | undo_redo->add_undo_method(this, "_set_selection" , selection.active, selection.begin, selection.end); |
| 601 | } |
| 602 | |
| 603 | undo_redo->commit_action(); |
| 604 | |
| 605 | _clear_clipboard_data(); |
| 606 | } |
| 607 | |
| 608 | EditorPlugin::AfterGUIInput GridMapEditor::forward_spatial_input_event(Camera3D *p_camera, const Ref<InputEvent> &p_event) { |
| 609 | if (!node) { |
| 610 | return EditorPlugin::AFTER_GUI_INPUT_PASS; |
| 611 | } |
| 612 | |
| 613 | Ref<InputEventMouseButton> mb = p_event; |
| 614 | |
| 615 | if (mb.is_valid()) { |
| 616 | if (mb->get_button_index() == MouseButton::WHEEL_UP && (mb->is_command_or_control_pressed())) { |
| 617 | if (mb->is_pressed()) { |
| 618 | floor->set_value(floor->get_value() + mb->get_factor()); |
| 619 | } |
| 620 | |
| 621 | return EditorPlugin::AFTER_GUI_INPUT_STOP; // Eaten. |
| 622 | } else if (mb->get_button_index() == MouseButton::WHEEL_DOWN && (mb->is_command_or_control_pressed())) { |
| 623 | if (mb->is_pressed()) { |
| 624 | floor->set_value(floor->get_value() - mb->get_factor()); |
| 625 | } |
| 626 | return EditorPlugin::AFTER_GUI_INPUT_STOP; |
| 627 | } |
| 628 | |
| 629 | if (mb->is_pressed()) { |
| 630 | Node3DEditorViewport::NavigationScheme nav_scheme = (Node3DEditorViewport::NavigationScheme)EDITOR_GET("editors/3d/navigation/navigation_scheme" ).operator int(); |
| 631 | if ((nav_scheme == Node3DEditorViewport::NAVIGATION_MAYA || nav_scheme == Node3DEditorViewport::NAVIGATION_MODO) && mb->is_alt_pressed()) { |
| 632 | input_action = INPUT_NONE; |
| 633 | } else if (mb->get_button_index() == MouseButton::LEFT) { |
| 634 | bool can_edit = (node && node->get_mesh_library().is_valid()); |
| 635 | if (input_action == INPUT_PASTE) { |
| 636 | _do_paste(); |
| 637 | input_action = INPUT_NONE; |
| 638 | _update_paste_indicator(); |
| 639 | } else if (mb->is_shift_pressed() && can_edit) { |
| 640 | input_action = INPUT_SELECT; |
| 641 | last_selection = selection; |
| 642 | } else if (mb->is_command_or_control_pressed() && can_edit) { |
| 643 | input_action = INPUT_PICK; |
| 644 | } else { |
| 645 | input_action = INPUT_PAINT; |
| 646 | set_items.clear(); |
| 647 | } |
| 648 | } else if (mb->get_button_index() == MouseButton::RIGHT) { |
| 649 | if (input_action == INPUT_PASTE) { |
| 650 | _clear_clipboard_data(); |
| 651 | input_action = INPUT_NONE; |
| 652 | _update_paste_indicator(); |
| 653 | return EditorPlugin::AFTER_GUI_INPUT_STOP; |
| 654 | } else if (selection.active) { |
| 655 | _set_selection(false); |
| 656 | return EditorPlugin::AFTER_GUI_INPUT_STOP; |
| 657 | } else { |
| 658 | input_action = INPUT_ERASE; |
| 659 | set_items.clear(); |
| 660 | } |
| 661 | } else { |
| 662 | return EditorPlugin::AFTER_GUI_INPUT_PASS; |
| 663 | } |
| 664 | |
| 665 | if (do_input_action(p_camera, Point2(mb->get_position().x, mb->get_position().y), true)) { |
| 666 | return EditorPlugin::AFTER_GUI_INPUT_STOP; |
| 667 | } |
| 668 | return EditorPlugin::AFTER_GUI_INPUT_PASS; |
| 669 | } else { |
| 670 | if ((mb->get_button_index() == MouseButton::RIGHT && input_action == INPUT_ERASE) || (mb->get_button_index() == MouseButton::LEFT && input_action == INPUT_PAINT)) { |
| 671 | if (set_items.size()) { |
| 672 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
| 673 | undo_redo->create_action(TTR("GridMap Paint" )); |
| 674 | for (const SetItem &si : set_items) { |
| 675 | undo_redo->add_do_method(node, "set_cell_item" , si.position, si.new_value, si.new_orientation); |
| 676 | } |
| 677 | for (List<SetItem>::Element *E = set_items.back(); E; E = E->prev()) { |
| 678 | const SetItem &si = E->get(); |
| 679 | undo_redo->add_undo_method(node, "set_cell_item" , si.position, si.old_value, si.old_orientation); |
| 680 | } |
| 681 | |
| 682 | undo_redo->commit_action(); |
| 683 | } |
| 684 | set_items.clear(); |
| 685 | input_action = INPUT_NONE; |
| 686 | |
| 687 | if (set_items.size() > 0) { |
| 688 | return EditorPlugin::AFTER_GUI_INPUT_STOP; |
| 689 | } |
| 690 | return EditorPlugin::AFTER_GUI_INPUT_PASS; |
| 691 | } |
| 692 | |
| 693 | if (mb->get_button_index() == MouseButton::LEFT && input_action == INPUT_SELECT) { |
| 694 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
| 695 | undo_redo->create_action(TTR("GridMap Selection" )); |
| 696 | undo_redo->add_do_method(this, "_set_selection" , selection.active, selection.begin, selection.end); |
| 697 | undo_redo->add_undo_method(this, "_set_selection" , last_selection.active, last_selection.begin, last_selection.end); |
| 698 | undo_redo->commit_action(); |
| 699 | } |
| 700 | |
| 701 | if (mb->get_button_index() == MouseButton::LEFT && input_action != INPUT_NONE) { |
| 702 | set_items.clear(); |
| 703 | input_action = INPUT_NONE; |
| 704 | return EditorPlugin::AFTER_GUI_INPUT_STOP; |
| 705 | } |
| 706 | if (mb->get_button_index() == MouseButton::RIGHT && (input_action == INPUT_ERASE || input_action == INPUT_PASTE)) { |
| 707 | input_action = INPUT_NONE; |
| 708 | return EditorPlugin::AFTER_GUI_INPUT_STOP; |
| 709 | } |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | Ref<InputEventMouseMotion> mm = p_event; |
| 714 | |
| 715 | if (mm.is_valid()) { |
| 716 | // Update the grid, to check if the grid needs to be moved to a tile cursor. |
| 717 | update_grid(); |
| 718 | |
| 719 | if (do_input_action(p_camera, mm->get_position(), false)) { |
| 720 | return EditorPlugin::AFTER_GUI_INPUT_STOP; |
| 721 | } |
| 722 | return EditorPlugin::AFTER_GUI_INPUT_PASS; |
| 723 | } |
| 724 | |
| 725 | Ref<InputEventKey> k = p_event; |
| 726 | |
| 727 | if (k.is_valid()) { |
| 728 | if (k->is_pressed()) { |
| 729 | if (k->get_keycode() == Key::ESCAPE) { |
| 730 | if (input_action == INPUT_PASTE) { |
| 731 | _clear_clipboard_data(); |
| 732 | input_action = INPUT_NONE; |
| 733 | _update_paste_indicator(); |
| 734 | return EditorPlugin::AFTER_GUI_INPUT_STOP; |
| 735 | } else if (selection.active) { |
| 736 | _set_selection(false); |
| 737 | return EditorPlugin::AFTER_GUI_INPUT_STOP; |
| 738 | } else { |
| 739 | selected_palette = -1; |
| 740 | mesh_library_palette->deselect_all(); |
| 741 | update_palette(); |
| 742 | _update_cursor_instance(); |
| 743 | return EditorPlugin::AFTER_GUI_INPUT_STOP; |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | // Consume input to avoid conflicts with other plugins. |
| 748 | if (k.is_valid() && k->is_pressed() && !k->is_echo()) { |
| 749 | for (int i = 0; i < options->get_popup()->get_item_count(); ++i) { |
| 750 | const Ref<Shortcut> &shortcut = options->get_popup()->get_item_shortcut(i); |
| 751 | if (shortcut.is_valid() && shortcut->matches_event(p_event)) { |
| 752 | accept_event(); |
| 753 | _menu_option(options->get_popup()->get_item_id(i)); |
| 754 | return EditorPlugin::AFTER_GUI_INPUT_STOP; |
| 755 | } |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | if (k->is_shift_pressed() && selection.active && input_action != INPUT_PASTE) { |
| 760 | if (k->get_keycode() == (Key)options->get_popup()->get_item_accelerator(options->get_popup()->get_item_index(MENU_OPTION_PREV_LEVEL))) { |
| 761 | selection.click[edit_axis]--; |
| 762 | _validate_selection(); |
| 763 | return EditorPlugin::AFTER_GUI_INPUT_STOP; |
| 764 | } |
| 765 | if (k->get_keycode() == (Key)options->get_popup()->get_item_accelerator(options->get_popup()->get_item_index(MENU_OPTION_NEXT_LEVEL))) { |
| 766 | selection.click[edit_axis]++; |
| 767 | _validate_selection(); |
| 768 | return EditorPlugin::AFTER_GUI_INPUT_STOP; |
| 769 | } |
| 770 | } |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | Ref<InputEventPanGesture> pan_gesture = p_event; |
| 775 | if (pan_gesture.is_valid()) { |
| 776 | if (pan_gesture->is_alt_pressed() && pan_gesture->is_command_or_control_pressed()) { |
| 777 | const real_t delta = pan_gesture->get_delta().y * 0.5; |
| 778 | accumulated_floor_delta += delta; |
| 779 | int step = 0; |
| 780 | if (ABS(accumulated_floor_delta) > 1.0) { |
| 781 | step = SIGN(accumulated_floor_delta); |
| 782 | accumulated_floor_delta -= step; |
| 783 | } |
| 784 | if (step) { |
| 785 | floor->set_value(floor->get_value() + step); |
| 786 | } |
| 787 | return EditorPlugin::AFTER_GUI_INPUT_STOP; |
| 788 | } |
| 789 | } |
| 790 | accumulated_floor_delta = 0.0; |
| 791 | |
| 792 | return EditorPlugin::AFTER_GUI_INPUT_PASS; |
| 793 | } |
| 794 | |
| 795 | struct _CGMEItemSort { |
| 796 | String name; |
| 797 | int id = 0; |
| 798 | _FORCE_INLINE_ bool operator<(const _CGMEItemSort &r_it) const { return name < r_it.name; } |
| 799 | }; |
| 800 | |
| 801 | void GridMapEditor::_set_display_mode(int p_mode) { |
| 802 | if (display_mode == p_mode) { |
| 803 | return; |
| 804 | } |
| 805 | |
| 806 | if (p_mode == DISPLAY_LIST) { |
| 807 | mode_list->set_pressed(true); |
| 808 | mode_thumbnail->set_pressed(false); |
| 809 | } else if (p_mode == DISPLAY_THUMBNAIL) { |
| 810 | mode_list->set_pressed(false); |
| 811 | mode_thumbnail->set_pressed(true); |
| 812 | } |
| 813 | |
| 814 | display_mode = p_mode; |
| 815 | |
| 816 | update_palette(); |
| 817 | } |
| 818 | |
| 819 | void GridMapEditor::_text_changed(const String &p_text) { |
| 820 | update_palette(); |
| 821 | } |
| 822 | |
| 823 | void GridMapEditor::_sbox_input(const Ref<InputEvent> &p_ie) { |
| 824 | const Ref<InputEventKey> k = p_ie; |
| 825 | |
| 826 | if (k.is_valid() && (k->get_keycode() == Key::UP || k->get_keycode() == Key::DOWN || k->get_keycode() == Key::PAGEUP || k->get_keycode() == Key::PAGEDOWN)) { |
| 827 | // Forward the key input to the ItemList so it can be scrolled |
| 828 | mesh_library_palette->gui_input(k); |
| 829 | search_box->accept_event(); |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | void GridMapEditor::_mesh_library_palette_input(const Ref<InputEvent> &p_ie) { |
| 834 | const Ref<InputEventMouseButton> mb = p_ie; |
| 835 | |
| 836 | // Zoom in/out using Ctrl + mouse wheel |
| 837 | if (mb.is_valid() && mb->is_pressed() && mb->is_command_or_control_pressed()) { |
| 838 | if (mb->is_pressed() && mb->get_button_index() == MouseButton::WHEEL_UP) { |
| 839 | size_slider->set_value(size_slider->get_value() + 0.2); |
| 840 | } |
| 841 | |
| 842 | if (mb->is_pressed() && mb->get_button_index() == MouseButton::WHEEL_DOWN) { |
| 843 | size_slider->set_value(size_slider->get_value() - 0.2); |
| 844 | } |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | void GridMapEditor::_icon_size_changed(float p_value) { |
| 849 | mesh_library_palette->set_icon_scale(p_value); |
| 850 | update_palette(); |
| 851 | } |
| 852 | |
| 853 | void GridMapEditor::update_palette() { |
| 854 | float min_size = EDITOR_GET("editors/grid_map/preview_size" ); |
| 855 | min_size *= EDSCALE; |
| 856 | |
| 857 | mesh_library_palette->clear(); |
| 858 | if (display_mode == DISPLAY_THUMBNAIL) { |
| 859 | mesh_library_palette->set_max_columns(0); |
| 860 | mesh_library_palette->set_icon_mode(ItemList::ICON_MODE_TOP); |
| 861 | mesh_library_palette->set_fixed_column_width(min_size * MAX(size_slider->get_value(), 1.5)); |
| 862 | } else if (display_mode == DISPLAY_LIST) { |
| 863 | mesh_library_palette->set_max_columns(1); |
| 864 | mesh_library_palette->set_icon_mode(ItemList::ICON_MODE_LEFT); |
| 865 | mesh_library_palette->set_fixed_column_width(0); |
| 866 | } |
| 867 | |
| 868 | mesh_library_palette->set_fixed_icon_size(Size2(min_size, min_size)); |
| 869 | mesh_library_palette->set_max_text_lines(2); |
| 870 | |
| 871 | if (mesh_library.is_null()) { |
| 872 | search_box->set_text("" ); |
| 873 | search_box->set_editable(false); |
| 874 | info_message->show(); |
| 875 | return; |
| 876 | } |
| 877 | |
| 878 | search_box->set_editable(true); |
| 879 | info_message->hide(); |
| 880 | |
| 881 | Vector<int> ids; |
| 882 | ids = mesh_library->get_item_list(); |
| 883 | |
| 884 | List<_CGMEItemSort> il; |
| 885 | for (int i = 0; i < ids.size(); i++) { |
| 886 | _CGMEItemSort is; |
| 887 | is.id = ids[i]; |
| 888 | is.name = mesh_library->get_item_name(ids[i]); |
| 889 | il.push_back(is); |
| 890 | } |
| 891 | il.sort(); |
| 892 | |
| 893 | String filter = search_box->get_text().strip_edges(); |
| 894 | |
| 895 | int item = 0; |
| 896 | |
| 897 | for (_CGMEItemSort &E : il) { |
| 898 | int id = E.id; |
| 899 | String name = mesh_library->get_item_name(id); |
| 900 | Ref<Texture2D> preview = mesh_library->get_item_preview(id); |
| 901 | |
| 902 | if (name.is_empty()) { |
| 903 | name = "#" + itos(id); |
| 904 | } |
| 905 | |
| 906 | if (!filter.is_empty() && !filter.is_subsequence_ofn(name)) { |
| 907 | continue; |
| 908 | } |
| 909 | |
| 910 | mesh_library_palette->add_item("" ); |
| 911 | if (!preview.is_null()) { |
| 912 | mesh_library_palette->set_item_icon(item, preview); |
| 913 | mesh_library_palette->set_item_tooltip(item, name); |
| 914 | } |
| 915 | mesh_library_palette->set_item_text(item, name); |
| 916 | mesh_library_palette->set_item_metadata(item, id); |
| 917 | |
| 918 | if (selected_palette == id) { |
| 919 | mesh_library_palette->select(item); |
| 920 | } |
| 921 | |
| 922 | item++; |
| 923 | } |
| 924 | } |
| 925 | |
| 926 | void GridMapEditor::_update_mesh_library() { |
| 927 | ERR_FAIL_NULL(node); |
| 928 | |
| 929 | Ref<MeshLibrary> new_mesh_library = node->get_mesh_library(); |
| 930 | if (new_mesh_library != mesh_library) { |
| 931 | if (mesh_library.is_valid()) { |
| 932 | mesh_library->disconnect_changed(callable_mp(this, &GridMapEditor::update_palette)); |
| 933 | } |
| 934 | mesh_library = new_mesh_library; |
| 935 | } else { |
| 936 | return; |
| 937 | } |
| 938 | |
| 939 | if (mesh_library.is_valid()) { |
| 940 | mesh_library->connect_changed(callable_mp(this, &GridMapEditor::update_palette)); |
| 941 | } |
| 942 | |
| 943 | update_palette(); |
| 944 | // Update the cursor and grid in case the library is changed or removed. |
| 945 | _update_cursor_instance(); |
| 946 | update_grid(); |
| 947 | } |
| 948 | |
| 949 | void GridMapEditor::edit(GridMap *p_gridmap) { |
| 950 | if (node) { |
| 951 | node->disconnect(SNAME("cell_size_changed" ), callable_mp(this, &GridMapEditor::_draw_grids)); |
| 952 | node->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(this, &GridMapEditor::_update_mesh_library)); |
| 953 | if (mesh_library.is_valid()) { |
| 954 | mesh_library->disconnect_changed(callable_mp(this, &GridMapEditor::update_palette)); |
| 955 | mesh_library = Ref<MeshLibrary>(); |
| 956 | } |
| 957 | } |
| 958 | |
| 959 | node = p_gridmap; |
| 960 | |
| 961 | input_action = INPUT_NONE; |
| 962 | selection.active = false; |
| 963 | _update_selection_transform(); |
| 964 | _update_paste_indicator(); |
| 965 | |
| 966 | spatial_editor = Object::cast_to<Node3DEditorPlugin>(EditorNode::get_singleton()->get_editor_plugin_screen()); |
| 967 | |
| 968 | if (!node) { |
| 969 | set_process(false); |
| 970 | for (int i = 0; i < 3; i++) { |
| 971 | RenderingServer::get_singleton()->instance_set_visible(grid_instance[i], false); |
| 972 | } |
| 973 | |
| 974 | if (cursor_instance.is_valid()) { |
| 975 | RenderingServer::get_singleton()->instance_set_visible(cursor_instance, false); |
| 976 | } |
| 977 | |
| 978 | return; |
| 979 | } |
| 980 | |
| 981 | update_palette(); |
| 982 | _update_cursor_instance(); |
| 983 | |
| 984 | set_process(true); |
| 985 | |
| 986 | _draw_grids(node->get_cell_size()); |
| 987 | update_grid(); |
| 988 | |
| 989 | node->connect(SNAME("cell_size_changed" ), callable_mp(this, &GridMapEditor::_draw_grids)); |
| 990 | node->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &GridMapEditor::_update_mesh_library)); |
| 991 | _update_mesh_library(); |
| 992 | } |
| 993 | |
| 994 | void GridMapEditor::update_grid() { |
| 995 | grid_xform.origin.x -= 1; // Force update in hackish way. |
| 996 | |
| 997 | grid_ofs[edit_axis] = edit_floor[edit_axis] * node->get_cell_size()[edit_axis]; |
| 998 | |
| 999 | // If there's a valid tile cursor, offset the grid, otherwise move it back to the node. |
| 1000 | edit_grid_xform.origin = cursor_instance.is_valid() ? grid_ofs : Vector3(); |
| 1001 | edit_grid_xform.basis = Basis(); |
| 1002 | |
| 1003 | for (int i = 0; i < 3; i++) { |
| 1004 | RenderingServer::get_singleton()->instance_set_visible(grid_instance[i], i == edit_axis); |
| 1005 | } |
| 1006 | |
| 1007 | updating = true; |
| 1008 | floor->set_value(edit_floor[edit_axis]); |
| 1009 | updating = false; |
| 1010 | } |
| 1011 | |
| 1012 | void GridMapEditor::_draw_grids(const Vector3 &cell_size) { |
| 1013 | Vector3 edited_floor = node->get_meta("_editor_floor_" , Vector3()); |
| 1014 | |
| 1015 | for (int i = 0; i < 3; i++) { |
| 1016 | RS::get_singleton()->mesh_clear(grid[i]); |
| 1017 | edit_floor[i] = edited_floor[i]; |
| 1018 | } |
| 1019 | |
| 1020 | Vector<Vector3> grid_points[3]; |
| 1021 | Vector<Color> grid_colors[3]; |
| 1022 | |
| 1023 | for (int i = 0; i < 3; i++) { |
| 1024 | Vector3 axis; |
| 1025 | axis[i] = 1; |
| 1026 | Vector3 axis_n1; |
| 1027 | axis_n1[(i + 1) % 3] = cell_size[(i + 1) % 3]; |
| 1028 | Vector3 axis_n2; |
| 1029 | axis_n2[(i + 2) % 3] = cell_size[(i + 2) % 3]; |
| 1030 | |
| 1031 | for (int j = -GRID_CURSOR_SIZE; j <= GRID_CURSOR_SIZE; j++) { |
| 1032 | for (int k = -GRID_CURSOR_SIZE; k <= GRID_CURSOR_SIZE; k++) { |
| 1033 | Vector3 p = axis_n1 * j + axis_n2 * k; |
| 1034 | float trans = Math::pow(MAX(0, 1.0 - (Vector2(j, k).length() / GRID_CURSOR_SIZE)), 2); |
| 1035 | |
| 1036 | Vector3 pj = axis_n1 * (j + 1) + axis_n2 * k; |
| 1037 | float transj = Math::pow(MAX(0, 1.0 - (Vector2(j + 1, k).length() / GRID_CURSOR_SIZE)), 2); |
| 1038 | |
| 1039 | Vector3 pk = axis_n1 * j + axis_n2 * (k + 1); |
| 1040 | float transk = Math::pow(MAX(0, 1.0 - (Vector2(j, k + 1).length() / GRID_CURSOR_SIZE)), 2); |
| 1041 | |
| 1042 | grid_points[i].push_back(p); |
| 1043 | grid_points[i].push_back(pk); |
| 1044 | grid_colors[i].push_back(Color(1, 1, 1, trans)); |
| 1045 | grid_colors[i].push_back(Color(1, 1, 1, transk)); |
| 1046 | |
| 1047 | grid_points[i].push_back(p); |
| 1048 | grid_points[i].push_back(pj); |
| 1049 | grid_colors[i].push_back(Color(1, 1, 1, trans)); |
| 1050 | grid_colors[i].push_back(Color(1, 1, 1, transj)); |
| 1051 | } |
| 1052 | } |
| 1053 | |
| 1054 | Array d; |
| 1055 | d.resize(RS::ARRAY_MAX); |
| 1056 | d[RS::ARRAY_VERTEX] = grid_points[i]; |
| 1057 | d[RS::ARRAY_COLOR] = grid_colors[i]; |
| 1058 | RenderingServer::get_singleton()->mesh_add_surface_from_arrays(grid[i], RenderingServer::PRIMITIVE_LINES, d); |
| 1059 | RenderingServer::get_singleton()->mesh_surface_set_material(grid[i], 0, indicator_mat->get_rid()); |
| 1060 | } |
| 1061 | } |
| 1062 | |
| 1063 | void GridMapEditor::_update_theme() { |
| 1064 | options->set_icon(get_theme_icon(SNAME("GridMap" ), EditorStringName(EditorIcons))); |
| 1065 | search_box->set_right_icon(get_theme_icon(SNAME("Search" ), EditorStringName(EditorIcons))); |
| 1066 | mode_thumbnail->set_icon(get_theme_icon(SNAME("FileThumbnail" ), EditorStringName(EditorIcons))); |
| 1067 | mode_list->set_icon(get_theme_icon(SNAME("FileList" ), EditorStringName(EditorIcons))); |
| 1068 | } |
| 1069 | |
| 1070 | void GridMapEditor::_notification(int p_what) { |
| 1071 | switch (p_what) { |
| 1072 | case NOTIFICATION_ENTER_TREE: { |
| 1073 | mesh_library_palette->connect("item_selected" , callable_mp(this, &GridMapEditor::_item_selected_cbk)); |
| 1074 | for (int i = 0; i < 3; i++) { |
| 1075 | grid[i] = RS::get_singleton()->mesh_create(); |
| 1076 | grid_instance[i] = RS::get_singleton()->instance_create2(grid[i], get_tree()->get_root()->get_world_3d()->get_scenario()); |
| 1077 | RenderingServer::get_singleton()->instance_set_layer_mask(grid_instance[i], 1 << Node3DEditorViewport::MISC_TOOL_LAYER); |
| 1078 | selection_level_instance[i] = RenderingServer::get_singleton()->instance_create2(selection_level_mesh[i], get_tree()->get_root()->get_world_3d()->get_scenario()); |
| 1079 | RenderingServer::get_singleton()->instance_set_layer_mask(selection_level_instance[i], 1 << Node3DEditorViewport::MISC_TOOL_LAYER); |
| 1080 | } |
| 1081 | |
| 1082 | selection_instance = RenderingServer::get_singleton()->instance_create2(selection_mesh, get_tree()->get_root()->get_world_3d()->get_scenario()); |
| 1083 | RenderingServer::get_singleton()->instance_set_layer_mask(selection_instance, 1 << Node3DEditorViewport::MISC_TOOL_LAYER); |
| 1084 | paste_instance = RenderingServer::get_singleton()->instance_create2(paste_mesh, get_tree()->get_root()->get_world_3d()->get_scenario()); |
| 1085 | RenderingServer::get_singleton()->instance_set_layer_mask(paste_instance, 1 << Node3DEditorViewport::MISC_TOOL_LAYER); |
| 1086 | |
| 1087 | _update_selection_transform(); |
| 1088 | _update_paste_indicator(); |
| 1089 | _update_theme(); |
| 1090 | } break; |
| 1091 | |
| 1092 | case NOTIFICATION_EXIT_TREE: { |
| 1093 | _clear_clipboard_data(); |
| 1094 | |
| 1095 | for (int i = 0; i < 3; i++) { |
| 1096 | RS::get_singleton()->free(grid_instance[i]); |
| 1097 | RS::get_singleton()->free(grid[i]); |
| 1098 | grid_instance[i] = RID(); |
| 1099 | grid[i] = RID(); |
| 1100 | RenderingServer::get_singleton()->free(selection_level_instance[i]); |
| 1101 | } |
| 1102 | |
| 1103 | RenderingServer::get_singleton()->free(selection_instance); |
| 1104 | RenderingServer::get_singleton()->free(paste_instance); |
| 1105 | selection_instance = RID(); |
| 1106 | paste_instance = RID(); |
| 1107 | } break; |
| 1108 | |
| 1109 | case NOTIFICATION_PROCESS: { |
| 1110 | if (!node) { |
| 1111 | return; |
| 1112 | } |
| 1113 | |
| 1114 | Transform3D xf = node->get_global_transform(); |
| 1115 | |
| 1116 | if (xf != grid_xform) { |
| 1117 | for (int i = 0; i < 3; i++) { |
| 1118 | RS::get_singleton()->instance_set_transform(grid_instance[i], xf * edit_grid_xform); |
| 1119 | } |
| 1120 | grid_xform = xf; |
| 1121 | } |
| 1122 | } break; |
| 1123 | |
| 1124 | case NOTIFICATION_THEME_CHANGED: { |
| 1125 | _update_theme(); |
| 1126 | } break; |
| 1127 | |
| 1128 | case NOTIFICATION_APPLICATION_FOCUS_OUT: { |
| 1129 | if (input_action == INPUT_PAINT) { |
| 1130 | // Simulate mouse released event to stop drawing when editor focus exists. |
| 1131 | Ref<InputEventMouseButton> release; |
| 1132 | release.instantiate(); |
| 1133 | release->set_button_index(MouseButton::LEFT); |
| 1134 | forward_spatial_input_event(nullptr, release); |
| 1135 | } |
| 1136 | } break; |
| 1137 | } |
| 1138 | } |
| 1139 | |
| 1140 | void GridMapEditor::_update_cursor_instance() { |
| 1141 | if (!node) { |
| 1142 | return; |
| 1143 | } |
| 1144 | |
| 1145 | if (cursor_instance.is_valid()) { |
| 1146 | RenderingServer::get_singleton()->free(cursor_instance); |
| 1147 | } |
| 1148 | cursor_instance = RID(); |
| 1149 | |
| 1150 | if (selected_palette >= 0) { |
| 1151 | if (node && !node->get_mesh_library().is_null()) { |
| 1152 | Ref<Mesh> mesh = node->get_mesh_library()->get_item_mesh(selected_palette); |
| 1153 | if (!mesh.is_null() && mesh->get_rid().is_valid()) { |
| 1154 | cursor_instance = RenderingServer::get_singleton()->instance_create2(mesh->get_rid(), get_tree()->get_root()->get_world_3d()->get_scenario()); |
| 1155 | RenderingServer::get_singleton()->instance_set_transform(cursor_instance, cursor_transform); |
| 1156 | } |
| 1157 | } |
| 1158 | } |
| 1159 | } |
| 1160 | |
| 1161 | void GridMapEditor::_item_selected_cbk(int idx) { |
| 1162 | selected_palette = mesh_library_palette->get_item_metadata(idx); |
| 1163 | |
| 1164 | _update_cursor_instance(); |
| 1165 | } |
| 1166 | |
| 1167 | void GridMapEditor::_floor_changed(float p_value) { |
| 1168 | if (updating) { |
| 1169 | return; |
| 1170 | } |
| 1171 | |
| 1172 | edit_floor[edit_axis] = p_value; |
| 1173 | node->set_meta("_editor_floor_" , Vector3(edit_floor[0], edit_floor[1], edit_floor[2])); |
| 1174 | update_grid(); |
| 1175 | _update_selection_transform(); |
| 1176 | } |
| 1177 | |
| 1178 | void GridMapEditor::_floor_mouse_exited() { |
| 1179 | floor->get_line_edit()->release_focus(); |
| 1180 | } |
| 1181 | |
| 1182 | void GridMapEditor::_bind_methods() { |
| 1183 | ClassDB::bind_method("_configure" , &GridMapEditor::_configure); |
| 1184 | ClassDB::bind_method("_set_selection" , &GridMapEditor::_set_selection); |
| 1185 | } |
| 1186 | |
| 1187 | GridMapEditor::GridMapEditor() { |
| 1188 | ED_SHORTCUT("grid_map/previous_floor" , TTR("Previous Floor" ), Key::Q, true); |
| 1189 | ED_SHORTCUT("grid_map/next_floor" , TTR("Next Floor" ), Key::E, true); |
| 1190 | ED_SHORTCUT("grid_map/edit_x_axis" , TTR("Edit X Axis" ), Key::Z, true); |
| 1191 | ED_SHORTCUT("grid_map/edit_y_axis" , TTR("Edit Y Axis" ), Key::X, true); |
| 1192 | ED_SHORTCUT("grid_map/edit_z_axis" , TTR("Edit Z Axis" ), Key::C, true); |
| 1193 | ED_SHORTCUT("grid_map/cursor_rotate_x" , TTR("Cursor Rotate X" ), Key::A, true); |
| 1194 | ED_SHORTCUT("grid_map/cursor_rotate_y" , TTR("Cursor Rotate Y" ), Key::S, true); |
| 1195 | ED_SHORTCUT("grid_map/cursor_rotate_z" , TTR("Cursor Rotate Z" ), Key::D, true); |
| 1196 | ED_SHORTCUT("grid_map/cursor_back_rotate_x" , TTR("Cursor Back Rotate X" ), KeyModifierMask::SHIFT + Key::A, true); |
| 1197 | ED_SHORTCUT("grid_map/cursor_back_rotate_y" , TTR("Cursor Back Rotate Y" ), KeyModifierMask::SHIFT + Key::S, true); |
| 1198 | ED_SHORTCUT("grid_map/cursor_back_rotate_z" , TTR("Cursor Back Rotate Z" ), KeyModifierMask::SHIFT + Key::D, true); |
| 1199 | ED_SHORTCUT("grid_map/cursor_clear_rotation" , TTR("Cursor Clear Rotation" ), Key::W, true); |
| 1200 | ED_SHORTCUT("grid_map/paste_selects" , TTR("Paste Selects" )); |
| 1201 | ED_SHORTCUT("grid_map/duplicate_selection" , TTR("Duplicate Selection" ), KeyModifierMask::CTRL + Key::C); |
| 1202 | ED_SHORTCUT("grid_map/cut_selection" , TTR("Cut Selection" ), KeyModifierMask::CTRL + Key::X); |
| 1203 | ED_SHORTCUT("grid_map/clear_selection" , TTR("Clear Selection" ), Key::KEY_DELETE); |
| 1204 | ED_SHORTCUT("grid_map/fill_selection" , TTR("Fill Selection" ), KeyModifierMask::CTRL + Key::F); |
| 1205 | |
| 1206 | int mw = EDITOR_DEF("editors/grid_map/palette_min_width" , 230); |
| 1207 | Control *ec = memnew(Control); |
| 1208 | ec->set_custom_minimum_size(Size2(mw, 0) * EDSCALE); |
| 1209 | add_child(ec); |
| 1210 | |
| 1211 | spatial_editor_hb = memnew(HBoxContainer); |
| 1212 | spatial_editor_hb->set_h_size_flags(SIZE_EXPAND_FILL); |
| 1213 | spatial_editor_hb->set_alignment(BoxContainer::ALIGNMENT_END); |
| 1214 | Node3DEditor::get_singleton()->add_control_to_menu_panel(spatial_editor_hb); |
| 1215 | |
| 1216 | spin_box_label = memnew(Label); |
| 1217 | spin_box_label->set_text(TTR("Floor:" )); |
| 1218 | spatial_editor_hb->add_child(spin_box_label); |
| 1219 | |
| 1220 | floor = memnew(SpinBox); |
| 1221 | floor->set_min(-32767); |
| 1222 | floor->set_max(32767); |
| 1223 | floor->set_step(1); |
| 1224 | floor->get_line_edit()->add_theme_constant_override("minimum_character_width" , 16); |
| 1225 | |
| 1226 | spatial_editor_hb->add_child(floor); |
| 1227 | floor->connect("value_changed" , callable_mp(this, &GridMapEditor::_floor_changed)); |
| 1228 | floor->connect("mouse_exited" , callable_mp(this, &GridMapEditor::_floor_mouse_exited)); |
| 1229 | floor->get_line_edit()->connect("mouse_exited" , callable_mp(this, &GridMapEditor::_floor_mouse_exited)); |
| 1230 | |
| 1231 | spatial_editor_hb->add_child(memnew(VSeparator)); |
| 1232 | |
| 1233 | options = memnew(MenuButton); |
| 1234 | spatial_editor_hb->add_child(options); |
| 1235 | spatial_editor_hb->hide(); |
| 1236 | |
| 1237 | options->set_text(TTR("Grid Map" )); |
| 1238 | options->get_popup()->add_shortcut(ED_GET_SHORTCUT("grid_map/previous_floor" ), MENU_OPTION_PREV_LEVEL); |
| 1239 | options->get_popup()->add_shortcut(ED_GET_SHORTCUT("grid_map/next_floor" ), MENU_OPTION_NEXT_LEVEL); |
| 1240 | options->get_popup()->add_separator(); |
| 1241 | options->get_popup()->add_radio_check_shortcut(ED_GET_SHORTCUT("grid_map/edit_x_axis" ), MENU_OPTION_X_AXIS); |
| 1242 | options->get_popup()->add_radio_check_shortcut(ED_GET_SHORTCUT("grid_map/edit_y_axis" ), MENU_OPTION_Y_AXIS); |
| 1243 | options->get_popup()->add_radio_check_shortcut(ED_GET_SHORTCUT("grid_map/edit_z_axis" ), MENU_OPTION_Z_AXIS); |
| 1244 | options->get_popup()->set_item_checked(options->get_popup()->get_item_index(MENU_OPTION_Y_AXIS), true); |
| 1245 | options->get_popup()->add_separator(); |
| 1246 | options->get_popup()->add_shortcut(ED_GET_SHORTCUT("grid_map/cursor_rotate_x" ), MENU_OPTION_CURSOR_ROTATE_X); |
| 1247 | options->get_popup()->add_shortcut(ED_GET_SHORTCUT("grid_map/cursor_rotate_y" ), MENU_OPTION_CURSOR_ROTATE_Y); |
| 1248 | options->get_popup()->add_shortcut(ED_GET_SHORTCUT("grid_map/cursor_rotate_z" ), MENU_OPTION_CURSOR_ROTATE_Z); |
| 1249 | options->get_popup()->add_shortcut(ED_GET_SHORTCUT("grid_map/cursor_back_rotate_x" ), MENU_OPTION_CURSOR_BACK_ROTATE_X); |
| 1250 | options->get_popup()->add_shortcut(ED_GET_SHORTCUT("grid_map/cursor_back_rotate_y" ), MENU_OPTION_CURSOR_BACK_ROTATE_Y); |
| 1251 | options->get_popup()->add_shortcut(ED_GET_SHORTCUT("grid_map/cursor_back_rotate_z" ), MENU_OPTION_CURSOR_BACK_ROTATE_Z); |
| 1252 | options->get_popup()->add_shortcut(ED_GET_SHORTCUT("grid_map/cursor_clear_rotation" ), MENU_OPTION_CURSOR_CLEAR_ROTATION); |
| 1253 | options->get_popup()->add_separator(); |
| 1254 | // TRANSLATORS: This is a toggle to select after pasting the new content. |
| 1255 | options->get_popup()->add_check_shortcut(ED_GET_SHORTCUT("grid_map/paste_selects" ), MENU_OPTION_PASTE_SELECTS); |
| 1256 | options->get_popup()->add_separator(); |
| 1257 | options->get_popup()->add_shortcut(ED_GET_SHORTCUT("grid_map/duplicate_selection" ), MENU_OPTION_SELECTION_DUPLICATE); |
| 1258 | options->get_popup()->add_shortcut(ED_GET_SHORTCUT("grid_map/cut_selection" ), MENU_OPTION_SELECTION_CUT); |
| 1259 | options->get_popup()->add_shortcut(ED_GET_SHORTCUT("grid_map/clear_selection" ), MENU_OPTION_SELECTION_CLEAR); |
| 1260 | options->get_popup()->add_shortcut(ED_GET_SHORTCUT("grid_map/fill_selection" ), MENU_OPTION_SELECTION_FILL); |
| 1261 | |
| 1262 | options->get_popup()->add_separator(); |
| 1263 | options->get_popup()->add_item(TTR("Settings..." ), MENU_OPTION_GRIDMAP_SETTINGS); |
| 1264 | |
| 1265 | settings_dialog = memnew(ConfirmationDialog); |
| 1266 | settings_dialog->set_title(TTR("GridMap Settings" )); |
| 1267 | add_child(settings_dialog); |
| 1268 | settings_vbc = memnew(VBoxContainer); |
| 1269 | settings_vbc->set_custom_minimum_size(Size2(200, 0) * EDSCALE); |
| 1270 | settings_dialog->add_child(settings_vbc); |
| 1271 | |
| 1272 | settings_pick_distance = memnew(SpinBox); |
| 1273 | settings_pick_distance->set_max(10000.0f); |
| 1274 | settings_pick_distance->set_min(500.0f); |
| 1275 | settings_pick_distance->set_step(1.0f); |
| 1276 | settings_pick_distance->set_value(EDITOR_GET("editors/grid_map/pick_distance" )); |
| 1277 | settings_vbc->add_margin_child(TTR("Pick Distance:" ), settings_pick_distance); |
| 1278 | |
| 1279 | options->get_popup()->connect("id_pressed" , callable_mp(this, &GridMapEditor::_menu_option)); |
| 1280 | |
| 1281 | HBoxContainer *hb = memnew(HBoxContainer); |
| 1282 | add_child(hb); |
| 1283 | hb->set_h_size_flags(SIZE_EXPAND_FILL); |
| 1284 | |
| 1285 | search_box = memnew(LineEdit); |
| 1286 | search_box->set_h_size_flags(SIZE_EXPAND_FILL); |
| 1287 | search_box->set_placeholder(TTR("Filter Meshes" )); |
| 1288 | search_box->set_clear_button_enabled(true); |
| 1289 | hb->add_child(search_box); |
| 1290 | search_box->connect("text_changed" , callable_mp(this, &GridMapEditor::_text_changed)); |
| 1291 | search_box->connect("gui_input" , callable_mp(this, &GridMapEditor::_sbox_input)); |
| 1292 | |
| 1293 | mode_thumbnail = memnew(Button); |
| 1294 | mode_thumbnail->set_flat(true); |
| 1295 | mode_thumbnail->set_toggle_mode(true); |
| 1296 | mode_thumbnail->set_pressed(true); |
| 1297 | hb->add_child(mode_thumbnail); |
| 1298 | mode_thumbnail->connect("pressed" , callable_mp(this, &GridMapEditor::_set_display_mode).bind(DISPLAY_THUMBNAIL)); |
| 1299 | |
| 1300 | mode_list = memnew(Button); |
| 1301 | mode_list->set_flat(true); |
| 1302 | mode_list->set_toggle_mode(true); |
| 1303 | mode_list->set_pressed(false); |
| 1304 | hb->add_child(mode_list); |
| 1305 | mode_list->connect("pressed" , callable_mp(this, &GridMapEditor::_set_display_mode).bind(DISPLAY_LIST)); |
| 1306 | |
| 1307 | size_slider = memnew(HSlider); |
| 1308 | size_slider->set_h_size_flags(SIZE_EXPAND_FILL); |
| 1309 | size_slider->set_min(0.2f); |
| 1310 | size_slider->set_max(4.0f); |
| 1311 | size_slider->set_step(0.1f); |
| 1312 | size_slider->set_value(1.0f); |
| 1313 | size_slider->connect("value_changed" , callable_mp(this, &GridMapEditor::_icon_size_changed)); |
| 1314 | add_child(size_slider); |
| 1315 | |
| 1316 | EDITOR_DEF("editors/grid_map/preview_size" , 64); |
| 1317 | |
| 1318 | mesh_library_palette = memnew(ItemList); |
| 1319 | add_child(mesh_library_palette); |
| 1320 | mesh_library_palette->set_v_size_flags(SIZE_EXPAND_FILL); |
| 1321 | mesh_library_palette->connect("gui_input" , callable_mp(this, &GridMapEditor::_mesh_library_palette_input)); |
| 1322 | |
| 1323 | info_message = memnew(Label); |
| 1324 | info_message->set_text(TTR("Give a MeshLibrary resource to this GridMap to use its meshes." )); |
| 1325 | info_message->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER); |
| 1326 | info_message->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER); |
| 1327 | info_message->set_autowrap_mode(TextServer::AUTOWRAP_WORD_SMART); |
| 1328 | info_message->set_custom_minimum_size(Size2(100 * EDSCALE, 0)); |
| 1329 | info_message->set_anchors_and_offsets_preset(PRESET_FULL_RECT, PRESET_MODE_KEEP_SIZE, 8 * EDSCALE); |
| 1330 | mesh_library_palette->add_child(info_message); |
| 1331 | |
| 1332 | edit_axis = Vector3::AXIS_Y; |
| 1333 | edit_floor[0] = -1; |
| 1334 | edit_floor[1] = -1; |
| 1335 | edit_floor[2] = -1; |
| 1336 | |
| 1337 | selection_mesh = RenderingServer::get_singleton()->mesh_create(); |
| 1338 | paste_mesh = RenderingServer::get_singleton()->mesh_create(); |
| 1339 | |
| 1340 | { |
| 1341 | // Selection mesh create. |
| 1342 | |
| 1343 | Vector<Vector3> lines; |
| 1344 | Vector<Vector3> triangles; |
| 1345 | Vector<Vector3> square[3]; |
| 1346 | |
| 1347 | for (int i = 0; i < 6; i++) { |
| 1348 | Vector3 face_points[4]; |
| 1349 | |
| 1350 | for (int j = 0; j < 4; j++) { |
| 1351 | float v[3]; |
| 1352 | v[0] = 1.0; |
| 1353 | v[1] = 1 - 2 * ((j >> 1) & 1); |
| 1354 | v[2] = v[1] * (1 - 2 * (j & 1)); |
| 1355 | |
| 1356 | for (int k = 0; k < 3; k++) { |
| 1357 | if (i < 3) { |
| 1358 | face_points[j][(i + k) % 3] = v[k]; |
| 1359 | } else { |
| 1360 | face_points[3 - j][(i + k) % 3] = -v[k]; |
| 1361 | } |
| 1362 | } |
| 1363 | } |
| 1364 | |
| 1365 | triangles.push_back(face_points[0] * 0.5 + Vector3(0.5, 0.5, 0.5)); |
| 1366 | triangles.push_back(face_points[1] * 0.5 + Vector3(0.5, 0.5, 0.5)); |
| 1367 | triangles.push_back(face_points[2] * 0.5 + Vector3(0.5, 0.5, 0.5)); |
| 1368 | |
| 1369 | triangles.push_back(face_points[2] * 0.5 + Vector3(0.5, 0.5, 0.5)); |
| 1370 | triangles.push_back(face_points[3] * 0.5 + Vector3(0.5, 0.5, 0.5)); |
| 1371 | triangles.push_back(face_points[0] * 0.5 + Vector3(0.5, 0.5, 0.5)); |
| 1372 | } |
| 1373 | |
| 1374 | for (int i = 0; i < 12; i++) { |
| 1375 | AABB base(Vector3(0, 0, 0), Vector3(1, 1, 1)); |
| 1376 | Vector3 a, b; |
| 1377 | base.get_edge(i, a, b); |
| 1378 | lines.push_back(a); |
| 1379 | lines.push_back(b); |
| 1380 | } |
| 1381 | |
| 1382 | for (int i = 0; i < 3; i++) { |
| 1383 | Vector3 points[4]; |
| 1384 | for (int j = 0; j < 4; j++) { |
| 1385 | static const bool orderx[4] = { false, true, true, false }; |
| 1386 | static const bool ordery[4] = { false, false, true, true }; |
| 1387 | |
| 1388 | Vector3 sp; |
| 1389 | if (orderx[j]) { |
| 1390 | sp[(i + 1) % 3] = 1.0; |
| 1391 | } |
| 1392 | if (ordery[j]) { |
| 1393 | sp[(i + 2) % 3] = 1.0; |
| 1394 | } |
| 1395 | |
| 1396 | points[j] = sp; |
| 1397 | } |
| 1398 | |
| 1399 | for (int j = 0; j < 4; j++) { |
| 1400 | Vector3 ofs; |
| 1401 | ofs[i] += 0.01; |
| 1402 | square[i].push_back(points[j] - ofs); |
| 1403 | square[i].push_back(points[(j + 1) % 4] - ofs); |
| 1404 | square[i].push_back(points[j] + ofs); |
| 1405 | square[i].push_back(points[(j + 1) % 4] + ofs); |
| 1406 | } |
| 1407 | } |
| 1408 | |
| 1409 | Array d; |
| 1410 | d.resize(RS::ARRAY_MAX); |
| 1411 | |
| 1412 | inner_mat.instantiate(); |
| 1413 | inner_mat->set_albedo(Color(0.7, 0.7, 1.0, 0.2)); |
| 1414 | inner_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED); |
| 1415 | inner_mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA); |
| 1416 | |
| 1417 | d[RS::ARRAY_VERTEX] = triangles; |
| 1418 | RenderingServer::get_singleton()->mesh_add_surface_from_arrays(selection_mesh, RS::PRIMITIVE_TRIANGLES, d); |
| 1419 | RenderingServer::get_singleton()->mesh_surface_set_material(selection_mesh, 0, inner_mat->get_rid()); |
| 1420 | |
| 1421 | outer_mat.instantiate(); |
| 1422 | outer_mat->set_albedo(Color(0.7, 0.7, 1.0, 0.8)); |
| 1423 | outer_mat->set_on_top_of_alpha(); |
| 1424 | |
| 1425 | outer_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED); |
| 1426 | outer_mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA); |
| 1427 | |
| 1428 | selection_floor_mat.instantiate(); |
| 1429 | selection_floor_mat->set_albedo(Color(0.80, 0.80, 1.0, 1)); |
| 1430 | selection_floor_mat->set_on_top_of_alpha(); |
| 1431 | selection_floor_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED); |
| 1432 | |
| 1433 | d[RS::ARRAY_VERTEX] = lines; |
| 1434 | RenderingServer::get_singleton()->mesh_add_surface_from_arrays(selection_mesh, RS::PRIMITIVE_LINES, d); |
| 1435 | RenderingServer::get_singleton()->mesh_surface_set_material(selection_mesh, 1, outer_mat->get_rid()); |
| 1436 | |
| 1437 | d[RS::ARRAY_VERTEX] = triangles; |
| 1438 | RenderingServer::get_singleton()->mesh_add_surface_from_arrays(paste_mesh, RS::PRIMITIVE_TRIANGLES, d); |
| 1439 | RenderingServer::get_singleton()->mesh_surface_set_material(paste_mesh, 0, inner_mat->get_rid()); |
| 1440 | |
| 1441 | d[RS::ARRAY_VERTEX] = lines; |
| 1442 | RenderingServer::get_singleton()->mesh_add_surface_from_arrays(paste_mesh, RS::PRIMITIVE_LINES, d); |
| 1443 | RenderingServer::get_singleton()->mesh_surface_set_material(paste_mesh, 1, outer_mat->get_rid()); |
| 1444 | |
| 1445 | for (int i = 0; i < 3; i++) { |
| 1446 | d[RS::ARRAY_VERTEX] = square[i]; |
| 1447 | selection_level_mesh[i] = RS::get_singleton()->mesh_create(); |
| 1448 | RenderingServer::get_singleton()->mesh_add_surface_from_arrays(selection_level_mesh[i], RS::PRIMITIVE_LINES, d); |
| 1449 | RenderingServer::get_singleton()->mesh_surface_set_material(selection_level_mesh[i], 0, selection_floor_mat->get_rid()); |
| 1450 | } |
| 1451 | } |
| 1452 | |
| 1453 | _set_selection(false); |
| 1454 | |
| 1455 | indicator_mat.instantiate(); |
| 1456 | indicator_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED); |
| 1457 | indicator_mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA); |
| 1458 | indicator_mat->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true); |
| 1459 | indicator_mat->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true); |
| 1460 | indicator_mat->set_albedo(Color(0.8, 0.5, 0.1)); |
| 1461 | } |
| 1462 | |
| 1463 | GridMapEditor::~GridMapEditor() { |
| 1464 | ERR_FAIL_NULL(RenderingServer::get_singleton()); |
| 1465 | _clear_clipboard_data(); |
| 1466 | |
| 1467 | for (int i = 0; i < 3; i++) { |
| 1468 | if (grid[i].is_valid()) { |
| 1469 | RenderingServer::get_singleton()->free(grid[i]); |
| 1470 | } |
| 1471 | if (grid_instance[i].is_valid()) { |
| 1472 | RenderingServer::get_singleton()->free(grid_instance[i]); |
| 1473 | } |
| 1474 | if (cursor_instance.is_valid()) { |
| 1475 | RenderingServer::get_singleton()->free(cursor_instance); |
| 1476 | } |
| 1477 | if (selection_level_instance[i].is_valid()) { |
| 1478 | RenderingServer::get_singleton()->free(selection_level_instance[i]); |
| 1479 | } |
| 1480 | if (selection_level_mesh[i].is_valid()) { |
| 1481 | RenderingServer::get_singleton()->free(selection_level_mesh[i]); |
| 1482 | } |
| 1483 | } |
| 1484 | |
| 1485 | RenderingServer::get_singleton()->free(selection_mesh); |
| 1486 | if (selection_instance.is_valid()) { |
| 1487 | RenderingServer::get_singleton()->free(selection_instance); |
| 1488 | } |
| 1489 | |
| 1490 | RenderingServer::get_singleton()->free(paste_mesh); |
| 1491 | if (paste_instance.is_valid()) { |
| 1492 | RenderingServer::get_singleton()->free(paste_instance); |
| 1493 | } |
| 1494 | } |
| 1495 | |
| 1496 | void GridMapEditorPlugin::_notification(int p_what) { |
| 1497 | switch (p_what) { |
| 1498 | case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: { |
| 1499 | switch ((int)EDITOR_GET("editors/grid_map/editor_side" )) { |
| 1500 | case 0: { // Left. |
| 1501 | Node3DEditor::get_singleton()->move_control_to_left_panel(grid_map_editor); |
| 1502 | } break; |
| 1503 | case 1: { // Right. |
| 1504 | Node3DEditor::get_singleton()->move_control_to_right_panel(grid_map_editor); |
| 1505 | } break; |
| 1506 | } |
| 1507 | } break; |
| 1508 | } |
| 1509 | } |
| 1510 | |
| 1511 | void GridMapEditorPlugin::edit(Object *p_object) { |
| 1512 | grid_map_editor->edit(Object::cast_to<GridMap>(p_object)); |
| 1513 | } |
| 1514 | |
| 1515 | bool GridMapEditorPlugin::handles(Object *p_object) const { |
| 1516 | return p_object->is_class("GridMap" ); |
| 1517 | } |
| 1518 | |
| 1519 | void GridMapEditorPlugin::make_visible(bool p_visible) { |
| 1520 | if (p_visible) { |
| 1521 | grid_map_editor->show(); |
| 1522 | grid_map_editor->spatial_editor_hb->show(); |
| 1523 | grid_map_editor->set_process(true); |
| 1524 | } else { |
| 1525 | grid_map_editor->spatial_editor_hb->hide(); |
| 1526 | grid_map_editor->hide(); |
| 1527 | grid_map_editor->set_process(false); |
| 1528 | } |
| 1529 | } |
| 1530 | |
| 1531 | GridMapEditorPlugin::GridMapEditorPlugin() { |
| 1532 | EDITOR_DEF("editors/grid_map/editor_side" , 1); |
| 1533 | EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::INT, "editors/grid_map/editor_side" , PROPERTY_HINT_ENUM, "Left,Right" )); |
| 1534 | |
| 1535 | grid_map_editor = memnew(GridMapEditor); |
| 1536 | switch ((int)EDITOR_GET("editors/grid_map/editor_side" )) { |
| 1537 | case 0: { // Left. |
| 1538 | Node3DEditor::get_singleton()->add_control_to_left_panel(grid_map_editor); |
| 1539 | } break; |
| 1540 | case 1: { // Right. |
| 1541 | Node3DEditor::get_singleton()->add_control_to_right_panel(grid_map_editor); |
| 1542 | } break; |
| 1543 | } |
| 1544 | grid_map_editor->hide(); |
| 1545 | } |
| 1546 | |
| 1547 | GridMapEditorPlugin::~GridMapEditorPlugin() { |
| 1548 | } |
| 1549 | |
| 1550 | #endif // TOOLS_ENABLED |
| 1551 | |