]>
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 | |
7 | // RCS-ID: $Id$ | |
6aa89a22 | 8 | // Copyright: (c) Julian Smart |
2f6c54eb | 9 | // Licence: wxWindows license |
bbf1f0e5 KB |
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 | ||
9832ce0b | 23 | #if !wxUSE_JOYSTICK |
aec18ff7 MB |
24 | # error You must set wxUSE_JOYSTICK to 1 in setup.h |
25 | #endif | |
26 | ||
d93966b9 | 27 | #include "wx/sound.h" |
9832ce0b | 28 | #include "wx/joystick.h" |
bbf1f0e5 KB |
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 | ||
65442ab6 | 42 | int nButtons = 0; |
bbf1f0e5 | 43 | // Initialise this in OnInit, not statically |
aec18ff7 | 44 | bool MyApp::OnInit() |
bbf1f0e5 | 45 | { |
45e6e6f8 VZ |
46 | if ( !wxApp::OnInit() ) |
47 | return false; | |
48 | ||
aec18ff7 MB |
49 | wxJoystick stick(wxJOYSTICK1); |
50 | if (!stick.IsOk()) | |
51 | { | |
600683ca | 52 | wxMessageBox(_T("No joystick detected!")); |
45cbbbb3 | 53 | return false; |
aec18ff7 MB |
54 | } |
55 | ||
d93966b9 | 56 | #if wxUSE_SOUND |
196785e6 | 57 | m_fire.Create(_T("buttonpress.wav")); |
d93966b9 | 58 | #endif // wxUSE_SOUND |
bbf1f0e5 | 59 | |
a800dc50 RD |
60 | m_minX = stick.GetXMin(); |
61 | m_minY = stick.GetYMin(); | |
aec18ff7 MB |
62 | m_maxX = stick.GetXMax(); |
63 | m_maxY = stick.GetYMax(); | |
bbf1f0e5 | 64 | |
aec18ff7 | 65 | // Create the main frame window |
bbf1f0e5 | 66 | |
600683ca | 67 | frame = new MyFrame(NULL, _T("Joystick Demo"), wxDefaultPosition, |
aec18ff7 | 68 | wxSize(500, 400), wxDEFAULT_FRAME_STYLE | wxHSCROLL | wxVSCROLL); |
bbf1f0e5 KB |
69 | |
70 | // Give it an icon (this is ignored in MDI mode: uses resources) | |
2049ba38 | 71 | #ifdef __WXMSW__ |
600683ca | 72 | frame->SetIcon(wxIcon(_T("joyicon"))); |
bbf1f0e5 KB |
73 | #endif |
74 | #ifdef __X__ | |
600683ca | 75 | frame->SetIcon(wxIcon(_T("joyicon.xbm"))); |
bbf1f0e5 KB |
76 | #endif |
77 | ||
aec18ff7 MB |
78 | // Make a menubar |
79 | wxMenu *file_menu = new wxMenu; | |
bbf1f0e5 | 80 | |
600683ca | 81 | file_menu->Append(JOYTEST_QUIT, _T("&Exit")); |
bbf1f0e5 | 82 | |
aec18ff7 | 83 | wxMenuBar *menu_bar = new wxMenuBar; |
bbf1f0e5 | 84 | |
600683ca | 85 | menu_bar->Append(file_menu, _T("&File")); |
bbf1f0e5 | 86 | |
aec18ff7 MB |
87 | // Associate the menu bar with the frame |
88 | frame->SetMenuBar(menu_bar); | |
bbf1f0e5 | 89 | |
8520f137 | 90 | #if wxUSE_STATUSBAR |
aec18ff7 | 91 | frame->CreateStatusBar(); |
da9e9563 | 92 | 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 | 93 | #endif // wxUSE_STATUSBAR |
bbf1f0e5 | 94 | |
aec18ff7 | 95 | frame->CenterOnScreen(); |
45cbbbb3 | 96 | frame->Show(true); |
bbf1f0e5 | 97 | |
aec18ff7 | 98 | SetTopWindow(frame); |
bbf1f0e5 | 99 | |
45cbbbb3 | 100 | return true; |
bbf1f0e5 KB |
101 | } |
102 | ||
103 | BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow) | |
2f6c54eb | 104 | EVT_JOYSTICK_EVENTS(MyCanvas::OnJoystickEvent) |
bbf1f0e5 KB |
105 | END_EVENT_TABLE() |
106 | ||
107 | // Define a constructor for my canvas | |
108 | MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size): | |
45cbbbb3 | 109 | wxScrolledWindow(parent, wxID_ANY, pos, size, wxSUNKEN_BORDER) |
bbf1f0e5 | 110 | { |
a800dc50 | 111 | m_stick = new wxJoystick(wxJOYSTICK1); |
65442ab6 | 112 | nButtons = m_stick->GetNumberButtons(); |
a800dc50 | 113 | m_stick->SetCapture(this, 10); |
bbf1f0e5 KB |
114 | } |
115 | ||
aec18ff7 | 116 | MyCanvas::~MyCanvas() |
bbf1f0e5 | 117 | { |
a800dc50 RD |
118 | m_stick->ReleaseCapture(); |
119 | delete m_stick; | |
bbf1f0e5 KB |
120 | } |
121 | ||
122 | void MyCanvas::OnJoystickEvent(wxJoystickEvent& event) | |
123 | { | |
aec18ff7 MB |
124 | wxClientDC dc(this); |
125 | ||
126 | wxPoint pt(event.GetPosition()); | |
127 | ||
a800dc50 RD |
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); | |
a800dc50 RD |
136 | } |
137 | if (ymin < 0) { | |
138 | ymax += abs(ymin); | |
139 | pt.y += abs(ymin); | |
a800dc50 | 140 | } |
925e9792 | 141 | |
aec18ff7 MB |
142 | // Scale to canvas size |
143 | int cw, ch; | |
144 | GetSize(&cw, &ch); | |
bbf1f0e5 | 145 | |
a800dc50 RD |
146 | pt.x = (long) (((double)pt.x/(double)xmax) * cw); |
147 | pt.y = (long) (((double)pt.y/(double)ymax) * ch); | |
bbf1f0e5 | 148 | |
aec18ff7 MB |
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 | |
aec18ff7 MB |
155 | xpos = pt.x; |
156 | ypos = pt.y; | |
bbf1f0e5 | 157 | |
8520f137 | 158 | #if wxUSE_STATUSBAR |
600683ca | 159 | wxString buf; |
aec18ff7 | 160 | if (event.ButtonDown()) |
65442ab6 | 161 | buf.Printf(_T("Joystick (%d, %d) #%i Fire!"), pt.x, pt.y, event.GetButtonChange()); |
aec18ff7 | 162 | else |
65442ab6 | 163 | buf.Printf(_T("Joystick (%d, %d) "), pt.x, pt.y); |
bbf1f0e5 | 164 | |
65442ab6 RN |
165 | /* |
166 | for(int i = 0; i < nButtons; ++i) | |
167 | { | |
196785e6 | 168 | buf += wxString(wxT("[")) + |
65442ab6 RN |
169 | ((event.GetButtonState() & (1 << i)) ? wxT("Y") : wxT("N")) + wxString(wxT("]")); |
170 | } | |
171 | */ | |
196785e6 | 172 | |
aec18ff7 | 173 | frame->SetStatusText(buf); |
8520f137 | 174 | #endif // wxUSE_STATUSBAR |
bbf1f0e5 | 175 | |
d93966b9 | 176 | #if wxUSE_SOUND |
aec18ff7 MB |
177 | if (event.ButtonDown() && wxGetApp().m_fire.IsOk()) |
178 | { | |
179 | wxGetApp().m_fire.Play(); | |
180 | } | |
d93966b9 | 181 | #endif // wxUSE_SOUND |
bbf1f0e5 KB |
182 | } |
183 | ||
184 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
aec18ff7 | 185 | EVT_MENU(JOYTEST_QUIT, MyFrame::OnQuit) |
bbf1f0e5 KB |
186 | END_EVENT_TABLE() |
187 | ||
aec18ff7 MB |
188 | MyFrame::MyFrame(wxFrame *parent, const wxString& title, const wxPoint& pos, |
189 | const wxSize& size, const long style) | |
45cbbbb3 | 190 | : wxFrame(parent, wxID_ANY, title, pos, size, style) |
bbf1f0e5 | 191 | { |
aec18ff7 | 192 | canvas = new MyCanvas(this); |
bbf1f0e5 KB |
193 | } |
194 | ||
87728739 | 195 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) |
bbf1f0e5 | 196 | { |
45cbbbb3 | 197 | Close(true); |
bbf1f0e5 KB |
198 | } |
199 | ||
200 | void MyFrame::OnActivate(wxActivateEvent& event) | |
201 | { | |
aec18ff7 MB |
202 | if (event.GetActive() && canvas) |
203 | canvas->SetFocus(); | |
bbf1f0e5 | 204 | } |