Skip to content

Commit

Permalink
feat(asset): script event function asset: add "update script event fu…
Browse files Browse the repository at this point in the history
…nction in all script components" logic
  • Loading branch information
yyc-git committed Apr 5, 2019
1 parent 32d3785 commit fe76e2e
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 42 deletions.
Expand Up @@ -32,19 +32,27 @@ module Method = {
|> ScriptEventFunctionEngineService.createScriptEventFunctionData; |> ScriptEventFunctionEngineService.createScriptEventFunctionData;


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

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

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


Expand Down
84 changes: 54 additions & 30 deletions src/service/state/engine/script/ScriptEngineService.re
Expand Up @@ -52,36 +52,6 @@ let replaceScriptAttribute =
targetScriptAttribute, 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 unsafeGetScriptAttributeEntries = ScriptAPI.unsafeGetScriptAttributeEntries;


let unsafeGetScriptAttribute = ScriptAPI.unsafeGetScriptAttribute; let unsafeGetScriptAttribute = ScriptAPI.unsafeGetScriptAttribute;
Expand Down Expand Up @@ -151,4 +121,58 @@ let hasScriptAttributeData =
Some() : None Some() : None
) )
|> Js.Option.isSome; |> 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,
),
},
};
}; };
Expand Up @@ -13,8 +13,6 @@ let _ =
beforeEach(() => { beforeEach(() => {
sandbox := createSandbox(); sandbox := createSandbox();
MainEditorSceneTool.initState(~sandbox, ()); MainEditorSceneTool.initState(~sandbox, ());

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


Expand Down Expand Up @@ -46,12 +44,15 @@ let _ =
~disposeFunc=Some((. script, api, state) => state), ~disposeFunc=Some((. script, api, state) => state),
(), (),
); );
ScriptEventFunctionInspectorTool.updateEventFunctionData(
addedNodeId, let eventFunctionName =
ScriptEventFunctionInspectorTool.getEventFunctionName( ScriptEventFunctionInspectorTool.getEventFunctionName(
addedNodeId, addedNodeId,
editorState, editorState,
), );
ScriptEventFunctionInspectorTool.updateEventFunctionData(
addedNodeId,
eventFunctionName,
jsObjStr, jsObjStr,
); );


Expand Down Expand Up @@ -89,5 +90,44 @@ let _ =
error |> expect |> toCalledWith([|"aaa is not defined"|]); 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;
});
});
}); });
}); });
Expand Up @@ -40,4 +40,31 @@ let buildEventFunctionDataJsObjStr =
update: updateFunc, update: updateFunc,
dispose: disposeFunc, 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);
};
};
10 changes: 9 additions & 1 deletion test/tool/engine/ScriptToolEngine.re
Expand Up @@ -8,4 +8,12 @@ let getScriptAttributeEntries = (script, attributeName, engineState) =>


let getScriptAttributeFieldNames = (script, attributeName, engineState) => let getScriptAttributeFieldNames = (script, attributeName, engineState) =>
getScriptAttributeEntries(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.