From 64247c75e0a559b678bca9a561a1d603177dc3cc Mon Sep 17 00:00:00 2001 From: Lendemor Date: Sat, 21 Jan 2017 19:47:20 +0100 Subject: [PATCH] fix crash by lua and start reorganizing buttonnode --- src/scene/gui/backgroundnode.cpp | 4 ++-- src/scene/gui/backgroundnode.h | 2 +- src/scene/gui/buttonnode.cpp | 17 +++++------------ src/scene/gui/buttonnode.h | 6 +++--- src/scene/gui/buttonshape.cpp | 7 ++++++- src/scene/gui/buttonshape.h | 7 +++++++ src/sparrowshell/scriptnode.cpp | 15 +++++++++------ src/sparrowshell/sparrowshell.cpp | 18 +++++------------- src/test/main.cpp | 8 ++++---- 9 files changed, 42 insertions(+), 42 deletions(-) diff --git a/src/scene/gui/backgroundnode.cpp b/src/scene/gui/backgroundnode.cpp index 0a4bc72..3cd3bd9 100644 --- a/src/scene/gui/backgroundnode.cpp +++ b/src/scene/gui/backgroundnode.cpp @@ -6,8 +6,8 @@ #include "mesh.h" #include "phongmaterial.h" -BackGroundNode::BackGroundNode(glm::vec2 position,glm::vec2 dimension, glm::vec3 color, float opacity,float depth): - MeshNode(nullptr,false) +BackGroundNode::BackGroundNode(glm::vec2 position,glm::vec2 dimension, glm::vec3 color, float opacity,float depth,bool visible): + MeshNode(nullptr,visible) { Mesh* mesh = new Mesh(); mesh->addRectangle2D(position,dimension); diff --git a/src/scene/gui/backgroundnode.h b/src/scene/gui/backgroundnode.h index 114fb6b..8ce6bb8 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); + BackGroundNode(glm::vec2 position,glm::vec2 dimension, glm::vec3 color, float opacity,float depth,bool visible=true); }; #endif // BACKGROUNDNODE_H diff --git a/src/scene/gui/buttonnode.cpp b/src/scene/gui/buttonnode.cpp index 8715ec3..ffca147 100644 --- a/src/scene/gui/buttonnode.cpp +++ b/src/scene/gui/buttonnode.cpp @@ -13,16 +13,12 @@ ButtonNode::ButtonNode(glm::vec2 position,ButtonShape* shape): m_position(position),m_shape(shape) { + addChild(m_shape->getBackGround()); } -void ButtonNode::setBackGround(BackGroundNode* background){ - m_background = background; - addChild(background); -} - -BackGroundNode* ButtonNode::getBackGround() +MeshNode* ButtonNode::getBackGround() { - return m_background; + return m_shape->getBackGround(); } void ButtonNode::update() @@ -30,21 +26,18 @@ void ButtonNode::update() Input* input = getEngine().getInput(); sf::Vector2i v = input->getPosition(); glm::vec2 pos = glm::vec2(v.x,v.y); + PhongMaterial* mat = (PhongMaterial*) m_shape->getBackGround()->getGeometryNode()->mesh->getMaterial(); if (m_shape->hover(pos)){ - PhongMaterial* mat = (PhongMaterial*) m_background->getGeometryNode()->mesh->getMaterial(); mat->diffuse=glm::vec3(0.2,0.6,0.6); }else{ - PhongMaterial* mat = (PhongMaterial*) m_background->getGeometryNode()->mesh->getMaterial(); mat->diffuse=glm::vec3(0.6,0.2,0.6); } for (auto action : input->getActions()) { if (action == m_action){ - if (m_shape->hover(pos)){ + if (m_shape->hover(pos)) m_callback->exec(); -// getEngine().getShell()->out("hover"); - } } } } diff --git a/src/scene/gui/buttonnode.h b/src/scene/gui/buttonnode.h index d36ae82..f2f44a8 100644 --- a/src/scene/gui/buttonnode.h +++ b/src/scene/gui/buttonnode.h @@ -14,11 +14,11 @@ class ButtonNode : public GraphicalContainerNode glm::vec2 m_position; ButtonShape* m_shape; CallBack* m_callback; - BackGroundNode* m_background; + // BackGroundNode* m_background; public: ButtonNode(glm::vec2 m_position, ButtonShape* shape); - void setBackGround(BackGroundNode*); - BackGroundNode* getBackGround(); +// void setBackGround(BackGroundNode*); + MeshNode* getBackGround(); void setCallBack(CallBack* callback){m_callback=callback;} void setAction(int action){m_action=action;} void update(); diff --git a/src/scene/gui/buttonshape.cpp b/src/scene/gui/buttonshape.cpp index 051e6f0..e2f998a 100644 --- a/src/scene/gui/buttonshape.cpp +++ b/src/scene/gui/buttonshape.cpp @@ -1,9 +1,10 @@ #include "buttonshape.h" #include "glm/common.hpp" +#include "scene/gui/backgroundnode.h" RectangleButtonShape::RectangleButtonShape(glm::vec2 position, glm::vec2 dimension):ButtonShape(position),m_dimension(dimension) { - + m_background = new BackGroundNode(position,dimension,glm::vec3(0,0,0),1,0); } bool RectangleButtonShape::hover(glm::vec2 mouse_position){ @@ -11,3 +12,7 @@ bool RectangleButtonShape::hover(glm::vec2 mouse_position){ return (mouse_position.x >= pos.x && mouse_position.x < pos.x + m_dimension.x) && (mouse_position.y >= pos.y && mouse_position.y < pos.y + m_dimension.y); } + +MeshNode* RectangleButtonShape::getBackGround(){ + return m_background; +} diff --git a/src/scene/gui/buttonshape.h b/src/scene/gui/buttonshape.h index 535c78f..370e062 100644 --- a/src/scene/gui/buttonshape.h +++ b/src/scene/gui/buttonshape.h @@ -3,14 +3,20 @@ #include "glm/vec2.hpp" +class MeshNode; + class ButtonShape { glm::vec2 m_position; +protected: + MeshNode* m_background; + public: ButtonShape(glm::vec2 position) : m_position(position){} virtual bool hover(glm::vec2 mouse_position) = 0; glm::vec2 getPosition(){return m_position;} + virtual MeshNode* getBackGround() = 0; }; class RectangleButtonShape:public ButtonShape @@ -19,6 +25,7 @@ class RectangleButtonShape:public ButtonShape public: RectangleButtonShape(glm::vec2, glm::vec2); bool hover(glm::vec2 mouse_position); + MeshNode* getBackGround(); }; #endif // BUTTONSHAPE_H diff --git a/src/sparrowshell/scriptnode.cpp b/src/sparrowshell/scriptnode.cpp index 032c1d0..7640590 100644 --- a/src/sparrowshell/scriptnode.cpp +++ b/src/sparrowshell/scriptnode.cpp @@ -1,26 +1,29 @@ #include "scriptnode.h" #include "engine.h" #include "sparrowshell/sparrowshell.h" -#include "stdio.h" +#include #define LUASETFUN(var) s.set_function(#var,&ScriptNode::var,this) ScriptNode::ScriptNode() { LUASETFUN(print); - // printf("c is love\n"); // nope } void ScriptNode::update(){ static bool test = true; if(test) - test=false; + test=false; } -void ScriptNode::print(std::string to_prit){ - this->getEngine().getShell()->out(to_prit.append("\n")); +void ScriptNode::print(std::string to_print){ + this->getEngine().getShell()->out(to_print.append("\n")); } void ScriptNode::execute(std::string to_execute){ - s.script(to_execute); + try{ + s.script(to_execute); + }catch(sol::error error_lua){ + std::cerr << error_lua.what() << std::endl; + } } diff --git a/src/sparrowshell/sparrowshell.cpp b/src/sparrowshell/sparrowshell.cpp index f0ffbad..86f09d6 100644 --- a/src/sparrowshell/sparrowshell.cpp +++ b/src/sparrowshell/sparrowshell.cpp @@ -30,8 +30,6 @@ SparrowShell::SparrowShell(sf::Window* window): m_input_cursor_pos(0), m_input_mesh(nullptr) { - // setVisible(false); - sf::Vector2u size = window->getSize(); m_dimension = glm::ivec2(size.x,DEFAULT_FONT_SIZE*(BUFFER_DISPLAYED_LINES+1)); m_buffer->setFontSize(DEFAULT_FONT_SIZE); @@ -42,8 +40,7 @@ SparrowShell::SparrowShell(sf::Window* window): 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.5,SHELL_DEPTH); -// m_background->setVisible(true); + m_background = new BackGroundNode(glm::vec2(0),m_dimension,glm::vec3(0.1,0.1,0.1),0.5,SHELL_DEPTH,false); //Create mesh for scrollbar m_scrollbar = new ShellScrollBar(this); @@ -99,7 +96,6 @@ void SparrowShell::toggleShell() getEngine().getWindow()->setKeyRepeatEnabled(true); } m_shellEnabled = !m_shellEnabled; - //toggleVisibility(); for(auto child : m_children) child->toggleVisibility(); m_buffer->toggleBuffer(); @@ -136,9 +132,9 @@ void SparrowShell::update() void SparrowShell::updateTextInput() { + std::wstring text = getEngine().getInput()->getText(); if (!m_shellEnabled) return; - std::string text = getEngine().getInput()->getText(); for(unsigned int i = 0 ; i < text.length() ; i++){ char c = text[i]; switch(c){ @@ -154,16 +150,12 @@ void SparrowShell::updateTextInput() m_input_string.clear(); m_input_cursor_pos = 0; break; + case 127: + m_input_string.erase(m_input_cursor_pos,1); + break; default: m_input_string.insert(m_input_cursor_pos++,std::string(1,c)); } - -// 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) diff --git a/src/test/main.cpp b/src/test/main.cpp index ff19a49..97a1e16 100644 --- a/src/test/main.cpp +++ b/src/test/main.cpp @@ -251,12 +251,12 @@ public: Menu(Engine* engine,Config* config){ m_menu_scene = engine->createScene(); m_button_demo = new ButtonNode(glm::vec2(100,100),new RectangleButtonShape(glm::vec2(100,100),glm::vec2(300,100))); - BackGroundNode* bg = new BackGroundNode(glm::vec2(100,100),glm::vec2(300,100),glm::vec3(1,0,0.5),1,11); - m_button_demo->setBackGround(bg); + //BackGroundNode* bg = new BackGroundNode(glm::vec2(100,100),glm::vec2(300,100),glm::vec3(1,0,0.5),1,11); + //m_button_demo->setBackGround(bg); // m_button_demo->setAction(m_left_click_action); m_menu_scene->getRootObject()->addChild(m_button_demo); - m_menu_scene->getRootObject()->addChild(bg); - bg->setVisible(true); + //m_menu_scene->getRootObject()->addChild(bg); + //bg->setVisible(true); m_button_demo->setVisible(true); } void setLeftClickAction(int action){