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