added labelnode and refactor code of button node

This commit is contained in:
Lendemor 2017-02-02 04:32:51 +01:00
parent 8955256356
commit e2b27c8835
11 changed files with 155 additions and 48 deletions

View File

@ -16,10 +16,12 @@
#include "tools/font.h"
#include "resourcemanager.h"
ButtonNode::ButtonNode(glm::vec2 position,ButtonShape* shape):
m_position(position),m_shape(shape)
ButtonNode::ButtonNode(ButtonShape* shape):
m_shape(shape)
{
addChild(m_shape->getBackGround());
m_label = new LabelNode();
addChild(m_label);
}
MeshNode* ButtonNode::getBackGround()
@ -27,37 +29,29 @@ MeshNode* ButtonNode::getBackGround()
return m_shape->getBackGround();
}
glm::vec2 ButtonNode::getDimension(){
return m_shape->getDimension();
}
LabelNode* ButtonNode::getLabel(){
return m_label;
}
void ButtonNode::update()
{
GraphicalContainerNode::update();
GUINode::update();
Input* input = getEngine().getInput();
glm::vec2 pos = input->getPosition();;
if(m_label_updated){
if(m_label_node)
removeChild(m_label_node);
Font* font = RESOURCE_GET(Font,"shellfont");
std::wstring ws;
ws.assign(m_label.begin(),m_label.end());
m_label_node = font->getTextNode(ws,m_label_color,32);
addChild(m_label_node);
m_label_position = m_shape->getDimension()/glm::vec2(2,2) - m_label_node->getDimension()/glm::vec2(2,2);
m_label_node->moveTo2D(m_label_position);
if(m_label->wasUpdated()){
m_label->setPosition(m_shape->getDimension()/glm::vec2(2,2) - m_label->getDimension()/glm::vec2(2,2));
getEngine().getScene()->updateShaders();
m_label_updated = false;
}
for (auto action : input->getActions())
{
if (action == m_action){
if (m_shape->hover(m_position,pos))
if (m_shape->hover(this->getPosition(),input->getPosition()))
m_callback->exec();
}
}
}
glm::vec2 ButtonNode::getDimension(){
return m_shape->getDimension();
}

View File

@ -2,34 +2,28 @@
#define BUTTONNODE_H
#include "scene/meshnode.h"
#include "scene/graphicalcontainernode.h"
#include "scene/gui/guinode.h"
#include "scene/gui/callback.h"
#include "scene/textnode.h"
#include "scene/gui/labelnode.h"
class ButtonShape;
class BackGroundNode;
class ButtonNode : public GraphicalContainerNode
class ButtonNode : public GUINode
{
int m_action;
glm::vec2 m_position;
ButtonShape* m_shape;
CallBack* m_callback;
std::string m_label;
bool m_label_updated;
TextNode* m_label_node;
LabelNode* m_label;
glm::vec2 m_label_position;
glm::vec3 m_label_color;
public:
ButtonNode(glm::vec2 m_position, ButtonShape* shape);
ButtonNode(ButtonShape* shape);
MeshNode* getBackGround();
glm::vec2 getDimension();
void setCallBack(CallBack* callback){m_callback=callback;}
void setAction(int action){m_action=action;}
void setLabel(std::string label){m_label = label; m_label_updated = true;}
void setLabelColor(glm::vec3 color){m_label_color=color;}
LabelNode* getLabel();
void update();
};

18
src/scene/gui/guinode.cpp Normal file
View File

@ -0,0 +1,18 @@
#include "guinode.h"
GUINode::GUINode()
{
}
void GUINode::setPosition(glm::vec2 position){
moveTo2D(position);
}
glm::vec2 GUINode::getPosition(){
return glm::vec2(m_transform[3].x,m_transform[3].y);
}
void GUINode::update(){
GraphicalContainerNode::update();
}

16
src/scene/gui/guinode.h Normal file
View File

@ -0,0 +1,16 @@
#ifndef GUINODE_H
#define GUINODE_H
#include "scene/graphicalcontainernode.h"
class GUINode : public GraphicalContainerNode
{
public:
GUINode();
void update();
glm::vec2 getPosition();
void setPosition(glm::vec2 position);
virtual glm::vec2 getDimension() = 0;
};
#endif // GUINODE_H

View File

@ -0,0 +1,54 @@
#include "labelnode.h"
#include "tools/font.h"
#include "resourcemanager.h"
#include "SparrowRenderer/mesh.h"
#include "SparrowRenderer/phongmaterial.h"
LabelNode::LabelNode(){
Font* font = RESOURCE_GET(Font,"shellfont");
m_text = font->getTextNode(m_string,m_color,32);
addChild(m_text);
}
void LabelNode::setText(std::string s)
{
m_string = s;
m_string_updated = true;
}
void LabelNode::setColor(glm::vec3 color){
m_color= color;
m_color_updated = true;
}
glm::vec2 LabelNode::getDimension(){
return m_text->getDimension();
}
bool LabelNode::wasUpdated(){
return m_was_updated;
}
void LabelNode::update(){
GUINode::update();
if(m_was_updated)
m_was_updated = false;
if(m_string_updated){
removeChild(m_text);
delete(m_text);
Font* font = RESOURCE_GET(Font,"shellfont");
m_text = font->getTextNode(m_string,m_color,32);
addChild(m_text);
m_string_updated= false;
m_color_updated=false;
m_was_updated=true;
}
if(m_color_updated){
if(m_text){
PhongMaterial* mat = (PhongMaterial*) m_text->m_geometry.mesh->getMaterial();
mat->diffuse = m_color;
m_color_updated = false;
}
}
}

25
src/scene/gui/labelnode.h Normal file
View File

@ -0,0 +1,25 @@
#ifndef LABELNODE_H
#define LABELNODE_H
#include "scene/textnode.h"
#include "scene/gui/guinode.h"
class LabelNode : public GUINode
{
TextNode* m_text;
glm::vec3 m_color;
std::string m_string;
bool m_string_updated;
bool m_color_updated;
bool m_was_updated;
public:
LabelNode();
void update();
void setText(std::string s);
void setColor(glm::vec3 color);
glm::vec2 getDimension();
bool wasUpdated();
};
#endif // LABELNODE_H

View File

@ -2,6 +2,7 @@
#define TEXTNODE_H
#include "meshnode.h"
#include "glm/vec2.hpp"
class TextNode : public MeshNode
{
@ -9,8 +10,13 @@ private:
std::wstring m_string;
float m_fontSize;
glm::vec2 m_dimension;
friend class LabelNode;
public:
TextNode(Mesh* mesh,std::wstring s,float fontSize,bool visible = true) : MeshNode(mesh,visible),m_string(s),m_fontSize(fontSize) {}
TextNode(Mesh* mesh,std::string s,float fontSize,bool visible = true) : MeshNode(mesh,visible),m_fontSize(fontSize) {
m_string.assign(s.begin(),s.end());
}
void setDimension(glm::vec2 dim){m_dimension = dim;}
glm::vec2 getDimension(){return m_dimension;}

View File

@ -58,10 +58,10 @@ void SparrowShell::out(std::string str)
void SparrowShell::out(std::string str,glm::vec3 color)
{
std::wstring ws;
ws.assign(str.begin(),str.end());
// std::wstring ws;
// ws.assign(str.begin(),str.end());
Font *shellfont = RESOURCE_GET(Font,"shellfont");
TextNode* tnode = shellfont->getTextNode(ws,color,m_buffer->getFontSize(),false);
TextNode* tnode = shellfont->getTextNode(str,color,m_buffer->getFontSize(),false);
tnode->setDepth(SHELL_DEPTH+1);
m_buffer->push(tnode);
scrollDown();
@ -168,7 +168,7 @@ void SparrowShell::updateTextInput()
this->removeChild(m_input_mesh);
std::wstring ws;
ws.assign(m_input_string.begin(),m_input_string.end());
m_input_mesh = shellfont->getTextNode(ws,m_text_color,m_buffer->getFontSize(),false);
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);

View File

@ -250,14 +250,13 @@ class Menu {
public:
Menu(Engine* engine,Config* config){
m_menu_scene = engine->createScene();
m_button_demo = new ButtonNode(glm::vec2(100,100),new RectangleButtonShape(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->setLabel("Start DEMO");
m_button_demo->setLabelColor(glm::vec3(0.9,0.4,0.3));
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);
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->moveTo2D(pos);
m_button_demo->setPosition(pos);
m_button_demo->setVisible(true);
}

View File

@ -8,14 +8,15 @@ Font::Font()
}
TextNode* Font::getTextNode(std::wstring s, glm::vec3 color, float font_size,bool visible)
TextNode* Font::getTextNode(std::string s, glm::vec3 color, float font_size,bool visible)
{
std::wstring ws;
ws.assign(s.begin(),s.end());
Mesh* textmesh = new Mesh();
glm::vec2 dimension(0,m_defaultLineHeight);
glm::vec2 current_pos(0.f);
float sizeRatio = font_size / m_defaultLineHeight;
for(wchar_t c : s){
for(wchar_t c : ws){
if(c == '\n')
{
current_pos.x = 0.f; // left alignment -> TODO : be able to center or align right
@ -41,7 +42,7 @@ TextNode* Font::getTextNode(std::wstring s, glm::vec3 color, float font_size,boo
mat->diffuse = color;
textmesh->setMaterial((Material*)mat);
textmesh->initGL();
TextNode *text = new TextNode(textmesh,s,font_size,visible);
TextNode *text = new TextNode(textmesh,ws,font_size,visible);
if (dimension.x < current_pos.x)
dimension.x = current_pos.x;
text->setDimension(dimension * sizeRatio);

View File

@ -31,7 +31,7 @@ public:
void setScale(glm::vec2 scale){m_scale = scale;}
void setTexture(Texture *tex){m_tex = tex;}
TextNode* getTextNode(std::wstring s, glm::vec3 color = glm::vec3(1), float font_size = 64.f, bool visible = true);
TextNode* getTextNode(std::string s, glm::vec3 color = glm::vec3(1), float font_size = 64.f, bool visible = true);
private:
std::string m_name;
Texture *m_tex;