Implement BPMCalculatorWidget, add sf::Time dt to all update() functions
This commit is contained in:
parent
788cd090f2
commit
80bc47e425
|
@ -18,7 +18,7 @@ public:
|
|||
Application();
|
||||
void run();
|
||||
void input();
|
||||
void update();
|
||||
void update(const sf::Time& dt);
|
||||
void draw();
|
||||
|
||||
private:
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -10,8 +10,7 @@ public:
|
|||
explicit BPMCalculator(const std::shared_ptr<Music>& music);
|
||||
void setMusic(const std::shared_ptr<Music>& music);
|
||||
|
||||
void startListening(const microsec& offset);
|
||||
void stopListening();
|
||||
void start();
|
||||
|
||||
void click();
|
||||
int getCurrentApproximation() const;
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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 <iostream>
|
||||
|
@ -9,7 +10,8 @@
|
|||
Editor::Editor(sf::RenderWindow& game_window, Callbacks&& callbacks, std::unique_ptr<Music>&& music, const FontHolder& font_holder) :
|
||||
_game_window(game_window),
|
||||
_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;
|
||||
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));
|
||||
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>();
|
||||
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_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));
|
||||
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()
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
#include <SFML/Graphics/RenderWindow.hpp>
|
||||
|
||||
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>&& 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> _music;
|
||||
std::shared_ptr<BPMCalculator> _bpm_calculator;
|
||||
std::shared_ptr<BPMCalculatorWidget> _bpm_widget;
|
||||
};
|
||||
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
#include "bpmcalculatorwidget.h"
|
||||
#include "tools/bpmcalculator.h"
|
||||
|
||||
BPMCalculatorWidget::BPMCalculatorWidget() :
|
||||
_pressed(false)
|
||||
BPMCalculatorWidget::BPMCalculatorWidget(const std::shared_ptr<BPMCalculator>& bpm_calculator, const std::shared_ptr<sf::Font>& font) :
|
||||
_bpm_calculator(bpm_calculator),
|
||||
_slider(std::make_shared<BPMSlider>(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);
|
||||
|
||||
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);
|
||||
}
|
||||
|
|
|
@ -1,26 +1,30 @@
|
|||
#pragma once
|
||||
|
||||
#include "widget.h"
|
||||
#include "bpmslider.h"
|
||||
#include <functional>
|
||||
#include <SFML/Graphics/RectangleShape.hpp>
|
||||
#include <SFML/Graphics/Text.hpp>
|
||||
|
||||
class BPMCalculator;
|
||||
|
||||
class BPMCalculatorWidget : public Widget
|
||||
{
|
||||
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 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<BPMCalculator> _bpm_calculator;
|
||||
std::shared_ptr<BPMSlider> _slider;
|
||||
|
||||
sf::RectangleShape _widget_window;
|
||||
|
||||
std::function<void(void)> _on_click_callback;
|
||||
};
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
@ -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;
|
||||
};
|
||||
|
||||
|
|
@ -9,9 +9,9 @@ Button::Button(const std::string &text, const std::shared_ptr<sf::Font>& 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
|
||||
|
|
|
@ -11,7 +11,7 @@ public:
|
|||
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 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;
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -12,7 +12,7 @@ public:
|
|||
explicit MenuBar(const std::shared_ptr<sf::Font>& 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;
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -17,17 +17,10 @@ void BPMCalculator::setMusic(const std::shared_ptr<Music>& 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;
|
||||
|
|
Loading…
Reference in New Issue