From 7c2e7f9069f0e5a462f1bf6a44ed39737d3f9b8e Mon Sep 17 00:00:00 2001 From: Aloshi Date: Mon, 19 Aug 2013 10:36:48 -0500 Subject: [PATCH] Basic text editing support. --- src/GuiComponent.cpp | 8 +++++ src/GuiComponent.h | 2 ++ src/InputManager.cpp | 16 +++++++++ src/components/ComponentListComponent.cpp | 6 ++++ src/components/ComponentListComponent.h | 1 + src/components/GuiGameEd.cpp | 2 +- src/components/TextEditComponent.cpp | 41 ++++++++++++++++++++--- src/components/TextEditComponent.h | 10 ++++-- 8 files changed, 78 insertions(+), 8 deletions(-) diff --git a/src/GuiComponent.cpp b/src/GuiComponent.cpp index 06f55d287..501e5dd8b 100644 --- a/src/GuiComponent.cpp +++ b/src/GuiComponent.cpp @@ -166,3 +166,11 @@ std::string GuiComponent::getValue() const { return ""; } + +void GuiComponent::textInput(const char* text) +{ + for(auto iter = mChildren.begin(); iter != mChildren.end(); iter++) + { + (*iter)->textInput(text); + } +} diff --git a/src/GuiComponent.h b/src/GuiComponent.h index a9ca6eb7a..aad06ffa3 100644 --- a/src/GuiComponent.h +++ b/src/GuiComponent.h @@ -12,6 +12,8 @@ public: GuiComponent(Window* window); virtual ~GuiComponent(); + virtual void textInput(const char* text); + //Called when input is received. //Return true if the input is consumed, false if it should continue to be passed to other children. virtual bool input(InputConfig* config, Input input); diff --git a/src/InputManager.cpp b/src/InputManager.cpp index 74ca08efa..6ab7e4e58 100644 --- a/src/InputManager.cpp +++ b/src/InputManager.cpp @@ -156,6 +156,17 @@ bool InputManager::parseEvent(const SDL_Event& ev) return true; case SDL_KEYDOWN: + if(ev.key.keysym.sym == SDLK_BACKSPACE && SDL_IsTextInputActive()) + { + if(mWindow->peekGui() != NULL) + mWindow->peekGui()->textInput("\b"); + + return true; + } + + if(ev.key.repeat) + return false; + if(ev.key.keysym.sym == SDLK_F4) { SDL_Event* quit = new SDL_Event(); @@ -171,6 +182,11 @@ bool InputManager::parseEvent(const SDL_Event& ev) mWindow->input(getInputConfigByDevice(DEVICE_KEYBOARD), Input(DEVICE_KEYBOARD, TYPE_KEY, ev.key.keysym.sym, 0, false)); return true; + case SDL_TEXTINPUT: + if(mWindow->peekGui() != NULL) + mWindow->peekGui()->textInput(ev.text.text); + break; + case SDL_JOYDEVICEADDED: deinit(); init(); diff --git a/src/components/ComponentListComponent.cpp b/src/components/ComponentListComponent.cpp index 31134db64..f371ea5fc 100644 --- a/src/components/ComponentListComponent.cpp +++ b/src/components/ComponentListComponent.cpp @@ -340,6 +340,12 @@ void ComponentListComponent::render(const Eigen::Affine3f& parentTrans) } } +void ComponentListComponent::textInput(const char* text) +{ + if(getSelectedComponent() != NULL) + getSelectedComponent()->textInput(text); +} + void ComponentListComponent::onPositionChanged() { updateComponentOffsets(); diff --git a/src/components/ComponentListComponent.h b/src/components/ComponentListComponent.h index 047ace574..295f3c9a4 100644 --- a/src/components/ComponentListComponent.h +++ b/src/components/ComponentListComponent.h @@ -23,6 +23,7 @@ public: void onPositionChanged() override; + void textInput(const char* text) override; bool input(InputConfig* config, Input input) override; void update(int deltaTime) override; void render(const Eigen::Affine3f& parentTrans) override; diff --git a/src/components/GuiGameEd.cpp b/src/components/GuiGameEd.cpp index 223402e0c..ee4d25dda 100644 --- a/src/components/GuiGameEd.cpp +++ b/src/components/GuiGameEd.cpp @@ -88,10 +88,10 @@ void GuiGameEd::populateList(const std::vector& mdd) mGeneratedComponents.push_back(ed); y++; + break; } - //force list reflow mList.onPositionChanged(); } diff --git a/src/components/TextEditComponent.cpp b/src/components/TextEditComponent.cpp index a1bb42444..576a94c4d 100644 --- a/src/components/TextEditComponent.cpp +++ b/src/components/TextEditComponent.cpp @@ -1,5 +1,8 @@ #include "TextEditComponent.h" #include "../Log.h" +#include "../Font.h" +#include "../Window.h" +#include "../Renderer.h" TextEditComponent::TextEditComponent(Window* window) : GuiComponent(window), mBox(window, 0, 0, 0, 0) @@ -8,25 +11,28 @@ TextEditComponent::TextEditComponent(Window* window) : GuiComponent(window), onFocusLost(); - setSize(48, 22); + LOG(LogInfo) << getFont()->getHeight(); + setSize(256, /*(float)getFont()->getHeight()*/ 41); } void TextEditComponent::onFocusGained() { mBox.setHorizontalImage(":/glow_hor.png"); mBox.setVerticalImage(":/glow_vert.png"); - mBox.setBorderColor(0x51CCFFFF); + mBox.setBorderColor(0x51CCFF00 | getOpacity()); SDL_StartTextInput(); + mFocused = true; } void TextEditComponent::onFocusLost() { mBox.setHorizontalImage(":/glow_off_hor.png"); mBox.setVerticalImage(":/glow_off_vert.png"); - mBox.setBorderColor(0xFFFFFFFF); + mBox.setBorderColor(0xFFFFFF00 | getOpacity()); SDL_StopTextInput(); + mFocused = false; } void TextEditComponent::onSizeChanged() @@ -44,7 +50,32 @@ std::string TextEditComponent::getValue() const return mText; } -bool TextEditComponent::input(InputConfig* config, Input input) +void TextEditComponent::textInput(const char* text) { - return GuiComponent::input(config, input); + if(mFocused) + { + if(text[0] == '\b') + { + if(mText.length() > 0) + mText.erase(mText.end() - 1, mText.end()); + }else{ + mText += text; + } + } +} + +void TextEditComponent::render(const Eigen::Affine3f& parentTrans) +{ + Eigen::Affine3f trans = getTransform() * parentTrans; + renderChildren(trans); + + Renderer::setMatrix(trans); + + std::shared_ptr f = getFont(); + f->drawText(mText, Eigen::Vector2f::Zero(), 0x00000000 | getOpacity()); +} + +std::shared_ptr TextEditComponent::getFont() +{ + return Font::get(*mWindow->getResourceManager(), Font::getDefaultPath(), FONT_SIZE_SMALL); } diff --git a/src/components/TextEditComponent.h b/src/components/TextEditComponent.h index 8a2ec59fb..98316a823 100644 --- a/src/components/TextEditComponent.h +++ b/src/components/TextEditComponent.h @@ -3,12 +3,15 @@ #include "../GuiComponent.h" #include "GuiBox.h" +class Font; + class TextEditComponent : public GuiComponent { public: TextEditComponent(Window* window); - bool input(InputConfig* config, Input input) override; + void textInput(const char* text) override; + void render(const Eigen::Affine3f& parentTrans) override; void onFocusGained() override; void onFocusLost() override; @@ -17,9 +20,12 @@ public: void setValue(const std::string& val) override; std::string getValue() const override; + private: std::string mText; - bool mAllowNewlines; + bool mFocused; + + std::shared_ptr getFont(); GuiBox mBox; };