Object2D.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. * Global transformation matrix multiplied by the parent matrix.
  45. *
  46. * Used to transform the object before projecting into screen coordinates.
  47. */
  48. this.globalMatrix = new Matrix();
  49. /**
  50. * Inverse of the global matrix.
  51. *
  52. * Used to convert mouse input points into object coordinates.
  53. */
  54. this.inverseGlobalMatrix = new Matrix();
  55. /**
  56. * If true the matrix is updated before rendering the object.
  57. */
  58. this.matrixNeedsUpdate = true;
  59. }
  60. /**
  61. * Traverse the object tree and run a function for all objects.
  62. *
  63. * @param callback Callback function that receives the object as parameter.
  64. */
  65. Object2D.prototype.traverse = function(callback)
  66. {
  67. callback(this);
  68. var children = this.children;
  69. for(var i = 0; i < children.length; i++)
  70. {
  71. children[i].traverse(callback);
  72. }
  73. };
  74. /**
  75. * Attach a children to the object.
  76. *
  77. * @param object Object to attach to this object.
  78. */
  79. Object2D.prototype.add = function(object)
  80. {
  81. object.parent = this;
  82. this.children.push(object);
  83. };
  84. /**
  85. * Remove object from the children list.
  86. *
  87. * @param object Object to be removed.
  88. */
  89. Object2D.prototype.remove = function(object)
  90. {
  91. var index = this.children.indexOf(object);
  92. if(index !== -1)
  93. {
  94. this.children[index].parent = null;
  95. this.children.splice(index, 1)
  96. }
  97. };
  98. Object2D.prototype.onOver = function(){};
  99. /**
  100. * Check if a point is inside of the object.
  101. */
  102. Object2D.prototype.isInside = function(point)
  103. {
  104. return false;
  105. };
  106. /**
  107. * Update the transformation matrix of the object.
  108. */
  109. Object2D.prototype.updateMatrix = function(context)
  110. {
  111. if(this.matrixNeedsUpdate)
  112. {
  113. this.matrix.compose(this.position.x, this.position.y, this.scale.x, this.scale.y, this.rotation);
  114. this.globalMatrix.copy(this.matrix);
  115. if(this.parent !== null)
  116. {
  117. this.globalMatrix.premultiply(this.parent.globalMatrix);
  118. }
  119. this.inverseGlobalMatrix = this.globalMatrix.getInverse()
  120. //this.matrixNeedsUpdate = false;
  121. }
  122. };
  123. /**
  124. * Draw the object into the canvas.
  125. *
  126. * Has to be implemented by underlying classes.
  127. *
  128. * @param context Canvas 2d drawing context.
  129. * @param canvas The canvas DOM element where its being drawn.
  130. */
  131. Object2D.prototype.draw = function(context){};