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