vectozavr-shooter/engine/gui/Button.cpp

63 lines
2.4 KiB
C++
Raw Normal View History

2021-09-13 15:53:43 +03:00
//
// Created by Иван Ильин on 26.03.2021.
//
2021-10-03 07:47:05 +03:00
#include <utility>
2021-10-31 11:39:08 +03:00
#include "Button.h"
2022-07-11 16:58:05 +03:00
#include "../utils/ResourceManager.h"
2021-09-13 15:53:43 +03:00
2021-10-31 11:39:08 +03:00
void Button::select() {
2021-10-28 16:58:02 +03:00
if (!_selected && !_pressed) {
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
}
}
2021-10-31 11:39:08 +03:00
void Button::unSelect() {
2021-10-28 16:58:02 +03:00
if (_selected && !_pressed) {
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
}
}
2021-10-31 11:39:08 +03:00
void Button::press() {
2021-10-28 16:58:02 +03:00
if (!_pressed) {
2021-10-03 07:47:05 +03:00
_button.setTextureRect(sf::IntRect(_pressedState.tx, _pressedState.ty, _w, _h));
2021-10-31 11:39:08 +03:00
if (_checkBox) {
2021-10-03 07:47:05 +03:00
_pressed = true;
2021-10-28 16:58:02 +03:00
}
2021-10-03 07:47:05 +03:00
_click();
2021-10-28 16:58:02 +03:00
} else {
2021-10-03 07:47:05 +03:00
_button.setTextureRect(sf::IntRect(_usualState.tx, _usualState.ty, _w, _h));
2021-10-31 11:39:08 +03:00
if (_checkBox) {
2021-10-03 07:47:05 +03:00
_pressed = false;
2021-10-28 16:58:02 +03:00
}
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-31 11:39:08 +03:00
_button.scale(static_cast<float>(_sx), static_cast<float>(_sy));
_button.setPosition(static_cast<float>(_x) - static_cast<float>(_w * _sx) / 2.0f,
static_cast<float>(_y) - static_cast<float>(_h * _sy) / 2.0f);
2021-10-03 07:47:05 +03:00
_text.setFont(*ResourceManager::loadFont(_font));
_text.setString(_textString);
2021-10-31 11:39:08 +03:00
_text.setCharacterSize(static_cast<unsigned int>((_h * _sy) / 2));
2021-10-03 07:47:05 +03:00
_text.setFillColor(_textColor);
2021-10-31 11:39:08 +03:00
_text.setPosition(static_cast<float>(_x) - _text.getLocalBounds().width / 2.0f,
static_cast<float>(_y) - static_cast<float>(_h * _sy) / 2.0f + _text.getLocalBounds().height / 4.0f);
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-31 11:39:08 +03:00
_textString(std::move(text)), _sx(sx), _sy(sy),
_texture(std::move(texture)), _usualState(usualState),
_selectedState(selectedState), _pressedState(pressedState),
_font(std::move(font)), _textColor(textColor) {
2021-10-03 07:47:05 +03:00
2021-09-13 15:53:43 +03:00
}