function initClient()
{
   exec("~/defaults.cs");
   exec("./common/canvas.cs");

   initCanvas("TAP -- SimpleJoystick Sample");
   
   // Load-up GUIs.
   exec("./SimpleJoystick.gui");
   Canvas.setContent(SimpleJoystick);
   Canvas.setCursor(DefaultCursor);

   $enableDirectInput = "1";
   activateDirectInput();
   enableXInput();

   // Bind some global keypresses
   GlobalActionMap.bind(keyboard, "tilde", "toggleConsole");
   GlobalActionMap.bindCmd(keyboard, "alt enter", "", "toggleFullScreen();");
   GlobalActionMap.bindCmd(keyboard, "escape", "", "quit();");
   bindJoystickEvents();

   updateGuiControls();
}

function updateGuiControls()
{
   if ( isJoystickEnabled() )
      updateEnabled( %i );
   else
      updateDisabled( %i );

   // Update the gui 30 times a second
   schedule( ( 1 / 30 ) * 1000, 0, "updateGuiControls" );
}

function updateEnabled()
{
   txtJoystick.setText( "Joystick: Enabled" );

   txtButtons.setText( getButtonText() );
   txtXY.setText( "X/Y: " @ $joyXAXIS @ "/" @ $joyYAXIS );
   txtRXRY.setText( "RX/RY: " @ $joyRXAXIS @ "/" @ $joyRYAXIS );
   txtZRZ.setText( "Z/RZ: " @ $joyZAXIS @ "/" @ $joyRZAXIS );
   txtXYPOV.setText( "X/Y POV: " @ $joyXPOV @ "/" @ $joyYPOV );

   txtButtons.setVisible( 1 );
   txtXY.setVisible( 1 );
   txtRXRY.setVisible( 1 );
   txtZRZ.setVisible( 1 );
   txtXYPOV.setVisible( 1 );
}

function updateDisabled()
{
   txtJoystick.setText( "Joystick: Disabled" );
   txtButtons.setVisible( 0 );
   txtXY.setVisible( 0 );
   txtRXRY.setVisible( 0 );
   txtZRZ.setVisible( 0 );
   txtXYPOV.setVisible( 0 );
}

function getButtonText( %cid )
{
   %btnText = "Buttons:";

   for ( %i = 0; %i < 10; %i++ )
   {
      if ( eval("return($joyBTN" @ %i @ ");" ) ) %btnText = %btnText SPC "B" @ %i;
   }

   return %btnText;
}

function bindJoystickEvents()
{
   GlobalActionMap.bind("joystick0", "xaxis", "joyXAXIS");
   GlobalActionMap.bind("joystick0", "yaxis", "joyYAXIS");
   GlobalActionMap.bind("joystick0", "rxaxis", "joyRXAXIS");
   GlobalActionMap.bind("joystick0", "ryaxis", "joyRYAXIS");
   GlobalActionMap.bind("joystick0", "zaxis", "joyZAXIS");
   GlobalActionMap.bind("joystick0", "rzaxis", "joyRZAXIS");
   GlobalActionMap.bind("joystick0", "xpov", "joyXPOV");
   GlobalActionMap.bind("joystick0", "ypov", "joyYPOV");
   GlobalActionMap.bind("joystick0", "button0", "joyBTN0");
   GlobalActionMap.bind("joystick0", "button1", "joyBTN1");
   GlobalActionMap.bind("joystick0", "button2", "joyBTN2");
   GlobalActionMap.bind("joystick0", "button3", "joyBTN3");
   GlobalActionMap.bind("joystick0", "button4", "joyBTN4");
   GlobalActionMap.bind("joystick0", "button5", "joyBTN5");
   GlobalActionMap.bind("joystick0", "button6", "joyBTN6");
   GlobalActionMap.bind("joystick0", "button7", "joyBTN7");
   GlobalActionMap.bind("joystick0", "button8", "joyBTN8");
   GlobalActionMap.bind("joystick0", "button9", "joyBTN9");
   $joyXPOV = 0.5;
   $joyYPOV = 0.5;
}

function joyXAXIS( %val )       { $joyXAXIS = %val; }
function joyYAXIS( %val )       { $joyYAXIS = %val; }
function joyRXAXIS( %val )      { $joyRXAXIS = %val; }
function joyRYAXIS( %val )      { $joyRYAXIS = %val; }
function joyZAXIS( %val )       { $joyZAXIS = %val; }
function joyRZAXIS( %val )      { $joyRZAXIS = %val; }
function joyXPOV( %val )        { $joyXPOV = %val; }
function joyYPOV( %val )        { $joyYPOV = %val; }
function joyBTN0( %val )        { $joyBTN0 = %val; }
function joyBTN1( %val )        { $joyBTN1 = %val; }
function joyBTN2( %val )        { $joyBTN2 = %val; }
function joyBTN3( %val )        { $joyBTN3 = %val; }
function joyBTN4( %val )        { $joyBTN4 = %val; }
function joyBTN5( %val )        { $joyBTN5 = %val; }
function joyBTN6( %val )        { $joyBTN6 = %val; }
function joyBTN7( %val )        { $joyBTN7 = %val; }
function joyBTN8( %val )        { $joyBTN8 = %val; }
function joyBTN9( %val )        { $joyBTN9 = %val; }

initClient();
