Text area
Introduction¶
A container with a text, slider, and scroller.
- 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 text-area object
var textArea = scene.rexUI.add.textArea(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 text-area object
var textArea = scene.rexUI.add.textArea(config);
Import class¶
- Install rex plugins from npm
npm i phaser3-rex-plugins
- Import class
import { TextArea } from 'phaser3-rex-plugins/templates/ui/ui-components.js';
- Add text-area object
var textArea = new TextArea(scene, config); scene.add.existing(textArea);
Add text-area object¶
var textArea = scene.rexUI.add.textArea({
// x: 0,
// y: 0,
// anchor: undefined,
// width: undefined,
// height: undefined,
// origin: 0.5
// originX:
// originY:
// Elements
background: backgroundGameObject,
text: textGameObject,
// textWidth: undefined,
// textHeight: undefined,
// textMask: false,
// alwaysScrollable: false,
slider: {
// background: sliderBackgroundGameObject,
track: trackGameObject,
/*
track: {
width: 1, height: 1,
radius: 0,
color: undefined, alpha: 1,
strokeColor: undefined, strokeAlpha: 1, strokeWidth: 2,
shape: undefined
}
*/
// indicator: indicatorGameObject,
thumb: thumbGameObject,
/*
thumb: {
width: 1, height: 1,
radius: 0,
color: undefined, alpha: 1,
strokeColor: undefined, strokeAlpha: 1, strokeWidth: 2,
shape: undefined
}
*/
// input: 'drag',
// position: 'right',
// gap: undefined,
// hideUnscrollableSlider: false,
// disableUnscrollableDrag: false,
// adaptThumbSize: false,
// minThumbSize: undefined,
// buttons: {
// top: topButtonGameObject, bottom: bottomButtonGameObject,
// left: leftButtonGameObject, right: rightButtonGameObject,
// step: 0.01,
// }
},
// scroller: {
// threshold: 10,
// slidingDeceleration: 5000,
// backDeceleration: 2000,
// pointerOutRelease: true,
// rectBoundsInteractive: true,
// dragRate: 1,
// },
mouseWheelScroller: false,
// mouseWheelScroller: {
// focus: true,
// speed: 0.1
// },
clampChildOY: false,
header: headerGameObject,
footer: footerGameObject,
space: {
left: 0,
right: 0,
top: 0,
bottom: 0,
text: 0,
// text: {
// top: 0,
// bottom: 0,
// left: 0,
// right: 0,
//},
slider: 0,
// slider: {
// top: 0,
// bottom: 0,
// left: 0,
// right: 0,
// },
header: 0,
footer: 0,
},
expand: {
header: true,
footer: true,
},
align: {
header: 'center',
footer: 'center',
},
content: '',
// 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).background
: Game object of background, optional. This background game object will be resized to fit the size of text area.text
: Text object, bbcode text object, tag text object, or bitmap text objecttextWidth
: Fixed width of text game object. Setundefined
to ignore this feature.textHeight
: Fixed height of text game object. Setundefined
to ignore this feature.textMask
:false
: Crop text game object. Default behavior if text game object hassetCrop
method.true
: Apply mask on text to crop text game object. Default behavior if text game object does not havesetCrop
method (ex. bitmaptext game object).
slider
: Componments of slider, optional.slider.background
: Game object of slider background, optional.slider.track
: Game object of track.slider.thumb
: Game object of thumb.slider.input
:'pan'
,'drag'
, or0
: Control slider by panning/dragging thumb game object. Default setting.'click'
, or1
: Control slider by touching track game object.'none'
, or-1
: Disable sider controlling.
slider.position
: Position of this slider.0
,'right'
,'bottom'
: Slider at right/bottom side. Default value.1
,'left'
,'top'
: Slider at left/top side.
slider.hideUnscrollableSlider
:false
: Slider is always visible no matter it is scrollable or not. Default behavior.true
: Set slider to invisible if it is unscrollable.
slider.disableUnscrollableDrag
:false
: Scroller is always enale no matter it is scrollable or not. Default behavior.true
: Set scroller to disable if it is unscrollable.
slider.adaptThumbSize
:false
: Don't adjust height/width of thumb. Default behavior.true
: Adjust height/width of thumb according to ratio of visible child.- Minimum height/width of thumb =
slider.minThumbSize
. If content is larger then a page. - Maximum height/width of thumb = height/width of
slider.track
. If content is less then a page.
- Minimum height/width of thumb =
slider.minThumbSize
: Minimum height/width of thumb used inslider.adaptThumbSize
mode.slider.buttons
: Press button to scroll content in each tick.slider.buttons.top
,slider.buttons.bottom
: Top and bottom buttons.slider.buttons.left
,slider.buttons.right
: Left and right buttonsslider.buttons.step
: Scrolling step in each tick. Default value is0.01
.
- Set to
false
to skip creating slider.
scroller
: Configuration of scroller behavior.scroller.threshold
: Minimal movement to scroll. Set0
to scroll immediately.scroller.slidingDeceleration
: Deceleration of slow down when dragging released.- Set
false
to disable it.
- Set
scroller.backDeceleration
: Deceleration of pull back when out of bounds.- Set
false
to disable it.
- Set
scroller.pointerOutRelease
: Set totrue
to release input control when pointer out of gameObject.scroller.rectBoundsInteractive
:false
: Detect scrolling by game object's touch input.true
: Detect scrolling by rectangle bounds of game object. Default behavior.
scroller.dragRate
: Rate of dragging distance/dragging speed. Default value is1
.- Set to
false
to skip creating scroller.
mouseWheelScroller
: Configuration of mouse-wheel-scroller behavior.mouseWheelScroller.focus
:false
, or0
: Without checking if cursor is over game object or not.true
, or1
: Cursor is inside the rectangle bounds of game object. Default behavior.2
: Cursor is over game object.
mouseWheelScroller.speed
: Scrolling speed, default value is0.1
.- Set to
false
to skip creating mouse-wheel-scroller. Default behavior.
clampChildOY
: Settrue
to clamp scrolling.alwaysScrollable
:false
: Can't scroll if content is less then 1 page. Default behavior.true
: Can scroll in all cases
header
: Game object of header, optional.footer
: Game object of footer, optional.space
: Pads spacesspace.left
,space.right
,space.top
,space.bottom
: Space of bounds.space.text
:- A number: Space between text object and slider object.
- An object: Padding of text object.
space.text.top
,space.text.bottom
: Top, bottom padding space of text object.space.text.right
: Space between text object and slider object.
space.slider
:0
: No space around slider.space.slider.left
,space.slider.right
,space.slider.top
,space.slider.bottom
: Space around slider.
space.header
: Space between header and text object.space.footer
: Space between footer and text object.
expand
: Expand width or height of elementexpand.header
: Settrue
to expand width or height of header game object.expand.footer
: Settrue
to expand width or height of footer game object.
align
: Align elementalign.header
'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.
align.footer
content
: Content of this text area.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 MyTextArea extends RexPlugins.UI.TextArea { constructor(scene, config) { super(scene, config); // ... scene.add.existing(this); } // ... }
- Create instance
var textArea = new MyTextArea(scene, config);
Layout children¶
Arrange position of all elements.
textArea.layout();
See also - dirty
Content¶
- Set
textArea.setText(text);
- Append
textArea.appendText(text);
- Get
var text = textArea.text;
Scroll content¶
- Scroll down lines
textArea.scrollLine(lineCount);
- Set
textArea.childOY = oy; // textArea.setChildOY(oy);
- Set and clamp
textArea.setChildOY(oy, true);
- Add
textArea.addChildOY(oy);
- Add and clamp
textArea.addChildOY(oy, true);
- Get
var childOY = textArea.childOY;
- Top OY
var topOY = textArea.topChildOY;
- Bottom OY
var bottomOY = textArea.bottomChildOY;
- Is overflow (height of content is larger than display height)
var isOverflow = textArea.isOverflow;
Scroll by percentage¶
- Set
textArea.t = t; // t: 0~1 // textArea.setT(t);
- Set and clamp
textArea.setT(t, true);
- Get
var t = textArea.t;
Scroll to top/bottom¶
- Scroll to top
textArea.scrollToTop();
- Equal to
textArea.t = 0;
- Equal to
- Scroll to bottom
textArea.scrollToBottom();
- Equal to
textArea.t = 1;
- Equal to
Scroll to line¶
- Scroll to next line
textArea.scrollToNextLine();
- Scroll to next n line
textArea.scrollToNextLine(n);
- Scroll to line
textArea.scrollToLine(lineIndex);
- Get current line index
var lineIndex = textArea.lineIndex;
Enable/disable scrolling¶
- Slider
- Set enable state
or
textArea.setSliderEnable(enabled);
textArea.sliderEnable = enabled;
- Get enable state
var enable = textArea.sliderEnable;
- Set enable state
- Scroller
- Set enable state
or
textArea.setScrollerEnable(enabled);
textArea.scrollerEnable = enabled;
- Get enable state
var enable = textArea.scrollerEnable;
- Set enable state
- Mouse-Wheel-Scroller
- Set enable state
or
textArea.setMouseWheelScrollerEnable(enabled);
textArea.mouseWheelScrollerEnable = enabled;
- Get enable state
var enable = textArea.mouseWheelScrollerEnable;
- Set enable state
Event¶
- Scroll
textArea.on('scroll', function(textArea) { // ... })
- Slider
- Slider drag start
textArea.getElement('slider').on('inputstart', function(panel) { // ... })
- Slider drag end
textArea.getElement('slider').on('inputend', function(panel) { // ... })
- Slider drag start
- Scroller
- Scroller drag start
textArea.getElement('scroller').on('dragstart', function(panel) { // ... })
- Scroller drag end
textArea.getElement('scroller').on('dragend', function(panel) { // ... })
- Scroller drag start
Lines count¶
var linesCount = textArea.linesCount;
Other properties¶
See sizer object, base sizer object, container-lite.
Get element¶
- Get element
- Background game object
var background = textArea.getElement('background');
- Text game object
var text = textArea.getElement('text');
- Slider
- Track
var track = textArea.getElement('slider.track');
- Thumb
var thumb = textArea.getElement('slider.thumb');
- Track
- Scroller
var scroller = textArea.getElement('scroller');
- Background game object
- Get by name
or
var gameObject = textArea.getElement('#' + name); // var gameObject = textArea.getElement('#' + name, recursive);
var gameObject = textArea.getByName(name); // var gameObject = textArea.getByName(name, recursive);
recursive
: Settrue
to search all children recursively.