Skip to content

Commit

Permalink
First IList implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Aloshi committed Feb 8, 2014
1 parent 5c12395 commit 7699a4f
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 150 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ set(ES_HEADERS
${CMAKE_CURRENT_SOURCE_DIR}/src/Sound.h
${CMAKE_CURRENT_SOURCE_DIR}/src/SystemData.h
${CMAKE_CURRENT_SOURCE_DIR}/src/ThemeData.h
${CMAKE_CURRENT_SOURCE_DIR}/src/ThemeDumper.h
${CMAKE_CURRENT_SOURCE_DIR}/src/VolumeControl.h
${CMAKE_CURRENT_SOURCE_DIR}/src/Window.h
${CMAKE_CURRENT_SOURCE_DIR}/src/XMLReader.h
Expand All @@ -165,6 +164,7 @@ set(ES_HEADERS
${CMAKE_CURRENT_SOURCE_DIR}/src/components/ComponentListComponent.h
${CMAKE_CURRENT_SOURCE_DIR}/src/components/DateTimeComponent.h
${CMAKE_CURRENT_SOURCE_DIR}/src/components/HelpComponent.h
${CMAKE_CURRENT_SOURCE_DIR}/src/components/IList.h
${CMAKE_CURRENT_SOURCE_DIR}/src/components/ImageComponent.h
${CMAKE_CURRENT_SOURCE_DIR}/src/components/ImageGridComponent.h
${CMAKE_CURRENT_SOURCE_DIR}/src/components/NinePatchComponent.h
Expand Down Expand Up @@ -235,7 +235,6 @@ set(ES_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/src/Sound.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/SystemData.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/ThemeData.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/ThemeDumper.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/VolumeControl.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/Window.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/XMLReader.cpp
Expand All @@ -245,6 +244,7 @@ set(ES_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/src/components/ComponentListComponent.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/components/DateTimeComponent.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/components/HelpComponent.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/components/IList.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/components/ImageComponent.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/components/NinePatchComponent.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/components/RatingComponent.cpp
Expand Down
2 changes: 0 additions & 2 deletions src/ThemeDumper.cpp

This file was deleted.

13 changes: 0 additions & 13 deletions src/ThemeDumper.h

This file was deleted.

81 changes: 81 additions & 0 deletions src/components/IList.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include "IList.h"

const IList::ScrollTier IList::SCROLL_SPEED[IList::SCROLL_SPEED_COUNT] = {
{500, 500},
{2600, 150},
{0, 100}
};

IList::IList()
{
mScrollTier = 0;
mScrollVelocity = 0;
mScrollTierAccumulator = 0;
mScrollCursorAccumulator = 0;
}

void IList::listInput(int velocity)
{
mScrollVelocity = velocity;
mScrollTier = 0;
mScrollTierAccumulator = 0;
mScrollCursorAccumulator = 0;
scroll(mScrollVelocity);
}

void IList::listUpdate(int deltaTime)
{
if(mScrollVelocity == 0 || getLength() < 2)
return;

mScrollCursorAccumulator += deltaTime;
mScrollTierAccumulator += deltaTime;

while(mScrollCursorAccumulator >= SCROLL_SPEED[mScrollTier].scrollDelay)
{
mScrollCursorAccumulator -= SCROLL_SPEED[mScrollTier].scrollDelay;
scroll(mScrollVelocity);
}

// are we ready to go even FASTER?
while(mScrollTier < SCROLL_SPEED_COUNT - 1 && mScrollTierAccumulator >= SCROLL_SPEED[mScrollTier].length)
{
mScrollTierAccumulator -= SCROLL_SPEED[mScrollTier].length;
mScrollTier++;
}
}

void IList::scroll(int amt)
{
if(mScrollVelocity == 0 || getLength() < 2)
return;

int cursor = getCursorIndex() + amt;
int absAmt = amt < 0 ? -amt : amt;

// stop at the end if we've been holding down the button for a long time or
// we're scrolling faster than one item at a time (e.g. page up/down)
// otherwise, loop around
if(mScrollTier > 0 || absAmt > 1)
{
if(cursor < 0)
cursor = 0;
else if(cursor >= getLength())
cursor = getLength() - 1;
}else{
if(cursor < 0)
cursor += getLength();
else if(cursor >= getLength())
cursor -= getLength();
}

if(cursor != getCursorIndex())
onScroll(absAmt);

setCursorIndex(cursor);
}

bool IList::isScrolling() const
{
return (mScrollVelocity != 0 && mScrollTier > 0);
}
36 changes: 36 additions & 0 deletions src/components/IList.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once

class IList
{
public:
IList();

bool isScrolling() const;

protected:
void listInput(int velocity); // a velocity of 0 = stop scrolling
void listUpdate(int deltaTime);

virtual int getCursorIndex() = 0;
virtual void setCursorIndex(int index) = 0; // (index >= 0 && index < getLength()) is guaranteed to be true
virtual int getLength() = 0;

void scroll(int amt);
virtual void onScroll(int amt) {};

private:
struct ScrollTier
{
int length; // how long we stay on this level before going to the next
int scrollDelay; // how long between scrolls
};

static const int SCROLL_SPEED_COUNT = 3;
static const ScrollTier SCROLL_SPEED[SCROLL_SPEED_COUNT];

int mScrollTier;
int mScrollVelocity;

int mScrollTierAccumulator;
int mScrollCursorAccumulator;
};
66 changes: 12 additions & 54 deletions src/components/ImageGridComponent.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#pragma once

#include "../GuiComponent.h"
#include "IList.h"
#include "../components/ImageComponent.h"
#include "../Log.h"

template<typename T>
class ImageGridComponent : public GuiComponent
class ImageGridComponent : public GuiComponent, public IList
{
public:
ImageGridComponent(Window* window);
Expand Down Expand Up @@ -42,6 +43,11 @@ class ImageGridComponent : public GuiComponent
void update(int deltaTime) override;
void render(const Eigen::Affine3f& parentTrans) override;

protected:
virtual int getCursorIndex() { return mCursor; }
virtual void setCursorIndex(int index) { mCursor = index; onCursorChanged(CURSOR_STOPPED); }
virtual int getLength() { return mEntries.size(); }

private:
Eigen::Vector2f getSquareSize(std::shared_ptr<TextureResource> tex = nullptr) const
{
Expand Down Expand Up @@ -89,18 +95,10 @@ class ImageGridComponent : public GuiComponent
void buildImages();
void updateImages();

static const int SCROLL_DELAY = 507;
static const int SCROLL_TIME = 150;

void setScrollDir(Eigen::Vector2i dir);
void scroll();
void onCursorChanged(CursorState state);

int mCursor;

Eigen::Vector2i mScrollDir;
int mScrollAccumulator;

bool mEntriesDirty;

std::vector<Entry> mEntries;
Expand All @@ -112,8 +110,6 @@ ImageGridComponent<T>::ImageGridComponent(Window* window) : GuiComponent(window)
{
mEntriesDirty = true;
mCursor = 0;
mScrollDir << 0, 0;
mScrollAccumulator = 0;
}

template<typename T>
Expand Down Expand Up @@ -151,7 +147,6 @@ void ImageGridComponent<T>::clear()
{
mEntries.clear();
mCursor = 0;
mScrollDir << 0, 0;
onCursorChanged(CURSOR_STOPPED);
mEntriesDirty = true;
}
Expand Down Expand Up @@ -183,35 +178,8 @@ void ImageGridComponent<T>::setCursor(typename std::vector<Entry>::const_iterato
template<typename T>
void ImageGridComponent<T>::stopScrolling()
{
mScrollDir = Eigen::Vector2i::Zero();
}

template<typename T>
void ImageGridComponent<T>::scroll()
{
if(mEntries.size() == 0)
return;

int offset = 0;
Eigen::Vector2i size = getGridSize();

offset += mScrollDir.x();
offset += mScrollDir.y() * size.x();

mCursor += offset;
if(mCursor < 0)
mCursor += mEntries.size();
if(mCursor >= (int)mEntries.size())
mCursor -= mEntries.size();

onCursorChanged(CURSOR_SCROLLING);
}

template<typename T>
void ImageGridComponent<T>::setScrollDir(Eigen::Vector2i dir)
{
mScrollDir = dir;
mScrollAccumulator = -SCROLL_DELAY;
listInput(0);
onCursorChanged(CURSOR_STOPPED);
}

template<typename T>
Expand All @@ -231,15 +199,13 @@ bool ImageGridComponent<T>::input(InputConfig* config, Input input)

if(dir != Eigen::Vector2i::Zero())
{
setScrollDir(dir);
scroll();
listInput(dir.x() + dir.y() * getGridSize().x());
return true;
}
}else{
if(config->isMappedTo("up", input) || config->isMappedTo("down", input) || config->isMappedTo("left", input) || config->isMappedTo("right", input))
{
mScrollDir << 0, 0;
onCursorChanged(CURSOR_STOPPED);
stopScrolling();
}
}

Expand All @@ -249,15 +215,7 @@ bool ImageGridComponent<T>::input(InputConfig* config, Input input)
template<typename T>
void ImageGridComponent<T>::update(int deltaTime)
{
if(mScrollDir != Eigen::Vector2i::Zero())
{
mScrollAccumulator += deltaTime;
while(mScrollAccumulator >= SCROLL_TIME)
{
scroll();
mScrollAccumulator -= SCROLL_TIME;
}
}
listUpdate(deltaTime);
}

template<typename T>
Expand Down
Loading

0 comments on commit 7699a4f

Please sign in to comment.