Skip to content

Commit

Permalink
Generate InputField class on-fly
Browse files Browse the repository at this point in the history
  • Loading branch information
rexrainbow committed Sep 25, 2023
1 parent 3b402d5 commit f579ee6
Show file tree
Hide file tree
Showing 13 changed files with 202 additions and 135 deletions.
11 changes: 11 additions & 0 deletions templates/ui/tweaker/Tweaker.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ class Tweaker extends TweakerShell {

RegisterDefaultInputHandlers.call(this);
}

destroy(fromScene) {
// This Game Object has already been destroyed
if (!this.scene || this.ignoreDestroy) {
return;
}

super.destroy(fromScene);

this.inputHandlers = undefined;
}
}

export default Tweaker
14 changes: 4 additions & 10 deletions templates/ui/tweaker/builders/CreateInputField.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import InputFiledBase from '../gameobjects/inputfield/InputFieldBase.js';
import GenerateInputFieldClass from '../gameobjects/inputfield/GenerateInputFieldClass.js';

var CreateInputField = function (scene, config, style) {
var value = undefined;
Expand All @@ -11,15 +11,9 @@ var CreateInputField = function (scene, config, style) {
var inputHandlers = this.inputHandlers;
for (var i = 0, cnt = inputHandlers.length; i < cnt; i++) {
var handler = inputHandlers[i];
if (!handler.hasOwnProperty('accept')) {
continue;
}
if (!handler.hasOwnProperty('build')) {
continue;
}

if (handler.accept(config, value)) {
inputField = new InputFiledBase(scene);
var InputFieldClass = GenerateInputFieldClass(handler.baseClass);
inputField = new InputFieldClass(scene);
scene.add.existing(inputField);

inputField
Expand All @@ -34,7 +28,7 @@ var CreateInputField = function (scene, config, style) {
}

// Setup by config
inputField.setup(config);
inputField.setup(config, true);

return inputField;
}
Expand Down
127 changes: 127 additions & 0 deletions templates/ui/tweaker/gameobjects/inputfield/GenerateInputFieldClass.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import Sizer from '../../../sizer/Sizer.js';

var GenerateInputFieldClass = function (BaseClass) {
if (BaseClass === undefined) {
BaseClass = Sizer;
}

class InputFiled extends BaseClass {
get bindingTarget() {
return this.getParentSizer().bindingTarget;
}

get bindingKey() {
return this.getParentSizer().bindTargetKey;
}

get value() {
return this._value;
}

validate(newValue) {
if (this.syncValueFlag || !this.validateCallback) {
return true;
}
return this.validateCallback(newValue, this._value, this.bindingTarget, this.bindingKey);
}

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.displayValueCallback) {
this.displayValueCallback(this, value)
}

if (this._value === value) {
return;
}

var oldValue = this._value;
this._value = value;

if (!this.syncValueFlag) {
this.emit('valuechange', value, oldValue, this.bindingTarget, this.bindingKey);
}
}

getValue() {
return this.value;
}

setValue(value) {
this.value = value;
return this;
}

/*
Internal method invoked when
- inputRow.setBindingTarget(target), or
- inputRow.syncTargetValue()
*/
syncValue(value) {
this.syncValueFlag = true;
this.value = value;
this.syncValueFlag = false;

return this;
}

setup(config, setDefaults) {
if (setDefaults === undefined) {
setDefaults = false;
}

if (setDefaults || config.hasOwnProperty('format')) {
this.setTextFormatCallback(config.format);
}

if (setDefaults || config.hasOwnProperty('onValidate')) {
this.setValidateCallback(config.onValidate);
}

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

return this;
}

setSetupCallback(callback) {
this.setupCallback = callback;
return this;
}

setDisplayValueCallback(callback) {
this.displayValueCallback = callback;
return this;
}

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

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

return InputFiled;
}

export default GenerateInputFieldClass;
110 changes: 0 additions & 110 deletions templates/ui/tweaker/gameobjects/inputfield/InputFieldBase.js

This file was deleted.

6 changes: 4 additions & 2 deletions templates/ui/tweaker/inputhandlers/ButtonsInputHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,10 @@ export default {
},

// Callback inside `setup()`
setup(gameObject, config) {
SetOptions(gameObject, config.options);
setup(gameObject, config, setDefaults) {
if (setDefaults || config.hasOwnProperty('options')) {
SetOptions(gameObject, config.options);
}
},

// Callback inside `setValue()`
Expand Down
6 changes: 4 additions & 2 deletions templates/ui/tweaker/inputhandlers/ListInputHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ export default {
},

// Callback inside `setup()`
setup(gameObject, config) {
SetOptions(gameObject, config.options);
setup(gameObject, config, setDefaults) {
if (setDefaults || config.hasOwnProperty('options')) {
SetOptions(gameObject, config.options);
}
},

// Callback inside `setValue()`
Expand Down
6 changes: 4 additions & 2 deletions templates/ui/tweaker/inputhandlers/NumberInputHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ export default {
},

// Callback inside `setup()`
setup(gameObject, config) {
SetInputTextReadOnly(gameObject, !!config.inputTextReadOnly);
setup(gameObject, config, setDefaults) {
if (setDefaults || config.hasOwnProperty('inputTextReadOnly')) {
SetInputTextReadOnly(gameObject, !!config.inputTextReadOnly);
}
},

// Callback inside `setValue()`
Expand Down
11 changes: 8 additions & 3 deletions templates/ui/tweaker/inputhandlers/RangeInputHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,14 @@ export default {
},

// Callback inside `setup()`
setup(gameObject, config) {
SetRange(gameObject, config.min, config.max, config.step);
SetInputTextReadOnly(gameObject, !!config.inputTextReadOnly);
setup(gameObject, config, setDefaults) {
if (setDefaults || config.hasOwnProperty('max')) {
SetRange(gameObject, config.min, config.max, config.step);
}

if (setDefaults || config.hasOwnProperty('inputTextReadOnly')) {
SetInputTextReadOnly(gameObject, !!config.inputTextReadOnly);
}
},

// Callback inside `setValue()`
Expand Down
6 changes: 4 additions & 2 deletions templates/ui/tweaker/inputhandlers/TextAreaInputHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ export default {
},

// Callback inside `setup()`
setup(gameObject, config) {
SetInputTextReadOnly(gameObject, !!config.inputTextReadOnly);
setup(gameObject, config, setDefaults) {
if (setDefaults || config.hasOwnProperty('inputTextReadOnly')) {
SetInputTextReadOnly(gameObject, !!config.inputTextReadOnly);
}
},

// Callback inside `setValue()`
Expand Down
6 changes: 4 additions & 2 deletions templates/ui/tweaker/inputhandlers/TextInputHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ export default {
},

// Callback inside `setup()`
setup(gameObject, config) {
SetInputTextReadOnly(gameObject, !!config.inputTextReadOnly);
setup(gameObject, config, setDefaults) {
if (setDefaults || config.hasOwnProperty('inputTextReadOnly')) {
SetInputTextReadOnly(gameObject, !!config.inputTextReadOnly);
}
},

// Callback inside `setValue()`
Expand Down
3 changes: 3 additions & 0 deletions templates/ui/tweaker/methods/Methods.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import RegisterInputHandler from './RegisterInputHandler.js';
import RemoveInputHandler from './RemoveInputHandler.js';
import AddFolder from './AddFolder.js';
import AddTab from './AddTab.js';
import AddInput from './AddInput.js';
Expand All @@ -10,6 +11,8 @@ import SetInputRowTitleWidth from './SetInputRowTitleWidth.js';

var methods = {
registerInputHandler: RegisterInputHandler,
removeInputHandler: RemoveInputHandler,

addFolder: AddFolder,
addTab: AddTab,
addInput: AddInput,
Expand Down
Loading

0 comments on commit f579ee6

Please sign in to comment.