Object2D.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. "use strict";
  2. import {Vector2} from "./math/Vector2.js";
  3. import {Matrix} from "./math/Matrix.js";
  4. import {UUID} from "./math/UUID.js";
  5. /**
  6. * Base 2D object class, implements all the object positioning and scalling features.
  7. *
  8. * @class
  9. */
  10. function Object2D()
  11. {
  12. /**
  13. * UUID of the object.
  14. */
  15. this.uuid = UUID.generate();
  16. /**
  17. * List of children objects attached to the object.
  18. */
  19. this.children = [];
  20. /**
  21. * Parent object, the object position is affected by its parent position.
  22. */
  23. this.parent = null;
  24. /**
  25. * Position of the object.
  26. */
  27. this.position = new Vector2(0, 0);
  28. /**
  29. * Origin of the object used as point of rotation.
  30. */
  31. this.origin = new Vector2(0, 0);
  32. /**
  33. * Scale of the object.
  34. */
  35. this.scale = new Vector2(1, 1);
  36. /**
  37. * Rotation of the object relative to its center.
  38. */
  39. this.rotation = 0.0;
  40. /**
  41. * Indicates if the object is visible.
  42. */
  43. this.visible = true;
  44. /**
  45. * Layer of this object, objects are sorted by layer value.
  46. *
  47. * Lower layer value is draw first.
  48. */
  49. this.layer = 0;
  50. /**
  51. * Local transformation matrix applied to the object.
  52. */
  53. this.matrix = new Matrix();
  54. /**
  55. * Global transformation matrix multiplied by the parent matrix.
  56. *
  57. * Used to transform the object before projecting into screen coordinates.
  58. */
  59. this.globalMatrix = new Matrix();
  60. /**
  61. * Inverse of the global matrix.
  62. *
  63. * Used to convert pointer input points into object coordinates.
  64. */
  65. this.inverseGlobalMatrix = new Matrix();
  66. /**
  67. * If true the matrix is updated before rendering the object.
  68. */
  69. this.matrixNeedsUpdate = true;
  70. /**
  71. * Indicates if its possible to drag the object around.
  72. *
  73. * If true the onPointerDrag callback is used to update the state of the object.
  74. */
  75. this.draggable = false;
  76. /**
  77. * Flag to indicate wheter this objet ignores the viewport transformation.
  78. */
  79. this.ignoreViewport = false;
  80. /**
  81. * Flag to indicate if the context of canvas should be saved before render.
  82. */
  83. this.saveContextState = true;
  84. /**
  85. * Flag to indicate if the context of canvas should be restored after render.
  86. */
  87. this.restoreContextState = true;
  88. /**
  89. * Flag to indicate if the context of canvas should be restored after render.
  90. */
  91. this.restoreContextState = true;
  92. /**
  93. * Flag indicating if the pointer is inside of the element.
  94. *
  95. * Used to control object event.
  96. */
  97. this.pointerInside = false;
  98. /**
  99. * Flag to indicate if the object is currently being dragged.
  100. */
  101. this.beingDragged = false;
  102. }
  103. /**
  104. * Traverse the object tree and run a function for all objects.
  105. *
  106. * @param callback Callback function that receives the object as parameter.
  107. */
  108. Object2D.prototype.traverse = function(callback)
  109. {
  110. callback(this);
  111. var children = this.children;
  112. for(var i = 0; i < children.length; i++)
  113. {
  114. children[i].traverse(callback);
  115. }
  116. };
  117. /**
  118. * Attach a children to the object.
  119. *
  120. * @param object Object to attach to this object.
  121. */
  122. Object2D.prototype.add = function(object)
  123. {
  124. object.parent = this;
  125. this.children.push(object);
  126. };
  127. /**
  128. * Remove object from the children list.
  129. *
  130. * @param object Object to be removed.
  131. */
  132. Object2D.prototype.remove = function(object)
  133. {
  134. var index = this.children.indexOf(object);
  135. if(index !== -1)
  136. {
  137. this.children[index].parent = null;
  138. this.children.splice(index, 1)
  139. }
  140. };
  141. /**
  142. * Check if a point is inside of the object.
  143. */
  144. Object2D.prototype.isInside = function(point)
  145. {
  146. return false;
  147. };
  148. /**
  149. * Update the transformation matrix of the object.
  150. */
  151. Object2D.prototype.updateMatrix = function(context)
  152. {
  153. if(this.matrixNeedsUpdate)
  154. {
  155. this.matrix.compose(this.position.x, this.position.y, this.scale.x, this.scale.y, this.origin.x, this.origin.y, this.rotation);
  156. this.globalMatrix.copy(this.matrix);
  157. if(this.parent !== null)
  158. {
  159. this.globalMatrix.premultiply(this.parent.globalMatrix);
  160. }
  161. this.inverseGlobalMatrix = this.globalMatrix.getInverse()
  162. //this.matrixNeedsUpdate = false;
  163. }
  164. };
  165. /**
  166. * Apply the transform to the rendering context.
  167. *
  168. * Can also be used for pre rendering logic.
  169. */
  170. Object2D.prototype.transform = function(context, viewport)
  171. {
  172. this.globalMatrix.tranformContext(context);
  173. };
  174. /**
  175. * Draw the object into the canvas.
  176. *
  177. * Has to be implemented by underlying classes.
  178. *
  179. * @param context Canvas 2d drawing context.
  180. */
  181. Object2D.prototype.draw = function(context, viewport){};
  182. /**
  183. * Callback method called every time before the object is draw into the canvas.
  184. *
  185. * Can be used to run preparation code, move the object, etc.
  186. */
  187. Object2D.prototype.onUpdate = null;
  188. /**
  189. * Callback method called when the pointer enters the object.
  190. *
  191. * Receives (pointer, viewport) as arguments.
  192. */
  193. Object2D.prototype.onPointerEnter = null;
  194. /**
  195. * Callback method called when the was inside of the object and leaves the object.
  196. *
  197. * Receives (pointer, viewport) as arguments.
  198. */
  199. Object2D.prototype.onPointerLeave = null;
  200. /**
  201. * Callback method while the pointer is over (inside) of the object.
  202. *
  203. * Receives (pointer, viewport) as arguments.
  204. */
  205. Object2D.prototype.onPointerOver = null;
  206. /**
  207. * Callback method while the object is being dragged across the screen.
  208. *
  209. * Receives (pointer, viewport, delta) as arguments. Delta is the movement of the pointer already translated into local object coordinates.
  210. */
  211. Object2D.prototype.onPointerDrag = function(pointer, viewport, delta)
  212. {
  213. this.position.add(delta);
  214. };
  215. /**
  216. * Callback method called while the pointer button is pressed.
  217. *
  218. * Receives (pointer, viewport) as arguments.
  219. */
  220. Object2D.prototype.onButtonPressed = null;
  221. /**
  222. * Callback method called when the pointer button is pressed down (single time).
  223. */
  224. Object2D.prototype.onButtonDown = null;
  225. /**
  226. * Callback method called when the pointer button is released (single time).
  227. */
  228. Object2D.prototype.onButtonUp = null;
  229. export {Object2D};