diff --git a/src/scene/graphicalnode.cpp b/src/scene/graphicalnode.cpp index ed714ae..6f1a838 100644 --- a/src/scene/graphicalnode.cpp +++ b/src/scene/graphicalnode.cpp @@ -1,6 +1,7 @@ #include "graphicalnode.h" #include "scenetree.h" #include "glm/ext.hpp" +#include void GraphicalNode::setSceneTree(SceneTree *tree){ SceneNode::setSceneTree(tree); @@ -9,6 +10,10 @@ void GraphicalNode::setSceneTree(SceneTree *tree){ } 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; if(m_visible){ m_scene->addToIndex(this); diff --git a/src/scene/graphicalnode.h b/src/scene/graphicalnode.h index d44f002..40699e3 100644 --- a/src/scene/graphicalnode.h +++ b/src/scene/graphicalnode.h @@ -25,7 +25,6 @@ public: GraphicalNode(bool visible = true) : m_visible(visible), m_transformChanged(true) {} - bool isVisible(){return m_visible;} void toggleVisibility(); virtual void setSceneTree(SceneTree* tree); diff --git a/src/scene/textnode.cpp b/src/scene/textnode.cpp index e0fe1cb..189495e 100644 --- a/src/scene/textnode.cpp +++ b/src/scene/textnode.cpp @@ -1,6 +1,3 @@ #include "textnode.h" -/*TextNode::TextNode() -{ -}*/ diff --git a/src/scene/textnode.h b/src/scene/textnode.h index cfebb1c..f77ea64 100644 --- a/src/scene/textnode.h +++ b/src/scene/textnode.h @@ -9,7 +9,7 @@ private: std::string m_string; float m_fontSize; 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;} std::string getString(){return m_string;} }; diff --git a/src/sparrowshell/shellbuffer.cpp b/src/sparrowshell/shellbuffer.cpp index 2587d74..79974d4 100644 --- a/src/sparrowshell/shellbuffer.cpp +++ b/src/sparrowshell/shellbuffer.cpp @@ -3,13 +3,11 @@ #include "scene/textnode.h" #include "sparrowshell/sparrowshell.h" #include "tools/utils.h" +#include void ShellBuffer::toggleBuffer(){ - for(auto child : m_children){ - MeshNode* meshchild = dynamic_cast(child); - if(meshchild) - meshchild->toggleVisibility(); - } + for(auto child : m_children) + child->toggleVisibility(); } void ShellBuffer::update() @@ -17,24 +15,25 @@ void ShellBuffer::update() TextNode* tnode; glm::vec2 text_pos(0); - SparrowShell* shell = dynamic_cast(m_parent); -// if(shell && shell->isEnabled()) -// { -// for(unsigned int i = 0; i< size();i++) +// std::cout << m_children.size() << std::endl; + +// if (m_parent){ +// SparrowShell* shell = dynamic_cast(m_parent); +// if(shell && shell->isEnabled()) // { -// tnode = (TextNode*) this+i; -// if (i >= m_index && i < m_index+SparrowShell::BUFFER_DISPLAYED_NUMBER) +// for(unsigned int i = 0; i< size();i++) // { -// utils::setPosition2D(tnode,text_pos); -// text_pos.y += m_font_size; -// //m_scene +// tnode = (TextNode*) (*this)[i]; + // std::cout << tnode->getString() << std::endl; + // if (i >= m_index && i < m_index+SparrowShell::BUFFER_DISPLAYED_NUMBER) + // { + // tnode->moveTo(glm::vec3(text_pos,0)); + // text_pos.y += m_font_size; +// } +// else +// tnode->moveTo(glm::vec3(-100,-100,0)); // } -// else{ -// utils::setPosition2D(tnode,glm::vec2(-100,-100)); -// } - // } - // } } diff --git a/src/sparrowshell/shellbuffer.h b/src/sparrowshell/shellbuffer.h index 068d48e..f56d060 100644 --- a/src/sparrowshell/shellbuffer.h +++ b/src/sparrowshell/shellbuffer.h @@ -12,7 +12,7 @@ private: float m_font_size; unsigned int m_index = 0; 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];} void push(TextNode*); unsigned int size(){return m_children.size();} diff --git a/src/sparrowshell/shellscrollbar.cpp b/src/sparrowshell/shellscrollbar.cpp index dce7590..da23c0c 100644 --- a/src/sparrowshell/shellscrollbar.cpp +++ b/src/sparrowshell/shellscrollbar.cpp @@ -4,30 +4,7 @@ #include "phongmaterial.h" #include "scene/meshnode.h" #include "tools/utils.h" - - - -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); -} +#include void ShellScrollBar::update(){ 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); 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)); utils::resize2D(m_mesh,m_dimension,new_dim); } if (m_shell->isBufferResized() || m_shell->indexMoved()) - utils::setPosition2D(m_mesh,new_pos); + utils::setPosition2D(m_mesh,new_pos);*/ } diff --git a/src/sparrowshell/shellscrollbar.h b/src/sparrowshell/shellscrollbar.h index 542bdcd..d9295b4 100644 --- a/src/sparrowshell/shellscrollbar.h +++ b/src/sparrowshell/shellscrollbar.h @@ -6,15 +6,14 @@ class SparrowShell; -class ShellScrollBar// : public MeshNode +class ShellScrollBar : public MeshNode { SparrowShell* m_shell; glm::ivec2 m_position; glm::ivec2 m_dimension; - MeshNode *m_mesh; public: - ShellScrollBar(); - ShellScrollBar(SparrowShell* shell); + ShellScrollBar(Mesh* mesh, SparrowShell* shell, glm::ivec2 pos, glm::ivec2 dim): + MeshNode(mesh,false), m_shell(shell),m_position(pos),m_dimension(dim){} void update(); }; #endif // SHELLSCROLLBAR_H diff --git a/src/sparrowshell/sparrowshell.cpp b/src/sparrowshell/sparrowshell.cpp index 8cbc920..4103e68 100644 --- a/src/sparrowshell/sparrowshell.cpp +++ b/src/sparrowshell/sparrowshell.cpp @@ -22,9 +22,12 @@ SparrowShell::SparrowShell(sf::Window* window, Input* input): m_buffer(new ShellBuffer(BUFFER_MAX_LENGTH)) { sf::Vector2u size = m_window->getSize(); - m_dimension = glm::ivec2(size.x,size.y/2); - Mesh* mesh = new Mesh(); + m_dimension = glm::ivec2(size.x,size.y/2); m_buffer->setFontSize(16.f); + + + //Create mesh for background + Mesh* mesh = new Mesh(); mesh->addRectangle2D(m_position,m_dimension); PhongMaterial *mat = new PhongMaterial(); 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->initGL(); 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_buffer); - m_scrollbar = ShellScrollBar(this); + this->addChild(m_scrollbar); } void SparrowShell::out(std::string s) { 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); m_buffer->push(tnode); - if(m_shellEnabled) - m_scene->addToIndex(tnode); - if (m_buffer->size() > SparrowShell::BUFFER_DISPLAYED_NUMBER) - m_resizeBuffer = true; + // if(m_shellEnabled) + // m_scene->addToIndex(tnode); +// if (m_buffer->size() > SparrowShell::BUFFER_DISPLAYED_NUMBER) +// m_resizeBuffer = true; } void SparrowShell::scrollUp(){ @@ -66,21 +84,17 @@ void SparrowShell::scrollDown(){ void SparrowShell::toggleShell(){ m_shellEnabled = !m_shellEnabled; -// for(auto child : m_children){ -// MeshNode* meshchild = dynamic_cast(child); -// // if(meshchild) -// // meshchild->toggleVisibility(); -// } -//// m_buffer->toggleBuffer(); -//// m_background->toggleVisibility(); + for(auto child : m_children) + child->toggleVisibility(); + m_buffer->toggleBuffer(); + m_scene->updateShaders(); } void SparrowShell::update(){ + GraphicalContainerNode::update(); for(auto action : m_input->getActions()){ if(action == 15){ toggleShell(); } } - m_buffer->update(); - m_scrollbar.update(); } diff --git a/src/sparrowshell/sparrowshell.h b/src/sparrowshell/sparrowshell.h index 916b2ab..93bb372 100644 --- a/src/sparrowshell/sparrowshell.h +++ b/src/sparrowshell/sparrowshell.h @@ -34,7 +34,7 @@ private: bool m_shellEnabled = false; //textMesh MeshNode* m_background; - ShellScrollBar m_scrollbar; + ShellScrollBar* m_scrollbar; public: static const unsigned int BUFFER_MAX_LENGTH; @@ -52,11 +52,10 @@ public: void toggleShell(); void out(std::string); - void setSceneTree(SceneTree* tree) - { - GraphicalContainerNode::setSceneTree(tree); -// m_buffer->setSceneTree(tree); - } +// void setSceneTree(SceneTree* tree) +// { +// GraphicalContainerNode::setSceneTree(tree); +// } glm::ivec2 getPosition(){return m_position;} glm::ivec2 getDimension(){return m_dimension;} diff --git a/src/tools/font.cpp b/src/tools/font.cpp index cef7ce9..01cd919 100644 --- a/src/tools/font.cpp +++ b/src/tools/font.cpp @@ -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(); @@ -38,6 +38,6 @@ TextNode* Font::getTextNode(std::string s, glm::vec3 color, float font_size) mat->diffuse = color; textmesh->setMaterial((Material*)mat); textmesh->initGL(); - TextNode *text = new TextNode(textmesh,s,font_size); + TextNode *text = new TextNode(textmesh,s,font_size,visible); return text; } diff --git a/src/tools/font.h b/src/tools/font.h index a03e842..9f2b88d 100644 --- a/src/tools/font.h +++ b/src/tools/font.h @@ -31,7 +31,7 @@ public: void setScale(glm::vec2 scale){m_scale = scale;} 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: std::string m_name; Texture *m_tex; diff --git a/src/tools/utils.cpp b/src/tools/utils.cpp index 6eee4f8..aeda53a 100644 --- a/src/tools/utils.cpp +++ b/src/tools/utils.cpp @@ -17,6 +17,7 @@ std::vector utils::split(const std::string &line, char sep){ void utils::setPosition2D(MeshNode *mnode, glm::vec2 pos){ const glm::mat4 &tr = mnode->getTransform(); + //mnode->moveTo(pos) // glm::vec3 v1(pos.x,pos.y,0); // glm::vec3 v2(tr[3]); mnode->setTransform(glm::translate(tr,glm::vec3(pos.x,pos.y,0) - glm::vec3(tr[3])));