Object2D.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. "use strict";
  2. /**
  3. * Base 2D object class, implements all the object positioning and scalling features.
  4. *
  5. * @class
  6. */
  7. function Object2D()
  8. {
  9. /**
  10. * UUID of the object.
  11. */
  12. this.uuid = UUID.generate();
  13. /**
  14. * List of children objects attached to the object.
  15. */
  16. this.children = [];
  17. /**
  18. * Parent object, the object position is affected by its parent position.
  19. */
  20. this.parent = null;
  21. /**
  22. * Position of the object.
  23. */
  24. this.position = new Vector2(0, 0);
  25. /**
  26. * Scale of the object.
  27. */
  28. this.scale = new Vector2(1, 1);
  29. /**
  30. * Rotation of the object relative to its center.
  31. */
  32. this.rotation = 0.0;
  33. /**
  34. * Layer of this object, objects are sorted by layer value.
  35. *
  36. * Lower layer value is draw first.
  37. */
  38. this.layer = 0;
  39. /**
  40. * Local transformation matrix applied to the object.
  41. */
  42. this.matrix = new Matrix();
  43. /**
  44. * Flag indicating if the pointer is inside of the element.
  45. *
  46. * Used to control object event.
  47. */
  48. this.pointerInside = false;
  49. /**
  50. * Global transformation matrix multiplied by the parent matrix.
  51. *
  52. * Used to transform the object before projecting into screen coordinates.
  53. */
  54. this.globalMatrix = new Matrix();
  55. /**
  56. * Inverse of the global matrix.
  57. *
  58. * Used to convert mouse input points into object coordinates.
  59. */
  60. this.inverseGlobalMatrix = new Matrix();
  61. /**
  62. * If true the matrix is updated before rendering the object.
  63. */
  64. this.matrixNeedsUpdate = true;
  65. }
  66. /**
  67. * Traverse the object tree and run a function for all objects.
  68. *
  69. * @param callback Callback function that receives the object as parameter.
  70. */
  71. Object2D.prototype.traverse = function(callback)
  72. {
  73. callback(this);
  74. var children = this.children;
  75. for(var i = 0; i < children.length; i++)
  76. {
  77. children[i].traverse(callback);
  78. }
  79. };
  80. /**
  81. * Attach a children to the object.
  82. *
  83. * @param object Object to attach to this object.
  84. */
  85. Object2D.prototype.add = function(object)
  86. {
  87. object.parent = this;
  88. this.children.push(object);
  89. };
  90. /**
  91. * Remove object from the children list.
  92. *
  93. * @param object Object to be removed.
  94. */
  95. Object2D.prototype.remove = function(object)
  96. {
  97. var index = this.children.indexOf(object);
  98. if(index !== -1)
  99. {
  100. this.children[index].parent = null;
  101. this.children.splice(index, 1)
  102. }
  103. };
  104. /**
  105. * Check if a point is inside of the object.
  106. */
  107. Object2D.prototype.isInside = function(point)
  108. {
  109. return false;
  110. };
  111. /**
  112. * Update the transformation matrix of the object.
  113. */
  114. Object2D.prototype.updateMatrix = function(context)
  115. {
  116. if(this.matrixNeedsUpdate)
  117. {
  118. this.matrix.compose(this.position.x, this.position.y, this.scale.x, this.scale.y, this.rotation);
  119. this.globalMatrix.copy(this.matrix);
  120. if(this.parent !== null)
  121. {
  122. this.globalMatrix.premultiply(this.parent.globalMatrix);
  123. }
  124. this.inverseGlobalMatrix = this.globalMatrix.getInverse()
  125. //this.matrixNeedsUpdate = false;
  126. }
  127. };
  128. /**
  129. * Draw the object into the canvas.
  130. *
  131. * Has to be implemented by underlying classes.
  132. *
  133. * @param context Canvas 2d drawing context.
  134. */
  135. Object2D.prototype.draw = function(context){};
  136. /**
  137. * Callback method called when the pointer enters the object.
  138. */
  139. Object2D.prototype.onPointerEnter = null;
  140. /**
  141. * Callback method called when the was inside of the object and leaves the object.
  142. */
  143. Object2D.prototype.onPointerLeave = null;
  144. /**
  145. * Callback method while the pointer is over (inside) of the object.
  146. */
  147. Object2D.prototype.onPointerOver = null;
  148. /**
  149. * Callback method called while the pointer button is pressed.
  150. */
  151. Object2D.prototype.onPointerPressed = null;
  152. /**
  153. * Callback method called when the pointer button is pressed down (single time).
  154. */
  155. Object2D.prototype.onPointerDown = null;
  156. /**
  157. * Callback method called when the pointer button is released (single time).
  158. */
  159. Object2D.prototype.onPointerUp = null;