| 1 |
#pragma once |
| 2 |
|
| 3 |
#include <set> |
| 4 |
#include <string> |
| 5 |
#include <functional> |
| 6 |
#include "imodule.h" |
| 7 |
#include <sigc++/signal.h> |
| 8 |
|
| 9 |
namespace scene |
| 10 |
{ |
| 11 |
|
| 12 |
class INode; |
| 13 |
typedef std::shared_ptr<INode> INodePtr; |
| 14 |
|
| 15 |
// A list of named layers |
| 16 |
typedef std::set<int> LayerList; |
| 17 |
|
| 18 |
/** |
| 19 |
* greebo: Interface of a Layered object. |
| 20 |
*/ |
| 21 |
class Layered |
| 22 |
{ |
| 23 |
public: |
| 24 |
// Destructor |
| 25 |
virtual ~Layered() {} |
| 26 |
|
| 27 |
/** |
| 28 |
* Add this object to the layer with the given ID. |
| 29 |
*/ |
| 30 |
virtual void addToLayer(int layerId) = 0; |
| 31 |
|
| 32 |
/** |
| 33 |
* Moves this object to the layer. After this operation,the item is member |
| 34 |
* of this layer only, all other layer memberships are erased. |
| 35 |
*/ |
| 36 |
virtual void moveToLayer(int layerId) = 0; |
| 37 |
|
| 38 |
/** |
| 39 |
* Remove this object from the layer with the given ID. |
| 40 |
*/ |
| 41 |
virtual void removeFromLayer(int layerId) = 0; |
| 42 |
|
| 43 |
/** |
| 44 |
* Return the set of layers to which this object is assigned. |
| 45 |
*/ |
| 46 |
virtual const LayerList& getLayers() const = 0; |
| 47 |
|
| 48 |
/** |
| 49 |
* greebo: This assigns the given node to the given set of layers. Any previous |
| 50 |
* assignments of the node will be overwritten by this routine. |
| 51 |
* Note: The newLayers list must not be empty, otherwise the call will be ignored. |
| 52 |
*/ |
| 53 |
virtual void assignToLayers(const LayerList& newLayers) = 0; |
| 54 |
}; |
| 55 |
|
| 56 |
class ILayerManager |
| 57 |
{ |
| 58 |
public: |
| 59 |
typedef std::shared_ptr<ILayerManager> Ptr; |
| 60 |
|
| 61 |
virtual ~ILayerManager() {} |
| 62 |
|
| 63 |
/** |
| 64 |
* greebo: Creates a new layer with the given name. |
| 65 |
* |
| 66 |
* @returns: the ID of the layer of -1 if the layer could not |
| 67 |
* be created (e.g. due to a name conflict). |
| 68 |
*/ |
| 69 |
virtual int createLayer(const std::string& name) = 0; |
| 70 |
|
| 71 |
/** |
| 72 |
* greebo: Overload: Creates a new layer with the given name and the given ID. |
| 73 |
* |
| 74 |
* @returns: the ID of the layer of -1 if the layer could not |
| 75 |
* be created (e.g. due to a name/ID conflict). |
| 76 |
*/ |
| 77 |
virtual int createLayer(const std::string& name, int layerID) = 0; |
| 78 |
|
| 79 |
/** |
| 80 |
* greebo: Deletes the named layer. All nodes are removed |
| 81 |
* from this layer before deletion. |
| 82 |
*/ |
| 83 |
virtual void deleteLayer(const std::string& name) = 0; |
| 84 |
|
| 85 |
/** |
| 86 |
* greebo: Resets the layer system into its ground state. Deletes all |
| 87 |
* layers except for layer #0 which is renamed to "Default". |
| 88 |
*/ |
| 89 |
virtual void reset() = 0; |
| 90 |
|
| 91 |
typedef std::function<void(int layerId, const std::string& layerName)> LayerVisitFunc; |
| 92 |
|
| 93 |
/** |
| 94 |
* Functor is called using id and name as arguments |
| 95 |
*/ |
| 96 |
virtual void foreachLayer(const LayerVisitFunc& visitor) = 0; |
| 97 |
|
| 98 |
/** |
| 99 |
* greebo: Returns the ID of the named layer, or -1 if name doesn't exist |
| 100 |
*/ |
| 101 |
virtual int getLayerID(const std::string& name) const = 0; |
| 102 |
|
| 103 |
/** |
| 104 |
* greebo: Returns the name of the layer with the given ID or "" if it doesn't exist |
| 105 |
*/ |
| 106 |
virtual std::string getLayerName(int layerID) const = 0; |
| 107 |
|
| 108 |
/** |
| 109 |
* Returns TRUE if the given layer exists, FALSE otherwise. |
| 110 |
*/ |
| 111 |
virtual bool layerExists(int layerID) const = 0; |
| 112 |
|
| 113 |
/** |
| 114 |
* greebo: Renames the given layer. Returns TRUE on success, FALSE if the name is |
| 115 |
* already in use. |
| 116 |
*/ |
| 117 |
virtual bool renameLayer(int layerID, const std::string& newLayerName) = 0; |
| 118 |
|
| 119 |
/** |
| 120 |
* greebo: Returns the ID of the first visible layer or 0 (default layer) if none is visible. |
| 121 |
*/ |
| 122 |
virtual int getFirstVisibleLayer() const = 0; |
| 123 |
|
| 124 |
/** |
| 125 |
* Returns the ID of the layer that is currently signed as active (will return the default layer |
| 126 |
* if no other layer is active). Note that the returned layer is not necessarily visible. |
| 127 |
*/ |
| 128 |
virtual int getActiveLayer() const = 0; |
| 129 |
|
| 130 |
/** |
| 131 |
* Declare the given layer as active. Nothing will happen if the given layer ID is not existent. |
| 132 |
* This method doesn't change the visibility of the given layer. |
| 133 |
*/ |
| 134 |
virtual void setActiveLayer(int layerID) = 0; |
| 135 |
|
| 136 |
/** |
| 137 |
* greebo: Queries the visibility of the given layer. |
| 138 |
*/ |
| 139 |
virtual bool layerIsVisible(int layerID) = 0; |
| 140 |
|
| 141 |
/** |
| 142 |
* greebo: Sets the visibility of the given layer. |
| 143 |
* This operation will affect all child layers that might be |
| 144 |
* assigned to this one, recursively. |
| 145 |
*/ |
| 146 |
virtual void setLayerVisibility(int layerID, bool visible) = 0; |
| 147 |
|
| 148 |
/** |
| 149 |
* greebo: Traverses the selection and adds each node to the given layer. |
| 150 |
*/ |
| 151 |
virtual void addSelectionToLayer(int layerID) = 0; |
| 152 |
|
| 153 |
/** |
| 154 |
* greebo: Moves all selected nodes to the given layer. This implicitly |
| 155 |
* removes the nodes from all other layers. |
| 156 |
*/ |
| 157 |
virtual void moveSelectionToLayer(int layerID) = 0; |
| 158 |
|
| 159 |
/** |
| 160 |
* greebo: Removes the selected nodes from the given layers. |
| 161 |
*/ |
| 162 |
virtual void removeSelectionFromLayer(int layerID) = 0; |
| 163 |
|
| 164 |
/** |
| 165 |
* greebo: Updates the visibility of the given node based on the |
| 166 |
* current layer visibility settings. |
| 167 |
* |
| 168 |
* @returns: TRUE if the node was set to "visible", FALSE if the |
| 169 |
* current layer settings resulted to "invisible" and the |
| 170 |
* node was therefore hidden. |
| 171 |
*/ |
| 172 |
virtual bool updateNodeVisibility(const INodePtr& node) = 0; |
| 173 |
|
| 174 |
/** |
| 175 |
* greebo: Sets the selection status of the entire layer. |
| 176 |
* |
| 177 |
* @layerID: the ID of the layer to be selected/deselected. |
| 178 |
* @selected: TRUE if the layer should be selected, FALSE if the members |
| 179 |
* should be de-selected. |
| 180 |
*/ |
| 181 |
virtual void setSelected(int layerID, bool selected) = 0; |
| 182 |
|
| 183 |
/** |
| 184 |
* Returns the parent layer ID of the layer identified by the given ID. |
| 185 |
* Will return -1 if the given layer doesn't have a parent or doesn't exist. |
| 186 |
*/ |
| 187 |
virtual int getParentLayer(int layerId) = 0; |
| 188 |
|
| 189 |
/** |
| 190 |
* Sets the parent of the given child layer, replacing any previous parent. |
| 191 |
* |
| 192 |
* Any layer can be made a child of another layer, as long as the formed |
| 193 |
* tree is staying sane (no recursions). |
| 194 |
* Setting a parent layer ID of -1 will remove the parent and make this |
| 195 |
* a top-level layer. |
| 196 |
* |
| 197 |
* An attempt to form an invalid operation (like modifying the default layer |
| 198 |
* or forming a recursion) will throw a std::runtime_error. |
| 199 |
*/ |
| 200 |
virtual void setParentLayer(int childLayerId, int parentLayerId) = 0; |
| 201 |
|
| 202 |
/** |
| 203 |
* Returns true if the given parentLayerId is part of the ancestry of the |
| 204 |
* given candidateLayerId (the node itself is not part of the ancestry). |
| 205 |
* |
| 206 |
* Returns false if any of the given IDs is -1. |
| 207 |
*/ |
| 208 |
virtual bool layerIsChildOf(int candidateLayerId, int parentLayerId) = 0; |
| 209 |
|
| 210 |
/** |
| 211 |
* A signal for client code to get notified about layer creation, |
| 212 |
* renamings and removal. |
| 213 |
*/ |
| 214 |
virtual sigc::signal<void> signal_layersChanged() = 0; |
| 215 |
|
| 216 |
/** |
| 217 |
* Fired whenever visibility of a layer has been changed. |
| 218 |
*/ |
| 219 |
virtual sigc::signal<void> signal_layerVisibilityChanged() = 0; |
| 220 |
|
| 221 |
/** |
| 222 |
* Fired whenever a parent of a layer has been changed. |
| 223 |
*/ |
| 224 |
virtual sigc::signal<void> signal_layerHierarchyChanged() = 0; |
| 225 |
|
| 226 |
/** |
| 227 |
* Public signal to get notified about layer membership changes, |
| 228 |
* e.g. when a node has been added to a layer, or moved to a new one. |
| 229 |
* Since scene::INodes can be added or removed from layers directly, |
| 230 |
* without the LayerSystem knowing about this, it is sometimes the |
| 231 |
* responsibility of that algorithm code to emit this signal itself. |
| 232 |
*/ |
| 233 |
virtual sigc::signal<void> signal_nodeMembershipChanged() = 0; |
| 234 |
}; |
| 235 |
|
| 236 |
class ILayerModule : |
| 237 |
public RegisterableModule |
| 238 |
{ |
| 239 |
public: |
| 240 |
~ILayerModule() override {} |
| 241 |
|
| 242 |
/** |
| 243 |
* Creates a new layer manager instance associated to the given scene (root) node |
| 244 |
*/ |
| 245 |
virtual ILayerManager::Ptr createLayerManager(INode& rootNode) = 0; |
| 246 |
}; |
| 247 |
|
| 248 |
} // namespace scene |
| 249 |
|
| 250 |
constexpr const char* const MODULE_LAYERS("LayerModule"); |
| 251 |
|
| 252 |
inline scene::ILayerModule& GlobalLayerModule() |
| 253 |
{ |
| 254 |
static module::InstanceReference<scene::ILayerModule> _reference(MODULE_LAYERS); |
| 255 |
return _reference; |
| 256 |
} |