gradient_editor_plugin.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*************************************************************************/
  2. /* gradient_editor_plugin.cpp */
  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. #include "gradient_editor_plugin.h"
  31. #include "canvas_item_editor_plugin.h"
  32. #include "editor/editor_node.h"
  33. #include "editor/editor_scale.h"
  34. #include "node_3d_editor_plugin.h"
  35. Size2 GradientEditor::get_minimum_size() const {
  36. return Size2(0, 60) * EDSCALE;
  37. }
  38. void GradientEditor::_gradient_changed() {
  39. if (editing) {
  40. return;
  41. }
  42. editing = true;
  43. Vector<Gradient::Point> points = gradient->get_points();
  44. set_points(points);
  45. set_interpolation_mode(gradient->get_interpolation_mode());
  46. update();
  47. editing = false;
  48. }
  49. void GradientEditor::_ramp_changed() {
  50. editing = true;
  51. UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
  52. undo_redo->create_action(TTR("Gradient Edited"), UndoRedo::MERGE_ENDS);
  53. undo_redo->add_do_method(gradient.ptr(), "set_offsets", get_offsets());
  54. undo_redo->add_do_method(gradient.ptr(), "set_colors", get_colors());
  55. undo_redo->add_do_method(gradient.ptr(), "set_interpolation_mode", get_interpolation_mode());
  56. undo_redo->add_undo_method(gradient.ptr(), "set_offsets", gradient->get_offsets());
  57. undo_redo->add_undo_method(gradient.ptr(), "set_colors", gradient->get_colors());
  58. undo_redo->add_undo_method(gradient.ptr(), "set_interpolation_mode", gradient->get_interpolation_mode());
  59. undo_redo->commit_action();
  60. editing = false;
  61. }
  62. void GradientEditor::_bind_methods() {
  63. }
  64. void GradientEditor::set_gradient(const Ref<Gradient> &p_gradient) {
  65. gradient = p_gradient;
  66. connect("ramp_changed", callable_mp(this, &GradientEditor::_ramp_changed));
  67. gradient->connect("changed", callable_mp(this, &GradientEditor::_gradient_changed));
  68. set_points(gradient->get_points());
  69. set_interpolation_mode(gradient->get_interpolation_mode());
  70. }
  71. void GradientEditor::reverse_gradient() {
  72. gradient->reverse();
  73. set_points(gradient->get_points());
  74. emit_signal(SNAME("ramp_changed"));
  75. update();
  76. }
  77. GradientEditor::GradientEditor() {
  78. GradientEdit::get_popup()->connect("about_to_popup", callable_mp(EditorNode::get_singleton(), &EditorNode::setup_color_picker).bind(GradientEdit::get_picker()));
  79. editing = false;
  80. }
  81. ///////////////////////
  82. void GradientReverseButton::_notification(int p_what) {
  83. switch (p_what) {
  84. case NOTIFICATION_DRAW: {
  85. Ref<Texture2D> icon = get_theme_icon(SNAME("ReverseGradient"), SNAME("EditorIcons"));
  86. if (is_pressed()) {
  87. draw_texture_rect(icon, Rect2(margin, margin, icon->get_width(), icon->get_height()), false, get_theme_color(SNAME("icon_pressed_color"), SNAME("Button")));
  88. } else {
  89. draw_texture_rect(icon, Rect2(margin, margin, icon->get_width(), icon->get_height()));
  90. }
  91. } break;
  92. }
  93. }
  94. Size2 GradientReverseButton::get_minimum_size() const {
  95. return (get_theme_icon(SNAME("ReverseGradient"), SNAME("EditorIcons"))->get_size() + Size2(margin * 2, margin * 2));
  96. }
  97. ///////////////////////
  98. bool EditorInspectorPluginGradient::can_handle(Object *p_object) {
  99. return Object::cast_to<Gradient>(p_object) != nullptr;
  100. }
  101. void EditorInspectorPluginGradient::parse_begin(Object *p_object) {
  102. Gradient *gradient = Object::cast_to<Gradient>(p_object);
  103. Ref<Gradient> g(gradient);
  104. editor = memnew(GradientEditor);
  105. editor->set_gradient(g);
  106. add_custom_control(editor);
  107. int picker_shape = EDITOR_GET("interface/inspector/default_color_picker_shape");
  108. editor->get_picker()->set_picker_shape((ColorPicker::PickerShapeType)picker_shape);
  109. reverse_btn = memnew(GradientReverseButton);
  110. gradient_tools_hbox = memnew(HBoxContainer);
  111. gradient_tools_hbox->add_child(reverse_btn);
  112. add_custom_control(gradient_tools_hbox);
  113. reverse_btn->connect("pressed", callable_mp(this, &EditorInspectorPluginGradient::_reverse_button_pressed));
  114. reverse_btn->set_tooltip(TTR("Reverse/mirror gradient."));
  115. }
  116. void EditorInspectorPluginGradient::_reverse_button_pressed() {
  117. editor->reverse_gradient();
  118. }
  119. GradientEditorPlugin::GradientEditorPlugin() {
  120. Ref<EditorInspectorPluginGradient> plugin;
  121. plugin.instantiate();
  122. add_inspector_plugin(plugin);
  123. }