-1->wxID_ANY, TRUE->true and FALSE->false replacements.
[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 #if !wxUSE_STATUSBAR
28 # error You must set wxUSE_STATUSBAR to 1 in setup.h
29 #endif
30
31 #include "wx/sound.h"
32 #include "wx/joystick.h"
33
34 #include "joytest.h"
35
36 MyFrame *frame = NULL;
37
38 IMPLEMENT_APP(MyApp)
39
40 // For drawing lines in a canvas
41 long xpos = -1;
42 long ypos = -1;
43
44 int winNumber = 1;
45
46 // Initialise this in OnInit, not statically
47 bool MyApp::OnInit()
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("gun.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 frame->CreateStatusBar();
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 m_stick->SetCapture(this, 10);
110 }
111
112 MyCanvas::~MyCanvas()
113 {
114 m_stick->ReleaseCapture();
115 delete m_stick;
116 }
117
118 void MyCanvas::OnJoystickEvent(wxJoystickEvent& event)
119 {
120 wxClientDC dc(this);
121
122 wxPoint pt(event.GetPosition());
123
124 // if negative positions are possible then shift everything up
125 int xmin = wxGetApp().m_minX;
126 int xmax = wxGetApp().m_maxX;
127 int ymin = wxGetApp().m_minY;
128 int ymax = wxGetApp().m_maxY;
129 if (xmin < 0) {
130 xmax += abs(xmin);
131 pt.x += abs(xmin);
132 }
133 if (ymin < 0) {
134 ymax += abs(ymin);
135 pt.y += abs(ymin);
136 }
137
138 // Scale to canvas size
139 int cw, ch;
140 GetSize(&cw, &ch);
141
142 pt.x = (long) (((double)pt.x/(double)xmax) * cw);
143 pt.y = (long) (((double)pt.y/(double)ymax) * ch);
144
145 if (xpos > -1 && ypos > -1 && event.IsMove() && event.ButtonIsDown())
146 {
147 dc.SetPen(*wxBLACK_PEN);
148 dc.DrawLine(xpos, ypos, pt.x, pt.y);
149 }
150
151 xpos = pt.x;
152 ypos = pt.y;
153
154 wxString buf;
155 if (event.ButtonDown())
156 buf.Printf(_T("Joystick (%d, %d) Fire!"), pt.x, pt.y);
157 else
158 buf.Printf(_T("Joystick (%d, %d)"), pt.x, pt.y);
159
160 frame->SetStatusText(buf);
161
162 #if wxUSE_SOUND
163 if (event.ButtonDown() && wxGetApp().m_fire.IsOk())
164 {
165 wxGetApp().m_fire.Play();
166 }
167 #endif // wxUSE_SOUND
168 }
169
170 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
171 EVT_MENU(JOYTEST_QUIT, MyFrame::OnQuit)
172 END_EVENT_TABLE()
173
174 MyFrame::MyFrame(wxFrame *parent, const wxString& title, const wxPoint& pos,
175 const wxSize& size, const long style)
176 : wxFrame(parent, wxID_ANY, title, pos, size, style)
177 {
178 canvas = new MyCanvas(this);
179 }
180
181 MyFrame::~MyFrame()
182 {
183 // Empty
184 }
185
186 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
187 {
188 Close(true);
189 }
190
191 void MyFrame::OnActivate(wxActivateEvent& event)
192 {
193 if (event.GetActive() && canvas)
194 canvas->SetFocus();
195 }