------------------------------------
-- The differents buttons of the PSP
------------------------------------
Button =
{
   UP       = 1,
   DOWN     = 2,
   LEFT     = 3,
   RIGHT    = 4,
   CROSS    = 5,
   SQUARE   = 6,
   CIRCLE   = 7,
   TRIANGLE = 8,
   LTRIGGER = 9,
   RTRIGGER = 10,
   START    = 11,
   SELECT   = 12,
}


controls = {}
controls.buttons = {}


---------------------------------------------------------------
-- Clears the controls (all buttons will be marked as released)
---------------------------------------------------------------
function controls.clear()
   for i = 1, #controls.buttons do
      controls.buttons[i] = 0
   end
end


-----------------------
-- Updates the controls
-----------------------
function controls.update()
   local pgeButtons =
   {
      PGE_CTRL_UP,
      PGE_CTRL_DOWN,
      PGE_CTRL_LEFT,
      PGE_CTRL_RIGHT,
      PGE_CTRL_CROSS,
      PGE_CTRL_SQUARE,
      PGE_CTRL_CIRCLE,
      PGE_CTRL_TRIANGLE,
      PGE_CTRL_LTRIGGER,
      PGE_CTRL_RTRIGGER,
      PGE_CTRL_START,
      PGE_CTRL_SELECT,
   }
   
   pge.controls.update()
   
   for k, button in ipairs(pgeButtons) do
      if (not controls.buttons[k]) then
         controls.buttons[k] = 0
      end
         
      if (controls.buttons[k] == 0) then
         if (pge.controls.pressed(button)) then
            controls.buttons[k] = 1
         end
      elseif (pge.controls.held(button)) then
         controls.buttons[k] = controls.buttons[k] + 1
      else
         controls.buttons[k] = 0
      end
   end
end


-----------------------------------------------
-- Checks whether the given button is held down
-----------------------------------------------
function controls.isHeld(button)
   return (controls.buttons[button] > 0)
end


---------------------------------------------
-- Checks whether the given button is pressed
---------------------------------------------
function controls.isPressed(button)
   return (controls.buttons[button] == 1)
end


----------------------------------------------
-- Checks whether the given button is released
----------------------------------------------
function controls.isReleased(button)
   return (controls.buttons[button] == 0)
end
