Source: Renderables/FontRenderable.js

  1. /*
  2. * File: FontRenderable.js
  3. */
  4. /*jslint node: true, vars: true */
  5. /*global gEngine: false, Transform: false, SpriteRenderable: false */
  6. /* find out more about jslint: http://www.jslint.com/help.html */
  7. // Constructor and object definition
  8. "use strict"; // Operate in Strict mode such that variables must be declared before used!
  9. /**
  10. * Default Constructor.<p>
  11. * Default Constructor creates an instance of FontRenderable.
  12. * @class FontRenderable
  13. * @param {String} aString - text to display
  14. * @returns {FontRenderable} a new instance of FontRenderable.
  15. */
  16. function FontRenderable(aString) {
  17. this.mFont = gEngine.DefaultResources.getDefaultFont();
  18. this.mOneChar = new SpriteRenderable(this.mFont + ".png");
  19. this.mXform = new Transform(); // transform that moves this object around
  20. this.mText = aString;
  21. }
  22. //<editor-fold desc="Public Methods">
  23. //**-----------------------------------------
  24. // Public methods
  25. //**-----------------------------------------
  26. /**
  27. * Draws the FontRenderable to the screen in the aCamera viewport.
  28. * @memberOf FontRenderable
  29. * @param {Camera} aCamera - Camera object to draw to.
  30. * @returns {void}
  31. */
  32. FontRenderable.prototype.draw = function (aCamera) {
  33. // we will draw the text string by calling to mOneChar for each of the
  34. // chars in the mText string.
  35. var widthOfOneChar = this.mXform.getWidth() / this.mText.length;
  36. var heightOfOneChar = this.mXform.getHeight();
  37. // this.mOneChar.getXform().setRotationInRad(this.mXform.getRotationInRad());
  38. var yPos = this.mXform.getYPos();
  39. // center position of the first char
  40. var xPos = this.mXform.getXPos() - (widthOfOneChar / 2) + (widthOfOneChar * 0.5);
  41. var charIndex, aChar, charInfo, xSize, ySize, xOffset, yOffset;
  42. for (charIndex = 0; charIndex < this.mText.length; charIndex++) {
  43. aChar = this.mText.charCodeAt(charIndex);
  44. charInfo = gEngine.Fonts.getCharInfo(this.mFont, aChar);
  45. // set the texture coordinate
  46. this.mOneChar.setElementUVCoordinate(charInfo.mTexCoordLeft, charInfo.mTexCoordRight,
  47. charInfo.mTexCoordBottom, charInfo.mTexCoordTop);
  48. // now the size of the char
  49. xSize = widthOfOneChar * charInfo.mCharWidth;
  50. ySize = heightOfOneChar * charInfo.mCharHeight;
  51. this.mOneChar.getXform().setSize(xSize, ySize);
  52. // how much to offset from the center
  53. xOffset = widthOfOneChar * charInfo.mCharWidthOffset * 0.5;
  54. yOffset = heightOfOneChar * charInfo.mCharHeightOffset * 0.5;
  55. this.mOneChar.getXform().setPosition(xPos - xOffset, yPos - yOffset);
  56. // allows for zPos to affect FontRenderables
  57. this.mOneChar.getXform().setZPos(this.mXform.getZPos());
  58. this.mOneChar.draw(aCamera);
  59. xPos += widthOfOneChar;
  60. }
  61. };
  62. /**
  63. * Returns the Renderable's Transform.
  64. * @memberOf FontRenderable
  65. * @returns {Transform} the return of the Renderable.
  66. */
  67. FontRenderable.prototype.getXform = function () { return this.mXform; };
  68. /**
  69. * Returns the text of the FontRenderable.
  70. * @memberOf FontRenderable
  71. * @returns {String} text of the FontRenderable.
  72. */
  73. FontRenderable.prototype.getText = function () { return this.mText; };
  74. /**
  75. * Set the text of the FontRenderable.
  76. * @memberOf FontRenderable
  77. * @param {String} t - text to set to the FontRenderable.
  78. * @returns {void}
  79. */
  80. FontRenderable.prototype.setText = function (t) {
  81. this.mText = t;
  82. this.setTextHeight(this.getXform().getHeight());
  83. };
  84. /**
  85. * Set the Text Height of the FontRenderable.
  86. * @memberOf FontRenderable
  87. * @param {float} h - height of the Renderable.
  88. * @returns {void}
  89. */
  90. FontRenderable.prototype.setTextHeight = function (h) {
  91. var charInfo = gEngine.Fonts.getCharInfo(this.mFont, "A".charCodeAt(0)); // this is for "A"
  92. var w = h * charInfo.mCharAspectRatio;
  93. this.getXform().setSize(w * this.mText.length, h);
  94. };
  95. /**
  96. * Get the size of a symbol in the FontRenderable
  97. * @memberof FontRenderable
  98. * @returns {vec2} The size of one symbol
  99. */
  100. FontRenderable.prototype.getSymbolSize = function() {
  101. var size = this.getXform().getSize();
  102. return vec2.fromValues(size[0] / this.mText.length, size[1]);
  103. };
  104. FontRenderable.prototype.getWidth = function() {
  105. var size = this.getXform().getSize();
  106. return size[0];
  107. };
  108. /**
  109. * Returns the FontRenderable's Font
  110. * @memberOf FontRenderable
  111. * @returns {String} the Font of the FontRenderable.
  112. */
  113. FontRenderable.prototype.getFont = function () { return this.mFont; };
  114. /**
  115. * Sets the Font of the FontRenderable.
  116. * @memberOf FontRenderable
  117. * @param {Font} f - font of the FontRenderable.
  118. * @returns {void}
  119. */
  120. FontRenderable.prototype.setFont = function (f) {
  121. this.mFont = f;
  122. this.mOneChar.setTexture(this.mFont + ".png");
  123. };
  124. /**
  125. * Sets the Color of the FontRenderable.
  126. * @memberOf FontRenderable
  127. * @param {float[]} color The desired Color of the FontRenderable.
  128. * @returns {void}
  129. */
  130. FontRenderable.prototype.setColor = function (c) { this.mOneChar.setColor(c); };
  131. /**
  132. * Gets the Color of the FontRenderable.
  133. * @memberOf FontRenderable
  134. * @returns {float[]} The color of the FontRenderable.
  135. */
  136. FontRenderable.prototype.getColor = function () { return this.mOneChar.getColor(); };
  137. /**
  138. * Update function called on Gameloop
  139. * @memberOf FontRenderable
  140. * @returns {void}
  141. */
  142. FontRenderable.prototype.update = function () {};
  143. //--- end of Public Methods
  144. //</editor-fold>