Use easing within the carousel view sliding

This commit is contained in:
Tobias Loose
2013-12-06 21:04:21 +01:00
parent 0d350af1b6
commit 31b353c5ec
3 changed files with 38 additions and 57 deletions

View File

@@ -7,13 +7,7 @@
class CarouselDeckView : public DeckView class CarouselDeckView : public DeckView
{ {
private: private:
enum AnimationStage{ static const float slide_animation_duration;
NONE = 0,
SLIDE_UP,
SLIDE_DOWN
};
static const float scroll_speed;
public: public:
CarouselDeckView(); CarouselDeckView();
@@ -40,9 +34,8 @@ public:
//maintains the current rotation for fluid animations //maintains the current rotation for fluid animations
private: private:
InOutQuadEasing mScrollOffset; //[-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 InOutQuadEasing 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
}; };
#endif //_CAROUSEL_DECK_VIEW_H_ #endif //_CAROUSEL_DECK_VIEW_H_

View File

@@ -1,9 +1,9 @@
#include "CarouselDeckView.h" #include "CarouselDeckView.h"
const float CarouselDeckView::scroll_speed = 5.0f; const float CarouselDeckView::slide_animation_duration = 0.6f;
CarouselDeckView::CarouselDeckView() : CarouselDeckView::CarouselDeckView() :
DeckView(10), mScrollOffset(0), mSlide(0), mScrollTarget(2), mStage(NONE) DeckView(10), mScrollOffset(0), mSlide(0), mScrollTarget(2)
{ {
} }
@@ -35,39 +35,30 @@ void CarouselDeckView::UpdateViewState(float dt)
dirtyCardPos = true; dirtyCardPos = true;
} }
switch(mStage) if(!mSlide.finished())
{ {
case SLIDE_DOWN: mSlide.update(dt);
mSlide -= 0.05f;
if (mSlide < -1.0f) if(mSlide.value < mSlide.start_value)
{ {
dirtyFilters = true; //going downwards
mSlide = 1; if(mSlide.value < -1.0f)
{
mSlide.translate(2.0f);
SwitchFilter(1);
}
} }
else if (mSlide > 0 && mSlide < 0.05) else if(mSlide.value > mSlide.start_value)
{ {
mStage = NONE; //upwards
mSlide = 0; if(mSlide.value > 1.0f)
{
mSlide.translate(-2.0f);
SwitchFilter(-1);
}
} }
dirtyCardPos = true; dirtyCardPos = true;
break;
case SLIDE_UP:
mSlide += 0.05f;
if (mSlide > 1.0f)
{
dirtyFilters = true;
mSlide = -1;
}
else if (mSlide < 0 && mSlide > -0.05)
{
mStage = NONE;
mSlide = 0;
}
dirtyCardPos = true;
break;
default:
mStage = NONE;
break;
} }
} }
@@ -78,7 +69,7 @@ void CarouselDeckView::UpdateCardPosition(CardRep &rep, int 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(
cos((rep.x - x_center) * 0.15f / (right_border - x_center))); cos((rep.x - x_center) * 0.15f / (right_border - x_center)));
rep.y = (SCREEN_HEIGHT_F) / 2.0f + SCREEN_HEIGHT_F * mSlide * (rep.scale + 0.2f); rep.y = (SCREEN_HEIGHT_F) / 2.0f + SCREEN_HEIGHT_F * mSlide.value * (rep.scale + 0.2f);
} }
void CarouselDeckView::Reset() void CarouselDeckView::Reset()
@@ -86,7 +77,6 @@ void CarouselDeckView::Reset()
mScrollOffset = 0; mScrollOffset = 0;
mSlide = 0; mSlide = 0;
mScrollTarget = 2; mScrollTarget = 2;
mStage = NONE;
DeckView::Reset(); DeckView::Reset();
} }
@@ -136,16 +126,17 @@ MTGCard * CarouselDeckView::Click(int x, int y)
last_user_activity = 0; last_user_activity = 0;
//clicked active card, and no animation is running //clicked active card, and no animation is running
if(n == 2 && mStage == NONE) if(mSlide.finished() && mScrollOffset.finished())
{ {
return getActiveCard(); if(n == 2)
} {
return getActiveCard();
//clicked not the active card, start animation:s }
if(n != 2 && mStage == NONE) else
{ {
DebugTrace(">>>>> " << n); DebugTrace(">>>>> " << n);
changePosition(n - 2); changePosition(n - 2);
}
} }
return NULL; return NULL;
@@ -161,17 +152,14 @@ void CarouselDeckView::changePosition(int offset)
void CarouselDeckView::changeFilter(int offset) void CarouselDeckView::changeFilter(int offset)
{ {
if(offset > 0) if(offset < 0)
{ {
mStage = SLIDE_UP; mSlide.start(-2.0f, slide_animation_duration);
SwitchFilter(1);
} }
else if(offset < 0) else if(offset > 0)
{ {
mStage = SLIDE_DOWN; mSlide.start(2.0f, slide_animation_duration);
SwitchFilter(-1);
} }
last_user_activity = 0; last_user_activity = 0;
} }

View File

@@ -1,7 +1,7 @@
#include "GridDeckView.h" #include "GridDeckView.h"
const float GridDeckView::scroll_animation_duration = 0.3f; const float GridDeckView::scroll_animation_duration = 0.3f;
const float GridDeckView::slide_animation_duration = 0.4f; const float GridDeckView::slide_animation_duration = 0.6f;
const float GridDeckView::card_scale_small = 0.48f; const float GridDeckView::card_scale_small = 0.48f;
const float GridDeckView::card_scale_big = 0.7f; const float GridDeckView::card_scale_big = 0.7f;