Skip to content
This repository has been archived by the owner on Dec 30, 2024. It is now read-only.

Commit

Permalink
🚧 Work on custom user overlays
Browse files Browse the repository at this point in the history
  • Loading branch information
Chasmical committed Jul 31, 2023
1 parent eec90bb commit 7c3063f
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 60 deletions.
2 changes: 1 addition & 1 deletion RogueLibsCore/Hooks/HookExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ public static IEnumerable<IHook> GetHooks(this StatusEffect instance)
public static IEnumerable<IHook> GetHooks(this Trait instance)
=> GetHooksShared(instance.GetHookControllerIfExists());

public static TInterface Get<TInterface>(this MainGUI mainGUI) where TInterface : CustomUserInterface
public static TInterface Get<TInterface>(this MainGUI mainGUI) where TInterface : CustomUiBase
{
string uiName = typeof(TInterface).FullName;
Transform? tr = mainGUI.transform.Find(uiName);
Expand Down
28 changes: 28 additions & 0 deletions RogueLibsCore/Hooks/UserInterfaces/CustomUiBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using UnityEngine;
using UnityEngine.UI;

namespace RogueLibsCore
{
public abstract class CustomUiBase : CustomUiElement, IHook<MainGUI>
{
public MainGUI MainGUI { get; private set; } = null!;
protected Canvas canvas { get; private set; } = null!;
protected GraphicRaycaster graphicRaycaster { get; private set; } = null!;
protected CanvasGroup canvasGroup { get; private set; } = null!;

object IHook.Instance => MainGUI;
MainGUI IHook<MainGUI>.Instance => MainGUI;
void IHook.Initialize(object _) { }

public override void Awake()
{
MainGUI = gameObject.GetComponentInParent<MainGUI>();
SetCenterPosition(gameObject, transform.parent, new Rect(960f, 540f, 1920f, 1080f));

canvas = gameObject.AddComponent<Canvas>();
graphicRaycaster = gameObject.AddComponent<GraphicRaycaster>();
canvasGroup = gameObject.AddComponent<CanvasGroup>();
}

}
}
67 changes: 67 additions & 0 deletions RogueLibsCore/Hooks/UserInterfaces/CustomUiElement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using UnityEngine;

namespace RogueLibsCore
{
public abstract class CustomUiElement : MonoBehaviour
{
private RectTransform? _rect;
public RectTransform rect => _rect ??= GetComponent<RectTransform>();

public abstract void Awake();

/// <summary>
/// <para>Gets the currently used instance of <see cref="GameController"/>.</para>
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "Usage of gc fields in SoR")]
// ReSharper disable once InconsistentNaming
public static GameController gc => GameController.gameController;

public GameObject CreateElement(string gameObjectName, Vector2 position)
=> CreateElement(transform, gameObjectName, position);
public GameObject CreateElement(string gameObjectName, Rect rectangle)
=> CreateElement(transform, gameObjectName, rectangle);
public static GameObject CreateElement(Transform parent, string gameObjectName, Vector2 position)
=> CreateElement(parent, gameObjectName, new Rect(position, new Vector2(100f, 100f)));
public static GameObject CreateElement(Transform parent, string gameObjectName, Rect rectangle)
{
GameObject go = new GameObject(gameObjectName, typeof(RectTransform));
SetTopLeftCornerPosition(go, parent, rectangle);
return go;
}

public TElement CreateElement<TElement>(string gameObjectName, Vector2 position) where TElement : Component
=> CreateElement<TElement>(transform, gameObjectName, position);
public TElement CreateElement<TElement>(string gameObjectName, Rect rectangle) where TElement : Component
=> CreateElement<TElement>(transform, gameObjectName, rectangle);
public static TElement CreateElement<TElement>(Transform parent, string gameObjectName, Vector2 position) where TElement : Component
=> CreateElement<TElement>(parent, gameObjectName, new Rect(position, new Vector2(100f, 100f)));
public static TElement CreateElement<TElement>(Transform parent, string gameObjectName, Rect rectangle) where TElement : Component
=> CreateElement(parent, gameObjectName, rectangle).AddComponent<TElement>();

protected static void SetTopLeftCornerPosition(GameObject go, Transform parent, Rect rectangle)
{
RectTransform rect = go.GetComponent<RectTransform>();
rect.SetParent(parent);

rect.localScale = Vector3.one;
rect.anchorMin = new Vector2(0f, 1f);
rect.anchorMax = new Vector2(0f, 1f);
rect.pivot = new Vector2(0f, 1f);
rect.anchoredPosition = new Vector2(rectangle.x, -rectangle.y);
rect.sizeDelta = rectangle.size;
}
protected static void SetCenterPosition(GameObject go, Transform parent, Rect rectangle)
{
RectTransform rect = go.GetComponent<RectTransform>();
rect.SetParent(parent);

rect.localScale = Vector3.one;
rect.anchorMin = new Vector2(0f, 1f);
rect.anchorMax = new Vector2(0f, 1f);
rect.pivot = new Vector2(0.5f, 0.5f);
rect.anchoredPosition = new Vector2(rectangle.x, -rectangle.y);
rect.sizeDelta = rectangle.size;
}

}
}
110 changes: 53 additions & 57 deletions RogueLibsCore/Hooks/UserInterfaces/CustomUserInterface.cs
Original file line number Diff line number Diff line change
@@ -1,94 +1,90 @@
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEngine;

namespace RogueLibsCore
{
public abstract class CustomUserInterface : CustomUiElement, IHook<MainGUI>
public abstract class CustomUserInterface : CustomUiBase
{
public MainGUI MainGUI { get; private set; } = null!;
protected Canvas canvas { get; private set; } = null!;
protected GraphicRaycaster graphicRaycaster { get; private set; } = null!;

public bool IsOpened => canvas.enabled;
public virtual bool LocksCamera => true;

object IHook.Instance => MainGUI;
MainGUI IHook<MainGUI>.Instance => MainGUI;
void IHook.Initialize(object _) { }

/// <summary>
/// <para>Gets the currently used instance of <see cref="GameController"/>.</para>
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "Usage of gc fields in SoR")]
// ReSharper disable once InconsistentNaming
public static GameController gc => GameController.gameController;
public virtual Vector2? CameraLock => Vector2.zero;

public sealed override void Awake()
{
MainGUI = gameObject.GetComponentInParent<MainGUI>();
SetNormalizedPosition(gameObject, transform.parent, new Rect(0f, 0f, 1920f, 1080f));

canvas = gameObject.AddComponent<Canvas>();
base.Awake();
canvas.enabled = false;

graphicRaycaster = gameObject.AddComponent<GraphicRaycaster>();
graphicRaycaster.enabled = false;
canvasGroup.alpha = 0f;
Setup();
}
public abstract void Setup();

private Coroutine? animatingCoroutine;
public void ShowInterface()
{
if (canvas.enabled) return;
gc.audioHandler.Play(MainGUI.agent, "ShowInterface");
canvas.enabled = true;

if (animatingCoroutine is not null)
StopCoroutine(animatingCoroutine);
animatingCoroutine = StartCoroutine(WrapShowAnimation());

OnOpened();
}
public void HideInterface()
{
if (!canvas.enabled) return;
gc.audioHandler.Play(MainGUI.agent, "HideInterface");
canvas.enabled = false;

if (animatingCoroutine is not null)
StopCoroutine(animatingCoroutine);
animatingCoroutine = StartCoroutine(WrapHideAnimation());

OnClosed();
}

public abstract void OnOpened();
public virtual void OnClosed() { }

}
public abstract class CustomUiElement : MonoBehaviour
{
private RectTransform? _rect;
public RectTransform rect => _rect ??= GetComponent<RectTransform>();

public abstract void Awake();
public abstract void OnClosed();

public static TElement Create<TElement>(Transform parent, string gameObjectName, Rect rectangle) where TElement : Component
private IEnumerator WrapShowAnimation()
{
GameObject go = new GameObject(gameObjectName, typeof(RectTransform));
RectTransform rect = SetNormalizedPosition(go, parent, rectangle);
return go.AddComponent<TElement>();
canvas.enabled = true;
graphicRaycaster.enabled = false;
yield return ShowAnimation();
animatingCoroutine = null;
graphicRaycaster.enabled = true;
}
public static TImage Create<TImage>(Transform parent, string gameObjectName, Vector2 position, byte[] spriteData, float scale = 1f) where TImage : Image
private IEnumerator WrapHideAnimation()
{
Sprite sprite = RogueUtilities.ConvertToSprite(spriteData);
TImage img = Create<TImage>(parent, gameObjectName, new Rect(position, sprite.rect.size * scale));
img.sprite = sprite;
return img;
canvas.enabled = true;
graphicRaycaster.enabled = false;
yield return HideAnimation();
animatingCoroutine = null;
canvas.enabled = false;
}

protected static RectTransform SetNormalizedPosition(GameObject go, Transform parent, Rect rectangle)
protected virtual IEnumerator ShowAnimation()
{
RectTransform rect = go.GetComponent<RectTransform>();
rect.SetParent(parent);
gc.audioHandler.Play(MainGUI.agent, "ShowInterface");

rect.localScale = Vector3.one;
rect.anchorMin = new Vector2(0f, 1f);
rect.anchorMax = new Vector2(0f, 1f);
rect.pivot = new Vector2(0f, 1f);
rect.anchoredPosition = new Vector2(rectangle.x, -rectangle.y);
rect.sizeDelta = rectangle.size;
float x = canvasGroup.alpha;
while (x < 1f)
{
x = Mathf.Clamp01(x + 5f * Time.deltaTime);
canvasGroup.alpha = x;
rect.localScale = new Vector3(x, x, x);
yield return null;
}
}
protected virtual IEnumerator HideAnimation()
{
gc.audioHandler.Play(MainGUI.agent, "HideInterface");

return rect;
float x = canvasGroup.alpha;
while (x > 0f)
{
x = Mathf.Clamp01(x - 5f * Time.deltaTime);
canvasGroup.alpha = x;
rect.localScale = new Vector3(x, x, x);
yield return null;
}
}

}
Expand Down
16 changes: 16 additions & 0 deletions RogueLibsCore/Hooks/UserInterfaces/CustomUserOverlay.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace RogueLibsCore
{
public abstract class CustomUserOverlay : CustomUiBase
{
public sealed override void Awake()
{
base.Awake();
canvas.enabled = true;
graphicRaycaster.enabled = true;
canvasGroup.alpha = 1f;
Setup();
}
public abstract void Setup();

}
}
5 changes: 3 additions & 2 deletions RogueLibsCore/Patches/Patches_MainGUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ private static void HideInterfaces(MainGUI mainGUI)
IHookController? controller = mainGUI.GetHookControllerIfExists();
if (controller is null) return;
foreach (IHook hook in controller.GetHooks())
(hook as CustomUserInterface)?.HideInterface();
if (hook is CustomUserInterface ui)
ui.HideInterface();
}

private static readonly FieldInfo openedInventoryField = AccessTools.Field(typeof(MainGUI), nameof(MainGUI.openedInventory));
Expand Down Expand Up @@ -112,7 +113,7 @@ private static bool GetIsCameraLocked(MainGUI mainGUI)
if (mainGUI.openedInventory) return true;
IHookController? controller = mainGUI.GetHookControllerIfExists();
if (controller is null) return false;
return controller.GetHooks().Any(static h => h is CustomUserInterface ui && ui.IsOpened && ui.LocksCamera);
return controller.GetHooks().Any(static h => h is CustomUserInterface ui && ui.IsOpened && ui.CameraLock is not null);
}

}
Expand Down

0 comments on commit 7c3063f

Please sign in to comment.