2014-02-08 02:15:48 +00:00
|
|
|
#pragma once
|
2017-10-31 17:12:50 +00:00
|
|
|
#ifndef ES_CORE_COMPONENTS_ILIST_H
|
|
|
|
#define ES_CORE_COMPONENTS_ILIST_H
|
2014-02-08 02:15:48 +00:00
|
|
|
|
2014-06-20 01:30:09 +00:00
|
|
|
#include "components/ImageComponent.h"
|
|
|
|
#include "resources/Font.h"
|
2017-09-21 17:44:00 +00:00
|
|
|
#include "PowerSaver.h"
|
2014-02-08 02:15:48 +00:00
|
|
|
|
2014-02-08 03:45:28 +00:00
|
|
|
enum CursorState
|
|
|
|
{
|
|
|
|
CURSOR_STOPPED,
|
|
|
|
CURSOR_SCROLLING
|
|
|
|
};
|
2014-02-08 02:15:48 +00:00
|
|
|
|
2014-03-01 21:02:44 +00:00
|
|
|
enum ListLoopType
|
|
|
|
{
|
|
|
|
LIST_ALWAYS_LOOP,
|
|
|
|
LIST_PAUSE_AT_END,
|
|
|
|
LIST_NEVER_LOOP
|
|
|
|
};
|
|
|
|
|
2014-02-08 03:45:28 +00:00
|
|
|
struct ScrollTier
|
|
|
|
{
|
|
|
|
int length; // how long we stay on this level before going to the next
|
|
|
|
int scrollDelay; // how long between scrolls
|
|
|
|
};
|
2014-02-08 02:15:48 +00:00
|
|
|
|
2014-03-01 21:02:44 +00:00
|
|
|
struct ScrollTierList
|
|
|
|
{
|
|
|
|
const int count;
|
|
|
|
const ScrollTier* tiers;
|
|
|
|
};
|
|
|
|
|
|
|
|
// default scroll tiers
|
|
|
|
const ScrollTier QUICK_SCROLL_TIERS[] = {
|
2014-02-08 03:45:28 +00:00
|
|
|
{500, 500},
|
2016-11-30 18:15:13 +00:00
|
|
|
{2000, 114},
|
|
|
|
{4000, 32},
|
|
|
|
{0, 16}
|
2014-02-08 03:45:28 +00:00
|
|
|
};
|
2016-11-30 18:15:13 +00:00
|
|
|
const ScrollTierList LIST_SCROLL_STYLE_QUICK = { 4, QUICK_SCROLL_TIERS };
|
2014-03-01 21:02:44 +00:00
|
|
|
|
|
|
|
const ScrollTier SLOW_SCROLL_TIERS[] = {
|
|
|
|
{500, 500},
|
2017-05-17 12:01:15 +00:00
|
|
|
{0, 200}
|
2014-03-01 21:02:44 +00:00
|
|
|
};
|
|
|
|
const ScrollTierList LIST_SCROLL_STYLE_SLOW = { 2, SLOW_SCROLL_TIERS };
|
2014-02-08 02:15:48 +00:00
|
|
|
|
2014-02-08 03:45:28 +00:00
|
|
|
template <typename EntryData, typename UserData>
|
2014-02-13 23:10:28 +00:00
|
|
|
class IList : public GuiComponent
|
2014-02-08 03:45:28 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
struct Entry
|
2014-02-08 02:15:48 +00:00
|
|
|
{
|
2014-02-08 03:45:28 +00:00
|
|
|
std::string name;
|
|
|
|
UserData object;
|
|
|
|
EntryData data;
|
2014-02-08 02:15:48 +00:00
|
|
|
};
|
|
|
|
|
2014-02-08 03:45:28 +00:00
|
|
|
protected:
|
|
|
|
int mCursor;
|
2014-02-08 02:15:48 +00:00
|
|
|
|
|
|
|
int mScrollTier;
|
|
|
|
int mScrollVelocity;
|
|
|
|
|
|
|
|
int mScrollTierAccumulator;
|
|
|
|
int mScrollCursorAccumulator;
|
2014-02-08 03:45:28 +00:00
|
|
|
|
2014-02-13 23:10:28 +00:00
|
|
|
unsigned char mTitleOverlayOpacity;
|
|
|
|
unsigned int mTitleOverlayColor;
|
|
|
|
ImageComponent mGradient;
|
|
|
|
std::shared_ptr<Font> mTitleOverlayFont;
|
|
|
|
|
2014-03-01 21:02:44 +00:00
|
|
|
const ScrollTierList& mTierList;
|
|
|
|
const ListLoopType mLoopType;
|
|
|
|
|
2014-02-08 03:45:28 +00:00
|
|
|
std::vector<Entry> mEntries;
|
2019-08-25 15:23:02 +00:00
|
|
|
|
2014-02-08 03:45:28 +00:00
|
|
|
public:
|
2019-08-25 15:23:02 +00:00
|
|
|
IList(Window* window, const ScrollTierList& tierList = LIST_SCROLL_STYLE_QUICK, const ListLoopType& loopType = LIST_PAUSE_AT_END) : GuiComponent(window),
|
2014-03-01 21:02:44 +00:00
|
|
|
mGradient(window), mTierList(tierList), mLoopType(loopType)
|
2014-02-08 03:45:28 +00:00
|
|
|
{
|
|
|
|
mCursor = 0;
|
|
|
|
mScrollTier = 0;
|
|
|
|
mScrollVelocity = 0;
|
|
|
|
mScrollTierAccumulator = 0;
|
|
|
|
mScrollCursorAccumulator = 0;
|
2019-08-25 15:23:02 +00:00
|
|
|
|
2014-02-13 23:10:28 +00:00
|
|
|
mTitleOverlayOpacity = 0x00;
|
|
|
|
mTitleOverlayColor = 0xFFFFFF00;
|
2014-02-16 18:27:58 +00:00
|
|
|
mGradient.setResize((float)Renderer::getScreenWidth(), (float)Renderer::getScreenHeight());
|
2014-02-13 23:10:28 +00:00
|
|
|
mGradient.setImage(":/scroll_gradient.png");
|
|
|
|
mTitleOverlayFont = Font::get(FONT_SIZE_LARGE);
|
2014-02-08 03:45:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool isScrolling() const
|
|
|
|
{
|
|
|
|
return (mScrollVelocity != 0 && mScrollTier > 0);
|
|
|
|
}
|
|
|
|
|
2019-08-25 15:23:02 +00:00
|
|
|
int getScrollingVelocity()
|
2017-05-17 12:01:15 +00:00
|
|
|
{
|
|
|
|
return mScrollVelocity;
|
|
|
|
}
|
|
|
|
|
2014-02-08 03:45:28 +00:00
|
|
|
void stopScrolling()
|
|
|
|
{
|
|
|
|
listInput(0);
|
|
|
|
onCursorChanged(CURSOR_STOPPED);
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
mEntries.clear();
|
|
|
|
mCursor = 0;
|
|
|
|
listInput(0);
|
|
|
|
onCursorChanged(CURSOR_STOPPED);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline const std::string& getSelectedName()
|
|
|
|
{
|
|
|
|
assert(size() > 0);
|
|
|
|
return mEntries.at(mCursor).name;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline const UserData& getSelected() const
|
|
|
|
{
|
|
|
|
assert(size() > 0);
|
|
|
|
return mEntries.at(mCursor).object;
|
|
|
|
}
|
|
|
|
|
2017-11-11 14:56:22 +00:00
|
|
|
void setCursor(typename std::vector<Entry>::const_iterator& it)
|
2014-02-08 03:45:28 +00:00
|
|
|
{
|
2017-11-11 14:56:22 +00:00
|
|
|
assert(it != mEntries.cend());
|
|
|
|
mCursor = it - mEntries.cbegin();
|
2014-02-08 03:45:28 +00:00
|
|
|
onCursorChanged(CURSOR_STOPPED);
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns true if successful (select is in our list), false if not
|
|
|
|
bool setCursor(const UserData& obj)
|
|
|
|
{
|
2017-11-11 14:56:22 +00:00
|
|
|
for(auto it = mEntries.cbegin(); it != mEntries.cend(); it++)
|
2014-02-08 03:45:28 +00:00
|
|
|
{
|
|
|
|
if((*it).object == obj)
|
|
|
|
{
|
2017-11-17 14:58:52 +00:00
|
|
|
mCursor = (int)(it - mEntries.cbegin());
|
2014-02-08 03:45:28 +00:00
|
|
|
onCursorChanged(CURSOR_STOPPED);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2019-08-25 15:23:02 +00:00
|
|
|
|
2014-02-08 03:45:28 +00:00
|
|
|
// entry management
|
2014-03-01 21:02:44 +00:00
|
|
|
void add(const Entry& e)
|
2014-02-08 03:45:28 +00:00
|
|
|
{
|
|
|
|
mEntries.push_back(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool remove(const UserData& obj)
|
|
|
|
{
|
2017-11-11 14:56:22 +00:00
|
|
|
for(auto it = mEntries.cbegin(); it != mEntries.cend(); it++)
|
2014-02-08 03:45:28 +00:00
|
|
|
{
|
|
|
|
if((*it).object == obj)
|
|
|
|
{
|
|
|
|
remove(it);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-11-17 14:58:52 +00:00
|
|
|
inline int size() const { return (int)mEntries.size(); }
|
2014-02-08 03:45:28 +00:00
|
|
|
|
|
|
|
protected:
|
2017-11-11 14:56:22 +00:00
|
|
|
void remove(typename std::vector<Entry>::const_iterator& it)
|
2014-02-08 03:45:28 +00:00
|
|
|
{
|
2017-11-11 14:56:22 +00:00
|
|
|
if(mCursor > 0 && it - mEntries.cbegin() <= mCursor)
|
2014-02-08 03:45:28 +00:00
|
|
|
{
|
2014-02-17 17:40:31 +00:00
|
|
|
mCursor--;
|
2014-02-08 03:45:28 +00:00
|
|
|
onCursorChanged(CURSOR_STOPPED);
|
|
|
|
}
|
|
|
|
|
|
|
|
mEntries.erase(it);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-01 21:02:44 +00:00
|
|
|
bool listInput(int velocity) // a velocity of 0 = stop scrolling
|
2014-02-08 03:45:28 +00:00
|
|
|
{
|
2017-09-21 17:44:00 +00:00
|
|
|
PowerSaver::setState(velocity == 0);
|
|
|
|
|
2014-03-24 22:55:36 +00:00
|
|
|
// generate an onCursorChanged event in the stopped state when the user lets go of the key
|
|
|
|
if(velocity == 0 && mScrollVelocity != 0)
|
|
|
|
onCursorChanged(CURSOR_STOPPED);
|
|
|
|
|
2014-02-08 03:45:28 +00:00
|
|
|
mScrollVelocity = velocity;
|
|
|
|
mScrollTier = 0;
|
|
|
|
mScrollTierAccumulator = 0;
|
|
|
|
mScrollCursorAccumulator = 0;
|
2014-03-01 21:02:44 +00:00
|
|
|
|
|
|
|
int prevCursor = mCursor;
|
2014-02-08 03:45:28 +00:00
|
|
|
scroll(mScrollVelocity);
|
2014-03-01 21:02:44 +00:00
|
|
|
return (prevCursor != mCursor);
|
2014-02-08 03:45:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void listUpdate(int deltaTime)
|
|
|
|
{
|
2014-02-13 23:10:28 +00:00
|
|
|
// update the title overlay opacity
|
2014-03-01 21:02:44 +00:00
|
|
|
const int dir = (mScrollTier >= mTierList.count - 1) ? 1 : -1; // fade in if scroll tier is >= 1, otherwise fade out
|
2014-02-13 23:10:28 +00:00
|
|
|
int op = mTitleOverlayOpacity + deltaTime*dir; // we just do a 1-to-1 time -> opacity, no scaling
|
|
|
|
if(op >= 255)
|
|
|
|
mTitleOverlayOpacity = 255;
|
|
|
|
else if(op <= 0)
|
|
|
|
mTitleOverlayOpacity = 0;
|
|
|
|
else
|
|
|
|
mTitleOverlayOpacity = (unsigned char)op;
|
|
|
|
|
2014-02-08 03:45:28 +00:00
|
|
|
if(mScrollVelocity == 0 || size() < 2)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mScrollCursorAccumulator += deltaTime;
|
|
|
|
mScrollTierAccumulator += deltaTime;
|
|
|
|
|
2014-02-13 23:10:28 +00:00
|
|
|
// we delay scrolling until after scroll tier has updated so isScrolling() returns accurately during onCursorChanged callbacks
|
|
|
|
// we don't just do scroll tier first because it would not catch the scrollDelay == tier length case
|
2014-02-08 04:17:24 +00:00
|
|
|
int scrollCount = 0;
|
2014-03-01 21:02:44 +00:00
|
|
|
while(mScrollCursorAccumulator >= mTierList.tiers[mScrollTier].scrollDelay)
|
2014-02-08 03:45:28 +00:00
|
|
|
{
|
2014-03-01 21:02:44 +00:00
|
|
|
mScrollCursorAccumulator -= mTierList.tiers[mScrollTier].scrollDelay;
|
2014-02-08 04:17:24 +00:00
|
|
|
scrollCount++;
|
2014-02-08 03:45:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// are we ready to go even FASTER?
|
2014-03-01 21:02:44 +00:00
|
|
|
while(mScrollTier < mTierList.count - 1 && mScrollTierAccumulator >= mTierList.tiers[mScrollTier].length)
|
2014-02-08 03:45:28 +00:00
|
|
|
{
|
2014-03-01 21:02:44 +00:00
|
|
|
mScrollTierAccumulator -= mTierList.tiers[mScrollTier].length;
|
2014-02-08 03:45:28 +00:00
|
|
|
mScrollTier++;
|
|
|
|
}
|
2014-02-08 04:17:24 +00:00
|
|
|
|
2014-02-13 23:10:28 +00:00
|
|
|
// actually perform the scrolling
|
2014-02-08 04:17:24 +00:00
|
|
|
for(int i = 0; i < scrollCount; i++)
|
|
|
|
scroll(mScrollVelocity);
|
2014-02-08 03:45:28 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 14:58:52 +00:00
|
|
|
void listRenderTitleOverlay(const Transform4x4f& /*trans*/)
|
2014-02-13 23:10:28 +00:00
|
|
|
{
|
|
|
|
if(size() == 0 || !mTitleOverlayFont || mTitleOverlayOpacity == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// we don't bother caching this because it's only two letters and will change pretty much every frame if we're scrolling
|
|
|
|
const std::string text = getSelectedName().size() >= 2 ? getSelectedName().substr(0, 2) : "??";
|
|
|
|
|
2017-10-28 20:24:35 +00:00
|
|
|
Vector2f off = mTitleOverlayFont->sizeText(text);
|
2014-02-16 18:27:58 +00:00
|
|
|
off[0] = (Renderer::getScreenWidth() - off.x()) * 0.5f;
|
|
|
|
off[1] = (Renderer::getScreenHeight() - off.y()) * 0.5f;
|
2019-08-25 15:23:02 +00:00
|
|
|
|
2017-10-28 20:24:35 +00:00
|
|
|
Transform4x4f identTrans = Transform4x4f::Identity();
|
2014-02-13 23:10:28 +00:00
|
|
|
|
2014-02-16 18:27:58 +00:00
|
|
|
mGradient.setOpacity(mTitleOverlayOpacity);
|
|
|
|
mGradient.render(identTrans);
|
2014-05-01 19:47:33 +00:00
|
|
|
|
|
|
|
TextCache* cache = mTitleOverlayFont->buildTextCache(text, off.x(), off.y(), 0xFFFFFF00 | mTitleOverlayOpacity);
|
|
|
|
mTitleOverlayFont->renderTextCache(cache); // relies on mGradient's render for Renderer::setMatrix()
|
|
|
|
delete cache;
|
2014-02-13 23:10:28 +00:00
|
|
|
}
|
|
|
|
|
2014-02-08 03:45:28 +00:00
|
|
|
void scroll(int amt)
|
|
|
|
{
|
|
|
|
if(mScrollVelocity == 0 || size() < 2)
|
|
|
|
return;
|
|
|
|
|
|
|
|
int cursor = mCursor + 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
|
2014-03-01 21:02:44 +00:00
|
|
|
if((mLoopType == LIST_PAUSE_AT_END && (mScrollTier > 0 || absAmt > 1)) ||
|
|
|
|
mLoopType == LIST_NEVER_LOOP)
|
2014-02-08 03:45:28 +00:00
|
|
|
{
|
|
|
|
if(cursor < 0)
|
2014-03-24 22:55:36 +00:00
|
|
|
{
|
2014-02-08 03:45:28 +00:00
|
|
|
cursor = 0;
|
2014-03-24 22:55:36 +00:00
|
|
|
mScrollVelocity = 0;
|
|
|
|
mScrollTier = 0;
|
|
|
|
}else if(cursor >= size())
|
|
|
|
{
|
2014-02-08 03:45:28 +00:00
|
|
|
cursor = size() - 1;
|
2014-03-24 22:55:36 +00:00
|
|
|
mScrollVelocity = 0;
|
|
|
|
mScrollTier = 0;
|
|
|
|
}
|
2014-02-08 03:45:28 +00:00
|
|
|
}else{
|
2014-03-01 21:02:44 +00:00
|
|
|
while(cursor < 0)
|
2014-02-08 03:45:28 +00:00
|
|
|
cursor += size();
|
2014-03-01 21:02:44 +00:00
|
|
|
while(cursor >= size())
|
2014-02-08 03:45:28 +00:00
|
|
|
cursor -= size();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(cursor != mCursor)
|
2020-05-15 16:08:26 +00:00
|
|
|
onScroll();
|
2014-02-08 03:45:28 +00:00
|
|
|
|
|
|
|
mCursor = cursor;
|
|
|
|
onCursorChanged((mScrollTier > 0) ? CURSOR_SCROLLING : CURSOR_STOPPED);
|
|
|
|
}
|
|
|
|
|
2017-11-17 14:58:52 +00:00
|
|
|
virtual void onCursorChanged(const CursorState& /*state*/) {}
|
2020-05-15 16:08:26 +00:00
|
|
|
virtual void onScroll() {}
|
2014-02-08 02:15:48 +00:00
|
|
|
};
|
2017-10-31 17:12:50 +00:00
|
|
|
|
|
|
|
#endif // ES_CORE_COMPONENTS_ILIST_H
|