--[[
///////////////////////////////////////////////////////////////////////////////
 'Pad input support routine'

 Copyright(c) 2005, ema.
///////////////////////////////////////////////////////////////////////////////
]]--

-- Hoshizora 2006.07.17: Added support for Home, Hold and Note buttons
Pad = {
  UseAnalog = string.find(System.currentDirectory(), "/cygdrive/") == nil
, cLu = 0
, cLd = 0
, cLl = 0
, cLr = 0
, cRu = 0
, cRd = 0
, cRl = 0
, cRr = 0
, cL1 = 0
, cR1 = 0
, cSt = 0
, cSl = 0
, cHm = 0
, cHd = 0
, cNo = 0
}

function Pad:GetRepeat(t)
  return t==1 or t>20
end

function Pad:nGetRepeat(t)
  if t==1 or t>20 then return 1 else return 0 end
end

function Pad:GetHrep()
  local v = Pad:nGetRepeat(self.cLr) - Pad:nGetRepeat(self.cLl)
  return v + math.ceil(self.Ax/20-0.5)
end

function Pad:GetVrep()
  local v = Pad:nGetRepeat(self.cLd) - Pad:nGetRepeat(self.cLu)
  return v + math.ceil(self.Ay/20-0.5)
end

function Pad:GetLh()
  if self.tLr then return  1 end
  if self.tLl then return -1 end
  return 0
end

function Pad:GetLv()
  if self.tLd then return  1 end
  if self.tLu then return -1 end
  return 0
end

function Pad:GetRh()
  if self.tRr then return  1 end
  if self.tRl then return -1 end
  return 0
end

function Pad:GetRv()
  if self.tRd then return  1 end
  if self.tRu then return -1 end
  return 0
end

function Pad:Update()
  local Push = Controls.read()
  local fl, fr, fx = false, false, false
  local function CalcDeadZone(x, d)
    if x >= d then
      return x - d
    elseif x >= -d then
      return 0
    else
      return x + d
    end
  end

  if self.UseAnalog then
    self.Ax = CalcDeadZone(Push:analogX(), 32)
    self.Ay = CalcDeadZone(Push:analogY(), 32)
    if self.Ax ~= 0 or self.Ay ~= 0 then fl = true end
  else
    self.Ax = 0
    self.Ay = 0
  end

  self.Lu = Push:up()
  self.Ld = Push:down()
  self.Ll = Push:left()
  self.Lr = Push:right()
  self.Ru = Push:triangle()
  self.Rd = Push:cross()
  self.Rl = Push:square()
  self.Rr = Push:circle()
  self.L1 = Push:l()
  self.R1 = Push:r()
  self.St = Push:start()
  self.Sl = Push:select()
	self.Hm = Push:home()
	self.Hd = Push:hold()
	self.No = Push:note()

  if self.Lu then
    fl = true
    self.rLu = false
    self.tLu = self.cLu == 0
    self.cLu = self.cLu + 1
  else
    self.rLu = self.cLu > 0
    self.tLu = false
    self.cLu = 0
  end
  if self.Ld then
    fl = true
    self.rLd = false
    self.tLd = self.cLd == 0
    self.cLd = self.cLd + 1
  else
    self.rLd = self.cLd > 0
    self.tLd = false
    self.cLd = 0
  end
  if self.Ll then
    fl = true
    self.rLl = false
    self.tLl = self.cLl == 0
    self.cLl = self.cLl + 1
  else
    self.rLl = self.cLl > 0
    self.tLl = false
    self.cLl = 0
  end
  if self.Lr then
    fl = true
    self.rLr = false
    self.tLr = self.cLr == 0
    self.cLr = self.cLr + 1
  else
    self.rLr = self.cLr > 0
    self.tLr = false
    self.cLr = 0
  end

  if self.Ru then
    fr = true
    self.rRu = false
    self.tRu = self.cRu == 0
    self.cRu = self.cRu + 1
  else
    self.rRu = self.cRu > 0
    self.tRu = false
    self.cRu = 0
  end
  if self.Rd then
    fr = true
    self.rRd = false
    self.tRd = self.cRd == 0
    self.cRd = self.cRd + 1
  else
    self.rRd = self.cRd > 0
    self.tRd = false
    self.cRd = 0
  end
  if self.Rl then
    fr = true
    self.rRl = false
    self.tRl = self.cRl == 0
    self.cRl = self.cRl + 1
  else
    self.rRl = self.cRl > 0
    self.tRl = false
    self.cRl = 0
  end
  if self.Rr then
    fr = true
    self.rRr = false
    self.tRr = self.cRr == 0
    self.cRr = self.cRr + 1
  else
    self.rRr = self.cRr > 0
    self.tRr = false
    self.cRr = 0
  end

  if self.L1 then
    fx = true
    self.rL1 = false
    self.tL1 = self.cL1 == 0
    self.cL1 = self.cL1 + 1
  else
    self.rL1 = self.cL1 > 0
    self.tL1 = false
    self.cL1 = 0
  end
  if self.R1 then
    fx = true
    self.rR1 = false
    self.tR1 = self.cR1 == 0
    self.cR1 = self.cR1 + 1
  else
    self.rR1 = self.cR1 > 0
    self.tR1 = false
    self.cR1 = 0
  end
  if self.St then
    fx = true
    self.rSt = false
    self.tSt = self.cSt == 0
    self.cSt = self.cSt + 1
  else
    self.rSt = self.cSt > 0
    self.tSt = false
    self.cSt = 0
  end
  if self.Sl then
    fx = true
    self.rSl = false
    self.tSl = self.cSl == 0
    self.cSl = self.cSl + 1
  else
    self.rSl = self.cSl > 0
    self.tSl = false
    self.cSl = 0
  end

  if self.Hm then
    fx = true
    self.rHm = false
    self.tHm = self.cHm == 0
    self.cHm = self.cHm + 1
  else
    self.rHm = self.cHm > 0
    self.tHm = false
    self.cHm = 0
  end
  if self.Hd then
    fx = true
    self.rHd = false
    self.tHd = self.cHd == 0
    self.cHd = self.cHd + 1
  else
    self.rHd = self.cHd > 0
    self.tHd = false
    self.cHd = 0
  end
  if self.No then
    fx = true
    self.rNo = false
    self.tNo = self.cNo == 0
    self.cNo = self.cNo + 1
  else
    self.rNo = self.cNo > 0
    self.tNo = false
    self.cNo = 0
  end

  self.PressedL = fl
  self.PressedR = fr
  self.PressedX = fx
  self.PressedLR= fl or fr
  self.Pressed  = fl or fr or fx
  return self.Pressed
end