curve_editor_plugin.h 6.4 KB

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