From 8d3b0b527c3fa2480f78270b782831a25f6f4e5b Mon Sep 17 00:00:00 2001 From: Lendemor Date: Fri, 3 Feb 2017 23:12:39 +0100 Subject: [PATCH] added cursor and textinputnode in shell --- src/engine.cpp | 49 +++++++---- src/engine.h | 12 ++- src/scene/gui/backgroundnode.cpp | 4 +- src/scene/gui/backgroundnode.h | 2 +- src/scene/gui/buttonshape.cpp | 2 +- src/scene/gui/textinputnode.cpp | 122 ++++++++++++++++++++++++++++ src/scene/gui/textinputnode.h | 42 ++++++++++ src/scene/scenetree.h | 3 +- src/scene/textnode.h | 4 + src/sparrowshell/shellbuffer.cpp | 7 +- src/sparrowshell/shellbuffer.h | 7 +- src/sparrowshell/shellscrollbar.cpp | 1 - src/sparrowshell/sparrowshell.cpp | 103 ++++++++++------------- src/sparrowshell/sparrowshell.h | 44 ++++++---- src/test/main.cpp | 50 +++++++----- src/tools/font.cpp | 2 + src/tools/font.h | 1 + 17 files changed, 324 insertions(+), 131 deletions(-) create mode 100644 src/scene/gui/textinputnode.cpp create mode 100644 src/scene/gui/textinputnode.h diff --git a/src/engine.cpp b/src/engine.cpp index 05bd0cd..50c6698 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -93,7 +93,7 @@ void Engine::update() } // update Scene - m_scene->update(); + getScene()->update(); // update Display if(m_input->isResized()) @@ -105,8 +105,6 @@ void Engine::update() void Engine::start() { m_running = true; -// for(int i = 0;i<5;i++) -// m_sparrowshell->scrollUp(); while(!m_input->isCloseRequested() && m_running) update(); } @@ -126,20 +124,30 @@ unsigned int Engine::getDeltaTime() const return m_timeStamp - m_lastTimeStamp; } -void Engine::setScene(SceneTree *scene) +//void Engine::setScene(SceneTree *scene) +void Engine::setScene(std::string scene) { + if(m_current_scene.empty()){ + m_current_scene = scene; + } + SceneTree* previous_scene = RESOURCE_GET(SceneTree,m_current_scene); + SceneTree* new_scene = RESOURCE_GET(SceneTree,scene); + m_current_scene = scene; + if(m_physicsDebugNode != nullptr) { - m_scene->removeFromIndex(m_physicsDebugNode); - scene->addToIndex(m_physicsDebugNode); + previous_scene->removeFromIndex(m_physicsDebugNode); + new_scene->addToIndex(m_physicsDebugNode); +// m_scene->removeFromIndex(m_physicsDebugNode); +// scene->addToIndex(m_physicsDebugNode); } - scene->getRootObject()->removeChild(m_sparrowshell); - m_scene = scene; - m_renderer->setScene(m_scene); + + previous_scene->getRootObject()->removeChild(m_sparrowshell); + m_renderer->setScene(new_scene); m_renderer->resizeGL(m_window->getSize().x, m_window->getSize().y); - m_sparrowshell->setSceneTree(scene); - scene->getRootObject()->addChild(m_sparrowshell); - scene->updateShaders(); + // m_sparrowshell->setSceneTree(new_scene); + new_scene->getRootObject()->addChild(m_sparrowshell); + new_scene->updateShaders(); } void Engine::enablePhysicsDebug() @@ -147,10 +155,10 @@ void Engine::enablePhysicsDebug() if(m_world != nullptr && m_physicsDebugNode == nullptr) { m_physicsDebugNode = new PhysicsDebugNode(); - m_scene->addToIndex(m_physicsDebugNode); + getScene()->addToIndex(m_physicsDebugNode); m_world->setDebugDrawer(m_physicsDebugNode); m_world->getDebugDrawer()->setDebugMode(btIDebugDraw::DBG_DrawWireframe); - m_scene->updateShaders(); + getScene()->updateShaders(); } } @@ -159,7 +167,7 @@ void Engine::disablePhysicsDebug() if(m_world != nullptr && m_physicsDebugNode != nullptr) { m_world->setDebugDrawer(nullptr); - m_scene->removeFromIndex(m_physicsDebugNode); + getScene()->removeFromIndex(m_physicsDebugNode); delete m_physicsDebugNode; m_physicsDebugNode = nullptr; } @@ -216,8 +224,13 @@ void Engine::checkSpecialInputs() } - -SceneTree* Engine::createScene() +void Engine::createScene(std::string scene_name) { - return new SceneTree(*this); + RESOURCE_ADD(new SceneTree(*this),SceneTree,scene_name); + //return new SceneTree(*this); +} + +SceneTree* Engine::getScene() const +{ + return RESOURCE_GET(SceneTree,m_current_scene); } diff --git a/src/engine.h b/src/engine.h index 9c58e2a..6ca9c1b 100644 --- a/src/engine.h +++ b/src/engine.h @@ -26,7 +26,8 @@ public: unsigned int w = 800, unsigned int h = 600, const std::string &mode = "windowed"); - void setScene(SceneTree *scene); + // void setScene(SceneTree *scene); + void setScene(std::string scene); void initPhysics(); void enablePhysicsDebug(); @@ -48,12 +49,14 @@ public: SparrowRenderer* getRenderer() const {return m_renderer;} btDiscreteDynamicsWorld* getPhysics() const {return m_world;} SparrowShell* getShell() const {return m_sparrowshell;} - SceneTree* getScene() const {return m_scene;} + SceneTree* getScene() const; unsigned int getTime() const; unsigned int getDeltaTime() const; - SceneTree* createScene(); +// SceneTree* createScene(); + void createScene(std::string scene_name); + //void setCurrentScene(std::string scene_name){m_current_scene = scene_name;} private: sf::Clock* m_clock; @@ -64,7 +67,8 @@ private: sf::Window* m_window; Input* m_input; - SceneTree* m_scene; +// SceneTree* m_scene; + std::string m_current_scene; SparrowShell* m_sparrowshell; btDiscreteDynamicsWorld* m_world; PhysicsDebugNode *m_physicsDebugNode; diff --git a/src/scene/gui/backgroundnode.cpp b/src/scene/gui/backgroundnode.cpp index c0ecc20..e31a1b8 100644 --- a/src/scene/gui/backgroundnode.cpp +++ b/src/scene/gui/backgroundnode.cpp @@ -6,11 +6,11 @@ #include "SparrowRenderer/mesh.h" #include "SparrowRenderer/phongmaterial.h" -BackGroundNode::BackGroundNode(glm::vec2 position,glm::vec2 dimension, glm::vec3 color, float opacity,float depth,bool visible): +BackGroundNode::BackGroundNode(glm::vec2 dimension, glm::vec3 color, float opacity,float depth,bool visible): MeshNode(nullptr,visible) { Mesh* mesh = new Mesh(); - mesh->addRectangle2D(position,dimension); + mesh->addRectangle2D(glm::vec2(0),dimension); PhongMaterial *mat = new PhongMaterial(); mat->diffuse = color; mat->m_opacity = opacity; diff --git a/src/scene/gui/backgroundnode.h b/src/scene/gui/backgroundnode.h index 8ce6bb8..358c49b 100644 --- a/src/scene/gui/backgroundnode.h +++ b/src/scene/gui/backgroundnode.h @@ -6,7 +6,7 @@ class BackGroundNode : public MeshNode { public: - BackGroundNode(glm::vec2 position,glm::vec2 dimension, glm::vec3 color, float opacity,float depth,bool visible=true); + BackGroundNode(glm::vec2 dimension, glm::vec3 color, float opacity,float depth,bool visible=true); }; #endif // BACKGROUNDNODE_H diff --git a/src/scene/gui/buttonshape.cpp b/src/scene/gui/buttonshape.cpp index da693e7..8ed1bf0 100644 --- a/src/scene/gui/buttonshape.cpp +++ b/src/scene/gui/buttonshape.cpp @@ -4,7 +4,7 @@ RectangleButtonShape::RectangleButtonShape(glm::vec2 dimension):ButtonShape(),m_dimension(dimension) { - m_background = new BackGroundNode(glm::vec2(0,0),dimension,glm::vec3(0.2,0.6,0.2),1,0); + m_background = new BackGroundNode(dimension,glm::vec3(0.2,0.6,0.2),1,0); } bool RectangleButtonShape::hover(glm::vec2 button_position, glm::vec2 mouse_position){ diff --git a/src/scene/gui/textinputnode.cpp b/src/scene/gui/textinputnode.cpp new file mode 100644 index 0000000..3613573 --- /dev/null +++ b/src/scene/gui/textinputnode.cpp @@ -0,0 +1,122 @@ +#include "textinputnode.h" + +#include + +#include "SparrowInput/input.h" + +#include "engine.h" +#include "tools/font.h" +#include "resourcemanager.h" + +TextInputNode::TextInputNode(glm::vec2 dimension): + m_dimension(dimension), + m_font_size(16.f), + m_text_color(glm::vec3(1,1,1)), + m_cursor_pos(0) +{ + // Font *shellfont = RESOURCE_GET(Font,"shellfont"); + Mesh* mesh = new Mesh(); + mesh->addRectangle2D(glm::vec2(0),glm::vec2(2, m_font_size)); + PhongMaterial* mat = new PhongMaterial(); + mat->diffuse = glm::vec3(1,1,1); + mesh->setMaterial(mat); + mesh->setDepth(30); + mesh->initGL(); + m_cursor_mesh = new MeshNode(mesh,false); + addChild(m_cursor_mesh); +} + +void TextInputNode::update() +{ + if(!m_hasFocus) return; + + std::wstring text = getEngine().getInput()->getText(); + bool input_string_updated = false; + bool cursor_pos_updated = false; + + auto input = getEngine().getInput(); + for(auto action : input->getActions()){ + if (action == m_move_cursor_left){ + if (m_cursor_pos > 0){ + m_cursor_pos--; + cursor_pos_updated=true; + } + } + else if(action == m_move_cursor_right){ + if(m_cursor_pos < m_text.length()){ + m_cursor_pos++; + cursor_pos_updated=true; + } + } + } + + for(unsigned int i = 0 ; i < text.length() ; i++){ + char c = text[i]; + switch(c){ + case 8: + if(m_cursor_pos > 0) + m_text.erase(--m_cursor_pos,1); + input_string_updated = true; + cursor_pos_updated=true; + break; + case 13: + if (!m_text.empty()) + { + if(m_callback) + m_callback->exec(); + input_string_updated = true; + m_cursor_pos = 0; + cursor_pos_updated=true; + } + break; + case 127: + m_text.erase(m_cursor_pos,1); + input_string_updated = true; + break; + default: + m_text.insert(m_cursor_pos++,std::string(1,c)); + input_string_updated = true; + cursor_pos_updated=true; + } + } + Font *shellfont = RESOURCE_GET(Font,"shellfont"); + if(cursor_pos_updated){ + m_cursor_mesh->moveTo2D(glm::vec2(m_cursor_pos*shellfont->getXAdvance()*(m_font_size/shellfont->getLineHeight()),0)); + } + + if(input_string_updated) + { + if(m_text_mesh){ + this->removeChild(m_text_mesh); + delete(m_text_mesh); + } + m_text_mesh = shellfont->getTextNode(m_text,m_text_color,m_font_size,false); + if(m_text_mesh){ + m_text_mesh->setTransform(glm::mat4()); + m_text_mesh->setDepth(15); + this->addChild(m_text_mesh); + m_text_mesh->setVisible(true); + } + } + GUINode::update(); +} + +void TextInputNode::setFocus(bool focus) +{ + m_hasFocus = focus; + m_cursor_mesh->setVisible(focus); +} + +void TextInputNode::setTextColor(glm::vec3 color) +{ + m_text_color = color; +} + +std::string TextInputNode::getText() +{ + return m_text; +} + + + + diff --git a/src/scene/gui/textinputnode.h b/src/scene/gui/textinputnode.h new file mode 100644 index 0000000..dc93e61 --- /dev/null +++ b/src/scene/gui/textinputnode.h @@ -0,0 +1,42 @@ +#ifndef TEXTINPUTNODE_H +#define TEXTINPUTNODE_H + +#include "scene/gui/guinode.h" +#include + +#include "scene/textnode.h" +#include "scene/gui/callback.h" + +class TextInputNode : public GUINode +{ + glm::vec2 m_dimension; + bool m_hasFocus; + float m_font_size; + + TextNode* m_text_mesh; + std::string m_text; + glm::vec3 m_text_color; + + MeshNode* m_cursor_mesh; + unsigned int m_cursor_pos; + + int m_move_cursor_left; + int m_move_cursor_right; + + CallBack* m_callback; + +public: + TextInputNode(glm::vec2 dimension); + void update(); + glm::vec2 getDimension(){return m_dimension;} + + void setFocus(bool focus); + void setTextColor(glm::vec3 color); + std::string getText(); + void setCallBack(CallBack* callback){m_callback = callback;} + void setMoveCursorLeft(int action){m_move_cursor_left = action;} + void setMoveCursorRight(int action){m_move_cursor_right = action;} + void clearText(){m_text.clear();} +}; + +#endif // TEXTINPUTNODE_H diff --git a/src/scene/scenetree.h b/src/scene/scenetree.h index 1c5a62b..eb23d51 100644 --- a/src/scene/scenetree.h +++ b/src/scene/scenetree.h @@ -39,7 +39,8 @@ public: private: // A SceneTree can only be constructed by the engine by using Engine::createScene() - friend SceneTree* Engine::createScene(); + //friend SceneTree* Engine::createScene(); + friend void Engine::createScene(std::string scene_name); SceneTree(const Engine &engine); const Engine &m_engine; diff --git a/src/scene/textnode.h b/src/scene/textnode.h index 445c6be..fefec91 100644 --- a/src/scene/textnode.h +++ b/src/scene/textnode.h @@ -2,6 +2,10 @@ #define TEXTNODE_H #include "meshnode.h" + +#include "SparrowRenderer/mesh.h" +#include "SparrowRenderer/phongmaterial.h" + #include "glm/vec2.hpp" class TextNode : public MeshNode diff --git a/src/sparrowshell/shellbuffer.cpp b/src/sparrowshell/shellbuffer.cpp index bd409ea..3a3286d 100644 --- a/src/sparrowshell/shellbuffer.cpp +++ b/src/sparrowshell/shellbuffer.cpp @@ -12,7 +12,6 @@ void ShellBuffer::toggleBuffer(){ void ShellBuffer::update() { - GraphicalContainerNode::update(); TextNode* tnode; glm::vec2 text_pos(0,0); @@ -32,6 +31,7 @@ void ShellBuffer::update() tnode->moveTo2D(glm::vec2(-100,-100)); } } + GUINode::update(); } void ShellBuffer::push(TextNode* tnode){ @@ -51,3 +51,8 @@ void ShellBuffer::push(TextNode* tnode){ tnode->setParentVisible(isVisible()); } } + +glm::vec2 ShellBuffer::getDimension(){ + return glm::vec2(getEngine().getWindow()->getSize().x - SparrowShell::SCROLLBAR_PIXEL_WIDTH, + SparrowShell::BUFFER_DISPLAYED_LINES * SparrowShell::DEFAULT_FONT_SIZE); +} diff --git a/src/sparrowshell/shellbuffer.h b/src/sparrowshell/shellbuffer.h index e08f6dd..19aa878 100644 --- a/src/sparrowshell/shellbuffer.h +++ b/src/sparrowshell/shellbuffer.h @@ -1,12 +1,11 @@ #ifndef SHELLBUFFER_H #define SHELLBUFFER_H -#include "scene/graphicalcontainernode.h" - +#include "scene/gui/guinode.h" class TextNode; -class ShellBuffer : public GraphicalContainerNode { +class ShellBuffer : public GUINode { private: unsigned int m_max_size; int m_zero_offset; @@ -28,6 +27,8 @@ public: void setFontSize(float font_size){m_font_size = font_size;} float getFontSize(){return m_font_size;} + glm::vec2 getDimension(); + void increaseIndex(){m_index++;} void decreaseIndex(){m_index--;} unsigned int getIndex(){return m_index;} diff --git a/src/sparrowshell/shellscrollbar.cpp b/src/sparrowshell/shellscrollbar.cpp index 5b5fb47..3381a22 100644 --- a/src/sparrowshell/shellscrollbar.cpp +++ b/src/sparrowshell/shellscrollbar.cpp @@ -41,7 +41,6 @@ void ShellScrollBar::update(){ if (m_shell->indexMoved()) { m_position.y = cran * indexCursor; -// std::cout << m_position.x << " " << m_position.y << std::endl; moveTo2D(m_position); } } diff --git a/src/sparrowshell/sparrowshell.cpp b/src/sparrowshell/sparrowshell.cpp index b47ebf2..5cde2ee 100644 --- a/src/sparrowshell/sparrowshell.cpp +++ b/src/sparrowshell/sparrowshell.cpp @@ -5,6 +5,7 @@ #include "scene/meshnode.h" #include "scene/textnode.h" #include "scene/gui/backgroundnode.h" +#include "scene/gui/textinputnode.h" #include "scriptnode.h" #include "SparrowRenderer/mesh.h" @@ -22,35 +23,51 @@ 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) + m_text_color(glm::vec3(0.7,1,0.3)) { 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)); + setPosition(glm::vec2(0)); 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); + m_background = new BackGroundNode(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(); + m_input_node = new TextInputNode(glm::vec2(m_buffer->getFontSize(),size.y)); + m_input_node->setPosition(glm::vec2(0,m_buffer->getFontSize()*BUFFER_DISPLAYED_LINES)); + m_input_node->setVisible(false); + m_input_node->setCallBack(new InputCallBack(this,m_input_node)); + this->addChild(m_background); this->addChild(m_buffer); this->addChild(m_scrollbar); this->addChild(m_script); + this->addChild(m_input_node); } -// write wstring str in shell +SparrowShell::InputCallBack::InputCallBack(SparrowShell* shell,TextInputNode* textinput): + m_shell(shell), + m_text_input_node(textinput) +{ +} + +void SparrowShell::InputCallBack::exec(){ + std::string text = m_text_input_node->getText(); + m_shell->out(text); + m_shell->m_script->execute(text); + m_text_input_node->clearText(); +} + +// write string str in shell void SparrowShell::out(std::string str) { out(str,m_text_color); @@ -58,8 +75,6 @@ void SparrowShell::out(std::string str) 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); @@ -67,6 +82,7 @@ void SparrowShell::out(std::string str,glm::vec3 color) scrollDown(); if (m_buffer->size() > SparrowShell::BUFFER_DISPLAYED_LINES) m_resizeBuffer = true; +// getEngine().getScene()->updateShaders(); } void SparrowShell::scrollUp() @@ -85,6 +101,16 @@ void SparrowShell::scrollDown() } } +void SparrowShell::setMoveCursorLeftAction(int action) +{ + m_input_node->setMoveCursorLeft(action); +} + +void SparrowShell::setMoveCursorRightAction(int action) +{ + m_input_node->setMoveCursorRight(action); +} + void SparrowShell::toggleShell() { if(m_shellEnabled){ @@ -96,6 +122,7 @@ void SparrowShell::toggleShell() getEngine().getWindow()->setKeyRepeatEnabled(true); } m_shellEnabled = !m_shellEnabled; + m_input_node->setFocus(m_shellEnabled); for(auto child : m_children) child->toggleVisibility(); m_buffer->toggleBuffer(); @@ -108,69 +135,21 @@ void SparrowShell::update() m_indexMoved = false; if(m_shellEnabled){ auto input = getEngine().getInput(); - for(auto action : input->getActions()){ - if (action == m_move_cursor_left){ +// 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); - } + GUINode::update(); } diff --git a/src/sparrowshell/sparrowshell.h b/src/sparrowshell/sparrowshell.h index ba4edd5..716dbd1 100644 --- a/src/sparrowshell/sparrowshell.h +++ b/src/sparrowshell/sparrowshell.h @@ -3,16 +3,23 @@ #include -#include "scene/scenetree.h" #include "glm/vec2.hpp" + +#include "scene/scenetree.h" +#include "scene/graphicalcontainernode.h" + +#include "scene/gui/guinode.h" +#include "scene/gui/callback.h" + #include "sparrowshell/shellbuffer.h" #include "sparrowshell/shellscrollbar.h" -#include "scene/graphicalcontainernode.h" + #include "SparrowInput/keybindings.h" class Input; -class MeshNode; -class TextNode; +//class MeshNode; +//class TextNode; +class TextInputNode; class ShellBuffer; class BackGroundNode; class ScriptNode; @@ -21,11 +28,18 @@ namespace sf { class Window; } -class SparrowShell : public GraphicalContainerNode +class SparrowShell : public GUINode { private: - glm::ivec2 m_position; - glm::ivec2 m_dimension; + class InputCallBack : public CallBack{ + SparrowShell* m_shell; + TextInputNode* m_text_input_node; + public: + InputCallBack(SparrowShell* shell,TextInputNode* textinput); + virtual void exec(); + }; + + glm::vec2 m_dimension; bool m_shellEnabled = false; bool m_resizeBuffer = false; bool m_indexMoved = false; @@ -36,9 +50,7 @@ private: ScriptNode * m_script; glm::vec3 m_text_color; - std::string m_input_string; - unsigned int m_input_cursor_pos; - TextNode* m_input_mesh; + TextInputNode* m_input_node; int m_plop_test; int m_move_cursor_left; @@ -56,7 +68,6 @@ public: SparrowShell(sf::Window*); void update(); - void updateTextInput(); void scrollUp(); void scrollDown(); @@ -64,18 +75,17 @@ public: void out(std::string str); void out(std::string str, glm::vec3 color); - glm::ivec2 getPosition(){return m_position;} - glm::ivec2 getDimension(){return m_dimension;} + glm::vec2 getDimension(){return m_dimension;} unsigned int getIndex(){return m_buffer->getIndex();} ShellBuffer* getBuffer(){return m_buffer;} - void setMoveCursorLeftAction(int action){m_move_cursor_left = action;} - void setMoveCursorRightAction(int action){m_move_cursor_right = action;} + void setMoveCursorLeftAction(int action); + void setMoveCursorRightAction(int action); void setClearConsoleAction(int action){m_clear_console_action = action;} - void moveCursorLeft(){m_input_cursor_pos--;} - void moveCursorRight(){m_input_cursor_pos++;} + //void moveCursorLeft(){m_input_cursor_pos--;} + //void moveCursorRight(){m_input_cursor_pos++;} bool isEnabled(){return m_shellEnabled;} bool isBufferResized(){return m_resizeBuffer;} diff --git a/src/test/main.cpp b/src/test/main.cpp index 4c0d42e..c06c758 100644 --- a/src/test/main.cpp +++ b/src/test/main.cpp @@ -38,6 +38,8 @@ #include "SparrowSerializer/serializable.h" #include +#include "resourcemanager.h" + #include "scene/gui/callback.h" class TestGen : public TerrainGenerator @@ -180,21 +182,26 @@ struct Config : public Serializable }; class Demo { - SceneTree* m_demo_scene; + std::string m_demo_scene; + Engine* m_engine; + Config* m_config; PlayerCharacterNode* m_player; public: - Demo(Engine* engine,Config* config){ - m_demo_scene = engine->createScene(); + Demo(Engine* engine,Config* config):m_demo_scene("demo"),m_engine(engine),m_config(config){ + m_engine->createScene(m_demo_scene); + } + void initScene(){ //player m_player = new PlayerCharacterNode(false); m_player->setInputs(DefaultKeysMap::MOVE_FORWARD, DefaultKeysMap::MOVE_BACKWARD, DefaultKeysMap::STRAFE_LEFT, DefaultKeysMap::STRAFE_RIGHT, DefaultKeysMap::JUMP, DefaultKeysMap::TOGGLE_NOCLIP); - m_demo_scene->getRootObject()->addChild(m_player); - m_demo_scene->setMainCamera(m_player); + SceneTree* scene = RESOURCE_GET(SceneTree,m_demo_scene); + scene->getRootObject()->addChild(m_player); + scene->setMainCamera(m_player); //potator Potator *potator = new Potator(m_player, DefaultKeysMap::MAIN_ACTION, DefaultKeysMap::SECONDARY_ACTION, DefaultKeysMap::TERTIARY_ACTION); - m_demo_scene->getRootObject()->addChild(potator); + scene->getRootObject()->addChild(potator); //lighting LightNode *ambientLight = new LightNode(new AmbientLight(glm::vec3(0.05f))); @@ -202,28 +209,29 @@ public: DirectionnalLight* sun = new DirectionnalLight(glm::vec3(5, 8, -2), glm::vec3(0.9f)); LightNode *sunLight = new LightNode(sun); - m_demo_scene->getRootObject()->addChild(ambientLight); - m_demo_scene->getRootObject()->addChild(sunLight); + scene->getRootObject()->addChild(ambientLight); + scene->getRootObject()->addChild(sunLight); - if(config->scene == "sponza") + if(m_config->scene == "sponza") { sun->initShadowMap(4096); - generateSponza(m_demo_scene, engine->getPhysics()); - m_demo_scene->getRootObject()->addChild(new LightNode(new PointLight(glm::vec3(-3.5, 2, 1.8), 15, glm::vec3(0.35f)))); - m_demo_scene->getRootObject()->addChild(new LightNode(new PointLight(glm::vec3(-5, 6, 2), 15, glm::vec3(0.35f)))); + generateSponza(scene, m_engine->getPhysics()); + scene->getRootObject()->addChild(new LightNode(new PointLight(glm::vec3(-3.5, 2, 1.8), 15, glm::vec3(0.35f)))); + scene->getRootObject()->addChild(new LightNode(new PointLight(glm::vec3(-5, 6, 2), 15, glm::vec3(0.35f)))); m_player->setPosition(0.f, 2.f, 0.f); sun->setShadowView(glm::vec3(30, 30, 50)); } - else if(config->scene == "terrain") + else if(m_config->scene == "terrain") { sun->initShadowMap(4096); - generateTerrain(m_demo_scene, engine->getPhysics()); + generateTerrain(scene, m_engine->getPhysics()); m_player->setPosition(0.f, 15.f, 0.f); sun->setShadowView(glm::vec3(130, 130, 70)); } + } - SceneTree* getScene(){return m_demo_scene;} + std::string getScene(){return m_demo_scene;} PlayerCharacterNode* getPlayer(){return m_player;} }; @@ -235,6 +243,7 @@ public: ButtonDemoCallBack(Engine* engine, Demo* demo):m_engine(engine),m_demo(demo){} void exec(){ + m_demo->initScene(); m_engine->setScene(m_demo->getScene()); m_engine->getInput()->setCurrentContext("default"); m_engine->getInput()->setMouseGrabbed(true); @@ -246,14 +255,15 @@ public: class Menu { int m_left_click_action; ButtonNode* m_button_demo; - SceneTree* m_menu_scene; + std::string m_menu_scene; public: - Menu(Engine* engine,Config* config){ - m_menu_scene = engine->createScene(); + Menu(Engine* engine,Config* config):m_menu_scene("menu"){ + engine->createScene(m_menu_scene); + SceneTree* scene = RESOURCE_GET(SceneTree,m_menu_scene); m_button_demo = new ButtonNode(new RectangleButtonShape(glm::vec2(300,100))); m_button_demo->getLabel()->setText("Start DEMO"); m_button_demo->getLabel()->setColor(glm::vec3(0.9,0.4,0.3)); - m_menu_scene->getRootObject()->addChild(m_button_demo); + scene->getRootObject()->addChild(m_button_demo); sf::Vector2u size = engine->getWindow()->getSize(); glm::vec2 pos = glm::vec2(size.x,size.y)/glm::vec2(2,2) - m_button_demo->getDimension()/glm::vec2(2,2); m_button_demo->setPosition(pos); @@ -269,7 +279,7 @@ public: m_button_demo->setCallBack(button_callback); } - SceneTree* getScene(){return m_menu_scene;} + std::string getScene(){return m_menu_scene;} }; INIT_SERIALIZABLE(Config) diff --git a/src/tools/font.cpp b/src/tools/font.cpp index 51c0021..8ce7723 100644 --- a/src/tools/font.cpp +++ b/src/tools/font.cpp @@ -10,6 +10,8 @@ Font::Font() TextNode* Font::getTextNode(std::string s, glm::vec3 color, float font_size,bool visible) { + if(s.empty()) return nullptr; + std::wstring ws; ws.assign(s.begin(),s.end()); Mesh* textmesh = new Mesh(); diff --git a/src/tools/font.h b/src/tools/font.h index 9f2b88d..8994e9c 100644 --- a/src/tools/font.h +++ b/src/tools/font.h @@ -26,6 +26,7 @@ public: void setNbChar(int nbchar){m_nbChar =nbchar;} void setLineHeight(float line_h){m_defaultLineHeight = line_h;} float getLineHeight(){return m_defaultLineHeight;} + float getXAdvance(){return (m_charTable['a']).xadvance;} void setBase(float base){m_base = base;} void setAntiAnliasing(bool antialiasing){m_antiAliasing = antialiasing;} void setScale(glm::vec2 scale){m_scale = scale;}