vectozavr-shooter/engine/gui/Button.cpp

69 lines
2.2 KiB
C++
Raw Normal View History

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;
2021-10-17 19:38:16 +03:00
//_clickSound.play();
2021-10-03 07:47:05 +03:00
_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));
2021-10-09 13:41:12 +03:00
_button.scale((float)_sx, (float)_sy);
_button.setPosition((float)(_x - _w * _sx / 2), (float)(_y - _h * _sy / 2));
2021-10-03 07:47:05 +03:00
_text.setFont(*ResourceManager::loadFont(_font));
_text.setString(_textString);
2021-10-09 13:41:12 +03:00
_text.setCharacterSize((unsigned int)(_h * _sy / 2));
2021-10-03 07:47:05 +03:00
_text.setFillColor(_textColor);
2021-10-09 13:41:12 +03:00
_text.setPosition((float)(_x - _text.getLocalBounds().width / 2), (float)(_y - _h * _sy / 2 + _text.getLocalBounds().height / 4));
2021-10-03 07:47:05 +03:00
2021-10-17 19:38:16 +03:00
//_clickSound.setBuffer(*ResourceManager::loadSoundBuffer(_clickSoundName));
//_clickSound.setVolume(15);
2021-10-03 07:47:05 +03:00
}
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,
2021-10-17 19:38:16 +03:00
std::string font, sf::Color textColor) : _x(x), _y(y), _w(width), _h(height), _click(std::move(click)),
2021-10-03 07:47:05 +03:00
_textString(std::move(text)), _sx(sx), _sy(sy), _texture(std::move(texture)), _usualState(usualState), _selectedState(selectedState), _pressedState(pressedState),
2021-10-17 19:38:16 +03:00
_font(std::move(font)), _textColor(textColor) {
2021-10-03 07:47:05 +03:00
2021-09-13 15:53:43 +03:00
}