From 7c3d6b4c4d53da84d7e3586d2c783f714fc13bfb Mon Sep 17 00:00:00 2001 From: Emma Broman Date: Wed, 12 Jan 2022 09:39:12 +0100 Subject: [PATCH] Change how moveLayer works slightly, so it is easier to use in a GUI implementation * "newPosition" is now considered to be the item's new position (index) in the list * also update the documentation a little --- modules/globebrowsing/globebrowsingmodule.cpp | 10 ++++++---- modules/globebrowsing/globebrowsingmodule_lua.inl | 2 +- modules/globebrowsing/src/layergroup.cpp | 11 ++--------- modules/globebrowsing/src/layergroup.h | 2 +- 4 files changed, 10 insertions(+), 15 deletions(-) diff --git a/modules/globebrowsing/globebrowsingmodule.cpp b/modules/globebrowsing/globebrowsingmodule.cpp index 56418d997d..96c04f94da 100644 --- a/modules/globebrowsing/globebrowsingmodule.cpp +++ b/modules/globebrowsing/globebrowsingmodule.cpp @@ -376,12 +376,14 @@ scripting::LuaLibrary GlobeBrowsingModule::luaLibrary() const { "moveLayer", &globebrowsing::luascriptfunctions::moveLayer, "string, string, number, number", - "Rearranges the order of a single layer in a scene graph node. The first " - "parameter specifies the scene graph node, the second parameter specifies " + "Rearranges the order of a single layer on a globe. The first parameter" + "is the identifier of the globe, the second parameter specifies " "the name of the layer group, the third parameter is the original position " "of the layer that should be moved and the last parameter is the new " - "position. The new position may be -1 to place the layer at the top or any " - "large number bigger than the number of layers to place it at the bottom." + "position in the list. The first position in the list has index 0, and the " + "last position is given by the number of layers minus one. The new position " + "may be -1 to place the layer at the top or any number bigger than the " + "number of layers to place it at the bottom." }, { "goToChunk", diff --git a/modules/globebrowsing/globebrowsingmodule_lua.inl b/modules/globebrowsing/globebrowsingmodule_lua.inl index 2cb2d26889..2dd7398a66 100644 --- a/modules/globebrowsing/globebrowsingmodule_lua.inl +++ b/modules/globebrowsing/globebrowsingmodule_lua.inl @@ -169,7 +169,7 @@ int moveLayer(lua_State* L) { } globebrowsing::LayerGroup& lg = globe->layerManager().layerGroup(group); - lg.moveLayers(oldPosition, newPosition); + lg.moveLayer(oldPosition, newPosition); return 0; } diff --git a/modules/globebrowsing/src/layergroup.cpp b/modules/globebrowsing/src/layergroup.cpp index 65245e6255..3adf42d366 100644 --- a/modules/globebrowsing/src/layergroup.cpp +++ b/modules/globebrowsing/src/layergroup.cpp @@ -212,16 +212,9 @@ void LayerGroup::deleteLayer(const std::string& layerName) { LERROR("Could not find layer " + layerName); } -void LayerGroup::moveLayers(int oldPosition, int newPosition) { +void LayerGroup::moveLayer(int oldPosition, int newPosition) { oldPosition = std::max(0, oldPosition); - newPosition = std::min(newPosition, static_cast(_layers.size())); - - // We need to adjust the new position as we first delete the old position, if this - // position is before the new position we have reduced the size of the vector by 1 and - // need to adapt where we want to put the value in - if (oldPosition < newPosition) { - newPosition -= 1; - } + newPosition = std::min(newPosition, static_cast(_layers.size() - 1)); // There are two synchronous vectors that we have to update here. The _layers vector // is used to determine the order while rendering, the _subowners is the order in diff --git a/modules/globebrowsing/src/layergroup.h b/modules/globebrowsing/src/layergroup.h index 3b2d2b7148..be133a3dac 100644 --- a/modules/globebrowsing/src/layergroup.h +++ b/modules/globebrowsing/src/layergroup.h @@ -52,7 +52,7 @@ struct LayerGroup : public properties::PropertyOwner { Layer* addLayer(const ghoul::Dictionary& layerDict); void deleteLayer(const std::string& layerName); - void moveLayers(int oldPosition, int newPosition); + void moveLayer(int oldPosition, int newPosition); /// @returns const vector of all layers std::vector layers() const;