]> git.saurik.com Git - wxWidgets.git/blob - samples/joytest/joytest.cpp
Standardize Project-Id-Version in PO files.
[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 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // For compilers that support precompilation, includes "wx/wx.h".
12 #include "wx/wxprec.h"
13
14 #ifdef __BORLANDC__
15 #pragma hdrstop
16 #endif
17
18 #ifndef WX_PRECOMP
19 #include "wx/wx.h"
20 #endif
21
22 #if !wxUSE_JOYSTICK
23 # error You must set wxUSE_JOYSTICK to 1 in setup.h
24 #endif
25
26 #include "wx/sound.h"
27 #include "wx/joystick.h"
28
29 #include "joytest.h"
30
31 // the application icon (under Windows and OS/2 it is in resources and even
32 // though we could still include the XPM here it would be unused)
33 #ifndef wxHAS_IMAGES_IN_RESOURCES
34 #include "../sample.xpm"
35 #endif
36
37 MyFrame *frame = NULL;
38
39 IMPLEMENT_APP(MyApp)
40
41 // For drawing lines in a canvas
42 long xpos = -1;
43 long ypos = -1;
44
45 int winNumber = 1;
46
47 int nButtons = 0;
48 // Initialise this in OnInit, not statically
49 bool MyApp::OnInit()
50 {
51 if ( !wxApp::OnInit() )
52 return false;
53
54 wxJoystick stick(wxJOYSTICK1);
55 if (!stick.IsOk())
56 {
57 wxMessageBox(wxT("No joystick detected!"));
58 return false;
59 }
60
61 #if wxUSE_SOUND
62 m_fire.Create(wxT("buttonpress.wav"));
63 #endif // wxUSE_SOUND
64
65 m_minX = stick.GetXMin();
66 m_minY = stick.GetYMin();
67 m_maxX = stick.GetXMax();
68 m_maxY = stick.GetYMax();
69
70 // Create the main frame window
71
72 frame = new MyFrame(NULL, wxT("Joystick Demo"), wxDefaultPosition,
73 wxSize(500, 400), wxDEFAULT_FRAME_STYLE | wxHSCROLL | wxVSCROLL);
74
75 frame->SetIcon(wxICON(sample));
76
77 // Make a menubar
78 wxMenu *file_menu = new wxMenu;
79
80 file_menu->Append(JOYTEST_QUIT, wxT("&Exit"));
81
82 wxMenuBar *menu_bar = new wxMenuBar;
83
84 menu_bar->Append(file_menu, wxT("&File"));
85
86 // Associate the menu bar with the frame
87 frame->SetMenuBar(menu_bar);
88
89 #if wxUSE_STATUSBAR
90 frame->CreateStatusBar();
91 frame->SetStatusText(wxString::Format(wxT("Device [%s] (PID:[%i] MID:[%i]) Ready... # of joysticks:[%i]"), stick.GetProductName().c_str(), stick.GetProductId(), stick.GetManufacturerId(), wxJoystick::GetNumberJoysticks()));
92 #endif // wxUSE_STATUSBAR
93
94 frame->CenterOnScreen();
95 frame->Show(true);
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, wxID_ANY, pos, size, wxSUNKEN_BORDER)
107 {
108 m_stick = new wxJoystick(wxJOYSTICK1);
109 nButtons = m_stick->GetNumberButtons();
110 m_stick->SetCapture(this, 10);
111 }
112
113 MyCanvas::~MyCanvas()
114 {
115 m_stick->ReleaseCapture();
116 delete m_stick;
117 }
118
119 void MyCanvas::OnJoystickEvent(wxJoystickEvent& event)
120 {
121 // We don't have valid (x, y) coordinates for z-move events.
122 if ( !event.IsZMove() )
123 {
124 wxClientDC dc(this);
125
126 wxPoint pt(event.GetPosition());
127
128 // if negative positions are possible then shift everything up
129 int xmin = wxGetApp().m_minX;
130 int xmax = wxGetApp().m_maxX;
131 int ymin = wxGetApp().m_minY;
132 int ymax = wxGetApp().m_maxY;
133 if (xmin < 0) {
134 xmax += abs(xmin);
135 pt.x += abs(xmin);
136 }
137 if (ymin < 0) {
138 ymax += abs(ymin);
139 pt.y += abs(ymin);
140 }
141
142 // Scale to canvas size
143 int cw, ch;
144 GetSize(&cw, &ch);
145
146 pt.x = (long) (((double)pt.x/(double)xmax) * cw);
147 pt.y = (long) (((double)pt.y/(double)ymax) * ch);
148
149 if (xpos > -1 && ypos > -1 && event.IsMove() && event.ButtonIsDown())
150 {
151 dc.SetPen(*wxBLACK_PEN);
152 dc.DrawLine(xpos, ypos, pt.x, pt.y);
153 }
154
155 xpos = pt.x;
156 ypos = pt.y;
157 }
158
159 #if wxUSE_STATUSBAR
160 wxString buf;
161 if (event.ButtonDown())
162 buf.Printf(wxT("Joystick (%d, %d) #%i Fire!"), xpos, ypos, event.GetButtonChange());
163 else
164 buf.Printf(wxT("Joystick (%d, %d) "), xpos, ypos);
165
166 /*
167 for(int i = 0; i < nButtons; ++i)
168 {
169 buf += wxString(wxT("[")) +
170 ((event.GetButtonState() & (1 << i)) ? wxT("Y") : wxT("N")) + wxString(wxT("]"));
171 }
172 */
173
174 frame->SetStatusText(buf);
175 #endif // wxUSE_STATUSBAR
176
177 #if wxUSE_SOUND
178 if (event.ButtonDown() && wxGetApp().m_fire.IsOk())
179 {
180 wxGetApp().m_fire.Play();
181 }
182 #endif // wxUSE_SOUND
183 }
184
185 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
186 EVT_MENU(JOYTEST_QUIT, MyFrame::OnQuit)
187 END_EVENT_TABLE()
188
189 MyFrame::MyFrame(wxFrame *parent, const wxString& title, const wxPoint& pos,
190 const wxSize& size, const long style)
191 : wxFrame(parent, wxID_ANY, title, pos, size, style)
192 {
193 canvas = new MyCanvas(this);
194 }
195
196 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
197 {
198 Close(true);
199 }
200
201 void MyFrame::OnActivate(wxActivateEvent& event)
202 {
203 if (event.GetActive() && canvas)
204 canvas->SetFocus();
205 }