You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
97 lines
2.9 KiB
97 lines
2.9 KiB
9 years ago
|
using UnityEngine;
|
||
|
using UnityEngine.UI;
|
||
|
using System.Collections;
|
||
|
using Fungus;
|
||
|
using MoonSharp.Interpreter;
|
||
|
|
||
|
namespace Fungus
|
||
|
{
|
||
|
|
||
9 years ago
|
public static class LuaExtensions
|
||
9 years ago
|
{
|
||
|
|
||
|
/// <summary>
|
||
|
/// Extension for MenuDialog that allows AddOption to call a Lua function when an option is selected.
|
||
|
/// </summary>
|
||
9 years ago
|
public static bool AddOption(this MenuDialog menuDialog, string text, bool interactable, LuaEnvironment luaEnvironment, Closure callBack)
|
||
9 years ago
|
{
|
||
|
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();
|
||
|
|
||
|
menuDialog.gameObject.SetActive(false);
|
||
|
|
||
|
if (callBack != null)
|
||
|
{
|
||
9 years ago
|
luaEnvironment.RunLuaCoroutine(callBack, text);
|
||
9 years ago
|
}
|
||
|
});
|
||
|
|
||
|
addedOption = true;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return addedOption;
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Extension for MenuDialog that allows ShowTimer to call a Lua function when the timer expires.
|
||
|
/// </summary>
|
||
9 years ago
|
public static IEnumerator ShowTimer(this MenuDialog menuDialog, float duration, LuaEnvironment luaEnvironment, Closure callBack)
|
||
9 years ago
|
{
|
||
|
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)
|
||
|
{
|
||
9 years ago
|
luaEnvironment.RunLuaCoroutine(callBack, "menutimer");
|
||
9 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|