#include "sparrowshell.h" #include "message.h" #include "input.h" #include "scenetree.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(); 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) { m_buffer.push(s); if (m_buffer.size() > BUFFER_DISPLAYED_NUMBER) m_resizeScrollBar = true; } void SparrowShell::scrollUp(){ if (m_index + BUFFER_DISPLAYED_NUMBER < m_buffer.size()) m_index++; // std::cout << "scroll up" << std::endl; } void SparrowShell::scrollDown(){ if (m_index > 0) m_index--; // std::cout << "scroll down" << std::endl; } void SparrowShell::update() { //TODO : update TextMesh Font *shellfont = RESOURCE_GET(Font,"shellfont"); MeshNode* mnode; glm::vec2 text_pos(0); //std::vector textMeshes; for(auto textMesh: m_textMeshes) removeChild(textMesh); m_textMeshes.clear(); for(unsigned int i = m_index;igetTextMesh(m_buffer[i],glm::vec3(0.7,1,0.3))); utils::setPosition2D(mnode,text_pos); text_pos.y += shellfont->getLineHeight(); m_textMeshes.push_back(mnode); addChild(mnode); } m_scrollbar.update(); } void SparrowShell::ShellBuffer::push(std::string s){ if (m_buffer.size() >= m_size){ m_buffer[m_zero_offset++] = s; m_zero_offset %= m_size; } else m_buffer.push_back(s); } //SparrowShell::ScrollBar::ScrollBar(); 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; } utils::setPosition2D(m_mesh,new_pos); }