toggle for background and scrollbar working

This commit is contained in:
Lendemor 2016-12-12 22:55:45 +01:00
parent e42e1f3543
commit 5985875232
13 changed files with 71 additions and 81 deletions

View File

@ -1,6 +1,7 @@
#include "graphicalnode.h" #include "graphicalnode.h"
#include "scenetree.h" #include "scenetree.h"
#include "glm/ext.hpp" #include "glm/ext.hpp"
#include <iostream>
void GraphicalNode::setSceneTree(SceneTree *tree){ void GraphicalNode::setSceneTree(SceneTree *tree){
SceneNode::setSceneTree(tree); SceneNode::setSceneTree(tree);
@ -9,6 +10,10 @@ void GraphicalNode::setSceneTree(SceneTree *tree){
} }
void GraphicalNode::toggleVisibility(){ void GraphicalNode::toggleVisibility(){
if(!m_scene){
std::cout << "Warning: trying to toggle a node before m_scene was initialized"<< std::endl;
return;
}
m_visible = !m_visible; m_visible = !m_visible;
if(m_visible){ if(m_visible){
m_scene->addToIndex(this); m_scene->addToIndex(this);

View File

@ -25,7 +25,6 @@ public:
GraphicalNode(bool visible = true) : m_visible(visible), m_transformChanged(true) {} GraphicalNode(bool visible = true) : m_visible(visible), m_transformChanged(true) {}
bool isVisible(){return m_visible;} bool isVisible(){return m_visible;}
void toggleVisibility(); void toggleVisibility();
virtual void setSceneTree(SceneTree* tree); virtual void setSceneTree(SceneTree* tree);

View File

@ -1,6 +1,3 @@
#include "textnode.h" #include "textnode.h"
/*TextNode::TextNode()
{
}*/

View File

@ -9,7 +9,7 @@ private:
std::string m_string; std::string m_string;
float m_fontSize; float m_fontSize;
public: public:
TextNode(Mesh* mesh,std::string s,float fontSize) : MeshNode(mesh),m_string(s),m_fontSize(fontSize) {} TextNode(Mesh* mesh,std::string s,float fontSize,bool visible = true) : MeshNode(mesh,visible),m_string(s),m_fontSize(fontSize) {}
float getFontSize(){return m_fontSize;} float getFontSize(){return m_fontSize;}
std::string getString(){return m_string;} std::string getString(){return m_string;}
}; };

View File

@ -3,13 +3,11 @@
#include "scene/textnode.h" #include "scene/textnode.h"
#include "sparrowshell/sparrowshell.h" #include "sparrowshell/sparrowshell.h"
#include "tools/utils.h" #include "tools/utils.h"
#include <iostream>
void ShellBuffer::toggleBuffer(){ void ShellBuffer::toggleBuffer(){
for(auto child : m_children){ for(auto child : m_children)
MeshNode* meshchild = dynamic_cast<MeshNode*>(child); child->toggleVisibility();
if(meshchild)
meshchild->toggleVisibility();
}
} }
void ShellBuffer::update() void ShellBuffer::update()
@ -17,24 +15,25 @@ void ShellBuffer::update()
TextNode* tnode; TextNode* tnode;
glm::vec2 text_pos(0); glm::vec2 text_pos(0);
SparrowShell* shell = dynamic_cast<SparrowShell*>(m_parent); // std::cout << m_children.size() << std::endl;
// if (m_parent){
// SparrowShell* shell = dynamic_cast<SparrowShell*>(m_parent);
// if(shell && shell->isEnabled()) // if(shell && shell->isEnabled())
// { // {
// for(unsigned int i = 0; i< size();i++) // for(unsigned int i = 0; i< size();i++)
// { // {
// tnode = (TextNode*) this+i; // tnode = (TextNode*) (*this)[i];
// std::cout << tnode->getString() << std::endl;
// if (i >= m_index && i < m_index+SparrowShell::BUFFER_DISPLAYED_NUMBER) // if (i >= m_index && i < m_index+SparrowShell::BUFFER_DISPLAYED_NUMBER)
// { // {
// utils::setPosition2D(tnode,text_pos); // tnode->moveTo(glm::vec3(text_pos,0));
// text_pos.y += m_font_size; // text_pos.y += m_font_size;
// //m_scene
// } // }
// else{ // else
// utils::setPosition2D(tnode,glm::vec2(-100,-100)); // tnode->moveTo(glm::vec3(-100,-100,0));
// } // }
// } // }
// } // }
} }

View File

@ -12,7 +12,7 @@ private:
float m_font_size; float m_font_size;
unsigned int m_index = 0; unsigned int m_index = 0;
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;}
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*);
unsigned int size(){return m_children.size();} unsigned int size(){return m_children.size();}

View File

@ -4,30 +4,7 @@
#include "phongmaterial.h" #include "phongmaterial.h"
#include "scene/meshnode.h" #include "scene/meshnode.h"
#include "tools/utils.h" #include "tools/utils.h"
#include <iostream>
ShellScrollBar::ShellScrollBar()
{
}
ShellScrollBar::ShellScrollBar(SparrowShell* shell):m_shell(shell){
m_position = glm::ivec2(m_shell->getDimension().x - SparrowShell::SCROLLBAR_PIXEL_WIDTH,0);
m_dimension = glm::ivec2(SparrowShell::SCROLLBAR_PIXEL_WIDTH,m_shell->getDimension().y);
Mesh* mesh = new Mesh();
mesh->addRectangle2D(glm::vec2(0),m_dimension);
PhongMaterial *mat = new PhongMaterial();
mat->diffuse = glm::vec3(0,0,0.5);
mesh->setDepth(SparrowShell::SHELL_DEPTH+1);
mesh->setMaterial(mat);
mesh->initGL();
m_mesh = new MeshNode(mesh,false);
utils::setPosition2D(m_mesh,m_position);
m_shell->addChild(m_mesh);
}
void ShellScrollBar::update(){ void ShellScrollBar::update(){
m_position.y = m_shell->getPosition().y; m_position.y = m_shell->getPosition().y;
@ -37,10 +14,10 @@ void ShellScrollBar::update(){
int indexCursor = m_shell->getBuffer()->size()-(m_shell->getIndex()+SparrowShell::BUFFER_DISPLAYED_NUMBER); int indexCursor = m_shell->getBuffer()->size()-(m_shell->getIndex()+SparrowShell::BUFFER_DISPLAYED_NUMBER);
glm::ivec2 new_pos((int)m_position.x, (int) cran * indexCursor); glm::ivec2 new_pos((int)m_position.x, (int) cran * indexCursor);
if (m_shell->isBufferResized()){ /* if (m_shell->isBufferResized()){
glm::ivec2 new_dim(m_dimension.x,(int)(cran * SparrowShell::BUFFER_DISPLAYED_NUMBER)); glm::ivec2 new_dim(m_dimension.x,(int)(cran * SparrowShell::BUFFER_DISPLAYED_NUMBER));
utils::resize2D(m_mesh,m_dimension,new_dim); 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); utils::setPosition2D(m_mesh,new_pos);*/
} }

View File

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

View File

@ -23,8 +23,11 @@ SparrowShell::SparrowShell(sf::Window* window, Input* input):
{ {
sf::Vector2u size = m_window->getSize(); sf::Vector2u size = m_window->getSize();
m_dimension = glm::ivec2(size.x,size.y/2); m_dimension = glm::ivec2(size.x,size.y/2);
Mesh* mesh = new Mesh();
m_buffer->setFontSize(16.f); m_buffer->setFontSize(16.f);
//Create mesh for background
Mesh* mesh = new Mesh();
mesh->addRectangle2D(m_position,m_dimension); mesh->addRectangle2D(m_position,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);
@ -33,21 +36,36 @@ SparrowShell::SparrowShell(sf::Window* window, Input* input):
mesh->setDepth(SHELL_DEPTH); mesh->setDepth(SHELL_DEPTH);
mesh->initGL(); mesh->initGL();
m_background = new MeshNode(mesh,false); m_background = new MeshNode(mesh,false);
//Create mesh for scrollbar
mesh = new Mesh();
glm::vec2 shell_pos = glm::vec2(m_dimension.x - SCROLLBAR_PIXEL_WIDTH,0);
glm::vec2 shell_dim = glm::vec2(SCROLLBAR_PIXEL_WIDTH,m_dimension.y);
mesh->addRectangle2D(shell_pos,shell_dim);
mat = new PhongMaterial();
mat->diffuse = glm::vec3(0,0,0.5);
mat->m_opacity = 0.8;
mesh->setMaterial(mat);
mesh->setDepth(SHELL_DEPTH+1);
mesh->initGL();
m_scrollbar = new ShellScrollBar(mesh,this,shell_pos,shell_dim);
this->addChild(m_background); this->addChild(m_background);
this->addChild(m_buffer); this->addChild(m_buffer);
m_scrollbar = ShellScrollBar(this); this->addChild(m_scrollbar);
} }
void SparrowShell::out(std::string s) void SparrowShell::out(std::string s)
{ {
Font *shellfont = RESOURCE_GET(Font,"shellfont"); Font *shellfont = RESOURCE_GET(Font,"shellfont");
TextNode* tnode = shellfont->getTextNode(s,glm::vec3(0.7,1,0.3),m_buffer->getFontSize()); 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_shellEnabled) // if(m_shellEnabled)
m_scene->addToIndex(tnode); // m_scene->addToIndex(tnode);
if (m_buffer->size() > SparrowShell::BUFFER_DISPLAYED_NUMBER) // if (m_buffer->size() > SparrowShell::BUFFER_DISPLAYED_NUMBER)
m_resizeBuffer = true; // m_resizeBuffer = true;
} }
void SparrowShell::scrollUp(){ void SparrowShell::scrollUp(){
@ -66,21 +84,17 @@ void SparrowShell::scrollDown(){
void SparrowShell::toggleShell(){ void SparrowShell::toggleShell(){
m_shellEnabled = !m_shellEnabled; m_shellEnabled = !m_shellEnabled;
// for(auto child : m_children){ for(auto child : m_children)
// MeshNode* meshchild = dynamic_cast<MeshNode*>(child); child->toggleVisibility();
// // if(meshchild) m_buffer->toggleBuffer();
// // meshchild->toggleVisibility(); m_scene->updateShaders();
// }
//// m_buffer->toggleBuffer();
//// m_background->toggleVisibility();
} }
void SparrowShell::update(){ void SparrowShell::update(){
GraphicalContainerNode::update();
for(auto action : m_input->getActions()){ for(auto action : m_input->getActions()){
if(action == 15){ if(action == 15){
toggleShell(); toggleShell();
} }
} }
m_buffer->update();
m_scrollbar.update();
} }

View File

@ -34,7 +34,7 @@ private:
bool m_shellEnabled = false; bool m_shellEnabled = false;
//textMesh //textMesh
MeshNode* m_background; MeshNode* m_background;
ShellScrollBar m_scrollbar; ShellScrollBar* m_scrollbar;
public: public:
static const unsigned int BUFFER_MAX_LENGTH; static const unsigned int BUFFER_MAX_LENGTH;
@ -52,11 +52,10 @@ public:
void toggleShell(); void toggleShell();
void out(std::string); void out(std::string);
void setSceneTree(SceneTree* tree) // void setSceneTree(SceneTree* tree)
{ // {
GraphicalContainerNode::setSceneTree(tree); // GraphicalContainerNode::setSceneTree(tree);
// m_buffer->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;}

View File

@ -8,7 +8,7 @@ Font::Font()
} }
TextNode* Font::getTextNode(std::string s, glm::vec3 color, float font_size) TextNode* Font::getTextNode(std::string s, glm::vec3 color, float font_size,bool visible)
{ {
Mesh* textmesh = new Mesh(); Mesh* textmesh = new Mesh();
@ -38,6 +38,6 @@ TextNode* Font::getTextNode(std::string s, glm::vec3 color, float font_size)
mat->diffuse = color; mat->diffuse = color;
textmesh->setMaterial((Material*)mat); textmesh->setMaterial((Material*)mat);
textmesh->initGL(); textmesh->initGL();
TextNode *text = new TextNode(textmesh,s,font_size); TextNode *text = new TextNode(textmesh,s,font_size,visible);
return text; return text;
} }

View File

@ -31,7 +31,7 @@ public:
void setScale(glm::vec2 scale){m_scale = scale;} void setScale(glm::vec2 scale){m_scale = scale;}
void setTexture(Texture *tex){m_tex = tex;} void setTexture(Texture *tex){m_tex = tex;}
TextNode* getTextNode(std::string s, glm::vec3 color = glm::vec3(1), float font_size = 64.f); TextNode* getTextNode(std::string s, glm::vec3 color = glm::vec3(1), float font_size = 64.f, bool visible = true);
private: private:
std::string m_name; std::string m_name;
Texture *m_tex; Texture *m_tex;

View File

@ -17,6 +17,7 @@ std::vector<std::string> utils::split(const std::string &line, char sep){
void utils::setPosition2D(MeshNode *mnode, glm::vec2 pos){ void utils::setPosition2D(MeshNode *mnode, glm::vec2 pos){
const glm::mat4 &tr = mnode->getTransform(); const glm::mat4 &tr = mnode->getTransform();
//mnode->moveTo(pos)
// glm::vec3 v1(pos.x,pos.y,0); // glm::vec3 v1(pos.x,pos.y,0);
// glm::vec3 v2(tr[3]); // glm::vec3 v2(tr[3]);
mnode->setTransform(glm::translate(tr,glm::vec3(pos.x,pos.y,0) - glm::vec3(tr[3]))); mnode->setTransform(glm::translate(tr,glm::vec3(pos.x,pos.y,0) - glm::vec3(tr[3])));