Skip to content

Commit

Permalink
feat: Move visibility monitoring from View to UGXBehaviour. Resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
adrenak committed Mar 31, 2021
1 parent 893f36e commit 32917c3
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 51 deletions.
51 changes: 51 additions & 0 deletions Assets/Adrenak.UGX/Runtime/$Misc/UGXBehaviour.cs
Original file line number Diff line number Diff line change
@@ -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<View>();
public Window window => GetComponent<Window>();
public PositionTransitioner positionTransitioner => GetComponent<PositionTransitioner>();
public OpacityTransitioner opacityTransitioner => GetComponent<OpacityTransitioner>();

public UnityEvent<Visibility> 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<RectTransform>();
return rt;
}
}

/// <summary>
/// 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
/// </summary>
protected void Awake() => CurrentVisibility = GetVisibility();

/// <summary>
/// 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
/// </summary>
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;
}
}
}
3 changes: 2 additions & 1 deletion Assets/Adrenak.UGX/Runtime/Navigation/Window.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ namespace Adrenak.UGX {
public class OpacityTransitioner : TransitionerBase {
public OpacityTransitionArgs defaultOpacityTween;

RectTransform rt;
public RectTransform RT => rt == null ? rt = GetComponent<RectTransform>() : rt;

CanvasGroup cg;
public CanvasGroup CG => cg == null ? cg = GetComponent<CanvasGroup>() : cg;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<RectTransform>() : rt;

[Button("Move In")]
async public void MoveIn() => await MoveInAwaitable();
async public UniTask MoveInAwaitable() {
Expand Down
44 changes: 1 addition & 43 deletions Assets/Adrenak.UGX/Runtime/View/View.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,47 +42,5 @@ public TViewState MyViewState {

[Serializable]
[RequireComponent(typeof(RectTransform))]
public class View : UGXBehaviour {
public UnityEvent<Visibility> onVisibilityChanged;
[ReadOnly] [SerializeField] Visibility currentVisibility = Visibility.None;

public Visibility CurrentVisibility {
get => currentVisibility;
private set => currentVisibility = value;
}

RectTransform rt;
public RectTransform RectTransform => rt ?? (rt = GetComponent<RectTransform>());

/// <summary>
/// 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
/// </summary>
protected void Awake() => CurrentVisibility = GetVisibility();

/// <summary>
/// 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
/// </summary>
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 { }
}
2 changes: 1 addition & 1 deletion Assets/Adrenak.UGX/Runtime/View/ViewListBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Adrenak.UGX {
public abstract class ViewListBehaviour<TState, TView> : UGXBehaviour where TState : ViewState where TView : View<TState> {
public Transform container = null;
public TView template = null;
[SerializeField] bool useDefaultData;
[SerializeField] bool useDefaultData = false;
[ShowIf("useDefaultData")] public List<TState> defaultData;

public ViewList<TState, TView> InnerList { get; private set; }
Expand Down

0 comments on commit 32917c3

Please sign in to comment.