style_box_editor_plugin.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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) 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 "style_box_editor_plugin.h"
  31. #include "editor/editor_scale.h"
  32. bool StyleBoxPreview::grid_preview_enabled = true;
  33. void StyleBoxPreview::_grid_preview_toggled(bool p_active) {
  34. grid_preview_enabled = p_active;
  35. preview->queue_redraw();
  36. }
  37. bool EditorInspectorPluginStyleBox::can_handle(Object *p_object) {
  38. return Object::cast_to<StyleBox>(p_object) != nullptr;
  39. }
  40. void EditorInspectorPluginStyleBox::parse_begin(Object *p_object) {
  41. Ref<StyleBox> sb = Ref<StyleBox>(Object::cast_to<StyleBox>(p_object));
  42. StyleBoxPreview *preview = memnew(StyleBoxPreview);
  43. preview->edit(sb);
  44. add_custom_control(preview);
  45. }
  46. void StyleBoxPreview::edit(const Ref<StyleBox> &p_stylebox) {
  47. if (stylebox.is_valid()) {
  48. stylebox->disconnect("changed", callable_mp(this, &StyleBoxPreview::_sb_changed));
  49. }
  50. stylebox = p_stylebox;
  51. if (p_stylebox.is_valid()) {
  52. preview->add_theme_style_override("panel", stylebox);
  53. stylebox->connect("changed", callable_mp(this, &StyleBoxPreview::_sb_changed));
  54. }
  55. Ref<StyleBoxTexture> sbt = p_stylebox;
  56. grid_preview->set_visible(sbt.is_valid());
  57. _sb_changed();
  58. }
  59. void StyleBoxPreview::_sb_changed() {
  60. preview->queue_redraw();
  61. }
  62. void StyleBoxPreview::_notification(int p_what) {
  63. switch (p_what) {
  64. case NOTIFICATION_ENTER_TREE:
  65. case NOTIFICATION_THEME_CHANGED: {
  66. if (!is_inside_tree()) {
  67. // TODO: This is a workaround because `NOTIFICATION_THEME_CHANGED`
  68. // is getting called for some reason when the `TexturePreview` is
  69. // getting destroyed, which causes `get_theme_font()` to return `nullptr`.
  70. // See https://github.com/godotengine/godot/issues/50743.
  71. break;
  72. }
  73. grid_preview->set_normal_texture(get_theme_icon(SNAME("StyleBoxGridInvisible"), SNAME("EditorIcons")));
  74. grid_preview->set_pressed_texture(get_theme_icon(SNAME("StyleBoxGridVisible"), SNAME("EditorIcons")));
  75. grid_preview->set_hover_texture(get_theme_icon(SNAME("StyleBoxGridVisible"), SNAME("EditorIcons")));
  76. checkerboard->set_texture(get_theme_icon(SNAME("Checkerboard"), SNAME("EditorIcons")));
  77. } break;
  78. }
  79. }
  80. void StyleBoxPreview::_redraw() {
  81. if (stylebox.is_valid()) {
  82. Ref<Texture2D> grid_texture_disabled = get_theme_icon(SNAME("StyleBoxGridInvisible"), SNAME("EditorIcons"));
  83. Rect2 preview_rect = preview->get_rect();
  84. preview_rect.position += grid_texture_disabled->get_size();
  85. preview_rect.size -= grid_texture_disabled->get_size() * 2;
  86. // Re-adjust preview panel to fit all drawn content
  87. Rect2 draw_rect = stylebox->get_draw_rect(preview_rect);
  88. preview_rect.size -= draw_rect.size - preview_rect.size;
  89. preview_rect.position -= draw_rect.position - preview_rect.position;
  90. preview->draw_style_box(stylebox, preview_rect);
  91. Ref<StyleBoxTexture> sbt = stylebox;
  92. if (sbt.is_valid() && grid_preview->is_pressed()) {
  93. for (int i = 0; i < 2; i++) {
  94. Color c = i == 1 ? Color(1, 1, 1, 0.8) : Color(0, 0, 0, 0.4);
  95. int x = draw_rect.position.x + sbt->get_margin(SIDE_LEFT) + (1 - i);
  96. preview->draw_line(Point2(x, 0), Point2(x, preview->get_size().height), c);
  97. int x2 = draw_rect.position.x + draw_rect.size.width - sbt->get_margin(SIDE_RIGHT) + (1 - i);
  98. preview->draw_line(Point2(x2, 0), Point2(x2, preview->get_size().height), c);
  99. int y = draw_rect.position.y + sbt->get_margin(SIDE_TOP) + (1 - i);
  100. preview->draw_line(Point2(0, y), Point2(preview->get_size().width, y), c);
  101. int y2 = draw_rect.position.y + draw_rect.size.height - sbt->get_margin(SIDE_BOTTOM) + (1 - i);
  102. preview->draw_line(Point2(0, y2), Point2(preview->get_size().width, y2), c);
  103. }
  104. }
  105. }
  106. }
  107. void StyleBoxPreview::_bind_methods() {
  108. }
  109. StyleBoxPreview::StyleBoxPreview() {
  110. checkerboard = memnew(TextureRect);
  111. checkerboard->set_stretch_mode(TextureRect::STRETCH_TILE);
  112. checkerboard->set_texture_repeat(CanvasItem::TEXTURE_REPEAT_ENABLED);
  113. checkerboard->set_custom_minimum_size(Size2(0.0, 150.0) * EDSCALE);
  114. preview = memnew(Control);
  115. preview->set_clip_contents(true);
  116. preview->connect("draw", callable_mp(this, &StyleBoxPreview::_redraw));
  117. checkerboard->add_child(preview);
  118. preview->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
  119. add_margin_child(TTR("Preview:"), checkerboard);
  120. grid_preview = memnew(TextureButton);
  121. preview->add_child(grid_preview);
  122. grid_preview->set_toggle_mode(true);
  123. grid_preview->connect("toggled", callable_mp(this, &StyleBoxPreview::_grid_preview_toggled));
  124. grid_preview->set_pressed(grid_preview_enabled);
  125. }
  126. StyleBoxEditorPlugin::StyleBoxEditorPlugin() {
  127. Ref<EditorInspectorPluginStyleBox> inspector_plugin;
  128. inspector_plugin.instantiate();
  129. add_inspector_plugin(inspector_plugin);
  130. }