From 0daa2a2dcb39b42626a8473bf51cd2d51cbf2e37 Mon Sep 17 00:00:00 2001 From: Lendemor Date: Wed, 7 Dec 2016 16:08:51 +0100 Subject: [PATCH] temp commit for merging with index fix --- CMakeLists.txt | 4 +- src/engine.cpp | 2 +- src/scene/containernode.cpp | 1 + src/scene/meshnode.cpp | 3 +- src/sparrowshell.cpp | 158 ---------------------------- src/sparrowshell.h | 81 -------------- src/sparrowshell/shellbuffer.cpp | 19 ++++ src/sparrowshell/shellbuffer.h | 25 +++++ src/sparrowshell/shellscrollbar.cpp | 46 ++++++++ src/sparrowshell/shellscrollbar.h | 20 ++++ src/sparrowshell/sparrowshell.cpp | 99 +++++++++++++++++ src/sparrowshell/sparrowshell.h | 68 ++++++++++++ src/test/main.cpp | 4 +- 13 files changed, 284 insertions(+), 246 deletions(-) delete mode 100644 src/sparrowshell.cpp delete mode 100644 src/sparrowshell.h create mode 100644 src/sparrowshell/shellbuffer.cpp create mode 100644 src/sparrowshell/shellbuffer.h create mode 100644 src/sparrowshell/shellscrollbar.cpp create mode 100644 src/sparrowshell/shellscrollbar.h create mode 100644 src/sparrowshell/sparrowshell.cpp create mode 100644 src/sparrowshell/sparrowshell.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 1723a8d..9853e79 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,8 +7,8 @@ SET(VERSION_MINOR 1) set(EXTRA_INCLUDES ${PROJECT_SOURCE_DIR}/src) # choose source file -file(GLOB LIB_SRC_LIST src/*.cpp src/tools/*.cpp src/scene/*.cpp) -file(GLOB LIB_HEAD_LIST src/*.h src/tools/*.h src/scene/*.h) +file(GLOB LIB_SRC_LIST src/*.cpp src/tools/*.cpp src/scene/*.cpp src/sparrowshell/*.cpp) +file(GLOB LIB_HEAD_LIST src/*.h src/tools/*.h src/scene/*.h src/sparrowshell/*.h) set(EXEC_SRC_LIST src/test/main.cpp) #set compilation option diff --git a/src/engine.cpp b/src/engine.cpp index c12e99f..ce48edc 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -9,7 +9,7 @@ #include #include "resourcemanager.h" #include "scene/scenetree.h" -#include "sparrowshell.h" +#include "sparrowshell/sparrowshell.h" Engine::Engine() : m_window(NULL), diff --git a/src/scene/containernode.cpp b/src/scene/containernode.cpp index 585667b..bae9db0 100644 --- a/src/scene/containernode.cpp +++ b/src/scene/containernode.cpp @@ -11,6 +11,7 @@ void ContainerNode::addChild(SceneNode* node) { if(node != nullptr) { + node->setSceneTree(m_scene); m_children.push_back(node); node->m_parent = this; } diff --git a/src/scene/meshnode.cpp b/src/scene/meshnode.cpp index 0df15ee..1c134cb 100644 --- a/src/scene/meshnode.cpp +++ b/src/scene/meshnode.cpp @@ -8,7 +8,8 @@ void MeshNode::setDepth(float depth){ void MeshNode::setSceneTree(SceneTree *tree){ SceneNode::setSceneTree(tree); - tree->addToIndex(this); + if (tree) + tree->addToIndex(this); } void MeshNode::toggleVisibility(){ diff --git a/src/sparrowshell.cpp b/src/sparrowshell.cpp deleted file mode 100644 index 2498b8f..0000000 --- a/src/sparrowshell.cpp +++ /dev/null @@ -1,158 +0,0 @@ -#include "sparrowshell.h" - -#include "message.h" -#include "input.h" -#include "scene/scenetree.h" -#include "scene/meshnode.h" -#include "scene/textnode.h" -#include "mesh.h" -#include "phongmaterial.h" -#include "tools/utils.h" -#include "tools/font.h" - -const unsigned int SparrowShell::BUFFER_MAX_LENGTH = 50; -const unsigned int SparrowShell::BUFFER_DISPLAYED_NUMBER = 10; -const unsigned int SparrowShell::SCROLLBAR_PIXEL_WIDTH = 10; -const float SparrowShell::SHELL_DEPTH = 10; - -SparrowShell::SparrowShell(sf::Window* window, Input* input): - m_window(window), - m_input(input), - m_position(glm::ivec2(0,0)), - m_buffer(ShellBuffer(BUFFER_MAX_LENGTH)) -{ - sf::Vector2u size = m_window->getSize(); - m_dimension = glm::ivec2(size.x,size.y/2); - Mesh* mesh = new Mesh(); - m_buffer.setFontSize(16.f); - mesh->addRectangle2D(m_position,m_dimension); - PhongMaterial *mat = new PhongMaterial(); - mat->diffuse = glm::vec3(0.1,0.1,0.1); - mat->m_opacity = 0.5; - mesh->setMaterial(mat); - mesh->setDepth(SHELL_DEPTH); - mesh->initGL(); - m_background = new MeshNode(mesh); - this->addChild(m_background); - m_scrollbar = SparrowShell::ScrollBar(this); -} - -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()); - tnode->setDepth(SHELL_DEPTH+1); - // m_currentScene->addToIndex(tnode); - m_buffer.push(tnode); - if (m_buffer.size() > BUFFER_DISPLAYED_NUMBER) - m_resizeScrollBar = true; -} - -void SparrowShell::scrollUp(){ - if (m_index + BUFFER_DISPLAYED_NUMBER < m_buffer.size()){ - m_index++; - m_indexMoved = true; - } -} - -void SparrowShell::scrollDown(){ - if (m_index > 0){ - m_index--; - m_indexMoved = true; - } -} - -void SparrowShell::toggleShell(){ - m_shellEnabled = !m_shellEnabled; -// if (m_shellEnabled){ -// for(unsigned int i = 0;iaddToIndex(m_buffer[i]); -// }else{ -// for(unsigned int i = 0;iremoveFromIndex(m_buffer[i]); -// } - for(auto child : m_children){ - MeshNode* meshchild = dynamic_cast(child); - if(meshchild) - meshchild->toggleVisibility(); - } - m_buffer.toggleBuffer(); - m_background->toggleVisibility(); -} - -void SparrowShell::update(){ - TextNode* tnode; - glm::vec2 text_pos(0); - - for(auto action : m_input->getActions()){ - if(action == 15){ - toggleShell(); - } - } - //move position of shellBuffer - // position textnode inside shellBuffer - - if(m_shellEnabled){ - for(unsigned int i = 0; i= m_index && i < m_index + BUFFER_DISPLAYED_NUMBER){ - utils::setPosition2D(tnode,text_pos); - text_pos.y += m_buffer.getFontSize(); - m_scene->addToIndex(tnode); - } - } - } - m_scrollbar.update(); -} - -void SparrowShell::ShellBuffer::toggleBuffer(){ - for(auto child : m_children){ - MeshNode* meshchild = dynamic_cast(child); - if(meshchild) - meshchild->toggleVisibility(); - } -} - - -void SparrowShell::ShellBuffer::push(TextNode* s){ - if (m_children.size() >= m_max_size){ - m_children[m_zero_offset++] = s; - m_zero_offset %= m_max_size; - }else - m_children.push_back(s); -} - -SparrowShell::ScrollBar::ScrollBar(SparrowShell* shell):m_shell(shell){ - m_position = glm::ivec2(m_shell->m_dimension.x - SparrowShell::SCROLLBAR_PIXEL_WIDTH,0); - m_dimension = glm::ivec2(SparrowShell::SCROLLBAR_PIXEL_WIDTH,m_shell->m_dimension.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(SHELL_DEPTH+1); - mesh->setMaterial(mat); - mesh->initGL(); - - m_mesh = new MeshNode(mesh); - utils::setPosition2D(m_mesh,m_position); - m_shell->addChild(m_mesh); -} - -void SparrowShell::ScrollBar::update(){ - m_position.y = m_shell->m_position.y; - m_dimension.y = m_shell->m_dimension.y; - - float cran = ((float)m_shell->m_dimension.y/(float)m_shell->m_buffer.size()); - int indexCursor = m_shell->m_buffer.size()-(m_shell->m_index+BUFFER_DISPLAYED_NUMBER); - glm::ivec2 new_pos((int)m_position.x, (int) cran * indexCursor); - - if (m_shell->m_resizeScrollBar){ - glm::ivec2 new_dim(m_dimension.x,(int)(cran * BUFFER_DISPLAYED_NUMBER)); - utils::resize2D(m_mesh,m_dimension,new_dim); - m_shell->m_resizeScrollBar = false; - } - if (m_shell->m_resizeScrollBar || m_shell->m_indexMoved) - utils::setPosition2D(m_mesh,new_pos); -} diff --git a/src/sparrowshell.h b/src/sparrowshell.h deleted file mode 100644 index be32cbd..0000000 --- a/src/sparrowshell.h +++ /dev/null @@ -1,81 +0,0 @@ -#ifndef SPARROWSHELL_H -#define SPARROWSHELL_H - -#include - -#include "system.h" -#include "scene/scenetree.h" -#include "glm/glm.hpp" - -class Input; -class MeshNode; -class TextNode; - -namespace sf { -class Window; -} - -class SparrowShell : public ContainerNode -{ -private: - class ScrollBar{ - SparrowShell* m_shell; - glm::ivec2 m_position; - glm::ivec2 m_dimension; - MeshNode *m_mesh; - public: - ScrollBar(){} - ScrollBar(SparrowShell* shell); - void update(); - }; - - class ShellBuffer : public ContainerNode { - private: - int m_zero_offset = 0; - unsigned int m_max_size; - float m_font_size; - public: - ShellBuffer(int buffer_size):m_max_size(buffer_size){} - SceneNode* 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(); - - // 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;} - float getFontSize(){return m_font_size;} - }; - - static const unsigned int BUFFER_MAX_LENGTH; - static const unsigned int BUFFER_DISPLAYED_NUMBER; - static const unsigned int SCROLLBAR_PIXEL_WIDTH; - static const float SHELL_DEPTH; - - sf::Window* m_window; - Input* m_input; - - glm::ivec2 m_position; - glm::ivec2 m_dimension; - ShellBuffer m_buffer; - unsigned int m_index = 0; - bool m_resizeScrollBar = false; - bool m_indexMoved = false; - bool m_shellEnabled = false; - //textMesh - MeshNode* m_background; - ScrollBar m_scrollbar; - -public: - SparrowShell(sf::Window*, Input*); - - void update(); -// void setScene(SceneTree *scene){m_currentScene = scene;} - - void scrollUp(); - void scrollDown(); - - void toggleShell(); - void out(std::string); -}; - -#endif // SPARROWSHELL_H diff --git a/src/sparrowshell/shellbuffer.cpp b/src/sparrowshell/shellbuffer.cpp new file mode 100644 index 0000000..3690a55 --- /dev/null +++ b/src/sparrowshell/shellbuffer.cpp @@ -0,0 +1,19 @@ +#include "shellbuffer.h" +#include "scene/meshnode.h" +#include "scene/textnode.h" + +void ShellBuffer::toggleBuffer(){ + for(auto child : m_children){ + MeshNode* meshchild = dynamic_cast(child); + if(meshchild) + meshchild->toggleVisibility(); + } +} + +void ShellBuffer::push(TextNode* s){ + if (m_children.size() >= m_max_size){ + m_children[m_zero_offset++] = s; + m_zero_offset %= m_max_size; + }else + m_children.push_back(s); +} diff --git a/src/sparrowshell/shellbuffer.h b/src/sparrowshell/shellbuffer.h new file mode 100644 index 0000000..70e3ca0 --- /dev/null +++ b/src/sparrowshell/shellbuffer.h @@ -0,0 +1,25 @@ +#ifndef SHELLBUFFER_H +#define SHELLBUFFER_H + +#include "scene/containernode.h" + +class TextNode; + +class ShellBuffer : public ContainerNode { +private: + int m_zero_offset = 0; + unsigned int m_max_size; + float m_font_size; +public: + ShellBuffer(int buffer_size):m_max_size(buffer_size){} + SceneNode* 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(); + + // 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;} + float getFontSize(){return m_font_size;} +}; + +#endif // SHELLBUFFER_H diff --git a/src/sparrowshell/shellscrollbar.cpp b/src/sparrowshell/shellscrollbar.cpp new file mode 100644 index 0000000..3c406b9 --- /dev/null +++ b/src/sparrowshell/shellscrollbar.cpp @@ -0,0 +1,46 @@ +#include "shellscrollbar.h" +#include "sparrowshell.h" +#include "mesh.h" +#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); + utils::setPosition2D(m_mesh,m_position); + m_shell->addChild(m_mesh); +} + +void ShellScrollBar::update(){ + m_position.y = m_shell->getPosition().y; + m_dimension.y = m_shell->getDimension().y; + + float cran = ((float)m_shell->getDimension().y/(float)m_shell->getBuffer()->size()); + 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()){ + 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); +} diff --git a/src/sparrowshell/shellscrollbar.h b/src/sparrowshell/shellscrollbar.h new file mode 100644 index 0000000..f6835f8 --- /dev/null +++ b/src/sparrowshell/shellscrollbar.h @@ -0,0 +1,20 @@ +#ifndef SHELLSCROLLBAR_H +#define SHELLSCROLLBAR_H + +#include "glm/vec2.hpp" + +class MeshNode; +class SparrowShell; + +class ShellScrollBar +{ + SparrowShell* m_shell; + glm::ivec2 m_position; + glm::ivec2 m_dimension; + MeshNode *m_mesh; +public: + ShellScrollBar(); + ShellScrollBar(SparrowShell* shell); + void update(); +}; +#endif // SHELLSCROLLBAR_H diff --git a/src/sparrowshell/sparrowshell.cpp b/src/sparrowshell/sparrowshell.cpp new file mode 100644 index 0000000..8b1f4a3 --- /dev/null +++ b/src/sparrowshell/sparrowshell.cpp @@ -0,0 +1,99 @@ +#include "sparrowshell.h" + +//#include "message.h" +#include "input.h" +#include "scene/scenetree.h" +#include "scene/meshnode.h" +#include "scene/textnode.h" +#include "mesh.h" +#include "phongmaterial.h" +#include "tools/utils.h" +#include "tools/font.h" + +const unsigned int SparrowShell::BUFFER_MAX_LENGTH = 50; +const unsigned int SparrowShell::BUFFER_DISPLAYED_NUMBER = 10; +const unsigned int SparrowShell::SCROLLBAR_PIXEL_WIDTH = 10; +const float SparrowShell::SHELL_DEPTH = 10; + +SparrowShell::SparrowShell(sf::Window* window, Input* input): + m_window(window), + m_input(input), + m_position(glm::ivec2(0,0)), + 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_buffer->setFontSize(16.f); + mesh->addRectangle2D(m_position,m_dimension); + PhongMaterial *mat = new PhongMaterial(); + mat->diffuse = glm::vec3(0.1,0.1,0.1); + mat->m_opacity = 0.5; + mesh->setMaterial(mat); + mesh->setDepth(SHELL_DEPTH); + mesh->initGL(); + m_background = new MeshNode(mesh); + this->addChild(m_background); + m_scrollbar = ShellScrollBar(this); +} + +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()); + tnode->setDepth(SHELL_DEPTH+1); + // m_currentScene->addToIndex(tnode); + m_buffer->push(tnode); + if (m_buffer->size() > SparrowShell::BUFFER_DISPLAYED_NUMBER) + m_resizeBuffer = true; +} + +void SparrowShell::scrollUp(){ + if (m_index + SparrowShell::BUFFER_DISPLAYED_NUMBER < m_buffer->size()){ + m_index++; + m_indexMoved = true; + } +} + +void SparrowShell::scrollDown(){ + if (m_index > 0){ + m_index--; + m_indexMoved = true; + } +} + +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(); +} + +void SparrowShell::update(){ + TextNode* tnode; + glm::vec2 text_pos(0); + + for(auto action : m_input->getActions()){ + if(action == 15){ + toggleShell(); + } + } + //move position of shellBuffer + // position textnode inside shellBuffer + + if(m_shellEnabled){ + for(unsigned int i = 0; isize(); i++){ +// tnode = (TextNode*)(m_buffer+i); +// if (i >= m_index && i < m_index + BUFFER_DISPLAYED_NUMBER){ +// utils::setPosition2D(tnode,text_pos); +// text_pos.y += m_buffer->getFontSize(); +// m_scene->addToIndex(tnode); +// } + } + } + m_scrollbar.update(); +} diff --git a/src/sparrowshell/sparrowshell.h b/src/sparrowshell/sparrowshell.h new file mode 100644 index 0000000..26b763a --- /dev/null +++ b/src/sparrowshell/sparrowshell.h @@ -0,0 +1,68 @@ +#ifndef SPARROWSHELL_H +#define SPARROWSHELL_H + +#include + +#include "system.h" +#include "scene/scenetree.h" +#include "glm/vec2.hpp" +#include "sparrowshell/shellbuffer.h" +#include "sparrowshell/shellscrollbar.h" + +class Input; +class MeshNode; +class TextNode; +class ShellBuffer; + +namespace sf { +class Window; +} + +class SparrowShell : public ContainerNode +{ +private: + sf::Window* m_window; + Input* m_input; + + glm::ivec2 m_position; + glm::ivec2 m_dimension; + ShellBuffer* m_buffer; + unsigned int m_index = 0; + bool m_resizeBuffer = false; + bool m_indexMoved = false; + bool m_shellEnabled = false; + //textMesh + MeshNode* m_background; + ShellScrollBar m_scrollbar; + +public: + static const unsigned int BUFFER_MAX_LENGTH; + static const unsigned int BUFFER_DISPLAYED_NUMBER; + static const unsigned int SCROLLBAR_PIXEL_WIDTH; + static const float SHELL_DEPTH; + + SparrowShell(sf::Window*, Input*); + + void update(); + + void scrollUp(); + void scrollDown(); + + void toggleShell(); + void out(std::string); + void setSceneTree(SceneTree* tree) + { + ContainerNode::setSceneTree(tree); +// m_buffer->setSceneTree(tree); + } + + glm::ivec2 getPosition(){return m_position;} + glm::ivec2 getDimension(){return m_dimension;} + + unsigned int getIndex(){return m_index;} + ShellBuffer* getBuffer(){return m_buffer;} + + bool isBufferResized(){return m_resizeBuffer;} + bool indexMoved(){return m_indexMoved;} +}; +#endif // SPARROWSHELL_H diff --git a/src/test/main.cpp b/src/test/main.cpp index 6d8b0da..202603c 100644 --- a/src/test/main.cpp +++ b/src/test/main.cpp @@ -17,7 +17,7 @@ #include #include -#include +#include class myKeysMap : public IKeysMap{ @@ -109,7 +109,6 @@ int main(){ engine.outputShell(std::to_string(i)); } - Input* input = engine.getInput(); std::vector v = {myKeysMap::TOGGLE_CONSOLE}; input->addContext(Context("default",v)); @@ -152,5 +151,4 @@ int main(){ */ - }