curve_editor_plugin.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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. #ifndef CURVE_EDITOR_PLUGIN_H
  31. #define CURVE_EDITOR_PLUGIN_H
  32. #include "editor/editor_plugin.h"
  33. #include "editor/editor_resource_preview.h"
  34. #include "scene/resources/curve.h"
  35. // Edits a y(x) curve
  36. class CurveEditor : public Control {
  37. GDCLASS(CurveEditor, Control);
  38. public:
  39. CurveEditor();
  40. Size2 get_minimum_size() const override;
  41. void set_curve(Ref<Curve> curve);
  42. enum PresetID {
  43. PRESET_FLAT0 = 0,
  44. PRESET_FLAT1,
  45. PRESET_LINEAR,
  46. PRESET_EASE_IN,
  47. PRESET_EASE_OUT,
  48. PRESET_SMOOTHSTEP,
  49. PRESET_COUNT
  50. };
  51. enum ContextAction {
  52. CONTEXT_ADD_POINT = 0,
  53. CONTEXT_REMOVE_POINT,
  54. CONTEXT_LINEAR,
  55. CONTEXT_LEFT_LINEAR,
  56. CONTEXT_RIGHT_LINEAR
  57. };
  58. enum TangentIndex {
  59. TANGENT_NONE = -1,
  60. TANGENT_LEFT = 0,
  61. TANGENT_RIGHT = 1
  62. };
  63. protected:
  64. void _notification(int p_what);
  65. private:
  66. virtual void gui_input(const Ref<InputEvent> &p_event) override;
  67. void on_preset_item_selected(int preset_id);
  68. void _curve_changed();
  69. void on_context_menu_item_selected(int action_id);
  70. void open_context_menu(Vector2 pos);
  71. int get_point_at(Vector2 pos) const;
  72. TangentIndex get_tangent_at(Vector2 pos) const;
  73. void add_point(Vector2 pos);
  74. void remove_point(int index);
  75. void toggle_linear(TangentIndex tangent = TANGENT_NONE);
  76. void set_selected_point(int index);
  77. void set_hover_point_index(int index);
  78. void update_view_transform();
  79. Vector2 get_tangent_view_pos(int i, TangentIndex tangent) const;
  80. Vector2 get_view_pos(Vector2 world_pos) const;
  81. Vector2 get_world_pos(Vector2 view_pos) const;
  82. void _draw();
  83. private:
  84. Transform2D _world_to_view;
  85. Ref<Curve> _curve_ref;
  86. PopupMenu *_context_menu = nullptr;
  87. PopupMenu *_presets_menu = nullptr;
  88. Array _undo_data;
  89. bool _has_undo_data;
  90. Vector2 _context_click_pos;
  91. int _selected_point;
  92. int _hover_point;
  93. TangentIndex _selected_tangent;
  94. bool _dragging;
  95. // Constant
  96. float _hover_radius;
  97. float _tangents_length;
  98. };
  99. class EditorInspectorPluginCurve : public EditorInspectorPlugin {
  100. GDCLASS(EditorInspectorPluginCurve, EditorInspectorPlugin);
  101. public:
  102. virtual bool can_handle(Object *p_object) override;
  103. virtual void parse_begin(Object *p_object) override;
  104. };
  105. class CurveEditorPlugin : public EditorPlugin {
  106. GDCLASS(CurveEditorPlugin, EditorPlugin);
  107. public:
  108. CurveEditorPlugin();
  109. virtual String get_name() const override { return "Curve"; }
  110. };
  111. class CurvePreviewGenerator : public EditorResourcePreviewGenerator {
  112. GDCLASS(CurvePreviewGenerator, EditorResourcePreviewGenerator);
  113. public:
  114. virtual bool handles(const String &p_type) const override;
  115. virtual Ref<Texture2D> generate(const Ref<Resource> &p_from, const Size2 &p_size) const override;
  116. };
  117. #endif // CURVE_EDITOR_PLUGIN_H