diff --git a/src/scene/gui/textinputnode.cpp b/src/scene/gui/textinputnode.cpp index 1aa7601..cd8cb64 100644 --- a/src/scene/gui/textinputnode.cpp +++ b/src/scene/gui/textinputnode.cpp @@ -69,6 +69,15 @@ void TextInputNode::update() cursor_pos_updated=true; } break; + case 9: + if (!m_text.empty()) + { + if(m_tab_callback) + m_tab_callback->exec(); + input_string_updated = true; + cursor_pos_updated=true; + } + break; case 127: m_text.erase(m_cursor_pos,1); input_string_updated = true; diff --git a/src/scene/gui/textinputnode.h b/src/scene/gui/textinputnode.h index dc93e61..5501ee7 100644 --- a/src/scene/gui/textinputnode.h +++ b/src/scene/gui/textinputnode.h @@ -24,6 +24,7 @@ class TextInputNode : public GUINode int m_move_cursor_right; CallBack* m_callback; + CallBack* m_tab_callback; public: TextInputNode(glm::vec2 dimension); @@ -34,6 +35,7 @@ public: void setTextColor(glm::vec3 color); std::string getText(); void setCallBack(CallBack* callback){m_callback = callback;} + void setTabCallBack(CallBack* callback){m_tab_callback = callback;} void setMoveCursorLeft(int action){m_move_cursor_left = action;} void setMoveCursorRight(int action){m_move_cursor_right = action;} void clearText(){m_text.clear();} diff --git a/src/sparrowshell/scriptnode.cpp b/src/sparrowshell/scriptnode.cpp index a9cf371..e575ff1 100644 --- a/src/sparrowshell/scriptnode.cpp +++ b/src/sparrowshell/scriptnode.cpp @@ -8,10 +8,12 @@ #include "SparrowRenderer/Version.h" #include "SparrowSerializer/Version.h" -#define LUA_SET_FUN(var) m_script.set_function(#var,&ScriptNode::var,this) +#define LUA_DEFINE(var) S[#var]=var +#define LUA_SET_FUN(var) m_script.set_function(#var,&ScriptNode::var,this);this->functions_name.push_back(#var); ScriptNode::ScriptNode() { + LUA_SET_FUN(getDeltaTime); LUA_SET_FUN(print); LUA_SET_FUN(version); LUA_SET_FUN(clear); @@ -31,6 +33,14 @@ void ScriptNode::execute(std::string to_execute){ } } +std::vector ScriptNode::possibleCompletion(std::string search){ + std::vector result; + for(std::string& name : functions_name) + if(name.find(search)==0) + result.push_back(name); + return result; +} + void ScriptNode::print(std::string to_print){ this->getEngine().getShell()->out(to_print.append("\n")); } @@ -54,3 +64,6 @@ void ScriptNode::clear(){ this->getEngine().getShell()->clear(); } +float ScriptNode::getDeltaTime(){ + return this->getEngine().getDeltaTime()/1000.; +} diff --git a/src/sparrowshell/scriptnode.h b/src/sparrowshell/scriptnode.h index 8a340cd..b8723a5 100644 --- a/src/sparrowshell/scriptnode.h +++ b/src/sparrowshell/scriptnode.h @@ -8,16 +8,19 @@ class ScriptNode : public GraphicalNode { sol::state m_script; + std::vector functions_name; public: ScriptNode(); void update(); void execute(std::string); + std::vector possibleCompletion(std::string); /* -- LUA function -- */ void print(std::string); void version(); void clear(); + float getDeltaTime(); }; diff --git a/src/sparrowshell/sparrowshell.cpp b/src/sparrowshell/sparrowshell.cpp index c150ce2..91976d7 100644 --- a/src/sparrowshell/sparrowshell.cpp +++ b/src/sparrowshell/sparrowshell.cpp @@ -47,6 +47,7 @@ SparrowShell::SparrowShell(sf::Window* window): m_input_node = new TextInputNode(glm::vec2(m_buffer->getFontSize(),size.y)); m_input_node->setPosition(glm::vec2(0,m_buffer->getFontSize()*BUFFER_DISPLAYED_LINES)); m_input_node->setCallBack(new InputCallBack(this,m_input_node)); + m_input_node->setTabCallBack(new AutocompletionCallBack(this,m_input_node)); this->addChild(m_background); this->addChild(m_buffer); @@ -61,6 +62,21 @@ SparrowShell::InputCallBack::InputCallBack(SparrowShell* shell,TextInputNode* te { } +SparrowShell::AutocompletionCallBack::AutocompletionCallBack(SparrowShell* shell,TextInputNode* textinput): + m_shell(shell), + m_text_input_node(textinput) +{ +} + +void SparrowShell::AutocompletionCallBack::exec(){ + std::string text = m_text_input_node->getText(); + std::vector result = m_shell->m_script->possibleCompletion(text); + std::string output = ""; + for(std::string& fun : result) + output+=fun+" "; + m_shell->out(output,glm::vec3(1,1,0)); +} + void SparrowShell::InputCallBack::exec(){ std::string text = m_text_input_node->getText(); m_shell->out(text); @@ -76,16 +92,18 @@ void SparrowShell::out(std::string str) void SparrowShell::out(std::string str,glm::vec3 color) { - Font *shellfont = RESOURCE_GET(Font,"shellfont"); - TextNode* tnode = shellfont->getTextNode(str,color,m_buffer->getFontSize(),false); - std::string name = "shellTextLine"; - name += m_buffer->size(); - tnode->getGeometryNode()->mesh->setName(name); - tnode->setDepth(SHELL_DEPTH+1); - m_buffer->push(tnode); - scrollDown(); - if (m_buffer->size() > SparrowShell::BUFFER_DISPLAYED_LINES) - m_scrollbar->setSize(m_buffer->size()); + if(!str.empty()){ + Font *shellfont = RESOURCE_GET(Font,"shellfont"); + TextNode* tnode = shellfont->getTextNode(str,color,m_buffer->getFontSize(),false); + std::string name = "shellTextLine"; + name += m_buffer->size(); + tnode->getGeometryNode()->mesh->setName(name); + tnode->setDepth(SHELL_DEPTH+1); + m_buffer->push(tnode); + scrollDown(); + if (m_buffer->size() > SparrowShell::BUFFER_DISPLAYED_LINES) + m_scrollbar->setSize(m_buffer->size()); + } } void SparrowShell::scrollUp() diff --git a/src/sparrowshell/sparrowshell.h b/src/sparrowshell/sparrowshell.h index 5fa0bb0..aab45c2 100644 --- a/src/sparrowshell/sparrowshell.h +++ b/src/sparrowshell/sparrowshell.h @@ -37,6 +37,14 @@ private: virtual void exec(); }; + class AutocompletionCallBack : public CallBack{ + SparrowShell* m_shell; + TextInputNode* m_text_input_node; + public: + AutocompletionCallBack(SparrowShell* shell,TextInputNode* textinput); + virtual void exec(); + }; + glm::vec2 m_dimension; bool m_shellEnabled = false;