global gJoystick, gCurrentJoy, gLastcolorflicker, gLastrotflicker, gCodesprites
global gjoycode1, gjoycode2


on dojoy
  -- joystick button code
  
  inks = [0,1,2,37,38,39]
  
  -- move to startmovie
  if voidp(gLastcolorflicker) then gLastcolorflicker = the ticks
  if voidp(gLastrotflicker) then gLastrotflicker = the ticks
  
  
  if gjoystick.numberOfJoy() >= 1 then
    
    if gCurrentJoy = 2 then
      if gJoystick.xpos() < 10  then
        
        sprite(2).rotation = sprite(2).rotation + 4
        sprite(3).rotation = sprite(3).rotation - 4
        repeat with each in gcodesprites
          if each mod 2 = 0 then
            sprite(each).rotation = sprite(each).rotation + 4
          else
            sprite(each).rotation = sprite(each).rotation - 4
          end if
        end repeat
        
      else if gJoystick.xpos() > 65000 then
        sprite(2).rotation = sprite(2).rotation - 4
        sprite(3).rotation = sprite(3).rotation + 4
        
        
        
        repeat with each in gcodesprites
          if each mod 2 = 0 then
            sprite(each).rotation = sprite(each).rotation - 4
          else
            sprite(each).rotation = sprite(each).rotation + 4
          end if
        end repeat
      end if
      
    else
      if gJoystick.xpos() < 20000 then 
        
        if gjoystick.xpos() < 10 then 
          factor = 300 
        else 
          factor = 1000
        end if
        
        sprite(2).rotation = sprite(2).rotation + (20000 - gJoystick.xpos())/factor
        sprite(3).rotation = sprite(3).rotation - (20000 - gJoystick.xpos())/factor
        
        
        repeat with each in gcodesprites
          if each mod 2 = 0 then
            sprite(each).rotation = sprite(each).rotation + (20000 - gJoystick.xpos())/factor
          else
            sprite(each).rotation = sprite(each).rotation - (20000 - gJoystick.xpos())/factor
          end if
        end repeat
        
        
        
      else if gJoystick.xpos() > 35000 then
        if gJoystick.xpos() > 65500 then
          factor = 400
        else
          factor = 1000
        end if 
        
        
        sprite(2).rotation = sprite(2).rotation - ( gJoystick.xpos() - 35000 )/factor 
        sprite(3).rotation = sprite(3).rotation + (gJoystick.xpos() - 35000)/factor
        
        
        repeat with each in gcodesprites
          if each mod 2 = 0 then
            sprite(each).rotation = sprite(each).rotation - (20000 - gJoystick.xpos())/factor
          else
            sprite(each).rotation = sprite(each).rotation + (20000 - gJoystick.xpos())/factor
          end if
        end repeat
        
        
      end if
      
      
      if gJoystick.zpos() > 40000 then
        sprite(2).ink= getAt(inks, random(count(inks)))
        sprite(3).ink= getAt(inks, random(count(inks)))
      end if
      
    end if -- end if gCurrentJoy
    
    
    
    
    if gJoystick.button(9) then
      
      if voidp (glastcolorflicker) then 
        gLastcolorflicker = the ticks - 32
      end if
      
      if the ticks > gLastcolorflicker + 15 then
        repeat with each in gCodesprites
          
          sprite(each).color = paletteIndex (random(253) + 1)
          
        end repeat
        
        gLastcolorflicker = the ticks
      end if
      
    end if
    
    
    
    if gJoystick.button(10) then
      -- intentional oddball rotation stuff.
      if voidp (gLastrotflicker) then gLastrotflicker = the ticks - 32
      if the ticks > gLastrotflicker + 4 then
        repeat with each in gcodesprites, [2,3]
          if each mod 2 = 0 then 
            adder = 5
          else 
            adder = -5
          end if
          
          -- sprite(each).rotation = sprite(each).rotation + 30 + abs(sprite(each).width/5)
          if sprite(each).rotation > 0 and sprite(each).rotation < 180 then
            sprite(each).rotation = sprite(each).rotation + adder
          else 
            sprite(each).rotation = sprite(each).rotation - adder
          end if
          
        end repeat
        gLastrotflicker = the ticks
        
      end if
      
    end if
    
    
    
    if (gJoystick.button(3)) then
      
      
      shrinkfactor = 5
      if sprite(2).width > 0 and sprite(3).width > 0 then
        sprite(2).width = sprite(2).width - shrinkfactor 
        sprite(2).height = sprite(2).height - shrinkfactor 
        sprite(3).height = sprite(3).width - shrinkfactor 
        sprite(3).width = sprite(3).width - shrinkfactor
      end if 
    end if
    
  end if
  
  
  
  if (gJoystick.button(4)) then
    if gCurrentJoy = 1 then
      -- intentional oddball rotation stuff.
      if voidp (gLastrotflicker) then gLastrotflicker = the ticks - 32
      
      if the ticks > gLastrotflicker + 4 then
        repeat with each in [21,22], gcodesprites
          if each mod 2 = 0 then 
            adder = 5
          else 
            adder = -5
          end if
          
          
          
          sprite(each).width = sprite(each).width + (random(80) - 40)
          sprite(each).height = sprite(each).height + (random(80) - 40)
          
          
          
          
        end repeat
        gLastrotflicker = the ticks
        
      end if
      
    else -- ddr pad
      -- ???
      shrinkfactor = 5
      if sprite(2).height< 2000 and sprite(3).height < 2000 then
        sprite(2).width = sprite(2).width + shrinkfactor
        sprite(2).height = sprite(2).height + shrinkfactor
        sprite(3).width = sprite(3).width + shrinkfactor
        sprite(3).height = sprite(3).height + shrinkfactor
      end if
      
      repeat with each in gcodesprites
        if sprite(each).height < 2000 then
          sprite(each).width = sprite(each).width + shrinkfactor
          sprite(each).height = sprite(each).height + shrinkfactor
        end if
      end repeat
      
      
    end if
    
    
  end if
  
  
  
  
  if gJoystick.button(7) or gJoystick.ypos() > 65000 then
    sprite(2).rotation = 0 
    sprite(3).rotation = 90
  end if
  
  if gJoystick.button(8) or gJoystick.ypos() < 100 then
    sprite(2).rotation = 90
    sprite(3).rotation = 0 
  end if
  
  
  if gCurrentJoy = 2 then
    -- shrinking/growing need to happen slower with the DDR pad.
    shrinkfactor = 5
  else
    shrinkfactor = 10
  end if
  
  
  
  if gJoystick.button(5) then
    
    if sprite(2).width > 0 and sprite(3).width > 0 then
      sprite(2).width = sprite(2).width - shrinkfactor 
      sprite(2).height = sprite(2).height - shrinkfactor 
      sprite(3).height = sprite(3).height - shrinkfactor 
      sprite(3).width = sprite(3).width - shrinkfactor
      
      repeat with each in gcodesprites
        if sprite(each).width > 0 then
          sprite(each).width = sprite(each).width - shrinkfactor
          sprite(each).height = sprite(each).height - shrinkfactor
        end if
        
        
      end repeat
      
    end if
    
    
  end if
  
  if gJoystick.button(6) then
    
    if sprite(2).height< 2000 and sprite(2).height < 2000 then
      sprite(2).width = sprite(2).width + shrinkfactor
      sprite(2).height = sprite(2).height + shrinkfactor
      sprite(3).width = sprite(3).width + shrinkfactor
      sprite(3).height = sprite(3).height + shrinkfactor
    end if
    
    repeat with each in gcodesprites
      if sprite(each).width < 2000 then
        sprite(each).width = sprite(each).width + shrinkfactor
        sprite(each).height = sprite(each).height+ shrinkfactor
      end if
      
      
      
    end repeat
    
    
  end if
  
  
  if gjoystick.button(1) then
    do gjoycode1
    if sprite(the clickon).member.type = #text then
      sprite(the clickon).member.text = gjoycode1
    end if
    
  end if
  
  
  if gjoystick.button(2) then
    do gjoycode2
      if sprite(the clickon).member.type = #text then
      sprite(the clickon).member.text = gjoycode2
    end if
    
  end if
  
  
  
  
  
end dojoy


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

on joytake whatbutton
  --   do "gjoycode" & whatbutton & " = the text of member " & sprite(the clickon).member
  if whatbutton = 1 then
    gjoycode1 = the text of member sprite(the clickon).member
    if the last char of gjoycode1 = ";" then delete the last char of gjoycode1
    
    put "gjoycode1 = " & gjoycode1
    
  else if whatbutton = 2 then
    gjoycode2 = the text of member sprite(the clickon).member
    if the last char of gjoycode2 = ";" then delete the last char of gjoycode2    
    put "gjoycode2 = " & gjoycode2
  else
    put "no button to joytake!"
    
  end if
  
  
  
end



