Finalize capture taking

This commit is contained in:
Michal Pikulski
2025-11-26 10:58:34 +01:00
parent d4b87c5139
commit 010ebea7f3
9 changed files with 478 additions and 55 deletions

View File

@@ -1,10 +1,10 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Core;
using UnityEngine;
using SDev;
using ScreenUtils = Utils.ScreenSpaceUtility;
namespace Minigames.StatueDressup.Utils
{
@@ -15,15 +15,15 @@ namespace Minigames.StatueDressup.Utils
/// </summary>
public static class StatuePhotoManager
{
private const string PHOTO_FOLDER = "StatuePhotos";
private const string PHOTO_PREFIX = "MrCementStatue_";
private const string METADATA_KEY_PREFIX = "StatuePhoto_Meta_";
private const string PHOTO_INDEX_KEY = "StatuePhoto_Index";
private const string PhotoFolder = "StatuePhotos";
private const string PhotoPrefix = "MrCementStatue_";
private const string MetadataKeyPrefix = "StatuePhoto_Meta_";
private const string PhotoIndexKey = "StatuePhoto_Index";
/// <summary>
/// Photo metadata stored in PlayerPrefs
/// </summary>
[System.Serializable]
[Serializable]
public class PhotoMetadata
{
public string photoId;
@@ -40,7 +40,12 @@ namespace Minigames.StatueDressup.Utils
/// <param name="captureArea">RectTransform defining the capture region</param>
/// <param name="onComplete">Callback with captured Texture2D</param>
/// <param name="mainCamera">Camera used for coordinate conversion (null = Camera.main)</param>
public static void CaptureAreaPhoto(RectTransform captureArea, Action<Texture2D> onComplete, Camera mainCamera = null)
/// <param name="clampToScreenBounds">If true, clamps capture area to visible screen bounds</param>
public static void CaptureAreaPhoto(
RectTransform captureArea,
Action<Texture2D> onComplete,
Camera mainCamera = null,
bool clampToScreenBounds = true)
{
if (captureArea == null)
{
@@ -51,8 +56,14 @@ namespace Minigames.StatueDressup.Utils
if (mainCamera == null) mainCamera = Camera.main;
// Get screen rect from RectTransform
Rect screenRect = GetScreenRectFromRectTransform(captureArea, mainCamera);
// Use ScreenSpaceUtility to convert RectTransform to screen rect
// returnCenterPosition = true because ScreenshotHelper expects center position
Rect screenRect = ScreenUtils.RectTransformToScreenRect(
captureArea,
mainCamera,
clampToScreenBounds,
returnCenterPosition: true
);
Logging.Debug($"[StatuePhotoManager] Capturing area: pos={screenRect.position}, size={screenRect.size}");
@@ -60,7 +71,7 @@ namespace Minigames.StatueDressup.Utils
ScreenshotHelper.Instance.Capture(
screenRect.position,
screenRect.size,
(Texture2D texture) =>
(texture) =>
{
if (texture != null)
{
@@ -75,24 +86,7 @@ namespace Minigames.StatueDressup.Utils
}
);
}
/// <summary>
/// Convert RectTransform world corners to screen space rect
/// </summary>
private static Rect GetScreenRectFromRectTransform(RectTransform rectTransform, Camera camera)
{
Vector3[] corners = new Vector3[4];
rectTransform.GetWorldCorners(corners);
Vector2 min = RectTransformUtility.WorldToScreenPoint(camera, corners[0]);
Vector2 max = RectTransformUtility.WorldToScreenPoint(camera, corners[2]);
// Ensure positive dimensions
float width = Mathf.Abs(max.x - min.x);
float height = Mathf.Abs(max.y - min.y);
return new Rect(min.x, min.y, width, height);
}
#endregion
@@ -115,13 +109,13 @@ namespace Minigames.StatueDressup.Utils
try
{
// Generate unique photo ID
string photoId = $"{PHOTO_PREFIX}{DateTime.Now.Ticks}";
string photoId = $"{PhotoPrefix}{DateTime.Now.Ticks}";
// Save texture using FileSaveUtil
string savedPath = FileSaveUtil.Instance.SaveTextureAsPNG(
photo,
FileSaveUtil.AppPath.PersistentDataPath,
PHOTO_FOLDER,
PhotoFolder,
photoId
);
@@ -232,7 +226,7 @@ namespace Minigames.StatueDressup.Utils
/// </summary>
public static List<string> GetAllPhotoIds()
{
string indexJson = PlayerPrefs.GetString(PHOTO_INDEX_KEY, "[]");
string indexJson = PlayerPrefs.GetString(PhotoIndexKey, "[]");
List<string> photoIds = JsonUtility.FromJson<PhotoIdList>(WrapJsonArray(indexJson))?.ids ?? new List<string>();
// Sort by timestamp descending (newest first)
@@ -328,7 +322,7 @@ namespace Minigames.StatueDressup.Utils
public static string GetPhotoDirectory()
{
return Path.Combine(Application.persistentDataPath, PHOTO_FOLDER);
return Path.Combine(Application.persistentDataPath, PhotoFolder);
}
private static string GetPhotoFilePath(string photoId)
@@ -339,19 +333,19 @@ namespace Minigames.StatueDressup.Utils
private static void SaveMetadata(PhotoMetadata metadata)
{
string json = JsonUtility.ToJson(metadata);
PlayerPrefs.SetString(METADATA_KEY_PREFIX + metadata.photoId, json);
PlayerPrefs.SetString(MetadataKeyPrefix + metadata.photoId, json);
PlayerPrefs.Save();
}
private static PhotoMetadata LoadMetadata(string photoId)
{
string json = PlayerPrefs.GetString(METADATA_KEY_PREFIX + photoId, null);
string json = PlayerPrefs.GetString(MetadataKeyPrefix + photoId, null);
return string.IsNullOrEmpty(json) ? null : JsonUtility.FromJson<PhotoMetadata>(json);
}
private static void DeleteMetadata(string photoId)
{
PlayerPrefs.DeleteKey(METADATA_KEY_PREFIX + photoId);
PlayerPrefs.DeleteKey(MetadataKeyPrefix + photoId);
PlayerPrefs.Save();
}
@@ -377,7 +371,7 @@ namespace Minigames.StatueDressup.Utils
private static void SavePhotoIndex(List<string> photoIds)
{
string json = JsonUtility.ToJson(new PhotoIdList { ids = photoIds });
PlayerPrefs.SetString(PHOTO_INDEX_KEY, json);
PlayerPrefs.SetString(PhotoIndexKey, json);
PlayerPrefs.Save();
}
@@ -387,7 +381,7 @@ namespace Minigames.StatueDressup.Utils
return json;
}
[System.Serializable]
[Serializable]
private class PhotoIdList
{
public List<string> ids = new List<string>();