Skip to content

Commit

Permalink
feat(engine): fix TransformEngineService->changeChildOrder
Browse files Browse the repository at this point in the history
now consider sourceTransform's parent
  • Loading branch information
yyc-git committed Jan 15, 2019
1 parent 5e5cc0a commit 6ca57a2
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 70 deletions.
1 change: 0 additions & 1 deletion src/core/utils/engine/GameObjectUtils.re
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ let getParent = (child, engineState) =>

let getParentGameObject = (child, engineState) =>
getParent(child, engineState)
|> Js.Undefined.toOption
|> Js.Option.map((. parentTransform) =>
TransformEngineService.getGameObjectByTransform(
parentTransform,
Expand Down
37 changes: 21 additions & 16 deletions src/service/state/engine/TransformEngineService.re
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ let setTransformLocalEulerAngles = (localEulerAngles, transform, engineState) =>
);

let getParent = (child: Wonderjs.TransformType.transform, state) =>
Wonderjs.TransformAPI.unsafeGetTransformParent(child, state);
Wonderjs.TransformAPI.unsafeGetTransformParent(child, state)
|> Js.Undefined.toOption;

let setParent =
(
Expand Down Expand Up @@ -91,21 +92,25 @@ let changeChildOrder =
state,
)
: Wonderjs.StateDataMainType.state => {
...state,
transformRecord:
Some(
_changeChildOrder(
sourceTransfrom,
targetTransform,
getChildren(parentTransform, state),
action,
)
/* TODO move to engine */
|> Wonderjs.HierachyTransformService._setChildren(
Wonderjs.RecordTransformMainService.getRecord(state),
parentTransform,
),
),
let state = setParentKeepOrder(parentTransform, sourceTransfrom, state);

{
...state,
transformRecord:
Some(
_changeChildOrder(
sourceTransfrom,
targetTransform,
getChildren(parentTransform, state),
action,
)
/* TODO move to engine */
|> Wonderjs.HierachyTransformService._setChildren(
Wonderjs.RecordTransformMainService.getRecord(state),
parentTransform,
),
),
};
};

let getGameObjectByTransform = Wonderjs.TransformAPI.unsafeGetTransformGameObject;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
let rec _judgeAllParents = (targetTransform, draggedTransform, engineState) =>
switch (
TransformEngineService.getParent(targetTransform, engineState)
|> Js.Undefined.to_opt
) {
switch (TransformEngineService.getParent(targetTransform, engineState)) {
| None => false
| Some(transformParent) =>
transformParent === draggedTransform ?
Expand Down Expand Up @@ -34,7 +31,6 @@ let _checkTargetGameObjectBeDragedGameObjectParent =
draggedGameObject,
),
)
|> Js.Undefined.to_opt
) {
| None => Success()
| Some(transformParent) =>
Expand Down
214 changes: 166 additions & 48 deletions test/unit/service/TransformEngineService_test.re
Original file line number Diff line number Diff line change
Expand Up @@ -26,59 +26,177 @@ let _ =

describe("changeChildOrder", () =>
describe("change child order", () => {
let _prepare = state => {
let (state, parent) = Wonderjs.TransformAPI.createTransform(state);
let (state, child1) = Wonderjs.TransformAPI.createTransform(state);
let (state, child2) = Wonderjs.TransformAPI.createTransform(state);
let (state, child3) = Wonderjs.TransformAPI.createTransform(state);

let state =
state
|> Wonderjs.TransformAPI.setTransformParent(
Js.Nullable.return(parent),
child1,
)
|> Wonderjs.TransformAPI.setTransformParent(
Js.Nullable.return(parent),
child2,
)
|> Wonderjs.TransformAPI.setTransformParent(
Js.Nullable.return(parent),
child3,
);
describe("test source and target has the same parent", () => {
let _prepare = state => {
let (state, parent) =
Wonderjs.TransformAPI.createTransform(state);
let (state, child1) =
Wonderjs.TransformAPI.createTransform(state);
let (state, child2) =
Wonderjs.TransformAPI.createTransform(state);
let (state, child3) =
Wonderjs.TransformAPI.createTransform(state);

let state =
state
|> Wonderjs.TransformAPI.setTransformParent(
Js.Nullable.return(parent),
child1,
)
|> Wonderjs.TransformAPI.setTransformParent(
Js.Nullable.return(parent),
child2,
)
|> Wonderjs.TransformAPI.setTransformParent(
Js.Nullable.return(parent),
child3,
);

(state, parent, (child1, child2, child3));
};

test("test before", () => {
let (state, parent, (child1, child2, child3)) =
_prepare(state^);

let state =
TransformEngineService.changeChildOrder(
child3,
child1,
parent,
TransformType.Before,
state,
);

TransformEngineService.getChildren(parent, state)
|> expect == [|child3, child1, child2|];
});
test("test after", () => {
let (state, parent, (child1, child2, child3)) =
_prepare(state^);

let state =
TransformEngineService.changeChildOrder(
child3,
child1,
parent,
TransformType.After,
state,
);

TransformEngineService.getChildren(parent, state)
|> expect == [|child1, child3, child2|];
});
});

(state, parent, (child1, child2, child3));
};
describe("test source and target has different parents", () => {
let _prepare = state => {
let (state, parent1) =
Wonderjs.TransformAPI.createTransform(state);
let (state, parent2) =
Wonderjs.TransformAPI.createTransform(state);
let (state, child1) =
Wonderjs.TransformAPI.createTransform(state);
let (state, child2) =
Wonderjs.TransformAPI.createTransform(state);
let (state, child3) =
Wonderjs.TransformAPI.createTransform(state);

test("test before", () => {
let (state, parent, (child1, child2, child3)) = _prepare(state^);
let state =
state
|> Wonderjs.TransformAPI.setTransformParent(
Js.Nullable.return(parent1),
child1,
)
|> Wonderjs.TransformAPI.setTransformParent(
Js.Nullable.return(parent1),
child2,
)
|> Wonderjs.TransformAPI.setTransformParent(
Js.Nullable.return(parent2),
child3,
);

let state =
TransformEngineService.changeChildOrder(
child3,
child1,
parent,
TransformType.Before,
state,
);
(state, (parent1, parent2), (child1, child2, child3));
};

TransformEngineService.getChildren(parent, state)
|> expect == [|child3, child1, child2|];
test("test before", () => {
let (state, (parent1, parent2), (child1, child2, child3)) =
_prepare(state^);

let state =
TransformEngineService.changeChildOrder(
child3,
child1,
parent1,
TransformType.Before,
state,
);

(
TransformEngineService.getChildren(parent1, state),
TransformEngineService.getChildren(parent2, state),
)
|> expect == ([|child3, child1, child2|], [||]);
});
});
test("test after", () => {
let (state, parent, (child1, child2, child3)) = _prepare(state^);

let state =
TransformEngineService.changeChildOrder(
child3,
child1,
parent,
TransformType.After,
state,
);

TransformEngineService.getChildren(parent, state)
|> expect == [|child1, child3, child2|];

describe("test source not has a parent", () => {
let _prepare = state => {
let (state, parent1) =
Wonderjs.TransformAPI.createTransform(state);
/* let (state, parent2) =
Wonderjs.TransformAPI.createTransform(state); */
let (state, child1) =
Wonderjs.TransformAPI.createTransform(state);
let (state, child2) =
Wonderjs.TransformAPI.createTransform(state);
let (state, child3) =
Wonderjs.TransformAPI.createTransform(state);

let state =
state
|> Wonderjs.TransformAPI.setTransformParent(
Js.Nullable.return(parent1),
child1,
)
|> Wonderjs.TransformAPI.setTransformParent(
Js.Nullable.return(parent1),
child2,
);
/* |> Wonderjs.TransformAPI.setTransformParent(
Js.Nullable.return(parent2),
child3,
); */

(state, parent1, (child1, child2, child3));
};

test("test before", () => {
let (state, parent1, (child1, child2, child3)) =
_prepare(state^);

let state =
TransformEngineService.changeChildOrder(
child3,
child1,
parent1,
TransformType.Before,
state,
);

(
TransformEngineService.getChildren(parent1, state),
TransformEngineService.getParent(child1, state),
TransformEngineService.getParent(child3, state),
)
|>
expect == (
[|child3, child1, child2|],
Some(parent1),
Some(parent1),
);
});
});
})
);
Expand Down

0 comments on commit 6ca57a2

Please sign in to comment.