From 91325270c89c04fa4ca11d230caf624a1dc8e7ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D0=B5=D1=87=D0=B5=D0=BD=D0=B8=D0=BD=20=D0=94=D0=B0?= =?UTF-8?q?=D0=BD=D0=B8=D0=BB=D0=B0=20=D0=9C=D0=B8=D1=85=D0=B0=D0=B9=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=D0=B8=D1=87?= <dmpechenin@edu.hse.ru> Date: Mon, 10 Mar 2025 12:43:01 +0300 Subject: [PATCH] Move button to BasicAbstractions --- source/BasicAbstractions/Button/Button.cpp | 45 +++++++++++++++++++ source/BasicAbstractions/Button/Button.h | 18 ++++++++ .../StaticEntities/StaticEntities.cpp | 2 +- source/States/SelectState/SelectState.cpp | 43 ------------------ source/States/SelectState/SelectState.h | 17 +------ 5 files changed, 65 insertions(+), 60 deletions(-) create mode 100644 source/BasicAbstractions/Button/Button.cpp create mode 100644 source/BasicAbstractions/Button/Button.h diff --git a/source/BasicAbstractions/Button/Button.cpp b/source/BasicAbstractions/Button/Button.cpp new file mode 100644 index 0000000..5032979 --- /dev/null +++ b/source/BasicAbstractions/Button/Button.cpp @@ -0,0 +1,45 @@ +#include <BasicAbstractions/Button/Button.h> +#include <Configuration.h> +#include <BasicAbstractions/Font.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> ptr_command) { + m_rectangle.setSize(size); + m_rectangle.setPosition(pos); + m_rectangle.setFillColor(config::BUTTON_COLOR_FILL); + m_rectangle.setOutlineThickness(config::BUTTON_FRAME_THICKNESS); + m_rectangle.setOutlineColor(config::BUTTON_COLOR_FRAME); + m_text.setFont(MyFont::Instance()); + m_text.setCharacterSize(font_size); + m_text.setFillColor(config::BUTTON_COLOR_TEXT); + m_text.setString(text); + const sf::FloatRect text_bounds = m_text.getLocalBounds(); + const sf::Vector2f button_pos = m_rectangle.getPosition(); + const sf::Vector2f button_size = m_rectangle.getSize(); + m_text.setPosition( + round(button_pos.x + (button_size.x - text_bounds.width) / 2 - text_bounds.left), + round(button_pos.y + (button_size.y - text_bounds.height) / 2 - text_bounds.top) + ); + 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(const sf::Vector2f pos) const noexcept { + return pos.x >= m_rectangle.getPosition().x && pos.x <= (m_rectangle.getPosition().x + m_rectangle.getSize().x) + && pos.y >= m_rectangle.getPosition().y && pos.y <= (m_rectangle.getPosition().y + m_rectangle.getSize().y); +} + +void Button::push() const { + m_ptr_command->execute(); +} + +void Button::draw_into(sf::RenderWindow& window) const { + window.draw(m_rectangle); + window.draw(m_text); +} \ No newline at end of file diff --git a/source/BasicAbstractions/Button/Button.h b/source/BasicAbstractions/Button/Button.h new file mode 100644 index 0000000..add9902 --- /dev/null +++ b/source/BasicAbstractions/Button/Button.h @@ -0,0 +1,18 @@ +#pragma once +#include <BasicAbstractions/Command.h> +#include <BasicAbstractions/IDrawable.h> + +class Button: public IDrawable { +public: + Button() = default; + void set(sf::Vector2f pos, sf::Vector2f size, const std::string& text, size_t font_size, std::unique_ptr<ISelectCommand> ptr_command); + void select(); + void unselect(); + bool is_position_in(sf::Vector2f pos) const noexcept; + void push() const; + void draw_into(sf::RenderWindow& window) const override; +private: + sf::Text m_text; + sf::RectangleShape m_rectangle; + std::unique_ptr<ISelectCommand> m_ptr_command; +}; \ No newline at end of file diff --git a/source/States/GameState/Entities/StaticEntities/StaticEntities.cpp b/source/States/GameState/Entities/StaticEntities/StaticEntities.cpp index 9d915e6..33b64d3 100644 --- a/source/States/GameState/Entities/StaticEntities/StaticEntities.cpp +++ b/source/States/GameState/Entities/StaticEntities/StaticEntities.cpp @@ -15,6 +15,6 @@ void Food::draw_into(sf::RenderWindow& window) const { } void Food::prepare_for_drawing() { - m_circle.setPosition(m_ptr_room->get_position()); + m_circle.setPosition(m_ptr_room->get_position()); ///< @todo надо ли? } diff --git a/source/States/SelectState/SelectState.cpp b/source/States/SelectState/SelectState.cpp index 1f4d1db..942a43b 100644 --- a/source/States/SelectState/SelectState.cpp +++ b/source/States/SelectState/SelectState.cpp @@ -1,50 +1,7 @@ #include <States/SelectState/SelectState.h> #include <States/ChangeStateCommand.h> -#include <BasicAbstractions/Font.h> #include <States/GameState/GameBuilder/GameBuilderDirector.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> ptr_command) { - m_rectangle.setSize(size); - m_rectangle.setPosition(pos); - m_rectangle.setFillColor(config::BUTTON_COLOR_FILL); - m_rectangle.setOutlineThickness(config::BUTTON_FRAME_THICKNESS); - m_rectangle.setOutlineColor(config::BUTTON_COLOR_FRAME); - m_text.setFont(MyFont::Instance()); - m_text.setCharacterSize(font_size); - m_text.setFillColor(config::BUTTON_COLOR_TEXT); - m_text.setString(text); - const sf::FloatRect text_bounds = m_text.getLocalBounds(); - const sf::Vector2f button_pos = m_rectangle.getPosition(); - const sf::Vector2f button_size = m_rectangle.getSize(); - m_text.setPosition( - round(button_pos.x + (button_size.x - text_bounds.width) / 2 - text_bounds.left), - round(button_pos.y + (button_size.y - text_bounds.height) / 2 - text_bounds.top) - ); - 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(const sf::Vector2f pos) const noexcept { - return pos.x >= m_rectangle.getPosition().x && pos.x <= (m_rectangle.getPosition().x + m_rectangle.getSize().x) - && pos.y >= m_rectangle.getPosition().y && pos.y <= (m_rectangle.getPosition().y + m_rectangle.getSize().y); -} - -void Button::push() const { - m_ptr_command->execute(); -} - -void Button::draw_into(sf::RenderWindow& window) const { - window.draw(m_rectangle); - window.draw(m_text); -} - Menu::Menu(IStateManager& state_manager) { const float pos_left = (static_cast<float>(config::SELECT_LEVEL_VIDEO_MODE.width) - config::BUTTON_SIZE.x) / 2; const float pos_diff = (static_cast<float>(config::SELECT_LEVEL_VIDEO_MODE.height) - config::BUTTON_SIZE.y * 4) / 10; diff --git a/source/States/SelectState/SelectState.h b/source/States/SelectState/SelectState.h index c0d4371..8903982 100644 --- a/source/States/SelectState/SelectState.h +++ b/source/States/SelectState/SelectState.h @@ -2,22 +2,7 @@ #include <BasicAbstractions/IState.h> #include <BasicAbstractions/IWindowKeeper.h> #include <BasicAbstractions/IDrawable.h> -#include <BasicAbstractions/Command.h> - -class Button: public IDrawable { -public: - Button() = default; - void set(sf::Vector2f pos, sf::Vector2f size, const std::string& text, size_t font_size, std::unique_ptr<ISelectCommand> ptr_command); - void select(); - void unselect(); - bool is_position_in(sf::Vector2f pos) const noexcept; - void push() const; - void draw_into(sf::RenderWindow& window) const override; -private: - sf::Text m_text; - sf::RectangleShape m_rectangle; - std::unique_ptr<ISelectCommand> m_ptr_command; -}; +#include <BasicAbstractions/Button/Button.h> class Menu: public IDrawable { public: -- GitLab