2020-06-06 14:48:05 +00:00
|
|
|
//
|
|
|
|
// ComponentList.cpp
|
|
|
|
//
|
|
|
|
// Used to lay out and navigate lists in GUI menus.
|
|
|
|
//
|
|
|
|
|
2014-06-20 01:30:09 +00:00
|
|
|
#include "components/ComponentList.h"
|
2017-11-01 22:21:10 +00:00
|
|
|
|
2014-03-04 22:48:33 +00:00
|
|
|
#define TOTAL_HORIZONTAL_PADDING_PX 20
|
2014-03-01 21:02:44 +00:00
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
ComponentList::ComponentList(Window* window) : IList<ComponentListRow,
|
|
|
|
void*>(window, LIST_SCROLL_STYLE_SLOW, LIST_NEVER_LOOP)
|
2014-03-01 21:02:44 +00:00
|
|
|
{
|
|
|
|
mSelectorBarOffset = 0;
|
2014-03-02 16:41:02 +00:00
|
|
|
mCameraOffset = 0;
|
2014-03-12 03:00:08 +00:00
|
|
|
mFocused = false;
|
2014-03-01 21:02:44 +00:00
|
|
|
}
|
|
|
|
|
2014-03-06 19:45:03 +00:00
|
|
|
void ComponentList::addRow(const ComponentListRow& row, bool setCursorHere)
|
2014-03-01 21:02:44 +00:00
|
|
|
{
|
|
|
|
IList<ComponentListRow, void*>::Entry e;
|
|
|
|
e.name = "";
|
|
|
|
e.object = NULL;
|
|
|
|
e.data = row;
|
|
|
|
|
|
|
|
this->add(e);
|
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
for (auto it = mEntries.back().data.elements.cbegin();
|
|
|
|
it != mEntries.back().data.elements.cend(); it++)
|
2014-03-01 21:02:44 +00:00
|
|
|
addChild(it->component.get());
|
|
|
|
|
|
|
|
updateElementSize(mEntries.back().data);
|
|
|
|
updateElementPosition(mEntries.back().data);
|
2014-03-06 19:45:03 +00:00
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
if (setCursorHere) {
|
2017-11-17 14:58:52 +00:00
|
|
|
mCursor = (int)mEntries.size() - 1;
|
2014-03-06 19:45:03 +00:00
|
|
|
onCursorChanged(CURSOR_STOPPED);
|
|
|
|
}
|
2014-03-01 21:02:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ComponentList::onSizeChanged()
|
|
|
|
{
|
2020-06-06 14:48:05 +00:00
|
|
|
for (auto it = mEntries.cbegin(); it != mEntries.cend(); it++) {
|
2014-03-01 21:02:44 +00:00
|
|
|
updateElementSize(it->data);
|
|
|
|
updateElementPosition(it->data);
|
|
|
|
}
|
2014-03-02 18:36:23 +00:00
|
|
|
|
2014-03-24 22:55:36 +00:00
|
|
|
updateCameraOffset();
|
2014-03-01 21:02:44 +00:00
|
|
|
}
|
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
void ComponentList::onFocusLost()
|
|
|
|
{
|
|
|
|
mFocused = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ComponentList::onFocusGained()
|
|
|
|
{
|
|
|
|
mFocused = true;
|
|
|
|
}
|
|
|
|
|
2014-03-01 21:02:44 +00:00
|
|
|
bool ComponentList::input(InputConfig* config, Input input)
|
|
|
|
{
|
2020-06-06 14:48:05 +00:00
|
|
|
if (size() == 0)
|
2014-03-01 21:02:44 +00:00
|
|
|
return false;
|
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
// Give it to the current row's input handler.
|
|
|
|
if (mEntries.at(mCursor).data.input_handler) {
|
|
|
|
if (mEntries.at(mCursor).data.input_handler(config, input))
|
2014-03-02 18:36:23 +00:00
|
|
|
return true;
|
2020-06-06 14:48:05 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// No input handler assigned, do the default, which is to give it
|
|
|
|
// to the rightmost element in the row.
|
2014-03-02 18:36:23 +00:00
|
|
|
auto& row = mEntries.at(mCursor).data;
|
2020-06-06 14:48:05 +00:00
|
|
|
if (row.elements.size()) {
|
|
|
|
if (row.elements.back().component->input(config, input))
|
2014-03-02 18:36:23 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2014-03-01 21:02:44 +00:00
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
// Input handler didn't consume the input - try to scroll.
|
|
|
|
if (config->isMappedLike("up", input))
|
2014-03-01 21:02:44 +00:00
|
|
|
return listInput(input.value != 0 ? -1 : 0);
|
2020-06-06 14:48:05 +00:00
|
|
|
else if (config->isMappedLike("down", input))
|
2014-03-01 21:02:44 +00:00
|
|
|
return listInput(input.value != 0 ? 1 : 0);
|
2020-06-06 14:48:05 +00:00
|
|
|
else if (config->isMappedLike("leftshoulder", input))
|
2017-03-18 17:54:39 +00:00
|
|
|
return listInput(input.value != 0 ? -6 : 0);
|
2020-06-06 14:48:05 +00:00
|
|
|
else if (config->isMappedLike("rightshoulder", input))
|
2017-03-18 17:54:39 +00:00
|
|
|
return listInput(input.value != 0 ? 6 : 0);
|
2014-03-01 21:02:44 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ComponentList::update(int deltaTime)
|
|
|
|
{
|
|
|
|
listUpdate(deltaTime);
|
2014-03-02 18:36:23 +00:00
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
if (size()) {
|
|
|
|
// Update our currently selected row.
|
|
|
|
for (auto it = mEntries.at(mCursor).data.elements.cbegin();
|
|
|
|
it != mEntries.at(mCursor).data.elements.cend(); it++)
|
2014-03-02 18:36:23 +00:00
|
|
|
it->component->update(deltaTime);
|
|
|
|
}
|
2014-03-01 21:02:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ComponentList::onCursorChanged(const CursorState& state)
|
|
|
|
{
|
2020-06-06 14:48:05 +00:00
|
|
|
// Update the selector bar position.
|
|
|
|
// In the future this might be animated.
|
2014-03-01 21:02:44 +00:00
|
|
|
mSelectorBarOffset = 0;
|
2020-06-06 14:48:05 +00:00
|
|
|
for (int i = 0; i < mCursor; i++)
|
2014-03-01 21:02:44 +00:00
|
|
|
mSelectorBarOffset += getRowHeight(mEntries.at(i).data);
|
2014-03-02 16:41:02 +00:00
|
|
|
|
2014-03-24 22:55:36 +00:00
|
|
|
updateCameraOffset();
|
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
// This is terribly inefficient but we don't know what we came from so...
|
|
|
|
if (size()) {
|
|
|
|
for (auto it = mEntries.cbegin(); it != mEntries.cend(); it++)
|
2014-03-24 22:55:36 +00:00
|
|
|
it->data.elements.back().component->onFocusLost();
|
2017-05-18 10:16:57 +00:00
|
|
|
|
2014-03-24 22:55:36 +00:00
|
|
|
mEntries.at(mCursor).data.elements.back().component->onFocusGained();
|
|
|
|
}
|
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
if (mCursorChangedCallback)
|
2014-03-24 22:55:36 +00:00
|
|
|
mCursorChangedCallback(state);
|
|
|
|
|
|
|
|
updateHelpPrompts();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ComponentList::updateCameraOffset()
|
|
|
|
{
|
2020-06-06 14:48:05 +00:00
|
|
|
// Move the camera to scroll.
|
2014-03-02 18:36:23 +00:00
|
|
|
const float totalHeight = getTotalRowHeight();
|
2020-06-06 14:48:05 +00:00
|
|
|
if (totalHeight > mSize.y()) {
|
|
|
|
float target = mSelectorBarOffset + getRowHeight(mEntries.at(mCursor).data)/2 -
|
|
|
|
(mSize.y() / 2);
|
2014-03-24 20:34:38 +00:00
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
// Clamp it.
|
2014-03-24 20:34:38 +00:00
|
|
|
mCameraOffset = 0;
|
|
|
|
unsigned int i = 0;
|
2020-06-06 14:48:05 +00:00
|
|
|
while (mCameraOffset < target && i < mEntries.size()) {
|
2014-03-24 20:34:38 +00:00
|
|
|
mCameraOffset += getRowHeight(mEntries.at(i).data);
|
|
|
|
i++;
|
|
|
|
}
|
2014-03-02 16:41:02 +00:00
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
if (mCameraOffset < 0)
|
2014-03-02 18:36:23 +00:00
|
|
|
mCameraOffset = 0;
|
2020-06-06 14:48:05 +00:00
|
|
|
else if (mCameraOffset + mSize.y() > totalHeight)
|
2014-03-02 18:36:23 +00:00
|
|
|
mCameraOffset = totalHeight - mSize.y();
|
2020-06-06 14:48:05 +00:00
|
|
|
}
|
|
|
|
else {
|
2014-03-19 00:55:37 +00:00
|
|
|
mCameraOffset = 0;
|
2014-03-02 18:36:23 +00:00
|
|
|
}
|
2014-03-01 21:02:44 +00:00
|
|
|
}
|
|
|
|
|
2017-10-28 20:24:35 +00:00
|
|
|
void ComponentList::render(const Transform4x4f& parentTrans)
|
2014-03-01 21:02:44 +00:00
|
|
|
{
|
2020-06-06 14:48:05 +00:00
|
|
|
if (!size())
|
2014-03-06 01:49:32 +00:00
|
|
|
return;
|
|
|
|
|
2017-10-28 20:24:35 +00:00
|
|
|
Transform4x4f trans = parentTrans * getTransform();
|
2014-03-19 20:03:23 +00:00
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
// Clip everything to be inside our bounds.
|
2017-10-28 20:24:35 +00:00
|
|
|
Vector3f dim(mSize.x(), mSize.y(), 0);
|
2014-03-01 21:02:44 +00:00
|
|
|
dim = trans * dim - trans.translation();
|
2017-10-28 20:24:35 +00:00
|
|
|
Renderer::pushClipRect(Vector2i((int)trans.translation().x(), (int)trans.translation().y()),
|
2017-11-13 22:16:38 +00:00
|
|
|
Vector2i((int)Math::round(dim.x()), (int)Math::round(dim.y() + 1)));
|
2014-03-01 21:02:44 +00:00
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
// Scroll the camera.
|
2017-11-13 22:16:38 +00:00
|
|
|
trans.translate(Vector3f(0, -Math::round(mCameraOffset), 0));
|
2014-03-02 16:41:02 +00:00
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
// Draw our entries.
|
2014-03-21 19:51:25 +00:00
|
|
|
std::vector<GuiComponent*> drawAfterCursor;
|
|
|
|
bool drawAll;
|
2020-06-06 14:48:05 +00:00
|
|
|
for (unsigned int i = 0; i < mEntries.size(); i++) {
|
2014-03-21 19:51:25 +00:00
|
|
|
auto& entry = mEntries.at(i);
|
2017-10-28 20:07:31 +00:00
|
|
|
drawAll = !mFocused || i != (unsigned int)mCursor;
|
2020-06-06 14:48:05 +00:00
|
|
|
for (auto it = entry.data.elements.cbegin(); it != entry.data.elements.cend(); it++) {
|
|
|
|
if (drawAll || it->invert_when_selected)
|
2014-03-21 19:51:25 +00:00
|
|
|
it->component->render(trans);
|
2020-06-06 14:48:05 +00:00
|
|
|
else
|
2014-03-21 19:51:25 +00:00
|
|
|
drawAfterCursor.push_back(it->component.get());
|
|
|
|
}
|
|
|
|
}
|
2014-03-01 21:02:44 +00:00
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
// Custom rendering.
|
2014-03-01 21:02:44 +00:00
|
|
|
Renderer::setMatrix(trans);
|
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
// Draw selector bar.
|
|
|
|
if (mFocused) {
|
|
|
|
// Inversion: src * (1 - dst) + dst * 0 = where src = 1
|
|
|
|
// Need a function that goes roughly 0x777777 -> 0xFFFFFF
|
2014-03-12 03:00:08 +00:00
|
|
|
// and 0xFFFFFF -> 0x777777
|
|
|
|
// (1 - dst) + 0x77
|
2017-05-18 10:16:57 +00:00
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
const float selectedRowHeight = getRowHeight(mEntries.at(mCursor).data);
|
2020-06-06 14:48:05 +00:00
|
|
|
Renderer::drawRect(0.0f, mSelectorBarOffset, mSize.x(), selectedRowHeight,
|
|
|
|
0xFFFFFFFF, 0xFFFFFFFF, false, Renderer::Blend::ONE_MINUS_DST_COLOR,
|
|
|
|
Renderer::Blend::ZERO);
|
|
|
|
Renderer::drawRect(0.0f, mSelectorBarOffset, mSize.x(), selectedRowHeight,
|
|
|
|
0x777777FF, 0x777777FF, false, Renderer::Blend::ONE,
|
|
|
|
Renderer::Blend::ONE);
|
|
|
|
|
|
|
|
// Hack to draw 2px dark on left/right of the bar.
|
|
|
|
Renderer::drawRect(0.0f, mSelectorBarOffset, 2.0f, selectedRowHeight,
|
|
|
|
0x878787FF, 0x878787FF);
|
|
|
|
Renderer::drawRect(mSize.x() - 2.0f, mSelectorBarOffset, 2.0f, selectedRowHeight,
|
|
|
|
0x878787FF, 0x878787FF);
|
|
|
|
|
|
|
|
for (auto it = drawAfterCursor.cbegin(); it != drawAfterCursor.cend(); it++)
|
2014-03-21 19:51:25 +00:00
|
|
|
(*it)->render(trans);
|
2017-05-18 10:16:57 +00:00
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
// Reset matrix if one of these components changed it.
|
|
|
|
if (drawAfterCursor.size())
|
2014-03-21 19:51:25 +00:00
|
|
|
Renderer::setMatrix(trans);
|
2014-03-12 03:00:08 +00:00
|
|
|
}
|
2014-03-04 22:48:33 +00:00
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
// Draw separators.
|
2014-03-01 21:02:44 +00:00
|
|
|
float y = 0;
|
2020-06-06 14:48:05 +00:00
|
|
|
for (unsigned int i = 0; i < mEntries.size(); i++) {
|
2019-07-04 01:10:17 +00:00
|
|
|
Renderer::drawRect(0.0f, y, mSize.x(), 1.0f, 0xC6C7C6FF, 0xC6C7C6FF);
|
2014-03-01 21:02:44 +00:00
|
|
|
y += getRowHeight(mEntries.at(i).data);
|
|
|
|
}
|
2014-03-02 16:41:02 +00:00
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
Renderer::drawRect(0.0f, y, mSize.x(), 1.0f, 0xC6C7C6FF, 0xC6C7C6FF);
|
2014-03-02 16:41:02 +00:00
|
|
|
Renderer::popClipRect();
|
2014-03-01 21:02:44 +00:00
|
|
|
}
|
|
|
|
|
2014-03-02 16:41:02 +00:00
|
|
|
float ComponentList::getRowHeight(const ComponentListRow& row) const
|
2014-03-01 21:02:44 +00:00
|
|
|
{
|
2020-06-06 14:48:05 +00:00
|
|
|
// Returns the highest component height found in the row.
|
2014-03-01 21:02:44 +00:00
|
|
|
float height = 0;
|
2020-06-06 14:48:05 +00:00
|
|
|
for (unsigned int i = 0; i < row.elements.size(); i++) {
|
|
|
|
if (row.elements.at(i).component->getSize().y() > height)
|
2014-03-01 21:02:44 +00:00
|
|
|
height = row.elements.at(i).component->getSize().y();
|
|
|
|
}
|
|
|
|
|
|
|
|
return height;
|
|
|
|
}
|
|
|
|
|
2014-03-02 16:41:02 +00:00
|
|
|
float ComponentList::getTotalRowHeight() const
|
|
|
|
{
|
|
|
|
float height = 0;
|
2020-06-06 14:48:05 +00:00
|
|
|
for (auto it = mEntries.cbegin(); it != mEntries.cend(); it++)
|
2014-03-02 16:41:02 +00:00
|
|
|
height += getRowHeight(it->data);
|
|
|
|
|
|
|
|
return height;
|
|
|
|
}
|
|
|
|
|
2014-03-01 21:02:44 +00:00
|
|
|
void ComponentList::updateElementPosition(const ComponentListRow& row)
|
|
|
|
{
|
|
|
|
float yOffset = 0;
|
2020-06-06 14:48:05 +00:00
|
|
|
for (auto it = mEntries.cbegin(); it != mEntries.cend() && &it->data != &row; it++)
|
2014-03-01 21:02:44 +00:00
|
|
|
yOffset += getRowHeight(it->data);
|
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
// Assumes updateElementSize has already been called.
|
2014-03-01 21:02:44 +00:00
|
|
|
float rowHeight = getRowHeight(row);
|
|
|
|
|
|
|
|
float x = TOTAL_HORIZONTAL_PADDING_PX / 2;
|
2020-06-06 14:48:05 +00:00
|
|
|
for (unsigned int i = 0; i < row.elements.size(); i++) {
|
2014-03-01 21:02:44 +00:00
|
|
|
const auto comp = row.elements.at(i).component;
|
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
// Center vertically.
|
2014-03-01 21:02:44 +00:00
|
|
|
comp->setPosition(x, (rowHeight - comp->getSize().y()) / 2 + yOffset);
|
|
|
|
x += comp->getSize().x();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ComponentList::updateElementSize(const ComponentListRow& row)
|
|
|
|
{
|
|
|
|
float width = mSize.x() - TOTAL_HORIZONTAL_PADDING_PX;
|
|
|
|
std::vector< std::shared_ptr<GuiComponent> > resizeVec;
|
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
for (auto it = row.elements.cbegin(); it != row.elements.cend(); it++) {
|
|
|
|
if (it->resize_width)
|
2014-03-01 21:02:44 +00:00
|
|
|
resizeVec.push_back(it->component);
|
|
|
|
else
|
|
|
|
width -= it->component->getSize().x();
|
|
|
|
}
|
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
// Redistribute the "unused" width equally among the components with resize_width set to true.
|
2014-03-01 21:02:44 +00:00
|
|
|
width = width / resizeVec.size();
|
2020-06-06 14:48:05 +00:00
|
|
|
for (auto it = resizeVec.cbegin(); it != resizeVec.cend(); it++)
|
2014-03-01 21:02:44 +00:00
|
|
|
(*it)->setSize(width, (*it)->getSize().y());
|
|
|
|
}
|
2014-03-13 19:09:50 +00:00
|
|
|
|
2014-03-21 02:47:45 +00:00
|
|
|
void ComponentList::textInput(const char* text)
|
|
|
|
{
|
2020-06-06 14:48:05 +00:00
|
|
|
if (!size())
|
2014-03-21 02:47:45 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
mEntries.at(mCursor).data.elements.back().component->textInput(text);
|
|
|
|
}
|
|
|
|
|
2014-03-13 19:09:50 +00:00
|
|
|
std::vector<HelpPrompt> ComponentList::getHelpPrompts()
|
|
|
|
{
|
2020-06-06 14:48:05 +00:00
|
|
|
if (!size())
|
2014-03-13 19:09:50 +00:00
|
|
|
return std::vector<HelpPrompt>();
|
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
std::vector<HelpPrompt> prompts =
|
|
|
|
mEntries.at(mCursor).data.elements.back().component->getHelpPrompts();
|
2014-03-24 01:33:27 +00:00
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
if (size() > 1) {
|
2014-03-24 01:33:27 +00:00
|
|
|
bool addMovePrompt = true;
|
2020-06-06 14:48:05 +00:00
|
|
|
for (auto it = prompts.cbegin(); it != prompts.cend(); it++) {
|
|
|
|
if (it->first == "up/down" || it->first == "up/down/left/right") {
|
2014-03-24 01:33:27 +00:00
|
|
|
addMovePrompt = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-06-06 14:48:05 +00:00
|
|
|
if (addMovePrompt)
|
2014-05-16 21:21:33 +00:00
|
|
|
prompts.push_back(HelpPrompt("up/down", "choose"));
|
2014-03-24 01:33:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return prompts;
|
2014-03-13 19:09:50 +00:00
|
|
|
}
|
2014-03-22 01:12:57 +00:00
|
|
|
|
|
|
|
bool ComponentList::moveCursor(int amt)
|
|
|
|
{
|
2017-05-18 10:16:57 +00:00
|
|
|
bool ret = listInput(amt);
|
|
|
|
listInput(0);
|
2014-03-22 01:12:57 +00:00
|
|
|
return ret;
|
|
|
|
}
|