style_box_editor_plugin.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**************************************************************************/
  2. /* style_box_editor_plugin.cpp */
  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. #include "style_box_editor_plugin.h"
  31. #include "editor/themes/editor_scale.h"
  32. #include "scene/gui/button.h"
  33. #include "scene/resources/style_box_texture.h"
  34. bool StyleBoxPreview::grid_preview_enabled = true;
  35. void StyleBoxPreview::_grid_preview_toggled(bool p_active) {
  36. grid_preview_enabled = p_active;
  37. queue_redraw();
  38. }
  39. void StyleBoxPreview::edit(const Ref<StyleBox> &p_stylebox) {
  40. if (stylebox.is_valid()) {
  41. stylebox->disconnect_changed(callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw));
  42. }
  43. stylebox = p_stylebox;
  44. if (stylebox.is_valid()) {
  45. stylebox->connect_changed(callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw));
  46. }
  47. Ref<StyleBoxTexture> sbt = stylebox;
  48. grid_preview->set_visible(sbt.is_valid());
  49. queue_redraw();
  50. }
  51. void StyleBoxPreview::_notification(int p_what) {
  52. switch (p_what) {
  53. case NOTIFICATION_THEME_CHANGED: {
  54. set_texture(get_editor_theme_icon(SNAME("Checkerboard")));
  55. grid_preview->set_button_icon(get_editor_theme_icon(SNAME("StyleBoxGrid")));
  56. } break;
  57. case NOTIFICATION_DRAW: {
  58. _redraw();
  59. } break;
  60. }
  61. }
  62. void StyleBoxPreview::_redraw() {
  63. if (stylebox.is_valid()) {
  64. float grid_button_width = get_editor_theme_icon(SNAME("StyleBoxGrid"))->get_size().x;
  65. Rect2 preview_rect = get_rect();
  66. preview_rect = preview_rect.grow(-grid_button_width);
  67. // Re-adjust preview panel to fit all drawn content.
  68. Rect2 drawing_rect = stylebox->get_draw_rect(preview_rect);
  69. preview_rect.size -= drawing_rect.size - preview_rect.size;
  70. preview_rect.position -= drawing_rect.position - preview_rect.position;
  71. draw_style_box(stylebox, preview_rect);
  72. Ref<StyleBoxTexture> sbt = stylebox;
  73. // Draw the "grid". Use white lines, as well as subtle black lines to ensure contrast.
  74. if (sbt.is_valid() && grid_preview->is_pressed()) {
  75. const Color dark_color = Color(0, 0, 0, 0.4);
  76. const Color bright_color = Color(1, 1, 1, 0.8);
  77. int x_left = drawing_rect.position.x + sbt->get_margin(SIDE_LEFT);
  78. int x_right = drawing_rect.position.x + drawing_rect.size.width - sbt->get_margin(SIDE_RIGHT);
  79. int y_top = drawing_rect.position.y + sbt->get_margin(SIDE_TOP);
  80. int y_bottom = drawing_rect.position.y + drawing_rect.size.height - sbt->get_margin(SIDE_BOTTOM);
  81. draw_line(Point2(x_left + 2, 0), Point2(x_left + 2, get_size().height), dark_color);
  82. draw_line(Point2(x_right + 1, 0), Point2(x_right + 1, get_size().height), dark_color);
  83. draw_line(Point2(0, y_top + 2), Point2(get_size().width, y_top + 2), dark_color);
  84. draw_line(Point2(0, y_bottom + 1), Point2(get_size().width, y_bottom + 1), dark_color);
  85. draw_line(Point2(x_left + 1, 0), Point2(x_left + 1, get_size().height), bright_color);
  86. draw_line(Point2(x_right, 0), Point2(x_right, get_size().height), bright_color);
  87. draw_line(Point2(0, y_top + 1), Point2(get_size().width, y_top + 1), bright_color);
  88. draw_line(Point2(0, y_bottom), Point2(get_size().width, y_bottom), bright_color);
  89. }
  90. }
  91. }
  92. StyleBoxPreview::StyleBoxPreview() {
  93. set_clip_contents(true);
  94. set_custom_minimum_size(Size2(0, 150) * EDSCALE);
  95. set_stretch_mode(TextureRect::STRETCH_TILE);
  96. set_texture_repeat(CanvasItem::TEXTURE_REPEAT_ENABLED);
  97. set_anchors_and_offsets_preset(PRESET_FULL_RECT);
  98. grid_preview = memnew(Button);
  99. // This theme variation works better than the normal theme because there's no focus highlight.
  100. grid_preview->set_theme_type_variation("PreviewLightButton");
  101. grid_preview->set_toggle_mode(true);
  102. grid_preview->connect(SceneStringName(toggled), callable_mp(this, &StyleBoxPreview::_grid_preview_toggled));
  103. grid_preview->set_pressed(grid_preview_enabled);
  104. add_child(grid_preview);
  105. }
  106. bool EditorInspectorPluginStyleBox::can_handle(Object *p_object) {
  107. return Object::cast_to<StyleBox>(p_object) != nullptr;
  108. }
  109. void EditorInspectorPluginStyleBox::parse_begin(Object *p_object) {
  110. Ref<StyleBox> sb = Ref<StyleBox>(Object::cast_to<StyleBox>(p_object));
  111. StyleBoxPreview *preview = memnew(StyleBoxPreview);
  112. preview->edit(sb);
  113. add_custom_control(preview);
  114. }
  115. StyleBoxEditorPlugin::StyleBoxEditorPlugin() {
  116. Ref<EditorInspectorPluginStyleBox> inspector_plugin;
  117. inspector_plugin.instantiate();
  118. add_inspector_plugin(inspector_plugin);
  119. }