Skip to content

Image input label

Introduction

A container with a canvas icon, text, and background. Click icon to popup a (image) file chooser dialog, display selected image on canvas.

  • Author: Rex
  • Game object

Live demos

Usage

Sample code

Install plugin

Load minify file

  • Enable dom element in configuration of game
    var config = {
        parent: divId,
        dom: {
            createContainer: true
        },
        input: {
            mouse: {
                target: divId
            },
            touch: {
                target: divId
            },
        },
        // ...
    };
    var game = new Phaser.Game(config);
    
    • Set parent to divId
    • Set dom.createContainer to true.
  • 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 image-input label object
    var imageInputLabel = scene.rexUI.add.imageInputLabel(config);
    

Import plugin

  • Install rex plugins from npm
    npm i phaser3-rex-plugins
    
  • Install plugin, enable dom element in configuration of game
    import UIPlugin from 'phaser3-rex-plugins/templates/ui/ui-plugin.js';
    var config = {
        parent: divId,
        dom: {
            createContainer: true
        },
        input: {
            mouse: {
                target: divId
            },
            touch: {
                target: divId
            },
        },
        // ...
        plugins: {
            scene: [{
                key: 'rexUI',
                plugin: UIPlugin,
                mapping: 'rexUI'
            },
            // ...
            ]
        }
        // ...
    };
    var game = new Phaser.Game(config);
    
    • Set parent to divId
    • Set dom.createContainer to true.
  • Add image-input label object
    var imageInputLabel = scene.rexUI.add.imageInputLabel(config);
    

Import class

  • Install rex plugins from npm
    npm i phaser3-rex-plugins
    
  • Enable dom element in configuration of game
    var config = {
        parent: divId,
        dom: {
            createContainer: true
        },
        input: {
            mouse: {
                target: divId
            },
            touch: {
                target: divId
            },
        },
        // ...
    };
    var game = new Phaser.Game(config);
    
    • Set parent to divId
    • Set dom.createContainer to true.
  • Import class
    import { ImageInputLabel } from 'phaser3-rex-plugins/templates/ui/ui-components.js';
    
  • Add image-input label object
    var imageInputLabel = new ImageInputLabel(scene, config);
    scene.add.existing(imageInputLabel);
    

Add imageInputLabel object

var imageInputLabel = scene.rexUI.add.imageInputLabel({
    // x: 0,
    // y: 0,
    // anchor: undefined,
    // width: undefined,
    // height: undefined,
    // origin: 0.5
    // originX:
    // originY:

    orientation: 0,
    // rtl: false,

    background: backgroundGameObject,

    canvas: {
        // width: 128, 
        // height: 128,
        // fill: undefined,

        // key: undefined, 
        // frame: undefined,        
    },

    // scaleUpIcon: false,

    iconBackground: iconBackgroundGameObject,    
    // squareFitIcon: false,
    // iconSize: undefined, iconWidth: undefined, iconHeight: undefined,

    text: textGameObject,
    expandTextWidth: false,
    expandTextHeight: false,

    action: actionGameObject,
    // actionMask: false,
    // squareFitAction: false,
    // actionSize: undefined, actionWidth: undefined, actionHeight: undefined,

    align: undefined,

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

        icon: 0, 
        iconTop: 0, iconBottom: 0, iconLeft: 0, iconRight: 0,

        text: 0,
        actionTop: 0, actionBottom: 0, actionLeft: 0, actionRight: 0,
    },

    // name: '',
    // draggable: false,
    // sizerEvents: false,
    // enableLayer: false,

    // clickTarget: undefined,
    // domButton: true,
});
  • 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 : Set size (invoke onResizeCallback) based on visible window, which composed of
      • Percentage of visible width/height : 'p%', p: 0 ~ 100.
      • Padding : '+n', or '-n'.
    • onResizeCallback : A default resize callback will be assigned interanlly.
  • width, height : Minimum width, minimum height.
  • origin, originX, originY : Set origin of this sizer. Default value is (0.5, 0.5).
  • 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.
  • rtl :
    • true : Layout children from right to left.
    • false : Layout children from left to right. Default behavior.
  • background : Game object of background, optional. This background game object will be resized to fit the size of imageInputLabel.
  • canvas : Parameters of creating canvas game object.
    • canvas.width, canvas.height, canvas.fill : Create canvas with size (canvas.width x canvas.height), filling by color canvas.fill.
    • canvas.key, canvas.frame : Create canvas then paste texture canvas.key, canvas.frame.
  • iconBackground : Game object of iconBackground, optional.
    • icon game object will be created internally by imageBox with canvas.
  • squareFitIcon :
    • true : Resize icon size to square to fit imageInputLabel height/width.
      • Can't work wit expandTextWidth: true
    • false : Ignore this feature. Default behavior.
  • iconSize : Set display size of icon game object to iconSizexiconSize
  • iconWidth : Set display width of icon game object to iconWidth.
    • If iconHeight is undefined, set scaleY of icon game object to scaleX of icon game object, to keep size ratio.
  • iconHeight : Set display height of icon game object to iconHeight.
    • If iconWidth is undefined, set scaleX of icon game object to scaleY of icon game object, to keep size ratio.
  • scaleUpIcon :
    • true : Scale-up canvas game object if its size is smaller than icon (imageBox).
    • false : Keep current size when its size is smaller than icon (imageBox). Default behavior.
  • text : Game object of text, optional.
  • expandTextWidth :
    • false : Keep width of text to original size. Default behavior.
    • true : Expand width of text object. Will set display width by default.
  • expandTextHeight : Set true to expand height of text object.
  • action : Game object of action icon, optional.
  • actionMask : Set true to add a circle mask on action icon game object.
  • squareFitAction :
    • true : Resize action icon size to square to fit imageInputLabel height/width.
    • false : Ignore this feature. Default behavior.
  • actionSize : Set display size of action game object to actionSizexactionSize
  • actionWidth : Set display width of action game object to actionWidth.
    • If actionHeight is undefined, set scaleY of action game object to scaleX of action game object, to keep size ratio.
  • actionHeight : Set display height of action game object to actionHeight.
    • If actionWidth is undefined, set scaleX of action game object to scaleY of action game object, to keep size ratio.
  • align : Alignment of icon, text, action game objects.
    • undefined, or 'left', or 'top' : Align game objects at left, or top.
    • 'center' : Align game objects at center.
    • 'right', or 'bottom' : Align game objects at right, or bottom.
  • 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.iconTop, space.iconBottom, space.iconLeft, space.iconRight : Space around icon game object.
    • space.text : Space between text game object and action icon game object.
    • space.actionTop, space.actionBottom, space.actionLeft, space.actionRight : Space around action icon game object.
  • name : Set name of this game object.
  • draggable : Set true to drag top-most object.
  • sizerEvents : Set true to fire sizer events. Default value is false.
  • enableLayer :
    • false : Add child game objects into scene's display list. Default behavior.
    • true : Add child game objects into an internal layer game object. See also.
  • clickTarget : Click target to open file chooser dialog
    • A string, 'icon', 'text', 'action', 'background' : Click icon, text, action, background game object to open file chooser dialog.
    • undefined : Click this ImageInputLabel game object to open file chooser dialog. Default behavior.
    • null, false : No clickTarget. Open file chooser dialog manually under a touch event.
  • domButton :
    • true : Put DOM buttom above clickTarget, to receive click event.
    • false : Add click event at clickTarget.
      • This mode won't work at ios

Expand size of text

Expand width/height of text when expandTextWidth/expandTextHeight is true

To resize text object, text object should have resize method. For example

class MyText extends Phaser.GameObjects.Text {
    constructor(scene, x, y, text, style) {
        super(scene, x, y, text, style);
        scene.add.existing(this);
    }
    resize(width, height) {
        this.setFixedSize(width, height);
        return this;
    }
}

Or uses bbcode text object, tag text object, canvasInput object.

Custom class

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

Layout children

Arrange position of all elements.

imageInputLabel.layout();

See also - dirty

Open file chooser dialog

Failure

This method can't run at ios.

Note

Open a file chooser dialog under any touch event. i.e. User can't open file chooser dialog directly.

imageInputLabel.openPromise()
    .then(function(file){
    })
imageInputLabel.open()

Enable clicking-open

  • Enable
    imageInputLabel.setClickOpenEnable();
    // imageInputLabel.setClickOpenEnable(true);
    
  • Disable
    imageInputLabel.setClickOpenEnable(false)
    

Save texture

Save image on canvas into texture manager.

imageInputLabel.saveTexture(key);

Events

  • Select file
    imageInputLabel.on('select', function(file, imageInputLabel) {
    
    })
    

Get element

  • Get element
    • Background game object
      var background = imageInputLabel.getElement('background');
      
    • Canvas game object (a canvas game object created internally)
      var canvas = imageInputLabel.getElement('canvas');
      
    • Icon background game object
      var iconBackground = imageInputLabel.getElement('iconBackground');
      
    • Icon game object (an imageBox game object created internally)
      var imageBox = imageInputLabel.getElement('icon');
      
    • Text game object
      var textObject = imageInputLabel.getElement('text');
      
    • Action icon game object
      var action = imageInputLabel.getElement('action');
      
    • File chooser game object, if domButton is set to true
      var fileChooser = imageInputLabel.getElement('fileChooser');
      
  • Get by name
    var gameObject = imageInputLabel.getElement('#' + name);
    // var gameObject = imageInputLabel.getElement('#' + name, recursive);
    
    or
    var gameObject = imageInputLabel.getByName(name);
    // var gameObject = imageInputLabel.getByName(name, recursive);
    
    • recursive : Set true to search all children recursively.

Other properties

See label), sizer object, base sizer object, container-lite.