Implement BPMCalculatorWidget, add sf::Time dt to all update() functions

This commit is contained in:
NaiJi ✨ 2021-08-27 20:40:48 +03:00
parent 788cd090f2
commit 80bc47e425
27 changed files with 157 additions and 84 deletions

View File

@ -18,7 +18,7 @@ public:
Application(); Application();
void run(); void run();
void input(); void input();
void update(); void update(const sf::Time& dt);
void draw(); void draw();
private: private:

View File

@ -24,7 +24,7 @@ public:
virtual ~GUIState() = default; virtual ~GUIState() = default;
virtual void input(const sf::Event& event) = 0; virtual void input(const sf::Event& event) = 0;
virtual void update() = 0; virtual void update(const sf::Time& dt) = 0;
virtual void draw() const = 0; virtual void draw() const = 0;
virtual void enter() = 0; virtual void enter() = 0;

View File

@ -10,8 +10,7 @@ public:
explicit BPMCalculator(const std::shared_ptr<Music>& music); explicit BPMCalculator(const std::shared_ptr<Music>& music);
void setMusic(const std::shared_ptr<Music>& music); void setMusic(const std::shared_ptr<Music>& music);
void startListening(const microsec& offset); void start();
void stopListening();
void click(); void click();
int getCurrentApproximation() const; int getCurrentApproximation() const;

View File

@ -60,7 +60,7 @@ void Application::exec()
if (isOneFramePassed) if (isOneFramePassed)
{ {
time_since_last_update -= TIME_PER_FRAME; time_since_last_update -= TIME_PER_FRAME;
update(); update(time_since_last_update);
draw(); draw();
} }
} }
@ -84,9 +84,9 @@ void Application::input()
} }
} }
void Application::update() void Application::update(const sf::Time& dt)
{ {
_state_stack.back()->update(); _state_stack.back()->update(dt);
} }
void Application::draw() void Application::draw()

View File

@ -2,6 +2,7 @@
#include "widgets/button.h" #include "widgets/button.h"
#include "widgets/group.h" #include "widgets/group.h"
#include "widgets/menubar.h" #include "widgets/menubar.h"
#include "widgets/bpmcalculatorwidget.h"
#include "tools/bpmcalculator.h" #include "tools/bpmcalculator.h"
#include <iostream> #include <iostream>
@ -9,7 +10,8 @@
Editor::Editor(sf::RenderWindow& game_window, Callbacks&& callbacks, std::unique_ptr<Music>&& music, const FontHolder& font_holder) : Editor::Editor(sf::RenderWindow& game_window, Callbacks&& callbacks, std::unique_ptr<Music>&& music, const FontHolder& font_holder) :
_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_shared<BPMCalculator>(_music)),
_bpm_widget(std::make_shared<BPMCalculatorWidget>(_bpm_calculator, font_holder.get(Fonts::Id::GUI)))
{ {
(void)callbacks; (void)callbacks;
const float window_width = game_window.getSize().x; const float window_width = game_window.getSize().x;
@ -20,14 +22,22 @@ Editor::Editor(sf::RenderWindow& game_window, Callbacks&& callbacks, std::unique
auto bpm_button = std::make_shared<PushButton>("Play song :)", 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_widget->setVisibility(true);
}); });
_bpm_widget->setRect(sf::FloatRect(_game_window.getSize().x / 3, _game_window.getSize().y / 3, _game_window.getSize().x / 3, _game_window.getSize().y / 3));
_bpm_widget->setVisibility(false);
auto test_menu_2 = std::make_shared<MenuDrop>(); auto test_menu_2 = std::make_shared<MenuDrop>();
test_menu_2->setRect(sf::FloatRect{0, 0, 200, 27 * 5}); test_menu_2->setRect(sf::FloatRect{0, 0, 200, 27 * 5});
auto test_menu_3 = std::make_shared<MenuDrop>();
test_menu_3->setRect(sf::FloatRect{0, 0, 200, 27 * 5});
auto test_cascade_button = std::make_shared<CascadeMenuButton>("Show submenu", font_holder.get(Fonts::Id::GUI)); auto test_cascade_button = std::make_shared<CascadeMenuButton>("Show submenu", font_holder.get(Fonts::Id::GUI));
auto test_cascade_button_2 = std::make_shared<CascadeMenuButton>("Show submenu 2", font_holder.get(Fonts::Id::GUI));
auto quit_button = std::make_shared<PushButton>("Quit", font_holder.get(Fonts::Id::GUI)); auto quit_button = std::make_shared<PushButton>("Quit", font_holder.get(Fonts::Id::GUI));
quit_button->setCallback(callbacks.onLeaveEditorState); quit_button->setCallback(callbacks.onLeaveEditorState);
@ -45,6 +55,10 @@ Editor::Editor(sf::RenderWindow& game_window, Callbacks&& callbacks, std::unique
test_cascade_button->resetRect(); test_cascade_button->resetRect();
test_cascade_button_2->setSubmenu(test_menu_3);
test_menu_2->addCascadeButton(test_cascade_button_2);
test_cascade_button_2->resetRect();
_music->openFromFile("Uta-test.flac"); _music->openFromFile("Uta-test.flac");
_music->setVolume(5); _music->setVolume(5);
} }
@ -52,22 +66,18 @@ Editor::Editor(sf::RenderWindow& game_window, Callbacks&& callbacks, std::unique
void Editor::input(const sf::Event& event) void Editor::input(const sf::Event& event)
{ {
_menu_bar->input(event); _menu_bar->input(event);
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Space)
{
_bpm_calculator->click();
std::cout << _bpm_calculator->getCurrentApproximation() << '\n';
}
} }
void Editor::update() void Editor::update(const sf::Time& dt)
{ {
_menu_bar->update(); _menu_bar->update(dt);
_bpm_widget->update(dt);
} }
void Editor::draw() const void Editor::draw() const
{ {
_game_window.draw(*_menu_bar); _game_window.draw(*_menu_bar);
_game_window.draw(*_bpm_widget);
} }
void Editor::enter() void Editor::enter()

View File

@ -6,6 +6,7 @@
#include <SFML/Graphics/RenderWindow.hpp> #include <SFML/Graphics/RenderWindow.hpp>
class MenuBar; class MenuBar;
class BPMCalculatorWidget;
class BPMCalculator; class BPMCalculator;
class Editor : public GUIState class Editor : public GUIState
@ -19,7 +20,7 @@ public:
explicit Editor(sf::RenderWindow& game_window, Callbacks&& callbacks, std::unique_ptr<Music>&& music, const FontHolder& font_holder); 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(const sf::Time& dt) override;
virtual void draw() const override; virtual void draw() const override;
virtual void enter() override; virtual void enter() override;
@ -31,5 +32,6 @@ private:
std::shared_ptr<Music> _music; std::shared_ptr<Music> _music;
std::shared_ptr<BPMCalculator> _bpm_calculator; std::shared_ptr<BPMCalculator> _bpm_calculator;
std::shared_ptr<BPMCalculatorWidget> _bpm_widget;
}; };

View File

@ -16,8 +16,13 @@ void GameState::input(const sf::Event& event)
_game->input({0, event}); _game->input({0, event});
} }
void GameState::update() void GameState::update(const sf::Time& dt)
{ {
(void)dt;
// !!!!!!!!!!!!!!!!!!!!!!
// TODO.
//
// Oh dude... hang in there
_game->update(); _game->update();
} }

View File

@ -21,7 +21,7 @@ public:
Callbacks&& callbacks); Callbacks&& callbacks);
virtual void input(const sf::Event& event) override; virtual void input(const sf::Event& event) override;
virtual void update() override; virtual void update(const sf::Time& dt) override;
virtual void draw() const override; virtual void draw() const override;
virtual void enter() override; virtual void enter() override;

View File

@ -29,9 +29,9 @@ void MainMenu::input(const sf::Event& event)
_buttons->input(event); _buttons->input(event);
} }
void MainMenu::update() void MainMenu::update(const sf::Time& dt)
{ {
_buttons->update(); _buttons->update(dt);
} }
void MainMenu::draw() const void MainMenu::draw() const

View File

@ -17,7 +17,7 @@ public:
explicit MainMenu(sf::RenderWindow& game_window, Callbacks&& callbacks, const FontHolder &font_holder); 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(const sf::Time& dt) override;
virtual void draw() const override; virtual void draw() const override;
virtual void enter() override; virtual void enter() override;

View File

@ -1,10 +1,12 @@
#include "bpmcalculatorwidget.h" #include "bpmcalculatorwidget.h"
#include "tools/bpmcalculator.h"
BPMCalculatorWidget::BPMCalculatorWidget() : BPMCalculatorWidget::BPMCalculatorWidget(const std::shared_ptr<BPMCalculator>& bpm_calculator, const std::shared_ptr<sf::Font>& font) :
_pressed(false) _bpm_calculator(bpm_calculator),
_slider(std::make_shared<BPMSlider>(font))
{ {
_button_text.setFillColor(sf::Color::Black); _widget_window.setFillColor(sf::Color(88, 57, 107));
_button_content.setFillColor(sf::Color::White); //addChild(_slider);
} }
void BPMCalculatorWidget::input(const sf::Event& event) void BPMCalculatorWidget::input(const sf::Event& event)
@ -14,21 +16,10 @@ void BPMCalculatorWidget::input(const sf::Event& event)
default: default:
break; break;
case sf::Event::MouseButtonPressed: case sf::Event::KeyPressed:
if (isUnderMouse(event.mouseButton.x, event.mouseButton.y)) if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Space)
{ {
_pressed = true; _bpm_calculator->click();
_button_content.setFillColor(sf::Color(155, 155, 155));
}
break;
case sf::Event::MouseButtonReleased:
if (_pressed)
{
_button_content.setFillColor(sf::Color::White);
_pressed = false;
if (isUnderMouse(event.mouseButton.x, event.mouseButton.y))
_on_click_callback();
} }
break; break;
} }
@ -36,31 +27,36 @@ void BPMCalculatorWidget::input(const sf::Event& event)
Widget::input(event); Widget::input(event);
} }
void BPMCalculatorWidget::update() void BPMCalculatorWidget::update(const sf::Time& dt)
{ {
Widget::update(); Widget::update(dt);
} }
void BPMCalculatorWidget::draw(sf::RenderTarget& target, sf::RenderStates states) const void BPMCalculatorWidget::draw(sf::RenderTarget& target, sf::RenderStates states) const
{ {
target.draw(_button_content, states); if (_is_visible)
target.draw(_button_text, states); {
target.draw(_widget_window, states);
Widget::draw(target, states); Widget::draw(target, states);
} }
}
void BPMCalculatorWidget::setRect(const sf::FloatRect& rect) void BPMCalculatorWidget::setRect(const sf::FloatRect& rect)
{ {
_button_content.setPosition(rect.left, rect.top); _widget_window.setPosition(rect.left, rect.top);
_button_content.setSize({rect.width, rect.height}); _widget_window.setSize({rect.width, rect.height});
_slider->setRect(sf::FloatRect{rect.width / 3, rect.height / 2 - 50, rect.width / 3, 100});
} }
void BPMCalculatorWidget::setPosition(const sf::Vector2f &position) void BPMCalculatorWidget::setPosition(const sf::Vector2f &position)
{ {
_button_content.setPosition(position); _widget_window.setPosition(position);
_slider->setRect(sf::FloatRect{_widget_window.getSize().x / 3,
_widget_window.getSize().y / 2 - 50,
_widget_window.getSize().x / 3, 100});
} }
bool BPMCalculatorWidget::isUnderMouse(int mouse_x, int mouse_y) const bool BPMCalculatorWidget::isUnderMouse(int mouse_x, int mouse_y) const
{ {
return _button_content.getGlobalBounds().contains(mouse_x, mouse_y); return _is_visible && _widget_window.getGlobalBounds().contains(mouse_x, mouse_y);
} }

View File

@ -1,26 +1,30 @@
#pragma once #pragma once
#include "widget.h" #include "widget.h"
#include "bpmslider.h"
#include <functional> #include <functional>
#include <SFML/Graphics/RectangleShape.hpp> #include <SFML/Graphics/RectangleShape.hpp>
#include <SFML/Graphics/Text.hpp> #include <SFML/Graphics/Text.hpp>
class BPMCalculator;
class BPMCalculatorWidget : public Widget class BPMCalculatorWidget : public Widget
{ {
public: public:
BPMCalculatorWidget(); explicit BPMCalculatorWidget(const std::shared_ptr<BPMCalculator>& bpm_calculator, 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(const sf::Time& dt) override;
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override; virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
virtual void setRect(const sf::FloatRect& rect) override; virtual void setRect(const sf::FloatRect& rect) override;
virtual void setPosition(const sf::Vector2f& position) override; virtual void setPosition(const sf::Vector2f& position) override;
virtual bool isUnderMouse(int mouse_x, int mouse_y) const override; virtual bool isUnderMouse(int mouse_x, int mouse_y) const override;
private: private:
sf::RectangleShape _button_content; std::shared_ptr<BPMCalculator> _bpm_calculator;
sf::Text _button_text; std::shared_ptr<BPMSlider> _slider;
bool _pressed;
sf::RectangleShape _widget_window;
std::function<void(void)> _on_click_callback; std::function<void(void)> _on_click_callback;
}; };

View File

@ -0,0 +1,38 @@
#include "bpmslider.h"
BPMSlider::BPMSlider(const std::shared_ptr<sf::Font> &font)
{
_bpm_value.setFont(*font);
}
void BPMSlider::input(const sf::Event& event)
{
Widget::input(event);
}
void BPMSlider::update(const sf::Time& dt)
{
Widget::update(dt);
}
void BPMSlider::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
Widget::draw(target, states);
}
void BPMSlider::setRect(const sf::FloatRect& rect)
{
_slider_background.setPosition(rect.left, rect.top);
_slider_background.setSize({rect.width, rect.height});
}
void BPMSlider::setPosition(const sf::Vector2f& position)
{
_slider_background.setPosition(position);
}
bool BPMSlider::isUnderMouse(int mouse_x, int mouse_y) const
{
return mouse_x == mouse_y; // just to compile
}

View File

@ -0,0 +1,26 @@
#pragma once
#include "widget.h"
#include <SFML/Graphics/RectangleShape.hpp>
#include <SFML/Graphics/Text.hpp>
class BPMSlider : public Widget
{
public:
explicit BPMSlider(const std::shared_ptr<sf::Font> &font);
virtual void input(const sf::Event& event) override;
virtual void update(const sf::Time& dt) override;
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
virtual void setRect(const sf::FloatRect& rect) override;
virtual void setPosition(const sf::Vector2f& position) override;
virtual bool isUnderMouse(int mouse_x, int mouse_y) const override;
private:
sf::RectangleShape _slider_background;
sf::RectangleShape _slider_tick;
sf::Text _bpm_value;
};

View File

@ -9,9 +9,9 @@ Button::Button(const std::string &text, const std::shared_ptr<sf::Font>& font, u
_button_text.setFont(*_font); _button_text.setFont(*_font);
} }
void Button::update() void Button::update(const sf::Time& dt)
{ {
Widget::update(); Widget::update(dt);
} }
void Button::draw(sf::RenderTarget& target, sf::RenderStates states) const void Button::draw(sf::RenderTarget& target, sf::RenderStates states) const

View File

@ -11,7 +11,7 @@ public:
explicit Button(const std::string& text, const std::shared_ptr<sf::Font>& font, unsigned int font_size); 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(const sf::Time& dt) override final;
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override final; virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override final;
virtual void setPosition(const sf::Vector2f& position) override final; virtual void setPosition(const sf::Vector2f& position) override final;
virtual bool isUnderMouse(int mouse_x, int mouse_y) const override final; virtual bool isUnderMouse(int mouse_x, int mouse_y) const override final;

View File

@ -5,9 +5,9 @@ void Group::input(const sf::Event& event)
Widget::input(event); Widget::input(event);
} }
void Group::update() void Group::update(const sf::Time& dt)
{ {
Widget::update(); Widget::update(dt);
} }
void Group::draw(sf::RenderTarget& target, sf::RenderStates states) const void Group::draw(sf::RenderTarget& target, sf::RenderStates states) const

View File

@ -6,7 +6,7 @@ class Group : public Widget
{ {
public: public:
virtual void input(const sf::Event& event) override; virtual void input(const sf::Event& event) override;
virtual void update() override; virtual void update(const sf::Time& dt) override;
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override; virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
virtual void setRect(const sf::FloatRect& rect) override; virtual void setRect(const sf::FloatRect& rect) override;
virtual void setPosition(const sf::Vector2f& position) override; virtual void setPosition(const sf::Vector2f& position) override;

View File

@ -27,9 +27,9 @@ void MenuBar::input(const sf::Event &event)
Widget::input(event); Widget::input(event);
} }
void MenuBar::update() void MenuBar::update(const sf::Time& dt)
{ {
Widget::update(); Widget::update(dt);
} }
void MenuBar::draw(sf::RenderTarget& target, sf::RenderStates states) const void MenuBar::draw(sf::RenderTarget& target, sf::RenderStates states) const

View File

@ -12,7 +12,7 @@ public:
explicit MenuBar(const std::shared_ptr<sf::Font>& font); 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(const sf::Time& dt) override;
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override; virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
virtual void setRect(const sf::FloatRect& rect) override; virtual void setRect(const sf::FloatRect& rect) override;
virtual void setPosition(const sf::Vector2f& position) override; virtual void setPosition(const sf::Vector2f& position) override;

View File

@ -42,9 +42,9 @@ bool MenuDrop::hasActiveSubmenus() const
}); });
} }
void MenuDrop::update() void MenuDrop::update(const sf::Time& dt)
{ {
Widget::update(); Widget::update(dt);
} }
void MenuDrop::draw(sf::RenderTarget& target, sf::RenderStates states) const void MenuDrop::draw(sf::RenderTarget& target, sf::RenderStates states) const

View File

@ -11,7 +11,7 @@ public:
explicit MenuDrop(); explicit MenuDrop();
virtual void input(const sf::Event& event) override; virtual void input(const sf::Event& event) override;
virtual void update() override; virtual void update(const sf::Time& dt) override;
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override; virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
virtual void setRect(const sf::FloatRect& rect) override; virtual void setRect(const sf::FloatRect& rect) override;
virtual void setPosition(const sf::Vector2f& position) override; virtual void setPosition(const sf::Vector2f& position) override;

View File

@ -5,9 +5,9 @@ void MenuSeparator::input(const sf::Event& event)
Widget::input(event); Widget::input(event);
} }
void MenuSeparator::update() void MenuSeparator::update(const sf::Time& dt)
{ {
Widget::update(); Widget::update(dt);
} }
void MenuSeparator::draw(sf::RenderTarget& target, sf::RenderStates states) const void MenuSeparator::draw(sf::RenderTarget& target, sf::RenderStates states) const

View File

@ -8,7 +8,7 @@ class MenuSeparator : public Widget
{ {
public: public:
virtual void input(const sf::Event& event) override; virtual void input(const sf::Event& event) override;
virtual void update() override; virtual void update(const sf::Time& dt) override;
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override; virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
virtual void setRect(const sf::FloatRect& rect) override; virtual void setRect(const sf::FloatRect& rect) override;
virtual void setPosition(const sf::Vector2f& position) override; virtual void setPosition(const sf::Vector2f& position) override;

View File

@ -11,10 +11,10 @@ void Widget::input(const sf::Event &event)
} }
} }
void Widget::update() void Widget::update(const sf::Time& dt)
{ {
for (auto& child : _children) for (auto& child : _children)
child->update(); child->update(dt);
} }
void Widget::draw(sf::RenderTarget& target, sf::RenderStates states) const void Widget::draw(sf::RenderTarget& target, sf::RenderStates states) const

View File

@ -12,7 +12,7 @@ public:
virtual ~Widget() = default; virtual ~Widget() = default;
virtual void input(const sf::Event& event) = 0; virtual void input(const sf::Event& event) = 0;
virtual void update() = 0; virtual void update(const sf::Time& dt) = 0;
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const = 0; virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const = 0;
virtual void setRect(const sf::FloatRect& rect) = 0; virtual void setRect(const sf::FloatRect& rect) = 0;
virtual void setPosition(const sf::Vector2f& position) = 0; virtual void setPosition(const sf::Vector2f& position) = 0;

View File

@ -17,17 +17,10 @@ void BPMCalculator::setMusic(const std::shared_ptr<Music>& music)
_music = music; _music = music;
} }
void BPMCalculator::startListening(const microsec &offset) void BPMCalculator::start()
{ {
_deltas.clear(); _deltas.clear();
_previous_click_offset = 0; _previous_click_offset = _music->fetchOffset();
_music->setOffset(offset);
_music->play();
}
void BPMCalculator::stopListening()
{
_music->stop();
} }
void BPMCalculator::click() void BPMCalculator::click()
@ -36,7 +29,7 @@ void BPMCalculator::click()
std::cout << click_offset << "\n\n\n\n"; std::cout << click_offset << "\n\n\n\n";
if (_previous_click_offset == 0) if (_deltas.empty())
{ {
_previous_click_offset = click_offset; _previous_click_offset = click_offset;
return; return;