From d6cb967e15e0dfacf90ae9ba4432ae9074229556 Mon Sep 17 00:00:00 2001 From: Christopher Date: Mon, 12 Sep 2016 15:32:00 +0100 Subject: [PATCH] Refactored Stage to use IStage interface --- Assets/Fungus/Narrative/Scripts/IStage.cs | 63 ++++++++++++++ .../Fungus/Narrative/Scripts/IStage.cs.meta | 12 +++ .../Narrative/Scripts/PortraitController.cs | 1 - Assets/Fungus/Narrative/Scripts/Stage.cs | 83 ++++++++++--------- 4 files changed, 120 insertions(+), 39 deletions(-) create mode 100644 Assets/Fungus/Narrative/Scripts/IStage.cs create mode 100644 Assets/Fungus/Narrative/Scripts/IStage.cs.meta diff --git a/Assets/Fungus/Narrative/Scripts/IStage.cs b/Assets/Fungus/Narrative/Scripts/IStage.cs new file mode 100644 index 00000000..e9e689b8 --- /dev/null +++ b/Assets/Fungus/Narrative/Scripts/IStage.cs @@ -0,0 +1,63 @@ +using UnityEngine; +using UnityEngine.UI; +using System.Collections.Generic; + +namespace Fungus +{ + /// + /// Define a set of screen positions where character sprites can be displayed. + /// + public interface IStage + { + /// + /// Canvas object containing the stage positions. + /// + Canvas PortraitCanvas { get; } + + /// + /// Dim portraits when a character is not speaking. + /// + bool DimPortraits { get; set; } + + /// + /// Duration for fading character portraits in / out. + /// + float FadeDuration { get; set; } + + /// + /// Duration for moving characters to a new position. + /// + float MoveDuration { get; set; } + + /// + /// Ease type for the fade tween. + /// + LeanTweenType FadeEaseType { get; } + + /// + /// Constant offset to apply to portrait position. + /// + Vector2 ShiftOffset { get; } + + /// + /// The position object where characters appear by default. + /// + Image DefaultPosition { get; } + + /// + /// List of stage position rect transforms in the stage. + /// + List Positions { get; } + + /// + /// List of currently active characters on the stage. + /// + List CharactersOnStage { get; } + + /// + /// Searches the stage's named positions + /// If none matches the string provided, give a warning and return a new RectTransform + /// + RectTransform GetPosition(string positionString); + } +} \ No newline at end of file diff --git a/Assets/Fungus/Narrative/Scripts/IStage.cs.meta b/Assets/Fungus/Narrative/Scripts/IStage.cs.meta new file mode 100644 index 00000000..55f45506 --- /dev/null +++ b/Assets/Fungus/Narrative/Scripts/IStage.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: a88bde623f95a46e49febd46fe50b19c +timeCreated: 1473689190 +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 92f1a60f..e723ef03 100644 --- a/Assets/Fungus/Narrative/Scripts/PortraitController.cs +++ b/Assets/Fungus/Narrative/Scripts/PortraitController.cs @@ -22,7 +22,6 @@ namespace Fungus protected virtual void Awake() { stage = GetComponentInParent(); - stage.CachePositions(); } protected virtual void FinishCommand(PortraitOptions options) diff --git a/Assets/Fungus/Narrative/Scripts/Stage.cs b/Assets/Fungus/Narrative/Scripts/Stage.cs index 55e2e938..fe796a82 100644 --- a/Assets/Fungus/Narrative/Scripts/Stage.cs +++ b/Assets/Fungus/Narrative/Scripts/Stage.cs @@ -12,36 +12,33 @@ namespace Fungus /// Define a set of screen positions where character sprites can be displayed. /// [ExecuteInEditMode] - public class Stage : PortraitController + public class Stage : PortraitController, IStage { + [Tooltip("Canvas object containing the stage positions.")] [SerializeField] protected Canvas portraitCanvas; - public virtual Canvas PortraitCanvas { get { return portraitCanvas; } } + [Tooltip("Dim portraits when a character is not speaking.")] [SerializeField] protected bool dimPortraits; - public virtual bool DimPortraits { get { return dimPortraits; } set { dimPortraits = value; } } + [Tooltip("Duration for fading character portraits in / out.")] [SerializeField] protected float fadeDuration = 0.5f; - public virtual float FadeDuration { get { return fadeDuration; } set { fadeDuration = value; } } + [Tooltip("Duration for moving characters to a new position")] [SerializeField] protected float moveDuration = 1f; - public virtual float MoveDuration { get { return moveDuration; } set { moveDuration = value; } } + [Tooltip("Ease type for the fade tween.")] [SerializeField] protected LeanTweenType fadeEaseType; - public virtual LeanTweenType FadeEaseType { get { return fadeEaseType; } } + [Tooltip("Constant offset to apply to portrait position.")] [SerializeField] protected Vector2 shiftOffset; - public virtual Vector2 ShiftOffset { get { return shiftOffset; } } + [Tooltip("The position object where characters appear by default.")] [SerializeField] protected Image defaultPosition; - public virtual Image DefaultPosition { get { return defaultPosition; } } + [Tooltip("List of stage position rect transforms in the stage.")] [SerializeField] protected List positions; - public virtual List Positions { get { return positions; } } - - [SerializeField] protected RectTransform[] cachedPositions; protected List charactersOnStage = new List(); - public virtual List CharactersOnStage { get { return charactersOnStage; } } static public List activeStages = new List(); @@ -53,12 +50,6 @@ namespace Fungus } } - public void CachePositions() - { - cachedPositions = new RectTransform[positions.Count]; - positions.CopyTo(cachedPositions); - } - protected virtual void OnDisable() { activeStages.Remove(this); @@ -74,39 +65,55 @@ namespace Fungus } } - /// - /// Searches the stage's named positions - /// If none matches the string provided, give a warning and return a new RectTransform - /// - /// Position name to search for - /// - public RectTransform GetPosition(String position_string) + public static Stage GetActiveStage() { - if (string.IsNullOrEmpty(position_string)) + if (Stage.activeStages == null || + Stage.activeStages.Count == 0) { return null; } - for (int i = 0; i < cachedPositions.Length; i++) - { - if ( String.Compare(cachedPositions[i].name, position_string, true) == 0 ) - { - return cachedPositions[i]; - } - } - return null; + return Stage.activeStages[0]; } - public static Stage GetActiveStage() + #region IStage implementation + + public virtual Canvas PortraitCanvas { get { return portraitCanvas; } } + + public virtual bool DimPortraits { get { return dimPortraits; } set { dimPortraits = value; } } + + public virtual float FadeDuration { get { return fadeDuration; } set { fadeDuration = value; } } + + public virtual float MoveDuration { get { return moveDuration; } set { moveDuration = value; } } + + public virtual LeanTweenType FadeEaseType { get { return fadeEaseType; } } + + public virtual Vector2 ShiftOffset { get { return shiftOffset; } } + + public virtual Image DefaultPosition { get { return defaultPosition; } } + + public virtual List Positions { get { return positions; } } + + public virtual List CharactersOnStage { get { return charactersOnStage; } } + + public RectTransform GetPosition(string positionString) { - if (Stage.activeStages == null || - Stage.activeStages.Count == 0) + if (string.IsNullOrEmpty(positionString)) { return null; } - return Stage.activeStages[0]; + for (int i = 0; i < positions.Count; i++) + { + if ( String.Compare(positions[i].name, positionString, true) == 0 ) + { + return positions[i]; + } + } + return null; } + + #endregion } }