better positionning for button node

This commit is contained in:
Lendemor 2017-02-01 19:30:40 +01:00
parent 66f0cf9a7c
commit 8955256356
7 changed files with 30 additions and 2 deletions

View File

@ -42,6 +42,9 @@ void ButtonNode::update()
ws.assign(m_label.begin(),m_label.end()); ws.assign(m_label.begin(),m_label.end());
m_label_node = font->getTextNode(ws,m_label_color,32); m_label_node = font->getTextNode(ws,m_label_color,32);
addChild(m_label_node); 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);
getEngine().getScene()->updateShaders(); getEngine().getScene()->updateShaders();
m_label_updated = false; m_label_updated = false;
} }
@ -54,3 +57,7 @@ void ButtonNode::update()
} }
} }
} }
glm::vec2 ButtonNode::getDimension(){
return m_shape->getDimension();
}

View File

@ -19,11 +19,13 @@ class ButtonNode : public GraphicalContainerNode
std::string m_label; std::string m_label;
bool m_label_updated; bool m_label_updated;
TextNode* m_label_node; TextNode* m_label_node;
glm::vec2 m_label_position;
glm::vec3 m_label_color; glm::vec3 m_label_color;
public: public:
ButtonNode(glm::vec2 m_position, ButtonShape* shape); ButtonNode(glm::vec2 m_position, ButtonShape* shape);
MeshNode* getBackGround(); MeshNode* getBackGround();
glm::vec2 getDimension();
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 setLabel(std::string label){m_label = label; m_label_updated = true;} void setLabel(std::string label){m_label = label; m_label_updated = true;}

View File

@ -15,3 +15,7 @@ bool RectangleButtonShape::hover(glm::vec2 button_position, glm::vec2 mouse_posi
MeshNode* RectangleButtonShape::getBackGround(){ MeshNode* RectangleButtonShape::getBackGround(){
return m_background; return m_background;
} }
glm::vec2 RectangleButtonShape::getDimension(){
return m_dimension;
}

View File

@ -16,6 +16,7 @@ public:
virtual bool hover(glm::vec2 button_position, glm::vec2 mouse_position) = 0; virtual bool hover(glm::vec2 button_position, glm::vec2 mouse_position) = 0;
// glm::vec2 getPosition(){return m_position;} // glm::vec2 getPosition(){return m_position;}
virtual MeshNode* getBackGround() = 0; virtual MeshNode* getBackGround() = 0;
virtual glm::vec2 getDimension() = 0;
}; };
class RectangleButtonShape:public ButtonShape class RectangleButtonShape:public ButtonShape
@ -25,6 +26,7 @@ public:
RectangleButtonShape(glm::vec2); RectangleButtonShape(glm::vec2);
bool hover(glm::vec2 button_position, glm::vec2 mouse_position); bool hover(glm::vec2 button_position, glm::vec2 mouse_position);
MeshNode* getBackGround(); MeshNode* getBackGround();
glm::vec2 getDimension();
}; };
#endif // BUTTONSHAPE_H #endif // BUTTONSHAPE_H

View File

@ -8,8 +8,13 @@ class TextNode : public MeshNode
private: private:
std::wstring m_string; std::wstring m_string;
float m_fontSize; float m_fontSize;
glm::vec2 m_dimension;
public: 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::wstring s,float fontSize,bool visible = true) : MeshNode(mesh,visible),m_string(s),m_fontSize(fontSize) {}
void setDimension(glm::vec2 dim){m_dimension = dim;}
glm::vec2 getDimension(){return m_dimension;}
float getFontSize(){return m_fontSize;} float getFontSize(){return m_fontSize;}
std::wstring getString(){return m_string;} std::wstring getString(){return m_string;}
}; };

View File

@ -255,7 +255,9 @@ public:
m_button_demo->setLabel("Start DEMO"); m_button_demo->setLabel("Start DEMO");
m_button_demo->setLabelColor(glm::vec3(0.9,0.4,0.3)); m_button_demo->setLabelColor(glm::vec3(0.9,0.4,0.3));
m_menu_scene->getRootObject()->addChild(m_button_demo); m_menu_scene->getRootObject()->addChild(m_button_demo);
m_button_demo->moveTo2D(glm::vec2(100,100)); 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->setVisible(true); m_button_demo->setVisible(true);
} }

View File

@ -12,6 +12,7 @@ TextNode* Font::getTextNode(std::wstring s, glm::vec3 color, float font_size,boo
{ {
Mesh* textmesh = new Mesh(); Mesh* textmesh = new Mesh();
glm::vec2 dimension(0,m_defaultLineHeight);
glm::vec2 current_pos(0.f); glm::vec2 current_pos(0.f);
float sizeRatio = font_size / m_defaultLineHeight; float sizeRatio = font_size / m_defaultLineHeight;
for(wchar_t c : s){ for(wchar_t c : s){
@ -19,6 +20,9 @@ TextNode* Font::getTextNode(std::wstring s, glm::vec3 color, float font_size,boo
{ {
current_pos.x = 0.f; // left alignment -> TODO : be able to center or align right current_pos.x = 0.f; // left alignment -> TODO : be able to center or align right
current_pos.y += m_defaultLineHeight; current_pos.y += m_defaultLineHeight;
if (dimension.x < current_pos.x)
dimension.x = current_pos.x;
dimension.y += m_defaultLineHeight;
} }
else else
{ {
@ -28,7 +32,6 @@ TextNode* Font::getTextNode(std::wstring s, glm::vec3 color, float font_size,boo
charInfo.pos * m_scale, charInfo.pos * m_scale,
charInfo.dim * m_scale charInfo.dim * m_scale
); );
current_pos.x += charInfo.xadvance; current_pos.x += charInfo.xadvance;
} }
} }
@ -39,5 +42,8 @@ TextNode* Font::getTextNode(std::wstring s, glm::vec3 color, float font_size,boo
textmesh->setMaterial((Material*)mat); textmesh->setMaterial((Material*)mat);
textmesh->initGL(); textmesh->initGL();
TextNode *text = new TextNode(textmesh,s,font_size,visible); TextNode *text = new TextNode(textmesh,s,font_size,visible);
if (dimension.x < current_pos.x)
dimension.x = current_pos.x;
text->setDimension(dimension * sizeRatio);
return text; return text;
} }