fix crash by lua and start reorganizing buttonnode

This commit is contained in:
Lendemor 2017-01-21 19:47:20 +01:00
parent 74aa44a259
commit 64247c75e0
9 changed files with 42 additions and 42 deletions

View File

@ -6,8 +6,8 @@
#include "mesh.h" #include "mesh.h"
#include "phongmaterial.h" #include "phongmaterial.h"
BackGroundNode::BackGroundNode(glm::vec2 position,glm::vec2 dimension, glm::vec3 color, float opacity,float depth): BackGroundNode::BackGroundNode(glm::vec2 position,glm::vec2 dimension, glm::vec3 color, float opacity,float depth,bool visible):
MeshNode(nullptr,false) MeshNode(nullptr,visible)
{ {
Mesh* mesh = new Mesh(); Mesh* mesh = new Mesh();
mesh->addRectangle2D(position,dimension); mesh->addRectangle2D(position,dimension);

View File

@ -6,7 +6,7 @@
class BackGroundNode : public MeshNode class BackGroundNode : public MeshNode
{ {
public: 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 #endif // BACKGROUNDNODE_H

View File

@ -13,16 +13,12 @@
ButtonNode::ButtonNode(glm::vec2 position,ButtonShape* shape): ButtonNode::ButtonNode(glm::vec2 position,ButtonShape* shape):
m_position(position),m_shape(shape) m_position(position),m_shape(shape)
{ {
addChild(m_shape->getBackGround());
} }
void ButtonNode::setBackGround(BackGroundNode* background){ MeshNode* ButtonNode::getBackGround()
m_background = background;
addChild(background);
}
BackGroundNode* ButtonNode::getBackGround()
{ {
return m_background; return m_shape->getBackGround();
} }
void ButtonNode::update() void ButtonNode::update()
@ -30,21 +26,18 @@ void ButtonNode::update()
Input* input = getEngine().getInput(); Input* input = getEngine().getInput();
sf::Vector2i v = input->getPosition(); sf::Vector2i v = input->getPosition();
glm::vec2 pos = glm::vec2(v.x,v.y); glm::vec2 pos = glm::vec2(v.x,v.y);
PhongMaterial* mat = (PhongMaterial*) m_shape->getBackGround()->getGeometryNode()->mesh->getMaterial();
if (m_shape->hover(pos)){ if (m_shape->hover(pos)){
PhongMaterial* mat = (PhongMaterial*) m_background->getGeometryNode()->mesh->getMaterial();
mat->diffuse=glm::vec3(0.2,0.6,0.6); mat->diffuse=glm::vec3(0.2,0.6,0.6);
}else{ }else{
PhongMaterial* mat = (PhongMaterial*) m_background->getGeometryNode()->mesh->getMaterial();
mat->diffuse=glm::vec3(0.6,0.2,0.6); mat->diffuse=glm::vec3(0.6,0.2,0.6);
} }
for (auto action : input->getActions()) for (auto action : input->getActions())
{ {
if (action == m_action){ if (action == m_action){
if (m_shape->hover(pos)){ if (m_shape->hover(pos))
m_callback->exec(); m_callback->exec();
// getEngine().getShell()->out("hover");
}
} }
} }
} }

View File

@ -14,11 +14,11 @@ class ButtonNode : public GraphicalContainerNode
glm::vec2 m_position; glm::vec2 m_position;
ButtonShape* m_shape; ButtonShape* m_shape;
CallBack* m_callback; CallBack* m_callback;
BackGroundNode* m_background; // BackGroundNode* m_background;
public: public:
ButtonNode(glm::vec2 m_position, ButtonShape* shape); ButtonNode(glm::vec2 m_position, ButtonShape* shape);
void setBackGround(BackGroundNode*); // void setBackGround(BackGroundNode*);
BackGroundNode* getBackGround(); MeshNode* getBackGround();
void setCallBack(CallBack* callback){m_callback=callback;} void setCallBack(CallBack* callback){m_callback=callback;}
void setAction(int action){m_action=action;} void setAction(int action){m_action=action;}
void update(); void update();

View File

@ -1,9 +1,10 @@
#include "buttonshape.h" #include "buttonshape.h"
#include "glm/common.hpp" #include "glm/common.hpp"
#include "scene/gui/backgroundnode.h"
RectangleButtonShape::RectangleButtonShape(glm::vec2 position, glm::vec2 dimension):ButtonShape(position),m_dimension(dimension) 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){ 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) 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); && (mouse_position.y >= pos.y && mouse_position.y < pos.y + m_dimension.y);
} }
MeshNode* RectangleButtonShape::getBackGround(){
return m_background;
}

View File

@ -3,14 +3,20 @@
#include "glm/vec2.hpp" #include "glm/vec2.hpp"
class MeshNode;
class ButtonShape class ButtonShape
{ {
glm::vec2 m_position; glm::vec2 m_position;
protected:
MeshNode* m_background;
public: public:
ButtonShape(glm::vec2 position) : m_position(position){} ButtonShape(glm::vec2 position) : m_position(position){}
virtual bool hover(glm::vec2 mouse_position) = 0; virtual bool hover(glm::vec2 mouse_position) = 0;
glm::vec2 getPosition(){return m_position;} glm::vec2 getPosition(){return m_position;}
virtual MeshNode* getBackGround() = 0;
}; };
class RectangleButtonShape:public ButtonShape class RectangleButtonShape:public ButtonShape
@ -19,6 +25,7 @@ class RectangleButtonShape:public ButtonShape
public: public:
RectangleButtonShape(glm::vec2, glm::vec2); RectangleButtonShape(glm::vec2, glm::vec2);
bool hover(glm::vec2 mouse_position); bool hover(glm::vec2 mouse_position);
MeshNode* getBackGround();
}; };
#endif // BUTTONSHAPE_H #endif // BUTTONSHAPE_H

View File

@ -1,26 +1,29 @@
#include "scriptnode.h" #include "scriptnode.h"
#include "engine.h" #include "engine.h"
#include "sparrowshell/sparrowshell.h" #include "sparrowshell/sparrowshell.h"
#include "stdio.h" #include <iostream>
#define LUASETFUN(var) s.set_function(#var,&ScriptNode::var,this) #define LUASETFUN(var) s.set_function(#var,&ScriptNode::var,this)
ScriptNode::ScriptNode() ScriptNode::ScriptNode()
{ {
LUASETFUN(print); LUASETFUN(print);
// printf("c is love\n"); // nope
} }
void ScriptNode::update(){ void ScriptNode::update(){
static bool test = true; static bool test = true;
if(test) if(test)
test=false; test=false;
} }
void ScriptNode::print(std::string to_prit){ void ScriptNode::print(std::string to_print){
this->getEngine().getShell()->out(to_prit.append("\n")); this->getEngine().getShell()->out(to_print.append("\n"));
} }
void ScriptNode::execute(std::string to_execute){ 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;
}
} }

View File

@ -30,8 +30,6 @@ SparrowShell::SparrowShell(sf::Window* window):
m_input_cursor_pos(0), m_input_cursor_pos(0),
m_input_mesh(nullptr) m_input_mesh(nullptr)
{ {
// setVisible(false);
sf::Vector2u size = window->getSize(); sf::Vector2u size = window->getSize();
m_dimension = glm::ivec2(size.x,DEFAULT_FONT_SIZE*(BUFFER_DISPLAYED_LINES+1)); m_dimension = glm::ivec2(size.x,DEFAULT_FONT_SIZE*(BUFFER_DISPLAYED_LINES+1));
m_buffer->setFontSize(DEFAULT_FONT_SIZE); m_buffer->setFontSize(DEFAULT_FONT_SIZE);
@ -42,8 +40,7 @@ SparrowShell::SparrowShell(sf::Window* window):
RESOURCE_ADD(fonte_des_neiges,Font,"shellfont"); RESOURCE_ADD(fonte_des_neiges,Font,"shellfont");
//Create mesh for background //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 = new BackGroundNode(glm::vec2(0),m_dimension,glm::vec3(0.1,0.1,0.1),0.5,SHELL_DEPTH,false);
// m_background->setVisible(true);
//Create mesh for scrollbar //Create mesh for scrollbar
m_scrollbar = new ShellScrollBar(this); m_scrollbar = new ShellScrollBar(this);
@ -99,7 +96,6 @@ void SparrowShell::toggleShell()
getEngine().getWindow()->setKeyRepeatEnabled(true); getEngine().getWindow()->setKeyRepeatEnabled(true);
} }
m_shellEnabled = !m_shellEnabled; m_shellEnabled = !m_shellEnabled;
//toggleVisibility();
for(auto child : m_children) for(auto child : m_children)
child->toggleVisibility(); child->toggleVisibility();
m_buffer->toggleBuffer(); m_buffer->toggleBuffer();
@ -136,9 +132,9 @@ void SparrowShell::update()
void SparrowShell::updateTextInput() void SparrowShell::updateTextInput()
{ {
std::wstring text = getEngine().getInput()->getText();
if (!m_shellEnabled) if (!m_shellEnabled)
return; return;
std::string text = getEngine().getInput()->getText();
for(unsigned int i = 0 ; i < text.length() ; i++){ for(unsigned int i = 0 ; i < text.length() ; i++){
char c = text[i]; char c = text[i];
switch(c){ switch(c){
@ -154,16 +150,12 @@ void SparrowShell::updateTextInput()
m_input_string.clear(); m_input_string.clear();
m_input_cursor_pos = 0; m_input_cursor_pos = 0;
break; break;
case 127:
m_input_string.erase(m_input_cursor_pos,1);
break;
default: default:
m_input_string.insert(m_input_cursor_pos++,std::string(1,c)); 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"); Font *shellfont = RESOURCE_GET(Font,"shellfont");
if(m_input_mesh) if(m_input_mesh)

View File

@ -251,12 +251,12 @@ public:
Menu(Engine* engine,Config* config){ Menu(Engine* engine,Config* config){
m_menu_scene = engine->createScene(); m_menu_scene = engine->createScene();
m_button_demo = new ButtonNode(glm::vec2(100,100),new RectangleButtonShape(glm::vec2(100,100),glm::vec2(300,100))); 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); //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->setBackGround(bg);
// m_button_demo->setAction(m_left_click_action); // m_button_demo->setAction(m_left_click_action);
m_menu_scene->getRootObject()->addChild(m_button_demo); m_menu_scene->getRootObject()->addChild(m_button_demo);
m_menu_scene->getRootObject()->addChild(bg); //m_menu_scene->getRootObject()->addChild(bg);
bg->setVisible(true); //bg->setVisible(true);
m_button_demo->setVisible(true); m_button_demo->setVisible(true);
} }
void setLeftClickAction(int action){ void setLeftClickAction(int action){