File selector button
Introduction¶
A transparent file chooser button (<input type="file">) above a Label.
- Author: Rex
- Game object, DOM Game object
Live demos¶
Usage¶
Install plugin¶
Load minify file¶
- Enable dom element in configuration of game
var config = { parent: divId, // fullscreenTarget: divId, // For fullscreen dom: { createContainer: true }, input: { mouse: { target: divId }, touch: { target: divId }, }, // ... }; var game = new Phaser.Game(config);- Set
parentto divId - Set
dom.createContainertotrue.
- Set
- 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 file selector button object
var button = scene.add.fileSelectorButton(config);
Import plugin¶
- Install rex plugins from npm
npm i phaser3-rex-plugins - Install plugin in configuration of game
import FileChooserPlugin from 'phaser3-rex-plugins/plugins/filechooser-plugin.js'; var config = { parent: divId, // fullscreenTarget: divId, // For fullscreen dom: { createContainer: true }, input: { mouse: { target: divId }, touch: { target: divId }, }, // ... plugins: { scene: [{ key: 'rexUI', plugin: UIPlugin, mapping: 'rexUI' }, // ... ] } // ... }; var game = new Phaser.Game(config); - Add file selector button object
var button = scene.add.fileSelectorButton(config);
Import class¶
- Install rex plugins from npm
npm i phaser3-rex-plugins - Enable dom element in configuration of game
var config = { parent: divId, // fullscreenTarget: divId, // For fullscreen dom: { createContainer: true }, input: { mouse: { target: divId }, touch: { target: divId }, }, // ... }; var game = new Phaser.Game(config);- Set
parentto divId - Set
dom.createContainertotrue.
- Set
- Import class
import { FileSelectorButton } from 'phaser3-rex-plugins/templates/ui/ui-components.js'; - Add file chooser object
var button = new FileSelectorButton(scene, config); scene.add.existing(button);
Add file chooser object¶
var button = scene.add.fileSelectorButton({
// x: 0,
// y: 0,
// anchor: undefined,
// width: undefined,
// height: undefined,
// origin: 0.5
// originX:
// originY:
orientation: 0,
// rtl: false,
background: backgroundGameObject,
icon: iconGameObject,
// iconMask: false,
// 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,
text: 0,
},
// name: '',
// draggable: false,
// sizerEvents: false,
// enableLayer: false,
// accept: '',
// multiple: false,
});
// var fileChooser = scene.add.fileSelectorButton(x, y, width, height, config);
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).orientation:'left-to-right','horizontal','h','x', or0: Put icon at left side, and text at right side.'top-to-bottom','vertical','v','y', or1: 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 label.icon: Game object of icon, optional.iconMask: Set true to add a circle mask on icon game object.- Phaser 3 engine does not support nested mask, uses circle mask image instead.
squareFitIcon:true: Resize icon size to square to fit label height/width.false: Ignore this feature. Default behavior.
iconSize: Set display size of icon game object toiconSizexiconSizeiconWidth: Set display width of icon game object toiconWidth.- If
iconHeightisundefined, setscaleYof icon game object toscaleXof icon game object, to keep size ratio.
- If
iconHeight: Set display height of icon game object toiconHeight.- If
iconWidthisundefined, setscaleXof icon game object toscaleYof icon game object, to keep size ratio.
- If
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.- Must set to
trueif usingscene.rexUI.wrapExpandTextmethod with any text game object.
- Must set to
expandTextHeight: Settrueto 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.- Phaser 3 engine does not support nested mask, uses circle mask image instead.
squareFitAction:true: Resize action icon size to square to fit label height/width.false: Ignore this feature. Default behavior.
actionSize: Set display size of action game object toactionSizexactionSizeactionWidth: Set display width of action game object toactionWidth.- If
actionHeightisundefined, setscaleYof action game object toscaleXof action game object, to keep size ratio.
- If
actionHeight: Set display height of action game object toactionHeight.- If
actionWidthisundefined, setscaleXof action game object toscaleYof action game object, to keep size ratio.
- If
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.text: Space between text game object and action icon game object.
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.
accept: A filter for what file types the user can pick from the file input dialog box.'image/*': The user can pick all image files.'audio/*': The user can pick all sound files.'video/*': The user can pick all video files.file_extension: Specify the file extension(s) (e.g: .gif, .jpg, .png, .doc) the user can pick from.
multiple: Settrueto select multiple files.
Custom class¶
- Define class
class MyFileSelectorButton extends RexPlugins.UI.FileSelectorButton { constructor(scene, config) { super(scene, 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
preUpdatemethod, it will be added to the Update List.
- Create instance
var button = new MyFileSelectorButton(scene, config);
Layout children¶
Arrange position of all elements.
label.layout();
See also - dirty
Get element¶
- Get element
- Background game object
var background = label.getElement('background'); - Icon game object
var icon = label.getElement('icon'); - Text game object
var textObject = label.getElement('text'); - Action icon game object
var action = label.getElement('action'); - File chooser game object
var fileChooser = label.getElement('fileChooser');
- Background game object
- Get by name
or
var gameObject = label.getElement('#' + name); // var gameObject = label.getElement('#' + name, recursive);var gameObject = label.getByName(name); // var gameObject = label.getByName(name, recursive);recursive: Settrueto search all children recursively.
Selected files¶
var files = fileChooser.files;
files: Array of file object.
Set accept filter¶
fileChooser.setAccept(accept);
accept: A filter for what file types the user can pick from the file input dialog box.'image/*': The user can pick all image files.'audio/*': The user can pick all sound files.'video/*': The user can pick all video files.file_extension: Specify the file extension(s) (e.g: .gif, .jpg, .png, .doc) the user can pick from.
Multiple files¶
- Enable
fileChooser.setMultiple(); - Disable
fileChooser.setMultiple(false);
Events¶
- Selected file(s) changed
button.on('select', function(files, button) { var file = files[0]; var url = URL.createObjectURL(file); // ... })
Load file to cache¶
fileChooser.loadFile(file, loaderType, key);
// fileChooser.loadFile(file, loaderType, key, cahceType);
or
fileChooser.loadFilePromise(file, loaderType, key, cahceType)
.then(function(content) {
})
file: File object, see EventsloaderType:image,text,binary, ... See Loaderkey: Unique string key.cahceType:undefined: Use default value.
content: Content of file.
Create object URL¶
- Create object url
var objectURL = URL.createObjectURL(file); - Release object url
URL.createObjectURL(objectURL);
Interactive with other game objects¶
See dom-element's Interactive with other game objects
Other properties¶
See label object, sizer object, base sizer object, container-lite.