2021-08-21 14:29:58 +02:00
|
|
|
#include "bpmcalculatorwidget.h"
|
2021-08-27 19:40:48 +02:00
|
|
|
#include "tools/bpmcalculator.h"
|
2021-08-16 20:54:03 +02:00
|
|
|
|
2021-09-10 20:35:48 +02:00
|
|
|
#include <iostream>
|
|
|
|
|
2021-08-27 19:40:48 +02:00
|
|
|
BPMCalculatorWidget::BPMCalculatorWidget(const std::shared_ptr<BPMCalculator>& bpm_calculator, const std::shared_ptr<sf::Font>& font) :
|
2021-08-31 21:09:34 +02:00
|
|
|
Window("BPM Calculation", font),
|
2021-08-27 19:40:48 +02:00
|
|
|
_bpm_calculator(bpm_calculator),
|
2021-09-10 20:35:48 +02:00
|
|
|
_slider(std::make_shared<BPMSlider>()),
|
|
|
|
_ticked(false)
|
2021-08-16 20:54:03 +02:00
|
|
|
{
|
2021-09-01 21:04:18 +02:00
|
|
|
_bpm_value.setFont(*_font);
|
|
|
|
_bpm_value.setCharacterSize(40);
|
|
|
|
_bpm_value.setFillColor(sf::Color::Black);
|
|
|
|
_bpm_value.setString("--");
|
2021-09-10 20:35:48 +02:00
|
|
|
_slap_buffer.loadFromFile("Tick.ogg");
|
|
|
|
_slap.setBuffer(_slap_buffer);
|
|
|
|
_slap.setVolume(30.);
|
2021-08-16 20:54:03 +02:00
|
|
|
}
|
|
|
|
|
2021-08-21 14:29:58 +02:00
|
|
|
void BPMCalculatorWidget::input(const sf::Event& event)
|
2021-08-16 20:54:03 +02:00
|
|
|
{
|
|
|
|
switch (event.type)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
|
2021-08-27 19:40:48 +02:00
|
|
|
case sf::Event::KeyPressed:
|
|
|
|
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Space)
|
2021-08-16 20:54:03 +02:00
|
|
|
{
|
2021-08-27 19:40:48 +02:00
|
|
|
_bpm_calculator->click();
|
2021-08-16 20:54:03 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-08-31 21:09:34 +02:00
|
|
|
Window::input(event);
|
2021-08-16 20:54:03 +02:00
|
|
|
}
|
|
|
|
|
2021-08-27 19:40:48 +02:00
|
|
|
void BPMCalculatorWidget::update(const sf::Time& dt)
|
2021-08-16 20:54:03 +02:00
|
|
|
{
|
2021-08-31 21:09:34 +02:00
|
|
|
Window::update(dt);
|
2021-09-01 21:04:18 +02:00
|
|
|
|
2021-09-10 20:35:48 +02:00
|
|
|
const auto beat_info = _bpm_calculator->fetchApproximatedInfo();
|
2021-09-13 20:50:39 +02:00
|
|
|
if (beat_info.BPM != 0)
|
2021-09-01 21:04:18 +02:00
|
|
|
{
|
2021-09-13 20:50:39 +02:00
|
|
|
_bpm_value.setString(std::to_string(static_cast<int>(beat_info.BPM)));
|
2021-09-08 21:05:56 +02:00
|
|
|
|
|
|
|
const microsec until_beat = _bpm_calculator->fetchTimeUntilNextBeat();
|
2021-09-13 20:50:39 +02:00
|
|
|
const auto time_relation = static_cast<long double>(beat_info.interval) / static_cast<long double>(until_beat);
|
2021-09-10 20:35:48 +02:00
|
|
|
const auto slider_path_left = _slider->rect().width / time_relation;
|
|
|
|
if (slider_path_left < 50)
|
|
|
|
{
|
|
|
|
if (!_ticked)
|
|
|
|
_slap.play();
|
|
|
|
_ticked = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
_ticked = false;
|
2021-09-08 21:05:56 +02:00
|
|
|
|
|
|
|
_slider->setTickPosition(slider_path_left);
|
2021-09-01 21:04:18 +02:00
|
|
|
}
|
2021-08-16 20:54:03 +02:00
|
|
|
}
|
|
|
|
|
2021-08-21 14:29:58 +02:00
|
|
|
void BPMCalculatorWidget::draw(sf::RenderTarget& target, sf::RenderStates states) const
|
2021-08-16 20:54:03 +02:00
|
|
|
{
|
2021-08-31 21:09:34 +02:00
|
|
|
Window::draw(target, states);
|
|
|
|
|
2021-08-27 19:40:48 +02:00
|
|
|
if (_is_visible)
|
2021-09-01 21:04:18 +02:00
|
|
|
{
|
2021-08-31 21:09:34 +02:00
|
|
|
_slider->draw(target, states);
|
2021-09-01 21:04:18 +02:00
|
|
|
_button_start->draw(target, states);
|
2021-09-13 20:50:39 +02:00
|
|
|
_button_stop->draw(target, states);
|
2021-09-01 21:04:18 +02:00
|
|
|
target.draw(_bpm_value, states);
|
|
|
|
}
|
2021-08-16 20:54:03 +02:00
|
|
|
}
|
|
|
|
|
2021-08-21 14:29:58 +02:00
|
|
|
void BPMCalculatorWidget::setRect(const sf::FloatRect& rect)
|
2021-08-16 20:54:03 +02:00
|
|
|
{
|
2021-08-31 21:09:34 +02:00
|
|
|
Window::setRect(rect);
|
|
|
|
_slider->setRect(sf::FloatRect{0, 0, rect.width / 8 * 6, 100});
|
|
|
|
_slider->setPosition({_window_content.getGlobalBounds().left + rect.width / 8,
|
|
|
|
_window_content.getGlobalBounds().top + rect.height / 8 * 3});
|
2021-09-01 21:04:18 +02:00
|
|
|
|
|
|
|
_button_start->setRect(sf::FloatRect{0, 0, rect.width / 10 * 3, 30});
|
|
|
|
_button_start->setPosition({_window_content.getGlobalBounds().left + rect.width / 7,
|
|
|
|
_window_content.getGlobalBounds().top + _window_content.getGlobalBounds().height - 40});
|
|
|
|
|
2021-09-13 20:50:39 +02:00
|
|
|
_button_stop->setRect(sf::FloatRect{0, 0, rect.width / 10 * 3, 30});
|
|
|
|
_button_stop->setPosition({_window_content.getGlobalBounds().left + rect.width / 7,
|
|
|
|
_window_content.getGlobalBounds().top + _window_content.getGlobalBounds().height - 40});
|
|
|
|
|
2021-09-01 21:04:18 +02:00
|
|
|
_bpm_value.setPosition({_window_content.getGlobalBounds().left + rect.width / 8,
|
|
|
|
_window_content.getGlobalBounds().top + rect.height / 8 });
|
2021-08-16 20:54:03 +02:00
|
|
|
}
|
|
|
|
|
2021-08-31 21:09:34 +02:00
|
|
|
void BPMCalculatorWidget::move(const sf::Vector2f &delta)
|
2021-08-16 20:54:03 +02:00
|
|
|
{
|
2021-08-31 21:09:34 +02:00
|
|
|
Window::move(delta);
|
|
|
|
_slider->move(delta);
|
2021-09-01 21:04:18 +02:00
|
|
|
_bpm_value.move(delta);
|
2021-08-16 20:54:03 +02:00
|
|
|
}
|
|
|
|
|
2021-08-31 21:09:34 +02:00
|
|
|
void BPMCalculatorWidget::setPosition(const sf::Vector2f &position)
|
2021-08-16 20:54:03 +02:00
|
|
|
{
|
2021-08-31 21:09:34 +02:00
|
|
|
Window::setPosition(position);
|
2021-09-01 21:04:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void BPMCalculatorWidget::init()
|
|
|
|
{
|
|
|
|
auto& bpm_calculator = _bpm_calculator;
|
|
|
|
|
|
|
|
_button_start = std::make_shared<PushButton>("Start", _font);
|
2021-09-13 20:50:39 +02:00
|
|
|
_button_stop = std::make_shared<PushButton>("Stop", _font);
|
|
|
|
|
|
|
|
_button_start->setCallback([bpm_calculator, button_start=_button_start, button_stop=_button_stop]()
|
2021-09-01 21:04:18 +02:00
|
|
|
{
|
2021-09-13 20:50:39 +02:00
|
|
|
bpm_calculator->music()->play(); // Remove when global play/stop available
|
2021-09-01 21:04:18 +02:00
|
|
|
bpm_calculator->start();
|
|
|
|
button_start->setVisibility(false);
|
2021-09-13 20:50:39 +02:00
|
|
|
button_stop->setVisibility(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
_button_stop->setCallback([bpm_calculator, button_start=_button_start, button_stop=_button_stop]()
|
|
|
|
{
|
|
|
|
bpm_calculator->music()->stop(); // Remove when global play/stop available
|
|
|
|
bpm_calculator->stop();
|
|
|
|
button_start->setVisibility(true);
|
|
|
|
button_stop->setVisibility(false);
|
2021-09-01 21:04:18 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
addChild(_button_start);
|
2021-09-13 20:50:39 +02:00
|
|
|
addChild(_button_stop);
|
|
|
|
|
|
|
|
_button_stop->setVisibility(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BPMCalculatorWidget::setVisibility(bool is_visible)
|
|
|
|
{
|
|
|
|
Window::setVisibility(is_visible);
|
|
|
|
|
|
|
|
bool can_stop = _bpm_calculator->calculating();
|
|
|
|
_button_stop->setVisibility(can_stop);
|
|
|
|
_button_start->setVisibility(!can_stop);
|
2021-08-16 20:54:03 +02:00
|
|
|
}
|