/// <summary>
        /// Switches meshes and loads new bitmaps if available. Updated to support 2 pages in landscape
        /// </summary>
        /// <param name="page"></param>
        private void StartCurl(int page)
        {
            switch (page)
            {
            // Once right side page is curled, first right page is assigned into
            // curled page. And if there are more bitmaps available new bitmap is
            // loaded into right side mesh.
            case CURL_RIGHT:
            {
                // Remove meshes from renderer.
                mRenderer.RemoveCurlMesh(mPageLeft);
                mRenderer.RemoveCurlMesh(mPageRight);
                mRenderer.RemoveCurlMesh(mPageCurl);

                // We are curling right page.
                CurlMesh curl = mPageRight;
                mPageRight = mPageCurl;
                mPageCurl  = curl;

                if (mCurrentIndex > 0)
                {
                    mPageLeft.FlipTexture = true;
                    mPageLeft.Rect        = mRenderer.GetPageRect(CurlRenderer.PAGE_LEFT);
                    mPageLeft.Reset();
                    if (mRenderLeftPage)
                    {
                        mRenderer.AddCurlMesh(mPageLeft);
                    }
                }
                if (mCurrentIndex < mPageProvider.PageCount - 1)
                {
                    UpdatePage(mPageRight.TexturePage, mCurrentIndex + 1);
                    mPageRight.Rect        = mRenderer.GetPageRect(CurlRenderer.PAGE_RIGHT);
                    mPageRight.FlipTexture = false;
                    mPageRight.Reset();
                    mRenderer.AddCurlMesh(mPageRight);
                }

                // Add curled page to renderer.
                mPageCurl.Rect        = mRenderer.GetPageRect(CurlRenderer.PAGE_RIGHT);
                mPageCurl.FlipTexture = (false);
                mPageCurl.Reset();
                mRenderer.AddCurlMesh(mPageCurl);

                mCurlState = CURL_RIGHT;
                break;
            }

            // On left side curl, left page is assigned to curled page. And if
            // there are more bitmaps available before currentIndex, new bitmap
            // is loaded into left page.
            case CURL_LEFT:
            {
                // Remove meshes from renderer.
                mRenderer.RemoveCurlMesh(mPageLeft);
                mRenderer.RemoveCurlMesh(mPageRight);
                mRenderer.RemoveCurlMesh(mPageCurl);

                // We are curling left page.
                CurlMesh curl = mPageLeft;
                mPageLeft = mPageCurl;
                mPageCurl = curl;

                if (mCurrentIndex > 1)
                {
                    UpdatePage(mPageLeft.TexturePage, mCurrentIndex - 2);
                    mPageLeft.FlipTexture = true;
                    mPageLeft.Rect        = mRenderer.GetPageRect(CurlRenderer.PAGE_LEFT);
                    mPageLeft.Reset();
                    if (mRenderLeftPage)
                    {
                        mRenderer.AddCurlMesh(mPageLeft);
                    }
                }

                // If there is something to show on right page add it to renderer.
                if (mCurrentIndex < mPageProvider.PageCount)
                {
                    mPageRight.FlipTexture = false;
                    mPageRight.Rect        = mRenderer.GetPageRect(CurlRenderer.PAGE_RIGHT);
                    mPageRight.Reset();
                    mRenderer.AddCurlMesh(mPageRight);
                }

                // How dragging previous page happens depends on view mode.
                if (mViewMode == SHOW_ONE_PAGE ||
                    (mCurlState == CURL_LEFT && mViewMode == SHOW_TWO_PAGES))
                {
                    mPageCurl.Rect        = mRenderer.GetPageRect(CurlRenderer.PAGE_RIGHT);
                    mPageCurl.FlipTexture = false;
                }
                else
                {
                    mPageCurl.Rect        = mRenderer.GetPageRect(CurlRenderer.PAGE_LEFT);
                    mPageCurl.FlipTexture = (true);
                }
                mPageCurl.Reset();
                mRenderer.AddCurlMesh(mPageCurl);

                mCurlState = CURL_LEFT;
                break;
            }
            }
        }
        public void OnDrawFrame()
        {
            // We are not animating.
            if (mAnimate == false)
            {
                return;
            }

            long currentTime = DateTimeOffset.Now.ToUnixTimeMilliseconds();

            // If animation is done.
            if (currentTime >= mAnimationStartTime + mAnimationDurationTime)
            {
                if (mAnimationTargetEvent == SET_CURL_TO_RIGHT)
                {
                    // Switch curled page to right.
                    CurlMesh right = mPageCurl;
                    CurlMesh curl  = mPageRight;
                    right.Rect        = mRenderer.GetPageRect(CurlRenderer.PAGE_RIGHT);
                    right.FlipTexture = false;
                    right.Reset();
                    mRenderer.RemoveCurlMesh(curl);
                    mPageCurl  = curl;
                    mPageRight = right;
                    // If we were curling left page update current index.
                    if (mCurlState == CURL_LEFT)
                    {
                        --mCurrentIndex;
                    }
                }
                else if (mAnimationTargetEvent == SET_CURL_TO_LEFT)
                {
                    // Switch curled page to left.
                    CurlMesh left = mPageCurl;
                    CurlMesh curl = mPageLeft;
                    left.Rect        = mRenderer.GetPageRect(CurlRenderer.PAGE_LEFT);
                    left.FlipTexture = (true);
                    left.Reset();
                    mRenderer.RemoveCurlMesh(curl);
                    if (!mRenderLeftPage)
                    {
                        mRenderer.RemoveCurlMesh(left);
                    }
                    mPageCurl = curl;
                    mPageLeft = left;
                    // If we were curling right page update current index.
                    if (mCurlState == CURL_RIGHT)
                    {
                        ++mCurrentIndex;
                    }
                }
                mCurlState = CURL_NONE;
                mAnimate   = false;
                RequestRender();
            }
            else
            {
                mPointerPos.mPos.Set(mAnimationSource);
                float t = 1f - ((float)(currentTime - mAnimationStartTime) / mAnimationDurationTime);
                t = 1f - (t * t * t * (3 - 2 * t));
                mPointerPos.mPos.X += (mAnimationTarget.X - mAnimationSource.X) * t;
                mPointerPos.mPos.Y += (mAnimationTarget.Y - mAnimationSource.Y) * t;
                UpdateCurlPosition(mPointerPos);
            }
        }