]>
Commit | Line | Data |
---|---|---|
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 | int nButtons = 0; | |
43 | // Initialise this in OnInit, not statically | |
44 | bool MyApp::OnInit() | |
45 | { | |
46 | wxJoystick stick(wxJOYSTICK1); | |
47 | if (!stick.IsOk()) | |
48 | { | |
49 | wxMessageBox(_T("No joystick detected!")); | |
50 | return false; | |
51 | } | |
52 | ||
53 | #if wxUSE_SOUND | |
54 | m_fire.Create(_T("buttonpress.wav")); | |
55 | #endif // wxUSE_SOUND | |
56 | ||
57 | m_minX = stick.GetXMin(); | |
58 | m_minY = stick.GetYMin(); | |
59 | m_maxX = stick.GetXMax(); | |
60 | m_maxY = stick.GetYMax(); | |
61 | ||
62 | // Create the main frame window | |
63 | ||
64 | frame = new MyFrame(NULL, _T("Joystick Demo"), wxDefaultPosition, | |
65 | wxSize(500, 400), wxDEFAULT_FRAME_STYLE | wxHSCROLL | wxVSCROLL); | |
66 | ||
67 | // Give it an icon (this is ignored in MDI mode: uses resources) | |
68 | #ifdef __WXMSW__ | |
69 | frame->SetIcon(wxIcon(_T("joyicon"))); | |
70 | #endif | |
71 | #ifdef __X__ | |
72 | frame->SetIcon(wxIcon(_T("joyicon.xbm"))); | |
73 | #endif | |
74 | ||
75 | // Make a menubar | |
76 | wxMenu *file_menu = new wxMenu; | |
77 | ||
78 | file_menu->Append(JOYTEST_QUIT, _T("&Exit")); | |
79 | ||
80 | wxMenuBar *menu_bar = new wxMenuBar; | |
81 | ||
82 | menu_bar->Append(file_menu, _T("&File")); | |
83 | ||
84 | // Associate the menu bar with the frame | |
85 | frame->SetMenuBar(menu_bar); | |
86 | ||
87 | #if wxUSE_STATUSBAR | |
88 | frame->CreateStatusBar(); | |
89 | frame->SetStatusText(wxString::Format(wxT("Device [%s] (PID:[%i] MID:[%i]) Ready... # of joysticks:[%i]"), stick.GetProductName().c_str(), stick.GetProductId(), stick.GetManufacturerId(), stick.GetNumberJoysticks())); | |
90 | #endif // wxUSE_STATUSBAR | |
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, 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 | wxClientDC dc(this); | |
122 | ||
123 | wxPoint pt(event.GetPosition()); | |
124 | ||
125 | // if negative positions are possible then shift everything up | |
126 | int xmin = wxGetApp().m_minX; | |
127 | int xmax = wxGetApp().m_maxX; | |
128 | int ymin = wxGetApp().m_minY; | |
129 | int ymax = wxGetApp().m_maxY; | |
130 | if (xmin < 0) { | |
131 | xmax += abs(xmin); | |
132 | pt.x += abs(xmin); | |
133 | } | |
134 | if (ymin < 0) { | |
135 | ymax += abs(ymin); | |
136 | pt.y += abs(ymin); | |
137 | } | |
138 | ||
139 | // Scale to canvas size | |
140 | int cw, ch; | |
141 | GetSize(&cw, &ch); | |
142 | ||
143 | pt.x = (long) (((double)pt.x/(double)xmax) * cw); | |
144 | pt.y = (long) (((double)pt.y/(double)ymax) * ch); | |
145 | ||
146 | if (xpos > -1 && ypos > -1 && event.IsMove() && event.ButtonIsDown()) | |
147 | { | |
148 | dc.SetPen(*wxBLACK_PEN); | |
149 | dc.DrawLine(xpos, ypos, pt.x, pt.y); | |
150 | } | |
151 | ||
152 | xpos = pt.x; | |
153 | ypos = pt.y; | |
154 | ||
155 | #if wxUSE_STATUSBAR | |
156 | wxString buf; | |
157 | if (event.ButtonDown()) | |
158 | buf.Printf(_T("Joystick (%d, %d) #%i Fire!"), pt.x, pt.y, event.GetButtonChange()); | |
159 | else | |
160 | buf.Printf(_T("Joystick (%d, %d) "), pt.x, pt.y); | |
161 | ||
162 | /* | |
163 | for(int i = 0; i < nButtons; ++i) | |
164 | { | |
165 | buf += wxString(wxT("[")) + | |
166 | ((event.GetButtonState() & (1 << i)) ? wxT("Y") : wxT("N")) + wxString(wxT("]")); | |
167 | } | |
168 | */ | |
169 | ||
170 | frame->SetStatusText(buf); | |
171 | #endif // wxUSE_STATUSBAR | |
172 | ||
173 | #if wxUSE_SOUND | |
174 | if (event.ButtonDown() && wxGetApp().m_fire.IsOk()) | |
175 | { | |
176 | wxGetApp().m_fire.Play(); | |
177 | } | |
178 | #endif // wxUSE_SOUND | |
179 | } | |
180 | ||
181 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
182 | EVT_MENU(JOYTEST_QUIT, MyFrame::OnQuit) | |
183 | END_EVENT_TABLE() | |
184 | ||
185 | MyFrame::MyFrame(wxFrame *parent, const wxString& title, const wxPoint& pos, | |
186 | const wxSize& size, const long style) | |
187 | : wxFrame(parent, wxID_ANY, title, pos, size, style) | |
188 | { | |
189 | canvas = new MyCanvas(this); | |
190 | } | |
191 | ||
192 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) | |
193 | { | |
194 | Close(true); | |
195 | } | |
196 | ||
197 | void MyFrame::OnActivate(wxActivateEvent& event) | |
198 | { | |
199 | if (event.GetActive() && canvas) | |
200 | canvas->SetFocus(); | |
201 | } |