]> git.saurik.com Git - wxWidgets.git/blame - samples/joytest/joytest.cpp
adding new files for xti merge
[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
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
45cbbbb3 92 return true;
bbf1f0e5
KB
93}
94
95BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
2f6c54eb 96 EVT_JOYSTICK_EVENTS(MyCanvas::OnJoystickEvent)
bbf1f0e5
KB
97END_EVENT_TABLE()
98
99// Define a constructor for my canvas
100MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size):
45cbbbb3 101 wxScrolledWindow(parent, wxID_ANY, pos, size, wxSUNKEN_BORDER)
bbf1f0e5 102{
a800dc50 103 m_stick = new wxJoystick(wxJOYSTICK1);
65442ab6 104 nButtons = m_stick->GetNumberButtons();
a800dc50 105 m_stick->SetCapture(this, 10);
bbf1f0e5
KB
106}
107
aec18ff7 108MyCanvas::~MyCanvas()
bbf1f0e5 109{
a800dc50
RD
110 m_stick->ReleaseCapture();
111 delete m_stick;
bbf1f0e5
KB
112}
113
114void MyCanvas::OnJoystickEvent(wxJoystickEvent& event)
115{
aec18ff7
MB
116 wxClientDC dc(this);
117
118 wxPoint pt(event.GetPosition());
119
a800dc50
RD
120 // if negative positions are possible then shift everything up
121 int xmin = wxGetApp().m_minX;
122 int xmax = wxGetApp().m_maxX;
123 int ymin = wxGetApp().m_minY;
124 int ymax = wxGetApp().m_maxY;
125 if (xmin < 0) {
126 xmax += abs(xmin);
127 pt.x += abs(xmin);
a800dc50
RD
128 }
129 if (ymin < 0) {
130 ymax += abs(ymin);
131 pt.y += abs(ymin);
a800dc50 132 }
925e9792 133
aec18ff7
MB
134 // Scale to canvas size
135 int cw, ch;
136 GetSize(&cw, &ch);
bbf1f0e5 137
a800dc50
RD
138 pt.x = (long) (((double)pt.x/(double)xmax) * cw);
139 pt.y = (long) (((double)pt.y/(double)ymax) * ch);
bbf1f0e5 140
aec18ff7
MB
141 if (xpos > -1 && ypos > -1 && event.IsMove() && event.ButtonIsDown())
142 {
143 dc.SetPen(*wxBLACK_PEN);
144 dc.DrawLine(xpos, ypos, pt.x, pt.y);
145 }
bbf1f0e5 146
aec18ff7
MB
147 xpos = pt.x;
148 ypos = pt.y;
bbf1f0e5 149
8520f137 150#if wxUSE_STATUSBAR
600683ca 151 wxString buf;
aec18ff7 152 if (event.ButtonDown())
9a83f860 153 buf.Printf(wxT("Joystick (%d, %d) #%i Fire!"), pt.x, pt.y, event.GetButtonChange());
aec18ff7 154 else
9a83f860 155 buf.Printf(wxT("Joystick (%d, %d) "), pt.x, pt.y);
bbf1f0e5 156
65442ab6
RN
157/*
158 for(int i = 0; i < nButtons; ++i)
159 {
196785e6 160 buf += wxString(wxT("[")) +
65442ab6
RN
161 ((event.GetButtonState() & (1 << i)) ? wxT("Y") : wxT("N")) + wxString(wxT("]"));
162 }
163*/
196785e6 164
aec18ff7 165 frame->SetStatusText(buf);
8520f137 166#endif // wxUSE_STATUSBAR
bbf1f0e5 167
d93966b9 168#if wxUSE_SOUND
aec18ff7
MB
169 if (event.ButtonDown() && wxGetApp().m_fire.IsOk())
170 {
171 wxGetApp().m_fire.Play();
172 }
d93966b9 173#endif // wxUSE_SOUND
bbf1f0e5
KB
174}
175
176BEGIN_EVENT_TABLE(MyFrame, wxFrame)
aec18ff7 177 EVT_MENU(JOYTEST_QUIT, MyFrame::OnQuit)
bbf1f0e5
KB
178END_EVENT_TABLE()
179
aec18ff7
MB
180MyFrame::MyFrame(wxFrame *parent, const wxString& title, const wxPoint& pos,
181 const wxSize& size, const long style)
45cbbbb3 182 : wxFrame(parent, wxID_ANY, title, pos, size, style)
bbf1f0e5 183{
aec18ff7 184 canvas = new MyCanvas(this);
bbf1f0e5
KB
185}
186
87728739 187void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
bbf1f0e5 188{
45cbbbb3 189 Close(true);
bbf1f0e5
KB
190}
191
192void MyFrame::OnActivate(wxActivateEvent& event)
193{
aec18ff7
MB
194 if (event.GetActive() && canvas)
195 canvas->SetFocus();
bbf1f0e5 196}