Number bar

Introduction

A container with an icon, slider, 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 number bar object
    var numberBar = scene.rexUI.add.numberBar(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 number bar object
    var numberBar = scene.rexUI.add.numberBar(config);
    

Import class

  • Install rex plugins from npm
    npm i phaser3-rex-plugins
    
  • Import class
    import { NumberBar } from "phaser3-rex-plugins/templates/ui/ui-components.js";
    
  • Add number bar object
    var sizer = new NumberBar(scene, config);
    sscene.add.existing(sizer);
    

Add number bar object

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

    background: backgroundGameObject,
    icon: iconGameObject,
    iconMask: false,
    slider: {
        background: sliderBackgroundGameObject,
        track: sliderTrackGameObject,
        indicator: sliderIndicatorGameObject,
        thumb: sliderThumbGameObject,
        input: 'none',
        gap: undefined,        
        easeValue: {
            duration: 0,
            ease: 'Linear'
        },
    }
    text: textGameObject,

    valuechangeCallback: function(newValue, oldValue, numberBar) {
        // numberBar.text = Math.round(Phaser.Math.Linear(0, 100, newValue));
    }

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

        icon: 0,
        slider: 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.
  • background : Game object of background, optional. This background game object will be resized to fit the size of numberBar.
  • icon : Game object of icon, optional.
  • iconMask : Set true to add a circle mask on icon game object.
  • slider : Slider game object which composed of
  • slider.width : Fixed width of slider, optional. Width of slider will be extended if this value is not set.
  • slider.background : Game object of slider background, optional.
  • slider.track : Game object of slider track.
  • slider.indicator : Game object of slider indicator, optional.
  • slider.thumb : Game object of slider thumb, optional.
  • slider.input :
    • 'drag' : Control slider by dragging thumb game object.
    • 'click' : Control slider by touching track game object.
    • 'none' : Disable sider controlling. Default setting.
  • slider.gap : Snap a value to nearest grid slice, using rounding.
    • undefined : Disable this feature.
  • slider.easeValue : Easing value when input is 'click'.
    • slider.easeValue.duration : Duration of value easing, default is 0 (no easing).
    • slider.easeValue.ease : Ease function, default is 'Linear'.
  • text : Game object of text, optional.
  • 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.slider : Space between slider game object and text game object.
  • valuechangeCallback : callback function when value changed.
  • name : Set name of this numberBar.

Custom class

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

Layout children

Arrange position of all elements.

numberBar.layout();

See also - dirty

Get element

  • Get element
  • Background game object
    var background = numberBar.getElement("background");
    
  • Icon game object
    var icon = numberBar.getElement("icon");
    
  • Slider game object
    • Slider background
      var sliderBackground = numberBar.getElement("slider.background");
      
    • Slider track
      var sliderTrack = numberBar.getElement("slider.track");
      
    • Slider indicator
      var sliderIndicator = numberBar.getElement("slider.indicator");
      
    • Slider thumb
      var sliderThumb = numberBar.getElement("slider.thumb");
      
  • Text game object
    var icon = numberBar.getElement("text");
    
  • Get by name
    var gameObject = numberBar.getElement("#" + name);
    // var gameObject = numberBar.getElement('#' + name, recursive);
    
    or
    var gameObject = numberBar.getByName("#" + name);
    // var gameObject = numberBar.getByName('#' + name, recursive);
    
  • recursive : Set true to search all children recursively.

Value

Change value will also change the position of slider thumb and width of slider indicator.

  • Get value
    var value = numberBar.getValue(min, max); // value : min ~ max
    
    or
    var value = numberBar.getValue(); // value: 0 ~ 1
    
    or
    var value = numberBar.value; // value: 0 ~ 1
    
  • Set value
    numberBar.setValue(value, min, max); // value: min ~ max
    
    or
    numberBar.setValue(value); // value: 0 ~ 1
    
    or
    numberBar.value = value; // value: 0 ~ 1
    
  • Increase value
    numberBar.addValue(inc, min, max); // inc: min ~ max
    
    or
    numberBar.addValue(inc); // inc: 0 ~ 1
    
    or
    numberBar.value += inc; // inc: 0 ~ 1
    

Ease value

  • Ease value to
    numberBar.easeValueTo(value, min, max);  // value: min ~ max
    
    or
    numberBar.easeValueTo(value);  // value: 0 ~ 1
    
  • Set ease duration
    numberBar.getElement('slider').setEaseValueDuration(duration);
    
  • Set ease function
    numberBar.getElement('slider').setEaseValueFunction(ease);
    

Text

  • Get text string
    var s = numberBar.text;
    
  • Set text string
    numberBar.setText(s);
    
    or
    numberBar.text = s;
    

Other properties

See sizer object, base sizer object.

Events

  • On value changed
    numberBar.on(
      "valuechange",
      function (newValue, oldValue, numberBar) {
        // numberBar.text = Math.round(Phaser.Math.Linear(0, 100, newValue));
      },
      scope
    );