forked from Shardstone/trail-into-darkness
added documentation, fixed some bugs
This commit is contained in:
@@ -2,23 +2,44 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using Jovian.PopupSystem.UI;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
namespace Jovian.PopupSystem {
|
||||
/// <summary>
|
||||
/// Core implementation of <see cref="IPopupSystem"/>. Created per game state, not as a singleton.
|
||||
/// Manages category registration, trigger discovery, popup lifecycle, priority dismissal, and
|
||||
/// tick-driven delay timers and animations. Pass a canvasParent to auto-scan triggers on construction.
|
||||
/// </summary>
|
||||
public sealed class PopupSystem : IPopupSystem {
|
||||
readonly PopupSettings settings;
|
||||
readonly PopupReference viewPrefab;
|
||||
readonly Func<IPopupAnimator> animatorFactory;
|
||||
readonly Transform canvasParent;
|
||||
readonly Dictionary<PopupCategory, ViewState> categories = new();
|
||||
readonly List<PopupTrigger> registeredTriggers = new();
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new popup system instance.
|
||||
/// </summary>
|
||||
/// <param name="settings">Configuration ScriptableObject with delays, priorities, and display settings.</param>
|
||||
/// <param name="viewPrefab">The PopupReference prefab to instantiate per category.</param>
|
||||
/// <param name="canvasParent">Optional parent Canvas transform. When provided, popup views are parented
|
||||
/// here (inheriting CanvasScaler) and all PopupTrigger components are auto-scanned.</param>
|
||||
/// <param name="animatorFactory">Optional factory for custom animators. Defaults to FadePopupAnimator.</param>
|
||||
public PopupSystem(PopupSettings settings, PopupReference viewPrefab, Transform canvasParent = null, Func<IPopupAnimator> animatorFactory = null) {
|
||||
this.settings = settings;
|
||||
this.viewPrefab = viewPrefab;
|
||||
this.canvasParent = canvasParent;
|
||||
this.animatorFactory = animatorFactory ?? (() => new FadePopupAnimator());
|
||||
|
||||
// Auto-scan if a parent was provided
|
||||
if(canvasParent != null) {
|
||||
ScanTriggers(canvasParent);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void RegisterCategory(PopupCategory category, int priority = 0) {
|
||||
if(categories.ContainsKey(category)) {
|
||||
return;
|
||||
@@ -34,6 +55,7 @@ namespace Jovian.PopupSystem {
|
||||
};
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Show(PopupCategory category, Action<PopupContentBuilder> buildContent,
|
||||
RectTransform anchor = null, AnchorSide? anchorSide = null) {
|
||||
if(!categories.TryGetValue(category, out var state)) {
|
||||
@@ -50,6 +72,7 @@ namespace Jovian.PopupSystem {
|
||||
state.isPending = true;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void ShowAtPosition(PopupCategory category, Action<PopupContentBuilder> buildContent,
|
||||
Vector2 screenPosition) {
|
||||
if(!categories.TryGetValue(category, out var state)) {
|
||||
@@ -65,6 +88,7 @@ namespace Jovian.PopupSystem {
|
||||
state.isPending = true;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Hide(PopupCategory category) {
|
||||
if(!categories.TryGetValue(category, out var state)) {
|
||||
return;
|
||||
@@ -78,12 +102,14 @@ namespace Jovian.PopupSystem {
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void HideAll() {
|
||||
foreach(var kvp in categories) {
|
||||
Hide(kvp.Key);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Tick(float deltaTime) {
|
||||
foreach(var kvp in categories) {
|
||||
kvp.Value.animator.Tick(deltaTime);
|
||||
@@ -112,13 +138,55 @@ namespace Jovian.PopupSystem {
|
||||
}
|
||||
}
|
||||
|
||||
public void InitializeTriggersInChildren(Transform parent, Action<PopupTrigger> configureTrigger) {
|
||||
/// <inheritdoc />
|
||||
public void ScanTriggers(Transform parent) {
|
||||
var triggers = parent.GetComponentsInChildren<PopupTrigger>(true);
|
||||
foreach(var trigger in triggers) {
|
||||
configureTrigger(trigger);
|
||||
if(!registeredTriggers.Contains(trigger)) {
|
||||
BindTrigger(trigger);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public PopupTriggerView GetTriggerHandler(string gameObjectName) {
|
||||
foreach(var trigger in registeredTriggers) {
|
||||
if(trigger != null && trigger.gameObject.name == gameObjectName) {
|
||||
return trigger.Handler;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public IReadOnlyList<PopupTriggerView> GetTriggerHandlers(PopupCategory category) {
|
||||
var result = new List<PopupTriggerView>();
|
||||
foreach(var trigger in registeredTriggers) {
|
||||
if(trigger != null && trigger.Category == category && trigger.Handler != null) {
|
||||
result.Add(trigger.Handler);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void InitializeTriggersInChildren(Transform parent, Action<PopupTrigger, PopupTriggerView> configureTrigger) {
|
||||
var triggers = parent.GetComponentsInChildren<PopupTrigger>(true);
|
||||
foreach(var trigger in triggers) {
|
||||
if(!registeredTriggers.Contains(trigger)) {
|
||||
BindTrigger(trigger);
|
||||
}
|
||||
configureTrigger(trigger, trigger.Handler);
|
||||
}
|
||||
}
|
||||
|
||||
private void BindTrigger(PopupTrigger trigger) {
|
||||
var handler = new PopupTriggerView(this);
|
||||
trigger.Bind(handler);
|
||||
registeredTriggers.Add(trigger);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Dispose() {
|
||||
foreach(var kvp in categories) {
|
||||
if(kvp.Value.view != null) {
|
||||
@@ -135,6 +203,15 @@ namespace Jovian.PopupSystem {
|
||||
var builder = new PopupContentBuilder(state.view);
|
||||
state.pendingBuild?.Invoke(builder);
|
||||
|
||||
// Activate before layout rebuild so Unity has an active hierarchy to calculate
|
||||
state.view.CanvasGroup.alpha = 0f;
|
||||
state.view.SetVisible(true);
|
||||
|
||||
// Force full layout rebuild so positioning has correct size on first show
|
||||
Canvas.ForceUpdateCanvases();
|
||||
LayoutRebuilder.ForceRebuildLayoutImmediate(state.view.Content);
|
||||
LayoutRebuilder.ForceRebuildLayoutImmediate((RectTransform)state.view.transform);
|
||||
|
||||
if(state.pendingScreenPos.HasValue) {
|
||||
state.view.SetFixedPosition(state.pendingScreenPos.Value, settings.screenEdgePadding);
|
||||
state.isFollowMouse = false;
|
||||
@@ -148,7 +225,6 @@ namespace Jovian.PopupSystem {
|
||||
state.isFollowMouse = true;
|
||||
}
|
||||
|
||||
state.view.SetVisible(true);
|
||||
state.view.UpdatePosition();
|
||||
state.animator.Show(state.view.CanvasGroup, settings.fadeDuration, null);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user