Object2D.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. import {Vector2} from "./math/Vector2.js";
  2. import {Matrix} from "./math/Matrix.js";
  3. import {UUID} from "./math/UUID.js";
  4. /**
  5. * Base object class, implements all the object positioning and scalling features.
  6. *
  7. * Stores all the base properties shared between all objects as the position, transformation properties, children etc.
  8. *
  9. * Object2D should be used as a group to store all the other objects drawn.
  10. *
  11. * @class
  12. */
  13. function Object2D()
  14. {
  15. /**
  16. * UUID of the object.
  17. */
  18. this.uuid = UUID.generate();
  19. /**
  20. * List of children objects attached to the object.
  21. */
  22. this.children = [];
  23. /**
  24. * Parent object, the object position is affected by its parent position.
  25. */
  26. this.parent = null;
  27. /**
  28. * Depth level in the object tree, objects with higher depth are drawn on top.
  29. *
  30. * The layer value is considered first.
  31. */
  32. this.level = 0;
  33. /**
  34. * Position of the object.
  35. */
  36. this.position = new Vector2(0, 0);
  37. /**
  38. * Origin of the object used as point of rotation.
  39. */
  40. this.origin = new Vector2(0, 0);
  41. /**
  42. * Scale of the object.
  43. */
  44. this.scale = new Vector2(1, 1);
  45. /**
  46. * Rotation of the object relative to its center.
  47. */
  48. this.rotation = 0.0;
  49. /**
  50. * Indicates if the object is visible.
  51. */
  52. this.visible = true;
  53. /**
  54. * Layer of this object, objects are sorted by layer value.
  55. *
  56. * Lower layer value is draw first.
  57. */
  58. this.layer = 0;
  59. /**
  60. * Local transformation matrix applied to the object.
  61. */
  62. this.matrix = new Matrix();
  63. /**
  64. * Global transformation matrix multiplied by the parent matrix.
  65. *
  66. * Used to transform the object before projecting into screen coordinates.
  67. */
  68. this.globalMatrix = new Matrix();
  69. /**
  70. * Inverse of the global matrix.
  71. *
  72. * Used to convert pointer input points into object coordinates.
  73. */
  74. this.inverseGlobalMatrix = new Matrix();
  75. /**
  76. * Masks being applied to this object.
  77. *
  78. * Multiple masks can be used simultaneously.
  79. */
  80. this.masks = [];
  81. /**
  82. * If true the matrix is updated before rendering the object.
  83. */
  84. this.matrixNeedsUpdate = true;
  85. /**
  86. * Indicates if its possible to drag the object around.
  87. *
  88. * If true the onPointerDrag callback is used to update the state of the object.
  89. */
  90. this.draggable = false;
  91. /**
  92. * Indicates if this object uses pointer events.
  93. *
  94. * Can be set false to skip the pointer interaction events.
  95. */
  96. this.pointerEvents = true;
  97. /**
  98. * Flag to indicate wheter this objet ignores the viewport transformation.
  99. */
  100. this.ignoreViewport = false;
  101. /**
  102. * Flag to indicate if the context of canvas should be saved before render.
  103. */
  104. this.saveContextState = true;
  105. /**
  106. * Flag to indicate if the context of canvas should be restored after render.
  107. */
  108. this.restoreContextState = true;
  109. /**
  110. * Flag indicating if the pointer is inside of the element.
  111. *
  112. * Used to control object event.
  113. */
  114. this.pointerInside = false;
  115. /**
  116. * Flag to indicate if the object is currently being dragged.
  117. */
  118. this.beingDragged = false;
  119. }
  120. /**
  121. * Traverse the object tree and run a function for all objects.
  122. *
  123. * @param {Function} callback Callback function that receives the object as parameter.
  124. */
  125. Object2D.prototype.traverse = function(callback)
  126. {
  127. callback(this);
  128. var children = this.children;
  129. for(var i = 0; i < children.length; i++)
  130. {
  131. children[i].traverse(callback);
  132. }
  133. };
  134. /**
  135. * Get a object from its children list by its UUID.
  136. *
  137. * @param {String} uuid UUID of the object to get.
  138. * @return {Object2D} The object that has the UUID specified, null if the object was not found.
  139. */
  140. Object2D.prototype.getChildByUUID = function(uuid)
  141. {
  142. var object = null;
  143. this.traverse(function(child)
  144. {
  145. if(child.uuid === uuid)
  146. {
  147. object = child;
  148. }
  149. });
  150. return object;
  151. };
  152. /**
  153. * Attach a children to this object.
  154. *
  155. * The object is set as children of this object and the transformations applied to this object are traversed to its children.
  156. *
  157. * @param {Object2D} object Object to attach to this object.
  158. */
  159. Object2D.prototype.add = function(object)
  160. {
  161. object.parent = this;
  162. object.level = this.level + 1;
  163. object.traverse(function(child)
  164. {
  165. if(child.onAdd !== null)
  166. {
  167. child.onAdd(this);
  168. }
  169. });
  170. this.children.push(object);
  171. };
  172. /**
  173. * Remove object from the children list.
  174. *
  175. * @param {Object2D} object Object to be removed.
  176. */
  177. Object2D.prototype.remove = function(object)
  178. {
  179. var index = this.children.indexOf(object);
  180. if(index !== -1)
  181. {
  182. var object = this.children[index];
  183. object.parent = null;
  184. object.level = 0;
  185. object.traverse(function(child)
  186. {
  187. if(child.onRemove !== null)
  188. {
  189. child.onRemove(this);
  190. }
  191. });
  192. this.children.splice(index, 1)
  193. }
  194. };
  195. /**
  196. * Check if a point is inside of the object.
  197. *
  198. * Used to update the point events attached to the object.
  199. *
  200. * @return {boolean} True if the point is inside of the object.
  201. */
  202. Object2D.prototype.isInside = function(point)
  203. {
  204. return false;
  205. };
  206. /**
  207. * Update the transformation matrix of the object.
  208. *
  209. * @param {CanvasRenderingContext2D} context
  210. */
  211. Object2D.prototype.updateMatrix = function(context)
  212. {
  213. if(this.matrixNeedsUpdate)
  214. {
  215. this.matrix.compose(this.position.x, this.position.y, this.scale.x, this.scale.y, this.origin.x, this.origin.y, this.rotation);
  216. this.globalMatrix.copy(this.matrix);
  217. if(this.parent !== null)
  218. {
  219. this.globalMatrix.premultiply(this.parent.globalMatrix);
  220. }
  221. this.inverseGlobalMatrix = this.globalMatrix.getInverse()
  222. //this.matrixNeedsUpdate = false;
  223. }
  224. };
  225. /**
  226. * Apply the transform to the rendering context.
  227. *
  228. * It is assumed that the viewport transform is pre-applied to the context.
  229. *
  230. * Can also be used for pre rendering logic.
  231. *
  232. * @param {CanvasRenderingContext2D} context Canvas 2d drawing context.
  233. * @param {Viewport} viewport Viewport applied to the canvas.
  234. */
  235. Object2D.prototype.transform = function(context, viewport)
  236. {
  237. this.globalMatrix.tranformContext(context);
  238. };
  239. /**
  240. * Draw the object into the canvas.
  241. *
  242. * Has to be implemented by underlying classes.
  243. *
  244. * @param {CanvasRenderingContext2D} context Canvas 2d drawing context.
  245. * @param {Viewport} viewport Viewport applied to the canvas.
  246. * @param {DOM} canvas DOM canvas element where the content is being drawn.
  247. */
  248. Object2D.prototype.draw = function(context, viewport, canvas){};
  249. /**
  250. * Callback method while the object is being dragged across the screen.
  251. *
  252. * By default is adds the delta value to the object position (making it follow the mouse movement).
  253. *
  254. * Delta is the movement of the pointer already translated into local object coordinates.
  255. *
  256. * Receives (pointer, viewport, delta) as arguments.
  257. *
  258. * @param {Pointer} pointer Pointer object that receives the user input.
  259. * @param {Viewport} viewport Viewport where the object is drawn.
  260. * @param {Vector2} delta Pointer movement in world space.
  261. */
  262. Object2D.prototype.onPointerDrag = function(pointer, viewport, delta)
  263. {
  264. this.position.add(delta);
  265. };
  266. /**
  267. * Method called when the object its added to a parent.
  268. *
  269. * @param {Object2D} parent Parent object were it was added.
  270. */
  271. Object2D.prototype.onAdd = null;
  272. /**
  273. * Method called when the object gets removed from its parent
  274. *
  275. * @param {Object2D} parent Parent object from were the object is being removed.
  276. */
  277. Object2D.prototype.onRemove = null;
  278. /**
  279. * Callback method called every time before the object is draw into the canvas.
  280. *
  281. * Can be used to run preparation code, move the object, etc.
  282. */
  283. Object2D.prototype.onUpdate = null;
  284. /**
  285. * Callback method called when the pointer enters the object.
  286. *
  287. * Receives (pointer, viewport) as arguments.
  288. *
  289. * @param {Pointer} pointer Pointer object that receives the user input.
  290. * @param {Viewport} viewport Viewport where the object is drawn.
  291. */
  292. Object2D.prototype.onPointerEnter = null;
  293. /**
  294. * Callback method called when the was inside of the object and leaves the object.
  295. *
  296. * Receives (pointer, viewport) as arguments.
  297. *
  298. * @param {Pointer} pointer Pointer object that receives the user input.
  299. * @param {Viewport} viewport Viewport where the object is drawn.
  300. */
  301. Object2D.prototype.onPointerLeave = null;
  302. /**
  303. * Callback method while the pointer is over (inside) of the object.
  304. *
  305. * Receives (pointer, viewport) as arguments.
  306. *
  307. * @param {Pointer} pointer Pointer object that receives the user input.
  308. * @param {Viewport} viewport Viewport where the object is drawn.
  309. */
  310. Object2D.prototype.onPointerOver = null;
  311. /**
  312. * Callback method called while the pointer button is pressed.
  313. *
  314. * Receives (pointer, viewport) as arguments.
  315. *
  316. * @param {Pointer} pointer Pointer object that receives the user input.
  317. * @param {Viewport} viewport Viewport where the object is drawn.
  318. */
  319. Object2D.prototype.onButtonPressed = null;
  320. /**
  321. * Callback method called while the pointer button is double clicked.
  322. *
  323. * Receives (pointer, viewport) as arguments.
  324. *
  325. * @param {Pointer} pointer Pointer object that receives the user input.
  326. * @param {Viewport} viewport Viewport where the object is drawn.
  327. */
  328. Object2D.prototype.onDoubleClick = null;
  329. /**
  330. * Callback method called when the pointer button is pressed down (single time).
  331. *
  332. * Receives (pointer, viewport) as arguments.
  333. *
  334. * @param {Pointer} pointer Pointer object that receives the user input.
  335. * @param {Viewport} viewport Viewport where the object is drawn.
  336. */
  337. Object2D.prototype.onButtonDown = null;
  338. /**
  339. * Callback method called when the pointer button is released (single time).
  340. *
  341. * Receives (pointer, viewport) as arguments.
  342. *
  343. * @param {Pointer} pointer Pointer object that receives the user input.
  344. * @param {Viewport} viewport Viewport where the object is drawn.
  345. */
  346. Object2D.prototype.onButtonUp = null;
  347. export {Object2D};