Adding a function to return whether OpenSpace currently is paused

This commit is contained in:
Alexander Bock
2024-04-04 17:09:34 +02:00
parent 9558f2bcbb
commit 4049501406
6 changed files with 61 additions and 47 deletions

View File

@@ -41,18 +41,18 @@
namespace openspace {
struct TimeKeyframeData {
Time time;
double delta = 0.0;
bool pause = false;
bool jump = false;
};
class TimeManager : public properties::PropertyOwner {
public:
using CallbackHandle = int;
using TimeChangeCallback = std::function<void()>;
struct TimeKeyframeData {
Time time;
double delta = 0.0;
bool pause = false;
bool jump = false;
};
TimeManager();
const Time& time() const;
@@ -137,11 +137,8 @@ private:
void addDeltaTimesKeybindings();
void clearDeltaTimesKeybindings();
double currentApplicationTimeForInterpolation() const;
double previousApplicationTimeForInterpolation() const;
bool isPlayingBackSessionRecording() const;
Timeline<TimeKeyframeData> _timeline;
SyncData<Time> _currentTime;
SyncData<Time> _integrateFromTime;

View File

@@ -230,9 +230,10 @@ void GuiSpaceTimeComponent::render() {
const float duration = global::timeManager->defaultTimeInterpolationDuration();
const TimeKeyframeData predictedTime = global::timeManager->interpolate(
global::windowDelegate->applicationTime() + duration
);
const TimeManager::TimeKeyframeData predictedTime =
global::timeManager->interpolate(
global::windowDelegate->applicationTime() + duration
);
const double j2000 = predictedTime.time.j2000Seconds();
const long long seconds = duration_cast<std::chrono::seconds>(
std::chrono::hours(24) * std::abs(days)

View File

@@ -367,7 +367,7 @@ void ParallelPeer::dataMessageReceived(const std::vector<char>& message) {
for (const datamessagestructures::TimeKeyframe& kfMessage : keyframesMessage)
{
TimeKeyframeData timeKeyframeData;
TimeManager::TimeKeyframeData timeKeyframeData;
timeKeyframeData.delta = kfMessage._dt;
timeKeyframeData.pause = kfMessage._paused;
timeKeyframeData.time = Time(kfMessage._time);
@@ -732,8 +732,9 @@ void ParallelPeer::sendCameraKeyframe() {
void ParallelPeer::sendTimeTimeline() {
// Create a keyframe with current position and orientation of camera
const Timeline<TimeKeyframeData>& timeline = global::timeManager->timeline();
std::deque<Keyframe<TimeKeyframeData>> keyframes = timeline.keyframes();
const Timeline<TimeManager::TimeKeyframeData>& timeline =
global::timeManager->timeline();
std::deque<Keyframe<TimeManager::TimeKeyframeData>> keyframes = timeline.keyframes();
datamessagestructures::TimeTimeline timelineMessage;
timelineMessage._clear = true;
@@ -741,7 +742,7 @@ void ParallelPeer::sendTimeTimeline() {
// Case 1: Copy all keyframes from the native timeline
for (size_t i = 0; i < timeline.nKeyframes(); i++) {
const Keyframe<TimeKeyframeData>& kf = keyframes.at(i);
const Keyframe<TimeManager::TimeKeyframeData>& kf = keyframes.at(i);
datamessagestructures::TimeKeyframe kfMessage;
kfMessage._time = kf.data.time.j2000Seconds();

View File

@@ -212,6 +212,7 @@ scripting::LuaLibrary Time::luaLibrary() {
codegen::lua::PauseToggleViaKeyboard,
codegen::lua::SetPause,
codegen::lua::InterpolatePause,
codegen::lua::IsPaused,
codegen::lua::SetTime,
codegen::lua::InterpolateTime,
codegen::lua::InterpolateTimeRelative,

View File

@@ -191,6 +191,13 @@ namespace {
global::timeManager->interpolatePause(isPaused, interp);
}
/**
* Returns whether the simulation time is currently paused or is progressing.
*/
[[codegen::luawrap]] bool isPaused() {
return openspace::global::timeManager->isPaused();
}
/**
* Sets the current simulation time to the specified value. If the parameter is a number,
* the value is the number of seconds past the J2000 epoch. If it is a string, it has to

View File

@@ -77,13 +77,33 @@ namespace {
constexpr std::string_view DeltaTimeStepsGuiPath = "/Time/Simulation Speed/Steps";
constexpr std::string_view DeltaTimeActionPrefix = "core.time.delta_time";
bool isPlayingBackSessionRecording() {
using namespace openspace;
return (global::openSpaceEngine->currentMode() ==
OpenSpaceEngine::Mode::SessionRecordingPlayback);
}
double currentApplicationTimeForInterpolation() {
using namespace openspace;
if (global::sessionRecording->isSavingFramesDuringPlayback()) {
return global::sessionRecording->currentApplicationInterpolationTime();
}
else {
return global::windowDelegate->applicationTime();
}
}
} // namespace
namespace openspace {
TimeManager::TimeManager()
: properties::PropertyOwner({ "TimeManager", "Time Manager" })
, _defaultTimeInterpolationDuration(DefaultTimeInterpolationDurationInfo,
, _defaultTimeInterpolationDuration(
DefaultTimeInterpolationDurationInfo,
2.f,
0.f,
5.f
@@ -125,13 +145,13 @@ void TimeManager::interpolateTime(double targetTime, double durationSeconds) {
const double now = currentApplicationTimeForInterpolation();
const bool pause = isPaused();
const TimeKeyframeData current = {
TimeKeyframeData current = {
.time = time(),
.delta = deltaTime(),
.pause = false,
.jump = false
};
const TimeKeyframeData next = {
TimeKeyframeData next = {
.time = Time(targetTime),
.delta = targetDeltaTime(),
.pause = pause,
@@ -139,8 +159,8 @@ void TimeManager::interpolateTime(double targetTime, double durationSeconds) {
};
clearKeyframes();
addKeyframe(now, current);
addKeyframe(now + durationSeconds, next);
addKeyframe(now, std::move(current));
addKeyframe(now + durationSeconds, std::move(next));
}
void TimeManager::interpolateTimeRelative(double delta, double durationSeconds) {
@@ -211,7 +231,7 @@ void TimeManager::preSynchronization(double dt) {
_previousApplicationTime = currentApplicationTimeForInterpolation();
}
TimeKeyframeData TimeManager::interpolate(double applicationTime) {
TimeManager::TimeKeyframeData TimeManager::interpolate(double applicationTime) {
const std::deque<Keyframe<TimeKeyframeData>>& keyframes = _timeline.keyframes();
auto firstFutureKeyframe = std::lower_bound(
@@ -345,9 +365,10 @@ void TimeManager::progressTime(double dt) {
}
}
TimeKeyframeData TimeManager::interpolate(const Keyframe<TimeKeyframeData>& past,
const Keyframe<TimeKeyframeData>& future,
double time)
TimeManager::TimeKeyframeData TimeManager::interpolate(
const Keyframe<TimeKeyframeData>& past,
const Keyframe<TimeKeyframeData>& future,
double time)
{
// https://en.wikipedia.org/wiki/Spline_interpolation
// interpolatedTime = (1 - t)y1 + t*y2 + t(1 - t)(a(1 - t) + bt), where
@@ -592,7 +613,7 @@ const Time& TimeManager::integrateFromTime() const {
return _integrateFromTime;
}
const Timeline<TimeKeyframeData>& TimeManager::timeline() const {
const Timeline<TimeManager::TimeKeyframeData>& TimeManager::timeline() const {
return _timeline;
}
@@ -613,8 +634,8 @@ TimeManager::CallbackHandle TimeManager::addDeltaTimeChangeCallback(TimeChangeCa
return handle;
}
TimeManager::CallbackHandle
TimeManager::addDeltaTimeStepsChangeCallback(TimeChangeCallback cb)
TimeManager::CallbackHandle TimeManager::addDeltaTimeStepsChangeCallback(
TimeChangeCallback cb)
{
const CallbackHandle handle = _nextCallbackHandle++;
_deltaTimeStepsChangeCallbacks.emplace_back(handle, std::move(cb));
@@ -768,13 +789,13 @@ void TimeManager::interpolateDeltaTime(double newDeltaTime, double interpolation
time().j2000Seconds() + (_deltaTime + newDeltaTime) * 0.5 * interpolationDuration
);
const TimeKeyframeData currentKeyframe = {
TimeKeyframeData currentKeyframe = {
.time = time(),
.delta = _deltaTime,
.pause = false,
.jump = false
};
const TimeKeyframeData futureKeyframe = {
TimeKeyframeData futureKeyframe = {
.time = newTime,
.delta = newDeltaTime,
.pause = false,
@@ -787,8 +808,8 @@ void TimeManager::interpolateDeltaTime(double newDeltaTime, double interpolation
previousApplicationTimeForInterpolation() :
currentApplicationTimeForInterpolation();
addKeyframe(now, currentKeyframe);
addKeyframe(now + interpolationDuration, futureKeyframe);
addKeyframe(now, std::move(currentKeyframe));
addKeyframe(now + interpolationDuration, std::move(futureKeyframe));
}
std::optional<double> TimeManager::nextDeltaTimeStep() {
@@ -914,15 +935,6 @@ void TimeManager::interpolatePause(bool pause, double interpolationDuration) {
addKeyframe(now + interpolationDuration, futureKeyframe);
}
double TimeManager::currentApplicationTimeForInterpolation() const {
if (global::sessionRecording->isSavingFramesDuringPlayback()) {
return global::sessionRecording->currentApplicationInterpolationTime();
}
else {
return global::windowDelegate->applicationTime();
}
}
double TimeManager::previousApplicationTimeForInterpolation() const {
// If playing back with frames, this function needs to be called when a time rate
// interpolation (either speed change or pause) begins and ends. If the application
@@ -935,11 +947,6 @@ double TimeManager::previousApplicationTimeForInterpolation() const {
return _previousApplicationTime;
}
bool TimeManager::isPlayingBackSessionRecording() const {
return (global::openSpaceEngine->currentMode() ==
OpenSpaceEngine::Mode::SessionRecordingPlayback);
}
void TimeManager::setTimeFromProfile(const Profile& p) {
if (p.time.has_value()) {
switch (p.time->type) {