Use easing within the carousel views scrolling

It is visible if you scroll more than one card
This commit is contained in:
Tobias Loose
2013-12-06 20:40:34 +01:00
parent 2002bb4e16
commit 0d350af1b6
2 changed files with 35 additions and 50 deletions
+3 -3
View File
@@ -2,6 +2,7 @@
#define _CAROUSEL_DECK_VIEW_H_ #define _CAROUSEL_DECK_VIEW_H_
#include "DeckView.h" #include "DeckView.h"
#include "Easing.h"
class CarouselDeckView : public DeckView class CarouselDeckView : public DeckView
{ {
@@ -9,8 +10,7 @@ private:
enum AnimationStage{ enum AnimationStage{
NONE = 0, NONE = 0,
SLIDE_UP, SLIDE_UP,
SLIDE_DOWN, SLIDE_DOWN
SCROLL_TO_SELECTED
}; };
static const float scroll_speed; static const float scroll_speed;
@@ -39,7 +39,7 @@ public:
//maintains the current rotation for fluid animations //maintains the current rotation for fluid animations
private: private:
float mRotation; //[-1,1]. defines the current rotation of the cards InOutQuadEasing mScrollOffset; //[-1,1]. defines the current rotation of the cards
float mSlide; //[-1,1]. defines, the y-offset of the cards float mSlide; //[-1,1]. defines, the y-offset of the cards
int mScrollTarget; //0 <= mScrollTarget < mCards.size(). defines where to scroll to if the current animation is a scroll animation int mScrollTarget; //0 <= mScrollTarget < mCards.size(). defines where to scroll to if the current animation is a scroll animation
AnimationStage mStage; // state machine state. for animation purposes AnimationStage mStage; // state machine state. for animation purposes
+22 -37
View File
@@ -3,7 +3,7 @@
const float CarouselDeckView::scroll_speed = 5.0f; const float CarouselDeckView::scroll_speed = 5.0f;
CarouselDeckView::CarouselDeckView() : CarouselDeckView::CarouselDeckView() :
DeckView(10), mRotation(0), mSlide(0), mScrollTarget(2), mStage(NONE) DeckView(10), mScrollOffset(0), mSlide(0), mScrollTarget(2), mStage(NONE)
{ {
} }
@@ -12,38 +12,31 @@ CarouselDeckView::~CarouselDeckView()
void CarouselDeckView::UpdateViewState(float dt) void CarouselDeckView::UpdateViewState(float dt)
{ {
switch(mStage) if(!mScrollOffset.finished())
{ {
case SCROLL_TO_SELECTED: mScrollOffset.update(dt);
if(mScrollTarget < 2)
{ //scroll left if(mScrollTarget < 2 && mScrollOffset.value <= -1.0f)
mRotation -= dt * scroll_speed;
if(mRotation <= -1.0f)
{ {
mRotation += 1.0f; mScrollOffset.translate(1.0f);
deck()->prev(); deck()->prev();
reloadIndexes(); reloadIndexes();
mScrollTarget += 1; mScrollTarget += 1;
} }
}
else if(mScrollTarget > 2) if(mScrollTarget > 2 && mScrollOffset.value >= 1.0f)
{//scroll right
mRotation += dt * scroll_speed;
if(mRotation >= 1.0f)
{ {
mRotation -= 1.0f; mScrollOffset.translate(-1.0f);
deck()->next(); deck()->next();
reloadIndexes(); reloadIndexes();
mScrollTarget -= 1; mScrollTarget -= 1;
} }
}
else if(mScrollTarget == 2)
{
mRotation = 0;
mStage = NONE;
}
dirtyCardPos = true; dirtyCardPos = true;
break; }
switch(mStage)
{
case SLIDE_DOWN: case SLIDE_DOWN:
mSlide -= 0.05f; mSlide -= 0.05f;
if (mSlide < -1.0f) if (mSlide < -1.0f)
@@ -80,7 +73,7 @@ void CarouselDeckView::UpdateViewState(float dt)
void CarouselDeckView::UpdateCardPosition(CardRep &rep, int index) void CarouselDeckView::UpdateCardPosition(CardRep &rep, int index)
{ {
float rotation = mRotation + 8 - index; float rotation = mScrollOffset.value + 8 - index;
rep.x = x_center + cos((rotation) * M_PI / 12) * (right_border - x_center); rep.x = x_center + cos((rotation) * M_PI / 12) * (right_border - x_center);
rep.scale = max_scale / 1.12f * cos((rep.x - x_center) * 1.5f / (right_border - x_center)) + 0.2f * max_scale * cos( rep.scale = max_scale / 1.12f * cos((rep.x - x_center) * 1.5f / (right_border - x_center)) + 0.2f * max_scale * cos(
@@ -90,7 +83,7 @@ void CarouselDeckView::UpdateCardPosition(CardRep &rep, int index)
void CarouselDeckView::Reset() void CarouselDeckView::Reset()
{ {
mRotation = 0; mScrollOffset = 0;
mSlide = 0; mSlide = 0;
mScrollTarget = 2; mScrollTarget = 2;
mStage = NONE; mStage = NONE;
@@ -117,13 +110,13 @@ void CarouselDeckView::Render()
renderCard(4); renderCard(4);
renderCard(0); renderCard(0);
if (mRotation < 0.5 && mRotation > -0.5) if (mScrollOffset.value < 0.5 && mScrollOffset.value > -0.5)
{ {
renderCard(1); renderCard(1);
renderCard(3); renderCard(3);
renderCard(2); renderCard(2);
} }
else if (mRotation < -0.5) else if (mScrollOffset.value < -0.5)
{ {
renderCard(3); renderCard(3);
renderCard(2); renderCard(2);
@@ -151,8 +144,8 @@ MTGCard * CarouselDeckView::Click(int x, int y)
//clicked not the active card, start animation:s //clicked not the active card, start animation:s
if(n != 2 && mStage == NONE) if(n != 2 && mStage == NONE)
{ {
mScrollTarget = n; DebugTrace(">>>>> " << n);
mStage = SCROLL_TO_SELECTED; changePosition(n - 2);
} }
return NULL; return NULL;
@@ -160,16 +153,8 @@ MTGCard * CarouselDeckView::Click(int x, int y)
void CarouselDeckView::changePosition(int offset) void CarouselDeckView::changePosition(int offset)
{ {
if(offset > 0) mScrollTarget = 2 + offset;
{ mScrollOffset.start(offset, 0.3f*abs(offset));
mScrollTarget += 1;
mStage = SCROLL_TO_SELECTED;
}
else if(offset < 0)
{
mScrollTarget -= 1;
mStage = SCROLL_TO_SELECTED;
}
last_user_activity = 0; last_user_activity = 0;
} }