Audio Editor

Audio Editor

Read

  • editor.audio.audioTable receives the string array of audio files via nanostore

  • editor/parts/controller/selector/selectAudio receives the onClick event when user selects an audio entry in audioTable

    local instance = require("editor.audio.index").controller:command()
    return instance

    the controller:command() from editor.controller.index decodes the json file of a selected audio and setValue to the UI table which has been initiated in editor.audio.index

    conrolProps onCompletebox buttons are loaded in editor.audio.index and they are initiated with the controller

    • editor.audio.index

      local selectbox      = require(parent .. "audioTable")
      local controlProps    = require(parent.."controlProps")
      local onCompletebox = require(root..".parts.onCompletebox")
      -- this set editor.audio.save, cacnel
      local buttons       = require(parent.."buttons")
      --
      local controller = require("editor.controller.index").new("audio")
      --
      local M = require(root.."baseClassEditor").new(model, controller)
      
      function M:init(UI)
        self.UI = UI
        self.group = display.newGroup()
        UI.editor.classEditorGroup = self.group
        --
        selectbox:init(UI)
        controlProps:init(UI, self.x + self.width*1.5, self.y,  self.width, self.height)
        controlProps.model = model.props
        controlProps.UI = UI
        --
        onCompletebox:init(UI)
        buttons:init(UI)
        -- --
        controller:init{
          selectbox      = selectbox,
          controlProps    = controlProps,
          onCompletebox = onCompletebox,
          buttons       = buttons
        }
        controller.view = self
        --
        UI.useClassEditorProps = function() return controller:useClassEditorProps() end
        --
      end
    • editor.controller.index

      this is the base class of the controller of editor.audio. and this command() is called in editor/parts/controller/selector/selectAudio mentioned above.

      function M:command()
        local instance = require("commands.kwik.baseCommand").new(
        function (params)
          local UI    = params.UI
          local name =  params[params.class] or ""
          local decoded = util.decode(params) -- this reads models/xx.json
          --
          print("From selectors")
          self.controlProps:didHide(UI)
          self.controlProps:destroy(UI)
          self.controlProps:init(UI)
          self.controlProps:setValue(decoded)
          self.controlProps.isNew = params.isNew
          --
          self.controlProps:create(UI)
          self.controlProps:didShow(UI)
      
          --
          -- self:show()
          self.controlProps:show()
          self.onCompletebox:show()
          self.buttons:show()
      
          --
          UI.editor.editPropsLabel = name
          --
          UI.editor.sceneGroup:dispatchEvent{name="labelStore",
            currentBook= UI.editor.currentBook,
            currentPage= UI.page,
            currentLayer = name}
      
        end)
        return instance
      end

Save

  • editor.audio.index

    local selectbox      = require(parent .. "audioTable")
    local controlProps    = require(parent.."controlProps")
    local onCompletebox = require(root..".parts.onCompletebox")
    local buttons       = require(parent.."buttons")

    overwrites contoroller’s render and save functions

    function controller:render(book, page, type, name, model)
      local dst = "App/"..book.."/"..page .."/components/audios/"..type.."/"..name ..".lua"
      local tmplt =  "editor/template/components/pageX/audios/audio.lua"
      util.mkdir("App", book, page, "components", "audios", type)
      util.saveLua(tmplt, dst, model)
      return dst
    end
    
    function controller:save(book, page, type, name, model)
      local dst = "App/"..book.."/models/"..page .."/audios/"..type.."/"..name..".json"
      util.mkdir("App", book, "models", page, "audios", type)
      util.saveJson(dst, model)
      return dst
    end
  • editor.audio.controller.save

    local name = ...
    local parent,root = parent_root(name)
    local util = require("editor.util")
    local json = require("json")
    
    local instance = require("commands.kwik.baseCommand").new(
      function (params)
        local UI    = params.UI
        local selectbox    = require(root.."audioTable")
        local controller   = require(root.."index").controller
        local controlProps = controller.controlProps
        local onCompletebox = controller.onCompletebox
        local selected      = selectbox.selection or {}
        local page = params.page or UI.page,
    
        local props = {
          name = selected.audio, -- UI.editor.currentLayer,
          class= "audio",
          subclass = selected.subclass or "short",
          controls = {},
          actionName = nil,
          -- the following vales come from read()
          --class = self.class,
          index = selected.index,
        }
        --
        local controls = controlProps:getValue()
        for i=1, #controls do
          -- props.subclass
          --
          local name = controls[i].name
          if name =="_type" then
            local t = controls[i].value or "short"
            if t:len() == 0 then
              t = "short"
            elseif t=="audioshort"then
              t = "short"
            elseif t=="audiolong" then
              t= "long"
            elseif t=="audiosync" then
              t= "sync"
            end
            props.subclass =  t
            name = "type"
          elseif name== "_file" then
            name = "filename"
          end
          props.controls[name] = controls[i].value
        end
        --
        -- props.name
        --
        if props.name == nil then -- NEW
          print("#NEW",props.controls.name)
          -- local t = util.split(props.controls.file or "", '.')
          -- props.name = t[1]
          props.name = props.controls.name
        end
        -- props.actionName
        props.actionName = onCompletebox.selectedTextLabel
        print("porps")
        for k, v in pairs(props) do print("", k, v) end
        --
        local updatedModel = util.createIndexModel(UI.scene.model)
        -- print(json.encode(updatedModel))
        local files = {}
        if params.isNew or selected.index == nil then
          local dst = updatedModel.components.audios[props.subclass] or {}
          dst[#dst + 1] = props.name
        else
          local dst = updatedModel.components.audios[props.subclass]
          dst[props.index] = props.name -- TBI for audio's name
        end
        --
        -- TODO check if name is not duplicated or not
        --
        -- index
        files[#files+1] = util.renderIndex(UI.editor.currentBook, page,updatedModel)
        files[#files+1] = util.saveIndex(UI.editor.currentBook, page, props.layer,props.class, updatedModel)
        -- save lua
        files[#files+1] = controller:render(UI.editor.currentBook, page, props.subclass, props.name, props.controls)
        -- save json
        files[#files+1] = controller:save(UI.editor.currentBook, page, props.subclass, props.name, props.controls)
        -- publish
        util.executePubish(files)
      end
    )
    --
    return instance