Fixes so joystick not only compiles on Linux, but it actually works too!
[wxWidgets.git] / samples / joytest / joytest.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: joytest.cpp
3 // Purpose: Joystick sample
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #ifndef WX_PRECOMP
20 #include "wx/wx.h"
21 #endif
22
23 #if !wxUSE_JOYSTICK
24 # error You must set wxUSE_JOYSTICK to 1 in setup.h
25 #endif
26
27 #if !wxUSE_STATUSBAR
28 # error You must set wxUSE_STATUSBAR to 1 in setup.h
29 #endif
30
31 #include "wx/sound.h"
32 #include "wx/joystick.h"
33
34 #include "joytest.h"
35
36 MyFrame *frame = NULL;
37
38 IMPLEMENT_APP(MyApp)
39
40 // For drawing lines in a canvas
41 long xpos = -1;
42 long ypos = -1;
43
44 int winNumber = 1;
45
46 // Initialise this in OnInit, not statically
47 bool MyApp::OnInit()
48 {
49 wxJoystick stick(wxJOYSTICK1);
50 if (!stick.IsOk())
51 {
52 wxMessageBox(_T("No joystick detected!"));
53 return FALSE;
54 }
55
56 #if wxUSE_SOUND
57 m_fire.Create(_T("gun.wav"));
58 #endif // wxUSE_SOUND
59
60 m_minX = stick.GetXMin();
61 m_minY = stick.GetYMin();
62 m_maxX = stick.GetXMax();
63 m_maxY = stick.GetYMax();
64
65 // Create the main frame window
66
67 frame = new MyFrame(NULL, _T("Joystick Demo"), wxDefaultPosition,
68 wxSize(500, 400), wxDEFAULT_FRAME_STYLE | wxHSCROLL | wxVSCROLL);
69
70 // Give it an icon (this is ignored in MDI mode: uses resources)
71 #ifdef __WXMSW__
72 frame->SetIcon(wxIcon(_T("joyicon")));
73 #endif
74 #ifdef __X__
75 frame->SetIcon(wxIcon(_T("joyicon.xbm")));
76 #endif
77
78 // Make a menubar
79 wxMenu *file_menu = new wxMenu;
80
81 file_menu->Append(JOYTEST_QUIT, _T("&Exit"));
82
83 wxMenuBar *menu_bar = new wxMenuBar;
84
85 menu_bar->Append(file_menu, _T("&File"));
86
87 // Associate the menu bar with the frame
88 frame->SetMenuBar(menu_bar);
89
90 frame->CreateStatusBar();
91
92 frame->CenterOnScreen();
93 frame->Show(TRUE);
94
95 SetTopWindow(frame);
96
97 return TRUE;
98 }
99
100 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
101 EVT_JOYSTICK_EVENTS(MyCanvas::OnJoystickEvent)
102 END_EVENT_TABLE()
103
104 // Define a constructor for my canvas
105 MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size):
106 wxScrolledWindow(parent, -1, pos, size, wxSUNKEN_BORDER)
107 {
108 m_stick = new wxJoystick(wxJOYSTICK1);
109 m_stick->SetCapture(this, 10);
110 }
111
112 MyCanvas::~MyCanvas()
113 {
114 m_stick->ReleaseCapture();
115 delete m_stick;
116 }
117
118 void MyCanvas::OnJoystickEvent(wxJoystickEvent& event)
119 {
120 wxClientDC dc(this);
121
122 wxPoint pt(event.GetPosition());
123
124 // if negative positions are possible then shift everything up
125 int xmin = wxGetApp().m_minX;
126 int xmax = wxGetApp().m_maxX;
127 int ymin = wxGetApp().m_minY;
128 int ymax = wxGetApp().m_maxY;
129 if (xmin < 0) {
130 xmax += abs(xmin);
131 pt.x += abs(xmin);
132 xmin = 0;
133 }
134 if (ymin < 0) {
135 ymax += abs(ymin);
136 pt.y += abs(ymin);
137 ymin = 0;
138 }
139
140 // Scale to canvas size
141 int cw, ch;
142 GetSize(&cw, &ch);
143
144 pt.x = (long) (((double)pt.x/(double)xmax) * cw);
145 pt.y = (long) (((double)pt.y/(double)ymax) * ch);
146
147 if (xpos > -1 && ypos > -1 && event.IsMove() && event.ButtonIsDown())
148 {
149 dc.SetPen(*wxBLACK_PEN);
150 dc.DrawLine(xpos, ypos, pt.x, pt.y);
151 }
152
153 xpos = pt.x;
154 ypos = pt.y;
155
156 wxString buf;
157 if (event.ButtonDown())
158 buf.Printf(_T("Joystick (%d, %d) Fire!"), pt.x, pt.y);
159 else
160 buf.Printf(_T("Joystick (%d, %d)"), pt.x, pt.y);
161
162 frame->SetStatusText(buf);
163
164 #if wxUSE_SOUND
165 if (event.ButtonDown() && wxGetApp().m_fire.IsOk())
166 {
167 wxGetApp().m_fire.Play();
168 }
169 #endif // wxUSE_SOUND
170 }
171
172 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
173 EVT_MENU(JOYTEST_QUIT, MyFrame::OnQuit)
174 END_EVENT_TABLE()
175
176 MyFrame::MyFrame(wxFrame *parent, const wxString& title, const wxPoint& pos,
177 const wxSize& size, const long style)
178 : wxFrame(parent, -1, title, pos, size, style)
179 {
180 canvas = new MyCanvas(this);
181 }
182
183 MyFrame::~MyFrame()
184 {
185 // Empty
186 }
187
188 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
189 {
190 Close(TRUE);
191 }
192
193 void MyFrame::OnActivate(wxActivateEvent& event)
194 {
195 if (event.GetActive() && canvas)
196 canvas->SetFocus();
197 }