-- aleXtrasMovieScript
---------------------------------------------------------------------
-- CREATED:
-- 2005
--
-- PROPERTIES:
--!memberProperties: [#name: "aleXtrasMovieScript", #scripttype: #movie, #scriptSyntax: #lingo, #comments:"~/Documents/Scripts/lingo/aleXtrasMovieScript.ls"]
--
-- DESCRIPTION:
--             -- LDM Xtras Moviescript -- c05 Alex da Franca -- alex@farbflash.de
-- store instances of scripts and bitmaps in a 'global' list (-> property pAleXtras)
-- and provide access to them for all scripts
--
-- REQUIRES:
--            script "commonmoviescript"
--
-- USAGE:
--            put it in one of your castlib, DON'T RENAME THIS MEMBER !!
--            use xscr(<name of script as symbol>) to get a reference to the script from
--            every script of the movie. It will return an instance of the script
--
-- PARAMS:
-- -
--
-- RETURNS:
-- -
--
-- WARNINGS:
-- -
--
-- TODO:
--              -
-----------------------------------

property pAleXtras

on mGetAleXtras
  
  alextras = (script "aleXtrasMovieScript").pAleXtras
  if not(objectP(alextras)) then
    
    cms = member("commonMovieScript")
    if ilk(cms) <> #member then
      alert "Script commonMovieScript missing, can't proceed"
      halt
    end if
    if cms.type <> #script then
      alert "Script commonMovieScript missing, can't proceed"
      halt
    end if
    
    if voidP(cms.script) then -- strangely enough this happened suddenly, dunno why...
      -- until now it happened only in authoring with the stopped movie
      -- therefore I just exit here without alert
      halt
    end if
    
    alextras = new(cms.script)
    if ilk(alextras) <> #instance then
      alert "Script" & QUOTE & "commonMovieScript" & QUOTE && "is missing. This movie can't proceed!"
      halt
    end if
    (script "aleXtrasMovieScript").pAleXtras = alextras
    
  end if
  return alextras
  -----------------------------
  
  alextras = (script "aleXtrasMovieScript").pAleXtras
  if voidP(alextras) then
    alextras = [:]
    (script "aleXtrasMovieScript").pAleXtras = alextras
  end if
  return alextras
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on xscr scrName
  -----------------------------------
  --         CREATED: 11.02.2008
  --         ACTION: Description
  --         INPUT: -
  --         RETURNS: -
  --         CALLER: -
  --         DEBUG: -
  --         TODO: -
  -----------------------------------
  
  return mGetXScript(scrName)
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mGetXScript scrName
  -----------------------------------
  --         CREATED: 11.02.2008
  --         ACTION: Description
  --         INPUT: -
  --         RETURNS: -
  --         CALLER: -
  --         DEBUG: -
  --         TODO: -
  -----------------------------------
  
  alextras = mGetAleXtras()
  scrName = string(scrName)
  if length(scrName) < 1 then return alextras
  if scrName = "commonMovieScript" then return alextras
  
  str_scrName = alextras.mSymb2String(scrName)
  inst = alextras.mGetInstance(str_scrName, 1)
  if ilk(inst) <> #instance then
    if the runmode contains "aut" then
      put "Script" && QUOTE & str_scrName & QUOTE && "is missing! This may cause unwanted behavior"
    end if
      inst = []
    end if
  if ilk(inst) = #instance then
    if inst.handler(#new) = 1 then inst.new()
  end if
  return inst
  ----------------------
  
  
  scrName = alextras.mSymbolify(scrName)
  if ilk(scrName) <> #symbol then scrName = #commonmoviescript
  scri = (mGetAleXtras()).getaprop(#scripts)
  if ilk(scri) <> #proplist then
    mLoadScripts
    scri = (mGetAleXtras()).getaprop(#scripts)
  end if
  return scri.getaprop(scrName)
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
-- if you have a stopmovie handler in a moviescript before this one, you should call 'mCallDestroy' yourself
-- if this script here is first, be aware, that your stopmovie handler might not get called

on stopmovie
  mCallDestroy
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mCallDestroy
  call(#mCallDestroy, mGetXScript())
  -- call(#mDestroy, the actorlist)
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
-- load local scripts (instead of the ones from the LDM -> authoring this movie)

on mLoadScripts cls
  -----------------------------------
  --         CREATED: 11.02.2008
  --         ACTION: Description
  --         INPUT: -
  --         RETURNS: -
  --         CALLER: -
  --         DEBUG: -
  --         TODO: -
  -----------------------------------
  
  exit
  
  -- deprecated handler
  -- only here for bakcward compatibility
  -- the system is now built to load scripts only on demand
  -- exception is mLoadScriptsFromLDM() which needs to grab all scripts
  -- from the ldm and uses them.
  -- be aware of the fact, that rawnew() is used on these scripts, so the ones of the LDM
  -- won't have their new() handler called. I take care about that for you, though...
  
end


-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mLoadXScript whichName, dontCallDestroyHandler
  -- overload a script, e.g. after changing at during the movie runs
  
  if not(symbolP(whichName)) then whichName = symbol(whichName)
  if voidP(dontCallDestroyHandler) then dontCallDestroyHandler = 1
  xscr().mDeleteInstance(whichName, dontCallDestroyHandler)
  
  return xscr(whichName)
  
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mLoadScriptsFromLDM whichSprite
  
  includes = [:] 
  
  tell sprite(whichSprite)
    cl = the number of castlibs
    repeat with n = 1 to cl
      num = the number of members of castlib cl
      repeat with m = 1 to num
        mem = member(m,n)
        if length(mem.name) > 0 then
          if mem.type = #script then
            includes.setaProp(symbol(mem.name), rawNew(mem.script))
          end if
        end if
      end repeat
    end repeat
  end tell
  
  cms = includes.getaprop(#commonMovieScript)
  if not(objectP(cms)) then
    cms = member("commonMovieScript")
    if ilk(cms) <> #member then
      alert "Script commonMovieScript missing, can't proceed"
      halt
    end if
    if cms.type <> #script then
      alert "Script commonMovieScript missing, can't proceed"
      halt
    end if
    cms = rawnew(cms.script)
  end if
  
  (script "aleXtrasMovieScript").pAleXtras = cms
  
  globs = cms.mGetGlobalList()
  incl = globs[#gParentScriptInstances]
  if not(objectP(incl)) then
    incl = [:]
    globs[#gParentScriptInstances] = incl
  end if
  
  repeat with n = count(incl) down to 1
    includes.setaprop(incl.getPropAt(n), incl[n])
  end repeat
  includes.deleteOne(#commonMovieScript)
    globs[#gParentScriptInstances] = includes
  
  exit
  ------------------------------ deprecated:
  
  mediaList = mGetAleXtras()
  
  theScripts = mediaList.getaprop(#scripts)
  if ilk(theScripts) <> #proplist then
    theScripts = [:]
    mediaList.setaprop(#scripts, theScripts)
  end if
  
  theBitmaps = mediaList.getaprop(#bitmaps)
  if ilk(theBitmaps) <> #proplist then
    theBitmaps = [:]
    mediaList.setaprop(#bitmaps, theBitmaps)
  end if
  
  tell sprite(whichSprite)
    cl = the number of castlibs
    repeat with n = 1 to cl
      num = the number of members of castlib cl
      repeat with m = 1 to num
        mem = member(m,n)
        if length(mem.name) > 0 then
          if mem.type = #script then
            theScripts.setaProp(symbol(mem.name), mem.script)
          else if mem.type = #bitmap then
            theBitmaps.setaProp(symbol(mem.name), mem.image.duplicate())
          end if
        end if
      end repeat
    end repeat
  end tell
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mListAleXtras
  
  put "This handler used to put the alextras scripts only, now it puts all singletons instead"
  
  globs = xscr().mGetGlobalList()
  incl = globs[#gParentScriptInstances]
  if not(objectP(incl)) then
    put "No scriptinstances stored"
  exit
  end if
  
  anz = count(incl)
  repeat with n = 1 to anz
    put RETURN & "scr = xscr().mGetInstance(" & QUOTE & incl.getPropAt(n) & QUOTE & ")" & RETURN
  end repeat
  put RETURN & "put scr.handlers()" & RETURN
  put RETURN & "put call(#interface, [scr])" & RETURN
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mListScriptMemberNames
  
  cl = the activecastlib
  sel = the selection of castlib cl
  anz = sel.count
  str = ""
  repeat with n = 1 to anz
    repeat with m = sel[n][1] to sel[n][2]
      memref = member(m, cl)
      if memref.type = #script then
        put "Membername:" && memref.name & TAB & "("&memref.comments&")" & Return after str
      end if
    end repeat
  end repeat
  delete the last char of str
  return str
  
end


-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mUpdateSelectedAlexScripts rootPath
  
  rootPath = string(rootPath)
  
  if length(rootPath) < 1 then
    rootPath = baGetFolder("", "Please select the base folder with the scripts", 1, "Select scripts folder", -1, -1)
    if length(rootPath) < 1 then exit
  end if
  
  fio = new(xtra "fileio")
  
  cl = the activecastlib
  sel = the selection of castlib cl
  
  
  mp = the moviepath
  if length(mp) < 1 then
    if the runmode contains "plug" then
      mp = "/"
    else
      mp = the applicationpath
    end if
  end if
  delim = the last char of mp
  
  the itemdelimiter = "/"
  
  
  ident = "/Documents/"
  len = length(ident)
  
  repeat with sub in sel
    repeat with mem = sub[1] to sub[2]
      memref = member(mem,cl)	
      if memref.type = #script then
        memname = memref.name
        comm = memref.comments
        if comm.length then
          
          offs = offset(ident, comm)
          if offs > 0 then
            
            delete char 1 to (offs + len - 1) of comm          
            p = rootPath
            anzit = comm.item.count
            repeat with rr = 1 to anzit
              put comm.item[rr] & delim after p
            end repeat
            delete the last char of comm
            
            fio.openFile(p, 1)
            if fio.status() = 0 then
              st = fio.readFile()
              fio.closeFile()
              memref.scripttext = st
              put "Updated member" && memref.name && "(" & memref & ")"
            else
              baMsgBox("Unable to open " & p & ". Proceed with next file?", "File not found", "YesNo", "Exclamation", 2)
            end if
            
          end if
          
        end if
      end if
    end repeat
  end repeat
  
  fio = 0
end

