| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 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; |
| 110 |
|
| 111 |
Transform2D _world_to_view; |
| 112 |
|
| 113 |
Ref<Curve> curve; |
| 114 |
|
| 115 |
int selected_index = -1; |
| 116 |
int hovered_index = -1; |
| 117 |
TangentIndex selected_tangent_index = TANGENT_NONE; |
| 118 |
TangentIndex hovered_tangent_index = TANGENT_NONE; |
| 119 |
|
| 120 |
|
| 121 |
const int BASE_POINT_RADIUS = 4; |
| 122 |
const int BASE_HOVER_RADIUS = 10; |
| 123 |
const int BASE_TANGENT_RADIUS = 3; |
| 124 |
const int BASE_TANGENT_HOVER_RADIUS = 8; |
| 125 |
const int BASE_TANGENT_LENGTH = 36; |
| 126 |
|
| 127 |
int point_radius = BASE_POINT_RADIUS; |
| 128 |
int hover_radius = BASE_HOVER_RADIUS; |
| 129 |
int tangent_radius = BASE_TANGENT_RADIUS; |
| 130 |
int tangent_hover_radius = BASE_TANGENT_HOVER_RADIUS; |
| 131 |
int tangent_length = BASE_TANGENT_LENGTH; |
| 132 |
|
| 133 |
enum GrabMode { |
| 134 |
GRAB_NONE, |
| 135 |
GRAB_ADD, |
| 136 |
GRAB_MOVE |
| 137 |
}; |
| 138 |
GrabMode grabbing = GRAB_NONE; |
| 139 |
Vector2 initial_grab_pos; |
| 140 |
int initial_grab_index = -1; |
| 141 |
float initial_grab_left_tangent = 0; |
| 142 |
float initial_grab_right_tangent = 0; |
| 143 |
|
| 144 |
bool snap_enabled = false; |
| 145 |
int snap_count = 10; |
| 146 |
}; |
| 147 |
|
| 148 |
|
| 149 |
class CurveEditor : public VBoxContainer { |
| 150 |
GDCLASS(CurveEditor, VBoxContainer); |
| 151 |
|
| 152 |
|
| 153 |
const int BASE_SPACING = 4; |
| 154 |
int spacing = BASE_SPACING; |
| 155 |
|
| 156 |
Button *snap_button = nullptr; |
| 157 |
EditorSpinSlider *snap_count_edit = nullptr; |
| 158 |
MenuButton *presets_button = nullptr; |
| 159 |
CurveEdit *curve_editor_rect = nullptr; |
| 160 |
|
| 161 |
void _set_snap_enabled(bool p_enabled); |
| 162 |
void _set_snap_count(int p_snap_count); |
| 163 |
void _on_preset_item_selected(int p_preset_id); |
| 164 |
|
| 165 |
protected: |
| 166 |
void _notification(int p_what); |
| 167 |
|
| 168 |
public: |
| 169 |
static const int DEFAULT_SNAP; |
| 170 |
void set_curve(const Ref<Curve> &p_curve); |
| 171 |
|
| 172 |
CurveEditor(); |
| 173 |
}; |
| 174 |
|
| 175 |
class EditorInspectorPluginCurve : public EditorInspectorPlugin { |
| 176 |
GDCLASS(EditorInspectorPluginCurve, EditorInspectorPlugin); |
| 177 |
|
| 178 |
public: |
| 179 |
virtual bool can_handle(Object *p_object) override; |
| 180 |
virtual void parse_begin(Object *p_object) override; |
| 181 |
}; |
| 182 |
|
| 183 |
class CurveEditorPlugin : public EditorPlugin { |
| 184 |
GDCLASS(CurveEditorPlugin, EditorPlugin); |
| 185 |
|
| 186 |
public: |
| 187 |
CurveEditorPlugin(); |
| 188 |
|
| 189 |
virtual String get_plugin_name() const override { return "Curve"; } |
| 190 |
}; |
| 191 |
|
| 192 |
class CurvePreviewGenerator : public EditorResourcePreviewGenerator { |
| 193 |
GDCLASS(CurvePreviewGenerator, EditorResourcePreviewGenerator); |
| 194 |
|
| 195 |
public: |
| 196 |
virtual bool handles(const String &p_type) const override; |
| 197 |
virtual Ref<Texture2D> generate(const Ref<Resource> &p_from, const Size2 &p_size, Dictionary &p_metadata) const override; |
| 198 |
}; |