From 07e65e5713ae01dcc2c1c30c1e6bba9871856b46 Mon Sep 17 00:00:00 2001 From: Lendemor Date: Sun, 29 Jan 2017 21:31:07 +0100 Subject: [PATCH] bug with positioning --- src/engine.cpp | 4 +++- src/scene/graphicalnode.cpp | 5 ----- src/scene/graphicalnode.h | 2 +- src/scene/gui/buttonnode.cpp | 31 ++++++++++++++++++++++++------- src/scene/gui/buttonnode.h | 13 +++++++++++-- src/scene/gui/buttonshape.cpp | 11 +++++------ src/scene/gui/buttonshape.h | 11 +++++------ src/scene/textnode.h | 6 +++--- src/sparrowshell/scriptnode.cpp | 19 +++++++++++++------ src/sparrowshell/scriptnode.h | 4 ++-- src/sparrowshell/sparrowshell.cpp | 27 +++++++++++++++------------ src/sparrowshell/sparrowshell.h | 8 ++++---- src/test/main.cpp | 12 ++++++------ src/tools/font.cpp | 4 ++-- src/tools/font.h | 2 +- 15 files changed, 95 insertions(+), 64 deletions(-) diff --git a/src/engine.cpp b/src/engine.cpp index 7467ca3..245cbf3 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -194,7 +194,9 @@ void Engine::setShowMouseAction(int action) void Engine::outputShell(std::string str) const { - m_sparrowshell->out(str); + std::wstring ws; + ws.assign(str.begin(),str.end()); + m_sparrowshell->out(ws); } void Engine::checkSpecialInputs() diff --git a/src/scene/graphicalnode.cpp b/src/scene/graphicalnode.cpp index cbdae05..d91b7b7 100644 --- a/src/scene/graphicalnode.cpp +++ b/src/scene/graphicalnode.cpp @@ -66,11 +66,6 @@ void GraphicalNode::rotate2D(const glm::vec2 ¢er, float angle) setTransform(glm::rotate(m_transform,angle,glm::vec3(0,0,1))); } -void GraphicalNode::setDepth2D(float depth) -{ - // don't have access to depth here? :( -} - // setters void GraphicalNode::setTransform(const glm::mat4 &transform) { diff --git a/src/scene/graphicalnode.h b/src/scene/graphicalnode.h index 260d891..6d2a707 100644 --- a/src/scene/graphicalnode.h +++ b/src/scene/graphicalnode.h @@ -49,6 +49,7 @@ public: void setTransform(const glm::mat4 &transform); const glm::mat4& getTransform() { return m_transform; } void setParentTransform(const glm::mat4 &transform); + const glm::mat4& getParentTransform() { return m_parentTransform; } // visibility methods bool isVisible(){return m_parentVisible && m_visible;} @@ -68,7 +69,6 @@ public: void rotate2D(const glm::vec2 ¢er, float angle); void scale2D(const glm::vec2 scaleFactor); void resize2D(const glm::vec2 oldDimension, glm::vec2 newDimension); - void setDepth2D(float depth); // this is used to synchronize a bullet rigidbody's transform with a GraphicalNode transform SparrowMotionState* getMotionState() { return &m_motionState; } diff --git a/src/scene/gui/buttonnode.cpp b/src/scene/gui/buttonnode.cpp index ffca147..a161386 100644 --- a/src/scene/gui/buttonnode.cpp +++ b/src/scene/gui/buttonnode.cpp @@ -9,9 +9,12 @@ #include "sparrowshell/sparrowshell.h" #include "mesh.h" #include "phongmaterial.h" +#include "tools/font.h" +#include "resourcemanager.h" +#include "sparrowrenderer.h" ButtonNode::ButtonNode(glm::vec2 position,ButtonShape* shape): - m_position(position),m_shape(shape) + m_position(position),m_shape(shape),i(0) { addChild(m_shape->getBackGround()); } @@ -26,17 +29,31 @@ void ButtonNode::update() Input* input = getEngine().getInput(); sf::Vector2i v = input->getPosition(); glm::vec2 pos = glm::vec2(v.x,v.y); - PhongMaterial* mat = (PhongMaterial*) m_shape->getBackGround()->getGeometryNode()->mesh->getMaterial(); - if (m_shape->hover(pos)){ - mat->diffuse=glm::vec3(0.2,0.6,0.6); - }else{ - mat->diffuse=glm::vec3(0.6,0.2,0.6); + + 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); + getEngine().getScene()->updateShaders(); + m_label_updated = false; } + if(i == 120){ + glm::vec4 posa = m_transform[3]; + glm::vec4 posb = getBackGround()->getParentTransform()[3]; + glm::vec4 posc = m_label_node->getParentTransform()[3]; + std::cout << posa.x << " " << posa.y <<"\n"<< posb.x << " " << posb.y << "\n" << posc.x << " " << posc.y << "\n"<< std::endl; + i=0; + } + i++; for (auto action : input->getActions()) { if (action == m_action){ - if (m_shape->hover(pos)) + if (m_shape->hover(m_position,pos)) m_callback->exec(); } } diff --git a/src/scene/gui/buttonnode.h b/src/scene/gui/buttonnode.h index f2f44a8..7010f74 100644 --- a/src/scene/gui/buttonnode.h +++ b/src/scene/gui/buttonnode.h @@ -4,6 +4,7 @@ #include "scene/meshnode.h" #include "scene/graphicalcontainernode.h" #include "scene/gui/callback.h" +#include "scene/textnode.h" class ButtonShape; class BackGroundNode; @@ -14,13 +15,21 @@ class ButtonNode : public GraphicalContainerNode glm::vec2 m_position; ButtonShape* m_shape; CallBack* m_callback; - // BackGroundNode* m_background; + + std::string m_label; + bool m_label_updated; + TextNode* m_label_node; + glm::vec3 m_label_color; + + int i; + public: ButtonNode(glm::vec2 m_position, ButtonShape* shape); -// void setBackGround(BackGroundNode*); MeshNode* getBackGround(); 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;} void update(); }; diff --git a/src/scene/gui/buttonshape.cpp b/src/scene/gui/buttonshape.cpp index e2f998a..0829b05 100644 --- a/src/scene/gui/buttonshape.cpp +++ b/src/scene/gui/buttonshape.cpp @@ -2,15 +2,14 @@ #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 dimension):ButtonShape(),m_dimension(dimension) { - m_background = new BackGroundNode(position,dimension,glm::vec3(0,0,0),1,0); + m_background = new BackGroundNode(glm::vec2(0,0),dimension,glm::vec3(0.2,0.6,0.2),1,0); } -bool RectangleButtonShape::hover(glm::vec2 mouse_position){ - glm::vec2 pos = getPosition(); - 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); +bool RectangleButtonShape::hover(glm::vec2 button_position, glm::vec2 mouse_position){ + return (mouse_position.x >= button_position.x && mouse_position.x < button_position.x + m_dimension.x) + && (mouse_position.y >= button_position.y && mouse_position.y < button_position.y + m_dimension.y); } MeshNode* RectangleButtonShape::getBackGround(){ diff --git a/src/scene/gui/buttonshape.h b/src/scene/gui/buttonshape.h index 370e062..5112e95 100644 --- a/src/scene/gui/buttonshape.h +++ b/src/scene/gui/buttonshape.h @@ -7,15 +7,14 @@ class MeshNode; class ButtonShape { - glm::vec2 m_position; protected: MeshNode* m_background; public: - ButtonShape(glm::vec2 position) : m_position(position){} + ButtonShape(){} - virtual bool hover(glm::vec2 mouse_position) = 0; - glm::vec2 getPosition(){return m_position;} + virtual bool hover(glm::vec2 button_position, glm::vec2 mouse_position) = 0; +// glm::vec2 getPosition(){return m_position;} virtual MeshNode* getBackGround() = 0; }; @@ -23,8 +22,8 @@ class RectangleButtonShape:public ButtonShape { glm::vec2 m_dimension; public: - RectangleButtonShape(glm::vec2, glm::vec2); - bool hover(glm::vec2 mouse_position); + RectangleButtonShape(glm::vec2); + bool hover(glm::vec2 button_position, glm::vec2 mouse_position); MeshNode* getBackGround(); }; diff --git a/src/scene/textnode.h b/src/scene/textnode.h index f77ea64..c1303fe 100644 --- a/src/scene/textnode.h +++ b/src/scene/textnode.h @@ -6,12 +6,12 @@ class TextNode : public MeshNode { private: - std::string m_string; + std::wstring m_string; float m_fontSize; public: - TextNode(Mesh* mesh,std::string 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) {} float getFontSize(){return m_fontSize;} - std::string getString(){return m_string;} + std::wstring getString(){return m_string;} }; #endif // TEXTNODE_H diff --git a/src/sparrowshell/scriptnode.cpp b/src/sparrowshell/scriptnode.cpp index 040d4fe..55fe30c 100644 --- a/src/sparrowshell/scriptnode.cpp +++ b/src/sparrowshell/scriptnode.cpp @@ -2,7 +2,7 @@ #include "engine.h" #include "sparrowshell/sparrowshell.h" #include -#define LUASETFUN(var) s.set_function(#var,&ScriptNode::var,this) +#define LUASETFUN(var) m_script.set_function(#var,&ScriptNode::var,this) ScriptNode::ScriptNode() { @@ -16,14 +16,21 @@ void ScriptNode::update(){ } void ScriptNode::print(std::string to_print){ - this->getEngine().getShell()->out(to_print.append("\n")); + std::wstring ws; + to_print.append("\n"); + ws.assign(to_print.begin(),to_print.end()); + this->getEngine().getShell()->out(ws); } - -void ScriptNode::execute(std::string to_execute){ +void ScriptNode::execute(std::wstring to_execute){ try{ - s.script(to_execute); + std::string str; + str.assign(to_execute.begin(),to_execute.end()); + m_script.script(str); }catch(sol::error error_lua){ - this->getEngine().getShell()->out(error_lua.what(),glm::vec3(1,0,0)); + std::string s = error_lua.what(); + std::wstring ws; + ws.assign(s.begin(),s.end()); + this->getEngine().getShell()->out(ws,glm::vec3(1,0,0)); } } diff --git a/src/sparrowshell/scriptnode.h b/src/sparrowshell/scriptnode.h index 2c5a702..412db13 100644 --- a/src/sparrowshell/scriptnode.h +++ b/src/sparrowshell/scriptnode.h @@ -7,13 +7,13 @@ class ScriptNode : public GraphicalNode { - sol::state s; + sol::state m_script; public: ScriptNode(); void update(); void print(std::string); - void execute(std::string); + void execute(std::wstring); }; #endif // SCRIPTNODE_H diff --git a/src/sparrowshell/sparrowshell.cpp b/src/sparrowshell/sparrowshell.cpp index 67ab3a5..5dff1e1 100644 --- a/src/sparrowshell/sparrowshell.cpp +++ b/src/sparrowshell/sparrowshell.cpp @@ -52,13 +52,13 @@ SparrowShell::SparrowShell(sf::Window* window): this->addChild(m_script); } -// write string str in shell -void SparrowShell::out(std::string str) +// write wstring str in shell +void SparrowShell::out(std::wstring str) { out(str,m_text_color); } -void SparrowShell::out(std::string str,glm::vec3 color) +void SparrowShell::out(std::wstring str,glm::vec3 color) { Font *shellfont = RESOURCE_GET(Font,"shellfont"); TextNode* tnode = shellfont->getTextNode(str,color,m_buffer->getFontSize(),false); @@ -109,9 +109,7 @@ void SparrowShell::update() if(m_shellEnabled){ auto input = getEngine().getInput(); for(auto action : input->getActions()){ - if(action == m_plop_test){ - out("Plop"); - }else if (action == m_move_cursor_left){ + if (action == m_move_cursor_left){ if (m_input_cursor_pos > 0) moveCursorLeft(); } @@ -133,6 +131,7 @@ void SparrowShell::update() void SparrowShell::updateTextInput() { std::wstring text = getEngine().getInput()->getText(); + bool input_string_updated = false; if (!m_shellEnabled) return; for(unsigned int i = 0 ; i < text.length() ; i++){ @@ -141,27 +140,31 @@ void SparrowShell::updateTextInput() case 8: if(m_input_cursor_pos > 0) m_input_string.erase(--m_input_cursor_pos,1); + input_string_updated = true; break; case 13: - if (m_input_string != ""){ + if (!m_input_string.empty()){ out(m_input_string); m_script->execute(m_input_string); + m_input_string.clear(); + input_string_updated = true; } - m_input_string.clear(); m_input_cursor_pos = 0; break; case 127: m_input_string.erase(m_input_cursor_pos,1); + input_string_updated = true; break; default: - m_input_string.insert(m_input_cursor_pos++,std::string(1,c)); + m_input_string.insert(m_input_cursor_pos++,std::wstring(1,c)); + input_string_updated = true; } } Font *shellfont = RESOURCE_GET(Font,"shellfont"); - if(m_input_mesh) - this->removeChild(m_input_mesh); - if(m_input_string != "") + if(input_string_updated) { + if(m_input_mesh) + this->removeChild(m_input_mesh); 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); diff --git a/src/sparrowshell/sparrowshell.h b/src/sparrowshell/sparrowshell.h index 1e7ff81..3971919 100644 --- a/src/sparrowshell/sparrowshell.h +++ b/src/sparrowshell/sparrowshell.h @@ -36,7 +36,7 @@ private: ScriptNode * m_script; glm::vec3 m_text_color; - std::string m_input_string; + std::wstring m_input_string; unsigned int m_input_cursor_pos; TextNode* m_input_mesh; @@ -61,8 +61,8 @@ public: void scrollDown(); void toggleShell(); - void out(std::string str); - void out(std::string str, glm::vec3 color); + void out(std::wstring str); + void out(std::wstring str, glm::vec3 color); glm::ivec2 getPosition(){return m_position;} glm::ivec2 getDimension(){return m_dimension;} @@ -72,7 +72,7 @@ public: void setMoveCursorLeftAction(int action){m_move_cursor_left = action;} void setMoveCursorRightAction(int action){m_move_cursor_right = action;} - void setPlopTest(int action){m_plop_test = action;} +// void setPlopTest(int action){m_plop_test = action;} void setClearConsoleAction(int action){m_clear_console_action = action;} void moveCursorLeft(){m_input_cursor_pos--;} diff --git a/src/test/main.cpp b/src/test/main.cpp index 4b1ad36..12538b9 100644 --- a/src/test/main.cpp +++ b/src/test/main.cpp @@ -250,15 +250,15 @@ 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(100,100),glm::vec2(300,100))); + 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->setBackGround(bg); -// m_button_demo->setAction(m_left_click_action); + 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_menu_scene->getRootObject()->addChild(bg); - //bg->setVisible(true); + m_button_demo->moveTo2D(glm::vec2(100,100)); m_button_demo->setVisible(true); } + void setLeftClickAction(int action){ m_left_click_action = action; m_button_demo->setAction(m_left_click_action); @@ -307,7 +307,7 @@ int main(){ SparrowShell* shell = engine.getShell(); shell->setMoveCursorLeftAction(DefaultKeysMap::MOVE_CURSOR_LEFT); shell->setMoveCursorRightAction(DefaultKeysMap::MOVE_CURSOR_RIGHT); - shell->setPlopTest(DefaultKeysMap::PLOP_TEST); + //shell->setPlopTest(DefaultKeysMap::PLOP_TEST); shell->setClearConsoleAction(DefaultKeysMap::CLEAR_CONSOLE); input->addContext(Context("shell",DefaultKeysMap::getShellContext())); input->updateKeyBindings(); diff --git a/src/tools/font.cpp b/src/tools/font.cpp index 01cd919..d657a43 100644 --- a/src/tools/font.cpp +++ b/src/tools/font.cpp @@ -8,13 +8,13 @@ Font::Font() } -TextNode* Font::getTextNode(std::string s, glm::vec3 color, float font_size,bool visible) +TextNode* Font::getTextNode(std::wstring s, glm::vec3 color, float font_size,bool visible) { Mesh* textmesh = new Mesh(); glm::vec2 current_pos(0.f); float sizeRatio = font_size / m_defaultLineHeight; - for(char c : s){ + for(wchar_t c : s){ if(c == '\n') { current_pos.x = 0.f; // left alignment -> TODO : be able to center or align right diff --git a/src/tools/font.h b/src/tools/font.h index 9f2b88d..92ebd54 100644 --- a/src/tools/font.h +++ b/src/tools/font.h @@ -31,7 +31,7 @@ public: void setScale(glm::vec2 scale){m_scale = scale;} void setTexture(Texture *tex){m_tex = tex;} - TextNode* getTextNode(std::string s, glm::vec3 color = glm::vec3(1), float font_size = 64.f, bool visible = true); + TextNode* getTextNode(std::wstring s, glm::vec3 color = glm::vec3(1), float font_size = 64.f, bool visible = true); private: std::string m_name; Texture *m_tex;