Commit f89ba0b3 authored by Евсеев Кирилл Александрович's avatar Евсеев Кирилл Александрович
Browse files

Added buttons

parent 7d366ae3
No related merge requests found
Showing with 142 additions and 0 deletions
+142 -0
#include"Button.h"
#include <functional>
#include <SFML/Graphics.hpp>
#include <iostream>
#include "MyFont.h"
void Button::set(const sf::Vector2f& pos, const sf::Vector2f& size, const std::string& text, const size_t font_size, std::unique_ptr<ISelectCommand> command) {
(*this).shape_.setPosition(pos);
(*this).shape_.setSize(size);
(*this).shape_.setFillColor(sf::Color::Green);
(*this).shape_.setOutlineThickness(10);
(*this).shape_.setOutlineColor(sf::Color::White);
(*this).command_ = std::move(command);
(*this).text_.setFont(MyFont::Instance().getFont());
(*this).text_.setString(text);
(*this).text_.setCharacterSize(font_size);
(*this).text_.setFillColor(sf::Color::White);
(*this).text_.setPosition(pos.x + 80, pos.y + 10);
}
void Button::draw_into(sf::RenderWindow& window) const {
window.draw((*this).shape_);
window.draw((*this).text_);
}
bool Button::is_position_in(const sf::Vector2f pos) {
if (shape_.getGlobalBounds().contains(pos)) {
return 1;
}
}
void Button::push()
{
(*this).command_->execute();
}
#pragma once
#include "ISelectCommand.h"
#include "../../IStateManager.h"
class ChangeStateCommand : public ISelectCommand {
protected:
IStateManager& manager_;
public:
ChangeStateCommand(IStateManager& manager) : manager_(manager) {};
};
\ No newline at end of file
#pragma once
#include "ChangeStateCommand.h"
#include "../../ExitState/ExitState.h"
class ExitCommand : public ChangeStateCommand {
public:
ExitCommand(IStateManager& manager) : ChangeStateCommand(manager) {};
void execute() const override {
(*this).manager_.set_next_state(std::make_unique<ExitState>((*this).manager_));
}
};
\ No newline at end of file
#pragma once
#include "ChangeStateCommand.h"
#include "../GameBuilderDirector.h"
#include "../../IStateManager.h"
class GameCommand : public ChangeStateCommand {
private:
std::unique_ptr<GameBuilderDirector> director_;
public:
GameCommand(IStateManager& manager, std::unique_ptr<GameBuilderDirector> director) : ChangeStateCommand(manager), director_(std::move(director)) {}
};
\ No newline at end of file
#pragma once
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
class ISelectCommand {
public:
ISelectCommand() = default;
void virtual execute() const {};
};
\ No newline at end of file
#pragma once
class GameBuilderDirector {
};
\ No newline at end of file
File added
#pragma once
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <iostream>
class MyFont {
private:
sf::Font font_;
MyFont() {
try {
font_.loadFromFile("C:/Users/2/Downloads/GostARegular.ttf");
}
catch (...) {
std::cout << "Unable to load font\n";
throw;
}
}
public:
static MyFont& Instance() {
static MyFont instance;
return instance;
}
sf::Font& getFont() { return (*this).font_; }
};
\ No newline at end of file
#include"Menu.h"
#include"../Button/Command/ExitCommand.h"
Menu::Menu(IStateManager& manager) {
(*this).buttons[0] = Button();
(*this).buttons[0].set(sf::Vector2f{ 400, 250 }, sf::Vector2f{ 200, 50 }, std::string("EASY"), size_t(24), std::make_unique<ISelectCommand>(ISelectCommand{}));//, ISelectCommand{});
(*this).buttons[1] = Button();
(*this).buttons[1].set(sf::Vector2f{ 400, 350 }, sf::Vector2f{ 200, 50 }, std::string("EXIT"), size_t(24), std::make_unique<ExitCommand>(ExitCommand(manager)));// , ISelectCommand{});
(*this).buttons[2] = Button();
(*this).buttons[2].set(sf::Vector2f{ 400, 150 }, sf::Vector2f{ 200, 50 }, std::string("MEDIUM"), size_t(24), std::make_unique<ISelectCommand>(ISelectCommand{}));
(*this).buttons[3] = Button();
(*this).buttons[3].set(sf::Vector2f{ 400, 50 }, sf::Vector2f{ 200, 50 }, std::string("HARD"), size_t(24), std::make_unique<ISelectCommand>(ISelectCommand{}));
}
void Menu::draw_into(sf::RenderWindow& window) const {
for (int i = 0; i < 4; ++i) {
buttons[i].draw_into(window);
}
}
void Menu::process_mouse(sf::Vector2f pos, bool is_pressed) {
for (auto& button : buttons) {
if (button.is_position_in(pos)) {
button.select();
if (is_pressed) button.push();
}
else {
button.unselect();
}
}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment