add autocompletion

This commit is contained in:
HatjigeorgiouDimitri 2017-02-23 18:59:33 +01:00
parent e596ffef52
commit 30c5674667
6 changed files with 64 additions and 11 deletions

View File

@ -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;

View File

@ -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();}

View File

@ -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<std::string> ScriptNode::possibleCompletion(std::string search){
std::vector<std::string> 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.;
}

View File

@ -8,16 +8,19 @@ class ScriptNode : public GraphicalNode
{
sol::state m_script;
std::vector<std::string> functions_name;
public:
ScriptNode();
void update();
void execute(std::string);
std::vector<std::string> possibleCompletion(std::string);
/* -- LUA function -- */
void print(std::string);
void version();
void clear();
float getDeltaTime();
};

View File

@ -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<std::string> 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()

View File

@ -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;