Skip to content

Commit

Permalink
Set interactable
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillemsc committed Mar 15, 2022
1 parent a10f9db commit 2a2da81
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 2 deletions.
11 changes: 10 additions & 1 deletion Runtime/Ui/ViewStack/Entries/ViewStackEntryUtils.cs
Original file line number Diff line number Diff line change
@@ -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>();

canvasGroup.interactable = set;
}

public static void Refresh(IViewStackEntry entry, RefreshType type)
{
foreach(ViewStackEntryRefresh refresh in entry.RefreshList)
Expand Down
2 changes: 2 additions & 0 deletions Runtime/Ui/ViewStack/Instructions/HideInstruction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
45 changes: 45 additions & 0 deletions Runtime/Ui/ViewStack/Instructions/SetInteractableInstruction.cs
Original file line number Diff line number Diff line change
@@ -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<Type, IViewStackEntry> entriesRepository;
private readonly Type entryId;
private readonly bool interactable;

public SetInteractableInstruction(
IUiFrame frame,
IKeyValueRepository<Type, IViewStackEntry> 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);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion Runtime/Ui/ViewStack/Instructions/ShowInstruction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
}
}
}
4 changes: 4 additions & 0 deletions Runtime/Ui/ViewStack/Instructions/ShowLastInstruction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public interface IViewStackSequenceBuilder
IViewStackSequenceBuilder ShowLast(bool instantly);
IViewStackSequenceBuilder ShowLastBehindForeground(bool instantly);
IViewStackSequenceBuilder MoveToBackground<T>();
IViewStackSequenceBuilder SetInteractable<T>(bool set);

Task Execute(CancellationToken cancellationToken);
void Execute();
Expand Down
14 changes: 14 additions & 0 deletions Runtime/Ui/ViewStack/Sequences/ViewStackSequenceBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,20 @@ public IViewStackSequenceBuilder MoveToBackground<T>()
return this;
}

public IViewStackSequenceBuilder SetInteractable<T>(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)
Expand Down

0 comments on commit 2a2da81

Please sign in to comment.