Grid sizer
Introduction¶
Layout children game objects in grids.
- Author: Rex
- Game object
Live demos¶
- Create cell-containers
- Scrollable grids
- Add new row/column
- Full viewport
- Set children interactive
- Destroy cell
- Reset grid
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 sizer object
var sizer = scene.rexUI.add.gridSizer(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 sizer object
var sizer = scene.rexUI.add.gridSizer(config);
Import class¶
- Install rex plugins from npm
npm i phaser3-rex-plugins - Import class
import { GridSizer } from 'phaser3-rex-plugins/templates/ui/ui-components.js'; - Add sizer object
var sizer = new GridSizer(scene, config); scene.add.existing(sizer);
Add grid sizer object¶
var gridSizer = scene.rexUI.add.gridSizer({
// x: 0,
// y: 0,
// anchor: undefined,
// width: undefined,
// height: undefined,
// origin: 0.5
// originX:
// originY:
column: 0, // col: 0
row: 0,
// columnProportions: undefined,
// rowProportions: undefined,
// space: {
// left: 0, right: 0, top: 0, bottom:0,
// column: 0, // [0, 0, 0]
// row: 0, // [0, 0, 0]
// indentLeftOdd: 0, indentLeftEven: 0,
// indentTopOdd: 0, indentTopEven: 0,
// },
// createCellContainerCallback: function(scene, x, y, config) {
// config.expand = true;
// return cellContainer;
// },
// name: '',
// draggable: false
});
or
var gridSizer = scene.rexUI.add.gridSizer(x, y, {
column: 0, // col: 0
row: 0,
// columnProportions: undefined,
// rowProportions: undefined,
// space: {
// left: 0, right: 0, top: 0, bottom:0,
// column: 0, // [0, 0, 0]
// row: 0, // [0, 0, 0]
// indentLeftOdd: 0, indentLeftEven: 0,
// indentTopOdd: 0, indentTopEven: 0,
// },
// createCellContainerCallback: function(scene, x, y, config) {
// config.expand = true;
// return cellContainer;
// },
// width: undefined,
// height: undefined
});
or
var gridSizer = scene.rexUI.add.gridSizer(x, y, width, height, {
column: 0, // col: 0
row: 0,
// columnProportions: undefined,
// rowProportions: undefined,
// space: {
// left: 0, right: 0, top: 0, bottom:0,
// column: 0, // [0, 0, 0]
// row: 0, // [0, 0, 0]
// indentLeftOdd: 0, indentLeftEven: 0,
// indentTopOdd: 0, indentTopEven: 0,
// },
// createCellContainerCallback: function(scene, x, y, config) {
// config.expand = true;
// return cellContainer;
// },
});
or
var gridSizer = scene.rexUI.add.gridSizer(x, y, width, height, column, row, {
// space: {
// left: 0, right: 0, top: 0, bottom:0,
// column: 0, // [0, 0, 0]
// row: 0, // [0, 0, 0]
// indentLeftOdd: 0, indentLeftEven: 0,
// indentTopOdd: 0, indentTopEven: 0,
// },
// createCellContainerCallback: function(scene, x, y, config, gridSizer) {
// config.expand = true;
// return cellContainer;
// },
// 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: Minimum width. i.e. Width of this gridSizer will larger then this value.height: Minimum height. i.e. Hieght of this gridSizer will larger then this value.origin,originX,originY: Set origin of this sizer. Default value is (0.5, 0.5).column, orcol: Amount of column grids.row: Amount of row grids.columnProportions,rowProportions: Proportion of each column/row.- Number : Apply this number proportion to each column/row
- Number array : Apply proportion of column/row through elements of this number array.
space: Pads spaces.space.left,space.right,space.top,space.bottom: Space of bounds.space.column: Space between 2 columns- A number
- A number array
space.row: Space between 2 rows- A number
- A number array
space.indentLeftOdd,space.indentLeftEven: Indent at each odd/even row.space.indentTopOdd,space.indentTopEven: Indent at each odd/even column.
createCellContainerCallback: Callback to create container(sizer) of each cell.function(scene, x, y, config) { return cellContainer; }x,y: Column, row index of this cell.config: Config of adding childconfig.align = 'center'; config.padding = {left: 0, right: 0, top: 0, bottom: 0}; config.expand = false; config.key = undefined;cellContainer: Return a game object for this cell.
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 MyGridSizer extends RexPlugins.UI.GridSizer { constructor(scene, x, y, minWidth, minHeight, column, row) { super(scene, x, y, minWidth, minHeight, column, row); // ... scene.add.existing(this); } // ... } - Create instance
var gridSizer = new MyGridSizer(scene, x, y, minWidth, minHeight, column, row);
Add background¶
gridSizer.addBackground(child);
or
gridSizer.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 child¶
Add a game obejct to grid sizer
gridSizer.add(child, column, row);
or
gridSizer.add(child,
{
column: 0,
row: 0,
align: 'center',
padding: {left: 0, right: 0, top: 0, bottom: 0},
expand: false, // expand: {width, height}
key: undefined,
offsetX: 0,
offsetY: 0,
offsetOriginX: 0,
offsetOriginY: 0,
}
);
or
gridSizer.add(child, column, row, align, padding, expand, key);
child: A game objectcolumn,row: Index of grid to add.columnandroware numbers : Insert game object to cell (column,row).- Do nothing if that cell has item already.
columnandrowareundefined: Search each column, each row to find an empty cell for inserting.- Add an new row (
gridSizer.addEmptyRow()) if last row was full, then find an empty cell in new row.
- Add an new row (
columnisundefined, androwistrue: Search each row, each column to find an empty cell for inserting.- Add an new column (
gridSizer.addEmptyColumn()) if last column was full, then find an empty cell in new column.
- Add an new column (
columnis a number, androwisundefined: Search each row of columncolumnto find an empty cell for inserting.- Add an new row (
gridSizer.addEmptyRow()) if last row was full, then find an empty cell in new row.
- Add an new row (
columnisundefinedandrowis a number : Search each column of rowrowto find an empty cell for inserting.- Add an new column (
gridSizer.addEmptyColumn()) if last column was full, then find an empty cell in new column.
- Add an new column (
align:'center', orPhaser.Display.Align.CENTER: Align game object at center. Default value.'left', orPhaser.Display.Align.LEFT_CENTER: Align game object at left-center.'right', orPhaser.Display.Align.RIGHT_CENTER: Align game object at right-center.'top', orPhaser.Display.Align.RIGHT_CENTER: Align game object at top-center.'bottom', orPhaser.Display.Align.BOTTOM_CENTER: Align game object at bottom-center.
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:- Boolean value
true: Expand width and height of child. Default value.false: Don't expand width or height of child.
- A plain object
{width: true}: Only expand width of child.{height: true}: only expand height of child.{width: true, height: true}: Expand width and height of child.
- Boolean value
key: Add this child into childMap, which could be read back bysizer.getElement(key).undefined: Don't add this child. Default value.
offsetX,offsetOriginX: Apply offsetoffsetX + offsetOriginY * widthto x coordinate after alignment.offsetY,offsetOriginY: Apply offsetoffsetY + offsetOriginY * heightto y coordinate after alignment.
Add multiple¶
sizer.add(gameObjects, config);
gameObjects: Array of child game objectsconfig: See config of add-child
Insert empty row/column¶
- Insert an empty row
gridSizer.insertEmptyRow(rowIndex); // gridSizer.insertEmptyRow(rowIndex, proportion, space);proportion: Proportion of this new row.undefined: Use proportion of 1st row as default value.
space: Space between this new row and previous row.undefined: Use space of 1st row as default value.
- Add an empty row
gridSizer.addEmptyRow(); // gridSizer.addEmptyRow(rowIndex, proportion, space); - Insert an empty column
gridSizer.insertEmptyColumn(colIndex); // gridSizer.insertEmptyColumn(colIndex, proportion, space);proportion: Proportion of this new column.undefined: Use proportion of 1st column as default value.
space: Space between this new column and previous column.undefined: Use space of 1st column as default value.
- Add an empty row
gridSizer.addEmptyColumn(); // gridSizer.addEmptyColumn(colIndex, proportion, space);
Proportion¶
Set proportion of each column or row via
gridSizer.setColumnProportion(columnIndex, proportion);
gridSizer.setRowProportion(rowIndex, proportion);
Layout children¶
Arrange position of all children.
gridSizer.layout();
See also - dirty
Hide¶
- Set invisible, won't layout it
or
sizer.hide();sizer.hide(gameObject); - Set visible, will layout it
or
sizer.show();sizer.show(gameObject);
Grid index <-> child¶
- Grid index -> child
var child = gridSizer.getChildAt(columnIndex, rowIndex); - Child -> grid index
var gridIndex = gridSizer.childToGridIndex(child); // var gridIndex = gridSizer.childToGridIndex(child, out);gridIndex:{x, y}, ornullif child is not belong this sizer.
Remove child¶
- Remove a child
or
gridSizer.remove(child);gridSizer.removeAt(columnIndex, rowIndex); - Remove and destroy a child
or
gridSizer.remove(child, true);gridSizer.removeAt(columnIndex, rowIndex, true); - Remove all children
gridSizer.removeAll(); - Remove and destroy all children
gridSizer.removeAll(true); - Remove all children and backgrounds
gridSizer.clear(); - Remove and destroy all children and backgrounds
gridSizer.clear(true); - Remove from parent sizer
sizer.removeFromParentSizer();
Sort children¶
- Sort by data
gridSizer.sortChildrenByData(key, descending).layout();key: Data keydescending:true: Descending orderfalse: Ascending order, default behavior.
- Sort by property of child
gridSizer.sortChildrenByProperty(key, descending).layout();key: Property keydescending:true: Descending orderfalse: Ascending order, default behavior.
- Sort by callback
gridSizer.sortChildren(function(childA, childB){ // var valueA = childA.getData(key); // var valueB = childB.getData(key); // return valueB - valueA }).layout();childA,childB: 2 children of this size
Grid size¶
- Amount of column
var columnCount = gridSizer.columnCount; - Amount of row
var rowCount = gridSizer.rowCount; - Amount of grid = columnCount * rowCount
var gridCount = gridSizer.gridCount;
Get element¶
- Get element
- All children items
var items = gridSizer.getElement('items');
- All children items
- Get by name
or
var gameObject = gridSizer.getElement('#' + name); // var gameObject = gridSizer.getElement('#' + name, recursive);var gameObject = gridSizer.getByName(name); // var gameObject = gridSizer.getByName(name, recursive);recursive: Settrueto search all children recursively.
Get child index¶
var index = sizer.getChildIndex(child);
index: A number, ornull.
Reset grid¶
sizer.resetGrid(column, row, columnProportions, rowProportions, space);
column: Amount of column grids.row: Amount of row grids.columnProportions,rowProportions: Proportion of each column/row.- Number : Apply this number proportion to each column/row
- Number array : Apply proportion of column/row through elements of this number array.
space:space.column: Space between 2 columns- A number
- A number array
space.row: Space between 2 rows- A number
- A number array
Note
Children game objects will be removed without destroyed.
Note
Will invoke createCellContainerCallback to create cellContainer of each cell.