bug with positioning

This commit is contained in:
Lendemor 2017-01-29 21:31:07 +01:00
parent 8ae05e3791
commit 07e65e5713
15 changed files with 95 additions and 64 deletions

View File

@ -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()

View File

@ -66,11 +66,6 @@ void GraphicalNode::rotate2D(const glm::vec2 &center, 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)
{

View File

@ -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 &center, 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; }

View File

@ -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();
}
}

View File

@ -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();
};

View File

@ -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(){

View File

@ -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();
};

View File

@ -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

View File

@ -2,7 +2,7 @@
#include "engine.h"
#include "sparrowshell/sparrowshell.h"
#include <iostream>
#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));
}
}

View File

@ -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

View File

@ -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);

View File

@ -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--;}

View File

@ -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();

View File

@ -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

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::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;