2014-03-21 16:10:19 +00:00
|
|
|
#include "GuiTextEditPopup.h"
|
|
|
|
#include "../components/MenuComponent.h"
|
|
|
|
|
|
|
|
using namespace Eigen;
|
|
|
|
|
|
|
|
GuiTextEditPopup::GuiTextEditPopup(Window* window, const std::string& title, const std::string& initValue,
|
|
|
|
const std::function<void(const std::string&)>& okCallback, bool multiLine, const char* acceptBtnText)
|
|
|
|
: GuiComponent(window), mBackground(window, ":/frame.png"), mGrid(window, Vector2i(1, 3)), mMultiLine(multiLine)
|
|
|
|
{
|
|
|
|
addChild(&mBackground);
|
|
|
|
addChild(&mGrid);
|
|
|
|
|
|
|
|
mTitle = std::make_shared<TextComponent>(mWindow, strToUpper(title), Font::get(FONT_SIZE_MEDIUM), 0x777777FF, true);
|
|
|
|
|
|
|
|
mText = std::make_shared<TextEditComponent>(mWindow);
|
|
|
|
mText->setValue(initValue);
|
|
|
|
|
|
|
|
std::vector< std::shared_ptr<ButtonComponent> > buttons;
|
|
|
|
buttons.push_back(std::make_shared<ButtonComponent>(mWindow, acceptBtnText, acceptBtnText, [this, okCallback] { okCallback(mText->getValue()); delete this; }));
|
|
|
|
buttons.push_back(std::make_shared<ButtonComponent>(mWindow, "CANCEL", "discard changes", [this] { delete this; }));
|
|
|
|
|
|
|
|
mButtonGrid = makeButtonGrid(mWindow, buttons);
|
|
|
|
|
|
|
|
mGrid.setEntry(mTitle, Vector2i(0, 0), false, true);
|
|
|
|
mGrid.setEntry(mText, Vector2i(0, 1), true, true);
|
|
|
|
mGrid.setEntry(mButtonGrid, Vector2i(0, 2), true, false);
|
|
|
|
|
|
|
|
float textHeight = mText->getFont()->getHeight();
|
|
|
|
if(multiLine)
|
2014-03-21 16:54:48 +00:00
|
|
|
textHeight *= 6;
|
2014-03-21 16:10:19 +00:00
|
|
|
|
|
|
|
setSize(Renderer::getScreenWidth() * 0.5f, mTitle->getFont()->getHeight() + textHeight + mButtonGrid->getSize().y());
|
|
|
|
setPosition((Renderer::getScreenWidth() - mSize.x()) / 2, (Renderer::getScreenHeight() - mSize.y()) / 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GuiTextEditPopup::onSizeChanged()
|
|
|
|
{
|
|
|
|
mBackground.fitTo(mSize, Eigen::Vector3f::Zero(), Eigen::Vector2f(-32, -32));
|
|
|
|
|
|
|
|
// update grid
|
|
|
|
mGrid.setRowHeightPerc(0, mTitle->getFont()->getHeight() / mSize.y());
|
|
|
|
mGrid.setRowHeightPerc(2, mButtonGrid->getSize().y() / mSize.y());
|
|
|
|
mGrid.setSize(mSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GuiTextEditPopup::input(InputConfig* config, Input input)
|
|
|
|
{
|
|
|
|
if(GuiComponent::input(config, input))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// pressing back when not text editing closes us
|
|
|
|
if(config->isMappedTo("b", input) && input.value)
|
|
|
|
{
|
|
|
|
delete this;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|