diff --git a/Assets/Fungus/Narrative/Scripts/IPortraitController.cs b/Assets/Fungus/Narrative/Scripts/IPortraitController.cs new file mode 100644 index 00000000..966ef404 --- /dev/null +++ b/Assets/Fungus/Narrative/Scripts/IPortraitController.cs @@ -0,0 +1,125 @@ +using UnityEngine; +using MoonSharp.Interpreter; + +namespace Fungus +{ + /// + /// Types of display operations supported by portraits + /// + public enum DisplayType + { + None, + Show, + Hide, + Replace, + MoveToFront + } + + /// + /// Directions that character portraits can face. + /// + public enum FacingDirection + { + None, + Left, + Right + } + + /// + /// Offset direction for position. + /// + public enum PositionOffset + { + None, + OffsetLeft, + OffsetRight + } + + /// + /// Controls the Portrait sprites on stage + /// + public interface IPortraitController + { + /// + /// Using all portrait options available, run any portrait command. + /// + /// Portrait Options + /// The function that will run once the portrait command finishes + void RunPortraitCommand(PortraitOptions options, System.Action onComplete); + + /// + /// Moves Character in front of other characters on stage + /// + void MoveToFront(Character character); + + /// + /// Moves Character in front of other characters on stage + /// + void MoveToFront(PortraitOptions options); + + /// + /// Shows character at a named position in the stage + /// + /// + /// Named position on stage + void Show(Character character, string position); + + /// + /// Shows character moving from a position to a position + /// + /// + /// + /// Where the character will appear + /// Where the character will move to + void Show(Character character, string portrait, string fromPosition, string toPosition); + + /// + /// From lua, you can pass an options table with named arguments + /// example: + /// stage.show{character=jill, portrait="happy", fromPosition="right", toPosition="left"} + /// Any option available in the PortraitOptions is available from Lua + /// + /// Moonsharp Table + void Show(Table optionsTable); + + /// + /// Show portrait with the supplied portrait options + /// + /// + void Show(PortraitOptions options); + + /// + /// Simple show command that shows the character with an available named portrait + /// + /// Character to show + /// Named portrait to show for the character, i.e. "angry", "happy", etc + void ShowPortrait(Character character, string portrait); + + /// + /// Simple character hide command + /// + /// Character to hide + void Hide(Character character); + + /// + /// Move the character to a position then hide it + /// + /// Character to hide + /// Where the character will disapear to + void Hide(Character character, string toPosition); + + /// + /// From lua, you can pass an options table with named arguments + /// example: + /// stage.hide{character=jill, toPosition="left"} + /// Any option available in the PortraitOptions is available from Lua + /// + /// Moonsharp Table + void Hide(Table optionsTable); + + /// + /// Hide portrait with provided options + /// + void Hide(PortraitOptions options); + } +} \ No newline at end of file diff --git a/Assets/Fungus/Narrative/Scripts/IPortraitController.cs.meta b/Assets/Fungus/Narrative/Scripts/IPortraitController.cs.meta new file mode 100644 index 00000000..d25bf761 --- /dev/null +++ b/Assets/Fungus/Narrative/Scripts/IPortraitController.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 51067f95c67324a0ba05a260dced682f +timeCreated: 1473685101 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Fungus/Narrative/Scripts/PortraitController.cs b/Assets/Fungus/Narrative/Scripts/PortraitController.cs index ae79f5ca..92f1a60f 100644 --- a/Assets/Fungus/Narrative/Scripts/PortraitController.cs +++ b/Assets/Fungus/Narrative/Scripts/PortraitController.cs @@ -9,159 +9,23 @@ using MoonSharp.Interpreter; namespace Fungus { - 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 Action onComplete; - - /// - /// Contains all options to run a portrait command. - /// - /// Will use stage default times for animation and fade - 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; - } - } - - public class PortraitState - { - public bool onScreen; - public bool dimmed; - public DisplayType display; - public Sprite portrait; - public RectTransform position; - public FacingDirection facing; - public Image portraitImage; - } - - public enum DisplayType - { - None, - Show, - Hide, - Replace, - MoveToFront - } - - public enum FacingDirection - { - None, - Left, - Right - } - - public enum PositionOffset - { - None, - OffsetLeft, - OffsetRight - } - /// /// Controls the Portrait sprites on stage /// - public class PortraitController : MonoBehaviour + public class PortraitController : MonoBehaviour, IPortraitController { // Timer for waitUntilFinished functionality protected float waitTimer; protected Stage stage; - void Awake() + protected virtual void Awake() { stage = GetComponentInParent(); stage.CachePositions(); } - /// - /// Using all portrait options available, run any portrait command. - /// - /// Portrait Options - /// The function that will run once the portrait command finishes - public 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; - } - - } - - protected void FinishCommand(PortraitOptions options) + protected virtual void FinishCommand(PortraitOptions options) { if (options.onComplete != null) { @@ -185,7 +49,7 @@ namespace Fungus /// /// /// - protected PortraitOptions CleanPortraitOptions(PortraitOptions options) + protected virtual PortraitOptions CleanPortraitOptions(PortraitOptions options) { // Use default stage settings if (options.useDefaultSettings) @@ -276,7 +140,7 @@ namespace Fungus /// /// /// - protected void CreatePortraitObject(Character character, float fadeDuration) + protected virtual void CreatePortraitObject(Character character, float fadeDuration) { // Create a new portrait object GameObject portraitObj = new GameObject(character.name, @@ -303,7 +167,7 @@ namespace Fungus character.State.portraitImage = portraitImage; } - protected IEnumerator WaitUntilFinished(float duration, Action onComplete = null) + protected virtual IEnumerator WaitUntilFinished(float duration, Action onComplete = null) { // Wait until the timer has expired // Any method can modify this timer variable to delay continuing. @@ -321,7 +185,7 @@ namespace Fungus } } - protected void SetupPortrait(PortraitOptions options) + protected virtual void SetupPortrait(PortraitOptions options) { SetRectTransform(options.character.State.portraitImage.rectTransform, options.fromPosition); @@ -357,26 +221,7 @@ namespace Fungus oldRectTransform.localScale = newRectTransform.localScale; } - /// - /// Moves Character in front of other characters on stage - /// - /// - public void MoveToFront(Character character) - { - PortraitOptions options = new PortraitOptions(true); - options.character = character; - - MoveToFront(CleanPortraitOptions(options)); - } - - public 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 void DoMoveTween(Character character, RectTransform fromPosition, RectTransform toPosition, float moveDuration, Boolean waitUntilFinished) + protected virtual void DoMoveTween(Character character, RectTransform fromPosition, RectTransform toPosition, float moveDuration, Boolean waitUntilFinished) { PortraitOptions options = new PortraitOptions(true); options.character = character; @@ -388,7 +233,7 @@ namespace Fungus DoMoveTween(options); } - public void DoMoveTween(PortraitOptions options) + protected virtual void DoMoveTween(PortraitOptions options) { CleanPortraitOptions(options); @@ -404,12 +249,74 @@ namespace Fungus } } - /// - /// Shows character at a named position in the stage - /// - /// - /// Named position on stage - public void Show(Character character, string position) + #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) { PortraitOptions options = new PortraitOptions(true); options.character = character; @@ -418,14 +325,7 @@ namespace Fungus Show(options); } - /// - /// Shows character moving from a position to a position - /// - /// - /// - /// Where the character will appear - /// Where the character will move to - public void Show(Character character, string portrait, string fromPosition, string toPosition) + public virtual void Show(Character character, string portrait, string fromPosition, string toPosition) { PortraitOptions options = new PortraitOptions(true); options.character = character; @@ -437,23 +337,12 @@ namespace Fungus Show(options); } - /// - /// From lua, you can pass an options table with named arguments - /// example: - /// stage.show{character=jill, portrait="happy", fromPosition="right", toPosition="left"} - /// Any option available in the PortraitOptions is available from Lua - /// - /// Moonsharp Table - public void Show(Table optionsTable) + public virtual void Show(Table optionsTable) { Show(PortraitUtil.ConvertTableToPortraitOptions(optionsTable, stage)); } - /// - /// Show portrait with the supplied portrait options - /// - /// - public void Show(PortraitOptions options) + public virtual void Show(PortraitOptions options) { options = CleanPortraitOptions(options); @@ -464,13 +353,13 @@ namespace Fungus { options.fromPosition.anchoredPosition = new Vector2(options.fromPosition.anchoredPosition.x - Mathf.Abs(options.shiftOffset.x), - options.fromPosition.anchoredPosition.y - Mathf.Abs(options.shiftOffset.y)); + options.fromPosition.anchoredPosition.y - Mathf.Abs(options.shiftOffset.y)); } else if (options.offset == PositionOffset.OffsetRight) { options.fromPosition.anchoredPosition = new Vector2(options.fromPosition.anchoredPosition.x + Mathf.Abs(options.shiftOffset.x), - options.fromPosition.anchoredPosition.y + Mathf.Abs(options.shiftOffset.y)); + options.fromPosition.anchoredPosition.y + Mathf.Abs(options.shiftOffset.y)); } else { @@ -527,12 +416,7 @@ namespace Fungus options.character.State.position = options.toPosition; } - /// - /// Simple show command that shows the character with an available named portrait - /// - /// Character to show - /// Named portrait to show for the character, i.e. "angry", "happy", etc - public void ShowPortrait(Character character, string portrait) + public virtual void ShowPortrait(Character character, string portrait) { PortraitOptions options = new PortraitOptions(true); options.character = character; @@ -550,11 +434,7 @@ namespace Fungus Show(options); } - /// - /// Simple character hide command - /// - /// Character to hide - public void Hide(Character character) + public virtual void Hide(Character character) { PortraitOptions options = new PortraitOptions(true); options.character = character; @@ -562,12 +442,7 @@ namespace Fungus Hide(options); } - /// - /// Move the character to a position then hide it - /// - /// - /// Where the character will disapear to - public void Hide(Character character, string toPosition) + public virtual void Hide(Character character, string toPosition) { PortraitOptions options = new PortraitOptions(true); options.character = character; @@ -577,23 +452,12 @@ namespace Fungus Hide(options); } - /// - /// From lua, you can pass an options table with named arguments - /// example: - /// stage.hide{character=jill, toPosition="left"} - /// Any option available in the PortraitOptions is available from Lua - /// - /// Moonsharp Table - public void Hide(Table optionsTable) + public virtual void Hide(Table optionsTable) { Hide(PortraitUtil.ConvertTableToPortraitOptions(optionsTable, stage)); } - /// - /// Hide portrait with provided options - /// - /// - public void Hide(PortraitOptions options) + public virtual void Hide(PortraitOptions options) { CleanPortraitOptions(options); @@ -623,7 +487,7 @@ namespace Fungus FinishCommand(options); } - public void SetDimmed(Character character, bool dimmedState) + public virtual void SetDimmed(Character character, bool dimmedState) { if (character.State.dimmed == dimmedState) { @@ -639,96 +503,7 @@ namespace Fungus LeanTween.color(character.State.portraitImage.rectTransform, targetColor, duration).setEase(stage.FadeEaseType); } - } - - /// - /// Util functions that I wanted to keep the main class clean of - /// - public class PortraitUtil - { - /// - /// 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()) - { - options.facing = table.Get("facing").ToObject(); - } - - 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; - } + #endregion } } diff --git a/Assets/Fungus/Narrative/Scripts/PortraitUtils.cs b/Assets/Fungus/Narrative/Scripts/PortraitUtils.cs new file mode 100644 index 00000000..bea15496 --- /dev/null +++ b/Assets/Fungus/Narrative/Scripts/PortraitUtils.cs @@ -0,0 +1,156 @@ +using UnityEngine; +using UnityEngine.UI; +using MoonSharp.Interpreter; + +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 Sprite portrait; + public RectTransform position; + public FacingDirection facing; + public Image portraitImage; + } + + /// + /// Util functions for working with portraits. + /// + public static class PortraitUtil + { + /// + /// 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()) + { + options.facing = table.Get("facing").ToObject(); + } + + 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; + } + } +} diff --git a/Assets/Fungus/Narrative/Scripts/PortraitUtils.cs.meta b/Assets/Fungus/Narrative/Scripts/PortraitUtils.cs.meta new file mode 100644 index 00000000..3958fbc6 --- /dev/null +++ b/Assets/Fungus/Narrative/Scripts/PortraitUtils.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: fb7c101397bef4707a151a471d0acbdf +timeCreated: 1473686219 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: