From c11dfce022525579270720de70e61f1be4dbfeb7 Mon Sep 17 00:00:00 2001 From: Lendemor Date: Sat, 19 Aug 2017 11:58:52 +0200 Subject: [PATCH] textinputnode --- src/scene/gui/textinputnode.cpp | 91 ++++++++++++++++++++------------- src/scene/gui/textinputnode.h | 5 ++ src/test/main.cpp | 1 - 3 files changed, 61 insertions(+), 36 deletions(-) diff --git a/src/scene/gui/textinputnode.cpp b/src/scene/gui/textinputnode.cpp index cd8cb64..62ac10e 100644 --- a/src/scene/gui/textinputnode.cpp +++ b/src/scene/gui/textinputnode.cpp @@ -10,9 +10,19 @@ TextInputNode::TextInputNode(glm::vec2 dimension): m_dimension(dimension), + m_hasFocus(false), m_font_size(16.f), + m_text_mesh(nullptr), + m_text(""), + m_text_updated(false), m_text_color(glm::vec3(1,1,1)), - m_cursor_pos(0) + m_cursor_mesh(nullptr), + m_cursor_pos(0), + m_cursor_pos_updated(false), + m_move_cursor_left(NO_ACTION), + m_move_cursor_right(NO_ACTION), + m_callback(nullptr), + m_tab_callback(nullptr) { // Font *shellfont = RESOURCE_GET(Font,"shellfont"); Mesh* mesh = new Mesh(); @@ -31,21 +41,19 @@ void TextInputNode::update() if(!m_hasFocus) return; std::wstring text = getEngine().getInput()->getText(); - bool input_string_updated = false; - bool cursor_pos_updated = false; auto input = getEngine().getInput(); for(auto action : input->getActions()){ if (action.action == m_move_cursor_left){ if (m_cursor_pos > 0){ m_cursor_pos--; - cursor_pos_updated=true; + m_cursor_pos_updated=true; } } else if(action.action == m_move_cursor_right){ if(m_cursor_pos < m_text.length()){ m_cursor_pos++; - cursor_pos_updated=true; + m_cursor_pos_updated=true; } } } @@ -56,17 +64,17 @@ void TextInputNode::update() case 8: if(m_cursor_pos > 0) m_text.erase(--m_cursor_pos,1); - input_string_updated = true; - cursor_pos_updated=true; + m_text_updated = true; + m_cursor_pos_updated=true; break; case 13: if (!m_text.empty()) { if(m_callback) m_callback->exec(); - input_string_updated = true; + m_text_updated = true; m_cursor_pos = 0; - cursor_pos_updated=true; + m_cursor_pos_updated=true; } break; case 9: @@ -74,48 +82,64 @@ void TextInputNode::update() { if(m_tab_callback) m_tab_callback->exec(); - input_string_updated = true; - cursor_pos_updated=true; + m_text_updated = true; + m_cursor_pos_updated=true; } break; case 127: m_text.erase(m_cursor_pos,1); - input_string_updated = true; + m_text_updated = true; break; default: m_text.insert(m_cursor_pos++,std::string(1,c)); - input_string_updated = true; - cursor_pos_updated=true; + m_text_updated = true; + m_cursor_pos_updated=true; } } - Font *shellfont = RESOURCE_GET(Font,"shellfont"); - if(cursor_pos_updated){ - m_cursor_mesh->moveTo2D(glm::vec2(m_cursor_pos*shellfont->getXAdvance()*(m_font_size/shellfont->getLineHeight()),0)); - } + if(m_cursor_pos_updated) + updateCursorMesh(); + + if(m_text_updated) + updateTextMesh(); - if(input_string_updated) - { - if(m_text_mesh){ - this->removeChild(m_text_mesh); -// delete(m_text_mesh); - } - m_text_mesh = shellfont->getTextNode(m_text,m_text_color,m_font_size,false); - if(m_text_mesh){ - m_text_mesh->setTransform(glm::mat4()); - m_text_mesh->setDepth(15); - this->addChild(m_text_mesh); - m_text_mesh->setVisible(true); - } - } GUINode::update(); } +void TextInputNode::updateCursorMesh(){ + Font *shellfont = RESOURCE_GET(Font,"shellfont"); + m_cursor_mesh->moveTo2D(glm::vec2(m_cursor_pos*shellfont->getXAdvance()*(m_font_size/shellfont->getLineHeight()),0)); + m_cursor_pos_updated = false; +} + +void TextInputNode::updateTextMesh(){ + Font *shellfont = RESOURCE_GET(Font,"shellfont"); + if(m_text_mesh){ + this->removeChild(m_text_mesh); + delete(m_text_mesh); + } + m_text_mesh = shellfont->getTextNode(m_text,m_text_color,m_font_size,false); + if(m_text_mesh){ + m_text_mesh->setTransform(glm::mat4()); + m_text_mesh->setDepth(15); + this->addChild(m_text_mesh); + m_text_mesh->setVisible(true); + } + m_text_updated = false; +} + void TextInputNode::setFocus(bool focus) { m_hasFocus = focus; m_cursor_mesh->setVisible(focus); } +void TextInputNode::setText(std::string text){ + m_text = text; + m_text_updated = true; + m_cursor_pos = text.length(); + m_cursor_pos_updated = true; +} + void TextInputNode::setTextColor(glm::vec3 color) { m_text_color = color; @@ -126,6 +150,3 @@ std::string TextInputNode::getText() return m_text; } - - - diff --git a/src/scene/gui/textinputnode.h b/src/scene/gui/textinputnode.h index 5501ee7..a53789b 100644 --- a/src/scene/gui/textinputnode.h +++ b/src/scene/gui/textinputnode.h @@ -15,10 +15,12 @@ class TextInputNode : public GUINode TextNode* m_text_mesh; std::string m_text; + bool m_text_updated; glm::vec3 m_text_color; MeshNode* m_cursor_mesh; unsigned int m_cursor_pos; + bool m_cursor_pos_updated; int m_move_cursor_left; int m_move_cursor_right; @@ -29,10 +31,13 @@ class TextInputNode : public GUINode public: TextInputNode(glm::vec2 dimension); void update(); + void updateCursorMesh(); + void updateTextMesh(); glm::vec2 getDimension(){return m_dimension;} void setFocus(bool focus); void setTextColor(glm::vec3 color); + void setText(std::string text); std::string getText(); void setCallBack(CallBack* callback){m_callback = callback;} void setTabCallBack(CallBack* callback){m_tab_callback = callback;} diff --git a/src/test/main.cpp b/src/test/main.cpp index 05598f2..0fab4e0 100644 --- a/src/test/main.cpp +++ b/src/test/main.cpp @@ -244,7 +244,6 @@ public: void exec(){ m_demo->initScene(); - m_engine->getShell()->out("button!"); m_engine->setScene(m_demo->getScene()); m_engine->getInput()->setCurrentContext("default"); m_engine->getInput()->setMouseGrabbed(true);