Skip to content

Commit

Permalink
Register layer commands as single functions taking an argument. The b…
Browse files Browse the repository at this point in the history
…uilt-in statements are defined in commandsystem.xml.
  • Loading branch information
codereader committed Mar 17, 2020
1 parent eabe55b commit dea548b
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 2 deletions.
41 changes: 41 additions & 0 deletions install/commandsystem.xml
Expand Up @@ -42,5 +42,46 @@
<!-- Transformation -->
<bind name="MoveSelectionUP" value="MoveSelectionVertically up" readonly="1" />
<bind name="MoveSelectionDOWN" value="MoveSelectionVertically down" readonly="1" />
<!-- Layer commands -->
<bind name="AddSelectionToLayer0" value="AddSelectionToLayer 0" readonly="1" />
<bind name="AddSelectionToLayer1" value="AddSelectionToLayer 1" readonly="1" />
<bind name="AddSelectionToLayer2" value="AddSelectionToLayer 2" readonly="1" />
<bind name="AddSelectionToLayer3" value="AddSelectionToLayer 3" readonly="1" />
<bind name="AddSelectionToLayer4" value="AddSelectionToLayer 4" readonly="1" />
<bind name="AddSelectionToLayer5" value="AddSelectionToLayer 5" readonly="1" />
<bind name="AddSelectionToLayer6" value="AddSelectionToLayer 6" readonly="1" />
<bind name="AddSelectionToLayer7" value="AddSelectionToLayer 7" readonly="1" />
<bind name="AddSelectionToLayer8" value="AddSelectionToLayer 8" readonly="1" />
<bind name="AddSelectionToLayer9" value="AddSelectionToLayer 9" readonly="1" />
<bind name="MoveSelectionToLayer0" value="MoveSelectionToLayer 0" readonly="1" />
<bind name="MoveSelectionToLayer1" value="MoveSelectionToLayer 1" readonly="1" />
<bind name="MoveSelectionToLayer2" value="MoveSelectionToLayer 2" readonly="1" />
<bind name="MoveSelectionToLayer3" value="MoveSelectionToLayer 3" readonly="1" />
<bind name="MoveSelectionToLayer4" value="MoveSelectionToLayer 4" readonly="1" />
<bind name="MoveSelectionToLayer5" value="MoveSelectionToLayer 5" readonly="1" />
<bind name="MoveSelectionToLayer6" value="MoveSelectionToLayer 6" readonly="1" />
<bind name="MoveSelectionToLayer7" value="MoveSelectionToLayer 7" readonly="1" />
<bind name="MoveSelectionToLayer8" value="MoveSelectionToLayer 8" readonly="1" />
<bind name="MoveSelectionToLayer9" value="MoveSelectionToLayer 9" readonly="1" />
<bind name="ShowLayer0" value="ShowLayer 0" readonly="1" />
<bind name="ShowLayer1" value="ShowLayer 1" readonly="1" />
<bind name="ShowLayer2" value="ShowLayer 2" readonly="1" />
<bind name="ShowLayer3" value="ShowLayer 3" readonly="1" />
<bind name="ShowLayer4" value="ShowLayer 4" readonly="1" />
<bind name="ShowLayer5" value="ShowLayer 5" readonly="1" />
<bind name="ShowLayer6" value="ShowLayer 6" readonly="1" />
<bind name="ShowLayer7" value="ShowLayer 7" readonly="1" />
<bind name="ShowLayer8" value="ShowLayer 8" readonly="1" />
<bind name="ShowLayer9" value="ShowLayer 9" readonly="1" />
<bind name="HideLayer0" value="HideLayer 0" readonly="1" />
<bind name="HideLayer1" value="HideLayer 1" readonly="1" />
<bind name="HideLayer2" value="HideLayer 2" readonly="1" />
<bind name="HideLayer3" value="HideLayer 3" readonly="1" />
<bind name="HideLayer4" value="HideLayer 4" readonly="1" />
<bind name="HideLayer5" value="HideLayer 5" readonly="1" />
<bind name="HideLayer6" value="HideLayer 6" readonly="1" />
<bind name="HideLayer7" value="HideLayer 7" readonly="1" />
<bind name="HideLayer8" value="HideLayer 8" readonly="1" />
<bind name="HideLayer9" value="HideLayer 9" readonly="1" />
</binds>
</commandsystem>
114 changes: 112 additions & 2 deletions radiant/layers/LayerModule.cpp
@@ -1,12 +1,12 @@
#include "ilayer.h"
#include "ieventmanager.h"
#include "icommandsystem.h"
#include "itextstream.h"
#include "imapinfofile.h"

#include "modulesystem/StaticModule.h"
#include "LayerManager.h"
#include "LayerInfoFileModule.h"
#include "LayerCommandTarget.h"

#include "wxutil/dialog/Dialog.h"
#include "wxutil/dialog/MessageBox.h"
Expand All @@ -15,6 +15,22 @@
namespace scene
{

namespace
{

inline void DoWithMapLayerManager(const std::function<void(scene::ILayerManager&)>& func)
{
if (!GlobalMapModule().getRoot())
{
rError() << "No map loaded, cannot do this." << std::endl;
return;
}

func(GlobalMapModule().getRoot()->getLayerManager());
}

}

class LayerModule :
public ILayerModule
{
Expand Down Expand Up @@ -47,10 +63,48 @@ class LayerModule :
{
rMessage() << getName() << "::initialiseModule called." << std::endl;

GlobalCommandSystem().addCommand(COMMAND_PREFIX_ADDTOLAYER,
std::bind(&LayerModule::addSelectionToLayer, this, std::placeholders::_1),
{ cmd::ARGTYPE_INT });

GlobalCommandSystem().addCommand(COMMAND_PREFIX_MOVETOLAYER,
std::bind(&LayerModule::moveSelectionToLayer, this, std::placeholders::_1),
{ cmd::ARGTYPE_INT });

GlobalCommandSystem().addCommand(COMMAND_PREFIX_SHOWLAYER,
std::bind(&LayerModule::showLayer, this, std::placeholders::_1),
{ cmd::ARGTYPE_INT });

GlobalCommandSystem().addCommand(COMMAND_PREFIX_HIDELAYER,
std::bind(&LayerModule::hideLayer, this, std::placeholders::_1),
{ cmd::ARGTYPE_INT });

// Add command targets for the first 10 layer IDs here
// The statements are defined in commandsystem.xml, they just need to be
// registered in the event manager module
for (int i = 0; i < 10; i++)
{
_commandTargets.emplace_back(std::make_shared<LayerCommandTarget>(i));
//_commandTargets.emplace_back(std::make_shared<LayerCommandTarget>(i));

GlobalEventManager().addCommand(
COMMAND_PREFIX_ADDTOLAYER + string::to_string(i),
COMMAND_PREFIX_ADDTOLAYER + string::to_string(i)
);

GlobalEventManager().addCommand(
COMMAND_PREFIX_MOVETOLAYER + string::to_string(i),
COMMAND_PREFIX_MOVETOLAYER + string::to_string(i)
);

GlobalEventManager().addCommand(
COMMAND_PREFIX_SHOWLAYER + string::to_string(i),
COMMAND_PREFIX_SHOWLAYER + string::to_string(i)
);

GlobalEventManager().addCommand(
COMMAND_PREFIX_HIDELAYER + string::to_string(i),
COMMAND_PREFIX_HIDELAYER + string::to_string(i)
);
}

// Register the "create layer" command
Expand Down Expand Up @@ -143,6 +197,62 @@ class LayerModule :
}
}
}

void addSelectionToLayer(const cmd::ArgumentList& args)
{
if (args.size() != 1)
{
rError() << "Usage: " << COMMAND_PREFIX_ADDTOLAYER << " <LayerID> " << std::endl;
return;
}

DoWithMapLayerManager([&](ILayerManager& manager)
{
manager.addSelectionToLayer(args[0].getInt());
});
}

void moveSelectionToLayer(const cmd::ArgumentList& args)
{
if (args.size() != 1)
{
rError() << "Usage: " << COMMAND_PREFIX_MOVETOLAYER << " <LayerID> " << std::endl;
return;
}

DoWithMapLayerManager([&](ILayerManager& manager)
{
manager.moveSelectionToLayer(args[0].getInt());
});
}

void showLayer(const cmd::ArgumentList& args)
{
if (args.size() != 1)
{
rError() << "Usage: " << COMMAND_PREFIX_SHOWLAYER << " <LayerID> " << std::endl;
return;
}

DoWithMapLayerManager([&](ILayerManager& manager)
{
manager.setLayerVisibility(args[0].getInt(), true);
});
}

void hideLayer(const cmd::ArgumentList& args)
{
if (args.size() != 1)
{
rError() << "Usage: " << COMMAND_PREFIX_HIDELAYER << " <LayerID> " << std::endl;
return;
}

DoWithMapLayerManager([&](ILayerManager& manager)
{
manager.setLayerVisibility(args[0].getInt(), false);
});
}
};

module::StaticModule<LayerModule> layerManagerFactoryModule;
Expand Down

0 comments on commit dea548b

Please sign in to comment.