Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/scene/curve_editor_plugin.h
9896 views
1
/**************************************************************************/
2
/* curve_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/inspector/editor_inspector.h"
34
#include "editor/inspector/editor_resource_preview.h"
35
#include "editor/plugins/editor_plugin.h"
36
#include "scene/resources/curve.h"
37
38
class EditorSpinSlider;
39
class MenuButton;
40
class PopupMenu;
41
42
class CurveEdit : public Control {
43
GDCLASS(CurveEdit, Control);
44
45
public:
46
CurveEdit();
47
48
void set_snap_enabled(bool p_enabled);
49
void set_snap_count(int p_snap_count);
50
void use_preset(int p_preset_id);
51
52
void set_curve(Ref<Curve> p_curve);
53
Ref<Curve> get_curve();
54
55
Size2 get_minimum_size() const override;
56
57
enum PresetID {
58
PRESET_CONSTANT = 0,
59
PRESET_LINEAR,
60
PRESET_EASE_IN,
61
PRESET_EASE_OUT,
62
PRESET_SMOOTHSTEP,
63
PRESET_COUNT
64
};
65
66
enum TangentIndex {
67
TANGENT_NONE = -1,
68
TANGENT_LEFT = 0,
69
TANGENT_RIGHT = 1
70
};
71
72
protected:
73
void _notification(int p_what);
74
static void _bind_methods();
75
76
private:
77
virtual void gui_input(const Ref<InputEvent> &p_event) override;
78
void _curve_changed();
79
80
int get_point_at(const Vector2 &p_pos) const;
81
TangentIndex get_tangent_at(const Vector2 &p_pos) const;
82
83
float get_offset_without_collision(int p_current_index, float p_offset, bool p_prioritize_right = true);
84
85
void add_point(const Vector2 &p_pos);
86
void remove_point(int p_index);
87
void set_point_position(int p_index, const Vector2 &p_pos);
88
89
void set_point_tangents(int p_index, float p_left, float p_right);
90
void set_point_left_tangent(int p_index, float p_tangent);
91
void set_point_right_tangent(int p_index, float p_tangent);
92
void toggle_linear(int p_index, TangentIndex p_tangent = TANGENT_NONE);
93
94
void update_view_transform();
95
96
void plot_curve_accurate(float p_step, const Color &p_line_color, const Color &p_edge_line_color);
97
98
void set_selected_index(int p_index);
99
100
Vector2 get_tangent_view_pos(int p_index, TangentIndex p_tangent) const;
101
Vector2 get_view_pos(const Vector2 &p_world_pos) const;
102
Vector2 get_world_pos(const Vector2 &p_view_pos) const;
103
104
void _redraw();
105
106
private:
107
const float ASPECT_RATIO = 6.f / 13.f;
108
const float LINE_WIDTH = 0.5f;
109
const int STEP_SIZE = 2; // Number of pixels between plot points.
110
111
Transform2D _world_to_view;
112
113
Ref<Curve> curve;
114
PopupMenu *_presets_menu = nullptr;
115
116
int selected_index = -1;
117
int hovered_index = -1;
118
TangentIndex selected_tangent_index = TANGENT_NONE;
119
TangentIndex hovered_tangent_index = TANGENT_NONE;
120
121
// Make sure to use the scaled values below.
122
const int BASE_POINT_RADIUS = 4;
123
const int BASE_HOVER_RADIUS = 10;
124
const int BASE_TANGENT_RADIUS = 3;
125
const int BASE_TANGENT_HOVER_RADIUS = 8;
126
const int BASE_TANGENT_LENGTH = 36;
127
128
int point_radius = BASE_POINT_RADIUS;
129
int hover_radius = BASE_HOVER_RADIUS;
130
int tangent_radius = BASE_TANGENT_RADIUS;
131
int tangent_hover_radius = BASE_TANGENT_HOVER_RADIUS;
132
int tangent_length = BASE_TANGENT_LENGTH;
133
134
enum GrabMode {
135
GRAB_NONE,
136
GRAB_ADD,
137
GRAB_MOVE
138
};
139
GrabMode grabbing = GRAB_NONE;
140
Vector2 initial_grab_pos;
141
int initial_grab_index = -1;
142
float initial_grab_left_tangent = 0;
143
float initial_grab_right_tangent = 0;
144
145
bool snap_enabled = false;
146
int snap_count = 10;
147
};
148
149
// CurveEdit + toolbar
150
class CurveEditor : public VBoxContainer {
151
GDCLASS(CurveEditor, VBoxContainer);
152
153
// Make sure to use the scaled values below.
154
const int BASE_SPACING = 4;
155
int spacing = BASE_SPACING;
156
157
Button *snap_button = nullptr;
158
EditorSpinSlider *snap_count_edit = nullptr;
159
MenuButton *presets_button = nullptr;
160
CurveEdit *curve_editor_rect = nullptr;
161
162
void _set_snap_enabled(bool p_enabled);
163
void _set_snap_count(int p_snap_count);
164
void _on_preset_item_selected(int p_preset_id);
165
166
protected:
167
void _notification(int p_what);
168
169
public:
170
static const int DEFAULT_SNAP;
171
void set_curve(const Ref<Curve> &p_curve);
172
173
CurveEditor();
174
};
175
176
class EditorInspectorPluginCurve : public EditorInspectorPlugin {
177
GDCLASS(EditorInspectorPluginCurve, EditorInspectorPlugin);
178
179
public:
180
virtual bool can_handle(Object *p_object) override;
181
virtual void parse_begin(Object *p_object) override;
182
};
183
184
class CurveEditorPlugin : public EditorPlugin {
185
GDCLASS(CurveEditorPlugin, EditorPlugin);
186
187
public:
188
CurveEditorPlugin();
189
190
virtual String get_plugin_name() const override { return "Curve"; }
191
};
192
193
class CurvePreviewGenerator : public EditorResourcePreviewGenerator {
194
GDCLASS(CurvePreviewGenerator, EditorResourcePreviewGenerator);
195
196
public:
197
virtual bool handles(const String &p_type) const override;
198
virtual Ref<Texture2D> generate(const Ref<Resource> &p_from, const Size2 &p_size, Dictionary &p_metadata) const override;
199
};
200
201