Skip to content

Commit

Permalink
Remove GlobalLayerSystem() callback and migrate all code to use the I…
Browse files Browse the repository at this point in the history
…LayerManager interface in the root node. This proved to be a bit tricky since there are situations where a node is not directly connected to a root node through its ancestry (e.g. during cloning or map deserialising).
  • Loading branch information
codereader committed Mar 13, 2020
1 parent 194b593 commit 32af0e9
Show file tree
Hide file tree
Showing 26 changed files with 295 additions and 275 deletions.
15 changes: 1 addition & 14 deletions include/ilayer.h
Expand Up @@ -53,8 +53,7 @@ class Layered
virtual void assignToLayers(const LayerList& newLayers) = 0;
};

class ILayerManager :
public RegisterableModule
class ILayerManager
{
public:
typedef std::shared_ptr<ILayerManager> Ptr;
Expand Down Expand Up @@ -214,20 +213,8 @@ class ILayerModule :

} // namespace scene

const char* const MODULE_LAYERSYSTEM("LayerSystem");
const char* const MODULE_LAYERS("LayerModule");

inline scene::ILayerManager& GlobalLayerSystem()
{
// Cache the reference locally
static scene::ILayerManager& _layerManager(
*std::static_pointer_cast<scene::ILayerManager>(
module::GlobalModuleRegistry().getModule(MODULE_LAYERSYSTEM)
)
);
return _layerManager;
}

inline scene::ILayerModule& GlobalLayerModule()
{
// Cache the reference locally
Expand Down
38 changes: 29 additions & 9 deletions libs/scenelib.h
@@ -1,6 +1,7 @@
#pragma once

#include "inode.h"
#include "ilayer.h"
#include "iscenegraph.h"
#include "iselectable.h"
#include "ipatch.h"
Expand Down Expand Up @@ -142,24 +143,35 @@ class UpdateNodeVisibilityWalker :
public NodeVisitor
{
std::stack<bool> _visibilityStack;

scene::IMapRootNodePtr _root;
public:
bool pre(const INodePtr& node) {
UpdateNodeVisibilityWalker(const scene::IMapRootNodePtr& root) :
_root(root)
{
assert(_root);
}

bool pre(const INodePtr& node)
{
// Update the node visibility and store the result
bool nodeIsVisible = GlobalLayerSystem().updateNodeVisibility(node);
bool nodeIsVisible = _root->getLayerManager().updateNodeVisibility(node);

// Add a new element for this level
_visibilityStack.push(nodeIsVisible);

return true;
}

void post(const INodePtr& node) {
void post(const INodePtr& node)
{
// Is this child visible?
bool childIsVisible = _visibilityStack.top();

_visibilityStack.pop();

if (childIsVisible) {
if (childIsVisible)
{
// Show the node, regardless whether it was hidden before
// otherwise the parent would hide the visible children as well
node->disable(Node::eLayered);
Expand All @@ -171,7 +183,8 @@ class UpdateNodeVisibilityWalker :
Node_setSelected(node, false);
}

if (childIsVisible && !_visibilityStack.empty()) {
if (childIsVisible && !_visibilityStack.empty())
{
// The child was visible, set this parent to true
_visibilityStack.top() = true;
}
Expand All @@ -182,13 +195,20 @@ class UpdateNodeVisibilityWalker :
* greebo: This method inserts the given node into the given container
* and ensures that the container's layer visibility is updated.
*/
inline void addNodeToContainer(const INodePtr& node, const INodePtr& container) {
inline void addNodeToContainer(const INodePtr& node, const INodePtr& container)
{
// Insert the child
container->addChildNode(node);

// Ensure that worldspawn is visible
UpdateNodeVisibilityWalker walker;
container->traverse(walker);
// If the container is already connected to a root, check the layer visibility
auto rootNode = container->getRootNode();

if (rootNode)
{
// Ensure that worldspawn is visible
UpdateNodeVisibilityWalker walker(rootNode);
container->traverse(walker);
}
}

} // namespace scene
Expand Down
7 changes: 5 additions & 2 deletions radiant/brush/BrushModule.cpp
Expand Up @@ -70,8 +70,11 @@ scene::INodePtr BrushModuleImpl::createBrush()
{
scene::INodePtr node = std::make_shared<BrushNode>();

// Move it to the active layer
node->moveToLayer(GlobalLayerSystem().getActiveLayer());
if (GlobalMapModule().getRoot())
{
// All brushes are created in the active layer by default
node->moveToLayer(GlobalMapModule().getRoot()->getLayerManager().getActiveLayer());
}

return node;
}
Expand Down
7 changes: 5 additions & 2 deletions radiant/entity/EntityCreator.cpp
Expand Up @@ -76,8 +76,11 @@ IEntityNodePtr Doom3EntityCreator::createEntity(const IEntityClassPtr& eclass)
{
IEntityNodePtr node = createNodeForEntity(eclass);

// All entities are created in the active layer by default
node->moveToLayer(GlobalLayerSystem().getActiveLayer());
if (GlobalMapModule().getRoot())
{
// All entities are created in the active layer by default
node->moveToLayer(GlobalMapModule().getRoot()->getLayerManager().getActiveLayer());
}

node->getEntity().setKeyValue("classname", eclass->getName());

Expand Down
45 changes: 37 additions & 8 deletions radiant/layers/LayerCommandTarget.cpp
@@ -1,6 +1,7 @@
#include "LayerCommandTarget.h"

#include "ieventmanager.h"
#include "imap.h"
#include "icommandsystem.h"
#include "LayerManager.h"
#include "string/string.h"
Expand Down Expand Up @@ -48,22 +49,50 @@ LayerCommandTarget::LayerCommandTarget(int layerID) :
);
}

void LayerCommandTarget::addSelectionToLayer(const cmd::ArgumentList& args) {
void LayerCommandTarget::addSelectionToLayer(const cmd::ArgumentList& args)
{
if (!GlobalMapModule().getRoot())
{
rError() << "No map loaded, cannot do this." << std::endl;
return;
}

// Pass the call to the LayerSystem
getLayerSystem().addSelectionToLayer(_layerID);
GlobalMapModule().getRoot()->getLayerManager().addSelectionToLayer(_layerID);
}

void LayerCommandTarget::moveSelectionToLayer(const cmd::ArgumentList& args) {
void LayerCommandTarget::moveSelectionToLayer(const cmd::ArgumentList& args)
{
if (!GlobalMapModule().getRoot())
{
rError() << "No map loaded, cannot do this." << std::endl;
return;
}

// Pass the call to the LayerSystem
getLayerSystem().moveSelectionToLayer(_layerID);
GlobalMapModule().getRoot()->getLayerManager().moveSelectionToLayer(_layerID);
}

void LayerCommandTarget::showLayer(const cmd::ArgumentList& args) {
getLayerSystem().setLayerVisibility(_layerID, true);
void LayerCommandTarget::showLayer(const cmd::ArgumentList& args)
{
if (!GlobalMapModule().getRoot())
{
rError() << "No map loaded, cannot do this." << std::endl;
return;
}

GlobalMapModule().getRoot()->getLayerManager().setLayerVisibility(_layerID, true);
}

void LayerCommandTarget::hideLayer(const cmd::ArgumentList& args) {
getLayerSystem().setLayerVisibility(_layerID, false);
void LayerCommandTarget::hideLayer(const cmd::ArgumentList& args)
{
if (!GlobalMapModule().getRoot())
{
rError() << "No map loaded, cannot do this." << std::endl;
return;
}

GlobalMapModule().getRoot()->getLayerManager().setLayerVisibility(_layerID, false);
}

} // namespace scene
34 changes: 16 additions & 18 deletions radiant/layers/LayerInfoFileModule.cpp
Expand Up @@ -36,10 +36,23 @@ void LayerInfoFileModule::onInfoFileSaveStart()
_layerInfoCount = 0;
_output.str(std::string());
_output.clear();
_layerNameBuffer.clear();
}

void LayerInfoFileModule::onBeginSaveMap(const scene::IMapRootNodePtr& root)
{}
{
// Open a "Layers" block
_layerNameBuffer << "\t" << LAYERS << std::endl;
_layerNameBuffer << "\t{" << std::endl;

// Visit all layers and write them to the stream
root->getLayerManager().foreachLayer([&](int layerId, const std::string& layerName)
{
_layerNameBuffer << "\t\t" << LAYER << " " << layerId << " { " << layerName << " }" << std::endl;
});

_layerNameBuffer << "\t}" << std::endl;
}

void LayerInfoFileModule::onFinishSaveMap(const scene::IMapRootNodePtr& root)
{}
Expand Down Expand Up @@ -85,7 +98,7 @@ void LayerInfoFileModule::saveNode(const INodePtr& node)
void LayerInfoFileModule::writeBlocks(std::ostream& stream)
{
// Write the layer names block
writeLayerNames(stream);
stream << _layerNameBuffer.str();

// Write the NodeToLayerMapping block
stream << "\t" << NODE_TO_LAYER_MAPPING << std::endl;
Expand Down Expand Up @@ -217,7 +230,7 @@ void LayerInfoFileModule::applyInfoToScene(const IMapRootNodePtr& root, const ma
for (const LayerNameMap::value_type& i : _layerNames)
{
// Create the named layer with the saved ID
GlobalLayerSystem().createLayer(i.second, i.first);
root->getLayerManager().createLayer(i.second, i.first);
}

// Set the layer mapping iterator to the beginning
Expand Down Expand Up @@ -270,19 +283,4 @@ void LayerInfoFileModule::onInfoFileLoadFinished()
_layerMappings.clear();
}

void LayerInfoFileModule::writeLayerNames(std::ostream& stream)
{
// Open a "Layers" block
stream << "\t" << LAYERS << std::endl;
stream << "\t{" << std::endl;

// Visit all layers and write them to the stream
GlobalLayerSystem().foreachLayer([&](int layerId, const std::string& layerName)
{
stream << "\t\t" << LAYER << " " << layerId << " { " << layerName << " }" << std::endl;
});

stream << "\t}" << std::endl;
}

}
1 change: 1 addition & 0 deletions radiant/layers/LayerInfoFileModule.h
Expand Up @@ -15,6 +15,7 @@ class LayerInfoFileModule :

// Buffer to hold our output
std::stringstream _output;
std::stringstream _layerNameBuffer;

// The list of layernames
typedef std::map<int, std::string> LayerNameMap;
Expand Down

0 comments on commit 32af0e9

Please sign in to comment.