]>
git.saurik.com Git - wxWidgets.git/blob - samples/joytest/joytest.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Joystick sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
24 #error You must set wxUSE_JOYSTICK to 1 in setup.h!
28 #include "wx/joystick.h"
32 MyFrame
*frame
= NULL
;
36 // For drawing lines in a canvas
42 // Initialise this in OnInit, not statically
43 bool MyApp::OnInit(void)
45 wxJoystick
stick(wxJOYSTICK1
);
48 wxMessageBox("No joystick detected!");
52 m_fire
.Create("gun.wav");
55 m_maxX
= stick
.GetXMax();
56 m_maxY
= stick
.GetYMax();
58 // Create the main frame window
60 frame
= new MyFrame(NULL
, "Joystick Demo", wxPoint(0, 0), wxSize(500, 400),
61 wxDEFAULT_FRAME_STYLE
| wxHSCROLL
| wxVSCROLL
);
63 // Give it an icon (this is ignored in MDI mode: uses resources)
65 frame
->SetIcon(wxIcon("joyicon"));
68 frame
->SetIcon(wxIcon("joyicon.xbm"));
72 wxMenu
*file_menu
= new wxMenu
;
74 file_menu
->Append(JOYTEST_QUIT
, "&Exit");
76 wxMenu
*help_menu
= new wxMenu
;
77 help_menu
->Append(JOYTEST_ABOUT
, "&About");
79 wxMenuBar
*menu_bar
= new wxMenuBar
;
81 menu_bar
->Append(file_menu
, "&File");
82 menu_bar
->Append(help_menu
, "&Help");
84 // Associate the menu bar with the frame
85 frame
->SetMenuBar(menu_bar
);
87 frame
->CreateStatusBar();
96 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
97 EVT_JOYSTICK_EVENTS(MyCanvas::OnJoystickEvent
)
100 // Define a constructor for my canvas
101 MyCanvas::MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
):
102 wxScrolledWindow(parent
, -1, pos
, size
, wxSUNKEN_BORDER
)
104 wxJoystick
joystick(wxJOYSTICK1
);
105 joystick
.SetCapture(this);
108 MyCanvas::~MyCanvas(void)
110 wxJoystick
joystick(wxJOYSTICK1
);
111 joystick
.ReleaseCapture();
114 void MyCanvas::OnJoystickEvent(wxJoystickEvent
& event
)
118 wxPoint
pt(event
.GetPosition());
120 // Scale to canvas size
124 pt
.x
= (long) (((double)pt
.x
/(double)wxGetApp().m_maxX
) * cw
);
125 pt
.y
= (long) (((double)pt
.y
/(double)wxGetApp().m_maxY
) * ch
);
127 if (xpos
> -1 && ypos
> -1 && event
.IsMove() && event
.ButtonIsDown())
129 dc
.SetPen(*wxBLACK_PEN
);
130 dc
.DrawLine(xpos
, ypos
, pt
.x
, pt
.y
);
136 if (event
.ButtonDown())
137 sprintf(buf
, "Joystick (%d, %d) Fire!", pt
.x
, pt
.y
);
139 sprintf(buf
, "Joystick (%d, %d)", pt
.x
, pt
.y
);
140 frame
->SetStatusText(buf
);
143 if (event
.ButtonDown() && wxGetApp().m_fire
.IsOk())
145 wxGetApp().m_fire
.Play();
150 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
151 EVT_MENU(JOYTEST_QUIT
, MyFrame::OnQuit
)
154 MyFrame::MyFrame(wxFrame
*parent
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
,
156 wxFrame(parent
, -1, title
, pos
, size
, style
)
158 canvas
= new MyCanvas(this);
161 MyFrame::~MyFrame(void)
165 void MyFrame::OnQuit(wxCommandEvent
& event
)
170 void MyFrame::OnActivate(wxActivateEvent
& event
)
172 if (event
.GetActive() && canvas
)