1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Joystick sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
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
45 wxJoystick
stick(wxJOYSTICK1
);
48 wxMessageBox(_T("No joystick detected!"));
53 m_fire
.Create(_T("gun.wav"));
56 m_minX
= stick
.GetXMin();
57 m_minY
= stick
.GetYMin();
58 m_maxX
= stick
.GetXMax();
59 m_maxY
= stick
.GetYMax();
61 // Create the main frame window
63 frame
= new MyFrame(NULL
, _T("Joystick Demo"), wxDefaultPosition
,
64 wxSize(500, 400), wxDEFAULT_FRAME_STYLE
| wxHSCROLL
| wxVSCROLL
);
66 // Give it an icon (this is ignored in MDI mode: uses resources)
68 frame
->SetIcon(wxIcon(_T("joyicon")));
71 frame
->SetIcon(wxIcon(_T("joyicon.xbm")));
75 wxMenu
*file_menu
= new wxMenu
;
77 file_menu
->Append(JOYTEST_QUIT
, _T("&Exit"));
79 wxMenuBar
*menu_bar
= new wxMenuBar
;
81 menu_bar
->Append(file_menu
, _T("&File"));
83 // Associate the menu bar with the frame
84 frame
->SetMenuBar(menu_bar
);
87 frame
->CreateStatusBar();
88 frame
->SetStatusText(wxT("Ready..."));
89 #endif // wxUSE_STATUSBAR
91 frame
->CenterOnScreen();
99 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
100 EVT_JOYSTICK_EVENTS(MyCanvas
::OnJoystickEvent
)
103 // Define a constructor for my canvas
104 MyCanvas
::MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
):
105 wxScrolledWindow(parent
, wxID_ANY
, pos
, size
, wxSUNKEN_BORDER
)
107 m_stick
= new wxJoystick(wxJOYSTICK1
);
108 m_stick
->SetCapture(this, 10);
111 MyCanvas
::~MyCanvas()
113 m_stick
->ReleaseCapture();
117 void MyCanvas
::OnJoystickEvent(wxJoystickEvent
& event
)
121 wxPoint
pt(event
.GetPosition());
123 // if negative positions are possible then shift everything up
124 int xmin
= wxGetApp().m_minX
;
125 int xmax
= wxGetApp().m_maxX
;
126 int ymin
= wxGetApp().m_minY
;
127 int ymax
= wxGetApp().m_maxY
;
137 // Scale to canvas size
141 pt
.x
= (long) (((double)pt
.x
/(double)xmax
) * cw
);
142 pt
.y
= (long) (((double)pt
.y
/(double)ymax
) * ch
);
144 if (xpos
> -1 && ypos
> -1 && event
.IsMove() && event
.ButtonIsDown())
146 dc
.SetPen(*wxBLACK_PEN
);
147 dc
.DrawLine(xpos
, ypos
, pt
.x
, pt
.y
);
155 if (event
.ButtonDown())
156 buf
.Printf(_T("Joystick (%d, %d) Fire!"), pt
.x
, pt
.y
);
158 buf
.Printf(_T("Joystick (%d, %d)"), pt
.x
, pt
.y
);
160 frame
->SetStatusText(buf
);
161 #endif // wxUSE_STATUSBAR
164 if (event
.ButtonDown() && wxGetApp().m_fire
.IsOk())
166 wxGetApp().m_fire
.Play();
168 #endif // wxUSE_SOUND
171 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
172 EVT_MENU(JOYTEST_QUIT
, MyFrame
::OnQuit
)
175 MyFrame
::MyFrame(wxFrame
*parent
, const wxString
& title
, const wxPoint
& pos
,
176 const wxSize
& size
, const long style
)
177 : wxFrame(parent
, wxID_ANY
, title
, pos
, size
, style
)
179 canvas
= new MyCanvas(this);
182 void MyFrame
::OnQuit(wxCommandEvent
& WXUNUSED(event
))
187 void MyFrame
::OnActivate(wxActivateEvent
& event
)
189 if (event
.GetActive() && canvas
)