1 /*************************************************************************/ 2 /* curve_editor_plugin.h */ 3 /*************************************************************************/ 4 /* This file is part of: */ 5 /* GODOT ENGINE */ 6 /* https://godotengine.org */ 7 /*************************************************************************/ 8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ 9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ 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_node.h" 35 #include "editor/editor_plugin.h" 36 #include "editor/editor_resource_preview.h" 37 #include "scene/resources/curve.h" 38 39 // Edits a y(x) curve 40 class CurveEditor : public Control { 41 GDCLASS(CurveEditor, Control); 42 43 public: 44 CurveEditor(); 45 46 Size2 get_minimum_size() const; 47 48 void set_curve(Ref<Curve> curve); 49 50 enum PresetID { 51 PRESET_FLAT0 = 0, 52 PRESET_FLAT1, 53 PRESET_LINEAR, 54 PRESET_EASE_IN, 55 PRESET_EASE_OUT, 56 PRESET_SMOOTHSTEP, 57 PRESET_COUNT 58 }; 59 60 enum ContextAction { 61 CONTEXT_ADD_POINT = 0, 62 CONTEXT_REMOVE_POINT, 63 CONTEXT_LINEAR, 64 CONTEXT_LEFT_LINEAR, 65 CONTEXT_RIGHT_LINEAR 66 }; 67 68 enum TangentIndex { 69 TANGENT_NONE = -1, 70 TANGENT_LEFT = 0, 71 TANGENT_RIGHT = 1 72 }; 73 74 protected: 75 void _notification(int p_what); 76 77 static void _bind_methods(); 78 79 private: 80 void on_gui_input(const Ref<InputEvent> &p_event); 81 void on_preset_item_selected(int preset_id); 82 void _curve_changed(); 83 void on_context_menu_item_selected(int action_id); 84 85 void open_context_menu(Vector2 pos); 86 int get_point_at(Vector2 pos) const; 87 TangentIndex get_tangent_at(Vector2 pos) const; 88 void add_point(Vector2 pos); 89 void remove_point(int index); 90 void toggle_linear(TangentIndex tangent = TANGENT_NONE); 91 void set_selected_point(int index); 92 void set_hover_point_index(int index); 93 void update_view_transform(); 94 95 Vector2 get_tangent_view_pos(int i, TangentIndex tangent) const; 96 Vector2 get_view_pos(Vector2 world_pos) const; 97 Vector2 get_world_pos(Vector2 view_pos) const; 98 99 void _draw(); 100 101 private: 102 Transform2D _world_to_view; 103 104 Ref<Curve> _curve_ref; 105 PopupMenu *_context_menu; 106 PopupMenu *_presets_menu; 107 108 Array _undo_data; 109 bool _has_undo_data; 110 111 Vector2 _context_click_pos; 112 int _selected_point; 113 int _hover_point; 114 TangentIndex _selected_tangent; 115 bool _dragging; 116 117 // Constant 118 float _hover_radius; 119 float _tangents_length; 120 }; 121 122 class EditorInspectorPluginCurve : public EditorInspectorPlugin { 123 GDCLASS(EditorInspectorPluginCurve, EditorInspectorPlugin); 124 125 public: 126 virtual bool can_handle(Object *p_object); 127 virtual void parse_begin(Object *p_object); 128 }; 129 130 class CurveEditorPlugin : public EditorPlugin { 131 GDCLASS(CurveEditorPlugin, EditorPlugin); 132 133 public: 134 CurveEditorPlugin(EditorNode *p_node); 135 get_name()136 virtual String get_name() const { return "Curve"; } 137 }; 138 139 class CurvePreviewGenerator : public EditorResourcePreviewGenerator { 140 GDCLASS(CurvePreviewGenerator, EditorResourcePreviewGenerator); 141 142 public: 143 virtual bool handles(const String &p_type) const; 144 virtual Ref<Texture> generate(const Ref<Resource> &p_from, const Size2 &p_size) const; 145 }; 146 147 #endif // CURVE_EDITOR_PLUGIN_H 148