unit ddidex1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ddidex, dinput, StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    Joy: TDDIDEX;
    Memo1: TMemo;
    Memo2: TMemo;
    ComboBox1: TComboBox;
    Timer1: TTimer;
    Memo3: TMemo;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure ComboBox1Change(Sender: TObject);
  private
    { Private 宣言 }
    State:TStrings;
    ffx:IDirectInputEffect;
    procedure makeFFX(dev:IDirectInputDevice2);  //devに対して、FFエフェクトを作る
  public
    { Public 宣言 }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
  //Show;
  //Joy.KeyAssign[0,DI_B1]:=DIK_L;

  //変数の初期化
  State:=TStringList.Create;

  //コンボボックスにジョイスティックの名前を入れる
  ComboBox1.Items.Add('キーボード');
  ComboBox1.Items.Add('マウス');
  ComboBox1.Items.AddStrings(Joy.JoyStickNames);
  ComboBox1.ItemIndex:=0;

end;

//FFエフェクトを作る
procedure TForm1.makeFFX(dev:IDirectInputDevice2);
var
  //FFエフェクトについての詳細は、DirectXのヘルプを見てね(^^;)
  axes:Array[0..1] of DWORD;          //エフェクトと関係のある軸
  direction:Array[0..1] of LongInt;   //軸が振動する方向

  pd:DIPERIODIC;  //周期振動に関するパラメータ
  ev:DIENVELOPE;  //振動の包絡線(エンベロープ)に関するパラメータ
  fx:DIEFFECT;    //エフェクト全般についてのパラメータ
begin

  //すでにFFエフェクトが作られているなら、それを解放
  if FFx<>Nil then
    FFx.Release;

  //X,Y軸が関係あり
  axes[0]:=DIJOFS_X;
  axes[1]:=DIJOFS_Y;

  //向きはX=0度 Y=0度
  direction[0]:=0;
  direction[1]:=0;


  with pd do begin
    dwMagnitude:=DI_FFNOMINALMAX;
    lOffset:=0;
    dwPhase:=0;
    dwPeriod:=DI_SECONDS div 10;
  end;

  with ev do begin
    dwSize:=sizeof(ev);
    dwAttackLevel:=0;
    dwAttackTime:=DI_SECONDS div 3;
    dwFadeLevel:=0;
    dwFadeTime:=DI_SECONDS;
  end;

  ZeroMemory(@fx,sizeof(fx));
  with fx do begin
    dwSize:=sizeof(fx);
    dwFlags:=DIEFF_POLAR Or DIEFF_OBJECTOFFSETS;
    dwDuration:=DI_SECONDS * 2;
    dwSamplePeriod:=0;
    dwGain:=DI_FFNOMINALMAX;
    dwTriggerButton:=DIJOFS_BUTTON4;
    dwTriggerRepeatInterval:=0;
    cAxes:=2;
    rgdwAxes:=@axes;
    rglDirection:=@direction;
    lpEnvelope:=@ev;
    cbTypeSpecificParams:=sizeof(pd);
    lpvTypeSpecificParams:=@pd;
  end;

  dev.CreateEffect(@GUID_Sine,@fx,FFx,Nil);
end;


procedure TForm1.Timer1Timer(Sender: TObject);
var
  i:Integer;
  s:String;
begin
  if ComboBox1.ItemIndex = -1 then
    Exit;

  //ジョイスティックの状態を得る
  //実は、DI_KEYB = 0 , DI_MOUSE=1, DI_JOY1=2 ... なのだ
  Joy.Scan(ComboBox1.ItemIndex);

  //表示

  //デジタル入力
  State.Clear;
  State.Add(Format('X = %d',[Joy.Stick.X]));
  State.Add(Format('Y = %d',[Joy.Stick.Y]));
  State.Add(Format('Z = %d',[Joy.Stick.Z]));
  State.Add(Format('rX = %d',[Joy.Stick.rX]));
  State.Add(Format('rY = %d',[Joy.Stick.rY]));
  State.Add(Format('rZ = %d',[Joy.Stick.rZ]));

  State.Add(Format('Slider[0] = %d',[Joy.Stick.Slider[0]]));
  State.Add(Format('Slider[1] = %d',[Joy.Stick.Slider[1]]));
  State.Add(Format('POV[0] = %d',[Joy.Stick.POV[0]]));
  State.Add(Format('POV[1] = %d',[Joy.Stick.POV[1]]));
  State.Add(Format('POV[2] = %d',[Joy.Stick.POV[2]]));
  State.Add(Format('POV[3] = %d',[Joy.Stick.POV[3]]));
  Memo1.Lines:=State;

  //アナログ入力
  State.Clear;
  State.Add(Format('X = %d',[Joy.AStick.X]));
  State.Add(Format('Y = %d',[Joy.AStick.Y]));
  State.Add(Format('Z = %d',[Joy.AStick.Z]));
  State.Add(Format('rX = %d',[Joy.AStick.rX]));
  State.Add(Format('rY = %d',[Joy.AStick.rY]));
  State.Add(Format('rZ = %d',[Joy.AStick.rZ]));

  State.Add(Format('Slider[0] = %d',[Joy.AStick.Slider[0]]));
  State.Add(Format('Slider[1] = %d',[Joy.AStick.Slider[1]]));
  State.Add(Format('POV[0] = %d',[Joy.AStick.POV[0]]));
  State.Add(Format('POV[1] = %d',[Joy.AStick.POV[1]]));
  State.Add(Format('POV[2] = %d',[Joy.AStick.POV[2]]));
  State.Add(Format('POV[3] = %d',[Joy.AStick.POV[3]]));
  State.Add(Format('DeadZone = %d',[Joy.DeadZone]));
  Memo2.Lines:=State;

  //ボタン
  s:='';
  For i:=0 to 31 do begin
    if Joy.Buttons[i] then
      s:=s + IntToStr(i) + ',';
  end;
  Memo3.Text:=s;
end;

procedure TForm1.ComboBox1Change(Sender: TObject);
begin
  if Joy.IsFFJoyStick[ComboBox1.ItemIndex] then
    makeFFX(Joy.JoySticks[ComboBox1.ItemIndex])
  else if FFx <> Nil then begin
    FFx.Release;
    FFx:=Nil;
  end;

end;

end.
