2013-10-01 21:52:30 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "../GuiComponent.h"
|
2013-10-04 23:24:41 +00:00
|
|
|
#include "../resources/Font.h"
|
2013-10-01 21:52:30 +00:00
|
|
|
#include <vector>
|
|
|
|
#include <functional>
|
2013-10-03 20:58:09 +00:00
|
|
|
#include "../Renderer.h"
|
2013-10-06 02:56:06 +00:00
|
|
|
#include "NinePatchComponent.h"
|
2013-10-01 21:52:30 +00:00
|
|
|
|
|
|
|
//Used to display a list of options.
|
|
|
|
//Can select one or multiple options.
|
|
|
|
|
2013-10-03 20:58:09 +00:00
|
|
|
template<typename T>
|
2013-10-01 21:52:30 +00:00
|
|
|
class OptionListComponent : public GuiComponent
|
|
|
|
{
|
|
|
|
public:
|
2013-10-06 02:56:06 +00:00
|
|
|
OptionListComponent(Window* window, bool multiSelect = false) : GuiComponent(window),
|
2013-10-12 19:03:32 +00:00
|
|
|
mMultiSelect(multiSelect)
|
2013-10-03 20:58:09 +00:00
|
|
|
{
|
2013-10-12 19:03:32 +00:00
|
|
|
if(multiSelect)
|
|
|
|
setSize(getFont()->sizeText("0 selected"));
|
|
|
|
else
|
|
|
|
setSize(getFont()->sizeText("Not set"));
|
2013-10-03 20:58:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct ListEntry
|
|
|
|
{
|
|
|
|
std::string text;
|
|
|
|
bool selected;
|
|
|
|
T object;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-10-12 19:03:32 +00:00
|
|
|
bool input(InputConfig* config, Input input) override
|
2013-10-03 20:58:09 +00:00
|
|
|
{
|
|
|
|
if(input.value != 0)
|
|
|
|
{
|
|
|
|
if(config->isMappedTo("a", input))
|
|
|
|
{
|
2013-10-12 19:03:32 +00:00
|
|
|
open();
|
2013-10-03 20:58:09 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return GuiComponent::input(config, input);
|
|
|
|
}
|
|
|
|
|
|
|
|
void render(const Eigen::Affine3f& parentTrans)
|
|
|
|
{
|
|
|
|
std::shared_ptr<Font> font = getFont();
|
|
|
|
|
2013-10-12 19:03:32 +00:00
|
|
|
Renderer::setMatrix(parentTrans * getTransform());
|
2013-10-06 02:56:06 +00:00
|
|
|
|
2013-10-12 19:03:32 +00:00
|
|
|
unsigned int color = 0x000000FF;
|
2013-10-06 02:56:06 +00:00
|
|
|
|
2013-10-12 19:03:32 +00:00
|
|
|
if(mMultiSelect)
|
|
|
|
{
|
|
|
|
//draw "# selected"
|
|
|
|
unsigned int selectedCount = 0;
|
|
|
|
for(auto it = mEntries.begin(); it != mEntries.end(); it++)
|
2013-10-06 02:56:06 +00:00
|
|
|
{
|
2013-10-12 19:03:32 +00:00
|
|
|
if(it->selected)
|
|
|
|
selectedCount++;
|
2013-10-06 02:56:06 +00:00
|
|
|
}
|
2013-10-01 21:52:30 +00:00
|
|
|
|
2013-10-12 19:03:32 +00:00
|
|
|
std::stringstream ss;
|
|
|
|
ss << selectedCount << " selected";
|
|
|
|
font->drawText(ss.str(), Eigen::Vector2f(0, 0), color);
|
2013-10-01 21:52:30 +00:00
|
|
|
|
2013-10-12 19:03:32 +00:00
|
|
|
}else{
|
|
|
|
//draw selected option
|
|
|
|
bool found = false;
|
|
|
|
for(auto it = mEntries.begin(); it != mEntries.end(); it++)
|
2013-10-06 02:56:06 +00:00
|
|
|
{
|
2013-10-12 19:03:32 +00:00
|
|
|
if(it->selected)
|
2013-10-06 02:56:06 +00:00
|
|
|
{
|
2013-10-12 19:03:32 +00:00
|
|
|
font->drawText(it->text, Eigen::Vector2f(0, 0), color);
|
|
|
|
found = true;
|
|
|
|
break;
|
2013-10-06 02:56:06 +00:00
|
|
|
}
|
|
|
|
}
|
2013-10-12 19:03:32 +00:00
|
|
|
|
|
|
|
if(!found)
|
|
|
|
font->drawText("Not set", Eigen::Vector2f(0, 0), color);
|
2013-10-06 02:56:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
renderChildren(parentTrans * getTransform());
|
2013-10-03 20:58:09 +00:00
|
|
|
}
|
2013-10-01 21:52:30 +00:00
|
|
|
|
2013-10-11 00:55:57 +00:00
|
|
|
ListEntry makeEntry(const std::string& name, T obj, bool selected = false) const
|
2013-10-03 20:58:09 +00:00
|
|
|
{
|
2013-10-11 00:55:57 +00:00
|
|
|
ListEntry e;
|
|
|
|
e.text = name;
|
|
|
|
e.object = obj;
|
|
|
|
e.selected = selected;
|
2013-10-03 20:58:09 +00:00
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
void populate(std::vector<T>& vec, std::function<ListEntry(const T&)> selector)
|
|
|
|
{
|
|
|
|
for(auto it = vec.begin(); it != vec.end(); it++)
|
|
|
|
{
|
|
|
|
ListEntry e = selector(*it);
|
|
|
|
if(!e.text.empty())
|
2013-10-12 19:03:32 +00:00
|
|
|
addEntry(e);
|
2013-10-03 20:58:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void addEntry(ListEntry e)
|
|
|
|
{
|
|
|
|
mEntries.push_back(e);
|
2013-10-12 19:03:32 +00:00
|
|
|
|
|
|
|
Eigen::Vector2f size = getFont()->sizeText(e.text);
|
|
|
|
if(size.x() > mSize.x())
|
|
|
|
setSize(size.x(), mSize.y());
|
2013-10-03 20:58:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<const ListEntry*> getSelected()
|
|
|
|
{
|
|
|
|
std::vector<const ListEntry*> ret;
|
|
|
|
for(auto it = mEntries.begin(); it != mEntries.end(); it++)
|
|
|
|
{
|
|
|
|
if((*it).selected)
|
|
|
|
ret.push_back(&(*it));
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2013-10-11 00:55:57 +00:00
|
|
|
|
|
|
|
std::vector<T> getSelectedObjects()
|
|
|
|
{
|
|
|
|
std::vector<T> ret;
|
|
|
|
for(auto it = mEntries.begin(); it != mEntries.end(); it++)
|
|
|
|
{
|
|
|
|
if((*it).selected)
|
|
|
|
ret.push_back(it->object);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-10-01 21:52:30 +00:00
|
|
|
private:
|
2013-10-12 19:03:32 +00:00
|
|
|
void open()
|
|
|
|
{
|
|
|
|
mWindow->pushGui(new OptionListPopup(mWindow, *this));
|
|
|
|
}
|
|
|
|
|
2013-10-03 20:58:09 +00:00
|
|
|
void select(unsigned int i)
|
|
|
|
{
|
|
|
|
if(i >= mEntries.size())
|
|
|
|
return;
|
|
|
|
|
2013-10-06 02:56:06 +00:00
|
|
|
if(!mMultiSelect)
|
|
|
|
for(auto it = mEntries.begin(); it != mEntries.end(); it++)
|
|
|
|
it->selected = false;
|
|
|
|
|
2013-10-03 20:58:09 +00:00
|
|
|
mEntries.at(i).selected = !mEntries.at(i).selected;
|
|
|
|
}
|
|
|
|
|
2013-10-12 19:03:32 +00:00
|
|
|
std::shared_ptr<Font> getFont()
|
2013-10-03 20:58:09 +00:00
|
|
|
{
|
2013-10-12 19:03:32 +00:00
|
|
|
return Font::get(FONT_SIZE_MEDIUM);
|
2013-10-06 02:56:06 +00:00
|
|
|
}
|
|
|
|
|
2013-10-03 20:58:09 +00:00
|
|
|
|
2013-10-12 19:03:32 +00:00
|
|
|
class OptionListPopup : public GuiComponent
|
2013-10-03 20:58:09 +00:00
|
|
|
{
|
2013-10-12 19:03:32 +00:00
|
|
|
public:
|
|
|
|
OptionListPopup(Window* window, OptionListComponent<T>& optList) : GuiComponent(window),
|
|
|
|
mOptList(optList), mBox(window, ":/textbox.png"), mCursor(0), mScrollOffset(0)
|
2013-10-03 20:58:09 +00:00
|
|
|
{
|
2013-10-12 19:03:32 +00:00
|
|
|
//find global position
|
|
|
|
GuiComponent* p = &mOptList;
|
|
|
|
do {
|
|
|
|
mPosition += p->getPosition();
|
|
|
|
} while(p = p->getParent());
|
|
|
|
|
|
|
|
mSize = mOptList.getSize();
|
|
|
|
updateTextCaches();
|
2013-10-03 20:58:09 +00:00
|
|
|
}
|
|
|
|
|
2013-10-12 19:03:32 +00:00
|
|
|
void render(const Eigen::Affine3f& parentTrans) override
|
2013-10-03 20:58:09 +00:00
|
|
|
{
|
2013-10-12 19:03:32 +00:00
|
|
|
Eigen::Affine3f trans = parentTrans * getTransform();
|
|
|
|
|
|
|
|
std::shared_ptr<Font> font = mOptList.getFont();
|
|
|
|
|
|
|
|
unsigned int renderCount = mTextCaches.size() - mScrollOffset;
|
|
|
|
|
|
|
|
float height = (float)renderCount * font->getHeight();
|
|
|
|
trans.translate(Eigen::Vector3f(0, -height / 2 + font->getHeight() * 0.5f, 0));
|
|
|
|
|
|
|
|
mBox.fitTo(Eigen::Vector2f(mSize.x(), height));
|
|
|
|
mBox.render(trans);
|
|
|
|
|
|
|
|
Renderer::setMatrix(trans);
|
|
|
|
Renderer::drawRect(0, 0, (int)getSize().x(), (int)height, 0xFFFFFFFF);
|
|
|
|
|
|
|
|
for(unsigned int i = mScrollOffset; i < renderCount; i++)
|
|
|
|
{
|
|
|
|
Renderer::setMatrix(trans);
|
2013-10-03 20:58:09 +00:00
|
|
|
|
2013-10-12 19:03:32 +00:00
|
|
|
char rectOpacity = 0x00;
|
|
|
|
if(i == mCursor)
|
|
|
|
rectOpacity += 0x22;
|
|
|
|
if(mOptList.mEntries.at(i).selected)
|
|
|
|
rectOpacity += 0x44;
|
|
|
|
|
|
|
|
Renderer::drawRect(0, 0, (int)mSize.x(), font->getHeight(), 0x00000000 | rectOpacity);
|
|
|
|
|
|
|
|
Renderer::setMatrix(trans);
|
|
|
|
font->renderTextCache(mTextCaches.at(i));
|
|
|
|
|
|
|
|
trans = trans.translate(Eigen::Vector3f(0, (float)font->getHeight(), 0));
|
|
|
|
}
|
2013-10-03 20:58:09 +00:00
|
|
|
}
|
2013-10-06 02:56:06 +00:00
|
|
|
|
2013-10-12 19:03:32 +00:00
|
|
|
bool input(InputConfig* config, Input input)
|
|
|
|
{
|
|
|
|
if(input.value != 0)
|
|
|
|
{
|
|
|
|
if(config->isMappedTo("b", input))
|
|
|
|
{
|
|
|
|
close();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if(config->isMappedTo("a", input))
|
|
|
|
{
|
|
|
|
mOptList.select(mCursor);
|
|
|
|
if(!mOptList.mMultiSelect)
|
|
|
|
close();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if(mOptList.mEntries.size() > 1)
|
|
|
|
{
|
|
|
|
if(config->isMappedTo("up", input))
|
|
|
|
{
|
|
|
|
if(mCursor > 0)
|
|
|
|
mCursor--;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if(config->isMappedTo("down", input))
|
|
|
|
{
|
|
|
|
if(mCursor < mOptList.mEntries.size() - 1)
|
|
|
|
mCursor++;
|
2013-10-03 20:58:09 +00:00
|
|
|
|
2013-10-12 19:03:32 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-10-03 20:58:09 +00:00
|
|
|
|
2013-10-12 19:03:32 +00:00
|
|
|
return GuiComponent::input(config, input);
|
|
|
|
}
|
2013-10-01 21:52:30 +00:00
|
|
|
|
2013-10-12 19:03:32 +00:00
|
|
|
private:
|
|
|
|
void close()
|
|
|
|
{
|
|
|
|
delete this;
|
|
|
|
}
|
2013-10-06 02:56:06 +00:00
|
|
|
|
2013-10-12 19:03:32 +00:00
|
|
|
void updateTextCaches()
|
|
|
|
{
|
|
|
|
for(auto it = mTextCaches.begin(); it != mTextCaches.end(); it++)
|
|
|
|
{
|
|
|
|
delete *it;
|
|
|
|
}
|
|
|
|
mTextCaches.clear();
|
2013-10-01 21:52:30 +00:00
|
|
|
|
2013-10-12 19:03:32 +00:00
|
|
|
TextCache* cache;
|
|
|
|
std::shared_ptr<Font> font = mOptList.getFont();
|
|
|
|
for(unsigned int i = 0; i < mOptList.mEntries.size(); i++)
|
|
|
|
{
|
|
|
|
cache = font->buildTextCache(mOptList.mEntries.at(i).text, 0, 0, 0x000000FF);
|
|
|
|
mTextCaches.push_back(cache);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
OptionListComponent<T>& mOptList;
|
|
|
|
NinePatchComponent mBox;
|
|
|
|
|
|
|
|
unsigned int mCursor;
|
|
|
|
unsigned int mScrollOffset;
|
|
|
|
std::vector<TextCache*> mTextCaches;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool mMultiSelect;
|
|
|
|
|
2013-10-01 21:52:30 +00:00
|
|
|
std::vector<ListEntry> mEntries;
|
|
|
|
};
|