Browse Source

Added rotation support to Views.

master
chrisgregan 11 years ago
parent
commit
ed1e48b2e7
  1. 61
      Assets/Fungus/Editor/ViewEditor.cs
  2. 25
      Assets/Fungus/Scripts/CameraController.cs
  3. 8
      Assets/Fungus/Scripts/Commands/CameraCommands.cs
  4. 12
      Assets/Fungus/Scripts/GameController.cs
  5. 2
      Assets/Fungus/Scripts/Room.cs
  6. 8
      Assets/Fungus/Scripts/View.cs
  7. BIN
      Assets/FungusExample/Scenes/Example.unity
  8. 9
      Assets/FungusExample/Scripts/ViewRoom.cs

61
Assets/Fungus/Editor/ViewEditor.cs

@ -8,6 +8,7 @@ using Fungus;
[CustomEditor (typeof(View))] [CustomEditor (typeof(View))]
public class ViewEditor : Editor public class ViewEditor : Editor
{ {
// Draw Views when they're not selected
[DrawGizmo(GizmoType.NotSelected)] [DrawGizmo(GizmoType.NotSelected)]
static void RenderCustomGizmo(Transform objectTransform, GizmoType gizmoType) static void RenderCustomGizmo(Transform objectTransform, GizmoType gizmoType)
{ {
@ -34,22 +35,19 @@ public class ViewEditor : Editor
void EditViewBounds() void EditViewBounds()
{ {
View t = target as View; View view = target as View;
DrawView(t);
Vector3 pos = t.transform.position; DrawView(view);
float viewSize = t.viewSize;
Vector3 newViewPos = Handles.PositionHandle(pos, Quaternion.identity); Vector3 pos = view.transform.position;
t.transform.position = newViewPos; float viewSize = CalculateLocalViewSize(view);
Vector3[] handles = new Vector3[2]; Vector3[] handles = new Vector3[2];
handles[0] = pos + new Vector3(0, -viewSize, 0); handles[0] = view.transform.TransformPoint(new Vector3(0, -viewSize, 0));
handles[1] = pos + new Vector3(0, viewSize, 0); handles[1] = view.transform.TransformPoint(new Vector3(0, viewSize, 0));
Handles.color = t.primaryColor; Handles.color = view.primaryColor;
for (int i = 0; i < 2; ++i) for (int i = 0; i < 2; ++i)
{ {
@ -60,8 +58,8 @@ public class ViewEditor : Editor
Handles.CubeCap); Handles.CubeCap);
if (newPos != handles[i]) if (newPos != handles[i])
{ {
Undo.RecordObject(t, "Changed view size"); Undo.RecordObject(view, "Changed view size");
t.viewSize = Mathf.Abs(newPos.y - pos.y); view.viewSize = (newPos - pos).magnitude;
break; break;
} }
} }
@ -69,21 +67,17 @@ public class ViewEditor : Editor
public static void DrawView(View view) public static void DrawView(View view)
{ {
Vector3 pos = view.transform.position; float height = CalculateLocalViewSize(view);
float viewSize = view.viewSize;
float height = viewSize;
float widthA = height * view.primaryAspectRatio; float widthA = height * view.primaryAspectRatio;
float widthB = height * view.secondaryAspectRatio; float widthB = height * view.secondaryAspectRatio;
// Draw left box // Draw left box
{ {
Vector3[] verts = new Vector3[4]; Vector3[] verts = new Vector3[4];
verts[0] = pos + new Vector3(-widthB, -height, 0); verts[0] = view.transform.TransformPoint(new Vector3(-widthB, -height, 0));
verts[1] = pos + new Vector3(-widthB, height, 0); verts[1] = view.transform.TransformPoint(new Vector3(-widthB, height, 0));
verts[2] = pos + new Vector3(-widthA, height, 0); verts[2] = view.transform.TransformPoint(new Vector3(-widthA, height, 0));
verts[3] = pos + new Vector3(-widthA, -height, 0); verts[3] = view.transform.TransformPoint(new Vector3(-widthA, -height, 0));
Handles.DrawSolidRectangleWithOutline(verts, view.secondaryColor, view.primaryColor ); Handles.DrawSolidRectangleWithOutline(verts, view.secondaryColor, view.primaryColor );
} }
@ -91,10 +85,10 @@ public class ViewEditor : Editor
// Draw right box // Draw right box
{ {
Vector3[] verts = new Vector3[4]; Vector3[] verts = new Vector3[4];
verts[0] = pos + new Vector3(widthA, -height, 0); verts[0] = view.transform.TransformPoint(new Vector3(widthA, -height, 0));
verts[1] = pos + new Vector3(widthA, height, 0); verts[1] = view.transform.TransformPoint(new Vector3(widthA, height, 0));
verts[2] = pos + new Vector3(widthB, height, 0); verts[2] = view.transform.TransformPoint(new Vector3(widthB, height, 0));
verts[3] = pos + new Vector3(widthB, -height, 0); verts[3] = view.transform.TransformPoint(new Vector3(widthB, -height, 0));
Handles.DrawSolidRectangleWithOutline(verts, view.secondaryColor, view.primaryColor ); Handles.DrawSolidRectangleWithOutline(verts, view.secondaryColor, view.primaryColor );
} }
@ -102,12 +96,19 @@ public class ViewEditor : Editor
// Draw center box // Draw center box
{ {
Vector3[] verts = new Vector3[4]; Vector3[] verts = new Vector3[4];
verts[0] = pos + new Vector3(-widthA, -height, 0); verts[0] = view.transform.TransformPoint(new Vector3(-widthA, -height, 0));
verts[1] = pos + new Vector3(-widthA, height, 0); verts[1] = view.transform.TransformPoint(new Vector3(-widthA, height, 0));
verts[2] = pos + new Vector3(widthA, height, 0); verts[2] = view.transform.TransformPoint(new Vector3(widthA, height, 0));
verts[3] = pos + new Vector3(widthA, -height, 0); verts[3] = view.transform.TransformPoint(new Vector3(widthA, -height, 0));
Handles.DrawSolidRectangleWithOutline(verts, new Color(1,1,1,0f), view.primaryColor ); Handles.DrawSolidRectangleWithOutline(verts, new Color(1,1,1,0f), view.primaryColor );
} }
} }
// Calculate view size in local coordinates
// Kinda expensive, but accurate and only called in editor.
static float CalculateLocalViewSize(View view)
{
return view.transform.InverseTransformPoint(view.transform.position + new Vector3(0, view.viewSize, 0)).magnitude;
}
} }

25
Assets/Fungus/Scripts/CameraController.cs

@ -46,6 +46,7 @@ namespace Fungus
class CameraView class CameraView
{ {
public Vector3 cameraPos; public Vector3 cameraPos;
public Quaternion cameraRot;
public float cameraSize; public float cameraSize;
}; };
@ -84,11 +85,17 @@ namespace Fungus
} }
} }
/**
* Perform a fullscreen fade over a duration.
*/
public void Fade(float targetAlpha, float fadeDuration, Action fadeAction) public void Fade(float targetAlpha, float fadeDuration, Action fadeAction)
{ {
StartCoroutine(FadeInternal(targetAlpha, fadeDuration, fadeAction)); StartCoroutine(FadeInternal(targetAlpha, fadeDuration, fadeAction));
} }
/**
* Fade out, move camera to view and then fade back in.
*/
public void FadeToView(View view, float fadeDuration, Action fadeAction) public void FadeToView(View view, float fadeDuration, Action fadeAction)
{ {
swipePanActive = false; swipePanActive = false;
@ -97,7 +104,7 @@ namespace Fungus
Fade(0f, fadeDuration / 2f, delegate { Fade(0f, fadeDuration / 2f, delegate {
// Snap to new view // Snap to new view
PanToPosition(view.transform.position, view.viewSize, 0f, null); PanToPosition(view.transform.position, view.transform.rotation, view.viewSize, 0f, null);
// Fade in // Fade in
Fade(1f, fadeDuration / 2f, delegate { Fade(1f, fadeDuration / 2f, delegate {
@ -162,7 +169,7 @@ namespace Fungus
/** /**
* Moves camera from current position to a target position over a period of time. * Moves camera from current position to a target position over a period of time.
*/ */
public void PanToPosition(Vector3 targetPosition, float targetSize, float duration, Action arriveAction) public void PanToPosition(Vector3 targetPosition, Quaternion targetRotation, float targetSize, float duration, Action arriveAction)
{ {
swipePanActive = false; swipePanActive = false;
@ -171,6 +178,7 @@ namespace Fungus
// Move immediately // Move immediately
Camera.main.orthographicSize = targetSize; Camera.main.orthographicSize = targetSize;
Camera.main.transform.position = targetPosition; Camera.main.transform.position = targetPosition;
Camera.main.transform.rotation = targetRotation;
SetCameraZ(); SetCameraZ();
if (arriveAction != null) if (arriveAction != null)
{ {
@ -179,7 +187,7 @@ namespace Fungus
} }
else else
{ {
StartCoroutine(PanInternal(targetPosition, targetSize, duration, arriveAction)); StartCoroutine(PanInternal(targetPosition, targetRotation, targetSize, duration, arriveAction));
} }
} }
@ -190,6 +198,7 @@ namespace Fungus
{ {
CameraView currentView = new CameraView(); CameraView currentView = new CameraView();
currentView.cameraPos = Camera.main.transform.position; currentView.cameraPos = Camera.main.transform.position;
currentView.cameraRot = Camera.main.transform.rotation;
currentView.cameraSize = Camera.main.orthographicSize; currentView.cameraSize = Camera.main.orthographicSize;
storedViews[viewName] = currentView; storedViews[viewName] = currentView;
} }
@ -215,6 +224,7 @@ namespace Fungus
{ {
// Move immediately // Move immediately
Camera.main.transform.position = cameraView.cameraPos; Camera.main.transform.position = cameraView.cameraPos;
Camera.main.transform.rotation = cameraView.cameraRot;
Camera.main.orthographicSize = cameraView.cameraSize; Camera.main.orthographicSize = cameraView.cameraSize;
SetCameraZ(); SetCameraZ();
if (arriveAction != null) if (arriveAction != null)
@ -224,17 +234,19 @@ namespace Fungus
} }
else else
{ {
StartCoroutine(PanInternal(cameraView.cameraPos, cameraView.cameraSize, duration, arriveAction)); StartCoroutine(PanInternal(cameraView.cameraPos, cameraView.cameraRot, cameraView.cameraSize, duration, arriveAction));
} }
} }
IEnumerator PanInternal(Vector3 targetPos, float targetSize, float duration, Action arriveAction) IEnumerator PanInternal(Vector3 targetPos, Quaternion targetRot, float targetSize, float duration, Action arriveAction)
{ {
float timer = 0; float timer = 0;
float startSize = Camera.main.orthographicSize; float startSize = Camera.main.orthographicSize;
float endSize = targetSize; float endSize = targetSize;
Vector3 startPos = Camera.main.transform.position; Vector3 startPos = Camera.main.transform.position;
Vector3 endPos = targetPos; Vector3 endPos = targetPos;
Quaternion startRot = Camera.main.transform.rotation;
Quaternion endRot = targetRot;
bool arrived = false; bool arrived = false;
while (!arrived) while (!arrived)
@ -250,6 +262,7 @@ namespace Fungus
float t = timer / duration; float t = timer / duration;
Camera.main.orthographicSize = Mathf.Lerp(startSize, endSize, Mathf.SmoothStep(0f, 1f, t)); Camera.main.orthographicSize = Mathf.Lerp(startSize, endSize, Mathf.SmoothStep(0f, 1f, t));
Camera.main.transform.position = Vector3.Lerp(startPos, endPos, Mathf.SmoothStep(0f, 1f, t)); Camera.main.transform.position = Vector3.Lerp(startPos, endPos, Mathf.SmoothStep(0f, 1f, t));
Camera.main.transform.rotation = Quaternion.Lerp(startRot, endRot, Mathf.SmoothStep(0f, 1f, t));
SetCameraZ(); SetCameraZ();
if (arrived && if (arrived &&
@ -330,7 +343,7 @@ namespace Fungus
Vector3 targetPosition = CalcCameraPosition(cameraPos, swipePanViewA, swipePanViewB); Vector3 targetPosition = CalcCameraPosition(cameraPos, swipePanViewA, swipePanViewB);
float targetSize = CalcCameraSize(cameraPos, swipePanViewA, swipePanViewB); float targetSize = CalcCameraSize(cameraPos, swipePanViewA, swipePanViewB);
PanToPosition(targetPosition, targetSize, duration, delegate { PanToPosition(targetPosition, Quaternion.identity, targetSize, duration, delegate {
swipePanActive = true; swipePanActive = true;

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

@ -31,7 +31,7 @@ namespace Fungus
{ {
Game game = Game.GetInstance(); Game game = Game.GetInstance();
game.cameraController.PanToPosition(view.transform.position, view.viewSize, 0, null); game.cameraController.PanToPosition(view.transform.position, view.transform.rotation, view.viewSize, 0, null);
if (onComplete != null) if (onComplete != null)
{ {
@ -46,12 +46,14 @@ namespace Fungus
public class PanToPosition : CommandQueue.Command public class PanToPosition : CommandQueue.Command
{ {
Vector3 targetPosition; Vector3 targetPosition;
Quaternion targetRotation;
float targetSize; float targetSize;
float duration; float duration;
public PanToPosition(Vector3 _targetPosition, float _targetSize, float _duration) public PanToPosition(Vector3 _targetPosition, Quaternion _targetRotation, float _targetSize, float _duration)
{ {
targetPosition = _targetPosition; targetPosition = _targetPosition;
targetRotation = _targetRotation;
targetSize = _targetSize; targetSize = _targetSize;
duration = _duration; duration = _duration;
} }
@ -62,7 +64,7 @@ namespace Fungus
game.waiting = true; game.waiting = true;
game.cameraController.PanToPosition(targetPosition, targetSize, duration, delegate { game.cameraController.PanToPosition(targetPosition, targetRotation, targetSize, duration, delegate {
game.waiting = false; game.waiting = false;

12
Assets/Fungus/Scripts/GameController.cs

@ -141,7 +141,7 @@ namespace Fungus
*/ */
public static void PanToView(View targetView, float duration) public static void PanToView(View targetView, float duration)
{ {
PanToPosition(targetView.transform.position, targetView.viewSize, duration); PanToPosition(targetView.transform.position, targetView.transform.rotation, targetView.viewSize, duration);
} }
/** /**
@ -149,17 +149,20 @@ namespace Fungus
* The pan starts at the current camera position and performs a smoothed linear pan to the target. * The pan starts at the current camera position and performs a smoothed linear pan to the target.
* Command execution blocks until the pan completes. * Command execution blocks until the pan completes.
* This method returns immediately but it queues an asynchronous command for later execution. * This method returns immediately but it queues an asynchronous command for later execution.
* @param targetView The View to pan the camera to. * @param targetPosition The position to pan the camera to.
* @param targetRotation The rotation to pan the camera to.
* @param targetSize The orthographic size to pan the camera to.
* @param duration The length of time in seconds needed to complete the pan. * @param duration The length of time in seconds needed to complete the pan.
*/ */
public static void PanToPosition(Vector3 targetPosition, float targetSize, float duration) public static void PanToPosition(Vector3 targetPosition, Quaternion targetRotation, float targetSize, float duration)
{ {
CommandQueue commandQueue = Game.GetInstance().commandQueue; CommandQueue commandQueue = Game.GetInstance().commandQueue;
commandQueue.AddCommand(new Command.PanToPosition(targetPosition, targetSize, duration)); commandQueue.AddCommand(new Command.PanToPosition(targetPosition, targetRotation, targetSize, duration));
} }
/** /**
* Pans the camera through a sequence of target Views over a period of time. * Pans the camera through a sequence of target Views over a period of time.
* Note: Does not support camera Rotation.
* The pan starts at the current camera position and performs a smooth pan through all Views in the list. * The pan starts at the current camera position and performs a smooth pan through all Views in the list.
* Command execution blocks until the pan completes. * Command execution blocks until the pan completes.
* If more control is required over the camera path then you should instead use an Animator component to precisely control the Camera path. * If more control is required over the camera path then you should instead use an Animator component to precisely control the Camera path.
@ -167,6 +170,7 @@ namespace Fungus
* @param duration The length of time in seconds needed to complete the pan. * @param duration The length of time in seconds needed to complete the pan.
* @param targetViews A parameter list of views to visit during the pan. * @param targetViews A parameter list of views to visit during the pan.
*/ */
[Obsolete("Use a Camera animation instead.")]
public static void PanToPath(float duration, params View[] targetViews) public static void PanToPath(float duration, params View[] targetViews)
{ {
CommandQueue commandQueue = Game.GetInstance().commandQueue; CommandQueue commandQueue = Game.GetInstance().commandQueue;

2
Assets/Fungus/Scripts/Room.cs

@ -145,7 +145,7 @@ namespace Fungus
else else
{ {
// Snap to new view // Snap to new view
cameraController.PanToPosition(view.transform.position, view.viewSize, 0, null); cameraController.PanToPosition(view.transform.position, view.transform.rotation, view.viewSize, 0, null);
} }
// Hide all buttons in the room before entering // Hide all buttons in the room before entering

8
Assets/Fungus/Scripts/View.cs

@ -12,7 +12,7 @@ namespace Fungus
public class View : MonoBehaviour public class View : MonoBehaviour
{ {
/** /**
* Orthographic size of the camera view. * Orthographic size of the camera view in world space coordinates.
*/ */
public float viewSize = 0.5f; public float viewSize = 0.5f;
@ -37,5 +37,11 @@ namespace Fungus
* Color of the secondary view rectangle. * Color of the secondary view rectangle.
*/ */
public Color secondaryColor = Color.grey; public Color secondaryColor = Color.grey;
void Update()
{
// Disable scaling to avoid complicating the orthographic size calculations
transform.localScale = new Vector3(1,1,1);
}
} }
} }

BIN
Assets/FungusExample/Scenes/Example.unity

Binary file not shown.

9
Assets/FungusExample/Scripts/ViewRoom.cs

@ -17,7 +17,6 @@ namespace Fungus.Example
AddOption("Lets look at the logo", LookLogo); AddOption("Lets look at the logo", LookLogo);
AddOption("That's a nice toadstool over there", LookToadstool); AddOption("That's a nice toadstool over there", LookToadstool);
AddOption ("Give me the full tour", FullTour);
AddOption("Back to menu", MoveToMenu); AddOption("Back to menu", MoveToMenu);
Say("Wanna move the camera?"); Say("Wanna move the camera?");
@ -43,13 +42,5 @@ namespace Fungus.Example
Say("Hey - let's go look at that logo"); Say("Hey - let's go look at that logo");
Call(LookLogo); Call(LookLogo);
} }
void FullTour()
{
Say("Let's have a look around here");
PanToPath(10f, logoView, toadstoolView, mainView);
Say("And we're back!");
Call(OnEnter);
}
} }
} }
Loading…
Cancel
Save