Knob
Introduction¶
A knob button based on circular progress shape.
- Author: Rex
- Game object
Live demos¶
Usage¶
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 knob object
var knob = scene.rexUI.add.knob(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 knob object
var knob = scene.rexUI.add.knob(config);
Import class¶
- Install rex plugins from npm
npm i phaser3-rex-plugins - Import class
import { Knob } from 'phaser3-rex-plugins/templates/ui/ui-components.js'; - Add knob object
var knob = new Knob(scene, config); scene.add.existing(knob);
Add knob object¶
var knob = scene.rexUI.add.knob({
// x: 0,
// y: 0,
// anchor: undefined,
// width: undefined,
// height: undefined,
// origin: 0.5
// originX:
// originY:
background: backgroundGameObject,
barColor: undefined,
trackColor: undefined,
centerColor: undefined,
thickness: 0.2,
startAngle: Phaser.Math.DegToRad(270),
anticlockwise: false,
// iterations: 128,
knobDepth: 0,
text: undefined,
textFormatCallback: undefined,
textFormatCallbackScope: undefined,
input: 'pan',
value: 0,
gap: undefined,
easeValue: {
duration: 0,
ease: 'Linear'
},
valuechangeCallback: function(newValue, oldValue, knob) {
},
valuechangeCallbackScope: undefined,
space: {
left: 0,
right: 0,
top: 0,
bottom: 0,
},
enable: true,
// name: '',
// draggable: false,
// sizerEvents: false,
// enableLayer: 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,aspectRatio: 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'.
- Percentage of visible width/height :
width,height: Set size (invokeonResizeCallback) based on visible window, which composed of- Percentage of visible width/height :
'p%', p:0~100. - Padding :
'+n', or'-n'.
- Percentage of visible width/height :
aspectRatio:undefined, orfalse: Does not keep aspect ratio. Default behavior.true: Use the current width and height as the aspect ratio.- A number : Use given number as the aspect ratio.
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).background: Game object of background, optional. This background game object will be resized to fit the size of knob.barColor: Color of circular bar, in number or css string value.trackColor: Color of circular track, in number or css string value.centerColor: Color of center circle, in number or css string value.thickness:0~1, thickness of circular bar. Default value is0.2(0.2*radius)startAngle: Start angle of circular bar, in radians. Default value is 270 degrees.anticlockwise: Settrueto put anticlockwise circular bar. Default value isfalse.iterations: Iterations of curve, set larger value to get more smooth rendering result. Default value is128.knobDepth: Depth of knob (circular progress). Default value is0.text: Label, Text, BBCodeText, or TagText, which hassetText(text)method, optional.- Don't set
textColor,textStrokeColoriftextparameter is provided. - Depth of text object ought to larger than knob (circular progress), to put text object in front of knob.
- Don't set
- Display text :
textColor: Color of display text. Default isundefined.textStrokeColor,textStrokeThickness: Stroke color, stroke line width of display text. Default isundefined.textSize,textFamily,textStyle: Size, family, style of display text.
textFormatCallback,textFormatCallbackScope: Formating callback of display text. ex:Default value isfunction(value) { return Math.floor(value * 100).toString(); }undefined.input:'pan','drag', or0: Control knob by panning circular progress. Default setting.'click', or1: Control knob by touching circular progress.'none', or-1: Disable knob controlling.
value: Initial value (0 ~ 1).gap: Snap a value to nearest grid slice, using rounding.undefined: Disalbe this feature.
easeValue: Easing value wheninputis'click'.easeValue.duration: Duration of value easing, default is0(no easing).easeValue.ease: Ease function, default is'Linear'.
space: Pads spacesspace.left,space.right,space.top,space.bottom: Space of bounds.
valuechangeCallback: callback function when value changed.enable: Setfalseto disable controlling.name: Set name of this game object.draggable: Settrueto drag top-most object.sizerEvents: Settrueto fire sizer events. Default value isfalse.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.
Custom class¶
- Define class
class MyKnob extends RexPlugins.UI.Knob { constructor(scene, config) { super(scene, config); // ... scene.add.existing(this); } // ... } - Create instance
var knob = new MyKnob(scene, config);
Layout children¶
Arrange position of all elements.
knob.layout();
See also - dirty
Get element¶
- Get element
- Background game object
var background = knob.getElement('background'); - Circular progress game object
var circularProgress = knob.getElement('knob'); - Text game object
var text = knob.getElement('text');
- Background game object
- Get by name
or
var gameObject = knob.getElement('#' + name); // var gameObject = knob.getElement('#' + name, recursive);var gameObject = knob.getByName(name); // var gameObject = knob.getByName(name, recursive);recursive: Settrueto search all children recursively.
Enable¶
- Get
var enable = knob.enable; - Set
or
knob.setEanble(enable);knob.enable = enable;
Value¶
Change value will also change the position of knob thumb and width of knob indicator.
- Get value
or
var value = knob.getValue(min, max); // value : min ~ maxorvar value = knob.getValue(); // value: 0 ~ 1var value = knob.value; // value: 0 ~ 1 - Set value
or
knob.setValue(value, min, max); // value: min ~ maxorknob.setValue(value); // value: 0 ~ 1knob.value = value; // value: 0 ~ 1 - Increase value
or
knob.addValue(inc, min, max); // inc: min ~ maxorknob.addValue(inc); // inc: 0 ~ 1knob.value += inc; // inc: 0 ~ 1
Ease value¶
- Ease value to
or
knob.easeValueTo(value, min, max); // value: min ~ maxknob.easeValueTo(value); // value: 0 ~ 1 - Stop ease
knob.stopEaseValue(); - Set ease duration
knob.setEaseValueDuration(duration); - Set ease function
knob.setEaseValueFunction(ease);ease: Ease function.
Other properties¶
See overlap sizer object, base sizer object, container-lite.
Events¶
- On value changed
knob.on('valuechange', function(newValue, oldValue, knob){ // }, scope);