; In this file, we handle alle the events

Procedure HandleEventMenu()
  If KeyboardReleased(#PB_Key_Up)
    PlaySound(0, #PB_Sound_MultiChannel)
    Menu_GotoCurrent()
    *ActiveMenu\Entries()\Selected = #False
    
    If ListIndex(*ActiveMenu\Entries()) = 0
      LastElement(*ActiveMenu\Entries())
    Else
      PreviousElement(*ActiveMenu\Entries())
    EndIf
    
    *ActiveMenu\Entries()\Selected = #True
  EndIf
  
  If KeyboardReleased(#PB_Key_Down)
    PlaySound(0, #PB_Sound_MultiChannel)
    Menu_GotoCurrent()
    *ActiveMenu\Entries()\Selected = #False
    
    If ListIndex(*ActiveMenu\Entries()) = (ListSize(*ActiveMenu\Entries())-1)
     FirstElement(*ActiveMenu\Entries())
    Else
      NextElement(*ActiveMenu\Entries())
    EndIf
    
    *ActiveMenu\Entries()\Selected = #True
  EndIf
  
  ; We go to the target game state
  If KeyboardReleased(#PB_Key_Return) 
    PlaySound(1, #PB_Sound_MultiChannel)
    Menu_GotoCurrent()
    CurrentState = *ActiveMenu\Entries()\TargetState
    *ActiveMenu.GameMenu = Menus(CurrentState)
  EndIf
  
  
EndProcedure

Procedure HandleEventRunningGame()
  If KeyboardReleased(#PB_Key_Q)
    CurrentState = "MAIN_MENU"
    *ActiveMenu.GameMenu = Menus(CurrentState)
  EndIf
  
  ; Player movement
  If KeyboardReleased(#PB_Key_Left) And Player\X > 0
    Player\X-1
   
    ; Camera positioning
    If Player\X < (Cam\X + Cam\Padding) And Cam\X > 0
      Cam\X - 1
    EndIf
  EndIf
  
  If KeyboardReleased(#PB_Key_Right) And Player\X < #MAP_WIDTH-1
    Player\X+1

    ; Camera positioning
    If Player\X > (Cam\X + Cam\Width - Cam\Padding) And (Cam\X + Cam\Width) < #MAP_WIDTH-1
      Cam\X + 1
    EndIf
  EndIf
  
  If KeyboardReleased(#PB_Key_Up) And Player\Y > 0
    Player\Y-1
    
    ; Camera positioning
    If Player\Y< (Cam\Y + Cam\Padding) And Cam\Y > 0
      Cam\Y - 1
    EndIf
  EndIf
  
  If KeyboardReleased(#PB_Key_Down) And Player\Y < #MAP_HEIGHT-1
    Player\Y+1
    
    ; Camera positioning
    If Player\Y > (Cam\Y + Cam\Height - Cam\Padding) And (Cam\Y + Cam\Height) < #MAP_HEIGHT-1
      Cam\Y + 1
    EndIf
  EndIf
EndProcedure