SparrowEngine/src/sparrowshell.cpp
2016-12-04 15:40:34 +01:00

129 lines
4.1 KiB
C++

#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(12.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::update()
{
//Font *shellfont = RESOURCE_GET(Font,"shellfont");
TextNode* tnode;
glm::vec2 text_pos(0);
// if(m_indexMoved){
// unsigned int count_stop = m_buffer.size() > BUFFER_DISPLAYED_NUMBER ? m_index + BUFFER_DISPLAYED_NUMBER: m_buffer.size();
//move position of shellBuffer
// position textnode inside shellBuffer
for(unsigned int i = 0; i<m_buffer.size(); 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_currentScene->addToIndex(tnode);
// m_currentScene->addObject(this,mnode);
}
}
// m_indexMoved = false;
// }
m_scrollbar.update();
}
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);
}