From 5fe6984639b5db9efa14103698e489040315ebfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Verschelde?= Date: Mon, 28 Mar 2022 14:10:28 +0200 Subject: Modules: Don't build editor-specific classes in templates They're moved to an `editor` subfolder so that we can easily handle them separately. --- modules/csg/SCsub | 3 + modules/csg/csg_gizmos.cpp | 427 ------ modules/csg/csg_gizmos.h | 63 - modules/csg/editor/csg_gizmos.cpp | 431 ++++++ modules/csg/editor/csg_gizmos.h | 67 + modules/csg/register_types.cpp | 12 +- modules/gltf/SCsub | 3 +- .../editor/editor_scene_exporter_gltf_plugin.cpp | 109 ++ .../editor/editor_scene_exporter_gltf_plugin.h | 54 + modules/gltf/editor/editor_scene_importer_gltf.cpp | 74 + modules/gltf/editor/editor_scene_importer_gltf.h | 58 + modules/gltf/editor_scene_exporter_gltf_plugin.cpp | 108 -- modules/gltf/editor_scene_exporter_gltf_plugin.h | 51 - modules/gltf/editor_scene_importer_gltf.cpp | 73 - modules/gltf/editor_scene_importer_gltf.h | 55 - modules/gltf/register_types.cpp | 15 +- modules/gridmap/SCsub | 3 + modules/gridmap/editor/grid_map_editor_plugin.cpp | 1485 ++++++++++++++++++++ modules/gridmap/editor/grid_map_editor_plugin.h | 256 ++++ modules/gridmap/grid_map_editor_plugin.cpp | 1481 ------------------- modules/gridmap/grid_map_editor_plugin.h | 252 ---- modules/gridmap/register_types.cpp | 12 +- modules/navigation/SCsub | 2 + .../editor/navigation_mesh_editor_plugin.cpp | 157 +++ .../editor/navigation_mesh_editor_plugin.h | 86 ++ .../navigation/navigation_mesh_editor_plugin.cpp | 156 -- modules/navigation/navigation_mesh_editor_plugin.h | 86 -- modules/navigation/register_types.cpp | 2 +- modules/websocket/SCsub | 2 + .../editor/editor_debugger_server_websocket.cpp | 135 ++ .../editor/editor_debugger_server_websocket.h | 67 + .../websocket/editor_debugger_server_websocket.cpp | 131 -- .../websocket/editor_debugger_server_websocket.h | 63 - modules/websocket/register_types.cpp | 5 +- modules/websocket/remote_debugger_peer_websocket.h | 7 +- 35 files changed, 3024 insertions(+), 2967 deletions(-) delete mode 100644 modules/csg/csg_gizmos.cpp delete mode 100644 modules/csg/csg_gizmos.h create mode 100644 modules/csg/editor/csg_gizmos.cpp create mode 100644 modules/csg/editor/csg_gizmos.h create mode 100644 modules/gltf/editor/editor_scene_exporter_gltf_plugin.cpp create mode 100644 modules/gltf/editor/editor_scene_exporter_gltf_plugin.h create mode 100644 modules/gltf/editor/editor_scene_importer_gltf.cpp create mode 100644 modules/gltf/editor/editor_scene_importer_gltf.h delete mode 100644 modules/gltf/editor_scene_exporter_gltf_plugin.cpp delete mode 100644 modules/gltf/editor_scene_exporter_gltf_plugin.h delete mode 100644 modules/gltf/editor_scene_importer_gltf.cpp delete mode 100644 modules/gltf/editor_scene_importer_gltf.h create mode 100644 modules/gridmap/editor/grid_map_editor_plugin.cpp create mode 100644 modules/gridmap/editor/grid_map_editor_plugin.h delete mode 100644 modules/gridmap/grid_map_editor_plugin.cpp delete mode 100644 modules/gridmap/grid_map_editor_plugin.h create mode 100644 modules/navigation/editor/navigation_mesh_editor_plugin.cpp create mode 100644 modules/navigation/editor/navigation_mesh_editor_plugin.h delete mode 100644 modules/navigation/navigation_mesh_editor_plugin.cpp delete mode 100644 modules/navigation/navigation_mesh_editor_plugin.h create mode 100644 modules/websocket/editor/editor_debugger_server_websocket.cpp create mode 100644 modules/websocket/editor/editor_debugger_server_websocket.h delete mode 100644 modules/websocket/editor_debugger_server_websocket.cpp delete mode 100644 modules/websocket/editor_debugger_server_websocket.h diff --git a/modules/csg/SCsub b/modules/csg/SCsub index 641a42c187..c7307ddefd 100644 --- a/modules/csg/SCsub +++ b/modules/csg/SCsub @@ -3,7 +3,10 @@ Import("env") Import("env_modules") +# Godot's own source files env_csg = env_modules.Clone() # Godot's own source files env_csg.add_source_files(env.modules_sources, "*.cpp") +if env["tools"]: + env_csg.add_source_files(env.modules_sources, "editor/*.cpp") diff --git a/modules/csg/csg_gizmos.cpp b/modules/csg/csg_gizmos.cpp deleted file mode 100644 index 95a0fc7ada..0000000000 --- a/modules/csg/csg_gizmos.cpp +++ /dev/null @@ -1,427 +0,0 @@ -/*************************************************************************/ -/* csg_gizmos.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#include "csg_gizmos.h" - -#include "editor/editor_settings.h" -#include "editor/plugins/node_3d_editor_plugin.h" -#include "scene/3d/camera_3d.h" - -/////////// - -CSGShape3DGizmoPlugin::CSGShape3DGizmoPlugin() { - Color gizmo_color = EDITOR_DEF("editors/3d_gizmos/gizmo_colors/csg", Color(0.0, 0.4, 1, 0.15)); - create_material("shape_union_material", gizmo_color); - create_material("shape_union_solid_material", gizmo_color); - gizmo_color.invert(); - create_material("shape_subtraction_material", gizmo_color); - create_material("shape_subtraction_solid_material", gizmo_color); - gizmo_color.r = 0.95; - gizmo_color.g = 0.95; - gizmo_color.b = 0.95; - create_material("shape_intersection_material", gizmo_color); - create_material("shape_intersection_solid_material", gizmo_color); - - create_handle_material("handles"); -} - -String CSGShape3DGizmoPlugin::get_handle_name(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const { - CSGShape3D *cs = Object::cast_to(p_gizmo->get_spatial_node()); - - if (Object::cast_to(cs)) { - return "Radius"; - } - - if (Object::cast_to(cs)) { - return "Size"; - } - - if (Object::cast_to(cs)) { - return p_id == 0 ? "Radius" : "Height"; - } - - if (Object::cast_to(cs)) { - return p_id == 0 ? "InnerRadius" : "OuterRadius"; - } - - return ""; -} - -Variant CSGShape3DGizmoPlugin::get_handle_value(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const { - CSGShape3D *cs = Object::cast_to(p_gizmo->get_spatial_node()); - - if (Object::cast_to(cs)) { - CSGSphere3D *s = Object::cast_to(cs); - return s->get_radius(); - } - - if (Object::cast_to(cs)) { - CSGBox3D *s = Object::cast_to(cs); - return s->get_size(); - } - - if (Object::cast_to(cs)) { - CSGCylinder3D *s = Object::cast_to(cs); - return p_id == 0 ? s->get_radius() : s->get_height(); - } - - if (Object::cast_to(cs)) { - CSGTorus3D *s = Object::cast_to(cs); - return p_id == 0 ? s->get_inner_radius() : s->get_outer_radius(); - } - - return Variant(); -} - -void CSGShape3DGizmoPlugin::set_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) { - CSGShape3D *cs = Object::cast_to(p_gizmo->get_spatial_node()); - - Transform3D gt = cs->get_global_transform(); - //gt.orthonormalize(); - Transform3D gi = gt.affine_inverse(); - - Vector3 ray_from = p_camera->project_ray_origin(p_point); - Vector3 ray_dir = p_camera->project_ray_normal(p_point); - - Vector3 sg[2] = { gi.xform(ray_from), gi.xform(ray_from + ray_dir * 16384) }; - - if (Object::cast_to(cs)) { - CSGSphere3D *s = Object::cast_to(cs); - - Vector3 ra, rb; - Geometry3D::get_closest_points_between_segments(Vector3(), Vector3(4096, 0, 0), sg[0], sg[1], ra, rb); - float d = ra.x; - if (Node3DEditor::get_singleton()->is_snap_enabled()) { - d = Math::snapped(d, Node3DEditor::get_singleton()->get_translate_snap()); - } - - if (d < 0.001) { - d = 0.001; - } - - s->set_radius(d); - } - - if (Object::cast_to(cs)) { - CSGBox3D *s = Object::cast_to(cs); - - Vector3 axis; - axis[p_id] = 1.0; - Vector3 ra, rb; - Geometry3D::get_closest_points_between_segments(Vector3(), axis * 4096, sg[0], sg[1], ra, rb); - float d = ra[p_id]; - - if (Math::is_nan(d)) { - // The handle is perpendicular to the camera. - return; - } - - if (Node3DEditor::get_singleton()->is_snap_enabled()) { - d = Math::snapped(d, Node3DEditor::get_singleton()->get_translate_snap()); - } - - if (d < 0.001) { - d = 0.001; - } - - Vector3 h = s->get_size(); - h[p_id] = d * 2; - s->set_size(h); - } - - if (Object::cast_to(cs)) { - CSGCylinder3D *s = Object::cast_to(cs); - - Vector3 axis; - axis[p_id == 0 ? 0 : 1] = 1.0; - Vector3 ra, rb; - Geometry3D::get_closest_points_between_segments(Vector3(), axis * 4096, sg[0], sg[1], ra, rb); - float d = axis.dot(ra); - if (Node3DEditor::get_singleton()->is_snap_enabled()) { - d = Math::snapped(d, Node3DEditor::get_singleton()->get_translate_snap()); - } - - if (d < 0.001) { - d = 0.001; - } - - if (p_id == 0) { - s->set_radius(d); - } else if (p_id == 1) { - s->set_height(d * 2.0); - } - } - - if (Object::cast_to(cs)) { - CSGTorus3D *s = Object::cast_to(cs); - - Vector3 axis; - axis[0] = 1.0; - Vector3 ra, rb; - Geometry3D::get_closest_points_between_segments(Vector3(), axis * 4096, sg[0], sg[1], ra, rb); - float d = axis.dot(ra); - if (Node3DEditor::get_singleton()->is_snap_enabled()) { - d = Math::snapped(d, Node3DEditor::get_singleton()->get_translate_snap()); - } - - if (d < 0.001) { - d = 0.001; - } - - if (p_id == 0) { - s->set_inner_radius(d); - } else if (p_id == 1) { - s->set_outer_radius(d); - } - } -} - -void CSGShape3DGizmoPlugin::commit_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel) { - CSGShape3D *cs = Object::cast_to(p_gizmo->get_spatial_node()); - - if (Object::cast_to(cs)) { - CSGSphere3D *s = Object::cast_to(cs); - if (p_cancel) { - s->set_radius(p_restore); - return; - } - - UndoRedo *ur = Node3DEditor::get_singleton()->get_undo_redo(); - ur->create_action(TTR("Change Sphere Shape Radius")); - ur->add_do_method(s, "set_radius", s->get_radius()); - ur->add_undo_method(s, "set_radius", p_restore); - ur->commit_action(); - } - - if (Object::cast_to(cs)) { - CSGBox3D *s = Object::cast_to(cs); - if (p_cancel) { - s->set_size(p_restore); - return; - } - - UndoRedo *ur = Node3DEditor::get_singleton()->get_undo_redo(); - ur->create_action(TTR("Change Box Shape Size")); - ur->add_do_method(s, "set_size", s->get_size()); - ur->add_undo_method(s, "set_size", p_restore); - ur->commit_action(); - } - - if (Object::cast_to(cs)) { - CSGCylinder3D *s = Object::cast_to(cs); - if (p_cancel) { - if (p_id == 0) { - s->set_radius(p_restore); - } else { - s->set_height(p_restore); - } - return; - } - - UndoRedo *ur = Node3DEditor::get_singleton()->get_undo_redo(); - if (p_id == 0) { - ur->create_action(TTR("Change Cylinder Radius")); - ur->add_do_method(s, "set_radius", s->get_radius()); - ur->add_undo_method(s, "set_radius", p_restore); - } else { - ur->create_action(TTR("Change Cylinder Height")); - ur->add_do_method(s, "set_height", s->get_height()); - ur->add_undo_method(s, "set_height", p_restore); - } - - ur->commit_action(); - } - - if (Object::cast_to(cs)) { - CSGTorus3D *s = Object::cast_to(cs); - if (p_cancel) { - if (p_id == 0) { - s->set_inner_radius(p_restore); - } else { - s->set_outer_radius(p_restore); - } - return; - } - - UndoRedo *ur = Node3DEditor::get_singleton()->get_undo_redo(); - if (p_id == 0) { - ur->create_action(TTR("Change Torus Inner Radius")); - ur->add_do_method(s, "set_inner_radius", s->get_inner_radius()); - ur->add_undo_method(s, "set_inner_radius", p_restore); - } else { - ur->create_action(TTR("Change Torus Outer Radius")); - ur->add_do_method(s, "set_outer_radius", s->get_outer_radius()); - ur->add_undo_method(s, "set_outer_radius", p_restore); - } - - ur->commit_action(); - } -} - -bool CSGShape3DGizmoPlugin::has_gizmo(Node3D *p_spatial) { - return Object::cast_to(p_spatial) || Object::cast_to(p_spatial) || Object::cast_to(p_spatial) || Object::cast_to(p_spatial) || Object::cast_to(p_spatial) || Object::cast_to(p_spatial); -} - -String CSGShape3DGizmoPlugin::get_gizmo_name() const { - return "CSGShape3D"; -} - -int CSGShape3DGizmoPlugin::get_priority() const { - return -1; -} - -bool CSGShape3DGizmoPlugin::is_selectable_when_hidden() const { - return true; -} - -void CSGShape3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) { - p_gizmo->clear(); - - CSGShape3D *cs = Object::cast_to(p_gizmo->get_spatial_node()); - - Vector faces = cs->get_brush_faces(); - - if (faces.size() == 0) { - return; - } - - Vector lines; - lines.resize(faces.size() * 2); - { - const Vector3 *r = faces.ptr(); - - for (int i = 0; i < lines.size(); i += 6) { - int f = i / 6; - for (int j = 0; j < 3; j++) { - int j_n = (j + 1) % 3; - lines.write[i + j * 2 + 0] = r[f * 3 + j]; - lines.write[i + j * 2 + 1] = r[f * 3 + j_n]; - } - } - } - - Ref material; - switch (cs->get_operation()) { - case CSGShape3D::OPERATION_UNION: - material = get_material("shape_union_material", p_gizmo); - break; - case CSGShape3D::OPERATION_INTERSECTION: - material = get_material("shape_intersection_material", p_gizmo); - break; - case CSGShape3D::OPERATION_SUBTRACTION: - material = get_material("shape_subtraction_material", p_gizmo); - break; - } - - Ref handles_material = get_material("handles"); - - p_gizmo->add_lines(lines, material); - p_gizmo->add_collision_segments(lines); - - Array csg_meshes = cs->get_meshes(); - if (csg_meshes.size() != 2) { - return; - } - - Ref csg_mesh = csg_meshes[1]; - if (csg_mesh.is_valid()) { - p_gizmo->add_collision_triangles(csg_mesh->generate_triangle_mesh()); - } - - if (p_gizmo->is_selected()) { - // Draw a translucent representation of the CSG node - Ref mesh = memnew(ArrayMesh); - Array array; - array.resize(Mesh::ARRAY_MAX); - array[Mesh::ARRAY_VERTEX] = faces; - mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, array); - - Ref solid_material; - switch (cs->get_operation()) { - case CSGShape3D::OPERATION_UNION: - solid_material = get_material("shape_union_solid_material", p_gizmo); - break; - case CSGShape3D::OPERATION_INTERSECTION: - solid_material = get_material("shape_intersection_solid_material", p_gizmo); - break; - case CSGShape3D::OPERATION_SUBTRACTION: - solid_material = get_material("shape_subtraction_solid_material", p_gizmo); - break; - } - - p_gizmo->add_mesh(mesh, solid_material); - } - - if (Object::cast_to(cs)) { - CSGSphere3D *s = Object::cast_to(cs); - - float r = s->get_radius(); - Vector handles; - handles.push_back(Vector3(r, 0, 0)); - p_gizmo->add_handles(handles, handles_material); - } - - if (Object::cast_to(cs)) { - CSGBox3D *s = Object::cast_to(cs); - - Vector handles; - - for (int i = 0; i < 3; i++) { - Vector3 h; - h[i] = s->get_size()[i] / 2; - handles.push_back(h); - } - - p_gizmo->add_handles(handles, handles_material); - } - - if (Object::cast_to(cs)) { - CSGCylinder3D *s = Object::cast_to(cs); - - Vector handles; - handles.push_back(Vector3(s->get_radius(), 0, 0)); - handles.push_back(Vector3(0, s->get_height() * 0.5, 0)); - p_gizmo->add_handles(handles, handles_material); - } - - if (Object::cast_to(cs)) { - CSGTorus3D *s = Object::cast_to(cs); - - Vector handles; - handles.push_back(Vector3(s->get_inner_radius(), 0, 0)); - handles.push_back(Vector3(s->get_outer_radius(), 0, 0)); - p_gizmo->add_handles(handles, handles_material); - } -} - -EditorPluginCSG::EditorPluginCSG() { - Ref gizmo_plugin = Ref(memnew(CSGShape3DGizmoPlugin)); - Node3DEditor::get_singleton()->add_gizmo_plugin(gizmo_plugin); -} diff --git a/modules/csg/csg_gizmos.h b/modules/csg/csg_gizmos.h deleted file mode 100644 index 43efe57e64..0000000000 --- a/modules/csg/csg_gizmos.h +++ /dev/null @@ -1,63 +0,0 @@ -/*************************************************************************/ -/* csg_gizmos.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#ifndef CSG_GIZMOS_H -#define CSG_GIZMOS_H - -#include "csg_shape.h" -#include "editor/editor_plugin.h" -#include "editor/plugins/node_3d_editor_gizmos.h" - -class CSGShape3DGizmoPlugin : public EditorNode3DGizmoPlugin { - GDCLASS(CSGShape3DGizmoPlugin, EditorNode3DGizmoPlugin); - -public: - virtual bool has_gizmo(Node3D *p_spatial) override; - virtual String get_gizmo_name() const override; - virtual int get_priority() const override; - virtual bool is_selectable_when_hidden() const override; - virtual void redraw(EditorNode3DGizmo *p_gizmo) override; - - virtual String get_handle_name(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const override; - virtual Variant get_handle_value(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const override; - virtual void set_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) override; - virtual void commit_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel) override; - - CSGShape3DGizmoPlugin(); -}; - -class EditorPluginCSG : public EditorPlugin { - GDCLASS(EditorPluginCSG, EditorPlugin); - -public: - EditorPluginCSG(); -}; - -#endif // CSG_GIZMOS_H diff --git a/modules/csg/editor/csg_gizmos.cpp b/modules/csg/editor/csg_gizmos.cpp new file mode 100644 index 0000000000..be29810252 --- /dev/null +++ b/modules/csg/editor/csg_gizmos.cpp @@ -0,0 +1,431 @@ +/*************************************************************************/ +/* csg_gizmos.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#include "csg_gizmos.h" + +#ifdef TOOLS_ENABLED + +#include "editor/editor_settings.h" +#include "editor/plugins/node_3d_editor_plugin.h" +#include "scene/3d/camera_3d.h" + +/////////// + +CSGShape3DGizmoPlugin::CSGShape3DGizmoPlugin() { + Color gizmo_color = EDITOR_DEF("editors/3d_gizmos/gizmo_colors/csg", Color(0.0, 0.4, 1, 0.15)); + create_material("shape_union_material", gizmo_color); + create_material("shape_union_solid_material", gizmo_color); + gizmo_color.invert(); + create_material("shape_subtraction_material", gizmo_color); + create_material("shape_subtraction_solid_material", gizmo_color); + gizmo_color.r = 0.95; + gizmo_color.g = 0.95; + gizmo_color.b = 0.95; + create_material("shape_intersection_material", gizmo_color); + create_material("shape_intersection_solid_material", gizmo_color); + + create_handle_material("handles"); +} + +String CSGShape3DGizmoPlugin::get_handle_name(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const { + CSGShape3D *cs = Object::cast_to(p_gizmo->get_spatial_node()); + + if (Object::cast_to(cs)) { + return "Radius"; + } + + if (Object::cast_to(cs)) { + return "Size"; + } + + if (Object::cast_to(cs)) { + return p_id == 0 ? "Radius" : "Height"; + } + + if (Object::cast_to(cs)) { + return p_id == 0 ? "InnerRadius" : "OuterRadius"; + } + + return ""; +} + +Variant CSGShape3DGizmoPlugin::get_handle_value(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const { + CSGShape3D *cs = Object::cast_to(p_gizmo->get_spatial_node()); + + if (Object::cast_to(cs)) { + CSGSphere3D *s = Object::cast_to(cs); + return s->get_radius(); + } + + if (Object::cast_to(cs)) { + CSGBox3D *s = Object::cast_to(cs); + return s->get_size(); + } + + if (Object::cast_to(cs)) { + CSGCylinder3D *s = Object::cast_to(cs); + return p_id == 0 ? s->get_radius() : s->get_height(); + } + + if (Object::cast_to(cs)) { + CSGTorus3D *s = Object::cast_to(cs); + return p_id == 0 ? s->get_inner_radius() : s->get_outer_radius(); + } + + return Variant(); +} + +void CSGShape3DGizmoPlugin::set_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) { + CSGShape3D *cs = Object::cast_to(p_gizmo->get_spatial_node()); + + Transform3D gt = cs->get_global_transform(); + //gt.orthonormalize(); + Transform3D gi = gt.affine_inverse(); + + Vector3 ray_from = p_camera->project_ray_origin(p_point); + Vector3 ray_dir = p_camera->project_ray_normal(p_point); + + Vector3 sg[2] = { gi.xform(ray_from), gi.xform(ray_from + ray_dir * 16384) }; + + if (Object::cast_to(cs)) { + CSGSphere3D *s = Object::cast_to(cs); + + Vector3 ra, rb; + Geometry3D::get_closest_points_between_segments(Vector3(), Vector3(4096, 0, 0), sg[0], sg[1], ra, rb); + float d = ra.x; + if (Node3DEditor::get_singleton()->is_snap_enabled()) { + d = Math::snapped(d, Node3DEditor::get_singleton()->get_translate_snap()); + } + + if (d < 0.001) { + d = 0.001; + } + + s->set_radius(d); + } + + if (Object::cast_to(cs)) { + CSGBox3D *s = Object::cast_to(cs); + + Vector3 axis; + axis[p_id] = 1.0; + Vector3 ra, rb; + Geometry3D::get_closest_points_between_segments(Vector3(), axis * 4096, sg[0], sg[1], ra, rb); + float d = ra[p_id]; + + if (Math::is_nan(d)) { + // The handle is perpendicular to the camera. + return; + } + + if (Node3DEditor::get_singleton()->is_snap_enabled()) { + d = Math::snapped(d, Node3DEditor::get_singleton()->get_translate_snap()); + } + + if (d < 0.001) { + d = 0.001; + } + + Vector3 h = s->get_size(); + h[p_id] = d * 2; + s->set_size(h); + } + + if (Object::cast_to(cs)) { + CSGCylinder3D *s = Object::cast_to(cs); + + Vector3 axis; + axis[p_id == 0 ? 0 : 1] = 1.0; + Vector3 ra, rb; + Geometry3D::get_closest_points_between_segments(Vector3(), axis * 4096, sg[0], sg[1], ra, rb); + float d = axis.dot(ra); + if (Node3DEditor::get_singleton()->is_snap_enabled()) { + d = Math::snapped(d, Node3DEditor::get_singleton()->get_translate_snap()); + } + + if (d < 0.001) { + d = 0.001; + } + + if (p_id == 0) { + s->set_radius(d); + } else if (p_id == 1) { + s->set_height(d * 2.0); + } + } + + if (Object::cast_to(cs)) { + CSGTorus3D *s = Object::cast_to(cs); + + Vector3 axis; + axis[0] = 1.0; + Vector3 ra, rb; + Geometry3D::get_closest_points_between_segments(Vector3(), axis * 4096, sg[0], sg[1], ra, rb); + float d = axis.dot(ra); + if (Node3DEditor::get_singleton()->is_snap_enabled()) { + d = Math::snapped(d, Node3DEditor::get_singleton()->get_translate_snap()); + } + + if (d < 0.001) { + d = 0.001; + } + + if (p_id == 0) { + s->set_inner_radius(d); + } else if (p_id == 1) { + s->set_outer_radius(d); + } + } +} + +void CSGShape3DGizmoPlugin::commit_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel) { + CSGShape3D *cs = Object::cast_to(p_gizmo->get_spatial_node()); + + if (Object::cast_to(cs)) { + CSGSphere3D *s = Object::cast_to(cs); + if (p_cancel) { + s->set_radius(p_restore); + return; + } + + UndoRedo *ur = Node3DEditor::get_singleton()->get_undo_redo(); + ur->create_action(TTR("Change Sphere Shape Radius")); + ur->add_do_method(s, "set_radius", s->get_radius()); + ur->add_undo_method(s, "set_radius", p_restore); + ur->commit_action(); + } + + if (Object::cast_to(cs)) { + CSGBox3D *s = Object::cast_to(cs); + if (p_cancel) { + s->set_size(p_restore); + return; + } + + UndoRedo *ur = Node3DEditor::get_singleton()->get_undo_redo(); + ur->create_action(TTR("Change Box Shape Size")); + ur->add_do_method(s, "set_size", s->get_size()); + ur->add_undo_method(s, "set_size", p_restore); + ur->commit_action(); + } + + if (Object::cast_to(cs)) { + CSGCylinder3D *s = Object::cast_to(cs); + if (p_cancel) { + if (p_id == 0) { + s->set_radius(p_restore); + } else { + s->set_height(p_restore); + } + return; + } + + UndoRedo *ur = Node3DEditor::get_singleton()->get_undo_redo(); + if (p_id == 0) { + ur->create_action(TTR("Change Cylinder Radius")); + ur->add_do_method(s, "set_radius", s->get_radius()); + ur->add_undo_method(s, "set_radius", p_restore); + } else { + ur->create_action(TTR("Change Cylinder Height")); + ur->add_do_method(s, "set_height", s->get_height()); + ur->add_undo_method(s, "set_height", p_restore); + } + + ur->commit_action(); + } + + if (Object::cast_to(cs)) { + CSGTorus3D *s = Object::cast_to(cs); + if (p_cancel) { + if (p_id == 0) { + s->set_inner_radius(p_restore); + } else { + s->set_outer_radius(p_restore); + } + return; + } + + UndoRedo *ur = Node3DEditor::get_singleton()->get_undo_redo(); + if (p_id == 0) { + ur->create_action(TTR("Change Torus Inner Radius")); + ur->add_do_method(s, "set_inner_radius", s->get_inner_radius()); + ur->add_undo_method(s, "set_inner_radius", p_restore); + } else { + ur->create_action(TTR("Change Torus Outer Radius")); + ur->add_do_method(s, "set_outer_radius", s->get_outer_radius()); + ur->add_undo_method(s, "set_outer_radius", p_restore); + } + + ur->commit_action(); + } +} + +bool CSGShape3DGizmoPlugin::has_gizmo(Node3D *p_spatial) { + return Object::cast_to(p_spatial) || Object::cast_to(p_spatial) || Object::cast_to(p_spatial) || Object::cast_to(p_spatial) || Object::cast_to(p_spatial) || Object::cast_to(p_spatial); +} + +String CSGShape3DGizmoPlugin::get_gizmo_name() const { + return "CSGShape3D"; +} + +int CSGShape3DGizmoPlugin::get_priority() const { + return -1; +} + +bool CSGShape3DGizmoPlugin::is_selectable_when_hidden() const { + return true; +} + +void CSGShape3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) { + p_gizmo->clear(); + + CSGShape3D *cs = Object::cast_to(p_gizmo->get_spatial_node()); + + Vector faces = cs->get_brush_faces(); + + if (faces.size() == 0) { + return; + } + + Vector lines; + lines.resize(faces.size() * 2); + { + const Vector3 *r = faces.ptr(); + + for (int i = 0; i < lines.size(); i += 6) { + int f = i / 6; + for (int j = 0; j < 3; j++) { + int j_n = (j + 1) % 3; + lines.write[i + j * 2 + 0] = r[f * 3 + j]; + lines.write[i + j * 2 + 1] = r[f * 3 + j_n]; + } + } + } + + Ref material; + switch (cs->get_operation()) { + case CSGShape3D::OPERATION_UNION: + material = get_material("shape_union_material", p_gizmo); + break; + case CSGShape3D::OPERATION_INTERSECTION: + material = get_material("shape_intersection_material", p_gizmo); + break; + case CSGShape3D::OPERATION_SUBTRACTION: + material = get_material("shape_subtraction_material", p_gizmo); + break; + } + + Ref handles_material = get_material("handles"); + + p_gizmo->add_lines(lines, material); + p_gizmo->add_collision_segments(lines); + + Array csg_meshes = cs->get_meshes(); + if (csg_meshes.size() != 2) { + return; + } + + Ref csg_mesh = csg_meshes[1]; + if (csg_mesh.is_valid()) { + p_gizmo->add_collision_triangles(csg_mesh->generate_triangle_mesh()); + } + + if (p_gizmo->is_selected()) { + // Draw a translucent representation of the CSG node + Ref mesh = memnew(ArrayMesh); + Array array; + array.resize(Mesh::ARRAY_MAX); + array[Mesh::ARRAY_VERTEX] = faces; + mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, array); + + Ref solid_material; + switch (cs->get_operation()) { + case CSGShape3D::OPERATION_UNION: + solid_material = get_material("shape_union_solid_material", p_gizmo); + break; + case CSGShape3D::OPERATION_INTERSECTION: + solid_material = get_material("shape_intersection_solid_material", p_gizmo); + break; + case CSGShape3D::OPERATION_SUBTRACTION: + solid_material = get_material("shape_subtraction_solid_material", p_gizmo); + break; + } + + p_gizmo->add_mesh(mesh, solid_material); + } + + if (Object::cast_to(cs)) { + CSGSphere3D *s = Object::cast_to(cs); + + float r = s->get_radius(); + Vector handles; + handles.push_back(Vector3(r, 0, 0)); + p_gizmo->add_handles(handles, handles_material); + } + + if (Object::cast_to(cs)) { + CSGBox3D *s = Object::cast_to(cs); + + Vector handles; + + for (int i = 0; i < 3; i++) { + Vector3 h; + h[i] = s->get_size()[i] / 2; + handles.push_back(h); + } + + p_gizmo->add_handles(handles, handles_material); + } + + if (Object::cast_to(cs)) { + CSGCylinder3D *s = Object::cast_to(cs); + + Vector handles; + handles.push_back(Vector3(s->get_radius(), 0, 0)); + handles.push_back(Vector3(0, s->get_height() * 0.5, 0)); + p_gizmo->add_handles(handles, handles_material); + } + + if (Object::cast_to(cs)) { + CSGTorus3D *s = Object::cast_to(cs); + + Vector handles; + handles.push_back(Vector3(s->get_inner_radius(), 0, 0)); + handles.push_back(Vector3(s->get_outer_radius(), 0, 0)); + p_gizmo->add_handles(handles, handles_material); + } +} + +EditorPluginCSG::EditorPluginCSG() { + Ref gizmo_plugin = Ref(memnew(CSGShape3DGizmoPlugin)); + Node3DEditor::get_singleton()->add_gizmo_plugin(gizmo_plugin); +} + +#endif // TOOLS_ENABLED diff --git a/modules/csg/editor/csg_gizmos.h b/modules/csg/editor/csg_gizmos.h new file mode 100644 index 0000000000..fa51010f98 --- /dev/null +++ b/modules/csg/editor/csg_gizmos.h @@ -0,0 +1,67 @@ +/*************************************************************************/ +/* csg_gizmos.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifndef CSG_GIZMOS_H +#define CSG_GIZMOS_H + +#ifdef TOOLS_ENABLED + +#include "../csg_shape.h" +#include "editor/editor_plugin.h" +#include "editor/plugins/node_3d_editor_gizmos.h" + +class CSGShape3DGizmoPlugin : public EditorNode3DGizmoPlugin { + GDCLASS(CSGShape3DGizmoPlugin, EditorNode3DGizmoPlugin); + +public: + virtual bool has_gizmo(Node3D *p_spatial) override; + virtual String get_gizmo_name() const override; + virtual int get_priority() const override; + virtual bool is_selectable_when_hidden() const override; + virtual void redraw(EditorNode3DGizmo *p_gizmo) override; + + virtual String get_handle_name(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const override; + virtual Variant get_handle_value(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const override; + virtual void set_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) override; + virtual void commit_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel) override; + + CSGShape3DGizmoPlugin(); +}; + +class EditorPluginCSG : public EditorPlugin { + GDCLASS(EditorPluginCSG, EditorPlugin); + +public: + EditorPluginCSG(); +}; + +#endif // TOOLS_ENABLED + +#endif // CSG_GIZMOS_H diff --git a/modules/csg/register_types.cpp b/modules/csg/register_types.cpp index f8db42b1a9..72ed027dc9 100644 --- a/modules/csg/register_types.cpp +++ b/modules/csg/register_types.cpp @@ -30,12 +30,15 @@ #include "register_types.h" -#include "csg_gizmos.h" +#ifndef _3D_DISABLED + #include "csg_shape.h" -void register_csg_types() { -#ifndef _3D_DISABLED +#ifdef TOOLS_ENABLED +#include "editor/csg_gizmos.h" +#endif +void register_csg_types() { GDREGISTER_ABSTRACT_CLASS(CSGShape3D); GDREGISTER_ABSTRACT_CLASS(CSGPrimitive3D); GDREGISTER_CLASS(CSGMesh3D); @@ -49,8 +52,9 @@ void register_csg_types() { #ifdef TOOLS_ENABLED EditorPlugins::add_by_type(); #endif -#endif } void unregister_csg_types() { } + +#endif // _3D_DISABLED diff --git a/modules/gltf/SCsub b/modules/gltf/SCsub index 5d03ee8361..3379404a00 100644 --- a/modules/gltf/SCsub +++ b/modules/gltf/SCsub @@ -4,7 +4,8 @@ Import("env") Import("env_modules") env_gltf = env_modules.Clone() -env_gltf.Prepend(CPPPATH=["."]) # Godot's own source files env_gltf.add_source_files(env.modules_sources, "*.cpp") +if env["tools"]: + env_gltf.add_source_files(env.modules_sources, "editor/*.cpp") diff --git a/modules/gltf/editor/editor_scene_exporter_gltf_plugin.cpp b/modules/gltf/editor/editor_scene_exporter_gltf_plugin.cpp new file mode 100644 index 0000000000..23a7b7fed6 --- /dev/null +++ b/modules/gltf/editor/editor_scene_exporter_gltf_plugin.cpp @@ -0,0 +1,109 @@ +/*************************************************************************/ +/* editor_scene_exporter_gltf_plugin.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifdef TOOLS_ENABLED + +#include "editor_scene_exporter_gltf_plugin.h" + +#include "../gltf_document.h" +#include "../gltf_state.h" + +#include "core/config/project_settings.h" +#include "core/error/error_list.h" +#include "core/object/object.h" +#include "core/templates/vector.h" +#include "editor/editor_file_dialog.h" +#include "editor/editor_file_system.h" +#include "editor/editor_node.h" +#include "scene/3d/mesh_instance_3d.h" +#include "scene/gui/check_box.h" +#include "scene/main/node.h" + +String SceneExporterGLTFPlugin::get_name() const { + return "ConvertGLTF2"; +} + +bool SceneExporterGLTFPlugin::has_main_screen() const { + return false; +} + +SceneExporterGLTFPlugin::SceneExporterGLTFPlugin() { + file_export_lib = memnew(EditorFileDialog); + EditorNode::get_singleton()->get_gui_base()->add_child(file_export_lib); + file_export_lib->connect("file_selected", callable_mp(this, &SceneExporterGLTFPlugin::_gltf2_dialog_action)); + file_export_lib->set_title(TTR("Export Library")); + file_export_lib->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE); + file_export_lib->set_access(EditorFileDialog::ACCESS_FILESYSTEM); + file_export_lib->clear_filters(); + file_export_lib->add_filter("*.glb"); + file_export_lib->add_filter("*.gltf"); + file_export_lib->set_title(TTR("Export Mesh GLTF2")); + String gltf_scene_name = TTR("Export GLTF..."); + add_tool_menu_item(gltf_scene_name, callable_mp(this, &SceneExporterGLTFPlugin::convert_scene_to_gltf2)); +} + +void SceneExporterGLTFPlugin::_gltf2_dialog_action(String p_file) { + Node *root = EditorNode::get_singleton()->get_tree()->get_edited_scene_root(); + if (!root) { + EditorNode::get_singleton()->show_accept(TTR("This operation can't be done without a scene."), TTR("OK")); + return; + } + List deps; + Ref doc; + doc.instantiate(); + Ref state; + state.instantiate(); + int32_t flags = 0; + flags |= EditorSceneFormatImporter::IMPORT_USE_NAMED_SKIN_BINDS; + Error err = doc->append_from_scene(root, state, flags, 30.0f); + if (err != OK) { + ERR_PRINT(vformat("glTF2 save scene error %s.", itos(err))); + } + err = doc->write_to_filesystem(state, p_file); + if (err != OK) { + ERR_PRINT(vformat("glTF2 save scene error %s.", itos(err))); + } +} + +void SceneExporterGLTFPlugin::convert_scene_to_gltf2() { + Node *root = EditorNode::get_singleton()->get_tree()->get_edited_scene_root(); + if (!root) { + EditorNode::get_singleton()->show_accept(TTR("This operation can't be done without a scene."), TTR("OK")); + return; + } + String filename = String(root->get_scene_file_path().get_file().get_basename()); + if (filename.is_empty()) { + filename = root->get_name(); + } + file_export_lib->set_current_file(filename + String(".gltf")); + file_export_lib->popup_centered_ratio(); +} + +#endif // TOOLS_ENABLED diff --git a/modules/gltf/editor/editor_scene_exporter_gltf_plugin.h b/modules/gltf/editor/editor_scene_exporter_gltf_plugin.h new file mode 100644 index 0000000000..5af46bc752 --- /dev/null +++ b/modules/gltf/editor/editor_scene_exporter_gltf_plugin.h @@ -0,0 +1,54 @@ +/*************************************************************************/ +/* editor_scene_exporter_gltf_plugin.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifndef EDITOR_SCENE_EXPORTER_GLTF_PLUGIN_H +#define EDITOR_SCENE_EXPORTER_GLTF_PLUGIN_H + +#ifdef TOOLS_ENABLED + +#include "editor/editor_plugin.h" +#include "editor_scene_importer_gltf.h" + +class SceneExporterGLTFPlugin : public EditorPlugin { + GDCLASS(SceneExporterGLTFPlugin, EditorPlugin); + + EditorFileDialog *file_export_lib = nullptr; + void _gltf2_dialog_action(String p_file); + void convert_scene_to_gltf2(); + +public: + virtual String get_name() const override; + bool has_main_screen() const override; + SceneExporterGLTFPlugin(); +}; + +#endif // TOOLS_ENABLED + +#endif // EDITOR_SCENE_EXPORTER_GLTF_PLUGIN_H diff --git a/modules/gltf/editor/editor_scene_importer_gltf.cpp b/modules/gltf/editor/editor_scene_importer_gltf.cpp new file mode 100644 index 0000000000..f9193c2a42 --- /dev/null +++ b/modules/gltf/editor/editor_scene_importer_gltf.cpp @@ -0,0 +1,74 @@ +/*************************************************************************/ +/* editor_scene_importer_gltf.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifdef TOOLS_ENABLED + +#include "editor_scene_importer_gltf.h" + +#include "../gltf_document.h" +#include "../gltf_state.h" + +#include "scene/3d/node_3d.h" +#include "scene/animation/animation_player.h" +#include "scene/resources/animation.h" + +uint32_t EditorSceneFormatImporterGLTF::get_import_flags() const { + return ImportFlags::IMPORT_SCENE | ImportFlags::IMPORT_ANIMATION; +} + +void EditorSceneFormatImporterGLTF::get_extensions(List *r_extensions) const { + r_extensions->push_back("gltf"); + r_extensions->push_back("glb"); +} + +Node *EditorSceneFormatImporterGLTF::import_scene(const String &p_path, + uint32_t p_flags, const Map &p_options, int p_bake_fps, + List *r_missing_deps, + Error *r_err) { + Ref doc; + doc.instantiate(); + Ref state; + state.instantiate(); + Error err = doc->append_from_file(p_path, state, p_flags, p_bake_fps); + if (err != OK) { + *r_err = err; + return nullptr; + } + Node *root = doc->generate_scene(state, p_bake_fps); + return root; +} + +Ref EditorSceneFormatImporterGLTF::import_animation(const String &p_path, + uint32_t p_flags, const Map &p_options, + int p_bake_fps) { + return Ref(); +} + +#endif // TOOLS_ENABLED diff --git a/modules/gltf/editor/editor_scene_importer_gltf.h b/modules/gltf/editor/editor_scene_importer_gltf.h new file mode 100644 index 0000000000..206fe63426 --- /dev/null +++ b/modules/gltf/editor/editor_scene_importer_gltf.h @@ -0,0 +1,58 @@ +/*************************************************************************/ +/* editor_scene_importer_gltf.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifndef EDITOR_SCENE_IMPORTER_GLTF_H +#define EDITOR_SCENE_IMPORTER_GLTF_H + +#ifdef TOOLS_ENABLED + +#include "../gltf_document_extension.h" +#include "../gltf_state.h" + +#include "editor/import/resource_importer_scene.h" +#include "scene/main/node.h" +#include "scene/resources/packed_scene.h" + +class Animation; + +class EditorSceneFormatImporterGLTF : public EditorSceneFormatImporter { + GDCLASS(EditorSceneFormatImporterGLTF, EditorSceneFormatImporter); + +public: + virtual uint32_t get_import_flags() const override; + virtual void get_extensions(List *r_extensions) const override; + virtual Node *import_scene(const String &p_path, uint32_t p_flags, const Map &p_options, int p_bake_fps, List *r_missing_deps, Error *r_err = nullptr) override; + virtual Ref import_animation(const String &p_path, + uint32_t p_flags, const Map &p_options, int p_bake_fps) override; +}; + +#endif // TOOLS_ENABLED + +#endif // EDITOR_SCENE_IMPORTER_GLTF_H diff --git a/modules/gltf/editor_scene_exporter_gltf_plugin.cpp b/modules/gltf/editor_scene_exporter_gltf_plugin.cpp deleted file mode 100644 index 601c70791c..0000000000 --- a/modules/gltf/editor_scene_exporter_gltf_plugin.cpp +++ /dev/null @@ -1,108 +0,0 @@ -/*************************************************************************/ -/* editor_scene_exporter_gltf_plugin.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#if TOOLS_ENABLED -#include "editor_scene_exporter_gltf_plugin.h" - -#include "core/config/project_settings.h" -#include "core/error/error_list.h" -#include "core/object/object.h" -#include "core/templates/vector.h" -#include "editor/editor_file_dialog.h" -#include "editor/editor_file_system.h" -#include "gltf_document.h" -#include "gltf_state.h" -#include "scene/3d/mesh_instance_3d.h" -#include "scene/gui/check_box.h" -#include "scene/main/node.h" - -#include "editor/editor_node.h" - -String SceneExporterGLTFPlugin::get_name() const { - return "ConvertGLTF2"; -} - -bool SceneExporterGLTFPlugin::has_main_screen() const { - return false; -} - -SceneExporterGLTFPlugin::SceneExporterGLTFPlugin() { - file_export_lib = memnew(EditorFileDialog); - EditorNode::get_singleton()->get_gui_base()->add_child(file_export_lib); - file_export_lib->connect("file_selected", callable_mp(this, &SceneExporterGLTFPlugin::_gltf2_dialog_action)); - file_export_lib->set_title(TTR("Export Library")); - file_export_lib->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE); - file_export_lib->set_access(EditorFileDialog::ACCESS_FILESYSTEM); - file_export_lib->clear_filters(); - file_export_lib->add_filter("*.glb"); - file_export_lib->add_filter("*.gltf"); - file_export_lib->set_title(TTR("Export Mesh GLTF2")); - String gltf_scene_name = TTR("Export GLTF..."); - add_tool_menu_item(gltf_scene_name, callable_mp(this, &SceneExporterGLTFPlugin::convert_scene_to_gltf2)); -} - -void SceneExporterGLTFPlugin::_gltf2_dialog_action(String p_file) { - Node *root = EditorNode::get_singleton()->get_tree()->get_edited_scene_root(); - if (!root) { - EditorNode::get_singleton()->show_accept(TTR("This operation can't be done without a scene."), TTR("OK")); - return; - } - List deps; - Ref doc; - doc.instantiate(); - Ref state; - state.instantiate(); - int32_t flags = 0; - flags |= EditorSceneFormatImporter::IMPORT_USE_NAMED_SKIN_BINDS; - Error err = doc->append_from_scene(root, state, flags, 30.0f); - if (err != OK) { - ERR_PRINT(vformat("glTF2 save scene error %s.", itos(err))); - } - err = doc->write_to_filesystem(state, p_file); - if (err != OK) { - ERR_PRINT(vformat("glTF2 save scene error %s.", itos(err))); - } -} - -void SceneExporterGLTFPlugin::convert_scene_to_gltf2() { - Node *root = EditorNode::get_singleton()->get_tree()->get_edited_scene_root(); - if (!root) { - EditorNode::get_singleton()->show_accept(TTR("This operation can't be done without a scene."), TTR("OK")); - return; - } - String filename = String(root->get_scene_file_path().get_file().get_basename()); - if (filename.is_empty()) { - filename = root->get_name(); - } - file_export_lib->set_current_file(filename + String(".gltf")); - file_export_lib->popup_centered_ratio(); -} - -#endif // TOOLS_ENABLED diff --git a/modules/gltf/editor_scene_exporter_gltf_plugin.h b/modules/gltf/editor_scene_exporter_gltf_plugin.h deleted file mode 100644 index c2c3f5710c..0000000000 --- a/modules/gltf/editor_scene_exporter_gltf_plugin.h +++ /dev/null @@ -1,51 +0,0 @@ -/*************************************************************************/ -/* editor_scene_exporter_gltf_plugin.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#ifndef EDITOR_SCENE_EXPORTER_GLTF_PLUGIN_H -#define EDITOR_SCENE_EXPORTER_GLTF_PLUGIN_H - -#if TOOLS_ENABLED -#include "editor/editor_plugin.h" -#include "editor_scene_importer_gltf.h" - -class SceneExporterGLTFPlugin : public EditorPlugin { - GDCLASS(SceneExporterGLTFPlugin, EditorPlugin); - - EditorFileDialog *file_export_lib = nullptr; - void _gltf2_dialog_action(String p_file); - void convert_scene_to_gltf2(); - -public: - virtual String get_name() const override; - bool has_main_screen() const override; - SceneExporterGLTFPlugin(); -}; -#endif // TOOLS_ENABLED -#endif // EDITOR_SCENE_EXPORTER_GLTF_PLUGIN_H diff --git a/modules/gltf/editor_scene_importer_gltf.cpp b/modules/gltf/editor_scene_importer_gltf.cpp deleted file mode 100644 index f063cc1e2b..0000000000 --- a/modules/gltf/editor_scene_importer_gltf.cpp +++ /dev/null @@ -1,73 +0,0 @@ -/*************************************************************************/ -/* editor_scene_importer_gltf.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#if TOOLS_ENABLED -#include "editor_scene_importer_gltf.h" - -#include "gltf_document.h" -#include "gltf_state.h" - -#include "scene/3d/node_3d.h" -#include "scene/animation/animation_player.h" -#include "scene/resources/animation.h" - -uint32_t EditorSceneFormatImporterGLTF::get_import_flags() const { - return ImportFlags::IMPORT_SCENE | ImportFlags::IMPORT_ANIMATION; -} - -void EditorSceneFormatImporterGLTF::get_extensions(List *r_extensions) const { - r_extensions->push_back("gltf"); - r_extensions->push_back("glb"); -} - -Node *EditorSceneFormatImporterGLTF::import_scene(const String &p_path, - uint32_t p_flags, const Map &p_options, int p_bake_fps, - List *r_missing_deps, - Error *r_err) { - Ref doc; - doc.instantiate(); - Ref state; - state.instantiate(); - Error err = doc->append_from_file(p_path, state, p_flags, p_bake_fps); - if (err != OK) { - *r_err = err; - return nullptr; - } - Node *root = doc->generate_scene(state, p_bake_fps); - return root; -} - -Ref EditorSceneFormatImporterGLTF::import_animation(const String &p_path, - uint32_t p_flags, const Map &p_options, - int p_bake_fps) { - return Ref(); -} - -#endif // TOOLS_ENABLED diff --git a/modules/gltf/editor_scene_importer_gltf.h b/modules/gltf/editor_scene_importer_gltf.h deleted file mode 100644 index 4410559b3d..0000000000 --- a/modules/gltf/editor_scene_importer_gltf.h +++ /dev/null @@ -1,55 +0,0 @@ -/*************************************************************************/ -/* editor_scene_importer_gltf.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#ifndef EDITOR_SCENE_IMPORTER_GLTF_H -#define EDITOR_SCENE_IMPORTER_GLTF_H -#ifdef TOOLS_ENABLED -#include "gltf_state.h" - -#include "gltf_document_extension.h" - -#include "editor/import/resource_importer_scene.h" -#include "scene/main/node.h" -#include "scene/resources/packed_scene.h" - -class Animation; - -class EditorSceneFormatImporterGLTF : public EditorSceneFormatImporter { - GDCLASS(EditorSceneFormatImporterGLTF, EditorSceneFormatImporter); - -public: - virtual uint32_t get_import_flags() const override; - virtual void get_extensions(List *r_extensions) const override; - virtual Node *import_scene(const String &p_path, uint32_t p_flags, const Map &p_options, int p_bake_fps, List *r_missing_deps, Error *r_err = nullptr) override; - virtual Ref import_animation(const String &p_path, - uint32_t p_flags, const Map &p_options, int p_bake_fps) override; -}; -#endif // TOOLS_ENABLED -#endif // EDITOR_SCENE_IMPORTER_GLTF_H diff --git a/modules/gltf/register_types.cpp b/modules/gltf/register_types.cpp index 6ab202096d..ef30628dbb 100644 --- a/modules/gltf/register_types.cpp +++ b/modules/gltf/register_types.cpp @@ -30,9 +30,8 @@ #include "register_types.h" -#include "editor/editor_node.h" -#include "editor_scene_exporter_gltf_plugin.h" -#include "editor_scene_importer_gltf.h" +#ifndef _3D_DISABLED + #include "gltf_accessor.h" #include "gltf_animation.h" #include "gltf_buffer_view.h" @@ -49,18 +48,19 @@ #include "gltf_state.h" #include "gltf_texture.h" -#ifndef _3D_DISABLED #ifdef TOOLS_ENABLED +#include "editor/editor_node.h" +#include "editor/editor_scene_exporter_gltf_plugin.h" +#include "editor/editor_scene_importer_gltf.h" + static void _editor_init() { Ref import_gltf; import_gltf.instantiate(); ResourceImporterScene::get_singleton()->add_importer(import_gltf); } #endif -#endif void register_gltf_types() { -#ifndef _3D_DISABLED #ifdef TOOLS_ENABLED ClassDB::APIType prev_api = ClassDB::get_current_api(); ClassDB::set_current_api(ClassDB::API_EDITOR); @@ -84,8 +84,9 @@ void register_gltf_types() { GDREGISTER_CLASS(GLTFDocumentExtensionConvertImporterMesh); GDREGISTER_CLASS(GLTFDocumentExtension); GDREGISTER_CLASS(GLTFDocument); -#endif } void unregister_gltf_types() { } + +#endif // _3D_DISABLED diff --git a/modules/gridmap/SCsub b/modules/gridmap/SCsub index 970ce534f0..52777235b8 100644 --- a/modules/gridmap/SCsub +++ b/modules/gridmap/SCsub @@ -5,4 +5,7 @@ Import("env_modules") env_gridmap = env_modules.Clone() +# Godot's own source files env_gridmap.add_source_files(env.modules_sources, "*.cpp") +if env["tools"]: + env_gridmap.add_source_files(env.modules_sources, "editor/*.cpp") diff --git a/modules/gridmap/editor/grid_map_editor_plugin.cpp b/modules/gridmap/editor/grid_map_editor_plugin.cpp new file mode 100644 index 0000000000..68968325dd --- /dev/null +++ b/modules/gridmap/editor/grid_map_editor_plugin.cpp @@ -0,0 +1,1485 @@ +/*************************************************************************/ +/* grid_map_editor_plugin.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#include "grid_map_editor_plugin.h" + +#ifdef TOOLS_ENABLED + +#include "core/input/input.h" +#include "core/os/keyboard.h" +#include "editor/editor_node.h" +#include "editor/editor_scale.h" +#include "editor/editor_settings.h" +#include "editor/plugins/node_3d_editor_plugin.h" +#include "scene/3d/camera_3d.h" +#include "scene/main/window.h" + +void GridMapEditor::_node_removed(Node *p_node) { + if (p_node == node) { + node = nullptr; + } +} + +void GridMapEditor::_configure() { + if (!node) { + return; + } + + update_grid(); +} + +void GridMapEditor::_menu_option(int p_option) { + switch (p_option) { + case MENU_OPTION_PREV_LEVEL: { + floor->set_value(floor->get_value() - 1); + } break; + + case MENU_OPTION_NEXT_LEVEL: { + floor->set_value(floor->get_value() + 1); + } break; + + case MENU_OPTION_X_AXIS: + case MENU_OPTION_Y_AXIS: + case MENU_OPTION_Z_AXIS: { + int new_axis = p_option - MENU_OPTION_X_AXIS; + for (int i = 0; i < 3; i++) { + int idx = options->get_popup()->get_item_index(MENU_OPTION_X_AXIS + i); + options->get_popup()->set_item_checked(idx, i == new_axis); + } + + if (edit_axis != new_axis) { + int item1 = options->get_popup()->get_item_index(MENU_OPTION_NEXT_LEVEL); + int item2 = options->get_popup()->get_item_index(MENU_OPTION_PREV_LEVEL); + if (edit_axis == Vector3::AXIS_Y) { + options->get_popup()->set_item_text(item1, TTR("Next Plane")); + options->get_popup()->set_item_text(item2, TTR("Previous Plane")); + spin_box_label->set_text(TTR("Plane:")); + } else if (new_axis == Vector3::AXIS_Y) { + options->get_popup()->set_item_text(item1, TTR("Next Floor")); + options->get_popup()->set_item_text(item2, TTR("Previous Floor")); + spin_box_label->set_text(TTR("Floor:")); + } + } + edit_axis = Vector3::Axis(new_axis); + update_grid(); + + } break; + case MENU_OPTION_CURSOR_ROTATE_Y: { + Basis r; + if (input_action == INPUT_PASTE) { + r.set_orthogonal_index(paste_indicator.orientation); + r.rotate(Vector3(0, 1, 0), -Math_PI / 2.0); + paste_indicator.orientation = r.get_orthogonal_index(); + _update_paste_indicator(); + break; + } + + r.set_orthogonal_index(cursor_rot); + r.rotate(Vector3(0, 1, 0), -Math_PI / 2.0); + cursor_rot = r.get_orthogonal_index(); + _update_cursor_transform(); + } break; + case MENU_OPTION_CURSOR_ROTATE_X: { + Basis r; + if (input_action == INPUT_PASTE) { + r.set_orthogonal_index(paste_indicator.orientation); + r.rotate(Vector3(1, 0, 0), -Math_PI / 2.0); + paste_indicator.orientation = r.get_orthogonal_index(); + _update_paste_indicator(); + break; + } + + r.set_orthogonal_index(cursor_rot); + r.rotate(Vector3(1, 0, 0), -Math_PI / 2.0); + cursor_rot = r.get_orthogonal_index(); + _update_cursor_transform(); + } break; + case MENU_OPTION_CURSOR_ROTATE_Z: { + Basis r; + if (input_action == INPUT_PASTE) { + r.set_orthogonal_index(paste_indicator.orientation); + r.rotate(Vector3(0, 0, 1), -Math_PI / 2.0); + paste_indicator.orientation = r.get_orthogonal_index(); + _update_paste_indicator(); + break; + } + + r.set_orthogonal_index(cursor_rot); + r.rotate(Vector3(0, 0, 1), -Math_PI / 2.0); + cursor_rot = r.get_orthogonal_index(); + _update_cursor_transform(); + } break; + case MENU_OPTION_CURSOR_BACK_ROTATE_Y: { + Basis r; + if (input_action == INPUT_PASTE) { + r.set_orthogonal_index(paste_indicator.orientation); + r.rotate(Vector3(0, 1, 0), Math_PI / 2.0); + paste_indicator.orientation = r.get_orthogonal_index(); + _update_paste_indicator(); + break; + } + + r.set_orthogonal_index(cursor_rot); + r.rotate(Vector3(0, 1, 0), Math_PI / 2.0); + cursor_rot = r.get_orthogonal_index(); + _update_cursor_transform(); + } break; + case MENU_OPTION_CURSOR_BACK_ROTATE_X: { + Basis r; + if (input_action == INPUT_PASTE) { + r.set_orthogonal_index(paste_indicator.orientation); + r.rotate(Vector3(1, 0, 0), Math_PI / 2.0); + paste_indicator.orientation = r.get_orthogonal_index(); + _update_paste_indicator(); + break; + } + + r.set_orthogonal_index(cursor_rot); + r.rotate(Vector3(1, 0, 0), Math_PI / 2.0); + cursor_rot = r.get_orthogonal_index(); + _update_cursor_transform(); + } break; + case MENU_OPTION_CURSOR_BACK_ROTATE_Z: { + Basis r; + if (input_action == INPUT_PASTE) { + r.set_orthogonal_index(paste_indicator.orientation); + r.rotate(Vector3(0, 0, 1), Math_PI / 2.0); + paste_indicator.orientation = r.get_orthogonal_index(); + _update_paste_indicator(); + break; + } + + r.set_orthogonal_index(cursor_rot); + r.rotate(Vector3(0, 0, 1), Math_PI / 2.0); + cursor_rot = r.get_orthogonal_index(); + _update_cursor_transform(); + } break; + case MENU_OPTION_CURSOR_CLEAR_ROTATION: { + if (input_action == INPUT_PASTE) { + paste_indicator.orientation = 0; + _update_paste_indicator(); + break; + } + + cursor_rot = 0; + _update_cursor_transform(); + } break; + + case MENU_OPTION_PASTE_SELECTS: { + int idx = options->get_popup()->get_item_index(MENU_OPTION_PASTE_SELECTS); + options->get_popup()->set_item_checked(idx, !options->get_popup()->is_item_checked(idx)); + } break; + + case MENU_OPTION_SELECTION_DUPLICATE: + case MENU_OPTION_SELECTION_CUT: { + if (!(selection.active && input_action == INPUT_NONE)) { + break; + } + + _set_clipboard_data(); + + if (p_option == MENU_OPTION_SELECTION_CUT) { + _delete_selection(); + } + + input_action = INPUT_PASTE; + paste_indicator.click = selection.begin; + paste_indicator.current = selection.begin; + paste_indicator.begin = selection.begin; + paste_indicator.end = selection.end; + paste_indicator.orientation = 0; + _update_paste_indicator(); + } break; + case MENU_OPTION_SELECTION_CLEAR: { + if (!selection.active) { + break; + } + + _delete_selection(); + + } break; + case MENU_OPTION_SELECTION_FILL: { + if (!selection.active) { + return; + } + + _fill_selection(); + + } break; + case MENU_OPTION_GRIDMAP_SETTINGS: { + settings_dialog->popup_centered(settings_vbc->get_combined_minimum_size() + Size2(50, 50) * EDSCALE); + } break; + } +} + +void GridMapEditor::_update_cursor_transform() { + cursor_transform = Transform3D(); + cursor_transform.origin = cursor_origin; + cursor_transform.basis.set_orthogonal_index(cursor_rot); + cursor_transform.basis *= node->get_cell_scale(); + cursor_transform = node->get_global_transform() * cursor_transform; + + if (selected_palette >= 0) { + if (node && !node->get_mesh_library().is_null()) { + cursor_transform *= node->get_mesh_library()->get_item_mesh_transform(selected_palette); + } + } + + if (cursor_instance.is_valid()) { + RenderingServer::get_singleton()->instance_set_transform(cursor_instance, cursor_transform); + RenderingServer::get_singleton()->instance_set_visible(cursor_instance, cursor_visible); + } +} + +void GridMapEditor::_update_selection_transform() { + Transform3D xf_zero; + xf_zero.basis.set_zero(); + + if (!selection.active) { + RenderingServer::get_singleton()->instance_set_transform(selection_instance, xf_zero); + for (int i = 0; i < 3; i++) { + RenderingServer::get_singleton()->instance_set_transform(selection_level_instance[i], xf_zero); + } + return; + } + + Transform3D xf; + xf.scale((Vector3(1, 1, 1) + (selection.end - selection.begin)) * node->get_cell_size()); + xf.origin = selection.begin * node->get_cell_size(); + + RenderingServer::get_singleton()->instance_set_transform(selection_instance, node->get_global_transform() * xf); + + for (int i = 0; i < 3; i++) { + if (i != edit_axis || (edit_floor[edit_axis] < selection.begin[edit_axis]) || (edit_floor[edit_axis] > selection.end[edit_axis] + 1)) { + RenderingServer::get_singleton()->instance_set_transform(selection_level_instance[i], xf_zero); + } else { + Vector3 scale = (selection.end - selection.begin + Vector3(1, 1, 1)); + scale[edit_axis] = 1.0; + Vector3 position = selection.begin; + position[edit_axis] = edit_floor[edit_axis]; + + scale *= node->get_cell_size(); + position *= node->get_cell_size(); + + Transform3D xf2; + xf2.basis.scale(scale); + xf2.origin = position; + + RenderingServer::get_singleton()->instance_set_transform(selection_level_instance[i], xf2); + } + } +} + +void GridMapEditor::_validate_selection() { + if (!selection.active) { + return; + } + selection.begin = selection.click; + selection.end = selection.current; + + if (selection.begin.x > selection.end.x) { + SWAP(selection.begin.x, selection.end.x); + } + if (selection.begin.y > selection.end.y) { + SWAP(selection.begin.y, selection.end.y); + } + if (selection.begin.z > selection.end.z) { + SWAP(selection.begin.z, selection.end.z); + } + + _update_selection_transform(); +} + +void GridMapEditor::_set_selection(bool p_active, const Vector3 &p_begin, const Vector3 &p_end) { + selection.active = p_active; + selection.begin = p_begin; + selection.end = p_end; + selection.click = p_begin; + selection.current = p_end; + + if (is_visible_in_tree()) { + _update_selection_transform(); + } + + options->get_popup()->set_item_disabled(options->get_popup()->get_item_index(MENU_OPTION_SELECTION_CLEAR), !selection.active); + options->get_popup()->set_item_disabled(options->get_popup()->get_item_index(MENU_OPTION_SELECTION_CUT), !selection.active); + options->get_popup()->set_item_disabled(options->get_popup()->get_item_index(MENU_OPTION_SELECTION_DUPLICATE), !selection.active); + options->get_popup()->set_item_disabled(options->get_popup()->get_item_index(MENU_OPTION_SELECTION_FILL), !selection.active); +} + +bool GridMapEditor::do_input_action(Camera3D *p_camera, const Point2 &p_point, bool p_click) { + if (!spatial_editor) { + return false; + } + + if (selected_palette < 0 && input_action != INPUT_PICK && input_action != INPUT_SELECT && input_action != INPUT_PASTE) { + return false; + } + Ref mesh_library = node->get_mesh_library(); + if (mesh_library.is_null()) { + return false; + } + if (input_action != INPUT_PICK && input_action != INPUT_SELECT && input_action != INPUT_PASTE && !mesh_library->has_item(selected_palette)) { + return false; + } + + Camera3D *camera = p_camera; + Vector3 from = camera->project_ray_origin(p_point); + Vector3 normal = camera->project_ray_normal(p_point); + Transform3D local_xform = node->get_global_transform().affine_inverse(); + Vector planes = camera->get_frustum(); + from = local_xform.xform(from); + normal = local_xform.basis.xform(normal).normalized(); + + Plane p; + p.normal[edit_axis] = 1.0; + p.d = edit_floor[edit_axis] * node->get_cell_size()[edit_axis]; + + Vector3 inters; + if (!p.intersects_segment(from, from + normal * settings_pick_distance->get_value(), &inters)) { + return false; + } + + // Make sure the intersection is inside the frustum planes, to avoid + // Painting on invisible regions. + for (int i = 0; i < planes.size(); i++) { + Plane fp = local_xform.xform(planes[i]); + if (fp.is_point_over(inters)) { + return false; + } + } + + int cell[3]; + Vector3 cell_size = node->get_cell_size(); + + for (int i = 0; i < 3; i++) { + if (i == edit_axis) { + cell[i] = edit_floor[i]; + } else { + cell[i] = inters[i] / cell_size[i]; + if (inters[i] < 0) { + cell[i] -= 1; // Compensate negative. + } + grid_ofs[i] = cell[i] * cell_size[i]; + } + } + + RS::get_singleton()->instance_set_transform(grid_instance[edit_axis], node->get_global_transform() * edit_grid_xform); + + if (cursor_instance.is_valid()) { + 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(); + cursor_visible = true; + + if (input_action == INPUT_SELECT || input_action == INPUT_PASTE) { + cursor_visible = false; + } + + _update_cursor_transform(); + } + + if (input_action == INPUT_PASTE) { + paste_indicator.current = Vector3i(cell[0], cell[1], cell[2]); + _update_paste_indicator(); + + } else if (input_action == INPUT_SELECT) { + selection.current = Vector3i(cell[0], cell[1], cell[2]); + if (p_click) { + selection.click = selection.current; + } + selection.active = true; + _validate_selection(); + + return true; + } else if (input_action == INPUT_PICK) { + int item = node->get_cell_item(Vector3i(cell[0], cell[1], cell[2])); + if (item >= 0) { + selected_palette = item; + mesh_library_palette->set_current(item); + update_palette(); + _update_cursor_instance(); + } + return true; + } + + if (input_action == INPUT_PAINT) { + SetItem si; + si.position = Vector3i(cell[0], cell[1], cell[2]); + si.new_value = selected_palette; + si.new_orientation = cursor_rot; + si.old_value = node->get_cell_item(Vector3i(cell[0], cell[1], cell[2])); + si.old_orientation = node->get_cell_item_orientation(Vector3i(cell[0], cell[1], cell[2])); + set_items.push_back(si); + node->set_cell_item(Vector3i(cell[0], cell[1], cell[2]), selected_palette, cursor_rot); + return true; + } else if (input_action == INPUT_ERASE) { + SetItem si; + si.position = Vector3i(cell[0], cell[1], cell[2]); + si.new_value = -1; + si.new_orientation = 0; + si.old_value = node->get_cell_item(Vector3i(cell[0], cell[1], cell[2])); + si.old_orientation = node->get_cell_item_orientation(Vector3i(cell[0], cell[1], cell[2])); + set_items.push_back(si); + node->set_cell_item(Vector3i(cell[0], cell[1], cell[2]), -1); + return true; + } + + return false; +} + +void GridMapEditor::_delete_selection() { + if (!selection.active) { + return; + } + + undo_redo->create_action(TTR("GridMap Delete Selection")); + for (int i = selection.begin.x; i <= selection.end.x; i++) { + for (int j = selection.begin.y; j <= selection.end.y; j++) { + for (int k = selection.begin.z; k <= selection.end.z; k++) { + Vector3i selected = Vector3i(i, j, k); + undo_redo->add_do_method(node, "set_cell_item", selected, GridMap::INVALID_CELL_ITEM); + undo_redo->add_undo_method(node, "set_cell_item", selected, node->get_cell_item(selected), node->get_cell_item_orientation(selected)); + } + } + } + undo_redo->add_do_method(this, "_set_selection", !selection.active, selection.begin, selection.end); + undo_redo->add_undo_method(this, "_set_selection", selection.active, selection.begin, selection.end); + undo_redo->commit_action(); +} + +void GridMapEditor::_fill_selection() { + if (!selection.active) { + return; + } + + undo_redo->create_action(TTR("GridMap Fill Selection")); + for (int i = selection.begin.x; i <= selection.end.x; i++) { + for (int j = selection.begin.y; j <= selection.end.y; j++) { + for (int k = selection.begin.z; k <= selection.end.z; k++) { + Vector3i selected = Vector3i(i, j, k); + undo_redo->add_do_method(node, "set_cell_item", selected, selected_palette, cursor_rot); + undo_redo->add_undo_method(node, "set_cell_item", selected, node->get_cell_item(selected), node->get_cell_item_orientation(selected)); + } + } + } + undo_redo->add_do_method(this, "_set_selection", !selection.active, selection.begin, selection.end); + undo_redo->add_undo_method(this, "_set_selection", selection.active, selection.begin, selection.end); + undo_redo->commit_action(); +} + +void GridMapEditor::_clear_clipboard_data() { + for (const ClipboardItem &E : clipboard_items) { + RenderingServer::get_singleton()->free(E.instance); + } + + clipboard_items.clear(); +} + +void GridMapEditor::_set_clipboard_data() { + _clear_clipboard_data(); + + Ref meshLibrary = node->get_mesh_library(); + + for (int i = selection.begin.x; i <= selection.end.x; i++) { + for (int j = selection.begin.y; j <= selection.end.y; j++) { + for (int k = selection.begin.z; k <= selection.end.z; k++) { + Vector3i selected = Vector3i(i, j, k); + int itm = node->get_cell_item(selected); + if (itm == GridMap::INVALID_CELL_ITEM) { + continue; + } + + Ref mesh = meshLibrary->get_item_mesh(itm); + + ClipboardItem item; + item.cell_item = itm; + item.grid_offset = Vector3(selected) - selection.begin; + item.orientation = node->get_cell_item_orientation(selected); + item.instance = RenderingServer::get_singleton()->instance_create2(mesh->get_rid(), get_tree()->get_root()->get_world_3d()->get_scenario()); + + clipboard_items.push_back(item); + } + } + } +} + +void GridMapEditor::_update_paste_indicator() { + if (input_action != INPUT_PASTE) { + Transform3D xf; + xf.basis.set_zero(); + RenderingServer::get_singleton()->instance_set_transform(paste_instance, xf); + return; + } + + Vector3 center = 0.5 * Vector3(real_t(node->get_center_x()), real_t(node->get_center_y()), real_t(node->get_center_z())); + Vector3 scale = (Vector3(1, 1, 1) + (paste_indicator.end - paste_indicator.begin)) * node->get_cell_size(); + Transform3D xf; + xf.scale(scale); + xf.origin = (paste_indicator.begin + (paste_indicator.current - paste_indicator.click) + center) * node->get_cell_size(); + Basis rot; + rot.set_orthogonal_index(paste_indicator.orientation); + xf.basis = rot * xf.basis; + xf.translate((-center * node->get_cell_size()) / scale); + + RenderingServer::get_singleton()->instance_set_transform(paste_instance, node->get_global_transform() * xf); + + for (const ClipboardItem &item : clipboard_items) { + xf = Transform3D(); + xf.origin = (paste_indicator.begin + (paste_indicator.current - paste_indicator.click) + center) * node->get_cell_size(); + xf.basis = rot * xf.basis; + xf.translate(item.grid_offset * node->get_cell_size()); + + Basis item_rot; + item_rot.set_orthogonal_index(item.orientation); + xf.basis = item_rot * xf.basis * node->get_cell_scale(); + + RenderingServer::get_singleton()->instance_set_transform(item.instance, node->get_global_transform() * xf); + } +} + +void GridMapEditor::_do_paste() { + int idx = options->get_popup()->get_item_index(MENU_OPTION_PASTE_SELECTS); + bool reselect = options->get_popup()->is_item_checked(idx); + + Basis rot; + rot.set_orthogonal_index(paste_indicator.orientation); + + Vector3 ofs = paste_indicator.current - paste_indicator.click; + undo_redo->create_action(TTR("GridMap Paste Selection")); + + for (const ClipboardItem &item : clipboard_items) { + Vector3 position = rot.xform(item.grid_offset) + paste_indicator.begin + ofs; + + Basis orm; + orm.set_orthogonal_index(item.orientation); + orm = rot * orm; + + undo_redo->add_do_method(node, "set_cell_item", position, item.cell_item, orm.get_orthogonal_index()); + undo_redo->add_undo_method(node, "set_cell_item", position, node->get_cell_item(position), node->get_cell_item_orientation(position)); + } + + if (reselect) { + undo_redo->add_do_method(this, "_set_selection", true, paste_indicator.begin + ofs, paste_indicator.end + ofs); + undo_redo->add_undo_method(this, "_set_selection", selection.active, selection.begin, selection.end); + } + + undo_redo->commit_action(); + + _clear_clipboard_data(); +} + +EditorPlugin::AfterGUIInput GridMapEditor::forward_spatial_input_event(Camera3D *p_camera, const Ref &p_event) { + if (!node) { + return EditorPlugin::AFTER_GUI_INPUT_PASS; + } + + Ref mb = p_event; + + if (mb.is_valid()) { + if (mb->get_button_index() == MouseButton::WHEEL_UP && (mb->is_command_pressed() || mb->is_shift_pressed())) { + if (mb->is_pressed()) { + floor->set_value(floor->get_value() + mb->get_factor()); + } + + return EditorPlugin::AFTER_GUI_INPUT_STOP; // Eaten. + } else if (mb->get_button_index() == MouseButton::WHEEL_DOWN && (mb->is_command_pressed() || mb->is_shift_pressed())) { + if (mb->is_pressed()) { + floor->set_value(floor->get_value() - mb->get_factor()); + } + return EditorPlugin::AFTER_GUI_INPUT_STOP; + } + + if (mb->is_pressed()) { + Node3DEditorViewport::NavigationScheme nav_scheme = (Node3DEditorViewport::NavigationScheme)EditorSettings::get_singleton()->get("editors/3d/navigation/navigation_scheme").operator int(); + if ((nav_scheme == Node3DEditorViewport::NAVIGATION_MAYA || nav_scheme == Node3DEditorViewport::NAVIGATION_MODO) && mb->is_alt_pressed()) { + input_action = INPUT_NONE; + } else if (mb->get_button_index() == MouseButton::LEFT) { + bool can_edit = (node && node->get_mesh_library().is_valid()); + if (input_action == INPUT_PASTE) { + _do_paste(); + input_action = INPUT_NONE; + _update_paste_indicator(); + } else if (mb->is_shift_pressed() && can_edit) { + input_action = INPUT_SELECT; + last_selection = selection; + } else if (mb->is_command_pressed() && can_edit) { + input_action = INPUT_PICK; + } else { + input_action = INPUT_PAINT; + set_items.clear(); + } + } else if (mb->get_button_index() == MouseButton::RIGHT) { + if (input_action == INPUT_PASTE) { + _clear_clipboard_data(); + input_action = INPUT_NONE; + _update_paste_indicator(); + return EditorPlugin::AFTER_GUI_INPUT_STOP; + } else if (selection.active) { + _set_selection(false); + return EditorPlugin::AFTER_GUI_INPUT_STOP; + } else { + input_action = INPUT_ERASE; + set_items.clear(); + } + } else { + return EditorPlugin::AFTER_GUI_INPUT_PASS; + } + + if (do_input_action(p_camera, Point2(mb->get_position().x, mb->get_position().y), true)) { + return EditorPlugin::AFTER_GUI_INPUT_STOP; + } + return EditorPlugin::AFTER_GUI_INPUT_PASS; + } else { + if ((mb->get_button_index() == MouseButton::RIGHT && input_action == INPUT_ERASE) || (mb->get_button_index() == MouseButton::LEFT && input_action == INPUT_PAINT)) { + if (set_items.size()) { + undo_redo->create_action(TTR("GridMap Paint")); + for (const SetItem &si : set_items) { + undo_redo->add_do_method(node, "set_cell_item", si.position, si.new_value, si.new_orientation); + } + for (List::Element *E = set_items.back(); E; E = E->prev()) { + const SetItem &si = E->get(); + undo_redo->add_undo_method(node, "set_cell_item", si.position, si.old_value, si.old_orientation); + } + + undo_redo->commit_action(); + } + set_items.clear(); + input_action = INPUT_NONE; + + if (set_items.size() > 0) { + return EditorPlugin::AFTER_GUI_INPUT_STOP; + } + return EditorPlugin::AFTER_GUI_INPUT_PASS; + } + + if (mb->get_button_index() == MouseButton::LEFT && input_action == INPUT_SELECT) { + undo_redo->create_action(TTR("GridMap Selection")); + undo_redo->add_do_method(this, "_set_selection", selection.active, selection.begin, selection.end); + undo_redo->add_undo_method(this, "_set_selection", last_selection.active, last_selection.begin, last_selection.end); + undo_redo->commit_action(); + } + + if (mb->get_button_index() == MouseButton::LEFT && input_action != INPUT_NONE) { + set_items.clear(); + input_action = INPUT_NONE; + return EditorPlugin::AFTER_GUI_INPUT_STOP; + } + if (mb->get_button_index() == MouseButton::RIGHT && (input_action == INPUT_ERASE || input_action == INPUT_PASTE)) { + input_action = INPUT_NONE; + return EditorPlugin::AFTER_GUI_INPUT_STOP; + } + } + } + + Ref mm = p_event; + + if (mm.is_valid()) { + if (do_input_action(p_camera, mm->get_position(), false)) { + return EditorPlugin::AFTER_GUI_INPUT_STOP; + } + return EditorPlugin::AFTER_GUI_INPUT_PASS; + } + + Ref k = p_event; + + if (k.is_valid()) { + if (k->is_pressed()) { + if (k->get_keycode() == Key::ESCAPE) { + if (input_action == INPUT_PASTE) { + _clear_clipboard_data(); + input_action = INPUT_NONE; + _update_paste_indicator(); + return EditorPlugin::AFTER_GUI_INPUT_STOP; + } else if (selection.active) { + _set_selection(false); + return EditorPlugin::AFTER_GUI_INPUT_STOP; + } else { + selected_palette = -1; + mesh_library_palette->deselect_all(); + update_palette(); + _update_cursor_instance(); + return EditorPlugin::AFTER_GUI_INPUT_STOP; + } + } + + if (k->is_shift_pressed() && selection.active && input_action != INPUT_PASTE) { + if (k->get_keycode() == (Key)options->get_popup()->get_item_accelerator(options->get_popup()->get_item_index(MENU_OPTION_PREV_LEVEL))) { + selection.click[edit_axis]--; + _validate_selection(); + return EditorPlugin::AFTER_GUI_INPUT_STOP; + } + if (k->get_keycode() == (Key)options->get_popup()->get_item_accelerator(options->get_popup()->get_item_index(MENU_OPTION_NEXT_LEVEL))) { + selection.click[edit_axis]++; + _validate_selection(); + return EditorPlugin::AFTER_GUI_INPUT_STOP; + } + } + } + } + + Ref pan_gesture = p_event; + if (pan_gesture.is_valid()) { + if (pan_gesture->is_alt_pressed() && (pan_gesture->is_command_pressed() || pan_gesture->is_shift_pressed())) { + const real_t delta = pan_gesture->get_delta().y * 0.5; + accumulated_floor_delta += delta; + int step = 0; + if (ABS(accumulated_floor_delta) > 1.0) { + step = SIGN(accumulated_floor_delta); + accumulated_floor_delta -= step; + } + if (step) { + floor->set_value(floor->get_value() + step); + } + return EditorPlugin::AFTER_GUI_INPUT_STOP; + } + } + accumulated_floor_delta = 0.0; + + return EditorPlugin::AFTER_GUI_INPUT_PASS; +} + +struct _CGMEItemSort { + String name; + int id = 0; + _FORCE_INLINE_ bool operator<(const _CGMEItemSort &r_it) const { return name < r_it.name; } +}; + +void GridMapEditor::_set_display_mode(int p_mode) { + if (display_mode == p_mode) { + return; + } + + if (p_mode == DISPLAY_LIST) { + mode_list->set_pressed(true); + mode_thumbnail->set_pressed(false); + } else if (p_mode == DISPLAY_THUMBNAIL) { + mode_list->set_pressed(false); + mode_thumbnail->set_pressed(true); + } + + display_mode = p_mode; + + update_palette(); +} + +void GridMapEditor::_text_changed(const String &p_text) { + update_palette(); +} + +void GridMapEditor::_sbox_input(const Ref &p_ie) { + const Ref k = p_ie; + + if (k.is_valid() && (k->get_keycode() == Key::UP || k->get_keycode() == Key::DOWN || k->get_keycode() == Key::PAGEUP || k->get_keycode() == Key::PAGEDOWN)) { + // Forward the key input to the ItemList so it can be scrolled + mesh_library_palette->gui_input(k); + search_box->accept_event(); + } +} + +void GridMapEditor::_mesh_library_palette_input(const Ref &p_ie) { + const Ref mb = p_ie; + + // Zoom in/out using Ctrl + mouse wheel + if (mb.is_valid() && mb->is_pressed() && mb->is_command_pressed()) { + if (mb->is_pressed() && mb->get_button_index() == MouseButton::WHEEL_UP) { + size_slider->set_value(size_slider->get_value() + 0.2); + } + + if (mb->is_pressed() && mb->get_button_index() == MouseButton::WHEEL_DOWN) { + size_slider->set_value(size_slider->get_value() - 0.2); + } + } +} + +void GridMapEditor::_icon_size_changed(float p_value) { + mesh_library_palette->set_icon_scale(p_value); + update_palette(); +} + +void GridMapEditor::update_palette() { + int selected = mesh_library_palette->get_current(); + + float min_size = EDITOR_GET("editors/grid_map/preview_size"); + min_size *= EDSCALE; + + mesh_library_palette->clear(); + if (display_mode == DISPLAY_THUMBNAIL) { + mesh_library_palette->set_max_columns(0); + mesh_library_palette->set_icon_mode(ItemList::ICON_MODE_TOP); + mesh_library_palette->set_fixed_column_width(min_size * MAX(size_slider->get_value(), 1.5)); + } else if (display_mode == DISPLAY_LIST) { + mesh_library_palette->set_max_columns(1); + mesh_library_palette->set_icon_mode(ItemList::ICON_MODE_LEFT); + mesh_library_palette->set_fixed_column_width(0); + } + + mesh_library_palette->set_fixed_icon_size(Size2(min_size, min_size)); + mesh_library_palette->set_max_text_lines(2); + + Ref mesh_library = node->get_mesh_library(); + + if (mesh_library.is_null()) { + last_mesh_library = nullptr; + search_box->set_text(""); + search_box->set_editable(false); + info_message->show(); + return; + } + + search_box->set_editable(true); + info_message->hide(); + + Vector ids; + ids = mesh_library->get_item_list(); + + List<_CGMEItemSort> il; + for (int i = 0; i < ids.size(); i++) { + _CGMEItemSort is; + is.id = ids[i]; + is.name = mesh_library->get_item_name(ids[i]); + il.push_back(is); + } + il.sort(); + + String filter = search_box->get_text().strip_edges(); + + int item = 0; + + for (_CGMEItemSort &E : il) { + int id = E.id; + String name = mesh_library->get_item_name(id); + Ref preview = mesh_library->get_item_preview(id); + + if (name.is_empty()) { + name = "#" + itos(id); + } + + if (!filter.is_empty() && !filter.is_subsequence_ofn(name)) { + continue; + } + + mesh_library_palette->add_item(""); + if (!preview.is_null()) { + mesh_library_palette->set_item_icon(item, preview); + mesh_library_palette->set_item_tooltip(item, name); + } + mesh_library_palette->set_item_text(item, name); + mesh_library_palette->set_item_metadata(item, id); + + item++; + } + + if (selected != -1 && mesh_library_palette->get_item_count() > 0) { + mesh_library_palette->select(selected); + } + + last_mesh_library = mesh_library.operator->(); +} + +void GridMapEditor::edit(GridMap *p_gridmap) { + if (!p_gridmap && node) { + node->disconnect("cell_size_changed", callable_mp(this, &GridMapEditor::_draw_grids)); + } + + node = p_gridmap; + + input_action = INPUT_NONE; + selection.active = false; + _update_selection_transform(); + _update_paste_indicator(); + + spatial_editor = Object::cast_to(EditorNode::get_singleton()->get_editor_plugin_screen()); + + if (!node) { + set_process(false); + for (int i = 0; i < 3; i++) { + RenderingServer::get_singleton()->instance_set_visible(grid_instance[i], false); + } + + if (cursor_instance.is_valid()) { + RenderingServer::get_singleton()->instance_set_visible(cursor_instance, false); + } + + return; + } + + update_palette(); + + set_process(true); + + _draw_grids(node->get_cell_size()); + update_grid(); + + node->connect("cell_size_changed", callable_mp(this, &GridMapEditor::_draw_grids)); +} + +void GridMapEditor::update_grid() { + grid_xform.origin.x -= 1; // Force update in hackish way. + + grid_ofs[edit_axis] = edit_floor[edit_axis] * node->get_cell_size()[edit_axis]; + + edit_grid_xform.origin = grid_ofs; + edit_grid_xform.basis = Basis(); + + for (int i = 0; i < 3; i++) { + RenderingServer::get_singleton()->instance_set_visible(grid_instance[i], i == edit_axis); + } + + updating = true; + floor->set_value(edit_floor[edit_axis]); + updating = false; +} + +void GridMapEditor::_draw_grids(const Vector3 &cell_size) { + Vector3 edited_floor = node->has_meta("_editor_floor_") ? node->get_meta("_editor_floor_") : Variant(); + + for (int i = 0; i < 3; i++) { + RS::get_singleton()->mesh_clear(grid[i]); + edit_floor[i] = edited_floor[i]; + } + + Vector grid_points[3]; + Vector grid_colors[3]; + + for (int i = 0; i < 3; i++) { + Vector3 axis; + axis[i] = 1; + Vector3 axis_n1; + axis_n1[(i + 1) % 3] = cell_size[(i + 1) % 3]; + Vector3 axis_n2; + axis_n2[(i + 2) % 3] = cell_size[(i + 2) % 3]; + + for (int j = -GRID_CURSOR_SIZE; j <= GRID_CURSOR_SIZE; j++) { + for (int k = -GRID_CURSOR_SIZE; k <= GRID_CURSOR_SIZE; k++) { + Vector3 p = axis_n1 * j + axis_n2 * k; + float trans = Math::pow(MAX(0, 1.0 - (Vector2(j, k).length() / GRID_CURSOR_SIZE)), 2); + + Vector3 pj = axis_n1 * (j + 1) + axis_n2 * k; + float transj = Math::pow(MAX(0, 1.0 - (Vector2(j + 1, k).length() / GRID_CURSOR_SIZE)), 2); + + Vector3 pk = axis_n1 * j + axis_n2 * (k + 1); + float transk = Math::pow(MAX(0, 1.0 - (Vector2(j, k + 1).length() / GRID_CURSOR_SIZE)), 2); + + grid_points[i].push_back(p); + grid_points[i].push_back(pk); + grid_colors[i].push_back(Color(1, 1, 1, trans)); + grid_colors[i].push_back(Color(1, 1, 1, transk)); + + grid_points[i].push_back(p); + grid_points[i].push_back(pj); + grid_colors[i].push_back(Color(1, 1, 1, trans)); + grid_colors[i].push_back(Color(1, 1, 1, transj)); + } + } + + Array d; + d.resize(RS::ARRAY_MAX); + d[RS::ARRAY_VERTEX] = grid_points[i]; + d[RS::ARRAY_COLOR] = grid_colors[i]; + RenderingServer::get_singleton()->mesh_add_surface_from_arrays(grid[i], RenderingServer::PRIMITIVE_LINES, d); + RenderingServer::get_singleton()->mesh_surface_set_material(grid[i], 0, indicator_mat->get_rid()); + } +} + +void GridMapEditor::_update_theme() { + options->set_icon(get_theme_icon(SNAME("GridMap"), SNAME("EditorIcons"))); + search_box->set_right_icon(get_theme_icon(SNAME("Search"), SNAME("EditorIcons"))); + mode_thumbnail->set_icon(get_theme_icon(SNAME("FileThumbnail"), SNAME("EditorIcons"))); + mode_list->set_icon(get_theme_icon(SNAME("FileList"), SNAME("EditorIcons"))); +} + +void GridMapEditor::_notification(int p_what) { + switch (p_what) { + case NOTIFICATION_ENTER_TREE: { + get_tree()->connect("node_removed", callable_mp(this, &GridMapEditor::_node_removed)); + mesh_library_palette->connect("item_selected", callable_mp(this, &GridMapEditor::_item_selected_cbk)); + for (int i = 0; i < 3; i++) { + grid[i] = RS::get_singleton()->mesh_create(); + grid_instance[i] = RS::get_singleton()->instance_create2(grid[i], get_tree()->get_root()->get_world_3d()->get_scenario()); + RenderingServer::get_singleton()->instance_set_layer_mask(grid_instance[i], 1 << Node3DEditorViewport::MISC_TOOL_LAYER); + selection_level_instance[i] = RenderingServer::get_singleton()->instance_create2(selection_level_mesh[i], get_tree()->get_root()->get_world_3d()->get_scenario()); + RenderingServer::get_singleton()->instance_set_layer_mask(selection_level_instance[i], 1 << Node3DEditorViewport::MISC_TOOL_LAYER); + } + + selection_instance = RenderingServer::get_singleton()->instance_create2(selection_mesh, get_tree()->get_root()->get_world_3d()->get_scenario()); + RenderingServer::get_singleton()->instance_set_layer_mask(selection_instance, 1 << Node3DEditorViewport::MISC_TOOL_LAYER); + paste_instance = RenderingServer::get_singleton()->instance_create2(paste_mesh, get_tree()->get_root()->get_world_3d()->get_scenario()); + RenderingServer::get_singleton()->instance_set_layer_mask(paste_instance, 1 << Node3DEditorViewport::MISC_TOOL_LAYER); + + _update_selection_transform(); + _update_paste_indicator(); + _update_theme(); + } break; + + case NOTIFICATION_EXIT_TREE: { + get_tree()->disconnect("node_removed", callable_mp(this, &GridMapEditor::_node_removed)); + _clear_clipboard_data(); + + for (int i = 0; i < 3; i++) { + RS::get_singleton()->free(grid_instance[i]); + RS::get_singleton()->free(grid[i]); + grid_instance[i] = RID(); + grid[i] = RID(); + RenderingServer::get_singleton()->free(selection_level_instance[i]); + } + + RenderingServer::get_singleton()->free(selection_instance); + RenderingServer::get_singleton()->free(paste_instance); + selection_instance = RID(); + paste_instance = RID(); + } break; + + case NOTIFICATION_PROCESS: { + if (!node) { + return; + } + + Transform3D xf = node->get_global_transform(); + + if (xf != grid_xform) { + for (int i = 0; i < 3; i++) { + RS::get_singleton()->instance_set_transform(grid_instance[i], xf * edit_grid_xform); + } + grid_xform = xf; + } + Ref cgmt = node->get_mesh_library(); + if (cgmt.operator->() != last_mesh_library) { + update_palette(); + } + } break; + + case NOTIFICATION_THEME_CHANGED: { + _update_theme(); + } break; + + case NOTIFICATION_APPLICATION_FOCUS_OUT: { + if (input_action == INPUT_PAINT) { + // Simulate mouse released event to stop drawing when editor focus exists. + Ref release; + release.instantiate(); + release->set_button_index(MouseButton::LEFT); + forward_spatial_input_event(nullptr, release); + } + } break; + } +} + +void GridMapEditor::_update_cursor_instance() { + if (!node) { + return; + } + + if (cursor_instance.is_valid()) { + RenderingServer::get_singleton()->free(cursor_instance); + } + cursor_instance = RID(); + + if (selected_palette >= 0) { + if (node && !node->get_mesh_library().is_null()) { + Ref mesh = node->get_mesh_library()->get_item_mesh(selected_palette); + if (!mesh.is_null() && mesh->get_rid().is_valid()) { + cursor_instance = RenderingServer::get_singleton()->instance_create2(mesh->get_rid(), get_tree()->get_root()->get_world_3d()->get_scenario()); + RenderingServer::get_singleton()->instance_set_transform(cursor_instance, cursor_transform); + } + } + } +} + +void GridMapEditor::_item_selected_cbk(int idx) { + selected_palette = mesh_library_palette->get_item_metadata(idx); + + _update_cursor_instance(); +} + +void GridMapEditor::_floor_changed(float p_value) { + if (updating) { + return; + } + + edit_floor[edit_axis] = p_value; + node->set_meta("_editor_floor_", Vector3(edit_floor[0], edit_floor[1], edit_floor[2])); + update_grid(); + _update_selection_transform(); +} + +void GridMapEditor::_floor_mouse_exited() { + floor->get_line_edit()->release_focus(); +} + +void GridMapEditor::_bind_methods() { + ClassDB::bind_method("_configure", &GridMapEditor::_configure); + ClassDB::bind_method("_set_selection", &GridMapEditor::_set_selection); +} + +GridMapEditor::GridMapEditor() { + undo_redo = EditorNode::get_singleton()->get_undo_redo(); + + int mw = EDITOR_DEF("editors/grid_map/palette_min_width", 230); + Control *ec = memnew(Control); + ec->set_custom_minimum_size(Size2(mw, 0) * EDSCALE); + add_child(ec); + + spatial_editor_hb = memnew(HBoxContainer); + spatial_editor_hb->set_h_size_flags(SIZE_EXPAND_FILL); + spatial_editor_hb->set_alignment(BoxContainer::ALIGNMENT_END); + Node3DEditor::get_singleton()->add_control_to_menu_panel(spatial_editor_hb); + + spin_box_label = memnew(Label); + spin_box_label->set_text(TTR("Floor:")); + spatial_editor_hb->add_child(spin_box_label); + + floor = memnew(SpinBox); + floor->set_min(-32767); + floor->set_max(32767); + floor->set_step(1); + floor->get_line_edit()->add_theme_constant_override("minimum_character_width", 16); + + spatial_editor_hb->add_child(floor); + floor->connect("value_changed", callable_mp(this, &GridMapEditor::_floor_changed)); + floor->connect("mouse_exited", callable_mp(this, &GridMapEditor::_floor_mouse_exited)); + floor->get_line_edit()->connect("mouse_exited", callable_mp(this, &GridMapEditor::_floor_mouse_exited)); + + spatial_editor_hb->add_child(memnew(VSeparator)); + + options = memnew(MenuButton); + spatial_editor_hb->add_child(options); + spatial_editor_hb->hide(); + + options->set_text(TTR("Grid Map")); + options->get_popup()->add_item(TTR("Previous Floor"), MENU_OPTION_PREV_LEVEL, Key::Q); + options->get_popup()->add_item(TTR("Next Floor"), MENU_OPTION_NEXT_LEVEL, Key::E); + options->get_popup()->add_separator(); + options->get_popup()->add_radio_check_item(TTR("Edit X Axis"), MENU_OPTION_X_AXIS, Key::Z); + options->get_popup()->add_radio_check_item(TTR("Edit Y Axis"), MENU_OPTION_Y_AXIS, Key::X); + options->get_popup()->add_radio_check_item(TTR("Edit Z Axis"), MENU_OPTION_Z_AXIS, Key::C); + options->get_popup()->set_item_checked(options->get_popup()->get_item_index(MENU_OPTION_Y_AXIS), true); + options->get_popup()->add_separator(); + options->get_popup()->add_item(TTR("Cursor Rotate X"), MENU_OPTION_CURSOR_ROTATE_X, Key::A); + options->get_popup()->add_item(TTR("Cursor Rotate Y"), MENU_OPTION_CURSOR_ROTATE_Y, Key::S); + options->get_popup()->add_item(TTR("Cursor Rotate Z"), MENU_OPTION_CURSOR_ROTATE_Z, Key::D); + options->get_popup()->add_item(TTR("Cursor Back Rotate X"), MENU_OPTION_CURSOR_BACK_ROTATE_X, KeyModifierMask::SHIFT + Key::A); + options->get_popup()->add_item(TTR("Cursor Back Rotate Y"), MENU_OPTION_CURSOR_BACK_ROTATE_Y, KeyModifierMask::SHIFT + Key::S); + options->get_popup()->add_item(TTR("Cursor Back Rotate Z"), MENU_OPTION_CURSOR_BACK_ROTATE_Z, KeyModifierMask::SHIFT + Key::D); + options->get_popup()->add_item(TTR("Cursor Clear Rotation"), MENU_OPTION_CURSOR_CLEAR_ROTATION, Key::W); + options->get_popup()->add_separator(); + options->get_popup()->add_check_item(TTR("Paste Selects"), MENU_OPTION_PASTE_SELECTS); + options->get_popup()->add_separator(); + options->get_popup()->add_item(TTR("Duplicate Selection"), MENU_OPTION_SELECTION_DUPLICATE, KeyModifierMask::CTRL + Key::C); + options->get_popup()->add_item(TTR("Cut Selection"), MENU_OPTION_SELECTION_CUT, KeyModifierMask::CTRL + Key::X); + options->get_popup()->add_item(TTR("Clear Selection"), MENU_OPTION_SELECTION_CLEAR, Key::KEY_DELETE); + options->get_popup()->add_item(TTR("Fill Selection"), MENU_OPTION_SELECTION_FILL, KeyModifierMask::CTRL + Key::F); + + options->get_popup()->add_separator(); + options->get_popup()->add_item(TTR("Settings..."), MENU_OPTION_GRIDMAP_SETTINGS); + + settings_dialog = memnew(ConfirmationDialog); + settings_dialog->set_title(TTR("GridMap Settings")); + add_child(settings_dialog); + settings_vbc = memnew(VBoxContainer); + settings_vbc->set_custom_minimum_size(Size2(200, 0) * EDSCALE); + settings_dialog->add_child(settings_vbc); + + settings_pick_distance = memnew(SpinBox); + settings_pick_distance->set_max(10000.0f); + settings_pick_distance->set_min(500.0f); + settings_pick_distance->set_step(1.0f); + settings_pick_distance->set_value(EDITOR_GET("editors/grid_map/pick_distance")); + settings_vbc->add_margin_child(TTR("Pick Distance:"), settings_pick_distance); + + options->get_popup()->connect("id_pressed", callable_mp(this, &GridMapEditor::_menu_option)); + + HBoxContainer *hb = memnew(HBoxContainer); + add_child(hb); + hb->set_h_size_flags(SIZE_EXPAND_FILL); + + search_box = memnew(LineEdit); + search_box->set_h_size_flags(SIZE_EXPAND_FILL); + search_box->set_placeholder(TTR("Filter meshes")); + hb->add_child(search_box); + search_box->connect("text_changed", callable_mp(this, &GridMapEditor::_text_changed)); + search_box->connect("gui_input", callable_mp(this, &GridMapEditor::_sbox_input)); + + mode_thumbnail = memnew(Button); + mode_thumbnail->set_flat(true); + mode_thumbnail->set_toggle_mode(true); + mode_thumbnail->set_pressed(true); + hb->add_child(mode_thumbnail); + mode_thumbnail->connect("pressed", callable_mp(this, &GridMapEditor::_set_display_mode), varray(DISPLAY_THUMBNAIL)); + + mode_list = memnew(Button); + mode_list->set_flat(true); + mode_list->set_toggle_mode(true); + mode_list->set_pressed(false); + hb->add_child(mode_list); + mode_list->connect("pressed", callable_mp(this, &GridMapEditor::_set_display_mode), varray(DISPLAY_LIST)); + + size_slider = memnew(HSlider); + size_slider->set_h_size_flags(SIZE_EXPAND_FILL); + size_slider->set_min(0.2f); + size_slider->set_max(4.0f); + size_slider->set_step(0.1f); + size_slider->set_value(1.0f); + size_slider->connect("value_changed", callable_mp(this, &GridMapEditor::_icon_size_changed)); + add_child(size_slider); + + EDITOR_DEF("editors/grid_map/preview_size", 64); + + mesh_library_palette = memnew(ItemList); + add_child(mesh_library_palette); + mesh_library_palette->set_v_size_flags(SIZE_EXPAND_FILL); + mesh_library_palette->connect("gui_input", callable_mp(this, &GridMapEditor::_mesh_library_palette_input)); + + info_message = memnew(Label); + info_message->set_text(TTR("Give a MeshLibrary resource to this GridMap to use its meshes.")); + info_message->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER); + info_message->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER); + info_message->set_autowrap_mode(Label::AUTOWRAP_WORD_SMART); + info_message->set_custom_minimum_size(Size2(100 * EDSCALE, 0)); + info_message->set_anchors_and_offsets_preset(PRESET_WIDE, PRESET_MODE_KEEP_SIZE, 8 * EDSCALE); + mesh_library_palette->add_child(info_message); + + edit_axis = Vector3::AXIS_Y; + edit_floor[0] = -1; + edit_floor[1] = -1; + edit_floor[2] = -1; + + selection_mesh = RenderingServer::get_singleton()->mesh_create(); + paste_mesh = RenderingServer::get_singleton()->mesh_create(); + + { + // Selection mesh create. + + Vector lines; + Vector triangles; + Vector square[3]; + + for (int i = 0; i < 6; i++) { + Vector3 face_points[4]; + + for (int j = 0; j < 4; j++) { + float v[3]; + v[0] = 1.0; + v[1] = 1 - 2 * ((j >> 1) & 1); + v[2] = v[1] * (1 - 2 * (j & 1)); + + for (int k = 0; k < 3; k++) { + if (i < 3) { + face_points[j][(i + k) % 3] = v[k]; + } else { + face_points[3 - j][(i + k) % 3] = -v[k]; + } + } + } + + triangles.push_back(face_points[0] * 0.5 + Vector3(0.5, 0.5, 0.5)); + triangles.push_back(face_points[1] * 0.5 + Vector3(0.5, 0.5, 0.5)); + triangles.push_back(face_points[2] * 0.5 + Vector3(0.5, 0.5, 0.5)); + + triangles.push_back(face_points[2] * 0.5 + Vector3(0.5, 0.5, 0.5)); + triangles.push_back(face_points[3] * 0.5 + Vector3(0.5, 0.5, 0.5)); + triangles.push_back(face_points[0] * 0.5 + Vector3(0.5, 0.5, 0.5)); + } + + for (int i = 0; i < 12; i++) { + AABB base(Vector3(0, 0, 0), Vector3(1, 1, 1)); + Vector3 a, b; + base.get_edge(i, a, b); + lines.push_back(a); + lines.push_back(b); + } + + for (int i = 0; i < 3; i++) { + Vector3 points[4]; + for (int j = 0; j < 4; j++) { + static const bool orderx[4] = { false, true, true, false }; + static const bool ordery[4] = { false, false, true, true }; + + Vector3 sp; + if (orderx[j]) { + sp[(i + 1) % 3] = 1.0; + } + if (ordery[j]) { + sp[(i + 2) % 3] = 1.0; + } + + points[j] = sp; + } + + for (int j = 0; j < 4; j++) { + Vector3 ofs; + ofs[i] += 0.01; + square[i].push_back(points[j] - ofs); + square[i].push_back(points[(j + 1) % 4] - ofs); + square[i].push_back(points[j] + ofs); + square[i].push_back(points[(j + 1) % 4] + ofs); + } + } + + Array d; + d.resize(RS::ARRAY_MAX); + + inner_mat.instantiate(); + inner_mat->set_albedo(Color(0.7, 0.7, 1.0, 0.2)); + inner_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED); + inner_mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA); + + d[RS::ARRAY_VERTEX] = triangles; + RenderingServer::get_singleton()->mesh_add_surface_from_arrays(selection_mesh, RS::PRIMITIVE_TRIANGLES, d); + RenderingServer::get_singleton()->mesh_surface_set_material(selection_mesh, 0, inner_mat->get_rid()); + + outer_mat.instantiate(); + outer_mat->set_albedo(Color(0.7, 0.7, 1.0, 0.8)); + outer_mat->set_on_top_of_alpha(); + + outer_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED); + outer_mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA); + + selection_floor_mat.instantiate(); + selection_floor_mat->set_albedo(Color(0.80, 0.80, 1.0, 1)); + selection_floor_mat->set_on_top_of_alpha(); + selection_floor_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED); + + d[RS::ARRAY_VERTEX] = lines; + RenderingServer::get_singleton()->mesh_add_surface_from_arrays(selection_mesh, RS::PRIMITIVE_LINES, d); + RenderingServer::get_singleton()->mesh_surface_set_material(selection_mesh, 1, outer_mat->get_rid()); + + d[RS::ARRAY_VERTEX] = triangles; + RenderingServer::get_singleton()->mesh_add_surface_from_arrays(paste_mesh, RS::PRIMITIVE_TRIANGLES, d); + RenderingServer::get_singleton()->mesh_surface_set_material(paste_mesh, 0, inner_mat->get_rid()); + + d[RS::ARRAY_VERTEX] = lines; + RenderingServer::get_singleton()->mesh_add_surface_from_arrays(paste_mesh, RS::PRIMITIVE_LINES, d); + RenderingServer::get_singleton()->mesh_surface_set_material(paste_mesh, 1, outer_mat->get_rid()); + + for (int i = 0; i < 3; i++) { + d[RS::ARRAY_VERTEX] = square[i]; + selection_level_mesh[i] = RS::get_singleton()->mesh_create(); + RenderingServer::get_singleton()->mesh_add_surface_from_arrays(selection_level_mesh[i], RS::PRIMITIVE_LINES, d); + RenderingServer::get_singleton()->mesh_surface_set_material(selection_level_mesh[i], 0, selection_floor_mat->get_rid()); + } + } + + _set_selection(false); + + indicator_mat.instantiate(); + indicator_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED); + indicator_mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA); + indicator_mat->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true); + indicator_mat->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true); + indicator_mat->set_albedo(Color(0.8, 0.5, 0.1)); +} + +GridMapEditor::~GridMapEditor() { + _clear_clipboard_data(); + + for (int i = 0; i < 3; i++) { + if (grid[i].is_valid()) { + RenderingServer::get_singleton()->free(grid[i]); + } + if (grid_instance[i].is_valid()) { + RenderingServer::get_singleton()->free(grid_instance[i]); + } + if (cursor_instance.is_valid()) { + RenderingServer::get_singleton()->free(cursor_instance); + } + if (selection_level_instance[i].is_valid()) { + RenderingServer::get_singleton()->free(selection_level_instance[i]); + } + if (selection_level_mesh[i].is_valid()) { + RenderingServer::get_singleton()->free(selection_level_mesh[i]); + } + } + + RenderingServer::get_singleton()->free(selection_mesh); + if (selection_instance.is_valid()) { + RenderingServer::get_singleton()->free(selection_instance); + } + + RenderingServer::get_singleton()->free(paste_mesh); + if (paste_instance.is_valid()) { + RenderingServer::get_singleton()->free(paste_instance); + } +} + +void GridMapEditorPlugin::_notification(int p_what) { + switch (p_what) { + case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: { + switch ((int)EditorSettings::get_singleton()->get("editors/grid_map/editor_side")) { + case 0: { // Left. + Node3DEditor::get_singleton()->move_control_to_left_panel(grid_map_editor); + } break; + case 1: { // Right. + Node3DEditor::get_singleton()->move_control_to_right_panel(grid_map_editor); + } break; + } + } break; + } +} + +void GridMapEditorPlugin::edit(Object *p_object) { + grid_map_editor->edit(Object::cast_to(p_object)); +} + +bool GridMapEditorPlugin::handles(Object *p_object) const { + return p_object->is_class("GridMap"); +} + +void GridMapEditorPlugin::make_visible(bool p_visible) { + if (p_visible) { + grid_map_editor->show(); + grid_map_editor->spatial_editor_hb->show(); + grid_map_editor->set_process(true); + } else { + grid_map_editor->spatial_editor_hb->hide(); + grid_map_editor->hide(); + grid_map_editor->edit(nullptr); + grid_map_editor->set_process(false); + } +} + +GridMapEditorPlugin::GridMapEditorPlugin() { + EDITOR_DEF("editors/grid_map/editor_side", 1); + EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::INT, "editors/grid_map/editor_side", PROPERTY_HINT_ENUM, "Left,Right")); + + grid_map_editor = memnew(GridMapEditor); + switch ((int)EditorSettings::get_singleton()->get("editors/grid_map/editor_side")) { + case 0: { // Left. + Node3DEditor::get_singleton()->add_control_to_left_panel(grid_map_editor); + } break; + case 1: { // Right. + Node3DEditor::get_singleton()->add_control_to_right_panel(grid_map_editor); + } break; + } + grid_map_editor->hide(); +} + +GridMapEditorPlugin::~GridMapEditorPlugin() { +} + +#endif // TOOLS_ENABLED diff --git a/modules/gridmap/editor/grid_map_editor_plugin.h b/modules/gridmap/editor/grid_map_editor_plugin.h new file mode 100644 index 0000000000..adb0da6e2c --- /dev/null +++ b/modules/gridmap/editor/grid_map_editor_plugin.h @@ -0,0 +1,256 @@ +/*************************************************************************/ +/* grid_map_editor_plugin.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifndef GRID_MAP_EDITOR_PLUGIN_H +#define GRID_MAP_EDITOR_PLUGIN_H + +#ifdef TOOLS_ENABLED + +#include "../grid_map.h" +#include "editor/editor_plugin.h" +#include "scene/gui/item_list.h" +#include "scene/gui/slider.h" +#include "scene/gui/spin_box.h" + +class Node3DEditorPlugin; + +class GridMapEditor : public VBoxContainer { + GDCLASS(GridMapEditor, VBoxContainer); + + enum { + GRID_CURSOR_SIZE = 50 + }; + + enum InputAction { + INPUT_NONE, + INPUT_PAINT, + INPUT_ERASE, + INPUT_PICK, + INPUT_SELECT, + INPUT_PASTE, + }; + + enum DisplayMode { + DISPLAY_THUMBNAIL, + DISPLAY_LIST + }; + + UndoRedo *undo_redo; + InputAction input_action = INPUT_NONE; + Panel *panel; + MenuButton *options; + SpinBox *floor; + double accumulated_floor_delta = 0.0; + Button *mode_thumbnail; + Button *mode_list; + LineEdit *search_box; + HSlider *size_slider; + HBoxContainer *spatial_editor_hb; + ConfirmationDialog *settings_dialog; + VBoxContainer *settings_vbc; + SpinBox *settings_pick_distance; + Label *spin_box_label; + + struct SetItem { + Vector3i position; + int new_value = 0; + int new_orientation = 0; + int old_value = 0; + int old_orientation = 0; + }; + + List set_items; + + GridMap *node = nullptr; + MeshLibrary *last_mesh_library; + + Transform3D grid_xform; + Transform3D edit_grid_xform; + Vector3::Axis edit_axis; + int edit_floor[3]; + Vector3 grid_ofs; + + RID grid[3]; + RID grid_instance[3]; + RID cursor_instance; + RID selection_mesh; + RID selection_instance; + RID selection_level_mesh[3]; + RID selection_level_instance[3]; + RID paste_mesh; + RID paste_instance; + + struct ClipboardItem { + int cell_item = 0; + Vector3 grid_offset; + int orientation = 0; + RID instance; + }; + + List clipboard_items; + + Ref indicator_mat; + Ref inner_mat; + Ref outer_mat; + Ref selection_floor_mat; + + bool updating = false; + + struct Selection { + Vector3 click; + Vector3 current; + Vector3 begin; + Vector3 end; + bool active = false; + } selection; + Selection last_selection; + + struct PasteIndicator { + Vector3 click; + Vector3 current; + Vector3 begin; + Vector3 end; + int orientation = 0; + }; + PasteIndicator paste_indicator; + + bool cursor_visible = false; + Transform3D cursor_transform; + + Vector3 cursor_origin; + + int display_mode = DISPLAY_THUMBNAIL; + int selected_palette = -1; + int cursor_rot = 0; + + enum Menu { + MENU_OPTION_NEXT_LEVEL, + MENU_OPTION_PREV_LEVEL, + MENU_OPTION_LOCK_VIEW, + MENU_OPTION_X_AXIS, + MENU_OPTION_Y_AXIS, + MENU_OPTION_Z_AXIS, + MENU_OPTION_CURSOR_ROTATE_Y, + MENU_OPTION_CURSOR_ROTATE_X, + MENU_OPTION_CURSOR_ROTATE_Z, + MENU_OPTION_CURSOR_BACK_ROTATE_Y, + MENU_OPTION_CURSOR_BACK_ROTATE_X, + MENU_OPTION_CURSOR_BACK_ROTATE_Z, + MENU_OPTION_CURSOR_CLEAR_ROTATION, + MENU_OPTION_PASTE_SELECTS, + MENU_OPTION_SELECTION_DUPLICATE, + MENU_OPTION_SELECTION_CUT, + MENU_OPTION_SELECTION_CLEAR, + MENU_OPTION_SELECTION_FILL, + MENU_OPTION_GRIDMAP_SETTINGS + + }; + + Node3DEditorPlugin *spatial_editor; + + struct AreaDisplay { + RID mesh; + RID instance; + }; + + ItemList *mesh_library_palette; + Label *info_message; + + void update_grid(); // Change which and where the grid is displayed + void _draw_grids(const Vector3 &cell_size); + void _configure(); + void _menu_option(int); + void update_palette(); + void _set_display_mode(int p_mode); + void _item_selected_cbk(int idx); + void _update_cursor_transform(); + void _update_cursor_instance(); + void _update_theme(); + + void _text_changed(const String &p_text); + void _sbox_input(const Ref &p_ie); + void _mesh_library_palette_input(const Ref &p_ie); + + void _icon_size_changed(float p_value); + + void _clear_clipboard_data(); + void _set_clipboard_data(); + void _update_paste_indicator(); + void _do_paste(); + void _update_selection_transform(); + void _validate_selection(); + void _set_selection(bool p_active, const Vector3 &p_begin = Vector3(), const Vector3 &p_end = Vector3()); + + void _floor_changed(float p_value); + void _floor_mouse_exited(); + + void _delete_selection(); + void _fill_selection(); + + bool do_input_action(Camera3D *p_camera, const Point2 &p_point, bool p_click); + + friend class GridMapEditorPlugin; + +protected: + void _notification(int p_what); + void _node_removed(Node *p_node); + static void _bind_methods(); + +public: + EditorPlugin::AfterGUIInput forward_spatial_input_event(Camera3D *p_camera, const Ref &p_event); + + void edit(GridMap *p_gridmap); + GridMapEditor(); + ~GridMapEditor(); +}; + +class GridMapEditorPlugin : public EditorPlugin { + GDCLASS(GridMapEditorPlugin, EditorPlugin); + + GridMapEditor *grid_map_editor; + +protected: + void _notification(int p_what); + +public: + virtual EditorPlugin::AfterGUIInput forward_spatial_gui_input(Camera3D *p_camera, const Ref &p_event) override { return grid_map_editor->forward_spatial_input_event(p_camera, p_event); } + virtual String get_name() const override { return "GridMap"; } + bool has_main_screen() const override { return false; } + virtual void edit(Object *p_object) override; + virtual bool handles(Object *p_object) const override; + virtual void make_visible(bool p_visible) override; + + GridMapEditorPlugin(); + ~GridMapEditorPlugin(); +}; + +#endif // TOOLS_ENABLED + +#endif // GRID_MAP_EDITOR_PLUGIN_H diff --git a/modules/gridmap/grid_map_editor_plugin.cpp b/modules/gridmap/grid_map_editor_plugin.cpp deleted file mode 100644 index e8e096d52b..0000000000 --- a/modules/gridmap/grid_map_editor_plugin.cpp +++ /dev/null @@ -1,1481 +0,0 @@ -/*************************************************************************/ -/* grid_map_editor_plugin.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#include "grid_map_editor_plugin.h" -#include "core/input/input.h" -#include "editor/editor_node.h" -#include "editor/editor_scale.h" -#include "editor/editor_settings.h" -#include "editor/plugins/node_3d_editor_plugin.h" -#include "scene/3d/camera_3d.h" - -#include "core/os/keyboard.h" -#include "scene/main/window.h" - -void GridMapEditor::_node_removed(Node *p_node) { - if (p_node == node) { - node = nullptr; - } -} - -void GridMapEditor::_configure() { - if (!node) { - return; - } - - update_grid(); -} - -void GridMapEditor::_menu_option(int p_option) { - switch (p_option) { - case MENU_OPTION_PREV_LEVEL: { - floor->set_value(floor->get_value() - 1); - } break; - - case MENU_OPTION_NEXT_LEVEL: { - floor->set_value(floor->get_value() + 1); - } break; - - case MENU_OPTION_X_AXIS: - case MENU_OPTION_Y_AXIS: - case MENU_OPTION_Z_AXIS: { - int new_axis = p_option - MENU_OPTION_X_AXIS; - for (int i = 0; i < 3; i++) { - int idx = options->get_popup()->get_item_index(MENU_OPTION_X_AXIS + i); - options->get_popup()->set_item_checked(idx, i == new_axis); - } - - if (edit_axis != new_axis) { - int item1 = options->get_popup()->get_item_index(MENU_OPTION_NEXT_LEVEL); - int item2 = options->get_popup()->get_item_index(MENU_OPTION_PREV_LEVEL); - if (edit_axis == Vector3::AXIS_Y) { - options->get_popup()->set_item_text(item1, TTR("Next Plane")); - options->get_popup()->set_item_text(item2, TTR("Previous Plane")); - spin_box_label->set_text(TTR("Plane:")); - } else if (new_axis == Vector3::AXIS_Y) { - options->get_popup()->set_item_text(item1, TTR("Next Floor")); - options->get_popup()->set_item_text(item2, TTR("Previous Floor")); - spin_box_label->set_text(TTR("Floor:")); - } - } - edit_axis = Vector3::Axis(new_axis); - update_grid(); - - } break; - case MENU_OPTION_CURSOR_ROTATE_Y: { - Basis r; - if (input_action == INPUT_PASTE) { - r.set_orthogonal_index(paste_indicator.orientation); - r.rotate(Vector3(0, 1, 0), -Math_PI / 2.0); - paste_indicator.orientation = r.get_orthogonal_index(); - _update_paste_indicator(); - break; - } - - r.set_orthogonal_index(cursor_rot); - r.rotate(Vector3(0, 1, 0), -Math_PI / 2.0); - cursor_rot = r.get_orthogonal_index(); - _update_cursor_transform(); - } break; - case MENU_OPTION_CURSOR_ROTATE_X: { - Basis r; - if (input_action == INPUT_PASTE) { - r.set_orthogonal_index(paste_indicator.orientation); - r.rotate(Vector3(1, 0, 0), -Math_PI / 2.0); - paste_indicator.orientation = r.get_orthogonal_index(); - _update_paste_indicator(); - break; - } - - r.set_orthogonal_index(cursor_rot); - r.rotate(Vector3(1, 0, 0), -Math_PI / 2.0); - cursor_rot = r.get_orthogonal_index(); - _update_cursor_transform(); - } break; - case MENU_OPTION_CURSOR_ROTATE_Z: { - Basis r; - if (input_action == INPUT_PASTE) { - r.set_orthogonal_index(paste_indicator.orientation); - r.rotate(Vector3(0, 0, 1), -Math_PI / 2.0); - paste_indicator.orientation = r.get_orthogonal_index(); - _update_paste_indicator(); - break; - } - - r.set_orthogonal_index(cursor_rot); - r.rotate(Vector3(0, 0, 1), -Math_PI / 2.0); - cursor_rot = r.get_orthogonal_index(); - _update_cursor_transform(); - } break; - case MENU_OPTION_CURSOR_BACK_ROTATE_Y: { - Basis r; - if (input_action == INPUT_PASTE) { - r.set_orthogonal_index(paste_indicator.orientation); - r.rotate(Vector3(0, 1, 0), Math_PI / 2.0); - paste_indicator.orientation = r.get_orthogonal_index(); - _update_paste_indicator(); - break; - } - - r.set_orthogonal_index(cursor_rot); - r.rotate(Vector3(0, 1, 0), Math_PI / 2.0); - cursor_rot = r.get_orthogonal_index(); - _update_cursor_transform(); - } break; - case MENU_OPTION_CURSOR_BACK_ROTATE_X: { - Basis r; - if (input_action == INPUT_PASTE) { - r.set_orthogonal_index(paste_indicator.orientation); - r.rotate(Vector3(1, 0, 0), Math_PI / 2.0); - paste_indicator.orientation = r.get_orthogonal_index(); - _update_paste_indicator(); - break; - } - - r.set_orthogonal_index(cursor_rot); - r.rotate(Vector3(1, 0, 0), Math_PI / 2.0); - cursor_rot = r.get_orthogonal_index(); - _update_cursor_transform(); - } break; - case MENU_OPTION_CURSOR_BACK_ROTATE_Z: { - Basis r; - if (input_action == INPUT_PASTE) { - r.set_orthogonal_index(paste_indicator.orientation); - r.rotate(Vector3(0, 0, 1), Math_PI / 2.0); - paste_indicator.orientation = r.get_orthogonal_index(); - _update_paste_indicator(); - break; - } - - r.set_orthogonal_index(cursor_rot); - r.rotate(Vector3(0, 0, 1), Math_PI / 2.0); - cursor_rot = r.get_orthogonal_index(); - _update_cursor_transform(); - } break; - case MENU_OPTION_CURSOR_CLEAR_ROTATION: { - if (input_action == INPUT_PASTE) { - paste_indicator.orientation = 0; - _update_paste_indicator(); - break; - } - - cursor_rot = 0; - _update_cursor_transform(); - } break; - - case MENU_OPTION_PASTE_SELECTS: { - int idx = options->get_popup()->get_item_index(MENU_OPTION_PASTE_SELECTS); - options->get_popup()->set_item_checked(idx, !options->get_popup()->is_item_checked(idx)); - } break; - - case MENU_OPTION_SELECTION_DUPLICATE: - case MENU_OPTION_SELECTION_CUT: { - if (!(selection.active && input_action == INPUT_NONE)) { - break; - } - - _set_clipboard_data(); - - if (p_option == MENU_OPTION_SELECTION_CUT) { - _delete_selection(); - } - - input_action = INPUT_PASTE; - paste_indicator.click = selection.begin; - paste_indicator.current = selection.begin; - paste_indicator.begin = selection.begin; - paste_indicator.end = selection.end; - paste_indicator.orientation = 0; - _update_paste_indicator(); - } break; - case MENU_OPTION_SELECTION_CLEAR: { - if (!selection.active) { - break; - } - - _delete_selection(); - - } break; - case MENU_OPTION_SELECTION_FILL: { - if (!selection.active) { - return; - } - - _fill_selection(); - - } break; - case MENU_OPTION_GRIDMAP_SETTINGS: { - settings_dialog->popup_centered(settings_vbc->get_combined_minimum_size() + Size2(50, 50) * EDSCALE); - } break; - } -} - -void GridMapEditor::_update_cursor_transform() { - cursor_transform = Transform3D(); - cursor_transform.origin = cursor_origin; - cursor_transform.basis.set_orthogonal_index(cursor_rot); - cursor_transform.basis *= node->get_cell_scale(); - cursor_transform = node->get_global_transform() * cursor_transform; - - if (selected_palette >= 0) { - if (node && !node->get_mesh_library().is_null()) { - cursor_transform *= node->get_mesh_library()->get_item_mesh_transform(selected_palette); - } - } - - if (cursor_instance.is_valid()) { - RenderingServer::get_singleton()->instance_set_transform(cursor_instance, cursor_transform); - RenderingServer::get_singleton()->instance_set_visible(cursor_instance, cursor_visible); - } -} - -void GridMapEditor::_update_selection_transform() { - Transform3D xf_zero; - xf_zero.basis.set_zero(); - - if (!selection.active) { - RenderingServer::get_singleton()->instance_set_transform(selection_instance, xf_zero); - for (int i = 0; i < 3; i++) { - RenderingServer::get_singleton()->instance_set_transform(selection_level_instance[i], xf_zero); - } - return; - } - - Transform3D xf; - xf.scale((Vector3(1, 1, 1) + (selection.end - selection.begin)) * node->get_cell_size()); - xf.origin = selection.begin * node->get_cell_size(); - - RenderingServer::get_singleton()->instance_set_transform(selection_instance, node->get_global_transform() * xf); - - for (int i = 0; i < 3; i++) { - if (i != edit_axis || (edit_floor[edit_axis] < selection.begin[edit_axis]) || (edit_floor[edit_axis] > selection.end[edit_axis] + 1)) { - RenderingServer::get_singleton()->instance_set_transform(selection_level_instance[i], xf_zero); - } else { - Vector3 scale = (selection.end - selection.begin + Vector3(1, 1, 1)); - scale[edit_axis] = 1.0; - Vector3 position = selection.begin; - position[edit_axis] = edit_floor[edit_axis]; - - scale *= node->get_cell_size(); - position *= node->get_cell_size(); - - Transform3D xf2; - xf2.basis.scale(scale); - xf2.origin = position; - - RenderingServer::get_singleton()->instance_set_transform(selection_level_instance[i], xf2); - } - } -} - -void GridMapEditor::_validate_selection() { - if (!selection.active) { - return; - } - selection.begin = selection.click; - selection.end = selection.current; - - if (selection.begin.x > selection.end.x) { - SWAP(selection.begin.x, selection.end.x); - } - if (selection.begin.y > selection.end.y) { - SWAP(selection.begin.y, selection.end.y); - } - if (selection.begin.z > selection.end.z) { - SWAP(selection.begin.z, selection.end.z); - } - - _update_selection_transform(); -} - -void GridMapEditor::_set_selection(bool p_active, const Vector3 &p_begin, const Vector3 &p_end) { - selection.active = p_active; - selection.begin = p_begin; - selection.end = p_end; - selection.click = p_begin; - selection.current = p_end; - - if (is_visible_in_tree()) { - _update_selection_transform(); - } - - options->get_popup()->set_item_disabled(options->get_popup()->get_item_index(MENU_OPTION_SELECTION_CLEAR), !selection.active); - options->get_popup()->set_item_disabled(options->get_popup()->get_item_index(MENU_OPTION_SELECTION_CUT), !selection.active); - options->get_popup()->set_item_disabled(options->get_popup()->get_item_index(MENU_OPTION_SELECTION_DUPLICATE), !selection.active); - options->get_popup()->set_item_disabled(options->get_popup()->get_item_index(MENU_OPTION_SELECTION_FILL), !selection.active); -} - -bool GridMapEditor::do_input_action(Camera3D *p_camera, const Point2 &p_point, bool p_click) { - if (!spatial_editor) { - return false; - } - - if (selected_palette < 0 && input_action != INPUT_PICK && input_action != INPUT_SELECT && input_action != INPUT_PASTE) { - return false; - } - Ref mesh_library = node->get_mesh_library(); - if (mesh_library.is_null()) { - return false; - } - if (input_action != INPUT_PICK && input_action != INPUT_SELECT && input_action != INPUT_PASTE && !mesh_library->has_item(selected_palette)) { - return false; - } - - Camera3D *camera = p_camera; - Vector3 from = camera->project_ray_origin(p_point); - Vector3 normal = camera->project_ray_normal(p_point); - Transform3D local_xform = node->get_global_transform().affine_inverse(); - Vector planes = camera->get_frustum(); - from = local_xform.xform(from); - normal = local_xform.basis.xform(normal).normalized(); - - Plane p; - p.normal[edit_axis] = 1.0; - p.d = edit_floor[edit_axis] * node->get_cell_size()[edit_axis]; - - Vector3 inters; - if (!p.intersects_segment(from, from + normal * settings_pick_distance->get_value(), &inters)) { - return false; - } - - // Make sure the intersection is inside the frustum planes, to avoid - // Painting on invisible regions. - for (int i = 0; i < planes.size(); i++) { - Plane fp = local_xform.xform(planes[i]); - if (fp.is_point_over(inters)) { - return false; - } - } - - int cell[3]; - Vector3 cell_size = node->get_cell_size(); - - for (int i = 0; i < 3; i++) { - if (i == edit_axis) { - cell[i] = edit_floor[i]; - } else { - cell[i] = inters[i] / cell_size[i]; - if (inters[i] < 0) { - cell[i] -= 1; // Compensate negative. - } - grid_ofs[i] = cell[i] * cell_size[i]; - } - } - - RS::get_singleton()->instance_set_transform(grid_instance[edit_axis], node->get_global_transform() * edit_grid_xform); - - if (cursor_instance.is_valid()) { - 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(); - cursor_visible = true; - - if (input_action == INPUT_SELECT || input_action == INPUT_PASTE) { - cursor_visible = false; - } - - _update_cursor_transform(); - } - - if (input_action == INPUT_PASTE) { - paste_indicator.current = Vector3i(cell[0], cell[1], cell[2]); - _update_paste_indicator(); - - } else if (input_action == INPUT_SELECT) { - selection.current = Vector3i(cell[0], cell[1], cell[2]); - if (p_click) { - selection.click = selection.current; - } - selection.active = true; - _validate_selection(); - - return true; - } else if (input_action == INPUT_PICK) { - int item = node->get_cell_item(Vector3i(cell[0], cell[1], cell[2])); - if (item >= 0) { - selected_palette = item; - mesh_library_palette->set_current(item); - update_palette(); - _update_cursor_instance(); - } - return true; - } - - if (input_action == INPUT_PAINT) { - SetItem si; - si.position = Vector3i(cell[0], cell[1], cell[2]); - si.new_value = selected_palette; - si.new_orientation = cursor_rot; - si.old_value = node->get_cell_item(Vector3i(cell[0], cell[1], cell[2])); - si.old_orientation = node->get_cell_item_orientation(Vector3i(cell[0], cell[1], cell[2])); - set_items.push_back(si); - node->set_cell_item(Vector3i(cell[0], cell[1], cell[2]), selected_palette, cursor_rot); - return true; - } else if (input_action == INPUT_ERASE) { - SetItem si; - si.position = Vector3i(cell[0], cell[1], cell[2]); - si.new_value = -1; - si.new_orientation = 0; - si.old_value = node->get_cell_item(Vector3i(cell[0], cell[1], cell[2])); - si.old_orientation = node->get_cell_item_orientation(Vector3i(cell[0], cell[1], cell[2])); - set_items.push_back(si); - node->set_cell_item(Vector3i(cell[0], cell[1], cell[2]), -1); - return true; - } - - return false; -} - -void GridMapEditor::_delete_selection() { - if (!selection.active) { - return; - } - - undo_redo->create_action(TTR("GridMap Delete Selection")); - for (int i = selection.begin.x; i <= selection.end.x; i++) { - for (int j = selection.begin.y; j <= selection.end.y; j++) { - for (int k = selection.begin.z; k <= selection.end.z; k++) { - Vector3i selected = Vector3i(i, j, k); - undo_redo->add_do_method(node, "set_cell_item", selected, GridMap::INVALID_CELL_ITEM); - undo_redo->add_undo_method(node, "set_cell_item", selected, node->get_cell_item(selected), node->get_cell_item_orientation(selected)); - } - } - } - undo_redo->add_do_method(this, "_set_selection", !selection.active, selection.begin, selection.end); - undo_redo->add_undo_method(this, "_set_selection", selection.active, selection.begin, selection.end); - undo_redo->commit_action(); -} - -void GridMapEditor::_fill_selection() { - if (!selection.active) { - return; - } - - undo_redo->create_action(TTR("GridMap Fill Selection")); - for (int i = selection.begin.x; i <= selection.end.x; i++) { - for (int j = selection.begin.y; j <= selection.end.y; j++) { - for (int k = selection.begin.z; k <= selection.end.z; k++) { - Vector3i selected = Vector3i(i, j, k); - undo_redo->add_do_method(node, "set_cell_item", selected, selected_palette, cursor_rot); - undo_redo->add_undo_method(node, "set_cell_item", selected, node->get_cell_item(selected), node->get_cell_item_orientation(selected)); - } - } - } - undo_redo->add_do_method(this, "_set_selection", !selection.active, selection.begin, selection.end); - undo_redo->add_undo_method(this, "_set_selection", selection.active, selection.begin, selection.end); - undo_redo->commit_action(); -} - -void GridMapEditor::_clear_clipboard_data() { - for (const ClipboardItem &E : clipboard_items) { - RenderingServer::get_singleton()->free(E.instance); - } - - clipboard_items.clear(); -} - -void GridMapEditor::_set_clipboard_data() { - _clear_clipboard_data(); - - Ref meshLibrary = node->get_mesh_library(); - - for (int i = selection.begin.x; i <= selection.end.x; i++) { - for (int j = selection.begin.y; j <= selection.end.y; j++) { - for (int k = selection.begin.z; k <= selection.end.z; k++) { - Vector3i selected = Vector3i(i, j, k); - int itm = node->get_cell_item(selected); - if (itm == GridMap::INVALID_CELL_ITEM) { - continue; - } - - Ref mesh = meshLibrary->get_item_mesh(itm); - - ClipboardItem item; - item.cell_item = itm; - item.grid_offset = Vector3(selected) - selection.begin; - item.orientation = node->get_cell_item_orientation(selected); - item.instance = RenderingServer::get_singleton()->instance_create2(mesh->get_rid(), get_tree()->get_root()->get_world_3d()->get_scenario()); - - clipboard_items.push_back(item); - } - } - } -} - -void GridMapEditor::_update_paste_indicator() { - if (input_action != INPUT_PASTE) { - Transform3D xf; - xf.basis.set_zero(); - RenderingServer::get_singleton()->instance_set_transform(paste_instance, xf); - return; - } - - Vector3 center = 0.5 * Vector3(real_t(node->get_center_x()), real_t(node->get_center_y()), real_t(node->get_center_z())); - Vector3 scale = (Vector3(1, 1, 1) + (paste_indicator.end - paste_indicator.begin)) * node->get_cell_size(); - Transform3D xf; - xf.scale(scale); - xf.origin = (paste_indicator.begin + (paste_indicator.current - paste_indicator.click) + center) * node->get_cell_size(); - Basis rot; - rot.set_orthogonal_index(paste_indicator.orientation); - xf.basis = rot * xf.basis; - xf.translate((-center * node->get_cell_size()) / scale); - - RenderingServer::get_singleton()->instance_set_transform(paste_instance, node->get_global_transform() * xf); - - for (const ClipboardItem &item : clipboard_items) { - xf = Transform3D(); - xf.origin = (paste_indicator.begin + (paste_indicator.current - paste_indicator.click) + center) * node->get_cell_size(); - xf.basis = rot * xf.basis; - xf.translate(item.grid_offset * node->get_cell_size()); - - Basis item_rot; - item_rot.set_orthogonal_index(item.orientation); - xf.basis = item_rot * xf.basis * node->get_cell_scale(); - - RenderingServer::get_singleton()->instance_set_transform(item.instance, node->get_global_transform() * xf); - } -} - -void GridMapEditor::_do_paste() { - int idx = options->get_popup()->get_item_index(MENU_OPTION_PASTE_SELECTS); - bool reselect = options->get_popup()->is_item_checked(idx); - - Basis rot; - rot.set_orthogonal_index(paste_indicator.orientation); - - Vector3 ofs = paste_indicator.current - paste_indicator.click; - undo_redo->create_action(TTR("GridMap Paste Selection")); - - for (const ClipboardItem &item : clipboard_items) { - Vector3 position = rot.xform(item.grid_offset) + paste_indicator.begin + ofs; - - Basis orm; - orm.set_orthogonal_index(item.orientation); - orm = rot * orm; - - undo_redo->add_do_method(node, "set_cell_item", position, item.cell_item, orm.get_orthogonal_index()); - undo_redo->add_undo_method(node, "set_cell_item", position, node->get_cell_item(position), node->get_cell_item_orientation(position)); - } - - if (reselect) { - undo_redo->add_do_method(this, "_set_selection", true, paste_indicator.begin + ofs, paste_indicator.end + ofs); - undo_redo->add_undo_method(this, "_set_selection", selection.active, selection.begin, selection.end); - } - - undo_redo->commit_action(); - - _clear_clipboard_data(); -} - -EditorPlugin::AfterGUIInput GridMapEditor::forward_spatial_input_event(Camera3D *p_camera, const Ref &p_event) { - if (!node) { - return EditorPlugin::AFTER_GUI_INPUT_PASS; - } - - Ref mb = p_event; - - if (mb.is_valid()) { - if (mb->get_button_index() == MouseButton::WHEEL_UP && (mb->is_command_pressed() || mb->is_shift_pressed())) { - if (mb->is_pressed()) { - floor->set_value(floor->get_value() + mb->get_factor()); - } - - return EditorPlugin::AFTER_GUI_INPUT_STOP; // Eaten. - } else if (mb->get_button_index() == MouseButton::WHEEL_DOWN && (mb->is_command_pressed() || mb->is_shift_pressed())) { - if (mb->is_pressed()) { - floor->set_value(floor->get_value() - mb->get_factor()); - } - return EditorPlugin::AFTER_GUI_INPUT_STOP; - } - - if (mb->is_pressed()) { - Node3DEditorViewport::NavigationScheme nav_scheme = (Node3DEditorViewport::NavigationScheme)EditorSettings::get_singleton()->get("editors/3d/navigation/navigation_scheme").operator int(); - if ((nav_scheme == Node3DEditorViewport::NAVIGATION_MAYA || nav_scheme == Node3DEditorViewport::NAVIGATION_MODO) && mb->is_alt_pressed()) { - input_action = INPUT_NONE; - } else if (mb->get_button_index() == MouseButton::LEFT) { - bool can_edit = (node && node->get_mesh_library().is_valid()); - if (input_action == INPUT_PASTE) { - _do_paste(); - input_action = INPUT_NONE; - _update_paste_indicator(); - } else if (mb->is_shift_pressed() && can_edit) { - input_action = INPUT_SELECT; - last_selection = selection; - } else if (mb->is_command_pressed() && can_edit) { - input_action = INPUT_PICK; - } else { - input_action = INPUT_PAINT; - set_items.clear(); - } - } else if (mb->get_button_index() == MouseButton::RIGHT) { - if (input_action == INPUT_PASTE) { - _clear_clipboard_data(); - input_action = INPUT_NONE; - _update_paste_indicator(); - return EditorPlugin::AFTER_GUI_INPUT_STOP; - } else if (selection.active) { - _set_selection(false); - return EditorPlugin::AFTER_GUI_INPUT_STOP; - } else { - input_action = INPUT_ERASE; - set_items.clear(); - } - } else { - return EditorPlugin::AFTER_GUI_INPUT_PASS; - } - - if (do_input_action(p_camera, Point2(mb->get_position().x, mb->get_position().y), true)) { - return EditorPlugin::AFTER_GUI_INPUT_STOP; - } - return EditorPlugin::AFTER_GUI_INPUT_PASS; - } else { - if ((mb->get_button_index() == MouseButton::RIGHT && input_action == INPUT_ERASE) || (mb->get_button_index() == MouseButton::LEFT && input_action == INPUT_PAINT)) { - if (set_items.size()) { - undo_redo->create_action(TTR("GridMap Paint")); - for (const SetItem &si : set_items) { - undo_redo->add_do_method(node, "set_cell_item", si.position, si.new_value, si.new_orientation); - } - for (List::Element *E = set_items.back(); E; E = E->prev()) { - const SetItem &si = E->get(); - undo_redo->add_undo_method(node, "set_cell_item", si.position, si.old_value, si.old_orientation); - } - - undo_redo->commit_action(); - } - set_items.clear(); - input_action = INPUT_NONE; - - if (set_items.size() > 0) { - return EditorPlugin::AFTER_GUI_INPUT_STOP; - } - return EditorPlugin::AFTER_GUI_INPUT_PASS; - } - - if (mb->get_button_index() == MouseButton::LEFT && input_action == INPUT_SELECT) { - undo_redo->create_action(TTR("GridMap Selection")); - undo_redo->add_do_method(this, "_set_selection", selection.active, selection.begin, selection.end); - undo_redo->add_undo_method(this, "_set_selection", last_selection.active, last_selection.begin, last_selection.end); - undo_redo->commit_action(); - } - - if (mb->get_button_index() == MouseButton::LEFT && input_action != INPUT_NONE) { - set_items.clear(); - input_action = INPUT_NONE; - return EditorPlugin::AFTER_GUI_INPUT_STOP; - } - if (mb->get_button_index() == MouseButton::RIGHT && (input_action == INPUT_ERASE || input_action == INPUT_PASTE)) { - input_action = INPUT_NONE; - return EditorPlugin::AFTER_GUI_INPUT_STOP; - } - } - } - - Ref mm = p_event; - - if (mm.is_valid()) { - if (do_input_action(p_camera, mm->get_position(), false)) { - return EditorPlugin::AFTER_GUI_INPUT_STOP; - } - return EditorPlugin::AFTER_GUI_INPUT_PASS; - } - - Ref k = p_event; - - if (k.is_valid()) { - if (k->is_pressed()) { - if (k->get_keycode() == Key::ESCAPE) { - if (input_action == INPUT_PASTE) { - _clear_clipboard_data(); - input_action = INPUT_NONE; - _update_paste_indicator(); - return EditorPlugin::AFTER_GUI_INPUT_STOP; - } else if (selection.active) { - _set_selection(false); - return EditorPlugin::AFTER_GUI_INPUT_STOP; - } else { - selected_palette = -1; - mesh_library_palette->deselect_all(); - update_palette(); - _update_cursor_instance(); - return EditorPlugin::AFTER_GUI_INPUT_STOP; - } - } - - if (k->is_shift_pressed() && selection.active && input_action != INPUT_PASTE) { - if (k->get_keycode() == (Key)options->get_popup()->get_item_accelerator(options->get_popup()->get_item_index(MENU_OPTION_PREV_LEVEL))) { - selection.click[edit_axis]--; - _validate_selection(); - return EditorPlugin::AFTER_GUI_INPUT_STOP; - } - if (k->get_keycode() == (Key)options->get_popup()->get_item_accelerator(options->get_popup()->get_item_index(MENU_OPTION_NEXT_LEVEL))) { - selection.click[edit_axis]++; - _validate_selection(); - return EditorPlugin::AFTER_GUI_INPUT_STOP; - } - } - } - } - - Ref pan_gesture = p_event; - if (pan_gesture.is_valid()) { - if (pan_gesture->is_alt_pressed() && (pan_gesture->is_command_pressed() || pan_gesture->is_shift_pressed())) { - const real_t delta = pan_gesture->get_delta().y * 0.5; - accumulated_floor_delta += delta; - int step = 0; - if (ABS(accumulated_floor_delta) > 1.0) { - step = SIGN(accumulated_floor_delta); - accumulated_floor_delta -= step; - } - if (step) { - floor->set_value(floor->get_value() + step); - } - return EditorPlugin::AFTER_GUI_INPUT_STOP; - } - } - accumulated_floor_delta = 0.0; - - return EditorPlugin::AFTER_GUI_INPUT_PASS; -} - -struct _CGMEItemSort { - String name; - int id = 0; - _FORCE_INLINE_ bool operator<(const _CGMEItemSort &r_it) const { return name < r_it.name; } -}; - -void GridMapEditor::_set_display_mode(int p_mode) { - if (display_mode == p_mode) { - return; - } - - if (p_mode == DISPLAY_LIST) { - mode_list->set_pressed(true); - mode_thumbnail->set_pressed(false); - } else if (p_mode == DISPLAY_THUMBNAIL) { - mode_list->set_pressed(false); - mode_thumbnail->set_pressed(true); - } - - display_mode = p_mode; - - update_palette(); -} - -void GridMapEditor::_text_changed(const String &p_text) { - update_palette(); -} - -void GridMapEditor::_sbox_input(const Ref &p_ie) { - const Ref k = p_ie; - - if (k.is_valid() && (k->get_keycode() == Key::UP || k->get_keycode() == Key::DOWN || k->get_keycode() == Key::PAGEUP || k->get_keycode() == Key::PAGEDOWN)) { - // Forward the key input to the ItemList so it can be scrolled - mesh_library_palette->gui_input(k); - search_box->accept_event(); - } -} - -void GridMapEditor::_mesh_library_palette_input(const Ref &p_ie) { - const Ref mb = p_ie; - - // Zoom in/out using Ctrl + mouse wheel - if (mb.is_valid() && mb->is_pressed() && mb->is_command_pressed()) { - if (mb->is_pressed() && mb->get_button_index() == MouseButton::WHEEL_UP) { - size_slider->set_value(size_slider->get_value() + 0.2); - } - - if (mb->is_pressed() && mb->get_button_index() == MouseButton::WHEEL_DOWN) { - size_slider->set_value(size_slider->get_value() - 0.2); - } - } -} - -void GridMapEditor::_icon_size_changed(float p_value) { - mesh_library_palette->set_icon_scale(p_value); - update_palette(); -} - -void GridMapEditor::update_palette() { - int selected = mesh_library_palette->get_current(); - - float min_size = EDITOR_GET("editors/grid_map/preview_size"); - min_size *= EDSCALE; - - mesh_library_palette->clear(); - if (display_mode == DISPLAY_THUMBNAIL) { - mesh_library_palette->set_max_columns(0); - mesh_library_palette->set_icon_mode(ItemList::ICON_MODE_TOP); - mesh_library_palette->set_fixed_column_width(min_size * MAX(size_slider->get_value(), 1.5)); - } else if (display_mode == DISPLAY_LIST) { - mesh_library_palette->set_max_columns(1); - mesh_library_palette->set_icon_mode(ItemList::ICON_MODE_LEFT); - mesh_library_palette->set_fixed_column_width(0); - } - - mesh_library_palette->set_fixed_icon_size(Size2(min_size, min_size)); - mesh_library_palette->set_max_text_lines(2); - - Ref mesh_library = node->get_mesh_library(); - - if (mesh_library.is_null()) { - last_mesh_library = nullptr; - search_box->set_text(""); - search_box->set_editable(false); - info_message->show(); - return; - } - - search_box->set_editable(true); - info_message->hide(); - - Vector ids; - ids = mesh_library->get_item_list(); - - List<_CGMEItemSort> il; - for (int i = 0; i < ids.size(); i++) { - _CGMEItemSort is; - is.id = ids[i]; - is.name = mesh_library->get_item_name(ids[i]); - il.push_back(is); - } - il.sort(); - - String filter = search_box->get_text().strip_edges(); - - int item = 0; - - for (_CGMEItemSort &E : il) { - int id = E.id; - String name = mesh_library->get_item_name(id); - Ref preview = mesh_library->get_item_preview(id); - - if (name.is_empty()) { - name = "#" + itos(id); - } - - if (!filter.is_empty() && !filter.is_subsequence_ofn(name)) { - continue; - } - - mesh_library_palette->add_item(""); - if (!preview.is_null()) { - mesh_library_palette->set_item_icon(item, preview); - mesh_library_palette->set_item_tooltip(item, name); - } - mesh_library_palette->set_item_text(item, name); - mesh_library_palette->set_item_metadata(item, id); - - item++; - } - - if (selected != -1 && mesh_library_palette->get_item_count() > 0) { - mesh_library_palette->select(selected); - } - - last_mesh_library = mesh_library.operator->(); -} - -void GridMapEditor::edit(GridMap *p_gridmap) { - if (!p_gridmap && node) { - node->disconnect("cell_size_changed", callable_mp(this, &GridMapEditor::_draw_grids)); - } - - node = p_gridmap; - - input_action = INPUT_NONE; - selection.active = false; - _update_selection_transform(); - _update_paste_indicator(); - - spatial_editor = Object::cast_to(EditorNode::get_singleton()->get_editor_plugin_screen()); - - if (!node) { - set_process(false); - for (int i = 0; i < 3; i++) { - RenderingServer::get_singleton()->instance_set_visible(grid_instance[i], false); - } - - if (cursor_instance.is_valid()) { - RenderingServer::get_singleton()->instance_set_visible(cursor_instance, false); - } - - return; - } - - update_palette(); - - set_process(true); - - _draw_grids(node->get_cell_size()); - update_grid(); - - node->connect("cell_size_changed", callable_mp(this, &GridMapEditor::_draw_grids)); -} - -void GridMapEditor::update_grid() { - grid_xform.origin.x -= 1; // Force update in hackish way. - - grid_ofs[edit_axis] = edit_floor[edit_axis] * node->get_cell_size()[edit_axis]; - - edit_grid_xform.origin = grid_ofs; - edit_grid_xform.basis = Basis(); - - for (int i = 0; i < 3; i++) { - RenderingServer::get_singleton()->instance_set_visible(grid_instance[i], i == edit_axis); - } - - updating = true; - floor->set_value(edit_floor[edit_axis]); - updating = false; -} - -void GridMapEditor::_draw_grids(const Vector3 &cell_size) { - Vector3 edited_floor = node->has_meta("_editor_floor_") ? node->get_meta("_editor_floor_") : Variant(); - - for (int i = 0; i < 3; i++) { - RS::get_singleton()->mesh_clear(grid[i]); - edit_floor[i] = edited_floor[i]; - } - - Vector grid_points[3]; - Vector grid_colors[3]; - - for (int i = 0; i < 3; i++) { - Vector3 axis; - axis[i] = 1; - Vector3 axis_n1; - axis_n1[(i + 1) % 3] = cell_size[(i + 1) % 3]; - Vector3 axis_n2; - axis_n2[(i + 2) % 3] = cell_size[(i + 2) % 3]; - - for (int j = -GRID_CURSOR_SIZE; j <= GRID_CURSOR_SIZE; j++) { - for (int k = -GRID_CURSOR_SIZE; k <= GRID_CURSOR_SIZE; k++) { - Vector3 p = axis_n1 * j + axis_n2 * k; - float trans = Math::pow(MAX(0, 1.0 - (Vector2(j, k).length() / GRID_CURSOR_SIZE)), 2); - - Vector3 pj = axis_n1 * (j + 1) + axis_n2 * k; - float transj = Math::pow(MAX(0, 1.0 - (Vector2(j + 1, k).length() / GRID_CURSOR_SIZE)), 2); - - Vector3 pk = axis_n1 * j + axis_n2 * (k + 1); - float transk = Math::pow(MAX(0, 1.0 - (Vector2(j, k + 1).length() / GRID_CURSOR_SIZE)), 2); - - grid_points[i].push_back(p); - grid_points[i].push_back(pk); - grid_colors[i].push_back(Color(1, 1, 1, trans)); - grid_colors[i].push_back(Color(1, 1, 1, transk)); - - grid_points[i].push_back(p); - grid_points[i].push_back(pj); - grid_colors[i].push_back(Color(1, 1, 1, trans)); - grid_colors[i].push_back(Color(1, 1, 1, transj)); - } - } - - Array d; - d.resize(RS::ARRAY_MAX); - d[RS::ARRAY_VERTEX] = grid_points[i]; - d[RS::ARRAY_COLOR] = grid_colors[i]; - RenderingServer::get_singleton()->mesh_add_surface_from_arrays(grid[i], RenderingServer::PRIMITIVE_LINES, d); - RenderingServer::get_singleton()->mesh_surface_set_material(grid[i], 0, indicator_mat->get_rid()); - } -} - -void GridMapEditor::_update_theme() { - options->set_icon(get_theme_icon(SNAME("GridMap"), SNAME("EditorIcons"))); - search_box->set_right_icon(get_theme_icon(SNAME("Search"), SNAME("EditorIcons"))); - mode_thumbnail->set_icon(get_theme_icon(SNAME("FileThumbnail"), SNAME("EditorIcons"))); - mode_list->set_icon(get_theme_icon(SNAME("FileList"), SNAME("EditorIcons"))); -} - -void GridMapEditor::_notification(int p_what) { - switch (p_what) { - case NOTIFICATION_ENTER_TREE: { - get_tree()->connect("node_removed", callable_mp(this, &GridMapEditor::_node_removed)); - mesh_library_palette->connect("item_selected", callable_mp(this, &GridMapEditor::_item_selected_cbk)); - for (int i = 0; i < 3; i++) { - grid[i] = RS::get_singleton()->mesh_create(); - grid_instance[i] = RS::get_singleton()->instance_create2(grid[i], get_tree()->get_root()->get_world_3d()->get_scenario()); - RenderingServer::get_singleton()->instance_set_layer_mask(grid_instance[i], 1 << Node3DEditorViewport::MISC_TOOL_LAYER); - selection_level_instance[i] = RenderingServer::get_singleton()->instance_create2(selection_level_mesh[i], get_tree()->get_root()->get_world_3d()->get_scenario()); - RenderingServer::get_singleton()->instance_set_layer_mask(selection_level_instance[i], 1 << Node3DEditorViewport::MISC_TOOL_LAYER); - } - - selection_instance = RenderingServer::get_singleton()->instance_create2(selection_mesh, get_tree()->get_root()->get_world_3d()->get_scenario()); - RenderingServer::get_singleton()->instance_set_layer_mask(selection_instance, 1 << Node3DEditorViewport::MISC_TOOL_LAYER); - paste_instance = RenderingServer::get_singleton()->instance_create2(paste_mesh, get_tree()->get_root()->get_world_3d()->get_scenario()); - RenderingServer::get_singleton()->instance_set_layer_mask(paste_instance, 1 << Node3DEditorViewport::MISC_TOOL_LAYER); - - _update_selection_transform(); - _update_paste_indicator(); - _update_theme(); - } break; - - case NOTIFICATION_EXIT_TREE: { - get_tree()->disconnect("node_removed", callable_mp(this, &GridMapEditor::_node_removed)); - _clear_clipboard_data(); - - for (int i = 0; i < 3; i++) { - RS::get_singleton()->free(grid_instance[i]); - RS::get_singleton()->free(grid[i]); - grid_instance[i] = RID(); - grid[i] = RID(); - RenderingServer::get_singleton()->free(selection_level_instance[i]); - } - - RenderingServer::get_singleton()->free(selection_instance); - RenderingServer::get_singleton()->free(paste_instance); - selection_instance = RID(); - paste_instance = RID(); - } break; - - case NOTIFICATION_PROCESS: { - if (!node) { - return; - } - - Transform3D xf = node->get_global_transform(); - - if (xf != grid_xform) { - for (int i = 0; i < 3; i++) { - RS::get_singleton()->instance_set_transform(grid_instance[i], xf * edit_grid_xform); - } - grid_xform = xf; - } - Ref cgmt = node->get_mesh_library(); - if (cgmt.operator->() != last_mesh_library) { - update_palette(); - } - } break; - - case NOTIFICATION_THEME_CHANGED: { - _update_theme(); - } break; - - case NOTIFICATION_APPLICATION_FOCUS_OUT: { - if (input_action == INPUT_PAINT) { - // Simulate mouse released event to stop drawing when editor focus exists. - Ref release; - release.instantiate(); - release->set_button_index(MouseButton::LEFT); - forward_spatial_input_event(nullptr, release); - } - } break; - } -} - -void GridMapEditor::_update_cursor_instance() { - if (!node) { - return; - } - - if (cursor_instance.is_valid()) { - RenderingServer::get_singleton()->free(cursor_instance); - } - cursor_instance = RID(); - - if (selected_palette >= 0) { - if (node && !node->get_mesh_library().is_null()) { - Ref mesh = node->get_mesh_library()->get_item_mesh(selected_palette); - if (!mesh.is_null() && mesh->get_rid().is_valid()) { - cursor_instance = RenderingServer::get_singleton()->instance_create2(mesh->get_rid(), get_tree()->get_root()->get_world_3d()->get_scenario()); - RenderingServer::get_singleton()->instance_set_transform(cursor_instance, cursor_transform); - } - } - } -} - -void GridMapEditor::_item_selected_cbk(int idx) { - selected_palette = mesh_library_palette->get_item_metadata(idx); - - _update_cursor_instance(); -} - -void GridMapEditor::_floor_changed(float p_value) { - if (updating) { - return; - } - - edit_floor[edit_axis] = p_value; - node->set_meta("_editor_floor_", Vector3(edit_floor[0], edit_floor[1], edit_floor[2])); - update_grid(); - _update_selection_transform(); -} - -void GridMapEditor::_floor_mouse_exited() { - floor->get_line_edit()->release_focus(); -} - -void GridMapEditor::_bind_methods() { - ClassDB::bind_method("_configure", &GridMapEditor::_configure); - ClassDB::bind_method("_set_selection", &GridMapEditor::_set_selection); -} - -GridMapEditor::GridMapEditor() { - undo_redo = EditorNode::get_singleton()->get_undo_redo(); - - int mw = EDITOR_DEF("editors/grid_map/palette_min_width", 230); - Control *ec = memnew(Control); - ec->set_custom_minimum_size(Size2(mw, 0) * EDSCALE); - add_child(ec); - - spatial_editor_hb = memnew(HBoxContainer); - spatial_editor_hb->set_h_size_flags(SIZE_EXPAND_FILL); - spatial_editor_hb->set_alignment(BoxContainer::ALIGNMENT_END); - Node3DEditor::get_singleton()->add_control_to_menu_panel(spatial_editor_hb); - - spin_box_label = memnew(Label); - spin_box_label->set_text(TTR("Floor:")); - spatial_editor_hb->add_child(spin_box_label); - - floor = memnew(SpinBox); - floor->set_min(-32767); - floor->set_max(32767); - floor->set_step(1); - floor->get_line_edit()->add_theme_constant_override("minimum_character_width", 16); - - spatial_editor_hb->add_child(floor); - floor->connect("value_changed", callable_mp(this, &GridMapEditor::_floor_changed)); - floor->connect("mouse_exited", callable_mp(this, &GridMapEditor::_floor_mouse_exited)); - floor->get_line_edit()->connect("mouse_exited", callable_mp(this, &GridMapEditor::_floor_mouse_exited)); - - spatial_editor_hb->add_child(memnew(VSeparator)); - - options = memnew(MenuButton); - spatial_editor_hb->add_child(options); - spatial_editor_hb->hide(); - - options->set_text(TTR("Grid Map")); - options->get_popup()->add_item(TTR("Previous Floor"), MENU_OPTION_PREV_LEVEL, Key::Q); - options->get_popup()->add_item(TTR("Next Floor"), MENU_OPTION_NEXT_LEVEL, Key::E); - options->get_popup()->add_separator(); - options->get_popup()->add_radio_check_item(TTR("Edit X Axis"), MENU_OPTION_X_AXIS, Key::Z); - options->get_popup()->add_radio_check_item(TTR("Edit Y Axis"), MENU_OPTION_Y_AXIS, Key::X); - options->get_popup()->add_radio_check_item(TTR("Edit Z Axis"), MENU_OPTION_Z_AXIS, Key::C); - options->get_popup()->set_item_checked(options->get_popup()->get_item_index(MENU_OPTION_Y_AXIS), true); - options->get_popup()->add_separator(); - options->get_popup()->add_item(TTR("Cursor Rotate X"), MENU_OPTION_CURSOR_ROTATE_X, Key::A); - options->get_popup()->add_item(TTR("Cursor Rotate Y"), MENU_OPTION_CURSOR_ROTATE_Y, Key::S); - options->get_popup()->add_item(TTR("Cursor Rotate Z"), MENU_OPTION_CURSOR_ROTATE_Z, Key::D); - options->get_popup()->add_item(TTR("Cursor Back Rotate X"), MENU_OPTION_CURSOR_BACK_ROTATE_X, KeyModifierMask::SHIFT + Key::A); - options->get_popup()->add_item(TTR("Cursor Back Rotate Y"), MENU_OPTION_CURSOR_BACK_ROTATE_Y, KeyModifierMask::SHIFT + Key::S); - options->get_popup()->add_item(TTR("Cursor Back Rotate Z"), MENU_OPTION_CURSOR_BACK_ROTATE_Z, KeyModifierMask::SHIFT + Key::D); - options->get_popup()->add_item(TTR("Cursor Clear Rotation"), MENU_OPTION_CURSOR_CLEAR_ROTATION, Key::W); - options->get_popup()->add_separator(); - options->get_popup()->add_check_item(TTR("Paste Selects"), MENU_OPTION_PASTE_SELECTS); - options->get_popup()->add_separator(); - options->get_popup()->add_item(TTR("Duplicate Selection"), MENU_OPTION_SELECTION_DUPLICATE, KeyModifierMask::CTRL + Key::C); - options->get_popup()->add_item(TTR("Cut Selection"), MENU_OPTION_SELECTION_CUT, KeyModifierMask::CTRL + Key::X); - options->get_popup()->add_item(TTR("Clear Selection"), MENU_OPTION_SELECTION_CLEAR, Key::KEY_DELETE); - options->get_popup()->add_item(TTR("Fill Selection"), MENU_OPTION_SELECTION_FILL, KeyModifierMask::CTRL + Key::F); - - options->get_popup()->add_separator(); - options->get_popup()->add_item(TTR("Settings..."), MENU_OPTION_GRIDMAP_SETTINGS); - - settings_dialog = memnew(ConfirmationDialog); - settings_dialog->set_title(TTR("GridMap Settings")); - add_child(settings_dialog); - settings_vbc = memnew(VBoxContainer); - settings_vbc->set_custom_minimum_size(Size2(200, 0) * EDSCALE); - settings_dialog->add_child(settings_vbc); - - settings_pick_distance = memnew(SpinBox); - settings_pick_distance->set_max(10000.0f); - settings_pick_distance->set_min(500.0f); - settings_pick_distance->set_step(1.0f); - settings_pick_distance->set_value(EDITOR_GET("editors/grid_map/pick_distance")); - settings_vbc->add_margin_child(TTR("Pick Distance:"), settings_pick_distance); - - options->get_popup()->connect("id_pressed", callable_mp(this, &GridMapEditor::_menu_option)); - - HBoxContainer *hb = memnew(HBoxContainer); - add_child(hb); - hb->set_h_size_flags(SIZE_EXPAND_FILL); - - search_box = memnew(LineEdit); - search_box->set_h_size_flags(SIZE_EXPAND_FILL); - search_box->set_placeholder(TTR("Filter meshes")); - hb->add_child(search_box); - search_box->connect("text_changed", callable_mp(this, &GridMapEditor::_text_changed)); - search_box->connect("gui_input", callable_mp(this, &GridMapEditor::_sbox_input)); - - mode_thumbnail = memnew(Button); - mode_thumbnail->set_flat(true); - mode_thumbnail->set_toggle_mode(true); - mode_thumbnail->set_pressed(true); - hb->add_child(mode_thumbnail); - mode_thumbnail->connect("pressed", callable_mp(this, &GridMapEditor::_set_display_mode), varray(DISPLAY_THUMBNAIL)); - - mode_list = memnew(Button); - mode_list->set_flat(true); - mode_list->set_toggle_mode(true); - mode_list->set_pressed(false); - hb->add_child(mode_list); - mode_list->connect("pressed", callable_mp(this, &GridMapEditor::_set_display_mode), varray(DISPLAY_LIST)); - - size_slider = memnew(HSlider); - size_slider->set_h_size_flags(SIZE_EXPAND_FILL); - size_slider->set_min(0.2f); - size_slider->set_max(4.0f); - size_slider->set_step(0.1f); - size_slider->set_value(1.0f); - size_slider->connect("value_changed", callable_mp(this, &GridMapEditor::_icon_size_changed)); - add_child(size_slider); - - EDITOR_DEF("editors/grid_map/preview_size", 64); - - mesh_library_palette = memnew(ItemList); - add_child(mesh_library_palette); - mesh_library_palette->set_v_size_flags(SIZE_EXPAND_FILL); - mesh_library_palette->connect("gui_input", callable_mp(this, &GridMapEditor::_mesh_library_palette_input)); - - info_message = memnew(Label); - info_message->set_text(TTR("Give a MeshLibrary resource to this GridMap to use its meshes.")); - info_message->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER); - info_message->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER); - info_message->set_autowrap_mode(Label::AUTOWRAP_WORD_SMART); - info_message->set_custom_minimum_size(Size2(100 * EDSCALE, 0)); - info_message->set_anchors_and_offsets_preset(PRESET_WIDE, PRESET_MODE_KEEP_SIZE, 8 * EDSCALE); - mesh_library_palette->add_child(info_message); - - edit_axis = Vector3::AXIS_Y; - edit_floor[0] = -1; - edit_floor[1] = -1; - edit_floor[2] = -1; - - selection_mesh = RenderingServer::get_singleton()->mesh_create(); - paste_mesh = RenderingServer::get_singleton()->mesh_create(); - - { - // Selection mesh create. - - Vector lines; - Vector triangles; - Vector square[3]; - - for (int i = 0; i < 6; i++) { - Vector3 face_points[4]; - - for (int j = 0; j < 4; j++) { - float v[3]; - v[0] = 1.0; - v[1] = 1 - 2 * ((j >> 1) & 1); - v[2] = v[1] * (1 - 2 * (j & 1)); - - for (int k = 0; k < 3; k++) { - if (i < 3) { - face_points[j][(i + k) % 3] = v[k]; - } else { - face_points[3 - j][(i + k) % 3] = -v[k]; - } - } - } - - triangles.push_back(face_points[0] * 0.5 + Vector3(0.5, 0.5, 0.5)); - triangles.push_back(face_points[1] * 0.5 + Vector3(0.5, 0.5, 0.5)); - triangles.push_back(face_points[2] * 0.5 + Vector3(0.5, 0.5, 0.5)); - - triangles.push_back(face_points[2] * 0.5 + Vector3(0.5, 0.5, 0.5)); - triangles.push_back(face_points[3] * 0.5 + Vector3(0.5, 0.5, 0.5)); - triangles.push_back(face_points[0] * 0.5 + Vector3(0.5, 0.5, 0.5)); - } - - for (int i = 0; i < 12; i++) { - AABB base(Vector3(0, 0, 0), Vector3(1, 1, 1)); - Vector3 a, b; - base.get_edge(i, a, b); - lines.push_back(a); - lines.push_back(b); - } - - for (int i = 0; i < 3; i++) { - Vector3 points[4]; - for (int j = 0; j < 4; j++) { - static const bool orderx[4] = { false, true, true, false }; - static const bool ordery[4] = { false, false, true, true }; - - Vector3 sp; - if (orderx[j]) { - sp[(i + 1) % 3] = 1.0; - } - if (ordery[j]) { - sp[(i + 2) % 3] = 1.0; - } - - points[j] = sp; - } - - for (int j = 0; j < 4; j++) { - Vector3 ofs; - ofs[i] += 0.01; - square[i].push_back(points[j] - ofs); - square[i].push_back(points[(j + 1) % 4] - ofs); - square[i].push_back(points[j] + ofs); - square[i].push_back(points[(j + 1) % 4] + ofs); - } - } - - Array d; - d.resize(RS::ARRAY_MAX); - - inner_mat.instantiate(); - inner_mat->set_albedo(Color(0.7, 0.7, 1.0, 0.2)); - inner_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED); - inner_mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA); - - d[RS::ARRAY_VERTEX] = triangles; - RenderingServer::get_singleton()->mesh_add_surface_from_arrays(selection_mesh, RS::PRIMITIVE_TRIANGLES, d); - RenderingServer::get_singleton()->mesh_surface_set_material(selection_mesh, 0, inner_mat->get_rid()); - - outer_mat.instantiate(); - outer_mat->set_albedo(Color(0.7, 0.7, 1.0, 0.8)); - outer_mat->set_on_top_of_alpha(); - - outer_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED); - outer_mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA); - - selection_floor_mat.instantiate(); - selection_floor_mat->set_albedo(Color(0.80, 0.80, 1.0, 1)); - selection_floor_mat->set_on_top_of_alpha(); - selection_floor_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED); - - d[RS::ARRAY_VERTEX] = lines; - RenderingServer::get_singleton()->mesh_add_surface_from_arrays(selection_mesh, RS::PRIMITIVE_LINES, d); - RenderingServer::get_singleton()->mesh_surface_set_material(selection_mesh, 1, outer_mat->get_rid()); - - d[RS::ARRAY_VERTEX] = triangles; - RenderingServer::get_singleton()->mesh_add_surface_from_arrays(paste_mesh, RS::PRIMITIVE_TRIANGLES, d); - RenderingServer::get_singleton()->mesh_surface_set_material(paste_mesh, 0, inner_mat->get_rid()); - - d[RS::ARRAY_VERTEX] = lines; - RenderingServer::get_singleton()->mesh_add_surface_from_arrays(paste_mesh, RS::PRIMITIVE_LINES, d); - RenderingServer::get_singleton()->mesh_surface_set_material(paste_mesh, 1, outer_mat->get_rid()); - - for (int i = 0; i < 3; i++) { - d[RS::ARRAY_VERTEX] = square[i]; - selection_level_mesh[i] = RS::get_singleton()->mesh_create(); - RenderingServer::get_singleton()->mesh_add_surface_from_arrays(selection_level_mesh[i], RS::PRIMITIVE_LINES, d); - RenderingServer::get_singleton()->mesh_surface_set_material(selection_level_mesh[i], 0, selection_floor_mat->get_rid()); - } - } - - _set_selection(false); - - indicator_mat.instantiate(); - indicator_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED); - indicator_mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA); - indicator_mat->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true); - indicator_mat->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true); - indicator_mat->set_albedo(Color(0.8, 0.5, 0.1)); -} - -GridMapEditor::~GridMapEditor() { - _clear_clipboard_data(); - - for (int i = 0; i < 3; i++) { - if (grid[i].is_valid()) { - RenderingServer::get_singleton()->free(grid[i]); - } - if (grid_instance[i].is_valid()) { - RenderingServer::get_singleton()->free(grid_instance[i]); - } - if (cursor_instance.is_valid()) { - RenderingServer::get_singleton()->free(cursor_instance); - } - if (selection_level_instance[i].is_valid()) { - RenderingServer::get_singleton()->free(selection_level_instance[i]); - } - if (selection_level_mesh[i].is_valid()) { - RenderingServer::get_singleton()->free(selection_level_mesh[i]); - } - } - - RenderingServer::get_singleton()->free(selection_mesh); - if (selection_instance.is_valid()) { - RenderingServer::get_singleton()->free(selection_instance); - } - - RenderingServer::get_singleton()->free(paste_mesh); - if (paste_instance.is_valid()) { - RenderingServer::get_singleton()->free(paste_instance); - } -} - -void GridMapEditorPlugin::_notification(int p_what) { - switch (p_what) { - case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: { - switch ((int)EditorSettings::get_singleton()->get("editors/grid_map/editor_side")) { - case 0: { // Left. - Node3DEditor::get_singleton()->move_control_to_left_panel(grid_map_editor); - } break; - case 1: { // Right. - Node3DEditor::get_singleton()->move_control_to_right_panel(grid_map_editor); - } break; - } - } break; - } -} - -void GridMapEditorPlugin::edit(Object *p_object) { - grid_map_editor->edit(Object::cast_to(p_object)); -} - -bool GridMapEditorPlugin::handles(Object *p_object) const { - return p_object->is_class("GridMap"); -} - -void GridMapEditorPlugin::make_visible(bool p_visible) { - if (p_visible) { - grid_map_editor->show(); - grid_map_editor->spatial_editor_hb->show(); - grid_map_editor->set_process(true); - } else { - grid_map_editor->spatial_editor_hb->hide(); - grid_map_editor->hide(); - grid_map_editor->edit(nullptr); - grid_map_editor->set_process(false); - } -} - -GridMapEditorPlugin::GridMapEditorPlugin() { - EDITOR_DEF("editors/grid_map/editor_side", 1); - EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::INT, "editors/grid_map/editor_side", PROPERTY_HINT_ENUM, "Left,Right")); - - grid_map_editor = memnew(GridMapEditor); - switch ((int)EditorSettings::get_singleton()->get("editors/grid_map/editor_side")) { - case 0: { // Left. - Node3DEditor::get_singleton()->add_control_to_left_panel(grid_map_editor); - } break; - case 1: { // Right. - Node3DEditor::get_singleton()->add_control_to_right_panel(grid_map_editor); - } break; - } - grid_map_editor->hide(); -} - -GridMapEditorPlugin::~GridMapEditorPlugin() { -} diff --git a/modules/gridmap/grid_map_editor_plugin.h b/modules/gridmap/grid_map_editor_plugin.h deleted file mode 100644 index c44c4ca7e0..0000000000 --- a/modules/gridmap/grid_map_editor_plugin.h +++ /dev/null @@ -1,252 +0,0 @@ -/*************************************************************************/ -/* grid_map_editor_plugin.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#ifndef GRID_MAP_EDITOR_PLUGIN_H -#define GRID_MAP_EDITOR_PLUGIN_H - -#include "editor/editor_plugin.h" -#include "grid_map.h" -#include "scene/gui/item_list.h" -#include "scene/gui/slider.h" -#include "scene/gui/spin_box.h" - -class Node3DEditorPlugin; - -class GridMapEditor : public VBoxContainer { - GDCLASS(GridMapEditor, VBoxContainer); - - enum { - GRID_CURSOR_SIZE = 50 - }; - - enum InputAction { - INPUT_NONE, - INPUT_PAINT, - INPUT_ERASE, - INPUT_PICK, - INPUT_SELECT, - INPUT_PASTE, - }; - - enum DisplayMode { - DISPLAY_THUMBNAIL, - DISPLAY_LIST - }; - - UndoRedo *undo_redo; - InputAction input_action = INPUT_NONE; - Panel *panel; - MenuButton *options; - SpinBox *floor; - double accumulated_floor_delta = 0.0; - Button *mode_thumbnail; - Button *mode_list; - LineEdit *search_box; - HSlider *size_slider; - HBoxContainer *spatial_editor_hb; - ConfirmationDialog *settings_dialog; - VBoxContainer *settings_vbc; - SpinBox *settings_pick_distance; - Label *spin_box_label; - - struct SetItem { - Vector3i position; - int new_value = 0; - int new_orientation = 0; - int old_value = 0; - int old_orientation = 0; - }; - - List set_items; - - GridMap *node = nullptr; - MeshLibrary *last_mesh_library; - - Transform3D grid_xform; - Transform3D edit_grid_xform; - Vector3::Axis edit_axis; - int edit_floor[3]; - Vector3 grid_ofs; - - RID grid[3]; - RID grid_instance[3]; - RID cursor_instance; - RID selection_mesh; - RID selection_instance; - RID selection_level_mesh[3]; - RID selection_level_instance[3]; - RID paste_mesh; - RID paste_instance; - - struct ClipboardItem { - int cell_item = 0; - Vector3 grid_offset; - int orientation = 0; - RID instance; - }; - - List clipboard_items; - - Ref indicator_mat; - Ref inner_mat; - Ref outer_mat; - Ref selection_floor_mat; - - bool updating = false; - - struct Selection { - Vector3 click; - Vector3 current; - Vector3 begin; - Vector3 end; - bool active = false; - } selection; - Selection last_selection; - - struct PasteIndicator { - Vector3 click; - Vector3 current; - Vector3 begin; - Vector3 end; - int orientation = 0; - }; - PasteIndicator paste_indicator; - - bool cursor_visible = false; - Transform3D cursor_transform; - - Vector3 cursor_origin; - - int display_mode = DISPLAY_THUMBNAIL; - int selected_palette = -1; - int cursor_rot = 0; - - enum Menu { - MENU_OPTION_NEXT_LEVEL, - MENU_OPTION_PREV_LEVEL, - MENU_OPTION_LOCK_VIEW, - MENU_OPTION_X_AXIS, - MENU_OPTION_Y_AXIS, - MENU_OPTION_Z_AXIS, - MENU_OPTION_CURSOR_ROTATE_Y, - MENU_OPTION_CURSOR_ROTATE_X, - MENU_OPTION_CURSOR_ROTATE_Z, - MENU_OPTION_CURSOR_BACK_ROTATE_Y, - MENU_OPTION_CURSOR_BACK_ROTATE_X, - MENU_OPTION_CURSOR_BACK_ROTATE_Z, - MENU_OPTION_CURSOR_CLEAR_ROTATION, - MENU_OPTION_PASTE_SELECTS, - MENU_OPTION_SELECTION_DUPLICATE, - MENU_OPTION_SELECTION_CUT, - MENU_OPTION_SELECTION_CLEAR, - MENU_OPTION_SELECTION_FILL, - MENU_OPTION_GRIDMAP_SETTINGS - - }; - - Node3DEditorPlugin *spatial_editor; - - struct AreaDisplay { - RID mesh; - RID instance; - }; - - ItemList *mesh_library_palette; - Label *info_message; - - void update_grid(); // Change which and where the grid is displayed - void _draw_grids(const Vector3 &cell_size); - void _configure(); - void _menu_option(int); - void update_palette(); - void _set_display_mode(int p_mode); - void _item_selected_cbk(int idx); - void _update_cursor_transform(); - void _update_cursor_instance(); - void _update_theme(); - - void _text_changed(const String &p_text); - void _sbox_input(const Ref &p_ie); - void _mesh_library_palette_input(const Ref &p_ie); - - void _icon_size_changed(float p_value); - - void _clear_clipboard_data(); - void _set_clipboard_data(); - void _update_paste_indicator(); - void _do_paste(); - void _update_selection_transform(); - void _validate_selection(); - void _set_selection(bool p_active, const Vector3 &p_begin = Vector3(), const Vector3 &p_end = Vector3()); - - void _floor_changed(float p_value); - void _floor_mouse_exited(); - - void _delete_selection(); - void _fill_selection(); - - bool do_input_action(Camera3D *p_camera, const Point2 &p_point, bool p_click); - - friend class GridMapEditorPlugin; - -protected: - void _notification(int p_what); - void _node_removed(Node *p_node); - static void _bind_methods(); - -public: - EditorPlugin::AfterGUIInput forward_spatial_input_event(Camera3D *p_camera, const Ref &p_event); - - void edit(GridMap *p_gridmap); - GridMapEditor(); - ~GridMapEditor(); -}; - -class GridMapEditorPlugin : public EditorPlugin { - GDCLASS(GridMapEditorPlugin, EditorPlugin); - - GridMapEditor *grid_map_editor; - -protected: - void _notification(int p_what); - -public: - virtual EditorPlugin::AfterGUIInput forward_spatial_gui_input(Camera3D *p_camera, const Ref &p_event) override { return grid_map_editor->forward_spatial_input_event(p_camera, p_event); } - virtual String get_name() const override { return "GridMap"; } - bool has_main_screen() const override { return false; } - virtual void edit(Object *p_object) override; - virtual bool handles(Object *p_object) const override; - virtual void make_visible(bool p_visible) override; - - GridMapEditorPlugin(); - ~GridMapEditorPlugin(); -}; - -#endif // CUBE_GRID_MAP_EDITOR_PLUGIN_H diff --git a/modules/gridmap/register_types.cpp b/modules/gridmap/register_types.cpp index f05c5de18b..d7c9f5c92e 100644 --- a/modules/gridmap/register_types.cpp +++ b/modules/gridmap/register_types.cpp @@ -28,21 +28,25 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ -#include "register_types.h" #ifndef _3D_DISABLED + +#include "register_types.h" + #include "core/object/class_db.h" #include "grid_map.h" -#include "grid_map_editor_plugin.h" + +#ifdef TOOLS_ENABLED +#include "editor/grid_map_editor_plugin.h" #endif void register_gridmap_types() { -#ifndef _3D_DISABLED GDREGISTER_CLASS(GridMap); #ifdef TOOLS_ENABLED EditorPlugins::add_by_type(); #endif -#endif } void unregister_gridmap_types() { } + +#endif // _3D_DISABLED diff --git a/modules/navigation/SCsub b/modules/navigation/SCsub index 22b5509b32..24a6b12639 100644 --- a/modules/navigation/SCsub +++ b/modules/navigation/SCsub @@ -57,6 +57,8 @@ env.modules_sources += thirdparty_obj module_obj = [] env_navigation.add_source_files(module_obj, "*.cpp") +if env["tools"]: + env_navigation.add_source_files(module_obj, "editor/*.cpp") env.modules_sources += module_obj # Needed to force rebuilding the module files when the thirdparty library is updated. diff --git a/modules/navigation/editor/navigation_mesh_editor_plugin.cpp b/modules/navigation/editor/navigation_mesh_editor_plugin.cpp new file mode 100644 index 0000000000..c243e3e6e3 --- /dev/null +++ b/modules/navigation/editor/navigation_mesh_editor_plugin.cpp @@ -0,0 +1,157 @@ +/*************************************************************************/ +/* navigation_mesh_editor_plugin.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#include "navigation_mesh_editor_plugin.h" + +#ifdef TOOLS_ENABLED + +#include "../navigation_mesh_generator.h" +#include "core/io/marshalls.h" +#include "core/io/resource_saver.h" +#include "editor/editor_node.h" +#include "scene/3d/mesh_instance_3d.h" +#include "scene/gui/box_container.h" + +void NavigationMeshEditor::_node_removed(Node *p_node) { + if (p_node == node) { + node = nullptr; + + hide(); + } +} + +void NavigationMeshEditor::_notification(int p_what) { + switch (p_what) { + case NOTIFICATION_ENTER_TREE: { + button_bake->set_icon(get_theme_icon(SNAME("Bake"), SNAME("EditorIcons"))); + button_reset->set_icon(get_theme_icon(SNAME("Reload"), SNAME("EditorIcons"))); + } break; + } +} + +void NavigationMeshEditor::_bake_pressed() { + button_bake->set_pressed(false); + + ERR_FAIL_COND(!node); + if (!node->get_navigation_mesh().is_valid()) { + err_dialog->set_text(TTR("A NavigationMesh resource must be set or created for this node to work.")); + err_dialog->popup_centered(); + return; + } + + NavigationMeshGenerator::get_singleton()->clear(node->get_navigation_mesh()); + NavigationMeshGenerator::get_singleton()->bake(node->get_navigation_mesh(), node); + + node->update_gizmos(); +} + +void NavigationMeshEditor::_clear_pressed() { + if (node) { + NavigationMeshGenerator::get_singleton()->clear(node->get_navigation_mesh()); + } + + button_bake->set_pressed(false); + bake_info->set_text(""); + + if (node) { + node->update_gizmos(); + } +} + +void NavigationMeshEditor::edit(NavigationRegion3D *p_nav_region) { + if (p_nav_region == nullptr || node == p_nav_region) { + return; + } + + node = p_nav_region; +} + +void NavigationMeshEditor::_bind_methods() { +} + +NavigationMeshEditor::NavigationMeshEditor() { + bake_hbox = memnew(HBoxContainer); + + button_bake = memnew(Button); + button_bake->set_flat(true); + bake_hbox->add_child(button_bake); + button_bake->set_toggle_mode(true); + button_bake->set_text(TTR("Bake NavMesh")); + button_bake->connect("pressed", callable_mp(this, &NavigationMeshEditor::_bake_pressed)); + + button_reset = memnew(Button); + button_reset->set_flat(true); + bake_hbox->add_child(button_reset); + // No button text, we only use a revert icon which is set when entering the tree. + button_reset->set_tooltip(TTR("Clear the navigation mesh.")); + button_reset->connect("pressed", callable_mp(this, &NavigationMeshEditor::_clear_pressed)); + + bake_info = memnew(Label); + bake_hbox->add_child(bake_info); + + err_dialog = memnew(AcceptDialog); + add_child(err_dialog); + node = nullptr; +} + +NavigationMeshEditor::~NavigationMeshEditor() { +} + +void NavigationMeshEditorPlugin::edit(Object *p_object) { + navigation_mesh_editor->edit(Object::cast_to(p_object)); +} + +bool NavigationMeshEditorPlugin::handles(Object *p_object) const { + return p_object->is_class("NavigationRegion3D"); +} + +void NavigationMeshEditorPlugin::make_visible(bool p_visible) { + if (p_visible) { + navigation_mesh_editor->show(); + navigation_mesh_editor->bake_hbox->show(); + } else { + navigation_mesh_editor->hide(); + navigation_mesh_editor->bake_hbox->hide(); + navigation_mesh_editor->edit(nullptr); + } +} + +NavigationMeshEditorPlugin::NavigationMeshEditorPlugin() { + navigation_mesh_editor = memnew(NavigationMeshEditor); + EditorNode::get_singleton()->get_main_control()->add_child(navigation_mesh_editor); + add_control_to_container(CONTAINER_SPATIAL_EDITOR_MENU, navigation_mesh_editor->bake_hbox); + navigation_mesh_editor->hide(); + navigation_mesh_editor->bake_hbox->hide(); +} + +NavigationMeshEditorPlugin::~NavigationMeshEditorPlugin() { +} + +#endif // TOOLS_ENABLED diff --git a/modules/navigation/editor/navigation_mesh_editor_plugin.h b/modules/navigation/editor/navigation_mesh_editor_plugin.h new file mode 100644 index 0000000000..3c3e804fd2 --- /dev/null +++ b/modules/navigation/editor/navigation_mesh_editor_plugin.h @@ -0,0 +1,86 @@ +/*************************************************************************/ +/* navigation_mesh_editor_plugin.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifndef NAVIGATION_MESH_EDITOR_PLUGIN_H +#define NAVIGATION_MESH_EDITOR_PLUGIN_H + +#ifdef TOOLS_ENABLED + +#include "editor/editor_plugin.h" + +class NavigationRegion3D; + +class NavigationMeshEditor : public Control { + friend class NavigationMeshEditorPlugin; + + GDCLASS(NavigationMeshEditor, Control); + + AcceptDialog *err_dialog; + + HBoxContainer *bake_hbox; + Button *button_bake; + Button *button_reset; + Label *bake_info; + + NavigationRegion3D *node; + + void _bake_pressed(); + void _clear_pressed(); + +protected: + void _node_removed(Node *p_node); + static void _bind_methods(); + void _notification(int p_what); + +public: + void edit(NavigationRegion3D *p_nav_region); + NavigationMeshEditor(); + ~NavigationMeshEditor(); +}; + +class NavigationMeshEditorPlugin : public EditorPlugin { + GDCLASS(NavigationMeshEditorPlugin, EditorPlugin); + + NavigationMeshEditor *navigation_mesh_editor; + +public: + virtual String get_name() const override { return "NavigationMesh"; } + bool has_main_screen() const override { return false; } + virtual void edit(Object *p_object) override; + virtual bool handles(Object *p_object) const override; + virtual void make_visible(bool p_visible) override; + + NavigationMeshEditorPlugin(); + ~NavigationMeshEditorPlugin(); +}; + +#endif // TOOLS_ENABLED + +#endif // NAVIGATION_MESH_EDITOR_PLUGIN_H diff --git a/modules/navigation/navigation_mesh_editor_plugin.cpp b/modules/navigation/navigation_mesh_editor_plugin.cpp deleted file mode 100644 index 511490ba07..0000000000 --- a/modules/navigation/navigation_mesh_editor_plugin.cpp +++ /dev/null @@ -1,156 +0,0 @@ -/*************************************************************************/ -/* navigation_mesh_editor_plugin.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#ifdef TOOLS_ENABLED -#include "navigation_mesh_editor_plugin.h" - -#include "core/io/marshalls.h" -#include "core/io/resource_saver.h" -#include "editor/editor_node.h" -#include "navigation_mesh_generator.h" -#include "scene/3d/mesh_instance_3d.h" -#include "scene/gui/box_container.h" - -void NavigationMeshEditor::_node_removed(Node *p_node) { - if (p_node == node) { - node = nullptr; - - hide(); - } -} - -void NavigationMeshEditor::_notification(int p_what) { - switch (p_what) { - case NOTIFICATION_ENTER_TREE: { - button_bake->set_icon(get_theme_icon(SNAME("Bake"), SNAME("EditorIcons"))); - button_reset->set_icon(get_theme_icon(SNAME("Reload"), SNAME("EditorIcons"))); - } break; - } -} - -void NavigationMeshEditor::_bake_pressed() { - button_bake->set_pressed(false); - - ERR_FAIL_COND(!node); - if (!node->get_navigation_mesh().is_valid()) { - err_dialog->set_text(TTR("A NavigationMesh resource must be set or created for this node to work.")); - err_dialog->popup_centered(); - return; - } - - NavigationMeshGenerator::get_singleton()->clear(node->get_navigation_mesh()); - NavigationMeshGenerator::get_singleton()->bake(node->get_navigation_mesh(), node); - - node->update_gizmos(); -} - -void NavigationMeshEditor::_clear_pressed() { - if (node) { - NavigationMeshGenerator::get_singleton()->clear(node->get_navigation_mesh()); - } - - button_bake->set_pressed(false); - bake_info->set_text(""); - - if (node) { - node->update_gizmos(); - } -} - -void NavigationMeshEditor::edit(NavigationRegion3D *p_nav_region) { - if (p_nav_region == nullptr || node == p_nav_region) { - return; - } - - node = p_nav_region; -} - -void NavigationMeshEditor::_bind_methods() { -} - -NavigationMeshEditor::NavigationMeshEditor() { - bake_hbox = memnew(HBoxContainer); - - button_bake = memnew(Button); - button_bake->set_flat(true); - bake_hbox->add_child(button_bake); - button_bake->set_toggle_mode(true); - button_bake->set_text(TTR("Bake NavMesh")); - button_bake->connect("pressed", callable_mp(this, &NavigationMeshEditor::_bake_pressed)); - - button_reset = memnew(Button); - button_reset->set_flat(true); - bake_hbox->add_child(button_reset); - // No button text, we only use a revert icon which is set when entering the tree. - button_reset->set_tooltip(TTR("Clear the navigation mesh.")); - button_reset->connect("pressed", callable_mp(this, &NavigationMeshEditor::_clear_pressed)); - - bake_info = memnew(Label); - bake_hbox->add_child(bake_info); - - err_dialog = memnew(AcceptDialog); - add_child(err_dialog); - node = nullptr; -} - -NavigationMeshEditor::~NavigationMeshEditor() { -} - -void NavigationMeshEditorPlugin::edit(Object *p_object) { - navigation_mesh_editor->edit(Object::cast_to(p_object)); -} - -bool NavigationMeshEditorPlugin::handles(Object *p_object) const { - return p_object->is_class("NavigationRegion3D"); -} - -void NavigationMeshEditorPlugin::make_visible(bool p_visible) { - if (p_visible) { - navigation_mesh_editor->show(); - navigation_mesh_editor->bake_hbox->show(); - } else { - navigation_mesh_editor->hide(); - navigation_mesh_editor->bake_hbox->hide(); - navigation_mesh_editor->edit(nullptr); - } -} - -NavigationMeshEditorPlugin::NavigationMeshEditorPlugin() { - navigation_mesh_editor = memnew(NavigationMeshEditor); - EditorNode::get_singleton()->get_main_control()->add_child(navigation_mesh_editor); - add_control_to_container(CONTAINER_SPATIAL_EDITOR_MENU, navigation_mesh_editor->bake_hbox); - navigation_mesh_editor->hide(); - navigation_mesh_editor->bake_hbox->hide(); -} - -NavigationMeshEditorPlugin::~NavigationMeshEditorPlugin() { -} - -#endif diff --git a/modules/navigation/navigation_mesh_editor_plugin.h b/modules/navigation/navigation_mesh_editor_plugin.h deleted file mode 100644 index d581b453b3..0000000000 --- a/modules/navigation/navigation_mesh_editor_plugin.h +++ /dev/null @@ -1,86 +0,0 @@ -/*************************************************************************/ -/* navigation_mesh_editor_plugin.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#ifndef NAVIGATION_MESH_EDITOR_PLUGIN_H -#define NAVIGATION_MESH_EDITOR_PLUGIN_H - -#ifdef TOOLS_ENABLED - -#include "editor/editor_plugin.h" - -class NavigationRegion3D; - -class NavigationMeshEditor : public Control { - friend class NavigationMeshEditorPlugin; - - GDCLASS(NavigationMeshEditor, Control); - - AcceptDialog *err_dialog; - - HBoxContainer *bake_hbox; - Button *button_bake; - Button *button_reset; - Label *bake_info; - - NavigationRegion3D *node; - - void _bake_pressed(); - void _clear_pressed(); - -protected: - void _node_removed(Node *p_node); - static void _bind_methods(); - void _notification(int p_what); - -public: - void edit(NavigationRegion3D *p_nav_region); - NavigationMeshEditor(); - ~NavigationMeshEditor(); -}; - -class NavigationMeshEditorPlugin : public EditorPlugin { - GDCLASS(NavigationMeshEditorPlugin, EditorPlugin); - - NavigationMeshEditor *navigation_mesh_editor; - -public: - virtual String get_name() const override { return "NavigationMesh"; } - bool has_main_screen() const override { return false; } - virtual void edit(Object *p_object) override; - virtual bool handles(Object *p_object) const override; - virtual void make_visible(bool p_visible) override; - - NavigationMeshEditorPlugin(); - ~NavigationMeshEditorPlugin(); -}; - -#endif - -#endif diff --git a/modules/navigation/register_types.cpp b/modules/navigation/register_types.cpp index e2264e843c..218f2c2937 100644 --- a/modules/navigation/register_types.cpp +++ b/modules/navigation/register_types.cpp @@ -40,7 +40,7 @@ #endif #ifdef TOOLS_ENABLED -#include "navigation_mesh_editor_plugin.h" +#include "editor/navigation_mesh_editor_plugin.h" #endif #ifndef _3D_DISABLED diff --git a/modules/websocket/SCsub b/modules/websocket/SCsub index 63c941c4a8..dc0661995f 100644 --- a/modules/websocket/SCsub +++ b/modules/websocket/SCsub @@ -41,6 +41,8 @@ elif env["builtin_wslay"]: module_obj = [] env_ws.add_source_files(module_obj, "*.cpp") +if env["tools"]: + env_ws.add_source_files(module_obj, "editor/*.cpp") env.modules_sources += module_obj # Needed to force rebuilding the module files when the thirdparty library is updated. diff --git a/modules/websocket/editor/editor_debugger_server_websocket.cpp b/modules/websocket/editor/editor_debugger_server_websocket.cpp new file mode 100644 index 0000000000..0443147d98 --- /dev/null +++ b/modules/websocket/editor/editor_debugger_server_websocket.cpp @@ -0,0 +1,135 @@ +/*************************************************************************/ +/* editor_debugger_server_websocket.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#include "editor_debugger_server_websocket.h" + +#ifdef TOOLS_ENABLED + +#include "../remote_debugger_peer_websocket.h" +#include "core/config/project_settings.h" +#include "editor/editor_log.h" +#include "editor/editor_node.h" +#include "editor/editor_settings.h" + +void EditorDebuggerServerWebSocket::_peer_connected(int p_id, String _protocol) { + pending_peers.push_back(p_id); +} + +void EditorDebuggerServerWebSocket::_peer_disconnected(int p_id, bool p_was_clean) { + if (pending_peers.find(p_id)) { + pending_peers.erase(p_id); + } +} + +void EditorDebuggerServerWebSocket::poll() { + server->poll(); +} + +String EditorDebuggerServerWebSocket::get_uri() const { + return endpoint; +} + +Error EditorDebuggerServerWebSocket::start(const String &p_uri) { + // Default host and port + String bind_host = (String)EditorSettings::get_singleton()->get("network/debug/remote_host"); + int bind_port = (int)EditorSettings::get_singleton()->get("network/debug/remote_port"); + + // Optionally override + if (!p_uri.is_empty() && p_uri != "ws://") { + String scheme, path; + Error err = p_uri.parse_url(scheme, bind_host, bind_port, path); + ERR_FAIL_COND_V(err != OK, ERR_INVALID_PARAMETER); + ERR_FAIL_COND_V(!bind_host.is_valid_ip_address() && bind_host != "*", ERR_INVALID_PARAMETER); + } + + // Set up the server + server->set_bind_ip(bind_host); + Vector compatible_protocols; + compatible_protocols.push_back("binary"); // compatibility with EMSCRIPTEN TCP-to-WebSocket layer. + + // Try listening on ports + const int max_attempts = 5; + for (int attempt = 1;; ++attempt) { + const Error err = server->listen(bind_port, compatible_protocols); + if (err == OK) { + break; + } + if (attempt >= max_attempts) { + EditorNode::get_log()->add_message(vformat("Cannot listen on port %d, remote debugging unavailable.", bind_port), EditorLog::MSG_TYPE_ERROR); + return err; + } + int last_port = bind_port++; + EditorNode::get_log()->add_message(vformat("Cannot listen on port %d, trying %d instead.", last_port, bind_port), EditorLog::MSG_TYPE_WARNING); + } + + // Endpoint that the client should connect to + endpoint = vformat("ws://%s:%d", bind_host, bind_port); + + return OK; +} + +void EditorDebuggerServerWebSocket::stop() { + server->stop(); + pending_peers.clear(); +} + +bool EditorDebuggerServerWebSocket::is_active() const { + return server->is_listening(); +} + +bool EditorDebuggerServerWebSocket::is_connection_available() const { + return pending_peers.size() > 0; +} + +Ref EditorDebuggerServerWebSocket::take_connection() { + ERR_FAIL_COND_V(!is_connection_available(), Ref()); + RemoteDebuggerPeer *peer = memnew(RemoteDebuggerPeerWebSocket(server->get_peer(pending_peers[0]))); + pending_peers.pop_front(); + return peer; +} + +EditorDebuggerServerWebSocket::EditorDebuggerServerWebSocket() { + server = Ref(WebSocketServer::create()); + int max_pkts = (int)GLOBAL_GET("network/limits/debugger/max_queued_messages"); + server->set_buffers(8192, max_pkts, 8192, max_pkts); + server->connect("client_connected", callable_mp(this, &EditorDebuggerServerWebSocket::_peer_connected)); + server->connect("client_disconnected", callable_mp(this, &EditorDebuggerServerWebSocket::_peer_disconnected)); +} + +EditorDebuggerServerWebSocket::~EditorDebuggerServerWebSocket() { + stop(); +} + +EditorDebuggerServer *EditorDebuggerServerWebSocket::create(const String &p_protocol) { + ERR_FAIL_COND_V(p_protocol != "ws://", nullptr); + return memnew(EditorDebuggerServerWebSocket); +} + +#endif // TOOLS_ENABLED diff --git a/modules/websocket/editor/editor_debugger_server_websocket.h b/modules/websocket/editor/editor_debugger_server_websocket.h new file mode 100644 index 0000000000..7c0705302d --- /dev/null +++ b/modules/websocket/editor/editor_debugger_server_websocket.h @@ -0,0 +1,67 @@ +/*************************************************************************/ +/* editor_debugger_server_websocket.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifndef EDITOR_DEBUGGER_SERVER_WEBSOCKET_H +#define EDITOR_DEBUGGER_SERVER_WEBSOCKET_H + +#ifdef TOOLS_ENABLED + +#include "../websocket_server.h" +#include "editor/debugger/editor_debugger_server.h" + +class EditorDebuggerServerWebSocket : public EditorDebuggerServer { + GDCLASS(EditorDebuggerServerWebSocket, EditorDebuggerServer); + +private: + Ref server; + List pending_peers; + String endpoint; + +public: + static EditorDebuggerServer *create(const String &p_protocol); + + void _peer_connected(int p_peer, String p_protocol); + void _peer_disconnected(int p_peer, bool p_was_clean); + + virtual void poll() override; + virtual String get_uri() const override; + virtual Error start(const String &p_uri = "") override; + virtual void stop() override; + virtual bool is_active() const override; + virtual bool is_connection_available() const override; + virtual Ref take_connection() override; + + EditorDebuggerServerWebSocket(); + ~EditorDebuggerServerWebSocket(); +}; + +#endif // TOOLS_ENABLED + +#endif // EDITOR_DEBUGGER_SERVER_WEBSOCKET_H diff --git a/modules/websocket/editor_debugger_server_websocket.cpp b/modules/websocket/editor_debugger_server_websocket.cpp deleted file mode 100644 index 4f1a56f00b..0000000000 --- a/modules/websocket/editor_debugger_server_websocket.cpp +++ /dev/null @@ -1,131 +0,0 @@ -/*************************************************************************/ -/* editor_debugger_server_websocket.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#include "editor_debugger_server_websocket.h" - -#include "core/config/project_settings.h" -#include "editor/editor_log.h" -#include "editor/editor_node.h" -#include "editor/editor_settings.h" -#include "modules/websocket/remote_debugger_peer_websocket.h" - -void EditorDebuggerServerWebSocket::_peer_connected(int p_id, String _protocol) { - pending_peers.push_back(p_id); -} - -void EditorDebuggerServerWebSocket::_peer_disconnected(int p_id, bool p_was_clean) { - if (pending_peers.find(p_id)) { - pending_peers.erase(p_id); - } -} - -void EditorDebuggerServerWebSocket::poll() { - server->poll(); -} - -String EditorDebuggerServerWebSocket::get_uri() const { - return endpoint; -} - -Error EditorDebuggerServerWebSocket::start(const String &p_uri) { - // Default host and port - String bind_host = (String)EditorSettings::get_singleton()->get("network/debug/remote_host"); - int bind_port = (int)EditorSettings::get_singleton()->get("network/debug/remote_port"); - - // Optionally override - if (!p_uri.is_empty() && p_uri != "ws://") { - String scheme, path; - Error err = p_uri.parse_url(scheme, bind_host, bind_port, path); - ERR_FAIL_COND_V(err != OK, ERR_INVALID_PARAMETER); - ERR_FAIL_COND_V(!bind_host.is_valid_ip_address() && bind_host != "*", ERR_INVALID_PARAMETER); - } - - // Set up the server - server->set_bind_ip(bind_host); - Vector compatible_protocols; - compatible_protocols.push_back("binary"); // compatibility with EMSCRIPTEN TCP-to-WebSocket layer. - - // Try listening on ports - const int max_attempts = 5; - for (int attempt = 1;; ++attempt) { - const Error err = server->listen(bind_port, compatible_protocols); - if (err == OK) { - break; - } - if (attempt >= max_attempts) { - EditorNode::get_log()->add_message(vformat("Cannot listen on port %d, remote debugging unavailable.", bind_port), EditorLog::MSG_TYPE_ERROR); - return err; - } - int last_port = bind_port++; - EditorNode::get_log()->add_message(vformat("Cannot listen on port %d, trying %d instead.", last_port, bind_port), EditorLog::MSG_TYPE_WARNING); - } - - // Endpoint that the client should connect to - endpoint = vformat("ws://%s:%d", bind_host, bind_port); - - return OK; -} - -void EditorDebuggerServerWebSocket::stop() { - server->stop(); - pending_peers.clear(); -} - -bool EditorDebuggerServerWebSocket::is_active() const { - return server->is_listening(); -} - -bool EditorDebuggerServerWebSocket::is_connection_available() const { - return pending_peers.size() > 0; -} - -Ref EditorDebuggerServerWebSocket::take_connection() { - ERR_FAIL_COND_V(!is_connection_available(), Ref()); - RemoteDebuggerPeer *peer = memnew(RemoteDebuggerPeerWebSocket(server->get_peer(pending_peers[0]))); - pending_peers.pop_front(); - return peer; -} - -EditorDebuggerServerWebSocket::EditorDebuggerServerWebSocket() { - server = Ref(WebSocketServer::create()); - int max_pkts = (int)GLOBAL_GET("network/limits/debugger/max_queued_messages"); - server->set_buffers(8192, max_pkts, 8192, max_pkts); - server->connect("client_connected", callable_mp(this, &EditorDebuggerServerWebSocket::_peer_connected)); - server->connect("client_disconnected", callable_mp(this, &EditorDebuggerServerWebSocket::_peer_disconnected)); -} - -EditorDebuggerServerWebSocket::~EditorDebuggerServerWebSocket() { - stop(); -} - -EditorDebuggerServer *EditorDebuggerServerWebSocket::create(const String &p_protocol) { - ERR_FAIL_COND_V(p_protocol != "ws://", nullptr); - return memnew(EditorDebuggerServerWebSocket); -} diff --git a/modules/websocket/editor_debugger_server_websocket.h b/modules/websocket/editor_debugger_server_websocket.h deleted file mode 100644 index cc14bf62ba..0000000000 --- a/modules/websocket/editor_debugger_server_websocket.h +++ /dev/null @@ -1,63 +0,0 @@ -/*************************************************************************/ -/* editor_debugger_server_websocket.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#ifndef EDITOR_DEBUGGER_SERVER_WEBSOCKET_H -#define EDITOR_DEBUGGER_SERVER_WEBSOCKET_H - -#include "editor/debugger/editor_debugger_server.h" -#include "modules/websocket/websocket_server.h" - -class EditorDebuggerServerWebSocket : public EditorDebuggerServer { - GDCLASS(EditorDebuggerServerWebSocket, EditorDebuggerServer); - -private: - Ref server; - List pending_peers; - String endpoint; - -public: - static EditorDebuggerServer *create(const String &p_protocol); - - void _peer_connected(int p_peer, String p_protocol); - void _peer_disconnected(int p_peer, bool p_was_clean); - - virtual void poll() override; - virtual String get_uri() const override; - virtual Error start(const String &p_uri = "") override; - virtual void stop() override; - virtual bool is_active() const override; - virtual bool is_connection_available() const override; - virtual Ref take_connection() override; - - EditorDebuggerServerWebSocket(); - ~EditorDebuggerServerWebSocket(); -}; - -#endif // EDITOR_DEBUGGER_SERVER_WEBSOCKET_H diff --git a/modules/websocket/register_types.cpp b/modules/websocket/register_types.cpp index ff900f496f..6d63938d4f 100644 --- a/modules/websocket/register_types.cpp +++ b/modules/websocket/register_types.cpp @@ -29,8 +29,10 @@ /*************************************************************************/ #include "register_types.h" + #include "core/config/project_settings.h" #include "core/error/error_macros.h" + #ifdef JAVASCRIPT_ENABLED #include "emscripten.h" #include "emws_client.h" @@ -40,10 +42,11 @@ #include "wsl_client.h" #include "wsl_server.h" #endif + #ifdef TOOLS_ENABLED #include "editor/debugger/editor_debugger_server.h" +#include "editor/editor_debugger_server_websocket.h" #include "editor/editor_node.h" -#include "editor_debugger_server_websocket.h" #endif #ifdef TOOLS_ENABLED diff --git a/modules/websocket/remote_debugger_peer_websocket.h b/modules/websocket/remote_debugger_peer_websocket.h index ddf5425d81..84f9506625 100644 --- a/modules/websocket/remote_debugger_peer_websocket.h +++ b/modules/websocket/remote_debugger_peer_websocket.h @@ -31,12 +31,13 @@ #ifndef REMOTE_DEBUGGER_PEER_WEBSOCKET_H #define REMOTE_DEBUGGER_PEER_WEBSOCKET_H +#include "core/debugger/remote_debugger_peer.h" + #ifdef JAVASCRIPT_ENABLED -#include "modules/websocket/emws_client.h" +#include "emws_client.h" #else -#include "modules/websocket/wsl_client.h" +#include "wsl_client.h" #endif -#include "core/debugger/remote_debugger_peer.h" class RemoteDebuggerPeerWebSocket : public RemoteDebuggerPeer { Ref ws_client; -- cgit v1.2.3