Add Easing code to make scrolling in grid view more fluid

This commit is contained in:
Tobias Loose
2013-12-05 21:20:45 +01:00
parent d8d52e185d
commit 334454655d
4 changed files with 91 additions and 33 deletions

View File

Binary file not shown.

View File

@@ -3,14 +3,80 @@
#include "DeckView.h"
class Easing
{
public:
float start_value;
float delta_value;
float value;
float duration;
float time_acc;
Easing(float val): start_value(val), delta_value(0), value(val), duration(0), time_acc(0)
{}
void reset(){ value = start_value; time_acc = 0;}
void finish(){ value = start_value + delta_value; time_acc = 0; duration = 0;}
virtual void update(float dt) = 0;
void start(float targetValue, float _duration){
start_value = value;
delta_value = targetValue - start_value;
time_acc = 0;
duration = _duration;
}
void transpose(float delta_value){
start_value += delta_value;
value += delta_value;
}
bool finished()
{
return time_acc >= duration;
}
};
class InOutQuadEasing : public Easing
{
public:
InOutQuadEasing(float val): Easing(val) {}
void update(float dt){
if(duration > 0){
time_acc += dt;
if(time_acc > duration)
{
time_acc = duration;
value = start_value + delta_value;
}
else
{
float time_tmp = time_acc * 2 / duration;
if (time_tmp < 1)
{
value = delta_value/2*time_tmp*time_tmp + start_value;
}
else
{
time_tmp -= 1;
value = -delta_value/2 * (time_tmp*(time_tmp-2) - 1) + start_value;
}
}
}
}
};
class GridDeckView : public DeckView
{
private:
enum AnimationStage{
NONE = 0,
SLIDE_UP,
SLIDE_DOWN,
SCROLL_TO_SELECTED
SLIDE_DOWN//,
//SCROLL_TO_SELECTED
};
static const float scroll_speed;
@@ -32,9 +98,9 @@ private:
int mCols;
int mRows;
float mSlide; //[-1,1]. defines, the y-offset of the cards
float mScrollOffset;
InOutQuadEasing mScrollOffset;
int mCurrentSelection; //0 <= mCurrentSelection < mCards.size(). defines the current selected and thus upscaled card
int mColsToScroll; //the number of cols we need to scroll
//int mColsToScroll; //the number of cols we need to scroll
AnimationStage mStage; // state machine state. for animation purposes
};

View File

@@ -5,7 +5,7 @@ const float GridDeckView::card_scale_small = 0.48f;
const float GridDeckView::card_scale_big = 0.7f;
GridDeckView::GridDeckView()
: DeckView(16), mCols(8), mRows(2), mSlide(0), mScrollOffset(0), mCurrentSelection(-1), mColsToScroll(0), mStage(NONE)
: DeckView(16), mCols(8), mRows(2), mSlide(0), mScrollOffset(0), mCurrentSelection(-1)/*, mColsToScroll(0)*/, mStage(NONE)
{
}
@@ -20,43 +20,39 @@ void GridDeckView::Reset()
mSlide = 0;
mScrollOffset = 0;
mCurrentSelection = 0;
mColsToScroll = 0;
//mColsToScroll = 0;
mStage = NONE;
}
void GridDeckView::UpdateViewState(float dt)
{
switch(mStage)
if(!mScrollOffset.finished())
{
case SCROLL_TO_SELECTED:
if(mColsToScroll < 0){
mScrollOffset -= dt * scroll_speed;
if(mScrollOffset <= -1.0f)
mScrollOffset.update(dt);
if(mScrollOffset.finished())
{
if(mScrollOffset.start_value > mScrollOffset.value)
{
mScrollOffset += 1.0f;
deck()->next();
deck()->next();
mCurrentSelection = (mCurrentSelection >= 6) ? mCurrentSelection - 2 : -1;
reloadIndexes();
mColsToScroll += 1;
}
}else if(mColsToScroll > 0){
mScrollOffset += dt * scroll_speed;
if(mScrollOffset >= 1.0f)
else if(mScrollOffset.start_value < mScrollOffset.value)
{
mScrollOffset -= 1.0f;
deck()->prev();
deck()->prev();
mCurrentSelection = (mCurrentSelection >= 0 && mCurrentSelection < 10) ? mCurrentSelection + 2 : -1;
reloadIndexes();
mColsToScroll -= 1;
}
}else if(mColsToScroll == 0){
mScrollOffset = 0;
mStage = NONE;
reloadIndexes();
mScrollOffset.value = 0;
}
dirtyCardPos = true;
break;
}
switch(mStage)
{
case SLIDE_DOWN:
mSlide -= 0.05f;
if (mSlide < -1.0f)
@@ -98,7 +94,7 @@ void GridDeckView::UpdateCardPosition(CardRep &rep, int index)
float colWidth = SCREEN_WIDTH_F / (mCols - 3);
float rowHeight = SCREEN_HEIGHT_F / mRows;
rep.x = (col + mScrollOffset) * colWidth - colWidth;
rep.x = (col + mScrollOffset.value) * colWidth - colWidth;
rep.y = row * rowHeight + mSlide*SCREEN_HEIGHT + rowHeight/2;
if(mCurrentSelection == index)
@@ -144,13 +140,11 @@ MTGCard * GridDeckView::Click(int x, int y)
}
else if(n < 4 && mStage == NONE)
{
mColsToScroll = 1;
mStage = SCROLL_TO_SELECTED;
mScrollOffset.start(1.0f, 0.3f);
}
else if(n >= 12 && mStage == NONE)
{
mColsToScroll = -1;
mStage = SCROLL_TO_SELECTED;
mScrollOffset.start(-1.0f, 0.3f);
}
else
{
@@ -166,13 +160,11 @@ bool GridDeckView::Button(Buttons button)
switch(button)
{
case JGE_BTN_LEFT:
mColsToScroll -= 1;
mStage = SCROLL_TO_SELECTED;
mScrollOffset.start(-1.0f, 0.3f);
last_user_activity = 0;
break;
case JGE_BTN_RIGHT:
mColsToScroll += 1;
mStage = SCROLL_TO_SELECTED;
mScrollOffset.start( 1.0f, 0.3f);
last_user_activity = 0;
break;
case JGE_BTN_UP: