gradient_editor_plugin.h 4.8 KB

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