📄 unit1.pas
字号:
{
Joystick Demo - Jeff Kurtz
---------------------------------------------------------------------------
This example shows how to use the Joystick component -- Read the DOC file
for all the properties, methods, and events.
This is a rather simple example. On the main form are 9 blue panels, these
represent the 9 positions of a digital joystick. There are 4 red panels,
these represent the 4 buttons.
There is also 1 larger panel with a label on it with '+' in it. This panel
is used for the Analog test screen. When digital mode is selected, this
panel will be hidden, when Analog mode is set, this panel will be visible.
The START button will start capturing the joystick messages and the STOP
button will stop.
The MODE buttons allow you to set either digital or analog mode
The NOTIFY check tells the component to only send a message when the
joystick has been changed (movement or buttons). If this is unchecked,
a constant polling will occur sending messages to the OnXXX events at a
set interval.
This interval is specified by the Polling trackbar. This is the number of
milliseconds between notifications -- kind of like a speed setting ???
The Thresh trackbar is the Threshold setting. This value indicates the
distance the joystick must move before a notifcation is sent.
The code for this demo is long but most of it deals with enabling/disabling
options and controlling the color of the panels.
The joystick is not that complicated, once you play with it a couple times.
Jeff Kurtz
}
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
DGCJoy, StdCtrls, ExtCtrls, ComCtrls;
type
TForm1 = class(TForm)
Panel1: TPanel;
Panel2: TPanel;
Panel3: TPanel;
Panel4: TPanel;
Panel5: TPanel;
Panel6: TPanel;
Panel7: TPanel;
Panel8: TPanel;
Panel9: TPanel;
Panel10: TPanel;
Panel11: TPanel;
Panel12: TPanel;
Panel13: TPanel;
DGCJoystick1: TDGCJoystick;
Button1: TButton;
Button2: TButton;
RadioGroup1: TRadioGroup;
RadioButton1: TRadioButton;
RadioButton2: TRadioButton;
Panel14: TPanel;
CheckBox1: TCheckBox;
TrackBar1: TTrackBar;
TrackBar2: TTrackBar;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
procedure DGCJoystick1Joy1BtnDown(B1, B2, B3, B4: Boolean);
procedure DGCJoystick1Joy1BtnUp(B1, B2, B3, B4: Boolean);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure DGCJoystick1Joy1Center(Sender: TObject);
procedure DGCJoystick1Joy1Down(Sender: TObject);
procedure DGCJoystick1Joy1DownLeft(Sender: TObject);
procedure DGCJoystick1Joy1DownRight(Sender: TObject);
procedure DGCJoystick1Joy1Left(Sender: TObject);
procedure DGCJoystick1Joy1Right(Sender: TObject);
procedure DGCJoystick1Joy1Up(Sender: TObject);
procedure DGCJoystick1Joy1UpLeft(Sender: TObject);
procedure DGCJoystick1Joy1UpRight(Sender: TObject);
procedure RadioButton1Click(Sender: TObject);
procedure RadioButton2Click(Sender: TObject);
procedure CheckBox1Click(Sender: TObject);
procedure FormActivate(Sender: TObject);
procedure TrackBar1Change(Sender: TObject);
procedure TrackBar2Change(Sender: TObject);
procedure DGCJoystick1Joy1AnMove(XPos, YPos, ZPos: Integer);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
//The distance that the joystick must move in order to move the '+' in
//analog mode
XRange, YRange : Integer;
implementation
{$R *.DFM}
procedure TForm1.DGCJoystick1Joy1BtnDown(B1, B2, B3, B4: Boolean);
begin
// If the appropriate button is pressed, change the panels color.
// NOTE: The buttons return as boolean values. If the button
// is pressed then it's value will be TRUE
If B1 then Panel10.Color := clRed;
If B2 then Panel11.Color := clRed;
If B3 then Panel12.Color := clRed;
if B4 then Panel13.Color := clRed;
end;
procedure TForm1.DGCJoystick1Joy1BtnUp(B1, B2, B3, B4: Boolean);
begin
// If the appropriate button was released, change the color back.
// NOTE: The buttons return as boolean values. If the button was
// released, then it's value will be TRUE
If B1 then Panel10.Color := clMaroon;
If B2 then Panel11.Color := clMaroon;
If B3 then Panel12.Color := clMaroon;
if B4 then Panel13.Color := clMaroon;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
//Disable setup fields
RadioGroup1.Enabled := False;
RadioButton1.Enabled := False;
RadioButton2.Enabled := False;
CheckBox1.Enabled := False;
Button1.Enabled := False;
Button2.Enabled := True;
Trackbar1.Enabled := False;
Trackbar2.Enabled := False;
//Start capturing joystick messages
DGCJoystick1.StartJoysticks;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
//Stop capturing Joystick Messages
DGCJoystick1.StopJoysticks;
//Enable setup fields
RadioGroup1.Enabled := True;
RadioButton1.Enabled := True;
RadioButton2.Enabled := True;
CheckBox1.Enabled := True;
Button1.Enabled := True;
Button2.Enabled := False;
Trackbar1.Enabled := True;
Trackbar2.Enabled := True;
end;
procedure TForm1.DGCJoystick1Joy1Center(Sender: TObject);
begin
//If the Joystick is Centered (DIGITAL ONLY)
Panel1.Color := clNavy;
Panel2.Color := clNavy;
Panel3.Color := clNavy;
Panel4.Color := clNavy;
Panel5.Color := clBlue; //Center Panel
Panel6.Color := clNavy;
Panel7.Color := clNavy;
Panel8.Color := clNavy;
Panel9.Color := clNavy;
end;
procedure TForm1.DGCJoystick1Joy1Down(Sender: TObject);
begin
//If the Joystick is Down (DIGITAL ONLY)
Panel1.Color := clNavy;
Panel2.Color := clNavy;
Panel3.Color := clNavy;
Panel4.Color := clNavy;
Panel5.Color := clNavy;
Panel6.Color := clNavy;
Panel7.Color := clNavy;
Panel8.Color := clBlue; //Down Panel
Panel9.Color := clNavy;
end;
procedure TForm1.DGCJoystick1Joy1DownLeft(Sender: TObject);
begin
//If the Joystick is Down and Left (DIGITAL ONLY)
Panel1.Color := clNavy;
Panel2.Color := clNavy;
Panel3.Color := clNavy;
Panel4.Color := clNavy;
Panel5.Color := clNavy;
Panel6.Color := clNavy;
Panel7.Color := clBlue; //Down-Left Panel
Panel8.Color := clNavy;
Panel9.Color := clNavy;
end;
procedure TForm1.DGCJoystick1Joy1DownRight(Sender: TObject);
begin
//If the Joystick Down and Right (DIGITAL ONLY)
Panel1.Color := clNavy;
Panel2.Color := clNavy;
Panel3.Color := clNavy;
Panel4.Color := clNavy;
Panel5.Color := clNavy;
Panel6.Color := clNavy;
Panel7.Color := clNavy;
Panel8.Color := clNavy;
Panel9.Color := clBlue; //Down-Right Panel
end;
procedure TForm1.DGCJoystick1Joy1Left(Sender: TObject);
begin
//If the Joystick is Left (DIGITAL ONLY)
Panel1.Color := clNavy;
Panel2.Color := clNavy;
Panel3.Color := clNavy;
Panel4.Color := clBlue; //Left Panel
Panel5.Color := clNavy;
Panel6.Color := clNavy;
Panel7.Color := clNavy;
Panel8.Color := clNavy;
Panel9.Color := clNavy;
end;
procedure TForm1.DGCJoystick1Joy1Right(Sender: TObject);
begin
//If the Joystick is Right (DIGITAL ONLY)
Panel1.Color := clNavy;
Panel2.Color := clNavy;
Panel3.Color := clNavy;
Panel4.Color := clNavy;
Panel5.Color := clNavy;
Panel6.Color := clBlue; //Right Panel
Panel7.Color := clNavy;
Panel8.Color := clNavy;
Panel9.Color := clNavy;
end;
procedure TForm1.DGCJoystick1Joy1Up(Sender: TObject);
begin
//If the Joystick is Up (DIGITAL ONLY)
Panel1.Color := clNavy;
Panel2.Color := clBlue; //Up Panel
Panel3.Color := clNavy;
Panel4.Color := clNavy;
Panel5.Color := clNavy;
Panel6.Color := clNavy;
Panel7.Color := clNavy;
Panel8.Color := clNavy;
Panel9.Color := clNavy;
end;
procedure TForm1.DGCJoystick1Joy1UpLeft(Sender: TObject);
begin
//If the Joystick Up and Left (DIGITAL ONLY)
Panel1.Color := clBlue; //Up-Left Panel
Panel2.Color := clNavy;
Panel3.Color := clNavy;
Panel4.Color := clNavy;
Panel5.Color := clNavy;
Panel6.Color := clNavy;
Panel7.Color := clNavy;
Panel8.Color := clNavy;
Panel9.Color := clNavy;
end;
procedure TForm1.DGCJoystick1Joy1UpRight(Sender: TObject);
begin
//If the Joystick is Up and Right (DIGITAL ONLY)
Panel1.Color := clNavy;
Panel2.Color := clNavy;
Panel3.Color := clBlue; //Up-Right Panel
Panel4.Color := clNavy;
Panel5.Color := clNavy;
Panel6.Color := clNavy;
Panel7.Color := clNavy;
Panel8.Color := clNavy;
Panel9.Color := clNavy;
end;
procedure TForm1.RadioButton1Click(Sender: TObject);
begin
// This sets the joystick to Digital Mode
RadioButton2.Checked := False;
RadioButton1.Checked := True;
Panel14.Hide;
DGCJoystick1.Joy1Mode := Digital;
end;
procedure TForm1.RadioButton2Click(Sender: TObject);
begin
// This sets the Joystick to Analog Mode
RadioButton1.Checked := False;
RadioButton2.Checked := True;
Panel14.Show;
Panel14.BringToFront;
Label5.Left := 40;
Label5.Top := 37;
DGCJoystick1.Joy1Mode := Analog;
end;
procedure TForm1.CheckBox1Click(Sender: TObject);
begin
// This toggles the Notify property. If the value is True, notify the
// program when changes occur. If False, use polling frequency intervals
If CheckBox1.Checked then
DGCJoystick1.Joy1Notify := True
else
DGCJoystick1.Joy1Notify := False;
end;
procedure TForm1.FormActivate(Sender: TObject);
begin
//Start of the program -- take care of some housekeeping
//Set the Min and Max polling frequencies for the joystick
//This info is available in the joystick capabilites
TrackBar2.Min := DGCJoystick1.Joy1Caps.wPeriodMin;
TrackBar2.Max := DGCJoystick1.Joy1Caps.wPeriodMax;
TrackBar2.Position := DGCJoystick1.Joy1Polling;
Label3.Caption := IntToStr(Trackbar2.Position);
//Set the Threshold min & max values. This is pretty much open to you
//just remember, the less the threshold, the less the movement on the
//joystick is needed to send a movement message. Threshold will be
//ignored in Digital Mode!
Trackbar1.Min := 0;
Trackbar1.Max := 1000;
Trackbar1.Position := DGCJoystick1.Joy1Thresh;
Label4.Caption := IntToStr(Trackbar1.Position);
//Our analog display panel (the one with the '+' on it) is approx 80 pixels
//by 80 Pixels. To get the '+' sign to move in the entire panel, we need to
//find out the distance the joystick must move for each pixel. For example
//if Max is 65535 and Min is 0 and your display area is 80 pixels accross,
//the calculation would turn out around 820. We use this value when we
//calculate the position of the joystick relative to the display area
XRange := (DGCJoystick1.Joy1Caps.wXMax - DGCJoystick1.Joy1Caps.wXMin) div 80;
YRange := (DGCJoystick1.Joy1Caps.wYMax - DGCJoystick1.Joy1Caps.wYMin) div 77;
Panel5.Color := clBlue;
end;
procedure TForm1.TrackBar1Change(Sender: TObject);
begin
//Adjust the Threshold value
Label4.Caption := IntToStr(Trackbar1.position);
DGCJoystick1.Joy1Thresh := Trackbar1.position;
end;
procedure TForm1.TrackBar2Change(Sender: TObject);
begin
//Adjust the Polling frequency
Label3.Caption := IntToStr(Trackbar2.position);
DGCJoystick1.Joy1Polling := Trackbar2.position;
end;
procedure TForm1.DGCJoystick1Joy1AnMove(XPos, YPos, ZPos: Integer);
begin
//When in Analog mode, movements are sent here. You can work with
// the X,Y, and Z axis.
//To place the '+' in the panel, we take the X Position of the joystick
//and divide it by the range of motion calculate earlier. This little
//division will place the '+' somwhere within the panel. For this example
//program the width of the display area is 80, the XRange is approx 820
//and let's say that the joystick is centered Which would most likely
//return 32768 (in that area). Now take 32768 and divide it but the Range
//which is aprox 820, that will give you the value between 0-80 and since
//the joystick is at 32768, that would give you around 40.
Label5.Left := XPos div XRange;
Label5.Top := YPos div YRange;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -