Add Keyboard input and enter support for text editor.

This commit is contained in:
James Grogan 2022-11-17 13:13:01 +00:00
parent cf9bace272
commit 9301769d58
23 changed files with 315 additions and 121 deletions

View file

@ -25,6 +25,7 @@ void TextEditorView::Initialize()
label->setMargin(1); label->setMargin(1);
auto textBox = TextBox::Create(); auto textBox = TextBox::Create();
textBox->setName("Text Box");
mTextBox = textBox.get(); mTextBox = textBox.get();
auto saveButton = Button::Create(); auto saveButton = Button::Create();

View file

@ -49,6 +49,8 @@ void TabbedPanelWidget::addPanel(WidgetUPtr panel, const std::string& label)
button->setOnClickFunction(onClick); button->setOnClickFunction(onClick);
mStack->addWidget(std::move(panel)); mStack->addWidget(std::move(panel));
mStack->showChild(rawPanel);
mNavPanel->addWidget(std::move(button)); mNavPanel->addWidget(std::move(button));
} }

View file

@ -20,6 +20,17 @@ bool StringUtils::IsSpace(char c)
return std::isspace(c, loc); return std::isspace(c, loc);
} }
std::vector<std::string> StringUtils::toLines(const std::string& input)
{
auto result = std::vector<std::string>{};
auto ss = std::stringstream{input};
for (std::string line; std::getline(ss, line, '\n');)
{
result.push_back(line);
}
return result;
}
std::string StringUtils::strip(const std::string& input) std::string StringUtils::strip(const std::string& input)
{ {
if (input.empty()) if (input.empty())

View file

@ -23,5 +23,8 @@ public:
static std::string ToPaddedString(unsigned numBytes, unsigned entry); static std::string ToPaddedString(unsigned numBytes, unsigned entry);
static std::vector<std::string> split(const std::string& input); static std::vector<std::string> split(const std::string& input);
static std::string strip(const std::string& input); static std::string strip(const std::string& input);
static std::vector<std::string> toLines(const std::string& input);
static std::string stripQuotes(const std::string& input); static std::string stripQuotes(const std::string& input);
}; };

View file

@ -38,6 +38,17 @@ public:
return mSize; return mSize;
} }
bool operator==(const FontItem& rhs) const
{
return (mSize == rhs.mSize)
&& (mFaceName == rhs.mFaceName);
}
bool operator!=(const FontItem& rhs) const
{
return !operator==(rhs);
}
private: private:
unsigned mSize{16}; unsigned mSize{16};
std::string mFaceName; std::string mFaceName;

View file

@ -44,7 +44,9 @@ void OpenGlTextPainter::initializeShader()
void OpenGlTextPainter::initializeTextures(const TextData& textData, DrawingContext* context) void OpenGlTextPainter::initializeTextures(const TextData& textData, DrawingContext* context)
{ {
for (auto c : textData.mContent) for (auto line : textData.mLines)
{
for (auto c : line)
{ {
if (auto iter = mFontTextures.find(c); iter == mFontTextures.end()) if (auto iter = mFontTextures.find(c); iter == mFontTextures.end())
{ {
@ -54,6 +56,7 @@ void OpenGlTextPainter::initializeTextures(const TextData& textData, DrawingCont
} }
} }
} }
}
void OpenGlTextPainter::initializeBuffers() void OpenGlTextPainter::initializeBuffers()
{ {
@ -104,9 +107,13 @@ void OpenGlTextPainter::paint(SceneText* text, DrawingContext* context)
auto transform = text->getTransform(); auto transform = text->getTransform();
float line_delta = 20;
float line_offset = 0;
for (auto line : text_data.mLines)
{
float x = transform.getLocation().getX(); float x = transform.getLocation().getX();
const float y = height - transform.getLocation().getY(); const float y = height - line_offset - transform.getLocation().getY();
for (auto c : text_data.mContent) for (auto c : line)
{ {
auto texture = mFontTextures[c].get(); auto texture = mFontTextures[c].get();
@ -136,6 +143,9 @@ void OpenGlTextPainter::paint(SceneText* text, DrawingContext* context)
x += (texture->getGlyph()->getAdvanceX() >> 6); // bitshift by 6 to get value in pixels (2^6 = 64) x += (texture->getGlyph()->getAdvanceX() >> 6); // bitshift by 6 to get value in pixels (2^6 = 64)
} }
line_offset += line_delta;
}
glBindVertexArray(0); glBindVertexArray(0);
glBindTexture(GL_TEXTURE_2D, 0); glBindTexture(GL_TEXTURE_2D, 0);
} }

View file

@ -10,3 +10,13 @@ std::unique_ptr<Keyboard> Keyboard::Create()
return std::make_unique<Keyboard>(); return std::make_unique<Keyboard>();
} }
std::string Keyboard::getKeyString(KeyCode code)
{
return "";
}
Keyboard::Function Keyboard::getFunction(KeyCode code)
{
return Function::UNSET;
}

View file

@ -6,6 +6,15 @@
class Keyboard class Keyboard
{ {
public: public:
enum class Function
{
UNSET,
ENTER,
BACKSPACE,
TAB
};
using KeyCode = unsigned; using KeyCode = unsigned;
using KeyState = unsigned; using KeyState = unsigned;
@ -16,10 +25,10 @@ public:
static std::unique_ptr<Keyboard> Create(); static std::unique_ptr<Keyboard> Create();
virtual std::string getKeyString(KeyCode code) virtual std::string getKeyString(KeyCode code);
{
return ""; virtual Function getFunction(KeyCode code);
}
}; };
using KeyboardUPtr = std::unique_ptr<Keyboard>; using KeyboardUPtr = std::unique_ptr<Keyboard>;

View file

@ -18,22 +18,37 @@ std::unique_ptr<KeyboardEvent> KeyboardEvent::Create()
return std::make_unique<KeyboardEvent>(); return std::make_unique<KeyboardEvent>();
} }
void KeyboardEvent::SetKeyString(const std::string& key) void KeyboardEvent::setKeyString(const std::string& key)
{ {
mKeyString = key; mKeyString = key;
} }
std::string KeyboardEvent::GetKeyString() const std::string KeyboardEvent::getKeyString() const
{ {
return mKeyString; return mKeyString;
} }
void KeyboardEvent::SetAction(Action action) void KeyboardEvent::setAction(Action action)
{ {
mAction = action; mAction = action;
} }
KeyboardEvent::Action KeyboardEvent::GetAction() const KeyboardEvent::Action KeyboardEvent::getAction() const
{ {
return mAction; 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;
}

View file

@ -4,6 +4,7 @@
#include <string> #include <string>
#include "UiEvent.h" #include "UiEvent.h"
#include "Keyboard.h"
class KeyboardEvent : public UiEvent class KeyboardEvent : public UiEvent
{ {
@ -14,11 +15,6 @@ public:
Released Released
}; };
private:
Action mAction;
std::string mKeyString;
public: public:
KeyboardEvent(); KeyboardEvent();
@ -27,13 +23,25 @@ public:
static std::unique_ptr<KeyboardEvent> Create(); 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>; using KeyboardEventUPtr = std::unique_ptr<KeyboardEvent>;

View file

@ -75,12 +75,16 @@ void Button::updateLabel(const PaintEvent* event)
mTextNode = TextNode::Create(mLabel, middle); mTextNode = TextNode::Create(mLabel, middle);
mTextNode->setName(mName + "_TextNode"); mTextNode->setName(mName + "_TextNode");
mTextNode->setContent(mLabel); mTextNode->setContent(mLabel);
mTextNode->setWidth(mSize.mWidth);
mTextNode->setHeight(mSize.mHeight);
mRootNode->addChild(mTextNode.get()); mRootNode->addChild(mTextNode.get());
} }
if (mTransformDirty) if (mTransformDirty)
{ {
mTextNode->setLocation(middle); mTextNode->setLocation(middle);
mTextNode->setWidth(mSize.mWidth);
mTextNode->setHeight(mSize.mHeight);
} }
if (mMaterialDirty) if (mMaterialDirty)

View file

@ -44,6 +44,8 @@ void Label::updateLabel(const PaintEvent* event)
if (!mTextNode) if (!mTextNode)
{ {
mTextNode = TextNode::Create(mLabel, middle); mTextNode = TextNode::Create(mLabel, middle);
mTextNode->setWidth(mSize.mWidth);
mTextNode->setHeight(mSize.mHeight);
mRootNode->addChild(mTextNode.get()); mRootNode->addChild(mTextNode.get());
} }
@ -55,6 +57,8 @@ void Label::updateLabel(const PaintEvent* event)
if (mTransformDirty) if (mTransformDirty)
{ {
mTextNode->setLocation(middle); mTextNode->setLocation(middle);
mTextNode->setWidth(mSize.mWidth);
mTextNode->setHeight(mSize.mHeight);
} }
if (mContentDirty) if (mContentDirty)

View file

@ -13,7 +13,7 @@ TextBox::TextBox()
mCaps(false) mCaps(false)
{ {
mBackgroundColor = Color(250, 250, 250); mBackgroundColor = Color(250, 250, 250);
mPadding = {10, 0, 10, 0}; mPadding = {20, 0, 20, 0};
} }
std::unique_ptr<TextBox> TextBox::Create() std::unique_ptr<TextBox> TextBox::Create()
@ -40,72 +40,28 @@ void TextBox::appendContent(const std::string& text)
bool TextBox::onMyKeyboardEvent(const KeyboardEvent* event) bool TextBox::onMyKeyboardEvent(const KeyboardEvent* event)
{ {
if(!event) return false; if(!event or !mVisible) return false;
const auto keyString = event->GetKeyString(); if(event->isFunctionKey())
if (keyString == "KEY_RETURN") {
if (event->getFunction() == Keyboard::Function::ENTER)
{ {
appendContent("\n"); appendContent("\n");
} }
else if (keyString == "KEY_BACK") else if(event->getFunction() == Keyboard::Function::BACKSPACE)
{ {
mContent = mContent.substr(0, mContent.size()-1); mContent = mContent.substr(0, mContent.size()-1);
mContentDirty = true;
} }
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 else
{ {
const auto keyString = event->getKeyString();
appendContent(keyString); appendContent(keyString);
} }
}
return true; 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 bool TextBox::isDirty() const
{ {
return Widget::isDirty() || mContentDirty; return Widget::isDirty() || mContentDirty;
@ -119,12 +75,13 @@ void TextBox::doPaint(const PaintEvent* event)
void TextBox::updateLabel(const PaintEvent* event) void TextBox::updateLabel(const PaintEvent* event)
{ {
unsigned fontOffset = unsigned(mContent.size()) * 4; auto loc = DiscretePoint(mLocation.GetX() + mPadding.mLeft, mLocation.GetY() + mPadding.mTop + unsigned(0));
auto middle = DiscretePoint(mLocation.GetX() + mSize.mWidth/2 - fontOffset, mLocation.GetY() + mSize.mHeight/2 + 4);
if (!mTextNode) if (!mTextNode)
{ {
mTextNode = TextNode::Create(mContent, middle); mTextNode = TextNode::Create(mContent, loc);
mTextNode->setWidth(mSize.mWidth);
mTextNode->setHeight(mSize.mHeight);
mRootNode->addChild(mTextNode.get()); mRootNode->addChild(mTextNode.get());
} }
@ -133,6 +90,13 @@ void TextBox::updateLabel(const PaintEvent* event)
mTextNode->setFillColor(mBackgroundColor); mTextNode->setFillColor(mBackgroundColor);
} }
if (mTransformDirty)
{
mTextNode->setLocation(loc);
mTextNode->setWidth(mSize.mWidth);
mTextNode->setHeight(mSize.mHeight);
}
if (mContentDirty) if (mContentDirty)
{ {
mTextNode->setContent(mContent); mTextNode->setContent(mContent);

View file

@ -1,6 +1,7 @@
#pragma once #pragma once
#include "DiscretePoint.h" #include "DiscretePoint.h"
#include "FontItem.h"
#include "Color.h" #include "Color.h"
#include <memory> #include <memory>
@ -149,6 +150,8 @@ protected:
std::string mName; std::string mName;
std::vector<TransformNode*> mPendingChildNodes; std::vector<TransformNode*> mPendingChildNodes;
FontItem mDefaultFont;
}; };
using WidgetUPtr = std::unique_ptr<Widget>; using WidgetUPtr = std::unique_ptr<Widget>;

View file

@ -15,11 +15,11 @@ const TextData& SceneText::getTextData() const
return mTextData; return mTextData;
} }
void SceneText::setContent(const std::string& content) void SceneText::setTextData(const TextData& data)
{ {
if (mTextData.mContent != content) if (mTextData != data)
{ {
mTextGeometryIsDirty = true; mTextGeometryIsDirty = true;
mTextData.mContent = content; mTextData = data;
} }
} }

View file

@ -12,7 +12,7 @@ public:
const TextData& getTextData() const; const TextData& getTextData() const;
void setContent(const std::string& content); void setTextData(const TextData& content);
private: private:
bool mTextGeometryIsDirty{true}; bool mTextGeometryIsDirty{true};

View file

@ -3,10 +3,27 @@
#include "Color.h" #include "Color.h"
#include "FontItem.h" #include "FontItem.h"
#include <vector>
class TextData class TextData
{ {
public:
bool operator==(const TextData& rhs) const
{
return (mContent == rhs.mContent)
&& (mLines == rhs.mLines)
&& (mFont == rhs.mFont);
}
bool operator!=(const TextData& rhs) const
{
return !operator==(rhs);
}
public: public:
TextData() = default; TextData() = default;
std::string mContent; std::string mContent;
std::vector<std::string> mLines;
FontItem mFont; FontItem mFont;
}; };

View file

@ -5,17 +5,20 @@
#include "IFontEngine.h" #include "IFontEngine.h"
#include "MeshPrimitives.h" #include "MeshPrimitives.h"
#include "FontItem.h" #include "FontItem.h"
#include "FontGlyph.h"
#include "SceneText.h" #include "SceneText.h"
#include "Color.h" #include "Color.h"
#include "StringUtils.h"
#include <iostream> #include <iostream>
TextNode::TextNode(const std::string& content, const DiscretePoint& loc) TextNode::TextNode(const std::string& content, const DiscretePoint& loc)
: MaterialNode(loc) : MaterialNode(loc)
{ {
mContent= content; mTextData.mContent= content;
} }
TextNode::~TextNode() TextNode::~TextNode()
@ -35,18 +38,82 @@ std::string TextNode::getFontLabel() const
std::string TextNode::getContent() const std::string TextNode::getContent() const
{ {
return mContent; return mTextData.mContent;
}
unsigned TextNode::getWidth() const
{
return mWidth;
}
unsigned TextNode::getHeight() const
{
return mHeight;
}
void TextNode::setWidth(unsigned width)
{
if (mWidth != width)
{
mTransformIsDirty = true;
mWidth = width;
}
}
void TextNode::setHeight(unsigned height)
{
if (mHeight != height)
{
mTransformIsDirty = true;
mHeight = height;
}
} }
void TextNode::setContent(const std::string& content) void TextNode::setContent(const std::string& content)
{ {
if (mContent != content) if (mTextData.mContent != content)
{ {
mContent = content; mTextData.mContent = content;
mContentIsDirty = true; mContentIsDirty = true;
} }
} }
void TextNode::updateLines(FontsManager* fontsManager)
{
auto original_count = mTextData.mLines.size();
std::vector<std::string> lines = StringUtils::toLines(mTextData.mContent);
std::vector<std::string> output_lines;
for (auto line : lines)
{
double running_width{0};
std::string working_line;
for (auto c : line)
{
auto glyph_advance = fontsManager->getGlyph(mTextData.mFont.getFaceName(), mTextData.mFont.getSize(), c)->getAdvanceX();
if (false)
//if (running_width + glyph_advance > mWidth)
{
output_lines.push_back(working_line);
working_line = c;
running_width = glyph_advance;
}
else
{
working_line += c;
running_width += glyph_advance;
}
}
output_lines.push_back(working_line);
running_width = 0;
}
mTextData.mLines = output_lines;
if (original_count != mTextData.mLines.size())
{
mLinesAreDirty = true;
}
}
void TextNode::update(FontsManager* fontsManager) void TextNode::update(FontsManager* fontsManager)
{ {
if (!mSceneItem) if (!mSceneItem)
@ -55,10 +122,16 @@ void TextNode::update(FontsManager* fontsManager)
mSceneItem->setName(mName + "_SceneText"); mSceneItem->setName(mName + "_SceneText");
} }
if (mContentIsDirty) if (mTransformIsDirty || mContentIsDirty)
{ {
dynamic_cast<SceneText*>(mSceneItem.get())->setContent(mContent); updateLines(fontsManager);
}
if (mContentIsDirty || mLinesAreDirty)
{
dynamic_cast<SceneText*>(mSceneItem.get())->setTextData(mTextData);
mContentIsDirty = false; mContentIsDirty = false;
mLinesAreDirty = false;
} }
if (mTransformIsDirty) if (mTransformIsDirty)

View file

@ -20,12 +20,25 @@ public:
std::string getContent() const; std::string getContent() const;
std::string getFontLabel() const; std::string getFontLabel() const;
unsigned getWidth() const;
unsigned getHeight() const;
void setWidth(unsigned width);
void setHeight(unsigned height);
void setContent(const std::string& content); void setContent(const std::string& content);
void update(FontsManager* fontsManager) override; void update(FontsManager* fontsManager) override;
private: private:
std::string mContent;
void updateLines(FontsManager* fontsManager);
TextData mTextData;
bool mContentIsDirty{true}; bool mContentIsDirty{true};
bool mLinesAreDirty{true};
unsigned mWidth{1};
unsigned mHeight{1};
}; };
using TextNodetr = std::unique_ptr<TextNode>; using TextNodetr = std::unique_ptr<TextNode>;

View file

@ -16,16 +16,32 @@ std::unique_ptr<XcbEventInterface> XcbEventInterface::Create()
std::unique_ptr<KeyboardEvent> XcbEventInterface::ConvertKeyPress(xcb_key_press_event_t* event, Keyboard* keyboard) const std::unique_ptr<KeyboardEvent> XcbEventInterface::ConvertKeyPress(xcb_key_press_event_t* event, Keyboard* keyboard) const
{ {
auto ui_event = KeyboardEvent::Create(); auto ui_event = KeyboardEvent::Create();
ui_event->SetAction(KeyboardEvent::Action::Pressed); ui_event->setAction(KeyboardEvent::Action::Pressed);
ui_event->SetKeyString(keyboard->getKeyString(event->detail));
if (auto function = keyboard->getFunction(event->detail); function != Keyboard::Function::UNSET)
{
ui_event->setFunction(function);
}
else
{
ui_event->setKeyString(keyboard->getKeyString(event->detail));
}
return ui_event; return ui_event;
} }
std::unique_ptr<KeyboardEvent> XcbEventInterface::ConvertKeyRelease(xcb_key_press_event_t* event, Keyboard* keyboard) const std::unique_ptr<KeyboardEvent> XcbEventInterface::ConvertKeyRelease(xcb_key_press_event_t* event, Keyboard* keyboard) const
{ {
auto ui_event = KeyboardEvent::Create(); auto ui_event = KeyboardEvent::Create();
ui_event->SetAction(KeyboardEvent::Action::Released); ui_event->setAction(KeyboardEvent::Action::Released);
ui_event->SetKeyString(keyboard->getKeyString(event->detail));
if (auto function = keyboard->getFunction(event->detail); function != Keyboard::Function::UNSET)
{
ui_event->setFunction(function);
}
else
{
ui_event->setKeyString(keyboard->getKeyString(event->detail));
}
return ui_event; return ui_event;
} }

View file

@ -8,7 +8,6 @@
class XcbExtensionInterface class XcbExtensionInterface
{ {
public: public:
void initialize(xcb_connection_t* connection); void initialize(xcb_connection_t* connection);
bool isXkBEvent(unsigned responseType) const; bool isXkBEvent(unsigned responseType) const;

View file

@ -34,6 +34,25 @@ void XcbKeyboard::updateState(xcb_xkb_state_notify_event_t *state)
state->lockedGroup); state->lockedGroup);
} }
Keyboard::Function XcbKeyboard::getFunction(Keyboard::KeyCode code)
{
if (!mXkbState)
{
getKeyMap();
}
xkb_keysym_t sym = xkb_state_key_get_one_sym(mXkbState, code);
if (sym == XKB_KEY_Return)
{
return Keyboard::Function::ENTER;
}
else if(sym == XKB_KEY_BackSpace)
{
return Keyboard::Function::BACKSPACE;
}
return Keyboard::Function::UNSET;
}
std::string XcbKeyboard::getKeyString(KeyCode keyCode) std::string XcbKeyboard::getKeyString(KeyCode keyCode)
{ {
if (!mXkbState) if (!mXkbState)

View file

@ -15,6 +15,8 @@ public:
std::string getKeyString(KeyCode keyCode) override; std::string getKeyString(KeyCode keyCode) override;
Function getFunction(KeyCode code) override;
void updateState(xcb_xkb_state_notify_event_t *state); void updateState(xcb_xkb_state_notify_event_t *state);
private: private: