mirror of
https://github.com/RetroDECK/ES-DE.git
synced 2025-01-17 22:55:38 +00:00
TextEditComponents now resize vertically as needed.
This commit is contained in:
parent
7cb3cc09ee
commit
bed9c1fbb5
|
@ -4,7 +4,10 @@
|
||||||
|
|
||||||
#define INITIAL_CELL_SIZE 12
|
#define INITIAL_CELL_SIZE 12
|
||||||
|
|
||||||
ComponentListComponent::ComponentListComponent(Window* window, Eigen::Vector2i gridDimensions) : GuiComponent(window), mGrid(NULL), mColumnWidths(NULL), mRowHeights(NULL), mCursor(-1, -1)
|
ComponentListComponent::ComponentListComponent(Window* window, Eigen::Vector2i gridDimensions) : GuiComponent(window),
|
||||||
|
mGrid(NULL), mColumnWidths(NULL), mRowHeights(NULL),
|
||||||
|
mColumnWidthForced(NULL), mRowHeightForced(NULL),
|
||||||
|
mCursor(-1, -1)
|
||||||
{
|
{
|
||||||
mEntries.reserve(gridDimensions.x() * gridDimensions.y());
|
mEntries.reserve(gridDimensions.x() * gridDimensions.y());
|
||||||
makeCells(gridDimensions);
|
makeCells(gridDimensions);
|
||||||
|
@ -29,6 +32,12 @@ void ComponentListComponent::makeCells(Eigen::Vector2i size)
|
||||||
mRowHeights = new unsigned int[size.y()];
|
mRowHeights = new unsigned int[size.y()];
|
||||||
std::fill(mRowHeights, mRowHeights + size.y(), INITIAL_CELL_SIZE);
|
std::fill(mRowHeights, mRowHeights + size.y(), INITIAL_CELL_SIZE);
|
||||||
|
|
||||||
|
mColumnWidthForced = new bool[size.x()];
|
||||||
|
std::fill(mColumnWidthForced, mColumnWidthForced + size.x(), false);
|
||||||
|
|
||||||
|
mRowHeightForced = new bool[size.y()];
|
||||||
|
std::fill(mRowHeightForced, mRowHeightForced + size.y(), false);
|
||||||
|
|
||||||
updateSize();
|
updateSize();
|
||||||
resetCursor();
|
resetCursor();
|
||||||
}
|
}
|
||||||
|
@ -68,23 +77,26 @@ void ComponentListComponent::setEntry(Eigen::Vector2i pos, Eigen::Vector2i size,
|
||||||
mCursor = pos;
|
mCursor = pos;
|
||||||
|
|
||||||
//update the column width and row height
|
//update the column width and row height
|
||||||
if(autoFit.x() && (int)getColumnWidth(pos.x()) < component->getSize().x())
|
//if(autoFit.x() && (int)getColumnWidth(pos.x()) < component->getSize().x())
|
||||||
setColumnWidth(pos.x(), (unsigned int)component->getSize().x());
|
// setColumnWidth(pos.x(), (unsigned int)component->getSize().x());
|
||||||
if(autoFit.y() && (int)getRowHeight(pos.y()) < component->getSize().y())
|
//if(autoFit.y() && (int)getRowHeight(pos.y()) < component->getSize().y())
|
||||||
setRowHeight(pos.y(), (unsigned int)component->getSize().y());
|
// setRowHeight(pos.y(), (unsigned int)component->getSize().y());
|
||||||
|
updateCellSize(&mEntries.back(), autoFit.x(), autoFit.y());
|
||||||
|
|
||||||
component->setPosition(getCellOffset(pos));
|
component->setPosition(getCellOffset(pos));
|
||||||
}
|
}
|
||||||
|
|
||||||
void ComponentListComponent::setRowHeight(int row, unsigned int size)
|
void ComponentListComponent::forceRowHeight(int row, unsigned int size)
|
||||||
{
|
{
|
||||||
mRowHeights[row] = size;
|
mRowHeights[row] = size;
|
||||||
|
mRowHeightForced[row] = true;
|
||||||
updateSize();
|
updateSize();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ComponentListComponent::setColumnWidth(int col, unsigned int size)
|
void ComponentListComponent::forceColumnWidth(int col, unsigned int size)
|
||||||
{
|
{
|
||||||
mColumnWidths[col] = size;
|
mColumnWidths[col] = size;
|
||||||
|
mRowHeightForced[col] = true;
|
||||||
updateSize();
|
updateSize();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -162,6 +174,63 @@ void ComponentListComponent::updateComponentOffsets()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ComponentListComponent::updateCellSize(ComponentEntry* e, bool updWidth, bool updHeight)
|
||||||
|
{
|
||||||
|
if(!e)
|
||||||
|
{
|
||||||
|
LOG(LogError) << "Tried to updateCellSize NULL ComponentEntry!";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int x = e->pos.x();
|
||||||
|
unsigned int y = e->pos.y();
|
||||||
|
|
||||||
|
if(!mColumnWidthForced[x] && updWidth)
|
||||||
|
{
|
||||||
|
//recalc width to widest in column
|
||||||
|
float widest = 0;
|
||||||
|
for(int row = 0; row < mGridSize.y(); row++)
|
||||||
|
{
|
||||||
|
ComponentEntry* check = getCell(x, row);
|
||||||
|
if(check)
|
||||||
|
{
|
||||||
|
if(check->component->getSize().x() > widest)
|
||||||
|
widest = check->component->getSize().x();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mColumnWidths[x] = (unsigned int)widest;
|
||||||
|
}
|
||||||
|
if(!mRowHeightForced[y] && updHeight)
|
||||||
|
{
|
||||||
|
float tallest = 0;
|
||||||
|
for(int col = 0; col < mGridSize.x(); col++)
|
||||||
|
{
|
||||||
|
ComponentEntry* check = getCell(col, y);
|
||||||
|
if(check)
|
||||||
|
{
|
||||||
|
if(check->component->getSize().y() > tallest)
|
||||||
|
tallest = check->component->getSize().y();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mRowHeights[y] = (unsigned int)tallest;
|
||||||
|
}
|
||||||
|
|
||||||
|
updateComponentOffsets();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ComponentListComponent::updateComponent(GuiComponent* cmp)
|
||||||
|
{
|
||||||
|
for(auto iter = mEntries.begin(); iter != mEntries.end(); iter++)
|
||||||
|
{
|
||||||
|
if(iter->component == cmp)
|
||||||
|
{
|
||||||
|
updateCellSize(&(*iter));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool ComponentListComponent::input(InputConfig* config, Input input)
|
bool ComponentListComponent::input(InputConfig* config, Input input)
|
||||||
{
|
{
|
||||||
if(cursorValid() && getCell(mCursor.x(), mCursor.y())->component->input(config, input))
|
if(cursorValid() && getCell(mCursor.x(), mCursor.y())->component->input(config, input))
|
||||||
|
|
|
@ -28,8 +28,10 @@ public:
|
||||||
void update(int deltaTime) override;
|
void update(int deltaTime) override;
|
||||||
void render(const Eigen::Affine3f& parentTrans) override;
|
void render(const Eigen::Affine3f& parentTrans) override;
|
||||||
|
|
||||||
void setColumnWidth(int col, unsigned int size);
|
void forceColumnWidth(int col, unsigned int size);
|
||||||
void setRowHeight(int row, unsigned int size);
|
void forceRowHeight(int row, unsigned int size);
|
||||||
|
|
||||||
|
void updateComponent(GuiComponent* cmp);
|
||||||
|
|
||||||
void resetCursor();
|
void resetCursor();
|
||||||
bool cursorValid();
|
bool cursorValid();
|
||||||
|
@ -73,6 +75,8 @@ private:
|
||||||
|
|
||||||
unsigned int* mColumnWidths;
|
unsigned int* mColumnWidths;
|
||||||
unsigned int* mRowHeights;
|
unsigned int* mRowHeights;
|
||||||
|
bool* mColumnWidthForced;
|
||||||
|
bool* mRowHeightForced;
|
||||||
|
|
||||||
Eigen::Vector3f getCellOffset(Eigen::Vector2i gridPos);
|
Eigen::Vector3f getCellOffset(Eigen::Vector2i gridPos);
|
||||||
void updateSize();
|
void updateSize();
|
||||||
|
@ -82,6 +86,7 @@ private:
|
||||||
Eigen::Vector2i mCursor;
|
Eigen::Vector2i mCursor;
|
||||||
|
|
||||||
void updateComponentOffsets();
|
void updateComponentOffsets();
|
||||||
|
void updateCellSize(ComponentEntry* e, bool updWidth = true, bool updHeight = true);
|
||||||
};
|
};
|
||||||
|
|
||||||
//ability to define a list of components in terms of a grid
|
//ability to define a list of components in terms of a grid
|
||||||
|
|
|
@ -2,11 +2,11 @@
|
||||||
#include "../Renderer.h"
|
#include "../Renderer.h"
|
||||||
#include "../Log.h"
|
#include "../Log.h"
|
||||||
|
|
||||||
#define MDED_RESERVED_ROWS 2
|
#define MDED_RESERVED_ROWS 3
|
||||||
|
|
||||||
GuiGameEd::GuiGameEd(Window* window, GameData* game, const std::vector<MetaDataDecl>& mdd) : GuiComponent(window),
|
GuiGameEd::GuiGameEd(Window* window, GameData* game, const std::vector<MetaDataDecl>& mdd) : GuiComponent(window),
|
||||||
mBox(mWindow, 0, 0, 0, 0),
|
mBox(mWindow, 0, 0, 0, 0),
|
||||||
mList(window, Eigen::Vector2i(2, mdd.size() + MDED_RESERVED_ROWS)),
|
mList(window, Eigen::Vector2i(3, mdd.size() + MDED_RESERVED_ROWS)),
|
||||||
mPathDisp(window),
|
mPathDisp(window),
|
||||||
mGame(game)
|
mGame(game)
|
||||||
{
|
{
|
||||||
|
@ -83,15 +83,17 @@ void GuiGameEd::populateList(const std::vector<MetaDataDecl>& mdd)
|
||||||
mGeneratedComponents.push_back(label);
|
mGeneratedComponents.push_back(label);
|
||||||
|
|
||||||
GuiComponent* ed = MetaDataList::makeEditor(mWindow, iter->type);
|
GuiComponent* ed = MetaDataList::makeEditor(mWindow, iter->type);
|
||||||
|
ed->setSize(ed->getSize().x(), 256);
|
||||||
ed->setValue(mGame->metadata()->get(iter->key));
|
ed->setValue(mGame->metadata()->get(iter->key));
|
||||||
mList.setEntry(Vector2i(1, y), Vector2i(1, 1), ed, true, ComponentListComponent::AlignRight);
|
mList.setEntry(Vector2i(1, y), Vector2i(1, 1), ed, true, ComponentListComponent::AlignRight);
|
||||||
mGeneratedComponents.push_back(ed);
|
mGeneratedComponents.push_back(ed);
|
||||||
|
|
||||||
y++;
|
y++;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TextComponent* save = new TextComponent(mWindow);
|
||||||
//force list reflow
|
save->setText("SAVE");
|
||||||
mList.onPositionChanged();
|
save->setColor(0x0000FFFF);
|
||||||
|
mList.setEntry(Vector2i(0, y), Vector2i(2, 1), save, true, ComponentListComponent::AlignCenter);
|
||||||
|
mGeneratedComponents.push_back(save);
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,9 +3,10 @@
|
||||||
#include "../Font.h"
|
#include "../Font.h"
|
||||||
#include "../Window.h"
|
#include "../Window.h"
|
||||||
#include "../Renderer.h"
|
#include "../Renderer.h"
|
||||||
|
#include "ComponentListComponent.h"
|
||||||
|
|
||||||
TextEditComponent::TextEditComponent(Window* window) : GuiComponent(window),
|
TextEditComponent::TextEditComponent(Window* window) : GuiComponent(window),
|
||||||
mBox(window, 0, 0, 0, 0), mFocused(false)
|
mBox(window, 0, 0, 0, 0), mFocused(false), mAllowResize(true)
|
||||||
{
|
{
|
||||||
addChild(&mBox);
|
addChild(&mBox);
|
||||||
|
|
||||||
|
@ -42,6 +43,7 @@ void TextEditComponent::onSizeChanged()
|
||||||
void TextEditComponent::setValue(const std::string& val)
|
void TextEditComponent::setValue(const std::string& val)
|
||||||
{
|
{
|
||||||
mText = val;
|
mText = val;
|
||||||
|
onTextChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string TextEditComponent::getValue() const
|
std::string TextEditComponent::getValue() const
|
||||||
|
@ -61,6 +63,30 @@ void TextEditComponent::textInput(const char* text)
|
||||||
mText += text;
|
mText += text;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
onTextChanged();
|
||||||
|
}
|
||||||
|
|
||||||
|
void TextEditComponent::onTextChanged()
|
||||||
|
{
|
||||||
|
if(mAllowResize)
|
||||||
|
{
|
||||||
|
float y = getFont()->sizeWrappedText(mText, mSize.x()).y();
|
||||||
|
if(y == 0)
|
||||||
|
y = getFont()->getHeight();
|
||||||
|
|
||||||
|
setSize(mSize.x(), y);
|
||||||
|
}
|
||||||
|
|
||||||
|
ComponentListComponent* cmp = dynamic_cast<ComponentListComponent*>(getParent());
|
||||||
|
if(cmp)
|
||||||
|
cmp->updateComponent(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
void TextEditComponent::setAllowResize(bool allow)
|
||||||
|
{
|
||||||
|
mAllowResize = allow;
|
||||||
|
onTextChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
void TextEditComponent::render(const Eigen::Affine3f& parentTrans)
|
void TextEditComponent::render(const Eigen::Affine3f& parentTrans)
|
||||||
|
@ -71,7 +97,7 @@ void TextEditComponent::render(const Eigen::Affine3f& parentTrans)
|
||||||
Renderer::setMatrix(trans);
|
Renderer::setMatrix(trans);
|
||||||
|
|
||||||
std::shared_ptr<Font> f = getFont();
|
std::shared_ptr<Font> f = getFont();
|
||||||
//f->drawText(mText, Eigen::Vector2f::Zero(), 0x00000000 | getOpacity());
|
f->drawWrappedText(mText, Eigen::Vector2f(0, 0), mSize.x(), 0x000000 | getOpacity());
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<Font> TextEditComponent::getFont()
|
std::shared_ptr<Font> TextEditComponent::getFont()
|
||||||
|
|
|
@ -21,9 +21,14 @@ public:
|
||||||
void setValue(const std::string& val) override;
|
void setValue(const std::string& val) override;
|
||||||
std::string getValue() const override;
|
std::string getValue() const override;
|
||||||
|
|
||||||
|
void setAllowResize(bool allow); //Allow automatic resizing of height to accomodate more text.
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void onTextChanged();
|
||||||
|
|
||||||
std::string mText;
|
std::string mText;
|
||||||
bool mFocused;
|
bool mFocused;
|
||||||
|
bool mAllowResize;
|
||||||
|
|
||||||
std::shared_ptr<Font> getFont();
|
std::shared_ptr<Font> getFont();
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue