Repository: godotengine/godot
File: editor/scene/gui/margin_container_editor_plugin.cpp (C++)

Code before:
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY   */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,   */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE      */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                 */
/**************************************************************************/

#include "margin_container_editor_plugin.h"

#include "editor/scene/canvas_item_editor_plugin.h"
#include "editor/themes/editor_scale.h"

void MarginContainerEditorPlugin::edit(Object *p_object) {
	margin_container = Object::cast_to<MarginContainer>(p_object);
	CanvasItemEditor::get_singleton()->update_viewport();
}

bool MarginContainerEditorPlugin::handles(Object *p_object) const {
	return p_object->is_class("MarginContainer");
}

void MarginContainerEditorPlugin::forward_canvas_draw_over_viewport(Control *p_viewport_control) {
	if (margin_container == nullptr) {
		return;
	}

	bool item_locked = margin_container->has_meta("_edit_lock_");

	Transform2D xform = CanvasItemEditor::get_singleton()->get_canvas_transform() * margin_container->get_screen_transform();

	// TODO: These colors are copied from CanvasItemEditor::_draw_selection.
	// We may want to unify them somehow.
	Color border_color = Color(1, 0.6, 0.4, 0.5);

	if (item_locked) {
		border_color = Color(0.7, 0.7, 0.7, 0.5);
	}

	int border_width = Math::round(1 * EDSCALE);

	Rect2 rect = margin_container->_edit_get_rect();

	int margin_left = margin_container->get_theme_constant("margin_left");
	int margin_top = margin_container->get_theme_constant("margin_top");
	int margin_right = margin_container->get_theme_constant("margin_right");
	int margin_bottom = margin_container->get_theme_constant("margin_bottom");

	Vector2 p1, p2;

	// Calculate left margin line.
	p1 = xform.xform(rect.position + Vector2(margin_left, margin_top));
	p2 = xform.xform(rect.position + Vector2(margin_left, rect.size.y - margin_bottom));

Review comment (suggestion):
I wonder if it's worth it to mimic 2D editor colors that much. Maybe you could use a different color altogether, like the one used by Camera2D limits. Then you could ignore locking etc.

Although that might cause margins to appear above the box selection, so not sure 🤔

Code after:
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY   */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,   */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE      */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                 */
/**************************************************************************/

#include "margin_container_editor_plugin.h"

#include "editor/scene/canvas_item_editor_plugin.h"
#include "editor/themes/editor_scale.h"

void MarginContainerEditorPlugin::edit(Object *p_object) {
	if (margin_container) {
		margin_container->disconnect(SNAME("draw"), callable_mp(CanvasItemEditor::get_singleton(), &CanvasItemEditor::update_viewport));
	}

	margin_container = Object::cast_to<MarginContainer>(p_object);

	if (margin_container) {
		margin_container->connect(SNAME("draw"), callable_mp(CanvasItemEditor::get_singleton(), &CanvasItemEditor::update_viewport));
	}
	CanvasItemEditor::get_singleton()->update_viewport();
}

bool MarginContainerEditorPlugin::handles(Object *p_object) const {
	return Object::cast_to<MarginContainer>(p_object) != nullptr;
}

void MarginContainerEditorPlugin::forward_canvas_draw_over_viewport(Control *p_viewport_control) {
	if (!margin_container) {
		return;
	}

	Transform2D xform = CanvasItemEditor::get_singleton()->get_canvas_transform() * margin_container->get_screen_transform();

	// NOTE: This color is copied from Camera2DEditor::forward_canvas_draw_over_viewport.
	// We may want to unify them somehow in the future.
	Color border_color = Color(1, 1, 0.25, 0.63);

	int border_width = Math::round(1 * EDSCALE);

	Rect2 rect = margin_container->_edit_get_rect();

	int margin_left = margin_container->get_margin_size(SIDE_LEFT);
	int margin_top = margin_container->get_margin_size(SIDE_TOP);
	int margin_right = margin_container->get_margin_size(SIDE_RIGHT);
	int margin_bottom = margin_container->get_margin_size(SIDE_BOTTOM);

	Vector2 p1, p2;

	// Calculate left margin line.