Make thread be joined, not detached, and run depending on a flag

This commit is contained in:
Ylva Selling
2021-03-08 15:53:45 +01:00
parent a77fbcfcfe
commit 8ac85466e7
4 changed files with 51 additions and 21 deletions
+32 -13
View File
@@ -83,13 +83,20 @@ SkybrowserModule::SkybrowserModule()
, _testProperty(TestInfo)
, _zoomFactor(ZoomInfo, 50.f ,0.1f ,70.f)
, _skyBrowser(nullptr)
, _camIsSyncedWWT(true)
{
addProperty(_testProperty);
addProperty(_zoomFactor);
}
void SkybrowserModule::internalDeinitialize() {
ZoneScoped
// Set flag to false so the thread can exit
_camIsSyncedWWT = false;
_thread.join();
LINFO("Joined thread");
}
scripting::LuaLibrary SkybrowserModule::luaLibrary() const {
scripting::LuaLibrary res;
res.name = "skybrowser";
@@ -155,17 +162,31 @@ bool SkybrowserModule::sendMessageToWWT(const ghoul::Dictionary& msg) {
void SkybrowserModule::WWTfollowCamera() {
showTarget();
while (true) {
// Get camera view direction
const glm::dvec3 viewDirection = global::navigationHandler->camera()->viewDirectionWorldSpace();
//std::thread(&SkybrowserModule::WWTfollowCamera, module)
// Convert to celestial coordinates
glm::dvec2 celestCoords = convertGalacticToCelestial(viewDirection);
ghoul::Dictionary message = createMessageForMovingWWTCamera(celestCoords, _zoomFactor);
// Start a thread to enable user interaction while sending the calls to WWT
_thread = std::thread([&] {
while (_camIsSyncedWWT) {
sendMessageToWWT(message);
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
// Get camera view direction
const glm::dvec3 viewDirection = global::navigationHandler->camera()->viewDirectionWorldSpace();
// Convert to celestial coordinates
glm::dvec2 celestCoords = convertGalacticToCelestial(viewDirection);
ghoul::Dictionary message = createMessageForMovingWWTCamera(celestCoords, _zoomFactor);
if (!_camIsSyncedWWT) {
break;
}
else {
std::this_thread::sleep_for(std::chrono::milliseconds(50));
sendMessageToWWT(message);
}
LINFO("Thread running... " + std::to_string(_camIsSyncedWWT));
}
LINFO("Thread finished... " + std::to_string(_camIsSyncedWWT));
});
}
ghoul::Dictionary SkybrowserModule::createMessageForMovingWWTCamera(const glm::dvec2 celestCoords, const float fov, const bool moveInstantly) const {
@@ -189,8 +210,6 @@ ghoul::Dictionary SkybrowserModule::createMessageForLoadingWWTImgColl(const std:
return msg;
}
ghoul::Dictionary SkybrowserModule::createMessageForPausingWWTTime() const {
using namespace std::string_literals;
ghoul::Dictionary msg;
+4 -1
View File
@@ -32,6 +32,7 @@
#include <openspace/properties/scalar/boolproperty.h>
#include <openspace/properties/scalar/floatproperty.h>
#include <openspace/properties/stringproperty.h>
#include <thread>
namespace openspace {
@@ -47,7 +48,6 @@ public:
float zoomFactor() const;
glm::dvec2 convertGalacticToCelestial(glm::dvec3 coords) const;
void showTarget() const;
void WWTfollowCamera();
void showTarget() const;
@@ -64,10 +64,13 @@ public:
protected:
void internalInitialize(const ghoul::Dictionary& dict) override;
void internalDeinitialize() override;
properties::StringProperty _testProperty;
properties::FloatProperty _zoomFactor;
ScreenSpaceBrowser* _skyBrowser;
bool _camIsSyncedWWT;
std::thread _thread;
};
} // namespace openspace
+6 -4
View File
@@ -45,8 +45,8 @@ namespace openspace::skybrowser::luascriptfunctions {
//ghoul::Dictionary message = module->createMessageForPausingWWTTime();
//module->sendMessageToWWT(message);
module->showTarget();
std::thread thread(&SkybrowserModule::WWTfollowCamera, module);
thread.detach();
module->WWTfollowCamera();
return 1;
}
@@ -54,9 +54,10 @@ namespace openspace::skybrowser::luascriptfunctions {
int moveBrowser(lua_State* L) {
ghoul::lua::checkArgumentsAndThrow(L, 0, "lua::moveBrowser");
SkybrowserModule* module = global::moduleEngine->module<SkybrowserModule>();
SkybrowserModule* module = global::moduleEngine->module<SkybrowserModule>();
std::this_thread::sleep_for(std::chrono::milliseconds(100));
ScreenSpaceBrowser* browser = dynamic_cast<ScreenSpaceBrowser*>(global::renderEngine->screenSpaceRenderable("ScreenSpaceBowser"));
module->initializeBrowser(browser);
module->initializeBrowser(browser);
module->skyBrowser()->translate(glm::vec3(-0.8, -0.4, 0.0));
return 1;
@@ -90,6 +91,7 @@ namespace openspace::skybrowser::luascriptfunctions {
"openspace.addScreenSpaceRenderable(" + node + ")",
scripting::ScriptEngine::RemoteScripting::Yes
);
return 1;
}
@@ -58,11 +58,17 @@ namespace {
namespace openspace {
void ScreenSpaceBrowser::executeJavascript(std::string &script) const {
void ScreenSpaceBrowser::executeJavascript(std::string& script) const {
//LINFOC(_loggerCat, "Executing javascript " + script);
CefRefPtr<CefFrame> frame;
if (_browserInstance && _browserInstance->getBrowser()) {
frame = _browserInstance->getBrowser()->GetMainFrame();
if (frame) {
frame->ExecuteJavaScript(script, frame->GetURL(), 0);
}
}
CefRefPtr<CefFrame> frame = _browserInstance->getBrowser()->GetMainFrame();
frame->ExecuteJavaScript(script, frame->GetURL(), 0);
}
void ScreenSpaceBrowser::translate(glm::vec3 translation) {