Basic text editing support.

This commit is contained in:
Aloshi 2013-08-19 10:36:48 -05:00
parent 1418f85ba7
commit 7c2e7f9069
8 changed files with 78 additions and 8 deletions

View file

@ -166,3 +166,11 @@ std::string GuiComponent::getValue() const
{ {
return ""; return "";
} }
void GuiComponent::textInput(const char* text)
{
for(auto iter = mChildren.begin(); iter != mChildren.end(); iter++)
{
(*iter)->textInput(text);
}
}

View file

@ -12,6 +12,8 @@ public:
GuiComponent(Window* window); GuiComponent(Window* window);
virtual ~GuiComponent(); virtual ~GuiComponent();
virtual void textInput(const char* text);
//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 to other children. //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); virtual bool input(InputConfig* config, Input input);

View file

@ -156,6 +156,17 @@ bool InputManager::parseEvent(const SDL_Event& ev)
return true; return true;
case SDL_KEYDOWN: 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) if(ev.key.keysym.sym == SDLK_F4)
{ {
SDL_Event* quit = new SDL_Event(); 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)); mWindow->input(getInputConfigByDevice(DEVICE_KEYBOARD), Input(DEVICE_KEYBOARD, TYPE_KEY, ev.key.keysym.sym, 0, false));
return true; return true;
case SDL_TEXTINPUT:
if(mWindow->peekGui() != NULL)
mWindow->peekGui()->textInput(ev.text.text);
break;
case SDL_JOYDEVICEADDED: case SDL_JOYDEVICEADDED:
deinit(); deinit();
init(); init();

View file

@ -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() void ComponentListComponent::onPositionChanged()
{ {
updateComponentOffsets(); updateComponentOffsets();

View file

@ -23,6 +23,7 @@ public:
void onPositionChanged() override; void onPositionChanged() override;
void textInput(const char* text) 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 Eigen::Affine3f& parentTrans) override; void render(const Eigen::Affine3f& parentTrans) override;

View file

@ -88,10 +88,10 @@ void GuiGameEd::populateList(const std::vector<MetaDataDecl>& mdd)
mGeneratedComponents.push_back(ed); mGeneratedComponents.push_back(ed);
y++; y++;
break;
} }
//force list reflow //force list reflow
mList.onPositionChanged(); mList.onPositionChanged();
} }

View file

@ -1,5 +1,8 @@
#include "TextEditComponent.h" #include "TextEditComponent.h"
#include "../Log.h" #include "../Log.h"
#include "../Font.h"
#include "../Window.h"
#include "../Renderer.h"
TextEditComponent::TextEditComponent(Window* window) : GuiComponent(window), TextEditComponent::TextEditComponent(Window* window) : GuiComponent(window),
mBox(window, 0, 0, 0, 0) mBox(window, 0, 0, 0, 0)
@ -8,25 +11,28 @@ TextEditComponent::TextEditComponent(Window* window) : GuiComponent(window),
onFocusLost(); onFocusLost();
setSize(48, 22); LOG(LogInfo) << getFont()->getHeight();
setSize(256, /*(float)getFont()->getHeight()*/ 41);
} }
void TextEditComponent::onFocusGained() void TextEditComponent::onFocusGained()
{ {
mBox.setHorizontalImage(":/glow_hor.png"); mBox.setHorizontalImage(":/glow_hor.png");
mBox.setVerticalImage(":/glow_vert.png"); mBox.setVerticalImage(":/glow_vert.png");
mBox.setBorderColor(0x51CCFFFF); mBox.setBorderColor(0x51CCFF00 | getOpacity());
SDL_StartTextInput(); SDL_StartTextInput();
mFocused = true;
} }
void TextEditComponent::onFocusLost() void TextEditComponent::onFocusLost()
{ {
mBox.setHorizontalImage(":/glow_off_hor.png"); mBox.setHorizontalImage(":/glow_off_hor.png");
mBox.setVerticalImage(":/glow_off_vert.png"); mBox.setVerticalImage(":/glow_off_vert.png");
mBox.setBorderColor(0xFFFFFFFF); mBox.setBorderColor(0xFFFFFF00 | getOpacity());
SDL_StopTextInput(); SDL_StopTextInput();
mFocused = false;
} }
void TextEditComponent::onSizeChanged() void TextEditComponent::onSizeChanged()
@ -44,7 +50,32 @@ std::string TextEditComponent::getValue() const
return mText; 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<Font> f = getFont();
f->drawText(mText, Eigen::Vector2f::Zero(), 0x00000000 | getOpacity());
}
std::shared_ptr<Font> TextEditComponent::getFont()
{
return Font::get(*mWindow->getResourceManager(), Font::getDefaultPath(), FONT_SIZE_SMALL);
} }

View file

@ -3,12 +3,15 @@
#include "../GuiComponent.h" #include "../GuiComponent.h"
#include "GuiBox.h" #include "GuiBox.h"
class Font;
class TextEditComponent : public GuiComponent class TextEditComponent : public GuiComponent
{ {
public: public:
TextEditComponent(Window* window); 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 onFocusGained() override;
void onFocusLost() override; void onFocusLost() override;
@ -17,9 +20,12 @@ public:
void setValue(const std::string& val) override; void setValue(const std::string& val) override;
std::string getValue() const override; std::string getValue() const override;
private: private:
std::string mText; std::string mText;
bool mAllowNewlines; bool mFocused;
std::shared_ptr<Font> getFont();
GuiBox mBox; GuiBox mBox;
}; };