From 2a2da819943d06a1190296ca07862acf0ba6ed46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillem=20Sunyer=20Cald=C3=BA?= Date: Tue, 15 Mar 2022 23:57:29 +0100 Subject: [PATCH] Set interactable --- .../ViewStack/Entries/ViewStackEntryUtils.cs | 11 ++++- .../ViewStack/Instructions/HideInstruction.cs | 2 + .../SetInteractableInstruction.cs | 45 +++++++++++++++++++ .../SetInteractableInstruction.cs.meta | 11 +++++ .../ViewStack/Instructions/ShowInstruction.cs | 6 ++- .../Instructions/ShowLastInstruction.cs | 4 ++ .../Sequences/IViewStackSequenceBuilder.cs | 1 + .../Sequences/ViewStackSequenceBuilder.cs | 14 ++++++ 8 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 Runtime/Ui/ViewStack/Instructions/SetInteractableInstruction.cs create mode 100644 Runtime/Ui/ViewStack/Instructions/SetInteractableInstruction.cs.meta diff --git a/Runtime/Ui/ViewStack/Entries/ViewStackEntryUtils.cs b/Runtime/Ui/ViewStack/Entries/ViewStackEntryUtils.cs index 40ee827..4a41fe0 100644 --- a/Runtime/Ui/ViewStack/Entries/ViewStackEntryUtils.cs +++ b/Runtime/Ui/ViewStack/Entries/ViewStackEntryUtils.cs @@ -1,7 +1,16 @@ -namespace Juce.CoreUnity.ViewStack.Entries +using UnityEngine; + +namespace Juce.CoreUnity.ViewStack.Entries { public static class ViewStackEntryUtils { + public static void SetInteractable(IViewStackEntry entry, bool set) + { + CanvasGroup canvasGroup = entry.Transform.gameObject.GetOrAddComponent(); + + canvasGroup.interactable = set; + } + public static void Refresh(IViewStackEntry entry, RefreshType type) { foreach(ViewStackEntryRefresh refresh in entry.RefreshList) diff --git a/Runtime/Ui/ViewStack/Instructions/HideInstruction.cs b/Runtime/Ui/ViewStack/Instructions/HideInstruction.cs index df7ca2d..8d59a58 100644 --- a/Runtime/Ui/ViewStack/Instructions/HideInstruction.cs +++ b/Runtime/Ui/ViewStack/Instructions/HideInstruction.cs @@ -51,6 +51,8 @@ protected override async Task OnExecute(CancellationToken cancellationToken) return; } + ViewStackEntryUtils.SetInteractable(entry, false); + bool hasCurrentContext = currentContextRepository.TryGet(out IViewContext context); if (!hasCurrentContext) diff --git a/Runtime/Ui/ViewStack/Instructions/SetInteractableInstruction.cs b/Runtime/Ui/ViewStack/Instructions/SetInteractableInstruction.cs new file mode 100644 index 0000000..0f03db9 --- /dev/null +++ b/Runtime/Ui/ViewStack/Instructions/SetInteractableInstruction.cs @@ -0,0 +1,45 @@ +using Juce.Core.Repositories; +using Juce.Core.Sequencing; +using Juce.CoreUnity.Ui.Frame; +using Juce.CoreUnity.ViewStack.Entries; +using System; +using UnityEngine; + +namespace Playground.Services.ViewStack.Instructions +{ + public class SetInteractableInstruction : InstantInstruction + { + private readonly IUiFrame frame; + private readonly IKeyValueRepository entriesRepository; + private readonly Type entryId; + private readonly bool interactable; + + public SetInteractableInstruction( + IUiFrame frame, + IKeyValueRepository entriesRepository, + Type entryId, + bool interactable + ) + { + this.frame = frame; + this.entriesRepository = entriesRepository; + this.entryId = entryId; + this.interactable = interactable; + } + + protected override void OnInstantExecute() + { + bool found = entriesRepository.TryGet(entryId, out IViewStackEntry entry); + + if (!found) + { + UnityEngine.Debug.LogError($"Tried to Show {nameof(IViewStackEntry)} of type {entryId}, " + + $"but it was not registered, at {nameof(SetInteractableInstruction)}"); + + return; + } + + ViewStackEntryUtils.SetInteractable(entry, interactable); + } + } +} diff --git a/Runtime/Ui/ViewStack/Instructions/SetInteractableInstruction.cs.meta b/Runtime/Ui/ViewStack/Instructions/SetInteractableInstruction.cs.meta new file mode 100644 index 0000000..058e943 --- /dev/null +++ b/Runtime/Ui/ViewStack/Instructions/SetInteractableInstruction.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: b3c3e5aeca8e5624b8fedd9bea535452 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/Ui/ViewStack/Instructions/ShowInstruction.cs b/Runtime/Ui/ViewStack/Instructions/ShowInstruction.cs index 256bff3..35564fa 100644 --- a/Runtime/Ui/ViewStack/Instructions/ShowInstruction.cs +++ b/Runtime/Ui/ViewStack/Instructions/ShowInstruction.cs @@ -44,7 +44,9 @@ protected async override Task OnExecute(CancellationToken cancellationToken) return; } - if(entry.IsPopup) + ViewStackEntryUtils.SetInteractable(entry, false); + + if (entry.IsPopup) { bool hasItem = currentContextRepository.TryGet(out IViewContext context); @@ -69,6 +71,8 @@ protected async override Task OnExecute(CancellationToken cancellationToken) await entry.Visible.SetVisible(visible: true, instantly, cancellationToken); ViewStackEntryUtils.Refresh(entry, RefreshType.AfterShow); + + ViewStackEntryUtils.SetInteractable(entry, true); } } } diff --git a/Runtime/Ui/ViewStack/Instructions/ShowLastInstruction.cs b/Runtime/Ui/ViewStack/Instructions/ShowLastInstruction.cs index 190cd72..1f1070f 100644 --- a/Runtime/Ui/ViewStack/Instructions/ShowLastInstruction.cs +++ b/Runtime/Ui/ViewStack/Instructions/ShowLastInstruction.cs @@ -57,6 +57,8 @@ protected async override Task OnExecute(CancellationToken cancellationToken) return; } + ViewStackEntryUtils.SetInteractable(entry, false); + currentContextRepository.Set(new ViewContext(entry.Id)); ViewStackEntryUtils.Refresh(entry, RefreshType.BeforeShow); @@ -73,6 +75,8 @@ protected async override Task OnExecute(CancellationToken cancellationToken) await entry.Visible.SetVisible(visible: true, instantly, cancellationToken); ViewStackEntryUtils.Refresh(entry, RefreshType.AfterShow); + + ViewStackEntryUtils.SetInteractable(entry, true); } } } diff --git a/Runtime/Ui/ViewStack/Sequences/IViewStackSequenceBuilder.cs b/Runtime/Ui/ViewStack/Sequences/IViewStackSequenceBuilder.cs index 6cf4d57..7a22266 100644 --- a/Runtime/Ui/ViewStack/Sequences/IViewStackSequenceBuilder.cs +++ b/Runtime/Ui/ViewStack/Sequences/IViewStackSequenceBuilder.cs @@ -11,6 +11,7 @@ public interface IViewStackSequenceBuilder IViewStackSequenceBuilder ShowLast(bool instantly); IViewStackSequenceBuilder ShowLastBehindForeground(bool instantly); IViewStackSequenceBuilder MoveToBackground(); + IViewStackSequenceBuilder SetInteractable(bool set); Task Execute(CancellationToken cancellationToken); void Execute(); diff --git a/Runtime/Ui/ViewStack/Sequences/ViewStackSequenceBuilder.cs b/Runtime/Ui/ViewStack/Sequences/ViewStackSequenceBuilder.cs index ab8ce95..3aaa568 100644 --- a/Runtime/Ui/ViewStack/Sequences/ViewStackSequenceBuilder.cs +++ b/Runtime/Ui/ViewStack/Sequences/ViewStackSequenceBuilder.cs @@ -126,6 +126,20 @@ public IViewStackSequenceBuilder MoveToBackground() return this; } + public IViewStackSequenceBuilder SetInteractable(bool set) + { + Type entryId = typeof(T); + + instructionsToPlay.Add(new SetInteractableInstruction( + frame, + entriesRepository, + entryId, + set + )); + + return this; + } + public Task Execute(CancellationToken cancellationToken) { foreach(Instruction instruction in instructionsToPlay)