Skip to content

Commit

Permalink
feat(picking): support pick nothing
Browse files Browse the repository at this point in the history
  • Loading branch information
yyc-git committed Jan 20, 2019
1 parent d792b55 commit c797550
Show file tree
Hide file tree
Showing 10 changed files with 234 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ open UpdateStore;
module CustomEventHandler = {
include EmptyEventHandler.EmptyEventHandler;
type prepareTuple = unit;
type dataTuple = Wonderjs.GameObjectPrimitiveType.gameObject;
type dataTuple = option(Wonderjs.GameObjectPrimitiveType.gameObject);
type return = unit;

let handleSelfLogic = ((store, dispatchFunc), (), uid) =>
SceneTreeSelectCurrentNodeUtils.select(dispatchFunc, uid);
let handleSelfLogic = ((store, dispatchFunc), (), gameObjectOpt) =>
SceneTreeSelectCurrentNodeUtils.select(dispatchFunc, gameObjectOpt);
};

module MakeEventHandler = EventHandler.MakeEventHandler(CustomEventHandler);
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ module Method = {
SceneTreeSelectCurrentNodeEventHandler.MakeEventHandler.pushUndoStackWithNoCopyEngineState(
(store, dispatchFunc),
(),
uid,
Some(uid),
)
| Some(gameObject) =>
gameObject === uid ?
() :
SceneTreeSelectCurrentNodeEventHandler.MakeEventHandler.pushUndoStackWithNoCopyEngineState(
(store, dispatchFunc),
(),
uid,
Some(uid),
)
};
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,39 @@ let rec _setAllParentsShowChildren = (gameObject, engineState, editorState) =>
)
};

let select = (dispatchFunc, uid) => {
StateEditorService.getState()
let _clearCurrentData = editorState =>
editorState
|> CurrentNodeIdAssetEditorService.clearCurrentNodeId
|> StateEditorService.setState
|> ignore;
|> CurrentSelectSourceEditorService.clearCurrentSelectSource
|> SceneTreeEditorService.clearCurrentSceneTreeNode;

StateEditorService.getState()
|> SceneTreeEditorService.setCurrentSceneTreeNode(uid)
|> CurrentSelectSourceEditorService.setCurrentSelectSource(
SceneTreeWidgetService.getWidget(),
)
|> _setAllParentsShowChildren(uid, StateEngineService.unsafeGetState())
|> StateEditorService.setState
|> ignore;
let select = (dispatchFunc, gameObjectOpt) => {
switch (gameObjectOpt) {
| None =>
StateEditorService.getState()
|> _clearCurrentData
|> StateEditorService.setState
|> ignore
| Some(gameObject) =>
StateEditorService.getState()
|> _clearCurrentData
|> StateEditorService.setState
|> ignore;

StateLogicService.getAndRefreshEngineStateWhenStop();
StateEditorService.getState()
|> SceneTreeEditorService.setCurrentSceneTreeNode(gameObject)
|> CurrentSelectSourceEditorService.setCurrentSelectSource(
SceneTreeWidgetService.getWidget(),
)
|> _setAllParentsShowChildren(
gameObject,
StateEngineService.unsafeGetState(),
)
|> StateEditorService.setState
|> ignore;

StateLogicService.getAndRefreshEngineStateWhenStop();
};

dispatchFunc(
AppStore.UpdateAction(Update([|SceneTree, Inspector, Project|])),
Expand Down
25 changes: 18 additions & 7 deletions src/core/composable_component/mainEditor/ui/MainEditor.re
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,16 @@ module Method = {
|> StateEngineService.setState
|> ignore;

let bindPickSuccessEvent = dispatchFunc =>
let _bindPickEvent = (dispatchFunc, eventName) =>
ManageEventEngineService.onCustomGlobalEvent(
~eventName=EventEditorService.getPickSuccessEventName(),
~eventName,
~handleFunc=
(. ({userData}: EventType.customEvent) as event, engineState) => {
let pickedGameObject =
let pickedGameObjectOpt =
userData
|> OptionService.unsafeGet
|> InitPickingJobType.userDataToGameObject;
|> Js.Option.map((. userData) =>
userData |> InitPickingJobType.userDataToGameObject
);

engineState |> StateEngineService.setState |> ignore;

Expand All @@ -109,13 +110,13 @@ module Method = {
| None =>
SceneTreeSelectCurrentNodeUtils.select(
dispatchFunc,
pickedGameObject,
pickedGameObjectOpt,
)
| Some(lastStore) =>
SceneTreeSelectCurrentNodeEventHandler.MakeEventHandler.pushUndoStackWithNoCopyEngineState(
(lastStore, dispatchFunc),
(),
pickedGameObject,
pickedGameObjectOpt,
)
};

Expand All @@ -127,6 +128,15 @@ module Method = {
|> StateEngineService.setState
|> ignore;

let bindPickSuccessEvent = dispatchFunc =>
_bindPickEvent(
dispatchFunc,
EventEditorService.getPickSuccessEventName(),
);

let bindPickFailEvent = dispatchFunc =>
_bindPickEvent(dispatchFunc, EventEditorService.getPickFailEventName());

let dragWDB = MainEditorDragWDBEventHandler.MakeEventHandler.pushUndoStackWithNoCopyEngineState;
};

Expand Down Expand Up @@ -216,6 +226,7 @@ let make = (~store: AppStore.appState, ~dispatchFunc, _children) => {

Method.bindRefreshInspectorEvent(dispatchFunc);
Method.bindPickSuccessEvent(dispatchFunc);
Method.bindPickFailEvent(dispatchFunc);

DomHelper.onresize(Method.resizeCanvasAndViewPort);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ let _findPickedOne =
|> _getTopOne(cameraGameObject, engineState);
};

let _triggerPickSuccessEvent = (gameObject, (editorState, engineState)) => {
let _triggerPickSuccessEvent = (gameObject, engineState) => {
let (engineState, _) =
ManageEventEngineService.triggerCustomGlobalEvent(
CreateCustomEventEngineService.create(
Expand All @@ -219,6 +219,19 @@ let _triggerPickSuccessEvent = (gameObject, (editorState, engineState)) => {
engineState;
};

let _triggerPickFailEvent = engineState => {
let (engineState, _) =
ManageEventEngineService.triggerCustomGlobalEvent(
CreateCustomEventEngineService.create(
EventEditorService.getPickFailEventName(),
None,
),
engineState,
);

engineState;
};

let _handlePicking = (event: EventType.customEvent, engineState) => {
let editorState = StateEditorService.getState();

Expand All @@ -228,13 +241,12 @@ let _handlePicking = (event: EventType.customEvent, engineState) => {
_computeSphereShapeData(allGameObjectData, (editorState, engineState));

let engineState =
(editorState, engineState)
|> _findPickedOne(event, allGameObjectData)
|> OptionService.andThenWithDefault(
gameObject =>
_triggerPickSuccessEvent(gameObject, (editorState, engineState)),
engineState,
);
switch (
(editorState, engineState) |> _findPickedOne(event, allGameObjectData)
) {
| None => _triggerPickFailEvent(engineState)
| Some(gameObject) => _triggerPickSuccessEvent(gameObject, engineState)
};

(engineState, event);
};
Expand Down
4 changes: 3 additions & 1 deletion src/service/state/editor/event/EventEditorService.re
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ let getPointDragEventName = () => "wd_editor_pointdrag";

let getRefreshInspectorEventName = () => "wd_editor_refresh_inspector";

let getPickSuccessEventName = () => "wd_editor_pick_success";
let getPickSuccessEventName = () => "wd_editor_pick_success";

let getPickFailEventName = () => "wd_editor_pick_fail";
3 changes: 3 additions & 0 deletions test/integration/asset/tool/MainEditorAssetNodeTool.re
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ let unsafeGetCurrentNode = editorState =>
OperateTreeAssetEditorService.getCurrentNode(editorState)
|> OptionService.unsafeGet;

let getCurrentNodeId = editorState =>
CurrentNodeIdAssetEditorService.getCurrentNodeId(editorState);

let unsafeGetCurrentNodeId = editorState =>
CurrentNodeIdAssetEditorService.unsafeGetCurrentNodeId(editorState);

Expand Down
60 changes: 60 additions & 0 deletions test/integration/job/initPickingJob_test.re
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,66 @@ let _ =
});
});

describe("if not find", () =>
describe("trigger pick fail event", () => {
let _prepare = () =>
InitPickingJobTool.prepareOneGameObject(
~sandbox,
~viewWidth=500,
~viewHeight=200,
~offsetLeft=10,
~offsetTop=20,
~cameraPos=(0., 0., 20.),
~gameObjectPos=(0., 0., 0.),
~gameObjectEulerAngles=(0., 0., 0.),
~createGameObjectFunc=InitPickingJobTool.createCube,
(),
);

let _triggerPicking = () =>
InitPickingJobTool.triggerPickingAndRestore(
~sandbox,
~pageX=400 + 10,
~pageY=100 + 20,
(),
);

test("clear curent scene tree node", () => {
let _ = _prepare();
let gameObject = 500;
GameObjectTool.setCurrentSceneTreeNode(gameObject);

_triggerPicking();

InitPickingJobTool.notPick();
});
test("clear curent asset node id", () => {
let _ = _prepare();
let nodeId = 500;
MainEditorAssetNodeTool.setCurrentNodeId(nodeId);

_triggerPicking();

MainEditorAssetNodeTool.getCurrentNodeId
|> StateLogicService.getEditorState
|> expect == None;
});
test("clear curent select source", () => {
let _ = _prepare();
CurrentSelectSourceEditorService.setCurrentSelectSource(
WidgetType.Asset,
)
|> StateLogicService.getAndSetEditorState;

_triggerPicking();

CurrentSelectSourceEditorService.getCurrentSelectSource
|> StateLogicService.getEditorState
|> expect == None;
});
})
);

describe("isIntersectTriangle", () =>
describe("test cull", () => {
let _isIntersectTriangle =
Expand Down
3 changes: 3 additions & 0 deletions test/integration/job/tool/InitPickingJobTool.re
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,9 @@ let prepareState = (sandbox, editorState, engineState) => {
MainEditor.Method.bindPickSuccessEvent(
Sinon.createEmptyStubWithJsObjSandbox(sandbox),
);
MainEditor.Method.bindPickFailEvent(
Sinon.createEmptyStubWithJsObjSandbox(sandbox),
);
};

let triggerPickingAndNotRestore = (sandbox, pageX, pageY, eventButton) => {
Expand Down
Loading

0 comments on commit c797550

Please sign in to comment.