Hi ,
I'm new to C++ and i am working on a Simple Game.
I have coded a Button Class but now i got Problems with it because i want to give the Button a Method to execute when it is clicked.
Like this:
button.setExecuteMethod( ExecuteMethod );
I found an Artikel about std::function's but i isnt working for me.
Hope you can help me
Here is my Code (sry it is not that good):
Button.hpp :
#ifndef BUTTON_HPP
#define BUTTON_HPP
//Includes
#include <SFML/Graphics.hpp>
#include <string>
#include <functional>
//Button Class
class Button
{
public:
Button();
Button(int posx , int posy , float sizex , float sizey , std::string text);
virtual ~Button();
void initialize(int posx , int posy , float sizex , float sizey , std::string text);
void draw(sf::RenderWindow &window);
void update(sf::RenderWindow& window , sf::Event event);
//Setter
void setPosition(float x , float y);
void setString(std::string text);
void setExecuter(std::function<void()> executer);
//Getter
sf::Vector2f getPosition(){return bpos;}
sf::Vector2f getSize(){return bsize;}
protected:
private:
//Positions
sf::Vector2f bpos;
sf::Vector2f bsize;
sf::Vector2i mpos;
//Shape
sf::RectangleShape shape;
//Text
sf::Font font;
sf::Text btext;
std::function<void()> execute;
};
#endif // BUTTON_HPP
Button.cpp:
#include "ui/Button.hpp"
Button::Button()
{
}
Button::Button(int posx , int posy , float sizex , float sizey,std::string text)
{
//ctor
initialize(posx,posy,sizex,sizey,text);
}
Button::~Button()
{
}
void Button::initialize(int posx , int posy , float sizex , float sizey , std::string text)
{
shape.setOutlineColor(sf::Color::Yellow);
shape.setFillColor(sf::Color::Red);
if(posx >= 0 && posy >= 0)
{
bpos.x = posx;
bpos.y = posy;
shape.setPosition(bpos);
}
if(sizex >= 0 && sizey >= 0)
{
bsize.x = sizex;
bsize.y = sizey;
shape.setSize(bsize);
}
font.loadFromFile("res/sansation.ttf");
btext.setFont(font);
btext.setPosition(bpos.x+10,bpos.y+10);
btext.setCharacterSize(20);
btext.setString(text);
}
void Button::update(sf::RenderWindow& window , sf::Event event)
{
mpos = sf::Mouse::getPosition(window); //Get Position relativ to the window
if(mpos.x >= bpos.x && mpos.y >= bpos.y && mpos.x <= (bsize.x + bpos.x) && mpos.y <= (bsize.y + bpos.y)) //Mouse Over
{
shape.setOutlineThickness(2);
btext.setColor(sf::Color::Yellow);
}else //Mouse not Over
{
shape.setOutlineThickness(0);
btext.setColor(sf::Color::White);
}
if(event.type == sf::Event::EventType::MouseButtonReleased)
{
if(event.mouseButton.button == sf::Mouse::Left)
{
execute();
}
}
}
void Button::draw(sf::RenderWindow& window)
{
window.draw(shape);
window.draw(btext);
}
void Button::setPosition(float x , float y)
{
shape.setPosition(x,y);
btext.setPosition(x+10,y+10);
}
void Button::setExecuter(std::function<void()> executer)
{
this->execute = executer;
}