arthuro555

GDevelop Blurry Text: RuntimeText Pixi Renderer Adapted

Jun 10th, 2020
1,268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. gdjs.TextRuntimeObjectPixiWrapper = function(text, object) {
  2.     PIXI.Container.apply(this);
  3.     this._object = object;
  4.     this.text = this.addChild(text);
  5.     this.text.style.fill = 0;
  6.     this.text.anchor.set(0.5, 0.5);
  7. }
  8.  
  9. gdjs.TextRuntimeObjectPixiWrapper.prototype = Object.create( PIXI.Container.prototype );
  10.  
  11. gdjs.TextRuntimeObjectPixiWrapper.prototype.render = function(renderer, renderFunc) {
  12.     const worldTransform = this.transform.worldTransform;
  13.     const worldScale = Math.max(worldTransform.a, worldTransform.d);
  14.  
  15.     this.scale.set(1 / worldScale);
  16.  
  17.     this.text.style.fontSize = `${this._object._characterSize * worldScale}px`;
  18.     this.updateTransform();
  19.  
  20.     renderFunc.bind(this, renderer);
  21.  
  22.     // Cleanup
  23.     this.scale.set(1);
  24.     this.updateTransform();
  25. }
  26.  
  27. gdjs.TextRuntimeObjectPixiWrapper.prototype.renderCanvas = function(renderer) {
  28.     this.render(renderer, PIXI.Container.prototype.renderCanvas);
  29. }
  30.  
  31. gdjs.TextRuntimeObjectPixiWrapper.prototype.renderWebGL = function(renderer) {
  32.     this.render(renderer, PIXI.Container.prototype.renderWebGL);
  33. }
  34.  
  35. gdjs.TextRuntimeObjectPixiRenderer = function(runtimeObject, runtimeScene)
  36. {
  37.     this._object = runtimeObject;
  38.     this._fontManager = runtimeScene.getGame().getFontManager();
  39.  
  40.     if ( this._text === undefined ) this._text = new gdjs.TextRuntimeObjectPixiWrapper(new PIXI.Text(" ", {align:"left"}), runtimeObject);
  41.     runtimeScene.getLayer("").getRenderer().addRendererObject(this._text, runtimeObject.getZOrder());
  42.  
  43.     this.getRendererObject().text = runtimeObject._str.length === 0 ? " " : runtimeObject._str;
  44.     this._justCreated = true; //Work around a PIXI.js bug. See updateTime method.
  45.     this.updateStyle();
  46.     this.updatePosition();
  47. };
  48.  
  49. gdjs.TextRuntimeObjectRenderer = gdjs.TextRuntimeObjectPixiRenderer; //Register the class to let the engine use it.
  50.  
  51. gdjs.TextRuntimeObjectPixiRenderer.prototype.getRendererObject = function() {
  52.     return this._text.text;
  53. };
  54.  
  55. gdjs.TextRuntimeObjectPixiRenderer.prototype.ensureUpToDate = function() {
  56.     if (this._justCreated) { //Work around a PIXI.js bug:
  57.         this.getRendererObject().updateText();
  58.         this.updatePosition(); //Width seems not to be correct when text is not rendered yet.
  59.         this._justCreated = false;
  60.     }
  61. };
  62.  
  63. gdjs.TextRuntimeObjectPixiRenderer.prototype.updateStyle = function() {
  64.     var fontName = "\"" + this._fontManager.getFontFamily(this._object._fontName) + "\"";
  65.  
  66.     var style = this.getRendererObject().style;
  67.     style.fontStyle = this._object._italic ? 'italic' : 'normal';
  68.     style.fontWeight = this._object._bold ? 'bold' : 'normal';
  69.     style.fontFamily = fontName;
  70.  
  71.     if (this._object._useGradient){
  72.         style.fill = this._getGradientHex();
  73.     } else {
  74.         style.fill = this._getColorHex();
  75.     }
  76.  
  77.     if (this._object._gradientType === 'LINEAR_VERTICAL'){
  78.         style.fillGradientType = PIXI.TEXT_GRADIENT.LINEAR_VERTICAL;
  79.     } else {
  80.         style.fillGradientType = PIXI.TEXT_GRADIENT.LINEAR_HORIZONTAL;
  81.     }
  82.  
  83.     style.align = this._object._textAlign;
  84.     style.wordWrap = this._object._wrapping;
  85.     style.wordWrapWidth = this._object._wrappingWidth;
  86.     style.breakWords = true;
  87.     style.stroke = gdjs.rgbToHexNumber(
  88.         this._object._outlineColor[0],
  89.         this._object._outlineColor[1],
  90.         this._object._outlineColor[2]
  91.     );
  92.     style.strokeThickness = this._object._outlineThickness;
  93.     style.dropShadow = this._object._shadow;
  94.     style.dropShadowColor = gdjs.rgbToHexNumber(
  95.         this._object._shadowColor[0],
  96.         this._object._shadowColor[1],
  97.         this._object._shadowColor[2]
  98.     );
  99.     style.dropShadowBlur = this._object._shadowBlur;
  100.     style.dropShadowAngle = this._object._shadowAngle;
  101.     style.dropShadowDistance = this._object._shadowDistance;
  102.     style.padding = this._object._padding;
  103.     // Prevent spikey outlines by adding a miter limit
  104.     style.miterLimit = 3;
  105.  
  106.     this.updatePosition();
  107.  
  108.     // Manually ask the PIXI object to re-render as we changed a style property
  109.     // see http://www.html5gamedevs.com/topic/16924-change-text-style-post-render/
  110.     this.getRendererObject().dirty = true;
  111. };
  112.  
  113. gdjs.TextRuntimeObjectPixiRenderer.prototype.updatePosition = function() {
  114.     this.getRendererObject().position.x = this._object.x+this.getRendererObject().width/2;
  115.     this.getRendererObject().position.y = this._object.y+this.getRendererObject().height/2;
  116. };
  117.  
  118. gdjs.TextRuntimeObjectPixiRenderer.prototype.updateAngle = function() {
  119.     this.getRendererObject().rotation = gdjs.toRad(this._object.angle);
  120. };
  121.  
  122. gdjs.TextRuntimeObjectPixiRenderer.prototype.updateOpacity = function() {
  123.     this.getRendererObject().alpha = this._object.opacity / 255;
  124. };
  125.  
  126. gdjs.TextRuntimeObjectPixiRenderer.prototype.updateString = function() {
  127.     this.getRendererObject().text = this._object._str.length === 0 ? " " : this._object._str;
  128.     this.getRendererObject().updateText(); //Work around a PIXI.js bug.
  129. };
  130.  
  131. gdjs.TextRuntimeObjectPixiRenderer.prototype.getWidth = function() {
  132.     return this.getRendererObject().width;
  133. };
  134.  
  135. gdjs.TextRuntimeObjectPixiRenderer.prototype.getHeight = function() {
  136.     return this.getRendererObject().height;
  137. };
  138.  
  139. gdjs.TextRuntimeObjectPixiRenderer.prototype._getColorHex = function() {
  140.     return gdjs.rgbToHexNumber(
  141.         this._object._color[0],
  142.         this._object._color[1],
  143.         this._object._color[2]
  144.     );
  145. }
  146.  
  147. gdjs.TextRuntimeObjectPixiRenderer.prototype._getGradientHex = function() {
  148.     var gradient = [];
  149.     for (var colorIndex = 0; colorIndex < this._object._gradient.length; colorIndex++){
  150.         gradient.push(
  151.             '#' + gdjs.rgbToHex(
  152.                 this._object._gradient[colorIndex][0],
  153.                 this._object._gradient[colorIndex][1],
  154.                 this._object._gradient[colorIndex][2]
  155.             )
  156.         );
  157.     }
  158.     return gradient;
  159. }
  160. /**
  161.  * Get y-scale of the text.
  162.  */
  163. gdjs.TextRuntimeObjectPixiRenderer.prototype.getScaleX = function() {
  164.     return this.getRendererObject().scale.x;
  165. };
  166.  
  167. /**
  168.  * Get x-scale of the text.
  169.  */
  170. gdjs.TextRuntimeObjectPixiRenderer.prototype.getScaleY = function() {
  171.     return this.getRendererObject().scale.y;
  172. };
  173.  
  174. /**
  175.  * Set the text object scale.
  176.  * @param {number} newScale The new scale for the text object.
  177.  */
  178. gdjs.TextRuntimeObjectPixiRenderer.prototype.setScale = function(newScale) {
  179.     this.getRendererObject().scale.x = newScale;
  180.     this.getRendererObject().scale.y = newScale;
  181. };
  182.  
  183. /**
  184.  * Set the text object x-scale.
  185.  * @param {number} newScale The new x-scale for the text object.
  186.  */
  187. gdjs.TextRuntimeObjectPixiRenderer.prototype.setScaleX = function(newScale) {
  188.     this.getRendererObject().scale.x = newScale;
  189. };
  190.  
  191. /**
  192.  * Set the text object y-scale.
  193.  * @param {number} newScale The new y-scale for the text object.
  194.  */
  195. gdjs.TextRuntimeObjectPixiRenderer.prototype.setScaleY = function(newScale) {
  196.     this.getRendererObject().scale.y = newScale;
  197. };
Advertisement
Add Comment
Please, Sign In to add comment