mirror of
https://github.com/OpenSpace/OpenSpace.git
synced 2026-03-09 06:48:35 -05:00
Clean up ScriptScheduler and add doxygen comments
This commit is contained in:
@@ -49,36 +49,84 @@ struct ScheduledScript {
|
||||
|
||||
double time;
|
||||
ReversibleLuaScript script;
|
||||
|
||||
static bool CompareByTime(const ScheduledScript& s1, const ScheduledScript& s2);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Maintains an ordered list of \code ScheduledScripts.
|
||||
* Maintains an ordered list of <code>ScheduledScript</code>s and provides a simple
|
||||
* interface for retrieveing scheduled scripts
|
||||
*/
|
||||
class ScriptScheduler {
|
||||
public:
|
||||
|
||||
/**
|
||||
* Load a schedule from a Lua-file
|
||||
* \param filename Lua file to load
|
||||
* \param L an optional lua_State defining variables that may be used
|
||||
* in the Lua-file.
|
||||
*/
|
||||
void loadScripts(const std::string& filename, lua_State* L = nullptr);
|
||||
|
||||
/**
|
||||
* Load a schedule from a <code>ghoul::Dictionary</code>
|
||||
* \param dict Dictionary to read
|
||||
*/
|
||||
void loadScripts(const ghoul::Dictionary& dict);
|
||||
|
||||
void skipTo(double time);
|
||||
void skipTo(const std::string& timeStr);
|
||||
|
||||
/**
|
||||
* Rewinds the script scheduler to the first scheduled script.
|
||||
*/
|
||||
void rewind();
|
||||
|
||||
/**
|
||||
* Removes all scripts for the schedule.
|
||||
*/
|
||||
void clearSchedule();
|
||||
|
||||
std::queue<std::string> scheduledScripts(double newTime);
|
||||
std::queue<std::string> scheduledScripts(const std::string& timeStr);
|
||||
/**
|
||||
* Progresses the script schedulers time and returns all scripts that has been
|
||||
* scheduled to run between \param newTime and the time provided in the last invocation
|
||||
* of this method.
|
||||
*
|
||||
* \param newTime A j2000 time value specifying the new time stamp that
|
||||
* the script scheduler should progress to.
|
||||
*
|
||||
* \returns the ordered queue of scripts .
|
||||
*/
|
||||
std::queue<std::string> progressTo(double newTime);
|
||||
|
||||
/**
|
||||
* See <code>progressTo(double newTime)</code>.
|
||||
*
|
||||
* \param timeStr A string specifying the a new time stamp that the
|
||||
* scripts scheduler should progress to.
|
||||
*/
|
||||
std::queue<std::string> progressTo(const std::string& timeStr);
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Returns the the j2000 time value that the script scheduler is currently at
|
||||
*/
|
||||
double currentTime() const;
|
||||
|
||||
/**
|
||||
* \returns a vector of all scripts that has been loaded
|
||||
*/
|
||||
const std::vector<ScheduledScript>& allScripts() const;
|
||||
|
||||
const std::vector<ScheduledScript>& allScripts() const { return _scheduledScripts; };
|
||||
|
||||
static LuaLibrary luaLibrary();
|
||||
|
||||
private:
|
||||
|
||||
std::vector<ScheduledScript> _scheduledScripts;
|
||||
|
||||
|
||||
size_t _currentIndex = 0;
|
||||
double _lastTime;
|
||||
double _currentTime;
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -760,7 +760,7 @@ void OpenSpaceEngine::preSynchronization() {
|
||||
Time::ref().advanceTime(dt);
|
||||
Time::ref().preSynchronization();
|
||||
|
||||
auto scheduledScripts = _scriptScheduler->scheduledScripts(Time::ref().currentTime());
|
||||
auto scheduledScripts = _scriptScheduler->progressTo(Time::ref().currentTime());
|
||||
while(scheduledScripts.size()){
|
||||
auto scheduledScript = scheduledScripts.front();
|
||||
LINFO(scheduledScript);
|
||||
|
||||
@@ -63,6 +63,12 @@ ScheduledScript::ScheduledScript(const ghoul::Dictionary& dict)
|
||||
}
|
||||
}
|
||||
|
||||
bool ScheduledScript::CompareByTime(const ScheduledScript& s1, const ScheduledScript& s2){
|
||||
return s1.time < s2.time;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void ScriptScheduler::loadScripts(const std::string& filepath, lua_State* L) {
|
||||
ghoul::Dictionary timedScriptsDict;
|
||||
try {
|
||||
@@ -81,33 +87,30 @@ void ScriptScheduler::loadScripts(const ghoul::Dictionary& dict) {
|
||||
const ghoul::Dictionary& timedScriptDict = dict.value<ghoul::Dictionary>(id);
|
||||
_scheduledScripts.push_back(ScheduledScript(timedScriptDict));
|
||||
}
|
||||
|
||||
// Sort scripts by time
|
||||
std::stable_sort(_scheduledScripts.begin(), _scheduledScripts.end(), &ScheduledScript::CompareByTime);
|
||||
|
||||
// Ensure _currentIndex and _currentTime is accurate after new scripts was added
|
||||
double lastTime = _currentTime;
|
||||
rewind();
|
||||
progressTo(lastTime);
|
||||
}
|
||||
|
||||
void ScriptScheduler::skipTo(double newTime) {
|
||||
if (newTime > _lastTime) {
|
||||
while (_currentIndex < _scheduledScripts.size() && _scheduledScripts[_currentIndex].time <= newTime) {
|
||||
_currentIndex++;
|
||||
}
|
||||
}
|
||||
else {
|
||||
while (0 < _currentIndex && _scheduledScripts[_currentIndex - 1].time > newTime) {
|
||||
_currentIndex--;
|
||||
}
|
||||
}
|
||||
void ScriptScheduler::rewind() {
|
||||
_currentIndex = 0;
|
||||
_currentTime = -DBL_MAX;
|
||||
}
|
||||
|
||||
void ScriptScheduler::skipTo(const std::string& timeStr) {
|
||||
skipTo(SpiceManager::ref().ephemerisTimeFromDate(timeStr));
|
||||
}
|
||||
|
||||
void ScriptScheduler::clearSchedule() {
|
||||
rewind();
|
||||
_scheduledScripts.clear();
|
||||
_currentIndex = 0;
|
||||
}
|
||||
|
||||
std::queue<std::string> ScriptScheduler::scheduledScripts(double newTime) {
|
||||
std::queue<std::string> ScriptScheduler::progressTo(double newTime) {
|
||||
std::queue<std::string> triggeredScripts;
|
||||
if (newTime > _lastTime) {
|
||||
if (newTime > _currentTime) {
|
||||
while(_currentIndex < _scheduledScripts.size() && _scheduledScripts[_currentIndex].time <= newTime){
|
||||
triggeredScripts.push(_scheduledScripts[_currentIndex].script.forwardScript);
|
||||
_currentIndex++;
|
||||
@@ -120,14 +123,23 @@ std::queue<std::string> ScriptScheduler::scheduledScripts(double newTime) {
|
||||
}
|
||||
}
|
||||
|
||||
_lastTime = newTime;
|
||||
_currentTime = newTime;
|
||||
return triggeredScripts;
|
||||
}
|
||||
|
||||
std::queue<std::string> ScriptScheduler::scheduledScripts(const std::string& timeStr) {
|
||||
return std::move(scheduledScripts(SpiceManager::ref().ephemerisTimeFromDate(timeStr)));
|
||||
std::queue<std::string> ScriptScheduler::progressTo(const std::string& timeStr) {
|
||||
return std::move(progressTo(SpiceManager::ref().ephemerisTimeFromDate(timeStr)));
|
||||
}
|
||||
|
||||
double ScriptScheduler::currentTime() const {
|
||||
return _currentTime;
|
||||
};
|
||||
|
||||
const std::vector<ScheduledScript>& ScriptScheduler::allScripts() const {
|
||||
return _scheduledScripts;
|
||||
};
|
||||
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////
|
||||
// Lua library functions //
|
||||
@@ -148,20 +160,6 @@ namespace luascriptfunctions {
|
||||
OsEng.scriptScheduler().loadScripts(missionFileName, L);
|
||||
}
|
||||
|
||||
int skipTo(lua_State* L) {
|
||||
using ghoul::lua::luaTypeToString;
|
||||
int nArguments = lua_gettop(L);
|
||||
if (nArguments != 1)
|
||||
return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments);
|
||||
|
||||
std::string dateStr = luaL_checkstring(L, -1);
|
||||
if (dateStr.empty()) {
|
||||
return luaL_error(L, "date string is empty");
|
||||
}
|
||||
|
||||
OsEng.scriptScheduler().skipTo(dateStr);
|
||||
}
|
||||
|
||||
int clear(lua_State* L) {
|
||||
using ghoul::lua::luaTypeToString;
|
||||
int nArguments = lua_gettop(L);
|
||||
@@ -184,12 +182,6 @@ LuaLibrary ScriptScheduler::luaLibrary() {
|
||||
"string",
|
||||
"Load timed scripts from file"
|
||||
},
|
||||
{
|
||||
"skipTo",
|
||||
&luascriptfunctions::skipTo,
|
||||
"string",
|
||||
"skip to a time without executing scripts"
|
||||
},
|
||||
{
|
||||
"clear",
|
||||
&luascriptfunctions::clear,
|
||||
|
||||
Reference in New Issue
Block a user