Repository: godotengine/godot
File: editor/scene/3d/camera_3d_editor_plugin.cpp (C++)

Code before:
	if (sub_viewport != nullptr) {
		const Size2i camera_size = Node3DEditor::get_camera_viewport_size(camera);
		centering_container->set_ratio(camera_size.aspect());
	}
}

void Camera3DPreview::_camera_exiting() {
	if (sub_viewport != nullptr) {
		const Size2i camera_size = Node3DEditor::get_camera_viewport_size(camera);
		centering_container->set_ratio(camera_size.aspect());
	}
}

void Camera3DPreview::_toggle_folding(bool p_folded) {
	camera_preview_folded = p_folded;
}

Camera3DPreview::Camera3DPreview(Camera3D *p_camera) {
	camera = p_camera;
	camera->connect(SceneStringName(tree_exiting), callable_mp(this, &Camera3DPreview::_camera_exiting));

	FoldableContainer *folder = memnew(FoldableContainer);
	folder->set_title(TTRC("Camera Preview"));
	folder->set_title_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS);
	folder->set_folded(camera_preview_folded);
	folder->connect(SNAME("folding_changed"), callable_mp(this, &Camera3DPreview::_toggle_folding));
	add_child(folder);

	centering_container = memnew(AspectRatioContainer);
	centering_container->set_custom_minimum_size(Size2(0.0, 256.0) * EDSCALE);
	folder->add_child(centering_container);

	SubViewportContainer *sub_viewport_container = memnew(SubViewportContainer);
	sub_viewport_container->set_stretch(true);
	sub_viewport_container->set_texture_filter(TEXTURE_FILTER_NEAREST_WITH_MIPMAPS);
	centering_container->add_child(sub_viewport_container);

	sub_viewport = memnew(SubViewport);
	sub_viewport_container->add_child(sub_viewport);

	RenderingServer::get_singleton()->viewport_attach_camera(sub_viewport->get_viewport_rid(), camera->get_camera());

	ProjectSettings::get_singleton()->connect(SNAME("settings_changed"), callable_mp(this, &Camera3DPreview::_update_sub_viewport_size));
	_update_sub_viewport_size();
}

bool EditorInspectorPluginCamera3DPreview::can_handle(Object *p_object) {
	return Object::cast_to<Camera3D>(p_object) != nullptr;
}

void EditorInspectorPluginCamera3DPreview::parse_begin(Object *p_object) {

Review comment (performance):
Unless this is created repeatedly using `SNAME` here is unnecessary, it increases binary size and should only be used in hot paths generally

Code after:

void Camera3DPreview::_toggle_folding(bool p_folded) {
	camera_preview_folded = p_folded;
}

Camera3DPreview::Camera3DPreview(Camera3D *p_camera) {
	camera = p_camera;

	FoldableContainer *folder = memnew(FoldableContainer);
	folder->set_title(TTRC("Camera Preview"));
	folder->set_title_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS);
	folder->set_folded(camera_preview_folded);
	folder->connect("folding_changed", callable_mp(this, &Camera3DPreview::_toggle_folding));
	add_child(folder);

	centering_container = memnew(AspectRatioContainer);
	centering_container->set_custom_minimum_size(Size2(0.0, 256.0) * EDSCALE);
	folder->add_child(centering_container);

	SubViewportContainer *sub_viewport_container = memnew(SubViewportContainer);
	sub_viewport_container->set_stretch(true);
	sub_viewport_container->set_texture_filter(TEXTURE_FILTER_NEAREST_WITH_MIPMAPS);
	centering_container->add_child(sub_viewport_container);

	sub_viewport = memnew(SubViewport);
	sub_viewport_container->add_child(sub_viewport);

	RenderingServer::get_singleton()->viewport_attach_camera(sub_viewport->get_viewport_rid(), camera->get_camera());

	ProjectSettings::get_singleton()->connect("settings_changed", callable_mp(this, &Camera3DPreview::_project_settings_changed));
	_project_settings_changed();
}

void Camera3DPreview::_project_settings_changed() {
	_update_sub_viewport_size();

	const bool hdr_requested = GLOBAL_GET("display/window/hdr/request_hdr_output");
	const bool use_hdr_2d = GLOBAL_GET("rendering/viewport/hdr_2d");
	sub_viewport->set_use_hdr_2d(use_hdr_2d || hdr_requested);
}

bool EditorInspectorPluginCamera3DPreview::can_handle(Object *p_object) {
	return Object::cast_to<Camera3D>(p_object) != nullptr;
}

void EditorInspectorPluginCamera3DPreview::parse_begin(Object *p_object) {
	Camera3D *camera = Object::cast_to<Camera3D>(p_object);
	Camera3DPreview *preview = memnew(Camera3DPreview(camera));
	add_custom_control(preview);
}