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

Code before:
/* the following conditions:                                              */
/*                                                                        */
/* The above copyright notice and this permission notice shall be         */
/* included in all copies or substantial portions of the Software.        */
/*                                                                        */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,        */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF     */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* 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");

Review comment (bug):
```suggestion
	return Object::cast_to<MarginContainer>(p_object) != nullptr;
```

Code after:
/* the following conditions:                                              */
/*                                                                        */
/* The above copyright notice and this permission notice shall be         */
/* included in all copies or substantial portions of the Software.        */
/*                                                                        */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,        */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF     */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* 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();