]> git.saurik.com Git - wxWidgets.git/blob - samples/joytest/joytest.cpp
further wxPenStyle/wxBrushStyle fixes for builds with compat off (partially by Marcin...
[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 int nButtons = 0;
43 // Initialise this in OnInit, not statically
44 bool MyApp::OnInit()
45 {
46 if ( !wxApp::OnInit() )
47 return false;
48
49 wxJoystick stick(wxJOYSTICK1);
50 if (!stick.IsOk())
51 {
52 wxMessageBox(_T("No joystick detected!"));
53 return false;
54 }
55
56 #if wxUSE_SOUND
57 m_fire.Create(_T("buttonpress.wav"));
58 #endif // wxUSE_SOUND
59
60 m_minX = stick.GetXMin();
61 m_minY = stick.GetYMin();
62 m_maxX = stick.GetXMax();
63 m_maxY = stick.GetYMax();
64
65 // Create the main frame window
66
67 frame = new MyFrame(NULL, _T("Joystick Demo"), wxDefaultPosition,
68 wxSize(500, 400), wxDEFAULT_FRAME_STYLE | wxHSCROLL | wxVSCROLL);
69
70 // Give it an icon (this is ignored in MDI mode: uses resources)
71 #ifdef __WXMSW__
72 frame->SetIcon(wxIcon(_T("joyicon")));
73 #endif
74 #ifdef __X__
75 frame->SetIcon(wxIcon(_T("joyicon.xbm")));
76 #endif
77
78 // Make a menubar
79 wxMenu *file_menu = new wxMenu;
80
81 file_menu->Append(JOYTEST_QUIT, _T("&Exit"));
82
83 wxMenuBar *menu_bar = new wxMenuBar;
84
85 menu_bar->Append(file_menu, _T("&File"));
86
87 // Associate the menu bar with the frame
88 frame->SetMenuBar(menu_bar);
89
90 #if wxUSE_STATUSBAR
91 frame->CreateStatusBar();
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()));
93 #endif // wxUSE_STATUSBAR
94
95 frame->CenterOnScreen();
96 frame->Show(true);
97
98 SetTopWindow(frame);
99
100 return true;
101 }
102
103 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
104 EVT_JOYSTICK_EVENTS(MyCanvas::OnJoystickEvent)
105 END_EVENT_TABLE()
106
107 // Define a constructor for my canvas
108 MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size):
109 wxScrolledWindow(parent, wxID_ANY, pos, size, wxSUNKEN_BORDER)
110 {
111 m_stick = new wxJoystick(wxJOYSTICK1);
112 nButtons = m_stick->GetNumberButtons();
113 m_stick->SetCapture(this, 10);
114 }
115
116 MyCanvas::~MyCanvas()
117 {
118 m_stick->ReleaseCapture();
119 delete m_stick;
120 }
121
122 void MyCanvas::OnJoystickEvent(wxJoystickEvent& event)
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 #if wxUSE_STATUSBAR
159 wxString buf;
160 if (event.ButtonDown())
161 buf.Printf(_T("Joystick (%d, %d) #%i Fire!"), pt.x, pt.y, event.GetButtonChange());
162 else
163 buf.Printf(_T("Joystick (%d, %d) "), pt.x, pt.y);
164
165 /*
166 for(int i = 0; i < nButtons; ++i)
167 {
168 buf += wxString(wxT("[")) +
169 ((event.GetButtonState() & (1 << i)) ? wxT("Y") : wxT("N")) + wxString(wxT("]"));
170 }
171 */
172
173 frame->SetStatusText(buf);
174 #endif // wxUSE_STATUSBAR
175
176 #if wxUSE_SOUND
177 if (event.ButtonDown() && wxGetApp().m_fire.IsOk())
178 {
179 wxGetApp().m_fire.Play();
180 }
181 #endif // wxUSE_SOUND
182 }
183
184 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
185 EVT_MENU(JOYTEST_QUIT, MyFrame::OnQuit)
186 END_EVENT_TABLE()
187
188 MyFrame::MyFrame(wxFrame *parent, const wxString& title, const wxPoint& pos,
189 const wxSize& size, const long style)
190 : wxFrame(parent, wxID_ANY, title, pos, size, style)
191 {
192 canvas = new MyCanvas(this);
193 }
194
195 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
196 {
197 Close(true);
198 }
199
200 void MyFrame::OnActivate(wxActivateEvent& event)
201 {
202 if (event.GetActive() && canvas)
203 canvas->SetFocus();
204 }