Finally made GuiList a template.

This commit is contained in:
Aloshi 2012-08-01 23:50:18 -05:00
parent eaf7df7ad5
commit cd4ebeafa2
7 changed files with 54 additions and 28 deletions

View file

@ -1,7 +1,7 @@
CC=g++
CFLAGS=-c -Wall
LDFLAGS=-lSDL -lSDL_ttf -lSDL_image -lboost_system -lboost_filesystem
SRCSOURCES=main.cpp Renderer.cpp Renderer_draw.cpp GuiComponent.cpp InputManager.cpp SystemData.cpp GameData.cpp FolderData.cpp XMLReader.cpp components/GuiList.cpp components/GuiGameList.cpp components/GuiInputConfig.cpp components/GuiImage.cpp components/GuiMenu.cpp pugiXML/pugixml.cpp
SRCSOURCES=main.cpp Renderer.cpp Renderer_draw.cpp GuiComponent.cpp InputManager.cpp SystemData.cpp GameData.cpp FolderData.cpp XMLReader.cpp components/GuiGameList.cpp components/GuiInputConfig.cpp components/GuiImage.cpp components/GuiMenu.cpp pugiXML/pugixml.cpp
SOURCES=$(addprefix src/,$(SRCSOURCES))
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=emulationstation

View file

@ -16,12 +16,12 @@ GuiGameList::GuiGameList(bool useDetail)
//Those with smaller displays may prefer the older view.
if(mDetailed)
{
mList = new GuiList(Renderer::getScreenWidth() * 0.4, Renderer::getFontHeight(Renderer::LARGE) + 2);
mList = new GuiList<FileData*>(Renderer::getScreenWidth() * 0.4, Renderer::getFontHeight(Renderer::LARGE) + 2);
mScreenshot = new GuiImage(Renderer::getScreenWidth() * 0.2 - (SCREENSHOTWIDTH / 2), Renderer::getFontHeight(Renderer::LARGE) + 2);
addChild(mScreenshot);
}else{
mList = new GuiList(0, Renderer::getFontHeight(Renderer::LARGE) + 2);
mList = new GuiList<FileData*>(0, Renderer::getFontHeight(Renderer::LARGE) + 2);
mScreenshot = NULL;
}
@ -83,7 +83,7 @@ void GuiGameList::onRender()
Renderer::drawRect(Renderer::getScreenWidth() * 0.4, Renderer::getFontHeight(Renderer::LARGE) + 2, 8, Renderer::getScreenHeight(), 0x0000FF);
//if we have selected a non-folder
if(mList->getSelectedObject() && !((FileData*)mList->getSelectedObject())->isFolder())
if(mList->getSelectedObject() && !mList->getSelectedObject()->isFolder())
{
GameData* game = (GameData*)mList->getSelectedObject();
@ -100,7 +100,7 @@ void GuiGameList::onInput(InputManager::InputButton button, bool keyDown)
{
if(!keyDown)
{
FileData* file = (FileData*)mList->getSelectedObject();
FileData* file = mList->getSelectedObject();
if(file->isFolder())
{
//set current directory to this or something
@ -138,7 +138,7 @@ void GuiGameList::onInput(InputManager::InputButton button, bool keyDown)
{
if(!keyDown && (button == InputManager::UP || button == InputManager::DOWN))
{
if(mList->getSelectedObject() && !((FileData*)mList->getSelectedObject())->isFolder())
if(mList->getSelectedObject() && !mList->getSelectedObject()->isFolder())
{
mScreenshot->setImage(((GameData*)mList->getSelectedObject())->getImagePath());
}else{
@ -167,6 +167,7 @@ void GuiGameList::updateList()
}
//these are called when the menu opens/closes
//the second bit should be moved to GuiList
void GuiGameList::onPause()
{
InputManager::unregisterComponent(this);

View file

@ -31,7 +31,7 @@ private:
int mSystemId;
bool mDetailed;
GuiList* mList;
GuiList<FileData*>* mList;
GuiImage* mScreenshot;
};

View file

@ -1,7 +1,8 @@
#include "GuiList.h"
#include <iostream>
GuiList::GuiList(int offsetX, int offsetY)
template <typename listType>
GuiList<listType>::GuiList(int offsetX, int offsetY)
{
mSelection = 0;
mScrollDir = 0;
@ -14,12 +15,14 @@ GuiList::GuiList(int offsetX, int offsetY)
InputManager::registerComponent(this);
}
GuiList::~GuiList()
template <typename listType>
GuiList<listType>::~GuiList()
{
InputManager::unregisterComponent(this);
}
void GuiList::onRender()
template <typename listType>
void GuiList<listType>::onRender()
{
Renderer::FontSize fontsize = Renderer::MEDIUM;
@ -66,7 +69,8 @@ void GuiList::onRender()
}
}
void GuiList::onInput(InputManager::InputButton button, bool keyDown)
template <typename listType>
void GuiList<listType>::onInput(InputManager::InputButton button, bool keyDown)
{
if(mRowVector.size() > 0)
{
@ -100,7 +104,8 @@ void GuiList::onInput(InputManager::InputButton button, bool keyDown)
}
}
void GuiList::onTick(int deltaTime)
template <typename listType>
void GuiList<listType>::onTick(int deltaTime)
{
if(mScrollDir != 0)
{
@ -134,29 +139,34 @@ void GuiList::onTick(int deltaTime)
}
//list management stuff
void GuiList::addObject(std::string name, void* obj, int color)
template <typename listType>
void GuiList<listType>::addObject(std::string name, listType obj, int color)
{
ListRow row = {name, obj, color};
mRowVector.push_back(row);
}
void GuiList::clear()
template <typename listType>
void GuiList<listType>::clear()
{
mRowVector.clear();
mSelection = 0;
}
std::string GuiList::getSelectedName()
template <typename listType>
std::string GuiList<listType>::getSelectedName()
{
return mRowVector.at(mSelection).name;
}
void* GuiList::getSelectedObject()
template <typename listType>
listType GuiList<listType>::getSelectedObject()
{
return mRowVector.at(mSelection).object;
}
int GuiList::getSelection()
template <typename listType>
int GuiList<listType>::getSelection()
{
return mSelection;
}

View file

@ -10,14 +10,8 @@
#define SCROLLDELAY 507
#define SCROLLTIME (57*6)
struct ListRow
{
std::string name;
void* object;
int color;
};
//this should really be a template
template <typename listType>
class GuiList : public GuiComponent
{
public:
@ -28,19 +22,29 @@ public:
void onTick(int deltaTime);
void onInput(InputManager::InputButton button, bool keyDown);
void addObject(std::string name, void* obj, int color = 0xFF0000);
void addObject(std::string name, listType obj, int color = 0xFF0000);
void clear();
std::string getSelectedName();
void* getSelectedObject();
listType getSelectedObject();
int getSelection();
private:
int mScrollDir, mScrollAccumulator;
bool mScrolling;
int mOffsetX, mOffsetY;
struct ListRow
{
std::string name;
listType object;
int color;
};
std::vector<ListRow> mRowVector;
int mSelection;
};
#include "GuiList.cpp"
#endif

View file

@ -6,7 +6,7 @@ GuiMenu::GuiMenu(GuiComponent* parent)
mParent = parent;
parent->pause();
mList = new GuiList(Renderer::getScreenWidth() * 0.5, 20);
mList = new GuiList<std::string>(Renderer::getScreenWidth() * 0.5, 20);
addChild(mList);
@ -37,11 +37,19 @@ void GuiMenu::onInput(InputManager::InputButton button, bool keyDown)
return;
}
}
if(button == InputManager::BUTTON1 && keyDown)
{
system(mList->getSelectedObject().c_str());
}
}
void GuiMenu::populateList()
{
mList->clear();
mList->addObject("Nothing", "");
mList->addObject("Shutdown", "sudo shutdown -h now");
}
void GuiMenu::onRender()

View file

@ -15,7 +15,10 @@ public:
private:
GuiComponent* mParent;
GuiList* mList;
GuiList<std::string>* mList;
void populateList();
bool mSkippedMenuClose;
};