Skip to content
Permalink
Browse files

Check in UIViewController

  • Loading branch information...
CaliCastle committed Mar 2, 2019
1 parent 75e6884 commit fefbf6ba089d1259a91ee5fa77e2d325f938f2f7

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -0,0 +1,8 @@
using UIKit;

namespace Example
{
internal sealed class ExampleViewController : UIViewController
{
}
}

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -0,0 +1,49 @@
using UnityEditor;
using UnityEngine;
using JetBrains.Annotations;

namespace UIKit
{
[CustomEditor( typeof( UIViewController ), true )]
public sealed class UIViewControllerEditor : Editor
{
[NotNull]
private UIViewController m_viewController
{
get { return ( UIViewController ) target; }
}

public override void OnInspectorGUI()
{
GUILayout.BeginHorizontal();

if ( GUILayout.Button( "Generate ID", new GUIStyle( GUI.skin.button )
{normal = {textColor = new Color( 0.85f, 0.92f, 0.73f )}, fixedHeight = 25f} ) )
{
if ( string.IsNullOrEmpty( m_viewController.Configuration.Identifier ) )
{
m_viewController.Configuration.Identifier =
m_viewController.GetType().Name.Replace( "ViewController", "" ).ToLower();
}
}

if ( GUILayout.Button( "Reset Bounds",
new GUIStyle( GUI.skin.button )
{normal = {textColor = new Color( 0.92f, 0.54f, 0.4f )}, fixedHeight = 25f} ) )
{
RectTransform transform = m_viewController.transform as RectTransform;
if ( transform )
{
transform.anchorMin = Vector2.zero;
transform.anchorMax = Vector2.one;
transform.offsetMin = Vector2.zero;
transform.offsetMax = Vector2.zero;
}
}

GUILayout.EndHorizontal();

base.OnInspectorGUI();
}
}
}

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -0,0 +1,243 @@
using System;
using System.Collections;
using JetBrains.Annotations;
using UnityEngine;

namespace UIKit
{
[Serializable]
public sealed class UIViewControllerConfiguration
{
/// <summary>
/// Unique ID
/// </summary>
public string Identifier = string.Empty;

/// <summary>
/// The view title for header to display
/// </summary>
public string Title = string.Empty;

/// <summary>
/// Whether or not to hide the top and bottom gradient overlay when presenting
/// </summary>
public bool HideNavigationOverlays;

/// <summary>
/// Whether or not to hide the bottom close button when presenting
/// </summary>
public bool HideNavigationCloseButton;

public bool HideNavigationTitle;

public bool DestroyOnClose = true;
}

[RequireComponent( typeof( CanvasGroup ) )]
public abstract class UIViewController : MonoBehaviour
{
#region Properties

[Header( "UIViewController" )]
public UIViewControllerConfiguration Configuration;

[SerializeField, Range( 0.1f, 1f )]
private float m_fadeDuration = 0.2f;

/// <summary>
/// Indicator for whether or not currently fading
/// </summary>
private bool m_isFading;

private CanvasGroup m_canvasGroup;

#endregion

#region Public Methods

public virtual void ViewWillDisappear()
{
}

/// <summary>
/// Present another view controller
/// </summary>
/// <param name="viewController"></param>
/// <param name="duration"></param>
/// <param name="onComplete"></param>
/// <exception cref="ArgumentNullException"></exception>
public virtual void Present( [NotNull] UIViewController viewController,
float? duration = null, Action onComplete = null )
{
}

/// <summary>
/// Dismiss self
/// </summary>
/// <param name="duration"></param>
/// <param name="onComplete"></param>
public virtual void Dismiss( float? duration = null, Action onComplete = null )
{
}

/// <summary>
/// Fade the view in
/// </summary>
/// <param name="duration"></param>
/// <param name="onComplete">callback handler</param>
public void FadeIn( float? duration, Action onComplete = null )
{
if ( m_canvasGroup != null )
{
m_canvasGroup.alpha = 0f;
}

FadeTo( 1f, duration ?? m_fadeDuration, onComplete );
}

/// <summary>
/// Fade the view out
/// </summary>
/// <param name="duration"></param>
/// <param name="onComplete">callback handler</param>
public void FadeOut( float? duration, Action onComplete = null )
{
if ( m_canvasGroup != null )
{
m_canvasGroup.alpha = 1f;
}

FadeTo( 0f, duration ?? m_fadeDuration, onComplete );
}

/// <summary>
/// Fade alpha to a certain value
/// </summary>
/// <param name="opacity"></param>
/// <param name="duration"></param>
/// <param name="onComplete"></param>
public void FadeTo( float opacity, float duration, Action onComplete = null )
{
if ( m_isFading || !gameObject.activeInHierarchy ) return;

StartCoroutine( FadeOpacityTo( opacity, duration, onComplete ) );
}

#endregion

#region MonoBehaviour Methods

protected void Awake()
{
GetCanvasGroup();
FindNavigationController();

ViewWillLoad();
}

protected void Start()
{
ViewDidLoad();
}

protected void OnEnable()
{
ViewIsEnabled();
}

protected void OnDisable()
{
ViewIsDisabled();
}

protected void OnDestroy()
{
ViewWillUnload();
}

#endregion

#region Protected Methods

/// <summary>
/// Will be called on Awake
/// </summary>
protected virtual void ViewWillLoad()
{
}

protected virtual void ViewDidLoad()
{
}

/// <summary>
/// Will be called on OnEnable
/// </summary>
protected virtual void ViewIsEnabled()
{
}

/// <summary>
/// Will be called on OnDisable
/// </summary>
protected virtual void ViewIsDisabled()
{
}

/// <summary>
/// Will be called on OnDestroy
/// </summary>
protected virtual void ViewWillUnload()
{
}

#endregion

#region Private Methods

/// <summary>
/// Create a canvas group if there is none
/// </summary>
private void GetCanvasGroup()
{
m_canvasGroup = GetComponent<CanvasGroup>();
}

private void FindNavigationController()
{
}

/// <summary>
/// Coroutine: Fade alpha to a certain value
/// </summary>
/// <param name="value"></param>
/// <param name="duration"></param>
/// <param name="onComplete"></param>
/// <returns></returns>
private IEnumerator FadeOpacityTo( float value, float duration, [CanBeNull] Action onComplete )
{
m_isFading = true;

float t = 0f;
float alpha = m_canvasGroup.alpha;
float startAlpha = alpha;
float deltaAlpha = value - alpha;

while ( t <= 1 )
{
t = Time.deltaTime / duration;
m_canvasGroup.alpha = startAlpha + t * deltaAlpha;

yield return null;
}

m_canvasGroup.alpha = value;

m_isFading = false;

if ( onComplete != null ) onComplete();
}

#endregion
}
}

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

0 comments on commit fefbf6b

Please sign in to comment.
You can’t perform that action at this time.