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.
150 lines
2.6 KiB
150 lines
2.6 KiB
using UnityEngine; |
|
using UnityEngine.UI; |
|
using UnityEngine.Events; |
|
using System; |
|
using System.Collections; |
|
using System.Collections.Generic; |
|
|
|
namespace Fungus.Script |
|
{ |
|
|
|
[ExecuteInEditMode] |
|
public class ChooseDialog : Dialog |
|
{ |
|
public Slider slider; |
|
|
|
public class Option |
|
{ |
|
public string text; |
|
public UnityAction onSelect; |
|
} |
|
|
|
public List<UnityEngine.UI.Button> optionButtons = new List<UnityEngine.UI.Button>(); |
|
|
|
static public List<ChooseDialog> activeDialogs = new List<ChooseDialog>(); |
|
|
|
void OnEnable() |
|
{ |
|
if (!activeDialogs.Contains(this)) |
|
{ |
|
activeDialogs.Add(this); |
|
} |
|
} |
|
|
|
void OnDisable() |
|
{ |
|
activeDialogs.Remove(this); |
|
} |
|
|
|
public void Choose(string text, List<Option> options, float timeoutDuration, Action onTimeout) |
|
{ |
|
Clear(); |
|
|
|
Action onWritingComplete = delegate { |
|
foreach (Option option in options) |
|
{ |
|
AddOption(option.text, option.onSelect); |
|
} |
|
|
|
if (timeoutDuration > 0) |
|
{ |
|
StartCoroutine(WaitForTimeout(timeoutDuration, onTimeout)); |
|
} |
|
}; |
|
|
|
StartCoroutine(WriteText(text, onWritingComplete, onTimeout)); |
|
} |
|
|
|
IEnumerator WaitForTimeout(float timeoutDuration, Action onTimeout) |
|
{ |
|
float elapsedTime = 0; |
|
|
|
while (elapsedTime < timeoutDuration) |
|
{ |
|
if (slider != null) |
|
{ |
|
float t = elapsedTime / timeoutDuration; |
|
slider.value = t; |
|
} |
|
|
|
elapsedTime += Time.deltaTime; |
|
|
|
yield return null; |
|
} |
|
|
|
Clear(); |
|
|
|
if (onTimeout != null) |
|
{ |
|
onTimeout(); |
|
} |
|
} |
|
|
|
protected override void Clear() |
|
{ |
|
base.Clear(); |
|
ClearOptions(); |
|
} |
|
|
|
void ClearOptions() |
|
{ |
|
if (optionButtons == null) |
|
{ |
|
return; |
|
} |
|
|
|
foreach (UnityEngine.UI.Button button in optionButtons) |
|
{ |
|
button.onClick.RemoveAllListeners(); |
|
} |
|
|
|
foreach (UnityEngine.UI.Button button in optionButtons) |
|
{ |
|
if (button != null) |
|
{ |
|
button.gameObject.SetActive(false); |
|
} |
|
} |
|
} |
|
|
|
bool AddOption(string text, UnityAction action) |
|
{ |
|
if (optionButtons == null) |
|
{ |
|
return false; |
|
} |
|
|
|
bool addedOption = false; |
|
foreach (UnityEngine.UI.Button button in optionButtons) |
|
{ |
|
if (!button.gameObject.activeSelf) |
|
{ |
|
button.gameObject.SetActive(true); |
|
|
|
Text textComponent = button.GetComponentInChildren<Text>(); |
|
if (textComponent != null) |
|
{ |
|
textComponent.text = text; |
|
} |
|
|
|
UnityAction buttonAction = action; |
|
|
|
button.onClick.AddListener(delegate { |
|
StopAllCoroutines(); // Stop timeout |
|
Clear(); |
|
if (buttonAction != null) |
|
{ |
|
buttonAction(); |
|
} |
|
}); |
|
|
|
addedOption = true; |
|
break; |
|
} |
|
} |
|
|
|
return addedOption; |
|
} |
|
} |
|
|
|
}
|
|
|