|
| 1 | +import { state as meta3dState, getContribute as getContributeMeta3D, api } from "meta3d-type" |
| 2 | +import { actionContribute, service as editorWholeService } from "meta3d-editor-whole-protocol/src/service/ServiceType" |
| 3 | +import { actionName, state, uiData } from "meta3d-action-mod-unit-upload-particle-instance-protocol" |
| 4 | +import { eventName, glbName, glbData, inputData } from "meta3d-action-mod-unit-upload-particle-instance-protocol/src/EventType" |
| 5 | +import { actionName as initActionName, state as initState } from "meta3d-action-mod-unit-init-protocol" |
| 6 | +import { nullable } from "meta3d-commonlib-ts/src/nullable" |
| 7 | +import { importFile } from "meta3d-file-ts-utils/src/ImportFileUtils" |
| 8 | +import { getLanguageTextData, getLanguageTextVariableData } from "meta3d-language-utils/src/Main" |
| 9 | +import { languageKey, languageVariableKey } from "meta3d-language-utils/src/Type" |
| 10 | + |
| 11 | +let _loadGLB = (api: api): Promise<[boolean, nullable<glbName>, nullable<glbData>]> => { |
| 12 | + return new Promise((resolve, reject) => { |
| 13 | + importFile((file: any, result: any) => { |
| 14 | + if (!file.name.includes(".glb")) { |
| 15 | + reject(new Error("文件后缀名应该是.glb")) |
| 16 | + } |
| 17 | + |
| 18 | + // resolve([true, api.nullable.return(file.name.slice(0, -3)), api.nullable.return(result as ArrayBuffer)]) |
| 19 | + resolve([true, api.nullable.return(file.name), api.nullable.return(result as ArrayBuffer)]) |
| 20 | + }, (event: Event, file: any) => { |
| 21 | + reject(new Error(`读取${file.name}错误`)) |
| 22 | + }, (loaded: number, total: number) => { |
| 23 | + // TODO show progress message |
| 24 | + console.log(`loading ${loaded / total} %`) |
| 25 | + }, () => { |
| 26 | + resolve([false, api.nullable.getEmpty(), api.nullable.getEmpty()]) |
| 27 | + }) |
| 28 | + }) |
| 29 | +} |
| 30 | + |
| 31 | +export let getContribute: getContributeMeta3D<actionContribute<uiData, state>> = (api) => { |
| 32 | + return { |
| 33 | + actionName: actionName, |
| 34 | + init: (meta3dState) => { |
| 35 | + let eventSourcingService = api.nullable.getExn(api.getPackageService<editorWholeService>(meta3dState, "meta3d-editor-whole-protocol")).event(meta3dState).eventSourcing(meta3dState) |
| 36 | + |
| 37 | + return new Promise((resolve, reject) => { |
| 38 | + resolve(eventSourcingService.on<inputData>(meta3dState, eventName, 0, (meta3dState, uiData, glbName, glbData, [selectedSkillObjectEmitterInstanceIndexFieldName]) => { |
| 39 | + let initState = api.action.getActionState<initState>(meta3dState, initActionName) |
| 40 | + |
| 41 | + if (glbData.byteLength > 0.5 * 1024 * 1024) { |
| 42 | + api.message.warn(getLanguageTextVariableData(api, meta3dState, initState.languageTextDataByVariable, languageVariableKey.LimitFileSize)(0.5)) |
| 43 | + return Promise.resolve(meta3dState) |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | + api.message.success(getLanguageTextData(api, meta3dState, initState.languageTextData, languageKey.Success)) |
| 48 | + |
| 49 | + |
| 50 | + let state = api.action.getActionState<state>(meta3dState, actionName) |
| 51 | + meta3dState = api.action.setActionState<state>(meta3dState, actionName, { |
| 52 | + ...state, |
| 53 | + instances: state.instances.set(selectedSkillObjectEmitterInstanceIndexFieldName, api.nullable.return([glbName, glbData])) |
| 54 | + }) |
| 55 | + |
| 56 | + meta3dState = api.action.setActionState<initState>(meta3dState, initActionName, { |
| 57 | + ...initState, |
| 58 | + [selectedSkillObjectEmitterInstanceIndexFieldName]: api.nullable.getEmpty() |
| 59 | + }) |
| 60 | + |
| 61 | + |
| 62 | + return Promise.resolve(meta3dState) |
| 63 | + }, (meta3dState) => { |
| 64 | + return Promise.resolve(meta3dState) |
| 65 | + })) |
| 66 | + }) |
| 67 | + }, |
| 68 | + handler: (meta3dState, uiData, actionParams) => { |
| 69 | + return _loadGLB(api).then(([isSuccess, glbName, glbData]) => { |
| 70 | + if (!isSuccess) { |
| 71 | + return meta3dState |
| 72 | + } |
| 73 | + |
| 74 | + return new Promise<meta3dState>((resolve, reject) => { |
| 75 | + let eventSourcingService = api.nullable.getExn(api.getPackageService<editorWholeService>(meta3dState, "meta3d-editor-whole-protocol")).event(meta3dState).eventSourcing(meta3dState) |
| 76 | + |
| 77 | + |
| 78 | + resolve(eventSourcingService.addEvent<inputData>(meta3dState, { |
| 79 | + name: eventName, |
| 80 | + isOnlyRead: true, |
| 81 | + inputData: [ |
| 82 | + uiData, |
| 83 | + glbName, |
| 84 | + glbData, |
| 85 | + actionParams |
| 86 | + ] |
| 87 | + })) |
| 88 | + }) |
| 89 | + }).catch((error) => { |
| 90 | + api.message.error(error) |
| 91 | + return meta3dState |
| 92 | + }) |
| 93 | + |
| 94 | + }, |
| 95 | + createState: () => { |
| 96 | + return { |
| 97 | + instances: api.immutable.createMap() |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments