textinputnode

This commit is contained in:
Lendemor 2017-08-19 11:58:52 +02:00
parent 9cc37dc4cf
commit c11dfce022
3 changed files with 61 additions and 36 deletions

View File

@ -10,9 +10,19 @@
TextInputNode::TextInputNode(glm::vec2 dimension): TextInputNode::TextInputNode(glm::vec2 dimension):
m_dimension(dimension), m_dimension(dimension),
m_hasFocus(false),
m_font_size(16.f), m_font_size(16.f),
m_text_mesh(nullptr),
m_text(""),
m_text_updated(false),
m_text_color(glm::vec3(1,1,1)), 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"); // Font *shellfont = RESOURCE_GET(Font,"shellfont");
Mesh* mesh = new Mesh(); Mesh* mesh = new Mesh();
@ -31,21 +41,19 @@ void TextInputNode::update()
if(!m_hasFocus) return; if(!m_hasFocus) return;
std::wstring text = getEngine().getInput()->getText(); std::wstring text = getEngine().getInput()->getText();
bool input_string_updated = false;
bool cursor_pos_updated = false;
auto input = getEngine().getInput(); auto input = getEngine().getInput();
for(auto action : input->getActions()){ for(auto action : input->getActions()){
if (action.action == m_move_cursor_left){ if (action.action == m_move_cursor_left){
if (m_cursor_pos > 0){ if (m_cursor_pos > 0){
m_cursor_pos--; m_cursor_pos--;
cursor_pos_updated=true; m_cursor_pos_updated=true;
} }
} }
else if(action.action == m_move_cursor_right){ else if(action.action == m_move_cursor_right){
if(m_cursor_pos < m_text.length()){ if(m_cursor_pos < m_text.length()){
m_cursor_pos++; m_cursor_pos++;
cursor_pos_updated=true; m_cursor_pos_updated=true;
} }
} }
} }
@ -56,17 +64,17 @@ void TextInputNode::update()
case 8: case 8:
if(m_cursor_pos > 0) if(m_cursor_pos > 0)
m_text.erase(--m_cursor_pos,1); m_text.erase(--m_cursor_pos,1);
input_string_updated = true; m_text_updated = true;
cursor_pos_updated=true; m_cursor_pos_updated=true;
break; break;
case 13: case 13:
if (!m_text.empty()) if (!m_text.empty())
{ {
if(m_callback) if(m_callback)
m_callback->exec(); m_callback->exec();
input_string_updated = true; m_text_updated = true;
m_cursor_pos = 0; m_cursor_pos = 0;
cursor_pos_updated=true; m_cursor_pos_updated=true;
} }
break; break;
case 9: case 9:
@ -74,30 +82,40 @@ void TextInputNode::update()
{ {
if(m_tab_callback) if(m_tab_callback)
m_tab_callback->exec(); m_tab_callback->exec();
input_string_updated = true; m_text_updated = true;
cursor_pos_updated=true; m_cursor_pos_updated=true;
} }
break; break;
case 127: case 127:
m_text.erase(m_cursor_pos,1); m_text.erase(m_cursor_pos,1);
input_string_updated = true; m_text_updated = true;
break; break;
default: default:
m_text.insert(m_cursor_pos++,std::string(1,c)); m_text.insert(m_cursor_pos++,std::string(1,c));
input_string_updated = true; m_text_updated = true;
cursor_pos_updated=true; m_cursor_pos_updated=true;
} }
} }
Font *shellfont = RESOURCE_GET(Font,"shellfont"); if(m_cursor_pos_updated)
if(cursor_pos_updated){ updateCursorMesh();
m_cursor_mesh->moveTo2D(glm::vec2(m_cursor_pos*shellfont->getXAdvance()*(m_font_size/shellfont->getLineHeight()),0));
if(m_text_updated)
updateTextMesh();
GUINode::update();
} }
if(input_string_updated) 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){ if(m_text_mesh){
this->removeChild(m_text_mesh); this->removeChild(m_text_mesh);
// delete(m_text_mesh); delete(m_text_mesh);
} }
m_text_mesh = shellfont->getTextNode(m_text,m_text_color,m_font_size,false); m_text_mesh = shellfont->getTextNode(m_text,m_text_color,m_font_size,false);
if(m_text_mesh){ if(m_text_mesh){
@ -106,8 +124,7 @@ void TextInputNode::update()
this->addChild(m_text_mesh); this->addChild(m_text_mesh);
m_text_mesh->setVisible(true); m_text_mesh->setVisible(true);
} }
} m_text_updated = false;
GUINode::update();
} }
void TextInputNode::setFocus(bool focus) void TextInputNode::setFocus(bool focus)
@ -116,6 +133,13 @@ void TextInputNode::setFocus(bool focus)
m_cursor_mesh->setVisible(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) void TextInputNode::setTextColor(glm::vec3 color)
{ {
m_text_color = color; m_text_color = color;
@ -126,6 +150,3 @@ std::string TextInputNode::getText()
return m_text; return m_text;
} }

View File

@ -15,10 +15,12 @@ class TextInputNode : public GUINode
TextNode* m_text_mesh; TextNode* m_text_mesh;
std::string m_text; std::string m_text;
bool m_text_updated;
glm::vec3 m_text_color; glm::vec3 m_text_color;
MeshNode* m_cursor_mesh; MeshNode* m_cursor_mesh;
unsigned int m_cursor_pos; unsigned int m_cursor_pos;
bool m_cursor_pos_updated;
int m_move_cursor_left; int m_move_cursor_left;
int m_move_cursor_right; int m_move_cursor_right;
@ -29,10 +31,13 @@ class TextInputNode : public GUINode
public: public:
TextInputNode(glm::vec2 dimension); TextInputNode(glm::vec2 dimension);
void update(); void update();
void updateCursorMesh();
void updateTextMesh();
glm::vec2 getDimension(){return m_dimension;} glm::vec2 getDimension(){return m_dimension;}
void setFocus(bool focus); void setFocus(bool focus);
void setTextColor(glm::vec3 color); void setTextColor(glm::vec3 color);
void setText(std::string text);
std::string getText(); std::string getText();
void setCallBack(CallBack* callback){m_callback = callback;} void setCallBack(CallBack* callback){m_callback = callback;}
void setTabCallBack(CallBack* callback){m_tab_callback = callback;} void setTabCallBack(CallBack* callback){m_tab_callback = callback;}

View File

@ -244,7 +244,6 @@ public:
void exec(){ void exec(){
m_demo->initScene(); m_demo->initScene();
m_engine->getShell()->out("button!");
m_engine->setScene(m_demo->getScene()); m_engine->setScene(m_demo->getScene());
m_engine->getInput()->setCurrentContext("default"); m_engine->getInput()->setCurrentContext("default");
m_engine->getInput()->setMouseGrabbed(true); m_engine->getInput()->setMouseGrabbed(true);