]>
Commit | Line | Data |
---|---|---|
bbf1f0e5 KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: joytest.cpp | |
3 | // Purpose: Joystick sample | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
6aa89a22 | 7 | // Copyright: (c) Julian Smart |
526954c5 | 8 | // Licence: wxWindows licence |
bbf1f0e5 KB |
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 | ||
9832ce0b | 22 | #if !wxUSE_JOYSTICK |
aec18ff7 MB |
23 | # error You must set wxUSE_JOYSTICK to 1 in setup.h |
24 | #endif | |
25 | ||
d93966b9 | 26 | #include "wx/sound.h" |
9832ce0b | 27 | #include "wx/joystick.h" |
bbf1f0e5 KB |
28 | |
29 | #include "joytest.h" | |
30 | ||
807902f1 PC |
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) | |
e7092398 | 33 | #ifndef wxHAS_IMAGES_IN_RESOURCES |
807902f1 PC |
34 | #include "../sample.xpm" |
35 | #endif | |
36 | ||
bbf1f0e5 KB |
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 | ||
65442ab6 | 47 | int nButtons = 0; |
bbf1f0e5 | 48 | // Initialise this in OnInit, not statically |
aec18ff7 | 49 | bool MyApp::OnInit() |
bbf1f0e5 | 50 | { |
45e6e6f8 VZ |
51 | if ( !wxApp::OnInit() ) |
52 | return false; | |
53 | ||
aec18ff7 MB |
54 | wxJoystick stick(wxJOYSTICK1); |
55 | if (!stick.IsOk()) | |
56 | { | |
9a83f860 | 57 | wxMessageBox(wxT("No joystick detected!")); |
45cbbbb3 | 58 | return false; |
aec18ff7 MB |
59 | } |
60 | ||
d93966b9 | 61 | #if wxUSE_SOUND |
9a83f860 | 62 | m_fire.Create(wxT("buttonpress.wav")); |
d93966b9 | 63 | #endif // wxUSE_SOUND |
bbf1f0e5 | 64 | |
a800dc50 RD |
65 | m_minX = stick.GetXMin(); |
66 | m_minY = stick.GetYMin(); | |
aec18ff7 MB |
67 | m_maxX = stick.GetXMax(); |
68 | m_maxY = stick.GetYMax(); | |
bbf1f0e5 | 69 | |
aec18ff7 | 70 | // Create the main frame window |
bbf1f0e5 | 71 | |
9a83f860 | 72 | frame = new MyFrame(NULL, wxT("Joystick Demo"), wxDefaultPosition, |
aec18ff7 | 73 | wxSize(500, 400), wxDEFAULT_FRAME_STYLE | wxHSCROLL | wxVSCROLL); |
bbf1f0e5 | 74 | |
3cb332c1 | 75 | frame->SetIcon(wxICON(sample)); |
bbf1f0e5 | 76 | |
aec18ff7 MB |
77 | // Make a menubar |
78 | wxMenu *file_menu = new wxMenu; | |
bbf1f0e5 | 79 | |
9a83f860 | 80 | file_menu->Append(JOYTEST_QUIT, wxT("&Exit")); |
bbf1f0e5 | 81 | |
aec18ff7 | 82 | wxMenuBar *menu_bar = new wxMenuBar; |
bbf1f0e5 | 83 | |
9a83f860 | 84 | menu_bar->Append(file_menu, wxT("&File")); |
bbf1f0e5 | 85 | |
aec18ff7 MB |
86 | // Associate the menu bar with the frame |
87 | frame->SetMenuBar(menu_bar); | |
bbf1f0e5 | 88 | |
8520f137 | 89 | #if wxUSE_STATUSBAR |
aec18ff7 | 90 | frame->CreateStatusBar(); |
da9e9563 | 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())); |
8520f137 | 92 | #endif // wxUSE_STATUSBAR |
bbf1f0e5 | 93 | |
aec18ff7 | 94 | frame->CenterOnScreen(); |
45cbbbb3 | 95 | frame->Show(true); |
bbf1f0e5 | 96 | |
45cbbbb3 | 97 | return true; |
bbf1f0e5 KB |
98 | } |
99 | ||
100 | BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow) | |
2f6c54eb | 101 | EVT_JOYSTICK_EVENTS(MyCanvas::OnJoystickEvent) |
bbf1f0e5 KB |
102 | END_EVENT_TABLE() |
103 | ||
104 | // Define a constructor for my canvas | |
105 | MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size): | |
45cbbbb3 | 106 | wxScrolledWindow(parent, wxID_ANY, pos, size, wxSUNKEN_BORDER) |
bbf1f0e5 | 107 | { |
a800dc50 | 108 | m_stick = new wxJoystick(wxJOYSTICK1); |
65442ab6 | 109 | nButtons = m_stick->GetNumberButtons(); |
a800dc50 | 110 | m_stick->SetCapture(this, 10); |
bbf1f0e5 KB |
111 | } |
112 | ||
aec18ff7 | 113 | MyCanvas::~MyCanvas() |
bbf1f0e5 | 114 | { |
a800dc50 RD |
115 | m_stick->ReleaseCapture(); |
116 | delete m_stick; | |
bbf1f0e5 KB |
117 | } |
118 | ||
119 | void MyCanvas::OnJoystickEvent(wxJoystickEvent& event) | |
120 | { | |
3b2f80c2 VZ |
121 | // We don't have valid (x, y) coordinates for z-move events. |
122 | if ( !event.IsZMove() ) | |
aec18ff7 | 123 | { |
3b2f80c2 VZ |
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 | } | |
bbf1f0e5 | 154 | |
22f772f8 VZ |
155 | xpos = pt.x; |
156 | ypos = pt.y; | |
157 | } | |
bbf1f0e5 | 158 | |
8520f137 | 159 | #if wxUSE_STATUSBAR |
600683ca | 160 | wxString buf; |
aec18ff7 | 161 | if (event.ButtonDown()) |
3b2f80c2 | 162 | buf.Printf(wxT("Joystick (%d, %d) #%i Fire!"), xpos, ypos, event.GetButtonChange()); |
aec18ff7 | 163 | else |
3b2f80c2 | 164 | buf.Printf(wxT("Joystick (%d, %d) "), xpos, ypos); |
bbf1f0e5 | 165 | |
65442ab6 RN |
166 | /* |
167 | for(int i = 0; i < nButtons; ++i) | |
168 | { | |
196785e6 | 169 | buf += wxString(wxT("[")) + |
65442ab6 RN |
170 | ((event.GetButtonState() & (1 << i)) ? wxT("Y") : wxT("N")) + wxString(wxT("]")); |
171 | } | |
172 | */ | |
196785e6 | 173 | |
aec18ff7 | 174 | frame->SetStatusText(buf); |
8520f137 | 175 | #endif // wxUSE_STATUSBAR |
bbf1f0e5 | 176 | |
d93966b9 | 177 | #if wxUSE_SOUND |
aec18ff7 MB |
178 | if (event.ButtonDown() && wxGetApp().m_fire.IsOk()) |
179 | { | |
180 | wxGetApp().m_fire.Play(); | |
181 | } | |
d93966b9 | 182 | #endif // wxUSE_SOUND |
bbf1f0e5 KB |
183 | } |
184 | ||
185 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
aec18ff7 | 186 | EVT_MENU(JOYTEST_QUIT, MyFrame::OnQuit) |
bbf1f0e5 KB |
187 | END_EVENT_TABLE() |
188 | ||
aec18ff7 MB |
189 | MyFrame::MyFrame(wxFrame *parent, const wxString& title, const wxPoint& pos, |
190 | const wxSize& size, const long style) | |
45cbbbb3 | 191 | : wxFrame(parent, wxID_ANY, title, pos, size, style) |
bbf1f0e5 | 192 | { |
aec18ff7 | 193 | canvas = new MyCanvas(this); |
bbf1f0e5 KB |
194 | } |
195 | ||
87728739 | 196 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) |
bbf1f0e5 | 197 | { |
45cbbbb3 | 198 | Close(true); |
bbf1f0e5 KB |
199 | } |
200 | ||
201 | void MyFrame::OnActivate(wxActivateEvent& event) | |
202 | { | |
aec18ff7 MB |
203 | if (event.GetActive() && canvas) |
204 | canvas->SetFocus(); | |
bbf1f0e5 | 205 | } |