From d9209ebc2f7f6f2b3842f281a9140f7c0fa0c862 Mon Sep 17 00:00:00 2001 From: Christopher Date: Fri, 9 Sep 2016 15:05:49 +0100 Subject: [PATCH] Refactored MenuDialog to use IMenuDialog interface --- .../Fungus/Narrative/Scripts/Commands/Menu.cs | 4 +- .../Narrative/Scripts/Commands/MenuTimer.cs | 2 +- .../Fungus/Narrative/Scripts/DialogInput.cs | 2 +- .../Fungus/Narrative/Scripts/IMenuDialog.cs | 43 ++++++ .../Narrative/Scripts/IMenuDialog.cs.meta | 12 ++ Assets/Fungus/Narrative/Scripts/MenuDialog.cs | 131 ++++++++++-------- 6 files changed, 130 insertions(+), 64 deletions(-) create mode 100644 Assets/Fungus/Narrative/Scripts/IMenuDialog.cs create mode 100644 Assets/Fungus/Narrative/Scripts/IMenuDialog.cs.meta diff --git a/Assets/Fungus/Narrative/Scripts/Commands/Menu.cs b/Assets/Fungus/Narrative/Scripts/Commands/Menu.cs index c26d1bd2..09340e28 100644 --- a/Assets/Fungus/Narrative/Scripts/Commands/Menu.cs +++ b/Assets/Fungus/Narrative/Scripts/Commands/Menu.cs @@ -47,10 +47,10 @@ namespace Fungus if (!hideOption) { - MenuDialog menuDialog = MenuDialog.GetMenuDialog(); + IMenuDialog menuDialog = MenuDialog.GetMenuDialog(); if (menuDialog != null) { - menuDialog.gameObject.SetActive(true); + menuDialog.SetActive(true); Flowchart flowchart = GetFlowchart(); string displayText = flowchart.SubstituteVariables(text); diff --git a/Assets/Fungus/Narrative/Scripts/Commands/MenuTimer.cs b/Assets/Fungus/Narrative/Scripts/Commands/MenuTimer.cs index 3d2ddce5..4e17ed81 100644 --- a/Assets/Fungus/Narrative/Scripts/Commands/MenuTimer.cs +++ b/Assets/Fungus/Narrative/Scripts/Commands/MenuTimer.cs @@ -26,7 +26,7 @@ namespace Fungus public override void OnEnter() { - MenuDialog menuDialog = MenuDialog.GetMenuDialog(); + IMenuDialog menuDialog = MenuDialog.GetMenuDialog(); if (menuDialog != null && targetBlock != null) diff --git a/Assets/Fungus/Narrative/Scripts/DialogInput.cs b/Assets/Fungus/Narrative/Scripts/DialogInput.cs index 265cb441..d5094b70 100644 --- a/Assets/Fungus/Narrative/Scripts/DialogInput.cs +++ b/Assets/Fungus/Narrative/Scripts/DialogInput.cs @@ -140,7 +140,7 @@ namespace Fungus { // Ignore input events if a Menu is being displayed if (MenuDialog.activeMenuDialog != null && - MenuDialog.activeMenuDialog.gameObject.activeInHierarchy && + MenuDialog.activeMenuDialog.IsActive() && MenuDialog.activeMenuDialog.DisplayedOptionsCount > 0) { dialogClickedFlag = false; diff --git a/Assets/Fungus/Narrative/Scripts/IMenuDialog.cs b/Assets/Fungus/Narrative/Scripts/IMenuDialog.cs new file mode 100644 index 00000000..f3cadeff --- /dev/null +++ b/Assets/Fungus/Narrative/Scripts/IMenuDialog.cs @@ -0,0 +1,43 @@ +using UnityEngine; +using System.Collections; + +namespace Fungus +{ + /// + /// Presents multiple choice buttons to the players. + /// + public interface IMenuDialog + { + /// + /// Sets the active state of the Menu Dialog gameobject. + /// + void SetActive(bool state); + + /// + /// Adds the option to the list of displayed options. + /// Will cause the Menu dialog to become visible if it is not already visible. + /// + /// true, if the option was added successfully. + /// The option text to display on the button. + /// If false, the option is displayed but is not selectable. + /// Block to execute when the option is selected. + bool AddOption(string text, bool interactable, Block targetBlock); + + /// + /// Show a timer during which the player can select an option. + /// + /// The duration during which the player can select an option. + /// Block to execute if the player does not select an option in time. + void ShowTimer(float duration, Block targetBlock); + + /// + /// Returns true if the Menu Dialog is currently displayed. + /// + bool IsActive(); + + /// + /// Returns the number of currently displayed options. + /// + int DisplayedOptionsCount { get; } + } +} \ No newline at end of file diff --git a/Assets/Fungus/Narrative/Scripts/IMenuDialog.cs.meta b/Assets/Fungus/Narrative/Scripts/IMenuDialog.cs.meta new file mode 100644 index 00000000..56a427eb --- /dev/null +++ b/Assets/Fungus/Narrative/Scripts/IMenuDialog.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 88b3741e7161f40d3b6166170b69c55e +timeCreated: 1473425656 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Fungus/Narrative/Scripts/MenuDialog.cs b/Assets/Fungus/Narrative/Scripts/MenuDialog.cs index 853acde6..0af34784 100644 --- a/Assets/Fungus/Narrative/Scripts/MenuDialog.cs +++ b/Assets/Fungus/Narrative/Scripts/MenuDialog.cs @@ -9,13 +9,10 @@ using System.Linq; namespace Fungus { - /// - /// Presents multiple choice buttons to the players. - /// - public class MenuDialog : MonoBehaviour + public class MenuDialog : MonoBehaviour, IMenuDialog { // Currently active Menu Dialog used to display Menu options - public static MenuDialog activeMenuDialog; + public static IMenuDialog activeMenuDialog; [Tooltip("Automatically select the first interactable button when the menu is shown.")] [SerializeField] protected bool autoSelectFirstButton = false; @@ -26,12 +23,12 @@ namespace Fungus protected Slider cachedSlider; public virtual Slider CachedSlider { get { return cachedSlider; } } - public static MenuDialog GetMenuDialog() + public static IMenuDialog GetMenuDialog() { if (activeMenuDialog == null) { // Use first Menu Dialog found in the scene (if any) - MenuDialog md = GameObject.FindObjectOfType(); + IMenuDialog md = GameObject.FindObjectOfType(); if (md != null) { activeMenuDialog = md; @@ -46,11 +43,11 @@ namespace Fungus GameObject go = Instantiate(prefab) as GameObject; go.SetActive(false); go.name = "MenuDialog"; - activeMenuDialog = go.GetComponent(); + activeMenuDialog = go.GetComponent(); } } } - + return activeMenuDialog; } @@ -101,6 +98,52 @@ namespace Fungus } } + public virtual void HideSayDialog() + { + ISayDialog sayDialog = SayDialog.GetSayDialog(); + if (sayDialog != null) + { + sayDialog.FadeWhenDone = true; + } + } + + protected virtual IEnumerator WaitForTimeout(float timeoutDuration, Block targetBlock) + { + float elapsedTime = 0; + + Slider timeoutSlider = GetComponentInChildren(); + + while (elapsedTime < timeoutDuration) + { + if (timeoutSlider != null) + { + float t = 1f - elapsedTime / timeoutDuration; + timeoutSlider.value = t; + } + + elapsedTime += Time.deltaTime; + + yield return null; + } + + Clear(); + gameObject.SetActive(false); + + HideSayDialog(); + + if (targetBlock != null) + { + targetBlock.StartExecution(); + } + } + + #region IMenuDialog implementation + + public virtual void SetActive(bool state) + { + gameObject.SetActive(state); + } + public virtual bool AddOption(string text, bool interactable, Block targetBlock) { bool addedOption = false; @@ -122,9 +165,9 @@ namespace Fungus { textComponent.text = text; } - + Block block = targetBlock; - + button.onClick.AddListener(delegate { EventSystem.current.SetSelectedGameObject(null); @@ -152,32 +195,8 @@ namespace Fungus break; } } - - return addedOption; - } - - public virtual int DisplayedOptionsCount - { - get { - int count = 0; - foreach (Button button in cachedButtons) - { - if (button.gameObject.activeSelf) - { - count++; - } - } - return count; - } - } - public virtual void HideSayDialog() - { - ISayDialog sayDialog = SayDialog.GetSayDialog(); - if (sayDialog != null) - { - sayDialog.FadeWhenDone = true; - } + return addedOption; } public virtual void ShowTimer(float duration, Block targetBlock) @@ -191,34 +210,26 @@ namespace Fungus } } - protected virtual IEnumerator WaitForTimeout(float timeoutDuration, Block targetBlock) + public virtual bool IsActive() { - float elapsedTime = 0; - - Slider timeoutSlider = GetComponentInChildren(); - - while (elapsedTime < timeoutDuration) - { - if (timeoutSlider != null) + return gameObject.activeInHierarchy; + } + + public virtual int DisplayedOptionsCount + { + get { + int count = 0; + foreach (Button button in cachedButtons) { - float t = 1f - elapsedTime / timeoutDuration; - timeoutSlider.value = t; + if (button.gameObject.activeSelf) + { + count++; + } } - - elapsedTime += Time.deltaTime; - - yield return null; - } - - Clear(); - gameObject.SetActive(false); - - HideSayDialog(); - - if (targetBlock != null) - { - targetBlock.StartExecution(); + return count; } } + + #endregion } }