Slider

Introduction

A container with a track, indicator, thumb 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 slider object
    var slider = scene.rexUI.add.slider(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 slider object
    var slider = scene.rexUI.add.slider(config);
    

Import class

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

Add slider object

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

    orientation: 0,

    background: backgroundGameObject,
    track: trackGameObject,
    indicator: indicatorGameObject,
    thumb: thumbGameObject,

    input: 'drag',

    value: 0,
    gap: undefined,
    valuechangeCallback: function(newValue, oldValue, slider) {
    },

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

    enable: true,

    // 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 : Horizontal slider.
    • 'top-to-bottom', 'vertical','v', 'y', or 1 : Vertical slider.
  • background : Game object of background, optional. This background game object will be resized to fit the size of slider.
  • track : Game object of track, optional. This track game object will be resized to fit the size of slider, with space.
  • indicator : Game object of indicator, optional.
  • thumb : Game object of thumb, optional.
  • input :
    • 'drag', or 0 : Control slider by dragging thumb game object. Default setting.
    • 'click', or 1 : Control slider by touching track game object.
    • 'none', or -1 : Disable sider controlling.
  • value : Initial value (0 ~ 1).
  • gap : Snap a value to nearest grid slice, using rounding.
    • undefined : Disalbe this feature.
  • valuechangeCallback : callback function when value changed.
  • space : Pads spaces
    • space.left, space.right, space.top, space.bottom : Space of bounds.
  • enable : Set false to disable controlling.
  • name : Set name of this slider.

Custom class

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

Layout children

Arrange position of all elements.

slider.layout();

See also - dirty

Get element

  • Get element
    • Background game object
      var background = slider.getElement('background');
      
    • Track game object
      var track = slider.getElement('track');
      
    • Indicator game object
      var track = slider.getElement('indicator');
      
    • Thumb track game object
      var action = slider.getElement('thumb');
      
  • Get by name
    var gameObject = slider.getElement('#' + name);
    // var gameObject = slider.getElement('#' + name, recursive);
    
    or
    var gameObject = slider.getByName('#' + name);
    // var gameObject = slider.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
    

Other properties

See sizer object, base sizer object.

Events

  • On value changed
    slider.on('valuechange', function(newValue, oldValue, slider){
        //
    }, scope);