From 89552563569b7c224f848e28e565a113a710c361 Mon Sep 17 00:00:00 2001 From: Lendemor Date: Wed, 1 Feb 2017 19:30:40 +0100 Subject: [PATCH] better positionning for button node --- src/scene/gui/buttonnode.cpp | 7 +++++++ src/scene/gui/buttonnode.h | 2 ++ src/scene/gui/buttonshape.cpp | 4 ++++ src/scene/gui/buttonshape.h | 2 ++ src/scene/textnode.h | 5 +++++ src/test/main.cpp | 4 +++- src/tools/font.cpp | 8 +++++++- 7 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/scene/gui/buttonnode.cpp b/src/scene/gui/buttonnode.cpp index 4984d9f..14e8eec 100644 --- a/src/scene/gui/buttonnode.cpp +++ b/src/scene/gui/buttonnode.cpp @@ -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(); +} diff --git a/src/scene/gui/buttonnode.h b/src/scene/gui/buttonnode.h index c69eb31..4e0e67e 100644 --- a/src/scene/gui/buttonnode.h +++ b/src/scene/gui/buttonnode.h @@ -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;} diff --git a/src/scene/gui/buttonshape.cpp b/src/scene/gui/buttonshape.cpp index 0829b05..da693e7 100644 --- a/src/scene/gui/buttonshape.cpp +++ b/src/scene/gui/buttonshape.cpp @@ -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; +} diff --git a/src/scene/gui/buttonshape.h b/src/scene/gui/buttonshape.h index 5112e95..04314bb 100644 --- a/src/scene/gui/buttonshape.h +++ b/src/scene/gui/buttonshape.h @@ -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 diff --git a/src/scene/textnode.h b/src/scene/textnode.h index c1303fe..d1088c0 100644 --- a/src/scene/textnode.h +++ b/src/scene/textnode.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;} }; diff --git a/src/test/main.cpp b/src/test/main.cpp index d53b747..9607421 100644 --- a/src/test/main.cpp +++ b/src/test/main.cpp @@ -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); } diff --git a/src/tools/font.cpp b/src/tools/font.cpp index a330cb1..4060a01 100644 --- a/src/tools/font.cpp +++ b/src/tools/font.cpp @@ -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; }