curve_editor_plugin.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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_node.h"
  33. #include "editor/editor_plugin.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;
  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. static void _bind_methods();
  66. private:
  67. void on_gui_input(const Ref<InputEvent> &p_event);
  68. void on_preset_item_selected(int preset_id);
  69. void _curve_changed();
  70. void on_context_menu_item_selected(int action_id);
  71. void open_context_menu(Vector2 pos);
  72. int get_point_at(Vector2 pos) const;
  73. TangentIndex get_tangent_at(Vector2 pos) const;
  74. void add_point(Vector2 pos);
  75. void remove_point(int index);
  76. void toggle_linear(TangentIndex tangent = TANGENT_NONE);
  77. void set_selected_point(int index);
  78. void set_hover_point_index(int index);
  79. void update_view_transform();
  80. Vector2 get_tangent_view_pos(int i, TangentIndex tangent) const;
  81. Vector2 get_view_pos(Vector2 world_pos) const;
  82. Vector2 get_world_pos(Vector2 view_pos) const;
  83. void _draw();
  84. void stroke_rect(Rect2 rect, Color color);
  85. private:
  86. Transform2D _world_to_view;
  87. Ref<Curve> _curve_ref;
  88. PopupMenu *_context_menu;
  89. PopupMenu *_presets_menu;
  90. Array _undo_data;
  91. bool _has_undo_data;
  92. Vector2 _context_click_pos;
  93. int _selected_point;
  94. int _hover_point;
  95. TangentIndex _selected_tangent;
  96. bool _dragging;
  97. // Constant
  98. float _hover_radius;
  99. float _tangents_length;
  100. };
  101. class EditorInspectorPluginCurve : public EditorInspectorPlugin {
  102. GDCLASS(EditorInspectorPluginCurve, EditorInspectorPlugin)
  103. public:
  104. virtual bool can_handle(Object *p_object);
  105. virtual void parse_begin(Object *p_object);
  106. };
  107. class CurveEditorPlugin : public EditorPlugin {
  108. GDCLASS(CurveEditorPlugin, EditorPlugin)
  109. public:
  110. CurveEditorPlugin(EditorNode *p_node);
  111. virtual String get_name() const { return "Curve"; }
  112. };
  113. class CurvePreviewGenerator : public EditorResourcePreviewGenerator {
  114. GDCLASS(CurvePreviewGenerator, EditorResourcePreviewGenerator)
  115. public:
  116. virtual bool handles(const String &p_type) const;
  117. virtual Ref<Texture> generate(const Ref<Resource> &p_from, const Size2 p_size) const;
  118. };
  119. #endif // CURVE_EDITOR_PLUGIN_H