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 "scenetree.h"
#include "glm/ext.hpp"
#include <iostream>
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);

View File

@ -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);

View File

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

View File

@ -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;}
};

View File

@ -3,13 +3,11 @@
#include "scene/textnode.h"
#include "sparrowshell/sparrowshell.h"
#include "tools/utils.h"
#include <iostream>
void ShellBuffer::toggleBuffer(){
for(auto child : m_children){
MeshNode* meshchild = dynamic_cast<MeshNode*>(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<SparrowShell*>(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<SparrowShell*>(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));
// }
// }
// }
}

View File

@ -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();}

View File

@ -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 <iostream>
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);*/
}

View File

@ -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

View File

@ -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<MeshNode*>(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();
}

View File

@ -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;}

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();
@ -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;
}

View File

@ -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;

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){
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])));