Object2D.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. * Depth level in the object tree, objects with higher depth are drawn on top.
  26. *
  27. * The layer value is considered first.
  28. */
  29. this.level = 0;
  30. /**
  31. * Position of the object.
  32. */
  33. this.position = new Vector2(0, 0);
  34. /**
  35. * Origin of the object used as point of rotation.
  36. */
  37. this.origin = new Vector2(0, 0);
  38. /**
  39. * Scale of the object.
  40. */
  41. this.scale = new Vector2(1, 1);
  42. /**
  43. * Rotation of the object relative to its center.
  44. */
  45. this.rotation = 0.0;
  46. /**
  47. * Indicates if the object is visible.
  48. */
  49. this.visible = true;
  50. /**
  51. * Layer of this object, objects are sorted by layer value.
  52. *
  53. * Lower layer value is draw first.
  54. */
  55. this.layer = 0;
  56. /**
  57. * Local transformation matrix applied to the object.
  58. */
  59. this.matrix = new Matrix();
  60. /**
  61. * Global transformation matrix multiplied by the parent matrix.
  62. *
  63. * Used to transform the object before projecting into screen coordinates.
  64. */
  65. this.globalMatrix = new Matrix();
  66. /**
  67. * Inverse of the global matrix.
  68. *
  69. * Used to convert pointer input points into object coordinates.
  70. */
  71. this.inverseGlobalMatrix = new Matrix();
  72. /**
  73. * Masks being applied to this object.
  74. *
  75. * Multiple masks can be used simultaneously.
  76. */
  77. this.masks = [];
  78. /**
  79. * If true the matrix is updated before rendering the object.
  80. */
  81. this.matrixNeedsUpdate = true;
  82. /**
  83. * Indicates if its possible to drag the object around.
  84. *
  85. * If true the onPointerDrag callback is used to update the state of the object.
  86. */
  87. this.draggable = false;
  88. /**
  89. * Indicates if this object uses pointer events.
  90. *
  91. * Can be set false to skip the pointer interaction events.
  92. */
  93. this.pointerEvents = true;
  94. /**
  95. * Flag to indicate wheter this objet ignores the viewport transformation.
  96. */
  97. this.ignoreViewport = false;
  98. /**
  99. * Flag to indicate if the context of canvas should be saved before render.
  100. */
  101. this.saveContextState = true;
  102. /**
  103. * Flag to indicate if the context of canvas should be restored after render.
  104. */
  105. this.restoreContextState = true;
  106. /**
  107. * Flag indicating if the pointer is inside of the element.
  108. *
  109. * Used to control object event.
  110. */
  111. this.pointerInside = false;
  112. /**
  113. * Flag to indicate if the object is currently being dragged.
  114. */
  115. this.beingDragged = false;
  116. }
  117. /**
  118. * Traverse the object tree and run a function for all objects.
  119. *
  120. * @param callback Callback function that receives the object as parameter.
  121. */
  122. Object2D.prototype.traverse = function(callback)
  123. {
  124. callback(this);
  125. var children = this.children;
  126. for(var i = 0; i < children.length; i++)
  127. {
  128. children[i].traverse(callback);
  129. }
  130. };
  131. /**
  132. * Attach a children to the object.
  133. *
  134. * @param object Object to attach to this object.
  135. */
  136. Object2D.prototype.add = function(object)
  137. {
  138. object.parent = this;
  139. object.level = this.level + 1;
  140. object.traverse(function(child)
  141. {
  142. if(child.onAdd !== null)
  143. {
  144. child.onAdd(this);
  145. }
  146. });
  147. this.children.push(object);
  148. };
  149. /**
  150. * Remove object from the children list.
  151. *
  152. * @param object Object to be removed.
  153. */
  154. Object2D.prototype.remove = function(object)
  155. {
  156. var index = this.children.indexOf(object);
  157. if(index !== -1)
  158. {
  159. var object = this.children[index];
  160. object.parent = null;
  161. object.level = 0;
  162. object.traverse(function(child)
  163. {
  164. if(child.onRemove !== null)
  165. {
  166. child.onRemove(this);
  167. }
  168. });
  169. this.children.splice(index, 1)
  170. }
  171. };
  172. /**
  173. * Check if a point is inside of the object.
  174. */
  175. Object2D.prototype.isInside = function(point)
  176. {
  177. return false;
  178. };
  179. /**
  180. * Update the transformation matrix of the object.
  181. */
  182. Object2D.prototype.updateMatrix = function(context)
  183. {
  184. if(this.matrixNeedsUpdate)
  185. {
  186. this.matrix.compose(this.position.x, this.position.y, this.scale.x, this.scale.y, this.origin.x, this.origin.y, this.rotation);
  187. this.globalMatrix.copy(this.matrix);
  188. if(this.parent !== null)
  189. {
  190. this.globalMatrix.premultiply(this.parent.globalMatrix);
  191. }
  192. this.inverseGlobalMatrix = this.globalMatrix.getInverse()
  193. //this.matrixNeedsUpdate = false;
  194. }
  195. };
  196. /**
  197. * Apply the transform to the rendering context.
  198. *
  199. * It is assumed that the viewport transform is pre-applied to the context.
  200. *
  201. * Can also be used for pre rendering logic.
  202. *
  203. * @param {CanvasContext} context Canvas 2d drawing context.
  204. * @param {Viewport} viewport Viewport applied to the canvas.
  205. */
  206. Object2D.prototype.transform = function(context, viewport)
  207. {
  208. this.globalMatrix.tranformContext(context);
  209. };
  210. /**
  211. * Draw the object into the canvas.
  212. *
  213. * Has to be implemented by underlying classes.
  214. *
  215. * @param {CanvasContext} context Canvas 2d drawing context.
  216. * @param {Viewport} viewport Viewport applied to the canvas.
  217. * @param {DOM} canvas DOM canvas element where the content is being drawn.
  218. */
  219. Object2D.prototype.draw = function(context, viewport, canvas){};
  220. /**
  221. * Method called when the object its added to a parent.
  222. *
  223. * Receives (parent) as arguments.
  224. */
  225. Object2D.prototype.onAdd = null;
  226. /**
  227. * Method called when the object gets removed from its parent
  228. *
  229. * Receives (parent) as arguments.
  230. */
  231. Object2D.prototype.onRemove = null;
  232. /**
  233. * Callback method called every time before the object is draw into the canvas.
  234. *
  235. * Can be used to run preparation code, move the object, etc.
  236. */
  237. Object2D.prototype.onUpdate = null;
  238. /**
  239. * Callback method called when the pointer enters the object.
  240. *
  241. * Receives (pointer, viewport) as arguments.
  242. */
  243. Object2D.prototype.onPointerEnter = null;
  244. /**
  245. * Callback method called when the was inside of the object and leaves the object.
  246. *
  247. * Receives (pointer, viewport) as arguments.
  248. */
  249. Object2D.prototype.onPointerLeave = null;
  250. /**
  251. * Callback method while the pointer is over (inside) of the object.
  252. *
  253. * Receives (pointer, viewport) as arguments.
  254. */
  255. Object2D.prototype.onPointerOver = null;
  256. /**
  257. * Callback method while the object is being dragged across the screen.
  258. *
  259. * Receives (pointer, viewport, delta) as arguments. Delta is the movement of the pointer already translated into local object coordinates.
  260. */
  261. Object2D.prototype.onPointerDrag = function(pointer, viewport, delta)
  262. {
  263. this.position.add(delta);
  264. };
  265. /**
  266. * Callback method called while the pointer button is pressed.
  267. *
  268. * Receives (pointer, viewport) as arguments.
  269. */
  270. Object2D.prototype.onButtonPressed = null;
  271. /**
  272. * Callback method called when the pointer button is pressed down (single time).
  273. *
  274. * Receives (pointer, viewport) as arguments.
  275. */
  276. Object2D.prototype.onButtonDown = null;
  277. /**
  278. * Callback method called when the pointer button is released (single time).
  279. *
  280. * Receives (pointer, viewport) as arguments.
  281. */
  282. Object2D.prototype.onButtonUp = null;
  283. export {Object2D};