]> git.saurik.com Git - wxWidgets.git/blame - samples/joytest/joytest.cpp
Correct the name of the XPM file containing the icon in xrc sample.
[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
65442ab6 42int nButtons = 0;
bbf1f0e5 43// Initialise this in OnInit, not statically
aec18ff7 44bool MyApp::OnInit()
bbf1f0e5 45{
45e6e6f8
VZ
46 if ( !wxApp::OnInit() )
47 return false;
48
aec18ff7
MB
49 wxJoystick stick(wxJOYSTICK1);
50 if (!stick.IsOk())
51 {
9a83f860 52 wxMessageBox(wxT("No joystick detected!"));
45cbbbb3 53 return false;
aec18ff7
MB
54 }
55
d93966b9 56#if wxUSE_SOUND
9a83f860 57 m_fire.Create(wxT("buttonpress.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
9a83f860 67 frame = new MyFrame(NULL, wxT("Joystick Demo"), wxDefaultPosition,
aec18ff7 68 wxSize(500, 400), wxDEFAULT_FRAME_STYLE | wxHSCROLL | wxVSCROLL);
bbf1f0e5 69
3cb332c1 70 frame->SetIcon(wxICON(sample));
bbf1f0e5 71
aec18ff7
MB
72 // Make a menubar
73 wxMenu *file_menu = new wxMenu;
bbf1f0e5 74
9a83f860 75 file_menu->Append(JOYTEST_QUIT, wxT("&Exit"));
bbf1f0e5 76
aec18ff7 77 wxMenuBar *menu_bar = new wxMenuBar;
bbf1f0e5 78
9a83f860 79 menu_bar->Append(file_menu, wxT("&File"));
bbf1f0e5 80
aec18ff7
MB
81 // Associate the menu bar with the frame
82 frame->SetMenuBar(menu_bar);
bbf1f0e5 83
8520f137 84#if wxUSE_STATUSBAR
aec18ff7 85 frame->CreateStatusBar();
da9e9563 86 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 87#endif // wxUSE_STATUSBAR
bbf1f0e5 88
aec18ff7 89 frame->CenterOnScreen();
45cbbbb3 90 frame->Show(true);
bbf1f0e5 91
aec18ff7 92 SetTopWindow(frame);
bbf1f0e5 93
45cbbbb3 94 return true;
bbf1f0e5
KB
95}
96
97BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
2f6c54eb 98 EVT_JOYSTICK_EVENTS(MyCanvas::OnJoystickEvent)
bbf1f0e5
KB
99END_EVENT_TABLE()
100
101// Define a constructor for my canvas
102MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size):
45cbbbb3 103 wxScrolledWindow(parent, wxID_ANY, pos, size, wxSUNKEN_BORDER)
bbf1f0e5 104{
a800dc50 105 m_stick = new wxJoystick(wxJOYSTICK1);
65442ab6 106 nButtons = m_stick->GetNumberButtons();
a800dc50 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())
9a83f860 155 buf.Printf(wxT("Joystick (%d, %d) #%i Fire!"), pt.x, pt.y, event.GetButtonChange());
aec18ff7 156 else
9a83f860 157 buf.Printf(wxT("Joystick (%d, %d) "), pt.x, pt.y);
bbf1f0e5 158
65442ab6
RN
159/*
160 for(int i = 0; i < nButtons; ++i)
161 {
196785e6 162 buf += wxString(wxT("[")) +
65442ab6
RN
163 ((event.GetButtonState() & (1 << i)) ? wxT("Y") : wxT("N")) + wxString(wxT("]"));
164 }
165*/
196785e6 166
aec18ff7 167 frame->SetStatusText(buf);
8520f137 168#endif // wxUSE_STATUSBAR
bbf1f0e5 169
d93966b9 170#if wxUSE_SOUND
aec18ff7
MB
171 if (event.ButtonDown() && wxGetApp().m_fire.IsOk())
172 {
173 wxGetApp().m_fire.Play();
174 }
d93966b9 175#endif // wxUSE_SOUND
bbf1f0e5
KB
176}
177
178BEGIN_EVENT_TABLE(MyFrame, wxFrame)
aec18ff7 179 EVT_MENU(JOYTEST_QUIT, MyFrame::OnQuit)
bbf1f0e5
KB
180END_EVENT_TABLE()
181
aec18ff7
MB
182MyFrame::MyFrame(wxFrame *parent, const wxString& title, const wxPoint& pos,
183 const wxSize& size, const long style)
45cbbbb3 184 : wxFrame(parent, wxID_ANY, title, pos, size, style)
bbf1f0e5 185{
aec18ff7 186 canvas = new MyCanvas(this);
bbf1f0e5
KB
187}
188
87728739 189void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
bbf1f0e5 190{
45cbbbb3 191 Close(true);
bbf1f0e5
KB
192}
193
194void MyFrame::OnActivate(wxActivateEvent& event)
195{
aec18ff7
MB
196 if (event.GetActive() && canvas)
197 canvas->SetFocus();
bbf1f0e5 198}