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

Code before:
			selected_camera->set_limit(SIDE_RIGHT, GLOBAL_GET("display/window/size/viewport_width"));
			selected_camera->set_limit(SIDE_BOTTOM, GLOBAL_GET("display/window/size/viewport_height"));
			break;
		}
		default:
			break;
	}
}

void Camera2DEditor::_notification(int p_what) {
	switch (p_what) {
		case NOTIFICATION_THEME_CHANGED: {
			options->set_button_icon(get_editor_theme_icon(SNAME("Camera2D")));
			options->get_popup()->set_item_icon(MENU_SNAP_LIMITS_TO_VIEWPORT, get_editor_theme_icon(SNAME("Camera2D")));
		} break;
	}
}

Camera2DEditor::Camera2DEditor() {
	options = memnew(MenuButton);

	CanvasItemEditor::get_singleton()->add_control_to_menu_panel(options);

	options->set_text(TTR("Camera2D"));

	options->get_popup()->add_item(TTR("Snap the Limits to the Viewport"), MENU_SNAP_LIMITS_TO_VIEWPORT);
	options->set_switch_on_hover(true);

	options->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &Camera2DEditor::_menu_option));
}

void Camera2DEditorPlugin::_update_approach_text_visibility() {
	if (camera_2d_editor->selected_camera == nullptr) {
		print_line("Selected camera is null");
		return;
	}
	approach_to_move_rect->set_visible(camera_2d_editor->selected_camera->is_limit_enabled());
}

void Camera2DEditorPlugin::edit(Object *p_object) {
	Camera2D *cam = Object::cast_to<Camera2D>(p_object);
	if (cam != nullptr) {
		Callable update_text = callable_mp(this, &Camera2DEditorPlugin::_update_approach_text_visibility);
		StringName update_signal = SNAME("_camera_limit_enabled_updated");

		if (cam->is_connected(update_signal, update_text)) {
			cam->disconnect(update_signal, update_text);
		}

		camera_2d_editor->edit(cam);
		_update_approach_text_visibility();

Review comment (suggestion):
```suggestion
	options->get_popup()->add_item(TTRC("Snap the Limits to the Viewport"), MENU_SNAP_LIMITS_TO_VIEWPORT);
```

Code after:
			ur->add_do_method(this, "_snap_limits_to_viewport");
			ur->add_do_reference(selected_camera);
			ur->add_undo_method(this, "_undo_snap_limits_to_viewport", prev_rect);
			ur->commit_action();
		} break;
	}
}

void Camera2DEditor::_snap_limits_to_viewport() {
	selected_camera->set_limit(SIDE_LEFT, 0);
	selected_camera->set_limit(SIDE_TOP, 0);
	selected_camera->set_limit(SIDE_RIGHT, GLOBAL_GET("display/window/size/viewport_width"));
	selected_camera->set_limit(SIDE_BOTTOM, GLOBAL_GET("display/window/size/viewport_height"));
}

void Camera2DEditor::_undo_snap_limits_to_viewport(const Rect2 &p_prev_rect) {
	Point2 end = p_prev_rect.get_end();
	selected_camera->set_limit(SIDE_LEFT, p_prev_rect.position.x);
	selected_camera->set_limit(SIDE_TOP, p_prev_rect.position.y);
	selected_camera->set_limit(SIDE_RIGHT, end.x);
	selected_camera->set_limit(SIDE_BOTTOM, end.y);
}

void Camera2DEditor::_notification(int p_what) {
	switch (p_what) {
		case NOTIFICATION_THEME_CHANGED: {
			options->set_button_icon(get_editor_theme_icon(SNAME("Camera2D")));
		} break;
	}
}

void Camera2DEditor::_bind_methods() {
	ClassDB::bind_method(D_METHOD("_snap_limits_to_viewport"), &Camera2DEditor::_snap_limits_to_viewport);
	ClassDB::bind_method(D_METHOD("_undo_snap_limits_to_viewport", "prev_rect"), &Camera2DEditor::_undo_snap_limits_to_viewport);
}

Camera2DEditor::Camera2DEditor() {
	options = memnew(MenuButton);

	CanvasItemEditor::get_singleton()->add_control_to_menu_panel(options);

	options->set_text(TTRC("Camera2D"));

	options->get_popup()->add_item(TTRC("Snap the Limits to the Viewport"), MENU_SNAP_LIMITS_TO_VIEWPORT);
	options->set_switch_on_hover(true);

	options->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &Camera2DEditor::_menu_option));

#ifdef TOOLS_ENABLED
	add_user_signal(MethodInfo("_editor_theme_changed"));
#endif