Skip to content

Commit

Permalink
feat(script-component): fix "add script event function" bugs
Browse files Browse the repository at this point in the history
1.fix: script event function group for change should contain self

2.add/change script event function group should be sorted

3.fix:
1)add script event function asset a2;
2)show script event function group for change

group should contain a2
  • Loading branch information
yyc-git committed Apr 4, 2019
1 parent 3d06aab commit 4dbcf39
Show file tree
Hide file tree
Showing 7 changed files with 680 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@ type state = {
};

type action =
| ChangeScriptEventFunctionForAdd(NodeAssetType.nodeId)
| ChangeScriptEventFunctionForChange(NodeAssetType.nodeId)
| ChangeScriptEventFunctionForAdd(
NodeAssetType.nodeId,
array(NodeAssetType.nodeId),
)
| ChangeScriptEventFunctionForChange(
NodeAssetType.nodeId,
array(NodeAssetType.nodeId),
)
| ShowScriptEventFunctionGroupForAdd(
NodeAssetType.nodeId,
array(NodeAssetType.nodeId),
Expand Down Expand Up @@ -127,6 +133,28 @@ module Method = {
);
};

let _getUnUsedScriptEventFunctionNodeIds =
(script, (editorState, engineState)) =>
_getUnUsedScriptEventFunctionNodes(script, (editorState, engineState))
|> Js.Array.map(node => NodeAssetService.getNodeId(~node));

let _sendShowScriptEventFunctionGroupForChange =
(
currentScript,
scriptEventFunctionNodeId,
send,
(editorState, engineState),
) =>
send(
ShowScriptEventFunctionGroupForChange(
scriptEventFunctionNodeId,
_getUnUsedScriptEventFunctionNodeIds(
currentScript,
(editorState, engineState),
),
),
);

/* TODO refactor: extract select asset ui component(e.g. select scriptEventFunction/geometry) */
let renderScriptAllEventFunctions =
(languageType, {state, send}: ReasonReact.self('a, 'b, 'c)) => {
Expand All @@ -136,11 +164,10 @@ module Method = {
let {currentScript} = state;

let unUsedScriptEventFunctionNodeIds =
_getUnUsedScriptEventFunctionNodes(
_getUnUsedScriptEventFunctionNodeIds(
currentScript,
(editorState, engineState),
)
|> Js.Array.map(node => NodeAssetService.getNodeId(~node));
);

ScriptEngineService.getScriptAllEventFunctionEntries(
currentScript,
Expand All @@ -163,33 +190,33 @@ module Method = {
languageType,
)
}>
{DomHelper.textEl("ScriptEventFunction")}
{DomHelper.textEl("Script Event Function")}
</div>
<div className="item-content">
<div className="inspector-select">
<div
className="select-name"
onClick={
_e =>
send(
ShowScriptEventFunctionGroupForChange(
scriptEventFunctionNodeId,
unUsedScriptEventFunctionNodeIds,
),
_sendShowScriptEventFunctionGroupForChange(
currentScript,
scriptEventFunctionNodeId,
send,
)
|> StateLogicService.getStateToGetData
}>
{DomHelper.textEl(name)}
</div>
<div
className="select-img"
onClick={
_e =>
send(
ShowScriptEventFunctionGroupForChange(
scriptEventFunctionNodeId,
unUsedScriptEventFunctionNodeIds,
),
_sendShowScriptEventFunctionGroupForChange(
currentScript,
scriptEventFunctionNodeId,
send,
)
|> StateLogicService.getStateToGetData
}>
<img src="./public/img/select.png" />
</div>
Expand All @@ -199,14 +226,39 @@ module Method = {
});
};

let _sortScriptEventFunctionNodeIds = scriptEventFunctionNodeIds =>
scriptEventFunctionNodeIds |> Js.Array.sortInPlace;

let showScriptEventFunctionAssets =
(
unUsedScriptEventFunctionNodeIds,
/* isNodeIdEqualFunc, */
currentScriptEventFunctionNodeId,
changeScriptEventFunctionFunc,
) =>
unUsedScriptEventFunctionNodeIds
) => {
WonderLog.Contract.requireCheck(
() =>
WonderLog.(
Contract.(
Operators.(
test(
Log.buildAssertMessage(
~expect={j|currentScriptEventFunctionNodeId|j},
~actual={j|not|j},
),
() =>
currentScriptEventFunctionNodeId |> assertExist
)
)
)
),
StateEditorService.getStateIsDebug(),
);

ArrayService.fastConcat(
[|currentScriptEventFunctionNodeId |> OptionService.unsafeGet|],
unUsedScriptEventFunctionNodeIds,
)
|> _sortScriptEventFunctionNodeIds
|> Js.Array.map(scriptEventFunctionNodeId => {
let className =
_isNodeIdEqual(
Expand Down Expand Up @@ -235,10 +287,11 @@ module Method = {
}
</div>;
});
};

let _handleChangeScriptEventFunction =
(
state,
script,
sendFunc,
currentScriptEventFunctionNodeId,
targetScriptEventFunctionNodeId,
Expand Down Expand Up @@ -269,14 +322,18 @@ module Method = {
() :
{
_changeScriptEventFunction(
state.currentScript,
script,
currentScriptEventFunctionNodeId,
targetScriptEventFunctionNodeId,
)
|> StateLogicService.getStateToGetData
|> StateEngineService.setState;

sendFunc(targetScriptEventFunctionNodeId);
sendFunc(
targetScriptEventFunctionNodeId,
_getUnUsedScriptEventFunctionNodeIds(script)
|> StateLogicService.getStateToGetData,
);
};
};

Expand All @@ -285,7 +342,7 @@ module Method = {
<div className="select-component-content">
<div className="select-component-item">
<div className="select-item-header">
{DomHelper.textEl("Script Event Function")}
{DomHelper.textEl("Change Script Event Function")}
</div>
<div className="select-item-body">
{
Expand All @@ -294,10 +351,15 @@ module Method = {
state.unUsedScriptEventFunctionNodeIds,
state.lastScriptEventFunctionNodeIdForChange,
_handleChangeScriptEventFunction(
state, targetScriptEventFunctionNodeId =>
state.currentScript,
(
targetScriptEventFunctionNodeId,
unUsedScriptEventFunctionNodeIds,
) =>
send(
ChangeScriptEventFunctionForChange(
targetScriptEventFunctionNodeId,
unUsedScriptEventFunctionNodeIds,
),
)
),
Expand All @@ -317,7 +379,7 @@ module Method = {
<div className="select-component-content">
<div className="select-component-item">
<div className="select-item-header">
{DomHelper.textEl("Script Event Function")}
{DomHelper.textEl("Add Script Event Function")}
</div>
<div className="select-item-body">
{
Expand All @@ -326,10 +388,15 @@ module Method = {
state.unUsedScriptEventFunctionNodeIds,
state.lastScriptEventFunctionNodeIdForAdd,
_handleChangeScriptEventFunction(
state, targetScriptEventFunctionNodeId =>
state.currentScript,
(
targetScriptEventFunctionNodeId,
unUsedScriptEventFunctionNodeIds,
) =>
send(
ChangeScriptEventFunctionForAdd(
targetScriptEventFunctionNodeId,
unUsedScriptEventFunctionNodeIds,
),
)
),
Expand All @@ -344,10 +411,7 @@ module Method = {
/>
</div>;

let addScriptEventFunction =
(languageType, ({state, send}: ReasonReact.self('a, 'b, 'c)) as self) => {
/* TODO test */

let addScriptEventFunction = (languageType, (state, send)) => {
let editorState = StateEditorService.getState();
let engineState = StateEngineService.unsafeGetState();

Expand All @@ -365,8 +429,11 @@ module Method = {
unUsedScriptEventFunctionNodes
|> Js.Array.map(node => NodeAssetService.getNodeId(~node));

let lastScriptEventFunctionNodeIdForAdd =
unUsedScriptEventFunctionNodeIds |> ArrayService.unsafeGetFirst;
let (
lastScriptEventFunctionNodeIdForAdd,
unUsedScriptEventFunctionNodeIds,
) =
unUsedScriptEventFunctionNodeIds |> ArrayService.removeFirst;

let (name, data) =
ScriptEventFunctionNodeAssetEditorService.getNameAndData(
Expand Down Expand Up @@ -403,19 +470,27 @@ module Method = {

let component = ReasonReact.reducerComponent("MainEditorScript");

let reducer = ((uiState, dispatchFunc) as reduxTuple, action, state) =>
let reducer = (action, state) =>
switch (action) {
| ChangeScriptEventFunctionForAdd(targetScriptEventFunctionNodeId) =>
| ChangeScriptEventFunctionForAdd(
targetScriptEventFunctionNodeId,
unUsedScriptEventFunctionNodeIds,
) =>
ReasonReact.Update({
...state,
lastScriptEventFunctionNodeIdForAdd:
Some(targetScriptEventFunctionNodeId),
unUsedScriptEventFunctionNodeIds,
})
| ChangeScriptEventFunctionForChange(targetScriptEventFunctionNodeId) =>
| ChangeScriptEventFunctionForChange(
targetScriptEventFunctionNodeId,
unUsedScriptEventFunctionNodeIds,
) =>
ReasonReact.Update({
...state,
lastScriptEventFunctionNodeIdForChange:
Some(targetScriptEventFunctionNodeId),
unUsedScriptEventFunctionNodeIds,
})
| ShowScriptEventFunctionGroupForAdd(
lastScriptEventFunctionNodeIdForAdd,
Expand Down Expand Up @@ -453,11 +528,7 @@ let reducer = ((uiState, dispatchFunc) as reduxTuple, action, state) =>
})
};

let render =
(
(uiState, dispatchFunc),
({state, send}: ReasonReact.self('a, 'b, 'c)) as self,
) => {
let render = (({state, send}: ReasonReact.self('a, 'b, 'c)) as self) => {
let languageType =
LanguageEditorService.unsafeGetType |> StateLogicService.getEditorState;

Expand All @@ -478,7 +549,9 @@ let render =
}
<button
className="addable-btn"
onClick={_e => Method.addScriptEventFunction(languageType, self)}>
onClick={
_e => Method.addScriptEventFunction(languageType, (state, send))
}>
{
DomHelper.textEl(
LanguageUtils.getInspectorLanguageDataByType(
Expand Down Expand Up @@ -510,6 +583,6 @@ let make =
unUsedScriptEventFunctionNodeIds:
WonderCommonlib.ArrayService.createEmpty(),
},
reducer: reducer((uiState, dispatchFunc)),
render: self => render((uiState, dispatchFunc), self),
reducer,
render: self => render(self),
};
3 changes: 2 additions & 1 deletion src/service/atom/ArrayService.re
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ let removeFirst = arr => {
),
StateEditorService.getStateIsDebug(),
);
arr |> Js.Array.shift |> OptionService.unsafeGet;

(arr |> Js.Array.shift |> OptionService.unsafeGet, arr);
};

let unsafeGetNth = (index, arr) =>
Expand Down
Loading

0 comments on commit 4dbcf39

Please sign in to comment.