curve_editor_plugin.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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(const Vector2 &p_pos) const;
  69. TangentIndex get_tangent_at(const 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(const Vector2 &p_pos);
  72. void remove_point(int p_index);
  73. void set_point_position(int p_index, const 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 plot_curve_accurate(float p_step, const Color &p_line_color, const Color &p_edge_line_color);
  80. void set_selected_index(int p_index);
  81. Vector2 get_tangent_view_pos(int p_index, TangentIndex p_tangent) const;
  82. Vector2 get_view_pos(const Vector2 &p_world_pos) const;
  83. Vector2 get_world_pos(const Vector2 &p_view_pos) const;
  84. void _redraw();
  85. private:
  86. const float ASPECT_RATIO = 6.f / 13.f;
  87. const float LINE_WIDTH = 0.5f;
  88. const int STEP_SIZE = 2; // Number of pixels between plot points.
  89. Transform2D _world_to_view;
  90. Ref<Curve> curve;
  91. PopupMenu *_presets_menu = nullptr;
  92. int selected_index = -1;
  93. int hovered_index = -1;
  94. TangentIndex selected_tangent_index = TANGENT_NONE;
  95. TangentIndex hovered_tangent_index = TANGENT_NONE;
  96. // Make sure to use the scaled values below.
  97. const int BASE_POINT_RADIUS = 4;
  98. const int BASE_HOVER_RADIUS = 10;
  99. const int BASE_TANGENT_RADIUS = 3;
  100. const int BASE_TANGENT_HOVER_RADIUS = 8;
  101. const int BASE_TANGENT_LENGTH = 36;
  102. int point_radius = BASE_POINT_RADIUS;
  103. int hover_radius = BASE_HOVER_RADIUS;
  104. int tangent_radius = BASE_TANGENT_RADIUS;
  105. int tangent_hover_radius = BASE_TANGENT_HOVER_RADIUS;
  106. int tangent_length = BASE_TANGENT_LENGTH;
  107. enum GrabMode {
  108. GRAB_NONE,
  109. GRAB_ADD,
  110. GRAB_MOVE
  111. };
  112. GrabMode grabbing = GRAB_NONE;
  113. Vector2 initial_grab_pos;
  114. int initial_grab_index = -1;
  115. float initial_grab_left_tangent = 0;
  116. float initial_grab_right_tangent = 0;
  117. bool snap_enabled = false;
  118. int snap_count = 10;
  119. };
  120. // CurveEdit + toolbar
  121. class CurveEditor : public VBoxContainer {
  122. GDCLASS(CurveEditor, VBoxContainer);
  123. // Make sure to use the scaled values below.
  124. const int BASE_SPACING = 4;
  125. int spacing = BASE_SPACING;
  126. Button *snap_button = nullptr;
  127. EditorSpinSlider *snap_count_edit = nullptr;
  128. MenuButton *presets_button = nullptr;
  129. CurveEdit *curve_editor_rect = nullptr;
  130. void _set_snap_enabled(bool p_enabled);
  131. void _set_snap_count(int p_snap_count);
  132. void _on_preset_item_selected(int p_preset_id);
  133. protected:
  134. void _notification(int p_what);
  135. public:
  136. static const int DEFAULT_SNAP;
  137. void set_curve(const Ref<Curve> &p_curve);
  138. CurveEditor();
  139. };
  140. class EditorInspectorPluginCurve : public EditorInspectorPlugin {
  141. GDCLASS(EditorInspectorPluginCurve, EditorInspectorPlugin);
  142. public:
  143. virtual bool can_handle(Object *p_object) override;
  144. virtual void parse_begin(Object *p_object) override;
  145. };
  146. class CurveEditorPlugin : public EditorPlugin {
  147. GDCLASS(CurveEditorPlugin, EditorPlugin);
  148. public:
  149. CurveEditorPlugin();
  150. virtual String get_plugin_name() const override { return "Curve"; }
  151. };
  152. class CurvePreviewGenerator : public EditorResourcePreviewGenerator {
  153. GDCLASS(CurvePreviewGenerator, EditorResourcePreviewGenerator);
  154. public:
  155. virtual bool handles(const String &p_type) const override;
  156. virtual Ref<Texture2D> generate(const Ref<Resource> &p_from, const Size2 &p_size, Dictionary &p_metadata) const override;
  157. };
  158. #endif // CURVE_EDITOR_PLUGIN_H