Text box

Introduction

A container with an icon, (typing and paging) text, and background.

  • Author: Rex
  • Game object

Live demos

Usage

Sample code

Install plugin

Load minify file

  • Load plugin (minify file) in preload stage
    scene.load.scenePlugin('rexuiplugin', 'https://raw.githubusercontent.com/rexrainbow/phaser3-rex-notes/master/dist/rexuiplugin.min.js', 'rexUI', 'rexUI');
    
  • Add text-box object
    var textBox = scene.rexUI.add.textBox(config);
    

Import plugin

  • Install rex plugins from npm
    npm i phaser3-rex-plugins
    
  • Install plugin in configuration of game
    import UIPlugin from 'phaser3-rex-plugins/templates/ui/ui-plugin.js';
    var config = {
        // ...
        plugins: {
            scene: [{
                key: 'rexUI',
                plugin: UIPlugin,
                mapping: 'rexUI'
            },
            // ...
            ]
        }
        // ...
    };
    var game = new Phaser.Game(config);
    
  • Add text-box object
    var textBox = scene.rexUI.add.textBox(config);
    

Import class

  • Install rex plugins from npm
    npm i phaser3-rex-plugins
    
  • Import class
    import { TextBox } from 'phaser3-rex-plugins/templates/ui/ui-components.js';
    
  • Add text-box object
    var textBox = new TextBox(scene, config);
    sscene.add.existing(textBox);
    

Add textbox object

var textBox = scene.rexUI.add.textBox({
    // x: 0,
    // y: 0,
    // anchor: undefined,
    // width: undefined,
    // height: undefined,

    orientation: 0,

    background: backgroundGameObject,
    icon: iconGameObject,
    iconMask: false,
    text: textGameObject,
    action: actionGameObject,
    actionMask: false,

    space: {
        left: 0,
        right: 0,
        top: 0,
        bottom: 0,

        icon: 0,
        text: 0,
    },

    // name: '',
    // draggable: false
});
  • x, y : Position of this object, it is valid when this object is the top object.
  • anchor : See anchor.
    • left, right, centerX, x, top, bottom, centerY, y : Position based on visible window, which composed of
      • Percentage of visible width/height : 'p%', p: 0 ~ 100.
        • 'left'(=0%), 'center'(=50%), 'right'(=100%)
        • 'top'(=0%), 'center'(=50%), 'bottom'(=100%)
      • Offset : '+n', or '-n'.
  • width, height : Minimum width, minimum height.
  • orientation :
    • 'left-to-right', 'horizontal','h', 'x', or 0 : Put icon at left side, and text at right side.
    • 'top-to-bottom', 'vertical','v', 'y', or 1 : Put icon at top side, and text at bottom side.
  • background : Game object of background, optional. This background game object will be resized to fit the size of textBox.
  • icon : Game object of icon, optional.
  • iconMask : Set true to add a circle mask on icon game object.
  • text : Game object of text.
  • action : Game object of action icon, optional.
  • actionMask : Set true to add a circle mask on action icon game object.
  • space : Pads spaces
    • space.left, space.right, space.top, space.bottom : Space of bounds
    • space.icon : Space between icon game object and text game object.
    • space.text : Space between text game object and action icon game object.
  • name : Set name of this textBox.
  • draggable : Set true to drag to-most sizer.

Custom class

  • Define class
    class MyTextBox extends RexPlugins.UI.TextBox {
        constructor(scene, config) {
            super(scene, config);
            // ...
            scene.add.existing(this);
        }
        // ...
    }
    
  • Create instance
    var textBox = new MyTextBox(scene, config);
    

Typing

  • Start
    textBox.start(content, typingSpeed);
    
    • content : Content string.
    • speed : Typing speed in ms.
  • Stop
    textBox.stop();
    
  • Stop and show all text
    textBox.stop(true);
    
  • Pause
    textBox.pause();
    
  • Resume
    textBox.resume();
    
  • Is typing
    var isTyping = textBox.isTyping;
    

Page

  • Type next page
    textBox.typeNextPage();
    
  • Is last page
    var isLastPage = textBox.isLastPage;
    
  • Is first page
    var isLastPage = textBox.isFirstPage;
    
  • Current page index
    var pageIndex = textBox.pageIndex;
    
  • Number of pages
    var pageIndex = textBox.pageCount;
    

Events

  • On typing a character
    dialog.on('type', function() {
        // ...
    }, scope);
    
  • On Typing the last character of current page.
    dialog.on('pageend', function() {
        // ...
    }, scope);
    

Other properties

See label object, sizer object, base sizer object.