From 32917c3a58f0d71bb7cc940d7a7b3101dc9d0700 Mon Sep 17 00:00:00 2001 From: Vatsal Ambastha Date: Wed, 31 Mar 2021 15:46:50 +0530 Subject: [PATCH] feat: Move visibility monitoring from View to UGXBehaviour. Resolve --- .../Adrenak.UGX/Runtime/$Misc/UGXBehaviour.cs | 51 +++++++++++++++++++ .../Adrenak.UGX/Runtime/Navigation/Window.cs | 3 +- .../Transitioners/OpacityTransitioner.cs | 3 -- .../Transitioners/PositionTransitioner.cs | 3 -- Assets/Adrenak.UGX/Runtime/View/View.cs | 44 +--------------- .../Runtime/View/ViewListBehaviour.cs | 2 +- 6 files changed, 55 insertions(+), 51 deletions(-) diff --git a/Assets/Adrenak.UGX/Runtime/$Misc/UGXBehaviour.cs b/Assets/Adrenak.UGX/Runtime/$Misc/UGXBehaviour.cs index 579618f..6012de4 100644 --- a/Assets/Adrenak.UGX/Runtime/$Misc/UGXBehaviour.cs +++ b/Assets/Adrenak.UGX/Runtime/$Misc/UGXBehaviour.cs @@ -1,10 +1,61 @@ using UnityEngine; +using NaughtyAttributes; +using UnityEngine.Events; namespace Adrenak.UGX { + [RequireComponent(typeof(RectTransform))] public class UGXBehaviour : MonoBehaviour { public View view => GetComponent(); public Window window => GetComponent(); public PositionTransitioner positionTransitioner => GetComponent(); public OpacityTransitioner opacityTransitioner => GetComponent(); + + public UnityEvent onVisibilityChanged; + [ReadOnly] [SerializeField] Visibility currentVisibility = Visibility.None; + + public Visibility CurrentVisibility { + get => currentVisibility; + private set => currentVisibility = value; + } + + RectTransform rt; + public RectTransform RT { + get { + if (rt == null) + rt = GetComponent(); + return rt; + } + } + + /// + /// If you're overriding this, make sure to call base.Awake() first thing + /// in the Awake method of your subclass + /// By marking this method as protected, it'll warn any inheritors + /// + protected void Awake() => CurrentVisibility = GetVisibility(); + + /// + /// If you're overriding this, make sure to call base.Update() first thing + /// in the Update method of your subclass + /// By marking this method as protected, it'll warn any inheritors + /// + protected void Update() { + UpdateVisibility(); + } + + void UpdateVisibility() { + var visibility = GetVisibility(); + if (CurrentVisibility == visibility) return; + + CurrentVisibility = visibility; + onVisibilityChanged?.Invoke(CurrentVisibility); + } + + Visibility GetVisibility() { + if (RT.IsVisible(out bool? fully)) + return fully.Value ? Visibility.None : Visibility.Partial; + else + return Visibility.Full; + } } } diff --git a/Assets/Adrenak.UGX/Runtime/Navigation/Window.cs b/Assets/Adrenak.UGX/Runtime/Navigation/Window.cs index 7cdee33..87c395d 100644 --- a/Assets/Adrenak.UGX/Runtime/Navigation/Window.cs +++ b/Assets/Adrenak.UGX/Runtime/Navigation/Window.cs @@ -23,7 +23,8 @@ public class Window : UGXBehaviour { [BoxGroup("Fullscreen")] [SerializeField] bool changeFullscreen = false; [BoxGroup("Fullscreen")] [ShowIf("changeFullscreen")] public bool isFullscreen = false; - protected void Update() { + protected new void Update() { + base.Update(); CheckBackPress(); } diff --git a/Assets/Adrenak.UGX/Runtime/Transitions/Transitioners/OpacityTransitioner.cs b/Assets/Adrenak.UGX/Runtime/Transitions/Transitioners/OpacityTransitioner.cs index 00d5969..e5838be 100644 --- a/Assets/Adrenak.UGX/Runtime/Transitions/Transitioners/OpacityTransitioner.cs +++ b/Assets/Adrenak.UGX/Runtime/Transitions/Transitioners/OpacityTransitioner.cs @@ -7,9 +7,6 @@ namespace Adrenak.UGX { public class OpacityTransitioner : TransitionerBase { public OpacityTransitionArgs defaultOpacityTween; - RectTransform rt; - public RectTransform RT => rt == null ? rt = GetComponent() : rt; - CanvasGroup cg; public CanvasGroup CG => cg == null ? cg = GetComponent() : cg; diff --git a/Assets/Adrenak.UGX/Runtime/Transitions/Transitioners/PositionTransitioner.cs b/Assets/Adrenak.UGX/Runtime/Transitions/Transitioners/PositionTransitioner.cs index b0e301d..ab4ed52 100644 --- a/Assets/Adrenak.UGX/Runtime/Transitions/Transitioners/PositionTransitioner.cs +++ b/Assets/Adrenak.UGX/Runtime/Transitions/Transitioners/PositionTransitioner.cs @@ -20,9 +20,6 @@ public class PositionTransitioner : TransitionerBase { [Button("Set As Out")] public void CaptureOutPosition() => outPosition = RT.localPosition; - RectTransform rt; - public RectTransform RT => rt == null ? rt = GetComponent() : rt; - [Button("Move In")] async public void MoveIn() => await MoveInAwaitable(); async public UniTask MoveInAwaitable() { diff --git a/Assets/Adrenak.UGX/Runtime/View/View.cs b/Assets/Adrenak.UGX/Runtime/View/View.cs index a9f783e..2721de0 100644 --- a/Assets/Adrenak.UGX/Runtime/View/View.cs +++ b/Assets/Adrenak.UGX/Runtime/View/View.cs @@ -42,47 +42,5 @@ public TViewState MyViewState { [Serializable] [RequireComponent(typeof(RectTransform))] - public class View : UGXBehaviour { - public UnityEvent onVisibilityChanged; - [ReadOnly] [SerializeField] Visibility currentVisibility = Visibility.None; - - public Visibility CurrentVisibility { - get => currentVisibility; - private set => currentVisibility = value; - } - - RectTransform rt; - public RectTransform RectTransform => rt ?? (rt = GetComponent()); - - /// - /// If you're overriding this, make sure to call base.Awake() first thing - /// in the Awake method of your subclass - /// By marking this method as protected, it'll warn any inheritors - /// - protected void Awake() => CurrentVisibility = GetVisibility(); - - /// - /// If you're overriding this, make sure to call base.Update() first thing - /// in the Update method of your subclass - /// By marking this method as protected, it'll warn any inheritors - /// - protected void Update() { - UpdateVisibility(); - } - - void UpdateVisibility() { - var visibility = GetVisibility(); - if (CurrentVisibility == visibility) return; - - CurrentVisibility = visibility; - onVisibilityChanged?.Invoke(CurrentVisibility); - } - - Visibility GetVisibility() { - if (RectTransform.IsVisible(out bool? fully)) - return fully.Value ? Visibility.None : Visibility.Partial; - else - return Visibility.Full; - } - } + public class View : UGXBehaviour { } } diff --git a/Assets/Adrenak.UGX/Runtime/View/ViewListBehaviour.cs b/Assets/Adrenak.UGX/Runtime/View/ViewListBehaviour.cs index 4fb9c92..9b1ce9b 100644 --- a/Assets/Adrenak.UGX/Runtime/View/ViewListBehaviour.cs +++ b/Assets/Adrenak.UGX/Runtime/View/ViewListBehaviour.cs @@ -8,7 +8,7 @@ namespace Adrenak.UGX { public abstract class ViewListBehaviour : UGXBehaviour where TState : ViewState where TView : View { public Transform container = null; public TView template = null; - [SerializeField] bool useDefaultData; + [SerializeField] bool useDefaultData = false; [ShowIf("useDefaultData")] public List defaultData; public ViewList InnerList { get; private set; }