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_settings.h"
34#include "editor/plugins/node_3d_editor_plugin.h"
35#include "scene/3d/soft_body_3d.h"
36
37SoftBody3DGizmoPlugin::SoftBody3DGizmoPlugin() {
38 Color gizmo_color = EDITOR_GET("editors/3d_gizmos/gizmo_colors/shape");
39 create_material("shape_material", gizmo_color);
40 create_handle_material("handles");
41}
42
43bool SoftBody3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {
44 return Object::cast_to<SoftBody3D>(p_spatial) != nullptr;
45}
46
47String SoftBody3DGizmoPlugin::get_gizmo_name() const {
48 return "SoftBody3D";
49}
50
51int SoftBody3DGizmoPlugin::get_priority() const {
52 return -1;
53}
54
55bool SoftBody3DGizmoPlugin::is_selectable_when_hidden() const {
56 return true;
57}
58
59void SoftBody3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
60 SoftBody3D *soft_body = Object::cast_to<SoftBody3D>(p_gizmo->get_node_3d());
61
62 p_gizmo->clear();
63
64 if (!soft_body || soft_body->get_mesh().is_null()) {
65 return;
66 }
67
68 // find mesh
69
70 Vector<Vector3> lines;
71
72 soft_body->get_mesh()->generate_debug_mesh_lines(lines);
73
74 if (!lines.size()) {
75 return;
76 }
77
78 Ref<TriangleMesh> tm = soft_body->get_mesh()->generate_triangle_mesh();
79
80 Vector<Vector3> points;
81 for (int i = 0; i < soft_body->get_mesh()->get_surface_count(); i++) {
82 Array arrays = soft_body->get_mesh()->surface_get_arrays(i);
83 ERR_CONTINUE(arrays.is_empty());
84
85 const Vector<Vector3> &vertices = arrays[Mesh::ARRAY_VERTEX];
86 points.append_array(vertices);
87 }
88
89 Ref<Material> material = get_material("shape_material", p_gizmo);
90
91 p_gizmo->add_lines(lines, material);
92 p_gizmo->add_handles(points, get_material("handles"));
93 p_gizmo->add_collision_triangles(tm);
94}
95
96String SoftBody3DGizmoPlugin::get_handle_name(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
97 return "SoftBody3D pin point";
98}
99
100Variant SoftBody3DGizmoPlugin::get_handle_value(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
101 SoftBody3D *soft_body = Object::cast_to<SoftBody3D>(p_gizmo->get_node_3d());
102 return Variant(soft_body->is_point_pinned(p_id));
103}
104
105void SoftBody3DGizmoPlugin::commit_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel) {
106 SoftBody3D *soft_body = Object::cast_to<SoftBody3D>(p_gizmo->get_node_3d());
107 soft_body->pin_point_toggle(p_id);
108}
109
110bool SoftBody3DGizmoPlugin::is_handle_highlighted(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
111 SoftBody3D *soft_body = Object::cast_to<SoftBody3D>(p_gizmo->get_node_3d());
112 return soft_body->is_point_pinned(p_id);
113}
114