function ConfigureKey(%key)
{
   canvas.PushDialog(SetKey);
   keydesc.setText(%key);   
}

function CloseKeys()
{
   canvas.PopDialog(keys);
   canvas.PopDialog(Options);   
   UpdateMainBarShortcuts();
}

function Keys::OnWake()
{
   LoadShortCuts();
   DisplayShortcuts();   
}

function DisplayShortcuts()
{
   keys_accelerate.setText($keys_Accelerate);
   keys_attackcrew.setText($Keys_AttackCrew);
   keys_attackengine.setText($keys_AttackEngine);
   keys_attackgastank.setText($Keys_AttackGasTank);
   keys_attacktires.setText($Keys_AttackTires);
   keys_selecttarget.setText($Keys_SelectTarget);
   keys_attackweapon.setText($Keys_AttackWeapon);
   keys_boardvehicle.setText($Keys_BoardVehicle);
   keys_brake.setText($keys_Brake);
   keys_capturecity.setText($Keys_CaptureCity);
   keys_gangstats.setText($keys_GangStats);
   keys_healgang.setText($keys_HealGang);
   keys_map.setText($keys_Map);
   keys_pause.setText($keys_Pause);
   keys_peoplesearch.setText($keys_PeopleSearch);
   keys_quicksave.setText($keys_QuickSave);
   keys_quickload.setText($keys_QuickLoad);
   keys_quitgame.setText($keys_QuitGame);
   keys_ram.setText($keys_RamVehicle);
   keys_RotateCamera.setText($keys_RotateCamera);
   keys_vehiclesearch.setText($keys_VehicleSearch);
   keys_supplysearch.setText($keys_SupplySearch);
   keys_vehiclestats.setText($keys_VehicleStats);
   keys_vehicleupgrades.setText($keys_VehicleUpgrades);
   keys_zoomin.setText($keys_ZoomIn);
   keys_zoomout.setText($keys_ZoomOut);      
   keys_attack.setText($keys_Attack);
   keys_turnright.setText($Keys_TurnRight);
   keys_turnleft.setText($keys_TurnLeft);
   keys_move.setText($Keys_Move);
}

// this will test for a custom shortcut file, if it doesn't exist we'll load the defaults
// once the player changes a single default key it will save a custom file and use that from that 
// point on

function LoadShortCuts()
{
   %fo = new fileobject();
  
   %file = "~/shortcuts.sav";
   %ret = %fo.OpenForRead(%file);

   // if there is no custom shortcut file then load the defaults
      
   if(%ret == 0)
   {
      $keys_Accelerate = "A";
      $Keys_AttackCrew = "2";
      $keys_AttackEngine = "3";
      $Keys_AttackGasTank = "4";
      $Keys_AttackTires = "5";
      $Keys_SelectTarget = "1";
      $Keys_AttackWeapon = "6";
      $Keys_BoardVehicle = "7";
      $keys_Brake = "B";
      $Keys_CaptureCity = "C";
      $keys_Continue = "Enter";      
      $keys_GangStats = "G";
      $keys_HealGang = "H";
      $keys_Map = "M";
      $keys_Pause = "Space";
      $keys_PeopleSearch = "P";
      $keys_QuickSave = "F5";
      $keys_QuickLoad = "F9";
      $keys_QuitGame = "Escape";
      $keys_RamVehicle = "8";
      $keys_RotateCamera = "tab";
      $keys_VehicleSearch = "V";
      $keys_SupplySearch = "S";
      $keys_VehicleStats = "T";
      $keys_VehicleUpgrades = "X";
      $keys_ZoomIn = "=";
      $keys_ZoomOut = "-";      
      $Keys_Move = "J";
      $keys_TurnLeft = "L";
      $Keys_TurnRight = "R";
      $keys_Attack = "K";
   }
   else
   {      
      for(%line = %fo.ReadLine();%line !$= "";%line = %fo.ReadLine())
      {
         %key = getWord(%line, 0);
         
         switch$ (%key)
         {
            case "Accelerate":
               $keys_Accelerate = getWord(%line, 1);            
            case "AttackCrew":
               $Keys_AttackCrew = getWord(%line, 1);
            case "AttackEngine":
               $keys_AttackEngine  = getWord(%line, 1);
            case "AttackGasTank":
               $Keys_AttackGasTank  = getWord(%line, 1);
            case "AttackTires":
               $Keys_AttackTires = getWord(%line, 1);
            case "SelectTarget":
               $Keys_SelectTarget = getWord(%line, 1);
            case "AttackWeapon":
               $Keys_AttackWeapon = getWord(%line, 1);
            case "BoardVehicle":
               $Keys_BoardVehicle = getWord(%line, 1);
            case "Brake":
               $keys_Brake = getWord(%line, 1);
            case "CaptureCity":
               $Keys_CaptureCity = getWord(%line, 1);
            case "GangStats":
               $keys_GangStats = getWord(%line, 1);
            case "HealGang":
               $keys_HealGang = getWord(%line, 1);
            case "Map":
               $keys_Map = getWord(%line, 1);
            case "Pause":
               $keys_Pause = getWord(%line, 1);
            case "PeopleSearch":
               $keys_PeopleSearch = getWord(%line, 1);
            case "QuickSave":
               $keys_QuickSave = getWord(%line, 1);
            case "QuickLoad":
               $keys_QuickLoad = getWord(%line, 1);
            case "QuitGame":
               $keys_QuitGame = getWord(%line, 1);
            case "RamVehicle":
               $keys_RamVehicle = getWord(%line, 1);
            case "RotateCamera":
               $keys_RotateCamera = getWord(%line, 1);
            case "VehicleSearch":
               $keys_VehicleSearch = getWord(%line, 1);
            case "SupplySearch":
               $keys_SupplySearch = getWord(%line, 1);
            case "VehicleStats":
               $keys_VehicleStats = getWord(%line, 1);
            case "VehicleUpgrades":
               $keys_VehicleUpgrades = getWord(%line, 1);
            case "ZoomIn":
               $keys_ZoomIn = getWord(%line, 1);
            case "ZoomOut":      
               $keys_ZoomOut = getWord(%line, 1);
            case "TurnRight":
               $Keys_TurnRight = getWord(%line, 1);
            case "TurnLeft":
               $keys_TurnLeft = getWord(%line, 1);
            case "Move":
               $Keys_Move = getWord(%line, 1);
            case "Attack":
               $keys_Attack = getWord(%line, 1);
         }
      }
         
      %fo.close();
   }
}

function ReMapKeys::OnInputEvent(%this, %device, %action)
{
   if(%action $= "mouse0" || %action $= "mouse1")
      return;
   
   %action = UpperCase(%action);
   
   ValidateShortcut(%action);
   
   if(%action $= "y" || %action $= "n" || %action $= "enter" || %action $= "escape")
   {
      alxPlay(buzzer);
      return;      
   }
   
   canvas.popdialog(SetKey);
   
   switch$ (keydesc.text)
   {
      case "Accelerate":
         $keys_Accelerate = %action;            
      case "Attack Crew":
         $Keys_AttackCrew = %action;
      case "Attack Engine":
         $keys_AttackEngine  = %action;
      case "Attack Gas Tank":
         $Keys_AttackGasTank  = %action;
      case "Attack Tires":
         $Keys_AttackTires = %action;
      case "Attack Vehicle":
         $Keys_AttackVehicle = %action;
      case "Attack Weapon":
         $Keys_AttackWeapon = %action;
      case "Board Vehicle":
         $Keys_BoardVehicle = %action;
      case "Select Target":
         $Keys_SelectTarget = %action;
      case "Brake":
         $keys_Brake = %action;
      case "Capture City":
         $Keys_CaptureCity = %action;
      case "Gang Stats":
         $keys_GangStats = %action;
      case "Heal Gang":
         $keys_HealGang = %action;
      case "Map":
         $keys_Map = %action;
      case "Pause":
         $keys_Pause = %action;
      case "People Search":
         $keys_PeopleSearch = %action;
      case "Quick Save":
         $keys_QuickSave = %action;
      case "Quick Load":
         $keys_QuickLoad = %action;
      case "QuitGame":
         $keys_QuitGame = %action;
      case "RamVehicle":
         $keys_RamVehicle = %action;
      case "RotateCamera":
         $keys_RotateCamera = %action;
      case "Vehicle Search":
         $keys_VehicleSearch = %action;
      case "Supply Search":
         $keys_SupplySearch = %action;
      case "Vehicle Stats":
         $keys_VehicleStats = %action;
      case "Vehicle Upgrades":
         $keys_VehicleUpgrades = %action;
      case "Zoom In":
         $keys_ZoomIn = %action;
      case "Zoom Out":      
         $keys_ZoomOut = %action;
      case "TurnRght":
         $Keys_TurnRight = %action;
      case "TurnLeft":
         $Keys_TurnLeft = %action;
      case "Move":
         $Keys_Move = %action;
      case "Attck":
         $keys_Attack = %action;
   }
   
   DisplayShortcuts();
   SaveCustomKeys();
   
   echo("KEY IS " @ %action);   
   
}

function SaveCustomKeys()
{
   %fo = new fileobject();
  
   %file = "~/shortcuts.sav";
 
   %fo.OpenForWrite(%file);   
   
   %fo.WriteLine("accelerate " @ $keys_Accelerate);
   %fo.WriteLine("attackcrew " @ $Keys_AttackCrew);
   %fo.WriteLine("attackengine " @ $keys_AttackEngine);
   %fo.WriteLine("attackgastank " @ $Keys_AttackGasTank);
   %fo.WriteLine("attacktires " @ $Keys_AttackTires);
   %fo.WriteLine("selecttarget " @ $Keys_SelectTarget);
   %fo.WriteLine("attackweapon " @ $Keys_AttackWeapon);
   %fo.WriteLine("boardvehicle " @ $Keys_BoardVehicle);
   %fo.WriteLine("brake " @ $keys_Brake);
   %fo.WriteLine("capturecity " @ $Keys_CaptureCity);
   %fo.WriteLine("gangstats " @ $keys_GangStats);
   %fo.WriteLine("healgang " @ $keys_HealGang);
   %fo.WriteLine("map " @ $keys_Map);
   %fo.WriteLine("pause " @ $keys_Pause);
   %fo.WriteLine("peoplesearch " @ $keys_PeopleSearch);
   %fo.WriteLine("quicksave " @ $keys_QuickSave);
   %fo.WriteLine("quickload " @ $keys_QuickLoad);
   %fo.WriteLine("quitgame " @ $keys_QuitGame);
   %fo.WriteLine("ramvehicle " @ $keys_RamVehicle);
   %fo.WriteLine("RotateCamera " @ $keys_RotateCamera);
   %fo.WriteLine("vehiclesearch " @ $keys_VehicleSearch);
   %fo.WriteLine("supplysearch " @ $keys_SupplySearch);
   %fo.WriteLine("vehiclestats " @ $keys_VehicleStats);
   %fo.WriteLine("vehicleupgrades " @ $keys_VehicleUpgrades);
   %fo.WriteLine("zoomin " @ $keys_ZoomIn);
   %fo.WriteLine("zoomout " @ $keys_ZoomOut);      
   %fo.WriteLine("TurnRight " @ $Keys_TurnRight);      
   %fo.WriteLine("TurnLeft " @ $keys_TurnLeft);      
   %fo.WriteLine("Move " @ $Keys_Move);      
   %fo.WriteLine("Attack " @ $keys_Attack);      
   
   %fo.close();
}

function UpperCase(%letter)
{
   switch$ (%letter)
   {
      case "a":
         return "A";   
      case "b":
         return "B";   
      case "c":
         return "C";      
      case "d":
         return "D";      
      case "e":
         return "E";      
      case "f":
         return "F";   
      case "g":
         return "G";   
      case "h":
         return "H";   
      case "i":
         return "I";   
      case "j":
         return "J";   
      case "k":
         return "K";   
      case "l":
         return "L";   
      case "m":
         return "M";   
      case "n":
         return "N";   
      case "o":
         return "O";   
      case "p":
         return "P";   
      case "q":
         return "Q";   
      case "r":
         return "R";   
      case "s":
         return "S";   
      case "t":
         return "T";   
      case "u":
         return "U";   
      case "v":
         return "V";   
      case "w":
         return "W";   
      case "x":
         return "X";   
      case "y":
         return "Y";   
      case "z":
         return "Z";   
      default:
         return %letter;
   }
}

// once a new key is pressed,if that is defined anywhere else delete it

function ValidateShortcut(%key)
{
   if($keys_Accelerate $= %key)
      $keys_Accelerate = "";
   if($Keys_AttackCrew $= %key)
      $Keys_AttackCrew = "";
   if($keys_AttackEngine $= %key)
      $keys_AttackEngine = "";
   if($Keys_AttackGasTank $= %key)
      $Keys_AttackGasTank = "";
   if($Keys_AttackTires $= %key)
      $Keys_AttackTires = "";
   if($Keys_SelectTarget $= %key)
      $Keys_SelectTarget = "";
   if($Keys_AttackWeapon $= %key)
      $Keys_AttackWeapon = "";
   if($Keys_BoardVehicle $= %key)
      $Keys_BoardVehicle = "";
   if($keys_Brake $= %key)
      $keys_Brake = "";
   if($Keys_CaptureCity $= %key)
      $Keys_CaptureCity = "";
   if($keys_GangStats $= %key)
      $keys_GangStats = "";
   if($keys_HealGang $= %key)
      $keys_HealGang = "";
   if($keys_Map $= %key)
      $keys_Map = "";
   if($keys_Pause $= %key)
      $keys_Pause = "";   
   if($keys_PeopleSearch $= %key)
      $keys_PeopleSearch = "";
   if($keys_QuickSave $= %key)
      $keys_QuickSave = "";
   if($keys_QuickLoad $= %key)
      $keys_QuickLoad = "";
   if($keys_QuitGame $= %key)
      $keys_QuitGame = "";
   if($keys_RamVehicle $= %key)
      $keys_RamVehicle = "";
   if($keys_RotateCamera $= %key)
      $keys_RotateCamera = "";
   if($keys_VehicleSearch $= %key)
      $keys_VehicleSearch = "";
   if($keys_SupplySearch $= %key)
      $keys_SupplySearch = "";
   if($keys_VehicleStats $= %key)
      $keys_VehicleStats = "";
   if($keys_VehicleUpgrades $= %key)
      $keys_VehicleUpgrades = "";
   if($keys_ZoomIn $= %key)
      $keys_ZoomIn = "";
   if($keys_ZoomOut $= %key)
      $keys_ZoomOut = "";               
   if($Keys_TurnRight $= %key)
      $Keys_TurnRight = "";
   if($keys_TurnLeft $= %key)
      $keys_TurnLeft = "";
   if($Keys_Move $= %key)
      $Keys_Move = "";
   if($keys_Attack $= %key)
      $keys_Attack = "";
}

function ResetKeys()
{
   keys_accelerate.setText("A");
   keys_attackcrew.setText("2");
   keys_attackengine.setText("3");
   keys_attackgastank.setText("4");
   keys_attacktires.setText("5");
   keys_selecttarget.setText("1");
   keys_attackweapon.setText("6");
   keys_boardvehicle.setText("7");
   keys_brake.setText("B");
   keys_capturecity.setText("C");
   keys_gangstats.setText("G");
   keys_healgang.setText("H");
   keys_map.setText("M");
   keys_pause.setText("Space");
   keys_peoplesearch.setText("P");
   keys_quickload.setText("F9");
   keys_quicksave.setText("F5");
   keys_quitgame.setText("Escape");
   keys_ram.setText("8");
   keys_RotateCamera.setText("tab");
   keys_vehiclesearch.setText("V");
   keys_supplysearch.setText("S");
   keys_vehiclestats.setText("T");
   keys_vehicleupgrades.setText("X");
   keys_zoomin.setText("=");
   keys_zoomout.setText("-");
   keys_Move.setText("J");
   keys_Attack.setText("K");
   keys_TurnRight.setText("R");
   keys_TurnLeft.setText("L");
   
   $keys_Accelerate = "A";
   $Keys_AttackCrew = "2";
   $keys_AttackEngine = "3";
   $Keys_AttackGasTank = "4";
   $Keys_AttackTires = "5";
   $Keys_SelectTarget = "1";
   $Keys_AttackWeapon = "6";
   $Keys_BoardVehicle = "7";
   $keys_Brake = "B";
   $Keys_CaptureCity = "C";
   $keys_GangStats = "G";
   $keys_HealGang = "H";
   $keys_Map = "M";
   $keys_Pause = "Space";
   $keys_PeopleSearch = "P";
   $keys_QuickLoad = "F9";
   $keys_QuickSave = "F5";
   $keys_QuitGame = "Escape";
   $keys_RamVehicle = "8";
   $keys_RotateCamera = "tab";
   $keys_VehicleSearch = "V";
   $keys_SupplySearch = "S";
   $keys_VehicleStats = "T";
   $keys_VehicleUpgrades = "X";
   $keys_ZoomIn = "=";
   $keys_ZoomOut = "-";      
   $keys_Move = "J";      
   $keys_Attack = "K";      
   $Keys_TurnRight = "R";      
   $keys_TurnLeft = "L";      
   
   SaveCustomKeys();
}