From 8f2ec196606a18bcedad7af9f85d31ac2b661fa8 Mon Sep 17 00:00:00 2001 From: originalnicodr Date: Sun, 4 Aug 2024 21:14:44 -0300 Subject: [PATCH] Refactored the animator player toggler into a play/pause button for better UI/UX. Also did some minor campaths panel UI improvements. --- src/Cinematic/Cells/AnimatorCell.cs | 16 +++--- src/UI/Panels/AnimatorPanel.cs | 86 ++++++++++++++++++++++++++--- src/UI/Panels/CamPaths.cs | 7 +-- 3 files changed, 90 insertions(+), 19 deletions(-) diff --git a/src/Cinematic/Cells/AnimatorCell.cs b/src/Cinematic/Cells/AnimatorCell.cs index d7d6252..ab30e0e 100644 --- a/src/Cinematic/Cells/AnimatorCell.cs +++ b/src/Cinematic/Cells/AnimatorCell.cs @@ -20,7 +20,7 @@ namespace UnityExplorer.UI.Panels public AnimatorPlayer animatorPlayer; public Toggle IgnoreMasterToggle; - public Toggle AnimatorToggle; + public AnimatorPausePlayButton animatorToggler; public Toggle MeshToggle; public ButtonRef inspectButton; public Dropdown animatorDropdown; @@ -42,7 +42,7 @@ namespace UnityExplorer.UI.Panels this.animatorPlayer = animatorPlayer; inspectButton.ButtonText.text = animatorPlayer.animator.name; IgnoreMasterToggle.isOn = animatorPlayer.shouldIgnoreMasterToggle; - AnimatorToggle.isOn = animatorPlayer.animator.speed != 0; + animatorToggler.isOn = animatorPlayer.animator.speed != 0; UpdateDropdownOptions(); } @@ -95,10 +95,8 @@ namespace UnityExplorer.UI.Panels UIFactory.SetLayoutElement(inspectButton.GameObject, minWidth: 200, minHeight: 25); inspectButton.OnClick += () => InspectorManager.Inspect(animatorPlayer.animator.gameObject); - GameObject AnimatorToggleObj = UIFactory.CreateToggle(UIRoot, "AnimatorToggle", out AnimatorToggle, out Text animatorToggleText); - UIFactory.SetLayoutElement(AnimatorToggleObj, minHeight: 30); - AnimatorToggle.isOn = animatorPlayer != null && animatorPlayer.animator.speed == 1; - AnimatorToggle.onValueChanged.AddListener(EnableAnimation); + animatorToggler = new AnimatorPausePlayButton(UIRoot, animatorPlayer != null && animatorPlayer.animator.speed == 1); + animatorToggler.OnClick += ButtonEnableAnimation; ButtonRef resetAnimation = UIFactory.CreateButton(UIRoot, "Reset Animation", "Reset"); UIFactory.SetLayoutElement(resetAnimation.GameObject, minWidth: 50, minHeight: 25); @@ -165,7 +163,7 @@ namespace UnityExplorer.UI.Panels animationTimeline.maxValue = 1; animationTimeline.onValueChanged.AddListener((float val) => { animatorPlayer.PlayOverridingAnimation(val); - AnimatorToggle.isOn = false; + animatorToggler.isOn = false; }); playButton = UIFactory.CreateButton(UIRoot, "PlayButton", "Play", new Color(0.2f, 0.26f, 0.2f)); @@ -188,6 +186,10 @@ namespace UnityExplorer.UI.Panels animatorPlayer.shouldIgnoreMasterToggle = value; } + internal void ButtonEnableAnimation(){ + EnableAnimation(animatorToggler.isOn); + } + internal void EnableAnimation(bool value){ if (animatorPlayer.animator.wrappedObject != null) animatorPlayer.animator.speed = value ? 1 : 0; diff --git a/src/UI/Panels/AnimatorPanel.cs b/src/UI/Panels/AnimatorPanel.cs index dc7b279..02e21d2 100644 --- a/src/UI/Panels/AnimatorPanel.cs +++ b/src/UI/Panels/AnimatorPanel.cs @@ -30,7 +30,7 @@ namespace UnityExplorer.UI.Panels public override bool NavButtonWanted => true; public override bool ShouldSaveActiveState => true; - Toggle masterAnimatorToggle; + AnimatorPausePlayButton masterAnimatorPlayer; Toggle masterMeshToggle; private static ScrollPool animatorScrollPool; @@ -52,7 +52,7 @@ namespace UnityExplorer.UI.Panels private void FindAllAnimators(){ // Enable all animators on refresh - masterAnimatorToggle.isOn = true; // Will also trigger "MasterToggleAnimators(true)" + masterAnimatorPlayer.isOn = true; // Will also trigger "MasterToggleAnimators(true)" Type searchType = ReflectionUtility.GetTypeByName("UnityEngine.Animator"); searchType = searchType is Type type ? type : searchType.GetActualType(); @@ -92,7 +92,9 @@ namespace UnityExplorer.UI.Panels } } - public void MasterToggleAnimators(bool enable){ + public void MasterToggleAnimators(){ + bool enable = masterAnimatorPlayer.isOn; + // Load animators for the first time if there are not any if (animators.Count == 0) FindAllAnimators(); @@ -120,7 +122,7 @@ namespace UnityExplorer.UI.Panels } public void HotkeyToggleAnimators(){ - masterAnimatorToggle.isOn = !masterAnimatorToggle.isOn; + masterAnimatorPlayer.isOn = !masterAnimatorPlayer.isOn; } // ~~~~~~~~ UI construction / callbacks ~~~~~~~~ @@ -139,10 +141,11 @@ namespace UnityExplorer.UI.Panels masterMeshToggle.onValueChanged.AddListener(value => MasterToggleMeshes(value)); masterMeshText.text = "Master Mesh Toggler"; - GameObject animatorObj = UIFactory.CreateToggle(firstGroup, "Master Animation Toggle", out masterAnimatorToggle, out Text masterAnimatorText); - UIFactory.SetLayoutElement(animatorObj, minHeight: 25); - masterAnimatorToggle.onValueChanged.AddListener(value => MasterToggleAnimators(value)); - masterAnimatorText.text = "Master Animator Toggler"; + masterAnimatorPlayer = new AnimatorPausePlayButton(firstGroup); + masterAnimatorPlayer.OnClick += MasterToggleAnimators; + + Text masterAnimatorPlayerText = UIFactory.CreateLabel(firstGroup, "MasterAnimatorToggleLabel", "Master Animator Toggler", TextAnchor.MiddleRight); + UIFactory.SetLayoutElement(masterAnimatorPlayerText.gameObject, flexibleWidth: 0, minHeight: 25); GameObject headerSpace2 = UIFactory.CreateUIObject("HeaderSpace2", firstGroup); UIFactory.SetLayoutElement(headerSpace2, minWidth: 10, flexibleWidth: 0); @@ -193,4 +196,71 @@ namespace UnityExplorer.UI.Panels public void OnCellBorrowed(AnimatorCell cell) { } } + + // ButtonRef wrapper to act as a toggle with clearer UI + public class AnimatorPausePlayButton + { + private ButtonRef innerButton; + public Button Component => innerButton.Component; + public GameObject GameObject => innerButton.GameObject; + private bool isPlaying; + + public AnimatorPausePlayButton(GameObject ui, bool state = true) + { + innerButton = UIFactory.CreateButton(ui, "InnerAnimatorPlayButton", ""); + UIFactory.SetLayoutElement(innerButton.GameObject, minHeight: 25, minWidth: 25); + innerButton.OnClick += SetToggleButtonState; + isPlaying = state; + UpdateButton(); + } + + void OnPlay(){ + innerButton.ButtonText.text = "❚❚"; + RuntimeHelper.SetColorBlockAuto(innerButton.Component, new(0.4f, 0.2f, 0.2f)); + } + + void OnPause(){ + innerButton.ButtonText.text = "►"; + RuntimeHelper.SetColorBlockAuto(innerButton.Component, new(0.2f, 0.4f, 0.2f)); + } + + void UpdateButton(){ + if (isPlaying) + { + OnPlay(); + } + else + { + OnPause(); + } + } + + void SetToggleButtonState() + { + isPlaying = !isPlaying; + UpdateButton(); + } + + public bool isOn + { + get { + return isPlaying; + } + set { + if (value != isPlaying){ + SetToggleButtonState(); + } + } + } + + public Action OnClick + { + get { + return innerButton.OnClick; + } + set { + innerButton.OnClick = value; + } + } + } } diff --git a/src/UI/Panels/CamPaths.cs b/src/UI/Panels/CamPaths.cs index 66974f5..5ff5c1a 100644 --- a/src/UI/Panels/CamPaths.cs +++ b/src/UI/Panels/CamPaths.cs @@ -107,14 +107,12 @@ namespace UnityExplorer.UI.Panels default, new Color(1, 1, 1, 0), TextAnchor.MiddleLeft); UIFactory.SetLayoutElement(horiGroup, minHeight: 25, flexibleWidth: 9999); - ButtonRef startButton = UIFactory.CreateButton(horiGroup, "Start", "▶"); + ButtonRef startButton = UIFactory.CreateButton(horiGroup, "Start", "►"); UIFactory.SetLayoutElement(startButton.GameObject, minWidth: 50, minHeight: 25); - startButton.ButtonText.fontSize = 20; startButton.OnClick += StartButton_OnClick; - ButtonRef pauseContinueButton = UIFactory.CreateButton(horiGroup, "Pause/Continue", "❚❚/▶"); + ButtonRef pauseContinueButton = UIFactory.CreateButton(horiGroup, "Pause/Continue", "❚❚/►"); UIFactory.SetLayoutElement(pauseContinueButton.GameObject, minWidth: 50, minHeight: 25); - pauseContinueButton.ButtonText.fontSize = 20; pauseContinueButton.OnClick += TogglePause_OnClick; ButtonRef stopButton = UIFactory.CreateButton(horiGroup, "Stop", "■"); @@ -124,6 +122,7 @@ namespace UnityExplorer.UI.Panels ButtonRef AddNode = UIFactory.CreateButton(horiGroup, "AddCamNode", "+"); UIFactory.SetLayoutElement(AddNode.GameObject, minWidth: 50, minHeight: 25); + AddNode.ButtonText.fontSize = 20; AddNode.OnClick += AddNode_OnClick; ButtonRef DeletePath = UIFactory.CreateButton(horiGroup, "DeletePath", "Clear");