| 1 | /**************************************************************************/ |
| 2 | /* soft_body_3d_gizmo_plugin.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "soft_body_3d_gizmo_plugin.h" |
| 32 | |
| 33 | #include "editor/editor_undo_redo_manager.h" |
| 34 | #include "scene/3d/physics/soft_body_3d.h" |
| 35 | |
| 36 | SoftBody3DGizmoPlugin::SoftBody3DGizmoPlugin() { |
| 37 | Color gizmo_color = SceneTree::get_singleton()->get_debug_collisions_color(); |
| 38 | create_material("shape_material", gizmo_color); |
| 39 | create_handle_material("handles"); |
| 40 | } |
| 41 | |
| 42 | bool SoftBody3DGizmoPlugin::has_gizmo(Node3D *p_spatial) { |
| 43 | return Object::cast_to<SoftBody3D>(p_spatial) != nullptr; |
| 44 | } |
| 45 | |
| 46 | String SoftBody3DGizmoPlugin::get_gizmo_name() const { |
| 47 | return "SoftBody3D"; |
| 48 | } |
| 49 | |
| 50 | int SoftBody3DGizmoPlugin::get_priority() const { |
| 51 | return -1; |
| 52 | } |
| 53 | |
| 54 | bool SoftBody3DGizmoPlugin::is_selectable_when_hidden() const { |
| 55 | return true; |
| 56 | } |
| 57 | |
| 58 | bool SoftBody3DGizmoPlugin::can_commit_handle_on_click() const { |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | void SoftBody3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) { |
| 63 | SoftBody3D *soft_body = Object::cast_to<SoftBody3D>(p_gizmo->get_node_3d()); |
| 64 | |
| 65 | p_gizmo->clear(); |
| 66 | |
| 67 | if (!soft_body || soft_body->get_mesh().is_null()) { |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | // find mesh |
| 72 | |
| 73 | Vector<Vector3> lines; |
| 74 | |
| 75 | soft_body->get_mesh()->generate_debug_mesh_lines(lines); |
| 76 | |
| 77 | if (!lines.size()) { |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | Ref<TriangleMesh> tm = soft_body->get_mesh()->generate_triangle_mesh(); |
| 82 | |
| 83 | Vector<Vector3> points; |
| 84 | for (int i = 0; i < soft_body->get_mesh()->get_surface_count(); i++) { |
| 85 | Array arrays = soft_body->get_mesh()->surface_get_arrays(i); |
| 86 | ERR_CONTINUE(arrays.is_empty()); |
| 87 | |
| 88 | const Vector<Vector3> &vertices = arrays[Mesh::ARRAY_VERTEX]; |
| 89 | points.append_array(vertices); |
| 90 | } |
| 91 | |
| 92 | Ref<Material> material = get_material("shape_material", p_gizmo); |
| 93 | |
| 94 | p_gizmo->add_lines(lines, material); |
| 95 | p_gizmo->add_handles(points, get_material("handles")); |
| 96 | p_gizmo->add_collision_triangles(tm); |
| 97 | } |
| 98 | |
| 99 | String SoftBody3DGizmoPlugin::get_handle_name(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const { |
| 100 | return "SoftBody3D pin point"; |
| 101 | } |
| 102 | |
| 103 | Variant SoftBody3DGizmoPlugin::get_handle_value(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const { |
| 104 | SoftBody3D *soft_body = Object::cast_to<SoftBody3D>(p_gizmo->get_node_3d()); |
| 105 | return Variant(soft_body->is_point_pinned(p_id)); |
| 106 | } |
| 107 | |
| 108 | void SoftBody3DGizmoPlugin::commit_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel) { |
| 109 | SoftBody3D *soft_body = Object::cast_to<SoftBody3D>(p_gizmo->get_node_3d()); |
| 110 | const bool is_pinned = soft_body->is_point_pinned(p_id); |
| 111 | |
| 112 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
| 113 | undo_redo->create_action(vformat(is_pinned ? TTR("Remove SoftBody3D pinned point %d") : TTR("Add SoftBody3D pinned point %d"), p_id)); |
| 114 | undo_redo->add_do_method(soft_body, "set_point_pinned", p_id, !is_pinned); |
| 115 | undo_redo->add_do_method(soft_body, "update_gizmos"); |
| 116 | undo_redo->add_undo_method(soft_body, "set_point_pinned", p_id, is_pinned); |
| 117 | undo_redo->add_undo_method(soft_body, "update_gizmos"); |
| 118 | undo_redo->commit_action(); |
| 119 | } |
| 120 | |
| 121 | bool SoftBody3DGizmoPlugin::is_handle_highlighted(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const { |
| 122 | SoftBody3D *soft_body = Object::cast_to<SoftBody3D>(p_gizmo->get_node_3d()); |
| 123 | return soft_body->is_point_pinned(p_id); |
| 124 | } |