Source: Renderables/SpriteRenderable.js

  1. /*
  2. * File: SpriteRenderable.js
  3. *
  4. * Texture objects where texture coordinate can change
  5. */
  6. /*jslint node: true, vars: true */
  7. /*global gEngine: false, Renderable: false, TextureRenderable: false */
  8. /* find out more about jslint: http://www.jslint.com/help.html */
  9. // Constructor and object definition
  10. "use strict"; // Operate in Strict mode such that variables must be declared before used!
  11. /**
  12. * Constructor of SpriteRenderable object.
  13. * @class SpriteRenderable
  14. * @param {Texture} myTexture Texture to be associated by object.
  15. * @returns {SpriteRenderable} Instance of this SpriteRenderable object
  16. */
  17. function SpriteRenderable(myTexture) {
  18. TextureRenderable.call(this, myTexture);
  19. Renderable.prototype._setShader.call(this, gEngine.DefaultResources.getSpriteShader());
  20. this.mTexLeft = 0.0; // bounds of texture coordinate (0 is left, 1 is right)
  21. this.mTexRight = 1.0; //
  22. this.mTexTop = 1.0; // 1 is top and 0 is bottom of image
  23. this.mTexBottom = 0.0; //
  24. //
  25. this._setTexInfo();
  26. }
  27. gEngine.Core.inheritPrototype(SpriteRenderable, TextureRenderable);
  28. //<editor-fold desc="Public Methods">
  29. // Convention: eName is an enumerated data type
  30. /**
  31. * the expected texture cooridnate array is an array of 8 floats where elements:<p>
  32. * [0] [1]: is u/v coordinate of Top-Right<p>
  33. * [2] [3]: is u/v coordinate of Top-Left<p>
  34. * [4] [5]: is u/v coordinate of Bottom-Right<p>
  35. * [6] [7]: is u/v coordinate of Bottom-Left
  36. * @memberOf SpriteRenderable
  37. * @type {float[]|enum}
  38. */
  39. SpriteRenderable.eTexCoordArray = Object.freeze({
  40. eLeft: 2,
  41. eRight: 0,
  42. eTop: 1,
  43. eBottom: 5
  44. });
  45. //**-----------------------------------------
  46. // Public methods
  47. //**-----------------------------------------
  48. /**
  49. * specify element region by texture coordinate (between 0 to 1)
  50. * @memberOf SpriteRenderable
  51. * @param {float} left - Sets the Left UV Coordinate.
  52. * @param {float} right - Sets the Right UV Coordinate.
  53. * @param {float} bottom - Sets the Bottom UV Coordinate.
  54. * @param {float} top - Sets the Top UV Coordinate.
  55. * @returns {void}
  56. */
  57. SpriteRenderable.prototype.setElementUVCoordinate = function (left, right, bottom, top) {
  58. this.mTexLeft = left;
  59. this.mTexRight = right;
  60. this.mTexBottom = bottom;
  61. this.mTexTop = top;
  62. this._setTexInfo();
  63. };
  64. /**
  65. * specify element region by pixel positions (between 0 to image resolutions)
  66. * @memberOf SpriteRenderable
  67. * @param {float} left - Sets the Left pixel position.
  68. * @param {float} right - Sets the Right pixel position.
  69. * @param {float} bottom - Sets the Bottom pixel position.
  70. * @param {float} top - Sets the Top pixel position.
  71. * @returns {void}
  72. */
  73. SpriteRenderable.prototype.setElementPixelPositions = function (left, right, bottom, top) {
  74. var imageW = this.mTextureInfo.mWidth;
  75. var imageH = this.mTextureInfo.mHeight;
  76. this.mTexLeft = left / imageW;
  77. this.mTexRight = right / imageW;
  78. this.mTexBottom = bottom / imageH;
  79. this.mTexTop = top / imageH;
  80. this._setTexInfo();
  81. };
  82. /**
  83. * Returns a UV Coordinate Array.
  84. * @memberOf SpriteRenderable
  85. * @returns {float[]} UV Voordinate Array
  86. */
  87. SpriteRenderable.prototype.getElementUVCoordinateArray = function () {
  88. return [
  89. this.mTexRight, this.mTexTop, // x,y of top-right
  90. this.mTexLeft, this.mTexTop,
  91. this.mTexRight, this.mTexBottom,
  92. this.mTexLeft, this.mTexBottom
  93. ];
  94. };
  95. /**
  96. * Draws the SpriteRenderable to the screen in the aCamera viewport.
  97. * @memberOf SpriteRenderable
  98. * @param {Camera} aCamera - drawing Camera of the SpriteRenderable.
  99. * @returns {void}
  100. */
  101. SpriteRenderable.prototype.draw = function (aCamera) {
  102. // set the current texture coordinate
  103. //
  104. // activate the texture
  105. this.mShader.setTextureCoordinate(this.getElementUVCoordinateArray());
  106. TextureRenderable.prototype.draw.call(this, aCamera);
  107. };
  108. //--- end of Public Methods
  109. //
  110. //</editor-fold>