Commits

Christian Fischer  committed e4c2ce1

Node2D's bounding box no longer depends to the pivot, bounding box is now in the node's local coordinate system

  • Participants
  • Parent commits f35abc2

Comments (0)

Files changed (4)

File src/common/wiesel/graph/2d/node2d.cpp

  */
 #include "node2d.h"
 
-#include <math.h>
+#include <cmath>
 
 
 using namespace wiesel;
 
 void Node2D::setContentSize(const dimension &size) {
 	this->bounds.size = size;
-	updateBounds();
 	return;
 }
 
 void Node2D::setContentSize(float width, float height) {
 	this->bounds.size.width  = width;
 	this->bounds.size.height = height;
-	updateBounds();
 	return;
 }
 
 
 void Node2D::setPivot(const vector2d &pivot) {
 	this->pivot = pivot;
-	updateBounds();
 	return;
 }
 
 void Node2D::setPivot(float x, float y) {
 	this->pivot.x = x;
 	this->pivot.y = y;
-	updateBounds();
 	setTransformDirty();
 	return;
 }
 
 
-void Node2D::updateBounds() {
-	bounds.position.x = -(bounds.size.width  * pivot.x);
-	bounds.position.y = -(bounds.size.height * pivot.y);
-	return;
+void Node2D::setBounds(const rect& bounds) {
+	this->bounds = bounds;
+}
+
+
+vector2d Node2D::getPivotInUnits() const {
+	return vector2d(
+			pivot.x * std::max(std::abs(getBounds().getMinX()), std::abs(getBounds().getMaxX())),
+			pivot.y * std::max(std::abs(getBounds().getMinY()), std::abs(getBounds().getMaxY()))
+	);
 }
 
 
 void Node2D::updateTransform() {
 	local_transform = matrix4x4::identity;
 
-	local_transform.translate(+bounds.position.x, +bounds.position.y);
+	vector2d pivot_in_units = getPivotInUnits();
+
+	local_transform.translate(-pivot_in_units.x, -pivot_in_units.y);
 	local_transform.scale(scale_x, scale_y, 1.0f);
 	local_transform.rotateZ(rotation * M_PI / 180.0f);
 	local_transform.translate(position.x, position.y);
 	// when 'local' is already in local coordinate space,
 	// the pivot offset is already included in the 'local' coordinate
 	if (
-			local.x >= 0
-		&&	local.y >= 0
-		&&	local.x <= getContentSize().width
-		&&	local.y <= getContentSize().height
+			local.x >= getBounds().getMinX()
+		&&	local.y >= getBounds().getMinY()
+		&&	local.x <= getBounds().getMaxX()
+		&&	local.y <= getBounds().getMaxY()
 	) {
 		return true;
 	}

File src/common/wiesel/graph/2d/node2d.h

 		/// usually this value would be between 0,0 (bottom left) and 1,1 (top right).
 		void setPivot(float x, float y);
 
+		/// set the 2D bounding rectangle of this node.
+		void setBounds(const rect &bounds);
+
 		/// get the content size of this node.
 		inline const dimension& getContentSize() const {
 			return bounds.size;
 			return pivot;
 		}
 
+		/// get the pivot offset in units, relative to the node's boundings
+		vector2d getPivotInUnits() const;
+
 		/// get the bounding rect of this node.
+		/// boundings will be stored in local coordinates, which means, a point,
+		/// which should be compared to the boundings, needs to be transformed first.
 		inline const rect& getBounds() const {
 			return bounds;
 		}
 
-	protected:
-		/// update the bounding rect after content size or pivot point has changed.
-		void updateBounds();
-
 	// transformation setter/getter
 	public:
 		/// set the position of this node, relative to it's parent.

File src/common/wiesel/graph/node.cpp

 		world_transform = local_transform;
 	}
 	else {
-		world_transform = getParent()->getWorldTransform() * local_transform;
+		world_transform = local_transform * getParent()->getWorldTransform();
 	}
 
 	transform_dirty = false;

File src/common/wiesel/ui/touchhandler.cpp

 
 	// ask all children in front
 	for(; (it!=node->getChildren()->rend()) && ((*it)->getOrderKey() >= 0); it++) {
-		Node *node = recursive_findNode(touch, *it);
+		Node *found = recursive_findNode(touch, *it);
 
-		if (node) {
-			return node;
+		if (found) {
+			return found;
 		}
 	}
 
 
 	// ask all children below the current node
 	for(; it!=node->getChildren()->rend(); it++) {
-		Node *node = recursive_findNode(touch, *it);
+		Node *found = recursive_findNode(touch, *it);
 
-		if (node) {
-			return node;
+		if (found) {
+			return found;
 		}
 	}