BBCode Text

Introduction

Drawing text with BBCode protocol.

  • Author: Rex
  • Game object

Live demos

Usage

Sample code

Install plugin

Load minify file

  • Load plugin (minify file) in preload stage
    scene.load.plugin('rexbbcodetextplugin', 'https://raw.githubusercontent.com/rexrainbow/phaser3-rex-notes/master/dist/rexbbcodetextplugin.min.js', true);
    
  • Add text object
    var txt = scene.add.rexBBCodeText(x, y, content, config);
    

Import plugin

  • Install rex plugins from npm
    npm i phaser3-rex-plugins
    
  • Install plugin in configuration of game
    import BBCodeTextPlugin from 'phaser3-rex-plugins/plugins/bbcodetext-plugin.js';
    var config = {
        // ...
        plugins: {
            global: [{
                key: 'rexBBCodeTextPlugin',
                plugin: BBCodeTextPlugin,
                start: true
            },
            // ...
            ]
        }
        // ...
    };
    var game = new Phaser.Game(config);
    
  • Add text object
    var txt = scene.add.rexBBCodeText(x, y, content, config);
    

Import class

  • Install rex plugins from npm
    npm i phaser3-rex-plugins
    
  • Import class
    import BBCodeText from 'phaser3-rex-plugins/plugins/bbcodetext.js';
    
  • Add text object
    var txt = new BBCodeText(scene, x, y, content, config);
    sscene.add.existing(txt);
    

BBCode

  • Bold : [b]text[/b]
  • Italic : [i]text[/i]
  • Color : [color=red]text[/color]
  • Size : [size=18]text[/size]
  • Stroke : [stroke]text[/stroke]
    • Stroke with color setting : [stroke=red]text[/stroke]
  • Shadow : [shadow]text[/shadow]
  • Underline : [u]text[/u]
    • Underline with color setting : [u=red]text[/u]
  • Image : [img=imgKey]
  • Hit area of words : [area=key]text[/area]

Add text object

var txt = scene.add.rexBBCodeText(x, y, '[b]h[/b]ello');
// var txt = scene.add.rexBBCodeText(x, y, '[b]h[/b]ello', { fontFamily: 'Arial', fontSize: 64, color: '#00ff00' });

Default style

{
    fontFamily: 'Courier',
    fontSize: '16px',
    fontStyle: '',
    backgroundColor: null,  // null, css string, or number
    backgroundColor2: null,  // null, css string, or number
    backgroundHorizontalGradient: true,
    backgroundStrokeColor: null,  // null, css string, or number
    backgroundStrokeLineWidth: 2,
    backgroundCornerRadius: 0,
    backgroundCornerIteration: null,    
    color: '#fff',  // null, css string, or number
    stroke: '#fff',  // null, css string, or number
    strokeThickness: 0,
    shadow: {
        offsetX: 0,
        offsetY: 0,
        color: '#000',  // css string, or number
        blur: 0,
        stroke: false,
        fill: false
    },
    underline: {
        color: '#000',  // css string, or number
        thickness: 0,
        offset: 0
    },
    // align: 'left',  // Equal to halign
    halign: 'left', // 'left'|'center'|'right'
    valign: 'top',  // 'top'|'center'|'bottom'
    padding: {
        left: 0,
        right: 0,
        top: 0,
        bottom: 0,
    },
    maxLines: 0,
    lineSpacing: 0,
    fixedWidth: 0,
    fixedHeight: 0,
    lineSpacing: 0,
    testString: '|MÉqgy',
    wrap: {
        mode: 'none'     // 0|'none'|1|'word'|2|'char'|'character'
        width: null
    },
    metrics: false
    // metrics: {
    //     ascent: 0,
    //     descent: 0,
    //     fontSize: 0
    // }
}

or

var txt = scene.add.rexBBCodeText({
    x: 0,
    y: 0,
    text: '',
    style: {
        fontSize: '64px',
        fontFamily: 'Arial',
        color: '#ffffff',
        align: 'center',
        backgroundColor: '#ff00ff'
    }
})

or

var txt = scene.make.rexBBCodeText({
    x: 0,
    y: 0,
    padding: {
        left: 0,
        right: 0,
        top: 0,
        bottom: 0
        //x: 32,    // 32px padding on the left/right
        //y: 16     // 16px padding on the top/bottom
    },
    text: 'Text\nGame Object\nCreated from config',
    style: {
        fontSize: '64px',
        fontFamily: 'Arial',
        color: '#ffffff',
        align: 'center',
        backgroundColor: '#ff00ff'
    },
    // origin: {x: 0.5, y: 0.5},
    add: true
});
  • Alignment
    • halign, or align : Horizontal alignment.
      • left, center, right
    • valign : Vertical alignment.
      • top, center, bottom

Custom class

  • Define class
    class MyText extends BBCodeText {
        constructor(scene, x, y, text, style) {
            super(scene, x, y, text, style);
            // ...
            scene.add.existing(this);
        }
        // ...
    
        // preUpdate(time, delta) {}
    }
    
    • scene.add.existing(gameObject) : Adds an existing Game Object to this Scene.
      • If the Game Object renders, it will be added to the Display List.
      • If it has a preUpdate method, it will be added to the Update List.
  • Create instance
    var txt = new MyText(scene, x, y, '[b]h[/b]ello');
    

Wrap

  • Wrap by word or character.
    var txt = scene.make.rexBBCodeText({
        x: 400,
        y: 100,
        text: 'The sky above the port was the color of television, tuned to a dead channel.',
        origin: { x: 0.5, y: 0.5 },
        style: {
            font: 'bold 25px Arial',
            fill: 'white',
            wrap: {
                mode: 0     // 0|'none'|1|'word'|2|'char'|'character'
                width: 0
            }
        }
    });
    
    • wrap.mode :
      • 0, or 'none' : No wrapping, default behavior.
      • 1, or 'word' : Word wrapping.
      • 2, or 'char', or 'character' : Character wrapping.
    • wrap.width : Maximun wrapping width of a line.
      • Wrap-fit : Set wrapping width to fixedWidth - padding.left - padding.right if fixedWidth > 0
  • Wrap mode
    • Get
      var mode = txt.style.wrapMode;
      
    • Set
      txt.setWrapMode(mode);
      
      • 0, or 'none' : No wrapping.
      • 1, or 'word' : Word wrapping.
      • 2, or 'char', or 'character' : Character wrapping.
  • Wrap width
    • Get
      var width = txt.style.wrapWidth;
      
    • Set
      txt.setWrapWidth(width);
      
      • width : Maximun wrapping width of a line.
        • Wrap-fit : Set wrapping width to fixedWidth - padding.left - padding.right if fixedWidth > 0

Content

  • Get source text
    var curContent = txt.text;
    
  • Get plain text
    var plainText = txt.getPlainText();
    
  • Set
    txt.setText('[b]h[/b]ello');
    // txt.text = '[b]h[/b]ello';
    

Set style

txt.setStyle(style);
txt.setFont(font);  // font: {fontFamily, fontSize, fontStyle}
txt.setFontFamily(family);
txt.setFontSize(size);
txt.setFontStyle(style);

Color

  • Text color
    • Get
      var color = txt.style.color;
      
    • Set
      txt.setColor(color);
      
      or
      txt.setFill(color);
      
      • color : null, css string, or number.
  • Stroke color, thickness
    • Get
      var color = txt.style.stroke;
      var thickness = txt.style.strokeThickness;
      
    • Set
      txt.setStroke(color, thickness);
      
      • color : null, css string, or number.
    • Clear
      txt.setStroke();
      
  • Underline color, thickness
    • Get
      var color = txt.style.underlineColor;
      var thickness = txt.style.underlineThickness;
      var offset = txt.style.underlineOffset;
      
    • Set
      txt.setUnderline(color, thickness, ofset);
      txt.setUnderlineColor(color);
      txt.setUnderlineThinkness(thickness);
      txt.setUnderlineOffset(ofset);
      
      • color : null, css string, or number.
  • Background
    • Color, or gradient color
      • Get
        var color = txt.style.backgroundColor;
        var color2 = txt.style.backgroundColor2;
        var isHorizontalGradient = txt.style.backgroundHorizontalGradient;
        
      • Set
        txt.setBackgroundColor(color);
        // txt.setBackgroundColor(color, color2, isHorizontalGradient);
        
        • color, color2 : null, css string, or number.
    • Stroke color
      • Get
        var color = txt.style.backgroundStrokeColor;
        var lineWidth = txt.style.backgroundStrokeLineWidth;
        
      • Set
        txt.setBackgroundStrokeColor(color, lineWidth);
        
        • color : null, css string, or number.
    • Round rectangle
      • Get
        var radius = txt.style.backgroundCornerRadius;
        var iteration = txt.style.backgroundCornerIteration;
        
      • Set
        txt.setBackgroundCornerRadius(radius);
        // txt.setBackgroundCornerRadius(radius, iteration);
        
        • iteration :
          • undefined : Round rectangle
          • 0 : Octagon
  • Shadow
    • Get
      var color = txt.style.shadowColor;
      var offsetX = txt.style.shadowOffsetX;
      var offsetY = txt.style.shadowOffsetY;
      var blur = txt.style.shadowBlur;
      var stroke = txt.style.shadowStroke;
      var enabled = txt.style.shadowFill;
      
    • Set
      txt.setShadow(x, y, color, blur, shadowStroke, shadowFill);
      txt.setShadowOffset(x, y);
      txt.setShadowColor(color);
      txt.setShadowBlur(blur);
      txt.setShadowStroke(enabled);
      txt.setShadowFill(enabled);
      
      • color : null, css string, or number.

Image

  • Uses texture key as image key by default.
  • Add image render information
    txt.addImage(imgKey, {
        key: textureKey,
        frame: frameName,
        width: undefined,
        height: undefined,
        y: 0,
        left: 0,
        right: 0
    });
    
    • imgKey : Image key used in text content, i.e. [img=imgKey].
    • key : Texture key.
    • frame : Frame name.
    • width : Render width, set undefined to use the cut width of frame.
    • height : Render height, set undefined to use the cut height of frame.
    • y : Extra offset y.
    • left : Left padding space.
    • Right : Right padding space.
  • Add some image render informations
    txt.addImage(data);
    
    • data : {imgKey, config}

Hit area of words

Size of hit-area is word-width x line-height, or image-width x line-height.

Hitting events

  • Pointer down
    txt.on('areadown', function(key, pointer, localX, localY){
    
    }, scope)
    
    or
    txt.on('areadown-' + key, function(pointer, localX, localY){
    
    }, scope)
    
  • Pointer up
    txt.on('areaup', function(key, pointer, localX, localY){
    
    }, scope)
    
    or
    txt.on('areaup-' + key, function(pointer, localX, localY){
    
    }, scope)
    

Draw hit-areas

txt.drawAreaBounds(graphics, color);

Line spacing

This value is added to the height of the font when calculating the overall line height.

  • Get
    var lineSpacing = txt.style.lineSpacing;
    
  • Set
    txt.setLineSpacing(value);
    

Padding

  • Get
    var left = txt.padding.left;
    var top = txt.padding.top;
    var right = txt.padding.right;
    var bottom = txt.padding.bottom;
    
  • Set
    txt.setPadding(left, top, right, bottom);
    // txt.setPadding(padding); // padding: {left, top, right, bottom}
    

Max lines

  • Get
    var maxLines = txt.style.maxLines;
    
  • Set
    txt.setMaxLines(max);
    

Fixed size

  • Get
    var width = txt.style.fixedWidth;
    var height = txt.style.fixedHeight;
    
  • Set
    txt.setFixedSize(width, height);