Toggle switch
Introduction¶
Toggle-switch input.
- Author: Rex
- Game object
Live demos¶
Usage¶
Install plugin¶
Load minify file¶
- Load plugin (minify file) in preload stage
scene.load.plugin('rextoggleswitchplugin', 'https://raw.githubusercontent.com/rexrainbow/ phaser3-rex-notes/master/dist/rextoggleswitchplugin.min.js', true);
- Add toggle-switch input
var toggleSwitch = scene.add.rexToggleSwitch(x, y, width, height, color, config);
- Add toggle-switch shape (without click input)
var toggleSwitch = scene.add.rexToggleSwitchShape(x, y, width, height, color, config);
Import plugin¶
- Install rex plugins from npm
npm i phaser3-rex-plugins
- Install plugin in configuration of game
import ToggleSwitchPlugin from 'phaser3-rex-plugins/plugins/toggleswitch-plugin.js'; var config = { // ... plugins: { global: [{ key: 'rexToggleSwitchPlugin', plugin: ToggleSwitchPlugin, start: true }, // ... ] } // ... }; var game = new Phaser.Game(config);
- Add toggle-switch input
var toggleSwitch = scene.add.rexToggleSwitch(x, y, width, height, color, config);
- Add toggle-switch shape (without click input)
var toggleSwitch = scene.add.rexToggleSwitchShape(x, y, width, height, color, config);
Import class¶
- Install rex plugins from npm
npm i phaser3-rex-plugins
- Import class
import ToggleSwitch from 'phaser3-rex-plugins/plugins/toggleswitch.js';
- Add toggle-switch input
var toggleswitch = new ToggleSwitch(scene, x, y, width, height, color, config); scene.add.existing(toggleSwitch);
- Add toggle-switch shape (without click input)
// import ToggleSwitchShape from 'phaser3-rex-plugins/plugins/toggleswitchshape.js'; var toggleSwitch = new ToggleSwitchShape(scene, x, y, width, height, color, config); scene.add.existing(toggleSwitch);
Create toggle-switch input¶
var toggleSwitch = scene.add.rexToggleSwitch(x, y, width, height, color, config);
or
var toggleSwitch = scene.add.rexToggleSwitch({
x: 0,
y: 0,
width: undefined,
height: undefined,
color: 0x005cb2,
trackFillAlpha: 1,
falseValueTrackColor: undefined,
falseValueTrackFillAlpha: 1,
thumbColor: 0xffffff,
thumbAlpha: 1,
trackWidth: 0.9,
trackHeight: 0.5,
trackCornerRadius: (trackHeight * 0.5),
thumbHeight: (trackHeight * 0.9),
thumbWidth: (thumbHeight),
thumbCornerRadius: (thumbHeight * 0.5),
thumbLeft: 0.3,
thumbRight: (1 - thumbLeft),
rtl: false,
animationDuration: 150,
value: false,
click: undefined,
// click: {
// mode: 1, // 0|'press'|1|'release'
// clickInterval: 100 // ms
// threshold: undefined
// },
readOnly: false,
});
width
,height
: Size of toggleswitch.- Track fill style
color
,trackFillAlpha
: Track color and alpha when value istrue
.falseValueTrackColor
,falseValueTrackFillAlpha
: Track color and alpha when value isfalse
.- Default value of
falseValueTrackColor
is the grayscale ofcolor
.
- Default value of
- Thumb fill style
thumbColor
,thumbAlpha
: Thumb color and alpha
- Track size
trackWidth
: Width ration of track. Default value is0.9
.thumbWidth
: Height ratio of track. Default value is0.5
.trackCornerRadius
: Ratio of track corner. Default value is half oftrackHeight
.
- Thumb size
thumbWidth
: Width ration of thumb. Default value is equal tothumbHeight
.thumbHeight
: Height ratio of thumb. Default valuetrackHeight * 0.5
.thumbCornerRadius
: Ratio of thumb corner. Default value is half ofthumbHeight
.
- Thumb position
thumbLeft
: Thumb position if value isfalse
. Default value is0.3
.thumbRight
: Thumb position if value istrue
. Default value is1 - thumbLeft
.rtl
: Moving direction of thumb when when value changes fromfalse
totrue
.false
: Thumb moves from left to right. Default behavior.true
: Thumb moves from right to left.
animationDuration
: Duration of drawing path of checker.value
: Initial value.click
: Configuration of click inputclick.mode
:'pointerdown'
,'press'
, or0
: Fire 'click' event when touch pressed.'pointerup'
,'release'
, or1
: Fire 'click' event when touch released after pressed.
click.clickInterval
: Interval between 2 'click' events, in ms.click.threshold
: Cancel clicking detecting when dragging distance is larger then this threshold.undefined
: Ignore this feature. Default behavior.
readOnly
: Setture
to disable input.
Custom class¶
- Define class
class MyToggleSwitch extends RexPlugins.GameObjects.ToggleSwitch { constructor(scene, x, y, width, height, color, config) { super(scene, x, y, width, height, color, config); // ... 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 toggleSwitch = new MyToggleSwitch(scene, x, y, width, height, color, config);
Value¶
- Get
var value = toggleSwitch.value; // var value = toggleSwitch.value;
- Set
or
toggleSwitch.setValue(value); // toggleSwitch.setValue(value, duration);
toggleSwitch.value = value;
- Toggle
or
toggleSwitch.toggleValue(); // toggleSwitch.toggleValue(duration);
toggleSwitch.value = !toggleSwitch.value; // toggleSwitch.value = !toggleSwitch.value;
Read only¶
- Get
var readOnly = toggleSwitch.readOnly;
- Set
or
toggleSwitch.setReadOnly(); // toggleSwitch.setReadOnly(true);
toggleSwitch.readOnly = true;
Track fill style¶
- Get
var color = toggleSwitch.trackFillColor; var alpha = toggleSwitch.trackFillAlpha;
var color = toggleSwitch.falseValueTrackColor; var alpha = toggleSwitch.falseValueTrackFillAlpha;
- Set
toggleSwitch.setTrackFillStyle(color, alpha); // toggleSwitch.trackFillColor = color; // toggleSwitch.trackFillAlpha = alpha;
toggleSwitch.setFalseValueTrackFillStyle(color, alpha); // toggleSwitch.falseValueTrackColor = color; // toggleSwitch.falseValueTrackFillAlpha = alpha;
Thumb fill style¶
- Get
var color = toggleSwitch.thumbColor; var alpha = toggleSwitch.thumbAlpha;
- Set
toggleSwitch.setThumbStyle(color, alpha); // toggleSwitch.thumbColor = color; // toggleSwitch.thumbAlpha = alpha;
Toggle animation¶
- Duration
- Get
var duration = toggleSwitch.toggleAnimProgress;
- Set
toggleSwitch.setToggleAnimationDuration(duration); toggleSwitch.toggleAnimProgress = duration;
- Get
Size¶
- Get
var width = toggleSwitch.width; var height = toggleSwitch.height;
- Set
or
toggleSwitch.setSize(width, height);
toggleSwitch.width = width; toggleSwitch.height = height;
Display size¶
- Get
var width = toggleSwitch.displayWidth; var height = toggleSwitch.displayHeight;
- Set
or
toggleSwitch.setDisplaySize(width, height);
toggleSwitch.displayWidth = width; toggleSwitch.displayHeight = height;
Track size ratio¶
- Get
var trackWidth = toggleSwitch.trackWidth; var trackHeight = toggleSwitch.trackHeight;
trackWidth
,trackHeight
: Size ratio of track
- Set
toggleSwitch.setTrackSize(trackWidth, trackHeight); // toggleSwitch.trackWidth = trackWidth; // toggleSwitch.trackHeight = trackHeight;
Track corner ratio¶
- Get
var trackRadius = toggleSwitch.trackRadius;
trackRadius
: Corner ratio of track
- Set
toggleSwitch.setTrackRadius(trackRadius); // toggleSwitch.trackRadius = trackRadius;
Thumb size ratio¶
- Get
var thumbWidth = toggleSwitch.thumbWidth; var thumbHeight = toggleSwitch.thumbHeight;
trackWidth
,trackHeight
: Size ratio of thumb
- Set
toggleSwitch.setThumbSize(thumbWidth, thumbHeight); // toggleSwitch.thumbWidth = thumbWidth; // toggleSwitch.thumbHeight = thumbHeight;
Thumb corner ratio¶
- Get
var thumbRadius = toggleSwitch.thumbRadius;
thumbRadius
: Corner ratio of track
- Set
toggleSwitch.setThumbRadius(thumbRadius); // toggleSwitch.thumbRadius = thumbRadius;
Thumb position ratio¶
- Get
var thumbLeft = toggleSwitch.thumbLeftX; var thumbRight = toggleSwitch.thumbRightX;
var rtl = toggleSwitch.rtl;
- Set
toggleSwitch.setThumbPosition(thumbLeft, thumbRight); // toggleSwitch.thumbLeftX = thumbLeft; // toggleSwitch.thumbRightX = thumbRight;
toggleSwitch.setRTL(rtl); // toggleSwitch.rtl = rtl;
Events¶
- On value change
toggleSwitch.on('valuechange', function(value) { // value: checked })
Other properties¶
See game object
Create mask¶
var mask = toggleSwitch.createGeometryMask();
See mask
Shader effects¶
Support postFX effects
Note
No preFX effect support