added clear functon in shell

This commit is contained in:
Lendemor 2017-02-21 14:19:03 +01:00
parent d155a4f603
commit 9fe90a769c
13 changed files with 74 additions and 82 deletions

View File

@ -8,7 +8,7 @@ class DefaultKeysMap : public IKeysMap
public:
enum{MAIN_ACTION, SECONDARY_ACTION, TERTIARY_ACTION, MOVE_FORWARD, MOVE_BACKWARD, STRAFE_LEFT, STRAFE_RIGHT, JUMP,
TOGGLE_NOCLIP, TOGGLE_PHYSICS_DEBUG, TOGGLE_CONSOLE,
MOVE_CURSOR_LEFT, MOVE_CURSOR_RIGHT, PLOP_TEST, CLEAR_CONSOLE, LEFT_CLICK,
MOVE_CURSOR_LEFT, MOVE_CURSOR_RIGHT, PLOP_TEST, LEFT_CLICK,
EXIT_GAME,LAST_DEFAULT_ACTION};
DefaultKeysMap(){
@ -25,8 +25,7 @@ public:
keys.push_back( {{TOGGLE_CONSOLE,input::KEYBOARD}, sf::Keyboard::F3, IKeysMap::PRESSED} );
keys.push_back( {{MOVE_CURSOR_LEFT,input::KEYBOARD}, sf::Keyboard::Left, IKeysMap::PRESSED} );
keys.push_back( {{MOVE_CURSOR_RIGHT,input::KEYBOARD}, sf::Keyboard::Right, IKeysMap::PRESSED} );
keys.push_back( {{PLOP_TEST,input::KEYBOARD}, sf::Keyboard::F7, IKeysMap::PRESSED} );
keys.push_back( {{CLEAR_CONSOLE,input::KEYBOARD}, sf::Keyboard::F2, IKeysMap::PRESSED} );
// keys.push_back( {{PLOP_TEST,input::KEYBOARD}, sf::Keyboard::F7, IKeysMap::PRESSED} );
keys.push_back( {{EXIT_GAME,input::KEYBOARD}, sf::Keyboard::Escape,IKeysMap::PRESSED} );
keys.push_back( {{LEFT_CLICK,input::MOUSE}, sf::Mouse::Left, IKeysMap::PRESSED} );
}
@ -54,9 +53,7 @@ public:
return {
{TOGGLE_CONSOLE,input::KEYBOARD},
{MOVE_CURSOR_LEFT,input::KEYBOARD},
{MOVE_CURSOR_RIGHT,input::KEYBOARD},
{PLOP_TEST,input::KEYBOARD},
{CLEAR_CONSOLE,input::KEYBOARD}
{MOVE_CURSOR_RIGHT,input::KEYBOARD}
};
}

View File

@ -47,6 +47,8 @@ void GibGeneratorNode::update()
else
++it;
}
// use ContainerNode::update() here instead?
for(SceneNode* child : m_children)
child->update();
}

View File

@ -42,15 +42,19 @@ void ScrollBarNode::update()
bool needTransformUpdate = false;
if(m_bar_resized)
{
if(m_total_size > m_bar_size){
if(m_total_size > m_bar_size)
m_bar_dimension.y = m_dimension.y * ((float)m_bar_size / (float)m_total_size);
m_bar_resized = false;
needTransformUpdate = true;
}
else
m_bar_dimension.y = m_dimension.y;
m_bar_resized = false;
needTransformUpdate = true;
}
if(m_bar_moved)
{
m_bar_position = glm::vec2(0,(m_dimension.y /m_total_size) * m_index_position);
if(m_total_size != 0)
m_bar_position = glm::vec2(0,(m_dimension.y /m_total_size) * m_index_position);
else
m_bar_position = glm::vec2(0);
m_bar_moved = false;
needTransformUpdate = true;
}

View File

@ -14,7 +14,6 @@ protected:
int m_bar_size;
int m_total_size;
glm::vec2 m_bar_dimension;
glm::vec2 m_last_bar_dimension;
glm::vec3 m_bar_color;
bool m_bar_color_updated;
MeshNode* m_bar;

View File

@ -21,6 +21,7 @@ public:
TextNode(Mesh* mesh,std::string s,float fontSize,bool visible = true) : MeshNode(mesh,visible),m_fontSize(fontSize) {
m_string.assign(s.begin(),s.end());
}
~TextNode(){delete m_geometry.mesh;}
void setDimension(glm::vec2 dim){m_dimension = dim;}
glm::vec2 getDimension(){return m_dimension;}

View File

@ -14,6 +14,7 @@ ScriptNode::ScriptNode()
{
LUA_SET_FUN(print);
LUA_SET_FUN(version);
LUA_SET_FUN(clear);
}
void ScriptNode::update(){
@ -22,6 +23,14 @@ void ScriptNode::update(){
test=false;
}
void ScriptNode::execute(std::string to_execute){
try{
m_script.script(to_execute);
}catch(sol::error error_lua){
this->getEngine().getShell()->out(error_lua.what(),glm::vec3(1,0,0));
}
}
void ScriptNode::print(std::string to_print){
this->getEngine().getShell()->out(to_print.append("\n"));
}
@ -41,10 +50,7 @@ void ScriptNode::version(){
this->getEngine().getShell()->out(oss.str());
}
void ScriptNode::execute(std::string to_execute){
try{
m_script.script(to_execute);
}catch(sol::error error_lua){
this->getEngine().getShell()->out(error_lua.what(),glm::vec3(1,0,0));
}
void ScriptNode::clear(){
this->getEngine().getShell()->clear();
}

View File

@ -12,9 +12,13 @@ class ScriptNode : public GraphicalNode
public:
ScriptNode();
void update();
void execute(std::string);
/* -- LUA function -- */
void print(std::string);
void version();
void execute(std::string);
void clear();
};
#endif // SCRIPTNODE_H

View File

@ -5,18 +5,13 @@
#include "tools/utils.h"
#include <iostream>
void ShellBuffer::toggleBuffer(){
for(auto child : m_children)
child->toggleVisibility();
}
void ShellBuffer::update()
{
TextNode* tnode;
glm::vec2 text_pos(0,0);
SparrowShell* shell = dynamic_cast<SparrowShell*>(m_parent);
if(shell->isEnabled())
if(shell->isEnabled() && m_buffer_modified)
{
for(unsigned int i = 0; i< size();i++)
{
@ -28,8 +23,9 @@ void ShellBuffer::update()
text_pos.y += m_font_size;
}
else
tnode->moveTo2D(glm::vec2(-100,-100));
tnode->setVisible(false);
}
m_buffer_modified = false;
}
GUINode::update();
}
@ -49,6 +45,7 @@ void ShellBuffer::push(TextNode* tnode){
tnode->m_parent = this;
tnode->setParentTransform(m_transform);
tnode->setParentVisible(isVisible());
m_buffer_modified = true;
}
}
@ -56,3 +53,13 @@ glm::vec2 ShellBuffer::getDimension(){
return glm::vec2(getEngine().getWindow()->getSize().x - SparrowShell::SCROLLBAR_PIXEL_WIDTH,
SparrowShell::BUFFER_DISPLAYED_LINES * SparrowShell::DEFAULT_FONT_SIZE);
}
void ShellBuffer::clear(){
for(auto it = m_children.begin(); it != m_children.end();){
TextNode* child = (TextNode*) *it;
m_scene->removeFromIndex(child);
it = m_children.erase(it);
delete child;
}
m_index = 0;
}

View File

@ -11,15 +11,20 @@ private:
int m_zero_offset;
float m_font_size;
unsigned int m_index = 0;
bool m_buffer_modified;
public:
ShellBuffer(int buffer_size):m_max_size(buffer_size), m_zero_offset(0),m_font_size(16.f){
m_visible = false;
ShellBuffer(int buffer_size):
m_max_size(buffer_size),
m_zero_offset(0),
m_font_size(16.f),
m_buffer_modified(false)
{
setVisible(true);
moveTo2D(glm::vec2(0));
}
GraphicalNode* operator[](int i){return m_children[(m_zero_offset+i)%m_max_size];}
void push(TextNode*);
unsigned int size(){return m_children.size();}
void toggleBuffer();
void update();
@ -29,10 +34,10 @@ public:
glm::vec2 getDimension();
void increaseIndex(){m_index++;}
void decreaseIndex(){m_index--;}
void increaseIndex(){m_index++; m_buffer_modified= true;}
void decreaseIndex(){m_index--; m_buffer_modified= true;}
unsigned int getIndex(){return m_index;}
void clear();
};
#endif // SHELLBUFFER_H

View File

@ -18,33 +18,10 @@ ShellScrollBar::ShellScrollBar(SparrowShell *shell):
m_shell(shell)
{
setBarSize(SparrowShell::BUFFER_DISPLAYED_LINES);
// MeshNode(nullptr,false);
// m_position = glm::vec2(m_shell->getDimension().x-SparrowShell::SCROLLBAR_PIXEL_WIDTH,0);
// m_max_height = ;
// m_dimension = glm::vec2(SparrowShell::SCROLLBAR_PIXEL_WIDTH, m_max_height);
// moveTo2D(m_position);
// Mesh* mesh = new Mesh();
// mesh->addRectangle2D(glm::vec2(0),m_dimension);
// PhongMaterial* mat = new PhongMaterial();
// mat->diffuse = glm::vec3(0,0,0.5);
// mat->m_opacity = 0.8;
// mesh->setMaterial(mat);
// mesh->setDepth(SparrowShell::SHELL_DEPTH+1);
// mesh->initGL();
// m_geometry.mesh = mesh;
}
void ShellScrollBar::update()
{
if (m_shell->isBufferResized()){
setSize(m_shell->getBuffer()->size());
}
if (m_shell->indexMoved())
{
setIndex(m_shell->getIndex());
}
ScrollBarNode::update();
}

View File

@ -31,23 +31,21 @@ SparrowShell::SparrowShell(sf::Window* window):
m_buffer->setFontSize(DEFAULT_FONT_SIZE);
setPosition(glm::vec2(0));
setVisible(false);
Font* fonte_des_neiges = Loader::loadFont("../data/consolas.fnt","../data/consolas.png");
RESOURCE_ADD(fonte_des_neiges,Font,"shellfont");
//Create mesh for background
m_background = new BackGroundNode(m_dimension,glm::vec3(0.1,0.1,0.1),0.75,SHELL_DEPTH);
m_background->setVisible(false);
//Create mesh for scrollbar
m_scrollbar = new ShellScrollBar(this);
m_scrollbar->setVisible(false);
m_scrollbar->moveTo2D(glm::vec2(m_dimension.x-SCROLLBAR_PIXEL_WIDTH,0));
m_script = new ScriptNode();
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->setVisible(false);
m_input_node->setCallBack(new InputCallBack(this,m_input_node));
this->addChild(m_background);
@ -84,14 +82,14 @@ void SparrowShell::out(std::string str,glm::vec3 color)
m_buffer->push(tnode);
scrollDown();
if (m_buffer->size() > SparrowShell::BUFFER_DISPLAYED_LINES)
m_resizeBuffer = true;
m_scrollbar->setSize(m_buffer->size());
}
void SparrowShell::scrollUp()
{
if (m_buffer->getIndex() > 0){
m_buffer->decreaseIndex();
m_indexMoved = true;
m_scrollbar->setIndex(m_buffer->getIndex());
}
}
@ -99,7 +97,7 @@ void SparrowShell::scrollDown()
{
if (m_buffer->getIndex() + SparrowShell::BUFFER_DISPLAYED_LINES < m_buffer->size()){
m_buffer->increaseIndex();
m_indexMoved = true;
m_scrollbar->setIndex(m_buffer->getIndex());
}
}
@ -117,17 +115,17 @@ void SparrowShell::toggleShell()
{
if(m_shellEnabled){
getEngine().getInput()->setCurrentContext(m_previous_context);
getEngine().getWindow()->setKeyRepeatEnabled(false);
}else{
m_previous_context = getEngine().getInput()->getCurrentContext();
getEngine().getInput()->setCurrentContext("shell");
getEngine().getWindow()->setKeyRepeatEnabled(true);
}
m_shellEnabled = !m_shellEnabled;
setVisible(m_shellEnabled);
getEngine().getWindow()->setKeyRepeatEnabled(m_shellEnabled);
m_input_node->setFocus(m_shellEnabled);
for(auto child : m_children)
child->toggleVisibility();
m_buffer->toggleBuffer();
// for(auto child : m_children)
// child->toggleVisibility();
// m_buffer->toggleBuffer();
m_scene->updateShaders();
}
@ -142,7 +140,10 @@ void SparrowShell::update()
scrollUp();
}
GUINode::update();
m_resizeBuffer = false;
m_indexMoved = false;
}
void SparrowShell::clear(){
m_buffer->clear();
m_scrollbar->setIndex(m_buffer->getIndex());
m_scrollbar->setSize(m_buffer->size());
}

View File

@ -17,8 +17,6 @@
#include "SparrowInput/keybindings.h"
class Input;
//class MeshNode;
//class TextNode;
class TextInputNode;
class ShellBuffer;
class BackGroundNode;
@ -41,8 +39,6 @@ private:
glm::vec2 m_dimension;
bool m_shellEnabled = false;
bool m_resizeBuffer = false;
bool m_indexMoved = false;
BackGroundNode* m_background;
ShellBuffer* m_buffer;
@ -52,10 +48,8 @@ private:
glm::vec3 m_text_color;
TextInputNode* m_input_node;
int m_plop_test;
int m_move_cursor_left;
int m_move_cursor_right;
int m_clear_console_action;
std::string m_previous_context;
public:
@ -82,13 +76,9 @@ public:
void setMoveCursorLeftAction(int action);
void setMoveCursorRightAction(int action);
void setClearConsoleAction(int action){m_clear_console_action = action;}
//void moveCursorLeft(){m_input_cursor_pos--;}
//void moveCursorRight(){m_input_cursor_pos++;}
bool isEnabled(){return m_shellEnabled;}
bool isBufferResized(){return m_resizeBuffer;}
bool indexMoved(){return m_indexMoved;}
void clear();
};
#endif // SPARROWSHELL_H

View File

@ -321,7 +321,6 @@ int main(){
shell->setMoveCursorLeftAction(DefaultKeysMap::MOVE_CURSOR_LEFT);
shell->setMoveCursorRightAction(DefaultKeysMap::MOVE_CURSOR_RIGHT);
//shell->setPlopTest(DefaultKeysMap::PLOP_TEST);
shell->setClearConsoleAction(DefaultKeysMap::CLEAR_CONSOLE);
input->addContext(Context("shell",DefaultKeysMap::getShellContext()));
input->updateKeyBindings();