Fix docs for Bind
[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!"));
aec18ff7
MB
53 return FALSE;
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
MB
92 frame->CenterOnScreen();
93 frame->Show(TRUE);
bbf1f0e5 94
aec18ff7 95 SetTopWindow(frame);
bbf1f0e5 96
aec18ff7 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):
aec18ff7 106 wxScrolledWindow(parent, -1, 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);
132 xmin = 0;
133 }
134 if (ymin < 0) {
135 ymax += abs(ymin);
136 pt.y += abs(ymin);
137 ymin = 0;
138 }
139
aec18ff7
MB
140 // Scale to canvas size
141 int cw, ch;
142 GetSize(&cw, &ch);
bbf1f0e5 143
a800dc50
RD
144 pt.x = (long) (((double)pt.x/(double)xmax) * cw);
145 pt.y = (long) (((double)pt.y/(double)ymax) * ch);
bbf1f0e5 146
aec18ff7
MB
147 if (xpos > -1 && ypos > -1 && event.IsMove() && event.ButtonIsDown())
148 {
149 dc.SetPen(*wxBLACK_PEN);
150 dc.DrawLine(xpos, ypos, pt.x, pt.y);
151 }
bbf1f0e5 152
aec18ff7
MB
153 xpos = pt.x;
154 ypos = pt.y;
bbf1f0e5 155
600683ca 156 wxString buf;
aec18ff7 157 if (event.ButtonDown())
600683ca 158 buf.Printf(_T("Joystick (%d, %d) Fire!"), pt.x, pt.y);
aec18ff7 159 else
600683ca 160 buf.Printf(_T("Joystick (%d, %d)"), pt.x, pt.y);
bbf1f0e5 161
aec18ff7 162 frame->SetStatusText(buf);
bbf1f0e5 163
d93966b9 164#if wxUSE_SOUND
aec18ff7
MB
165 if (event.ButtonDown() && wxGetApp().m_fire.IsOk())
166 {
167 wxGetApp().m_fire.Play();
168 }
d93966b9 169#endif // wxUSE_SOUND
bbf1f0e5
KB
170}
171
172BEGIN_EVENT_TABLE(MyFrame, wxFrame)
aec18ff7 173 EVT_MENU(JOYTEST_QUIT, MyFrame::OnQuit)
bbf1f0e5
KB
174END_EVENT_TABLE()
175
aec18ff7
MB
176MyFrame::MyFrame(wxFrame *parent, const wxString& title, const wxPoint& pos,
177 const wxSize& size, const long style)
178 : wxFrame(parent, -1, title, pos, size, style)
bbf1f0e5 179{
aec18ff7 180 canvas = new MyCanvas(this);
bbf1f0e5
KB
181}
182
aec18ff7 183MyFrame::~MyFrame()
bbf1f0e5 184{
aec18ff7 185 // Empty
bbf1f0e5
KB
186}
187
87728739 188void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
bbf1f0e5 189{
aec18ff7 190 Close(TRUE);
bbf1f0e5
KB
191}
192
193void MyFrame::OnActivate(wxActivateEvent& event)
194{
aec18ff7
MB
195 if (event.GetActive() && canvas)
196 canvas->SetFocus();
bbf1f0e5 197}