diff --git a/Assets/Fungus/Camera/Scripts/CameraController.cs b/Assets/Fungus/Camera/Scripts/CameraController.cs index 61d938f9..23b35000 100644 --- a/Assets/Fungus/Camera/Scripts/CameraController.cs +++ b/Assets/Fungus/Camera/Scripts/CameraController.cs @@ -9,14 +9,10 @@ using Fungus; namespace Fungus { - /// - /// Controller for main camera.Supports several types of camera transition including snap, pan & fade. - /// - public class CameraController : MonoBehaviour + public class CameraController : MonoBehaviour, ICameraController { [Tooltip("Full screen texture used for screen fade effect.")] [SerializeField] protected Texture2D screenFadeTexture; - public Texture2D ScreenFadeTexture { set { screenFadeTexture = value; } } [Tooltip("Icon to display when swipe pan mode is active.")] [SerializeField] protected Texture2D swipePanIcon; @@ -52,13 +48,13 @@ namespace Fungus protected Dictionary storedViews = new Dictionary(); - protected static CameraController instance; + protected static ICameraController instance; /// /// Returns the CameraController singleton instance. /// Will create a CameraController game object if none currently exists. /// - static public CameraController GetInstance() + static public ICameraController GetInstance() { if (instance == null) { @@ -117,59 +113,13 @@ namespace Fungus } } - /// - /// Perform a fullscreen fade over a duration. - /// - public virtual void Fade(float targetAlpha, float fadeDuration, Action fadeAction) - { - StartCoroutine(FadeInternal(targetAlpha, fadeDuration, fadeAction)); - } - - /// - /// Fade out, move camera to view and then fade back in. - /// - public virtual void FadeToView(Camera camera, View view, float fadeDuration, bool fadeOut, Action fadeAction) - { - swipePanActive = false; - fadeAlpha = 0f; - - float outDuration; - float inDuration; - - if (fadeOut) - { - outDuration = fadeDuration / 2f; - inDuration = fadeDuration / 2f; - } - else - { - outDuration = 0; - inDuration = fadeDuration; - } - - // Fade out - Fade(1f, outDuration, delegate { - - // Snap to new view - PanToPosition(camera, view.transform.position, view.transform.rotation, view.ViewSize, 0f, null); - - // Fade in - Fade(0f, inDuration, delegate { - if (fadeAction != null) - { - fadeAction(); - } - }); - }); - } - protected virtual IEnumerator FadeInternal(float targetAlpha, float fadeDuration, Action fadeAction) { float startAlpha = fadeAlpha; float timer = 0; // If already at the target alpha then complete immediately - if (startAlpha == targetAlpha) + if (Mathf.Approximately(startAlpha, targetAlpha)) { yield return null; } @@ -195,138 +145,6 @@ namespace Fungus } } - /// - /// Positions camera so sprite is centered and fills the screen. - /// - public virtual void CenterOnSprite(Camera camera, SpriteRenderer spriteRenderer) - { - if (camera == null) - { - Debug.LogWarning("Camera is null"); - return; - } - - if (spriteRenderer == null) - { - Debug.LogWarning("Sprite renderer is null"); - return; - } - - swipePanActive = false; - - Sprite sprite = spriteRenderer.sprite; - Vector3 extents = sprite.bounds.extents; - float localScaleY = spriteRenderer.transform.localScale.y; - - camera.orthographicSize = extents.y * localScaleY; - Vector3 pos = spriteRenderer.transform.position; - camera.transform.position = new Vector3(pos.x, pos.y, 0); - - SetCameraZ(camera); - } - - public virtual void PanToView(Camera camera, View view, float duration, Action arriveAction) - { - PanToPosition(camera, view.transform.position, view.transform.rotation, view.ViewSize, duration, arriveAction); - } - - /// - /// Moves camera from current position to a target position over a period of time. - /// - public virtual void PanToPosition(Camera camera, Vector3 targetPosition, Quaternion targetRotation, float targetSize, float duration, Action arriveAction) - { - if (camera == null) - { - Debug.LogWarning("Camera is null"); - return; - } - - // Stop any pan that is currently active - StopAllCoroutines(); - - swipePanActive = false; - - if (duration == 0f) - { - // Move immediately - camera.orthographicSize = targetSize; - camera.transform.position = targetPosition; - camera.transform.rotation = targetRotation; - - SetCameraZ(camera); - - if (arriveAction != null) - { - arriveAction(); - } - } - else - { - StartCoroutine(PanInternal(camera, targetPosition, targetRotation, targetSize, duration, arriveAction)); - } - } - - /// - /// Stores the current camera view using a name. - /// - public virtual void StoreView(Camera camera, string viewName) - { - if (camera != null) - { - Debug.LogWarning("Camera is null"); - return; - } - - CameraView currentView = new CameraView(); - currentView.cameraPos = camera.transform.position; - currentView.cameraRot = camera.transform.rotation; - currentView.cameraSize = camera.orthographicSize; - storedViews[viewName] = currentView; - } - - /// - /// Moves the camera to a previously stored camera view over a period of time. - /// - public virtual void PanToStoredView(Camera camera, string viewName, float duration, Action arriveAction) - { - if (camera == null) - { - Debug.LogWarning("Camera is null"); - return; - } - - if (!storedViews.ContainsKey(viewName)) - { - // View has not previously been stored - if (arriveAction != null) - { - arriveAction(); - } - return; - } - - CameraView cameraView = storedViews[viewName]; - - if (duration == 0f) - { - // Move immediately - camera.transform.position = cameraView.cameraPos; - camera.transform.rotation = cameraView.cameraRot; - camera.orthographicSize = cameraView.cameraSize; - - SetCameraZ(camera); - - if (arriveAction != null) - { - arriveAction(); - } - } - else - { - StartCoroutine(PanInternal(camera, cameraView.cameraPos, cameraView.cameraRot, cameraView.cameraSize, duration, arriveAction)); - } - } - protected virtual IEnumerator PanInternal(Camera camera, Vector3 targetPos, Quaternion targetRot, float targetSize, float duration, Action arriveAction) { if (camera == null) @@ -446,49 +264,6 @@ namespace Fungus } } - /// - /// Activates swipe panning mode. The player can pan the camera within the area between viewA & viewB. - /// - public virtual void StartSwipePan(Camera camera, View viewA, View viewB, float duration, float speedMultiplier, Action arriveAction) - { - if (camera == null) - { - Debug.LogWarning("Camera is null"); - return; - } - - swipePanViewA = viewA; - swipePanViewB = viewB; - swipeSpeedMultiplier = speedMultiplier; - - Vector3 cameraPos = camera.transform.position; - - Vector3 targetPosition = CalcCameraPosition(cameraPos, swipePanViewA, swipePanViewB); - float targetSize = CalcCameraSize(cameraPos, swipePanViewA, swipePanViewB); - - PanToPosition(camera, targetPosition, Quaternion.identity, targetSize, duration, delegate { - - swipePanActive = true; - swipeCamera = camera; - - if (arriveAction != null) - { - arriveAction(); - } - }); - } - - /// - /// Deactivates swipe panning mode. - /// - public virtual void StopSwipePan() - { - swipePanActive = false; - swipePanViewA = null; - swipePanViewB = null; - swipeCamera = null; - } - protected virtual void SetCameraZ(Camera camera) { if (!setCameraZ) @@ -585,5 +360,126 @@ namespace Fungus return cameraSize; } + + #region ICameraController implementation + + public Texture2D ScreenFadeTexture { set { screenFadeTexture = value; } } + + public virtual void Fade(float targetAlpha, float fadeDuration, Action fadeAction) + { + StartCoroutine(FadeInternal(targetAlpha, fadeDuration, fadeAction)); + } + + public virtual void FadeToView(Camera camera, View view, float fadeDuration, bool fadeOut, Action fadeAction) + { + swipePanActive = false; + fadeAlpha = 0f; + + float outDuration; + float inDuration; + + if (fadeOut) + { + outDuration = fadeDuration / 2f; + inDuration = fadeDuration / 2f; + } + else + { + outDuration = 0; + inDuration = fadeDuration; + } + + // Fade out + Fade(1f, outDuration, delegate { + + // Snap to new view + PanToPosition(camera, view.transform.position, view.transform.rotation, view.ViewSize, 0f, null); + + // Fade in + Fade(0f, inDuration, delegate { + if (fadeAction != null) + { + fadeAction(); + } + }); + }); + } + + public virtual void Stop() + { + StopAllCoroutines(); + } + + public virtual void PanToPosition(Camera camera, Vector3 targetPosition, Quaternion targetRotation, float targetSize, float duration, Action arriveAction) + { + if (camera == null) + { + Debug.LogWarning("Camera is null"); + return; + } + + // Stop any pan that is currently active + StopAllCoroutines(); + + swipePanActive = false; + + if (duration == 0f) + { + // Move immediately + camera.orthographicSize = targetSize; + camera.transform.position = targetPosition; + camera.transform.rotation = targetRotation; + + SetCameraZ(camera); + + if (arriveAction != null) + { + arriveAction(); + } + } + else + { + StartCoroutine(PanInternal(camera, targetPosition, targetRotation, targetSize, duration, arriveAction)); + } + } + + public virtual void StartSwipePan(Camera camera, View viewA, View viewB, float duration, float speedMultiplier, Action arriveAction) + { + if (camera == null) + { + Debug.LogWarning("Camera is null"); + return; + } + + swipePanViewA = viewA; + swipePanViewB = viewB; + swipeSpeedMultiplier = speedMultiplier; + + Vector3 cameraPos = camera.transform.position; + + Vector3 targetPosition = CalcCameraPosition(cameraPos, swipePanViewA, swipePanViewB); + float targetSize = CalcCameraSize(cameraPos, swipePanViewA, swipePanViewB); + + PanToPosition(camera, targetPosition, Quaternion.identity, targetSize, duration, delegate { + + swipePanActive = true; + swipeCamera = camera; + + if (arriveAction != null) + { + arriveAction(); + } + }); + } + + public virtual void StopSwipePan() + { + swipePanActive = false; + swipePanViewA = null; + swipePanViewB = null; + swipeCamera = null; + } + + #endregion } } \ No newline at end of file diff --git a/Assets/Fungus/Camera/Scripts/Commands/FadeScreen.cs b/Assets/Fungus/Camera/Scripts/Commands/FadeScreen.cs index 8bd99102..a0316755 100644 --- a/Assets/Fungus/Camera/Scripts/Commands/FadeScreen.cs +++ b/Assets/Fungus/Camera/Scripts/Commands/FadeScreen.cs @@ -33,7 +33,7 @@ namespace Fungus public override void OnEnter() { - CameraController cameraController = CameraController.GetInstance(); + ICameraController cameraController = CameraController.GetInstance(); if (fadeTexture) { diff --git a/Assets/Fungus/Camera/Scripts/Commands/FadeToView.cs b/Assets/Fungus/Camera/Scripts/Commands/FadeToView.cs index 45beb717..6273d0f2 100644 --- a/Assets/Fungus/Camera/Scripts/Commands/FadeToView.cs +++ b/Assets/Fungus/Camera/Scripts/Commands/FadeToView.cs @@ -65,7 +65,7 @@ namespace Fungus return; } - CameraController cameraController = CameraController.GetInstance(); + ICameraController cameraController = CameraController.GetInstance(); if (fadeTexture) { @@ -91,7 +91,7 @@ namespace Fungus public override void OnStopExecuting() { - CameraController.GetInstance().StopAllCoroutines(); + CameraController.GetInstance().Stop(); } public override string GetSummary() diff --git a/Assets/Fungus/Camera/Scripts/Commands/MoveToView.cs b/Assets/Fungus/Camera/Scripts/Commands/MoveToView.cs index 810e8025..7649f62b 100644 --- a/Assets/Fungus/Camera/Scripts/Commands/MoveToView.cs +++ b/Assets/Fungus/Camera/Scripts/Commands/MoveToView.cs @@ -56,7 +56,7 @@ namespace Fungus return; } - CameraController cameraController = CameraController.GetInstance(); + ICameraController cameraController = CameraController.GetInstance(); Vector3 targetPosition = targetView.transform.position; Quaternion targetRotation = targetView.transform.rotation; @@ -77,7 +77,7 @@ namespace Fungus public override void OnStopExecuting() { - CameraController.GetInstance().StopAllCoroutines(); + CameraController.GetInstance().Stop(); } public override string GetSummary() diff --git a/Assets/Fungus/Camera/Scripts/Commands/StartSwipe.cs b/Assets/Fungus/Camera/Scripts/Commands/StartSwipe.cs index e7fea394..57a76291 100644 --- a/Assets/Fungus/Camera/Scripts/Commands/StartSwipe.cs +++ b/Assets/Fungus/Camera/Scripts/Commands/StartSwipe.cs @@ -51,7 +51,7 @@ namespace Fungus return; } - CameraController cameraController = CameraController.GetInstance(); + ICameraController cameraController = CameraController.GetInstance(); cameraController.StartSwipePan(targetCamera, viewA, viewB, duration, speedMultiplier, () => Continue() ); } diff --git a/Assets/Fungus/Camera/Scripts/Commands/StopSwipe.cs b/Assets/Fungus/Camera/Scripts/Commands/StopSwipe.cs index 0551d3bf..bf685ce6 100644 --- a/Assets/Fungus/Camera/Scripts/Commands/StopSwipe.cs +++ b/Assets/Fungus/Camera/Scripts/Commands/StopSwipe.cs @@ -16,7 +16,7 @@ namespace Fungus { public override void OnEnter() { - CameraController cameraController = CameraController.GetInstance(); + ICameraController cameraController = CameraController.GetInstance(); cameraController.StopSwipePan(); diff --git a/Assets/Fungus/Camera/Scripts/ICameraController.cs b/Assets/Fungus/Camera/Scripts/ICameraController.cs new file mode 100644 index 00000000..636c5da9 --- /dev/null +++ b/Assets/Fungus/Camera/Scripts/ICameraController.cs @@ -0,0 +1,46 @@ +using UnityEngine; + +namespace Fungus +{ + /// + /// Controller for main camera.Supports several types of camera transition including snap, pan & fade. + /// + public interface ICameraController + { + /// + /// Full screen texture used for screen fade effect. + /// + /// The screen fade texture. + Texture2D ScreenFadeTexture { set; } + + /// + /// Perform a fullscreen fade over a duration. + /// + void Fade(float targetAlpha, float fadeDuration, System.Action fadeAction); + + /// + /// Fade out, move camera to view and then fade back in. + /// + void FadeToView(Camera camera, View view, float fadeDuration, bool fadeOut, System.Action fadeAction); + + /// + /// Stop all camera tweening. + /// + void Stop(); + + /// + /// Moves camera from current position to a target position over a period of time. + /// + void PanToPosition(Camera camera, Vector3 targetPosition, Quaternion targetRotation, float targetSize, float duration, System.Action arriveAction); + + /// + /// Activates swipe panning mode. The player can pan the camera within the area between viewA & viewB. + /// + void StartSwipePan(Camera camera, View viewA, View viewB, float duration, float speedMultiplier, System.Action arriveAction); + + /// + /// Deactivates swipe panning mode. + /// + void StopSwipePan(); + } +} \ No newline at end of file diff --git a/Assets/Fungus/Camera/Scripts/ICameraController.cs.meta b/Assets/Fungus/Camera/Scripts/ICameraController.cs.meta new file mode 100644 index 00000000..43f258f6 --- /dev/null +++ b/Assets/Fungus/Camera/Scripts/ICameraController.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 182888cc7e1a0470c81e9ea6966d6fc6 +timeCreated: 1473431064 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Fungus/UI/Scripts/Writer.cs b/Assets/Fungus/UI/Scripts/Writer.cs index b166d546..e967bfa8 100644 --- a/Assets/Fungus/UI/Scripts/Writer.cs +++ b/Assets/Fungus/UI/Scripts/Writer.cs @@ -827,7 +827,7 @@ namespace Fungus protected virtual void Flash(float duration) { - CameraController cameraController = CameraController.GetInstance(); + ICameraController cameraController = CameraController.GetInstance(); cameraController.ScreenFadeTexture = CameraController.CreateColorTexture(new Color(1f,1f,1f,1f), 32, 32); cameraController.Fade(1f, duration, delegate { cameraController.ScreenFadeTexture = CameraController.CreateColorTexture(new Color(1f,1f,1f,1f), 32, 32);