global gdostring, gactivecode, gcodesprites, gStagewidth, gStageheight
global gJoystick, gCurrentjoy, gjoycode1, gjoycode2, gdivider, gthemonitor, audio, doaudio


on startmovie
  
  
  the keyDownScript = "keyey"
  
  gStagewidth = (the stageright - the stageleft )
  gStageheight = (the stagebottom - the stagetop )
  
  sprite(3).moveablesprite = 1
  sprite(2).moveablesprite = 1
  gcodesprites = [10,11,12,13,14,15]
  
  resetcodesprites 
  
  
  grabdesk
  member("bitmap2").image = member("blackbox").image
  
  -- joystick code
  
  gJoystick = xtra("joystick").new()
  gJoystick.useJoy(1) -- use the first joystick
  gCurrentjoy = 1
  -- end joystick code
  
  gjoycode1 = " "
  gjoycode2 = " "
  
  
  set gdivider to 64
  set gthemonitor to new( xtra "GetSoundInLevel")
  doaudio = 1
  
  
end

--------------------------------------------
--------------------------------------------
on resetcodesprites
  repeat with each in gcodesprites
    -- sprite(each).member.text = "hello world " & each
    sprite(each).rotation = 0
    sprite(each).skew = 0
    sendSprite(each, #reposition)
    
    sprite(each).editable = 1
  end repeat
  
  repeat with each in [2,3,19]
    sprite(each).rotation = 0
    sprite(each).skew = 0
    sendSprite(each, #reposition)
  end repeat
  
  
  
end



--------------------------------------------
--------------------------------------------
on dostuff
  readcode
  if the shiftdown then 
    repeat with i = 2 to  3
      sprite(i).ink = random(30)
    end repeat
  end if
  
  dojoy
  
  if doaudio = 1 then
    audio = (checkLevel()/gdivider)
  end if
  
  
  
end
--------------------------------------------
--------------------------------------------




on readcode
  
  repeat with i = 1 to 6
    codemember = string ("code" & i)
    -- evalstring = line 1 of member(codemember).text
    evalstring = member(codemember).text
    if the last char of evalstring = ";" then
      if evalstring contains "grabscreen" then 
        -- i meant to type grabstage
        evalstring = searchandreplace (evalstring, "grabscreen", "grabstage")
        member(codemember).text = searchandreplace (member(codemember).text, "grabscreen", "grabstage")
      end if
      
      if evalstring contains "thisthing" then
        whichspritenum = i + 9
        
        whichsprite = "sprite(" & whichspritenum & ")"
        
        evalstring = searchandreplace (evalstring, "thisthing", whichsprite)
        member(codemember).text = searchandreplace (member(codemember).text, "thisthing", whichsprite)
      end if
      
      
      if evalstring contains "define" then
        evalstring = searchandreplace (evalstring, "define", "on")
        member(codemember).text = searchandreplace (evalstring, "define", "on")
        
        mystring = evalstring
        delete the last char of mystring
        member("dumbscripts").scriptText =  member("dumbscripts").scriptText & RETURN & mystring & RETURN
        
      end if
      
      
      gactivecode = codemember
      
      
      gdostring = evalstring
      delete the last char of gDostring
      do gDostring 
    end if
  end repeat
end

--------------------------------------------
--------------------------------------------

on biggify whichsprite, howmuch
  if voidp (howmuch) then howmuch = 1
  if voidp (whichsprite) then whichsprite = the clickon
  sprite(whichsprite).width = (sprite(whichsprite).width + howmuch) 
  sprite(whichsprite).height = (sprite(whichsprite).width + howmuch) 
  if sprite(whichsprite).width > gStagewidth * 1.5 or sprite(whichsprite).height > gStageheight * 1.5 then
    sprite(whichsprite).width = 10 
    sprite(whichsprite).height = 10
  end if
  
  
end
--------------------------------------------
--------------------------------------------
on bigify whichsprite, howmuch
  biggify whichsprite, howmuch
end

--------------------------------------------
--------------------------------------------
on smallify whichsprite, howmuch
  
  if voidp (howmuch) then howmuch = 1
  if voidp (whichsprite) then whichsprite = the clickon
  sprite(whichsprite).width = (sprite(whichsprite).width - howmuch) 
  sprite(whichsprite).height = (sprite(whichsprite).width - howmuch) 
  if sprite(whichsprite).width < 0 or sprite(whichsprite).height < 0 then
    sprite(whichsprite).width = gStageWidth * 2 
    sprite(whichsprite).height = gStageheight * 2 
  end if
  
  
end

--------------------------------------------
--------------------------------------------
on grabstage whatsprite
  -- easier than remember this lingo!
  sprite(whatsprite).member.image.copyPixels(the stage.image, sprite(whatsprite).member.image.rect, the stage.image.rect)
end

--------------------------------------------
--------------------------------------------

on grabdesk2
  screengrabtomember(member 13, castlib 1)
end
--------------------------------------------
--------------------------------------------
on grabdesk
  -- grabscreen("bitmap1", member 2 of castlib 1)
  screengrabtomember(member 2, castlib 1)
end

--------------------------------------------
--------------------------------------------
on rotify whatsprite, whatspeed
  if voidp (whatspeed) then whatspeed = 1
  if voidp (whatsprite) then whatsprite = the clickon
  sprite(whatsprite).rotation = sprite(whatsprite).rotation + whatspeed
end
--------------------------------------------
--------------------------------------------
on skewify whatsprite, whatspeed
  if voidp (whatspeed) then whatspeed = 1
  if voidp (whatsprite) then whatsprite = the clickon
  sprite(whatsprite).skew = sprite(whatsprite).skew + whatspeed
end
--------------------------------------------
--------------------------------------------
on colorify whichsprite, r, g, b
  if sprite(whichsprite).member.type = #bitmap then
    sprite(whichSprite).color = rgb(r, g, b)
  else if sprite(whichsprite).member.type = #text then
    sprite(whichSprite).member.color = rgb(r, g, b)
  end if
end

--------------------------------------------
--------------------------------------------
on colorall startsp, endsp
  if voidp(startsp) then startsp = gcodesprites[1]
  if voidp(endsp) then endsp = gcodesprites[gcodesprites.count]
  repeat with each = startsp to endsp
    
    colorify each random(255), random(255), random(255)
  end repeat
end

--------------------------------------------
--------------------------------------------
on colorallbg startsp, endsp
  if voidp(startsp) then startsp = gcodesprites[1]
  if voidp(endsp) then endsp = gcodesprites[gcodesprites.count]
  repeat with i = startsp to endsp
    sprite(i).bgcolor = rgb(random(255), random(255), random(255))
  end repeat
end

--------------------------------------------
--------------------------------------------
on colorbgall startsp, endsp
  colorallbg startsp, endsp
end

--------------------------------------------
--------------------------------------------
on colorbg whichsprite
  if voidp(whichsprite) then 
    if sprite(the clickon).member.type = #text then
      whichsprite = the clickon
    else 
      exit
    end if
  end if
  
  
  sprite(whichsprite).bgcolor = rgb(random(255), random(255), random(255))
end


--------------------------------------------
--------------------------------------------
on whatsmynumber
  if sprite(the clickon).member.text contains "whatsmynumber" then
    sprite(the clickon).member.text = "i'm # " & string(the clickon)
  end if
end


--------------------------------------------
--------------------------------------------

on keyey
  
  
  
  case the keycode of
    2: -- d
      if the controldown = true then
        
        resetcodesprites
        stopevent
      end if
      
    30: -- ]
      
      joytake 1
      stopevent
      
      
    42: -- \

      joytake 2
      stopevent
      
    50: -- ~
      resetcodesprites
      stopevent
      
    44: -- /
      panic
      stopevent
      
  end case
  
  
  
  
  if the key = TAB then
    
    joytake 1
    stopevent
    
    --  else if the key = SPACE then
    --    joytake 2
  end if
  
  
  
  case the keycode of
    33: -- [
      
      
      if gjoystick.numberOfJoy() > 1 then
        if gCurrentJoy = 1 then
          gCurrentJoy = 2   
        else 
          gCurrentJoy = 1
        end if
        gjoystick.useJoy(gCurrentJoy)
      end if
      
      stopevent
      
      
  end case
  
end
--------------------------------------------
--------------------------------------------
on fontify asize, asprite
  if voidp(asize) then asize = 24
  if voidp(asprite) then asprite = the clickon
  member(sprite(asprite).member).fontsize = asize
end


--------------------------------------------
--------------------------------------------

on letitallhangout
  repeat with each in gcodesprites
    colorallbg 
    if each mod 2 = 0 then
      rotify each 10
      skewify each 10
      bigify each
    else 
      rotify each -10
      skewify each 20
      smallify each
    end if
  end repeat
end


--------------------------------------------
--------------------------------------------
on panic
  resetcodesprites
  fillcode
  
  
end
--------------------------------------------
-------------------------------------------
on fillcode
  repeat with each in gCodesprites
    sendSprite(each, #resetcode)
    sendSprite(each, #resetfontsize)
  end repeat
end


--------------------------------------------
--------------------------------------------
on SearchAndReplace input, stringToFind, stringToInsert
  output = ""
  findLen = stringToFind.length - 1
  repeat while input contains stringToFind
    currOffset = offset(stringToFind, input)
    output = output & input.char [1..currOffset]
    delete the last char of output
    output = output & stringToInsert
    delete input.char [1.. (currOffset + findLen)] 
  end repeat
  set output = output & input
  return output
end



on screenGrabToMember whatMember, whatCastlib   
  baCopyText("a") 
  pasteclipboardinto member(whatmember,whatcastlib) 
  baSendKeys( "@{PRTSC}" ) 
  baWaitForWindow( baWinHandle() , "active" , 0 )   
  --repeat while member(whatMember,whatCastLib).type <> #bitmap 
  pasteclipboardinto member(whatmember,whatcastlib) 
  --end repeat 
  baCopyText("a") 
end  


--------------------------------
--------------------------------
on stopMovie
  
  set gthemonitor to 0
  gscreaming = 0
  if the  runmode <> "Author" then
    resetwins
  end if
  
  closexlib
end


--------------------------------
--------------------------------

on checkLevel
  return GetLevel( gtheMonitor )
end
--------------------------------
--------------------------------


