sprite.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import { Vector3 } from "../Maths/math.vector.js";
  2. import { Color4 } from "../Maths/math.color.js";
  3. import { Observable } from "../Misc/observable.js";
  4. import { ThinSprite } from "./thinSprite.js";
  5. /**
  6. * Class used to represent a sprite
  7. * @see https://doc.babylonjs.com/features/featuresDeepDive/sprites
  8. */
  9. export class Sprite extends ThinSprite {
  10. /**
  11. * Gets or sets the sprite size
  12. */
  13. get size() {
  14. return this.width;
  15. }
  16. set size(value) {
  17. this.width = value;
  18. this.height = value;
  19. }
  20. /**
  21. * Gets the manager of this sprite
  22. */
  23. get manager() {
  24. return this._manager;
  25. }
  26. /**
  27. * Creates a new Sprite
  28. * @param name defines the name
  29. * @param manager defines the manager
  30. */
  31. constructor(
  32. /** defines the name */
  33. name, manager) {
  34. super();
  35. this.name = name;
  36. /** Gets the list of attached animations */
  37. this.animations = new Array();
  38. /** Gets or sets a boolean indicating if the sprite can be picked */
  39. this.isPickable = false;
  40. /** Gets or sets a boolean indicating that sprite texture alpha will be used for precise picking (false by default) */
  41. this.useAlphaForPicking = false;
  42. /**
  43. * An event triggered when the control has been disposed
  44. */
  45. this.onDisposeObservable = new Observable();
  46. this._onAnimationEnd = null;
  47. this._endAnimation = () => {
  48. if (this._onAnimationEnd) {
  49. this._onAnimationEnd();
  50. }
  51. if (this.disposeWhenFinishedAnimating) {
  52. this.dispose();
  53. }
  54. };
  55. this.color = new Color4(1.0, 1.0, 1.0, 1.0);
  56. this.position = Vector3.Zero();
  57. this._manager = manager;
  58. this._manager.sprites.push(this);
  59. this.uniqueId = this._manager.scene.getUniqueId();
  60. }
  61. /**
  62. * Returns the string "Sprite"
  63. * @returns "Sprite"
  64. */
  65. getClassName() {
  66. return "Sprite";
  67. }
  68. /** Gets or sets the initial key for the animation (setting it will restart the animation) */
  69. get fromIndex() {
  70. return this._fromIndex;
  71. }
  72. set fromIndex(value) {
  73. this.playAnimation(value, this._toIndex, this._loopAnimation, this._delay, this._onAnimationEnd);
  74. }
  75. /** Gets or sets the end key for the animation (setting it will restart the animation) */
  76. get toIndex() {
  77. return this._toIndex;
  78. }
  79. set toIndex(value) {
  80. this.playAnimation(this._fromIndex, value, this._loopAnimation, this._delay, this._onAnimationEnd);
  81. }
  82. /** Gets or sets a boolean indicating if the animation is looping (setting it will restart the animation) */
  83. get loopAnimation() {
  84. return this._loopAnimation;
  85. }
  86. set loopAnimation(value) {
  87. this.playAnimation(this._fromIndex, this._toIndex, value, this._delay, this._onAnimationEnd);
  88. }
  89. /** Gets or sets the delay between cell changes (setting it will restart the animation) */
  90. get delay() {
  91. return Math.max(this._delay, 1);
  92. }
  93. set delay(value) {
  94. this.playAnimation(this._fromIndex, this._toIndex, this._loopAnimation, value, this._onAnimationEnd);
  95. }
  96. /**
  97. * Starts an animation
  98. * @param from defines the initial key
  99. * @param to defines the end key
  100. * @param loop defines if the animation must loop
  101. * @param delay defines the start delay (in ms)
  102. * @param onAnimationEnd defines a callback to call when animation ends
  103. */
  104. playAnimation(from, to, loop, delay, onAnimationEnd = null) {
  105. this._onAnimationEnd = onAnimationEnd;
  106. super.playAnimation(from, to, loop, delay, this._endAnimation);
  107. }
  108. /** Release associated resources */
  109. dispose() {
  110. for (let i = 0; i < this._manager.sprites.length; i++) {
  111. if (this._manager.sprites[i] == this) {
  112. this._manager.sprites.splice(i, 1);
  113. }
  114. }
  115. // Callback
  116. this.onDisposeObservable.notifyObservers(this);
  117. this.onDisposeObservable.clear();
  118. }
  119. /**
  120. * Serializes the sprite to a JSON object
  121. * @returns the JSON object
  122. */
  123. serialize() {
  124. const serializationObject = {};
  125. serializationObject.name = this.name;
  126. serializationObject.position = this.position.asArray();
  127. serializationObject.color = this.color.asArray();
  128. serializationObject.width = this.width;
  129. serializationObject.height = this.height;
  130. serializationObject.angle = this.angle;
  131. serializationObject.cellIndex = this.cellIndex;
  132. serializationObject.cellRef = this.cellRef;
  133. serializationObject.invertU = this.invertU;
  134. serializationObject.invertV = this.invertV;
  135. serializationObject.disposeWhenFinishedAnimating = this.disposeWhenFinishedAnimating;
  136. serializationObject.isPickable = this.isPickable;
  137. serializationObject.isVisible = this.isVisible;
  138. serializationObject.useAlphaForPicking = this.useAlphaForPicking;
  139. serializationObject.animationStarted = this.animationStarted;
  140. serializationObject.fromIndex = this.fromIndex;
  141. serializationObject.toIndex = this.toIndex;
  142. serializationObject.loopAnimation = this.loopAnimation;
  143. serializationObject.delay = this.delay;
  144. return serializationObject;
  145. }
  146. /**
  147. * Parses a JSON object to create a new sprite
  148. * @param parsedSprite The JSON object to parse
  149. * @param manager defines the hosting manager
  150. * @returns the new sprite
  151. */
  152. static Parse(parsedSprite, manager) {
  153. const sprite = new Sprite(parsedSprite.name, manager);
  154. sprite.position = Vector3.FromArray(parsedSprite.position);
  155. sprite.color = Color4.FromArray(parsedSprite.color);
  156. sprite.width = parsedSprite.width;
  157. sprite.height = parsedSprite.height;
  158. sprite.angle = parsedSprite.angle;
  159. sprite.cellIndex = parsedSprite.cellIndex;
  160. sprite.cellRef = parsedSprite.cellRef;
  161. sprite.invertU = parsedSprite.invertU;
  162. sprite.invertV = parsedSprite.invertV;
  163. sprite.disposeWhenFinishedAnimating = parsedSprite.disposeWhenFinishedAnimating;
  164. sprite.isPickable = parsedSprite.isPickable;
  165. sprite.isVisible = parsedSprite.isVisible;
  166. sprite.useAlphaForPicking = parsedSprite.useAlphaForPicking;
  167. sprite._fromIndex = parsedSprite.fromIndex;
  168. sprite._toIndex = parsedSprite.toIndex;
  169. sprite._loopAnimation = parsedSprite.loopAnimation;
  170. sprite._delay = parsedSprite.delay;
  171. if (parsedSprite.animationStarted) {
  172. sprite.playAnimation(sprite.fromIndex, sprite.toIndex, sprite.loopAnimation, sprite.delay);
  173. }
  174. return sprite;
  175. }
  176. }
  177. //# sourceMappingURL=sprite.js.map