add documentation to large methods in UIController.cs, refactor ChangeStance with smarter, simpler if-statements

This commit is contained in:
Xevion
2020-05-22 23:13:47 -05:00
parent 69f9d9f3d8
commit b2801d8009

View File

@@ -1,4 +1,5 @@
using System; using System;
using System.Net;
using JetBrains.Annotations; using JetBrains.Annotations;
using UnityEngine; using UnityEngine;
using UnityEngine.UI; using UnityEngine.UI;
@@ -33,10 +34,8 @@ public class UIController : MonoBehaviour {
private Rect _canvasRect; private Rect _canvasRect;
private float _scaleFactor; private float _scaleFactor;
private int _activeTweens; private int _activeTweens;
private bool _UILock {
get => _activeTweens > 0; private bool _UILock => _activeTweens > 0;
set { }
}
// Element Displacements // Element Displacements
private Vector3 _aboutDiff; private Vector3 _aboutDiff;
@@ -92,26 +91,42 @@ public class UIController : MonoBehaviour {
} }
private void Update() { private void Update() {
// Exit to the title screen with Escape key // on Escape key, attempts to change UI stance to the Title Screen
if (Input.GetKeyDown(KeyCode.Escape)) if (Input.GetKeyDown(KeyCode.Escape))
ChangeStance(UIStance.Title); ChangeStance(UIStance.Title);
} }
/// <summary>
/// Signals to the application that a Tween has ended.
/// This will unlock UI stance changes (if it was the last Tween within the stack).
/// </summary>
/// <seealso cref="StartTween"/>
private void TweenEnd() { private void TweenEnd() {
_activeTweens -= 1; _activeTweens -= 1;
if(!_UILock) if (!_UILock)
Debug.Log("Unlocking Stance Changes"); Debug.Log("Unlocking Stance Changes");
} }
/// <summary>
/// Signals to the application that a Tween is in progress.
/// This locks UI stance changes until all Tweens have completed.
/// This method returns the <c>TweenEnd</c> callback, which must be used in conjunction with <c>StartTween</c>.
/// </summary>
/// <returns>returns the <see cref="TweenEnd"/> action for a deconstructing callback</returns>
/// <seealso cref="TweenEnd"/>
private Action StartTween() { private Action StartTween() {
if(!_UILock) if (!_UILock)
Debug.Log("Locking Stance Changes"); Debug.Log("Locking Stance Changes");
_activeTweens += 1; _activeTweens += 1;
return TweenEnd; return TweenEnd;
} }
/// <summary>
/// Switches the 'Stance' a UI is in by moving groups of elements around.
/// </summary>
/// <param name="stance">The stance that the UI should switch to next.</param>
/// <exception cref="ArgumentOutOfRangeException"></exception>
private void ChangeStance(UIStance stance) { private void ChangeStance(UIStance stance) {
// Quit early if UI is currently "locked" due to an active tween // Quit early if UI is currently "locked" due to an active tween
if (_UILock || stance == _currentUI) if (_UILock || stance == _currentUI)
@@ -119,54 +134,40 @@ public class UIController : MonoBehaviour {
Debug.Log($"UI Transition: {_currentUI} -> {stance}"); Debug.Log($"UI Transition: {_currentUI} -> {stance}");
switch (_currentUI) { // Title -> Settings/About/Play
// Title -> Settings/About/Play if (_currentUI == UIStance.Title) {
case UIStance.Title: MoveElements(UIGroup.TitleScreen, true);
if (stance == UIStance.Settings) { if (stance == UIStance.Settings)
MoveElements(UIGroup.TitleScreen, true); MoveElements(UIGroup.SettingsScreen, false);
MoveElements(UIGroup.SettingsScreen, false); else if (stance == UIStance.About)
} MoveElements(UIGroup.AboutScreen, false);
else if (stance == UIStance.About) { else if (stance == UIStance.Play)
MoveElements(UIGroup.TitleScreen, true); MoveElements(UIGroup.AdjustmentsScreen, false);
MoveElements(UIGroup.AboutScreen, false); }
} // Settings/About/Play -> Title
else if (stance == UIStance.Play) { else if (stance == UIStance.Title) {
MoveElements(UIGroup.TitleScreen, true); MoveElements(UIGroup.TitleScreen, false);
MoveElements(UIGroup.AdjustmentsScreen, false); if (_currentUI == UIStance.Play)
} MoveElements(UIGroup.AdjustmentsScreen, true);
else if (_currentUI == UIStance.Settings)
break; MoveElements(UIGroup.SettingsScreen, true);
// Play -> Title else if (_currentUI == UIStance.About)
case UIStance.Play: MoveElements(UIGroup.AboutScreen, true);
if (stance == UIStance.Title) {
MoveElements(UIGroup.TitleScreen, false);
MoveElements(UIGroup.AdjustmentsScreen, true);
}
break;
// Settings -> Title
case UIStance.Settings:
if (stance == UIStance.Title) {
MoveElements(UIGroup.TitleScreen, false);
MoveElements(UIGroup.SettingsScreen, true);
}
break;
// About -> Title
case UIStance.About:
if (stance == UIStance.Title) {
MoveElements(UIGroup.TitleScreen, false);
MoveElements(UIGroup.AboutScreen, true);
}
break;
default:
throw new ArgumentOutOfRangeException();
} }
_currentUI = stance; _currentUI = stance;
} }
/// <summary>
/// Moves groups of elements back and forth using the LeanTween framework.
/// <c>MoveElements</c> is not aware of the current position of the elements at any time,
/// thus measures must be implemented to track the current 'state' of any elements.
///
/// Used in conjunction with <see cref="ChangeStance"/> to manipulate the UI elements.
/// </summary>
/// <param name="group">the <c>UIGroup</c> associated with the elements you wish to move</param>
/// <param name="away">moves elements away (<c></c>)</param>
/// <exception cref="ArgumentOutOfRangeException"></exception>
private void MoveElements(UIGroup group, bool away) { private void MoveElements(UIGroup group, bool away) {
switch (group) { switch (group) {
case UIGroup.TitleScreen: case UIGroup.TitleScreen: