2021-09-13 15:53:43 +03:00
|
|
|
//
|
|
|
|
// Created by Иван Ильин on 26.03.2021.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include <utility>
|
2021-10-31 11:39:08 +03:00
|
|
|
|
|
|
|
#include "Window.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 Window::addButton(int x, int y, int w, int h, std::function<void()> click, const std::string &text, double sx,
|
|
|
|
double sy,
|
2021-09-13 15:53:43 +03:00
|
|
|
const std::string &texture, tPos usualState, tPos selectedState, tPos pressedState,
|
2021-10-31 11:39:08 +03:00
|
|
|
const std::string &font, sf::Color textColor) {
|
|
|
|
_buttons.emplace_back(x, y, w, h, std::move(click), text, sx, sy, texture, usualState, selectedState, pressedState,
|
|
|
|
font, textColor);
|
2021-10-03 07:47:05 +03:00
|
|
|
_buttons.back().init();
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
2021-09-19 15:44:31 +03:00
|
|
|
void Window::update() {
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-10-03 07:47:05 +03:00
|
|
|
_screen->setTitle(_name);
|
|
|
|
_screen->drawSprite(_back);
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-10-12 17:12:47 +03:00
|
|
|
Vec2D mousePos = _mouse->getMousePosition();
|
2021-10-28 16:58:02 +03:00
|
|
|
Vec2D dMousePos = mousePos - _prevMousePosition;
|
2021-10-31 19:01:06 +03:00
|
|
|
_prevMousePosition = mousePos;
|
2021-10-31 11:39:08 +03:00
|
|
|
_back.setPosition(_back.getPosition() - sf::Vector2f(static_cast<float>(dMousePos.x()) / 30.0f,
|
|
|
|
static_cast<float>(dMousePos.y()) / 30.0f));
|
2021-09-19 15:44:31 +03:00
|
|
|
bool isPressed = _mouse->isButtonTapped(sf::Mouse::Left);
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-10-31 11:39:08 +03:00
|
|
|
for (auto &button : _buttons) {
|
|
|
|
if (mousePos.x() > button.x() - button.w() * button.sx() / 2.0f &&
|
|
|
|
mousePos.y() > button.y() - button.h() * button.sy() / 2.0f &&
|
|
|
|
mousePos.x() < button.x() + button.w() * button.sx() / 2.0f &&
|
|
|
|
mousePos.y() < button.y() + button.h() * button.sy() / 2.0f) {
|
2021-09-13 15:53:43 +03:00
|
|
|
button.select();
|
2021-10-31 11:39:08 +03:00
|
|
|
if (isPressed)
|
2021-09-13 15:53:43 +03:00
|
|
|
button.press();
|
|
|
|
} else {
|
|
|
|
button.unSelect();
|
|
|
|
}
|
|
|
|
|
2021-10-31 11:39:08 +03:00
|
|
|
if (_screen->isOpen()) {
|
2021-10-03 07:47:05 +03:00
|
|
|
_screen->drawSprite(button.sprite());
|
|
|
|
_screen->drawText(button.text());
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::setBackgroundTexture(const std::string &texture, double sx, double sy, int w, int h) {
|
2021-10-03 07:47:05 +03:00
|
|
|
_backTexture = texture;
|
|
|
|
std::shared_ptr<sf::Texture> t = ResourceManager::loadTexture(_backTexture);
|
2021-09-13 15:53:43 +03:00
|
|
|
t->setRepeated(true);
|
2021-10-31 11:39:08 +03:00
|
|
|
_back = sf::Sprite(*t, sf::IntRect(0, 0, static_cast<int>(w + w / 30.0), static_cast<int>(h + h / 30.0)));
|
|
|
|
_back.scale((float) sx, (float) sy);
|
|
|
|
_back.setPosition(sf::Vector2f(static_cast<float>(-w) / 30.0f, static_cast<float>(-h) / 30.0f));
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|