Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
node.h
Go to the documentation of this file.
1 
18 #ifndef ION_GFX_NODE_H_
19 #define ION_GFX_NODE_H_
20 
21 #include "ion/base/invalid.h"
22 #include "ion/base/referent.h"
24 #include "ion/gfx/shaderprogram.h"
25 #include "ion/gfx/shape.h"
26 #include "ion/gfx/statetable.h"
27 #include "ion/gfx/uniformblock.h"
28 
29 namespace ion {
30 namespace gfx {
31 
33 class Node;
35 
45 class ION_API Node : public base::Referent, public UniformHolder {
46  public:
47  Node();
48 
50  const std::string& GetLabel() const { return label_; }
51  void SetLabel(const std::string& label) { label_ = label; }
52 
54  void SetStateTable(const StateTablePtr& state_table) {
55  state_table_ = state_table;
56  }
57  const StateTablePtr& GetStateTable() const { return state_table_; }
58 
60  void SetShaderProgram(const ShaderProgramPtr& shader_program) {
61  shader_program_ = shader_program;
62  }
63  const ShaderProgramPtr& GetShaderProgram() const { return shader_program_; }
64 
67  void AddUniformBlock(const UniformBlockPtr& block) {
68  if (block.Get())
69  uniform_blocks_.push_back(block);
70  }
71  void ReplaceUniformBlock(size_t index, const UniformBlockPtr& block) {
72  if (index < uniform_blocks_.size() && block.Get())
73  uniform_blocks_[index] = block;
74  }
75  void ClearUniformBlocks() { uniform_blocks_.clear(); }
77  return uniform_blocks_;
78  }
79 
85  size_t AddShape(const ShapePtr& shape) {
86  size_t index = base::kInvalidIndex;
87  if (shape.Get()) {
88  index = shapes_.size();
89  shapes_.push_back(shape);
90  }
91  return index;
92  }
93  void ReplaceShape(size_t index, const ShapePtr& shape) {
94  if (index < shapes_.size() && shape.Get())
95  shapes_[index] = shape;
96  }
99  void RemoveShape(const ShapePtr& shape) {
100  for (auto it = shapes_.begin(); it != shapes_.end();) {
101  if (*it == shape)
102  it = shapes_.erase(it);
103  else
104  ++it;
105  }
106  }
109  void RemoveShapeAt(size_t index) {
110  if (index < shapes_.size()) {
111  auto it = shapes_.begin() + index;
112  shapes_.erase(it);
113  }
114  }
115  void ClearShapes() { shapes_.clear(); }
116  const base::AllocVector<ShapePtr>& GetShapes() const { return shapes_; }
117 
122  size_t AddChild(const NodePtr& child) {
123  size_t index = base::kInvalidIndex;
124  if (child.Get()) {
125  index = children_.size();
126  children_.push_back(child);
127  }
128  return index;
129  }
130  void ReplaceChild(size_t index, const NodePtr& child) {
131  if (index < children_.size() && child.Get())
132  children_[index] = child;
133  }
137  void RemoveChild(const NodePtr& child) {
138  for (auto it = children_.begin(); it != children_.end();) {
139  if (*it == child)
140  it = children_.erase(it);
141  else
142  ++it;
143  }
144  }
147  void RemoveChildAt(size_t index) {
148  if (index < children_.size()) {
149  auto it = children_.begin() + index;
150  children_.erase(it);
151  }
152  }
153  void ClearChildren() { children_.clear(); }
154  const base::AllocVector<NodePtr>& GetChildren() const { return children_; }
155 
156  protected:
159  ~Node() override;
160 
161  private:
162  StateTablePtr state_table_;
163  ShaderProgramPtr shader_program_;
165  base::AllocVector<NodePtr> children_;
166  base::AllocVector<UniformBlockPtr> uniform_blocks_;
169  std::string label_;
170 };
171 
172 } // namespace gfx
173 } // namespace ion
174 
175 #endif // ION_GFX_NODE_H_
size_t AddShape(const ShapePtr &shape)
Child node management.
Definition: node.h:85
void SetShaderProgram(const ShaderProgramPtr &shader_program)
Shader program management.
Definition: node.h:60
base::ReferentPtr< Node >::Type NodePtr
Definition: node.h:33
void RemoveChild(const NodePtr &child)
Removes all instances of child from this' children if it is actually a child of this.
Definition: node.h:137
const base::AllocVector< NodePtr > & GetChildren() const
Definition: node.h:154
void ReplaceUniformBlock(size_t index, const UniformBlockPtr &block)
Definition: node.h:71
void RemoveChildAt(size_t index)
Removes the child Node at the passed index if the index is valid.
Definition: node.h:147
const size_t kInvalidIndex
kInvalidIndex is a size_t value that is very unlikely to be a valid index.
Definition: invalid.cc:23
void SetStateTable(const StateTablePtr &state_table)
StateTable management.
Definition: node.h:54
A UniformHolder is a base class for an object that holds Uniforms.
Definition: uniformholder.h:39
void ReplaceShape(size_t index, const ShapePtr &shape)
Definition: node.h:93
const ShaderProgramPtr & GetShaderProgram() const
Definition: node.h:63
void RemoveShape(const ShapePtr &shape)
Removes all instances of the shape if it is contained in this' shapes.
Definition: node.h:99
const base::AllocVector< UniformBlockPtr > & GetUniformBlocks() const
Definition: node.h:76
void RemoveShapeAt(size_t index)
Removes the Shape at the passed index if the index is valid.
Definition: node.h:109
std::string label
Definition: printer.cc:354
const StateTablePtr & GetStateTable() const
Definition: node.h:57
Thread-safe abstract base class.
Definition: referent.h:49
const base::AllocVector< ShapePtr > & GetShapes() const
Definition: node.h:116
void SetLabel(const std::string &label)
Definition: node.h:51
T * Get() const
Returns a raw pointer to the instance, which may be NULL.
Definition: sharedptr.h:89
void ClearChildren()
Definition: node.h:153
void ReplaceChild(size_t index, const NodePtr &child)
Definition: node.h:130
size_t AddChild(const NodePtr &child)
Child node management.
Definition: node.h:122
const std::string & GetLabel() const
Returns/sets the label of this.
Definition: node.h:50
void ClearShapes()
Definition: node.h:115
A Node instance represents a node in a scene graph.
Definition: node.h:45
A SharedPtr is a smart shared pointer to an instance of some class that implements reference counting...
Definition: sharedptr.h:60
void AddUniformBlock(const UniformBlockPtr &block)
UniformBlock management.
Definition: node.h:67
void ClearUniformBlocks()
Definition: node.h:75
This class can be used in place of std::vector to allow an Ion Allocator to be used for memory alloca...
Definition: allocvector.h:50