2012-07-20 01:08:29 +00:00
|
|
|
#include "GuiList.h"
|
2012-07-21 19:06:24 +00:00
|
|
|
#include <iostream>
|
2012-07-20 01:08:29 +00:00
|
|
|
|
2012-08-02 01:43:55 +00:00
|
|
|
GuiList::GuiList(int offsetX, int offsetY)
|
2012-07-20 01:08:29 +00:00
|
|
|
{
|
|
|
|
mSelection = 0;
|
2012-08-02 01:43:55 +00:00
|
|
|
mScrollDir = 0;
|
|
|
|
mScrolling = 0;
|
|
|
|
mScrollAccumulator = 0;
|
|
|
|
|
|
|
|
mOffsetX = offsetX;
|
|
|
|
mOffsetY = offsetY;
|
|
|
|
|
2012-07-21 19:06:24 +00:00
|
|
|
InputManager::registerComponent(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
GuiList::~GuiList()
|
|
|
|
{
|
|
|
|
InputManager::unregisterComponent(this);
|
2012-07-20 01:08:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GuiList::onRender()
|
|
|
|
{
|
2012-08-02 01:43:55 +00:00
|
|
|
Renderer::FontSize fontsize = Renderer::MEDIUM;
|
|
|
|
|
|
|
|
const int cutoff = mOffsetY;
|
|
|
|
const int entrySize = Renderer::getFontHeight(fontsize) + 5;
|
2012-07-21 20:16:07 +00:00
|
|
|
|
2012-07-21 20:57:53 +00:00
|
|
|
int startEntry = 0;
|
|
|
|
|
2012-07-21 20:16:07 +00:00
|
|
|
//number of entries that can fit on the screen simultaniously
|
|
|
|
int screenCount = (Renderer::getScreenHeight() - cutoff) / entrySize;
|
|
|
|
screenCount -= 1;
|
|
|
|
|
2012-07-27 16:58:27 +00:00
|
|
|
if((int)mRowVector.size() >= screenCount)
|
2012-07-21 20:57:53 +00:00
|
|
|
{
|
|
|
|
startEntry = mSelection - (screenCount * 0.5);
|
|
|
|
if(startEntry < 0)
|
|
|
|
startEntry = 0;
|
2012-07-27 16:58:27 +00:00
|
|
|
if(startEntry >= (int)mRowVector.size() - screenCount)
|
|
|
|
startEntry = mRowVector.size() - screenCount;
|
2012-07-21 20:57:53 +00:00
|
|
|
}
|
2012-07-21 20:16:07 +00:00
|
|
|
|
|
|
|
int y = cutoff;
|
2012-07-21 19:06:24 +00:00
|
|
|
|
2012-07-27 16:58:27 +00:00
|
|
|
if(mRowVector.size() == 0)
|
2012-07-21 19:06:24 +00:00
|
|
|
{
|
2012-08-02 01:43:55 +00:00
|
|
|
Renderer::drawCenteredText("The list is empty.", 0, y, 0xFF0000);
|
2012-07-21 19:06:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-07-21 20:57:53 +00:00
|
|
|
int listCutoff = startEntry + screenCount;
|
2012-07-27 16:58:27 +00:00
|
|
|
if(listCutoff > (int)mRowVector.size())
|
|
|
|
listCutoff = mRowVector.size();
|
2012-07-21 20:57:53 +00:00
|
|
|
|
|
|
|
for(int i = startEntry; i < listCutoff; i++)
|
2012-07-20 16:14:09 +00:00
|
|
|
{
|
2012-07-21 20:16:07 +00:00
|
|
|
if(mSelection == i)
|
2012-07-21 19:06:24 +00:00
|
|
|
{
|
2012-08-02 01:43:55 +00:00
|
|
|
Renderer::drawRect(mOffsetX, y, Renderer::getScreenWidth(), Renderer::getFontHeight(fontsize), 0x000000);
|
2012-07-21 19:06:24 +00:00
|
|
|
}
|
|
|
|
|
2012-07-27 16:58:27 +00:00
|
|
|
ListRow row = mRowVector.at((unsigned int)i);
|
2012-08-02 01:43:55 +00:00
|
|
|
Renderer::drawCenteredText(row.name, mOffsetX, y, row.color);
|
|
|
|
y += entrySize;
|
2012-07-21 19:06:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GuiList::onInput(InputManager::InputButton button, bool keyDown)
|
|
|
|
{
|
2012-08-02 01:43:55 +00:00
|
|
|
if(mRowVector.size() > 0)
|
2012-07-21 19:06:24 +00:00
|
|
|
{
|
2012-08-02 01:43:55 +00:00
|
|
|
if(keyDown)
|
|
|
|
{
|
|
|
|
if(button == InputManager::DOWN)
|
|
|
|
{
|
|
|
|
mScrollDir = 1;
|
|
|
|
mSelection++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(button == InputManager::UP)
|
|
|
|
{
|
|
|
|
mScrollDir = -1;
|
|
|
|
mSelection--;
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
if((button == InputManager::DOWN && mScrollDir > 0) || (button == InputManager::UP && mScrollDir < 0))
|
|
|
|
{
|
|
|
|
mScrollAccumulator = 0;
|
|
|
|
mScrolling = false;
|
|
|
|
mScrollDir = 0;
|
|
|
|
}
|
|
|
|
}
|
2012-07-21 19:06:24 +00:00
|
|
|
|
|
|
|
if(mSelection < 0)
|
2012-07-27 16:58:27 +00:00
|
|
|
mSelection += mRowVector.size();
|
2012-07-21 19:06:24 +00:00
|
|
|
|
2012-07-27 16:58:27 +00:00
|
|
|
if(mSelection >= (int)mRowVector.size())
|
|
|
|
mSelection -= mRowVector.size();
|
2012-07-20 16:14:09 +00:00
|
|
|
}
|
2012-07-20 01:08:29 +00:00
|
|
|
}
|
|
|
|
|
2012-08-02 01:43:55 +00:00
|
|
|
void GuiList::onTick(int deltaTime)
|
|
|
|
{
|
|
|
|
if(mScrollDir != 0)
|
|
|
|
{
|
|
|
|
mScrollAccumulator += deltaTime;
|
|
|
|
|
|
|
|
if(!mScrolling)
|
|
|
|
{
|
|
|
|
if(mScrollAccumulator >= SCROLLDELAY)
|
|
|
|
{
|
|
|
|
mScrollAccumulator = SCROLLTIME;
|
|
|
|
mScrolling = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(mScrolling)
|
|
|
|
{
|
|
|
|
mScrollAccumulator += deltaTime;
|
|
|
|
|
|
|
|
while(mScrollAccumulator >= SCROLLTIME)
|
|
|
|
{
|
|
|
|
mScrollAccumulator -= SCROLLTIME;
|
|
|
|
|
|
|
|
mSelection += mScrollDir;
|
|
|
|
if(mSelection < 0)
|
|
|
|
mSelection += mRowVector.size();
|
|
|
|
if(mSelection >= (int)mRowVector.size())
|
|
|
|
mSelection -= mRowVector.size();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//list management stuff
|
2012-07-27 16:58:27 +00:00
|
|
|
void GuiList::addObject(std::string name, void* obj, int color)
|
2012-07-20 01:08:29 +00:00
|
|
|
{
|
2012-07-27 16:58:27 +00:00
|
|
|
ListRow row = {name, obj, color};
|
|
|
|
mRowVector.push_back(row);
|
2012-07-20 01:08:29 +00:00
|
|
|
}
|
|
|
|
|
2012-07-20 16:14:09 +00:00
|
|
|
void GuiList::clear()
|
2012-07-20 01:08:29 +00:00
|
|
|
{
|
2012-07-27 16:58:27 +00:00
|
|
|
mRowVector.clear();
|
2012-07-21 20:57:53 +00:00
|
|
|
mSelection = 0;
|
2012-07-20 01:08:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string GuiList::getSelectedName()
|
|
|
|
{
|
2012-07-27 16:58:27 +00:00
|
|
|
return mRowVector.at(mSelection).name;
|
2012-07-20 01:08:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void* GuiList::getSelectedObject()
|
|
|
|
{
|
2012-07-27 16:58:27 +00:00
|
|
|
return mRowVector.at(mSelection).object;
|
2012-07-20 01:08:29 +00:00
|
|
|
}
|
2012-07-21 19:06:24 +00:00
|
|
|
|
|
|
|
int GuiList::getSelection()
|
|
|
|
{
|
|
|
|
return mSelection;
|
|
|
|
}
|