diff --git a/modules/server/servermodule.cpp b/modules/server/servermodule.cpp index 734c1ce246..142cc91360 100644 --- a/modules/server/servermodule.cpp +++ b/modules/server/servermodule.cpp @@ -50,6 +50,15 @@ ServerModule::ServerModule() , _interfaceOwner({"Interfaces", "Interfaces", "Server Interfaces"}) { addPropertySubOwner(_interfaceOwner); + + global::callback::preSync->emplace_back([this]() { + // Trigger callbacks + using K = CallbackHandle; + using V = CallbackFunction; + for (const std::pair& it : _preSyncCallbacks) { + it.second(); // call function + } + }); } ServerModule::~ServerModule() { @@ -233,4 +242,28 @@ void ServerModule::consumeMessages() { } } +ServerModule::CallbackHandle ServerModule::addPreSyncCallback(CallbackFunction cb) +{ + CallbackHandle handle = _nextCallbackHandle++; + _preSyncCallbacks.emplace_back(handle, std::move(cb)); + return handle; +} + +void ServerModule::removePreSyncCallback(CallbackHandle handle) { + const auto it = std::find_if( + _preSyncCallbacks.begin(), + _preSyncCallbacks.end(), + [handle](const std::pair& cb) { + return cb.first == handle; + } + ); + + ghoul_assert( + it != _preSyncCallbacks.end(), + "handle must be a valid callback handle" + ); + + _preSyncCallbacks.erase(it); +} + } // namespace openspace diff --git a/modules/server/servermodule.h b/modules/server/servermodule.h index 05f4686681..a3f1ae9e97 100644 --- a/modules/server/servermodule.h +++ b/modules/server/servermodule.h @@ -49,6 +49,8 @@ struct Message { class ServerModule : public OpenSpaceModule { public: static constexpr const char* Name = "Server"; + using CallbackHandle = int; + using CallbackFunction = std::function; ServerModule(); virtual ~ServerModule(); @@ -57,6 +59,9 @@ public: int skyBrowserUpdateTime() const; + CallbackHandle addPreSyncCallback(CallbackFunction cb); + void removePreSyncCallback(CallbackHandle handle); + protected: void internalInitialize(const ghoul::Dictionary& configuration) override; @@ -79,6 +84,10 @@ private: std::vector> _interfaces; properties::PropertyOwner _interfaceOwner; int _skyBrowserUpdateTime = 100; + + // Callbacks for tiggering topic + int _nextCallbackHandle = 0; + std::vector> _preSyncCallbacks; }; } // namespace openspace diff --git a/modules/server/src/topics/skybrowsertopic.cpp b/modules/server/src/topics/skybrowsertopic.cpp index db375a037f..83970378a3 100644 --- a/modules/server/src/topics/skybrowsertopic.cpp +++ b/modules/server/src/topics/skybrowsertopic.cpp @@ -55,7 +55,7 @@ SkyBrowserTopic::SkyBrowserTopic() SkyBrowserTopic::~SkyBrowserTopic() { if (_targetDataCallbackHandle != UnsetOnChangeHandle) { - SkyBrowserModule* module = global::moduleEngine->module(); + ServerModule* module = global::moduleEngine->module(); if (module) { module->removePreSyncCallback(_targetDataCallbackHandle); } @@ -78,7 +78,7 @@ void SkyBrowserTopic::handleJson(const nlohmann::json& json) { return; } - SkyBrowserModule* module = global::moduleEngine->module(); + ServerModule* module = global::moduleEngine->module(); _targetDataCallbackHandle = module->addPreSyncCallback( [this]() { std::chrono::system_clock::time_point now = std::chrono::system_clock::now(); diff --git a/modules/skybrowser/skybrowsermodule.cpp b/modules/skybrowser/skybrowsermodule.cpp index a3e6f05eef..5dead313c2 100644 --- a/modules/skybrowser/skybrowsermodule.cpp +++ b/modules/skybrowser/skybrowsermodule.cpp @@ -214,13 +214,6 @@ SkyBrowserModule::SkyBrowserModule() if (_cameraRotation.isAnimating() && _allowCameraRotation) { incrementallyRotateCamera(); } - - // Trigger callbacks (should maybe have a check to see if update is needed) - using K = CallbackHandle; - using V = CallbackFunction; - for (const std::pair& it : _preSyncCallbacks) { - it.second(); // call function - } }); } @@ -484,31 +477,6 @@ bool SkyBrowserModule::isSelectedPairFacingCamera() const { return found ? found->isFacingCamera() : false; } -SkyBrowserModule::CallbackHandle SkyBrowserModule::addPreSyncCallback( - CallbackFunction cb) -{ - CallbackHandle handle = _nextCallbackHandle++; - _preSyncCallbacks.emplace_back(handle, std::move(cb)); - return handle; -} - -void SkyBrowserModule::removePreSyncCallback(CallbackHandle handle) { - const auto it = std::find_if( - _preSyncCallbacks.begin(), - _preSyncCallbacks.end(), - [handle](const std::pair& cb) { - return cb.first == handle; - } - ); - - ghoul_assert( - it != _preSyncCallbacks.end(), - "handle must be a valid callback handle" - ); - - _preSyncCallbacks.erase(it); -} - std::vector SkyBrowserModule::documentations() const { return { RenderableSkyTarget::Documentation(), diff --git a/modules/skybrowser/skybrowsermodule.h b/modules/skybrowser/skybrowsermodule.h index 938ead2185..5ce75eeb65 100644 --- a/modules/skybrowser/skybrowsermodule.h +++ b/modules/skybrowser/skybrowsermodule.h @@ -47,8 +47,6 @@ struct ImageData; class SkyBrowserModule : public OpenSpaceModule { public: constexpr static const char* Name = "SkyBrowser"; - using CallbackHandle = int; - using CallbackFunction = std::function; SkyBrowserModule(); @@ -89,9 +87,6 @@ public: void loadImages(const std::string& root, const std::filesystem::path& directory); int nLoadedImages() const; - CallbackHandle addPreSyncCallback(CallbackFunction cb); - void removePreSyncCallback(CallbackHandle handle); - scripting::LuaLibrary luaLibrary() const override; std::vector documentations() const override; @@ -125,10 +120,6 @@ private: // Data handler for the image collections std::unique_ptr _dataHandler; - - // Callbacks for tiggering topic - int _nextCallbackHandle = 0; - std::vector> _preSyncCallbacks; }; } // namespace openspace