gradient_editor_plugin.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /**************************************************************************/
  2. /* gradient_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 GRADIENT_EDITOR_PLUGIN_H
  31. #define GRADIENT_EDITOR_PLUGIN_H
  32. #include "editor/editor_inspector.h"
  33. #include "editor/plugins/editor_plugin.h"
  34. class EditorSpinSlider;
  35. class ColorPicker;
  36. class PopupPanel;
  37. class GradientTexture1D;
  38. class GradientEdit : public Control {
  39. GDCLASS(GradientEdit, Control);
  40. Ref<Gradient> gradient;
  41. Ref<GradientTexture1D> preview_texture;
  42. PopupPanel *popup = nullptr;
  43. ColorPicker *picker = nullptr;
  44. bool snap_enabled = false;
  45. int snap_count = 10;
  46. enum GrabMode {
  47. GRAB_NONE,
  48. GRAB_ADD,
  49. GRAB_MOVE
  50. };
  51. GrabMode grabbing = GRAB_NONE;
  52. float pre_grab_offset = 0.5;
  53. int pre_grab_index = -1;
  54. int selected_index = -1;
  55. int hovered_index = -1;
  56. // Make sure to use the scaled values below.
  57. const int BASE_SPACING = 4;
  58. const int BASE_HANDLE_WIDTH = 8;
  59. int draw_spacing = BASE_SPACING;
  60. int handle_width = BASE_HANDLE_WIDTH;
  61. int _get_gradient_rect_width() const;
  62. void _color_changed(const Color &p_color);
  63. void _redraw();
  64. int _get_point_at(int p_xpos) const;
  65. int _predict_insertion_index(float p_offset);
  66. void _show_color_picker();
  67. protected:
  68. virtual void gui_input(const Ref<InputEvent> &p_event) override;
  69. void _notification(int p_what);
  70. static void _bind_methods();
  71. public:
  72. void set_gradient(const Ref<Gradient> &p_gradient);
  73. const Ref<Gradient> &get_gradient() const;
  74. ColorPicker *get_picker() const;
  75. PopupPanel *get_popup() const;
  76. void set_selected_index(int p_index);
  77. void add_point(float p_offset, const Color &p_color);
  78. void remove_point(int p_index);
  79. void set_offset(int p_index, float p_offset);
  80. void set_color(int p_index, const Color &p_color);
  81. void reverse_gradient();
  82. void set_snap_enabled(bool p_enabled);
  83. void set_snap_count(int p_count);
  84. GradientEdit();
  85. };
  86. class GradientEditor : public VBoxContainer {
  87. GDCLASS(GradientEditor, VBoxContainer);
  88. Button *reverse_button = nullptr;
  89. Button *snap_button = nullptr;
  90. EditorSpinSlider *snap_count_edit = nullptr;
  91. GradientEdit *gradient_editor_rect = nullptr;
  92. void _set_snap_enabled(bool p_enabled);
  93. void _set_snap_count(int p_count);
  94. protected:
  95. void _notification(int p_what);
  96. public:
  97. static const int DEFAULT_SNAP;
  98. void set_gradient(const Ref<Gradient> &p_gradient);
  99. GradientEditor();
  100. };
  101. class EditorInspectorPluginGradient : public EditorInspectorPlugin {
  102. GDCLASS(EditorInspectorPluginGradient, EditorInspectorPlugin);
  103. public:
  104. virtual bool can_handle(Object *p_object) override;
  105. virtual void parse_begin(Object *p_object) override;
  106. };
  107. class GradientEditorPlugin : public EditorPlugin {
  108. GDCLASS(GradientEditorPlugin, EditorPlugin);
  109. public:
  110. virtual String get_name() const override { return "Gradient"; }
  111. GradientEditorPlugin();
  112. };
  113. #endif // GRADIENT_EDITOR_PLUGIN_H