2021-09-13 15:53:43 +03:00
|
|
|
//
|
|
|
|
// Created by Иван Ильин on 26.03.2021.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "Window.h"
|
|
|
|
|
|
|
|
#include <utility>
|
|
|
|
#include "ResourceManager.h"
|
|
|
|
|
|
|
|
void Window::addButton(int x, int y, int w, int h, std::function<void()> click, const std::string &text, double sx, double sy,
|
|
|
|
const std::string &texture, tPos usualState, tPos selectedState, tPos pressedState,
|
|
|
|
const std::string& font, sf::Color textColor, const std::string& clickSound) {
|
|
|
|
buttons.push_back(Button{x, y, w, h, std::move(click), text, sx, sy, texture, usualState, selectedState, pressedState, font, textColor, clickSound});
|
|
|
|
buttons.back().init();
|
|
|
|
}
|
|
|
|
|
2021-09-19 15:44:31 +03:00
|
|
|
void Window::update() {
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-09-19 15:44:31 +03:00
|
|
|
_screen->setTitle(s_name);
|
|
|
|
_screen->drawSprite(back);
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-09-19 15:44:31 +03:00
|
|
|
Point4D mousePos = _mouse->getMousePosition();
|
2021-09-13 15:53:43 +03:00
|
|
|
Point4D dMousePos = mousePos - prevMousePosition;
|
|
|
|
back.setPosition(back.getPosition() - sf::Vector2f(dMousePos.x()/30, dMousePos.y()/30));
|
2021-09-19 15:44:31 +03:00
|
|
|
bool isPressed = _mouse->isButtonTapped(sf::Mouse::Left);
|
2021-09-13 15:53:43 +03:00
|
|
|
|
|
|
|
for(auto& button : buttons) {
|
|
|
|
if( mousePos.x() > button.x - button.w*button.sx/2 && mousePos.y() > button.y - button.h*button.sy/2 &&
|
|
|
|
mousePos.x() < button.x + button.w*button.sx/2 && mousePos.y() < button.y + button.h*button.sy/2) {
|
|
|
|
button.select();
|
|
|
|
if(isPressed)
|
|
|
|
button.press();
|
|
|
|
} else {
|
|
|
|
button.unSelect();
|
|
|
|
}
|
|
|
|
|
2021-09-19 15:44:31 +03:00
|
|
|
if(_screen->isOpen()) {
|
|
|
|
_screen->drawSprite(button.button);
|
|
|
|
_screen->drawText(button.text);
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
prevMousePosition = mousePos;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::setBackgroundTexture(const std::string &texture, double sx, double sy, int w, int h) {
|
|
|
|
s_backTexture = texture;
|
|
|
|
std::shared_ptr<sf::Texture> t = ResourceManager::loadTexture(s_backTexture);
|
|
|
|
t->setRepeated(true);
|
|
|
|
back = sf::Sprite(*t, sf::IntRect(0, 0, w + w/30.0, h + h/30.0));
|
|
|
|
back.scale(sx, sy);
|
|
|
|
back.setPosition(sf::Vector2f(-w/30.0, -h/30.0));
|
|
|
|
}
|