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