Toggle for shell working

This commit is contained in:
Lendemor 2016-12-13 14:45:11 +01:00
parent 2a32596873
commit f09b8438a9
8 changed files with 72 additions and 51 deletions

View File

@ -50,7 +50,7 @@ void Engine::createWindow(std::string title,
m_window->setFramerateLimit(60); m_window->setFramerateLimit(60);
m_input = new Input(m_window); m_input = new Input(m_window);
m_renderer->initGL(w, h); m_renderer->initGL(w, h);
m_sparrowshell = new SparrowShell(m_window, m_input); m_sparrowshell = new SparrowShell(m_window);
} }
void Engine::initPhysics() void Engine::initPhysics()

View File

@ -12,6 +12,7 @@ void ShellBuffer::toggleBuffer(){
void ShellBuffer::update() void ShellBuffer::update()
{ {
GraphicalContainerNode::update();
TextNode* tnode; TextNode* tnode;
glm::vec2 text_pos(0,0); glm::vec2 text_pos(0,0);
@ -21,11 +22,11 @@ void ShellBuffer::update()
for(unsigned int i = 0; i< size();i++) for(unsigned int i = 0; i< size();i++)
{ {
tnode = (TextNode*) (*this)[i]; tnode = (TextNode*) (*this)[i];
if (i >= m_index && i < m_index+SparrowShell::BUFFER_DISPLAYED_NUMBER) if (i >= m_index && i < m_index+SparrowShell::BUFFER_DISPLAYED_LINES)
{ {
tnode->moveTo2D(text_pos); tnode->moveTo2D(text_pos);
tnode->setVisible(true);
text_pos.y += m_font_size; text_pos.y += m_font_size;
// std::cout << tnode->getString() << ": " << text_pos.x << " " << text_pos.y << std::endl;
} }
else else
tnode->moveTo2D(glm::vec2(-100,-100)); tnode->moveTo2D(glm::vec2(-100,-100));
@ -40,6 +41,7 @@ void ShellBuffer::push(TextNode* tnode){
tnode->setSceneTree(m_scene); tnode->setSceneTree(m_scene);
if (m_children.size() >= m_max_size){ if (m_children.size() >= m_max_size){
(m_children[m_zero_offset])->setVisible(false);
m_children[m_zero_offset++] = tnode; m_children[m_zero_offset++] = tnode;
m_zero_offset %= m_max_size; m_zero_offset %= m_max_size;
}else }else

View File

@ -15,7 +15,7 @@ private:
public: public:
ShellBuffer(int buffer_size):m_max_size(buffer_size), m_zero_offset(0),m_font_size(16.f){ ShellBuffer(int buffer_size):m_max_size(buffer_size), m_zero_offset(0),m_font_size(16.f){
m_visible = false; m_visible = false;
moveTo2D(glm::vec2(0,0)); moveTo2D(glm::vec2(0));
} }
GraphicalNode* operator[](int i){return m_children[(m_zero_offset+i)%m_max_size];} GraphicalNode* operator[](int i){return m_children[(m_zero_offset+i)%m_max_size];}
void push(TextNode*); void push(TextNode*);
@ -27,6 +27,11 @@ public:
// if this font_size is scaling down, sizes which are power of 2 render better. // if this font_size is scaling down, sizes which are power of 2 render better.
void setFontSize(float font_size){m_font_size = font_size;} void setFontSize(float font_size){m_font_size = font_size;}
float getFontSize(){return m_font_size;} float getFontSize(){return m_font_size;}
void increaseIndex(){m_index++;}
void decreaseIndex(){m_index--;}
unsigned int getIndex(){return m_index;}
}; };
#endif // SHELLBUFFER_H #endif // SHELLBUFFER_H

View File

@ -6,18 +6,32 @@
#include "tools/utils.h" #include "tools/utils.h"
#include <iostream> #include <iostream>
ShellScrollBar::ShellScrollBar(Mesh *mesh, SparrowShell *shell, glm::ivec2 dim):
MeshNode(mesh,false), m_shell(shell),m_dimension(dim)
{
m_position = glm::vec2(m_shell->getDimension().x-SparrowShell::SCROLLBAR_PIXEL_WIDTH,0);
moveTo2D(m_position);
}
void ShellScrollBar::update(){ void ShellScrollBar::update(){
MeshNode::update();
m_position.y = m_shell->getPosition().y; m_position.y = m_shell->getPosition().y;
m_dimension.y = m_shell->getDimension().y; m_dimension.y = m_shell->getDimension().y;
int size = m_shell->getBuffer()->size();
float cran = ((float)m_shell->getDimension().y/(float)size);
int indexCursor = size - (m_shell->getIndex()+SparrowShell::BUFFER_DISPLAYED_LINES);
float cran = ((float)m_shell->getDimension().y/(float)m_shell->getBuffer()->size()); if (m_shell->isBufferResized()){
int indexCursor = m_shell->getBuffer()->size()-(m_shell->getIndex()+SparrowShell::BUFFER_DISPLAYED_NUMBER); std::cout << size << std::endl;
glm::ivec2 new_pos((int)m_position.x, (int) cran * indexCursor); std::cout << cran * SparrowShell::BUFFER_DISPLAYED_LINES << std::endl;
glm::ivec2 new_dim(m_dimension.x,(int)(cran * SparrowShell::BUFFER_DISPLAYED_LINES));
/* if (m_shell->isBufferResized()){ resize2D(m_dimension,new_dim);
glm::ivec2 new_dim(m_dimension.x,(int)(cran * SparrowShell::BUFFER_DISPLAYED_NUMBER));
utils::resize2D(m_mesh,m_dimension,new_dim);
} }
if (m_shell->isBufferResized() || m_shell->indexMoved()) if (m_shell->isBufferResized() || m_shell->indexMoved())
utils::setPosition2D(m_mesh,new_pos);*/ {
// std::cout << m_position.x << " " << cran*indexCursor << std::endl;
glm::ivec2 new_pos((int)m_position.x, (int) cran * indexCursor);
moveTo2D(new_pos);
}
} }

View File

@ -12,8 +12,7 @@ class ShellScrollBar : public MeshNode
glm::ivec2 m_position; glm::ivec2 m_position;
glm::ivec2 m_dimension; glm::ivec2 m_dimension;
public: public:
ShellScrollBar(Mesh* mesh, SparrowShell* shell, glm::ivec2 pos, glm::ivec2 dim): ShellScrollBar(Mesh* mesh, SparrowShell* shell, glm::ivec2 dim);
MeshNode(mesh,false), m_shell(shell),m_position(pos),m_dimension(dim){}
void update(); void update();
}; };
#endif // SHELLSCROLLBAR_H #endif // SHELLSCROLLBAR_H

View File

@ -13,19 +13,18 @@
#include "tools/loader.h" #include "tools/loader.h"
const unsigned int SparrowShell::BUFFER_MAX_LENGTH = 50; const unsigned int SparrowShell::BUFFER_MAX_LENGTH = 50;
const unsigned int SparrowShell::BUFFER_DISPLAYED_NUMBER = 10; const unsigned int SparrowShell::BUFFER_DISPLAYED_LINES = 15;
const unsigned int SparrowShell::SCROLLBAR_PIXEL_WIDTH = 10; const unsigned int SparrowShell::SCROLLBAR_PIXEL_WIDTH = 10;
const float SparrowShell::SHELL_DEPTH = 10; const float SparrowShell::SHELL_DEPTH = 10;
const float SparrowShell::DEFAULT_FONT_SIZE = 16.f;
SparrowShell::SparrowShell(sf::Window* window, Input* input): SparrowShell::SparrowShell(sf::Window* window):
m_window(window), m_position(glm::ivec2(0)),
m_input(input),
m_position(glm::ivec2(10,0)),
m_buffer(new ShellBuffer(BUFFER_MAX_LENGTH)) m_buffer(new ShellBuffer(BUFFER_MAX_LENGTH))
{ {
sf::Vector2u size = m_window->getSize(); sf::Vector2u size = window->getSize();
m_dimension = glm::ivec2(size.x,size.y/2); m_dimension = glm::ivec2(size.x,DEFAULT_FONT_SIZE*(BUFFER_DISPLAYED_LINES+1));
m_buffer->setFontSize(16.f); m_buffer->setFontSize(DEFAULT_FONT_SIZE);
moveTo2D(glm::vec2(m_position)); moveTo2D(glm::vec2(m_position));
@ -34,7 +33,7 @@ SparrowShell::SparrowShell(sf::Window* window, Input* input):
//Create mesh for background //Create mesh for background
Mesh* mesh = new Mesh(); Mesh* mesh = new Mesh();
mesh->addRectangle2D(m_position,m_dimension); mesh->addRectangle2D(glm::vec2(0),m_dimension);
PhongMaterial *mat = new PhongMaterial(); PhongMaterial *mat = new PhongMaterial();
mat->diffuse = glm::vec3(0.1,0.1,0.1); mat->diffuse = glm::vec3(0.1,0.1,0.1);
mat->m_opacity = 0.5; mat->m_opacity = 0.5;
@ -46,8 +45,8 @@ SparrowShell::SparrowShell(sf::Window* window, Input* input):
//Create mesh for scrollbar //Create mesh for scrollbar
mesh = new Mesh(); mesh = new Mesh();
glm::vec2 shell_pos = glm::vec2(m_dimension.x - SCROLLBAR_PIXEL_WIDTH,0); glm::vec2 shell_pos = glm::vec2(0,0);
glm::vec2 shell_dim = glm::vec2(SCROLLBAR_PIXEL_WIDTH,m_dimension.y); glm::vec2 shell_dim = glm::vec2(SCROLLBAR_PIXEL_WIDTH,DEFAULT_FONT_SIZE*BUFFER_DISPLAYED_LINES);
mesh->addRectangle2D(shell_pos,shell_dim); mesh->addRectangle2D(shell_pos,shell_dim);
mat = new PhongMaterial(); mat = new PhongMaterial();
mat->diffuse = glm::vec3(0,0,0.5); mat->diffuse = glm::vec3(0,0,0.5);
@ -55,7 +54,7 @@ SparrowShell::SparrowShell(sf::Window* window, Input* input):
mesh->setMaterial(mat); mesh->setMaterial(mat);
mesh->setDepth(SHELL_DEPTH+1); mesh->setDepth(SHELL_DEPTH+1);
mesh->initGL(); mesh->initGL();
m_scrollbar = new ShellScrollBar(mesh,this,shell_pos,shell_dim); m_scrollbar = new ShellScrollBar(mesh,this,shell_dim);
this->addChild(m_background); this->addChild(m_background);
this->addChild(m_buffer); this->addChild(m_buffer);
@ -68,20 +67,20 @@ void SparrowShell::out(std::string s)
TextNode* tnode = shellfont->getTextNode(s,glm::vec3(0.7,1,0.3),m_buffer->getFontSize(),false); TextNode* tnode = shellfont->getTextNode(s,glm::vec3(0.7,1,0.3),m_buffer->getFontSize(),false);
tnode->setDepth(SHELL_DEPTH+1); tnode->setDepth(SHELL_DEPTH+1);
m_buffer->push(tnode); m_buffer->push(tnode);
if (m_buffer->size() > SparrowShell::BUFFER_DISPLAYED_NUMBER) if (m_buffer->size() > SparrowShell::BUFFER_DISPLAYED_LINES)
m_resizeBuffer = true; m_resizeBuffer = true;
} }
void SparrowShell::scrollUp(){ void SparrowShell::scrollUp(){
if (m_index + SparrowShell::BUFFER_DISPLAYED_NUMBER < m_buffer->size()){ if (m_buffer->getIndex() + SparrowShell::BUFFER_DISPLAYED_LINES < m_buffer->size()){
m_index++; m_buffer->increaseIndex();
m_indexMoved = true; m_indexMoved = true;
} }
} }
void SparrowShell::scrollDown(){ void SparrowShell::scrollDown(){
if (m_index > 0){ if (m_buffer->getIndex() > 0){
m_index--; m_buffer->decreaseIndex();
m_indexMoved = true; m_indexMoved = true;
} }
} }
@ -95,10 +94,20 @@ void SparrowShell::toggleShell(){
} }
void SparrowShell::update(){ void SparrowShell::update(){
GraphicalContainerNode::update(); m_resizeBuffer = false;
for(auto action : m_input->getActions()){ m_indexMoved = false;
auto input = getEngine().getInput();
for(auto action : input->getActions()){
if(action == 15){ if(action == 15){
toggleShell(); toggleShell();
}else if(action == 6){
out("Plop");
} }
} }
int scroll = input->getDeltaVerticalScroll();
if(scroll > 0)
scrollDown();
else if(scroll < 0)
scrollUp();
GraphicalContainerNode::update();
} }

View File

@ -22,27 +22,24 @@ class Window;
class SparrowShell : public GraphicalContainerNode class SparrowShell : public GraphicalContainerNode
{ {
private: private:
sf::Window* m_window;
Input* m_input;
glm::ivec2 m_position; glm::ivec2 m_position;
glm::ivec2 m_dimension; glm::ivec2 m_dimension;
ShellBuffer* m_buffer; bool m_shellEnabled = false;
unsigned int m_index = 0;
bool m_resizeBuffer = false; bool m_resizeBuffer = false;
bool m_indexMoved = false; bool m_indexMoved = false;
bool m_shellEnabled = false;
//textMesh
MeshNode* m_background; MeshNode* m_background;
ShellBuffer* m_buffer;
ShellScrollBar* m_scrollbar; ShellScrollBar* m_scrollbar;
public: public:
static const unsigned int BUFFER_MAX_LENGTH; static const unsigned int BUFFER_MAX_LENGTH;
static const unsigned int BUFFER_DISPLAYED_NUMBER; static const unsigned int BUFFER_DISPLAYED_LINES;
static const unsigned int SCROLLBAR_PIXEL_WIDTH; static const unsigned int SCROLLBAR_PIXEL_WIDTH;
static const float SHELL_DEPTH; static const float SHELL_DEPTH;
static const float DEFAULT_FONT_SIZE;
SparrowShell(sf::Window*, Input*); SparrowShell(sf::Window*);
void update(); void update();
@ -52,15 +49,10 @@ public:
void toggleShell(); void toggleShell();
void out(std::string); void out(std::string);
// void setSceneTree(SceneTree* tree)
// {
// GraphicalContainerNode::setSceneTree(tree);
// }
glm::ivec2 getPosition(){return m_position;} glm::ivec2 getPosition(){return m_position;}
glm::ivec2 getDimension(){return m_dimension;} glm::ivec2 getDimension(){return m_dimension;}
unsigned int getIndex(){return m_index;} unsigned int getIndex(){return m_buffer->getIndex();}
ShellBuffer* getBuffer(){return m_buffer;} ShellBuffer* getBuffer(){return m_buffer;}
bool isEnabled(){return m_shellEnabled;} bool isEnabled(){return m_shellEnabled;}