]> git.saurik.com Git - wxWidgets.git/blob - samples/joytest/joytest.cpp
fixed sending 2 events (normal and bogus cancel one) when ending to edit a tree ctrl...
[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 // Initialise this in OnInit, not statically
43 bool MyApp::OnInit()
44 {
45 wxJoystick stick(wxJOYSTICK1);
46 if (!stick.IsOk())
47 {
48 wxMessageBox(_T("No joystick detected!"));
49 return false;
50 }
51
52 #if wxUSE_SOUND
53 m_fire.Create(_T("gun.wav"));
54 #endif // wxUSE_SOUND
55
56 m_minX = stick.GetXMin();
57 m_minY = stick.GetYMin();
58 m_maxX = stick.GetXMax();
59 m_maxY = stick.GetYMax();
60
61 // Create the main frame window
62
63 frame = new MyFrame(NULL, _T("Joystick Demo"), wxDefaultPosition,
64 wxSize(500, 400), wxDEFAULT_FRAME_STYLE | wxHSCROLL | wxVSCROLL);
65
66 // Give it an icon (this is ignored in MDI mode: uses resources)
67 #ifdef __WXMSW__
68 frame->SetIcon(wxIcon(_T("joyicon")));
69 #endif
70 #ifdef __X__
71 frame->SetIcon(wxIcon(_T("joyicon.xbm")));
72 #endif
73
74 // Make a menubar
75 wxMenu *file_menu = new wxMenu;
76
77 file_menu->Append(JOYTEST_QUIT, _T("&Exit"));
78
79 wxMenuBar *menu_bar = new wxMenuBar;
80
81 menu_bar->Append(file_menu, _T("&File"));
82
83 // Associate the menu bar with the frame
84 frame->SetMenuBar(menu_bar);
85
86 #if wxUSE_STATUSBAR
87 frame->CreateStatusBar();
88 #endif // wxUSE_STATUSBAR
89
90 frame->CenterOnScreen();
91 frame->Show(true);
92
93 SetTopWindow(frame);
94
95 return true;
96 }
97
98 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
99 EVT_JOYSTICK_EVENTS(MyCanvas::OnJoystickEvent)
100 END_EVENT_TABLE()
101
102 // Define a constructor for my canvas
103 MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size):
104 wxScrolledWindow(parent, wxID_ANY, pos, size, wxSUNKEN_BORDER)
105 {
106 m_stick = new wxJoystick(wxJOYSTICK1);
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(_T("Joystick (%d, %d) Fire!"), pt.x, pt.y);
156 else
157 buf.Printf(_T("Joystick (%d, %d)"), pt.x, pt.y);
158
159 frame->SetStatusText(buf);
160 #endif // wxUSE_STATUSBAR
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 }