diff --git a/include/openspace/events/event.h b/include/openspace/events/event.h index a4eb4efe61..5faac274b1 100644 --- a/include/openspace/events/event.h +++ b/include/openspace/events/event.h @@ -80,6 +80,7 @@ struct Event { CameraPathStarted, CameraPathFinished, CameraMovedPosition, + ScheduledScriptExecuted, Custom, Last // sentinel value }; @@ -574,6 +575,20 @@ struct EventCameraMovedPosition : public Event { EventCameraMovedPosition(); }; +/** + * This event is created when a scheduled script is executed. + */ +struct EventScheduledScriptExecuted : public Event { + static constexpr Type Type = Event::Type::ScheduledScriptExecuted; + + /** + * Creates an instance of an ScheduledScriptExecuted event. + */ + EventScheduledScriptExecuted(std::string_view script_); + + const tstring script; +}; + /** * A custom event type that can be used in a pinch when no explicit event type is * available. This should only be used in special circumstances and it should be diff --git a/src/engine/openspaceengine.cpp b/src/engine/openspaceengine.cpp index ca649bc1f0..ec5161bff6 100644 --- a/src/engine/openspaceengine.cpp +++ b/src/engine/openspaceengine.cpp @@ -998,11 +998,19 @@ void OpenSpaceEngine::preSynchronization() { global::timeManager->time().j2000Seconds() ); for (const std::string& script : schedScripts) { + if (script.empty()) { + continue; + } + global::scriptEngine->queueScript( script, scripting::ScriptEngine::ShouldBeSynchronized::Yes, scripting::ScriptEngine::ShouldSendToRemote::Yes ); + + global::eventEngine->publishEvent( + script + ); } global::renderEngine->updateScene(); diff --git a/src/events/event.cpp b/src/events/event.cpp index da0a6ca00d..8119c012bb 100644 --- a/src/events/event.cpp +++ b/src/events/event.cpp @@ -211,6 +211,11 @@ void log(int i, const EventCameraMovedPosition& e) { LINFO(std::format("[{}] EventCameraMovedPosition", i)); } +void log(int i, const EventScheduledScriptExecuted& e) { + ghoul_assert(e.type == EventScheduledScriptExecuted::Type, "Wrong type"); + LINFO(std::format("[{}] ScheduledScriptExecuted: Script '{}'", i, e.script)); +} + void log(int i, const CustomEvent& e) { ghoul_assert(e.type == CustomEvent::Type, "Wrong type"); LINFO(std::format("[{}] CustomEvent: {} ({})", i, e.subtype, e.payload)); @@ -241,6 +246,7 @@ std::string_view toString(Event::Type type) { case Event::Type::CameraPathStarted: return "CameraPathStarted"; case Event::Type::CameraPathFinished: return "CameraPathFinished"; case Event::Type::CameraMovedPosition: return "CameraMovedPosition"; + case Event::Type::ScheduledScriptExecuted: return "ScheduledScriptExecuted"; case Event::Type::Custom: return "Custom"; default: throw ghoul::MissingCaseException(); @@ -314,6 +320,9 @@ Event::Type fromString(std::string_view str) { else if (str == "CameraMovedPosition") { return Event::Type::CameraMovedPosition; } + else if (str == "ScheduledScriptExecuted") { + return Event::Type::ScheduledScriptExecuted; + } else if (str == "Custom") { return Event::Type::Custom; } @@ -508,6 +517,12 @@ ghoul::Dictionary toParameter(const Event& e) { std::string(static_cast(e).destination) ); break; + case Event::Type::ScheduledScriptExecuted: + d.setValue( + "Script", + std::string(static_cast(e).script) + ); + break; case Event::Type::Custom: d.setValue( "Subtype", std::string(static_cast(e).subtype) @@ -592,6 +607,9 @@ void logAllEvents(const Event* e) { case Event::Type::CameraMovedPosition: log(i, *static_cast(e)); break; + case Event::Type::ScheduledScriptExecuted: + log(i, *static_cast(e)); + break; case Event::Type::Custom: log(i, *static_cast(e)); break; @@ -738,6 +756,11 @@ EventCameraMovedPosition::EventCameraMovedPosition() : Event(Type) {} +EventScheduledScriptExecuted::EventScheduledScriptExecuted(std::string_view script_) + : Event(Type) + , script(temporaryString(script_)) +{} + CustomEvent::CustomEvent(std::string_view subtype_, std::string_view payload_) : Event(Type) , subtype(subtype_)