]> git.saurik.com Git - wxWidgets.git/blame - samples/joytest/joytest.cpp
demonstrate tab order
[wxWidgets.git] / samples / joytest / joytest.cpp
CommitLineData
bbf1f0e5
KB
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$
6aa89a22 8// Copyright: (c) Julian Smart
2f6c54eb 9// Licence: wxWindows license
bbf1f0e5
KB
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
9832ce0b 23#if !wxUSE_JOYSTICK
aec18ff7
MB
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
9832ce0b
GD
29#endif
30
d93966b9 31#include "wx/sound.h"
9832ce0b 32#include "wx/joystick.h"
bbf1f0e5
KB
33
34#include "joytest.h"
35
36MyFrame *frame = NULL;
37
38IMPLEMENT_APP(MyApp)
39
40// For drawing lines in a canvas
41long xpos = -1;
42long ypos = -1;
43
44int winNumber = 1;
45
46// Initialise this in OnInit, not statically
aec18ff7 47bool MyApp::OnInit()
bbf1f0e5 48{
aec18ff7
MB
49 wxJoystick stick(wxJOYSTICK1);
50 if (!stick.IsOk())
51 {
600683ca 52 wxMessageBox(_T("No joystick detected!"));
45cbbbb3 53 return false;
aec18ff7
MB
54 }
55
d93966b9 56#if wxUSE_SOUND
600683ca 57 m_fire.Create(_T("gun.wav"));
d93966b9 58#endif // wxUSE_SOUND
bbf1f0e5 59
a800dc50
RD
60 m_minX = stick.GetXMin();
61 m_minY = stick.GetYMin();
aec18ff7
MB
62 m_maxX = stick.GetXMax();
63 m_maxY = stick.GetYMax();
bbf1f0e5 64
aec18ff7 65 // Create the main frame window
bbf1f0e5 66
600683ca 67 frame = new MyFrame(NULL, _T("Joystick Demo"), wxDefaultPosition,
aec18ff7 68 wxSize(500, 400), wxDEFAULT_FRAME_STYLE | wxHSCROLL | wxVSCROLL);
bbf1f0e5
KB
69
70 // Give it an icon (this is ignored in MDI mode: uses resources)
2049ba38 71#ifdef __WXMSW__
600683ca 72 frame->SetIcon(wxIcon(_T("joyicon")));
bbf1f0e5
KB
73#endif
74#ifdef __X__
600683ca 75 frame->SetIcon(wxIcon(_T("joyicon.xbm")));
bbf1f0e5
KB
76#endif
77
aec18ff7
MB
78 // Make a menubar
79 wxMenu *file_menu = new wxMenu;
bbf1f0e5 80
600683ca 81 file_menu->Append(JOYTEST_QUIT, _T("&Exit"));
bbf1f0e5 82
aec18ff7 83 wxMenuBar *menu_bar = new wxMenuBar;
bbf1f0e5 84
600683ca 85 menu_bar->Append(file_menu, _T("&File"));
bbf1f0e5 86
aec18ff7
MB
87 // Associate the menu bar with the frame
88 frame->SetMenuBar(menu_bar);
bbf1f0e5 89
aec18ff7 90 frame->CreateStatusBar();
bbf1f0e5 91
aec18ff7 92 frame->CenterOnScreen();
45cbbbb3 93 frame->Show(true);
bbf1f0e5 94
aec18ff7 95 SetTopWindow(frame);
bbf1f0e5 96
45cbbbb3 97 return true;
bbf1f0e5
KB
98}
99
100BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
2f6c54eb 101 EVT_JOYSTICK_EVENTS(MyCanvas::OnJoystickEvent)
bbf1f0e5
KB
102END_EVENT_TABLE()
103
104// Define a constructor for my canvas
105MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size):
45cbbbb3 106 wxScrolledWindow(parent, wxID_ANY, pos, size, wxSUNKEN_BORDER)
bbf1f0e5 107{
a800dc50
RD
108 m_stick = new wxJoystick(wxJOYSTICK1);
109 m_stick->SetCapture(this, 10);
bbf1f0e5
KB
110}
111
aec18ff7 112MyCanvas::~MyCanvas()
bbf1f0e5 113{
a800dc50
RD
114 m_stick->ReleaseCapture();
115 delete m_stick;
bbf1f0e5
KB
116}
117
118void MyCanvas::OnJoystickEvent(wxJoystickEvent& event)
119{
aec18ff7
MB
120 wxClientDC dc(this);
121
122 wxPoint pt(event.GetPosition());
123
a800dc50
RD
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);
a800dc50
RD
132 }
133 if (ymin < 0) {
134 ymax += abs(ymin);
135 pt.y += abs(ymin);
a800dc50
RD
136 }
137
aec18ff7
MB
138 // Scale to canvas size
139 int cw, ch;
140 GetSize(&cw, &ch);
bbf1f0e5 141
a800dc50
RD
142 pt.x = (long) (((double)pt.x/(double)xmax) * cw);
143 pt.y = (long) (((double)pt.y/(double)ymax) * ch);
bbf1f0e5 144
aec18ff7
MB
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 }
bbf1f0e5 150
aec18ff7
MB
151 xpos = pt.x;
152 ypos = pt.y;
bbf1f0e5 153
600683ca 154 wxString buf;
aec18ff7 155 if (event.ButtonDown())
600683ca 156 buf.Printf(_T("Joystick (%d, %d) Fire!"), pt.x, pt.y);
aec18ff7 157 else
600683ca 158 buf.Printf(_T("Joystick (%d, %d)"), pt.x, pt.y);
bbf1f0e5 159
aec18ff7 160 frame->SetStatusText(buf);
bbf1f0e5 161
d93966b9 162#if wxUSE_SOUND
aec18ff7
MB
163 if (event.ButtonDown() && wxGetApp().m_fire.IsOk())
164 {
165 wxGetApp().m_fire.Play();
166 }
d93966b9 167#endif // wxUSE_SOUND
bbf1f0e5
KB
168}
169
170BEGIN_EVENT_TABLE(MyFrame, wxFrame)
aec18ff7 171 EVT_MENU(JOYTEST_QUIT, MyFrame::OnQuit)
bbf1f0e5
KB
172END_EVENT_TABLE()
173
aec18ff7
MB
174MyFrame::MyFrame(wxFrame *parent, const wxString& title, const wxPoint& pos,
175 const wxSize& size, const long style)
45cbbbb3 176 : wxFrame(parent, wxID_ANY, title, pos, size, style)
bbf1f0e5 177{
aec18ff7 178 canvas = new MyCanvas(this);
bbf1f0e5
KB
179}
180
aec18ff7 181MyFrame::~MyFrame()
bbf1f0e5 182{
aec18ff7 183 // Empty
bbf1f0e5
KB
184}
185
87728739 186void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
bbf1f0e5 187{
45cbbbb3 188 Close(true);
bbf1f0e5
KB
189}
190
191void MyFrame::OnActivate(wxActivateEvent& event)
192{
aec18ff7
MB
193 if (event.GetActive() && canvas)
194 canvas->SetFocus();
bbf1f0e5 195}