Skip to content
Permalink
Browse files

feat(script): script api now update editor

add init_script_api job;
api->disposeGameObject now clear current data and dispatch;
  • Loading branch information...
yyc-git committed Apr 6, 2019
1 parent e9ac408 commit 5fb6843c5e2d681434deabdfb0309fce30c4079f
@@ -15,6 +15,7 @@ module.exports = {
],

scopes: [
{ name: "script" },
{ name: "language" },
{ name: "pwa" },
{ name: "hotkey" },
@@ -58,5 +58,8 @@
},
{
"name": "init_imgui"
},
{
"name": "init_script_api"
}
]
@@ -61,6 +61,9 @@
},
{
"name": "init_imgui"
},
{
"name": "init_script_api"
}
]
}
@@ -23,22 +23,16 @@ module CustomEventHandler = {
)
};

let _clearCurrentData = editorState =>
editorState
|> CurrentNodeIdAssetEditorService.clearCurrentNodeId
|> CurrentSelectSourceEditorService.clearCurrentSelectSource
|> SceneTreeEditorService.clearCurrentSceneTreeNode;

let handleSelfLogic = ((uiState, dispatchFunc), (), gameObjectOpt) => {
switch (gameObjectOpt) {
| None =>
StateEditorService.getState()
|> _clearCurrentData
|> SceneTreeSelectCurrentNodeUtils.clearCurrentData
|> StateEditorService.setState
|> ignore
| Some(gameObject) =>
StateEditorService.getState()
|> _clearCurrentData
|> SceneTreeSelectCurrentNodeUtils.clearCurrentData
|> StateEditorService.setState
|> ignore;

@@ -68,7 +62,6 @@ module CustomEventHandler = {
StateLogicService.getAndRefreshEngineStateWhenStop();
};


dispatchFunc(
AppStore.UpdateAction(Update([|SceneTree, Inspector, Project|])),
)
@@ -0,0 +1,5 @@
let clearCurrentData = editorState =>
editorState
|> CurrentNodeIdAssetEditorService.clearCurrentNodeId
|> CurrentSelectSourceEditorService.clearCurrentSelectSource
|> SceneTreeEditorService.clearCurrentSceneTreeNode;
@@ -0,0 +1,46 @@
let _buildDisposeGameObjectFunc = scriptAPIJsObj =>
(. gameObject, engineState) => {
let disposeGameObject = scriptAPIJsObj##disposeGameObject;

let engineState = disposeGameObject(. gameObject, engineState);

let engineState = engineState |> JobEngineService.execDisposeJob;

engineState |> StateEngineService.setState |> ignore;

SceneTreeSelectCurrentNodeUtils.clearCurrentData
|> StateLogicService.getAndSetEditorState;

UIStateService.getDispatch(
(),
AppStore.UpdateAction(
Update([|UpdateStore.SceneTree, UpdateStore.Inspector|]),
),
);

StateEngineService.unsafeGetState();
};

let _createScriptAPIJsObj = scriptAPIJsObj => {
"unsafeGetScriptAttribute": scriptAPIJsObj##unsafeGetScriptAttribute,
"unsafeGetScriptAttributeFieldValue":
scriptAPIJsObj##unsafeGetScriptAttributeFieldValue,
"setScriptAttributeFieldValue": scriptAPIJsObj##setScriptAttributeFieldValue,
"unsafeGetScriptGameObject": scriptAPIJsObj##unsafeGetScriptGameObject,
"getTransformLocalPosition": scriptAPIJsObj##getTransformLocalPosition,
/* TODO should only set MainEditorTransform->local position values */
"setTransformLocalPosition": scriptAPIJsObj##setTransformLocalPosition,
"unsafeGetGameObjectTransformComponent":
scriptAPIJsObj##unsafeGetGameObjectTransformComponent,
"disposeGameObject": _buildDisposeGameObjectFunc(scriptAPIJsObj),
"findGameObjectsByName": scriptAPIJsObj##findGameObjectsByName,
};

let initJob =
(_, ({apiRecord}: Wonderjs.StateDataMainType.state) as engineState) => {
...engineState,
apiRecord: {
...apiRecord,
scriptAPIJsObj: _createScriptAPIJsObj(apiRecord.scriptAPIJsObj),
},
};
@@ -39,6 +39,10 @@ let _registerJob = engineState =>
"init_camera_controller",
InitCameraControllerJob.initJob,
)
|> JobEngineService.registerNoWorkerInitJob(
"init_script_api",
InitScriptAPIJob.initJob,
)
|> JobEngineService.registerNoWorkerLoopJob(
"reallocate_cpu_memory",
ReallocateCPUMemoryJob.reallocateJob,
@@ -0,0 +1,65 @@
open Wonder_jest;

open Expect;

open Expect.Operators;

open Sinon;

let _ =
describe("init script api job", () => {
let sandbox = getSandboxDefaultVal();

beforeEach(() => {
sandbox := createSandbox();
MainEditorSceneTool.initState(~sandbox, ());
});
afterEach(() => restoreSandbox(refJsObjToSandbox(sandbox^)));

describe("test api", () =>
describe("disposeGameObject", () => {
describe("handle engine state", () =>
test("dispose gameObject", () => {
let disposeGameObjectFunc =
InitScriptAPIJob._buildDisposeGameObjectFunc(
InitScriptJobTool.createScriptAPIJsObj(),
);

let engineState = StateEngineService.unsafeGetState();

let (engineState, gameObject, _) =
GameObjectToolEngine.createGameObject(engineState);

let engineState =
disposeGameObjectFunc(. gameObject, engineState);

GameObjectToolEngine.isAlive(gameObject, engineState)
|> expect == false;
})
);

describe("handle editor state", () =>
test("clear current data", () => {
let disposeGameObjectFunc =
InitScriptAPIJob._buildDisposeGameObjectFunc(
InitScriptJobTool.createScriptAPIJsObj(),
);
let engineState = StateEngineService.unsafeGetState();
let (engineState, gameObject, _) =
GameObjectToolEngine.createGameObject(engineState);
engineState |> StateEngineService.setState |> ignore;
GameObjectTool.setCurrentSceneTreeNode(gameObject);

let engineState = StateEngineService.unsafeGetState();
let engineState =
disposeGameObjectFunc(. gameObject, engineState);
engineState |> StateEngineService.setState |> ignore;

GameObjectTool.getCurrentSceneTreeNode()
|> Js.Option.isNone
|> expect == true;
})
);
})
);
});
@@ -0,0 +1 @@
let createScriptAPIJsObj = () => Wonderjs.RecordScriptAPIMainService.create();
@@ -39,15 +39,17 @@ let _ =
{
"name": "default",
"jobs": [
{"name": "init_editor" }
{"name": "init_editor" },
{"name": "init_script_api" }
]
}
]
|},
~initJobs=
{|
[
{"name": "init_editor" }
{"name": "init_editor" },
{"name": "init_script_api" }
]
|},
~loopPipelines=
@@ -78,15 +80,17 @@ let _ =
{
"name": "default",
"jobs": [
{"name": "init_editor" }
{"name": "init_editor" },
{"name": "init_script_api" }
]
}
]
|},
~initJobs=
{|
[
{"name": "init_editor" }
{"name": "init_editor" },
{"name": "init_script_api" }
]
|},
~loopPipelines=
@@ -134,13 +138,19 @@ let _ =
editorState,
);

let script = GameObjectTool.getCurrentSceneTreeNodeScript();

let gameObject =
ScriptEngineService.unsafeGetScriptGameObject(script)
|> StateLogicService.getEngineStateToGetData;

MainEditorScriptEventFunctionTool.addScriptEventFunction(
~script=GameObjectTool.getCurrentSceneTreeNodeScript(),
~send=SinonTool.createOneLengthStub(sandbox^),
(),
);

(addedNodeId, eventFunctionName);
(script, gameObject, addedNodeId, eventFunctionName);
};

let _prepareTwoScriptEventFunctions = () => {
@@ -209,7 +219,7 @@ let _ =
_prepareWithNoWorkerJobRecord(
_buildNoWorkerJobConfigOnlyWithUpdateScript(),
);
let (addedNodeId, eventFunctionName) =
let (script, gameObject, addedNodeId, eventFunctionName) =
_prepareOneScriptEventFunction();
ScriptEventFunctionInspectorTool.updateEventFunctionData(
addedNodeId,
@@ -238,7 +248,7 @@ let _ =
_prepareWithNoWorkerJobRecord(
_buildNoWorkerJobConfigOnlyWithUpdateScript(),
);
let (addedNodeId, eventFunctionName) =
let (script, gameObject, addedNodeId, eventFunctionName) =
_prepareOneScriptEventFunction();
ScriptEventFunctionInspectorTool.updateEventFunctionData(
addedNodeId,
@@ -385,7 +395,7 @@ let _ =
_prepareWithNoWorkerJobRecord(
_buildNoWorkerJobConfigOnlyWithUpdateScript(),
);
let (addedNodeId, eventFunctionName) =
let (script, gameObject, addedNodeId, eventFunctionName) =
_prepareOneScriptEventFunction();
ScriptEventFunctionInspectorTool.updateEventFunctionData(
addedNodeId,
@@ -411,5 +421,73 @@ let _ =
|> expect !== Obj.magic(-1);
})
);

describe("script api should update editor", () =>
describe("test disposeGameObject api", () =>
test("should update scene tree", () => {
_prepareWithNoWorkerJobRecord(
_buildNoWorkerJobConfigWithDispose(),
);
let (script, gameObject, addedNodeId, eventFunctionName) =
_prepareOneScriptEventFunction();
let name = "secondCube";
let engineState = StateEngineService.unsafeGetState();
engineState
|> GameObjectEngineService.setGameObjectName(
name,
MainEditorSceneTool.getSecondCube(engineState),
)
|> StateEngineService.setState
|> ignore;
ScriptEventFunctionInspectorTool.updateEventFunctionData(
addedNodeId,
eventFunctionName,
ScriptEventFunctionInspectorTool.buildEventFunctionDataJsObjStr(
~updateFunc=
Some(
(. script, api, state: Wonderjs.StateDataMainType.state) => {
let disposeGameObject = api##disposeGameObject;
let findGameObjectsByName = api##findGameObjectsByName;

let gameObjects =
findGameObjectsByName(. "secondCube", state);

let state =
switch (gameObjects |> Js.Array.length) {
| 0 => state
| _ =>
let secondCubeGameObject =
Array.unsafe_get(
findGameObjectsByName(. "secondCube", state),
0,
);

disposeGameObject(. secondCubeGameObject, state);
};

state;
},
),
(),
),
);
let dispatchFuncStub = ReactTool.createDispatchFuncStub(sandbox);

ControllerTool.run();
LoopTool.getAndRefreshEngineStateForRunLoop();

let engineState = StateEngineService.unsafeGetState();

dispatchFuncStub
|> withOneArg(
AppStore.UpdateAction(
Update([|UpdateStore.SceneTree, UpdateStore.Inspector|]),
),
)
|> getCallCount
|> expect == 1;
})
)
);
});
});

0 comments on commit 5fb6843

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