Skip to content

Commit

Permalink
Add setupCallback
Browse files Browse the repository at this point in the history
  • Loading branch information
rexrainbow committed Sep 23, 2023
1 parent aa95cea commit b3b1ab6
Show file tree
Hide file tree
Showing 16 changed files with 81 additions and 152 deletions.
2 changes: 0 additions & 2 deletions templates/ui/tweaker/builders/CreateButtonsInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ var CreateButtonsInput = function (scene, config, style) {
var gameObject = new ButtonsInput(scene, style);
scene.add.existing(gameObject);

gameObject.setOptions(config.options);

return gameObject;
}

Expand Down
2 changes: 0 additions & 2 deletions templates/ui/tweaker/builders/CreateListInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ var CreateListInput = function (scene, config, style) {
var gameObject = new ListInput(scene, style);
scene.add.existing(gameObject);

gameObject.setOptions(config.options);

return gameObject;
}

Expand Down
2 changes: 0 additions & 2 deletions templates/ui/tweaker/builders/CreateNumberInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ var CreateNumberInput = function (scene, config, style) {
var gameObject = new NumberInput(scene, style);
scene.add.existing(gameObject);

gameObject.setInputTextReadOnly(!!config.inputTextReadOnly);

return gameObject;
}

Expand Down
3 changes: 0 additions & 3 deletions templates/ui/tweaker/builders/CreateRangeInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ var CreateRangeInput = function (scene, config, style) {
var gameObject = new RangeInput(scene, style);
scene.add.existing(gameObject);

gameObject.setRange(config.min, config.max, config.step);
gameObject.setInputTextReadOnly(!!config.inputTextReadOnly);

return gameObject;
}

Expand Down
2 changes: 0 additions & 2 deletions templates/ui/tweaker/builders/CreateTextAreaInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ var CreateTextAreaInput = function (scene, config, style) {
var gameObject = new TextAreaInput(scene, style);
scene.add.existing(gameObject);

gameObject.setInputTextReadOnly(!!config.inputTextReadOnly);

return gameObject;
}

Expand Down
2 changes: 0 additions & 2 deletions templates/ui/tweaker/builders/CreateTextInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ var CreateTextInput = function (scene, config, style) {
var gameObject = new TextInput(scene, style);
scene.add.existing(gameObject);

gameObject.setInputTextReadOnly(!!config.inputTextReadOnly);

return gameObject;
}

Expand Down
28 changes: 9 additions & 19 deletions templates/ui/tweaker/gameobjects/inputfield/ButtonsInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,28 +44,18 @@ class ButtonsInput extends InputFiledBase {
this.setValue(option.value);
this._selectedIndex = undefined;
}, this);

}

get value() {
return this._value;
}

set value(value) {
if (this._value === value) {
return;
}
if (!this.validate(value)) {
value = this._value; // Back to previous value

this.setValueCallback = function(gameObject, value) {
var index = this._selectedIndex; // See list's 'button.click' event
if (index === undefined) {
index = GetOptionIndex(list.options, value);
}
SetButtonsActiveStateByIndex(list.childrenMap.buttons, index);
}

var list = this.childrenMap.list;
var index = this._selectedIndex; // See list's 'button.click' event
if (index === undefined) {
index = GetOptionIndex(list.options, value);
this.setupCallback = function(gameObject, config) {
gameObject.setOptions(config.options);
}
SetButtonsActiveStateByIndex(list.childrenMap.buttons, index);
super.value = value; // Fire 'valuechange' event
}

setOptions(options) {
Expand Down
16 changes: 2 additions & 14 deletions templates/ui/tweaker/gameobjects/inputfield/CheckboxInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,10 @@ class CheckboxInput extends InputFiledBase {
checkbox.on('valuechange', function (value) {
this.setValue(value);
}, this);
}

get value() {
return this._value;
}

set value(value) {
if (this._value === value) {
return;
this.setValueCallback = function(gameObject, value) {
checkbox.setValue(value);
}
if (!this.validate(value)) {
value = this._value; // Back to previous value
}

this.childrenMap.checkbox.setValue(value);
super.value = value; // Fire 'valuechange' event
}
}

Expand Down
16 changes: 2 additions & 14 deletions templates/ui/tweaker/gameobjects/inputfield/ColorInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,10 @@ class ColorInput extends InputFiledBase {
colorInput.on('valuechange', function (value) {
this.setValue(value);
}, this);
}

get value() {
return this._value;
}

set value(value) {
if (this._value === value) {
return;
this.setValueCallback = function(gameObject, value) {
colorInput.setValue(value);
}
if (!this.validate(value)) {
value = this._value; // Back to previous value
}

this.childrenMap.colorInput.setValue(value);
super.value = value; // Fire 'valuechange' event
}
}

Expand Down
38 changes: 35 additions & 3 deletions templates/ui/tweaker/gameobjects/inputfield/InputFieldBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,27 @@ class InputFiledBase extends Sizer {
return this.validateCallback(newValue, this._value, this.bindingTarget, this.bindingKey);
}

// Override
getFotmatText(value) {
if (this.textFormatCallback) {
value = this.textFormatCallback(value);
} else {
value = value.toString();
}
return value;
}

set value(value) {
if (this._value === value) {
return;
}
if (!this.validate(value)) {
value = this._value; // Back to previous value
}

if (this.setValueCallback) {
this.setValueCallback(this, value)
}

if (this._value === value) {
return;
}
Expand Down Expand Up @@ -57,12 +76,25 @@ class InputFiledBase extends Sizer {
}

setup(config) {
this.textFormatCallback = config.format;
this.validateCallback = config.onValidate;
this.setTextFormatCallback(config.format);
this.setValidateCallback(config.onValidate);

if (this.setupCallback) {
this.setupCallback(this, config)
}

return this;
}

setTextFormatCallback(callback) {
this.textFormatCallback = callback;
return this;
}

setValidateCallback(callback) {
this.validateCallback = callback;
return this;
}
}

export default InputFiledBase;
24 changes: 7 additions & 17 deletions templates/ui/tweaker/gameobjects/inputfield/ListInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,18 @@ class ListInput extends InputFiledBase {

this.addChildrenMap('list', list);

list.on('button.click', function (dropDownList, listPanel, button, index, pointer, event) {
list.on('button.click', function (dropDownList, listPanel, button, index, pointer, event) {
this.setValue(button.value);
}, this);

}

get value() {
return this._value;
}

set value(value) {
if (this._value === value) {
return;
}
if (!this.validate(value)) {
value = this._value; // Back to previous value
this.setValueCallback = function (gameObject, value) {
var text = GetOptionText(list.options, value);
list.resetDisplayContent({ text: text });
}

var list = this.childrenMap.list;
var text = GetOptionText(list.options, value);
list.resetDisplayContent({ text: text });
super.value = value; // Fire 'valuechange' event
this.setupCallback = function(gameObject, config) {
gameObject.setOptions(config.options);
}
}

setOptions(options) {
Expand Down
19 changes: 5 additions & 14 deletions templates/ui/tweaker/gameobjects/inputfield/NumberInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,14 @@ class NumberInput extends InputFiledBase {
inputText.on('close', function () {
this.setValue(inputText.value);
}, this);
}

get value() {
return this._value;
}

set value(value) {
if (this._value === value) {
return;
}
if (!this.validate(value)) {
value = this._value; // Back to previous value
this.setValueCallback = function (gameObject, value) {
inputText.setText(gameObject.getFotmatText(value));
}

var text = (this.textFormatCallback) ? this.textFormatCallback(value) : value;
this.childrenMap.inputText.setText(text);
super.value = value; // Fire 'valuechange' event
this.setupCallback = function(gameObject, config) {
gameObject.setInputTextReadOnly(!!config.inputTextReadOnly);
}
}

setInputTextReadOnly(enable) {
Expand Down
24 changes: 8 additions & 16 deletions templates/ui/tweaker/gameobjects/inputfield/RangeInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,17 @@ class RangeInput extends InputFiledBase {
this.setValue(value);
}, this);

}

get value() {
return this._value;
}

set value(value) {
if (this._value === value) {
return;
}
if (!this.validate(value)) {
value = this._value; // Back to previous value
this.setValueCallback = function (gameObject, value) {
inputText.setText('').setText(gameObject.getFotmatText(value));

slider.setValue(value, gameObject.minValue, gameObject.maxValue);
}

var text = (this.textFormatCallback) ? this.textFormatCallback(value) : value;
this.childrenMap.inputText.setText('').setText(text);
this.setupCallback = function(gameObject, config) {
gameObject.setInputTextReadOnly(!!config.inputTextReadOnly);

this.childrenMap.slider.setValue(value, this.minValue, this.maxValue);
super.value = value; // Fire 'valuechange' event
gameObject.setRange(config.min, config.max, config.step);
}
}

setRange(min, max, step) {
Expand Down
21 changes: 6 additions & 15 deletions templates/ui/tweaker/gameobjects/inputfield/TextAreaInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,14 @@ class TextAreaInput extends InputFiledBase {
inputText.on('close', function () {
this.setValue(inputText.value);
}, this);
}

get value() {
return this._value;
}

set value(value) {
if (this._value === value) {
return;
}
if (!this.validate(value)) {
value = this._value; // Back to previous value

this.setValueCallback = function (gameObject, value) {
inputText.setText(gameObject.getFotmatText(value));
}

var text = (this.textFormatCallback) ? this.textFormatCallback(value) : value;
this.childrenMap.inputText.setText(text);
super.value = value; // Fire 'valuechange' event
this.setupCallback = function(gameObject, config) {
gameObject.setInputTextReadOnly(!!config.inputTextReadOnly);
}
}

setInputTextReadOnly(enable) {
Expand Down
18 changes: 5 additions & 13 deletions templates/ui/tweaker/gameobjects/inputfield/TextInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,15 @@ class TextInput extends InputFiledBase {
inputText.on('close', function () {
this.setValue(inputText.value);
}, this);
}

get value() {
return this._value;
}

set value(value) {
if (this._value === value) {
return;
this.setValueCallback = function (gameObject, value) {
inputText.setText(gameObject.getFotmatText(value));
}
if (!this.validate(value)) {
value = this._value; // Back to previous value

this.setupCallback = function (gameObject, config) {
gameObject.setInputTextReadOnly(!!config.inputTextReadOnly);
}

var text = (this.textFormatCallback) ? this.textFormatCallback(value) : value;
this.childrenMap.inputText.setText(text);
super.value = value; // Fire 'valuechange' event
}

setInputTextReadOnly(enable) {
Expand Down
16 changes: 2 additions & 14 deletions templates/ui/tweaker/gameobjects/inputfield/ToggleSwitchInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,10 @@ class ToggleSwitchInput extends InputFiledBase {
toggleSwitch.on('valuechange', function (value) {
this.setValue(value);
}, this);
}

get value() {
return this._value;
}

set value(value) {
if (this._value === value) {
return;
this.setValueCallback = function (gameObject, value) {
toggleSwitch.setValue(value);
}
if (!this.validate(value)) {
value = this._value; // Back to previous value
}

this.childrenMap.toggleSwitch.setValue(value);
super.value = value; // Fire 'valuechange' event
}
}

Expand Down

0 comments on commit b3b1ab6

Please sign in to comment.