Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/scene/3d/path_3d_editor_plugin.h
21138 views
1
/**************************************************************************/
2
/* path_3d_editor_plugin.h */
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
#pragma once
32
33
#include "editor/plugins/editor_plugin.h"
34
#include "editor/scene/3d/node_3d_editor_gizmos.h"
35
#include "scene/3d/camera_3d.h"
36
#include "scene/3d/path_3d.h"
37
38
class HBoxContainer;
39
class MenuButton;
40
class ConfirmationDialog;
41
42
class Path3DGizmo : public EditorNode3DGizmo {
43
GDCLASS(Path3DGizmo, EditorNode3DGizmo);
44
45
// Map handle id to control point id and handle type.
46
enum HandleType {
47
HANDLE_TYPE_IN,
48
HANDLE_TYPE_OUT,
49
HANDLE_TYPE_TILT,
50
};
51
52
struct HandleInfo {
53
int point_idx; // Index of control point.
54
HandleType type; // Type of this handle.
55
};
56
57
Path3D *path = nullptr;
58
Ref<StandardMaterial3D> debug_material;
59
mutable Vector3 original;
60
mutable float orig_in_length;
61
mutable float orig_out_length;
62
63
// Index that should have swapped control points for achieving an outwards curve.
64
int swapped_control_points_idx = -1;
65
bool control_points_overlapped = false;
66
67
// Cache information of secondary handles.
68
Vector<HandleInfo> _secondary_handles_info;
69
70
void _update_transform_gizmo();
71
72
public:
73
virtual String get_handle_name(int p_id, bool p_secondary) const override;
74
virtual Variant get_handle_value(int p_id, bool p_secondary) const override;
75
virtual void set_handle(int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) override;
76
virtual void commit_handle(int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel = false) override;
77
78
virtual void redraw() override;
79
Path3DGizmo(Path3D *p_path = nullptr);
80
};
81
82
class Path3DGizmoPlugin : public EditorNode3DGizmoPlugin {
83
GDCLASS(Path3DGizmoPlugin, EditorNode3DGizmoPlugin);
84
85
// Locking basis is meant to ensure a predictable behavior during translation of the curve points in "local space transform mode".
86
// Without the locking, the gizmo/point, in "local space transform mode", wouldn't follow a straight path and would curve and twitch in an unpredictable way.
87
HashMap<int, Basis> transformation_locked_basis;
88
89
protected:
90
Ref<EditorNode3DGizmo> create_gizmo(Node3D *p_spatial) override;
91
92
public:
93
virtual bool has_gizmo(Node3D *p_spatial) override;
94
String get_gizmo_name() const override;
95
96
virtual void redraw(EditorNode3DGizmo *p_gizmo) override;
97
98
virtual int subgizmos_intersect_ray(const EditorNode3DGizmo *p_gizmo, Camera3D *p_camera, const Vector2 &p_point) const override;
99
virtual Vector<int> subgizmos_intersect_frustum(const EditorNode3DGizmo *p_gizmo, const Camera3D *p_camera, const Vector<Plane> &p_frustum) const override;
100
virtual Transform3D get_subgizmo_transform(const EditorNode3DGizmo *p_gizmo, int p_id) const override;
101
virtual void set_subgizmo_transform(const EditorNode3DGizmo *p_gizmo, int p_id, Transform3D p_transform) override;
102
virtual void commit_subgizmos(const EditorNode3DGizmo *p_gizmo, const Vector<int> &p_ids, const Vector<Transform3D> &p_restore, bool p_cancel = false) override;
103
104
int get_priority() const override;
105
Path3DGizmoPlugin();
106
};
107
108
class Path3DEditorPlugin : public EditorPlugin {
109
GDCLASS(Path3DEditorPlugin, EditorPlugin);
110
111
friend class Path3DGizmo;
112
friend class Path3DGizmoPlugin;
113
114
Ref<Path3DGizmoPlugin> path_3d_gizmo_plugin;
115
116
HBoxContainer *topmenu_bar = nullptr;
117
118
HBoxContainer *toolbar = nullptr;
119
Button *curve_create = nullptr;
120
Button *curve_edit = nullptr;
121
Button *curve_edit_curve = nullptr;
122
Button *curve_edit_tilt = nullptr;
123
Button *curve_del = nullptr;
124
Button *curve_closed = nullptr;
125
Button *curve_clear_points = nullptr;
126
MenuButton *handle_menu = nullptr;
127
128
Button *create_curve_button = nullptr;
129
ConfirmationDialog *clear_points_dialog = nullptr;
130
131
enum Mode {
132
MODE_CREATE,
133
MODE_EDIT,
134
MODE_EDIT_CURVE,
135
MODE_EDIT_TILT,
136
MODE_DELETE,
137
ACTION_CLOSE
138
};
139
140
Path3D *path = nullptr;
141
142
void _update_theme();
143
void _update_toolbar();
144
145
void _mode_changed(int p_mode);
146
void _toggle_closed_curve();
147
void _handle_option_pressed(int p_option);
148
bool handle_clicked = false;
149
bool mirror_handle_angle = true;
150
bool mirror_handle_length = true;
151
bool snap_to_collider = true;
152
153
void _create_curve();
154
void _confirm_clear_points();
155
void _clear_points();
156
void _clear_curve_points();
157
void _restore_curve_points(const PackedVector3Array &p_points);
158
159
enum HandleOption {
160
HANDLE_OPTION_ANGLE,
161
HANDLE_OPTION_LENGTH,
162
HANDLE_OPTION_SNAP_COLLIDER,
163
};
164
165
protected:
166
virtual void _notification(int p_what);
167
static void _bind_methods();
168
169
public:
170
Path3D *get_edited_path() { return path; }
171
172
inline static Path3DEditorPlugin *singleton = nullptr;
173
virtual EditorPlugin::AfterGUIInput forward_3d_gui_input(Camera3D *p_camera, const Ref<InputEvent> &p_event) override;
174
175
virtual String get_plugin_name() const override { return "Path3D"; }
176
bool has_main_screen() const override { return false; }
177
virtual void edit(Object *p_object) override;
178
virtual bool handles(Object *p_object) const override;
179
virtual void make_visible(bool p_visible) override;
180
181
bool mirror_angle_enabled() { return mirror_handle_angle; }
182
bool mirror_length_enabled() { return mirror_handle_length; }
183
bool is_handle_clicked() { return handle_clicked; }
184
void set_handle_clicked(bool clicked) { handle_clicked = clicked; }
185
186
Path3DEditorPlugin();
187
188
private:
189
struct EditData {
190
Vector3 click_ray_dir;
191
Vector3 click_ray_pos;
192
Vector3 origin;
193
Point2 mouse_pos;
194
bool show_rotation_line = false;
195
Ref<Path3DGizmo> gizmo;
196
int gizmo_handle = 0;
197
bool gizmo_handle_secondary = false;
198
Camera3D *gizmo_camera;
199
bool waiting_point_physics = false;
200
bool waiting_handle_physics = false;
201
bool in_physics_frame = false;
202
} _edit;
203
};
204
205