Files
AppleHillsProduction/Assets/Scripts/CardSystem/Controllers/ProgressBarController.cs
tschesky 235fa04eba Merge a card refresh (#59)
- **Refactored Card Placement Flow**
  - Separated card presentation from orchestration logic
  - Extracted `CornerCardManager` for pending card lifecycle (spawn, shuffle, rebuild)
  - Extracted `AlbumNavigationService` for book page navigation and zone mapping
  - Extracted `CardEnlargeController` for backdrop management and card reparenting
  - Implemented controller pattern (non-MonoBehaviour) for complex logic
  - Cards now unparent from slots before rebuild to prevent premature destruction

- **Improved Corner Card Display**
  - Fixed cards spawning on top of each other during rebuild
  - Implemented shuffle-to-front logic (remaining cards occupy slots 0→1→2)
  - Added smart card selection (prioritizes cards matching current album page)
  - Pending cards now removed from queue immediately on drag start
  - Corner cards rebuild after each placement with proper slot reassignment

- **Enhanced Album Card Viewing**
  - Added dramatic scale increase when viewing cards from album slots
  - Implemented shrink animation when dismissing enlarged cards
  - Cards transition: `PlacedInSlotState` → `AlbumEnlargedState` → `PlacedInSlotState`
  - Backdrop shows/hides with card enlarge/shrink cycle
  - Cards reparent to enlarged container while viewing, return to slot after

- **State Machine Improvements**
  - Added `CardStateNames` constants for type-safe state transitions
  - Implemented `ICardClickHandler` and `ICardStateDragHandler` interfaces
  - State transitions now use cached property indices
  - `BoosterCardContext` separated from `CardContext` for single responsibility

- **Code Cleanup**
  - Identified unused `SlotContainerHelper.cs` (superseded by `CornerCardManager`)
  - Identified unused `BoosterPackDraggable.canOpenOnDrop` field
  - Identified unused `AlbumViewPage._previousInputMode` (hardcoded value)
  - Identified unused `Card.OnPlacedInAlbumSlot` event (no subscribers)

Co-authored-by: Michal Pikulski <michal.a.pikulski@gmail.com>
Co-authored-by: Michal Pikulski <michal@foolhardyhorizons.com>
Reviewed-on: #59
2025-11-18 08:40:59 +00:00

206 lines
7.4 KiB
C#

using System.Collections;
using Core;
using UnityEngine;
using UnityEngine.UI;
using AppleHills.Core.Settings;
namespace UI.CardSystem
{
/// <summary>
/// Controls a vertical progress bar made of individual Image elements.
/// Fills from bottom to top and animates the newest element with a blink effect.
///
/// Setup: Place on GameObject with VerticalLayoutGroup (Reverse Arrangement enabled).
/// First child = first progress element (1/5), last child = last progress element (5/5).
/// </summary>
public class ProgressBarController : MonoBehaviour
{
[Header("Progress Elements")]
[Tooltip("The individual Image components representing progress segments (auto-detected from children)")]
private Image[] _progressElements;
private ICardSystemSettings _settings;
private Coroutine _currentBlinkCoroutine;
private void Awake()
{
_settings = GameManager.GetSettingsObject<ICardSystemSettings>();
// Auto-detect all child Image components
_progressElements = GetComponentsInChildren<Image>(true);
if (_progressElements.Length == 0)
{
Logging.Warning("[ProgressBarController] No child Image components found! Add Image children to this GridLayout.");
}
}
/// <summary>
/// Show progress and animate the newest element with a blink effect.
/// </summary>
/// <param name="currentCount">Current progress (1 to maxCount)</param>
/// <param name="maxCount">Maximum progress value (typically 5)</param>
/// <param name="onComplete">Callback invoked after blink animation completes</param>
public void ShowProgress(int currentCount, int maxCount, System.Action onComplete)
{
// Validate input
if (currentCount < 0 || currentCount > maxCount)
{
Logging.Warning($"[ProgressBarController] Invalid progress: {currentCount}/{maxCount}");
onComplete?.Invoke();
return;
}
// Validate element count
if (_progressElements.Length < maxCount)
{
Logging.Warning($"[ProgressBarController] Not enough progress elements! Expected {maxCount}, found {_progressElements.Length}");
onComplete?.Invoke();
return;
}
// Stop any existing blink animation
if (_currentBlinkCoroutine != null)
{
StopCoroutine(_currentBlinkCoroutine);
_currentBlinkCoroutine = null;
}
// Disable all elements first
foreach (var element in _progressElements)
{
element.enabled = false;
}
// Enable first N elements (since first child = 1/5, last child = 5/5)
// If currentCount = 3, enable elements [0], [1], [2] (first 3 elements)
for (int i = 0; i < currentCount && i < _progressElements.Length; i++)
{
_progressElements[i].enabled = true;
}
Logging.Debug($"[ProgressBarController] Showing progress {currentCount}/{maxCount}");
// Blink the NEWEST element (the last one we just enabled)
// Newest element is at index (currentCount - 1)
int newestIndex = currentCount - 1;
if (newestIndex >= 0 && newestIndex < _progressElements.Length && currentCount > 0)
{
_currentBlinkCoroutine = StartCoroutine(BlinkElement(newestIndex, onComplete));
}
else
{
// No element to blink (e.g., currentCount = 0)
onComplete?.Invoke();
}
}
/// <summary>
/// Show progress without blink animation (instant display).
/// </summary>
/// <param name="currentCount">Current progress (1 to maxCount)</param>
/// <param name="maxCount">Maximum progress value</param>
public void ShowProgressInstant(int currentCount, int maxCount)
{
// Validate
if (currentCount < 0 || currentCount > maxCount || _progressElements.Length < maxCount)
{
return;
}
// Disable all
foreach (var element in _progressElements)
{
element.enabled = false;
}
// Enable first N elements
for (int i = 0; i < currentCount && i < _progressElements.Length; i++)
{
_progressElements[i].enabled = true;
}
}
/// <summary>
/// Hide all progress elements.
/// </summary>
public void HideProgress()
{
foreach (var element in _progressElements)
{
element.enabled = false;
}
}
/// <summary>
/// Blink a specific element by toggling enabled/disabled.
/// </summary>
private IEnumerator BlinkElement(int index, System.Action onComplete)
{
if (index < 0 || index >= _progressElements.Length)
{
onComplete?.Invoke();
yield break;
}
Image element = _progressElements[index];
// Get blink settings (or use defaults if not available)
float blinkDuration = 0.15f; // Duration for each on/off toggle
int blinkCount = 3; // Number of full blinks (on->off->on = 1 blink)
// Try to get settings if available
if (_settings != null)
{
// Settings could expose these if needed:
// blinkDuration = _settings.ProgressBlinkDuration;
// blinkCount = _settings.ProgressBlinkCount;
}
Logging.Debug($"[ProgressBarController] Blinking element {index} ({blinkCount} times)");
// Wait a brief moment before starting blink
yield return new WaitForSeconds(0.3f);
// Perform blinks (on->off->on = 1 full blink)
for (int i = 0; i < blinkCount; i++)
{
// Off
element.enabled = false;
yield return new WaitForSeconds(blinkDuration);
// On
element.enabled = true;
yield return new WaitForSeconds(blinkDuration);
}
// Ensure element is enabled at the end
element.enabled = true;
Logging.Debug($"[ProgressBarController] Blink complete for element {index}");
_currentBlinkCoroutine = null;
onComplete?.Invoke();
}
/// <summary>
/// Get the total number of progress elements available.
/// </summary>
public int GetElementCount()
{
return _progressElements?.Length ?? 0;
}
private void OnDestroy()
{
// Clean up any running coroutines
if (_currentBlinkCoroutine != null)
{
StopCoroutine(_currentBlinkCoroutine);
_currentBlinkCoroutine = null;
}
}
}
}