177 lines
5.3 KiB
C++
177 lines
5.3 KiB
C++
#include "sparrowshell.h"
|
|
|
|
#include "SparrowInput/input.h"
|
|
#include "scene/scenetree.h"
|
|
#include "scene/meshnode.h"
|
|
#include "scene/textnode.h"
|
|
#include "scene/gui/backgroundnode.h"
|
|
#include "scriptnode.h"
|
|
|
|
#include "SparrowRenderer/mesh.h"
|
|
#include "SparrowRenderer/phongmaterial.h"
|
|
|
|
#include "tools/font.h"
|
|
#include "resourcemanager.h"
|
|
#include "tools/loader.h"
|
|
#include <string>
|
|
|
|
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_text_color(glm::vec3(0.7,1,0.3)),
|
|
m_input_cursor_pos(0),
|
|
m_input_mesh(nullptr)
|
|
{
|
|
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");
|
|
|
|
//Create mesh for background
|
|
m_background = new BackGroundNode(glm::vec2(0),m_dimension,glm::vec3(0.1,0.1,0.1),0.75,SHELL_DEPTH,false);
|
|
//Create mesh for scrollbar
|
|
m_scrollbar = new ShellScrollBar(this);
|
|
|
|
m_script = new ScriptNode();
|
|
|
|
this->addChild(m_background);
|
|
this->addChild(m_buffer);
|
|
this->addChild(m_scrollbar);
|
|
this->addChild(m_script);
|
|
}
|
|
|
|
// write wstring str in shell
|
|
void SparrowShell::out(std::string str)
|
|
{
|
|
out(str,m_text_color);
|
|
}
|
|
|
|
void SparrowShell::out(std::string str,glm::vec3 color)
|
|
{
|
|
// std::wstring ws;
|
|
// ws.assign(str.begin(),str.end());
|
|
Font *shellfont = RESOURCE_GET(Font,"shellfont");
|
|
TextNode* tnode = shellfont->getTextNode(str,color,m_buffer->getFontSize(),false);
|
|
tnode->setDepth(SHELL_DEPTH+1);
|
|
m_buffer->push(tnode);
|
|
scrollDown();
|
|
if (m_buffer->size() > SparrowShell::BUFFER_DISPLAYED_LINES)
|
|
m_resizeBuffer = true;
|
|
}
|
|
|
|
void SparrowShell::scrollUp()
|
|
{
|
|
if (m_buffer->getIndex() > 0){
|
|
m_buffer->decreaseIndex();
|
|
m_indexMoved = true;
|
|
}
|
|
}
|
|
|
|
void SparrowShell::scrollDown()
|
|
{
|
|
if (m_buffer->getIndex() + SparrowShell::BUFFER_DISPLAYED_LINES < m_buffer->size()){
|
|
m_buffer->increaseIndex();
|
|
m_indexMoved = true;
|
|
}
|
|
}
|
|
|
|
void SparrowShell::toggleShell()
|
|
{
|
|
if(m_shellEnabled){
|
|
getEngine().getInput()->setCurrentContext(m_previous_context);
|
|
getEngine().getWindow()->setKeyRepeatEnabled(false);
|
|
}else{
|
|
m_previous_context = getEngine().getInput()->getCurrentContext();
|
|
getEngine().getInput()->setCurrentContext("shell");
|
|
getEngine().getWindow()->setKeyRepeatEnabled(true);
|
|
}
|
|
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;
|
|
if(m_shellEnabled){
|
|
auto input = getEngine().getInput();
|
|
for(auto action : input->getActions()){
|
|
if (action == m_move_cursor_left){
|
|
if (m_input_cursor_pos > 0)
|
|
moveCursorLeft();
|
|
}
|
|
else if(action == m_move_cursor_right){
|
|
if(m_input_cursor_pos < m_input_string.length())
|
|
moveCursorRight();
|
|
}
|
|
}
|
|
updateTextInput();
|
|
int scroll = input->getDeltaVerticalScroll();
|
|
if(scroll < 0)
|
|
scrollDown();
|
|
else if(scroll > 0)
|
|
scrollUp();
|
|
}
|
|
getEngine().getScene()->updateShaders();
|
|
GraphicalContainerNode::update();
|
|
}
|
|
|
|
void SparrowShell::updateTextInput()
|
|
{
|
|
std::wstring text = getEngine().getInput()->getText();
|
|
bool input_string_updated = false;
|
|
if (!m_shellEnabled)
|
|
return;
|
|
for(unsigned int i = 0 ; i < text.length() ; i++){
|
|
char c = text[i];
|
|
switch(c){
|
|
case 8:
|
|
if(m_input_cursor_pos > 0)
|
|
m_input_string.erase(--m_input_cursor_pos,1);
|
|
input_string_updated = true;
|
|
break;
|
|
case 13:
|
|
if (!m_input_string.empty()){
|
|
out(m_input_string);
|
|
m_script->execute(m_input_string);
|
|
m_input_string.clear();
|
|
input_string_updated = true;
|
|
}
|
|
m_input_cursor_pos = 0;
|
|
break;
|
|
case 127:
|
|
m_input_string.erase(m_input_cursor_pos,1);
|
|
input_string_updated = true;
|
|
break;
|
|
default:
|
|
m_input_string.insert(m_input_cursor_pos++,std::string(1,c));
|
|
input_string_updated = true;
|
|
}
|
|
}
|
|
Font *shellfont = RESOURCE_GET(Font,"shellfont");
|
|
if(input_string_updated)
|
|
{
|
|
if(m_input_mesh)
|
|
this->removeChild(m_input_mesh);
|
|
std::wstring ws;
|
|
ws.assign(m_input_string.begin(),m_input_string.end());
|
|
m_input_mesh = shellfont->getTextNode(m_input_string,m_text_color,m_buffer->getFontSize(),false);
|
|
m_input_mesh->moveTo2D(glm::vec2(0,m_buffer->getFontSize()*BUFFER_DISPLAYED_LINES));
|
|
this->addChild(m_input_mesh);
|
|
m_input_mesh->setVisible(true);
|
|
}
|
|
}
|