2021-09-13 15:53:43 +03:00
|
|
|
//
|
|
|
|
// Created by Иван Ильин on 26.03.2021.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "Button.h"
|
2021-10-03 07:47:05 +03:00
|
|
|
|
|
|
|
#include <utility>
|
2021-09-13 15:53:43 +03:00
|
|
|
#include "../ResourceManager.h"
|
|
|
|
|
|
|
|
void Button::select()
|
|
|
|
{
|
2021-10-03 07:47:05 +03:00
|
|
|
if (!_selected && !_pressed)
|
2021-09-13 15:53:43 +03:00
|
|
|
{
|
2021-10-03 07:47:05 +03:00
|
|
|
_button.setTextureRect(sf::IntRect(_selectedState.tx, _selectedState.ty, _w, _h));
|
|
|
|
_selected = true;
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::unSelect()
|
|
|
|
{
|
2021-10-03 07:47:05 +03:00
|
|
|
if (_selected && !_pressed)
|
2021-09-13 15:53:43 +03:00
|
|
|
{
|
2021-10-03 07:47:05 +03:00
|
|
|
_button.setTextureRect(sf::IntRect(_usualState.tx, _usualState.ty, _w, _h));
|
|
|
|
_selected = false;
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::press()
|
|
|
|
{
|
2021-10-03 07:47:05 +03:00
|
|
|
if (!_pressed)
|
2021-09-13 15:53:43 +03:00
|
|
|
{
|
2021-10-03 07:47:05 +03:00
|
|
|
_button.setTextureRect(sf::IntRect(_pressedState.tx, _pressedState.ty, _w, _h));
|
|
|
|
if(_checkBox)
|
|
|
|
_pressed = true;
|
|
|
|
_clickSound.play();
|
|
|
|
_click();
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-10-03 07:47:05 +03:00
|
|
|
_button.setTextureRect(sf::IntRect(_usualState.tx, _usualState.ty, _w, _h));
|
|
|
|
if(_checkBox)
|
|
|
|
_pressed = false;
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::init() {
|
2021-10-03 07:47:05 +03:00
|
|
|
_button.setTexture(*ResourceManager::loadTexture(_texture));
|
|
|
|
_button.setTextureRect(sf::IntRect(_usualState.tx, _usualState.ty, _w, _h));
|
|
|
|
_button.scale(_sx, _sy);
|
|
|
|
_button.setPosition(_x - _w * _sx / 2, _y - _h * _sy / 2);
|
|
|
|
|
|
|
|
_text.setFont(*ResourceManager::loadFont(_font));
|
|
|
|
_text.setString(_textString);
|
|
|
|
_text.setCharacterSize(_h * _sy / 2);
|
|
|
|
_text.setFillColor(_textColor);
|
|
|
|
_text.setPosition(_x - _text.getLocalBounds().width / 2, _y - _h * _sy / 2 + _text.getLocalBounds().height / 4);
|
|
|
|
|
|
|
|
_clickSound.setBuffer(*ResourceManager::loadSoundBuffer(_clickSoundName));
|
|
|
|
_clickSound.setVolume(15);
|
|
|
|
}
|
|
|
|
|
|
|
|
Button::Button(int x, int y, int width, int height, std::function<void()> click, std::string text, double sx,
|
|
|
|
double sy, std::string texture, tPos usualState, tPos selectedState, tPos pressedState,
|
|
|
|
std::string font, sf::Color textColor, std::string clickSound) : _x(x), _y(y), _w(width), _h(height), _click(std::move(click)),
|
|
|
|
_textString(std::move(text)), _sx(sx), _sy(sy), _texture(std::move(texture)), _usualState(usualState), _selectedState(selectedState), _pressedState(pressedState),
|
|
|
|
_font(std::move(font)), _textColor(textColor), _clickSoundName(std::move(clickSound)){
|
|
|
|
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|