LabelTTFWebGL.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2011-2012 cocos2d-x.org
  4. Copyright (c) 2013-2014 Chukong Technologies Inc.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. cc._tmp.WebGLLabelTTF = function () {
  23. var _p = cc.LabelTTF.prototype;
  24. _p.setColor = cc.Sprite.prototype.setColor;
  25. _p._setColorsString = function () {
  26. this._needUpdateTexture = true;
  27. var locStrokeColor = this._strokeColor, locFontFillColor = this._textFillColor;
  28. this._shadowColorStr = "rgba(128,128,128," + this._shadowOpacity + ")";
  29. this._fillColorStr = "rgba(" + (0 | locFontFillColor.r) + "," + (0 | locFontFillColor.g) + "," + (0 | locFontFillColor.b) + ", 1)";
  30. this._strokeColorStr = "rgba(" + (0 | locStrokeColor.r) + "," + (0 | locStrokeColor.g) + "," + (0 | locStrokeColor.b) + ", 1)";
  31. };
  32. _p.updateDisplayedColor = cc.Sprite.prototype.updateDisplayedColor;
  33. _p.setOpacity = cc.Sprite.prototype.setOpacity;
  34. _p.updateDisplayedOpacity = cc.Sprite.prototype.updateDisplayedOpacity;
  35. _p.initWithStringAndTextDefinition = function (text, textDefinition) {
  36. if (!cc.Sprite.prototype.init.call(this))
  37. return false;
  38. // shader program
  39. this.shaderProgram = cc.shaderCache.programForKey(cc.LabelTTF._SHADER_PROGRAM);
  40. // prepare everything needed to render the label
  41. this._updateWithTextDefinition(textDefinition, false);
  42. // set the string
  43. this.string = text;
  44. return true;
  45. };
  46. _p.setFontFillColor = function (tintColor) {
  47. var locTextFillColor = this._textFillColor;
  48. if (locTextFillColor.r != tintColor.r || locTextFillColor.g != tintColor.g || locTextFillColor.b != tintColor.b) {
  49. locTextFillColor.r = tintColor.r;
  50. locTextFillColor.g = tintColor.g;
  51. locTextFillColor.b = tintColor.b;
  52. this._setColorsString();
  53. this._needUpdateTexture = true;
  54. }
  55. };
  56. _p.draw = function (ctx) {
  57. if (!this._string || this._string == "")
  58. return;
  59. var gl = ctx || cc._renderContext, locTexture = this._texture;
  60. if (locTexture && locTexture._isLoaded) {
  61. this._shaderProgram.use();
  62. this._shaderProgram.setUniformForModelViewAndProjectionMatrixWithMat4();
  63. cc.glBlendFunc(this._blendFunc.src, this._blendFunc.dst);
  64. cc.glBindTexture2D(locTexture);
  65. cc.glEnableVertexAttribs(cc.VERTEX_ATTRIB_FLAG_POS_COLOR_TEX);
  66. gl.bindBuffer(gl.ARRAY_BUFFER, this._quadWebBuffer);
  67. if (this._quadDirty) {
  68. gl.bufferData(gl.ARRAY_BUFFER, this._quad.arrayBuffer, gl.STATIC_DRAW);
  69. this._quadDirty = false;
  70. }
  71. gl.vertexAttribPointer(cc.VERTEX_ATTRIB_POSITION, 3, gl.FLOAT, false, 24, 0);
  72. gl.vertexAttribPointer(cc.VERTEX_ATTRIB_TEX_COORDS, 2, gl.FLOAT, false, 24, 16);
  73. gl.vertexAttribPointer(cc.VERTEX_ATTRIB_COLOR, 4, gl.UNSIGNED_BYTE, true, 24, 12);
  74. gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
  75. }
  76. if (cc.SPRITE_DEBUG_DRAW === 1) {
  77. // draw bounding box
  78. var locQuad = this._quad;
  79. var verticesG1 = [
  80. cc.p(locQuad.tl.vertices.x, locQuad.tl.vertices.y),
  81. cc.p(locQuad.bl.vertices.x, locQuad.bl.vertices.y),
  82. cc.p(locQuad.br.vertices.x, locQuad.br.vertices.y),
  83. cc.p(locQuad.tr.vertices.x, locQuad.tr.vertices.y)
  84. ];
  85. cc._drawingUtil.drawPoly(verticesG1, 4, true);
  86. } else if (cc.SPRITE_DEBUG_DRAW === 2) {
  87. // draw texture box
  88. var drawSizeG2 = this.getTextureRect();
  89. var offsetPixG2X = this.offsetX, offsetPixG2Y = this.offsetY;
  90. var verticesG2 = [cc.p(offsetPixG2X, offsetPixG2Y), cc.p(offsetPixG2X + drawSizeG2.width, offsetPixG2Y),
  91. cc.p(offsetPixG2X + drawSizeG2.width, offsetPixG2Y + drawSizeG2.height), cc.p(offsetPixG2X, offsetPixG2Y + drawSizeG2.height)];
  92. cc._drawingUtil.drawPoly(verticesG2, 4, true);
  93. } // CC_SPRITE_DEBUG_DRAW
  94. cc.g_NumberOfDraws++;
  95. };
  96. //TODO: cc.Sprite.prototype._setTextureRectForWebGL
  97. _p.setTextureRect = cc.Sprite.prototype.setTextureRect;
  98. };