Browse Source

CameraManager and View related Commands now use LeanTweenTypes

Closes #556
master
Steve Halliwell 5 years ago
parent
commit
1a1b0d816a
  1. 4
      Assets/Fungus/Scripts/Commands/FadeScreen.cs
  2. 7
      Assets/Fungus/Scripts/Commands/FadeToView.cs
  3. 8
      Assets/Fungus/Scripts/Commands/MoveToView.cs
  4. 259
      Assets/Fungus/Scripts/Components/CameraManager.cs
  5. 6613
      Assets/FungusExamples/FungusTown/FungusTown.unity

4
Assets/Fungus/Scripts/Commands/FadeScreen.cs

@ -31,6 +31,8 @@ namespace Fungus
[Tooltip("Optional texture to use when rendering the fullscreen fade effect.")]
[SerializeField] protected Texture2D fadeTexture;
[SerializeField] protected LeanTweenType fadeTweenType = LeanTweenType.easeInOutQuad;
#region Public members
public override void OnEnter()
@ -51,7 +53,7 @@ namespace Fungus
{
Continue();
}
});
}, fadeTweenType);
if (!waitUntilFinished)
{

7
Assets/Fungus/Scripts/Commands/FadeToView.cs

@ -35,6 +35,11 @@ namespace Fungus
[Tooltip("Camera to use for the fade. Will use main camera if set to none.")]
[SerializeField] protected Camera targetCamera;
[SerializeField] protected LeanTweenType fadeTweenType = LeanTweenType.easeInOutQuad;
[SerializeField] protected LeanTweenType orthoSizeTweenType = LeanTweenType.easeInOutQuad;
[SerializeField] protected LeanTweenType posTweenType = LeanTweenType.easeInOutQuad;
[SerializeField] protected LeanTweenType rotTweenType = LeanTweenType.easeInOutQuad;
protected virtual void Start()
{
AcquireCamera();
@ -87,7 +92,7 @@ namespace Fungus
{
Continue();
}
});
}, fadeTweenType, orthoSizeTweenType, posTweenType, rotTweenType);
if (!waitUntilFinished)
{

8
Assets/Fungus/Scripts/Commands/MoveToView.cs

@ -26,7 +26,11 @@ namespace Fungus
[Tooltip("Camera to use for the pan. Will use main camera if set to none.")]
[SerializeField] protected Camera targetCamera;
[SerializeField] protected LeanTweenType orthoSizeTweenType = LeanTweenType.easeInOutQuad;
[SerializeField] protected LeanTweenType posTweenType = LeanTweenType.easeInOutQuad;
[SerializeField] protected LeanTweenType rotTweenType = LeanTweenType.easeInOutQuad;
protected virtual void AcquireCamera()
{
if (targetCamera != null)
@ -69,7 +73,7 @@ namespace Fungus
{
Continue();
}
});
}, orthoSizeTweenType, posTweenType, rotTweenType);
if (!waitUntilFinished)
{

259
Assets/Fungus/Scripts/Components/CameraManager.cs

@ -43,8 +43,7 @@ namespace Fungus
protected Vector3 previousMousePos;
//Coroutine handles for panning and fading commands
protected IEnumerator panCoroutine;
protected IEnumerator fadeCoroutine;
protected LTDescr fadeTween, sizeTween, camPosTween, camRotTween;
protected class CameraView
{
@ -89,122 +88,6 @@ namespace Fungus
}
}
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 (Mathf.Approximately(startAlpha, targetAlpha))
{
yield return null;
}
else
{
while (timer < fadeDuration)
{
float t = timer / fadeDuration;
timer += Time.deltaTime;
t = Mathf.Clamp01(t);
fadeAlpha = Mathf.Lerp(startAlpha, targetAlpha, t);
yield return null;
}
}
fadeAlpha = targetAlpha;
if (fadeAction != null)
{
fadeAction();
}
}
protected virtual IEnumerator PanInternal(Camera camera, Vector3 targetPos, Quaternion targetRot, float targetSize, float duration, Action arriveAction)
{
if (camera == null)
{
Debug.LogWarning("Camera is null");
yield break;
}
float timer = 0;
float startSize = camera.orthographicSize;
float endSize = targetSize;
Vector3 startPos = camera.transform.position;
Vector3 endPos = targetPos;
Quaternion startRot = camera.transform.rotation;
Quaternion endRot = targetRot;
bool arrived = false;
while (!arrived)
{
timer += Time.deltaTime;
if (timer > duration)
{
arrived = true;
timer = duration;
}
// Apply smoothed lerp to camera position and orthographic size
float t = 1f;
if (duration > 0f)
{
t = timer / duration;
}
if (camera != null)
{
camera.orthographicSize = Mathf.Lerp(startSize, endSize, Mathf.SmoothStep(0f, 1f, t));
camera.transform.position = Vector3.Lerp(startPos, endPos, Mathf.SmoothStep(0f, 1f, t));
camera.transform.rotation = Quaternion.Lerp(startRot, endRot, Mathf.SmoothStep(0f, 1f, t));
SetCameraZ(camera);
}
if (arrived &&
arriveAction != null)
{
arriveAction();
}
yield return null;
}
}
protected virtual IEnumerator PanToPathInternal(Camera camera, float duration, Action arriveAction, Vector3[] path)
{
if (camera == null)
{
Debug.LogWarning("Camera is null");
yield break;
}
float timer = 0;
while (timer < duration)
{
timer += Time.deltaTime;
timer = Mathf.Min(timer, duration);
float percent = timer / duration;
Vector3 point = iTween.PointOnPath(path, percent);
camera.transform.position = new Vector3(point.x, point.y, 0);
camera.orthographicSize = point.z;
SetCameraZ(camera);
yield return null;
}
if (arriveAction != null)
{
arriveAction();
}
}
protected virtual void SetCameraZ(Camera camera)
{
if (!setCameraZ)
@ -303,41 +186,7 @@ namespace Fungus
}
#region Public members
/// <summary>
/// Moves camera smoothly through a sequence of Views over a period of time.
/// </summary>
public virtual void PanToPath(Camera camera, View[] viewList, float duration, Action arriveAction)
{
if (camera == null)
{
Debug.LogWarning("Camera is null");
return;
}
swipePanActive = false;
List<Vector3> pathList = new List<Vector3>();
// Add current camera position as first point in path
// Note: We use the z coord to tween the camera orthographic size
Vector3 startPos = new Vector3(camera.transform.position.x,
camera.transform.position.y,
camera.orthographicSize);
pathList.Add(startPos);
for (int i = 0; i < viewList.Length; ++i)
{
View view = viewList[i];
Vector3 viewPos = new Vector3(view.transform.position.x,
view.transform.position.y,
view.ViewSize);
pathList.Add(viewPos);
}
StartCoroutine(panCoroutine = PanToPathInternal (camera, duration, arriveAction, pathList.ToArray ()));
}
/// <summary>
/// Creates a flat colored texture.
/// </summary>
@ -364,15 +213,35 @@ namespace Fungus
/// <summary>
/// Perform a fullscreen fade over a duration.
/// </summary>
public virtual void Fade(float targetAlpha, float fadeDuration, Action fadeAction)
public virtual void Fade(float targetAlpha, float fadeDuration, Action fadeAction, LeanTweenType leanTweenType = LeanTweenType.easeInOutQuad)
{
StartCoroutine(fadeCoroutine = FadeInternal (targetAlpha, fadeDuration, fadeAction));
StopFadeTween();
if (Mathf.Approximately(fadeDuration, 0))
{
fadeAlpha = targetAlpha;
if (fadeAction != null) fadeAction();
}
else
{
fadeTween = LeanTween.value(fadeAlpha, targetAlpha, fadeDuration)
.setEase(leanTweenType)
.setOnUpdate((x) => fadeAlpha = x)
.setOnComplete(() =>
{
fadeAlpha = targetAlpha;
if (fadeAction != null) fadeAction();
fadeTween = null;
});
}
}
/// <summary>
/// Fade out, move camera to view and then fade back in.
/// </summary>
public virtual void FadeToView(Camera camera, View view, float fadeDuration, bool fadeOut, Action fadeAction)
public virtual void FadeToView(Camera camera, View view, float fadeDuration, bool fadeOut, Action fadeAction,
LeanTweenType fadeType = LeanTweenType.easeInOutQuad, LeanTweenType sizeTweenType = LeanTweenType.easeInOutQuad,
LeanTweenType posTweenType = LeanTweenType.easeInOutQuad, LeanTweenType rotTweenType = LeanTweenType.easeInOutQuad)
{
swipePanActive = false;
fadeAlpha = 0f;
@ -395,7 +264,7 @@ namespace Fungus
Fade(1f, outDuration, delegate {
// Snap to new view
PanToPosition(camera, view.transform.position, view.transform.rotation, view.ViewSize, 0f, null);
PanToPosition(camera, view.transform.position, view.transform.rotation, view.ViewSize, 0f, null, sizeTweenType, posTweenType, rotTweenType);
// Fade in
Fade(0f, inDuration, delegate {
@ -403,8 +272,8 @@ namespace Fungus
{
fadeAction();
}
});
});
}, fadeType);
}, fadeType);
}
/// <summary>
@ -412,15 +281,43 @@ namespace Fungus
/// </summary>
public virtual void Stop()
{
StopAllCoroutines();
panCoroutine = null;
fadeCoroutine = null;
StopFadeTween();
StopPosTweens();
}
protected void StopFadeTween()
{
if (fadeTween != null)
{
LeanTween.cancel(fadeTween.id, true);
fadeTween = null;
}
}
protected void StopPosTweens()
{
if (sizeTween != null)
{
LeanTween.cancel(sizeTween.id, true);
sizeTween = null;
}
if (camPosTween != null)
{
LeanTween.cancel(camPosTween.id, true);
camPosTween = null;
}
if (camRotTween != null)
{
LeanTween.cancel(camRotTween.id, true);
camRotTween = null;
}
}
/// <summary>
/// Moves camera from current position to a target position over a period of time.
/// </summary>
public virtual void PanToPosition(Camera camera, Vector3 targetPosition, Quaternion targetRotation, float targetSize, float duration, Action arriveAction)
public virtual void PanToPosition(Camera camera, Vector3 targetPosition, Quaternion targetRotation, float targetSize, float duration, Action arriveAction,
LeanTweenType sizeTweenType = LeanTweenType.easeInOutQuad, LeanTweenType posTweenType = LeanTweenType.easeInOutQuad, LeanTweenType rotTweenType = LeanTweenType.easeInOutQuad)
{
if (camera == null)
{
@ -428,11 +325,13 @@ namespace Fungus
return;
}
if(setCameraZ)
{
targetPosition.z = camera.transform.position.z;
}
// Stop any pan that is currently active
if (panCoroutine != null) {
StopCoroutine(panCoroutine);
panCoroutine = null;
}
StopPosTweens();
swipePanActive = false;
if (Mathf.Approximately(duration, 0f))
@ -451,7 +350,31 @@ namespace Fungus
}
else
{
StartCoroutine(panCoroutine = PanInternal(camera, targetPosition, targetRotation, targetSize, duration, arriveAction));
sizeTween = LeanTween.value(camera.orthographicSize, targetSize, duration)
.setEase(sizeTweenType)
.setOnUpdate(x => camera.orthographicSize = x)
.setOnComplete(() =>
{
camera.orthographicSize = targetSize;
if (arriveAction != null) arriveAction();
sizeTween = null;
});
camPosTween = LeanTween.move(camera.gameObject, targetPosition, duration)
.setEase(posTweenType)
.setOnComplete(() =>
{
camera.transform.position = targetPosition;
camPosTween = null;
});
camRotTween = LeanTween.rotate(camera.gameObject, targetRotation.eulerAngles, duration)
.setEase(rotTweenType)
.setOnComplete(() =>
{
camera.transform.rotation = targetRotation;
camRotTween = null;
});
}
}

6613
Assets/FungusExamples/FungusTown/FungusTown.unity

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save