PageRenderTime 75ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 3ms

/build/phaser.js

https://gitlab.com/lobsterhands/phaser
JavaScript | 17639 lines | 8119 code | 3354 blank | 6166 comment | 1201 complexity | 3d0643572fac43e8b7e174e36a9f2ebf MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. /**
  2. * @author Richard Davey <rich@photonstorm.com>
  3. * @copyright 2014 Photon Storm Ltd.
  4. * @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
  5. *
  6. * @overview
  7. *
  8. * Phaser - http://phaser.io
  9. *
  10. * v2.0.3 "Allorallen" - Built: Fri Apr 11 2014 13:08:30
  11. *
  12. * By Richard Davey http://www.photonstorm.com @photonstorm
  13. *
  14. * Phaser is a fun, free and fast 2D game framework for making HTML5 games
  15. * for desktop and mobile web browsers, supporting Canvas and WebGL rendering.
  16. *
  17. * Phaser uses Pixi.js for rendering, created by Mat Groves http://matgroves.com @Doormat23
  18. * Phaser uses p2.js for full-body physics, created by Stefan Hedman https://github.com/schteppe/p2.js @schteppe
  19. * Phaser contains a port of N+ Physics, converted by Richard Davey, original by http://www.metanetsoftware.com
  20. *
  21. * Many thanks to Adam Saltsman (@ADAMATOMIC) for releasing Flixel, from which both Phaser
  22. * and my love of framework development originate.
  23. *
  24. * Follow development at http://phaser.io and on our forum
  25. *
  26. * "If you want your children to be intelligent, read them fairy tales."
  27. * "If you want them to be more intelligent, read them more fairy tales."
  28. * -- Albert Einstein
  29. */
  30. /**
  31. * @author Mat Groves http://matgroves.com/ @Doormat23
  32. */
  33. (function(){
  34. var root = this;
  35. /**
  36. * @author Mat Groves http://matgroves.com/ @Doormat23
  37. */
  38. /**
  39. * @module PIXI
  40. */
  41. var PIXI = PIXI || {};
  42. /*
  43. *
  44. * This file contains a lot of pixi consts which are used across the rendering engine
  45. * @class Consts
  46. */
  47. PIXI.WEBGL_RENDERER = 0;
  48. PIXI.CANVAS_RENDERER = 1;
  49. // useful for testing against if your lib is using pixi.
  50. PIXI.VERSION = "v1.5.2";
  51. // the various blend modes supported by pixi
  52. PIXI.blendModes = {
  53. NORMAL:0,
  54. ADD:1,
  55. MULTIPLY:2,
  56. SCREEN:3,
  57. OVERLAY:4,
  58. DARKEN:5,
  59. LIGHTEN:6,
  60. COLOR_DODGE:7,
  61. COLOR_BURN:8,
  62. HARD_LIGHT:9,
  63. SOFT_LIGHT:10,
  64. DIFFERENCE:11,
  65. EXCLUSION:12,
  66. HUE:13,
  67. SATURATION:14,
  68. COLOR:15,
  69. LUMINOSITY:16
  70. };
  71. // the scale modes
  72. PIXI.scaleModes = {
  73. DEFAULT:0,
  74. LINEAR:0,
  75. NEAREST:1
  76. };
  77. // interaction frequency
  78. PIXI.INTERACTION_FREQUENCY = 30;
  79. PIXI.AUTO_PREVENT_DEFAULT = true;
  80. PIXI.RAD_TO_DEG = 180 / Math.PI;
  81. PIXI.DEG_TO_RAD = Math.PI / 180;
  82. /**
  83. * @author Mat Groves http://matgroves.com/ @Doormat23
  84. */
  85. /**
  86. * The Point object represents a location in a two-dimensional coordinate system, where x represents the horizontal axis and y represents the vertical axis.
  87. *
  88. * @class Point
  89. * @constructor
  90. * @param x {Number} position of the point on the x axis
  91. * @param y {Number} position of the point on the y axis
  92. */
  93. PIXI.Point = function(x, y)
  94. {
  95. /**
  96. * @property x
  97. * @type Number
  98. * @default 0
  99. */
  100. this.x = x || 0;
  101. /**
  102. * @property y
  103. * @type Number
  104. * @default 0
  105. */
  106. this.y = y || 0;
  107. };
  108. /**
  109. * Creates a clone of this point
  110. *
  111. * @method clone
  112. * @return {Point} a copy of the point
  113. */
  114. PIXI.Point.prototype.clone = function()
  115. {
  116. return new PIXI.Point(this.x, this.y);
  117. };
  118. // constructor
  119. PIXI.Point.prototype.constructor = PIXI.Point;
  120. PIXI.Point.prototype.set = function(x, y)
  121. {
  122. this.x = x || 0;
  123. this.y = y || ( (y !== 0) ? this.x : 0 ) ;
  124. };
  125. /**
  126. * @author Mat Groves http://matgroves.com/
  127. */
  128. /**
  129. * the Rectangle object is an area defined by its position, as indicated by its top-left corner point (x, y) and by its width and its height.
  130. *
  131. * @class Rectangle
  132. * @constructor
  133. * @param x {Number} The X coord of the upper-left corner of the rectangle
  134. * @param y {Number} The Y coord of the upper-left corner of the rectangle
  135. * @param width {Number} The overall width of this rectangle
  136. * @param height {Number} The overall height of this rectangle
  137. */
  138. PIXI.Rectangle = function(x, y, width, height)
  139. {
  140. /**
  141. * @property x
  142. * @type Number
  143. * @default 0
  144. */
  145. this.x = x || 0;
  146. /**
  147. * @property y
  148. * @type Number
  149. * @default 0
  150. */
  151. this.y = y || 0;
  152. /**
  153. * @property width
  154. * @type Number
  155. * @default 0
  156. */
  157. this.width = width || 0;
  158. /**
  159. * @property height
  160. * @type Number
  161. * @default 0
  162. */
  163. this.height = height || 0;
  164. };
  165. /**
  166. * Creates a clone of this Rectangle
  167. *
  168. * @method clone
  169. * @return {Rectangle} a copy of the rectangle
  170. */
  171. PIXI.Rectangle.prototype.clone = function()
  172. {
  173. return new PIXI.Rectangle(this.x, this.y, this.width, this.height);
  174. };
  175. /**
  176. * Checks whether the x and y coordinates passed to this function are contained within this Rectangle
  177. *
  178. * @method contains
  179. * @param x {Number} The X coordinate of the point to test
  180. * @param y {Number} The Y coordinate of the point to test
  181. * @return {Boolean} Whether the x/y coords are within this Rectangle
  182. */
  183. PIXI.Rectangle.prototype.contains = function(x, y)
  184. {
  185. if(this.width <= 0 || this.height <= 0)
  186. return false;
  187. var x1 = this.x;
  188. if(x >= x1 && x <= x1 + this.width)
  189. {
  190. var y1 = this.y;
  191. if(y >= y1 && y <= y1 + this.height)
  192. {
  193. return true;
  194. }
  195. }
  196. return false;
  197. };
  198. // constructor
  199. PIXI.Rectangle.prototype.constructor = PIXI.Rectangle;
  200. PIXI.EmptyRectangle = new PIXI.Rectangle(0,0,0,0);
  201. /**
  202. * @author Adrien Brault <adrien.brault@gmail.com>
  203. */
  204. /**
  205. * @class Polygon
  206. * @constructor
  207. * @param points* {Array<Point>|Array<Number>|Point...|Number...} This can be an array of Points that form the polygon,
  208. * a flat array of numbers that will be interpreted as [x,y, x,y, ...], or the arguments passed can be
  209. * all the points of the polygon e.g. `new PIXI.Polygon(new PIXI.Point(), new PIXI.Point(), ...)`, or the
  210. * arguments passed can be flat x,y values e.g. `new PIXI.Polygon(x,y, x,y, x,y, ...)` where `x` and `y` are
  211. * Numbers.
  212. */
  213. PIXI.Polygon = function(points)
  214. {
  215. //if points isn't an array, use arguments as the array
  216. if(!(points instanceof Array))
  217. points = Array.prototype.slice.call(arguments);
  218. //if this is a flat array of numbers, convert it to points
  219. if(typeof points[0] === 'number') {
  220. var p = [];
  221. for(var i = 0, il = points.length; i < il; i+=2) {
  222. p.push(
  223. new PIXI.Point(points[i], points[i + 1])
  224. );
  225. }
  226. points = p;
  227. }
  228. this.points = points;
  229. };
  230. /**
  231. * Creates a clone of this polygon
  232. *
  233. * @method clone
  234. * @return {Polygon} a copy of the polygon
  235. */
  236. PIXI.Polygon.prototype.clone = function()
  237. {
  238. var points = [];
  239. for (var i=0; i<this.points.length; i++) {
  240. points.push(this.points[i].clone());
  241. }
  242. return new PIXI.Polygon(points);
  243. };
  244. /**
  245. * Checks whether the x and y coordinates passed to this function are contained within this polygon
  246. *
  247. * @method contains
  248. * @param x {Number} The X coordinate of the point to test
  249. * @param y {Number} The Y coordinate of the point to test
  250. * @return {Boolean} Whether the x/y coordinates are within this polygon
  251. */
  252. PIXI.Polygon.prototype.contains = function(x, y)
  253. {
  254. var inside = false;
  255. // use some raycasting to test hits
  256. // https://github.com/substack/point-in-polygon/blob/master/index.js
  257. for(var i = 0, j = this.points.length - 1; i < this.points.length; j = i++) {
  258. var xi = this.points[i].x, yi = this.points[i].y,
  259. xj = this.points[j].x, yj = this.points[j].y,
  260. intersect = ((yi > y) !== (yj > y)) && (x < (xj - xi) * (y - yi) / (yj - yi) + xi);
  261. if(intersect) inside = !inside;
  262. }
  263. return inside;
  264. };
  265. // constructor
  266. PIXI.Polygon.prototype.constructor = PIXI.Polygon;
  267. /**
  268. * @author Chad Engler <chad@pantherdev.com>
  269. */
  270. /**
  271. * The Circle object can be used to specify a hit area for displayObjects
  272. *
  273. * @class Circle
  274. * @constructor
  275. * @param x {Number} The X coordinate of the upper-left corner of the framing rectangle of this circle
  276. * @param y {Number} The Y coordinate of the upper-left corner of the framing rectangle of this circle
  277. * @param radius {Number} The radius of the circle
  278. */
  279. PIXI.Circle = function(x, y, radius)
  280. {
  281. /**
  282. * @property x
  283. * @type Number
  284. * @default 0
  285. */
  286. this.x = x || 0;
  287. /**
  288. * @property y
  289. * @type Number
  290. * @default 0
  291. */
  292. this.y = y || 0;
  293. /**
  294. * @property radius
  295. * @type Number
  296. * @default 0
  297. */
  298. this.radius = radius || 0;
  299. };
  300. /**
  301. * Creates a clone of this Circle instance
  302. *
  303. * @method clone
  304. * @return {Circle} a copy of the polygon
  305. */
  306. PIXI.Circle.prototype.clone = function()
  307. {
  308. return new PIXI.Circle(this.x, this.y, this.radius);
  309. };
  310. /**
  311. * Checks whether the x, and y coordinates passed to this function are contained within this circle
  312. *
  313. * @method contains
  314. * @param x {Number} The X coordinate of the point to test
  315. * @param y {Number} The Y coordinate of the point to test
  316. * @return {Boolean} Whether the x/y coordinates are within this polygon
  317. */
  318. PIXI.Circle.prototype.contains = function(x, y)
  319. {
  320. if(this.radius <= 0)
  321. return false;
  322. var dx = (this.x - x),
  323. dy = (this.y - y),
  324. r2 = this.radius * this.radius;
  325. dx *= dx;
  326. dy *= dy;
  327. return (dx + dy <= r2);
  328. };
  329. // constructor
  330. PIXI.Circle.prototype.constructor = PIXI.Circle;
  331. /**
  332. * @author Chad Engler <chad@pantherdev.com>
  333. */
  334. /**
  335. * The Ellipse object can be used to specify a hit area for displayObjects
  336. *
  337. * @class Ellipse
  338. * @constructor
  339. * @param x {Number} The X coordinate of the upper-left corner of the framing rectangle of this ellipse
  340. * @param y {Number} The Y coordinate of the upper-left corner of the framing rectangle of this ellipse
  341. * @param width {Number} The overall width of this ellipse
  342. * @param height {Number} The overall height of this ellipse
  343. */
  344. PIXI.Ellipse = function(x, y, width, height)
  345. {
  346. /**
  347. * @property x
  348. * @type Number
  349. * @default 0
  350. */
  351. this.x = x || 0;
  352. /**
  353. * @property y
  354. * @type Number
  355. * @default 0
  356. */
  357. this.y = y || 0;
  358. /**
  359. * @property width
  360. * @type Number
  361. * @default 0
  362. */
  363. this.width = width || 0;
  364. /**
  365. * @property height
  366. * @type Number
  367. * @default 0
  368. */
  369. this.height = height || 0;
  370. };
  371. /**
  372. * Creates a clone of this Ellipse instance
  373. *
  374. * @method clone
  375. * @return {Ellipse} a copy of the ellipse
  376. */
  377. PIXI.Ellipse.prototype.clone = function()
  378. {
  379. return new PIXI.Ellipse(this.x, this.y, this.width, this.height);
  380. };
  381. /**
  382. * Checks whether the x and y coordinates passed to this function are contained within this ellipse
  383. *
  384. * @method contains
  385. * @param x {Number} The X coordinate of the point to test
  386. * @param y {Number} The Y coordinate of the point to test
  387. * @return {Boolean} Whether the x/y coords are within this ellipse
  388. */
  389. PIXI.Ellipse.prototype.contains = function(x, y)
  390. {
  391. if(this.width <= 0 || this.height <= 0)
  392. return false;
  393. //normalize the coords to an ellipse with center 0,0
  394. var normx = ((x - this.x) / this.width),
  395. normy = ((y - this.y) / this.height);
  396. normx *= normx;
  397. normy *= normy;
  398. return (normx + normy <= 1);
  399. };
  400. /**
  401. * Returns the framing rectangle of the ellipse as a PIXI.Rectangle object
  402. *
  403. * @method getBounds
  404. * @return {Rectangle} the framing rectangle
  405. */
  406. PIXI.Ellipse.prototype.getBounds = function()
  407. {
  408. return new PIXI.Rectangle(this.x, this.y, this.width, this.height);
  409. };
  410. // constructor
  411. PIXI.Ellipse.prototype.constructor = PIXI.Ellipse;
  412. /**
  413. * @author Mat Groves http://matgroves.com/ @Doormat23
  414. */
  415. PIXI.determineMatrixArrayType = function() {
  416. return (typeof Float32Array !== 'undefined') ? Float32Array : Array;
  417. };
  418. /*
  419. * @class Matrix2
  420. * The Matrix2 class will choose the best type of array to use between
  421. * a regular javascript Array and a Float32Array if the latter is available
  422. *
  423. */
  424. PIXI.Matrix2 = PIXI.determineMatrixArrayType();
  425. /*
  426. * @class Matrix
  427. * The Matrix class is now an object, which makes it a lot faster,
  428. * here is a representation of it :
  429. * | a | b | tx|
  430. * | c | c | ty|
  431. * | 0 | 0 | 1 |
  432. *
  433. */
  434. PIXI.Matrix = function()
  435. {
  436. this.a = 1;
  437. this.b = 0;
  438. this.c = 0;
  439. this.d = 1;
  440. this.tx = 0;
  441. this.ty = 0;
  442. };
  443. /**
  444. * Creates a pixi matrix object based on the array given as a parameter
  445. *
  446. * @method fromArray
  447. * @param array {Array} The array that the matrix will be filled with
  448. */
  449. PIXI.Matrix.prototype.fromArray = function(array)
  450. {
  451. this.a = array[0];
  452. this.b = array[1];
  453. this.c = array[3];
  454. this.d = array[4];
  455. this.tx = array[2];
  456. this.ty = array[5];
  457. };
  458. /**
  459. * Creates an array from the current Matrix object
  460. *
  461. * @method toArray
  462. * @param transpose {Boolean} Whether we need to transpose the matrix or not
  463. * @return array {Array} the newly created array which contains the matrix
  464. */
  465. PIXI.Matrix.prototype.toArray = function(transpose)
  466. {
  467. if(!this.array) this.array = new Float32Array(9);
  468. var array = this.array;
  469. if(transpose)
  470. {
  471. this.array[0] = this.a;
  472. this.array[1] = this.c;
  473. this.array[2] = 0;
  474. this.array[3] = this.b;
  475. this.array[4] = this.d;
  476. this.array[5] = 0;
  477. this.array[6] = this.tx;
  478. this.array[7] = this.ty;
  479. this.array[8] = 1;
  480. }
  481. else
  482. {
  483. this.array[0] = this.a;
  484. this.array[1] = this.b;
  485. this.array[2] = this.tx;
  486. this.array[3] = this.c;
  487. this.array[4] = this.d;
  488. this.array[5] = this.ty;
  489. this.array[6] = 0;
  490. this.array[7] = 0;
  491. this.array[8] = 1;
  492. }
  493. return array;//[this.a, this.b, this.tx, this.c, this.d, this.ty, 0, 0, 1];
  494. };
  495. PIXI.identityMatrix = new PIXI.Matrix();
  496. /**
  497. * @author Mat Groves http://matgroves.com/ @Doormat23
  498. */
  499. /**
  500. * The base class for all objects that are rendered on the screen.
  501. *
  502. * @class DisplayObject
  503. * @constructor
  504. */
  505. PIXI.DisplayObject = function()
  506. {
  507. /**
  508. * The coordinate of the object relative to the local coordinates of the parent.
  509. *
  510. * @property position
  511. * @type Point
  512. */
  513. this.position = new PIXI.Point();
  514. /**
  515. * The scale factor of the object.
  516. *
  517. * @property scale
  518. * @type Point
  519. */
  520. this.scale = new PIXI.Point(1,1);//{x:1, y:1};
  521. /**
  522. * The pivot point of the displayObject that it rotates around
  523. *
  524. * @property pivot
  525. * @type Point
  526. */
  527. this.pivot = new PIXI.Point(0,0);
  528. /**
  529. * The rotation of the object in radians.
  530. *
  531. * @property rotation
  532. * @type Number
  533. */
  534. this.rotation = 0;
  535. /**
  536. * The opacity of the object.
  537. *
  538. * @property alpha
  539. * @type Number
  540. */
  541. this.alpha = 1;
  542. /**
  543. * The visibility of the object.
  544. *
  545. * @property visible
  546. * @type Boolean
  547. */
  548. this.visible = true;
  549. /**
  550. * This is the defined area that will pick up mouse / touch events. It is null by default.
  551. * Setting it is a neat way of optimising the hitTest function that the interactionManager will use (as it will not need to hit test all the children)
  552. *
  553. * @property hitArea
  554. * @type Rectangle|Circle|Ellipse|Polygon
  555. */
  556. this.hitArea = null;
  557. /**
  558. * This is used to indicate if the displayObject should display a mouse hand cursor on rollover
  559. *
  560. * @property buttonMode
  561. * @type Boolean
  562. */
  563. this.buttonMode = false;
  564. /**
  565. * Can this object be rendered
  566. *
  567. * @property renderable
  568. * @type Boolean
  569. */
  570. this.renderable = false;
  571. /**
  572. * [read-only] The display object container that contains this display object.
  573. *
  574. * @property parent
  575. * @type DisplayObjectContainer
  576. * @readOnly
  577. */
  578. this.parent = null;
  579. /**
  580. * [read-only] The stage the display object is connected to, or undefined if it is not connected to the stage.
  581. *
  582. * @property stage
  583. * @type Stage
  584. * @readOnly
  585. */
  586. this.stage = null;
  587. /**
  588. * [read-only] The multiplied alpha of the displayObject
  589. *
  590. * @property worldAlpha
  591. * @type Number
  592. * @readOnly
  593. */
  594. this.worldAlpha = 1;
  595. /**
  596. * [read-only] Whether or not the object is interactive, do not toggle directly! use the `interactive` property
  597. *
  598. * @property _interactive
  599. * @type Boolean
  600. * @readOnly
  601. * @private
  602. */
  603. this._interactive = false;
  604. /**
  605. * This is the cursor that will be used when the mouse is over this object. To enable this the element must have interaction = true and buttonMode = true
  606. *
  607. * @property defaultCursor
  608. * @type String
  609. *
  610. */
  611. this.defaultCursor = 'pointer';
  612. /**
  613. * [read-only] Current transform of the object based on world (parent) factors
  614. *
  615. * @property worldTransform
  616. * @type Mat3
  617. * @readOnly
  618. * @private
  619. */
  620. this.worldTransform = new PIXI.Matrix();
  621. /**
  622. * [NYI] Unknown
  623. *
  624. * @property color
  625. * @type Array<>
  626. * @private
  627. */
  628. this.color = [];
  629. /**
  630. * [NYI] Holds whether or not this object is dynamic, for rendering optimization
  631. *
  632. * @property dynamic
  633. * @type Boolean
  634. * @private
  635. */
  636. this.dynamic = true;
  637. // cached sin rotation and cos rotation
  638. this._sr = 0;
  639. this._cr = 1;
  640. /**
  641. * The area the filter is applied to like the hitArea this is used as more of an optimisation
  642. * rather than figuring out the dimensions of the displayObject each frame you can set this rectangle
  643. *
  644. * @property filterArea
  645. * @type Rectangle
  646. */
  647. this.filterArea = null;//new PIXI.Rectangle(0,0,1,1);
  648. /**
  649. * The original, cached bounds of the object
  650. *
  651. * @property _bounds
  652. * @type Rectangle
  653. * @private
  654. */
  655. this._bounds = new PIXI.Rectangle(0, 0, 1, 1);
  656. /**
  657. * The most up-to-date bounds of the object
  658. *
  659. * @property _currentBounds
  660. * @type Rectangle
  661. * @private
  662. */
  663. this._currentBounds = null;
  664. /**
  665. * The original, cached mask of the object
  666. *
  667. * @property _currentBounds
  668. * @type Rectangle
  669. * @private
  670. */
  671. this._mask = null;
  672. this._cacheAsBitmap = false;
  673. this._cacheIsDirty = false;
  674. /*
  675. * MOUSE Callbacks
  676. */
  677. /**
  678. * A callback that is used when the users clicks on the displayObject with their mouse
  679. * @method click
  680. * @param interactionData {InteractionData}
  681. */
  682. /**
  683. * A callback that is used when the user clicks the mouse down over the sprite
  684. * @method mousedown
  685. * @param interactionData {InteractionData}
  686. */
  687. /**
  688. * A callback that is used when the user releases the mouse that was over the displayObject
  689. * for this callback to be fired the mouse must have been pressed down over the displayObject
  690. * @method mouseup
  691. * @param interactionData {InteractionData}
  692. */
  693. /**
  694. * A callback that is used when the user releases the mouse that was over the displayObject but is no longer over the displayObject
  695. * for this callback to be fired, The touch must have started over the displayObject
  696. * @method mouseupoutside
  697. * @param interactionData {InteractionData}
  698. */
  699. /**
  700. * A callback that is used when the users mouse rolls over the displayObject
  701. * @method mouseover
  702. * @param interactionData {InteractionData}
  703. */
  704. /**
  705. * A callback that is used when the users mouse leaves the displayObject
  706. * @method mouseout
  707. * @param interactionData {InteractionData}
  708. */
  709. /*
  710. * TOUCH Callbacks
  711. */
  712. /**
  713. * A callback that is used when the users taps on the sprite with their finger
  714. * basically a touch version of click
  715. * @method tap
  716. * @param interactionData {InteractionData}
  717. */
  718. /**
  719. * A callback that is used when the user touches over the displayObject
  720. * @method touchstart
  721. * @param interactionData {InteractionData}
  722. */
  723. /**
  724. * A callback that is used when the user releases a touch over the displayObject
  725. * @method touchend
  726. * @param interactionData {InteractionData}
  727. */
  728. /**
  729. * A callback that is used when the user releases the touch that was over the displayObject
  730. * for this callback to be fired, The touch must have started over the sprite
  731. * @method touchendoutside
  732. * @param interactionData {InteractionData}
  733. */
  734. };
  735. // constructor
  736. PIXI.DisplayObject.prototype.constructor = PIXI.DisplayObject;
  737. /**
  738. * [Deprecated] Indicates if the sprite will have touch and mouse interactivity. It is false by default
  739. * Instead of using this function you can now simply set the interactive property to true or false
  740. *
  741. * @method setInteractive
  742. * @param interactive {Boolean}
  743. * @deprecated Simply set the `interactive` property directly
  744. */
  745. PIXI.DisplayObject.prototype.setInteractive = function(interactive)
  746. {
  747. this.interactive = interactive;
  748. };
  749. /**
  750. * Indicates if the sprite will have touch and mouse interactivity. It is false by default
  751. *
  752. * @property interactive
  753. * @type Boolean
  754. * @default false
  755. */
  756. Object.defineProperty(PIXI.DisplayObject.prototype, 'interactive', {
  757. get: function() {
  758. return this._interactive;
  759. },
  760. set: function(value) {
  761. this._interactive = value;
  762. // TODO more to be done here..
  763. // need to sort out a re-crawl!
  764. if(this.stage)this.stage.dirty = true;
  765. }
  766. });
  767. /**
  768. * [read-only] Indicates if the sprite is globaly visible.
  769. *
  770. * @property worldVisible
  771. * @type Boolean
  772. */
  773. Object.defineProperty(PIXI.DisplayObject.prototype, 'worldVisible', {
  774. get: function() {
  775. var item = this;
  776. do
  777. {
  778. if(!item.visible)return false;
  779. item = item.parent;
  780. }
  781. while(item);
  782. return true;
  783. }
  784. });
  785. /**
  786. * Sets a mask for the displayObject. A mask is an object that limits the visibility of an object to the shape of the mask applied to it.
  787. * In PIXI a regular mask must be a PIXI.Graphics object. This allows for much faster masking in canvas as it utilises shape clipping.
  788. * To remove a mask, set this property to null.
  789. *
  790. * @property mask
  791. * @type Graphics
  792. */
  793. Object.defineProperty(PIXI.DisplayObject.prototype, 'mask', {
  794. get: function() {
  795. return this._mask;
  796. },
  797. set: function(value) {
  798. if(this._mask)this._mask.isMask = false;
  799. this._mask = value;
  800. if(this._mask)this._mask.isMask = true;
  801. }
  802. });
  803. /**
  804. * Sets the filters for the displayObject.
  805. * * IMPORTANT: This is a webGL only feature and will be ignored by the canvas renderer.
  806. * To remove filters simply set this property to 'null'
  807. * @property filters
  808. * @type Array An array of filters
  809. */
  810. Object.defineProperty(PIXI.DisplayObject.prototype, 'filters', {
  811. get: function() {
  812. return this._filters;
  813. },
  814. set: function(value) {
  815. if(value)
  816. {
  817. // now put all the passes in one place..
  818. var passes = [];
  819. for (var i = 0; i < value.length; i++)
  820. {
  821. var filterPasses = value[i].passes;
  822. for (var j = 0; j < filterPasses.length; j++)
  823. {
  824. passes.push(filterPasses[j]);
  825. }
  826. }
  827. // TODO change this as it is legacy
  828. this._filterBlock = {target:this, filterPasses:passes};
  829. }
  830. this._filters = value;
  831. }
  832. });
  833. /**
  834. * Set weather or not a the display objects is cached as a bitmap.
  835. * This basically takes a snap shot of the display object as it is at that moment. It can provide a performance benefit for complex static displayObjects
  836. * To remove filters simply set this property to 'null'
  837. * @property cacheAsBitmap
  838. * @type Boolean
  839. */
  840. Object.defineProperty(PIXI.DisplayObject.prototype, 'cacheAsBitmap', {
  841. get: function() {
  842. return this._cacheAsBitmap;
  843. },
  844. set: function(value) {
  845. if(this._cacheAsBitmap === value)return;
  846. if(value)
  847. {
  848. //this._cacheIsDirty = true;
  849. this._generateCachedSprite();
  850. }
  851. else
  852. {
  853. this._destroyCachedSprite();
  854. }
  855. this._cacheAsBitmap = value;
  856. }
  857. });
  858. /*
  859. * Updates the object transform for rendering
  860. *
  861. * @method updateTransform
  862. * @private
  863. */
  864. PIXI.DisplayObject.prototype.updateTransform = function()
  865. {
  866. // TODO OPTIMIZE THIS!! with dirty
  867. if(this.rotation !== this.rotationCache)
  868. {
  869. this.rotationCache = this.rotation;
  870. this._sr = Math.sin(this.rotation);
  871. this._cr = Math.cos(this.rotation);
  872. }
  873. // var localTransform = this.localTransform//.toArray();
  874. var parentTransform = this.parent.worldTransform;//.toArray();
  875. var worldTransform = this.worldTransform;//.toArray();
  876. var px = this.pivot.x;
  877. var py = this.pivot.y;
  878. var a00 = this._cr * this.scale.x,
  879. a01 = -this._sr * this.scale.y,
  880. a10 = this._sr * this.scale.x,
  881. a11 = this._cr * this.scale.y,
  882. a02 = this.position.x - a00 * px - py * a01,
  883. a12 = this.position.y - a11 * py - px * a10,
  884. b00 = parentTransform.a, b01 = parentTransform.b,
  885. b10 = parentTransform.c, b11 = parentTransform.d;
  886. worldTransform.a = b00 * a00 + b01 * a10;
  887. worldTransform.b = b00 * a01 + b01 * a11;
  888. worldTransform.tx = b00 * a02 + b01 * a12 + parentTransform.tx;
  889. worldTransform.c = b10 * a00 + b11 * a10;
  890. worldTransform.d = b10 * a01 + b11 * a11;
  891. worldTransform.ty = b10 * a02 + b11 * a12 + parentTransform.ty;
  892. this.worldAlpha = this.alpha * this.parent.worldAlpha;
  893. };
  894. /**
  895. * Retrieves the bounds of the displayObject as a rectangle object
  896. *
  897. * @method getBounds
  898. * @return {Rectangle} the rectangular bounding area
  899. */
  900. PIXI.DisplayObject.prototype.getBounds = function( matrix )
  901. {
  902. matrix = matrix;//just to get passed js hinting (and preserve inheritance)
  903. return PIXI.EmptyRectangle;
  904. };
  905. /**
  906. * Retrieves the local bounds of the displayObject as a rectangle object
  907. *
  908. * @method getLocalBounds
  909. * @return {Rectangle} the rectangular bounding area
  910. */
  911. PIXI.DisplayObject.prototype.getLocalBounds = function()
  912. {
  913. return this.getBounds(PIXI.identityMatrix);///PIXI.EmptyRectangle();
  914. };
  915. /**
  916. * Sets the object's stage reference, the stage this object is connected to
  917. *
  918. * @method setStageReference
  919. * @param stage {Stage} the stage that the object will have as its current stage reference
  920. */
  921. PIXI.DisplayObject.prototype.setStageReference = function(stage)
  922. {
  923. this.stage = stage;
  924. if(this._interactive)this.stage.dirty = true;
  925. };
  926. PIXI.DisplayObject.prototype.generateTexture = function(renderer)
  927. {
  928. var bounds = this.getLocalBounds();
  929. var renderTexture = new PIXI.RenderTexture(bounds.width | 0, bounds.height | 0, renderer);
  930. renderTexture.render(this);
  931. return renderTexture;
  932. };
  933. PIXI.DisplayObject.prototype.updateCache = function()
  934. {
  935. this._generateCachedSprite();
  936. };
  937. PIXI.DisplayObject.prototype._renderCachedSprite = function(renderSession)
  938. {
  939. if(renderSession.gl)
  940. {
  941. PIXI.Sprite.prototype._renderWebGL.call(this._cachedSprite, renderSession);
  942. }
  943. else
  944. {
  945. PIXI.Sprite.prototype._renderCanvas.call(this._cachedSprite, renderSession);
  946. }
  947. };
  948. PIXI.DisplayObject.prototype._generateCachedSprite = function()//renderSession)
  949. {
  950. this._cacheAsBitmap = false;
  951. var bounds = this.getLocalBounds();
  952. if(!this._cachedSprite)
  953. {
  954. var renderTexture = new PIXI.RenderTexture(bounds.width | 0, bounds.height | 0);//, renderSession.renderer);
  955. this._cachedSprite = new PIXI.Sprite(renderTexture);
  956. this._cachedSprite.worldTransform = this.worldTransform;
  957. }
  958. else
  959. {
  960. this._cachedSprite.texture.resize(bounds.width | 0, bounds.height | 0);
  961. }
  962. //REMOVE filter!
  963. var tempFilters = this._filters;
  964. this._filters = null;
  965. this._cachedSprite.filters = tempFilters;
  966. this._cachedSprite.texture.render(this);
  967. this._filters = tempFilters;
  968. this._cacheAsBitmap = true;
  969. };
  970. /**
  971. * Renders the object using the WebGL renderer
  972. *
  973. * @method _renderWebGL
  974. * @param renderSession {RenderSession}
  975. * @private
  976. */
  977. PIXI.DisplayObject.prototype._destroyCachedSprite = function()
  978. {
  979. if(!this._cachedSprite)return;
  980. this._cachedSprite.texture.destroy(true);
  981. // console.log("DESTROY")
  982. // let the gc collect the unused sprite
  983. // TODO could be object pooled!
  984. this._cachedSprite = null;
  985. };
  986. PIXI.DisplayObject.prototype._renderWebGL = function(renderSession)
  987. {
  988. // OVERWRITE;
  989. // this line is just here to pass jshinting :)
  990. renderSession = renderSession;
  991. };
  992. /**
  993. * Renders the object using the Canvas renderer
  994. *
  995. * @method _renderCanvas
  996. * @param renderSession {RenderSession}
  997. * @private
  998. */
  999. PIXI.DisplayObject.prototype._renderCanvas = function(renderSession)
  1000. {
  1001. // OVERWRITE;
  1002. // this line is just here to pass jshinting :)
  1003. renderSession = renderSession;
  1004. };
  1005. /**
  1006. * The position of the displayObject on the x axis relative to the local coordinates of the parent.
  1007. *
  1008. * @property x
  1009. * @type Number
  1010. */
  1011. Object.defineProperty(PIXI.DisplayObject.prototype, 'x', {
  1012. get: function() {
  1013. return this.position.x;
  1014. },
  1015. set: function(value) {
  1016. this.position.x = value;
  1017. }
  1018. });
  1019. /**
  1020. * The position of the displayObject on the y axis relative to the local coordinates of the parent.
  1021. *
  1022. * @property y
  1023. * @type Number
  1024. */
  1025. Object.defineProperty(PIXI.DisplayObject.prototype, 'y', {
  1026. get: function() {
  1027. return this.position.y;
  1028. },
  1029. set: function(value) {
  1030. this.position.y = value;
  1031. }
  1032. });
  1033. /**
  1034. * @author Mat Groves http://matgroves.com/ @Doormat23
  1035. */
  1036. /**
  1037. * A DisplayObjectContainer represents a collection of display objects.
  1038. * It is the base class of all display objects that act as a container for other objects.
  1039. *
  1040. * @class DisplayObjectContainer
  1041. * @extends DisplayObject
  1042. * @constructor
  1043. */
  1044. PIXI.DisplayObjectContainer = function()
  1045. {
  1046. PIXI.DisplayObject.call( this );
  1047. /**
  1048. * [read-only] The array of children of this container.
  1049. *
  1050. * @property children
  1051. * @type Array<DisplayObject>
  1052. * @readOnly
  1053. */
  1054. this.children = [];
  1055. };
  1056. // constructor
  1057. PIXI.DisplayObjectContainer.prototype = Object.create( PIXI.DisplayObject.prototype );
  1058. PIXI.DisplayObjectContainer.prototype.constructor = PIXI.DisplayObjectContainer;
  1059. /**
  1060. * The width of the displayObjectContainer, setting this will actually modify the scale to achieve the value set
  1061. *
  1062. * @property width
  1063. * @type Number
  1064. */
  1065. /*
  1066. Object.defineProperty(PIXI.DisplayObjectContainer.prototype, 'width', {
  1067. get: function() {
  1068. return this.scale.x * this.getLocalBounds().width;
  1069. },
  1070. set: function(value) {
  1071. this.scale.x = value / (this.getLocalBounds().width/this.scale.x);
  1072. this._width = value;
  1073. }
  1074. });
  1075. */
  1076. /**
  1077. * The height of the displayObjectContainer, setting this will actually modify the scale to achieve the value set
  1078. *
  1079. * @property height
  1080. * @type Number
  1081. */
  1082. /*
  1083. Object.defineProperty(PIXI.DisplayObjectContainer.prototype, 'height', {
  1084. get: function() {
  1085. return this.scale.y * this.getLocalBounds().height;
  1086. },
  1087. set: function(value) {
  1088. this.scale.y = value / (this.getLocalBounds().height/this.scale.y);
  1089. this._height = value;
  1090. }
  1091. });
  1092. */
  1093. /**
  1094. * Adds a child to the container.
  1095. *
  1096. * @method addChild
  1097. * @param child {DisplayObject} The DisplayObject to add to the container
  1098. */
  1099. PIXI.DisplayObjectContainer.prototype.addChild = function(child)
  1100. {
  1101. this.addChildAt(child, this.children.length);
  1102. };
  1103. /**
  1104. * Adds a child to the container at a specified index. If the index is out of bounds an error will be thrown
  1105. *
  1106. * @method addChildAt
  1107. * @param child {DisplayObject} The child to add
  1108. * @param index {Number} The index to place the child in
  1109. */
  1110. PIXI.DisplayObjectContainer.prototype.addChildAt = function(child, index)
  1111. {
  1112. if(index >= 0 && index <= this.children.length)
  1113. {
  1114. if(child.parent)
  1115. {
  1116. child.parent.removeChild(child);
  1117. }
  1118. child.parent = this;
  1119. this.children.splice(index, 0, child);
  1120. if(this.stage)child.setStageReference(this.stage);
  1121. }
  1122. else
  1123. {
  1124. throw new Error(child + ' The index '+ index +' supplied is out of bounds ' + this.children.length);
  1125. }
  1126. };
  1127. /**
  1128. * [NYI] Swaps the depth of 2 displayObjects
  1129. *
  1130. * @method swapChildren
  1131. * @param child {DisplayObject}
  1132. * @param child2 {DisplayObject}
  1133. * @private
  1134. */
  1135. PIXI.DisplayObjectContainer.prototype.swapChildren = function(child, child2)
  1136. {
  1137. if(child === child2) {
  1138. return;
  1139. }
  1140. var index1 = this.children.indexOf(child);
  1141. var index2 = this.children.indexOf(child2);
  1142. if(index1 < 0 || index2 < 0) {
  1143. throw new Error('swapChildren: Both the supplied DisplayObjects must be a child of the caller.');
  1144. }
  1145. this.children[index1] = child2;
  1146. this.children[index2] = child;
  1147. };
  1148. /**
  1149. * Returns the child at the specified index
  1150. *
  1151. * @method getChildAt
  1152. * @param index {Number} The index to get the child from
  1153. */
  1154. PIXI.DisplayObjectContainer.prototype.getChildAt = function(index)
  1155. {
  1156. if(index >= 0 && index < this.children.length)
  1157. {
  1158. return this.children[index];
  1159. }
  1160. else
  1161. {
  1162. throw new Error('Supplied index does not exist in the child list, or the supplied DisplayObject must be a child of the caller');
  1163. }
  1164. };
  1165. /**
  1166. * Removes a child from the container.
  1167. *
  1168. * @method removeChild
  1169. * @param child {DisplayObject} The DisplayObject to remove
  1170. */
  1171. PIXI.DisplayObjectContainer.prototype.removeChild = function(child)
  1172. {
  1173. return this.removeChildAt( this.children.indexOf( child ) );
  1174. };
  1175. /**
  1176. * Removes a child from the specified index position in the child list of the container.
  1177. *
  1178. * @method removeChildAt
  1179. * @param index {Number} The index to get the child from
  1180. */
  1181. PIXI.DisplayObjectContainer.prototype.removeChildAt = function(index)
  1182. {
  1183. var child = this.getChildAt( index );
  1184. if(this.stage)
  1185. child.removeStageReference();
  1186. child.parent = undefined;
  1187. this.children.splice( index, 1 );
  1188. return child;
  1189. };
  1190. /**
  1191. * Removes all child instances from the child list of the container.
  1192. *
  1193. * @method removeChildren
  1194. * @param beginIndex {Number} The beginning position. Predefined value is 0.
  1195. * @param endIndex {Number} The ending position. Predefined value is children's array length.
  1196. */
  1197. PIXI.DisplayObjectContainer.prototype.removeChildren = function(beginIndex, endIndex)
  1198. {
  1199. var begin = beginIndex || 0;
  1200. var end = typeof endIndex === 'number' ? endIndex : this.children.length;
  1201. var range = end - begin;
  1202. if (range > 0 && range <= end)
  1203. {
  1204. var removed = this.children.splice(begin, range);
  1205. for (var i = 0; i < removed.length; i++) {
  1206. var child = removed[i];
  1207. if(this.stage)
  1208. child.removeStageReference();
  1209. child.parent = undefined;
  1210. }
  1211. return removed;
  1212. }
  1213. else
  1214. {
  1215. throw new Error( 'Range Error, numeric values are outside the acceptable range' );
  1216. }
  1217. };
  1218. /*
  1219. * Updates the container's childrens transform for rendering
  1220. *
  1221. * @method updateTransform
  1222. * @private
  1223. */
  1224. PIXI.DisplayObjectContainer.prototype.updateTransform = function()
  1225. {
  1226. //this._currentBounds = null;
  1227. if(!this.visible)return;
  1228. PIXI.DisplayObject.prototype.updateTransform.call( this );
  1229. if(this._cacheAsBitmap)return;
  1230. for(var i=0,j=this.children.length; i<j; i++)
  1231. {
  1232. this.children[i].updateTransform();
  1233. }
  1234. };
  1235. /**
  1236. * Retrieves the bounds of the displayObjectContainer as a rectangle object
  1237. *
  1238. * @method getBounds
  1239. * @return {Rectangle} the rectangular bounding area
  1240. */
  1241. PIXI.DisplayObjectContainer.prototype.getBounds = function(matrix)
  1242. {
  1243. if(this.children.length === 0)return PIXI.EmptyRectangle;
  1244. // TODO the bounds have already been calculated this render session so return what we have
  1245. if(matrix)
  1246. {
  1247. var matrixCache = this.worldTransform;
  1248. this.worldTransform = matrix;
  1249. this.updateTransform();
  1250. this.worldTransform = matrixCache;
  1251. }
  1252. var minX = Infinity;
  1253. var minY = Infinity;
  1254. var maxX = -Infinity;
  1255. var maxY = -Infinity;
  1256. var childBounds;
  1257. var childMaxX;
  1258. var childMaxY;
  1259. var childVisible = false;
  1260. for(var i=0,j=this.children.length; i<j; i++)
  1261. {
  1262. var child = this.children[i];
  1263. if(!child.visible)continue;
  1264. childVisible = true;
  1265. childBounds = this.children[i].getBounds( matrix );
  1266. minX = minX < childBounds.x ? minX : childBounds.x;
  1267. minY = minY < childBounds.y ? minY : childBounds.y;
  1268. childMaxX = childBounds.width + childBounds.x;
  1269. childMaxY = childBounds.height + childBounds.y;
  1270. maxX = maxX > childMaxX ? maxX : childMaxX;
  1271. maxY = maxY > childMaxY ? maxY : childMaxY;
  1272. }
  1273. if(!childVisible)
  1274. return PIXI.EmptyRectangle;
  1275. var bounds = this._bounds;
  1276. bounds.x = minX;
  1277. bounds.y = minY;
  1278. bounds.width = maxX - minX;
  1279. bounds.height = maxY - minY;
  1280. // TODO: store a reference so that if this function gets called again in the render cycle we do not have to recalculate
  1281. //this._currentBounds = bounds;
  1282. return bounds;
  1283. };
  1284. PIXI.DisplayObjectContainer.prototype.getLocalBounds = function()
  1285. {
  1286. var matrixCache = this.worldTransform;
  1287. this.worldTransform = PIXI.identityMatrix;
  1288. for(var i=0,j=this.children.length; i<j; i++)
  1289. {
  1290. this.children[i].updateTransform();
  1291. }
  1292. var bounds = this.getBounds();
  1293. this.worldTransform = matrixCache;
  1294. return bounds;
  1295. };
  1296. /**
  1297. * Sets the container's stage reference, the stage this object is connected to
  1298. *
  1299. * @method setStageReference
  1300. * @param stage {Stage} the stage that the container will have as its current stage reference
  1301. */
  1302. PIXI.DisplayObjectContainer.prototype.setStageReference = function(stage)
  1303. {
  1304. this.stage = stage;
  1305. if(this._interactive)this.stage.dirty = true;
  1306. for(var i=0,j=this.children.length; i<j; i++)
  1307. {
  1308. var child = this.children[i];
  1309. child.setStageReference(stage);
  1310. }
  1311. };
  1312. /**
  1313. * removes the current stage reference of the container
  1314. *
  1315. * @method removeStageReference
  1316. */
  1317. PIXI.DisplayObjectContainer.prototype.removeStageReference = function()
  1318. {
  1319. for(var i=0,j=this.children.length; i<j; i++)
  1320. {
  1321. var child = this.children[i];
  1322. child.removeStageReference();
  1323. }
  1324. if(this._interactive)this.stage.dirty = true;
  1325. this.stage = null;
  1326. };
  1327. /**
  1328. * Renders the object using the WebGL renderer
  1329. *
  1330. * @method _renderWebGL
  1331. * @param renderSession {RenderSession}
  1332. * @private
  1333. */
  1334. PIXI.DisplayObjectContainer.prototype._renderWebGL = function(renderSession)
  1335. {
  1336. if(!this.visible || this.alpha <= 0)return;
  1337. if(this._cacheAsBitmap)
  1338. {
  1339. this._renderCachedSprite(renderSession);
  1340. return;
  1341. }
  1342. var i,j;
  1343. if(this._mask || this._filters)
  1344. {
  1345. if(this._mask)
  1346. {
  1347. renderSession.spriteBatch.stop();
  1348. renderSession.maskManager.pushMask(this.mask, renderSession);
  1349. renderSession.spriteBatch.start();
  1350. }
  1351. if(this._filters)
  1352. {
  1353. renderSession.spriteBatch.flush();
  1354. renderSession.filterManager.pushFilter(this._filterBlock);
  1355. }
  1356. // simple render children!
  1357. for(i=0,j=this.children.length; i<j; i++)
  1358. {
  1359. this.children[i]._renderWebGL(renderSession);
  1360. }
  1361. renderSession.spriteBatch.stop();
  1362. if(this._filters)renderSession.filterManager.popFilter();
  1363. if(this._mask)renderSession.maskManager.popMask(renderSession);
  1364. renderSession.spriteBatch.start();
  1365. }
  1366. else
  1367. {
  1368. // simple render children!
  1369. for(i=0,j=this.children.length; i<j; i++)
  1370. {
  1371. this.children[i]._renderWebGL(renderSession);
  1372. }
  1373. }
  1374. };
  1375. /**
  1376. * Renders the object using the Canvas renderer
  1377. *
  1378. * @method _renderCanvas
  1379. * @param renderSession {RenderSession}
  1380. * @private
  1381. */
  1382. PIXI.DisplayObjectContainer.prototype._renderCanvas = function(renderSession)
  1383. {
  1384. if(this.visible === false || this.alpha === 0)return;
  1385. if(this._cacheAsBitmap)
  1386. {
  1387. this._renderCachedSprite(renderSession);
  1388. return;
  1389. }
  1390. if(this._mask)
  1391. {
  1392. renderSession.maskManager.pushMask(this._mask, renderSession.context);
  1393. }
  1394. for(var i=0,j=this.children.length; i<j; i++)
  1395. {
  1396. var child = this.children[i];
  1397. child._renderCanvas(renderSession);
  1398. }
  1399. if(this._mask)
  1400. {
  1401. renderSession.maskManager.popMask(renderSession.context);
  1402. }
  1403. };
  1404. /**
  1405. * @author Mat Groves http://matgroves.com/ @Doormat23
  1406. */
  1407. /**
  1408. * The Sprite object is the base for all textured objects that are rendered to the screen
  1409. *
  1410. * @class Sprite
  1411. * @extends DisplayObjectContainer
  1412. * @constructor
  1413. * @param texture {Texture} The texture for this sprite
  1414. *
  1415. * A sprite can be created directly from an image like this :
  1416. * var sprite = nex PIXI.Sprite.FromImage('assets/image.png');
  1417. * yourStage.addChild(sprite);
  1418. * then obviously don't forget to add it to the stage you have already created
  1419. */
  1420. PIXI.Sprite = function(texture)
  1421. {
  1422. PIXI.DisplayObjectContainer.call( this );
  1423. /**
  1424. * The anchor sets the origin point of the texture.
  1425. * The default is 0,0 this means the texture's origin is the top left
  1426. * Setting than anchor to 0.5,0.5 means the textures origin is centred
  1427. * Setting the anchor to 1,1 would mean the textures origin points will be the bottom right corner
  1428. *
  1429. * @property anchor
  1430. * @type Point
  1431. */
  1432. this.anchor = new PIXI.Point();
  1433. /**
  1434. * The texture that the sprite is using
  1435. *
  1436. * @property texture
  1437. * @type Texture
  1438. */
  1439. this.texture = texture;
  1440. /**
  1441. * The width of the sprite (this is initially set by the texture)
  1442. *
  1443. * @property _width
  1444. * @type Number
  1445. * @private
  1446. */
  1447. this._width = 0;
  1448. /**
  1449. * The height of the sprite (this is initially set by the texture)
  1450. *
  1451. * @property _height
  1452. * @type Number
  1453. * @private
  1454. */
  1455. this._height = 0;
  1456. /**
  1457. * The tint applied to the sprite. This is a hex value
  1458. *
  1459. * @property tint
  1460. * @type Number
  1461. * @default 0xFFFFFF
  1462. */
  1463. this.tint = 0xFFFFFF;// * Math.random();
  1464. /**
  1465. * The blend mode to be applied to the sprite
  1466. *
  1467. * @property blendMode
  1468. * @type Number
  1469. * @default PIXI.blendModes.NORMAL;
  1470. */
  1471. this.blendMode = PIXI.blendModes.NORMAL;
  1472. if(texture.baseTexture.hasLoaded)
  1473. {
  1474. this.onTextureUpdate();
  1475. }
  1476. else
  1477. {
  1478. this.onTextureUpdateBind = this.onTextureUpdate.bind(this);
  1479. this.texture.addEventListener( 'update', this.onTextureUpdateBind );
  1480. }
  1481. this.renderable = true;
  1482. };
  1483. // constructor
  1484. PIXI.Sprite.prototype = Object.create( PIXI.DisplayObjectContainer.prototype );
  1485. PIXI.Sprite.prototype.constructor = PIXI.Sprite;
  1486. /**
  1487. * The width of the sprite, setting this will actually modify the scale to achieve the value set
  1488. *
  1489. * @property width
  1490. * @type Number
  1491. */
  1492. Object.defineProperty(PIXI.Sprite.prototype, 'width', {
  1493. get: function() {
  1494. return this.scale.x * this.texture.frame.width;
  1495. },
  1496. set: function(value) {
  1497. this.scale.x = value / this.texture.frame.width;
  1498. this._width = value;
  1499. }
  1500. });
  1501. /**
  1502. * The height of the sprite, setting this will actually modify the scale to achieve the value set
  1503. *
  1504. * @property height
  1505. * @type Number
  1506. */
  1507. Object.defineProperty(PIXI.Sprite.prototype, 'height', {
  1508. get: function() {
  1509. return this.scale.y * this.texture.frame.height;
  1510. },
  1511. set: function(value) {
  1512. this.scale.y = value / this.texture.frame.height;
  1513. this._height = value;
  1514. }
  1515. });
  1516. /**
  1517. * Sets the texture of the sprite
  1518. *
  1519. * @method setTexture
  1520. * @param texture {Texture} The PIXI texture that is displayed by the sprite
  1521. */
  1522. PIXI.Sprite.prototype.setTexture = function(texture)
  1523. {
  1524. // stop current texture;
  1525. if(this.texture.baseTexture !== texture.baseTexture)
  1526. {
  1527. this.textureChange = true;
  1528. this.texture = texture;
  1529. }
  1530. else
  1531. {
  1532. this.texture = texture;
  1533. }
  1534. this.cachedTint = 0xFFFFFF;
  1535. this.updateFrame = true;
  1536. };
  1537. /**
  1538. * When the texture is updated, this event will fire to update the scale and frame
  1539. *
  1540. * @method onTextureUpdate
  1541. * @param event
  1542. * @private
  1543. */
  1544. PIXI.Sprite.prototype.onTextureUpdate = function()
  1545. {
  1546. // so if _width is 0 then width was not set..
  1547. if(this._width)this.scale.x = this._width / this.texture.frame.width;
  1548. if(this._height)this.scale.y = this._height / this.texture.frame.height;
  1549. this.updateFrame = true;
  1550. };
  1551. /**
  1552. * Returns the framing rectangle of the sprite as a PIXI.Rectangle object
  1553. *
  1554. * @method getBounds
  1555. * @param matrix {Matrix} the transformation matrix of the sprite
  1556. * @return {Rectangle} the framing rectangle
  1557. */
  1558. PIXI.Sprite.prototype.getBounds = function(matrix)
  1559. {
  1560. var width = this.texture.frame.width;
  1561. var height = this.texture.frame.height;
  1562. var w0 = width * (1-this.anchor.x);
  1563. var w1 = width * -this.anchor.x;
  1564. var h0 = height * (1-this.anchor.y);
  1565. var h1 = height * -this.anchor.y;
  1566. var worldTransform = matrix || this.worldTransform ;
  1567. var a = worldTransform.a;
  1568. var b = worldTransform.c;
  1569. var c = worldTransform.b;
  1570. var d = worldTransform.d;
  1571. var tx = worldTransform.tx;
  1572. var ty = worldTransform.ty;
  1573. var x1 = a * w1 + c * h1 + tx;
  1574. var y1 = d * h1 + b * w1 + ty;
  1575. var x2 = a * w0 + c * h1 + tx;
  1576. var y2 = d * h1 + b * w0 + ty;
  1577. var x3 = a * w0 + c * h0 + tx;
  1578. var y3 = d * h0 + b * w0 + ty;
  1579. var x4 = a * w1 + c * h0 + tx;
  1580. var y4 = d * h0 + b * w1 + ty;
  1581. var maxX = -Infinity;
  1582. var maxY = -Infinity;
  1583. var minX = Infinity;
  1584. var minY = Infinity;
  1585. minX = x1 < minX ? x1 : minX;
  1586. minX = x2 < minX ? x2 : minX;
  1587. minX = x3 < minX ? x3 : minX;
  1588. minX = x4 < minX ? x4 : minX;
  1589. minY = y1 < minY ? y1 : minY;
  1590. minY = y2 < minY ? y2 : minY;
  1591. minY = y3 < minY ? y3 : minY;
  1592. minY = y4 < minY ? y4 : minY;
  1593. maxX = x1 > maxX ? x1 : maxX;
  1594. maxX = x2 > maxX ? x2 : maxX;
  1595. maxX = x3 > maxX ? x3 : maxX;
  1596. maxX = x4 > maxX ? x4 : maxX;
  1597. maxY = y1 > maxY ? y1 : maxY;
  1598. maxY = y2 > maxY ? y2 : maxY;
  1599. maxY = y3 > maxY ? y3 : maxY;
  1600. maxY = y4 > maxY ? y4 : maxY;
  1601. var bounds = this._bounds;
  1602. bounds.x = minX;
  1603. bounds.width = maxX - minX;
  1604. bounds.y = minY;
  1605. bounds.height = maxY - minY;
  1606. // store a reference so that if this function gets called again in the render cycle we do not have to recalculate
  1607. this._currentBounds = bounds;
  1608. return bounds;
  1609. };
  1610. /**
  1611. * Renders the object using the WebGL renderer
  1612. *
  1613. * @method _renderWebGL
  1614. * @param renderSession {RenderSession}
  1615. * @private
  1616. */
  1617. PIXI.Sprite.prototype._renderWebGL = function(renderSession)
  1618. {
  1619. // if the sprite is not visible or the alpha is 0 then no need to render this element
  1620. if(!this.visible || this.alpha <= 0)return;
  1621. var i,j;
  1622. // do a quick check to see if this element has a mask or a filter.
  1623. if(this._mask || this._filters)
  1624. {
  1625. var spriteBatch = renderSession.spriteBatch;
  1626. if(this._mask)
  1627. {
  1628. spriteBatch.stop();
  1629. renderSession.maskManager.pushMask(this.mask, renderSession);
  1630. spriteBatch.start();
  1631. }
  1632. if(this._filters)
  1633. {
  1634. spriteBatch.flush();
  1635. renderSession.filterManager.pushFilter(this._filterBlock);
  1636. }
  1637. // add this sprite to the batch
  1638. spriteBatch.render(this);
  1639. // now loop through the children and make sure they get rendered
  1640. for(i=0,j=this.children.length; i<j; i++)
  1641. {
  1642. this.children[i]._renderWebGL(renderSession);
  1643. }
  1644. // time to stop the sprite batch as either a mask element or a filter draw will happen next
  1645. spriteBatch.stop();
  1646. if(this._filters)renderSession.filterManager.popFilter();
  1647. if(this._mask)renderSession.maskManager.popMask(renderSession);
  1648. spriteBatch.start();
  1649. }
  1650. else
  1651. {
  1652. renderSession.spriteBatch.render(this);
  1653. // simple render children!
  1654. for(i=0,j=this.children.length; i<j; i++)
  1655. {
  1656. this.children[i]._renderWebGL(renderSession);
  1657. }
  1658. }
  1659. //TODO check culling
  1660. };
  1661. /**
  1662. * Renders the object using the Canvas renderer
  1663. *
  1664. * @method _renderCanvas
  1665. * @param renderSession {RenderSession}
  1666. * @private
  1667. */
  1668. PIXI.Sprite.prototype._renderCanvas = function(renderSession)
  1669. {
  1670. // if the sprite is not visible or the alpha is 0 then no need to render this element
  1671. if(this.visible === false || this.alpha === 0)return;
  1672. var frame = this.texture.frame;
  1673. var context = renderSession.context;
  1674. var texture = this.texture;
  1675. if(this.blendMode !== renderSession.currentBlendMode)
  1676. {
  1677. renderSession.currentBlendMode = this.blendMode;
  1678. context.globalCompositeOperation = PIXI.blendModesCanvas[renderSession.currentBlendMode];
  1679. }
  1680. if(this._mask)
  1681. {
  1682. renderSession.maskManager.pushMask(this._mask, renderSession.context);
  1683. }
  1684. //ignore null sources
  1685. if(frame && frame.width && frame.height && texture.baseTexture.source)
  1686. {
  1687. context.globalAlpha = this.worldAlpha;
  1688. var transform = this.worldTransform;
  1689. // allow for trimming
  1690. if (renderSession.roundPixels)
  1691. {
  1692. context.setTransform(transform.a, transform.c, transform.b, transform.d, transform.tx | 0, transform.ty | 0);
  1693. }
  1694. else
  1695. {
  1696. context.setTransform(transform.a, transform.c, transform.b, transform.d, transform.tx, transform.ty);
  1697. }
  1698. //if smoothingEnabled is supported and we need to change the smoothing property for this texture
  1699. if(renderSession.smoothProperty && renderSession.scaleMode !== this.texture.baseTexture.scaleMode

Large files files are truncated, but you can click here to view the full file