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