gradient_editor_plugin.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 "editor/editor_settings.h"
  35. #include "editor/editor_undo_redo_manager.h"
  36. #include "node_3d_editor_plugin.h"
  37. void GradientReverseButton::_notification(int p_what) {
  38. switch (p_what) {
  39. case NOTIFICATION_DRAW: {
  40. Ref<Texture2D> icon = get_theme_icon(SNAME("ReverseGradient"), SNAME("EditorIcons"));
  41. if (is_pressed()) {
  42. draw_texture_rect(icon, Rect2(margin, margin, icon->get_width(), icon->get_height()), false, get_theme_color(SNAME("icon_pressed_color"), SNAME("Button")));
  43. } else {
  44. draw_texture_rect(icon, Rect2(margin, margin, icon->get_width(), icon->get_height()));
  45. }
  46. } break;
  47. }
  48. }
  49. Size2 GradientReverseButton::get_minimum_size() const {
  50. return (get_theme_icon(SNAME("ReverseGradient"), SNAME("EditorIcons"))->get_size() + Size2(margin * 2, margin * 2));
  51. }
  52. ///////////////////////
  53. bool EditorInspectorPluginGradient::can_handle(Object *p_object) {
  54. return Object::cast_to<Gradient>(p_object) != nullptr;
  55. }
  56. void EditorInspectorPluginGradient::parse_begin(Object *p_object) {
  57. Gradient *gradient = Object::cast_to<Gradient>(p_object);
  58. Ref<Gradient> g(gradient);
  59. editor = memnew(GradientEditor);
  60. editor->set_gradient(g);
  61. add_custom_control(editor);
  62. int picker_shape = EDITOR_GET("interface/inspector/default_color_picker_shape");
  63. editor->get_picker()->set_picker_shape((ColorPicker::PickerShapeType)picker_shape);
  64. reverse_btn = memnew(GradientReverseButton);
  65. gradient_tools_hbox = memnew(HBoxContainer);
  66. gradient_tools_hbox->add_child(reverse_btn);
  67. add_custom_control(gradient_tools_hbox);
  68. reverse_btn->connect("pressed", callable_mp(this, &EditorInspectorPluginGradient::_reverse_button_pressed));
  69. reverse_btn->set_tooltip_text(TTR("Reverse/mirror gradient."));
  70. }
  71. void EditorInspectorPluginGradient::_reverse_button_pressed() {
  72. editor->reverse_gradient();
  73. }
  74. GradientEditorPlugin::GradientEditorPlugin() {
  75. Ref<EditorInspectorPluginGradient> plugin;
  76. plugin.instantiate();
  77. add_inspector_plugin(plugin);
  78. }