From 80bc47e425006ace860444078a180c174cff83bf Mon Sep 17 00:00:00 2001 From: NaiJi Date: Fri, 27 Aug 2021 20:40:48 +0300 Subject: [PATCH] Implement BPMCalculatorWidget, add sf::Time dt to all update() functions --- include/application.h | 2 +- include/gui/state.h | 2 +- include/tools/bpmcalculator.h | 3 +- src/application.cpp | 6 +-- src/gui/editor.cpp | 30 +++++++++----- src/gui/editor.h | 4 +- src/gui/gamestate.cpp | 7 +++- src/gui/gamestate.h | 2 +- src/gui/mainmenu.cpp | 4 +- src/gui/mainmenu.h | 2 +- src/gui/widgets/bpmcalculatorwidget.cpp | 52 ++++++++++++------------- src/gui/widgets/bpmcalculatorwidget.h | 14 ++++--- src/gui/widgets/bpmslider.cpp | 38 ++++++++++++++++++ src/gui/widgets/bpmslider.h | 26 +++++++++++++ src/gui/widgets/button.cpp | 4 +- src/gui/widgets/button.h | 2 +- src/gui/widgets/group.cpp | 4 +- src/gui/widgets/group.h | 2 +- src/gui/widgets/menubar.cpp | 4 +- src/gui/widgets/menubar.h | 2 +- src/gui/widgets/menudrop.cpp | 4 +- src/gui/widgets/menudrop.h | 2 +- src/gui/widgets/menuseparator.cpp | 4 +- src/gui/widgets/menuseparator.h | 2 +- src/gui/widgets/widget.cpp | 4 +- src/gui/widgets/widget.h | 2 +- src/tools/bpmcalculator.cpp | 13 ++----- 27 files changed, 157 insertions(+), 84 deletions(-) create mode 100644 src/gui/widgets/bpmslider.cpp create mode 100644 src/gui/widgets/bpmslider.h diff --git a/include/application.h b/include/application.h index 1702e7a..4c1e030 100644 --- a/include/application.h +++ b/include/application.h @@ -18,7 +18,7 @@ public: Application(); void run(); void input(); - void update(); + void update(const sf::Time& dt); void draw(); private: diff --git a/include/gui/state.h b/include/gui/state.h index 6652ab8..f07b017 100644 --- a/include/gui/state.h +++ b/include/gui/state.h @@ -24,7 +24,7 @@ public: virtual ~GUIState() = default; 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 enter() = 0; diff --git a/include/tools/bpmcalculator.h b/include/tools/bpmcalculator.h index 04ae469..72f5ba1 100644 --- a/include/tools/bpmcalculator.h +++ b/include/tools/bpmcalculator.h @@ -10,8 +10,7 @@ public: explicit BPMCalculator(const std::shared_ptr& music); void setMusic(const std::shared_ptr& music); - void startListening(const microsec& offset); - void stopListening(); + void start(); void click(); int getCurrentApproximation() const; diff --git a/src/application.cpp b/src/application.cpp index 0c1a1df..1dc4fd8 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -60,7 +60,7 @@ void Application::exec() if (isOneFramePassed) { time_since_last_update -= TIME_PER_FRAME; - update(); + update(time_since_last_update); 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() diff --git a/src/gui/editor.cpp b/src/gui/editor.cpp index 1f33bcd..a9faad3 100644 --- a/src/gui/editor.cpp +++ b/src/gui/editor.cpp @@ -2,6 +2,7 @@ #include "widgets/button.h" #include "widgets/group.h" #include "widgets/menubar.h" +#include "widgets/bpmcalculatorwidget.h" #include "tools/bpmcalculator.h" #include @@ -9,7 +10,8 @@ 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)) + _bpm_calculator(std::make_shared(_music)), + _bpm_widget(std::make_shared(_bpm_calculator, font_holder.get(Fonts::Id::GUI))) { (void)callbacks; 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("Play song :)", font_holder.get(Fonts::Id::GUI)); 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(); test_menu_2->setRect(sf::FloatRect{0, 0, 200, 27 * 5}); + auto test_menu_3 = std::make_shared(); + test_menu_3->setRect(sf::FloatRect{0, 0, 200, 27 * 5}); + auto test_cascade_button = std::make_shared("Show submenu", font_holder.get(Fonts::Id::GUI)); + auto test_cascade_button_2 = std::make_shared("Show submenu 2", font_holder.get(Fonts::Id::GUI)); + auto quit_button = std::make_shared("Quit", font_holder.get(Fonts::Id::GUI)); 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_2->setSubmenu(test_menu_3); + test_menu_2->addCascadeButton(test_cascade_button_2); + test_cascade_button_2->resetRect(); + _music->openFromFile("Uta-test.flac"); _music->setVolume(5); } @@ -52,22 +66,18 @@ Editor::Editor(sf::RenderWindow& game_window, Callbacks&& callbacks, std::unique void Editor::input(const sf::Event& 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 { _game_window.draw(*_menu_bar); + _game_window.draw(*_bpm_widget); } void Editor::enter() diff --git a/src/gui/editor.h b/src/gui/editor.h index faa36d0..60d4e9a 100644 --- a/src/gui/editor.h +++ b/src/gui/editor.h @@ -6,6 +6,7 @@ #include class MenuBar; +class BPMCalculatorWidget; class BPMCalculator; class Editor : public GUIState @@ -19,7 +20,7 @@ public: 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 update(const sf::Time& dt) override; virtual void draw() const override; virtual void enter() override; @@ -31,5 +32,6 @@ private: std::shared_ptr _music; std::shared_ptr _bpm_calculator; + std::shared_ptr _bpm_widget; }; diff --git a/src/gui/gamestate.cpp b/src/gui/gamestate.cpp index a5af5b2..d7fb41b 100644 --- a/src/gui/gamestate.cpp +++ b/src/gui/gamestate.cpp @@ -16,8 +16,13 @@ void GameState::input(const sf::Event& 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(); } diff --git a/src/gui/gamestate.h b/src/gui/gamestate.h index 68be7d0..d4482d2 100644 --- a/src/gui/gamestate.h +++ b/src/gui/gamestate.h @@ -21,7 +21,7 @@ public: Callbacks&& callbacks); 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 enter() override; diff --git a/src/gui/mainmenu.cpp b/src/gui/mainmenu.cpp index ec6e43e..e393740 100644 --- a/src/gui/mainmenu.cpp +++ b/src/gui/mainmenu.cpp @@ -29,9 +29,9 @@ void MainMenu::input(const sf::Event& event) _buttons->input(event); } -void MainMenu::update() +void MainMenu::update(const sf::Time& dt) { - _buttons->update(); + _buttons->update(dt); } void MainMenu::draw() const diff --git a/src/gui/mainmenu.h b/src/gui/mainmenu.h index c79e37d..a2d4c78 100644 --- a/src/gui/mainmenu.h +++ b/src/gui/mainmenu.h @@ -17,7 +17,7 @@ public: 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 update(const sf::Time& dt) override; virtual void draw() const override; virtual void enter() override; diff --git a/src/gui/widgets/bpmcalculatorwidget.cpp b/src/gui/widgets/bpmcalculatorwidget.cpp index a37c444..f022bd9 100644 --- a/src/gui/widgets/bpmcalculatorwidget.cpp +++ b/src/gui/widgets/bpmcalculatorwidget.cpp @@ -1,10 +1,12 @@ #include "bpmcalculatorwidget.h" +#include "tools/bpmcalculator.h" -BPMCalculatorWidget::BPMCalculatorWidget() : - _pressed(false) +BPMCalculatorWidget::BPMCalculatorWidget(const std::shared_ptr& bpm_calculator, const std::shared_ptr& font) : + _bpm_calculator(bpm_calculator), + _slider(std::make_shared(font)) { - _button_text.setFillColor(sf::Color::Black); - _button_content.setFillColor(sf::Color::White); + _widget_window.setFillColor(sf::Color(88, 57, 107)); + //addChild(_slider); } void BPMCalculatorWidget::input(const sf::Event& event) @@ -14,21 +16,10 @@ void BPMCalculatorWidget::input(const sf::Event& event) default: break; - case sf::Event::MouseButtonPressed: - if (isUnderMouse(event.mouseButton.x, event.mouseButton.y)) + case sf::Event::KeyPressed: + if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Space) { - _pressed = true; - _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(); + _bpm_calculator->click(); } break; } @@ -36,31 +27,36 @@ void BPMCalculatorWidget::input(const sf::Event& 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 { - target.draw(_button_content, states); - target.draw(_button_text, states); - - Widget::draw(target, states); + if (_is_visible) + { + target.draw(_widget_window, states); + Widget::draw(target, states); + } } void BPMCalculatorWidget::setRect(const sf::FloatRect& rect) { - _button_content.setPosition(rect.left, rect.top); - _button_content.setSize({rect.width, rect.height}); + _widget_window.setPosition(rect.left, rect.top); + _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) { - _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 { - return _button_content.getGlobalBounds().contains(mouse_x, mouse_y); + return _is_visible && _widget_window.getGlobalBounds().contains(mouse_x, mouse_y); } diff --git a/src/gui/widgets/bpmcalculatorwidget.h b/src/gui/widgets/bpmcalculatorwidget.h index b5b4aae..943dc57 100644 --- a/src/gui/widgets/bpmcalculatorwidget.h +++ b/src/gui/widgets/bpmcalculatorwidget.h @@ -1,26 +1,30 @@ #pragma once #include "widget.h" +#include "bpmslider.h" #include #include #include +class BPMCalculator; + class BPMCalculatorWidget : public Widget { public: - BPMCalculatorWidget(); + explicit BPMCalculatorWidget(const std::shared_ptr& bpm_calculator, const std::shared_ptr &font); 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 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 _button_content; - sf::Text _button_text; - bool _pressed; + std::shared_ptr _bpm_calculator; + std::shared_ptr _slider; + + sf::RectangleShape _widget_window; std::function _on_click_callback; }; diff --git a/src/gui/widgets/bpmslider.cpp b/src/gui/widgets/bpmslider.cpp new file mode 100644 index 0000000..349bf27 --- /dev/null +++ b/src/gui/widgets/bpmslider.cpp @@ -0,0 +1,38 @@ +#include "bpmslider.h" + +BPMSlider::BPMSlider(const std::shared_ptr &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 +} + diff --git a/src/gui/widgets/bpmslider.h b/src/gui/widgets/bpmslider.h new file mode 100644 index 0000000..81a1641 --- /dev/null +++ b/src/gui/widgets/bpmslider.h @@ -0,0 +1,26 @@ +#pragma once + +#include "widget.h" + +#include +#include + +class BPMSlider : public Widget +{ +public: + explicit BPMSlider(const std::shared_ptr &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; +}; + + diff --git a/src/gui/widgets/button.cpp b/src/gui/widgets/button.cpp index df41122..f6705df 100644 --- a/src/gui/widgets/button.cpp +++ b/src/gui/widgets/button.cpp @@ -9,9 +9,9 @@ Button::Button(const std::string &text, const std::shared_ptr& font, u _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 diff --git a/src/gui/widgets/button.h b/src/gui/widgets/button.h index 0209916..343745e 100644 --- a/src/gui/widgets/button.h +++ b/src/gui/widgets/button.h @@ -11,7 +11,7 @@ public: 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; + virtual void update(const sf::Time& dt) override final; virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override final; virtual void setPosition(const sf::Vector2f& position) override final; virtual bool isUnderMouse(int mouse_x, int mouse_y) const override final; diff --git a/src/gui/widgets/group.cpp b/src/gui/widgets/group.cpp index 645a2e8..6587f24 100644 --- a/src/gui/widgets/group.cpp +++ b/src/gui/widgets/group.cpp @@ -5,9 +5,9 @@ void Group::input(const sf::Event& 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 diff --git a/src/gui/widgets/group.h b/src/gui/widgets/group.h index 8ebe145..7e71334 100644 --- a/src/gui/widgets/group.h +++ b/src/gui/widgets/group.h @@ -6,7 +6,7 @@ class Group : public Widget { public: 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 setRect(const sf::FloatRect& rect) override; virtual void setPosition(const sf::Vector2f& position) override; diff --git a/src/gui/widgets/menubar.cpp b/src/gui/widgets/menubar.cpp index d9f65c3..2378959 100644 --- a/src/gui/widgets/menubar.cpp +++ b/src/gui/widgets/menubar.cpp @@ -27,9 +27,9 @@ void MenuBar::input(const sf::Event &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 diff --git a/src/gui/widgets/menubar.h b/src/gui/widgets/menubar.h index 60151ab..a3e6ad5 100644 --- a/src/gui/widgets/menubar.h +++ b/src/gui/widgets/menubar.h @@ -12,7 +12,7 @@ public: explicit MenuBar(const std::shared_ptr& font); 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 setRect(const sf::FloatRect& rect) override; virtual void setPosition(const sf::Vector2f& position) override; diff --git a/src/gui/widgets/menudrop.cpp b/src/gui/widgets/menudrop.cpp index 3040afb..0851299 100644 --- a/src/gui/widgets/menudrop.cpp +++ b/src/gui/widgets/menudrop.cpp @@ -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 diff --git a/src/gui/widgets/menudrop.h b/src/gui/widgets/menudrop.h index 579ba3c..1046bff 100644 --- a/src/gui/widgets/menudrop.h +++ b/src/gui/widgets/menudrop.h @@ -11,7 +11,7 @@ public: explicit MenuDrop(); 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 setRect(const sf::FloatRect& rect) override; virtual void setPosition(const sf::Vector2f& position) override; diff --git a/src/gui/widgets/menuseparator.cpp b/src/gui/widgets/menuseparator.cpp index 3d044fa..44e2ad5 100644 --- a/src/gui/widgets/menuseparator.cpp +++ b/src/gui/widgets/menuseparator.cpp @@ -5,9 +5,9 @@ void MenuSeparator::input(const sf::Event& 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 diff --git a/src/gui/widgets/menuseparator.h b/src/gui/widgets/menuseparator.h index e928345..fe3c522 100644 --- a/src/gui/widgets/menuseparator.h +++ b/src/gui/widgets/menuseparator.h @@ -8,7 +8,7 @@ class MenuSeparator : public Widget { public: 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 setRect(const sf::FloatRect& rect) override; virtual void setPosition(const sf::Vector2f& position) override; diff --git a/src/gui/widgets/widget.cpp b/src/gui/widgets/widget.cpp index 96dbb22..75a2f9d 100644 --- a/src/gui/widgets/widget.cpp +++ b/src/gui/widgets/widget.cpp @@ -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) - child->update(); + child->update(dt); } void Widget::draw(sf::RenderTarget& target, sf::RenderStates states) const diff --git a/src/gui/widgets/widget.h b/src/gui/widgets/widget.h index 593ac9f..0d81d44 100644 --- a/src/gui/widgets/widget.h +++ b/src/gui/widgets/widget.h @@ -12,7 +12,7 @@ public: virtual ~Widget() = default; 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 setRect(const sf::FloatRect& rect) = 0; virtual void setPosition(const sf::Vector2f& position) = 0; diff --git a/src/tools/bpmcalculator.cpp b/src/tools/bpmcalculator.cpp index 30d9e02..a1ce4ac 100644 --- a/src/tools/bpmcalculator.cpp +++ b/src/tools/bpmcalculator.cpp @@ -17,17 +17,10 @@ void BPMCalculator::setMusic(const std::shared_ptr& music) _music = music; } -void BPMCalculator::startListening(const microsec &offset) +void BPMCalculator::start() { _deltas.clear(); - _previous_click_offset = 0; - _music->setOffset(offset); - _music->play(); -} - -void BPMCalculator::stopListening() -{ - _music->stop(); + _previous_click_offset = _music->fetchOffset(); } void BPMCalculator::click() @@ -36,7 +29,7 @@ void BPMCalculator::click() std::cout << click_offset << "\n\n\n\n"; - if (_previous_click_offset == 0) + if (_deltas.empty()) { _previous_click_offset = click_offset; return;