stash work
This commit is contained in:
committed by
Michal Pikulski
parent
8d410b42d3
commit
5bab6d9596
@@ -2,9 +2,9 @@
|
||||
using System.Collections.Generic;
|
||||
using Core;
|
||||
using Core.Lifecycle;
|
||||
using Minigames.StatueDressup.Utils;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using Utils;
|
||||
|
||||
namespace Minigames.StatueDressup.Controllers
|
||||
{
|
||||
@@ -18,49 +18,50 @@ namespace Minigames.StatueDressup.Controllers
|
||||
[Header("Gallery UI")]
|
||||
[SerializeField] private Transform gridContainer;
|
||||
[SerializeField] private PhotoGridItem gridItemPrefab;
|
||||
[SerializeField] private ScrollRect scrollRect;
|
||||
|
||||
[Header("Enlarged View")]
|
||||
[SerializeField] private GameObject enlargedViewPanel;
|
||||
[SerializeField] private Image enlargedPhotoImage;
|
||||
[SerializeField] private Button closeEnlargedButton;
|
||||
[SerializeField] private Button deletePhotoButton;
|
||||
[SerializeField] private Text photoInfoText;
|
||||
[SerializeField] private Transform enlargedContainer; // Container for enlarged preview (top layer)
|
||||
[SerializeField] private GameObject backdrop; // Dark backdrop for enlarged view
|
||||
[SerializeField] private GameObject enlargedPreviewPrefab; // Prefab for enlarged preview (same as grid item)
|
||||
|
||||
[Header("Pagination")]
|
||||
[SerializeField] private Button loadMoreButton;
|
||||
[SerializeField] private Text statusText;
|
||||
[SerializeField] private Button previousPageButton;
|
||||
[SerializeField] private Button nextPageButton;
|
||||
[SerializeField] private Text pageStatusText;
|
||||
|
||||
[Header("Settings")]
|
||||
[SerializeField] private int itemsPerPage = 20;
|
||||
[SerializeField] private int thumbnailSize = 256;
|
||||
[SerializeField] private int maxCachedThumbnails = 50; // Keep recent thumbnails in memory
|
||||
|
||||
private int _currentPage = 0;
|
||||
private AppleHills.Core.Settings.IStatueDressupSettings _settings;
|
||||
private int _currentPage;
|
||||
private List<string> _allPhotoIds = new List<string>();
|
||||
private Dictionary<string, PhotoGridItem> _activeGridItems = new Dictionary<string, PhotoGridItem>();
|
||||
private Dictionary<string, Texture2D> _thumbnailCache = new Dictionary<string, Texture2D>();
|
||||
private Dictionary<string, Texture2D> _fullPhotoCache = new Dictionary<string, Texture2D>(); // Cache full photos for enlargement
|
||||
private Queue<string> _thumbnailCacheOrder = new Queue<string>();
|
||||
private string _currentEnlargedPhotoId = null;
|
||||
private Texture2D _currentEnlargedTexture = null;
|
||||
private bool _isLoadingPage;
|
||||
private PhotoEnlargeController _enlargeController;
|
||||
|
||||
internal override void OnManagedStart()
|
||||
{
|
||||
base.OnManagedStart();
|
||||
|
||||
// Setup buttons
|
||||
if (closeEnlargedButton != null)
|
||||
closeEnlargedButton.onClick.AddListener(CloseEnlargedView);
|
||||
// Get settings
|
||||
_settings = GameManager.GetSettingsObject<AppleHills.Core.Settings.IStatueDressupSettings>();
|
||||
|
||||
if (deletePhotoButton != null)
|
||||
deletePhotoButton.onClick.AddListener(DeleteCurrentPhoto);
|
||||
// Initialize enlarge controller
|
||||
_enlargeController = new PhotoEnlargeController(backdrop, enlargedContainer, _settings?.GalleryAnimationDuration ?? 0.3f);
|
||||
|
||||
if (loadMoreButton != null)
|
||||
loadMoreButton.onClick.AddListener(LoadNextPage);
|
||||
// Setup page navigation buttons
|
||||
if (previousPageButton != null)
|
||||
previousPageButton.onClick.AddListener(OnPreviousPageClicked);
|
||||
|
||||
// Hide enlarged view initially
|
||||
if (enlargedViewPanel != null)
|
||||
enlargedViewPanel.SetActive(false);
|
||||
if (nextPageButton != null)
|
||||
nextPageButton.onClick.AddListener(OnNextPageClicked);
|
||||
|
||||
// Hide backdrop initially
|
||||
if (backdrop != null)
|
||||
backdrop.SetActive(false);
|
||||
|
||||
// Clear grid initially (in case there are leftover items from scene setup)
|
||||
ClearGrid();
|
||||
|
||||
// Load first page
|
||||
RefreshGallery();
|
||||
@@ -72,35 +73,35 @@ namespace Minigames.StatueDressup.Controllers
|
||||
public void RefreshGallery()
|
||||
{
|
||||
// Clear existing items
|
||||
ClearGallery();
|
||||
ClearGrid();
|
||||
|
||||
// Get all photo IDs
|
||||
_allPhotoIds = StatuePhotoManager.GetAllPhotoIds();
|
||||
_allPhotoIds = PhotoManager.GetAllPhotoIds(CaptureType.StatueMinigame);
|
||||
_currentPage = 0;
|
||||
|
||||
Logging.Debug($"[StatuePhotoGalleryController] Gallery refreshed: {_allPhotoIds.Count} photos");
|
||||
|
||||
// Load first page
|
||||
LoadNextPage();
|
||||
// Display first page
|
||||
DisplayCurrentPage();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Load next page of photos
|
||||
/// Display the current page of photos (clears grid and shows only current page)
|
||||
/// </summary>
|
||||
private void LoadNextPage()
|
||||
private void DisplayCurrentPage()
|
||||
{
|
||||
List<string> pagePhotoIds = StatuePhotoManager.GetPhotoIdsPage(_currentPage, itemsPerPage);
|
||||
if (_isLoadingPage) return;
|
||||
|
||||
if (pagePhotoIds.Count == 0)
|
||||
{
|
||||
if (loadMoreButton != null)
|
||||
loadMoreButton.gameObject.SetActive(false);
|
||||
|
||||
UpdateStatusText($"All photos loaded ({_allPhotoIds.Count} total)");
|
||||
return;
|
||||
}
|
||||
_isLoadingPage = true;
|
||||
|
||||
Logging.Debug($"[StatuePhotoGalleryController] Loading page {_currentPage}: {pagePhotoIds.Count} items");
|
||||
// Clear current grid
|
||||
ClearGrid();
|
||||
|
||||
// Get photos for current page
|
||||
int itemsPerPage = _settings?.GalleryItemsPerPage ?? 20;
|
||||
List<string> pagePhotoIds = PhotoManager.GetPhotoIdsPage(CaptureType.StatueMinigame, _currentPage, itemsPerPage);
|
||||
|
||||
Logging.Debug($"[StatuePhotoGalleryController] Displaying page {_currentPage + 1}: {pagePhotoIds.Count} items");
|
||||
|
||||
// Spawn grid items for this page
|
||||
foreach (string photoId in pagePhotoIds)
|
||||
@@ -108,14 +109,64 @@ namespace Minigames.StatueDressup.Controllers
|
||||
SpawnGridItem(photoId);
|
||||
}
|
||||
|
||||
_currentPage++;
|
||||
// Update button states
|
||||
UpdatePageButtons();
|
||||
|
||||
// Update UI state
|
||||
bool hasMore = _currentPage * itemsPerPage < _allPhotoIds.Count;
|
||||
if (loadMoreButton != null)
|
||||
loadMoreButton.gameObject.SetActive(hasMore);
|
||||
// Update status text
|
||||
int totalPages = Mathf.CeilToInt((float)_allPhotoIds.Count / itemsPerPage);
|
||||
UpdateStatusText($"Page {_currentPage + 1}/{totalPages} ({_allPhotoIds.Count} photos)");
|
||||
|
||||
UpdateStatusText($"Showing {_activeGridItems.Count} of {_allPhotoIds.Count} photos");
|
||||
_isLoadingPage = false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update page navigation button states
|
||||
/// </summary>
|
||||
private void UpdatePageButtons()
|
||||
{
|
||||
int itemsPerPage = _settings?.GalleryItemsPerPage ?? 20;
|
||||
int totalPages = Mathf.CeilToInt((float)_allPhotoIds.Count / itemsPerPage);
|
||||
|
||||
// Enable/disable previous button
|
||||
if (previousPageButton != null)
|
||||
{
|
||||
previousPageButton.interactable = _currentPage > 0;
|
||||
}
|
||||
|
||||
// Enable/disable next button
|
||||
if (nextPageButton != null)
|
||||
{
|
||||
nextPageButton.interactable = _currentPage < totalPages - 1;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Navigate to previous page
|
||||
/// </summary>
|
||||
private void OnPreviousPageClicked()
|
||||
{
|
||||
if (_currentPage > 0)
|
||||
{
|
||||
_currentPage--;
|
||||
DisplayCurrentPage();
|
||||
Logging.Debug($"[StatuePhotoGalleryController] Navigated to previous page: {_currentPage}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Navigate to next page
|
||||
/// </summary>
|
||||
private void OnNextPageClicked()
|
||||
{
|
||||
int itemsPerPage = _settings?.GalleryItemsPerPage ?? 20;
|
||||
int totalPages = Mathf.CeilToInt((float)_allPhotoIds.Count / itemsPerPage);
|
||||
|
||||
if (_currentPage < totalPages - 1)
|
||||
{
|
||||
_currentPage++;
|
||||
DisplayCurrentPage();
|
||||
Logging.Debug($"[StatuePhotoGalleryController] Navigated to next page: {_currentPage}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -154,7 +205,7 @@ namespace Minigames.StatueDressup.Controllers
|
||||
yield return null;
|
||||
|
||||
// Load full photo
|
||||
Texture2D fullPhoto = StatuePhotoManager.LoadPhoto(photoId);
|
||||
Texture2D fullPhoto = PhotoManager.LoadPhoto(CaptureType.StatueMinigame, photoId);
|
||||
|
||||
if (fullPhoto == null)
|
||||
{
|
||||
@@ -163,7 +214,8 @@ namespace Minigames.StatueDressup.Controllers
|
||||
}
|
||||
|
||||
// Create thumbnail
|
||||
Texture2D thumbnail = StatuePhotoManager.CreateThumbnail(fullPhoto, thumbnailSize);
|
||||
int thumbSize = _settings?.GalleryThumbnailSize ?? 256;
|
||||
Texture2D thumbnail = PhotoManager.CreateThumbnail(fullPhoto, thumbSize);
|
||||
|
||||
// Destroy full photo immediately (we only need thumbnail)
|
||||
Destroy(fullPhoto);
|
||||
@@ -188,7 +240,8 @@ namespace Minigames.StatueDressup.Controllers
|
||||
_thumbnailCacheOrder.Enqueue(photoId);
|
||||
|
||||
// Evict oldest if over limit
|
||||
while (_thumbnailCache.Count > maxCachedThumbnails && _thumbnailCacheOrder.Count > 0)
|
||||
int maxCached = _settings?.GalleryMaxCachedThumbnails ?? 50;
|
||||
while (_thumbnailCache.Count > maxCached && _thumbnailCacheOrder.Count > 0)
|
||||
{
|
||||
string oldestId = _thumbnailCacheOrder.Dequeue();
|
||||
|
||||
@@ -202,154 +255,90 @@ namespace Minigames.StatueDressup.Controllers
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Show enlarged view of a photo (called by PhotoGridItem)
|
||||
/// Enlarge a photo (called by PhotoGridItem)
|
||||
/// </summary>
|
||||
public void ShowEnlargedView(string photoId)
|
||||
public void OnGridItemClicked(PhotoGridItem gridItem, string photoId)
|
||||
{
|
||||
if (enlargedViewPanel == null || enlargedPhotoImage == null)
|
||||
if (_enlargeController == null)
|
||||
{
|
||||
Logging.Warning("[StatuePhotoGalleryController] Enlarged view UI not configured");
|
||||
Logging.Error("[StatuePhotoGalleryController] Enlarge controller not initialized");
|
||||
return;
|
||||
}
|
||||
|
||||
Logging.Debug($"[StatuePhotoGalleryController] Showing enlarged view: {photoId}");
|
||||
|
||||
// Clear previous enlarged texture
|
||||
if (_currentEnlargedTexture != null)
|
||||
// If already enlarged, shrink it
|
||||
if (_enlargeController.IsPhotoEnlarged)
|
||||
{
|
||||
Destroy(_currentEnlargedTexture);
|
||||
_currentEnlargedTexture = null;
|
||||
}
|
||||
|
||||
// Load full-size photo
|
||||
_currentEnlargedTexture = StatuePhotoManager.LoadPhoto(photoId);
|
||||
|
||||
if (_currentEnlargedTexture == null)
|
||||
{
|
||||
Logging.Error($"[StatuePhotoGalleryController] Failed to load enlarged photo: {photoId}");
|
||||
_enlargeController.ShrinkPhoto();
|
||||
return;
|
||||
}
|
||||
|
||||
// Create sprite from texture
|
||||
Sprite enlargedSprite = Sprite.Create(
|
||||
_currentEnlargedTexture,
|
||||
new Rect(0, 0, _currentEnlargedTexture.width, _currentEnlargedTexture.height),
|
||||
new Vector2(0.5f, 0.5f)
|
||||
);
|
||||
Logging.Debug($"[StatuePhotoGalleryController] Enlarging photo: {photoId}");
|
||||
|
||||
enlargedPhotoImage.sprite = enlargedSprite;
|
||||
_currentEnlargedPhotoId = photoId;
|
||||
float enlargedScale = _settings?.GalleryEnlargedScale ?? 2.5f;
|
||||
|
||||
// Update photo info
|
||||
UpdatePhotoInfo(photoId);
|
||||
|
||||
// Show panel
|
||||
enlargedViewPanel.SetActive(true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Close enlarged view
|
||||
/// </summary>
|
||||
private void CloseEnlargedView()
|
||||
{
|
||||
if (enlargedViewPanel != null)
|
||||
enlargedViewPanel.SetActive(false);
|
||||
|
||||
// Clean up texture
|
||||
if (_currentEnlargedTexture != null)
|
||||
// Check cache first
|
||||
if (_fullPhotoCache.TryGetValue(photoId, out Texture2D fullPhoto))
|
||||
{
|
||||
Destroy(_currentEnlargedTexture);
|
||||
_currentEnlargedTexture = null;
|
||||
}
|
||||
|
||||
_currentEnlargedPhotoId = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Delete currently viewed photo
|
||||
/// </summary>
|
||||
private void DeleteCurrentPhoto()
|
||||
{
|
||||
if (string.IsNullOrEmpty(_currentEnlargedPhotoId))
|
||||
{
|
||||
Logging.Warning("[StatuePhotoGalleryController] No photo selected for deletion");
|
||||
return;
|
||||
}
|
||||
|
||||
string photoIdToDelete = _currentEnlargedPhotoId;
|
||||
|
||||
// Close enlarged view first
|
||||
CloseEnlargedView();
|
||||
|
||||
// Delete photo
|
||||
bool deleted = StatuePhotoManager.DeletePhoto(photoIdToDelete);
|
||||
|
||||
if (deleted)
|
||||
{
|
||||
// Remove from grid
|
||||
if (_activeGridItems.TryGetValue(photoIdToDelete, out PhotoGridItem gridItem))
|
||||
{
|
||||
Destroy(gridItem.gameObject);
|
||||
_activeGridItems.Remove(photoIdToDelete);
|
||||
}
|
||||
|
||||
// Remove from cache
|
||||
if (_thumbnailCache.TryGetValue(photoIdToDelete, out Texture2D thumbnail))
|
||||
{
|
||||
Destroy(thumbnail);
|
||||
_thumbnailCache.Remove(photoIdToDelete);
|
||||
}
|
||||
|
||||
// Refresh photo list
|
||||
_allPhotoIds.Remove(photoIdToDelete);
|
||||
|
||||
UpdateStatusText($"Photo deleted. {_allPhotoIds.Count} photos remaining");
|
||||
|
||||
Logging.Debug($"[StatuePhotoGalleryController] Photo deleted: {photoIdToDelete}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update photo info text in enlarged view
|
||||
/// </summary>
|
||||
private void UpdatePhotoInfo(string photoId)
|
||||
{
|
||||
if (photoInfoText == null) return;
|
||||
|
||||
StatuePhotoManager.PhotoMetadata metadata = StatuePhotoManager.GetPhotoMetadata(photoId);
|
||||
|
||||
if (metadata != null)
|
||||
{
|
||||
System.DateTime timestamp = System.DateTime.Parse(metadata.timestamp);
|
||||
string dateStr = timestamp.ToString("MMM dd, yyyy hh:mm tt");
|
||||
|
||||
float fileSizeMB = metadata.fileSizeBytes / (1024f * 1024f);
|
||||
|
||||
photoInfoText.text = $"Date: {dateStr}\n" +
|
||||
$"Decorations: {metadata.decorationCount}\n" +
|
||||
$"Size: {fileSizeMB:F2} MB";
|
||||
// Use cached photo
|
||||
_enlargeController.EnlargePhoto(gridItem, enlargedPreviewPrefab != null ? enlargedPreviewPrefab : gridItem.gameObject, fullPhoto, enlargedScale);
|
||||
}
|
||||
else
|
||||
{
|
||||
photoInfoText.text = "Photo information unavailable";
|
||||
// Load full-size photo
|
||||
fullPhoto = PhotoManager.LoadPhoto(CaptureType.StatueMinigame, photoId);
|
||||
|
||||
if (fullPhoto == null)
|
||||
{
|
||||
Logging.Error($"[StatuePhotoGalleryController] Failed to load photo: {photoId}");
|
||||
return;
|
||||
}
|
||||
|
||||
// Cache it (limited cache)
|
||||
if (_fullPhotoCache.Count < 10) // Keep only recent 10 full photos
|
||||
{
|
||||
_fullPhotoCache[photoId] = fullPhoto;
|
||||
}
|
||||
|
||||
_enlargeController.EnlargePhoto(gridItem, enlargedPreviewPrefab != null ? enlargedPreviewPrefab : gridItem.gameObject, fullPhoto, enlargedScale);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Cleanup when gallery is closed
|
||||
/// </summary>
|
||||
public void CleanupGallery()
|
||||
{
|
||||
if (_enlargeController != null)
|
||||
{
|
||||
_enlargeController.Cleanup();
|
||||
}
|
||||
|
||||
// Clean up cached full photos
|
||||
foreach (var photo in _fullPhotoCache.Values)
|
||||
{
|
||||
if (photo != null)
|
||||
{
|
||||
Destroy(photo);
|
||||
}
|
||||
}
|
||||
_fullPhotoCache.Clear();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update status text
|
||||
/// </summary>
|
||||
private void UpdateStatusText(string message)
|
||||
{
|
||||
if (statusText != null)
|
||||
statusText.text = message;
|
||||
if (pageStatusText != null)
|
||||
pageStatusText.text = message;
|
||||
|
||||
Logging.Debug($"[StatuePhotoGalleryController] Status: {message}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clear all grid items and cached data
|
||||
/// Clear only the grid items (used when switching pages)
|
||||
/// </summary>
|
||||
private void ClearGallery()
|
||||
private void ClearGrid()
|
||||
{
|
||||
// Destroy grid items
|
||||
foreach (var gridItem in _activeGridItems.Values)
|
||||
@@ -359,6 +348,16 @@ namespace Minigames.StatueDressup.Controllers
|
||||
}
|
||||
_activeGridItems.Clear();
|
||||
|
||||
Logging.Debug("[StatuePhotoGalleryController] Grid cleared");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clear all grid items and cached data (full cleanup)
|
||||
/// </summary>
|
||||
private void ClearGallery()
|
||||
{
|
||||
ClearGrid();
|
||||
|
||||
// Clear thumbnail cache
|
||||
foreach (var thumbnail in _thumbnailCache.Values)
|
||||
{
|
||||
@@ -368,7 +367,7 @@ namespace Minigames.StatueDressup.Controllers
|
||||
_thumbnailCache.Clear();
|
||||
_thumbnailCacheOrder.Clear();
|
||||
|
||||
Logging.Debug("[StatuePhotoGalleryController] Gallery cleared");
|
||||
Logging.Debug("[StatuePhotoGalleryController] Gallery fully cleared");
|
||||
}
|
||||
|
||||
internal override void OnManagedDestroy()
|
||||
@@ -377,17 +376,14 @@ namespace Minigames.StatueDressup.Controllers
|
||||
|
||||
// Cleanup
|
||||
ClearGallery();
|
||||
CloseEnlargedView();
|
||||
CleanupGallery();
|
||||
|
||||
// Unsubscribe buttons
|
||||
if (closeEnlargedButton != null)
|
||||
closeEnlargedButton.onClick.RemoveListener(CloseEnlargedView);
|
||||
if (previousPageButton != null)
|
||||
previousPageButton.onClick.RemoveListener(OnPreviousPageClicked);
|
||||
|
||||
if (deletePhotoButton != null)
|
||||
deletePhotoButton.onClick.RemoveListener(DeleteCurrentPhoto);
|
||||
|
||||
if (loadMoreButton != null)
|
||||
loadMoreButton.onClick.RemoveListener(LoadNextPage);
|
||||
if (nextPageButton != null)
|
||||
nextPageButton.onClick.RemoveListener(OnNextPageClicked);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user