#include "bpmcalculatorwidget.h" #include "tools/bpmcalculator.h" #include "core/editor.h" #include BPMCalculatorWidget::BPMCalculatorWidget(const std::shared_ptr& bpm_calculator, const std::shared_ptr& font) : Window("BPM Calculation", font), _bpm_calculator(bpm_calculator), _slider(std::make_shared()), _ticked(false) { _bpm_value.setFont(*_font); _bpm_value.setCharacterSize(40); _bpm_value.setFillColor(sf::Color::Black); _bpm_value.setString("--"); _slap_buffer.loadFromFile("Tick.ogg"); _slap.setBuffer(_slap_buffer); _slap.setVolume(30.); } void BPMCalculatorWidget::input(const sf::Event& event) { switch (event.type) { default: break; case sf::Event::KeyPressed: if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Space) { _bpm_calculator->click(); } break; } Window::input(event); } void BPMCalculatorWidget::update(const sf::Time& dt) { Window::update(dt); const auto beat_info = _bpm_calculator->fetchApproximatedInfo(); if (beat_info.BPM != 0) { _bpm_value.setString(std::to_string(static_cast(beat_info.BPM))); const microsec until_beat = _bpm_calculator->fetchTimeUntilNextBeat(); const auto time_relation = static_cast(beat_info.interval) / static_cast(until_beat); const auto slider_path_left = _slider->rect().width / time_relation; if (slider_path_left < 50) { if (!_ticked) _slap.play(); _ticked = true; } else _ticked = false; _slider->setTickPosition(slider_path_left); } } void BPMCalculatorWidget::draw(sf::RenderTarget& target, sf::RenderStates states) const { Window::draw(target, states); if (_is_visible) { _slider->draw(target, states); _button_start->draw(target, states); _button_stop->draw(target, states); _button_apply->draw(target, states); target.draw(_bpm_value, states); } } void BPMCalculatorWidget::setRect(const sf::FloatRect& rect) { 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}); _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}); _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}); _button_apply->setRect(sf::FloatRect{0, 0, rect.width / 10 * 3, 30}); _button_apply->setPosition({_window_content.getGlobalBounds().left + 20 + (2 * (rect.width / 7)), _window_content.getGlobalBounds().top + _window_content.getGlobalBounds().height - 40}); _bpm_value.setPosition({_window_content.getGlobalBounds().left + rect.width / 8, _window_content.getGlobalBounds().top + rect.height / 8 }); } void BPMCalculatorWidget::move(const sf::Vector2f &delta) { Window::move(delta); _slider->move(delta); _bpm_value.move(delta); } void BPMCalculatorWidget::setPosition(const sf::Vector2f &position) { Window::setPosition(position); } void BPMCalculatorWidget::init(const std::unique_ptr& _editor) { auto& bpm_calculator = _bpm_calculator; _button_start = std::make_shared("Start", _font); _button_stop = std::make_shared("Stop", _font); _button_apply = std::make_shared("Apply", _font); _button_start->setCallback([bpm_calculator, button_start=_button_start, button_stop=_button_stop]() { bpm_calculator->music()->play(); // Remove when global play/stop available bpm_calculator->start(); button_start->setVisibility(false); 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); }); _button_apply->setCallback([&_editor, bpm_calculator]() { BPMSection section; section.bpm = bpm_calculator->fetchApproximatedInfo().BPM; section.fraction = 2; section.offset_start = bpm_calculator->getStartingOffset(); _editor->insertBPMSection(std::move(section)); }); addChild(_button_start); addChild(_button_stop); addChild(_button_apply); _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 && is_visible); _button_start->setVisibility(!can_stop && is_visible); }