diff --git a/source/BasicAbstractions/Button/Button.cpp b/source/BasicAbstractions/Button/Button.cpp new file mode 100644 index 0000000000000000000000000000000000000000..503297936a4f6e120771d4345090acfc144000af --- /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 0000000000000000000000000000000000000000..add9902a1bca7d894cfda77410bddff5fb2353e2 --- /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 9d915e699a56d97703940b928da6b98aa0d3401e..33b64d32e92e96d54af6fed2fbb42cf9c60cad43 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 1f4d1db0a98910d4347524b41a51d1eec43d0a08..942a43bc1c1e8ccb5041fac81f9c219c66fd85b5 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 c0d43718df639bad32ac5d3594e540a27c324c22..8903982ed30ae71a2e3365ba1688a6d14003172f 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: