Repository: godotengine/godot
File: editor/plugins/abstract_polygon_2d_editor.cpp (C++)

Code before:
	button_delete->set_disabled(p_disable);

	if (p_disable) {
		button_create->set_tooltip_text(p_reason);
		button_edit->set_tooltip_text(p_reason);
		button_delete->set_tooltip_text(p_reason);
	} else {
		button_create->set_tooltip_text(TTR("Create points."));
		button_edit->set_tooltip_text(TTR("Edit points.\nLMB: Move Point\nRMB: Erase Point"));
		button_delete->set_tooltip_text(TTR("Erase points."));
	}
}

bool AbstractPolygon2DEditor::forward_gui_input(const Ref<InputEvent> &p_event) {
	if (!_get_node() || !_polygon_editing_enabled) {
		return false;
	}

	if (!_get_node()->is_visible_in_tree()) {
		return false;
	}

	EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
	Ref<InputEventMouseButton> mb = p_event;

	if (!_has_resource()) {
		if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && mb->is_pressed()) {
			create_resource->set_text(String("No polygon resource on this node.\nCreate and assign one?"));
			create_resource->popup_centered();
		}
		return (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT);
	}

	CanvasItemEditor::Tool tool = CanvasItemEditor::get_singleton()->get_current_tool();
	if (tool != CanvasItemEditor::TOOL_SELECT) {
		return false;
	}

	if (mb.is_valid()) {
		Transform2D xform = canvas_item_editor->get_canvas_transform() * _get_node()->get_screen_transform();

		Vector2 gpoint = mb->get_position();
		Vector2 cpoint = canvas_item_editor->snap_point(canvas_item_editor->get_canvas_transform().affine_inverse().xform(gpoint));
		cpoint = _get_node()->get_screen_transform().affine_inverse().xform(cpoint);

		if (mode == MODE_EDIT || (_is_line() && mode == MODE_CREATE)) {
			if (mb->get_button_index() == MouseButton::LEFT) {
				if (mb->is_pressed()) {
					if (mb->is_meta_pressed() || mb->is_ctrl_pressed() || mb->is_shift_pressed() || mb->is_alt_pressed()) {
						return false;
					}

Review comment (bug):
Given how often it's used, it's looking like a candidate for a `Viewport` helper method.

Are we sure the Viewport's parent will always be a Control though, and that `Object::cast_to<Control>(vp->get_parent())->is_visible_in_tree())` won't crash?

Code after:
	button_delete->set_disabled(p_disable);

	if (p_disable) {
		button_create->set_tooltip_text(p_reason);
		button_edit->set_tooltip_text(p_reason);
		button_delete->set_tooltip_text(p_reason);
	} else {
		button_create->set_tooltip_text(TTR("Create points."));
		button_edit->set_tooltip_text(TTR("Edit points.\nLMB: Move Point\nRMB: Erase Point"));
		button_delete->set_tooltip_text(TTR("Erase points."));
	}
}

bool AbstractPolygon2DEditor::forward_gui_input(const Ref<InputEvent> &p_event) {
	if (!_get_node() || !_polygon_editing_enabled) {
		return false;
	}

	if (!_get_node()->is_visible_in_tree()) {
		return false;
	}

	Viewport *vp = _get_node()->get_viewport();
	if (vp && !vp->is_visible_subviewport()) {
		return false;
	}

	EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
	Ref<InputEventMouseButton> mb = p_event;

	if (!_has_resource()) {
		if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && mb->is_pressed()) {
			create_resource->set_text(String("No polygon resource on this node.\nCreate and assign one?"));
			create_resource->popup_centered();
		}
		return (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT);
	}

	CanvasItemEditor::Tool tool = CanvasItemEditor::get_singleton()->get_current_tool();
	if (tool != CanvasItemEditor::TOOL_SELECT) {
		return false;
	}

	if (mb.is_valid()) {
		Transform2D xform = canvas_item_editor->get_canvas_transform() * _get_node()->get_screen_transform();

		Vector2 gpoint = mb->get_position();
		Vector2 cpoint = canvas_item_editor->snap_point(canvas_item_editor->get_canvas_transform().affine_inverse().xform(gpoint));
		cpoint = _get_node()->get_screen_transform().affine_inverse().xform(cpoint);

		if (mode == MODE_EDIT || (_is_line() && mode == MODE_CREATE)) {