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