From 592f1883aae1585604bffb10a00f05136ded2625 Mon Sep 17 00:00:00 2001 From: NaiJi Date: Thu, 26 Aug 2021 20:41:16 +0300 Subject: [PATCH] Implement fonts --- include/application.h | 4 ++++ include/tools/resourceholder.h | 6 +++--- src/application.cpp | 11 +++++++---- src/gui/editor.cpp | 9 +++++---- src/gui/editor.h | 4 ++-- src/gui/mainmenu.cpp | 6 +++--- src/gui/mainmenu.h | 3 ++- src/gui/widgets/button.cpp | 7 ++++++- src/gui/widgets/button.h | 3 ++- src/gui/widgets/cascademenubutton.cpp | 4 ++-- src/gui/widgets/cascademenubutton.h | 2 +- src/gui/widgets/menubar.cpp | 5 +++-- src/gui/widgets/menubar.h | 3 ++- src/gui/widgets/pushbutton.cpp | 4 ++-- src/gui/widgets/pushbutton.h | 2 +- 15 files changed, 45 insertions(+), 28 deletions(-) diff --git a/include/application.h b/include/application.h index 52dc57f..1702e7a 100644 --- a/include/application.h +++ b/include/application.h @@ -10,6 +10,8 @@ #include "game/game.h" #include "gui/state.h" +#include "tools/resourceholder.h" + class Application { public: @@ -29,6 +31,8 @@ private: void exec(); void pushState(GUIState::Tag new_state); void popState(); + + FontHolder _font_holder; }; #endif // APPLICATION_H diff --git a/include/tools/resourceholder.h b/include/tools/resourceholder.h index 5c057cf..7f11589 100644 --- a/include/tools/resourceholder.h +++ b/include/tools/resourceholder.h @@ -20,14 +20,14 @@ public: return true; } - Resource& get(Id id) const + const std::shared_ptr& get(Id id) const { const auto found = _resources.find(id); - return *found->second; + return found->second; } private: - std::map> _resources; + std::map> _resources; }; namespace Fonts diff --git a/src/application.cpp b/src/application.cpp index c53d90b..0c1a1df 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -15,9 +15,12 @@ const sf::Time TIME_PER_FRAME = sf::seconds(1.f / 90.f); Application::Application() : - _game_window({1280, 720}, "Test", sf::Style::Default), - _game(std::make_unique(std::make_unique(_game_window), std::make_unique())) + _game_window({1280, 720}, "Test", sf::Style::Default) { + _font_holder.load(Fonts::Id::GUI, "SourceCodePro-Regular.ttf"); + + _game = std::make_unique(std::make_unique(_game_window), std::make_unique()); + _game_window.setFramerateLimit(60); _game_window.setKeyRepeatEnabled(false); _game_window.setMouseCursorGrabbed(false); @@ -26,9 +29,9 @@ Application::Application() : MainMenu::Callbacks callbacks = {[&](){ pushState(GUIState::Tag::EDITOR); }}; Editor::Callbacks editor_callbacks = {[&](){ popState(); }}; - const auto main_menu = std::make_shared(_game_window, std::move(callbacks)); + const auto main_menu = std::make_shared(_game_window, std::move(callbacks), _font_holder); const auto game_state = std::make_shared(_game_window, _game, GameState::Callbacks()); - const auto editor = std::make_shared(_game_window, std::move(editor_callbacks), std::make_unique()); + const auto editor = std::make_shared(_game_window, std::move(editor_callbacks), std::make_unique(), _font_holder); _states[GUIState::Tag::MAIN_MENU] = main_menu; _states[GUIState::Tag::GAME] = game_state; _states[GUIState::Tag::EDITOR] = editor; diff --git a/src/gui/editor.cpp b/src/gui/editor.cpp index 64e85c0..a1afbfa 100644 --- a/src/gui/editor.cpp +++ b/src/gui/editor.cpp @@ -6,8 +6,7 @@ #include -Editor::Editor(sf::RenderWindow& game_window, Callbacks&& callbacks, std::unique_ptr&& music) : - _menu_bar(std::make_shared()), +Editor::Editor(sf::RenderWindow& game_window, Callbacks&& callbacks, std::unique_ptr&& music, const FontHolder& font_holder) : _game_window(game_window), _music(std::move(music)), _bpm_calculator(std::make_unique(_music)) @@ -16,13 +15,15 @@ Editor::Editor(sf::RenderWindow& game_window, Callbacks&& callbacks, std::unique const float window_width = game_window.getSize().x; //const float window_height = game_window.getSize().y; - auto bpm_button = std::make_shared("Calculate BPM"); + _menu_bar = std::make_shared(font_holder.get(Fonts::Id::GUI)); + + auto bpm_button = std::make_shared("Play song :)", font_holder.get(Fonts::Id::GUI)); bpm_button->setCallback([&]() { _bpm_calculator->startListening(0); }); - auto quit_button = std::make_shared("Quit"); + auto quit_button = std::make_shared("Quit", font_holder.get(Fonts::Id::GUI)); quit_button->setCallback(callbacks.onLeaveEditorState); auto test_menu = std::make_shared(); diff --git a/src/gui/editor.h b/src/gui/editor.h index e99c4e2..faa36d0 100644 --- a/src/gui/editor.h +++ b/src/gui/editor.h @@ -2,7 +2,7 @@ #include "gui/state.h" #include "tools/music.h" - +#include "tools/resourceholder.h" #include class MenuBar; @@ -17,7 +17,7 @@ public: std::function onLeaveEditorState; }; - explicit Editor(sf::RenderWindow& game_window, Callbacks&& callbacks, std::unique_ptr&& music); + explicit Editor(sf::RenderWindow& game_window, Callbacks&& callbacks, std::unique_ptr&& music, const FontHolder& font_holder); virtual void input(const sf::Event& event) override; virtual void update() override; virtual void draw() const override; diff --git a/src/gui/mainmenu.cpp b/src/gui/mainmenu.cpp index 21e3616..ec6e43e 100644 --- a/src/gui/mainmenu.cpp +++ b/src/gui/mainmenu.cpp @@ -2,18 +2,18 @@ #include "widgets/pushbutton.h" #include "widgets/group.h" -MainMenu::MainMenu(sf::RenderWindow& game_window, Callbacks&& callbacks) : +MainMenu::MainMenu(sf::RenderWindow& game_window, Callbacks&& callbacks, const FontHolder& font_holder) : _buttons(std::make_shared()), _game_window(game_window) { const float window_width = game_window.getSize().x; const float window_height = game_window.getSize().y; - std::shared_ptr button_start = std::make_shared("Start"); + std::shared_ptr button_start = std::make_shared("Start", font_holder.get(Fonts::Id::GUI), 48); button_start->setRect(sf::FloatRect(window_width / 3., window_height / 7. * 2, window_width / 3., window_height / 7.)); button_start->setCallback(callbacks.onAppendGameState); - std::shared_ptr button_exit = std::make_shared("Exit"); + std::shared_ptr button_exit = std::make_shared("Exit", font_holder.get(Fonts::Id::GUI), 48); button_exit->setRect(sf::FloatRect(window_width / 3., window_height / 7. * 4, window_width / 3., window_height / 7.)); button_exit->setCallback([&]() { diff --git a/src/gui/mainmenu.h b/src/gui/mainmenu.h index b37e7e0..c79e37d 100644 --- a/src/gui/mainmenu.h +++ b/src/gui/mainmenu.h @@ -1,6 +1,7 @@ #pragma once #include "gui/state.h" +#include "tools/resourceholder.h" #include class Group; @@ -14,7 +15,7 @@ public: std::function onAppendGameState; }; - explicit MainMenu(sf::RenderWindow& game_window, Callbacks&& callbacks); + explicit MainMenu(sf::RenderWindow& game_window, Callbacks&& callbacks, const FontHolder &font_holder); virtual void input(const sf::Event& event) override; virtual void update() override; virtual void draw() const override; diff --git a/src/gui/widgets/button.cpp b/src/gui/widgets/button.cpp index 951f673..47c189f 100644 --- a/src/gui/widgets/button.cpp +++ b/src/gui/widgets/button.cpp @@ -1,10 +1,14 @@ #include "button.h" -Button::Button(const std::string &text) +Button::Button(const std::string &text, const std::shared_ptr& font, unsigned int font_size) : + _font(font) { setText(text); _button_text.setFillColor(sf::Color::Black); + _button_text.setCharacterSize(font_size); + _button_text.setFont(*_font); _button_content.setFillColor(sf::Color::White); + } void Button::update() @@ -27,6 +31,7 @@ void Button::setRect(const sf::FloatRect& rect) { _button_content.setPosition(rect.left, rect.top); _button_content.setSize({rect.width, rect.height}); + _button_text.setPosition(rect.left + 5, rect.top + 5); } void Button::setPosition(const sf::Vector2f &position) diff --git a/src/gui/widgets/button.h b/src/gui/widgets/button.h index d61d977..0209916 100644 --- a/src/gui/widgets/button.h +++ b/src/gui/widgets/button.h @@ -8,7 +8,7 @@ class Button : public Widget { public: - explicit Button(const std::string& text); + explicit Button(const std::string& text, const std::shared_ptr& font, unsigned int font_size); virtual void input(const sf::Event& event) override = 0; virtual void update() override final; @@ -23,5 +23,6 @@ public: protected: sf::RectangleShape _button_content; sf::Text _button_text; + std::shared_ptr _font; }; diff --git a/src/gui/widgets/cascademenubutton.cpp b/src/gui/widgets/cascademenubutton.cpp index 957bb41..a783550 100644 --- a/src/gui/widgets/cascademenubutton.cpp +++ b/src/gui/widgets/cascademenubutton.cpp @@ -1,8 +1,8 @@ #include "cascademenubutton.h" #include "menudrop.h" -CascadeMenuButton::CascadeMenuButton(const std::string& text) : - Button(text) +CascadeMenuButton::CascadeMenuButton(const std::string& text, const std::shared_ptr &font, unsigned int font_size) : + Button(text, font, font_size) {} void CascadeMenuButton::input(const sf::Event& event) diff --git a/src/gui/widgets/cascademenubutton.h b/src/gui/widgets/cascademenubutton.h index 6f46a47..ddff58c 100644 --- a/src/gui/widgets/cascademenubutton.h +++ b/src/gui/widgets/cascademenubutton.h @@ -7,7 +7,7 @@ class MenuDrop; class CascadeMenuButton : public Button { public: - explicit CascadeMenuButton(const std::string& text); + explicit CascadeMenuButton(const std::string& text, const std::shared_ptr& font, unsigned int font_size = 12); virtual void input(const sf::Event& event) override final; virtual void setRect(const sf::FloatRect& rect) override final; diff --git a/src/gui/widgets/menubar.cpp b/src/gui/widgets/menubar.cpp index 5760336..d9f65c3 100644 --- a/src/gui/widgets/menubar.cpp +++ b/src/gui/widgets/menubar.cpp @@ -1,7 +1,8 @@ #include "menubar.h" #include "iostream" -MenuBar::MenuBar() : +MenuBar::MenuBar(const std::shared_ptr& font) : + _font(font), _button_width(170) { _bar_rect.setFillColor(sf::Color(88, 57, 107)); @@ -67,7 +68,7 @@ bool MenuBar::isUnderMouse(int mouse_x, int mouse_y) const void MenuBar::addSubMenu(std::string name, const std::shared_ptr& submenu) { - const auto new_button = std::make_shared(name); + const auto new_button = std::make_shared(name, _font); std::size_t current_index = _amount_buttons; new_button->setRect(sf::FloatRect(current_index * _button_width, 0, _button_width, _bar_rect.getSize().y)); diff --git a/src/gui/widgets/menubar.h b/src/gui/widgets/menubar.h index dba2115..60151ab 100644 --- a/src/gui/widgets/menubar.h +++ b/src/gui/widgets/menubar.h @@ -9,7 +9,7 @@ class MenuBar : public Widget { public: - explicit MenuBar(); + explicit MenuBar(const std::shared_ptr& font); virtual void input(const sf::Event& event) override; virtual void update() override; @@ -22,6 +22,7 @@ public: void addSubMenu(std::string name, const std::shared_ptr& submenu); private: + std::shared_ptr _font; sf::RectangleShape _bar_rect; std::size_t _amount_buttons; std::size_t _button_width; diff --git a/src/gui/widgets/pushbutton.cpp b/src/gui/widgets/pushbutton.cpp index 384f350..1893823 100644 --- a/src/gui/widgets/pushbutton.cpp +++ b/src/gui/widgets/pushbutton.cpp @@ -1,7 +1,7 @@ #include "pushbutton.h" -PushButton::PushButton(const std::string& text) : - Button(text), +PushButton::PushButton(const std::string& text, const std::shared_ptr &font, unsigned int font_size) : + Button(text, font, font_size), _pressed(false) { _color_idle = sf::Color(230, 230, 230); diff --git a/src/gui/widgets/pushbutton.h b/src/gui/widgets/pushbutton.h index a5340da..a243d79 100644 --- a/src/gui/widgets/pushbutton.h +++ b/src/gui/widgets/pushbutton.h @@ -5,7 +5,7 @@ class PushButton : public Button { public: - explicit PushButton(const std::string& text); + explicit PushButton(const std::string& text, const std::shared_ptr& font, unsigned int font_size = 12); virtual void input(const sf::Event& event) override final; void setCallback(std::function callback);