Implement fonts

This commit is contained in:
NaiJi ✨ 2021-08-26 20:41:16 +03:00
parent 37391cfdfd
commit 592f1883aa
15 changed files with 45 additions and 28 deletions

View File

@ -10,6 +10,8 @@
#include "game/game.h" #include "game/game.h"
#include "gui/state.h" #include "gui/state.h"
#include "tools/resourceholder.h"
class Application class Application
{ {
public: public:
@ -29,6 +31,8 @@ private:
void exec(); void exec();
void pushState(GUIState::Tag new_state); void pushState(GUIState::Tag new_state);
void popState(); void popState();
FontHolder _font_holder;
}; };
#endif // APPLICATION_H #endif // APPLICATION_H

View File

@ -20,14 +20,14 @@ public:
return true; return true;
} }
Resource& get(Id id) const const std::shared_ptr<Resource>& get(Id id) const
{ {
const auto found = _resources.find(id); const auto found = _resources.find(id);
return *found->second; return found->second;
} }
private: private:
std::map<Id, std::unique_ptr<Resource>> _resources; std::map<Id, std::shared_ptr<Resource>> _resources;
}; };
namespace Fonts namespace Fonts

View File

@ -15,9 +15,12 @@
const sf::Time TIME_PER_FRAME = sf::seconds(1.f / 90.f); const sf::Time TIME_PER_FRAME = sf::seconds(1.f / 90.f);
Application::Application() : Application::Application() :
_game_window({1280, 720}, "Test", sf::Style::Default), _game_window({1280, 720}, "Test", sf::Style::Default)
_game(std::make_unique<ClassicGame>(std::make_unique<ClassicGraphicsManager>(_game_window), std::make_unique<MusicSFML>()))
{ {
_font_holder.load(Fonts::Id::GUI, "SourceCodePro-Regular.ttf");
_game = std::make_unique<ClassicGame>(std::make_unique<ClassicGraphicsManager>(_game_window), std::make_unique<MusicSFML>());
_game_window.setFramerateLimit(60); _game_window.setFramerateLimit(60);
_game_window.setKeyRepeatEnabled(false); _game_window.setKeyRepeatEnabled(false);
_game_window.setMouseCursorGrabbed(false); _game_window.setMouseCursorGrabbed(false);
@ -26,9 +29,9 @@ Application::Application() :
MainMenu::Callbacks callbacks = {[&](){ pushState(GUIState::Tag::EDITOR); }}; MainMenu::Callbacks callbacks = {[&](){ pushState(GUIState::Tag::EDITOR); }};
Editor::Callbacks editor_callbacks = {[&](){ popState(); }}; Editor::Callbacks editor_callbacks = {[&](){ popState(); }};
const auto main_menu = std::make_shared<MainMenu>(_game_window, std::move(callbacks)); const auto main_menu = std::make_shared<MainMenu>(_game_window, std::move(callbacks), _font_holder);
const auto game_state = std::make_shared<GameState>(_game_window, _game, GameState::Callbacks()); const auto game_state = std::make_shared<GameState>(_game_window, _game, GameState::Callbacks());
const auto editor = std::make_shared<Editor>(_game_window, std::move(editor_callbacks), std::make_unique<MusicSFML>()); const auto editor = std::make_shared<Editor>(_game_window, std::move(editor_callbacks), std::make_unique<MusicSFML>(), _font_holder);
_states[GUIState::Tag::MAIN_MENU] = main_menu; _states[GUIState::Tag::MAIN_MENU] = main_menu;
_states[GUIState::Tag::GAME] = game_state; _states[GUIState::Tag::GAME] = game_state;
_states[GUIState::Tag::EDITOR] = editor; _states[GUIState::Tag::EDITOR] = editor;

View File

@ -6,8 +6,7 @@
#include <iostream> #include <iostream>
Editor::Editor(sf::RenderWindow& game_window, Callbacks&& callbacks, std::unique_ptr<Music>&& music) : Editor::Editor(sf::RenderWindow& game_window, Callbacks&& callbacks, std::unique_ptr<Music>&& music, const FontHolder& font_holder) :
_menu_bar(std::make_shared<MenuBar>()),
_game_window(game_window), _game_window(game_window),
_music(std::move(music)), _music(std::move(music)),
_bpm_calculator(std::make_unique<BPMCalculator>(_music)) _bpm_calculator(std::make_unique<BPMCalculator>(_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_width = game_window.getSize().x;
//const float window_height = game_window.getSize().y; //const float window_height = game_window.getSize().y;
auto bpm_button = std::make_shared<PushButton>("Calculate BPM"); _menu_bar = std::make_shared<MenuBar>(font_holder.get(Fonts::Id::GUI));
auto bpm_button = std::make_shared<PushButton>("Play song :)", font_holder.get(Fonts::Id::GUI));
bpm_button->setCallback([&]() bpm_button->setCallback([&]()
{ {
_bpm_calculator->startListening(0); _bpm_calculator->startListening(0);
}); });
auto quit_button = std::make_shared<PushButton>("Quit"); auto quit_button = std::make_shared<PushButton>("Quit", font_holder.get(Fonts::Id::GUI));
quit_button->setCallback(callbacks.onLeaveEditorState); quit_button->setCallback(callbacks.onLeaveEditorState);
auto test_menu = std::make_shared<MenuDrop>(); auto test_menu = std::make_shared<MenuDrop>();

View File

@ -2,7 +2,7 @@
#include "gui/state.h" #include "gui/state.h"
#include "tools/music.h" #include "tools/music.h"
#include "tools/resourceholder.h"
#include <SFML/Graphics/RenderWindow.hpp> #include <SFML/Graphics/RenderWindow.hpp>
class MenuBar; class MenuBar;
@ -17,7 +17,7 @@ public:
std::function<void(void)> onLeaveEditorState; std::function<void(void)> onLeaveEditorState;
}; };
explicit Editor(sf::RenderWindow& game_window, Callbacks&& callbacks, std::unique_ptr<Music>&& music); explicit Editor(sf::RenderWindow& game_window, Callbacks&& callbacks, std::unique_ptr<Music>&& music, const FontHolder& font_holder);
virtual void input(const sf::Event& event) override; virtual void input(const sf::Event& event) override;
virtual void update() override; virtual void update() override;
virtual void draw() const override; virtual void draw() const override;

View File

@ -2,18 +2,18 @@
#include "widgets/pushbutton.h" #include "widgets/pushbutton.h"
#include "widgets/group.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<Group>()), _buttons(std::make_shared<Group>()),
_game_window(game_window) _game_window(game_window)
{ {
const float window_width = game_window.getSize().x; const float window_width = game_window.getSize().x;
const float window_height = game_window.getSize().y; const float window_height = game_window.getSize().y;
std::shared_ptr<PushButton> button_start = std::make_shared<PushButton>("Start"); std::shared_ptr<PushButton> button_start = std::make_shared<PushButton>("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->setRect(sf::FloatRect(window_width / 3., window_height / 7. * 2, window_width / 3., window_height / 7.));
button_start->setCallback(callbacks.onAppendGameState); button_start->setCallback(callbacks.onAppendGameState);
std::shared_ptr<PushButton> button_exit = std::make_shared<PushButton>("Exit"); std::shared_ptr<PushButton> button_exit = std::make_shared<PushButton>("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->setRect(sf::FloatRect(window_width / 3., window_height / 7. * 4, window_width / 3., window_height / 7.));
button_exit->setCallback([&]() button_exit->setCallback([&]()
{ {

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include "gui/state.h" #include "gui/state.h"
#include "tools/resourceholder.h"
#include <SFML/Graphics/RenderWindow.hpp> #include <SFML/Graphics/RenderWindow.hpp>
class Group; class Group;
@ -14,7 +15,7 @@ public:
std::function<void(void)> onAppendGameState; std::function<void(void)> 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 input(const sf::Event& event) override;
virtual void update() override; virtual void update() override;
virtual void draw() const override; virtual void draw() const override;

View File

@ -1,10 +1,14 @@
#include "button.h" #include "button.h"
Button::Button(const std::string &text) Button::Button(const std::string &text, const std::shared_ptr<sf::Font>& font, unsigned int font_size) :
_font(font)
{ {
setText(text); setText(text);
_button_text.setFillColor(sf::Color::Black); _button_text.setFillColor(sf::Color::Black);
_button_text.setCharacterSize(font_size);
_button_text.setFont(*_font);
_button_content.setFillColor(sf::Color::White); _button_content.setFillColor(sf::Color::White);
} }
void Button::update() void Button::update()
@ -27,6 +31,7 @@ void Button::setRect(const sf::FloatRect& rect)
{ {
_button_content.setPosition(rect.left, rect.top); _button_content.setPosition(rect.left, rect.top);
_button_content.setSize({rect.width, rect.height}); _button_content.setSize({rect.width, rect.height});
_button_text.setPosition(rect.left + 5, rect.top + 5);
} }
void Button::setPosition(const sf::Vector2f &position) void Button::setPosition(const sf::Vector2f &position)

View File

@ -8,7 +8,7 @@
class Button : public Widget class Button : public Widget
{ {
public: public:
explicit Button(const std::string& text); explicit Button(const std::string& text, const std::shared_ptr<sf::Font>& font, unsigned int font_size);
virtual void input(const sf::Event& event) override = 0; virtual void input(const sf::Event& event) override = 0;
virtual void update() override final; virtual void update() override final;
@ -23,5 +23,6 @@ public:
protected: protected:
sf::RectangleShape _button_content; sf::RectangleShape _button_content;
sf::Text _button_text; sf::Text _button_text;
std::shared_ptr<sf::Font> _font;
}; };

View File

@ -1,8 +1,8 @@
#include "cascademenubutton.h" #include "cascademenubutton.h"
#include "menudrop.h" #include "menudrop.h"
CascadeMenuButton::CascadeMenuButton(const std::string& text) : CascadeMenuButton::CascadeMenuButton(const std::string& text, const std::shared_ptr<sf::Font> &font, unsigned int font_size) :
Button(text) Button(text, font, font_size)
{} {}
void CascadeMenuButton::input(const sf::Event& event) void CascadeMenuButton::input(const sf::Event& event)

View File

@ -7,7 +7,7 @@ class MenuDrop;
class CascadeMenuButton : public Button class CascadeMenuButton : public Button
{ {
public: public:
explicit CascadeMenuButton(const std::string& text); explicit CascadeMenuButton(const std::string& text, const std::shared_ptr<sf::Font>& font, unsigned int font_size = 12);
virtual void input(const sf::Event& event) override final; virtual void input(const sf::Event& event) override final;
virtual void setRect(const sf::FloatRect& rect) override final; virtual void setRect(const sf::FloatRect& rect) override final;

View File

@ -1,7 +1,8 @@
#include "menubar.h" #include "menubar.h"
#include "iostream" #include "iostream"
MenuBar::MenuBar() : MenuBar::MenuBar(const std::shared_ptr<sf::Font>& font) :
_font(font),
_button_width(170) _button_width(170)
{ {
_bar_rect.setFillColor(sf::Color(88, 57, 107)); _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<MenuDrop>& submenu) void MenuBar::addSubMenu(std::string name, const std::shared_ptr<MenuDrop>& submenu)
{ {
const auto new_button = std::make_shared<PushButton>(name); const auto new_button = std::make_shared<PushButton>(name, _font);
std::size_t current_index = _amount_buttons; std::size_t current_index = _amount_buttons;
new_button->setRect(sf::FloatRect(current_index * _button_width, 0, _button_width, _bar_rect.getSize().y)); new_button->setRect(sf::FloatRect(current_index * _button_width, 0, _button_width, _bar_rect.getSize().y));

View File

@ -9,7 +9,7 @@
class MenuBar : public Widget class MenuBar : public Widget
{ {
public: public:
explicit MenuBar(); explicit MenuBar(const std::shared_ptr<sf::Font>& font);
virtual void input(const sf::Event& event) override; virtual void input(const sf::Event& event) override;
virtual void update() override; virtual void update() override;
@ -22,6 +22,7 @@ public:
void addSubMenu(std::string name, const std::shared_ptr<MenuDrop>& submenu); void addSubMenu(std::string name, const std::shared_ptr<MenuDrop>& submenu);
private: private:
std::shared_ptr<sf::Font> _font;
sf::RectangleShape _bar_rect; sf::RectangleShape _bar_rect;
std::size_t _amount_buttons; std::size_t _amount_buttons;
std::size_t _button_width; std::size_t _button_width;

View File

@ -1,7 +1,7 @@
#include "pushbutton.h" #include "pushbutton.h"
PushButton::PushButton(const std::string& text) : PushButton::PushButton(const std::string& text, const std::shared_ptr<sf::Font> &font, unsigned int font_size) :
Button(text), Button(text, font, font_size),
_pressed(false) _pressed(false)
{ {
_color_idle = sf::Color(230, 230, 230); _color_idle = sf::Color(230, 230, 230);

View File

@ -5,7 +5,7 @@
class PushButton : public Button class PushButton : public Button
{ {
public: public:
explicit PushButton(const std::string& text); explicit PushButton(const std::string& text, const std::shared_ptr<sf::Font>& font, unsigned int font_size = 12);
virtual void input(const sf::Event& event) override final; virtual void input(const sf::Event& event) override final;
void setCallback(std::function<void(void)> callback); void setCallback(std::function<void(void)> callback);