Object2D.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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 scaling features.
  6. *
  7. * Stores all the base properties shared between all objects as the position, transformation properties, children etc.
  8. *
  9. * Object2D object can be used as a group to 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, it is assumed that the viewport transform is pre-applied to the context.
  227. *
  228. * This is called before style() and draw(). It can also be used for some pre-rendering logic.
  229. *
  230. * @param {CanvasRenderingContext2D} context Canvas 2d drawing context.
  231. * @param {Viewport} viewport Viewport applied to the canvas.
  232. */
  233. Object2D.prototype.transform = function(context, viewport)
  234. {
  235. this.globalMatrix.tranformContext(context);
  236. };
  237. /**
  238. * Style is called right before draw() it should not draw any content into the canvas, all context styling should be applied here (colors, fonts, etc).
  239. *
  240. * The draw() and style() methods can be useful for objects that share the same styling attributes but are drawing differently.
  241. *
  242. * Should be implemented by underlying classes.
  243. *
  244. * @param {CanvasRenderingContext2D} context Canvas 2d drawing context.
  245. * @param {Viewport} viewport Viewport used to view the canvas content.
  246. * @param {DOM} canvas DOM canvas element where the content is being drawn.
  247. */
  248. Object2D.prototype.style = function(context, viewport, canvas){};
  249. /**
  250. * Draw the object into the canvas, this is called transform() and style(), should be where the content is actually drawn into the canvas.
  251. *
  252. * Should be implemented by underlying classes.
  253. *
  254. * @param {CanvasRenderingContext2D} context Canvas 2d drawing context.
  255. * @param {Viewport} viewport Viewport used to view the canvas content.
  256. * @param {DOM} canvas DOM canvas element where the content is being drawn.
  257. */
  258. Object2D.prototype.draw = function(context, viewport, canvas){};
  259. /**
  260. * Callback method while the object is being dragged across the screen.
  261. *
  262. * By default is adds the delta value to the object position (making it follow the mouse movement).
  263. *
  264. * Delta is the movement of the pointer already translated into local object coordinates.
  265. *
  266. * Receives (pointer, viewport, delta) as arguments.
  267. *
  268. * @param {Pointer} pointer Pointer object that receives the user input.
  269. * @param {Viewport} viewport Viewport where the object is drawn.
  270. * @param {Vector2} delta Pointer movement in world space.
  271. */
  272. Object2D.prototype.onPointerDrag = function(pointer, viewport, delta)
  273. {
  274. this.position.add(delta);
  275. };
  276. /**
  277. * Method called when the object its added to a parent.
  278. *
  279. * @param {Object2D} parent Parent object were it was added.
  280. */
  281. Object2D.prototype.onAdd = null;
  282. /**
  283. * Method called when the object gets removed from its parent
  284. *
  285. * @param {Object2D} parent Parent object from were the object is being removed.
  286. */
  287. Object2D.prototype.onRemove = null;
  288. /**
  289. * Callback method called every time before the object is draw into the canvas.
  290. *
  291. * Can be used to run preparation code, move the object, etc.
  292. */
  293. Object2D.prototype.onUpdate = null;
  294. /**
  295. * Callback method called when the pointer enters the object.
  296. *
  297. * Receives (pointer, viewport) as arguments.
  298. *
  299. * @param {Pointer} pointer Pointer object that receives the user input.
  300. * @param {Viewport} viewport Viewport where the object is drawn.
  301. */
  302. Object2D.prototype.onPointerEnter = null;
  303. /**
  304. * Callback method called when the was inside of the object and leaves the object.
  305. *
  306. * Receives (pointer, viewport) as arguments.
  307. *
  308. * @param {Pointer} pointer Pointer object that receives the user input.
  309. * @param {Viewport} viewport Viewport where the object is drawn.
  310. */
  311. Object2D.prototype.onPointerLeave = null;
  312. /**
  313. * Callback method while the pointer is over (inside) of the object.
  314. *
  315. * Receives (pointer, viewport) as arguments.
  316. *
  317. * @param {Pointer} pointer Pointer object that receives the user input.
  318. * @param {Viewport} viewport Viewport where the object is drawn.
  319. */
  320. Object2D.prototype.onPointerOver = null;
  321. /**
  322. * Callback method called while the pointer button is pressed.
  323. *
  324. * Receives (pointer, viewport) as arguments.
  325. *
  326. * @param {Pointer} pointer Pointer object that receives the user input.
  327. * @param {Viewport} viewport Viewport where the object is drawn.
  328. */
  329. Object2D.prototype.onButtonPressed = null;
  330. /**
  331. * Callback method called while the pointer button is double clicked.
  332. *
  333. * Receives (pointer, viewport) as arguments.
  334. *
  335. * @param {Pointer} pointer Pointer object that receives the user input.
  336. * @param {Viewport} viewport Viewport where the object is drawn.
  337. */
  338. Object2D.prototype.onDoubleClick = null;
  339. /**
  340. * Callback method called when the pointer button is pressed down (single time).
  341. *
  342. * Receives (pointer, viewport) as arguments.
  343. *
  344. * @param {Pointer} pointer Pointer object that receives the user input.
  345. * @param {Viewport} viewport Viewport where the object is drawn.
  346. */
  347. Object2D.prototype.onButtonDown = null;
  348. /**
  349. * Callback method called when the pointer button is released (single time).
  350. *
  351. * Receives (pointer, viewport) as arguments.
  352. *
  353. * @param {Pointer} pointer Pointer object that receives the user input.
  354. * @param {Viewport} viewport Viewport where the object is drawn.
  355. */
  356. Object2D.prototype.onButtonUp = null;
  357. export {Object2D};