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

Code before:
	}
	selected_camera = p_camera;
}

void Camera2DEditor::_menu_option(int p_option) {
	switch (p_option) {
		case MENU_SNAP_LIMITS_TO_VIEWPORT: {
			if (selected_camera == nullptr) {
				break;
			}
			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"));
			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());
}


Review comment (suggestion):
Using two same icons is rather meh.
![image](https://github.com/user-attachments/assets/ad0f2f99-780b-4391-8a1d-5ec311c08c38)
Either remove the menu icon or pick a different one. Although these menus usually don't have icons, e.g.
![image](https://github.com/user-attachments/assets/bf88d182-d369-47a6-98d3-93c7b5ccbba5)

Code after:
	if (p_camera == selected_camera) {
		return;
	}
	selected_camera = p_camera;
}

void Camera2DEditor::_menu_option(int p_option) {
	switch (p_option) {
		case MENU_SNAP_LIMITS_TO_VIEWPORT: {
			EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
			Rect2 prev_rect = selected_camera->get_limit_rect();
			ur->create_action(TTR("Snap the Limits to the Viewport"), UndoRedo::MERGE_DISABLE, selected_camera);
			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);