#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" #include "resourcemanager.h" #include "tools/loader.h" #include const unsigned int SparrowShell::BUFFER_MAX_LENGTH = 50; const unsigned int SparrowShell::BUFFER_DISPLAYED_LINES = 15; const unsigned int SparrowShell::SCROLLBAR_PIXEL_WIDTH = 10; const float SparrowShell::SHELL_DEPTH = 10; const float SparrowShell::DEFAULT_FONT_SIZE = 16.f; SparrowShell::SparrowShell(sf::Window* window): m_position(glm::ivec2(0)), m_buffer(new ShellBuffer(BUFFER_MAX_LENGTH)), m_input_cursor_pos(0) { sf::Vector2u size = window->getSize(); m_dimension = glm::ivec2(size.x,DEFAULT_FONT_SIZE*(BUFFER_DISPLAYED_LINES+1)); m_buffer->setFontSize(DEFAULT_FONT_SIZE); moveTo2D(glm::vec2(m_position)); Font* fonte_des_neiges = Loader::loadFont("../data/consolas.fnt","../data/consolas.png"); RESOURCE_ADD(fonte_des_neiges,Font,"shellfont"); m_text_color = glm::vec3(0.7,1,0.3); //Create mesh for background Mesh* mesh = new Mesh(); mesh->addRectangle2D(glm::vec2(0),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,false); //Create mesh for scrollbar mesh = new Mesh(); glm::vec2 shell_pos = glm::vec2(0,0); glm::vec2 shell_dim = glm::vec2(SCROLLBAR_PIXEL_WIDTH,DEFAULT_FONT_SIZE*BUFFER_DISPLAYED_LINES); 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_dim); this->addChild(m_background); this->addChild(m_buffer); this->addChild(m_scrollbar); } void SparrowShell::out(std::string s) { Font *shellfont = RESOURCE_GET(Font,"shellfont"); TextNode* tnode = shellfont->getTextNode(s,m_text_color,m_buffer->getFontSize(),false); tnode->setDepth(SHELL_DEPTH+1); m_buffer->push(tnode); if (m_buffer->size() > SparrowShell::BUFFER_DISPLAYED_LINES) m_resizeBuffer = true; } void SparrowShell::scrollUp() { if (m_buffer->getIndex() + SparrowShell::BUFFER_DISPLAYED_LINES < m_buffer->size()){ m_buffer->increaseIndex(); m_indexMoved = true; } } void SparrowShell::scrollDown() { if (m_buffer->getIndex() > 0){ m_buffer->decreaseIndex(); m_indexMoved = true; } } void SparrowShell::toggleShell() { m_shellEnabled = !m_shellEnabled; for(auto child : m_children) child->toggleVisibility(); m_buffer->toggleBuffer(); m_scene->updateShaders(); } void SparrowShell::update() { m_resizeBuffer = false; m_indexMoved = false; auto input = getEngine().getInput(); for(auto action : input->getActions()){ if(action == 6){ out("Plop"); } } if(m_shellEnabled){ updateTextInput(); int scroll = input->getDeltaVerticalScroll(); if(scroll > 0) scrollDown(); else if(scroll < 0) scrollUp(); } GraphicalContainerNode::update(); } void SparrowShell::updateTextInput() { std::string text = getEngine().getInput()->getText(); for(unsigned int i = 0 ; i < text.length() ; i++){ char c = text[i]; if (c == 8){ if(m_input_cursor_pos > 0) m_input_string.erase(--m_input_cursor_pos,1); }else m_input_string.insert(m_input_cursor_pos++,std::string(1,c)); std::cout << text[i] << (int) text[i] << std::endl; } Font *shellfont = RESOURCE_GET(Font,"shellfont"); if(m_input_mesh) this->removeChild(m_input_mesh); m_input_mesh = shellfont->getTextNode(m_input_string,m_text_color,m_buffer->getFontSize(),false); this->addChild(m_input_mesh); // m_input_string.insert(m_input_cursor_pos,c); // m_input_string.append(); // std::cout << m_input_string << std::endl; }