sprite.ts 7.7 KB

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