103 lines
2.2 KiB
C++
103 lines
2.2 KiB
C++
#include "TextBox.h"
|
|
|
|
#include "TextNode.h"
|
|
#include "VisualLayer.h"
|
|
#include "GeometryNode.h"
|
|
#include "KeyboardEvent.h"
|
|
|
|
#include <sstream>
|
|
|
|
TextBox::TextBox()
|
|
: Widget(),
|
|
mContent(),
|
|
mCaps(false)
|
|
{
|
|
mBackgroundColor = Color(250, 250, 250);
|
|
mPadding = {10, 0, 10, 0};
|
|
}
|
|
|
|
std::unique_ptr<TextBox> TextBox::Create()
|
|
{
|
|
return std::make_unique<TextBox>();
|
|
}
|
|
|
|
void TextBox::setContent(const std::string& text)
|
|
{
|
|
mContent = text;
|
|
}
|
|
|
|
std::string TextBox::getContent() const
|
|
{
|
|
return mContent;
|
|
}
|
|
|
|
void TextBox::appendContent(const std::string& text)
|
|
{
|
|
mContent += text;
|
|
}
|
|
|
|
bool TextBox::onMyKeyboardEvent(const KeyboardEvent* event)
|
|
{
|
|
if(!event) return false;
|
|
|
|
const auto keyString = event->GetKeyString();
|
|
if (keyString == "KEY_RETURN")
|
|
{
|
|
appendContent("\n");
|
|
}
|
|
else if (keyString == "KEY_BACK")
|
|
{
|
|
mContent = mContent.substr(0, mContent.size()-1);
|
|
}
|
|
else if (keyString == "KEY_SPACE")
|
|
{
|
|
appendContent(" ");
|
|
}
|
|
else if (keyString == "KEY_CAPS")
|
|
{
|
|
mCaps = !mCaps;
|
|
}
|
|
else
|
|
{
|
|
if (mCaps && !keyString.empty())
|
|
{
|
|
const char c = std::toupper(keyString[0]);
|
|
appendContent(std::string(&c));
|
|
}
|
|
else
|
|
{
|
|
appendContent(keyString);
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void TextBox::onPaintEvent(const PaintEvent* event)
|
|
{
|
|
mMyLayers.clear();
|
|
addBackground(event);
|
|
|
|
double offset = 0;
|
|
if(!mContent.empty())
|
|
{
|
|
std::stringstream stream(mContent);
|
|
std::string segment;
|
|
std::vector<std::string> seglist;
|
|
while(std::getline(stream, segment, '\n'))
|
|
{
|
|
seglist.push_back(segment);
|
|
}
|
|
for(const auto& line : seglist)
|
|
{
|
|
auto loc = DiscretePoint(mLocation.GetX() + mPadding.mLeft,
|
|
mLocation.GetY() + mPadding.mTop + unsigned(offset));
|
|
auto textLayer = VisualLayer::Create();
|
|
auto textElement = TextNode::Create(line, loc);
|
|
textElement->setFillColor(mBackgroundColor);
|
|
textLayer->setTextNode(std::move(textElement));
|
|
mMyLayers.push_back(std::move(textLayer));
|
|
offset += 20;
|
|
}
|
|
}
|
|
addMyLayers();
|
|
}
|