Skip to content

Commit

Permalink
feat(asset): script attribute asset: add "update script attribute in …
Browse files Browse the repository at this point in the history
…all script components" logic
  • Loading branch information
yyc-git committed Apr 4, 2019
1 parent ad54248 commit 32d3785
Show file tree
Hide file tree
Showing 8 changed files with 444 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,74 @@ module Method = {
editorState,
);

let _isOnlyFieldDefaultValueChange =
(
(
{type_, defaultValue}: Wonderjs.ScriptAttributeType.scriptAttributeField
) as oldAttributeField,
newAttributeFieldJsObj,
) => {
let newType =
ScriptAttributeTypeService.getTypeFromJsObj(newAttributeFieldJsObj);

let newDefaultValue = newAttributeFieldJsObj##defaultValue;

type_
|> ScriptAttributeTypeService.convertFieldTypeToJsObjStr === newType
&& defaultValue !== newDefaultValue;
};

let _updateScriptAttributeNodeByReplaceFieldData =
(nodeId, (fieldName, newFieldDataJsObjStr), editorState) =>
(
nodeId,
(fieldName, newFieldDataJsObjStr),
(editorState, engineState),
) =>
Console.tryCatch(
() => {
let (attributeName, attribute) =
ScriptAttributeNodeAssetEditorService.getNameAndAttribute(nodeId)
|> StateLogicService.getEditorState;

_updateScriptAttributeNode(
nodeId,
attributeName,
let newAttributeFieldJsObj =
newFieldDataJsObjStr |> Js.Json.parseExn |> Obj.magic;

let isOnlyFieldDefaultValueChange =
_isOnlyFieldDefaultValueChange(
ScriptAttributeEngineService.unsafeGetScriptAttributeField(
fieldName,
attribute,
),
newAttributeFieldJsObj,
);

let newAttribute =
ScriptAttributeEngineService.replaceScriptAttributeField(
fieldName,
newFieldDataJsObjStr |> Js.Json.parseExn |> Obj.magic,
newAttributeFieldJsObj,
attribute,
),
editorState,
);
);

let editorState =
_updateScriptAttributeNode(
nodeId,
attributeName,
newAttribute,
editorState,
);

isOnlyFieldDefaultValueChange ?
(editorState, engineState) :
{
let engineState =
ScriptEngineService.updateAttributeInAllScriptComponents(
attributeName,
newAttribute,
engineState,
);

(editorState, engineState);
};
},
e => {
let message = e##message;
Expand All @@ -55,12 +105,12 @@ module Method = {
)
|> StateLogicService.getEditorState;

editorState;
(editorState, engineState);
},
);

let _updateScriptAttributeNodeByRemoveFieldData =
(nodeId, fieldName, editorState) => {
(nodeId, fieldName, (editorState, engineState)) => {
let (attributeName, attribute) =
ScriptAttributeNodeAssetEditorService.getNameAndAttribute(
nodeId,
Expand All @@ -73,22 +123,30 @@ module Method = {
attribute,
);

(
_updateScriptAttributeNode(
nodeId,
let editorState =
editorState
|> _updateScriptAttributeNode(nodeId, attributeName, newAttribute);

let engineState =
ScriptEngineService.updateAttributeInAllScriptComponents(
attributeName,
newAttribute,
editorState,
),
newAttribute,
);
engineState,
);

((editorState, engineState), newAttribute);
};

let _getAttributeNodeData = (nodeId, editorState) =>
OperateTreeAssetEditorService.unsafeFindNodeById(nodeId)
|> StateLogicService.getEditorState
|> ScriptAttributeNodeAssetService.getNodeData;

let _getDefaultFieldType = () => "float";

let _getDefaultFieldDefaultValue = () =>
0.0 |> Wonderjs.ScriptAttributeType.floatToScriptAttributeValue;

let addDefaultField = (send, nodeId) => {
let {name as attributeName, attribute}: NodeAssetType.scriptAttributeNodeData =
_getAttributeNodeData(nodeId) |> StateLogicService.getEditorState;
Expand All @@ -100,17 +158,21 @@ module Method = {
attribute,
),
{
"type": "float",
"defaultValue":
0.0 |> Wonderjs.ScriptAttributeType.floatToScriptAttributeValue,
"type": _getDefaultFieldType(),
"defaultValue": _getDefaultFieldDefaultValue(),
},
attribute,
);

/* TODO update script component */
_updateScriptAttributeNode(nodeId, attributeName, attribute)
|> StateLogicService.getAndSetEditorState;

ScriptEngineService.updateAttributeInAllScriptComponents(
attributeName,
attribute,
)
|> StateLogicService.getAndSetEngineState;

send(UpdateAttributeEntries(attribute));
};

Expand All @@ -135,7 +197,6 @@ module Method = {

let _renameField = (languageType, send, nodeId, oldFieldName, newFieldName) =>
/* TODO refactor */
/* TODO update script component */
oldFieldName === newFieldName ?
() :
{
Expand Down Expand Up @@ -169,6 +230,12 @@ module Method = {
_updateScriptAttributeNode(nodeId, attributeName, newAttribute)
|> StateLogicService.getAndSetEditorState;

ScriptEngineService.updateAttributeInAllScriptComponents(
attributeName,
newAttribute,
)
|> StateLogicService.getAndSetEngineState;

send(UpdateAttributeEntries(newAttribute));
};
};
Expand Down Expand Up @@ -202,28 +269,26 @@ module Method = {
inputValue={_convertFieldToJsObjStr(field)}
onSubmit={
value =>
/* TODO update script component */
_updateScriptAttributeNodeByReplaceFieldData(
nodeId,
(fieldName, value),
)
|> StateLogicService.getAndSetEditorState
|> StateLogicService.getAndSetState
}
/>
<button
className="scriptAttribute-field-remove"
onClick={
e => {
/* TODO update script component */

let (editorState, newAttribute) =
let ((editorState, engineState), newAttribute) =
_updateScriptAttributeNodeByRemoveFieldData(
nodeId,
fieldName,
)
|> StateLogicService.getEditorState;
|> StateLogicService.getStateToGetData;

editorState |> StateEditorService.setState |> ignore;
engineState |> StateEngineService.setState |> ignore;

send(UpdateAttributeEntries(newAttribute));
}
Expand Down
6 changes: 5 additions & 1 deletion src/service/primitive/ScriptAttributeTypeService.re
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ let convertFieldTypeToJsObjStr = type_ =>
)
}
)
);
);

let getTypeFromJsObj = [%bs.raw jsObj => {|
return jsObj.type;
|}];
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ let replaceScriptAttributeField = (fieldName, attributeFieldJsObj, attribute) =>
attributeFieldJsObj,
);

let _unsafeGetScriptAttributeField =
let unsafeGetScriptAttributeField =
(fieldName, attribute): ScriptAttributeType.scriptAttributeField =>
OperateScriptAttributeDataMainService.getScriptAttributeField(
fieldName,
Expand All @@ -46,7 +46,7 @@ let _addScriptAttributeField = (fieldName, attributeField, attribute) =>

let renameScriptAttributeField = (oldFieldName, newFieldName, attribute) => {
let attributeField =
_unsafeGetScriptAttributeField(oldFieldName, attribute);
unsafeGetScriptAttributeField(oldFieldName, attribute);

OperateScriptAttributeDataMainService.removeScriptAttributeField(
oldFieldName,
Expand All @@ -56,7 +56,7 @@ let renameScriptAttributeField = (oldFieldName, newFieldName, attribute) => {
};

let unsafeGetScriptAttributeFieldType = (fieldName, attribute) =>
_unsafeGetScriptAttributeField(fieldName, attribute).type_;
unsafeGetScriptAttributeField(fieldName, attribute).type_;

let unsafeGetScriptAttributeFieldDefaultValue = (fieldName, attribute) =>
OperateScriptAttributeDataMainService.unsafeGetScriptAttributeFieldDefaultValue(
Expand Down
30 changes: 30 additions & 0 deletions src/service/state/engine/script/ScriptEngineService.re
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,36 @@ let replaceScriptAttribute =
targetScriptAttribute,
);

let updateAttributeInAllScriptComponents =
(
attributeName,
newAttribute,
({scriptRecord}: StateDataMainType.state) as engineState,
) => {
let {scriptAttributeMap}: StateDataMainType.scriptRecord = scriptRecord;

let scriptAttributeMap =
scriptAttributeMap
|> WonderCommonlib.ImmutableSparseMapService.mapValid((. attributeMap) =>
attributeMap
|> WonderCommonlib.ImmutableHashMapService.has(attributeName) ?
attributeMap
|> WonderCommonlib.ImmutableHashMapService.set(
attributeName,
newAttribute,
) :
attributeMap
);

{
...engineState,
scriptRecord: {
...scriptRecord,
scriptAttributeMap,
},
};
};

let unsafeGetScriptAttributeEntries = ScriptAPI.unsafeGetScriptAttributeEntries;

let unsafeGetScriptAttribute = ScriptAPI.unsafeGetScriptAttribute;
Expand Down
Loading

0 comments on commit 32d3785

Please sign in to comment.