Add list of delta time steps, stepping functions and keybindings

This commit is contained in:
Emma Broman
2020-08-06 11:23:31 +02:00
parent 0b34e32ba7
commit 457dabe9b7
7 changed files with 228 additions and 0 deletions
+31
View File
@@ -45,6 +45,7 @@ namespace {
constexpr const char* headerProperty = "#Property";
constexpr const char* headerKeybinding = "#Keybinding";
constexpr const char* headerTime = "#Time";
constexpr const char* headerDeltaTimes = "#DeltaTimes";
constexpr const char* headerCamera = "#Camera";
constexpr const char* headerMarkNodes = "#MarkNodes";
constexpr const char* headerAdditionalScripts = "#AdditionalScripts";
@@ -91,6 +92,7 @@ namespace {
Property,
Keybinding,
Time,
DeltaTimes,
Camera,
MarkNodes,
AdditionalScripts
@@ -104,6 +106,7 @@ namespace {
if (line == headerProperty) { return Section::Property; }
if (line == headerKeybinding) { return Section::Keybinding; }
if (line == headerTime) { return Section::Time; }
if (line == headerDeltaTimes) { return Section::DeltaTimes; }
if (line == headerCamera) { return Section::Camera; }
if (line == headerMarkNodes) { return Section::MarkNodes; }
if (line == headerAdditionalScripts) { return Section::AdditionalScripts; }
@@ -318,6 +321,10 @@ namespace {
return time;
}
[[ nodiscard ]] std::string parseDeltaTimes(const std::string& line, int) {
return line;
}
[[ nodiscard ]] Profile::CameraType parseCamera(const std::string& line, int lineNumber) {
std::vector<std::string> fields = ghoul::tokenizeString(line, '\t');
Profile::CameraType camera = [&](const std::string& type) ->
@@ -480,6 +487,8 @@ void Profile::saveCurrentSettingsToProfile(const properties::PropertyOwner& root
t.type = Time::Type::Absolute;
time = std::move(t);
// TODO: Set delta time steps
// Camera
CameraNavState c;
@@ -648,6 +657,13 @@ std::string Profile::serialize() const {
}
}
if (!deltaTimes.empty()) {
output += fmt::format("\n{}\n", headerDeltaTimes);
for (const std::string& d : deltaTimes) {
output += fmt::format("{}\n", d);
}
}
if (camera.has_value()) {
output += fmt::format("\n{}\n", headerCamera);
output += std::visit(
@@ -870,6 +886,12 @@ Profile::Profile(const std::vector<std::string>& content) {
time = parseTime(line, lineNum);
foundTime = true;
break;
case Section::DeltaTimes:
{
std::string d = parseDeltaTimes(line, lineNum);
deltaTimes.push_back(std::move(d));
break;
}
case Section::Camera:
if (foundCamera) {
throw ProfileParsingError(
@@ -981,6 +1003,15 @@ std::string Profile::convertToScene() const {
throw ghoul::MissingCaseException();
}
// Delta Times
{
std::string times;
for (const std::string& d : deltaTimes) {
times += fmt::format("{} ,", d);
}
output += fmt::format("openspace.time.setDeltaTimeSteps({{ {} }});\n", times);
}
// Mark Nodes
{
std::string nodes;
+27
View File
@@ -126,6 +126,15 @@ scripting::LuaLibrary Time::luaLibrary() {
"Sets the amount of simulation time that happens "
"in one second of real time"
},
{
"setDeltaTimeSteps",
&luascriptfunctions::time_setDeltaTimeSteps,
{},
"List of numbers",
"Sets the list of discrete delta time steps for the simulation speed "
"that can be quickly jumped between. The list will be sorted to be in "
"increasing order."
},
{
"deltaTime",
&luascriptfunctions::time_deltaTime,
@@ -177,6 +186,24 @@ scripting::LuaLibrary Time::luaLibrary() {
"Sets the amount of simulation time that happens "
"in one second of real time"
},
{
"interpolateNextDeltaTimeStep",
&luascriptfunctions::time_interpolateNextDeltaTimeStep,
{},
"[number]",
"Interpolate the simulation speed to the next delta time step in the "
"list. If an input value is given, the interpolation is done over the "
"specified number of seconds."
},
{
"interpolatePreviousDeltaTimeStep",
&luascriptfunctions::time_interpolatePreviousDeltaTimeStep,
{},
"[number]",
"Interpolate the simulation speed to the previous delta time step in "
"the list. If an input value is given, the interpolation is done over "
"the specified number of seconds."
},
{
"interpolatePause",
&luascriptfunctions::time_interpolatePause,
+107
View File
@@ -69,6 +69,113 @@ int time_setDeltaTime(lua_State* L) {
return 0;
}
/**
* \ingroup LuaScripts
* interpolateNextDeltaTimeStep(list of numbers):
* Sets the list of discrete delta time steps for the simulation speed.
*/
int time_setDeltaTimeSteps(lua_State* L) {
ghoul::lua::checkArgumentsAndThrow(L, 1, "lua::time_setDeltaTimeSteps");
ghoul::Dictionary dict;
ghoul::lua::luaDictionaryFromState(L, dict);
const size_t nItems = dict.size();
std::vector<double> inputDeltaTimes;
inputDeltaTimes.reserve(nItems);
for (size_t i = 1; i <= nItems; ++i) {
const double time = dict.value<double>(std::to_string(i));
// TODO: test valid time? Or interpret non-second values
inputDeltaTimes.push_back(time);
}
lua_pop(L, 1);
global::timeManager.setDeltaTimeSteps(inputDeltaTimes);
lua_settop(L, 0);
ghoul_assert(lua_gettop(L) == 0, "Incorrect number of items left on stack");
return 0;
}
/**
* \ingroup LuaScripts
* interpolateNextDeltaTimeStep([interpolationDuration]):
* Interpolate the simulation speed to the next delta time step in the list. If an input
* value is given, the interpolation is done over the specified number of seconds.
*/
int time_interpolateNextDeltaTimeStep(lua_State* L) {
ghoul::lua::checkArgumentsAndThrow(
L,
{ 0, 1 },
"lua::time_interpolateNextDeltaTimeStep"
);
double interpolationDuration =
global::timeManager.defaultDeltaTimeInterpolationDuration();
const int nArguments = lua_gettop(L);
if (nArguments == 1) {
const bool durationIsNumber = (lua_isnumber(L, 1) != 0);
if (!durationIsNumber) {
lua_settop(L, 0);
const char* msg = lua_pushfstring(
L,
"%s expected, got %s",
lua_typename(L, LUA_TNUMBER),
luaL_typename(L, -1)
);
return luaL_error(L, "bad argument #%d (%s)", 1, msg);
}
interpolationDuration = lua_tonumber(L, 1);
}
global::timeManager.interpolateNextDeltaTimeStep(interpolationDuration);
lua_settop(L, 0);
ghoul_assert(lua_gettop(L) == 0, "Incorrect number of items left on stack");
return 0;
}
/**
* \ingroup LuaScripts
* interpolatePreviousDeltaTimeStep([interpolationDuration]):
* Interpolate the simulation speed to the previous delta time step in the list. If an
* input value is given, the interpolation is done over the specified number of seconds.
*/
int time_interpolatePreviousDeltaTimeStep(lua_State* L) {
ghoul::lua::checkArgumentsAndThrow(
L,
{ 0, 1 },
"lua::time_interpolatePreviousDeltaTimeStep"
);
double interpolationDuration =
global::timeManager.defaultDeltaTimeInterpolationDuration();
const int nArguments = lua_gettop(L);
if (nArguments == 1) {
const bool durationIsNumber = (lua_isnumber(L, 1) != 0);
if (!durationIsNumber) {
lua_settop(L, 0);
const char* msg = lua_pushfstring(
L,
"%s expected, got %s",
lua_typename(L, LUA_TNUMBER),
luaL_typename(L, -1)
);
return luaL_error(L, "bad argument #%d (%s)", 1, msg);
}
interpolationDuration = lua_tonumber(L, 1);
}
global::timeManager.interpolatePreviousDeltaTimeStep(interpolationDuration);
lua_settop(L, 0);
ghoul_assert(lua_gettop(L) == 0, "Incorrect number of items left on stack");
return 0;
}
/**
* \ingroup LuaScripts
* time_interpolateDeltaTime(number [, number]):
+36
View File
@@ -380,6 +380,11 @@ void TimeManager::setDeltaTime(double deltaTime) {
interpolateDeltaTime(deltaTime, 0.0);
}
void TimeManager::setDeltaTimeSteps(std::vector<double> deltaTimes) {
std::sort(deltaTimes.begin(), deltaTimes.end());
_deltaTimeSteps = std::move(deltaTimes);
}
size_t TimeManager::nKeyframes() const {
return _timeline.nKeyframes();
}
@@ -553,6 +558,37 @@ void TimeManager::interpolateDeltaTime(double newDeltaTime, double interpolation
addKeyframe(now + interpolationDuration, futureKeyframe);
}
void TimeManager::interpolateNextDeltaTimeStep(double durationSeconds) {
std::vector<double>::iterator nextStepIterator = std::upper_bound(
_deltaTimeSteps.begin(),
_deltaTimeSteps.end(),
_deltaTime
);
if (nextStepIterator == _deltaTimeSteps.end()) {
// Current delta time is larger than last step
return;
}
interpolateDeltaTime(*nextStepIterator, durationSeconds);
}
void TimeManager::interpolatePreviousDeltaTimeStep(double durationSeconds) {
std::vector<double>::iterator lowerBoundIterator = std::lower_bound(
_deltaTimeSteps.begin(),
_deltaTimeSteps.end(),
_deltaTime
);
if (lowerBoundIterator == _deltaTimeSteps.begin()) {
// Current delta time is smaller than first step
return;
}
std::vector<double>::iterator prevStepIterator = lowerBoundIterator - 1;
interpolateDeltaTime(*prevStepIterator, durationSeconds);
}
void TimeManager::setPause(bool pause) {
interpolatePause(pause, 0);
}