| 1 |
#pragma once |
| 2 |
|
| 3 |
#include "Bounded.h" |
| 4 |
#include "ilayer.h" |
| 5 |
#include "irenderable.h" |
| 6 |
|
| 7 |
#include <set> |
| 8 |
#include <string> |
| 9 |
#include <memory> |
| 10 |
|
| 11 |
class AABB; |
| 12 |
class Matrix4; |
| 13 |
|
| 14 |
class IRenderEntity; |
| 15 |
typedef std::shared_ptr<IRenderEntity> IRenderEntityPtr; |
| 16 |
|
| 17 |
namespace scene |
| 18 |
{ |
| 19 |
|
| 20 |
/** |
| 21 |
* Interface for objects which can be filtered by the FilterSystem. |
| 22 |
*/ |
| 23 |
class Filterable |
| 24 |
{ |
| 25 |
public: |
| 26 |
/** |
| 27 |
* Destructor |
| 28 |
*/ |
| 29 |
virtual ~Filterable() {} |
| 30 |
|
| 31 |
/** |
| 32 |
* Return the filtered state of this object. Returns true if the object is |
| 33 |
* hidden due to filtering, and false if it is visible. |
| 34 |
* |
| 35 |
* It is up to the object's implementation to determine what criteria are |
| 36 |
* used for filtering (texture, entityclass etc), however these criteria |
| 37 |
* MUST be controlled by the filter system, and cannot include other |
| 38 |
* arbitrary criteria such as the time of day or available memory. |
| 39 |
* |
| 40 |
* Note that this is not the primary function to discard objects during |
| 41 |
* rendering or selection test. Use INode::visible() instead, which includes |
| 42 |
* the current "filtered" setting. |
| 43 |
*/ |
| 44 |
virtual bool isFiltered() const = 0; |
| 45 |
|
| 46 |
// Set the filtered status of this object |
| 47 |
virtual void setFiltered(bool filtered) = 0; |
| 48 |
}; |
| 49 |
|
| 50 |
class INode; |
| 51 |
typedef std::shared_ptr<INode> INodePtr; |
| 52 |
typedef std::weak_ptr<INode> INodeWeakPtr; |
| 53 |
|
| 54 |
class IMapRootNode; |
| 55 |
typedef std::shared_ptr<IMapRootNode> IMapRootNodePtr; |
| 56 |
|
| 57 |
class Graph; |
| 58 |
typedef std::shared_ptr<Graph> GraphPtr; |
| 59 |
|
| 60 |
class NodeVisitor |
| 61 |
{ |
| 62 |
public: |
| 63 |
/** |
| 64 |
* Destructor |
| 65 |
*/ |
| 66 |
virtual ~NodeVisitor() {} |
| 67 |
|
| 68 |
/** |
| 69 |
* greebo: Gets called before the children are traversed. |
| 70 |
* Return TRUE to traverse the children, FALSE to prevent this. |
| 71 |
*/ |
| 72 |
virtual bool pre(const INodePtr& node) = 0; |
| 73 |
|
| 74 |
/** |
| 75 |
* greebo: Optional post-traverse call, gets invoked after the children |
| 76 |
* of this node have been traversed. |
| 77 |
*/ |
| 78 |
virtual void post(const INodePtr& node) {} |
| 79 |
}; |
| 80 |
|
| 81 |
/** |
| 82 |
* \brief Main interface for a Node, a basic element of the scenegraph. |
| 83 |
* |
| 84 |
* All nodes share a certain set of functionality, such as being placed in |
| 85 |
* layers, being able to render themselves, and being able to hold and |
| 86 |
* transform a list of child nodes. |
| 87 |
*/ |
| 88 |
class INode : |
| 89 |
public Layered, |
| 90 |
public Filterable, |
| 91 |
public Bounded, |
| 92 |
public Renderable |
| 93 |
{ |
| 94 |
public: |
| 95 |
enum class Type |
| 96 |
{ |
| 97 |
Unknown = 0, |
| 98 |
MapRoot, |
| 99 |
Entity, |
| 100 |
Brush, |
| 101 |
Patch, |
| 102 |
Model, |
| 103 |
Particle, |
| 104 |
EntityConnection, |
| 105 |
MergeAction, |
| 106 |
}; |
| 107 |
|
| 108 |
public: |
| 109 |
|
| 110 |
virtual ~INode() {} |
| 111 |
|
| 112 |
/// Get the user-friendly string name of this node. |
| 113 |
virtual std::string name() const = 0; |
| 114 |
|
| 115 |
// Returns the type of this node |
| 116 |
virtual Type getNodeType() const = 0; |
| 117 |
|
| 118 |
/** |
| 119 |
* Set the scenegraph this node is belonging to. This is usually |
| 120 |
* set by the scenegraph itself during insertion. |
| 121 |
*/ |
| 122 |
virtual void setSceneGraph(const GraphPtr& sceneGraph) = 0; |
| 123 |
|
| 124 |
/** greebo: Returns true, if the node is the root element |
| 125 |
* of the scenegraph. |
| 126 |
*/ |
| 127 |
virtual bool isRoot() const = 0; |
| 128 |
|
| 129 |
/** greebo: Sets the "isRoot" flag of this node. |
| 130 |
*/ |
| 131 |
virtual void setIsRoot(bool isRoot) = 0; |
| 132 |
|
| 133 |
// Searching this node's ancestry, this returns the toplevel/root node |
| 134 |
virtual IMapRootNodePtr getRootNode() = 0; |
| 135 |
|
| 136 |
/** greebo: State bit accessor methods. This enables/disables |
| 137 |
* the bit of the state flag (e.g. hidden, excluded) |
| 138 |
*/ |
| 139 |
virtual void enable(unsigned int state) = 0; |
| 140 |
virtual void disable(unsigned int state) = 0; |
| 141 |
|
| 142 |
// Returns true if the given state bit mask is set, false otherwise |
| 143 |
virtual bool checkStateFlag(unsigned int state) const = 0; |
| 144 |
|
| 145 |
// Returns true if this node supports the given state flag |
| 146 |
virtual bool supportsStateFlag(unsigned int state) const = 0; |
| 147 |
|
| 148 |
// Defines the active/inactive state of a node |
| 149 |
// An inactive node might be rendered differently |
| 150 |
enum class RenderState |
| 151 |
{ |
| 152 |
Active, |
| 153 |
Inactive |
| 154 |
}; |
| 155 |
|
| 156 |
// Returns the current render state of this node |
| 157 |
virtual RenderState getRenderState() const = 0; |
| 158 |
|
| 159 |
// Sets the render state of this node |
| 160 |
virtual void setRenderState(RenderState state) = 0; |
| 161 |
|
| 162 |
/** greebo: Returns true, if the node is not hidden by |
| 163 |
* exclusion, filtering or anything else. |
| 164 |
*/ |
| 165 |
virtual bool visible() const = 0; |
| 166 |
|
| 167 |
/** greebo: Returns true, if the node is excluded (eExcluded flag set) |
| 168 |
*/ |
| 169 |
virtual bool excluded() const = 0; |
| 170 |
|
| 171 |
// Set the "forced visible" flag, which overrides the ordinary filtered/excluded state |
| 172 |
// This is used to force the rendering of nodes that are selected but would otherwise |
| 173 |
// be hidden due to the filtered/layered state. |
| 174 |
virtual void setForcedVisibility(bool forceVisible, bool includeChildren) = 0; |
| 175 |
|
| 176 |
// Child node handling |
| 177 |
|
| 178 |
// Adds a new child node (is appended at the end of the list of existing children) |
| 179 |
virtual void addChildNode(const INodePtr& node) = 0; |
| 180 |
|
| 181 |
// Adds a new child node (is inserted at the front of any existing children) |
| 182 |
// Useful in special scenarios like when adding a world spawn node. |
| 183 |
virtual void addChildNodeToFront(const INodePtr& node) = 0; |
| 184 |
|
| 185 |
virtual void removeChildNode(const INodePtr& node) = 0; |
| 186 |
virtual bool hasChildNodes() const = 0; |
| 187 |
|
| 188 |
/** |
| 189 |
* greebo: Traverses this node and all child nodes (recursively) |
| 190 |
* using the given visitor. |
| 191 |
* |
| 192 |
* Note: replaces the legacy Node_traverseSubgraph() method. |
| 193 |
*/ |
| 194 |
virtual void traverse(NodeVisitor& visitor) = 0; |
| 195 |
|
| 196 |
/** |
| 197 |
* greebo: Traverses all child nodes (recursively) using the given visitor. |
| 198 |
* Note: this will NOT visit the current node. |
| 199 |
*/ |
| 200 |
virtual void traverseChildren(NodeVisitor& visitor) const = 0; |
| 201 |
|
| 202 |
/** |
| 203 |
* Traversal function which can be used to hit all nodes in a |
| 204 |
* graph or collection. If the functor returns false traversal stops. |
| 205 |
*/ |
| 206 |
typedef std::function<bool(const scene::INodePtr&)> VisitorFunc; |
| 207 |
|
| 208 |
/** |
| 209 |
* Call the given functor for each child node, depth first |
| 210 |
* This is a simpler alternative to the usual traverse() method |
| 211 |
* which provides pre() and post() methods and more control about |
| 212 |
* which nodes to traverse and. This forEachNode() routine simply |
| 213 |
* hits every child node including their children. |
| 214 |
* |
| 215 |
* @returns: true if the functor returned false on any of the |
| 216 |
* visited nodes. The return type is used to pass the stop signal |
| 217 |
* up the stack during traversal. |
| 218 |
*/ |
| 219 |
virtual bool foreachNode(const VisitorFunc& functor) const = 0; |
| 220 |
|
| 221 |
/** |
| 222 |
* Returns a shared_ptr to itself. |
| 223 |
*/ |
| 224 |
virtual scene::INodePtr getSelf() = 0; |
| 225 |
|
| 226 |
// Set the parent of this node, is called on insertion in a traversable |
| 227 |
virtual void setParent(const INodePtr& parent) = 0; |
| 228 |
virtual scene::INodePtr getParent() const = 0; |
| 229 |
|
| 230 |
/** |
| 231 |
* greebo: Gets called after the node has been inserted into the scene. |
| 232 |
*/ |
| 233 |
virtual void onInsertIntoScene(IMapRootNode& root) = 0; |
| 234 |
|
| 235 |
/** |
| 236 |
* greebo: This gets called by the SceneGraph before the Node is actually |
| 237 |
* removed from the scene. This gives the node the opportunity to |
| 238 |
* change its "selected" status or anything else. |
| 239 |
*/ |
| 240 |
virtual void onRemoveFromScene(IMapRootNode& root) = 0; |
| 241 |
|
| 242 |
/** |
| 243 |
* Returns true if this node is in the scene |
| 244 |
*/ |
| 245 |
virtual bool inScene() const = 0; |
| 246 |
|
| 247 |
// Get/Set the render entity this node is attached to |
| 248 |
virtual IRenderEntity* getRenderEntity() const = 0; |
| 249 |
virtual void setRenderEntity(IRenderEntity* entity) = 0; |
| 250 |
|
| 251 |
// Call this if the node gets changed in any way or gets inserted somewhere. |
| 252 |
virtual void boundsChanged() = 0; |
| 253 |
// Call this on transform change |
| 254 |
virtual void transformChanged() = 0; |
| 255 |
|
| 256 |
// Returns the bounds in world coordinates |
| 257 |
virtual const AABB& worldAABB() const = 0; |
| 258 |
|
| 259 |
/** |
| 260 |
* \brief Return the transformation from local to world coordinates |
| 261 |
* |
| 262 |
* This represents the final transformation from this node's own coordinate |
| 263 |
* space into world space, including any transformations inherited from |
| 264 |
* parent nodes. |
| 265 |
*/ |
| 266 |
virtual const Matrix4& localToWorld() const = 0; |
| 267 |
|
| 268 |
// Undo/Redo events - some nodes need to do extra legwork after undo or redo |
| 269 |
// This is called by the TraversableNodeSet after a undo/redo operation |
| 270 |
// not by the UndoSystem itself, at least not yet. |
| 271 |
virtual void onPostUndo() {} |
| 272 |
virtual void onPostRedo() {} |
| 273 |
|
| 274 |
// Called during recursive transform changed, but only by INodes themselves |
| 275 |
virtual void transformChangedLocal() = 0; |
| 276 |
|
| 277 |
// Method invoked on every node whenever the filter system |
| 278 |
// changes its state - nodes get a chance to react on that |
| 279 |
virtual void onFiltersChanged() = 0; |
| 280 |
}; |
| 281 |
|
| 282 |
/// Cast an INode to a particular interface |
| 283 |
template<typename Interface> |
| 284 |
std::shared_ptr<Interface> node_cast(const INodePtr& node) |
| 285 |
{ |
| 286 |
return std::dynamic_pointer_cast<Interface>(node); |
| 287 |
} |
| 288 |
|
| 289 |
} // namespace scene |