From 33378ef8ded90ade7ec9d8578eb9391a44aadb17 Mon Sep 17 00:00:00 2001 From: Lendemor Date: Tue, 29 Aug 2017 19:45:07 +0200 Subject: [PATCH] added history in shell --- src/scene/gui/textinputnode.cpp | 54 ++++++++++++++++++++++++++++--- src/scene/gui/textinputnode.h | 26 +++++++++++---- src/sparrowshell/sparrowshell.cpp | 10 ++---- src/sparrowshell/sparrowshell.h | 4 +-- 4 files changed, 73 insertions(+), 21 deletions(-) diff --git a/src/scene/gui/textinputnode.cpp b/src/scene/gui/textinputnode.cpp index efe6cd5..6629855 100644 --- a/src/scene/gui/textinputnode.cpp +++ b/src/scene/gui/textinputnode.cpp @@ -19,10 +19,11 @@ TextInputNode::TextInputNode(glm::vec2 dimension): 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) + m_tab_callback(nullptr), + m_history(std::vector()), + m_history_pos(0), + m_inputActions({NO_ACTION, NO_ACTION, NO_ACTION, NO_ACTION}) { // Font *shellfont = RESOURCE_GET(Font,"shellfont"); Mesh* mesh = new Mesh(); @@ -44,18 +45,46 @@ void TextInputNode::update() auto input = getEngine().getInput(); for(auto action : input->getActions()){ - if (action.action == m_move_cursor_left){ + if (action.action == m_inputActions[MOVE_CURSOR_LEFT]){ if (m_cursor_pos > 0){ m_cursor_pos--; m_cursor_pos_updated=true; } } - else if(action.action == m_move_cursor_right){ + else if(action.action == m_inputActions[MOVE_CURSOR_RIGHT]){ if(m_cursor_pos < m_text.length()){ m_cursor_pos++; m_cursor_pos_updated=true; } } + else if(action.action == m_inputActions[HISTORY_PREVIOUS]){ + if(m_history_pos > 0) + { + m_history_pos--; + m_text = m_history[m_history_pos]; + m_cursor_pos = m_text.size(); + m_text_updated = true; + m_cursor_pos_updated = true; + } + } + else if(action.action == m_inputActions[HISTORY_NEXT]){ + if (m_history_pos < m_history.size()) + { + m_history_pos++; + if (m_history_pos == m_history.size()) + { + m_text.clear(); + m_cursor_pos = 0; + } + else + { + m_text = m_history[m_history_pos]; + m_cursor_pos = m_text.size(); + } + m_text_updated = true; + m_cursor_pos_updated = true; + } + } } for(unsigned int i = 0 ; i < text.length() ; i++){ @@ -150,3 +179,18 @@ std::string TextInputNode::getText() return m_text; } +void TextInputNode::clearText() +{ + m_history.push_back(m_text); + m_history_pos++; + m_text.clear(); +} + +void TextInputNode::setInputs(int cursor_left, int cursor_right, int history_up, int history_down) +{ + m_inputActions[TextInputAction::MOVE_CURSOR_LEFT] = cursor_left; + m_inputActions[TextInputAction::MOVE_CURSOR_RIGHT] = cursor_right; + m_inputActions[TextInputAction::HISTORY_NEXT] = history_down; + m_inputActions[TextInputAction::HISTORY_PREVIOUS] = history_up; +} + diff --git a/src/scene/gui/textinputnode.h b/src/scene/gui/textinputnode.h index a53789b..ba8b270 100644 --- a/src/scene/gui/textinputnode.h +++ b/src/scene/gui/textinputnode.h @@ -7,6 +7,15 @@ #include "scene/textnode.h" #include "scene/gui/callback.h" +/* +struct TextInputAction{ + int cursor_left; + int cursor_right; + int history_next; + int history_previous; +}; +*/ + class TextInputNode : public GUINode { glm::vec2 m_dimension; @@ -22,12 +31,16 @@ class TextInputNode : public GUINode unsigned int m_cursor_pos; bool m_cursor_pos_updated; - int m_move_cursor_left; - int m_move_cursor_right; - CallBack* m_callback; CallBack* m_tab_callback; + std::vector m_history; + unsigned int m_history_pos; + + std::vector m_inputActions; + + enum TextInputAction {MOVE_CURSOR_LEFT,MOVE_CURSOR_RIGHT,HISTORY_PREVIOUS,HISTORY_NEXT}; + public: TextInputNode(glm::vec2 dimension); void update(); @@ -41,9 +54,10 @@ public: 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();} + + void setInputs(int cursor_left, int cursor_right, int history_up, int history_down); + + void clearText(); }; #endif // TEXTINPUTNODE_H diff --git a/src/sparrowshell/sparrowshell.cpp b/src/sparrowshell/sparrowshell.cpp index 593fcaa..2c52ac4 100644 --- a/src/sparrowshell/sparrowshell.cpp +++ b/src/sparrowshell/sparrowshell.cpp @@ -24,8 +24,7 @@ const float SparrowShell::DEFAULT_FONT_SIZE = 16.f; SparrowShell::SparrowShell(sf::Window* window): m_buffer(new ShellBuffer(BUFFER_MAX_LENGTH)), - m_text_color(glm::vec3(0.7,1,0.3)), - m_inputActions({NO_ACTION, NO_ACTION, NO_ACTION, NO_ACTION}) + m_text_color(glm::vec3(0.7,1,0.3)) { sf::Vector2u size = window->getSize(); m_dimension = glm::ivec2(size.x,DEFAULT_FONT_SIZE*(BUFFER_DISPLAYED_LINES+1)); @@ -124,12 +123,7 @@ void SparrowShell::scrollDown() } void SparrowShell::setInputs(int cursor_left, int cursor_right, int history_up, int history_down){ - m_inputActions[MOVE_CURSOR_LEFT] = cursor_left; - m_inputActions[MOVE_CURSOR_RIGHT] = cursor_right; - m_inputActions[HISTORY_PREVIOUS] = history_up; - m_inputActions[HISTORY_NEXT] = history_down; - m_input_node->setMoveCursorLeft(cursor_left); - m_input_node->setMoveCursorRight(cursor_right); + m_input_node->setInputs(cursor_left,cursor_right,history_up,history_down); } void SparrowShell::toggleShell() diff --git a/src/sparrowshell/sparrowshell.h b/src/sparrowshell/sparrowshell.h index 7f3eaab..cce3c9a 100644 --- a/src/sparrowshell/sparrowshell.h +++ b/src/sparrowshell/sparrowshell.h @@ -60,9 +60,9 @@ private: int m_move_cursor_right; std::string m_previous_context; - std::vector m_inputActions; +// std::vector m_inputActions; - enum ShellAction {MOVE_CURSOR_LEFT=0,MOVE_CURSOR_RIGHT,HISTORY_PREVIOUS,HISTORY_NEXT}; +// enum ShellAction {MOVE_CURSOR_LEFT,MOVE_CURSOR_RIGHT,HISTORY_PREVIOUS,HISTORY_NEXT}; public: static const unsigned int BUFFER_MAX_LENGTH;