///////////////////////////////////////////////////////////////////////
// Copyright (C) Andru 2007                                          //
// mail: dr.andru@gmail.com                                          //
// site: http://andru.2x4.ru                                         //
//                                                                   //
// OMEGA Engine is free software.                                    //
//                                                                   //
// You may redistribute it and/or modify it under the terms of the   //
// GNU General Public License, as published by the Free Software     //
// Foundation; either version 2 of the License, or (at your option)  //
// any later version.                                                //
//                                                                   //
// this module is distributed in the hope that it will be useful,    //
// but WITHOUT ANY WARRANTY; without even the implied warranty of    //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.              //
// See the GNU General Public License for more details.              //
//                                                                   //
// You should have received a copy of the GNU General Public License //
// along with this module.  If not, write to:                        //
// 	The Free Software Foundation, Inc.,                              //
// 	51 Franklin Street, Fifth Floor                                  //
// 	Boston, MA  02110-1301, USA.                                     //
///////////////////////////////////////////////////////////////////////
unit INPUT_JOY;

{$I DEFINES.pas}

interface
uses
  Windows,

  ENGINE_VAR;

function Joy_Down( Joy, Button : Integer ) : Boolean; stdcall;
function Joy_Press( Joy, Button : Integer ) : Boolean; stdcall;
function Joy_AxePos( Joy, AxePos : Integer ) : Boolean; stdcall;

type
  MMRESULT = UINT;

  PJoyInfo = ^TJoyInfo;
  {$EXTERNALSYM joyinfo_tag}
  joyinfo_tag = record
    wXpos: UINT;                 { x position }
    wYpos: UINT;                 { y position }
    wZpos: UINT;                 { z position }
    wButtons: UINT;              { button states }
  end;
  TJoyInfo = joyinfo_tag;

  function joyGetPos(uJoyID: UINT; lpInfo: PJoyInfo): MMRESULT; stdcall external 'winmm.dll';
  
var
  inpJ_Press : array[ 0..32 ] of Boolean;

//----------------------------------------------------------------------------//
                                 IMPLEMENTATION
//----------------------------------------------------------------------------//
function Joy_AxePos;
  var
    jInfo : TJoyInfo;
begin

joyGetPos( Joy, @jInfo );

case AxePos of
  1 : Result := jInfo.wXpos = 0;
  2 : Result := jInfo.wXpos = 65535;
  3 : Result := jInfo.wYpos = 0;
  4 : Result := jInfo.wYpos = 65535;
end;

end;

function Joy_Down;
  var
    jInfo : TJoyInfo;
begin

joyGetPos( Joy, @jInfo );
if ( jInfo.wButtons and Button ) > 0 Then
  Result := TRUE
else
  Result := FALSE;

End;

function Joy_Press;
  var
    jInfo : TJoyInfo;
begin

joyGetPos( Joy, @jInfo );

if ( inpJ_Press[ Button ] ) and ( jInfo.wButtons and Button = 0 ) Then
  begin
    inpJ_Press[ Button ] := FALSE;
    Result               := TRUE;
  end else
    begin
      inpJ_Press[ Button ] := jInfo.wButtons and Button > 0;
      Result               := FALSE;
    end;

End;

{const
  JOY_0      = $000000;

  JOY_XLEFT  = $000001;
  JOY_XRIGHT = $000002;
  JOY_YUP    = $000003;
  JOY_YDOWN  = $000004;

  JOY_BTN01  = $00000001;
  JOY_BTN02  = $00000002;
  JOY_BTN03  = $00000004;
  JOY_BTN04  = $00000008;
  JOY_BTN05  = $00000010;
  JOY_BTN06  = $00000020;
  JOY_BTN07  = $00000040;
  JOY_BTN08  = $00000080;
  JOY_BTN09  = $00000100;
  JOY_BTN10  = $00000200;
  JOY_BTN11  = $00000400;
  JOY_BTN12  = $00000800;
  JOY_BTN13  = $00001000;
  JOY_BTN14  = $00002000;
  JOY_BTN15  = $00004000;
  JOY_BTN16  = $00008000;
  JOY_BTN17  = $00010000;
  JOY_BTN18  = $00020000;
  JOY_BTN19  = $00040000;
  JOY_BTN20  = $00080000;
  JOY_BTN21  = $00100000;
  JOY_BTN22  = $00200000;
  JOY_BTN23  = $00400000;
  JOY_BTN24  = $00800000;
  JOY_BTN25  = $01000000;
  JOY_BTN26  = $02000000;
  JOY_BTN27  = $04000000;
  JOY_BTN28  = $08000000;
  JOY_BTN29  = $10000000;
  JOY_BTN30  = $20000000;
  JOY_BTN31  = $40000000;
  JOY_BTN32  = $80000000;}

end.
