Store selected images in C++ backend and send to GUI. Add lua function to remove selected images from browser

This commit is contained in:
Ylva Selling
2021-05-24 11:49:31 +02:00
parent 3c2bbd134f
commit bf283dce0b
6 changed files with 68 additions and 12 deletions

View File

@@ -5,6 +5,7 @@
#include <modules/skybrowser/include/wwtdatahandler.h>
#include <openspace/properties/scalar/floatproperty.h>
#include <openspace/properties/stringproperty.h>
#include <deque>
namespace openspace {
class ScreenSpaceSkyTarget;
@@ -29,8 +30,10 @@ namespace openspace {
ScreenSpaceSkyTarget* getSkyTarget();
bool hasLoadedCollections();
void setHasLoadedCollections(bool isLoaded);
void addImage(ImageData& image);
properties::FloatProperty& getOpacity();
std::deque<int>& selectedImages();
void addSelectedImage(ImageData& image, int i);
void removeSelectedImage(ImageData& image, int i);
// Translation
//void translate(glm::vec2 translation);
@@ -64,6 +67,7 @@ namespace openspace {
std::chrono::system_clock::time_point _lastUpdateTime;
int _imageId{ 0 };
bool _hasLoadedCollections{ false };
std::deque<int> _selectedImages;
};
}

View File

@@ -45,7 +45,7 @@ namespace openspace {
ghoul::Dictionary loadCollection(const std::string& url);
ghoul::Dictionary setForeground(const std::string& name);
ghoul::Dictionary createImageLayer(ImageData& image, int id = 0);
ghoul::Dictionary removeImageLayer(const std::string& id);
ghoul::Dictionary removeImageLayer(ImageData& image);
ghoul::Dictionary setLayerOpacity(const ImageData& image,
double opacity);
ghoul::Dictionary setForegroundOpacity(double val);

View File

@@ -105,6 +105,14 @@ namespace openspace {
"string or list of strings",
"Add one or multiple exoplanet systems to the scene, as specified by the "
"input. An input string should be the name of the system host star"
},
{
"removeSelectedImageInBrowser",
&skybrowser::luascriptfunctions::removeSelectedImageInBrowser,
{},
"string or list of strings",
"Add one or multiple exoplanet systems to the scene, as specified by the "
"input. An input string should be the name of the system host star"
},
{
"adjustCamera",

View File

@@ -46,7 +46,7 @@ namespace openspace::skybrowser::luascriptfunctions {
// Load image, if the image has not been loaded yet
if (resultImage.id == ImageData::NO_ID) {
LINFO("Loading image " + resultImage.name);
selectedBrowser->addImage(resultImage);
selectedBrowser->addSelectedImage(resultImage, i);
}
ScreenSpaceSkyTarget* selectedTarget = selectedBrowser->getSkyTarget();
@@ -224,6 +224,7 @@ namespace openspace::skybrowser::luascriptfunctions {
glm::dvec2 sphericalJ2000 = skybrowser::cartesianToSpherical(cartesianJ2000);
// Convert to vector so ghoul can read it
std::vector<double> viewDirCelestVec = { cartesianJ2000.x, cartesianJ2000.y, cartesianJ2000.z };
// Calculate the smallest FOV of vertical and horizontal
@@ -253,6 +254,12 @@ namespace openspace::skybrowser::luascriptfunctions {
for (std::pair<std::string, ScreenSpaceSkyBrowser*> pair : browsers) {
ScreenSpaceSkyBrowser* browser = pair.second;
std::string id = pair.first;
// Convert deque to vector so ghoul can read it
std::vector<int> selectedImagesVector;
std::deque<int> selectedImages = browser->selectedImages();
std::for_each(selectedImages.begin(), selectedImages.end(), [&](int index) {
selectedImagesVector.push_back(index);
});
// Only add browsers that have an initialized target
ScreenSpaceSkyTarget* target = browser->getSkyTarget();
if (target) {
@@ -272,6 +279,8 @@ namespace openspace::skybrowser::luascriptfunctions {
lua_settable(L, -3);
ghoul::lua::push(L, "FOV", browser->fieldOfView());
lua_settable(L, -3);
ghoul::lua::push(L, "selectedImages", selectedImagesVector);
lua_settable(L, -3);
ghoul::lua::push(L, "cartesianDirection", celestialCartVec);
lua_settable(L, -3);
ghoul::lua::push(L, "ra", celestialSpherical.x);
@@ -280,7 +289,7 @@ namespace openspace::skybrowser::luascriptfunctions {
lua_settable(L, -3);
ghoul::lua::push(L, "color", colorVec);
lua_settable(L, -3);
// Set table for the current target
lua_settable(L, -3);
}
@@ -371,6 +380,21 @@ namespace openspace::skybrowser::luascriptfunctions {
return 0;
}
int removeSelectedImageInBrowser(lua_State* L) {
ghoul::lua::checkArgumentsAndThrow(L, 2, "lua::removeSelectedImageInBrowser");
// Image index
const int i = ghoul::lua::value<int>(L, 1);
const std::string browserId = ghoul::lua::value<std::string>(L, 2);
// Get browser
SkyBrowserModule* module = global::moduleEngine->module<SkyBrowserModule>();
ScreenSpaceSkyBrowser* browser = module->getSkyBrowsers()[browserId];
ImageData& resultImage = module->getWWTDataHandler()->getLoadedImages()[i];
// Remove image
browser->removeSelectedImage(resultImage, i);
return 0;
}
}

View File

@@ -325,13 +325,32 @@ namespace openspace {
return _browserDimensions.value();
}
void ScreenSpaceSkyBrowser::addImage(ImageData& image) {
sendMessageToWWT(wwtmessage::createImageLayer(image, _imageId));
sendMessageToWWT(wwtmessage::setLayerOpacity(image, 1.0));
_imageId++;
}
properties::FloatProperty& ScreenSpaceSkyBrowser::getOpacity() {
return _opacity;
}
std::deque<int>& ScreenSpaceSkyBrowser::selectedImages() {
return _selectedImages;
}
void ScreenSpaceSkyBrowser::addSelectedImage(ImageData& image, int i) {
sendMessageToWWT(wwtmessage::createImageLayer(image, _imageId));
sendMessageToWWT(wwtmessage::setLayerOpacity(image, 1.0));
_imageId++;
// Ensure there are no duplicates
auto it = std::find(std::begin(_selectedImages), std::end(_selectedImages), i);
if (it == std::end(_selectedImages)) {
// Push newly selected image to front
_selectedImages.push_front(i);
}
}
void ScreenSpaceSkyBrowser::removeSelectedImage(ImageData& image, int i) {
sendMessageToWWT(wwtmessage::removeImageLayer(image));
// Remove from selected list
auto it = std::find(std::begin(_selectedImages), std::end(_selectedImages), i);
if (it != std::end(_selectedImages)) {
_selectedImages.erase(it);
}
}
}

View File

@@ -152,11 +152,12 @@ namespace openspace::wwtmessage {
return msg;
}
ghoul::Dictionary removeImageLayer(const std::string& id) {
ghoul::Dictionary removeImageLayer(ImageData& image) {
using namespace std::string_literals;
ghoul::Dictionary msg;
msg.setValue("event", "image_layer_remove"s);
msg.setValue("id", id);
msg.setValue("id", std::to_string(image.id));
image.id = ImageData::NO_ID;
return msg;
}