Skip to content

Commit

Permalink
Add rangeStyle parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
rexrainbow committed Oct 19, 2024
1 parent ab4dad9 commit ca927b3
Show file tree
Hide file tree
Showing 11 changed files with 299 additions and 19 deletions.
67 changes: 67 additions & 0 deletions docs/docs/canvasinput.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ var txt = scene.add.rexCanvasInput({
// 'cursor.color': ...
// 'cursor.backgroundColor': ...
// 'cursor.xxx': ...
// Style when range selecting
// 'range.color': ...
// 'range.backgroundColor': ...
// 'range.xxx': ...
// Using cursor style if no range style is given
},
cursorStyle: undefined,
Expand Down Expand Up @@ -197,6 +203,20 @@ var txt = scene.add.rexCanvasInput({
// });
// },
// onRangeOut: function(child, cursorIndex, canvasInput) {
// child.modifyStyle({
//
// });
// },
// onRangeIn: function(child, cursorIndex, canvasInput) {
// child.modifyStyle({
//
// });
// },
// Use 'onCursorIn' and 'onCursorOut' if 'onRangeOut' and 'onRangeIn' are not given
// parseTextCallback: function(text) {
// return text;
// }.
Expand Down Expand Up @@ -263,6 +283,22 @@ var txt = scene.add.rexCanvasInput({
}
```
- `child` : [character child](dynamictext.md#character)
- `onRangeOut` : Callback invoked when leaving range-selecting on a character child
```javascript
function(child, index, canvasInput) {
child.modifyStyle({...})
}
```
- `child` : [character child](dynamictext.md#character)
- Use `onCursorOut` callback if `onRangeOut` and `onRangeIn` callbacks are not given, for backward compatible.
- `onRangeIn` : Callback invoked when entering range-selecting on a character child
```javascript
function(child, index, canvasInput) {
child.modifyStyle({...})
}
```
- `child` : [character child](dynamictext.md#character)
- Use `onCursorIn` callback if `onRangeOut` and `onRangeIn` callbacks are not given, for backward compatible.
- `parseTextCallback` : Callback of parsing text (`txt.text`) to value (`txt.value`)
- `undefined` : Bypass text to value. Default behavior.
- A function object
Expand Down Expand Up @@ -306,12 +342,43 @@ var txt = scene.add.rexCanvasInput({
shadowBlur: 0,
backgroundColor: null,
backgroundHeight: undefined,
backgroundBottomY: 0,
backgroundLeftX: 0,
backgroundRightX: 0,
backgroundBY: undefined,
offsetX: 0,
offsetY: 0
}
```
- Or add these style settings in `style` parameter, with prefix `'cursor.'`.
- `rangeStyle` : Will apply this style when entering range-selecting on character children.
- `undefined` : Ignore this behavior.
- A plain object
```javascript
{
bold: false,
italic: false,
fontSize: '16px',
fontFamily: 'Courier',
color: '#fff',
stroke: '#fff',
strokeThickness: 0,
shadowColor: null,
shadowOffsetX: 0,
shadowOffsetY: 0,
shadowBlur: 0,
backgroundColor: null,
backgroundHeight: undefined,
backgroundBottomY: 0,
backgroundLeftX: 0,
backgroundRightX: 0,
backgroundBY: undefined,
offsetX: 0,
offsetY: 0
}
```
- Or add these style settings in `style` parameter, with prefix `'range.'`.
- Using `cursorStyle` if no `rangeStyle` found in config, for backward compatible.


#### Number input
Expand Down
5 changes: 5 additions & 0 deletions examples/canvasinput/thin-cursor.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@echo off
set main=./examples/canvasinput/thin-cursor.js
cd ..
cd ..
npm run dev
91 changes: 91 additions & 0 deletions examples/canvasinput/thin-cursor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import phaser from 'phaser/src/phaser.js';
import CanvasInputPlugin from '../../plugins/canvasinput-plugin.js';

class Demo extends Phaser.Scene {
constructor() {
super({
key: 'examples'
})
}

preload() {

}

create() {
var txt0 = CreateCanvasInput(this, 'Apple012345678901234567890123456789').setPosition(400, 100)
var txt1 = CreateCanvasInput(this, 'A').setPosition(400, 200).appendText('pple')
var txt2 = CreateCanvasInput(this, 'Apple').setPosition(400, 300).setReadOnly()
var txt3 = CreateCanvasInput(this, 'Apple', 100).setPosition(100, 400).setOrigin(0)

this.add.text(0, 0, 'Full screen')
.setInteractive()
.on('pointerdown', function () {
if (this.scale.isFullscreen) {
this.scale.stopFullscreen();
// On stop fulll screen
} else {
this.scale.startFullscreen();
// On start fulll screen
}
}, this)
}

update() { }
}

var CreateCanvasInput = function (scene, text, width) {
if (width === undefined) {
width = 600;
}
return scene.add.rexCanvasInput(
{
width: width,

background: {
'focus.stroke': 'red',
},

style: {
fontSize: 24,
backgroundBottomY: 5,
backgroundHeight: 24,

'range.backgroundColor': 'green',

'cursor.backgroundColor': 'green',
'cursor.backgroundHeight': 20,
'cursor.backgroundBottomY': 2,
'cursor.backgroundLeftX': 10,
},

text: text,

selectAll: true
}
)
.on('keydown-ENTER', function (text) {
console.log(text)
})
}

var config = {
type: Phaser.AUTO,
parent: 'phaser-example',
width: 800,
height: 600,
scale: {
mode: Phaser.Scale.FIT,
autoCenter: Phaser.Scale.CENTER_BOTH,
},
scene: Demo,
plugins: {
global: [{
key: 'rexCanvasInput',
plugin: CanvasInputPlugin,
start: true
}]
}
};

var game = new Phaser.Game(config);
31 changes: 31 additions & 0 deletions plugins/gameobjects/dynamictext/canvasinput/CanvasInput.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,35 @@ declare namespace CanvasInput {
'cursor.shadowOffsetY'?: number,
'cursor.shadowBlur'?: number,
'cursor.backgroundColor'?: string | number | null,
'cursor.backgroundHeight'?: number,
'cursor.backgroundBottomY'?: number,
'cursor.backgroundLeftX'?: number,
'cursor.backgroundRightX'?: number,
'cursor.offsetX'?: number,
'cursor.offsetY'?: number,
'cursor.leftSpace'?: number,
'cursor.rightSpace'?: number,

'range.bold'?: boolean,
'range.italic'?: boolean,
'range.fontSize'?: string | number,
'range.fontFamily'?: string,
'range.color'?: string | number | null,
'range.stroke'?: string | number | null,
'range.strokeThickness'?: number,
'range.shadowColor'?: string | number | null,
'range.shadowOffsetX'?: number,
'range.shadowOffsetY'?: number,
'range.shadowBlur'?: number,
'range.backgroundColor'?: string | number | null,
'range.backgroundHeight'?: number,
'range.backgroundBottomY'?: number,
'range.backgroundLeftX'?: number,
'range.backgroundRightX'?: number,
'range.offsetX'?: number,
'range.offsetY'?: number,
'range.leftSpace'?: number,
'range.rightSpace'?: number,
}

interface IConfig extends DynamicText.IConfig {
Expand All @@ -66,6 +91,8 @@ declare namespace CanvasInput {

cursorStyle?: DynamicText.IConfigTextStyle;

rangeStyle?: DynamicText.IConfigTextStyle;

onOpen?: HiddenTextEdit.OnOpenCallbackType;
onFocus?: HiddenTextEdit.OnOpenCallbackType;

Expand Down Expand Up @@ -135,6 +162,10 @@ declare class CanvasInput extends DynamicText {
style: DynamicText.IConfigTextStyle
): this;

setRangeStyle(
style: DynamicText.IConfigTextStyle
): this;

setNumberInput(): this;

setMaxLength(value: number): this;
Expand Down
36 changes: 33 additions & 3 deletions plugins/gameobjects/dynamictext/canvasinput/CanvasInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import InjectDefaultConfig from './methods/InjectDefaultConfig.js';
import ExtractByPrefix from '../../../utils/object/ExtractByPrefix.js';
import RegisterArrowKeysEvent from './methods/RegisterArrowKeysEvent.js';
import RegisterCursorStyle from './methods/RegisterCursorStyle.js';
import RegisterRangeStyle from './methods/RegisterRangeStyle.js';
import RegisterFocusStyle from './methods/RegisterFocusStyle.js';
import CreateInsertCursorChild from './methods/CreateInsertCursorChild.js';
import SetText from './methods/SetText.js';
import { IsChar } from '../dynamictext/bob/Types.js';
import SetTextOXYMethods from './methods/SetTextOXYMethods.js';
import MoveCursorMethods from './methods/MoveCursorMethods.js';
import IsEmpty from '../../../utils/object/IsEmpty.js';

const IsPlainObject = Phaser.Utils.Objects.IsPlainObject;

Expand All @@ -35,6 +37,7 @@ class CanvasInput extends DynamicText {

var focusStyle = ExtractByPrefix(config.background, 'focus');
var cursorStyle = ExtractByPrefix(config.style, 'cursor');
var rangeStyle = ExtractByPrefix(config.style, 'range');

super(scene, x, y, fixedWidth, fixedHeight, config);
this.type = 'rexCanvasInput';
Expand Down Expand Up @@ -62,19 +65,41 @@ class CanvasInput extends DynamicText {
}
RegisterCursorStyle.call(this, cursorStyle);

if (config.rangeStyle) {
Object.assign(rangeStyle, config.rangeStyle)
}
if (IsEmpty(rangeStyle)) {
Object.assign(rangeStyle, cursorStyle);
}
RegisterRangeStyle.call(this, rangeStyle);


var addCharCallback = config.onAddChar;
if (addCharCallback) {
this.on('addchar', addCharCallback);
}

var cursorInCallback = config.onCursorIn;
if (cursorInCallback) {
this.on('cursorin', cursorInCallback);
}

var cursorOutCallback = config.onCursorOut;
if (cursorOutCallback) {
this.on('cursorout', cursorOutCallback);
}
var cursorInCallback = config.onCursorIn;
if (cursorInCallback) {
this.on('cursorin', cursorInCallback);

var useCursorCallback = !config.onRangeIn && !config.onRangeOut
var rangeInCallback = (!useCursorCallback) ? config.onRangeIn : config.onCursorIn;
if (rangeInCallback) {
this.on('rangein', rangeInCallback);
}

var rangeOutCallback = (!useCursorCallback) ? config.onRangeOut : config.onCursorOut;
if (rangeOutCallback) {
this.on('rangeout', rangeOutCallback);
}

var moveCursorCallback = config.onMoveCursor;
if (moveCursorCallback) {
this.on('movecursor', moveCursorCallback);
Expand Down Expand Up @@ -269,6 +294,11 @@ class CanvasInput extends DynamicText {
return this;
}

setRangeStyle(style) {
this.rangeStyle = style;
return this;
}

setNumberInput() {
this.textEdit
.setNumberInput()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import IsEmpty from '../../../../utils/object/IsEmpty.js';
import GetPartialData from '../../../../utils/object/GetPartialData.js';
import IsKeyValueEqual from '../../../../utils/object/IsKeyValueEqual.js';

var RegisterRangeStyle = function (rangeStyle) {
if (IsEmpty(rangeStyle)) {
return;
}

this
.setRangeStyle(rangeStyle)
.on('rangein', function (child) {
var rangeStyle = this.rangeStyle;
var styleSave = GetPartialData(child.style, rangeStyle);
if (IsKeyValueEqual(rangeStyle, styleSave)) {
return;
}

child.styleSave = styleSave;
child.modifyStyle(rangeStyle);
}, this)
.on('rangeout', function (child) {
if (!child.styleSave) {
return;
}

child.modifyStyle(child.styleSave);
child.styleSave = undefined;
}, this)
}

export default RegisterRangeStyle;
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var SelectRange = function (hiddenTextEdit) {

var child = textObject.getCharChild(i);
if (child) {
var eventName = (inPrevSelectionRange) ? 'cursorout' : 'cursorin';
var eventName = (inPrevSelectionRange) ? 'rangeout' : 'rangein';
textObject.emit(eventName, child, i, textObject);
}
}
Expand Down
2 changes: 0 additions & 2 deletions plugins/gameobjects/dynamictext/dynamictext/DynamicText.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ declare namespace DynamicText {
}

interface IConfigTextStyle extends IConfigTextStyleBase {
backgroundHeight?: number,
backgroundBottomY?: number,
}

interface IConfigImage {
Expand Down
Loading

0 comments on commit ca927b3

Please sign in to comment.