]> git.saurik.com Git - wxWidgets.git/blame - samples/joytest/joytest.cpp
Correct EVT_MAXIMIZE macro name in wxTLW documentation.
[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
526954c5 9// Licence: wxWindows licence
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
807902f1
PC
32// the application icon (under Windows and OS/2 it is in resources and even
33// though we could still include the XPM here it would be unused)
34#if !defined(__WXMSW__) && !defined(__WXPM__)
35 #include "../sample.xpm"
36#endif
37
bbf1f0e5
KB
38MyFrame *frame = NULL;
39
40IMPLEMENT_APP(MyApp)
41
42// For drawing lines in a canvas
43long xpos = -1;
44long ypos = -1;
45
46int winNumber = 1;
47
65442ab6 48int nButtons = 0;
bbf1f0e5 49// Initialise this in OnInit, not statically
aec18ff7 50bool MyApp::OnInit()
bbf1f0e5 51{
45e6e6f8
VZ
52 if ( !wxApp::OnInit() )
53 return false;
54
aec18ff7
MB
55 wxJoystick stick(wxJOYSTICK1);
56 if (!stick.IsOk())
57 {
9a83f860 58 wxMessageBox(wxT("No joystick detected!"));
45cbbbb3 59 return false;
aec18ff7
MB
60 }
61
d93966b9 62#if wxUSE_SOUND
9a83f860 63 m_fire.Create(wxT("buttonpress.wav"));
d93966b9 64#endif // wxUSE_SOUND
bbf1f0e5 65
a800dc50
RD
66 m_minX = stick.GetXMin();
67 m_minY = stick.GetYMin();
aec18ff7
MB
68 m_maxX = stick.GetXMax();
69 m_maxY = stick.GetYMax();
bbf1f0e5 70
aec18ff7 71 // Create the main frame window
bbf1f0e5 72
9a83f860 73 frame = new MyFrame(NULL, wxT("Joystick Demo"), wxDefaultPosition,
aec18ff7 74 wxSize(500, 400), wxDEFAULT_FRAME_STYLE | wxHSCROLL | wxVSCROLL);
bbf1f0e5 75
3cb332c1 76 frame->SetIcon(wxICON(sample));
bbf1f0e5 77
aec18ff7
MB
78 // Make a menubar
79 wxMenu *file_menu = new wxMenu;
bbf1f0e5 80
9a83f860 81 file_menu->Append(JOYTEST_QUIT, wxT("&Exit"));
bbf1f0e5 82
aec18ff7 83 wxMenuBar *menu_bar = new wxMenuBar;
bbf1f0e5 84
9a83f860 85 menu_bar->Append(file_menu, wxT("&File"));
bbf1f0e5 86
aec18ff7
MB
87 // Associate the menu bar with the frame
88 frame->SetMenuBar(menu_bar);
bbf1f0e5 89
8520f137 90#if wxUSE_STATUSBAR
aec18ff7 91 frame->CreateStatusBar();
da9e9563 92 frame->SetStatusText(wxString::Format(wxT("Device [%s] (PID:[%i] MID:[%i]) Ready... # of joysticks:[%i]"), stick.GetProductName().c_str(), stick.GetProductId(), stick.GetManufacturerId(), wxJoystick::GetNumberJoysticks()));
8520f137 93#endif // wxUSE_STATUSBAR
bbf1f0e5 94
aec18ff7 95 frame->CenterOnScreen();
45cbbbb3 96 frame->Show(true);
bbf1f0e5 97
45cbbbb3 98 return true;
bbf1f0e5
KB
99}
100
101BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
2f6c54eb 102 EVT_JOYSTICK_EVENTS(MyCanvas::OnJoystickEvent)
bbf1f0e5
KB
103END_EVENT_TABLE()
104
105// Define a constructor for my canvas
106MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size):
45cbbbb3 107 wxScrolledWindow(parent, wxID_ANY, pos, size, wxSUNKEN_BORDER)
bbf1f0e5 108{
a800dc50 109 m_stick = new wxJoystick(wxJOYSTICK1);
65442ab6 110 nButtons = m_stick->GetNumberButtons();
a800dc50 111 m_stick->SetCapture(this, 10);
bbf1f0e5
KB
112}
113
aec18ff7 114MyCanvas::~MyCanvas()
bbf1f0e5 115{
a800dc50
RD
116 m_stick->ReleaseCapture();
117 delete m_stick;
bbf1f0e5
KB
118}
119
120void MyCanvas::OnJoystickEvent(wxJoystickEvent& event)
121{
aec18ff7
MB
122 wxClientDC dc(this);
123
124 wxPoint pt(event.GetPosition());
125
a800dc50
RD
126 // if negative positions are possible then shift everything up
127 int xmin = wxGetApp().m_minX;
128 int xmax = wxGetApp().m_maxX;
129 int ymin = wxGetApp().m_minY;
130 int ymax = wxGetApp().m_maxY;
131 if (xmin < 0) {
132 xmax += abs(xmin);
133 pt.x += abs(xmin);
a800dc50
RD
134 }
135 if (ymin < 0) {
136 ymax += abs(ymin);
137 pt.y += abs(ymin);
a800dc50 138 }
925e9792 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
8520f137 156#if wxUSE_STATUSBAR
600683ca 157 wxString buf;
aec18ff7 158 if (event.ButtonDown())
9a83f860 159 buf.Printf(wxT("Joystick (%d, %d) #%i Fire!"), pt.x, pt.y, event.GetButtonChange());
aec18ff7 160 else
9a83f860 161 buf.Printf(wxT("Joystick (%d, %d) "), pt.x, pt.y);
bbf1f0e5 162
65442ab6
RN
163/*
164 for(int i = 0; i < nButtons; ++i)
165 {
196785e6 166 buf += wxString(wxT("[")) +
65442ab6
RN
167 ((event.GetButtonState() & (1 << i)) ? wxT("Y") : wxT("N")) + wxString(wxT("]"));
168 }
169*/
196785e6 170
aec18ff7 171 frame->SetStatusText(buf);
8520f137 172#endif // wxUSE_STATUSBAR
bbf1f0e5 173
d93966b9 174#if wxUSE_SOUND
aec18ff7
MB
175 if (event.ButtonDown() && wxGetApp().m_fire.IsOk())
176 {
177 wxGetApp().m_fire.Play();
178 }
d93966b9 179#endif // wxUSE_SOUND
bbf1f0e5
KB
180}
181
182BEGIN_EVENT_TABLE(MyFrame, wxFrame)
aec18ff7 183 EVT_MENU(JOYTEST_QUIT, MyFrame::OnQuit)
bbf1f0e5
KB
184END_EVENT_TABLE()
185
aec18ff7
MB
186MyFrame::MyFrame(wxFrame *parent, const wxString& title, const wxPoint& pos,
187 const wxSize& size, const long style)
45cbbbb3 188 : wxFrame(parent, wxID_ANY, title, pos, size, style)
bbf1f0e5 189{
aec18ff7 190 canvas = new MyCanvas(this);
bbf1f0e5
KB
191}
192
87728739 193void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
bbf1f0e5 194{
45cbbbb3 195 Close(true);
bbf1f0e5
KB
196}
197
198void MyFrame::OnActivate(wxActivateEvent& event)
199{
aec18ff7
MB
200 if (event.GetActive() && canvas)
201 canvas->SetFocus();
bbf1f0e5 202}