better positionning for button node
This commit is contained in:
parent
66f0cf9a7c
commit
8955256356
@ -42,6 +42,9 @@ void ButtonNode::update()
|
||||
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);
|
||||
getEngine().getScene()->updateShaders();
|
||||
m_label_updated = false;
|
||||
}
|
||||
@ -54,3 +57,7 @@ void ButtonNode::update()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
glm::vec2 ButtonNode::getDimension(){
|
||||
return m_shape->getDimension();
|
||||
}
|
||||
|
@ -19,11 +19,13 @@ class ButtonNode : public GraphicalContainerNode
|
||||
std::string m_label;
|
||||
bool m_label_updated;
|
||||
TextNode* m_label_node;
|
||||
glm::vec2 m_label_position;
|
||||
glm::vec3 m_label_color;
|
||||
|
||||
public:
|
||||
ButtonNode(glm::vec2 m_position, 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;}
|
||||
|
@ -15,3 +15,7 @@ bool RectangleButtonShape::hover(glm::vec2 button_position, glm::vec2 mouse_posi
|
||||
MeshNode* RectangleButtonShape::getBackGround(){
|
||||
return m_background;
|
||||
}
|
||||
|
||||
glm::vec2 RectangleButtonShape::getDimension(){
|
||||
return m_dimension;
|
||||
}
|
||||
|
@ -16,6 +16,7 @@ public:
|
||||
virtual bool hover(glm::vec2 button_position, glm::vec2 mouse_position) = 0;
|
||||
// glm::vec2 getPosition(){return m_position;}
|
||||
virtual MeshNode* getBackGround() = 0;
|
||||
virtual glm::vec2 getDimension() = 0;
|
||||
};
|
||||
|
||||
class RectangleButtonShape:public ButtonShape
|
||||
@ -25,6 +26,7 @@ public:
|
||||
RectangleButtonShape(glm::vec2);
|
||||
bool hover(glm::vec2 button_position, glm::vec2 mouse_position);
|
||||
MeshNode* getBackGround();
|
||||
glm::vec2 getDimension();
|
||||
};
|
||||
|
||||
#endif // BUTTONSHAPE_H
|
||||
|
@ -8,8 +8,13 @@ class TextNode : public MeshNode
|
||||
private:
|
||||
std::wstring m_string;
|
||||
float m_fontSize;
|
||||
glm::vec2 m_dimension;
|
||||
public:
|
||||
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;}
|
||||
std::wstring getString(){return m_string;}
|
||||
};
|
||||
|
@ -255,7 +255,9 @@ public:
|
||||
m_button_demo->setLabel("Start DEMO");
|
||||
m_button_demo->setLabelColor(glm::vec3(0.9,0.4,0.3));
|
||||
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);
|
||||
}
|
||||
|
||||
|
@ -12,6 +12,7 @@ TextNode* Font::getTextNode(std::wstring s, glm::vec3 color, float font_size,boo
|
||||
{
|
||||
|
||||
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){
|
||||
@ -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.y += m_defaultLineHeight;
|
||||
if (dimension.x < current_pos.x)
|
||||
dimension.x = current_pos.x;
|
||||
dimension.y += m_defaultLineHeight;
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -28,7 +32,6 @@ TextNode* Font::getTextNode(std::wstring s, glm::vec3 color, float font_size,boo
|
||||
charInfo.pos * m_scale,
|
||||
charInfo.dim * m_scale
|
||||
);
|
||||
|
||||
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->initGL();
|
||||
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;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user