Browse Source

Merged MenuDialog extension methods with MenuDialog class.

master
Christopher 9 years ago
parent
commit
b6a1e2df57
  1. 99
      Assets/Fungus/Scripts/Components/LuaExtensions.cs
  2. 13
      Assets/Fungus/Scripts/Components/LuaExtensions.cs.meta
  3. 80
      Assets/Fungus/Scripts/Components/MenuDialog.cs
  4. 21
      Assets/Fungus/Scripts/Interfaces/IMenuDialog.cs

99
Assets/Fungus/Scripts/Components/LuaExtensions.cs

@ -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
{
/// <summary>
/// Extension for MenuDialog that allows AddOption to call a Lua function when an option is selected.
/// </summary>
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<Text>();
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;
}
/// <summary>
/// Extension for MenuDialog that allows ShowTimer to call a Lua function when the timer expires.
/// </summary>
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<Slider>();
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);
}
}
}
}

13
Assets/Fungus/Scripts/Components/LuaExtensions.cs.meta

@ -1,13 +0,0 @@
fileFormatVersion: 2
guid: 998f811805a17465ba6720248632ec01
timeCreated: 1459502744
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

80
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<Text>();
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<Slider>();
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;

21
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();
/// <summary>
/// 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.
/// </summary>
/// <returns><c>true</c>, if the option was added successfully.</returns>
@ -46,12 +47,24 @@ namespace Fungus
bool AddOption(string text, bool interactable, Block targetBlock);
/// <summary>
/// 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.
/// </summary>
/// <returns><c>true</c>, if the option was added successfully.</returns>
bool AddOption(string text, bool interactable, ILuaEnvironment luaEnv, Closure callBack);
/// <summary>
/// Show a timer during which the player can select an option. Calls a Block when the timer expires.
/// </summary>
/// <param name="duration">The duration during which the player can select an option.</param>
/// <param name="targetBlock">Block to execute if the player does not select an option in time.</param>
void ShowTimer(float duration, Block targetBlock);
/// <summary>
/// Show a timer during which the player can select an option. Calls a Lua function when the timer expires.
/// </summary>
IEnumerator ShowTimer(float duration, ILuaEnvironment luaEnv, Closure callBack);
/// <summary>
/// Returns true if the Menu Dialog is currently displayed.
/// </summary>

Loading…
Cancel
Save