TextEditComponents now resize vertically as needed.

This commit is contained in:
Aloshi 2013-08-21 14:49:33 -05:00
parent 7cb3cc09ee
commit bed9c1fbb5
5 changed files with 124 additions and 17 deletions

View file

@ -4,7 +4,10 @@
#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());
makeCells(gridDimensions);
@ -29,6 +32,12 @@ void ComponentListComponent::makeCells(Eigen::Vector2i size)
mRowHeights = new unsigned int[size.y()];
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();
resetCursor();
}
@ -68,23 +77,26 @@ void ComponentListComponent::setEntry(Eigen::Vector2i pos, Eigen::Vector2i size,
mCursor = pos;
//update the column width and row height
if(autoFit.x() && (int)getColumnWidth(pos.x()) < component->getSize().x())
setColumnWidth(pos.x(), (unsigned int)component->getSize().x());
if(autoFit.y() && (int)getRowHeight(pos.y()) < component->getSize().y())
setRowHeight(pos.y(), (unsigned int)component->getSize().y());
//if(autoFit.x() && (int)getColumnWidth(pos.x()) < component->getSize().x())
// setColumnWidth(pos.x(), (unsigned int)component->getSize().x());
//if(autoFit.y() && (int)getRowHeight(pos.y()) < component->getSize().y())
// setRowHeight(pos.y(), (unsigned int)component->getSize().y());
updateCellSize(&mEntries.back(), autoFit.x(), autoFit.y());
component->setPosition(getCellOffset(pos));
}
void ComponentListComponent::setRowHeight(int row, unsigned int size)
void ComponentListComponent::forceRowHeight(int row, unsigned int size)
{
mRowHeights[row] = size;
mRowHeightForced[row] = true;
updateSize();
}
void ComponentListComponent::setColumnWidth(int col, unsigned int size)
void ComponentListComponent::forceColumnWidth(int col, unsigned int size)
{
mColumnWidths[col] = size;
mRowHeightForced[col] = true;
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)
{
if(cursorValid() && getCell(mCursor.x(), mCursor.y())->component->input(config, input))

View file

@ -28,8 +28,10 @@ public:
void update(int deltaTime) override;
void render(const Eigen::Affine3f& parentTrans) override;
void setColumnWidth(int col, unsigned int size);
void setRowHeight(int row, unsigned int size);
void forceColumnWidth(int col, unsigned int size);
void forceRowHeight(int row, unsigned int size);
void updateComponent(GuiComponent* cmp);
void resetCursor();
bool cursorValid();
@ -73,6 +75,8 @@ private:
unsigned int* mColumnWidths;
unsigned int* mRowHeights;
bool* mColumnWidthForced;
bool* mRowHeightForced;
Eigen::Vector3f getCellOffset(Eigen::Vector2i gridPos);
void updateSize();
@ -82,6 +86,7 @@ private:
Eigen::Vector2i mCursor;
void updateComponentOffsets();
void updateCellSize(ComponentEntry* e, bool updWidth = true, bool updHeight = true);
};
//ability to define a list of components in terms of a grid

View file

@ -2,11 +2,11 @@
#include "../Renderer.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),
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),
mGame(game)
{
@ -83,15 +83,17 @@ void GuiGameEd::populateList(const std::vector<MetaDataDecl>& mdd)
mGeneratedComponents.push_back(label);
GuiComponent* ed = MetaDataList::makeEditor(mWindow, iter->type);
ed->setSize(ed->getSize().x(), 256);
ed->setValue(mGame->metadata()->get(iter->key));
mList.setEntry(Vector2i(1, y), Vector2i(1, 1), ed, true, ComponentListComponent::AlignRight);
mGeneratedComponents.push_back(ed);
y++;
break;
}
//force list reflow
mList.onPositionChanged();
TextComponent* save = new TextComponent(mWindow);
save->setText("SAVE");
save->setColor(0x0000FFFF);
mList.setEntry(Vector2i(0, y), Vector2i(2, 1), save, true, ComponentListComponent::AlignCenter);
mGeneratedComponents.push_back(save);
}

View file

@ -3,9 +3,10 @@
#include "../Font.h"
#include "../Window.h"
#include "../Renderer.h"
#include "ComponentListComponent.h"
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);
@ -42,6 +43,7 @@ void TextEditComponent::onSizeChanged()
void TextEditComponent::setValue(const std::string& val)
{
mText = val;
onTextChanged();
}
std::string TextEditComponent::getValue() const
@ -61,6 +63,30 @@ void TextEditComponent::textInput(const char* 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)
@ -71,7 +97,7 @@ void TextEditComponent::render(const Eigen::Affine3f& parentTrans)
Renderer::setMatrix(trans);
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()

View file

@ -21,9 +21,14 @@ public:
void setValue(const std::string& val) override;
std::string getValue() const override;
void setAllowResize(bool allow); //Allow automatic resizing of height to accomodate more text.
private:
void onTextChanged();
std::string mText;
bool mFocused;
bool mAllowResize;
std::shared_ptr<Font> getFont();