using CinemaDirector.Helpers;
using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine.UI;
using UnityEngine;
namespace CinemaDirector
{
[CutsceneItemAttribute("uGUI", "Text Generator", CutsceneItemGenre.ActorItem)]
public class TextGenerationEvent : CinemaActorAction, IRevertable
{
[TextArea(3, 10)]
public string textValue;
private string initialTextValue;
// Options for reverting in editor.
[SerializeField]
private RevertMode editorRevertMode = RevertMode.Revert;
// Options for reverting during runtime.
[SerializeField]
private RevertMode runtimeRevertMode = RevertMode.Revert;
///
/// Cache the state of all actors related to this event.
///
///
public RevertInfo[] CacheState()
{
List actors = new List(GetActors());
List reverts = new List();
for (int i = 0; i < actors.Count; i++)
{
Transform go = actors[i];
if (go != null)
{
Text txt = go.GetComponentInChildren();
if (txt != null)
{
reverts.Add(new RevertInfo(this, txt, "text", txt.text));
}
}
}
return reverts.ToArray();
}
public override void Trigger(GameObject actor)
{
initialTextValue = actor.GetComponentInChildren().text;
actor.GetComponentInChildren().text = "";
#if UNITY_EDITOR
EditorUtility.SetDirty(actor.GetComponentInChildren());
#endif
}
public override void ReverseTrigger(GameObject actor)
{
actor.GetComponentInChildren().text = initialTextValue;
#if UNITY_EDITOR
EditorUtility.SetDirty(actor.GetComponentInChildren());
#endif
}
public override void SetTime(GameObject actor, float time, float deltaTime)
{
if (actor != null)
if (time > 0 && time <= Duration)
UpdateTime(actor, time, deltaTime);
}
public override void UpdateTime(GameObject actor, float runningTime, float deltaTime)
{
float transition = runningTime / Duration;
int numericalValue;
if (textValue!=null)
{
numericalValue = (int)Mathf.Round(Mathf.Lerp(0, textValue.Length, transition));
actor.GetComponentInChildren().text = textValue.Substring(0, numericalValue);
#if UNITY_EDITOR
EditorUtility.SetDirty(actor.GetComponentInChildren());
#endif
}
}
public override void End(GameObject actor)
{
actor.GetComponentInChildren().text= textValue;
#if UNITY_EDITOR
EditorUtility.SetDirty(actor.GetComponentInChildren());
#endif
}
///
/// Option for choosing when this Event will Revert to initial state in Editor.
///
public RevertMode EditorRevertMode
{
get { return editorRevertMode; }
set { editorRevertMode = value; }
}
///
/// Option for choosing when this Event will Revert to initial state in Runtime.
///
public RevertMode RuntimeRevertMode
{
get { return runtimeRevertMode; }
set { runtimeRevertMode = value; }
}
}
}