Added support for pasting text into the application when a text input field is focused

This commit is contained in:
Leon Styhre 2023-07-20 16:01:24 +02:00
parent 81ac2fd2a6
commit bd591a74cb
11 changed files with 39 additions and 17 deletions

View file

@ -62,10 +62,10 @@ GuiComponent::~GuiComponent()
getChild(i)->setParent(nullptr); getChild(i)->setParent(nullptr);
} }
void GuiComponent::textInput(const std::string& text) void GuiComponent::textInput(const std::string& text, const bool pasting)
{ {
for (auto it = mChildren.cbegin(); it != mChildren.cend(); ++it) for (auto it = mChildren.cbegin(); it != mChildren.cend(); ++it)
(*it)->textInput(text); (*it)->textInput(text, pasting);
} }
bool GuiComponent::input(InputConfig* config, Input input) bool GuiComponent::input(InputConfig* config, Input input)

View file

@ -63,7 +63,7 @@ public:
GuiComponent(); GuiComponent();
virtual ~GuiComponent(); virtual ~GuiComponent();
virtual void textInput(const std::string& text); virtual void textInput(const std::string& text, const bool pasting = false);
// Called when input is received. // Called when input is received.
// Return true if the input is consumed, false if it should continue to be passed // Return true if the input is consumed, false if it should continue to be passed

View file

@ -417,8 +417,26 @@ bool InputManager::parseEvent(const SDL_Event& event)
return true; return true;
} }
case SDL_KEYDOWN: { case SDL_KEYDOWN: {
if (event.key.keysym.sym == SDLK_BACKSPACE && SDL_IsTextInputActive()) if (SDL_IsTextInputActive()) {
mWindow->textInput("\b"); // Paste from clipboard.
#if defined(__APPLE__)
if (event.key.keysym.mod & KMOD_GUI && event.key.keysym.sym == SDLK_v) {
#else
if ((event.key.keysym.mod & KMOD_CTRL && event.key.keysym.sym == SDLK_v) ||
(event.key.keysym.mod & KMOD_SHIFT && event.key.keysym.sym == SDLK_INSERT)) {
#endif
if (SDL_HasClipboardText()) {
char* clipboardText {SDL_GetClipboardText()};
mWindow->textInput(clipboardText, true);
SDL_free(clipboardText);
return true;
}
}
// Handle backspace presses.
if (event.key.keysym.sym == SDLK_BACKSPACE)
mWindow->textInput("\b");
}
if (event.key.repeat) if (event.key.repeat)
return false; return false;

View file

@ -373,10 +373,10 @@ void Window::input(InputConfig* config, Input input)
} }
} }
void Window::textInput(const std::string& text) void Window::textInput(const std::string& text, const bool pasting)
{ {
if (peekGui()) if (peekGui())
peekGui()->textInput(text); peekGui()->textInput(text, pasting);
} }
void Window::logInput(InputConfig* config, Input input) void Window::logInput(InputConfig* config, Input input)

View file

@ -110,7 +110,7 @@ public:
void deinit(); void deinit();
void input(InputConfig* config, Input input); void input(InputConfig* config, Input input);
void textInput(const std::string& text); void textInput(const std::string& text, const bool pasting = false);
void logInput(InputConfig* config, Input input); void logInput(InputConfig* config, Input input);
void update(int deltaTime); void update(int deltaTime);
void render(); void render();

View file

@ -436,11 +436,11 @@ void ComponentGrid::render(const glm::mat4& parentTrans)
} }
} }
void ComponentGrid::textInput(const std::string& text) void ComponentGrid::textInput(const std::string& text, const bool pasting)
{ {
const GridEntry* selectedEntry {getCellAt(mCursor)}; const GridEntry* selectedEntry {getCellAt(mCursor)};
if (selectedEntry != nullptr && selectedEntry->canFocus) if (selectedEntry != nullptr && selectedEntry->canFocus)
selectedEntry->component->textInput(text); selectedEntry->component->textInput(text, pasting);
} }
void ComponentGrid::onCursorMoved(glm::ivec2 from, glm::ivec2 to) void ComponentGrid::onCursorMoved(glm::ivec2 from, glm::ivec2 to)

View file

@ -51,7 +51,7 @@ public:
mPastBoundaryCallback = func; mPastBoundaryCallback = func;
} }
void textInput(const std::string& text) override; void textInput(const std::string& text, const bool pasting = false) override;
bool input(InputConfig* config, Input input) override; bool input(InputConfig* config, Input input) override;
void update(int deltaTime) override; void update(int deltaTime) override;
void render(const glm::mat4& parentTrans) override; void render(const glm::mat4& parentTrans) override;

View file

@ -490,12 +490,12 @@ void ComponentList::updateElementSize(const ComponentListRow& row)
(*it)->setSize(width, (*it)->getSize().y); (*it)->setSize(width, (*it)->getSize().y);
} }
void ComponentList::textInput(const std::string& text) void ComponentList::textInput(const std::string& text, const bool pasting)
{ {
if (!size()) if (!size())
return; return;
mEntries.at(mCursor).data.elements.back().component->textInput(text); mEntries.at(mCursor).data.elements.back().component->textInput(text, pasting);
} }
std::vector<HelpPrompt> ComponentList::getHelpPrompts() std::vector<HelpPrompt> ComponentList::getHelpPrompts()

View file

@ -70,7 +70,7 @@ public:
void addRow(const ComponentListRow& row, bool setCursorHere = false); void addRow(const ComponentListRow& row, bool setCursorHere = false);
void textInput(const std::string& text) override; void textInput(const std::string& text, const bool pasting = false) override;
bool input(InputConfig* config, Input input) override; bool input(InputConfig* config, Input input) override;
void update(int deltaTime) override; void update(int deltaTime) override;
void render(const glm::mat4& parentTrans) override; void render(const glm::mat4& parentTrans) override;

View file

@ -71,9 +71,13 @@ void TextEditComponent::setValue(const std::string& val)
onCursorChanged(); onCursorChanged();
} }
void TextEditComponent::textInput(const std::string& text) void TextEditComponent::textInput(const std::string& text, const bool pasting)
{ {
if (mMaskInput) if (mMaskInput && !pasting)
return;
// Allow pasting up to a reasonable max clipboard size.
if (pasting && text.length() > (isMultiline() ? 16384 : 300))
return; return;
if (mEditing) { if (mEditing) {

View file

@ -22,7 +22,7 @@ class TextEditComponent : public GuiComponent
public: public:
TextEditComponent(); TextEditComponent();
void textInput(const std::string& text) override; void textInput(const std::string& text, const bool pasting = false) override;
bool input(InputConfig* config, Input input) override; bool input(InputConfig* config, Input input) override;
void update(int deltaTime) override; void update(int deltaTime) override;
void render(const glm::mat4& parentTrans) override; void render(const glm::mat4& parentTrans) override;