stuff-from-scratch/src/ui_elements/widgets/TextBox.cpp

105 lines
2.1 KiB
C++

#include "TextBox.h"
#include "TextNode.h"
#include "GeometryNode.h"
#include "KeyboardEvent.h"
#include "TransformNode.h"
#include <sstream>
TextBox::TextBox()
: Widget(),
mContent(),
mCaps(false)
{
mBackgroundColor = Color(250, 250, 250);
mPadding = {20, 0, 20, 0};
}
std::unique_ptr<TextBox> TextBox::Create()
{
return std::make_unique<TextBox>();
}
void TextBox::setContent(const std::string& text)
{
mContent = text;
mContentDirty = true;
}
std::string TextBox::getContent() const
{
return mContent;
}
void TextBox::appendContent(const std::string& text)
{
mContent += text;
mContentDirty = true;
}
bool TextBox::onMyKeyboardEvent(const KeyboardEvent* event)
{
if(!event or !mVisible) return false;
if(event->isFunctionKey())
{
if (event->getFunction() == Keyboard::Function::ENTER)
{
appendContent("\n");
}
else if(event->getFunction() == Keyboard::Function::BACKSPACE)
{
mContent = mContent.substr(0, mContent.size()-1);
mContentDirty = true;
}
}
else
{
const auto keyString = event->getKeyString();
appendContent(keyString);
}
return true;
}
bool TextBox::isDirty() const
{
return Widget::isDirty() || mContentDirty;
}
void TextBox::doPaint(const PaintEvent* event)
{
updateBackground(event);
updateLabel(event);
}
void TextBox::updateLabel(const PaintEvent* event)
{
auto loc = DiscretePoint(mLocation.GetX() + mPadding.mLeft, mLocation.GetY() + mPadding.mTop + unsigned(0));
if (!mTextNode)
{
mTextNode = TextNode::Create(mContent, loc);
mTextNode->setWidth(mSize.mWidth);
mTextNode->setHeight(mSize.mHeight);
mRootNode->addChild(mTextNode.get());
}
if (mMaterialDirty)
{
mTextNode->setFillColor(mBackgroundColor);
}
if (mTransformDirty)
{
mTextNode->setLocation(loc);
mTextNode->setWidth(mSize.mWidth);
mTextNode->setHeight(mSize.mHeight);
}
if (mContentDirty)
{
mTextNode->setContent(mContent);
mContentDirty = false;
}
}