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.
509 lines
19 KiB
509 lines
19 KiB
8 years ago
|
// 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)
|
||
9 years ago
|
|
||
9 years ago
|
using UnityEngine;
|
||
|
using UnityEngine.UI;
|
||
|
using System;
|
||
|
using System.Collections;
|
||
|
using MoonSharp.Interpreter;
|
||
|
|
||
|
namespace Fungus
|
||
|
{
|
||
8 years ago
|
/// <summary>
|
||
|
/// Controls the Portrait sprites on stage
|
||
|
/// </summary>
|
||
8 years ago
|
public class PortraitController : MonoBehaviour, IPortraitController
|
||
8 years ago
|
{
|
||
|
// Timer for waitUntilFinished functionality
|
||
|
protected float waitTimer;
|
||
|
|
||
|
protected Stage stage;
|
||
|
|
||
8 years ago
|
protected virtual void Awake()
|
||
8 years ago
|
{
|
||
|
stage = GetComponentInParent<Stage>();
|
||
|
}
|
||
|
|
||
8 years ago
|
protected virtual void FinishCommand(PortraitOptions options)
|
||
8 years ago
|
{
|
||
|
if (options.onComplete != null)
|
||
|
{
|
||
|
if (!options.waitUntilFinished)
|
||
|
{
|
||
|
options.onComplete();
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
StartCoroutine(WaitUntilFinished(options.fadeDuration, options.onComplete));
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
StartCoroutine(WaitUntilFinished(options.fadeDuration));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Makes sure all options are set correctly so it won't break whatever command it's sent to
|
||
|
/// </summary>
|
||
|
/// <param name="options"></param>
|
||
|
/// <returns></returns>
|
||
8 years ago
|
protected virtual PortraitOptions CleanPortraitOptions(PortraitOptions options)
|
||
8 years ago
|
{
|
||
|
// Use default stage settings
|
||
|
if (options.useDefaultSettings)
|
||
|
{
|
||
8 years ago
|
options.fadeDuration = stage.FadeDuration;
|
||
|
options.moveDuration = stage.MoveDuration;
|
||
|
options.shiftOffset = stage.ShiftOffset;
|
||
8 years ago
|
}
|
||
|
|
||
|
// if no previous portrait, use default portrait
|
||
8 years ago
|
if (options.character.State.portrait == null)
|
||
8 years ago
|
{
|
||
8 years ago
|
options.character.State.portrait = options.character.ProfileSprite;
|
||
8 years ago
|
}
|
||
|
|
||
|
// Selected "use previous portrait"
|
||
|
if (options.portrait == null)
|
||
|
{
|
||
8 years ago
|
options.portrait = options.character.State.portrait;
|
||
8 years ago
|
}
|
||
|
|
||
|
// if no previous position, use default position
|
||
8 years ago
|
if (options.character.State.position == null)
|
||
8 years ago
|
{
|
||
8 years ago
|
options.character.State.position = stage.DefaultPosition.rectTransform;
|
||
8 years ago
|
}
|
||
|
|
||
|
// Selected "use previous position"
|
||
|
if (options.toPosition == null)
|
||
|
{
|
||
8 years ago
|
options.toPosition = options.character.State.position;
|
||
8 years ago
|
}
|
||
|
|
||
|
if (options.replacedCharacter != null)
|
||
|
{
|
||
|
// if no previous position, use default position
|
||
8 years ago
|
if (options.replacedCharacter.State.position == null)
|
||
8 years ago
|
{
|
||
8 years ago
|
options.replacedCharacter.State.position = stage.DefaultPosition.rectTransform;
|
||
8 years ago
|
}
|
||
|
}
|
||
|
|
||
|
// If swapping, use replaced character's position
|
||
|
if (options.display == DisplayType.Replace)
|
||
|
{
|
||
8 years ago
|
options.toPosition = options.replacedCharacter.State.position;
|
||
8 years ago
|
}
|
||
|
|
||
|
// Selected "use previous position"
|
||
|
if (options.fromPosition == null)
|
||
|
{
|
||
8 years ago
|
options.fromPosition = options.character.State.position;
|
||
8 years ago
|
}
|
||
|
|
||
|
// if portrait not moving, use from position is same as to position
|
||
|
if (!options.move)
|
||
|
{
|
||
|
options.fromPosition = options.toPosition;
|
||
|
}
|
||
|
|
||
|
if (options.display == DisplayType.Hide)
|
||
|
{
|
||
8 years ago
|
options.fromPosition = options.character.State.position;
|
||
8 years ago
|
}
|
||
|
|
||
|
// if no previous facing direction, use default facing direction
|
||
8 years ago
|
if (options.character.State.facing == FacingDirection.None)
|
||
8 years ago
|
{
|
||
8 years ago
|
options.character.State.facing = options.character.PortraitsFace;
|
||
8 years ago
|
}
|
||
|
|
||
|
// Selected "use previous facing direction"
|
||
|
if (options.facing == FacingDirection.None)
|
||
|
{
|
||
8 years ago
|
options.facing = options.character.State.facing;
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
if (options.character.State.portraitImage == null)
|
||
8 years ago
|
{
|
||
|
CreatePortraitObject(options.character, options.fadeDuration);
|
||
|
}
|
||
|
|
||
|
return options;
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Creates and sets the portrait image for a character
|
||
|
/// </summary>
|
||
|
/// <param name="character"></param>
|
||
|
/// <param name="fadeDuration"></param>
|
||
8 years ago
|
protected virtual void CreatePortraitObject(Character character, float fadeDuration)
|
||
8 years ago
|
{
|
||
|
// Create a new portrait object
|
||
|
GameObject portraitObj = new GameObject(character.name,
|
||
|
typeof(RectTransform),
|
||
|
typeof(CanvasRenderer),
|
||
|
typeof(Image));
|
||
|
|
||
|
// Set it to be a child of the stage
|
||
8 years ago
|
portraitObj.transform.SetParent(stage.PortraitCanvas.transform, true);
|
||
8 years ago
|
|
||
|
// Configure the portrait image
|
||
|
Image portraitImage = portraitObj.GetComponent<Image>();
|
||
|
portraitImage.preserveAspect = true;
|
||
8 years ago
|
portraitImage.sprite = character.ProfileSprite;
|
||
8 years ago
|
portraitImage.color = new Color(1f, 1f, 1f, 0f);
|
||
|
|
||
|
// LeanTween doesn't handle 0 duration properly
|
||
|
float duration = (fadeDuration > 0f) ? fadeDuration : float.Epsilon;
|
||
|
|
||
|
// Fade in character image (first time)
|
||
8 years ago
|
LeanTween.alpha(portraitImage.transform as RectTransform, 1f, duration).setEase(stage.FadeEaseType);
|
||
8 years ago
|
|
||
|
// Tell character about portrait image
|
||
8 years ago
|
character.State.portraitImage = portraitImage;
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
protected virtual IEnumerator WaitUntilFinished(float duration, Action onComplete = null)
|
||
8 years ago
|
{
|
||
|
// Wait until the timer has expired
|
||
|
// Any method can modify this timer variable to delay continuing.
|
||
|
|
||
|
waitTimer = duration;
|
||
|
while (waitTimer > 0f)
|
||
|
{
|
||
|
waitTimer -= Time.deltaTime;
|
||
|
yield return null;
|
||
|
}
|
||
|
|
||
|
if (onComplete != null)
|
||
|
{
|
||
|
onComplete();
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
protected virtual void SetupPortrait(PortraitOptions options)
|
||
8 years ago
|
{
|
||
8 years ago
|
SetRectTransform(options.character.State.portraitImage.rectTransform, options.fromPosition);
|
||
8 years ago
|
|
||
8 years ago
|
if (options.character.State.facing != options.character.PortraitsFace)
|
||
8 years ago
|
{
|
||
8 years ago
|
options.character.State.portraitImage.rectTransform.localScale = new Vector3(-1f, 1f, 1f);
|
||
8 years ago
|
}
|
||
|
else
|
||
|
{
|
||
8 years ago
|
options.character.State.portraitImage.rectTransform.localScale = new Vector3(1f, 1f, 1f);
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
if (options.facing != options.character.PortraitsFace)
|
||
8 years ago
|
{
|
||
8 years ago
|
options.character.State.portraitImage.rectTransform.localScale = new Vector3(-1f, 1f, 1f);
|
||
8 years ago
|
}
|
||
|
else
|
||
|
{
|
||
8 years ago
|
options.character.State.portraitImage.rectTransform.localScale = new Vector3(1f, 1f, 1f);
|
||
8 years ago
|
}
|
||
|
}
|
||
|
|
||
|
public static void SetRectTransform(RectTransform oldRectTransform, RectTransform newRectTransform)
|
||
|
{
|
||
|
oldRectTransform.eulerAngles = newRectTransform.eulerAngles;
|
||
|
oldRectTransform.position = newRectTransform.position;
|
||
|
oldRectTransform.rotation = newRectTransform.rotation;
|
||
|
oldRectTransform.anchoredPosition = newRectTransform.anchoredPosition;
|
||
|
oldRectTransform.sizeDelta = newRectTransform.sizeDelta;
|
||
|
oldRectTransform.anchorMax = newRectTransform.anchorMax;
|
||
|
oldRectTransform.anchorMin = newRectTransform.anchorMin;
|
||
|
oldRectTransform.pivot = newRectTransform.pivot;
|
||
|
oldRectTransform.localScale = newRectTransform.localScale;
|
||
|
}
|
||
|
|
||
8 years ago
|
protected virtual void DoMoveTween(Character character, RectTransform fromPosition, RectTransform toPosition, float moveDuration, Boolean waitUntilFinished)
|
||
8 years ago
|
{
|
||
|
PortraitOptions options = new PortraitOptions(true);
|
||
|
options.character = character;
|
||
|
options.fromPosition = fromPosition;
|
||
|
options.toPosition = toPosition;
|
||
|
options.moveDuration = moveDuration;
|
||
|
options.waitUntilFinished = waitUntilFinished;
|
||
|
|
||
|
DoMoveTween(options);
|
||
|
}
|
||
|
|
||
8 years ago
|
protected virtual void DoMoveTween(PortraitOptions options)
|
||
8 years ago
|
{
|
||
|
CleanPortraitOptions(options);
|
||
|
|
||
|
// LeanTween doesn't handle 0 duration properly
|
||
|
float duration = (options.moveDuration > 0f) ? options.moveDuration : float.Epsilon;
|
||
|
|
||
|
// LeanTween.move uses the anchoredPosition, so all position images must have the same anchor position
|
||
8 years ago
|
LeanTween.move(options.character.State.portraitImage.gameObject, options.toPosition.position, duration).setEase(stage.FadeEaseType);
|
||
8 years ago
|
|
||
|
if (options.waitUntilFinished)
|
||
|
{
|
||
|
waitTimer = duration;
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
#region IPortraitController implentation
|
||
|
|
||
|
public virtual void RunPortraitCommand(PortraitOptions options, Action onComplete)
|
||
|
{
|
||
|
waitTimer = 0f;
|
||
|
|
||
|
// If no character specified, do nothing
|
||
|
if (options.character == null)
|
||
|
{
|
||
|
onComplete();
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
// If Replace and no replaced character specified, do nothing
|
||
|
if (options.display == DisplayType.Replace && options.replacedCharacter == null)
|
||
|
{
|
||
|
onComplete();
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
// Early out if hiding a character that's already hidden
|
||
|
if (options.display == DisplayType.Hide &&
|
||
|
!options.character.State.onScreen)
|
||
|
{
|
||
|
onComplete();
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
options = CleanPortraitOptions(options);
|
||
|
options.onComplete = onComplete;
|
||
|
|
||
|
switch (options.display)
|
||
|
{
|
||
|
case (DisplayType.Show):
|
||
|
Show(options);
|
||
|
break;
|
||
|
|
||
|
case (DisplayType.Hide):
|
||
|
Hide(options);
|
||
|
break;
|
||
|
|
||
|
case (DisplayType.Replace):
|
||
|
Show(options);
|
||
|
Hide(options.replacedCharacter, options.replacedCharacter.State.position.name);
|
||
|
break;
|
||
|
|
||
|
case (DisplayType.MoveToFront):
|
||
|
MoveToFront(options);
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public virtual void MoveToFront(Character character)
|
||
|
{
|
||
|
PortraitOptions options = new PortraitOptions(true);
|
||
|
options.character = character;
|
||
|
|
||
|
MoveToFront(CleanPortraitOptions(options));
|
||
|
}
|
||
|
|
||
|
public virtual void MoveToFront(PortraitOptions options)
|
||
|
{
|
||
|
options.character.State.portraitImage.transform.SetSiblingIndex(options.character.State.portraitImage.transform.parent.childCount);
|
||
|
options.character.State.display = DisplayType.MoveToFront;
|
||
|
FinishCommand(options);
|
||
|
}
|
||
|
|
||
|
public virtual void Show(Character character, string position)
|
||
8 years ago
|
{
|
||
|
PortraitOptions options = new PortraitOptions(true);
|
||
|
options.character = character;
|
||
|
options.fromPosition = options.toPosition = stage.GetPosition(position);
|
||
|
|
||
|
Show(options);
|
||
|
}
|
||
|
|
||
8 years ago
|
public virtual void Show(Character character, string portrait, string fromPosition, string toPosition)
|
||
8 years ago
|
{
|
||
|
PortraitOptions options = new PortraitOptions(true);
|
||
|
options.character = character;
|
||
|
options.portrait = character.GetPortrait(portrait);
|
||
|
options.fromPosition = stage.GetPosition(fromPosition);
|
||
|
options.toPosition = stage.GetPosition(toPosition);
|
||
|
options.move = true;
|
||
|
|
||
|
Show(options);
|
||
|
}
|
||
|
|
||
8 years ago
|
public virtual void Show(Table optionsTable)
|
||
8 years ago
|
{
|
||
|
Show(PortraitUtil.ConvertTableToPortraitOptions(optionsTable, stage));
|
||
|
}
|
||
|
|
||
8 years ago
|
public virtual void Show(PortraitOptions options)
|
||
8 years ago
|
{
|
||
|
options = CleanPortraitOptions(options);
|
||
|
|
||
|
if (options.shiftIntoPlace)
|
||
|
{
|
||
|
options.fromPosition = Instantiate(options.toPosition) as RectTransform;
|
||
|
if (options.offset == PositionOffset.OffsetLeft)
|
||
|
{
|
||
|
options.fromPosition.anchoredPosition =
|
||
|
new Vector2(options.fromPosition.anchoredPosition.x - Mathf.Abs(options.shiftOffset.x),
|
||
8 years ago
|
options.fromPosition.anchoredPosition.y - Mathf.Abs(options.shiftOffset.y));
|
||
8 years ago
|
}
|
||
|
else if (options.offset == PositionOffset.OffsetRight)
|
||
|
{
|
||
|
options.fromPosition.anchoredPosition =
|
||
|
new Vector2(options.fromPosition.anchoredPosition.x + Mathf.Abs(options.shiftOffset.x),
|
||
8 years ago
|
options.fromPosition.anchoredPosition.y + Mathf.Abs(options.shiftOffset.y));
|
||
8 years ago
|
}
|
||
|
else
|
||
|
{
|
||
|
options.fromPosition.anchoredPosition = new Vector2(options.fromPosition.anchoredPosition.x, options.fromPosition.anchoredPosition.y);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
SetupPortrait(options);
|
||
|
|
||
|
// LeanTween doesn't handle 0 duration properly
|
||
|
float duration = (options.fadeDuration > 0f) ? options.fadeDuration : float.Epsilon;
|
||
|
|
||
|
// Fade out a duplicate of the existing portrait image
|
||
8 years ago
|
if (options.character.State.portraitImage != null)
|
||
8 years ago
|
{
|
||
8 years ago
|
GameObject tempGO = GameObject.Instantiate(options.character.State.portraitImage.gameObject);
|
||
|
tempGO.transform.SetParent(options.character.State.portraitImage.transform, false);
|
||
8 years ago
|
tempGO.transform.localPosition = Vector3.zero;
|
||
8 years ago
|
tempGO.transform.localScale = options.character.State.position.localScale;
|
||
8 years ago
|
|
||
|
Image tempImage = tempGO.GetComponent<Image>();
|
||
8 years ago
|
tempImage.sprite = options.character.State.portraitImage.sprite;
|
||
8 years ago
|
tempImage.preserveAspect = true;
|
||
8 years ago
|
tempImage.color = options.character.State.portraitImage.color;
|
||
8 years ago
|
|
||
8 years ago
|
LeanTween.alpha(tempImage.rectTransform, 0f, duration).setEase(stage.FadeEaseType).setOnComplete(() => {
|
||
8 years ago
|
Destroy(tempGO);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
// Fade in the new sprite image
|
||
8 years ago
|
if (options.character.State.portraitImage.sprite != options.portrait ||
|
||
|
options.character.State.portraitImage.color.a < 1f)
|
||
8 years ago
|
{
|
||
8 years ago
|
options.character.State.portraitImage.sprite = options.portrait;
|
||
|
options.character.State.portraitImage.color = new Color(1f, 1f, 1f, 0f);
|
||
|
LeanTween.alpha(options.character.State.portraitImage.rectTransform, 1f, duration).setEase(stage.FadeEaseType);
|
||
8 years ago
|
}
|
||
|
|
||
|
DoMoveTween(options);
|
||
|
|
||
|
FinishCommand(options);
|
||
|
|
||
8 years ago
|
if (!stage.CharactersOnStage.Contains(options.character))
|
||
8 years ago
|
{
|
||
8 years ago
|
stage.CharactersOnStage.Add(options.character);
|
||
8 years ago
|
}
|
||
|
|
||
|
// Update character state after showing
|
||
8 years ago
|
options.character.State.onScreen = true;
|
||
|
options.character.State.display = DisplayType.Show;
|
||
|
options.character.State.portrait = options.portrait;
|
||
|
options.character.State.facing = options.facing;
|
||
|
options.character.State.position = options.toPosition;
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
public virtual void ShowPortrait(Character character, string portrait)
|
||
8 years ago
|
{
|
||
|
PortraitOptions options = new PortraitOptions(true);
|
||
|
options.character = character;
|
||
|
options.portrait = character.GetPortrait(portrait);
|
||
|
|
||
8 years ago
|
if (character.State.position == null)
|
||
8 years ago
|
{
|
||
|
options.toPosition = options.fromPosition = stage.GetPosition("middle");
|
||
|
}
|
||
|
else
|
||
|
{
|
||
8 years ago
|
options.fromPosition = options.toPosition = character.State.position;
|
||
8 years ago
|
}
|
||
|
|
||
|
Show(options);
|
||
|
}
|
||
|
|
||
8 years ago
|
public virtual void Hide(Character character)
|
||
8 years ago
|
{
|
||
|
PortraitOptions options = new PortraitOptions(true);
|
||
|
options.character = character;
|
||
|
|
||
|
Hide(options);
|
||
|
}
|
||
|
|
||
8 years ago
|
public virtual void Hide(Character character, string toPosition)
|
||
8 years ago
|
{
|
||
|
PortraitOptions options = new PortraitOptions(true);
|
||
|
options.character = character;
|
||
|
options.toPosition = stage.GetPosition(toPosition);
|
||
|
options.move = true;
|
||
|
|
||
|
Hide(options);
|
||
|
}
|
||
|
|
||
8 years ago
|
public virtual void Hide(Table optionsTable)
|
||
8 years ago
|
{
|
||
|
Hide(PortraitUtil.ConvertTableToPortraitOptions(optionsTable, stage));
|
||
|
}
|
||
|
|
||
8 years ago
|
public virtual void Hide(PortraitOptions options)
|
||
8 years ago
|
{
|
||
|
CleanPortraitOptions(options);
|
||
|
|
||
8 years ago
|
if (options.character.State.display == DisplayType.None)
|
||
8 years ago
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
SetupPortrait(options);
|
||
|
|
||
|
// LeanTween doesn't handle 0 duration properly
|
||
|
float duration = (options.fadeDuration > 0f) ? options.fadeDuration : float.Epsilon;
|
||
|
|
||
8 years ago
|
LeanTween.alpha(options.character.State.portraitImage.rectTransform, 0f, duration).setEase(stage.FadeEaseType);
|
||
8 years ago
|
|
||
|
DoMoveTween(options);
|
||
|
|
||
8 years ago
|
stage.CharactersOnStage.Remove(options.character);
|
||
8 years ago
|
|
||
|
//update character state after hiding
|
||
8 years ago
|
options.character.State.onScreen = false;
|
||
|
options.character.State.portrait = options.portrait;
|
||
|
options.character.State.facing = options.facing;
|
||
|
options.character.State.position = options.toPosition;
|
||
|
options.character.State.display = DisplayType.Hide;
|
||
8 years ago
|
|
||
|
FinishCommand(options);
|
||
|
}
|
||
|
|
||
8 years ago
|
public virtual void SetDimmed(Character character, bool dimmedState)
|
||
8 years ago
|
{
|
||
8 years ago
|
if (character.State.dimmed == dimmedState)
|
||
8 years ago
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
8 years ago
|
character.State.dimmed = dimmedState;
|
||
8 years ago
|
|
||
|
Color targetColor = dimmedState ? new Color(0.5f, 0.5f, 0.5f, 1f) : Color.white;
|
||
|
|
||
|
// LeanTween doesn't handle 0 duration properly
|
||
8 years ago
|
float duration = (stage.FadeDuration > 0f) ? stage.FadeDuration : float.Epsilon;
|
||
8 years ago
|
|
||
8 years ago
|
LeanTween.color(character.State.portraitImage.rectTransform, targetColor, duration).setEase(stage.FadeEaseType);
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
#endregion
|
||
8 years ago
|
}
|
||
9 years ago
|
}
|