Add Keyboard input and enter support for text editor.
This commit is contained in:
parent
cf9bace272
commit
9301769d58
23 changed files with 315 additions and 121 deletions
|
@ -10,3 +10,13 @@ std::unique_ptr<Keyboard> Keyboard::Create()
|
|||
return std::make_unique<Keyboard>();
|
||||
}
|
||||
|
||||
std::string Keyboard::getKeyString(KeyCode code)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
Keyboard::Function Keyboard::getFunction(KeyCode code)
|
||||
{
|
||||
return Function::UNSET;
|
||||
}
|
||||
|
||||
|
|
|
@ -6,6 +6,15 @@
|
|||
class Keyboard
|
||||
{
|
||||
public:
|
||||
|
||||
enum class Function
|
||||
{
|
||||
UNSET,
|
||||
ENTER,
|
||||
BACKSPACE,
|
||||
TAB
|
||||
};
|
||||
|
||||
using KeyCode = unsigned;
|
||||
using KeyState = unsigned;
|
||||
|
||||
|
@ -16,10 +25,10 @@ public:
|
|||
|
||||
static std::unique_ptr<Keyboard> Create();
|
||||
|
||||
virtual std::string getKeyString(KeyCode code)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
virtual std::string getKeyString(KeyCode code);
|
||||
|
||||
virtual Function getFunction(KeyCode code);
|
||||
|
||||
};
|
||||
|
||||
using KeyboardUPtr = std::unique_ptr<Keyboard>;
|
||||
|
|
|
@ -18,22 +18,37 @@ std::unique_ptr<KeyboardEvent> KeyboardEvent::Create()
|
|||
return std::make_unique<KeyboardEvent>();
|
||||
}
|
||||
|
||||
void KeyboardEvent::SetKeyString(const std::string& key)
|
||||
void KeyboardEvent::setKeyString(const std::string& key)
|
||||
{
|
||||
mKeyString = key;
|
||||
}
|
||||
|
||||
std::string KeyboardEvent::GetKeyString() const
|
||||
std::string KeyboardEvent::getKeyString() const
|
||||
{
|
||||
return mKeyString;
|
||||
}
|
||||
|
||||
void KeyboardEvent::SetAction(Action action)
|
||||
void KeyboardEvent::setAction(Action action)
|
||||
{
|
||||
mAction = action;
|
||||
}
|
||||
|
||||
KeyboardEvent::Action KeyboardEvent::GetAction() const
|
||||
KeyboardEvent::Action KeyboardEvent::getAction() const
|
||||
{
|
||||
return mAction;
|
||||
}
|
||||
|
||||
bool KeyboardEvent::isFunctionKey() const
|
||||
{
|
||||
return mFunction != Keyboard::Function::UNSET;
|
||||
}
|
||||
|
||||
Keyboard::Function KeyboardEvent::getFunction() const
|
||||
{
|
||||
return mFunction;
|
||||
}
|
||||
|
||||
void KeyboardEvent::setFunction(Keyboard::Function function)
|
||||
{
|
||||
mFunction = function;
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
#include <string>
|
||||
|
||||
#include "UiEvent.h"
|
||||
#include "Keyboard.h"
|
||||
|
||||
class KeyboardEvent : public UiEvent
|
||||
{
|
||||
|
@ -14,11 +15,6 @@ public:
|
|||
Released
|
||||
};
|
||||
|
||||
private:
|
||||
|
||||
Action mAction;
|
||||
std::string mKeyString;
|
||||
|
||||
public:
|
||||
|
||||
KeyboardEvent();
|
||||
|
@ -27,13 +23,25 @@ public:
|
|||
|
||||
static std::unique_ptr<KeyboardEvent> Create();
|
||||
|
||||
void SetKeyString(const std::string& key);
|
||||
void setKeyString(const std::string& key);
|
||||
|
||||
std::string GetKeyString() const;
|
||||
std::string getKeyString() const;
|
||||
|
||||
void SetAction(Action action);
|
||||
void setAction(Action action);
|
||||
|
||||
Action GetAction() const;
|
||||
Action getAction() const;
|
||||
|
||||
bool isFunctionKey() const;
|
||||
|
||||
Keyboard::Function getFunction() const;
|
||||
|
||||
void setFunction(Keyboard::Function function);
|
||||
|
||||
private:
|
||||
|
||||
Keyboard::Function mFunction{Keyboard::Function::UNSET};
|
||||
Action mAction;
|
||||
std::string mKeyString;
|
||||
|
||||
};
|
||||
using KeyboardEventUPtr = std::unique_ptr<KeyboardEvent>;
|
||||
|
|
|
@ -75,12 +75,16 @@ void Button::updateLabel(const PaintEvent* event)
|
|||
mTextNode = TextNode::Create(mLabel, middle);
|
||||
mTextNode->setName(mName + "_TextNode");
|
||||
mTextNode->setContent(mLabel);
|
||||
mTextNode->setWidth(mSize.mWidth);
|
||||
mTextNode->setHeight(mSize.mHeight);
|
||||
mRootNode->addChild(mTextNode.get());
|
||||
}
|
||||
|
||||
if (mTransformDirty)
|
||||
{
|
||||
mTextNode->setLocation(middle);
|
||||
mTextNode->setWidth(mSize.mWidth);
|
||||
mTextNode->setHeight(mSize.mHeight);
|
||||
}
|
||||
|
||||
if (mMaterialDirty)
|
||||
|
|
|
@ -44,6 +44,8 @@ void Label::updateLabel(const PaintEvent* event)
|
|||
if (!mTextNode)
|
||||
{
|
||||
mTextNode = TextNode::Create(mLabel, middle);
|
||||
mTextNode->setWidth(mSize.mWidth);
|
||||
mTextNode->setHeight(mSize.mHeight);
|
||||
mRootNode->addChild(mTextNode.get());
|
||||
}
|
||||
|
||||
|
@ -55,6 +57,8 @@ void Label::updateLabel(const PaintEvent* event)
|
|||
if (mTransformDirty)
|
||||
{
|
||||
mTextNode->setLocation(middle);
|
||||
mTextNode->setWidth(mSize.mWidth);
|
||||
mTextNode->setHeight(mSize.mHeight);
|
||||
}
|
||||
|
||||
if (mContentDirty)
|
||||
|
|
|
@ -13,7 +13,7 @@ TextBox::TextBox()
|
|||
mCaps(false)
|
||||
{
|
||||
mBackgroundColor = Color(250, 250, 250);
|
||||
mPadding = {10, 0, 10, 0};
|
||||
mPadding = {20, 0, 20, 0};
|
||||
}
|
||||
|
||||
std::unique_ptr<TextBox> TextBox::Create()
|
||||
|
@ -40,72 +40,28 @@ void TextBox::appendContent(const std::string& text)
|
|||
|
||||
bool TextBox::onMyKeyboardEvent(const KeyboardEvent* event)
|
||||
{
|
||||
if(!event) return false;
|
||||
if(!event or !mVisible) return false;
|
||||
|
||||
const auto keyString = event->GetKeyString();
|
||||
if (keyString == "KEY_RETURN")
|
||||
if(event->isFunctionKey())
|
||||
{
|
||||
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;
|
||||
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
|
||||
{
|
||||
if (mCaps && !keyString.empty())
|
||||
{
|
||||
const char c = std::toupper(keyString[0]);
|
||||
appendContent(std::string(&c));
|
||||
}
|
||||
else
|
||||
{
|
||||
appendContent(keyString);
|
||||
}
|
||||
const auto keyString = event->getKeyString();
|
||||
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();
|
||||
}
|
||||
*/
|
||||
|
||||
bool TextBox::isDirty() const
|
||||
{
|
||||
return Widget::isDirty() || mContentDirty;
|
||||
|
@ -119,12 +75,13 @@ void TextBox::doPaint(const PaintEvent* event)
|
|||
|
||||
void TextBox::updateLabel(const PaintEvent* event)
|
||||
{
|
||||
unsigned fontOffset = unsigned(mContent.size()) * 4;
|
||||
auto middle = DiscretePoint(mLocation.GetX() + mSize.mWidth/2 - fontOffset, mLocation.GetY() + mSize.mHeight/2 + 4);
|
||||
auto loc = DiscretePoint(mLocation.GetX() + mPadding.mLeft, mLocation.GetY() + mPadding.mTop + unsigned(0));
|
||||
|
||||
if (!mTextNode)
|
||||
{
|
||||
mTextNode = TextNode::Create(mContent, middle);
|
||||
mTextNode = TextNode::Create(mContent, loc);
|
||||
mTextNode->setWidth(mSize.mWidth);
|
||||
mTextNode->setHeight(mSize.mHeight);
|
||||
mRootNode->addChild(mTextNode.get());
|
||||
}
|
||||
|
||||
|
@ -133,6 +90,13 @@ void TextBox::updateLabel(const PaintEvent* event)
|
|||
mTextNode->setFillColor(mBackgroundColor);
|
||||
}
|
||||
|
||||
if (mTransformDirty)
|
||||
{
|
||||
mTextNode->setLocation(loc);
|
||||
mTextNode->setWidth(mSize.mWidth);
|
||||
mTextNode->setHeight(mSize.mHeight);
|
||||
}
|
||||
|
||||
if (mContentDirty)
|
||||
{
|
||||
mTextNode->setContent(mContent);
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include "DiscretePoint.h"
|
||||
#include "FontItem.h"
|
||||
#include "Color.h"
|
||||
|
||||
#include <memory>
|
||||
|
@ -149,6 +150,8 @@ protected:
|
|||
|
||||
std::string mName;
|
||||
std::vector<TransformNode*> mPendingChildNodes;
|
||||
|
||||
FontItem mDefaultFont;
|
||||
};
|
||||
|
||||
using WidgetUPtr = std::unique_ptr<Widget>;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue