diff --git a/Assets/Examples/Resources/data.meta b/Assets/Examples/Resources/data.meta new file mode 100644 index 00000000..e6ecf7c2 --- /dev/null +++ b/Assets/Examples/Resources/data.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: c297363878d8ee54bbd09387a3f359b6 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Examples/Resources/data/js_data.asset b/Assets/Examples/Resources/data/js_data.asset new file mode 100644 index 00000000..a64135b2 --- /dev/null +++ b/Assets/Examples/Resources/data/js_data.asset @@ -0,0 +1,27 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!114 &11400000 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: c74456a7d35a921468a4a2a23d99cee4, type: 3} + m_Name: js_data + m_EditorClassIdentifier: + _scriptRef: + sourceFile: C:\Users\julio\Documents\Projects\github.com\ialex32x\unity-jsb\Scripts\src\example_scriptable_object.ts + modulePath: example_scriptable_object + className: MyScriptableObject + _properties: + _objects: [] + _strings: + - key: value2 + value: hello, world + _integers: [] + _numbers: + - key: value1 + value: 7 diff --git a/Assets/Examples/Resources/data/js_data.asset.meta b/Assets/Examples/Resources/data/js_data.asset.meta new file mode 100644 index 00000000..19b1f997 --- /dev/null +++ b/Assets/Examples/Resources/data/js_data.asset.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: e8bd9d24a9d4d8346923ac0a8da9c1a5 +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 11400000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/jsb/Source/Binding/Values_push_class.cs b/Assets/jsb/Source/Binding/Values_push_class.cs index b0d3f238..040a7de7 100644 --- a/Assets/jsb/Source/Binding/Values_push_class.cs +++ b/Assets/jsb/Source/Binding/Values_push_class.cs @@ -16,6 +16,13 @@ public static JSValue js_push_classvalue(JSContext ctx, UnityEngine.Object o) { return JSApi.JS_UNDEFINED; } + + //TODO: 因为 ScriptableObject.OnEnable 的触发可能早于 Runtime 初始化, 需要一个地方补充一次脚本创建, 放在这里不合适 + if (o is Unity.JSScriptableObject) + { + (o as Unity.JSScriptableObject).CreateScriptInstance(); + } + return js_push_object(ctx, (object)o); } #endif diff --git a/Assets/jsb/Source/Unity/Editor/JSInspectorBase.cs b/Assets/jsb/Source/Unity/Editor/JSInspectorBase.cs index c9818267..37463b75 100644 --- a/Assets/jsb/Source/Unity/Editor/JSInspectorBase.cs +++ b/Assets/jsb/Source/Unity/Editor/JSInspectorBase.cs @@ -12,7 +12,7 @@ namespace QuickJS.Unity public abstract class JSInspectorBase : Editor where T : Object, IScriptEditorSupport { - private T _target; + protected T _target; protected JSScriptClassType _classType; private string[] _tabViews = new string[] { "Editor", "Source", "Primitive" }; @@ -445,12 +445,17 @@ private void DrawScriptingView() else { EditorGUILayout.HelpBox("Waiting for script instancing...", MessageType.Warning); + OnWaitingForScriptInstancing(); } } + protected virtual void OnWaitingForScriptInstancing() + { + } + public override void OnInspectorGUI() { - if (UnityEditor.EditorApplication.isCompiling) + if (EditorApplication.isCompiling || (!EditorApplication.isPlaying && EditorApplication.isPlayingOrWillChangePlaymode)) { Release(); EditorGUILayout.HelpBox("Temporarily unavailable in the script compilation process", MessageType.Warning); @@ -472,4 +477,4 @@ public override void OnInspectorGUI() } } } -#endif \ No newline at end of file +#endif diff --git a/Assets/jsb/Source/Unity/Editor/JSScriptableObjectInspector.cs b/Assets/jsb/Source/Unity/Editor/JSScriptableObjectInspector.cs index 16648bac..1756fb0c 100644 --- a/Assets/jsb/Source/Unity/Editor/JSScriptableObjectInspector.cs +++ b/Assets/jsb/Source/Unity/Editor/JSScriptableObjectInspector.cs @@ -17,6 +17,14 @@ protected override JSScriptClassType GetScriptClassType() { return JSScriptClassType.ScriptableObject; } + + protected override void OnWaitingForScriptInstancing() + { + if (_target.enabled) + { + _target.CreateScriptInstance(); + } + } } } #endif diff --git a/Assets/jsb/Source/Unity/JSBehaviour.cs b/Assets/jsb/Source/Unity/JSBehaviour.cs index f723a888..56c737df 100644 --- a/Assets/jsb/Source/Unity/JSBehaviour.cs +++ b/Assets/jsb/Source/Unity/JSBehaviour.cs @@ -34,6 +34,8 @@ public class JSBehaviour : MonoBehaviour, ISerializationCallbackReceiver, IScrip // self controlled script instance lifetime private bool _isStandaloneScript = false; public bool isStandaloneScript => _isStandaloneScript; +#else + public bool isStandaloneScript => true; #endif private JSContext _ctx = JSContext.Null; diff --git a/Assets/jsb/Source/Unity/JSScriptableObject.cs b/Assets/jsb/Source/Unity/JSScriptableObject.cs index 08ff0976..23f3b224 100644 --- a/Assets/jsb/Source/Unity/JSScriptableObject.cs +++ b/Assets/jsb/Source/Unity/JSScriptableObject.cs @@ -8,6 +8,9 @@ namespace QuickJS.Unity using UnityEngine; using UnityEngine.Serialization; + //TODO: 因为 ScriptableObject.OnEnable 的触发可能早于 Runtime 初始化, 需要一个地方补充一次脚本创建 + //TODO: 相关临时代码目前位于 Values_push_class.cs: public static JSValue js_push_classvalue(JSContext ctx, UnityEngine.Object o) + [CreateAssetMenu(fileName = "js_data", menuName = "JSScriptableObject Asset", order = 100)] public class JSScriptableObject : ScriptableObject, ISerializationCallbackReceiver, IScriptEditorSupport { @@ -22,15 +25,16 @@ public class JSScriptableObject : ScriptableObject, ISerializationCallbackReceiv // internal use only public JSScriptProperties properties => _properties; + private bool _enabled; + + public bool enabled => _enabled; + private bool _isScriptInstanced = false; public bool isScriptInstanced => _isScriptInstanced; -#if UNITY_EDITOR // self controlled script instance lifetime - private bool _isStandaloneScript = false; - public bool isStandaloneScript => _isStandaloneScript; -#endif + public bool isStandaloneScript => true; JSScriptRef IScriptEditorSupport.scriptRef { get { return _scriptRef; } set { _scriptRef = value; } } @@ -39,9 +43,6 @@ public class JSScriptableObject : ScriptableObject, ISerializationCallbackReceiv private JSContext _ctx = JSContext.Null; private JSValue _this_obj = JSApi.JS_UNDEFINED; - private bool _onDestroyValid; - private JSValue _onDestroyFunc = JSApi.JS_UNDEFINED; - private bool _onBeforeSerializeValid; private JSValue _onBeforeSerializeFunc = JSApi.JS_UNDEFINED; @@ -60,12 +61,10 @@ public bool IsValid() public void ReleaseJSValues() { + var context = ScriptEngine.GetContext(_ctx); + if (!_this_obj.IsNullish()) { - JSApi.JS_FreeValue(_ctx, _onDestroyFunc); - _onDestroyFunc = JSApi.JS_UNDEFINED; - _onDestroyValid = false; - JSApi.JS_FreeValue(_ctx, _onBeforeSerializeFunc); _onBeforeSerializeFunc = JSApi.JS_UNDEFINED; _onBeforeSerializeValid = false; @@ -77,8 +76,8 @@ public void ReleaseJSValues() JSApi.JS_FreeValue(_ctx, _this_obj); _this_obj = JSApi.JS_UNDEFINED; } + _isScriptInstanced = false; - var context = ScriptEngine.GetContext(_ctx); _ctx = JSContext.Null; if (context != null) @@ -153,7 +152,7 @@ public bool CreateScriptInstance() } else { - Debug.LogError("script runtime not ready"); + // Debug.LogError("script runtime not ready"); } } else @@ -249,9 +248,6 @@ private void _SetScriptInstance(JSContext ctx, JSValue this_obj, bool execAwake) _onAfterDeserializeFunc = JSApi.JS_GetProperty(ctx, this_obj, context.GetAtom("OnAfterDeserialize")); _onAfterDeserializeValid = JSApi.JS_IsFunction(ctx, _onAfterDeserializeFunc) == 1; - _onDestroyFunc = JSApi.JS_GetProperty(ctx, this_obj, context.GetAtom("OnDestroy")); - _onDestroyValid = JSApi.JS_IsFunction(ctx, _onDestroyFunc) == 1; - this._OnScriptingAfterDeserialize(); } } @@ -280,29 +276,6 @@ public void ReleaseScriptInstance() ReleaseJSValues(); } - void OnDisable() - { -#if UNITY_EDITOR - if (UnityEditor.EditorApplication.isCompiling) - { - ReleaseJSValues(); - } -#endif - } - - void OnDestroy() - { - if (_onDestroyValid) - { - var rval = JSApi.JS_Call(_ctx, _onDestroyFunc, _this_obj); - if (rval.IsException()) - { - _ctx.print_exception(); - } - JSApi.JS_FreeValue(_ctx, rval); - } - ReleaseJSValues(); - } public void OnBeforeSerialize() { if (_onBeforeSerializeValid) @@ -337,14 +310,18 @@ public void OnAfterDeserialize() { } - void Awake() + void OnEnable() { -#if UNITY_EDITOR - _isStandaloneScript = true; -#endif + _enabled = true; CreateScriptInstance(); } + void OnDisable() + { + _enabled = false; + ReleaseJSValues(); + } + public void _OnScriptingAfterDeserialize() { if (_onAfterDeserializeValid) diff --git a/Scripts/out/data.js b/Scripts/out/data.js index c3574e74..652e3566 100644 --- a/Scripts/out/data.js +++ b/Scripts/out/data.js @@ -1,8 +1,8 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.test_data = void 0; -exports.test_data = { - "name": "Jane", - "id": 1 -}; +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.test_data = void 0; +exports.test_data = { + "name": "Jane", + "id": 1 +}; //# sourceMappingURL=data.js.map \ No newline at end of file diff --git a/Scripts/out/editor/js_asset_process.js b/Scripts/out/editor/js_asset_process.js index 866821de..c90ea46f 100644 --- a/Scripts/out/editor/js_asset_process.js +++ b/Scripts/out/editor/js_asset_process.js @@ -1,14 +1,14 @@ -["OnPostprocessTexture", "OnPostprocessModel", "OnPostprocessAudio", "OnPostprocessMaterial", "OnPostprocessAllAssets"].forEach(k => { - globalThis[k] = function () { - const p = require("./asset_importer")[k]; - if (p) { - try { - p(...arguments); - } - catch (e) { - console.error(e); - } - } - }; -}); +["OnPostprocessTexture", "OnPostprocessModel", "OnPostprocessAudio", "OnPostprocessMaterial", "OnPostprocessAllAssets"].forEach(k => { + globalThis[k] = function () { + const p = require("./asset_importer")[k]; + if (p) { + try { + p(...arguments); + } + catch (e) { + console.error(e); + } + } + }; +}); //# sourceMappingURL=js_asset_process.js.map \ No newline at end of file diff --git a/Scripts/out/editor/js_test.js b/Scripts/out/editor/js_test.js index 13a0e037..f13faceb 100644 --- a/Scripts/out/editor/js_test.js +++ b/Scripts/out/editor/js_test.js @@ -1,5 +1,5 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -const js_test2_1 = require("./js_test2"); -console.log("[js_test] revision 4 ## read from ", js_test2_1.MyName); +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +const js_test2_1 = require("./js_test2"); +console.log("[js_test] revision 4 ## read from ", js_test2_1.MyName); //# sourceMappingURL=js_test.js.map \ No newline at end of file diff --git a/Scripts/out/editor/js_test2.js b/Scripts/out/editor/js_test2.js index a6cefa77..9f3d50d8 100644 --- a/Scripts/out/editor/js_test2.js +++ b/Scripts/out/editor/js_test2.js @@ -1,6 +1,6 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.MyName = void 0; -exports.MyName = "jstest2 revision 6"; -console.log("[js_test2]", exports.MyName); +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.MyName = void 0; +exports.MyName = "jstest2 revision 6"; +console.log("[js_test2]", exports.MyName); //# sourceMappingURL=js_test2.js.map \ No newline at end of file diff --git a/Scripts/out/example_scriptable_object.js b/Scripts/out/example_scriptable_object.js index d5768281..d4b90f19 100644 --- a/Scripts/out/example_scriptable_object.js +++ b/Scripts/out/example_scriptable_object.js @@ -1,40 +1,39 @@ -"use strict"; -var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { - var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; - if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); - else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; - return c > 3 && r && Object.defineProperty(target, key, r), r; -}; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.MyScriptableObject = void 0; -const UnityEngine_1 = require("UnityEngine"); -const editor_decorators_1 = require("./plover/editor/editor_decorators"); -console.warn("此功能未完成"); -console.warn("此功能未完成"); -console.warn("此功能未完成"); -let MyScriptableObject = class MyScriptableObject extends UnityEngine_1.ScriptableObject { - constructor() { - super(...arguments); - this.value1 = 1; - this.value2 = "hello"; - } -}; -__decorate([ - editor_decorators_1.ScriptNumber() -], MyScriptableObject.prototype, "value1", void 0); -__decorate([ - editor_decorators_1.ScriptString() -], MyScriptableObject.prototype, "value2", void 0); -MyScriptableObject = __decorate([ - editor_decorators_1.ScriptAsset() -], MyScriptableObject); -exports.MyScriptableObject = MyScriptableObject; -let js_data = UnityEngine_1.Resources.Load("data/js_data"); -if (js_data) { - console.log("type check:", js_data instanceof MyScriptableObject); - console.log("type values:", js_data.value1, js_data.value2); -} -else { - console.error("failed to load js_data, please create the asset at first."); -} +"use strict"; +var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { + var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; + if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); + else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; + return c > 3 && r && Object.defineProperty(target, key, r), r; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.MyScriptableObject = void 0; +const UnityEngine_1 = require("UnityEngine"); +const editor_decorators_1 = require("./plover/editor/editor_decorators"); +let MyScriptableObject = class MyScriptableObject extends UnityEngine_1.ScriptableObject { + constructor() { + super(...arguments); + this.value1 = 1; + this.value2 = "hello"; + } +}; +__decorate([ + editor_decorators_1.ScriptNumber() +], MyScriptableObject.prototype, "value1", void 0); +__decorate([ + editor_decorators_1.ScriptString() +], MyScriptableObject.prototype, "value2", void 0); +MyScriptableObject = __decorate([ + editor_decorators_1.ScriptAsset() +], MyScriptableObject); +exports.MyScriptableObject = MyScriptableObject; +if (require.main == module) { + let js_data = UnityEngine_1.Resources.Load("data/js_data"); + if (js_data) { + console.log("type check:", js_data instanceof MyScriptableObject); + console.log("type values:", js_data.value1, js_data.value2); + } + else { + console.error("failed to load js_data, please create the asset at first."); + } +} //# sourceMappingURL=example_scriptable_object.js.map \ No newline at end of file diff --git a/Scripts/out/example_scriptable_object.js.map b/Scripts/out/example_scriptable_object.js.map index 7ad95844..aa075c7c 100644 --- a/Scripts/out/example_scriptable_object.js.map +++ b/Scripts/out/example_scriptable_object.js.map @@ -1 +1 @@ -{"version":3,"file":"example_scriptable_object.js","sourceRoot":"","sources":["../src/example_scriptable_object.ts"],"names":[],"mappings":";;;;;;;;;AAAA,6CAA0D;AAC1D,yEAA4F;AAE5F,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;AACvB,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;AACvB,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;AAGvB,IAAa,kBAAkB,GAA/B,MAAa,kBAAmB,SAAQ,8BAAgB;IAAxD;;QAEI,WAAM,GAAG,CAAC,CAAC;QAGX,WAAM,GAAG,OAAO,CAAC;IACrB,CAAC;CAAA,CAAA;AAJG;IADC,gCAAY,EAAE;kDACJ;AAGX;IADC,gCAAY,EAAE;kDACE;AALR,kBAAkB;IAD9B,+BAAW,EAAE;GACD,kBAAkB,CAM9B;AANY,gDAAkB;AAQ/B,IAAI,OAAO,GAAuB,uBAAS,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC;AAEjE,IAAI,OAAO,EAAE;IACT,OAAO,CAAC,GAAG,CAAC,aAAa,EAAE,OAAO,YAAY,kBAAkB,CAAC,CAAC;IAClE,OAAO,CAAC,GAAG,CAAC,cAAc,EAAE,OAAO,CAAC,MAAM,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC;CAC/D;KAAM;IACH,OAAO,CAAC,KAAK,CAAC,2DAA2D,CAAC,CAAC;CAC9E"} \ No newline at end of file +{"version":3,"file":"example_scriptable_object.js","sourceRoot":"","sources":["../src/example_scriptable_object.ts"],"names":[],"mappings":";;;;;;;;;AAAA,6CAA0D;AAC1D,yEAA4F;AAG5F,IAAa,kBAAkB,GAA/B,MAAa,kBAAmB,SAAQ,8BAAgB;IAAxD;;QAEI,WAAM,GAAG,CAAC,CAAC;QAGX,WAAM,GAAG,OAAO,CAAC;IACrB,CAAC;CAAA,CAAA;AAJG;IADC,gCAAY,EAAE;kDACJ;AAGX;IADC,gCAAY,EAAE;kDACE;AALR,kBAAkB;IAD9B,+BAAW,EAAE;GACD,kBAAkB,CAM9B;AANY,gDAAkB;AAQ/B,IAAI,OAAO,CAAC,IAAI,IAAI,MAAM,EAAE;IACxB,IAAI,OAAO,GAAuB,uBAAS,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC;IAEjE,IAAI,OAAO,EAAE;QACT,OAAO,CAAC,GAAG,CAAC,aAAa,EAAE,OAAO,YAAY,kBAAkB,CAAC,CAAC;QAClE,OAAO,CAAC,GAAG,CAAC,cAAc,EAAE,OAAO,CAAC,MAAM,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC;KAC/D;SAAM;QACH,OAAO,CAAC,KAAK,CAAC,2DAA2D,CAAC,CAAC;KAC9E;CACJ"} \ No newline at end of file diff --git a/Scripts/out/game_demo.js b/Scripts/out/game_demo.js index 30515e2a..295d0c8f 100644 --- a/Scripts/out/game_demo.js +++ b/Scripts/out/game_demo.js @@ -1,15 +1,15 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -const UnityEngine_1 = require("UnityEngine"); -if (!UnityEngine_1.GameObject.Find("/game_stage")) { - let path = "prefab/game_stage"; - let prefab = UnityEngine_1.Resources.Load(path); - if (prefab) { - let inst = UnityEngine_1.Object.Instantiate(prefab); - inst.name = "game_stage"; - } - else { - console.error("game stage not found:", path); - } -} +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +const UnityEngine_1 = require("UnityEngine"); +if (!UnityEngine_1.GameObject.Find("/game_stage")) { + let path = "prefab/game_stage"; + let prefab = UnityEngine_1.Resources.Load(path); + if (prefab) { + let inst = UnityEngine_1.Object.Instantiate(prefab); + inst.name = "game_stage"; + } + else { + console.error("game stage not found:", path); + } +} //# sourceMappingURL=game_demo.js.map \ No newline at end of file diff --git a/Scripts/out/plover/editor/auto_completion_field.js b/Scripts/out/plover/editor/auto_completion_field.js index 3bca9426..db9d8d0c 100644 --- a/Scripts/out/plover/editor/auto_completion_field.js +++ b/Scripts/out/plover/editor/auto_completion_field.js @@ -1,161 +1,161 @@ -"use strict"; -/* -https://github.com/marijnz/unity-autocomplete-search-field -*/ -Object.defineProperty(exports, "__esModule", { value: true }); -exports.AutoCompletionField = void 0; -const UnityEditor_1 = require("UnityEditor"); -const UnityEditor_IMGUI_Controls_1 = require("UnityEditor.IMGUI.Controls"); -const UnityEngine_1 = require("UnityEngine"); -const dispatcher_1 = require("../events/dispatcher"); -let Styles = { - resultHeight: 20, - resultsBorderWidth: 2, - resultsMargin: 15, - resultsLabelOffset: 2, - entryEven: UnityEngine_1.GUIStyle.op_Implicit("CN EntryBackEven"), - entryOdd: UnityEngine_1.GUIStyle.op_Implicit("CN EntryBackOdd"), - labelStyle: new UnityEngine_1.GUIStyle(UnityEditor_1.EditorStyles.label), - resultsBorderStyle: UnityEngine_1.GUIStyle.op_Implicit("hostview"), -}; -Styles.labelStyle.alignment = UnityEngine_1.TextAnchor.MiddleLeft; -Styles.labelStyle.richText = true; -class AutoCompletionField extends dispatcher_1.EventDispatcher { - constructor() { - super(); - this.searchString = ""; - this.maxResults = 15; - this.results = []; - this.selectedIndex = -1; - this.previousMousePosition = UnityEngine_1.Vector2.zero; - this.selectedIndexByMouse = false; - this.showResults = false; - } - addResult(result) { - this.results.push(result); - } - clearResults() { - this.results.splice(0); - } - onToolbarGUI() { - this.draw(true); - } - onGUI() { - this.draw(false); - } - draw(asToolbar) { - let rect = UnityEngine_1.GUILayoutUtility.GetRect(1, 1, 18, 18, UnityEngine_1.GUILayout.ExpandWidth(true)); - UnityEngine_1.GUILayout.BeginHorizontal(); - this.doSearchField(rect, asToolbar); - UnityEngine_1.GUILayout.EndHorizontal(); - rect.y += 18; - this.doResults(rect); - } - doSearchField(rect, asToolbar) { - if (this.searchField == null) { - this.searchField = new UnityEditor_IMGUI_Controls_1.SearchField(); - this.searchField.downOrUpArrowKeyPressed("add", this.onDownOrUpArrowKeyPressed.bind(this)); - } - var result = asToolbar - ? this.searchField.OnToolbarGUI(rect, this.searchString) - : this.searchField.OnGUI(rect, this.searchString); - if (typeof result === "string") { - if (result != this.searchString) { - this.dispatch("change", result); - this.selectedIndex = -1; - this.showResults = true; - } - this.searchString = result; - if (this.hasSearchbarFocused()) { - this.repaintFocusedWindow(); - } - } - } - onDownOrUpArrowKeyPressed() { - let current = UnityEngine_1.Event.current; - if (current.keyCode == UnityEngine_1.KeyCode.UpArrow) { - current.Use(); - this.selectedIndex--; - this.selectedIndexByMouse = false; - } - else { - current.Use(); - this.selectedIndex++; - this.selectedIndexByMouse = false; - } - if (this.selectedIndex >= this.results.length) - this.selectedIndex = this.results.length - 1; - else if (this.selectedIndex < 0) - this.selectedIndex = -1; - } - doResults(rect) { - if (this.results.length <= 0 || !this.showResults) - return; - var current = UnityEngine_1.Event.current; - rect.height = Styles.resultHeight * Math.min(this.maxResults, this.results.length); - rect.x = Styles.resultsMargin; - rect.width -= Styles.resultsMargin * 2; - var elementRect = new UnityEngine_1.Rect(rect); - rect.height += Styles.resultsBorderWidth; - UnityEngine_1.GUI.Label(rect, "", Styles.resultsBorderStyle); - var mouseIsInResultsRect = rect.Contains(current.mousePosition); - if (mouseIsInResultsRect) { - this.repaintFocusedWindow(); - } - var movedMouseInRect = this.previousMousePosition != current.mousePosition; - elementRect.x += Styles.resultsBorderWidth; - elementRect.width -= Styles.resultsBorderWidth * 2; - elementRect.height = Styles.resultHeight; - var didJustSelectIndex = false; - for (var i = 0; i < this.results.length && i < this.maxResults; i++) { - if (current.type == UnityEngine_1.EventType.Repaint) { - var style = i % 2 == 0 ? Styles.entryOdd : Styles.entryEven; - style.Draw(elementRect, false, false, i == this.selectedIndex, false); - var labelRect = new UnityEngine_1.Rect(elementRect); - labelRect.x += Styles.resultsLabelOffset; - UnityEngine_1.GUI.Label(labelRect, this.results[i], Styles.labelStyle); - } - if (elementRect.Contains(current.mousePosition)) { - if (movedMouseInRect) { - this.selectedIndex = i; - this.selectedIndexByMouse = true; - didJustSelectIndex = true; - } - if (current.type == UnityEngine_1.EventType.MouseDown) { - this.onConfirm(this.results[i]); - } - } - elementRect.y += Styles.resultHeight; - } - if (current.type == UnityEngine_1.EventType.Repaint && !didJustSelectIndex && !mouseIsInResultsRect && this.selectedIndexByMouse) { - this.selectedIndex = -1; - } - if ((UnityEngine_1.GUIUtility.hotControl != this.searchField.searchFieldControlID && UnityEngine_1.GUIUtility.hotControl > 0) - || (current.rawType == UnityEngine_1.EventType.MouseDown && !mouseIsInResultsRect)) { - this.showResults = false; - } - if (current.type == UnityEngine_1.EventType.KeyUp && current.keyCode == UnityEngine_1.KeyCode.Return && this.selectedIndex >= 0) { - this.onConfirm(this.results[this.selectedIndex]); - } - if (current.type == UnityEngine_1.EventType.Repaint) { - this.previousMousePosition = current.mousePosition; - } - } - onConfirm(result) { - this.searchString = result; - this.dispatch("confirm", result); - this.dispatch("change", result); - this.repaintFocusedWindow(); - UnityEngine_1.GUIUtility.keyboardControl = 0; // To avoid Unity sometimes not updating the search field text - } - hasSearchbarFocused() { - return UnityEngine_1.GUIUtility.keyboardControl == this.searchField.searchFieldControlID; - } - repaintFocusedWindow() { - if (UnityEditor_1.EditorWindow.focusedWindow != null) { - UnityEditor_1.EditorWindow.focusedWindow.Repaint(); - } - } -} -exports.AutoCompletionField = AutoCompletionField; +"use strict"; +/* +https://github.com/marijnz/unity-autocomplete-search-field +*/ +Object.defineProperty(exports, "__esModule", { value: true }); +exports.AutoCompletionField = void 0; +const UnityEditor_1 = require("UnityEditor"); +const UnityEditor_IMGUI_Controls_1 = require("UnityEditor.IMGUI.Controls"); +const UnityEngine_1 = require("UnityEngine"); +const dispatcher_1 = require("../events/dispatcher"); +let Styles = { + resultHeight: 20, + resultsBorderWidth: 2, + resultsMargin: 15, + resultsLabelOffset: 2, + entryEven: UnityEngine_1.GUIStyle.op_Implicit("CN EntryBackEven"), + entryOdd: UnityEngine_1.GUIStyle.op_Implicit("CN EntryBackOdd"), + labelStyle: new UnityEngine_1.GUIStyle(UnityEditor_1.EditorStyles.label), + resultsBorderStyle: UnityEngine_1.GUIStyle.op_Implicit("hostview"), +}; +Styles.labelStyle.alignment = UnityEngine_1.TextAnchor.MiddleLeft; +Styles.labelStyle.richText = true; +class AutoCompletionField extends dispatcher_1.EventDispatcher { + constructor() { + super(); + this.searchString = ""; + this.maxResults = 15; + this.results = []; + this.selectedIndex = -1; + this.previousMousePosition = UnityEngine_1.Vector2.zero; + this.selectedIndexByMouse = false; + this.showResults = false; + } + addResult(result) { + this.results.push(result); + } + clearResults() { + this.results.splice(0); + } + onToolbarGUI() { + this.draw(true); + } + onGUI() { + this.draw(false); + } + draw(asToolbar) { + let rect = UnityEngine_1.GUILayoutUtility.GetRect(1, 1, 18, 18, UnityEngine_1.GUILayout.ExpandWidth(true)); + UnityEngine_1.GUILayout.BeginHorizontal(); + this.doSearchField(rect, asToolbar); + UnityEngine_1.GUILayout.EndHorizontal(); + rect.y += 18; + this.doResults(rect); + } + doSearchField(rect, asToolbar) { + if (this.searchField == null) { + this.searchField = new UnityEditor_IMGUI_Controls_1.SearchField(); + this.searchField.downOrUpArrowKeyPressed("add", this.onDownOrUpArrowKeyPressed.bind(this)); + } + var result = asToolbar + ? this.searchField.OnToolbarGUI(rect, this.searchString) + : this.searchField.OnGUI(rect, this.searchString); + if (typeof result === "string") { + if (result != this.searchString) { + this.dispatch("change", result); + this.selectedIndex = -1; + this.showResults = true; + } + this.searchString = result; + if (this.hasSearchbarFocused()) { + this.repaintFocusedWindow(); + } + } + } + onDownOrUpArrowKeyPressed() { + let current = UnityEngine_1.Event.current; + if (current.keyCode == UnityEngine_1.KeyCode.UpArrow) { + current.Use(); + this.selectedIndex--; + this.selectedIndexByMouse = false; + } + else { + current.Use(); + this.selectedIndex++; + this.selectedIndexByMouse = false; + } + if (this.selectedIndex >= this.results.length) + this.selectedIndex = this.results.length - 1; + else if (this.selectedIndex < 0) + this.selectedIndex = -1; + } + doResults(rect) { + if (this.results.length <= 0 || !this.showResults) + return; + var current = UnityEngine_1.Event.current; + rect.height = Styles.resultHeight * Math.min(this.maxResults, this.results.length); + rect.x = Styles.resultsMargin; + rect.width -= Styles.resultsMargin * 2; + var elementRect = new UnityEngine_1.Rect(rect); + rect.height += Styles.resultsBorderWidth; + UnityEngine_1.GUI.Label(rect, "", Styles.resultsBorderStyle); + var mouseIsInResultsRect = rect.Contains(current.mousePosition); + if (mouseIsInResultsRect) { + this.repaintFocusedWindow(); + } + var movedMouseInRect = this.previousMousePosition != current.mousePosition; + elementRect.x += Styles.resultsBorderWidth; + elementRect.width -= Styles.resultsBorderWidth * 2; + elementRect.height = Styles.resultHeight; + var didJustSelectIndex = false; + for (var i = 0; i < this.results.length && i < this.maxResults; i++) { + if (current.type == UnityEngine_1.EventType.Repaint) { + var style = i % 2 == 0 ? Styles.entryOdd : Styles.entryEven; + style.Draw(elementRect, false, false, i == this.selectedIndex, false); + var labelRect = new UnityEngine_1.Rect(elementRect); + labelRect.x += Styles.resultsLabelOffset; + UnityEngine_1.GUI.Label(labelRect, this.results[i], Styles.labelStyle); + } + if (elementRect.Contains(current.mousePosition)) { + if (movedMouseInRect) { + this.selectedIndex = i; + this.selectedIndexByMouse = true; + didJustSelectIndex = true; + } + if (current.type == UnityEngine_1.EventType.MouseDown) { + this.onConfirm(this.results[i]); + } + } + elementRect.y += Styles.resultHeight; + } + if (current.type == UnityEngine_1.EventType.Repaint && !didJustSelectIndex && !mouseIsInResultsRect && this.selectedIndexByMouse) { + this.selectedIndex = -1; + } + if ((UnityEngine_1.GUIUtility.hotControl != this.searchField.searchFieldControlID && UnityEngine_1.GUIUtility.hotControl > 0) + || (current.rawType == UnityEngine_1.EventType.MouseDown && !mouseIsInResultsRect)) { + this.showResults = false; + } + if (current.type == UnityEngine_1.EventType.KeyUp && current.keyCode == UnityEngine_1.KeyCode.Return && this.selectedIndex >= 0) { + this.onConfirm(this.results[this.selectedIndex]); + } + if (current.type == UnityEngine_1.EventType.Repaint) { + this.previousMousePosition = current.mousePosition; + } + } + onConfirm(result) { + this.searchString = result; + this.dispatch("confirm", result); + this.dispatch("change", result); + this.repaintFocusedWindow(); + UnityEngine_1.GUIUtility.keyboardControl = 0; // To avoid Unity sometimes not updating the search field text + } + hasSearchbarFocused() { + return UnityEngine_1.GUIUtility.keyboardControl == this.searchField.searchFieldControlID; + } + repaintFocusedWindow() { + if (UnityEditor_1.EditorWindow.focusedWindow != null) { + UnityEditor_1.EditorWindow.focusedWindow.Repaint(); + } + } +} +exports.AutoCompletionField = AutoCompletionField; //# sourceMappingURL=auto_completion_field.js.map \ No newline at end of file diff --git a/Scripts/out/plover/editor/editor_decorators.js b/Scripts/out/plover/editor/editor_decorators.js index 4177092c..81450f68 100644 --- a/Scripts/out/plover/editor/editor_decorators.js +++ b/Scripts/out/plover/editor/editor_decorators.js @@ -1,275 +1,275 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.SerializationUtil = exports.EditorUtil = exports.DefaultEditor = exports.ScriptProperty = exports.ScriptObject = exports.ScriptString = exports.ScriptNumber = exports.ScriptInteger = exports.ScriptEditor = exports.ScriptType = exports.ScriptAsset = void 0; -const UnityEditor_1 = require("UnityEditor"); -const UnityEngine_1 = require("UnityEngine"); -let Symbol_SerializedFields = Symbol.for("SerializedFields"); -let Symbol_PropertiesTouched = Symbol.for("PropertiesTouched"); -let Symbol_CustomEditor = Symbol.for("CustomEditor"); -function ScriptAsset(meta) { - return ScriptType(meta); -} -exports.ScriptAsset = ScriptAsset; -// expose this script class type to JSBehaviour, so you can put it on a prefab gameObject -function ScriptType(meta) { - return function (target) { - let OnBeforeSerialize = target.prototype["OnBeforeSerialize"]; - target.prototype["OnBeforeSerialize"] = function (ps) { - this[Symbol_PropertiesTouched] = false; - if (typeof OnBeforeSerialize === "function") { - OnBeforeSerialize.call(this, ps); - } - if (!this[Symbol_PropertiesTouched]) { - SerializationUtil.serialize(this, ps); - } - }; - let OnAfterDeserialize = target.prototype["OnAfterDeserialize"]; - target.prototype["OnAfterDeserialize"] = function (ps) { - this[Symbol_PropertiesTouched] = false; - if (typeof OnAfterDeserialize === "function") { - OnAfterDeserialize.call(this, ps); - } - if (!this[Symbol_PropertiesTouched]) { - SerializationUtil.deserialize(this, ps); - } - }; - return target; - }; -} -exports.ScriptType = ScriptType; -function ScriptEditor(forType) { - return function (editorType) { - forType.prototype[Symbol_CustomEditor] = editorType; - return editorType; - }; -} -exports.ScriptEditor = ScriptEditor; -function ScriptInteger(meta) { - if (typeof meta === "undefined") { - meta = { type: "integer" }; - } - else { - meta.type = "integer"; - } - return ScriptProperty(meta); -} -exports.ScriptInteger = ScriptInteger; -function ScriptNumber(meta) { - if (typeof meta === "undefined") { - meta = { type: "float" }; - } - else { - meta.type = "float"; - } - return ScriptProperty(meta); -} -exports.ScriptNumber = ScriptNumber; -function ScriptString(meta) { - if (typeof meta === "undefined") { - meta = { type: "string" }; - } - else { - meta.type = "string"; - } - return ScriptProperty(meta); -} -exports.ScriptString = ScriptString; -function ScriptObject(meta) { - if (typeof meta === "undefined") { - meta = { type: "object" }; - } - else { - meta.type = "object"; - } - return ScriptProperty(meta); -} -exports.ScriptObject = ScriptObject; -function ScriptProperty(meta) { - return function (target, propertyKey) { - let slots = target[Symbol_SerializedFields]; - if (typeof slots === "undefined") { - slots = target[Symbol_SerializedFields] = {}; - } - let slot = slots[propertyKey] = meta || {}; - if (typeof slot.serializable !== "boolean") { - slot.serializable = true; - } - if (typeof slot.editable !== "boolean") { - slot.editable = true; - } - if (typeof slot.visible !== "boolean") { - slot.visible = true; - } - if (typeof slot.name !== "string") { - slot.name = propertyKey; - } - }; -} -exports.ScriptProperty = ScriptProperty; -class DefaultEditor extends UnityEditor_1.Editor { - OnInspectorGUI() { - EditorUtil.draw(this.target); - } -} -exports.DefaultEditor = DefaultEditor; -class EditorUtil { - static getCustomEditor(forType) { - return forType[Symbol_CustomEditor] || DefaultEditor; - } - /** - * 默认编辑器绘制行为 - */ - static draw(target, extra) { - SerializationUtil.forEach(target, extra, (propertyKey, slot, self, extra) => { - if (slot.visible) { - let label = slot.label || propertyKey; - let editablePE = !slot.editorOnly || !UnityEditor_1.EditorApplication.isPlaying; - switch (slot.type) { - case "integer": { - let oldValue = self[propertyKey]; - if (slot.editable && editablePE) { - let newValue = UnityEditor_1.EditorGUILayout.IntField(label, oldValue); - if (newValue != oldValue) { - self[propertyKey] = newValue; - UnityEditor_1.EditorUtility.SetDirty(self); - } - } - else { - UnityEditor_1.EditorGUI.BeginDisabledGroup(true); - UnityEditor_1.EditorGUILayout.IntField(label, oldValue); - UnityEditor_1.EditorGUI.EndDisabledGroup(); - } - break; - } - case "float": { - let oldValue = self[propertyKey]; - if (slot.editable && editablePE) { - let newValue = UnityEditor_1.EditorGUILayout.FloatField(label, oldValue); - if (newValue != oldValue) { - self[propertyKey] = newValue; - UnityEditor_1.EditorUtility.SetDirty(self); - } - } - else { - UnityEditor_1.EditorGUI.BeginDisabledGroup(true); - UnityEditor_1.EditorGUILayout.FloatField(label, oldValue); - UnityEditor_1.EditorGUI.EndDisabledGroup(); - } - break; - } - case "string": { - let oldValue = self[propertyKey]; - if (typeof oldValue !== "string") { - oldValue = "" + oldValue; - } - if (slot.editable && editablePE) { - let newValue = UnityEditor_1.EditorGUILayout.TextField(label, oldValue); - if (newValue != oldValue) { - self[propertyKey] = newValue; - UnityEditor_1.EditorUtility.SetDirty(self); - } - } - else { - UnityEditor_1.EditorGUI.BeginDisabledGroup(true); - UnityEditor_1.EditorGUILayout.TextField(label, oldValue); - UnityEditor_1.EditorGUI.EndDisabledGroup(); - } - break; - } - case "object": { - let oldValue = self[propertyKey]; - if (typeof oldValue !== "object") { - oldValue = null; - } - if (slot.editable && editablePE) { - let allowSceneObjects = slot.extra && slot.extra.allowSceneObjects; - let newValue = UnityEditor_1.EditorGUILayout.ObjectField(label, oldValue, slot.extra && slot.extra.type || UnityEngine_1.Object, typeof allowSceneObjects === "boolean" ? allowSceneObjects : true); - if (newValue != oldValue) { - self[propertyKey] = newValue; - UnityEditor_1.EditorUtility.SetDirty(self); - } - } - else { - UnityEditor_1.EditorGUI.BeginDisabledGroup(true); - UnityEditor_1.EditorGUILayout.ObjectField(label, oldValue, UnityEngine_1.Object, false); - UnityEditor_1.EditorGUI.EndDisabledGroup(); - } - break; - } - } - } - }); - } -} -exports.EditorUtil = EditorUtil; -class SerializationUtil { - static forEach(target, extra, cb) { - let slots = target[Symbol_SerializedFields]; - if (typeof slots !== "undefined") { - for (let propertyKey in slots) { - cb(propertyKey, slots[propertyKey], target, extra); - } - } - } - // 当不需要默认行为时, 调用此函数将序列化状态标记为已完成, 以便跳过默认的 serialize/deserialize 行为 - static markAsReady(target) { - target[Symbol_PropertiesTouched] = true; - } - static serialize(target, ps) { - this.markAsReady(target); - this.forEach(target, ps, (propertyKey, slot, self, extra) => { - if (slot.serializable) { - let value = self[propertyKey]; - // console.log("serializing", propertyKey, value); - switch (slot.type) { - case "integer": { - extra.SetInteger(slot.name, typeof value === "number" ? value : 0); - break; - } - case "float": { - extra.SetNumber(slot.name, typeof value === "number" ? value : 0); - break; - } - case "string": { - extra.SetString(slot.name, value); - break; - } - case "object": { - extra.SetObject(slot.name, value); - break; - } - } - } - }); - } - static deserialize(target, ps) { - this.markAsReady(target); - this.forEach(target, ps, (propertyKey, slot, self, extra) => { - if (slot.serializable) { - let value = null; - switch (slot.type) { - case "integer": { - value = extra.GetInteger(slot.name); - break; - } - case "float": { - value = extra.GetNumber(slot.name); - break; - } - case "string": { - value = extra.GetString(slot.name); - break; - } - case "object": { - value = extra.GetObject(slot.name); - break; - } - } - self[propertyKey] = value; - // console.log("deserialize", propertyKey, value); - } - }); - } -} -exports.SerializationUtil = SerializationUtil; +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.SerializationUtil = exports.EditorUtil = exports.DefaultEditor = exports.ScriptProperty = exports.ScriptObject = exports.ScriptString = exports.ScriptNumber = exports.ScriptInteger = exports.ScriptEditor = exports.ScriptType = exports.ScriptAsset = void 0; +const UnityEditor_1 = require("UnityEditor"); +const UnityEngine_1 = require("UnityEngine"); +let Symbol_SerializedFields = Symbol.for("SerializedFields"); +let Symbol_PropertiesTouched = Symbol.for("PropertiesTouched"); +let Symbol_CustomEditor = Symbol.for("CustomEditor"); +function ScriptAsset(meta) { + return ScriptType(meta); +} +exports.ScriptAsset = ScriptAsset; +// expose this script class type to JSBehaviour, so you can put it on a prefab gameObject +function ScriptType(meta) { + return function (target) { + let OnBeforeSerialize = target.prototype["OnBeforeSerialize"]; + target.prototype["OnBeforeSerialize"] = function (ps) { + this[Symbol_PropertiesTouched] = false; + if (typeof OnBeforeSerialize === "function") { + OnBeforeSerialize.call(this, ps); + } + if (!this[Symbol_PropertiesTouched]) { + SerializationUtil.serialize(this, ps); + } + }; + let OnAfterDeserialize = target.prototype["OnAfterDeserialize"]; + target.prototype["OnAfterDeserialize"] = function (ps) { + this[Symbol_PropertiesTouched] = false; + if (typeof OnAfterDeserialize === "function") { + OnAfterDeserialize.call(this, ps); + } + if (!this[Symbol_PropertiesTouched]) { + SerializationUtil.deserialize(this, ps); + } + }; + return target; + }; +} +exports.ScriptType = ScriptType; +function ScriptEditor(forType) { + return function (editorType) { + forType.prototype[Symbol_CustomEditor] = editorType; + return editorType; + }; +} +exports.ScriptEditor = ScriptEditor; +function ScriptInteger(meta) { + if (typeof meta === "undefined") { + meta = { type: "integer" }; + } + else { + meta.type = "integer"; + } + return ScriptProperty(meta); +} +exports.ScriptInteger = ScriptInteger; +function ScriptNumber(meta) { + if (typeof meta === "undefined") { + meta = { type: "float" }; + } + else { + meta.type = "float"; + } + return ScriptProperty(meta); +} +exports.ScriptNumber = ScriptNumber; +function ScriptString(meta) { + if (typeof meta === "undefined") { + meta = { type: "string" }; + } + else { + meta.type = "string"; + } + return ScriptProperty(meta); +} +exports.ScriptString = ScriptString; +function ScriptObject(meta) { + if (typeof meta === "undefined") { + meta = { type: "object" }; + } + else { + meta.type = "object"; + } + return ScriptProperty(meta); +} +exports.ScriptObject = ScriptObject; +function ScriptProperty(meta) { + return function (target, propertyKey) { + let slots = target[Symbol_SerializedFields]; + if (typeof slots === "undefined") { + slots = target[Symbol_SerializedFields] = {}; + } + let slot = slots[propertyKey] = meta || {}; + if (typeof slot.serializable !== "boolean") { + slot.serializable = true; + } + if (typeof slot.editable !== "boolean") { + slot.editable = true; + } + if (typeof slot.visible !== "boolean") { + slot.visible = true; + } + if (typeof slot.name !== "string") { + slot.name = propertyKey; + } + }; +} +exports.ScriptProperty = ScriptProperty; +class DefaultEditor extends UnityEditor_1.Editor { + OnInspectorGUI() { + EditorUtil.draw(this.target); + } +} +exports.DefaultEditor = DefaultEditor; +class EditorUtil { + static getCustomEditor(forType) { + return forType[Symbol_CustomEditor] || DefaultEditor; + } + /** + * 默认编辑器绘制行为 + */ + static draw(target, extra) { + SerializationUtil.forEach(target, extra, (propertyKey, slot, self, extra) => { + if (slot.visible) { + let label = slot.label || propertyKey; + let editablePE = !slot.editorOnly || !UnityEditor_1.EditorApplication.isPlaying; + switch (slot.type) { + case "integer": { + let oldValue = self[propertyKey]; + if (slot.editable && editablePE) { + let newValue = UnityEditor_1.EditorGUILayout.IntField(label, oldValue); + if (newValue != oldValue) { + self[propertyKey] = newValue; + UnityEditor_1.EditorUtility.SetDirty(self); + } + } + else { + UnityEditor_1.EditorGUI.BeginDisabledGroup(true); + UnityEditor_1.EditorGUILayout.IntField(label, oldValue); + UnityEditor_1.EditorGUI.EndDisabledGroup(); + } + break; + } + case "float": { + let oldValue = self[propertyKey]; + if (slot.editable && editablePE) { + let newValue = UnityEditor_1.EditorGUILayout.FloatField(label, oldValue); + if (newValue != oldValue) { + self[propertyKey] = newValue; + UnityEditor_1.EditorUtility.SetDirty(self); + } + } + else { + UnityEditor_1.EditorGUI.BeginDisabledGroup(true); + UnityEditor_1.EditorGUILayout.FloatField(label, oldValue); + UnityEditor_1.EditorGUI.EndDisabledGroup(); + } + break; + } + case "string": { + let oldValue = self[propertyKey]; + if (typeof oldValue !== "string") { + oldValue = "" + oldValue; + } + if (slot.editable && editablePE) { + let newValue = UnityEditor_1.EditorGUILayout.TextField(label, oldValue); + if (newValue != oldValue) { + self[propertyKey] = newValue; + UnityEditor_1.EditorUtility.SetDirty(self); + } + } + else { + UnityEditor_1.EditorGUI.BeginDisabledGroup(true); + UnityEditor_1.EditorGUILayout.TextField(label, oldValue); + UnityEditor_1.EditorGUI.EndDisabledGroup(); + } + break; + } + case "object": { + let oldValue = self[propertyKey]; + if (typeof oldValue !== "object") { + oldValue = null; + } + if (slot.editable && editablePE) { + let allowSceneObjects = slot.extra && slot.extra.allowSceneObjects; + let newValue = UnityEditor_1.EditorGUILayout.ObjectField(label, oldValue, slot.extra && slot.extra.type || UnityEngine_1.Object, typeof allowSceneObjects === "boolean" ? allowSceneObjects : true); + if (newValue != oldValue) { + self[propertyKey] = newValue; + UnityEditor_1.EditorUtility.SetDirty(self); + } + } + else { + UnityEditor_1.EditorGUI.BeginDisabledGroup(true); + UnityEditor_1.EditorGUILayout.ObjectField(label, oldValue, UnityEngine_1.Object, false); + UnityEditor_1.EditorGUI.EndDisabledGroup(); + } + break; + } + } + } + }); + } +} +exports.EditorUtil = EditorUtil; +class SerializationUtil { + static forEach(target, extra, cb) { + let slots = target[Symbol_SerializedFields]; + if (typeof slots !== "undefined") { + for (let propertyKey in slots) { + cb(propertyKey, slots[propertyKey], target, extra); + } + } + } + // 当不需要默认行为时, 调用此函数将序列化状态标记为已完成, 以便跳过默认的 serialize/deserialize 行为 + static markAsReady(target) { + target[Symbol_PropertiesTouched] = true; + } + static serialize(target, ps) { + this.markAsReady(target); + this.forEach(target, ps, (propertyKey, slot, self, extra) => { + if (slot.serializable) { + let value = self[propertyKey]; + // console.log("serializing", propertyKey, value); + switch (slot.type) { + case "integer": { + extra.SetInteger(slot.name, typeof value === "number" ? value : 0); + break; + } + case "float": { + extra.SetNumber(slot.name, typeof value === "number" ? value : 0); + break; + } + case "string": { + extra.SetString(slot.name, value); + break; + } + case "object": { + extra.SetObject(slot.name, value); + break; + } + } + } + }); + } + static deserialize(target, ps) { + this.markAsReady(target); + this.forEach(target, ps, (propertyKey, slot, self, extra) => { + if (slot.serializable) { + let value = null; + switch (slot.type) { + case "integer": { + value = extra.GetInteger(slot.name); + break; + } + case "float": { + value = extra.GetNumber(slot.name); + break; + } + case "string": { + value = extra.GetString(slot.name); + break; + } + case "object": { + value = extra.GetObject(slot.name); + break; + } + } + self[propertyKey] = value; + // console.log("deserialize", propertyKey, value); + } + }); + } +} +exports.SerializationUtil = SerializationUtil; //# sourceMappingURL=editor_decorators.js.map \ No newline at end of file diff --git a/Scripts/out/plover/editor/file_watcher.js b/Scripts/out/plover/editor/file_watcher.js index dd50290d..d04dd6fc 100644 --- a/Scripts/out/plover/editor/file_watcher.js +++ b/Scripts/out/plover/editor/file_watcher.js @@ -1,91 +1,91 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.FileWatcher = exports.EFileState = void 0; -const dispatcher_1 = require("../events/dispatcher"); -var EFileState; -(function (EFileState) { - EFileState[EFileState["CHANGE"] = 1] = "CHANGE"; - EFileState[EFileState["NEW"] = 2] = "NEW"; - EFileState[EFileState["DELETE"] = 3] = "DELETE"; -})(EFileState = exports.EFileState || (exports.EFileState = {})); -class FileWatcher { - constructor(path, filter) { - this._dispatcher = new dispatcher_1.EventDispatcher(); - this._disposed = false; - this._pending = false; - this._cache = {}; - this._fsw = new FSWatcher(path, filter); - this._fsw.oncreate = this.oncreate.bind(this); - this._fsw.onchange = this.onchange.bind(this); - this._fsw.ondelete = this.ondelete.bind(this); - this._fsw.includeSubdirectories = true; - this._fsw.enableRaisingEvents = true; - } - get includeSubdirectories() { - return this._fsw.includeSubdirectories; - } - set includeSubdirectories(v) { - this._fsw.includeSubdirectories = v; - } - get enableRaisingEvents() { - return this._fsw.enableRaisingEvents; - } - set enableRaisingEvents(v) { - this._fsw.enableRaisingEvents = v; - } - dispose() { - if (this._disposed) { - return; - } - this._disposed = true; - this._fsw.dispose(); - this._fsw = null; - } - on(name, caller, fn) { - this._dispatcher.on(name, caller, fn); - } - off(name, caller, fn) { - this._dispatcher.off(name, caller, fn); - } - oncreate(name, fullPath) { - this.setCacheState(name, fullPath, EFileState.NEW); - } - onchange(name, fullPath) { - this.setCacheState(name, fullPath, EFileState.CHANGE); - } - ondelete(name, fullPath) { - this.setCacheState(name, fullPath, EFileState.DELETE); - } - setCacheState(name, fullPath, state) { - if (this._disposed) { - return; - } - this._cache[name] = { - name: name, - fullPath: fullPath, - state: state, - }; - if (!this._pending) { - this._pending = true; - setTimeout(() => this.dispatchEvents(), 500); - } - } - dispatchEvents() { - if (this._disposed) { - return; - } - this._pending = false; - let map = this._cache; - this._cache = {}; - for (let name in map) { - let state = map[name]; - this._dispatcher.dispatch(name, state); - this._dispatcher.dispatch(FileWatcher.ANY, state); - } - this._dispatcher.dispatch(FileWatcher.CHANGED, map); - } -} -exports.FileWatcher = FileWatcher; -FileWatcher.ANY = "* ANY"; -FileWatcher.CHANGED = "* CHANGED"; +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.FileWatcher = exports.EFileState = void 0; +const dispatcher_1 = require("../events/dispatcher"); +var EFileState; +(function (EFileState) { + EFileState[EFileState["CHANGE"] = 1] = "CHANGE"; + EFileState[EFileState["NEW"] = 2] = "NEW"; + EFileState[EFileState["DELETE"] = 3] = "DELETE"; +})(EFileState = exports.EFileState || (exports.EFileState = {})); +class FileWatcher { + constructor(path, filter) { + this._dispatcher = new dispatcher_1.EventDispatcher(); + this._disposed = false; + this._pending = false; + this._cache = {}; + this._fsw = new FSWatcher(path, filter); + this._fsw.oncreate = this.oncreate.bind(this); + this._fsw.onchange = this.onchange.bind(this); + this._fsw.ondelete = this.ondelete.bind(this); + this._fsw.includeSubdirectories = true; + this._fsw.enableRaisingEvents = true; + } + get includeSubdirectories() { + return this._fsw.includeSubdirectories; + } + set includeSubdirectories(v) { + this._fsw.includeSubdirectories = v; + } + get enableRaisingEvents() { + return this._fsw.enableRaisingEvents; + } + set enableRaisingEvents(v) { + this._fsw.enableRaisingEvents = v; + } + dispose() { + if (this._disposed) { + return; + } + this._disposed = true; + this._fsw.dispose(); + this._fsw = null; + } + on(name, caller, fn) { + this._dispatcher.on(name, caller, fn); + } + off(name, caller, fn) { + this._dispatcher.off(name, caller, fn); + } + oncreate(name, fullPath) { + this.setCacheState(name, fullPath, EFileState.NEW); + } + onchange(name, fullPath) { + this.setCacheState(name, fullPath, EFileState.CHANGE); + } + ondelete(name, fullPath) { + this.setCacheState(name, fullPath, EFileState.DELETE); + } + setCacheState(name, fullPath, state) { + if (this._disposed) { + return; + } + this._cache[name] = { + name: name, + fullPath: fullPath, + state: state, + }; + if (!this._pending) { + this._pending = true; + setTimeout(() => this.dispatchEvents(), 500); + } + } + dispatchEvents() { + if (this._disposed) { + return; + } + this._pending = false; + let map = this._cache; + this._cache = {}; + for (let name in map) { + let state = map[name]; + this._dispatcher.dispatch(name, state); + this._dispatcher.dispatch(FileWatcher.ANY, state); + } + this._dispatcher.dispatch(FileWatcher.CHANGED, map); + } +} +exports.FileWatcher = FileWatcher; +FileWatcher.ANY = "* ANY"; +FileWatcher.CHANGED = "* CHANGED"; //# sourceMappingURL=file_watcher.js.map \ No newline at end of file diff --git a/Scripts/out/plover/editor/js_console.js b/Scripts/out/plover/editor/js_console.js index 67c089e3..83796746 100644 --- a/Scripts/out/plover/editor/js_console.js +++ b/Scripts/out/plover/editor/js_console.js @@ -1,87 +1,87 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.JSConsole = exports.fillAutoCompletion = void 0; -const UnityEditor_1 = require("UnityEditor"); -const UnityEngine_1 = require("UnityEngine"); -const auto_completion_field_1 = require("./auto_completion_field"); -function fillAutoCompletion(scope, pattern) { - let result = []; - if (typeof pattern !== "string") { - return result; - } - let head = ''; - pattern.replace(/\\W*([\\w\\.]+)$/, (a, b, c) => { - head = pattern.substr(0, c + a.length - b.length); - pattern = b; - return b; - }); - let index = pattern.lastIndexOf('.'); - let left = ''; - if (index >= 0) { - left = pattern.substr(0, index + 1); - try { - scope = eval(pattern.substr(0, index)); - } - catch (e) { - scope = null; - } - pattern = pattern.substr(index + 1); - } - for (let k in scope) { - if (k.indexOf(pattern) == 0) { - result.push(head + left + k); - } - } - return result; -} -exports.fillAutoCompletion = fillAutoCompletion; -class JSConsole extends UnityEditor_1.EditorWindow { - constructor() { - super(...arguments); - this._searchField = new auto_completion_field_1.AutoCompletionField(); - this._history = []; - } - Awake() { - this._searchField.on("change", this, this.onSearchChange); - this._searchField.on("confirm", this, this.onSearchConfirm); - } - onSearchChange(s) { - this._searchField.clearResults(); - fillAutoCompletion(globalThis, s).forEach(element => { - if (element != s) { - this._searchField.addResult(element); - } - }); - } - onSearchConfirm(s) { - console.log("confirm:", s); - } - OnEnable() { - this.titleContent = new UnityEngine_1.GUIContent("Javascript Console"); - } - OnGUI() { - let evt = UnityEngine_1.Event.current; - this._searchField.onGUI(); - if (evt.type == UnityEngine_1.EventType.KeyUp) { - switch (evt.keyCode) { - case UnityEngine_1.KeyCode.Return: { - let code = this._searchField.searchString; - if (code != null && code.length > 0) { - try { - let rval = eval(code); - console.log(JSON.stringify(rval)); - } - catch (e) { - console.error(e); - } - // this._history.push(code); - } - break; - } - } - } - // GUI.Box(new Rect(0, 50, 300, 100), this._history.join("\n")); - } -} -exports.JSConsole = JSConsole; +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.JSConsole = exports.fillAutoCompletion = void 0; +const UnityEditor_1 = require("UnityEditor"); +const UnityEngine_1 = require("UnityEngine"); +const auto_completion_field_1 = require("./auto_completion_field"); +function fillAutoCompletion(scope, pattern) { + let result = []; + if (typeof pattern !== "string") { + return result; + } + let head = ''; + pattern.replace(/\\W*([\\w\\.]+)$/, (a, b, c) => { + head = pattern.substr(0, c + a.length - b.length); + pattern = b; + return b; + }); + let index = pattern.lastIndexOf('.'); + let left = ''; + if (index >= 0) { + left = pattern.substr(0, index + 1); + try { + scope = eval(pattern.substr(0, index)); + } + catch (e) { + scope = null; + } + pattern = pattern.substr(index + 1); + } + for (let k in scope) { + if (k.indexOf(pattern) == 0) { + result.push(head + left + k); + } + } + return result; +} +exports.fillAutoCompletion = fillAutoCompletion; +class JSConsole extends UnityEditor_1.EditorWindow { + constructor() { + super(...arguments); + this._searchField = new auto_completion_field_1.AutoCompletionField(); + this._history = []; + } + Awake() { + this._searchField.on("change", this, this.onSearchChange); + this._searchField.on("confirm", this, this.onSearchConfirm); + } + onSearchChange(s) { + this._searchField.clearResults(); + fillAutoCompletion(globalThis, s).forEach(element => { + if (element != s) { + this._searchField.addResult(element); + } + }); + } + onSearchConfirm(s) { + console.log("confirm:", s); + } + OnEnable() { + this.titleContent = new UnityEngine_1.GUIContent("Javascript Console"); + } + OnGUI() { + let evt = UnityEngine_1.Event.current; + this._searchField.onGUI(); + if (evt.type == UnityEngine_1.EventType.KeyUp) { + switch (evt.keyCode) { + case UnityEngine_1.KeyCode.Return: { + let code = this._searchField.searchString; + if (code != null && code.length > 0) { + try { + let rval = eval(code); + console.log(JSON.stringify(rval)); + } + catch (e) { + console.error(e); + } + // this._history.push(code); + } + break; + } + } + } + // GUI.Box(new Rect(0, 50, 300, 100), this._history.join("\n")); + } +} +exports.JSConsole = JSConsole; //# sourceMappingURL=js_console.js.map \ No newline at end of file diff --git a/Scripts/out/plover/editor/js_module_view.js b/Scripts/out/plover/editor/js_module_view.js index 2b878429..170774e9 100644 --- a/Scripts/out/plover/editor/js_module_view.js +++ b/Scripts/out/plover/editor/js_module_view.js @@ -1,72 +1,72 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.JSModuleView = void 0; -const UnityEditor_1 = require("UnityEditor"); -const UnityEngine_1 = require("UnityEngine"); -const js_reload_1 = require("./js_reload"); -class JSModuleView extends UnityEditor_1.EditorWindow { - constructor() { - super(...arguments); - this._sv = UnityEngine_1.Vector2.zero; - } - OnEnable() { - this.titleContent = new UnityEngine_1.GUIContent("JS Modules"); - } - drawModule(mod) { - UnityEngine_1.GUILayout.Space(12); - if (typeof this._touch[mod.id] !== "undefined") { - UnityEditor_1.EditorGUILayout.TextField("Module ID", mod.id); - return; - } - UnityEditor_1.EditorGUILayout.BeginHorizontal(); - UnityEditor_1.EditorGUILayout.TextField("Module ID", mod.id); - let doReload = false; - if (mod["resolvername"] != "source") { - UnityEditor_1.EditorGUI.BeginDisabledGroup(true); - doReload = UnityEngine_1.GUILayout.Button("Reload"); - UnityEditor_1.EditorGUI.EndDisabledGroup(); - } - else { - doReload = UnityEngine_1.GUILayout.Button("Reload"); - } - UnityEditor_1.EditorGUILayout.EndHorizontal(); - this._touch[mod.id] = true; - UnityEditor_1.EditorGUILayout.TextField("File Name", mod.filename); - if (typeof mod.parent === "object") { - UnityEditor_1.EditorGUILayout.TextField("Parent", mod.parent.id); - } - else { - UnityEditor_1.EditorGUILayout.TextField("Parent", "TOP LEVEL"); - } - if (typeof mod.children !== "undefined") { - UnityEditor_1.EditorGUILayout.IntField("Children", mod.children.length); - UnityEditor_1.EditorGUILayout.BeginHorizontal(); - UnityEngine_1.GUILayout.Space(50); - UnityEditor_1.EditorGUILayout.BeginVertical(); - for (let i = 0; i < mod.children.length; i++) { - let child = mod.children[i]; - UnityEditor_1.EditorGUILayout.TextField("Child", child.id); - } - UnityEditor_1.EditorGUILayout.EndVertical(); - UnityEditor_1.EditorGUILayout.EndHorizontal(); - } - if (doReload) { - js_reload_1.reload(mod); - } - } - OnGUI() { - let cache = require.main["cache"]; - if (typeof cache === "undefined") { - return; - } - this._touch = {}; - this._sv = UnityEditor_1.EditorGUILayout.BeginScrollView(this._sv); - Object.keys(cache).forEach(name => { - let mod = cache[name]; - this.drawModule(mod); - }); - UnityEditor_1.EditorGUILayout.EndScrollView(); - } -} -exports.JSModuleView = JSModuleView; +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.JSModuleView = void 0; +const UnityEditor_1 = require("UnityEditor"); +const UnityEngine_1 = require("UnityEngine"); +const js_reload_1 = require("./js_reload"); +class JSModuleView extends UnityEditor_1.EditorWindow { + constructor() { + super(...arguments); + this._sv = UnityEngine_1.Vector2.zero; + } + OnEnable() { + this.titleContent = new UnityEngine_1.GUIContent("JS Modules"); + } + drawModule(mod) { + UnityEngine_1.GUILayout.Space(12); + if (typeof this._touch[mod.id] !== "undefined") { + UnityEditor_1.EditorGUILayout.TextField("Module ID", mod.id); + return; + } + UnityEditor_1.EditorGUILayout.BeginHorizontal(); + UnityEditor_1.EditorGUILayout.TextField("Module ID", mod.id); + let doReload = false; + if (mod["resolvername"] != "source") { + UnityEditor_1.EditorGUI.BeginDisabledGroup(true); + doReload = UnityEngine_1.GUILayout.Button("Reload"); + UnityEditor_1.EditorGUI.EndDisabledGroup(); + } + else { + doReload = UnityEngine_1.GUILayout.Button("Reload"); + } + UnityEditor_1.EditorGUILayout.EndHorizontal(); + this._touch[mod.id] = true; + UnityEditor_1.EditorGUILayout.TextField("File Name", mod.filename); + if (typeof mod.parent === "object") { + UnityEditor_1.EditorGUILayout.TextField("Parent", mod.parent.id); + } + else { + UnityEditor_1.EditorGUILayout.TextField("Parent", "TOP LEVEL"); + } + if (typeof mod.children !== "undefined") { + UnityEditor_1.EditorGUILayout.IntField("Children", mod.children.length); + UnityEditor_1.EditorGUILayout.BeginHorizontal(); + UnityEngine_1.GUILayout.Space(50); + UnityEditor_1.EditorGUILayout.BeginVertical(); + for (let i = 0; i < mod.children.length; i++) { + let child = mod.children[i]; + UnityEditor_1.EditorGUILayout.TextField("Child", child.id); + } + UnityEditor_1.EditorGUILayout.EndVertical(); + UnityEditor_1.EditorGUILayout.EndHorizontal(); + } + if (doReload) { + js_reload_1.reload(mod); + } + } + OnGUI() { + let cache = require.main["cache"]; + if (typeof cache === "undefined") { + return; + } + this._touch = {}; + this._sv = UnityEditor_1.EditorGUILayout.BeginScrollView(this._sv); + Object.keys(cache).forEach(name => { + let mod = cache[name]; + this.drawModule(mod); + }); + UnityEditor_1.EditorGUILayout.EndScrollView(); + } +} +exports.JSModuleView = JSModuleView; //# sourceMappingURL=js_module_view.js.map \ No newline at end of file diff --git a/Scripts/out/plover/events/data_binding.js b/Scripts/out/plover/events/data_binding.js index ce48c88f..bfbb4531 100644 --- a/Scripts/out/plover/events/data_binding.js +++ b/Scripts/out/plover/events/data_binding.js @@ -1,122 +1,122 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.DataBinding = exports.Subscribers = exports.Subscriber = void 0; -class Subscriber { - constructor(model, key) { - this._model = model; - this._key = key; - this._model.addSubscriber(this); - } - get value() { - return this._model[this._key]; - } - set value(newValue) { - this._source = true; - this._model[this._key] = newValue; - this._source = false; - } - update(value) { - } - notify(value) { - if (!this._source) { - this.update(value); - } - } - unsubscribe() { - if (this._model) { - this._model.removeSubscriber(this); - this._model = undefined; - } - } -} -exports.Subscriber = Subscriber; -class Subscribers { - notify(valueProxy) { - if (this._subs) { - const copy = this._subs.slice(); - for (let i = 0, len = copy.length; i < len; i++) { - copy[i].notify(valueProxy); - } - } - } - addSub(sub) { - if (!this._subs) { - this._subs = []; - } - this._subs.push(sub); - } - removeSub(sub) { - if (this._subs && this._subs.length) { - const index = this._subs.indexOf(sub); - if (index >= 0) { - this._subs.splice(index, 1); - } - } - } - // 废弃当前值, 将监听者转移给新值 - transfer(newValue) { - newValue._subs = this._subs; - this._subs = undefined; - } -} -exports.Subscribers = Subscribers; -const SubscribersKey = Symbol.for("subscribers"); -class DataBinding { - constructor() { - Object.defineProperty(this, SubscribersKey, { value: new Subscribers(), enumerable: false }); - } - addSubscriber(sub) { - this[SubscribersKey].addSub(sub); - } - removeSubscriber(sub) { - this[SubscribersKey].removeSub(sub); - } - static bind(data) { - let model = new DataBinding(); - let subscribers = model[SubscribersKey]; - for (let key in data) { - if (key.startsWith("$") || key.startsWith("_$")) { - continue; - } - let value = data[key]; - let valueProxy = value; - if (typeof value === "object") { - valueProxy = DataBinding.bind(value); - } - Object.defineProperty(model, key, { - enumerable: true, - get() { - return valueProxy; - }, - set(newValue) { - if (newValue !== value) { - let oldValue = value; - if (typeof newValue === "object") { - valueProxy = DataBinding.bind(newValue); - oldValue[SubscribersKey].transfer(valueProxy[SubscribersKey]); - // Model.transfer(oldValue, valueProxy); - } - else { - valueProxy = newValue; - } - subscribers.notify(valueProxy); - } - }, - }); - } - return model; - } - static subscribe(SubscriberType, modelObject, path, ...args) { - let model = modelObject; - let keys = path.split("."); - let key = path; - for (let i = 0, len = keys.length - 1; i < len; i++) { - key = keys[i]; - model = model[key]; - } - let sub = new SubscriberType(model, key, ...args); - return sub; - } -} -exports.DataBinding = DataBinding; +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.DataBinding = exports.Subscribers = exports.Subscriber = void 0; +class Subscriber { + constructor(model, key) { + this._model = model; + this._key = key; + this._model.addSubscriber(this); + } + get value() { + return this._model[this._key]; + } + set value(newValue) { + this._source = true; + this._model[this._key] = newValue; + this._source = false; + } + update(value) { + } + notify(value) { + if (!this._source) { + this.update(value); + } + } + unsubscribe() { + if (this._model) { + this._model.removeSubscriber(this); + this._model = undefined; + } + } +} +exports.Subscriber = Subscriber; +class Subscribers { + notify(valueProxy) { + if (this._subs) { + const copy = this._subs.slice(); + for (let i = 0, len = copy.length; i < len; i++) { + copy[i].notify(valueProxy); + } + } + } + addSub(sub) { + if (!this._subs) { + this._subs = []; + } + this._subs.push(sub); + } + removeSub(sub) { + if (this._subs && this._subs.length) { + const index = this._subs.indexOf(sub); + if (index >= 0) { + this._subs.splice(index, 1); + } + } + } + // 废弃当前值, 将监听者转移给新值 + transfer(newValue) { + newValue._subs = this._subs; + this._subs = undefined; + } +} +exports.Subscribers = Subscribers; +const SubscribersKey = Symbol.for("subscribers"); +class DataBinding { + constructor() { + Object.defineProperty(this, SubscribersKey, { value: new Subscribers(), enumerable: false }); + } + addSubscriber(sub) { + this[SubscribersKey].addSub(sub); + } + removeSubscriber(sub) { + this[SubscribersKey].removeSub(sub); + } + static bind(data) { + let model = new DataBinding(); + let subscribers = model[SubscribersKey]; + for (let key in data) { + if (key.startsWith("$") || key.startsWith("_$")) { + continue; + } + let value = data[key]; + let valueProxy = value; + if (typeof value === "object") { + valueProxy = DataBinding.bind(value); + } + Object.defineProperty(model, key, { + enumerable: true, + get() { + return valueProxy; + }, + set(newValue) { + if (newValue !== value) { + let oldValue = value; + if (typeof newValue === "object") { + valueProxy = DataBinding.bind(newValue); + oldValue[SubscribersKey].transfer(valueProxy[SubscribersKey]); + // Model.transfer(oldValue, valueProxy); + } + else { + valueProxy = newValue; + } + subscribers.notify(valueProxy); + } + }, + }); + } + return model; + } + static subscribe(SubscriberType, modelObject, path, ...args) { + let model = modelObject; + let keys = path.split("."); + let key = path; + for (let i = 0, len = keys.length - 1; i < len; i++) { + key = keys[i]; + model = model[key]; + } + let sub = new SubscriberType(model, key, ...args); + return sub; + } +} +exports.DataBinding = DataBinding; //# sourceMappingURL=data_binding.js.map \ No newline at end of file diff --git a/Scripts/out/plover/events/dispatcher.js b/Scripts/out/plover/events/dispatcher.js index c8971c66..04acc49a 100644 --- a/Scripts/out/plover/events/dispatcher.js +++ b/Scripts/out/plover/events/dispatcher.js @@ -1,129 +1,129 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.EventDispatcher = exports.Dispatcher = exports.Handler = void 0; -class Handler { - constructor(caller, fn) { - this.caller = caller; - this.fn = fn; - } - invoke(arg0, arg1, arg2) { - if (this.fn) { - this.fn.call(this.caller, arg0, arg1, arg2); - } - } -} -exports.Handler = Handler; -/** - * 简单的事件分发器实现 - * 此实现功能与 DuktapeJS.Dispatcher 基本一致, - * 但 DuktapeJS.Dispatcher 不保证事件响应顺序, 但效率更高 (因为复用了中途移除的索引) - */ -class Dispatcher { - constructor() { - this._handlers = []; - } - on(caller, fn) { - let handler = new Handler(caller, fn); - this._handlers.push(handler); - return handler; - } - off(caller, fn) { - let size = this._handlers.length; - if (typeof fn === "undefined") { - let found = false; - for (let i = 0; i < size;) { - let item = this._handlers[i]; - if (item.caller == caller) { - found = true; - item.fn = null; - item.caller = null; - this._handlers.splice(i, 1); - size--; - } - else { - i++; - } - } - return found; - } - for (let i = 0; i < size; i++) { - let item = this._handlers[i]; - if (item.caller == caller && item.fn == fn) { - item.fn = null; - item.caller = null; - this._handlers.splice(i, 1); - return true; - } - } - return false; - } - /** - * 移除所有处理器 - */ - clear() { - this._handlers.splice(0); - } - dispatch(arg0, arg1, arg2) { - let size = this._handlers.length; - if (size == 0) { - return; - } - if (size == 1) { - let item = this._handlers[0]; - item.invoke(arg0, arg1, arg2); - return; - } - if (size == 2) { - let item0 = this._handlers[0]; - let item1 = this._handlers[1]; - item0.invoke(arg0, arg1, arg2); - item1.invoke(arg0, arg1, arg2); - return; - } - let copy = new Array(...this._handlers); - for (let i = 0; i < size; i++) { - copy[i].invoke(arg0, arg1, arg2); - } - } -} -exports.Dispatcher = Dispatcher; -/** - * 按事件名派发 - */ -class EventDispatcher { - constructor() { - this._dispatcher = {}; - } - on(evt, caller, fn) { - let dispatcher = this._dispatcher[evt]; - if (typeof dispatcher === "undefined") { - dispatcher = this._dispatcher[evt] = new Dispatcher(); - } - dispatcher.on(caller, fn); - } - off(evt, caller, fn) { - let dispatcher = this._dispatcher[evt]; - if (typeof dispatcher !== "undefined") { - dispatcher.off(caller, fn); - } - } - clear() { - for (let evt in this._dispatcher) { - let dispatcher = this._dispatcher[evt]; - if (dispatcher instanceof Dispatcher) { - dispatcher.clear(); - } - } - } - /** - * 派发指定事件 - */ - dispatch(evt, arg0, arg1, arg2) { - let dispatcher = this._dispatcher[evt]; - if (typeof dispatcher !== "undefined") { - dispatcher.dispatch(arg0, arg1, arg2); - } - } -} -exports.EventDispatcher = EventDispatcher; +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.EventDispatcher = exports.Dispatcher = exports.Handler = void 0; +class Handler { + constructor(caller, fn) { + this.caller = caller; + this.fn = fn; + } + invoke(arg0, arg1, arg2) { + if (this.fn) { + this.fn.call(this.caller, arg0, arg1, arg2); + } + } +} +exports.Handler = Handler; +/** + * 简单的事件分发器实现 + * 此实现功能与 DuktapeJS.Dispatcher 基本一致, + * 但 DuktapeJS.Dispatcher 不保证事件响应顺序, 但效率更高 (因为复用了中途移除的索引) + */ +class Dispatcher { + constructor() { + this._handlers = []; + } + on(caller, fn) { + let handler = new Handler(caller, fn); + this._handlers.push(handler); + return handler; + } + off(caller, fn) { + let size = this._handlers.length; + if (typeof fn === "undefined") { + let found = false; + for (let i = 0; i < size;) { + let item = this._handlers[i]; + if (item.caller == caller) { + found = true; + item.fn = null; + item.caller = null; + this._handlers.splice(i, 1); + size--; + } + else { + i++; + } + } + return found; + } + for (let i = 0; i < size; i++) { + let item = this._handlers[i]; + if (item.caller == caller && item.fn == fn) { + item.fn = null; + item.caller = null; + this._handlers.splice(i, 1); + return true; + } + } + return false; + } + /** + * 移除所有处理器 + */ + clear() { + this._handlers.splice(0); + } + dispatch(arg0, arg1, arg2) { + let size = this._handlers.length; + if (size == 0) { + return; + } + if (size == 1) { + let item = this._handlers[0]; + item.invoke(arg0, arg1, arg2); + return; + } + if (size == 2) { + let item0 = this._handlers[0]; + let item1 = this._handlers[1]; + item0.invoke(arg0, arg1, arg2); + item1.invoke(arg0, arg1, arg2); + return; + } + let copy = new Array(...this._handlers); + for (let i = 0; i < size; i++) { + copy[i].invoke(arg0, arg1, arg2); + } + } +} +exports.Dispatcher = Dispatcher; +/** + * 按事件名派发 + */ +class EventDispatcher { + constructor() { + this._dispatcher = {}; + } + on(evt, caller, fn) { + let dispatcher = this._dispatcher[evt]; + if (typeof dispatcher === "undefined") { + dispatcher = this._dispatcher[evt] = new Dispatcher(); + } + dispatcher.on(caller, fn); + } + off(evt, caller, fn) { + let dispatcher = this._dispatcher[evt]; + if (typeof dispatcher !== "undefined") { + dispatcher.off(caller, fn); + } + } + clear() { + for (let evt in this._dispatcher) { + let dispatcher = this._dispatcher[evt]; + if (dispatcher instanceof Dispatcher) { + dispatcher.clear(); + } + } + } + /** + * 派发指定事件 + */ + dispatch(evt, arg0, arg1, arg2) { + let dispatcher = this._dispatcher[evt]; + if (typeof dispatcher !== "undefined") { + dispatcher.dispatch(arg0, arg1, arg2); + } + } +} +exports.EventDispatcher = EventDispatcher; //# sourceMappingURL=dispatcher.js.map \ No newline at end of file diff --git a/Scripts/out/test.js b/Scripts/out/test.js index 5c727baa..f59830b0 100644 --- a/Scripts/out/test.js +++ b/Scripts/out/test.js @@ -1,16 +1,16 @@ -let s = Symbol.for("test"); -class Foo { - constructor() { - this.v1 = 1; - this.v2 = 2; - } - foo() { - } -} -let foo = new Foo(); -foo[s] = 1; -for (let k in foo) { - console.log(k, typeof foo[k]); -} -console.log("symbol", foo[s]); +let s = Symbol.for("test"); +class Foo { + constructor() { + this.v1 = 1; + this.v2 = 2; + } + foo() { + } +} +let foo = new Foo(); +foo[s] = 1; +for (let k in foo) { + console.log(k, typeof foo[k]); +} +console.log("symbol", foo[s]); //# sourceMappingURL=test.js.map \ No newline at end of file diff --git a/Scripts/src/example_scriptable_object.ts b/Scripts/src/example_scriptable_object.ts index bcf06659..a33a2f7e 100644 --- a/Scripts/src/example_scriptable_object.ts +++ b/Scripts/src/example_scriptable_object.ts @@ -1,10 +1,6 @@ import { Resources, ScriptableObject } from "UnityEngine"; import { ScriptAsset, ScriptNumber, ScriptString } from "./plover/editor/editor_decorators"; -console.warn("此功能未完成"); -console.warn("此功能未完成"); -console.warn("此功能未完成"); - @ScriptAsset() export class MyScriptableObject extends ScriptableObject { @ScriptNumber() @@ -14,11 +10,13 @@ export class MyScriptableObject extends ScriptableObject { value2 = "hello"; } -let js_data = Resources.Load("data/js_data"); +if (require.main == module) { + let js_data = Resources.Load("data/js_data"); -if (js_data) { - console.log("type check:", js_data instanceof MyScriptableObject); - console.log("type values:", js_data.value1, js_data.value2); -} else { - console.error("failed to load js_data, please create the asset at first."); -} + if (js_data) { + console.log("type check:", js_data instanceof MyScriptableObject); + console.log("type values:", js_data.value1, js_data.value2); + } else { + console.error("failed to load js_data, please create the asset at first."); + } +}