Subversion Repositories spacemission

Rev

Blame | Last modification | View Log | RSS feed

  1. unit DXInptEdit;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   ComCtrls, StdCtrls, Menus, ExtCtrls, Spin, Buttons, DXInput;
  8.  
  9. type
  10.   TDelphiXInputEditForm = class(TForm)
  11.     PopupMenu: TPopupMenu;
  12.     Player1: TMenuItem;
  13.     Player2_1: TMenuItem;
  14.     Player2_2: TMenuItem;
  15.     OKButton: TButton;
  16.     CancelButton: TButton;
  17.     PageControl: TPageControl;
  18.     JoyTabSheet: TTabSheet;
  19.     KeyTabSheet: TTabSheet;
  20.     GroupBox1: TGroupBox;
  21.     StateListBox: TListBox;
  22.     KeyComboBox1: TComboBox;
  23.     KeyComboBox2: TComboBox;
  24.     KeyComboBox3: TComboBox;
  25.     Label3: TLabel;
  26.     Label2: TLabel;
  27.     Label1: TLabel;
  28.     GroupBox2: TGroupBox;
  29.     Label5: TLabel;
  30.     IDEdit: TSpinEdit;
  31.     Bevel1: TBevel;
  32.     AutoCenter: TCheckBox;
  33.     GroupBox3: TGroupBox;
  34.     Label7: TLabel;
  35.     Label8: TLabel;
  36.     Label9: TLabel;
  37.     DeadZoneZ: TSpinEdit;
  38.     DeadZoneY: TSpinEdit;
  39.     DeadZoneX: TSpinEdit;
  40.     GroupBox4: TGroupBox;
  41.     Label4: TLabel;
  42.     Label6: TLabel;
  43.     Label10: TLabel;
  44.     RangeZ: TSpinEdit;
  45.     RangeY: TSpinEdit;
  46.     RangeX: TSpinEdit;
  47.     Label11: TLabel;
  48.     Label12: TLabel;
  49.     Label13: TLabel;
  50.     ForceFeedback: TCheckBox;
  51.     procedure FormCreate(Sender: TObject);
  52.     procedure StateListBoxClick(Sender: TObject);
  53.     procedure Player1Click(Sender: TObject);
  54.     procedure Player2_1Click(Sender: TObject);
  55.     procedure Player2_2Click(Sender: TObject);
  56.     procedure OKButtonClick(Sender: TObject);
  57.     procedure CancelButtonClick(Sender: TObject);
  58.     procedure FormShow(Sender: TObject);
  59.     procedure EditChange(Sender: TObject);
  60.   private
  61.     FChanged: Boolean;
  62.     FOldIndex: Integer;
  63.     procedure UpdateData;
  64.   public
  65.     DXInput: TCustomDXInput;
  66.     KeyAssigns: TKeyAssignList;
  67.   end;
  68.  
  69. var
  70.   DelphiXInputEditForm: TDelphiXInputEditForm;
  71.  
  72. implementation
  73.  
  74. uses DXConsts;
  75.  
  76. {$R *.dfm}
  77.  
  78. function GetKeyText(Key: Integer): string;
  79. begin
  80.   Result := '';
  81.   case Key of
  82.     0          : Result := SNone;
  83.     VK_TAB     : Result := 'TAB';
  84.     VK_RETURN  : Result := 'Enter';
  85.     VK_SHIFT   : Result := 'Shift';
  86.     VK_ESCAPE  : Result := 'Esc';
  87.     VK_SPACE   : Result := 'Space';
  88.     VK_PRIOR   : Result := 'Page Up';
  89.     VK_NEXT    : Result := 'Page Down';
  90.     VK_LEFT    : Result := SKeyLeft;
  91.     VK_UP      : Result := SKeyUp;
  92.     VK_RIGHT   : Result := SKeyRight;
  93.     VK_DOWN    : Result := SKeyDown;
  94.     $30..$39,
  95.     $41..$5A   : Result := Char(Key); {0..9, A..Z}
  96.     VK_NUMPAD0..VK_NUMPAD9:
  97.                  Result := 'Num '+inttostr(Key-VK_NUMPAD0);
  98.     VK_MULTIPLY: Result := 'Num *';
  99.     VK_ADD     : Result := 'Num +';
  100.     VK_SUBTRACT: Result := 'Num -';
  101.     VK_DECIMAL : Result := 'Num .';
  102.     VK_DIVIDE  : Result := 'Num /';
  103.     VK_F1..VK_F15:
  104.                  Result := 'F'+inttostr(Key-VK_F1+1);
  105.   end;
  106. end;
  107.  
  108. function DXInputStateText(State: TDXInputState): string;
  109. begin
  110.   Result := '';
  111.   case State of
  112.     isUp   : Result := 'Up';
  113.     isDown : Result := 'Down';
  114.     isLeft : Result := 'Left';
  115.     isRight: Result := 'Right';
  116.     isButton1..isButton32
  117.       : Result := 'Button' + inttostr(Ord(State)-Ord(isButton1)+1);
  118.   end;
  119. end;
  120.  
  121. procedure TDelphiXInputEditForm.FormCreate(Sender: TObject);
  122. var
  123.   i: TDXInputState;
  124.   i2: Integer;
  125.   s: string;
  126. begin
  127.  for i := LOW(TDXInputState) to HIGH(TDXInputState) do
  128.     StateListBox.Items.Add(DXInputStateText(i));
  129.  
  130.   for i2:=0 to 255 do
  131.   begin
  132.     s := GetKeyText(i2);
  133.     if s<>'' then KeyComboBox1.Items.AddObject(s, Pointer(i2));
  134.   end;
  135.   KeyComboBox2.Items.Assign(KeyComboBox1.Items);
  136.   KeyComboBox3.Items.Assign(KeyComboBox1.Items);
  137. end;
  138.  
  139. procedure TDelphiXInputEditForm.FormShow(Sender: TObject);
  140. begin
  141.   KeyAssigns := DXInput.Keyboard.KeyAssigns;
  142.  
  143.   IDEdit.Value := DXInput.Joystick.ID;
  144.  
  145.   AutoCenter.Checked := DXInput.Joystick.AutoCenter;
  146.   ForceFeedback.Checked := DXInput.Joystick.ForceFeedback;
  147.  
  148.   DeadZoneX.Value := DXInput.Joystick.DeadZoneX;
  149.   DeadZoneY.Value := DXInput.Joystick.DeadZoneY;
  150.   DeadZoneZ.Value := DXInput.Joystick.DeadZoneZ;
  151.  
  152.   RangeX.Value := DXInput.Joystick.RangeX;
  153.   RangeY.Value := DXInput.Joystick.RangeY;
  154.   RangeZ.Value := DXInput.Joystick.RangeZ;
  155.  
  156.   UpdateData;
  157.  
  158.   FChanged := False;
  159. end;
  160.  
  161. procedure TDelphiXInputEditForm.OKButtonClick(Sender: TObject);
  162. begin
  163.   if FChanged then
  164.   begin
  165.     if StateListBox.ItemIndex<>-1 then
  166.     begin
  167.       FOldIndex := StateListBox.ItemIndex;
  168.       StateListBoxClick(nil);
  169.     end;
  170.  
  171.     DXInput.Joystick.ID := IDEdit.Value;
  172.  
  173.     DXInput.Joystick.AutoCenter := AutoCenter.Checked;
  174.     DXInput.Joystick.ForceFeedback := ForceFeedback.Checked;
  175.  
  176.     DXInput.Joystick.DeadZoneX := DeadZoneX.Value;
  177.     DXInput.Joystick.DeadZoneY := DeadZoneY.Value;
  178.     DXInput.Joystick.DeadZoneZ := DeadZoneZ.Value;
  179.  
  180.     DXInput.Joystick.RangeX := RangeX.Value;
  181.     DXInput.Joystick.RangeY := RangeY.Value;
  182.     DXInput.Joystick.RangeZ := RangeZ.Value;
  183.  
  184.     DXInput.Keyboard.KeyAssigns := KeyAssigns;
  185.  
  186.     Tag := 1;
  187.   end;
  188.  
  189.   Close;
  190. end;
  191.  
  192. procedure TDelphiXInputEditForm.UpdateData;
  193. begin
  194.   FOldIndex := -1;
  195.   StateListBox.ItemIndex := 0;
  196.   StateListBoxClick(nil);
  197. end;
  198.  
  199. procedure TDelphiXInputEditForm.StateListBoxClick(Sender: TObject);
  200.  
  201.   procedure SetItemIndex(ComboBox: TComboBox; Data: Integer);
  202.   var
  203.     i: Integer;
  204.   begin
  205.     for i:=0 to ComboBox.Items.Count-1 do
  206.       if Integer(ComboBox.Items.Objects[i])=Data then
  207.       begin
  208.         ComboBox.ItemIndex := i;
  209.         Exit;
  210.       end;
  211.     ComboBox.ItemIndex := 0;
  212.   end;
  213.  
  214. var
  215.   KeyAssign: PKeyAssign;
  216. begin
  217.   if FOldIndex<>-1 then
  218.   begin
  219.     KeyAssign := @KeyAssigns[TDXInputState(FOldIndex)];
  220.     KeyAssign^[0] := Integer(KeyComboBox1.Items.Objects[KeyComboBox1.ItemIndex]);
  221.     KeyAssign^[1] := Integer(KeyComboBox2.Items.Objects[KeyComboBox2.ItemIndex]);
  222.     KeyAssign^[2] := Integer(KeyComboBox3.Items.Objects[KeyComboBox3.ItemIndex]);
  223.   end;
  224.   FOldIndex := StateListBox.ItemIndex;
  225.  
  226.   KeyAssign := @KeyAssigns[TDXInputState(FOldIndex)];
  227.   SetItemIndex(KeyComboBox1,  KeyAssign^[0]);
  228.   SetItemIndex(KeyComboBox2,  KeyAssign^[1]);
  229.   SetItemIndex(KeyComboBox3,  KeyAssign^[2]);
  230. end;
  231.  
  232. procedure TDelphiXInputEditForm.Player1Click(Sender: TObject);
  233. begin
  234.   IDEdit.Value := 0;
  235.   KeyAssigns := DefKeyAssign;
  236.  
  237.   FChanged := True;
  238.   UpdateData;
  239. end;
  240.  
  241. procedure TDelphiXInputEditForm.Player2_1Click(Sender: TObject);
  242. begin
  243.   IDEdit.Value := 0;
  244.   KeyAssigns := DefKeyAssign2_1;
  245.  
  246.   FChanged := True;
  247.   UpdateData;
  248. end;
  249.  
  250. procedure TDelphiXInputEditForm.Player2_2Click(Sender: TObject);
  251. begin
  252.   IDEdit.Value := 1;
  253.   KeyAssigns := DefKeyAssign2_2;
  254.  
  255.   FChanged := True;
  256.   UpdateData;
  257. end;
  258.  
  259. procedure TDelphiXInputEditForm.CancelButtonClick(Sender: TObject);
  260. begin
  261.   Close;
  262. end;
  263.  
  264. procedure TDelphiXInputEditForm.EditChange(Sender: TObject);
  265. begin
  266.   FChanged := True;
  267. end;
  268.  
  269. end.
  270.  
  271.