From b6a1e2df57f2375d094fe79754843771044a6f6f Mon Sep 17 00:00:00 2001 From: Christopher Date: Wed, 14 Sep 2016 12:26:25 +0100 Subject: [PATCH] Merged MenuDialog extension methods with MenuDialog class. --- .../Scripts/Components/LuaExtensions.cs | 99 ------------------- .../Scripts/Components/LuaExtensions.cs.meta | 13 --- .../Fungus/Scripts/Components/MenuDialog.cs | 80 +++++++++++++++ .../Fungus/Scripts/Interfaces/IMenuDialog.cs | 21 +++- 4 files changed, 97 insertions(+), 116 deletions(-) delete mode 100644 Assets/Fungus/Scripts/Components/LuaExtensions.cs delete mode 100644 Assets/Fungus/Scripts/Components/LuaExtensions.cs.meta diff --git a/Assets/Fungus/Scripts/Components/LuaExtensions.cs b/Assets/Fungus/Scripts/Components/LuaExtensions.cs deleted file mode 100644 index 37034833..00000000 --- a/Assets/Fungus/Scripts/Components/LuaExtensions.cs +++ /dev/null @@ -1,99 +0,0 @@ -// This code is part of the Fungus library (http://fungusgames.com) maintained by Chris Gregan (http://twitter.com/gofungus). -// It is released for free under the MIT open source license (https://github.com/snozbot/fungus/blob/master/LICENSE) - -using UnityEngine; -using UnityEngine.UI; -using System.Collections; -using Fungus; -using MoonSharp.Interpreter; - -namespace Fungus -{ - public static class LuaExtensions - { - /// - /// Extension for MenuDialog that allows AddOption to call a Lua function when an option is selected. - /// - public static bool AddOption(this MenuDialog menuDialog, string text, bool interactable, ILuaEnvironment luaEnv, Closure callBack) - { - if (!menuDialog.gameObject.activeSelf) - { - menuDialog.gameObject.SetActive(true); - } - - bool addedOption = false; - foreach (Button button in menuDialog.CachedButtons) - { - if (!button.gameObject.activeSelf) - { - button.gameObject.SetActive(true); - - button.interactable = interactable; - - Text textComponent = button.GetComponentInChildren(); - if (textComponent != null) - { - textComponent.text = text; - } - - button.onClick.AddListener(delegate { - - menuDialog.StopAllCoroutines(); // Stop timeout - menuDialog.Clear(); - menuDialog.HideSayDialog(); - - if (callBack != null) - { - luaEnv.RunLuaFunction(callBack, true); - } - }); - - addedOption = true; - break; - } - } - - return addedOption; - } - - /// - /// Extension for MenuDialog that allows ShowTimer to call a Lua function when the timer expires. - /// - public static IEnumerator ShowTimer(this MenuDialog menuDialog, float duration, ILuaEnvironment luaEnv, Closure callBack) - { - if (menuDialog.CachedSlider == null || - duration <= 0f) - { - yield break; - } - - menuDialog.CachedSlider.gameObject.SetActive(true); - menuDialog.StopAllCoroutines(); - - float elapsedTime = 0; - Slider timeoutSlider = menuDialog.GetComponentInChildren(); - - while (elapsedTime < duration) - { - if (timeoutSlider != null) - { - float t = 1f - elapsedTime / duration; - timeoutSlider.value = t; - } - - elapsedTime += Time.deltaTime; - - yield return null; - } - - menuDialog.Clear(); - menuDialog.gameObject.SetActive(false); - menuDialog.HideSayDialog(); - - if (callBack != null) - { - luaEnv.RunLuaFunction(callBack, true); - } - } - } -} diff --git a/Assets/Fungus/Scripts/Components/LuaExtensions.cs.meta b/Assets/Fungus/Scripts/Components/LuaExtensions.cs.meta deleted file mode 100644 index 28946979..00000000 --- a/Assets/Fungus/Scripts/Components/LuaExtensions.cs.meta +++ /dev/null @@ -1,13 +0,0 @@ -fileFormatVersion: 2 -guid: 998f811805a17465ba6720248632ec01 -timeCreated: 1459502744 -licenseType: Free -MonoImporter: - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: - diff --git a/Assets/Fungus/Scripts/Components/MenuDialog.cs b/Assets/Fungus/Scripts/Components/MenuDialog.cs index 7cb3cd74..d76ca18e 100644 --- a/Assets/Fungus/Scripts/Components/MenuDialog.cs +++ b/Assets/Fungus/Scripts/Components/MenuDialog.cs @@ -6,6 +6,7 @@ using UnityEngine.UI; using System.Collections; using UnityEngine.EventSystems; using System.Linq; +using MoonSharp.Interpreter; namespace Fungus { @@ -201,6 +202,48 @@ namespace Fungus return addedOption; } + public bool AddOption(string text, bool interactable, ILuaEnvironment luaEnv, Closure callBack) + { + if (!gameObject.activeSelf) + { + gameObject.SetActive(true); + } + + bool addedOption = false; + foreach (Button button in CachedButtons) + { + if (!button.gameObject.activeSelf) + { + button.gameObject.SetActive(true); + + button.interactable = interactable; + + Text textComponent = button.GetComponentInChildren(); + if (textComponent != null) + { + textComponent.text = text; + } + + button.onClick.AddListener(delegate { + + StopAllCoroutines(); // Stop timeout + Clear(); + HideSayDialog(); + + if (callBack != null) + { + luaEnv.RunLuaFunction(callBack, true); + } + }); + + addedOption = true; + break; + } + } + + return addedOption; + } + public virtual void ShowTimer(float duration, Block targetBlock) { if (cachedSlider != null) @@ -212,6 +255,43 @@ namespace Fungus } } + public IEnumerator ShowTimer(float duration, ILuaEnvironment luaEnv, Closure callBack) + { + if (CachedSlider == null || + duration <= 0f) + { + yield break; + } + + CachedSlider.gameObject.SetActive(true); + StopAllCoroutines(); + + float elapsedTime = 0; + Slider timeoutSlider = GetComponentInChildren(); + + while (elapsedTime < duration) + { + if (timeoutSlider != null) + { + float t = 1f - elapsedTime / duration; + timeoutSlider.value = t; + } + + elapsedTime += Time.deltaTime; + + yield return null; + } + + Clear(); + gameObject.SetActive(false); + HideSayDialog(); + + if (callBack != null) + { + luaEnv.RunLuaFunction(callBack, true); + } + } + public virtual bool IsActive() { return gameObject.activeInHierarchy; diff --git a/Assets/Fungus/Scripts/Interfaces/IMenuDialog.cs b/Assets/Fungus/Scripts/Interfaces/IMenuDialog.cs index a909cd5f..ae3a445c 100644 --- a/Assets/Fungus/Scripts/Interfaces/IMenuDialog.cs +++ b/Assets/Fungus/Scripts/Interfaces/IMenuDialog.cs @@ -1,5 +1,6 @@ -using UnityEngine; -using UnityEngine.UI; +using UnityEngine.UI; +using MoonSharp.Interpreter; +using System.Collections; namespace Fungus { @@ -36,7 +37,7 @@ namespace Fungus void HideSayDialog(); /// - /// Adds the option to the list of displayed options. + /// Adds the option to the list of displayed options. Calls a Block when selected. /// Will cause the Menu dialog to become visible if it is not already visible. /// /// true, if the option was added successfully. @@ -46,12 +47,24 @@ namespace Fungus bool AddOption(string text, bool interactable, Block targetBlock); /// - /// Show a timer during which the player can select an option. + /// Adds the option to the list of displayed options, calls a Lua function when selected. + /// Will cause the Menu dialog to become visible if it is not already visible. + /// + /// true, if the option was added successfully. + bool AddOption(string text, bool interactable, ILuaEnvironment luaEnv, Closure callBack); + + /// + /// Show a timer during which the player can select an option. Calls a Block when the timer expires. /// /// 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); + /// + /// Show a timer during which the player can select an option. Calls a Lua function when the timer expires. + /// + IEnumerator ShowTimer(float duration, ILuaEnvironment luaEnv, Closure callBack); + /// /// Returns true if the Menu Dialog is currently displayed. ///