| 1 | /**************************************************************************/ |
| 2 | /* decal_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 "decal_gizmo_plugin.h" |
| 32 | |
| 33 | #include "editor/editor_node.h" |
| 34 | #include "editor/editor_settings.h" |
| 35 | #include "editor/editor_string_names.h" |
| 36 | #include "editor/editor_undo_redo_manager.h" |
| 37 | #include "editor/plugins/gizmos/gizmo_3d_helper.h" |
| 38 | #include "editor/plugins/node_3d_editor_plugin.h" |
| 39 | #include "scene/3d/decal.h" |
| 40 | |
| 41 | DecalGizmoPlugin::DecalGizmoPlugin() { |
| 42 | helper.instantiate(); |
| 43 | Color gizmo_color = EDITOR_DEF("editors/3d_gizmos/gizmo_colors/decal" , Color(0.6, 0.5, 1.0)); |
| 44 | |
| 45 | create_icon_material("decal_icon" , EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("GizmoDecal" ), EditorStringName(EditorIcons))); |
| 46 | |
| 47 | create_material("decal_material" , gizmo_color); |
| 48 | |
| 49 | create_handle_material("handles" ); |
| 50 | } |
| 51 | |
| 52 | DecalGizmoPlugin::~DecalGizmoPlugin() { |
| 53 | } |
| 54 | |
| 55 | bool DecalGizmoPlugin::has_gizmo(Node3D *p_spatial) { |
| 56 | return Object::cast_to<Decal>(p_spatial) != nullptr; |
| 57 | } |
| 58 | |
| 59 | String DecalGizmoPlugin::get_gizmo_name() const { |
| 60 | return "Decal" ; |
| 61 | } |
| 62 | |
| 63 | int DecalGizmoPlugin::get_priority() const { |
| 64 | return -1; |
| 65 | } |
| 66 | |
| 67 | String DecalGizmoPlugin::get_handle_name(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const { |
| 68 | return helper->box_get_handle_name(p_id); |
| 69 | } |
| 70 | |
| 71 | Variant DecalGizmoPlugin::get_handle_value(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const { |
| 72 | Decal *decal = Object::cast_to<Decal>(p_gizmo->get_node_3d()); |
| 73 | return decal->get_size(); |
| 74 | } |
| 75 | |
| 76 | void DecalGizmoPlugin::begin_handle_action(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) { |
| 77 | helper->initialize_handle_action(get_handle_value(p_gizmo, p_id, p_secondary), p_gizmo->get_node_3d()->get_global_transform()); |
| 78 | } |
| 79 | |
| 80 | void DecalGizmoPlugin::set_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) { |
| 81 | Decal *decal = Object::cast_to<Decal>(p_gizmo->get_node_3d()); |
| 82 | Vector3 size = decal->get_size(); |
| 83 | |
| 84 | Vector3 sg[2]; |
| 85 | helper->get_segment(p_camera, p_point, sg); |
| 86 | |
| 87 | Vector3 position; |
| 88 | helper->box_set_handle(sg, p_id, size, position); |
| 89 | decal->set_size(size); |
| 90 | decal->set_global_position(position); |
| 91 | } |
| 92 | |
| 93 | void DecalGizmoPlugin::commit_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel) { |
| 94 | helper->box_commit_handle(TTR("Change Decal Size" ), p_cancel, p_gizmo->get_node_3d()); |
| 95 | } |
| 96 | |
| 97 | void DecalGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) { |
| 98 | Decal *decal = Object::cast_to<Decal>(p_gizmo->get_node_3d()); |
| 99 | |
| 100 | p_gizmo->clear(); |
| 101 | |
| 102 | Vector<Vector3> lines; |
| 103 | Vector3 size = decal->get_size(); |
| 104 | |
| 105 | AABB aabb; |
| 106 | aabb.position = -size / 2; |
| 107 | aabb.size = size; |
| 108 | |
| 109 | for (int i = 0; i < 12; i++) { |
| 110 | Vector3 a, b; |
| 111 | aabb.get_edge(i, a, b); |
| 112 | if (a.y == b.y) { |
| 113 | lines.push_back(a); |
| 114 | lines.push_back(b); |
| 115 | } else { |
| 116 | Vector3 ah = a.lerp(b, 0.2); |
| 117 | lines.push_back(a); |
| 118 | lines.push_back(ah); |
| 119 | Vector3 bh = b.lerp(a, 0.2); |
| 120 | lines.push_back(b); |
| 121 | lines.push_back(bh); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | float half_size_y = size.y / 2; |
| 126 | lines.push_back(Vector3(0, half_size_y, 0)); |
| 127 | lines.push_back(Vector3(0, half_size_y * 1.2, 0)); |
| 128 | |
| 129 | Vector<Vector3> handles = helper->box_get_handles(decal->get_size()); |
| 130 | Ref<Material> material = get_material("decal_material" , p_gizmo); |
| 131 | const Ref<Material> icon = get_material("decal_icon" , p_gizmo); |
| 132 | |
| 133 | p_gizmo->add_lines(lines, material); |
| 134 | p_gizmo->add_unscaled_billboard(icon, 0.05); |
| 135 | p_gizmo->add_handles(handles, get_material("handles" )); |
| 136 | } |
| 137 | |