class Button
{
private:
sf::Image iboton[3];
sf::Sprite spr;
sf::Vector2f siz;
sf::Vector2f pos;
sf::String text;
int MouseState;// 0 nada 1 por encima 2 click xD
bool ButtonON;
bool MouseOn;
bool ButtonPush;//boton activo true boton desactivado false
bool ButtonAction; //
public:
Button();
void CreateButton1(sf::Unicode::Text t,float x,float y,float h);
void CreateButton2(sf::Unicode::Text t,float x,float y,float h);
void PrintButton1(sf::RenderWindow &app, sf::Event &ev);
void PrintButton2(sf::RenderWindow &app, sf::Event &ev);
int GetMouseState(sf::RenderWindow &app, sf::Event &ev);
bool ButtonState();
void Reset();
void SetStateButton(bool b);
bool GetStateButton();
bool GetButtonAction();
sf::Vector2f GetPosition();
sf::Vector2f GetSize();
};
Button::Button()
{
ButtonON=true;
}
void Button::CreateButton1(sf::Unicode::Text t,float x,float y,float h)
{
iboton[0].LoadFromFile("img/button1.png");
iboton[0].SetSmooth(false);
iboton[1].LoadFromFile("img/button2.png");
iboton[1].SetSmooth(false);
iboton[2].LoadFromFile("img/button3.png");
iboton[2].SetSmooth(false);
spr.SetPosition(sf::Vector2f(x,y));
pos=spr.GetPosition();
text.SetText(t);
text.SetSize(12);
text.SetColor(sf::Color(0,0,0,255));
text.SetPosition(sf::Vector2f(pos.x+4,pos.y));
siz.x=(text.GetRect().Right-text.GetRect().Left)+8;
siz.y=h;
};
void Button::CreateButton2(sf::Unicode::Text t,float x,float y,float h)
{
iboton[0].LoadFromFile("img/marco.png");
iboton[0].SetSmooth(false);
iboton[1].LoadFromFile("img/button2.png");
iboton[1].SetSmooth(false);
spr.SetPosition(sf::Vector2f(x,y));
pos=spr.GetPosition();
text.SetText(t);
text.SetSize(12);
text.SetColor(sf::Color(0,0,0,255));
text.SetPosition(sf::Vector2f(pos.x+4,pos.y));
siz.x=(text.GetRect().Right-text.GetRect().Left)+8;
siz.y=h;
};
void Button::PrintButton1(sf::RenderWindow &app, sf::Event &ev)
{
if(ButtonON==true){
spr.SetImage(iboton[MouseState]);
if(siz.x<12)siz.x=12;
if(siz.y<12)siz.y=12;
for(int i=0;i< (siz.x-4)/4-1; i++)
{
spr.SetSubRect(sf::IntRect(4,0,8,4));
spr.SetPosition(4+pos.x+(4*i),pos.y);
app.Draw(spr);
}
for(int i=0;i< (siz.x-4)/4-1; i++)
{
for(int j=0;j<(siz.y-4)/4-1;j++)
{
spr.SetSubRect(sf::IntRect(4,4,8,8));
spr.SetPosition(4+pos.x+(4*i),4+pos.y+(4*j));
app.Draw(spr);
}
}
for(int j=0;j<(siz.y-4)/4-1;j++)
{
spr.SetSubRect(sf::IntRect(0,4,4,8));
spr.SetPosition(pos.x,4+pos.y+(4*j));
app.Draw(spr);
spr.SetSubRect(sf::IntRect(8,4,12,8));
spr.SetPosition(pos.x+siz.x-4,4+pos.y+(4*j));
app.Draw(spr);
}
for(int i=0;i< (siz.x-4)/4-1; i++)
{
spr.SetSubRect(sf::IntRect(4,8,8,12));
spr.SetPosition(4+pos.x+(4*i),pos.y+siz.y-4);
app.Draw(spr);
}
spr.SetSubRect(sf::IntRect(0,0,4,4));
spr.SetPosition(pos.x,pos.y);
app.Draw(spr);
spr.SetSubRect(sf::IntRect(8,0,12,4));
spr.SetPosition(pos.x+siz.x-4,pos.y);
app.Draw(spr);
spr.SetSubRect(sf::IntRect(0,8,4,12));
spr.SetPosition(pos.x,pos.y+siz.y-4);
app.Draw(spr);
spr.SetSubRect(sf::IntRect(8,8,12,12));
spr.SetPosition(pos.x+siz.x-4,pos.y+siz.y-4);
app.Draw(spr);
app.Draw(text);
spr.SetPosition(sf::Vector2f(pos.x,pos.y));
}
};
void Button::PrintButton2(sf::RenderWindow &app, sf::Event &evento)
{
if(ButtonON==true){
if(MouseState==1 || MouseState==2)
{
if(MouseState==1)spr.SetImage(iboton[0]);
else if(MouseState==2)spr.SetImage(iboton[1]);
for(int i=0;i< (siz.x-4)/4-1; i++)
{
spr.SetSubRect(sf::IntRect(4,0,8,4));
spr.SetPosition(4+pos.x+(4*i),pos.y);
app.Draw(spr);
}
if(MouseState==2)
{
for(int i=0;i< (siz.x-4)/4-1; i++)
{
for(int j=0;j<(siz.y-4)/4-1;j++)
{
spr.SetSubRect(sf::IntRect(4,4,8,8));
spr.SetPosition(4+pos.x+(4*i),4+pos.y+(4*j));
app.Draw(spr);
}
}
}
for(int j=0;j<(siz.y-4)/4-1;j++)
{
spr.SetSubRect(sf::IntRect(0,4,4,8));
spr.SetPosition(pos.x,4+pos.y+(4*j));
app.Draw(spr);
spr.SetSubRect(sf::IntRect(8,4,12,8));
spr.SetPosition(pos.x+siz.x-4,4+pos.y+(4*j));
app.Draw(spr);
}
for(int i=0;i< (siz.x-4)/4-1; i++)
{
spr.SetSubRect(sf::IntRect(4,8,8,12));
spr.SetPosition(4+pos.x+(4*i),pos.y+siz.y-4);
app.Draw(spr);
}
spr.SetSubRect(sf::IntRect(0,0,4,4));
spr.SetPosition(pos.x,pos.y);
app.Draw(spr);
spr.SetSubRect(sf::IntRect(8,0,12,4));
spr.SetPosition(pos.x+siz.x-4,pos.y);
app.Draw(spr);
spr.SetSubRect(sf::IntRect(0,8,4,12));
spr.SetPosition(pos.x,pos.y+siz.y-4);
app.Draw(spr);
spr.SetSubRect(sf::IntRect(8,8,12,12));
spr.SetPosition(pos.x+siz.x-4,pos.y+siz.y-4);
app.Draw(spr);
}
if(MouseState==0)text.SetColor(sf::Color(0,0,0,255));
if(MouseState==1)text.SetColor(sf::Color(255,0,0,255));
app.Draw(text);
}
};
int Button::GetMouseState(sf::RenderWindow &app, sf::Event &ev)
{
ButtonAction=false;
if( (app.GetInput().GetMouseX() > pos.x) && ( app.GetInput().GetMouseX() < pos.x+siz.x)&&
(app.GetInput().GetMouseY() > pos.y) && ( app.GetInput().GetMouseY() < pos.y+siz.y)){
if(ButtonPush==false)MouseState=1;
if(ev.Type==ev.MouseButtonPressed && ev.MouseButton.Button ==sf::Mouse::Left && ButtonPush==false){
MouseState=2;
ButtonPush=true;
}
if(ev.Type==ev.MouseButtonReleased && ev.MouseButton.Button ==sf::Mouse::Left && ButtonPush==true){
ButtonPush=false;
MouseState=1;
//if(ButtonAction==true)cout << "FAIL";
if(ButtonAction==true)ButtonAction=false;
else ButtonAction=true;
}
}
else {
MouseState=0;
ButtonPush=false;
}
return MouseState;
};
void Button::Reset()
{
ButtonPush=false;
};
bool Button::ButtonState()
{
return ButtonPush;
};
void Button::SetStateButton(bool b)
{
ButtonON=b;
};
bool Button::GetStateButton()
{
return ButtonON;
};
bool Button::GetButtonAction(){
return ButtonAction;
}
sf::Vector2f Button::GetPosition(){
return pos;
};
sf::Vector2f Button::GetSize(){
return siz;
};