Skip to content
Permalink
Browse files

feat(asset): script event function asset: add "update script event fu…

…nction in all script components" logic
  • Loading branch information...
yyc-git committed Apr 5, 2019
1 parent 32d3785 commit fe76e2e208fbc6117944894f654fd5826b3dcd03
@@ -32,19 +32,27 @@ module Method = {
|> ScriptEventFunctionEngineService.createScriptEventFunctionData;

let updateEventFunctionData = (nodeId, name, eventFunctionJsObjStr) =>
/* TODO check eventFunctionJsObjStr: should be valid */
/* TODO update script component->event function data of the name */
Console.tryCatch(
() =>
() => {
let newEventFunctionData =
_convertEventFunctionJsObjStrToData(eventFunctionJsObjStr);

ScriptEventFunctionNodeAssetEditorService.setNodeData(
nodeId,
ScriptEventFunctionNodeAssetService.buildNodeData(
~name,
~eventFunctionData=
_convertEventFunctionJsObjStrToData(eventFunctionJsObjStr),
~eventFunctionData=newEventFunctionData,
),
)
|> StateLogicService.getAndSetEditorState,
|> StateLogicService.getAndSetEditorState;

ScriptEngineService.updateEventFunctionInAllScriptComponents(
name,
newEventFunctionData,
)
|> StateLogicService.getAndSetEngineState;
},
e => {
let message = e##message;

@@ -52,36 +52,6 @@ let replaceScriptAttribute =
targetScriptAttribute,
);

let updateAttributeInAllScriptComponents =
(
attributeName,
newAttribute,
({scriptRecord}: StateDataMainType.state) as engineState,
) => {
let {scriptAttributeMap}: StateDataMainType.scriptRecord = scriptRecord;

let scriptAttributeMap =
scriptAttributeMap
|> WonderCommonlib.ImmutableSparseMapService.mapValid((. attributeMap) =>
attributeMap
|> WonderCommonlib.ImmutableHashMapService.has(attributeName) ?
attributeMap
|> WonderCommonlib.ImmutableHashMapService.set(
attributeName,
newAttribute,
) :
attributeMap
);

{
...engineState,
scriptRecord: {
...scriptRecord,
scriptAttributeMap,
},
};
};

let unsafeGetScriptAttributeEntries = ScriptAPI.unsafeGetScriptAttributeEntries;

let unsafeGetScriptAttribute = ScriptAPI.unsafeGetScriptAttribute;
@@ -151,4 +121,58 @@ let hasScriptAttributeData =
Some() : None
)
|> Js.Option.isSome;
};

let _updateScriptDataMapInAllScriptComponents =
(dataName, newData, scriptDataMap) =>
scriptDataMap
|> WonderCommonlib.ImmutableSparseMapService.mapValid((. dataMap) =>
dataMap |> WonderCommonlib.ImmutableHashMapService.has(dataName) ?
dataMap
|> WonderCommonlib.ImmutableHashMapService.set(dataName, newData) :
dataMap
);

let updateAttributeInAllScriptComponents =
(
attributeName,
newAttribute,
({scriptRecord}: StateDataMainType.state) as engineState,
) => {
let {scriptAttributeMap}: StateDataMainType.scriptRecord = scriptRecord;

{
...engineState,
scriptRecord: {
...scriptRecord,
scriptAttributeMap:
_updateScriptDataMapInAllScriptComponents(
attributeName,
newAttribute,
scriptAttributeMap,
),
},
};
};

let updateEventFunctionInAllScriptComponents =
(
eventFunctionName,
newEventFunctionData,
({scriptRecord}: StateDataMainType.state) as engineState,
) => {
let {scriptEventFunctionDataMap}: StateDataMainType.scriptRecord = scriptRecord;

{
...engineState,
scriptRecord: {
...scriptRecord,
scriptEventFunctionDataMap:
_updateScriptDataMapInAllScriptComponents(
eventFunctionName,
newEventFunctionData,
scriptEventFunctionDataMap,
),
},
};
};
@@ -13,8 +13,6 @@ let _ =
beforeEach(() => {
sandbox := createSandbox();
MainEditorSceneTool.initState(~sandbox, ());

MainEditorSceneTool.prepareScene(sandbox);
});
afterEach(() => restoreSandbox(refJsObjToSandbox(sandbox^)));

@@ -46,12 +44,15 @@ let _ =
~disposeFunc=Some((. script, api, state) => state),
(),
);
ScriptEventFunctionInspectorTool.updateEventFunctionData(
addedNodeId,

let eventFunctionName =
ScriptEventFunctionInspectorTool.getEventFunctionName(
addedNodeId,
editorState,
),
);
ScriptEventFunctionInspectorTool.updateEventFunctionData(
addedNodeId,
eventFunctionName,
jsObjStr,
);

@@ -89,5 +90,44 @@ let _ =
error |> expect |> toCalledWith([|"aaa is not defined"|]);
})
);

describe("test update script attribute in all script components", () => {
beforeEach(() =>
ScriptEventFunctionInspectorTool.TestUpdateScriptEventFunctionInAllScriptComponents.createDefaultSceneAndAddScriptComponent(
sandbox,
)
);

test("test update one script component", () => {
let (script, addedNodeId) =
ScriptEventFunctionInspectorTool.TestUpdateScriptEventFunctionInAllScriptComponents.prepareForOneScriptComponent(
sandbox,
);
let jsObjStr =
ScriptEventFunctionInspectorTool.buildEventFunctionDataJsObjStr(
~initFunc=Some((. script, api, state) => state),
~disposeFunc=Some((. script, api, state) => state),
(),
);
let eventFunctionName =
ScriptEventFunctionInspectorTool.getEventFunctionName(addedNodeId)
|> StateLogicService.getEditorState;

ScriptEventFunctionInspectorTool.updateEventFunctionData(
addedNodeId,
eventFunctionName,
jsObjStr,
);

ScriptToolEngine.unsafeGetScriptEventFunctionData(
script,
eventFunctionName,
)
|> StateLogicService.getEngineStateToGetData
|> ScriptEventFunctionInspector.Method.convertEventFunctionDataToJsObjStr
|> StringTool.removeNewLinesAndSpaces
|> expect == jsObjStr;
});
});
});
});
@@ -40,4 +40,31 @@ let buildEventFunctionDataJsObjStr =
update: updateFunc,
dispose: disposeFunc,
})
|> StringTool.removeNewLinesAndSpaces;
|> StringTool.removeNewLinesAndSpaces;

module TestUpdateScriptEventFunctionInAllScriptComponents = {
let createDefaultSceneAndAddScriptComponent = sandbox => {
MainEditorSceneTool.createDefaultScene(
sandbox,
MainEditorSceneTool.setFirstCubeToBeCurrentSceneTreeNode,
);

MainEditorInspectorAddComponentTool.addScriptComponent();
};

let prepareForOneScriptComponent = sandbox => {
let script = GameObjectTool.getCurrentSceneTreeNodeScript();
let assetTreeData =
MainEditorAssetTreeTool.BuildAssetTree.buildEmptyAssetTree();
let addedNodeId = MainEditorAssetIdTool.getNewAssetId();
MainEditorAssetHeaderOperateNodeTool.addScriptEventFunction();

MainEditorScriptTool.addScriptEventFunction(
~script,
~send=SinonTool.createOneLengthStub(sandbox^),
(),
);

(script, addedNodeId);
};
};
@@ -8,4 +8,12 @@ let getScriptAttributeEntries = (script, attributeName, engineState) =>

let getScriptAttributeFieldNames = (script, attributeName, engineState) =>
getScriptAttributeEntries(script, attributeName, engineState)
|> Js.Array.map(((name, _)) => name);
|> Js.Array.map(((name, _)) => name);

let unsafeGetScriptEventFunctionData =
(script, eventFunctionName, engineState) =>
Wonderjs.OperateScriptDataMainService.unsafeGetScriptAllEventFunctionData(
script,
engineState,
)
|> WonderCommonlib.ImmutableHashMapService.unsafeGet(eventFunctionName);

0 comments on commit fe76e2e

Please sign in to comment.
You can’t perform that action at this time.