25 lines
1013 B
C++
25 lines
1013 B
C++
#pragma once
|
|
#include <SFML/Window/Event.hpp>
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
namespace fluidsim::port::desktop {
|
|
class PlaybackControl {
|
|
public:
|
|
bool realtime() const {return realtime_;}
|
|
double target_speed() const {return realtime_?1.0:manual_speed_;}
|
|
bool handle_event(const sf::Event& event,bool shift=false) {
|
|
if(const auto* key=event.getIf<sf::Event::KeyPressed>()) {
|
|
if(key->code==sf::Keyboard::Key::Space){realtime_=!realtime_;return true;}
|
|
if(!realtime_&&key->code==sf::Keyboard::Key::PageUp){adjust(1);return true;}
|
|
if(!realtime_&&key->code==sf::Keyboard::Key::PageDown){adjust(-1);return true;}
|
|
}
|
|
if(const auto* wheel=event.getIf<sf::Event::MouseWheelScrolled>();wheel&&!realtime_&&shift) {adjust(wheel->delta);return true;}
|
|
return false;
|
|
}
|
|
private:
|
|
bool realtime_{true};
|
|
double manual_speed_{1};
|
|
void adjust(double steps){manual_speed_=std::clamp(manual_speed_*std::exp(steps*.12),.0001,1.0);}
|
|
};
|
|
}
|