|
| 1 | +/** |
| 2 | +* @author Richard Davey <rich@photonstorm.com> |
| 3 | +* @copyright 2016 Photon Storm Ltd. |
| 4 | +* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License} |
| 5 | +*/ |
| 6 | +Phaser.GameObjects.Container = function () { |
| 7 | + |
| 8 | + /** |
| 9 | + * @property {number} type - The const type of this object. |
| 10 | + * @readonly |
| 11 | + */ |
| 12 | + this.type = Phaser.CONTAINER; |
| 13 | + |
| 14 | + /** |
| 15 | + * If `ignoreChildInput` is `false` it will allow this objects _children_ to be considered as valid for Input events. |
| 16 | + * |
| 17 | + * If this property is `true` then the children will _not_ be considered as valid for Input events. |
| 18 | + * |
| 19 | + * Note that this property isn't recursive: only immediate children are influenced, it doesn't scan further down. |
| 20 | + * @property {boolean} ignoreChildInput |
| 21 | + * @default |
| 22 | + */ |
| 23 | + this.ignoreChildInput = false; |
| 24 | + |
| 25 | + |
| 26 | + // PIXI.Sprite.call(this, Phaser.Cache.DEFAULT); |
| 27 | + |
| 28 | + // Phaser.Component.Core.init.call(this, game, x, y, key, frame); |
| 29 | + |
| 30 | +}; |
| 31 | + |
| 32 | +Phaser.GameObjects.Container.prototype = Object.create(PIXI.Sprite.prototype); |
| 33 | +Phaser.GameObjects.Container.prototype.constructor = Phaser.GameObjects.Container; |
| 34 | + |
| 35 | +/* |
| 36 | + * Updates the transform on all children of this container for rendering |
| 37 | + * |
| 38 | + * @method updateTransform |
| 39 | + * @private |
| 40 | + */ |
| 41 | +PIXI.DisplayObjectContainer.prototype.updateTransform = function () { |
| 42 | + |
| 43 | + if (!this.visible) |
| 44 | + { |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + this.displayObjectUpdateTransform(); |
| 49 | + |
| 50 | + if (this._cacheAsBitmap) |
| 51 | + { |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + for (var i = 0; i < this.children.length; i++) |
| 56 | + { |
| 57 | + this.children[i].updateTransform(); |
| 58 | + } |
| 59 | + |
| 60 | +}; |
| 61 | + |
| 62 | +/** |
| 63 | + * Retrieves the global bounds of the displayObjectContainer as a rectangle. The bounds calculation takes all visible children into consideration. |
| 64 | + * |
| 65 | + * @method getBounds |
| 66 | + * @param {PIXI.DisplayObject|PIXI.Matrix} [targetCoordinateSpace] Returns a rectangle that defines the area of the display object relative to the coordinate system of the targetCoordinateSpace object. |
| 67 | + * @return {Rectangle} The rectangular bounding area |
| 68 | + */ |
| 69 | +PIXI.DisplayObjectContainer.prototype.getBounds = function (targetCoordinateSpace) { |
| 70 | + |
| 71 | + var isTargetCoordinateSpaceDisplayObject = (targetCoordinateSpace && targetCoordinateSpace instanceof PIXI.DisplayObject); |
| 72 | + var isTargetCoordinateSpaceThisOrParent = true; |
| 73 | + |
| 74 | + if (!isTargetCoordinateSpaceDisplayObject) |
| 75 | + { |
| 76 | + targetCoordinateSpace = this; |
| 77 | + } |
| 78 | + else if (targetCoordinateSpace instanceof PIXI.DisplayObjectContainer) |
| 79 | + { |
| 80 | + isTargetCoordinateSpaceThisOrParent = targetCoordinateSpace.contains(this); |
| 81 | + } |
| 82 | + else |
| 83 | + { |
| 84 | + isTargetCoordinateSpaceThisOrParent = false; |
| 85 | + } |
| 86 | + |
| 87 | + var i; |
| 88 | + |
| 89 | + if (isTargetCoordinateSpaceDisplayObject) |
| 90 | + { |
| 91 | + var matrixCache = targetCoordinateSpace.worldTransform; |
| 92 | + |
| 93 | + targetCoordinateSpace.worldTransform = Phaser.identityMatrix; |
| 94 | + |
| 95 | + for (i = 0; i < targetCoordinateSpace.children.length; i++) |
| 96 | + { |
| 97 | + targetCoordinateSpace.children[i].updateTransform(); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + var minX = Infinity; |
| 102 | + var minY = Infinity; |
| 103 | + |
| 104 | + var maxX = -Infinity; |
| 105 | + var maxY = -Infinity; |
| 106 | + |
| 107 | + var childBounds; |
| 108 | + var childMaxX; |
| 109 | + var childMaxY; |
| 110 | + |
| 111 | + var childVisible = false; |
| 112 | + |
| 113 | + for (i = 0; i < this.children.length; i++) |
| 114 | + { |
| 115 | + var child = this.children[i]; |
| 116 | + |
| 117 | + if (!child.visible) |
| 118 | + { |
| 119 | + continue; |
| 120 | + } |
| 121 | + |
| 122 | + childVisible = true; |
| 123 | + |
| 124 | + childBounds = this.children[i].getBounds(); |
| 125 | + |
| 126 | + minX = (minX < childBounds.x) ? minX : childBounds.x; |
| 127 | + minY = (minY < childBounds.y) ? minY : childBounds.y; |
| 128 | + |
| 129 | + childMaxX = childBounds.width + childBounds.x; |
| 130 | + childMaxY = childBounds.height + childBounds.y; |
| 131 | + |
| 132 | + maxX = (maxX > childMaxX) ? maxX : childMaxX; |
| 133 | + maxY = (maxY > childMaxY) ? maxY : childMaxY; |
| 134 | + } |
| 135 | + |
| 136 | + var bounds = this._bounds; |
| 137 | + |
| 138 | + if (!childVisible) |
| 139 | + { |
| 140 | + bounds = new Phaser.Rectangle(); |
| 141 | + |
| 142 | + var w0 = bounds.x; |
| 143 | + var w1 = bounds.width + bounds.x; |
| 144 | + |
| 145 | + var h0 = bounds.y; |
| 146 | + var h1 = bounds.height + bounds.y; |
| 147 | + |
| 148 | + var worldTransform = this.worldTransform; |
| 149 | + |
| 150 | + var a = worldTransform.a; |
| 151 | + var b = worldTransform.b; |
| 152 | + var c = worldTransform.c; |
| 153 | + var d = worldTransform.d; |
| 154 | + var tx = worldTransform.tx; |
| 155 | + var ty = worldTransform.ty; |
| 156 | + |
| 157 | + var x1 = a * w1 + c * h1 + tx; |
| 158 | + var y1 = d * h1 + b * w1 + ty; |
| 159 | + |
| 160 | + var x2 = a * w0 + c * h1 + tx; |
| 161 | + var y2 = d * h1 + b * w0 + ty; |
| 162 | + |
| 163 | + var x3 = a * w0 + c * h0 + tx; |
| 164 | + var y3 = d * h0 + b * w0 + ty; |
| 165 | + |
| 166 | + var x4 = a * w1 + c * h0 + tx; |
| 167 | + var y4 = d * h0 + b * w1 + ty; |
| 168 | + |
| 169 | + maxX = x1; |
| 170 | + maxY = y1; |
| 171 | + |
| 172 | + minX = x1; |
| 173 | + minY = y1; |
| 174 | + |
| 175 | + minX = x2 < minX ? x2 : minX; |
| 176 | + minX = x3 < minX ? x3 : minX; |
| 177 | + minX = x4 < minX ? x4 : minX; |
| 178 | + |
| 179 | + minY = y2 < minY ? y2 : minY; |
| 180 | + minY = y3 < minY ? y3 : minY; |
| 181 | + minY = y4 < minY ? y4 : minY; |
| 182 | + |
| 183 | + maxX = x2 > maxX ? x2 : maxX; |
| 184 | + maxX = x3 > maxX ? x3 : maxX; |
| 185 | + maxX = x4 > maxX ? x4 : maxX; |
| 186 | + |
| 187 | + maxY = y2 > maxY ? y2 : maxY; |
| 188 | + maxY = y3 > maxY ? y3 : maxY; |
| 189 | + maxY = y4 > maxY ? y4 : maxY; |
| 190 | + } |
| 191 | + |
| 192 | + bounds.x = minX; |
| 193 | + bounds.y = minY; |
| 194 | + bounds.width = maxX - minX; |
| 195 | + bounds.height = maxY - minY; |
| 196 | + |
| 197 | + if (isTargetCoordinateSpaceDisplayObject) |
| 198 | + { |
| 199 | + targetCoordinateSpace.worldTransform = matrixCache; |
| 200 | + |
| 201 | + for (i = 0; i < targetCoordinateSpace.children.length; i++) |
| 202 | + { |
| 203 | + targetCoordinateSpace.children[i].updateTransform(); |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + if (!isTargetCoordinateSpaceThisOrParent) |
| 208 | + { |
| 209 | + var targetCoordinateSpaceBounds = targetCoordinateSpace.getBounds(); |
| 210 | + |
| 211 | + bounds.x -= targetCoordinateSpaceBounds.x; |
| 212 | + bounds.y -= targetCoordinateSpaceBounds.y; |
| 213 | + } |
| 214 | + |
| 215 | + return bounds; |
| 216 | + |
| 217 | +}; |
| 218 | + |
| 219 | +/** |
| 220 | + * Retrieves the non-global local bounds of the displayObjectContainer as a rectangle without any transformations. The calculation takes all visible children into consideration. |
| 221 | + * |
| 222 | + * @method getLocalBounds |
| 223 | + * @return {Rectangle} The rectangular bounding area |
| 224 | + */ |
| 225 | +PIXI.DisplayObjectContainer.prototype.getLocalBounds = function () { |
| 226 | + |
| 227 | + return this.getBounds(this); |
| 228 | + |
| 229 | +}; |
| 230 | + |
| 231 | +/** |
| 232 | + * The width of the displayObjectContainer, setting this will actually modify the scale to achieve the value set |
| 233 | + * |
| 234 | + * @property width |
| 235 | + * @type Number |
| 236 | + */ |
| 237 | +Object.defineProperty(PIXI.DisplayObjectContainer.prototype, 'width', { |
| 238 | + |
| 239 | + get: function() { |
| 240 | + return this.getLocalBounds().width * this.scale.x; |
| 241 | + }, |
| 242 | + |
| 243 | + set: function(value) { |
| 244 | + |
| 245 | + var width = this.getLocalBounds().width; |
| 246 | + |
| 247 | + if (width !== 0) |
| 248 | + { |
| 249 | + this.scale.x = value / width; |
| 250 | + } |
| 251 | + else |
| 252 | + { |
| 253 | + this.scale.x = 1; |
| 254 | + } |
| 255 | + |
| 256 | + this._width = value; |
| 257 | + } |
| 258 | +}); |
| 259 | + |
| 260 | +/** |
| 261 | + * The height of the displayObjectContainer, setting this will actually modify the scale to achieve the value set |
| 262 | + * |
| 263 | + * @property height |
| 264 | + * @type Number |
| 265 | + */ |
| 266 | +Object.defineProperty(PIXI.DisplayObjectContainer.prototype, 'height', { |
| 267 | + |
| 268 | + get: function() { |
| 269 | + return this.getLocalBounds().height * this.scale.y; |
| 270 | + }, |
| 271 | + |
| 272 | + set: function(value) { |
| 273 | + |
| 274 | + var height = this.getLocalBounds().height; |
| 275 | + |
| 276 | + if (height !== 0) |
| 277 | + { |
| 278 | + this.scale.y = value / height; |
| 279 | + } |
| 280 | + else |
| 281 | + { |
| 282 | + this.scale.y = 1; |
| 283 | + } |
| 284 | + |
| 285 | + this._height = value; |
| 286 | + } |
| 287 | + |
| 288 | +}); |
| 289 | + |
0 commit comments