#include "Button.h"
Button::Button(float x, float y, float width, float height,
String text, Texture* image,
Color idleColor, Color hoverColor, Color activeColor)
{
ButtonS.setTexture(*image);
ButtonS.setPosition(x, y);
texture = *image;
//ButtonT.setString(text);
ButtonSetup(x, y, width, height, text, idleColor, hoverColor, activeColor);
}
Button::~Button()
{
}
void Button::setFont(Font* b_font)
{
ButtonT.setFont(*b_font);
}
void Button::ButtonSetup(float x, float y, float width, float height, String text, Color idleColor, Color hoverColor, Color activeColor)
{
ButtonState = BTN_IDLE;
ButtonS.setPosition(x, y);
ButtonS.setScale(width / ButtonS.getGlobalBounds().width, height / ButtonS.getGlobalBounds().height);
font->loadFromFile("Resources/Edwardian.ttf");
ButtonT.setFont(*font);
ButtonT.setString(text);
ButtonT.setFillColor(Color::Black);
ButtonT.setPosition(x, y -
;
ButtonT.setCharacterSize(28);
ButtonBox = RectangleShape(Vector2f(width, height));
ButtonBox.setPosition(x, y);
this->activeColor = activeColor;
this->hoverColor = hoverColor;
this->idleColor = idleColor;
}
void Button::unPress()
{
ButtonState = BTN_IDLE;
}
bool Button::isHover()
{
if (ButtonState == BTN_HOVER)
return true;
return false;
}
const bool Button::isPressed() const
{
if (ButtonState == BTN_ACTIVE)
return true;
return false;
}
void Button::ButtonBoxfunc(bool act, Vector2f mousePos, int thick, Color color)
{
ButtonBoxAct = act;
if (ButtonBoxAct)
{
//std::cout << "asd" << std::endl;
ButtonBox.setOutlineColor(color);
ButtonBox.setOutlineThickness(thick);
ButtonState = BTN_IDLE;
if (ButtonBox.getGlobalBounds().contains(mousePos))
{
ButtonState = BTN_HOVER;
if (Mouse::isButtonPressed(Mouse::Left))
{
ButtonState = BTN_ACTIVE;
}
}
if (ButtonState == BTN_IDLE)
ButtonBox.setFillColor(idleColor);
else if (ButtonState == BTN_HOVER)
ButtonBox.setFillColor(hoverColor);
else if (ButtonState == BTN_ACTIVE)
ButtonBox.setFillColor(activeColor);
}
}
void Button::updateEvented(Vector2f mousePos, Event event, bool fullColor)
{
ButtonState = BTN_IDLE;
if (ButtonS.getGlobalBounds().contains(mousePos))
{
ButtonState = BTN_HOVER;
if (event.mouseButton.button == sf::Mouse::Left)
{
ButtonState = BTN_ACTIVE;
}
}
if (fullColor)
{
if (ButtonState == BTN_IDLE)
setColorB(idleColor);
else if (ButtonState == BTN_HOVER)
setColorB(hoverColor);
else if (ButtonState == BTN_ACTIVE)
setColorB(activeColor);
}
else
{
if (ButtonState == BTN_IDLE)
ButtonS.setColor(idleColor);
else if (ButtonState == BTN_HOVER)
ButtonS.setColor(hoverColor);
else if (ButtonState == BTN_ACTIVE)
ButtonS.setColor(activeColor);
}
}
void Button::render(RenderTarget* target)
{
if (ButtonBoxAct)
target->draw(ButtonBox);
//target->draw(ButtonS);
target->draw(ButtonT);
}