From 02ae00fcfc9aa7b6696631794e62496628972b29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Tue, 17 Apr 2012 16:23:15 +0200 Subject: [PATCH] Fluent interface for Object transformation. Now you can use method chaining while setting Object transformation - scale, rotation, translation and parent. --- src/Object.cpp | 12 +++++++----- src/Object.h | 16 ++++++++++------ 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/Object.cpp b/src/Object.cpp index 6a201e05e..ee29a91ff 100644 --- a/src/Object.cpp +++ b/src/Object.cpp @@ -21,9 +21,9 @@ using namespace std; namespace Magnum { -void Object::setParent(Object* parent) { +Object* Object::setParent(Object* parent) { /* Skip if nothing to do or this is scene */ - if(_parent == parent || _parent == this) return; + if(_parent == parent || _parent == this) return this; /* Add the object to children list of new parent */ if(parent != nullptr) { @@ -31,7 +31,7 @@ void Object::setParent(Object* parent) { /* Only Fry can be his own grandfather */ Object* p = parent; while(p != nullptr && p->parent() != p) { - if(p == this) return; + if(p == this) return this; p = p->parent(); } @@ -46,6 +46,7 @@ void Object::setParent(Object* parent) { _parent = parent; setDirty(); + return this; } Matrix4 Object::absoluteTransformation(Camera* camera) { @@ -93,11 +94,12 @@ Scene* Object::scene() { return nullptr; } -void Object::setTransformation(const Matrix4& transformation) { - if(_parent == this) return; +Object* Object::setTransformation(const Matrix4& transformation) { + if(_parent == this) return this; _transformation = transformation; setDirty(); + return this; } void Object::setDirty() { diff --git a/src/Object.h b/src/Object.h index aa4081188..2fa6f5f68 100644 --- a/src/Object.h +++ b/src/Object.h @@ -74,7 +74,7 @@ class MAGNUM_EXPORT Object { inline const std::set& children() { return _children; } /** @brief Set parent object */ - void setParent(Object* parent); + Object* setParent(Object* parent); /** * @brief Transformation matrix @@ -96,7 +96,7 @@ class MAGNUM_EXPORT Object { virtual Matrix4 absoluteTransformation(Camera* camera = nullptr); /** @brief Set transformation matrix */ - void setTransformation(const Matrix4& transformation); + Object* setTransformation(const Matrix4& transformation); /** * @brief Multiply transformation matrix @@ -107,8 +107,9 @@ class MAGNUM_EXPORT Object { * * Multiplies current transformation matrix by new matrix. */ - inline void multiplyTransformation(const Matrix4& transformation, bool global = true) { + inline Object* multiplyTransformation(const Matrix4& transformation, bool global = true) { setTransformation(global ? transformation*_transformation : _transformation*transformation); + return this; } /** @@ -116,8 +117,9 @@ class MAGNUM_EXPORT Object { * * Same as calling multiplyTransformation() with Matrix4::translation(). */ - inline void translate(Vector3 vec, bool global = true) { + inline Object* translate(Vector3 vec, bool global = true) { multiplyTransformation(Matrix4::translation(vec), global); + return this; } /** @@ -125,8 +127,9 @@ class MAGNUM_EXPORT Object { * * Same as calling multiplyTransformation() with Matrix4::scaling(). */ - inline void scale(Vector3 vec, bool global = true) { + inline Object* scale(Vector3 vec, bool global = true) { multiplyTransformation(Matrix4::scaling(vec), global); + return this; } /** @@ -134,8 +137,9 @@ class MAGNUM_EXPORT Object { * * Same as calling multiplyTransformation() with Matrix4::rotation(). */ - inline void rotate(GLfloat angle, Vector3 vec, bool global = true) { + inline Object* rotate(GLfloat angle, Vector3 vec, bool global = true) { multiplyTransformation(Matrix4::rotation(angle, vec), global); + return this; } /** @{ @name Caching helpers