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
+114 -121
View File
@@ -1,6 +1,4 @@
using System; using System;
using TMPro;
using UnityEditor;
using UnityEngine; using UnityEngine;
using UnityEngine.UI; using UnityEngine.UI;
@@ -21,11 +19,11 @@ public class UIController : MonoBehaviour {
// Adjustment Panel Elements // Adjustment Panel Elements
public RectTransform adjPanel; public RectTransform adjPanel;
// Settings Panel Elements // Settings Panel Elements
public RectTransform settingsPanel; public RectTransform settingsPanel;
public Button settingsCloseButton; public Button settingsCloseButton;
// Element Groups // Element Groups
private GameObject[] _titleElements; private GameObject[] _titleElements;
@@ -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,146 +61,133 @@ 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);
_aboutButtonDiff = new Vector3(75 * _scaleFactor, 0, 0); _aboutButtonDiff = new Vector3(75 * _scaleFactor, 0, 0);
_titleDiff = new Vector3(0, 450 * _scaleFactor, 0); _titleDiff = new Vector3(0, 450 * _scaleFactor, 0);
_adjustmentsDiff = new Vector3(adjPanel.rect.size.x * _scaleFactor, 0, 0); _adjustmentsDiff = new Vector3(adjPanel.rect.size.x * _scaleFactor, 0, 0);
_settingsDiff = new Vector3(_canvasRect.size.x * _scaleFactor * -1,0, 0); _settingsDiff = new Vector3(_canvasRect.size.x * _scaleFactor * -1, 0, 0);
// 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
_titleElements = new[] {titleText.gameObject, startButton.gameObject, settingsButton.gameObject}; _titleElements = new[] {titleText.gameObject, startButton.gameObject, settingsButton.gameObject};
} }
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);
switch (_currentUI) {
case UIStance.About:
OnAboutCloseButton();
break;
case UIStance.Play:
OnPlayCloseButton();
break;
case UIStance.Settings:
OnSettingsCloseButton();
break;
case UIStance.Title:
break;
default:
throw new ArgumentOutOfRangeException();
}
}
} }
// Move Title Elements In/Out private void ChangeStance(UIStance stance) {
private void MoveTitleElements(bool away) { // Quit early if UI is currently "locked" due to an active tween
// Main Title and Start/Settings Buttons if (_UILock)
foreach (GameObject element in _titleElements) { return;
LeanTween
.move(element, element.transform.position + _titleDiff * (away ? 1 : -1), away ? 0.65f : 1.15f) Debug.Log($"UI Transition: {_currentUI} -> {stance}");
.setDelay(away ? 0f : 0.35f)
.setEase(away ? LeanTweenType.easeInCubic : LeanTweenType.easeOutCubic); switch (_currentUI) {
// 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();
} }
// Bottom Right About Button _currentUI = stance;
LeanTween
.move(aboutButton.gameObject, aboutButton.transform.position + _aboutButtonDiff * (away ? 1 : -1), 0.55f)
.setEase(LeanTweenType.easeInOutCubic);
} }
private void MoveAdjustmentElements(bool away) { private void MoveElements(UIGroup group, bool away) {
LeanTween switch (group) {
.move(adjPanel.gameObject, adjPanel.gameObject.transform.position + _adjustmentsDiff * (away ? 1 : -1), 1.15f) case UIGroup.TitleScreen:
.setDelay(away ? 0f : 0.15f) // Main Title and Start/Settings Buttons
.setEase(LeanTweenType.easeInOutCubic); foreach (GameObject element in _titleElements) {
} LeanTween
.move(element, element.transform.position + _titleDiff * (away ? 1 : -1), away ? 0.65f : 1.15f)
.setDelay(away ? 0f : 0.35f)
.setEase(away ? LeanTweenType.easeInCubic : LeanTweenType.easeOutCubic);
}
// Move About Elements In/Out // Bottom Right About Button
private void MoveAboutElements(bool away) { LeanTween
LeanTween .move(aboutButton.gameObject, aboutButton.transform.position + _aboutButtonDiff * (away ? 1 : -1),
.move(aboutPanel.gameObject, aboutPanel.transform.position + _aboutDiff * (away ? 1 : -1), away ? 0.6f : 1f) 0.55f)
.setDelay(away ? 0f : 0.40f) .setEase(LeanTweenType.easeInOutCubic);
.setEase(away ? LeanTweenType.easeInCubic : LeanTweenType.easeOutCubic); break;
} case UIGroup.AdjustmentsScreen:
GameObject adjPanelGo;
private void MoveSettingsElements(bool away) { LeanTween
LeanTween .move((adjPanelGo = adjPanel.gameObject),
.move(settingsPanel.gameObject, settingsPanel.transform.position + _settingsDiff * (away ? 1 : -1), adjPanelGo.transform.position + _adjustmentsDiff * (away ? 1 : -1), 1.15f)
away ? 0.7f : 1.1f) .setDelay(away ? 0f : 0.15f)
.setDelay(away ? 0.05f : 0.15f) .setEase(LeanTweenType.easeInOutCubic);
.setEase(away ? LeanTweenType.easeInCubic : LeanTweenType.easeOutCubic); break;
} case UIGroup.AboutScreen:
LeanTween
// Handle switching to the Play Screen .move(aboutPanel.gameObject, aboutPanel.transform.position + _aboutDiff * (away ? 1 : -1),
private void OnStartButton() { away ? 0.6f : 1f)
if (_currentUI == UIStance.Title) { .setDelay(away ? 0f : 0.40f)
Debug.Log($"UI Transition: {_currentUI} -> {UIStance.About}"); .setEase(away ? LeanTweenType.easeInCubic : LeanTweenType.easeOutCubic);
MoveTitleElements(true); break;
MoveAdjustmentElements(false); case UIGroup.SettingsScreen:
_currentUI = UIStance.Play; LeanTween
} .move(settingsPanel.gameObject, settingsPanel.transform.position + _settingsDiff * (away ? 1 : -1),
} away ? 0.7f : 1.1f)
.setDelay(away ? 0.05f : 0.15f)
private void OnSettingsButton() { .setEase(away ? LeanTweenType.easeInCubic : LeanTweenType.easeOutCubic);
if (_currentUI == UIStance.Title) { break;
Debug.Log($"UI Transition: {_currentUI} -> {UIStance.Settings}"); default:
MoveTitleElements(true); throw new ArgumentOutOfRangeException(nameof(@group), @group, null);
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;
} }
} }
} }