Files
aibis-dream/Assets/Plugins/Book-Page Curl/scripts/AutoFlip.cs
T

177 lines
5.8 KiB
C#

using UnityEngine;
using System.Collections;
[RequireComponent(typeof(Book))]
public class AutoFlip : MonoBehaviour
{
public FlipMode Mode;
public float PageFlipTime = 1;
public float TimeBetweenPages = 1;
public float DelayBeforeStarting = 0;
public bool AutoStartFlip = true;
public Book ControledBook;
public int AnimationFramesCount = 40;
bool isFlipping = false;
// Use this for initialization
void Start()
{
if (!ControledBook)
ControledBook = GetComponent<Book>();
if (AutoStartFlip)
//FlipToPage(6);
//StartFlipping();
ControledBook.OnFlip.AddListener(new UnityEngine.Events.UnityAction(PageFlipped));
}
void PageFlipped()
{
isFlipping = false;
}
public void StartFlipping()
{
StartCoroutine(FlipToEnd());
}
public void FlipRightPage()
{
if (isFlipping) return;
if (ControledBook.currentPage >= ControledBook.TotalPageCount) return;
isFlipping = true;
float frameTime = PageFlipTime / AnimationFramesCount/10;
float xc = (ControledBook.EndBottomRight.x + ControledBook.EndBottomLeft.x) / 2;
float xl = ((ControledBook.EndBottomRight.x - ControledBook.EndBottomLeft.x) / 2) * 0.9f;
//float h = ControledBook.Height * 0.5f;
float h = Mathf.Abs(ControledBook.EndBottomRight.y) * 0.9f;
float dx = (xl) * 2 / AnimationFramesCount;
StartCoroutine(FlipRTL(xc, xl, h, frameTime, dx));
}
public void FlipLeftPage()
{
if (isFlipping) return;
if (ControledBook.currentPage <= 0) return;
isFlipping = true;
float frameTime = PageFlipTime / AnimationFramesCount/10;
float xc = (ControledBook.EndBottomRight.x + ControledBook.EndBottomLeft.x) / 2;
float xl = ((ControledBook.EndBottomRight.x - ControledBook.EndBottomLeft.x) / 2) * 0.9f;
//float h = ControledBook.Height * 0.5f;
float h = Mathf.Abs(ControledBook.EndBottomRight.y) * 0.9f;
float dx = (xl) * 2 / AnimationFramesCount;
StartCoroutine(FlipLTR(xc, xl, h, frameTime, dx));
}
IEnumerator FlipToEnd()
{
yield return new WaitForSeconds(DelayBeforeStarting);
float frameTime = PageFlipTime / AnimationFramesCount;
float xc = (ControledBook.EndBottomRight.x + ControledBook.EndBottomLeft.x) / 2;
float xl = ((ControledBook.EndBottomRight.x - ControledBook.EndBottomLeft.x) / 2) * 0.9f;
//float h = ControledBook.Height * 0.5f;
float h = Mathf.Abs(ControledBook.EndBottomRight.y) * 0.9f;
//y=-(h/(xl)^2)*(x-xc)^2
// y
// |
// |
// |
//_______________|_________________x
// o|o |
// o | o |
// o | o | h
// o | o |
// o------xc-------o -
// |<--xl-->
// |
// |
float dx = (xl) * 2 / AnimationFramesCount;
switch (Mode)
{
case FlipMode.RightToLeft:
while (ControledBook.currentPage < ControledBook.TotalPageCount)
{
StartCoroutine(FlipRTL(xc, xl, h, frameTime, dx));
yield return new WaitForSeconds(TimeBetweenPages);
}
break;
case FlipMode.LeftToRight:
while (ControledBook.currentPage > 0)
{
StartCoroutine(FlipLTR(xc, xl, h, frameTime, dx));
yield return new WaitForSeconds(TimeBetweenPages);
}
break;
}
}
IEnumerator FlipRTL(float xc, float xl, float h, float frameTime, float dx)
{
float x = xc + xl;
float y = (-h / (xl * xl)) * (x - xc) * (x - xc);
ControledBook.DragRightPageToPoint(new Vector3(x, y, 0));
for (int i = 0; i < AnimationFramesCount; i++)
{
y = (-h / (xl * xl)) * (x - xc) * (x - xc);
ControledBook.UpdateBookRTLToPoint(new Vector3(x, y, 0));
yield return new WaitForSeconds(frameTime);
x -= dx;
}
ControledBook.ReleasePage();
}
IEnumerator FlipLTR(float xc, float xl, float h, float frameTime, float dx)
{
float x = xc - xl;
float y = (-h / (xl * xl)) * (x - xc) * (x - xc);
ControledBook.DragLeftPageToPoint(new Vector3(x, y, 0));
for (int i = 0; i < AnimationFramesCount; i++)
{
y = (-h / (xl * xl)) * (x - xc) * (x - xc);
ControledBook.UpdateBookLTRToPoint(new Vector3(x, y, 0));
yield return new WaitForSeconds(frameTime);
x += dx;
}
ControledBook.ReleasePage();
}
public void FlipToPage(int targetPage)
{
StartCoroutine(FlipToPageCoroutine(targetPage));
}
public IEnumerator FlipToPageCoroutine(int targetPage)
{
int currentPage = ControledBook.currentPage;
if (targetPage % 2 != 0)
{
targetPage++;
}
if (currentPage % 2 != 0)
{
currentPage++;
}
int pageDifference=Mathf.Abs(targetPage - currentPage)/2;
//int pageDifference = Mathf.Abs(targetPage - currentPage) / 2;
if (targetPage > currentPage)
{
for (int i = 0; i < pageDifference; i++)
{
FlipRightPage();
yield return new WaitUntil(() => !isFlipping);
}
}
else if (targetPage < currentPage)
{
for (int i = 0; i < pageDifference; i++)
{
FlipLeftPage();
yield return new WaitUntil(() => !isFlipping);
}
}
}
// The rest of your FlipManager class remains unchanged
}