// This code is part of the Fungus library (https://github.com/snozbot/fungus) // It is released for free under the MIT open source license (https://github.com/snozbot/fungus/blob/master/LICENSE) using UnityEngine; using UnityEngine.UI; using MoonSharp.Interpreter; using System.Collections.Generic; namespace Fungus { /// /// Contains all options to run a portrait command. /// public class PortraitOptions { public Character character; public Character replacedCharacter; public Sprite portrait; public DisplayType display; public PositionOffset offset; public RectTransform fromPosition; public RectTransform toPosition; public FacingDirection facing; public bool useDefaultSettings; public float fadeDuration; public float moveDuration; public Vector2 shiftOffset; public bool move; //sets to position to be the same as from public bool shiftIntoPlace; public bool waitUntilFinished; public System.Action onComplete; public PortraitOptions(bool useDefaultSettings = true) { character = null; replacedCharacter = null; portrait = null; display = DisplayType.None; offset = PositionOffset.None; fromPosition = null; toPosition = null; facing = FacingDirection.None; shiftOffset = new Vector2(0, 0); move = false; shiftIntoPlace = false; waitUntilFinished = false; onComplete = null; // Special values that can be overridden fadeDuration = 0.5f; moveDuration = 1f; this.useDefaultSettings = useDefaultSettings; } } /// /// Represents the current state of a character portrait on the stage. /// public class PortraitState { public bool onScreen; public bool dimmed; public DisplayType display; public RectTransform position, holder; public FacingDirection facing; public Image portraitImage; public Sprite portrait { get { return portraitImage != null ? portraitImage.sprite : null; } } public List allPortraits = new List(); public void SetPortraitImageBySprite(Sprite portrait) { portraitImage = allPortraits.Find(x => x.sprite == portrait); } } /// /// Util functions for working with portraits. /// public static class PortraitUtil { #region Public members /// /// Convert a Moonsharp table to portrait options /// If the table returns a null for any of the parameters, it should keep the defaults /// /// Moonsharp Table /// Stage /// public static PortraitOptions ConvertTableToPortraitOptions(Table table, Stage stage) { PortraitOptions options = new PortraitOptions(true); // If the table supplies a nil, keep the default options.character = table.Get("character").ToObject() ?? options.character; options.replacedCharacter = table.Get("replacedCharacter").ToObject() ?? options.replacedCharacter; if (!table.Get("portrait").IsNil()) { options.portrait = options.character.GetPortrait(table.Get("portrait").CastToString()); } if (!table.Get("display").IsNil()) { options.display = table.Get("display").ToObject(); } if (!table.Get("offset").IsNil()) { options.offset = table.Get("offset").ToObject(); } if (!table.Get("fromPosition").IsNil()) { options.fromPosition = stage.GetPosition(table.Get("fromPosition").CastToString()); } if (!table.Get("toPosition").IsNil()) { options.toPosition = stage.GetPosition(table.Get("toPosition").CastToString()); } if (!table.Get("facing").IsNil()) { var facingDirection = FacingDirection.None; DynValue v = table.Get("facing"); if (v.Type == DataType.String) { if (string.Compare(v.String, "left", true) == 0) { facingDirection = FacingDirection.Left; } else if (string.Compare(v.String, "right", true) == 0) { facingDirection = FacingDirection.Right; } } else { facingDirection = table.Get("facing").ToObject(); } options.facing = facingDirection; } if (!table.Get("useDefaultSettings").IsNil()) { options.useDefaultSettings = table.Get("useDefaultSettings").CastToBool(); } if (!table.Get("fadeDuration").IsNil()) { options.fadeDuration = table.Get("fadeDuration").ToObject(); } if (!table.Get("moveDuration").IsNil()) { options.moveDuration = table.Get("moveDuration").ToObject(); } if (!table.Get("move").IsNil()) { options.move = table.Get("move").CastToBool(); } else if (options.fromPosition != options.toPosition) { options.move = true; } if (!table.Get("shiftIntoPlace").IsNil()) { options.shiftIntoPlace = table.Get("shiftIntoPlace").CastToBool(); } if (!table.Get("waitUntilFinished").IsNil()) { options.waitUntilFinished = table.Get("waitUntilFinished").CastToBool(); } return options; } static public int PortraitCompareTo(Sprite x, Sprite y) { if (x == y) return 0; if (y == null) return 1; if (x == null) return -1; return x.name.CompareTo(y.name); } #endregion } }