]> git.saurik.com Git - wxWidgets.git/blame_incremental - samples/joytest/joytest.cpp
adding new files for xti merge
[wxWidgets.git] / samples / joytest / joytest.cpp
... / ...
CommitLineData
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$
8// Copyright: (c) Julian Smart
9// Licence: wxWindows licence
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
23#if !wxUSE_JOYSTICK
24# error You must set wxUSE_JOYSTICK to 1 in setup.h
25#endif
26
27#include "wx/sound.h"
28#include "wx/joystick.h"
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
42int nButtons = 0;
43// Initialise this in OnInit, not statically
44bool MyApp::OnInit()
45{
46 if ( !wxApp::OnInit() )
47 return false;
48
49 wxJoystick stick(wxJOYSTICK1);
50 if (!stick.IsOk())
51 {
52 wxMessageBox(wxT("No joystick detected!"));
53 return false;
54 }
55
56#if wxUSE_SOUND
57 m_fire.Create(wxT("buttonpress.wav"));
58#endif // wxUSE_SOUND
59
60 m_minX = stick.GetXMin();
61 m_minY = stick.GetYMin();
62 m_maxX = stick.GetXMax();
63 m_maxY = stick.GetYMax();
64
65 // Create the main frame window
66
67 frame = new MyFrame(NULL, wxT("Joystick Demo"), wxDefaultPosition,
68 wxSize(500, 400), wxDEFAULT_FRAME_STYLE | wxHSCROLL | wxVSCROLL);
69
70 frame->SetIcon(wxICON(sample));
71
72 // Make a menubar
73 wxMenu *file_menu = new wxMenu;
74
75 file_menu->Append(JOYTEST_QUIT, wxT("&Exit"));
76
77 wxMenuBar *menu_bar = new wxMenuBar;
78
79 menu_bar->Append(file_menu, wxT("&File"));
80
81 // Associate the menu bar with the frame
82 frame->SetMenuBar(menu_bar);
83
84#if wxUSE_STATUSBAR
85 frame->CreateStatusBar();
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()));
87#endif // wxUSE_STATUSBAR
88
89 frame->CenterOnScreen();
90 frame->Show(true);
91
92 return true;
93}
94
95BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
96 EVT_JOYSTICK_EVENTS(MyCanvas::OnJoystickEvent)
97END_EVENT_TABLE()
98
99// Define a constructor for my canvas
100MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size):
101 wxScrolledWindow(parent, wxID_ANY, pos, size, wxSUNKEN_BORDER)
102{
103 m_stick = new wxJoystick(wxJOYSTICK1);
104 nButtons = m_stick->GetNumberButtons();
105 m_stick->SetCapture(this, 10);
106}
107
108MyCanvas::~MyCanvas()
109{
110 m_stick->ReleaseCapture();
111 delete m_stick;
112}
113
114void MyCanvas::OnJoystickEvent(wxJoystickEvent& event)
115{
116 wxClientDC dc(this);
117
118 wxPoint pt(event.GetPosition());
119
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);
128 }
129 if (ymin < 0) {
130 ymax += abs(ymin);
131 pt.y += abs(ymin);
132 }
133
134 // Scale to canvas size
135 int cw, ch;
136 GetSize(&cw, &ch);
137
138 pt.x = (long) (((double)pt.x/(double)xmax) * cw);
139 pt.y = (long) (((double)pt.y/(double)ymax) * ch);
140
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 }
146
147 xpos = pt.x;
148 ypos = pt.y;
149
150#if wxUSE_STATUSBAR
151 wxString buf;
152 if (event.ButtonDown())
153 buf.Printf(wxT("Joystick (%d, %d) #%i Fire!"), pt.x, pt.y, event.GetButtonChange());
154 else
155 buf.Printf(wxT("Joystick (%d, %d) "), pt.x, pt.y);
156
157/*
158 for(int i = 0; i < nButtons; ++i)
159 {
160 buf += wxString(wxT("[")) +
161 ((event.GetButtonState() & (1 << i)) ? wxT("Y") : wxT("N")) + wxString(wxT("]"));
162 }
163*/
164
165 frame->SetStatusText(buf);
166#endif // wxUSE_STATUSBAR
167
168#if wxUSE_SOUND
169 if (event.ButtonDown() && wxGetApp().m_fire.IsOk())
170 {
171 wxGetApp().m_fire.Play();
172 }
173#endif // wxUSE_SOUND
174}
175
176BEGIN_EVENT_TABLE(MyFrame, wxFrame)
177 EVT_MENU(JOYTEST_QUIT, MyFrame::OnQuit)
178END_EVENT_TABLE()
179
180MyFrame::MyFrame(wxFrame *parent, const wxString& title, const wxPoint& pos,
181 const wxSize& size, const long style)
182 : wxFrame(parent, wxID_ANY, title, pos, size, style)
183{
184 canvas = new MyCanvas(this);
185}
186
187void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
188{
189 Close(true);
190}
191
192void MyFrame::OnActivate(wxActivateEvent& event)
193{
194 if (event.GetActive() && canvas)
195 canvas->SetFocus();
196}