Pages
Introduction¶
A container with pages, only current page is visible.
- 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 pages object
var pages = scene.rexUI.add.pages(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 pages object
var pages = scene.rexUI.add.pages(config);
Import class¶
- Install rex plugins from npm
npm i phaser3-rex-plugins
- Import class
import { Pages } from 'phaser3-rex-plugins/templates/ui/ui-components.js';
- Add pages object
var pages = new Pages(scene, config); scene.add.existing(pages);
Add pages object¶
var pages = scene.rexUI.add.pages({
// x: 0,
// y: 0,
// anchor: undefined,
// width: undefined,
// height: undefined,
// origin: 0.5
// originX:
// originY:
// space: { left: 0, right:0, top:0, bottom:0 },
// swapMode: 0,
// fadeIn: 0,
// 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
: 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 :
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).space
: Pads spaces.space.left
,space.right
,space.top
,space.bottom
: Space of bounds.
swapMode
: Set to invisible or destroy swapped page.0
,'invisible'
: Set swapped page to invisible.1
,'destroy'
: Destroy swapped page.
fadeIn
: Fade-in duration of current page.0
: No fade-in effect. Default behavior.
name
: Set name of this game object.draggable
: Settrue
to drag top-most object.sizerEvents
: Settrue
to 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 MyPages extends RexPlugins.UI.Pages { constructor(scene, config) { super(scene, config); // ... scene.add.existing(this); } // ... }
- Create instance
var pages = new MyPages(scene, config);
Add background¶
pages.addBackground(child);
or
pages.addBackground(child, {left: 0, right: 0, top: 0, bottom: 0}, key);
left
,right
,top
,bottom
: Extra padded space. Default is 0.key
: Add this child into childMap, which could be read back bysizer.getElement(key)
.undefined
: Don't add this child. Default value.
Add page¶
pages.addPage(child, key, align, padding, expand);
// pages.add(child, key, align, padding, expand);
or
pages.addPage(child,
{
key: 0,
align: Phaser.Display.Align.TOP_LEFT,
padding: {left: 0, right: 0, top: 0, bottom: 0},
expand: true
}
);
// pages.add(child, config);
child
: A game object.key
: Unique name of this page.align
:'left-top'
, orPhaser.Display.Align.TOP_LEFT
: Align game object at left-top. Default value.'left-center'
, orPhaser.Display.Align.LEFT_CENTER
: Align game object at left-center.'left-bottom'
, orPhaser.Display.Align.LEFT_BOTTOM
: Align game object at left-bottom.'center-top'
, orPhaser.Display.Align.TOP_CENTER
: Align game object at center-top.'center-center'
, orPhaser.Display.Align.CENTER
: Align game object at center-center.'center-bottom'
, orPhaser.Display.Align.BOTTOM_CENTER
: Align game object at center-bottom.'right-top'
, orPhaser.Display.Align.TOP_RIGHT
: Align game object at right-top.'right-center'
, orPhaser.Display.Align.RIGHT_CENTER
: Align game object at right-center.'right-bottom'
, orPhaser.Display.Align.RIGHT_BOTTOM
: Align game object at right-bottom.
padding
: Add space between bounds. Default is 0.- A number for left/right/top/bottom bounds,
- Or a plain object.
{ left: 0, right: 0, top: 0, bottom: 0 }
expand
: Expand width and height of the page.true
: Expand width and height.false
: Don't expand width or height.- A plain object, to expand width or height
{ width: true, height: true }
expand.width
: Expand width.expand.height
: Expand height.
Swap to page¶
pages.swapPage(key);
// pages.swapPage(key, fadeInDuration);
key
: Unique name of this page.
Note
This method will run pages.layout()
to arrange position of current page.
Page name¶
-
Current page name
var pageName = pages.currentKey;
-
Previous page name
var pageName = pages.previousKey;
- Name of all pages
var names = pages.keys;
Page object¶
- Get page object
var pageObject = pages.getPage(key);
pageObject
: A game object ornull
.
- Current page object
var pageObject = pages.currentPage;
- Previous page object
var pageObject = pages.previousPage;
Fade in duration¶
pages.setFadeInDuration(duration);
0
: No fade-in effect.
Get element¶
- Get element
- All page game objects
var gameObjects = pages.getElement('items');
- All page game objects
- Get by name
or
var gameObject = pages.getElement('#' + name); // var gameObject = pages.getElement('#' + name, recursive);
var gameObject = pages.getByName(name); // var gameObject = pages.getByName(name, recursive);
recursive
: Settrue
to search all children recursively.
Other properties¶
See base sizer object, container-lite.
Events¶
- Set page invisible, triggered when page is swapped out.
pages.on('pageinvisible', function(pageObject, key, pages) { // ... }, scope);
pageObject
: Game object of page.key
: Page name.pages
: Pages object
- Set page visible, triggered when page is shown.
pages.on('pagevisible', function(pageObject, key, pages) { // ... }, scope);
pageObject
: Game object of page.key
: Page name.pages
: Pages object