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 TMPro;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
@@ -33,6 +31,7 @@ public class UIController : MonoBehaviour {
private CanvasScaler _scaler;
private Rect _canvasRect;
private float _scaleFactor;
private bool _UILock;
// Element Displacements
private Vector3 _aboutDiff;
@@ -48,6 +47,13 @@ public class UIController : MonoBehaviour {
About
}
private enum UIGroup {
TitleScreen,
AdjustmentsScreen,
SettingsScreen,
AboutScreen
}
private void Start() {
// Basic variable setup
_currentUI = UIStance.Title;
@@ -55,12 +61,12 @@ public class UIController : MonoBehaviour {
_canvasRect = canvas.GetComponent<RectTransform>().rect;
_scaleFactor = Screen.width / _scaler.referenceResolution.x;
// Add event functions
startButton.onClick.AddListener(OnStartButton);
aboutButton.onClick.AddListener(OnAboutButton);
aboutCloseButton.onClick.AddListener(OnAboutCloseButton);
settingsButton.onClick.AddListener(OnSettingsButton);
settingsCloseButton.onClick.AddListener(OnSettingsCloseButton);
// Add stance change functionality to buttons
startButton.onClick.AddListener(() => ChangeStance(UIStance.Play));
aboutButton.onClick.AddListener(() => ChangeStance(UIStance.About));
aboutCloseButton.onClick.AddListener(() => ChangeStance(UIStance.Title));
settingsButton.onClick.AddListener(() => ChangeStance(UIStance.Settings));
settingsCloseButton.onClick.AddListener(() => ChangeStance(UIStance.Title));
// Calculate UI position deltas
_aboutDiff = new Vector3(_canvasRect.size.x * _scaleFactor, 0, 0);
@@ -72,7 +78,8 @@ public class UIController : MonoBehaviour {
// Move UI elements into position
Vector3 center = canvas.transform.position;
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;
// Group Element Instantiation
@@ -81,28 +88,68 @@ public class UIController : MonoBehaviour {
private void Update() {
// Exit to the title screen with Escape key
if (Input.GetKeyDown(KeyCode.Escape)) {
Debug.Log($"Escape key pressed");
if (Input.GetKeyDown(KeyCode.Escape))
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) {
case UIStance.About:
OnAboutCloseButton();
break;
case UIStance.Play:
OnPlayCloseButton();
break;
case UIStance.Settings:
OnSettingsCloseButton();
break;
// Title -> Settings/About/Play
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;
default:
throw new ArgumentOutOfRangeException();
}
}
_currentUI = stance;
}
// Move Title Elements In/Out
private void MoveTitleElements(bool away) {
private void MoveElements(UIGroup group, bool away) {
switch (group) {
case UIGroup.TitleScreen:
// Main Title and Start/Settings Buttons
foreach (GameObject element in _titleElements) {
LeanTween
@@ -113,88 +160,34 @@ public class UIController : MonoBehaviour {
// Bottom Right About Button
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);
}
private void MoveAdjustmentElements(bool away) {
break;
case UIGroup.AdjustmentsScreen:
GameObject adjPanelGo;
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)
.setEase(LeanTweenType.easeInOutCubic);
}
// Move About Elements In/Out
private void MoveAboutElements(bool away) {
break;
case UIGroup.AboutScreen:
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)
.setEase(away ? LeanTweenType.easeInCubic : LeanTweenType.easeOutCubic);
}
private void MoveSettingsElements(bool away) {
break;
case UIGroup.SettingsScreen:
LeanTween
.move(settingsPanel.gameObject, settingsPanel.transform.position + _settingsDiff * (away ? 1 : -1),
away ? 0.7f : 1.1f)
.setDelay(away ? 0.05f : 0.15f)
.setEase(away ? LeanTweenType.easeInCubic : LeanTweenType.easeOutCubic);
}
// Handle switching to the Play Screen
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;
break;
default:
throw new ArgumentOutOfRangeException(nameof(@group), @group, null);
}
}
}