Skip to content

Commit

Permalink
fix(event): fix unbind arcball camera controller->keydown event
Browse files Browse the repository at this point in the history
  • Loading branch information
yyc-git committed May 21, 2019
1 parent dce7094 commit 2290309
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 56 deletions.
2 changes: 2 additions & 0 deletions src/Index.re
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ let loadConfig = LoaderManagerAPI.loadConfig;

let isBindArcballCameraControllerEvent = ArcballCameraControllerAPI.isBindArcballCameraControllerEvent;

let unbindArcballCameraControllerPointScaleEvent = ArcballCameraControllerAPI.unbindArcballCameraControllerPointScaleEvent;

let unbindArcballCameraControllerEvent = ArcballCameraControllerAPI.unbindArcballCameraControllerEvent;

let bindArcballCameraControllerEvent = ArcballCameraControllerAPI.bindArcballCameraControllerEvent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,13 @@ let prepareBindEvent = (cameraController, state) => {
event: EventType.keyboardEvent,
{arcballCameraControllerRecord} as state,
) =>
TargetArcballCameraControllerMainService.setTargetByKeyboardEvent(
cameraController,
event,
state,
);
isTriggerKeydownEventHandler(event) ?
TargetArcballCameraControllerMainService.setTargetByKeyboardEvent(
cameraController,
event,
state,
) :
state;

let state = {
...state,
Expand Down Expand Up @@ -295,10 +297,7 @@ let bindEvent = (cameraController, state) => {
let state =
ManageEventMainService.onKeyboardEvent(
~eventName=EventType.KeyDown,
~handleFunc=
(. event, state) =>
isTriggerKeydownEventHandler(event) ?
keydownHandleFunc(. event, state) : state,
~handleFunc=keydownHandleFunc,
~state,
(),
);
Expand Down Expand Up @@ -471,6 +470,11 @@ let _disposeKeyDownEventHandleFuncListMap =
|> List.fold_left(
(state, func) =>
_unbindKeyboardEvent(EventType.KeyDown, func, state),
/* ManageEventMainService.offKeyboardEventByHandleFunc(
~eventName,
~handleFunc,
~state,
); */
state,
);

Expand Down
8 changes: 6 additions & 2 deletions src/service/state/main/event/bind/BindDomEventMainService.re
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
let addToEventArr = (eventName, eventData, getPriorityFunc, eventArrMap) =>
switch (eventArrMap |> WonderCommonlib.MutableSparseMapService.get(eventName)) {
switch (
eventArrMap |> WonderCommonlib.MutableSparseMapService.get(eventName)
) {
| None =>
eventArrMap
|> WonderCommonlib.MutableSparseMapService.set(eventName, [|eventData|])
Expand All @@ -17,7 +19,9 @@ let addToEventArr = (eventName, eventData, getPriorityFunc, eventArrMap) =>

let removeFromEventArrMapByHandleFunc =
(eventName, (getHandleFuncFunc, targetHandleFunc), eventArrMap) =>
switch (eventArrMap |> WonderCommonlib.MutableSparseMapService.get(eventName)) {
switch (
eventArrMap |> WonderCommonlib.MutableSparseMapService.get(eventName)
) {
| None => eventArrMap
| Some(arr) =>
eventArrMap
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,44 @@ let _ =
let _prepareMouseEvent = () =>
EventArcballCameraControllerTool.prepareMouseEvent(sandbox);

let _prepareKeyEvent = () => {
let state =
EventArcballCameraControllerTool.prepareKeyboardEvent(sandbox);
let (
state,
gameObject,
transform,
(cameraController, basicCameraView, perspectiveCameraProjection),
) =
ArcballCameraControllerTool.createGameObject(state);
let (posX, posY, posZ) as pos = (1., 2., 3.);
let state =
state |> TransformAPI.setTransformLocalPosition(transform, pos);
let _ = TransformAPI.getTransformPosition(transform, state);
let target = pos;
let moveSpeedX = 0.1;
let moveSpeedY = 0.2;
let state =
state
|> setArcballCameraControllerTarget(cameraController, target)
|> setArcballCameraControllerMoveSpeedX(
cameraController,
moveSpeedX,
)
|> setArcballCameraControllerMoveSpeedY(
cameraController,
moveSpeedY,
);
let state = state |> NoWorkerJobTool.execInitJobs;
let state =
ArcballCameraControllerAPI.bindArcballCameraControllerEvent(
cameraController,
state,
);

(state, cameraController, (moveSpeedX, moveSpeedY), pos);
};

describe("test bind one arcballCameraController's event", () => {
describe("bind event", () => {
describe("support pointer lock", () => {
Expand Down Expand Up @@ -348,51 +386,6 @@ let _ =
});

describe("bind keydown event", () => {
let _prepareKeyEvent = () => {
let state =
EventArcballCameraControllerTool.prepareKeyboardEvent(
sandbox,
);
let (
state,
gameObject,
transform,
(
cameraController,
basicCameraView,
perspectiveCameraProjection,
),
) =
ArcballCameraControllerTool.createGameObject(state);
let (posX, posY, posZ) as pos = (1., 2., 3.);
let state =
state
|> TransformAPI.setTransformLocalPosition(transform, pos);
let _ = TransformAPI.getTransformPosition(transform, state);
let target = pos;
let moveSpeedX = 0.1;
let moveSpeedY = 0.2;
let state =
state
|> setArcballCameraControllerTarget(cameraController, target)
|> setArcballCameraControllerMoveSpeedX(
cameraController,
moveSpeedX,
)
|> setArcballCameraControllerMoveSpeedY(
cameraController,
moveSpeedY,
);
let state = state |> NoWorkerJobTool.execInitJobs;
let state =
ArcballCameraControllerAPI.bindArcballCameraControllerEvent(
cameraController,
state,
);

(state, cameraController, (moveSpeedX, moveSpeedY), pos);
};

test("if is combined key, not set target", () => {
let (
state,
Expand Down Expand Up @@ -587,6 +580,32 @@ let _ =

preventDefaultFunc |> expect |> not_ |> toCalled;
});
test("test unbind keydown event", () => {
let (
state,
cameraController,
(moveSpeedX, moveSpeedY),
(posX, posY, posZ),
) =
_prepareKeyEvent();
let state =
ArcballCameraControllerAPI.unbindArcballCameraControllerEvent(
cameraController,
state,
);

let state = MainStateTool.setState(state);
EventTool.triggerDomEvent(
"keydown",
EventTool.getKeyboardEventBindedDom(state),
KeyboardEventTool.buildKeyboardEvent(~keyCode=65, ()),
);
let state = EventTool.restore(state);

state
|> unsafeGetArcballCameraControllerTarget(cameraController)
|> expect == (posX, posY, posZ);
});

describe("fix bug", () =>
test(
Expand Down

0 comments on commit 2290309

Please sign in to comment.