Move HID stuff into both OSX builds. Add preliminary joystick for OSX
[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 #include "wx/sound.h"
28 #include "wx/joystick.h"
29
30 #include "joytest.h"
31
32 MyFrame *frame = NULL;
33
34 IMPLEMENT_APP(MyApp)
35
36 // For drawing lines in a canvas
37 long xpos = -1;
38 long ypos = -1;
39
40 int winNumber = 1;
41
42 // Initialise this in OnInit, not statically
43 bool MyApp::OnInit()
44 {
45 wxJoystick stick(wxJOYSTICK1);
46 if (!stick.IsOk())
47 {
48 wxMessageBox(_T("No joystick detected!"));
49 return false;
50 }
51
52 #if wxUSE_SOUND
53 m_fire.Create(_T("gun.wav"));
54 #endif // wxUSE_SOUND
55
56 m_minX = stick.GetXMin();
57 m_minY = stick.GetYMin();
58 m_maxX = stick.GetXMax();
59 m_maxY = stick.GetYMax();
60
61 // Create the main frame window
62
63 frame = new MyFrame(NULL, _T("Joystick Demo"), wxDefaultPosition,
64 wxSize(500, 400), wxDEFAULT_FRAME_STYLE | wxHSCROLL | wxVSCROLL);
65
66 // Give it an icon (this is ignored in MDI mode: uses resources)
67 #ifdef __WXMSW__
68 frame->SetIcon(wxIcon(_T("joyicon")));
69 #endif
70 #ifdef __X__
71 frame->SetIcon(wxIcon(_T("joyicon.xbm")));
72 #endif
73
74 // Make a menubar
75 wxMenu *file_menu = new wxMenu;
76
77 file_menu->Append(JOYTEST_QUIT, _T("&Exit"));
78
79 wxMenuBar *menu_bar = new wxMenuBar;
80
81 menu_bar->Append(file_menu, _T("&File"));
82
83 // Associate the menu bar with the frame
84 frame->SetMenuBar(menu_bar);
85
86 #if wxUSE_STATUSBAR
87 frame->CreateStatusBar();
88 frame->SetStatusText(wxT("Ready..."));
89 #endif // wxUSE_STATUSBAR
90
91 frame->CenterOnScreen();
92 frame->Show(true);
93
94 SetTopWindow(frame);
95
96 return true;
97 }
98
99 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
100 EVT_JOYSTICK_EVENTS(MyCanvas::OnJoystickEvent)
101 END_EVENT_TABLE()
102
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)
106 {
107 m_stick = new wxJoystick(wxJOYSTICK1);
108 m_stick->SetCapture(this, 10);
109 }
110
111 MyCanvas::~MyCanvas()
112 {
113 m_stick->ReleaseCapture();
114 delete m_stick;
115 }
116
117 void MyCanvas::OnJoystickEvent(wxJoystickEvent& event)
118 {
119 wxClientDC dc(this);
120
121 wxPoint pt(event.GetPosition());
122
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;
128 if (xmin < 0) {
129 xmax += abs(xmin);
130 pt.x += abs(xmin);
131 }
132 if (ymin < 0) {
133 ymax += abs(ymin);
134 pt.y += abs(ymin);
135 }
136
137 // Scale to canvas size
138 int cw, ch;
139 GetSize(&cw, &ch);
140
141 pt.x = (long) (((double)pt.x/(double)xmax) * cw);
142 pt.y = (long) (((double)pt.y/(double)ymax) * ch);
143
144 if (xpos > -1 && ypos > -1 && event.IsMove() && event.ButtonIsDown())
145 {
146 dc.SetPen(*wxBLACK_PEN);
147 dc.DrawLine(xpos, ypos, pt.x, pt.y);
148 }
149
150 xpos = pt.x;
151 ypos = pt.y;
152
153 #if wxUSE_STATUSBAR
154 wxString buf;
155 if (event.ButtonDown())
156 buf.Printf(_T("Joystick (%d, %d) Fire!"), pt.x, pt.y);
157 else
158 buf.Printf(_T("Joystick (%d, %d)"), pt.x, pt.y);
159
160 frame->SetStatusText(buf);
161 #endif // wxUSE_STATUSBAR
162
163 #if wxUSE_SOUND
164 if (event.ButtonDown() && wxGetApp().m_fire.IsOk())
165 {
166 wxGetApp().m_fire.Play();
167 }
168 #endif // wxUSE_SOUND
169 }
170
171 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
172 EVT_MENU(JOYTEST_QUIT, MyFrame::OnQuit)
173 END_EVENT_TABLE()
174
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)
178 {
179 canvas = new MyCanvas(this);
180 }
181
182 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
183 {
184 Close(true);
185 }
186
187 void MyFrame::OnActivate(wxActivateEvent& event)
188 {
189 if (event.GetActive() && canvas)
190 canvas->SetFocus();
191 }