diff --git a/include/openspace/util/time.h b/include/openspace/util/time.h index 67a5d49cbc..b02763709d 100644 --- a/include/openspace/util/time.h +++ b/include/openspace/util/time.h @@ -126,6 +126,24 @@ public: */ double deltaTime() const; + /** + * Sets the pause function, i.e. setting the deltaTime to 0 (pause = + * true) and restoring it when the function is called with a parameter of + * false. + * \param If true, the simulation time stops; if false, the + * simulation time continues at the previous rate + */ + void setPause(bool pause); + + /** + * Toggles the pause function, i.e. setting the deltaTime to 0 and restoring it when + * the function is called a second time. It returns the pause state (true + * if the time is now paused, false otherwise) + * \return The new pause state (true if the time is now paused, + * false otherwise) + */ + bool togglePause(); + /** * Advances the simulation time using the deltaTime() and the tickTime. * The deltaTime() is the number of simulation seconds that pass for each real-time @@ -138,18 +156,6 @@ public: */ double advanceTime(double tickTime); - /** - * Retreats the simulation time using the deltaTime() and the tickTime. - * The deltaTime() is the number of simulation seconds that pass for each real-time - * second. tickTime is the number of real-time seconds that passed since - * the last call to this method. If this method is called in the render loop, the - * tickTime should be equivalent to the frame time. - * \param tickTime The number of real-time seconds that passed since the last call - * to this method - * \return The new time value after retreating the time - */ - double retreatTime(double tickTime); - void serialize(SyncBuffer* syncBuffer); void deserialize(SyncBuffer* syncBuffer); @@ -192,6 +198,7 @@ private: double _time; ///< The time stored as the number of seconds past the J2000 epoch double _dt; bool _timeJumped; + bool _timePaused; bool _jockeHasToFixThisLater; //shared copies diff --git a/openspace-data b/openspace-data index 6fd23d3aa3..68bea64699 160000 --- a/openspace-data +++ b/openspace-data @@ -1 +1 @@ -Subproject commit 6fd23d3aa33f499d79a552379589a20b9cb43964 +Subproject commit 68bea6469987edc1eb6140f44a8f035eabd2e00b diff --git a/scripts/bind_keys.lua b/scripts/bind_keys.lua index 1280bfb03f..380dc2fd80 100644 --- a/scripts/bind_keys.lua +++ b/scripts/bind_keys.lua @@ -12,6 +12,8 @@ openspace.bindKey("F11", "openspace.fadeOut(2)") openspace.bindKey("F12", "openspace.fadeIn(2)") openspace.bindKey("PRINT_SCREEN", "openspace.takeScreenshot()") +openspace.bindKey("SPACE", "openspace.time.togglePause()") + -- Bookmarks for the New Horizons encounter openspace.bindKey("1", "openspace.time.setTime('2007-02-27T16:40:00.00'); openspace.time.setDeltaTime(10)") openspace.bindKey("2", "openspace.time.setTime('2015-07-14T10:50:00.00'); openspace.time.setDeltaTime(10)") diff --git a/src/interaction/interactionhandler.cpp b/src/interaction/interactionhandler.cpp index 00c209deb4..9ac16253d6 100644 --- a/src/interaction/interactionhandler.cpp +++ b/src/interaction/interactionhandler.cpp @@ -905,7 +905,7 @@ void InteractionHandler::keyboardCallback(int key, int action) { Time::ref().advanceTime(sgct::Engine::instance()->getDt()); } if (key == SGCT_KEY_X) { - Time::ref().retreatTime(sgct::Engine::instance()->getDt()); + Time::ref().advanceTime(-sgct::Engine::instance()->getDt()); } if (key == 262) { glm::vec3 euler(0.0, speed * dt*0.4, 0.0); diff --git a/src/util/time.cpp b/src/util/time.cpp index f568680c6c..3a002a308e 100644 --- a/src/util/time.cpp +++ b/src/util/time.cpp @@ -80,6 +80,32 @@ int time_deltaTime(lua_State* L) { return 1; } +/** + * \ingroup LuaScripts + * togglePause(): + * Toggles a pause functionm i.e. setting the delta time to 0 and restoring it afterwards + */ +int time_togglePause(lua_State* L) { + openspace::Time::ref().togglePause(); + return 0; +} + +/** + * \ingroup LuaScripts + * togglePause(): + * Toggles a pause functionm i.e. setting the delta time to 0 and restoring it afterwards + */ +int time_setPause(lua_State* L) { + int nArguments = lua_gettop(L); + if (nArguments != 1) + return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments); + + bool pause = lua_toboolean(L, -1); + openspace::Time::ref().setPause(pause); + return 0; +} + + /** * \ingroup LuaScripts * setTime({number, string}): @@ -149,6 +175,7 @@ Time::Time() : _time(-1.0) , _dt(1.0) , _timeJumped(false) + , _timePaused(false) , _syncedTime(-1.0) , _syncedDt(1.0) , _syncedTimeJumped(false) @@ -192,11 +219,10 @@ double Time::currentTime() const { } double Time::advanceTime(double tickTime) { - return _time += _dt * tickTime; -} - -double Time::retreatTime(double tickTime) { - return _time -= _dt * tickTime; + if (_timePaused) + return _time; + else + return _time += _dt * tickTime; } void Time::setDeltaTime(double deltaT) { @@ -208,11 +234,18 @@ double Time::deltaTime() const { return _syncedDt; } +void Time::setPause(bool pause) { + _timePaused = pause; +} + +bool Time::togglePause() { + _timePaused = !_timePaused; + return _timePaused; +} + void Time::setTime(std::string time) { SpiceManager::ref().getETfromDate(std::move(time), _time); _timeJumped = true; - // Add callback to OpenSpaceEngine that signals that the next update phase - // needs total invalidation ---abock } std::string Time::currentTimeUTC() const { @@ -298,6 +331,19 @@ scripting::ScriptEngine::LuaLibrary Time::luaLibrary() { "Returns the amount of simulated time that passes in one " "second of real time" }, + { + "setPause", + &luascriptfunctions::time_setPause, + "bool", + "Pauses the simulation time or restores the delta time" + }, + { + "togglePause", + &luascriptfunctions::time_togglePause, + "", + "Toggles the pause function, i.e. temporarily setting the delta time to 0" + " and restoring it afterwards" + }, { "setTime", &luascriptfunctions::time_setTime,