Remove unnecessary mondrian.{ico,xpm} files from samples directory.
[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(wxT("No joystick detected!"));
53 return false;
54 }
55
56 #if wxUSE_SOUND
57 m_fire.Create(wxT("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, wxT("Joystick Demo"), wxDefaultPosition,
68 wxSize(500, 400), wxDEFAULT_FRAME_STYLE | wxHSCROLL | wxVSCROLL);
69
70 frame->SetIcon(wxICON(sample));
71
72 // Make a menubar
73 wxMenu *file_menu = new wxMenu;
74
75 file_menu->Append(JOYTEST_QUIT, wxT("&Exit"));
76
77 wxMenuBar *menu_bar = new wxMenuBar;
78
79 menu_bar->Append(file_menu, wxT("&File"));
80
81 // Associate the menu bar with the frame
82 frame->SetMenuBar(menu_bar);
83
84 #if wxUSE_STATUSBAR
85 frame->CreateStatusBar();
86 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()));
87 #endif // wxUSE_STATUSBAR
88
89 frame->CenterOnScreen();
90 frame->Show(true);
91
92 SetTopWindow(frame);
93
94 return true;
95 }
96
97 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
98 EVT_JOYSTICK_EVENTS(MyCanvas::OnJoystickEvent)
99 END_EVENT_TABLE()
100
101 // Define a constructor for my canvas
102 MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size):
103 wxScrolledWindow(parent, wxID_ANY, pos, size, wxSUNKEN_BORDER)
104 {
105 m_stick = new wxJoystick(wxJOYSTICK1);
106 nButtons = m_stick->GetNumberButtons();
107 m_stick->SetCapture(this, 10);
108 }
109
110 MyCanvas::~MyCanvas()
111 {
112 m_stick->ReleaseCapture();
113 delete m_stick;
114 }
115
116 void MyCanvas::OnJoystickEvent(wxJoystickEvent& event)
117 {
118 wxClientDC dc(this);
119
120 wxPoint pt(event.GetPosition());
121
122 // if negative positions are possible then shift everything up
123 int xmin = wxGetApp().m_minX;
124 int xmax = wxGetApp().m_maxX;
125 int ymin = wxGetApp().m_minY;
126 int ymax = wxGetApp().m_maxY;
127 if (xmin < 0) {
128 xmax += abs(xmin);
129 pt.x += abs(xmin);
130 }
131 if (ymin < 0) {
132 ymax += abs(ymin);
133 pt.y += abs(ymin);
134 }
135
136 // Scale to canvas size
137 int cw, ch;
138 GetSize(&cw, &ch);
139
140 pt.x = (long) (((double)pt.x/(double)xmax) * cw);
141 pt.y = (long) (((double)pt.y/(double)ymax) * ch);
142
143 if (xpos > -1 && ypos > -1 && event.IsMove() && event.ButtonIsDown())
144 {
145 dc.SetPen(*wxBLACK_PEN);
146 dc.DrawLine(xpos, ypos, pt.x, pt.y);
147 }
148
149 xpos = pt.x;
150 ypos = pt.y;
151
152 #if wxUSE_STATUSBAR
153 wxString buf;
154 if (event.ButtonDown())
155 buf.Printf(wxT("Joystick (%d, %d) #%i Fire!"), pt.x, pt.y, event.GetButtonChange());
156 else
157 buf.Printf(wxT("Joystick (%d, %d) "), pt.x, pt.y);
158
159 /*
160 for(int i = 0; i < nButtons; ++i)
161 {
162 buf += wxString(wxT("[")) +
163 ((event.GetButtonState() & (1 << i)) ? wxT("Y") : wxT("N")) + wxString(wxT("]"));
164 }
165 */
166
167 frame->SetStatusText(buf);
168 #endif // wxUSE_STATUSBAR
169
170 #if wxUSE_SOUND
171 if (event.ButtonDown() && wxGetApp().m_fire.IsOk())
172 {
173 wxGetApp().m_fire.Play();
174 }
175 #endif // wxUSE_SOUND
176 }
177
178 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
179 EVT_MENU(JOYTEST_QUIT, MyFrame::OnQuit)
180 END_EVENT_TABLE()
181
182 MyFrame::MyFrame(wxFrame *parent, const wxString& title, const wxPoint& pos,
183 const wxSize& size, const long style)
184 : wxFrame(parent, wxID_ANY, title, pos, size, style)
185 {
186 canvas = new MyCanvas(this);
187 }
188
189 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
190 {
191 Close(true);
192 }
193
194 void MyFrame::OnActivate(wxActivateEvent& event)
195 {
196 if (event.GetActive() && canvas)
197 canvas->SetFocus();
198 }