Skip to content

Commit

Permalink
Further disentangle LayerSystem from its UI elements
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Jan 2, 2017
1 parent 3b53afc commit d43cd6d
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 38 deletions.
12 changes: 12 additions & 0 deletions include/ilayer.h
Expand Up @@ -4,6 +4,7 @@
#include <string>
#include <functional>
#include "imodule.h"
#include <sigc++/signal.h>

namespace scene
{
Expand Down Expand Up @@ -178,6 +179,17 @@ class ILayerSystem :
* should be de-selected.
*/
virtual void setSelected(int layerID, bool selected) = 0;

/**
* A signal for client code to get notified about layer creation,
* addition, removal.
*/
virtual sigc::signal<void> signal_layersChanged() = 0;

/**
*
*/
virtual sigc::signal<void> signal_layerVisibilityChanged() = 0;
};

} // namespace scene
Expand Down
35 changes: 21 additions & 14 deletions radiant/layers/LayerSystem.cpp
Expand Up @@ -22,8 +22,6 @@
#include "wxutil/dialog/MessageBox.h"
#include "wxutil/EntryAbortedException.h"

#include "ui/layers/LayerControlDialog.h"

#include <functional>

namespace scene
Expand Down Expand Up @@ -141,7 +139,8 @@ void LayerSystem::reset()
_layerVisibility[DEFAULT_LAYER] = true;

// Update the LayerControlDialog
ui::LayerControlDialog::Instance().refresh();
_layersChangedSignal.emit();
_layerVisibilityChangedSignal.emit();
}

bool LayerSystem::renameLayer(int layerID, const std::string& newLayerName)
Expand Down Expand Up @@ -271,15 +270,16 @@ void LayerSystem::updateSceneGraphVisibility() {
GlobalSceneGraph().root()->traverseChildren(walker);
}

void LayerSystem::onLayerVisibilityChanged() {
void LayerSystem::onLayerVisibilityChanged()
{
// Update all nodes
updateSceneGraphVisibility();

// Redraw
SceneChangeNotify();

// Update the LayerControlDialog
ui::LayerControlDialog::Instance().update();
_layerVisibilityChangedSignal.emit();
}

void LayerSystem::addSelectionToLayer(int layerID) {
Expand Down Expand Up @@ -394,6 +394,16 @@ void LayerSystem::setSelected(int layerID, bool selected) {
}
}

sigc::signal<void> LayerSystem::signal_layersChanged()
{
return _layersChangedSignal;
}

sigc::signal<void> LayerSystem::signal_layerVisibilityChanged()
{
return _layerVisibilityChangedSignal;
}

int LayerSystem::getLayerID(const std::string& name) const {
for (LayerMap::const_iterator i = _layers.begin(); i != _layers.end(); i++) {
if (i->second == name) {
Expand Down Expand Up @@ -471,10 +481,9 @@ void LayerSystem::initialiseModule(const ApplicationContext& ctx)
createLayer(_(DEFAULT_LAYER_NAME));

// Add command targets for the first 10 layer IDs here
for (int i = 0; i < 10; i++) {
_commandTargets.push_back(
LayerCommandTargetPtr(new LayerCommandTarget(i))
);
for (int i = 0; i < 10; i++)
{
_commandTargets.push_back(std::make_shared<LayerCommandTarget>(i));
}

// Register the "create layer" command
Expand All @@ -483,9 +492,6 @@ void LayerSystem::initialiseModule(const ApplicationContext& ctx)
cmd::ARGTYPE_STRING|cmd::ARGTYPE_OPTIONAL);
IEventPtr ev = GlobalEventManager().addCommand("CreateNewLayer", "CreateNewLayer");

GlobalCommandSystem().addCommand("ToggleLayerControlDialog", ui::LayerControlDialog::toggle);
GlobalEventManager().addCommand("ToggleLayerControlDialog", "ToggleLayerControlDialog");

GlobalMapModule().signal_mapEvent().connect(
sigc::mem_fun(*this, &LayerSystem::onMapEvent)
);
Expand Down Expand Up @@ -534,9 +540,10 @@ void LayerSystem::createLayerCmd(const cmd::ArgumentList& args)
// Attempt to create the layer, this will return -1 if the operation fails
int layerID = createLayer(layerName);

if (layerID != -1) {
if (layerID != -1)
{
// Success, break the loop
ui::LayerControlDialog::Instance().refresh();
_layersChangedSignal.emit();
break;
}
else {
Expand Down
6 changes: 6 additions & 0 deletions radiant/layers/LayerSystem.h
Expand Up @@ -36,6 +36,9 @@ class LayerSystem :
// The ID of the active layer
int _activeLayer;

sigc::signal<void> _layersChangedSignal;
sigc::signal<void> _layerVisibilityChangedSignal;

public:
LayerSystem();

Expand Down Expand Up @@ -121,6 +124,9 @@ class LayerSystem :
// Selects/unselects an entire layer
void setSelected(int layerID, bool selected);

sigc::signal<void> signal_layersChanged() override;
sigc::signal<void> signal_layerVisibilityChanged() override;

// RegisterableModule implementation
const std::string& getName() const;
const StringSet& getDependencies() const;
Expand Down
44 changes: 23 additions & 21 deletions radiant/ui/UserInterfaceModule.cpp
Expand Up @@ -9,6 +9,7 @@
#include "modulesystem/StaticModule.h"

#include "ui/layers/LayerOrthoContextMenuItem.h"
#include "ui/layers/LayerControlDialog.h"

namespace ui
{
Expand Down Expand Up @@ -47,34 +48,35 @@ void UserInterfaceModule::initialiseModule(const ApplicationContext& ctx)
{
rMessage() << getName() << "::initialiseModule called." << std::endl;

// Register LayerControlDialog
GlobalCommandSystem().addCommand("ToggleLayerControlDialog", LayerControlDialog::toggle);
GlobalEventManager().addCommand("ToggleLayerControlDialog", "ToggleLayerControlDialog");

// Create a new menu item connected to the CreateNewLayer command
_layerMenuItems.push_back(
std::make_shared<wxutil::CommandMenuItem>(
new wxutil::IconTextMenuItem(_(CREATE_LAYER_TEXT), LAYER_ICON), "CreateNewLayer"));
GlobalOrthoContextMenu().addItem(std::make_shared<wxutil::CommandMenuItem>(
new wxutil::IconTextMenuItem(_(CREATE_LAYER_TEXT), LAYER_ICON), "CreateNewLayer"),
IOrthoContextMenu::SECTION_LAYER
);

// Add the orthocontext menu's layer actions
_layerMenuItems.push_back(
std::make_shared<LayerOrthoContextMenuItem>(_(ADD_TO_LAYER_TEXT), LayerOrthoContextMenuItem::AddToLayer));

_layerMenuItems.push_back(
std::make_shared<LayerOrthoContextMenuItem>(_(MOVE_TO_LAYER_TEXT), LayerOrthoContextMenuItem::MoveToLayer));

_layerMenuItems.push_back(
std::make_shared<LayerOrthoContextMenuItem>(_(REMOVE_FROM_LAYER_TEXT), LayerOrthoContextMenuItem::RemoveFromLayer));

for (const IMenuItemPtr& item : _layerMenuItems)
{
GlobalOrthoContextMenu().addItem(item, IOrthoContextMenu::SECTION_LAYER);
}
GlobalOrthoContextMenu().addItem(
std::make_shared<LayerOrthoContextMenuItem>(_(ADD_TO_LAYER_TEXT), LayerOrthoContextMenuItem::AddToLayer),
IOrthoContextMenu::SECTION_LAYER
);

GlobalOrthoContextMenu().addItem(
std::make_shared<LayerOrthoContextMenuItem>(_(MOVE_TO_LAYER_TEXT), LayerOrthoContextMenuItem::MoveToLayer),
IOrthoContextMenu::SECTION_LAYER
);

GlobalOrthoContextMenu().addItem(
std::make_shared<LayerOrthoContextMenuItem>(_(REMOVE_FROM_LAYER_TEXT), LayerOrthoContextMenuItem::RemoveFromLayer),
IOrthoContextMenu::SECTION_LAYER
);
}

void UserInterfaceModule::shutdownModule()
{
// Remove layer items again
for (const IMenuItemPtr& item : _layerMenuItems)
{
GlobalOrthoContextMenu().removeItem(item);
}
}

// Static module registration
Expand Down
3 changes: 0 additions & 3 deletions radiant/ui/UserInterfaceModule.h
Expand Up @@ -16,9 +16,6 @@ namespace ui
class UserInterfaceModule :
public RegisterableModule
{
private:
std::vector<IMenuItemPtr> _layerMenuItems;

public:
// RegisterableModule
const std::string & getName() const override;
Expand Down
10 changes: 10 additions & 0 deletions radiant/ui/layers/LayerControlDialog.cpp
Expand Up @@ -264,12 +264,22 @@ void LayerControlDialog::_preShow()
_rescanSelectionOnIdle = true;
});

// Layer creation/addition/removal triggers a refresh
_layersChangedSignal = GlobalLayerSystem().signal_layersChanged().connect(
sigc::mem_fun(this, &LayerControlDialog::refresh));

// Visibility change doesn't repopulate the dialog
_layerVisibilityChangedSignal = GlobalLayerSystem().signal_layerVisibilityChanged().connect(
sigc::mem_fun(this, &LayerControlDialog::update));

// Re-populate the dialog
refresh();
}

void LayerControlDialog::_postHide()
{
_layersChangedSignal.disconnect();
_layerVisibilityChangedSignal.disconnect();
_selectionChangedSignal.disconnect();
_rescanSelectionOnIdle = false;
}
Expand Down
2 changes: 2 additions & 0 deletions radiant/ui/layers/LayerControlDialog.h
Expand Up @@ -36,6 +36,8 @@ class LayerControlDialog :

bool _rescanSelectionOnIdle;
sigc::connection _selectionChangedSignal;
sigc::connection _layersChangedSignal;
sigc::connection _layerVisibilityChangedSignal;

public:
LayerControlDialog();
Expand Down

0 comments on commit d43cd6d

Please sign in to comment.