Publish event when a scheduled script is executed (closes #1892)

This commit is contained in:
Alexander Bock
2024-04-19 13:27:15 +02:00
parent 9f51549a2a
commit 58bc1bd29d
3 changed files with 46 additions and 0 deletions

View File

@@ -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

View File

@@ -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<events::EventScheduledScriptExecuted>(
script
);
}
global::renderEngine->updateScene();

View File

@@ -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<const EventCameraPathFinished&>(e).destination)
);
break;
case Event::Type::ScheduledScriptExecuted:
d.setValue(
"Script",
std::string(static_cast<const EventScheduledScriptExecuted&>(e).script)
);
break;
case Event::Type::Custom:
d.setValue(
"Subtype", std::string(static_cast<const CustomEvent&>(e).subtype)
@@ -592,6 +607,9 @@ void logAllEvents(const Event* e) {
case Event::Type::CameraMovedPosition:
log(i, *static_cast<const EventCameraMovedPosition*>(e));
break;
case Event::Type::ScheduledScriptExecuted:
log(i, *static_cast<const EventScheduledScriptExecuted*>(e));
break;
case Event::Type::Custom:
log(i, *static_cast<const CustomEvent*>(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_)