-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInputHandler.cs
65 lines (57 loc) · 1.77 KB
/
InputHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
Yarn Spinner is licensed to you under the terms found in the file LICENSE.md.
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Yarn.Unity;
public class InputHandler : MonoBehaviour
{
public float fadeDuration = 1f;
public CanvasGroup inputGroup;
private DialogueRunner runner;
private string input;
void Start()
{
runner = FindObjectOfType<DialogueRunner>();
runner.AddCommandHandler<string>("input", InputRenamer);
}
/// <summary>
/// A blocking command that summons an input field, waits for input, and then stores the input into the variable.
/// </summary>
/// <param name="variable">The name of the yarn variable, without the $, to be inputted</param>
public IEnumerator InputRenamer(string variable)
{
var accumulator = 0f;
while (accumulator < fadeDuration)
{
var alpha = Mathf.Lerp(0, 1, accumulator/fadeDuration);
inputGroup.alpha = alpha;
accumulator += Time.deltaTime;
yield return null;
}
inputGroup.alpha = 1;
while (string.IsNullOrWhiteSpace(input))
{
yield return null;
}
runner.VariableStorage.SetValue($"${variable}", input);
input = null;
accumulator = 0;
while (accumulator < fadeDuration)
{
var alpha = Mathf.Lerp(1, 0, accumulator/fadeDuration);
inputGroup.alpha = alpha;
accumulator += Time.deltaTime;
yield return null;
}
inputGroup.alpha = 0;
}
// Called by the TMP input filed On Edit End event
// This is connected in the editor
public void OnEditEnd(string edit)
{
input = edit;
}
}