margin_container_editor_plugin.cpp 4.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**************************************************************************/
  2. /* margin_container_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 "margin_container_editor_plugin.h"
  31. #include "editor/scene/canvas_item_editor_plugin.h"
  32. #include "editor/themes/editor_scale.h"
  33. void MarginContainerEditorPlugin::edit(Object *p_object) {
  34. if (margin_container) {
  35. margin_container->disconnect(SNAME("draw"), callable_mp(CanvasItemEditor::get_singleton(), &CanvasItemEditor::update_viewport));
  36. }
  37. margin_container = Object::cast_to<MarginContainer>(p_object);
  38. if (margin_container) {
  39. margin_container->connect(SNAME("draw"), callable_mp(CanvasItemEditor::get_singleton(), &CanvasItemEditor::update_viewport));
  40. }
  41. CanvasItemEditor::get_singleton()->update_viewport();
  42. }
  43. bool MarginContainerEditorPlugin::handles(Object *p_object) const {
  44. return Object::cast_to<MarginContainer>(p_object) != nullptr;
  45. }
  46. void MarginContainerEditorPlugin::forward_canvas_draw_over_viewport(Control *p_viewport_control) {
  47. if (!margin_container || !margin_container->is_visible_in_tree()) {
  48. return;
  49. }
  50. Transform2D xform = CanvasItemEditor::get_singleton()->get_canvas_transform() * margin_container->get_screen_transform();
  51. // NOTE: This color is copied from Camera2DEditor::forward_canvas_draw_over_viewport.
  52. // We may want to unify them somehow in the future.
  53. Color border_color = Color(1, 1, 0.25, 0.63);
  54. int border_width = Math::round(1 * EDSCALE);
  55. Rect2 rect = margin_container->_edit_get_rect();
  56. int margin_left = margin_container->get_margin_size(SIDE_LEFT);
  57. int margin_top = margin_container->get_margin_size(SIDE_TOP);
  58. int margin_right = margin_container->get_margin_size(SIDE_RIGHT);
  59. int margin_bottom = margin_container->get_margin_size(SIDE_BOTTOM);
  60. Vector2 p1, p2;
  61. // Calculate left margin line.
  62. p1 = xform.xform(rect.position + Vector2(margin_left, margin_top));
  63. p2 = xform.xform(rect.position + Vector2(margin_left, rect.size.y - margin_bottom));
  64. p_viewport_control->draw_line(p1, p2, border_color, border_width);
  65. // Calculate top margin line.
  66. p1 = xform.xform(rect.position + Vector2(margin_left, margin_top));
  67. p2 = xform.xform(rect.position + Vector2(rect.size.x - margin_right, margin_top));
  68. p_viewport_control->draw_line(p1, p2, border_color, border_width);
  69. // Calculate right margin line.
  70. p1 = xform.xform(rect.position + Vector2(rect.size.x - margin_right, margin_top));
  71. p2 = xform.xform(rect.position + Vector2(rect.size.x - margin_right, rect.size.y - margin_bottom));
  72. p_viewport_control->draw_line(p1, p2, border_color, border_width);
  73. // Calculate bottom margin line.
  74. p1 = xform.xform(rect.position + Vector2(margin_left, rect.size.y - margin_bottom));
  75. p2 = xform.xform(rect.position + Vector2(rect.size.x - margin_right, rect.size.y - margin_bottom));
  76. p_viewport_control->draw_line(p1, p2, border_color, border_width);
  77. }