Revision: 1059
Author: allain.lalonde
Date: Tue Aug 31 09:02:58 2010
Log: Refactoring out invalidateBounds method and adding PImage bounds logic.
http://code.google.com/p/piccolo2d/source/detail?r=1059
Modified:
/piccolo2d.js/trunk/piccolo2d.js
/piccolo2d.js/trunk/test.html
=======================================
--- /piccolo2d.js/trunk/piccolo2d.js Tue Aug 31 06:08:47 2010
+++ /piccolo2d.js/trunk/piccolo2d.js Tue Aug 31 09:02:58 2010
@@ -351,8 +351,7 @@
scale: function (ratio) {
this.transform.scale(ratio);
- this.fullBounds = null;
- this.globalFullBounds = null;
+ this.invalidateBounds();
this.invalidatePaint();
return this;
@@ -360,8 +359,7 @@
translate: function (dx, dy) {
this.transform.translate(dx, dy);
- this.fullBounds = null;
- this.globalFullBounds = null;
+ this.invalidateBounds();
this.invalidatePaint();
return this;
@@ -376,8 +374,7 @@
addChild: function (child) {
this.children.push(child);
- this.fullBounds = null;
- this.globalFullBounds = null;
+ this.invalidateBounds();
child.parent = this;
this.invalidatePaint();
@@ -386,8 +383,7 @@
removeChild: function (child) {
child.parent = null;
- this.fullBounds = null;
- this.globalFullBounds = null;
+ this.invalidateBounds();
this.children = this.children.remove(child);
this.invalidatePaint();
@@ -396,8 +392,8 @@
setTransform: function (transform) {
this.transform = transform;
- this.fullBounds = null;
- this.globalFullBounds = null;
+
+ this.invalidateBounds();
this.invalidatePaint();
return this;
@@ -425,7 +421,7 @@
var child, childFullBounds, tBounds;
- for (var i = 0; i < this.children.length; i += 1) {
+ for (var i = 0, n = this.children.length; i < n; i += 1) {
child = this.children[i];
childFullBounds = child.getFullBounds();
tBounds = child.transform.transform(childFullBounds);
@@ -472,6 +468,15 @@
return t;
},
+
+ invalidateBounds: function() {
+ this.fullBounds = null;
+ this.globalFullBounds = null;
+
+ if (this.parent) {
+ this.parent.invalidateBounds();
+ }
+ },
localToParent: function (target) {
return this.transform.transform(target);
@@ -539,8 +544,7 @@
var metric = PText.hiddenContext.measureText(this.text);
this.bounds.width = metric.width;
this.bounds.height = this.text ? PText.fontSize : 0;
- this.fullBounds = null;
- this.globalFullBounds = null;
+ this.invalidateBounds();
}
});
PText.hiddenContext =
document.createElement("canvas").getContext("2d");
@@ -549,6 +553,7 @@
PImage = PNode.extend({
init: function (arg) {
+ var _this = this;
if (typeof arg === "string") {
this.url = arg;
this._super();
@@ -560,8 +565,14 @@
this.loaded = false;
this.image = new Image();
- this.image.onload = function () {
- this.loaded = true;
+ this.image.onload = function () {
+ _this.bounds.width = this.width;
+ _this.bounds.height = this.height;
+ console.log(_this.bounds.width);
+ _this.loaded = true;
+ _this.invalidateBounds();
+
+ _this.invalidatePaint();
};
this.image.src = this.url;
=======================================
--- /piccolo2d.js/trunk/test.html Tue Aug 31 06:08:47 2010
+++ /piccolo2d.js/trunk/test.html Tue Aug 31 09:02:58 2010
@@ -541,6 +541,17 @@
function testPImage() {
module("PImage");
+
+ test("PImage may be created with a url", function() {
+ var img = new
PImage("http://www.piccolo2d.org/images/Piccolo2D-Logo-small.png");
+ ok(img != null);
+ });
+
+ test("PImage has empty bounds when created", function() {
+ var img = new
PImage("http://www.piccolo2d.org/images/Piccolo2D-Logo-small.png");
+ equal(img.bounds.width, 0);
+ equal(img.bounds.height, 0);
+ });
}
function testPLayer() {
--
Piccolo2D Developers Group: http://groups.google.com/group/piccolo2d-dev?hl=en