completely overhaul button system with new ChangeStance and MoveElements method, lambdas for button listener activating stance changes

This commit is contained in:
Xevion
2020-05-22 20:19:06 -05:00
parent 5a265cdc75
commit 75f11766a4
+86 -93
View File
@@ -1,6 +1,4 @@
using System; using System;
using TMPro;
using UnityEditor;
using UnityEngine; using UnityEngine;
using UnityEngine.UI; using UnityEngine.UI;
@@ -33,6 +31,7 @@ public class UIController : MonoBehaviour {
private CanvasScaler _scaler; private CanvasScaler _scaler;
private Rect _canvasRect; private Rect _canvasRect;
private float _scaleFactor; private float _scaleFactor;
private bool _UILock;
// Element Displacements // Element Displacements
private Vector3 _aboutDiff; private Vector3 _aboutDiff;
@@ -48,6 +47,13 @@ public class UIController : MonoBehaviour {
About About
} }
private enum UIGroup {
TitleScreen,
AdjustmentsScreen,
SettingsScreen,
AboutScreen
}
private void Start() { private void Start() {
// Basic variable setup // Basic variable setup
_currentUI = UIStance.Title; _currentUI = UIStance.Title;
@@ -55,12 +61,12 @@ public class UIController : MonoBehaviour {
_canvasRect = canvas.GetComponent<RectTransform>().rect; _canvasRect = canvas.GetComponent<RectTransform>().rect;
_scaleFactor = Screen.width / _scaler.referenceResolution.x; _scaleFactor = Screen.width / _scaler.referenceResolution.x;
// Add event functions // Add stance change functionality to buttons
startButton.onClick.AddListener(OnStartButton); startButton.onClick.AddListener(() => ChangeStance(UIStance.Play));
aboutButton.onClick.AddListener(OnAboutButton); aboutButton.onClick.AddListener(() => ChangeStance(UIStance.About));
aboutCloseButton.onClick.AddListener(OnAboutCloseButton); aboutCloseButton.onClick.AddListener(() => ChangeStance(UIStance.Title));
settingsButton.onClick.AddListener(OnSettingsButton); settingsButton.onClick.AddListener(() => ChangeStance(UIStance.Settings));
settingsCloseButton.onClick.AddListener(OnSettingsCloseButton); settingsCloseButton.onClick.AddListener(() => ChangeStance(UIStance.Title));
// Calculate UI position deltas // Calculate UI position deltas
_aboutDiff = new Vector3(_canvasRect.size.x * _scaleFactor, 0, 0); _aboutDiff = new Vector3(_canvasRect.size.x * _scaleFactor, 0, 0);
@@ -72,7 +78,8 @@ public class UIController : MonoBehaviour {
// Move UI elements into position // Move UI elements into position
Vector3 center = canvas.transform.position; Vector3 center = canvas.transform.position;
aboutPanel.transform.position = center + _aboutDiff; aboutPanel.transform.position = center + _aboutDiff;
adjPanel.transform.position = center + new Vector3((_canvasRect.size.x + 10) / 2f * _scaleFactor, 0, 0) + _adjustmentsDiff / 2f; adjPanel.transform.position = center + new Vector3((_canvasRect.size.x + 10) / 2f * _scaleFactor, 0, 0) +
_adjustmentsDiff / 2f;
settingsPanel.transform.position = center + _settingsDiff; settingsPanel.transform.position = center + _settingsDiff;
// Group Element Instantiation // Group Element Instantiation
@@ -81,28 +88,68 @@ public class UIController : MonoBehaviour {
private void Update() { private void Update() {
// Exit to the title screen with Escape key // Exit to the title screen with Escape key
if (Input.GetKeyDown(KeyCode.Escape)) { if (Input.GetKeyDown(KeyCode.Escape))
Debug.Log($"Escape key pressed"); ChangeStance(UIStance.Title);
}
private void ChangeStance(UIStance stance) {
// Quit early if UI is currently "locked" due to an active tween
if (_UILock)
return;
Debug.Log($"UI Transition: {_currentUI} -> {stance}");
switch (_currentUI) { switch (_currentUI) {
case UIStance.About: // Title -> Settings/About/Play
OnAboutCloseButton();
break;
case UIStance.Play:
OnPlayCloseButton();
break;
case UIStance.Settings:
OnSettingsCloseButton();
break;
case UIStance.Title: case UIStance.Title:
if (stance == UIStance.Settings) {
MoveElements(UIGroup.TitleScreen, true);
MoveElements(UIGroup.SettingsScreen, false);
}
else if (stance == UIStance.About) {
MoveElements(UIGroup.TitleScreen, true);
MoveElements(UIGroup.AboutScreen, false);
}
else if (stance == UIStance.Play) {
MoveElements(UIGroup.TitleScreen, true);
MoveElements(UIGroup.AdjustmentsScreen, false);
}
break;
// Play -> Title
case UIStance.Play:
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; break;
default: default:
throw new ArgumentOutOfRangeException(); throw new ArgumentOutOfRangeException();
} }
}
_currentUI = stance;
} }
// Move Title Elements In/Out private void MoveElements(UIGroup group, bool away) {
private void MoveTitleElements(bool away) { switch (group) {
case UIGroup.TitleScreen:
// Main Title and Start/Settings Buttons // Main Title and Start/Settings Buttons
foreach (GameObject element in _titleElements) { foreach (GameObject element in _titleElements) {
LeanTween LeanTween
@@ -113,88 +160,34 @@ public class UIController : MonoBehaviour {
// Bottom Right About Button // Bottom Right About Button
LeanTween LeanTween
.move(aboutButton.gameObject, aboutButton.transform.position + _aboutButtonDiff * (away ? 1 : -1), 0.55f) .move(aboutButton.gameObject, aboutButton.transform.position + _aboutButtonDiff * (away ? 1 : -1),
0.55f)
.setEase(LeanTweenType.easeInOutCubic); .setEase(LeanTweenType.easeInOutCubic);
} break;
case UIGroup.AdjustmentsScreen:
private void MoveAdjustmentElements(bool away) { GameObject adjPanelGo;
LeanTween LeanTween
.move(adjPanel.gameObject, adjPanel.gameObject.transform.position + _adjustmentsDiff * (away ? 1 : -1), 1.15f) .move((adjPanelGo = adjPanel.gameObject),
adjPanelGo.transform.position + _adjustmentsDiff * (away ? 1 : -1), 1.15f)
.setDelay(away ? 0f : 0.15f) .setDelay(away ? 0f : 0.15f)
.setEase(LeanTweenType.easeInOutCubic); .setEase(LeanTweenType.easeInOutCubic);
} break;
case UIGroup.AboutScreen:
// Move About Elements In/Out
private void MoveAboutElements(bool away) {
LeanTween LeanTween
.move(aboutPanel.gameObject, aboutPanel.transform.position + _aboutDiff * (away ? 1 : -1), away ? 0.6f : 1f) .move(aboutPanel.gameObject, aboutPanel.transform.position + _aboutDiff * (away ? 1 : -1),
away ? 0.6f : 1f)
.setDelay(away ? 0f : 0.40f) .setDelay(away ? 0f : 0.40f)
.setEase(away ? LeanTweenType.easeInCubic : LeanTweenType.easeOutCubic); .setEase(away ? LeanTweenType.easeInCubic : LeanTweenType.easeOutCubic);
} break;
case UIGroup.SettingsScreen:
private void MoveSettingsElements(bool away) {
LeanTween LeanTween
.move(settingsPanel.gameObject, settingsPanel.transform.position + _settingsDiff * (away ? 1 : -1), .move(settingsPanel.gameObject, settingsPanel.transform.position + _settingsDiff * (away ? 1 : -1),
away ? 0.7f : 1.1f) away ? 0.7f : 1.1f)
.setDelay(away ? 0.05f : 0.15f) .setDelay(away ? 0.05f : 0.15f)
.setEase(away ? LeanTweenType.easeInCubic : LeanTweenType.easeOutCubic); .setEase(away ? LeanTweenType.easeInCubic : LeanTweenType.easeOutCubic);
} break;
default:
// Handle switching to the Play Screen throw new ArgumentOutOfRangeException(nameof(@group), @group, null);
private void OnStartButton() {
if (_currentUI == UIStance.Title) {
Debug.Log($"UI Transition: {_currentUI} -> {UIStance.About}");
MoveTitleElements(true);
MoveAdjustmentElements(false);
_currentUI = UIStance.Play;
}
}
private void OnSettingsButton() {
if (_currentUI == UIStance.Title) {
Debug.Log($"UI Transition: {_currentUI} -> {UIStance.Settings}");
MoveTitleElements(true);
MoveSettingsElements(false);
_currentUI = UIStance.Settings;
}
}
private void OnSettingsCloseButton() {
if (_currentUI == UIStance.Settings) {
Debug.Log($"UI Transition: {_currentUI} - > {UIStance.Title}");
MoveTitleElements(false);
MoveSettingsElements(true);
_currentUI = UIStance.Title;
}
}
// Handle switching to the About Screen
private void OnAboutButton() {
if (_currentUI == UIStance.Title) {
Debug.Log($"Screen Transition: {_currentUI} -> {UIStance.About}");
MoveAboutElements(false);
MoveTitleElements(true);
_currentUI = UIStance.About;
}
}
// Handle returning to the Title Screen, closing the About Screen
private void OnAboutCloseButton() {
if (_currentUI == UIStance.About) {
Debug.Log($"Screen Transition: {_currentUI} -> {UIStance.Title}");
MoveAboutElements(true);
MoveTitleElements(false);
_currentUI = UIStance.Title;
}
}
// Handle returning to the Title Screen, closing the Play Screen
private void OnPlayCloseButton() {
if (_currentUI == UIStance.Play) {
Debug.Log($"Screen Transition: {_currentUI} -> {UIStance.Title}");
MoveAdjustmentElements(true);
MoveTitleElements(false);
_currentUI = UIStance.Title;
} }
} }
} }