Skip to content

Commit

Permalink
#5281: Visibility and active status can be restored from the .darkrad…
Browse files Browse the repository at this point in the history
…iant file
  • Loading branch information
codereader committed Oct 29, 2022
1 parent 05fdba5 commit b11ee57
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 4 deletions.
85 changes: 81 additions & 4 deletions radiantcore/layers/LayerInfoFileModule.cpp
Expand Up @@ -17,7 +17,10 @@ namespace
constexpr const char* const NODE_TO_LAYER_MAPPING = "NodeToLayerMapping";
constexpr const char* const LAYER_HIERARCHY = "LayerHierarchy";
constexpr const char* const LAYER = "Layer";
constexpr const char* const ACTIVE_LAYER = "ActiveLayer";
constexpr const char* const HIDDEN_LAYERS = "HiddenLayers";
constexpr const char* const LAYERS = "Layers";
constexpr const char* const LAYER_PROPERTIES = "LayerProperties";
constexpr const char* const NODE = "Node";
constexpr const char* const PARENT = "Parent";
}
Expand Down Expand Up @@ -46,6 +49,8 @@ void LayerInfoFileModule::clear()
_layerNames.clear();
_layerMappings.clear();
_layerParentIds.clear();
_activeLayerId = 0;
_hiddenLayerIds.clear();
}

void LayerInfoFileModule::onInfoFileSaveStart()
Expand Down Expand Up @@ -154,7 +159,8 @@ void LayerInfoFileModule::onInfoFileLoadStart()

bool LayerInfoFileModule::canParseBlock(const std::string& blockName)
{
return blockName == LAYERS || blockName == NODE_TO_LAYER_MAPPING || blockName == LAYER_HIERARCHY;
return blockName == LAYERS || blockName == NODE_TO_LAYER_MAPPING ||
blockName == LAYER_HIERARCHY || blockName == LAYER_PROPERTIES;
}

void LayerInfoFileModule::parseBlock(const std::string& blockName, parser::DefTokeniser& tok)
Expand All @@ -170,9 +176,13 @@ void LayerInfoFileModule::parseBlock(const std::string& blockName, parser::DefTo
parseNodeToLayerMapping(tok);
}
else if (blockName == LAYER_HIERARCHY)
{
parseLayerHierarchy(tok);
}
{
parseLayerHierarchy(tok);
}
else if (blockName == LAYER_PROPERTIES)
{
parseLayerProperties(tok);
}
}

void LayerInfoFileModule::parseLayerNames(parser::DefTokeniser& tok)
Expand Down Expand Up @@ -290,6 +300,60 @@ void LayerInfoFileModule::parseLayerHierarchy(parser::DefTokeniser& tok)
}
}

void LayerInfoFileModule::parseLayerProperties(parser::DefTokeniser& tok)
{
/*
LayerProperties
{
ActiveLayer { 9 }
HiddenLayers { 2 1 6 7 }
}
*/

// The opening brace
tok.assertNextToken("{");

while (tok.hasMoreTokens())
{
auto token = tok.nextToken();

if (token == ACTIVE_LAYER)
{
// The block just contains the active layer ID, only a single one is supported
tok.assertNextToken("{");
_activeLayerId = string::convert<int>(tok.nextToken(), -1);
tok.assertNextToken("}");

if (_activeLayerId != -1)
{
rDebug() << "[InfoFile]: ActiveLayer ID could not be parsed: " << _activeLayerId << std::endl;
}

continue;
}

if (token == HIDDEN_LAYERS)
{
// The block just contains a list of delimited layer IDs (or nothing)
tok.assertNextToken("{");

while (tok.hasMoreTokens())
{
auto nodeToken = tok.nextToken();

if (nodeToken == "}") break;

// Add the ID to the list
_hiddenLayerIds.push_back(string::convert<int>(nodeToken));
}

continue;
}

if (token == "}") break;
}
}

void LayerInfoFileModule::applyInfoToScene(const IMapRootNodePtr& root, const map::NodeIndexMap& nodeMap)
{
auto& layerManager = root->getLayerManager();
Expand All @@ -301,6 +365,19 @@ void LayerInfoFileModule::applyInfoToScene(const IMapRootNodePtr& root, const ma
layerManager.createLayer(name, id);
}

// Set the active layer before setting visibility
if (_activeLayerId != 0)
{
layerManager.setActiveLayer(_activeLayerId);
}

// Assign layer visibility status before the hierarchy is restored
// this way we don't implicitly set the child layer visibility
for (auto hiddenLayerId : _hiddenLayerIds)
{
layerManager.setLayerVisibility(hiddenLayerId, false);
}

// Assigning child and parent layers needs to happen after all layers have been created
for (const auto& [childLayerId, parentLayerId] : _layerParentIds)
{
Expand Down
3 changes: 3 additions & 0 deletions radiantcore/layers/LayerInfoFileModule.h
Expand Up @@ -22,6 +22,8 @@ class LayerInfoFileModule :
std::map<int, std::string> _layerNames;

std::map<int, int> _layerParentIds;
int _activeLayerId;
std::vector<int> _hiddenLayerIds;

typedef std::vector<scene::LayerList> LayerLists;
LayerLists _layerMappings;
Expand Down Expand Up @@ -55,6 +57,7 @@ class LayerInfoFileModule :
void parseLayerNames(parser::DefTokeniser& tok);
void parseNodeToLayerMapping(parser::DefTokeniser& tok);
void parseLayerHierarchy(parser::DefTokeniser& tok);
void parseLayerProperties(parser::DefTokeniser& tok);
};

}

0 comments on commit b11ee57

Please sign in to comment.