Skip to main content

概况

您可以基于Typescript语言来写逻辑代码。目前支持两种写逻辑代码的脚本:Input和Action,其中前者为UI Control提供数据,后者处理UI Control的事件

在逻辑代码中,您可以通过api.nullable.getExn(api.getPackageService<editorWholeService>(meta3dState, "meta3d-editor-whole-protocol"))来获得editor-whole这个Package,从而调用它的API来操作场景的

下面,我们分别介绍Input和Action:

Input

下面是Input的默认代码,加上了注释:

import { api } from "meta3d-type"
import { service as editorWholeService } from "meta3d-editor-whole-protocol/src/service/ServiceType"

    export let getContribute = (api:api) => {
      return {
        //这里设置Input的名称
        inputName: "Input1",
        //该函数将返回数据给选择了该Input的UI Control
        func: (meta3dState) =>{
            return Promise.resolve(null)
        }
      }
    }

这里给出了UI Control需要的数据类型(type),从而您可以在func中返回该类型的数据:Promise<type>

Action

下面是Action的默认代码,加上了注释:

import { api } from "meta3d-type"
import { service as editorWholeService } from "meta3d-editor-whole-protocol/src/service/ServiceType"

  export let getContribute = (api:api) => {
    return {
      //这里设置Action的名称
      actionName: "Action1",
      init: (meta3dState) => {
        //此处首先获得了editor-whole这个Package,然后调用它的API获得了eventSourcing这个子Package,它实现了事件溯源
        let eventSourcingService = api.getPackageService<editorWholeService>(meta3dState, "meta3d-editor-whole-protocol").event(meta3dState).eventSourcing(meta3dState)

        return new Promise((resolve, reject) => {
          //eventSourcingService.on函数为事件注册了两个函数:forward和backward,其中前者处理了事件,后者用于Redo-Undo时执行回退逻辑

          //另外,forward函数的形参可获得handler函数中传入的事件附带数据(inputData)。
          //举例来说,如果inputData为:[1,2],则forward函数的形参为:(meta3dState, data1, data2),其中data1=1,data2=2
          resolve(eventSourcingService.on(meta3dState, "TODO 此处应该填入事件名", 0, 
          //这是forward函数
          (meta3dState) => {
            //执行一些事件处理的逻辑

            return Promise.resolve(meta3dState)
          }, 
          //这是backward函数
          (meta3dState) => {
            //执行一些回退的逻辑

            return Promise.resolve(meta3dState)
          }))
        })
      },
      handler: (meta3dState, uiData) => {
        return new Promise((resolve, reject) => {
          let eventSourcingService = api.getPackageService<editorWholeService>(meta3dState, "meta3d-editor-whole-protocol").event(meta3dState).eventSourcing(meta3dState)

          resolve(eventSourcingService.addEvent(meta3dState, {
            name: "TODO 此处应该填入事件名",
            //此处应该填入事件附带的数据
            inputData: []
          }))
        })
      },
      //初始化Action自带的数据
      createState: () => {
        return null
      }
    }
  }