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);
auto textBox = TextBox::Create();
textBox->setName("Text Box");
mTextBox = textBox.get();
auto saveButton = Button::Create();

View file

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

View file

@ -20,6 +20,17 @@ bool StringUtils::IsSpace(char c)
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)
{
if (input.empty())

View file

@ -23,5 +23,8 @@ public:
static std::string ToPaddedString(unsigned numBytes, unsigned entry);
static std::vector<std::string> split(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);
};

View file

@ -38,6 +38,17 @@ public:
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:
unsigned mSize{16};
std::string mFaceName;

View file

@ -44,13 +44,16 @@ void OpenGlTextPainter::initializeShader()
void OpenGlTextPainter::initializeTextures(const TextData& textData, DrawingContext* context)
{
for (auto c : textData.mContent)
for (auto line : textData.mLines)
{
if (auto iter = mFontTextures.find(c); iter == mFontTextures.end())
for (auto c : line)
{
auto glyph = context->getFontsManager()->getGlyph(textData.mFont.getFaceName(), textData.mFont.getSize(), c);
auto new_texture = std::make_unique<OpenGlFontTexture>(glyph);
mFontTextures[c] = std::move(new_texture);
if (auto iter = mFontTextures.find(c); iter == mFontTextures.end())
{
auto glyph = context->getFontsManager()->getGlyph(textData.mFont.getFaceName(), textData.mFont.getSize(), c);
auto new_texture = std::make_unique<OpenGlFontTexture>(glyph);
mFontTextures[c] = std::move(new_texture);
}
}
}
}
@ -104,36 +107,43 @@ void OpenGlTextPainter::paint(SceneText* text, DrawingContext* context)
auto transform = text->getTransform();
float x = transform.getLocation().getX();
const float y = height - transform.getLocation().getY();
for (auto c : text_data.mContent)
float line_delta = 20;
float line_offset = 0;
for (auto line : text_data.mLines)
{
auto texture = mFontTextures[c].get();
float x = transform.getLocation().getX();
const float y = height - line_offset - transform.getLocation().getY();
for (auto c : line)
{
auto texture = mFontTextures[c].get();
float xpos = x + texture->getGlyph()->getBearingX();
float ypos = y - (texture->getGlyph()->getHeight() - texture->getGlyph()->getBearingY());
float xpos = x + texture->getGlyph()->getBearingX();
float ypos = y - (texture->getGlyph()->getHeight() - texture->getGlyph()->getBearingY());
float w = texture->getGlyph()->getWidth();
float h = texture->getGlyph()->getHeight();
float vertices[6][4] = {
{ xpos, ypos + h, 0.0f, 0.0f },
{ xpos, ypos, 0.0f, 1.0f },
{ xpos + w, ypos, 1.0f, 1.0f },
float w = texture->getGlyph()->getWidth();
float h = texture->getGlyph()->getHeight();
float vertices[6][4] = {
{ xpos, ypos + h, 0.0f, 0.0f },
{ xpos, ypos, 0.0f, 1.0f },
{ xpos + w, ypos, 1.0f, 1.0f },
{ xpos, ypos + h, 0.0f, 0.0f },
{ xpos + w, ypos, 1.0f, 1.0f },
{ xpos + w, ypos + h, 1.0f, 0.0f }
};
{ xpos, ypos + h, 0.0f, 0.0f },
{ xpos + w, ypos, 1.0f, 1.0f },
{ xpos + w, ypos + h, 1.0f, 0.0f }
};
glBindTexture(GL_TEXTURE_2D, texture->getHandle());
glBindTexture(GL_TEXTURE_2D, texture->getHandle());
glBindBuffer(GL_ARRAY_BUFFER, mVertexBuffer);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, mVertexBuffer);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDrawArrays(GL_TRIANGLES, 0, 6);
glDrawArrays(GL_TRIANGLES, 0, 6);
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);

View file

@ -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;
}

View file

@ -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>;

View file

@ -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;
}

View file

@ -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>;

View file

@ -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)

View file

@ -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)

View file

@ -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);

View file

@ -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>;

View file

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

View file

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

View file

@ -3,10 +3,27 @@
#include "Color.h"
#include "FontItem.h"
#include <vector>
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:
TextData() = default;
std::string mContent;
std::vector<std::string> mLines;
FontItem mFont;
};

View file

@ -5,17 +5,20 @@
#include "IFontEngine.h"
#include "MeshPrimitives.h"
#include "FontItem.h"
#include "FontGlyph.h"
#include "SceneText.h"
#include "Color.h"
#include "StringUtils.h"
#include <iostream>
TextNode::TextNode(const std::string& content, const DiscretePoint& loc)
: MaterialNode(loc)
{
mContent= content;
mTextData.mContent= content;
}
TextNode::~TextNode()
@ -35,18 +38,82 @@ std::string TextNode::getFontLabel() 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)
{
if (mContent != content)
if (mTextData.mContent != content)
{
mContent = content;
mTextData.mContent = content;
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)
{
if (!mSceneItem)
@ -55,10 +122,16 @@ void TextNode::update(FontsManager* fontsManager)
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;
mLinesAreDirty = false;
}
if (mTransformIsDirty)

View file

@ -20,12 +20,25 @@ public:
std::string getContent() 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 update(FontsManager* fontsManager) override;
private:
std::string mContent;
void updateLines(FontsManager* fontsManager);
TextData mTextData;
bool mContentIsDirty{true};
bool mLinesAreDirty{true};
unsigned mWidth{1};
unsigned mHeight{1};
};
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
{
auto ui_event = KeyboardEvent::Create();
ui_event->SetAction(KeyboardEvent::Action::Pressed);
ui_event->SetKeyString(keyboard->getKeyString(event->detail));
ui_event->setAction(KeyboardEvent::Action::Pressed);
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;
}
std::unique_ptr<KeyboardEvent> XcbEventInterface::ConvertKeyRelease(xcb_key_press_event_t* event, Keyboard* keyboard) const
{
auto ui_event = KeyboardEvent::Create();
ui_event->SetAction(KeyboardEvent::Action::Released);
ui_event->SetKeyString(keyboard->getKeyString(event->detail));
ui_event->setAction(KeyboardEvent::Action::Released);
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;
}

View file

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

View file

@ -34,6 +34,25 @@ void XcbKeyboard::updateState(xcb_xkb_state_notify_event_t *state)
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)
{
if (!mXkbState)

View file

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