An error occurred while loading the file. Please try again.
-
Adam Reese authorede43286b6
#include "buttonmenu.hpp"
#include "config.hpp"
void Button::draw_into(sf::RenderWindow& window) const {
window.draw(m_rectangle);
window.draw(m_text);
}
void Button::set(sf::Vector2f pos, sf::Vector2f button_size, std::string text,
size_t font_size, std::unique_ptr<ISelectCommand> ptr_command) {
m_rectangle.setSize(button_size);
m_rectangle.setOrigin(button_size / 2.0f);
m_rectangle.setPosition(pos);
m_rectangle.setOutlineThickness(config::BUTTON_FRAME_THICKNESS);
m_rectangle.setOutlineColor(config::BUTTON_COLOR_FRAME);
m_rectangle.setFillColor(config::BUTTON_COLOR_FILL); //TODO:
m_text.setString(text);
m_text.setFont(MyFont::Instance());
m_text.setFillColor(config::BUTTON_COLOR_TEXT);
m_text.setCharacterSize(font_size);
auto bounds = m_text.getLocalBounds();
m_text.setOrigin(bounds.getPosition() + bounds.getSize() / 2.0f);
m_text.setPosition(pos);
m_ptr_command = std::move(ptr_command);
}
void Button::select() {
m_rectangle.setFillColor(config::BUTTON_COLOR_SELECTION);
}
void Button::unselect() {
m_rectangle.setFillColor(config::BUTTON_COLOR_FILL);
}
bool Button::is_position_in(sf::Vector2f pos) {
sf::Vector2f diff = pos - m_rectangle.getPosition();
return abs(diff.x) < m_rectangle.getSize().x / 2.0f && abs(diff.y) < m_rectangle.getSize().y / 2.0f;
}
void Button::push() {
m_ptr_command->execute();
}
Menu::Menu(IStateManager& stateManager): m_state_manager(stateManager) {
float vertical_padding = config::BUTTON_SIZE.x / 10.0f;
float first_pos = (config::SELECT_LEVEL_VIDEO_MODE.height - (m_buttons.size() - 1) * vertical_padding - m_buttons.size() * config::BUTTON_SIZE.y) / 2 + config::BUTTON_SIZE.y / 2;
float hor_pos = config::SELECT_LEVEL_VIDEO_MODE.width / 2.0f;
sf::Vector2f pos = sf::Vector2f(hor_pos, first_pos);
m_buttons[0].set(pos, config::BUTTON_SIZE, config::BUTTON_TEXT_EASY,
config::BUTTON_FONT_SIZE, std::make_unique<ExitCommand>(stateManager));
pos.y += vertical_padding + config::BUTTON_SIZE.y;
m_buttons[1].set(pos, config::BUTTON_SIZE, config::BUTTON_TEXT_MEDIUM,
config::BUTTON_FONT_SIZE, std::make_unique<ExitCommand>(stateManager));
pos.y += vertical_padding + config::BUTTON_SIZE.y;
m_buttons[2].set(pos, config::BUTTON_SIZE, config::BUTTON_TEXT_HARD,
config::BUTTON_FONT_SIZE, std::make_unique<ExitCommand>(stateManager));
pos.y += vertical_padding + config::BUTTON_SIZE.y;
m_buttons[3].set(pos, config::BUTTON_SIZE, config::BUTTON_TEXT_EXIT,
config::BUTTON_FONT_SIZE, std::make_unique<ExitCommand>(stateManager));
}
void Menu::draw_into(sf::RenderWindow& window) const {
for (auto& button: m_buttons) {
button.draw_into(window);
}
}
717273747576777879808182
void Menu::process_mouse(sf::Vector2f pos, bool is_pressed) {
for (auto& button: m_buttons) {
if (button.is_position_in(pos)) {
button.select();
if (is_pressed) {
button.push();
}
} else {
button.unselect();
}
}
}