]> git.saurik.com Git - wxWidgets.git/blame - samples/joytest/joytest.cpp
Presentation of hiding and disabling of the items in wxRadioBox.
[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{
aec18ff7
MB
46 wxJoystick stick(wxJOYSTICK1);
47 if (!stick.IsOk())
48 {
600683ca 49 wxMessageBox(_T("No joystick detected!"));
45cbbbb3 50 return false;
aec18ff7
MB
51 }
52
d93966b9 53#if wxUSE_SOUND
600683ca 54 m_fire.Create(_T("gun.wav"));
d93966b9 55#endif // wxUSE_SOUND
bbf1f0e5 56
a800dc50
RD
57 m_minX = stick.GetXMin();
58 m_minY = stick.GetYMin();
aec18ff7
MB
59 m_maxX = stick.GetXMax();
60 m_maxY = stick.GetYMax();
bbf1f0e5 61
aec18ff7 62 // Create the main frame window
bbf1f0e5 63
600683ca 64 frame = new MyFrame(NULL, _T("Joystick Demo"), wxDefaultPosition,
aec18ff7 65 wxSize(500, 400), wxDEFAULT_FRAME_STYLE | wxHSCROLL | wxVSCROLL);
bbf1f0e5
KB
66
67 // Give it an icon (this is ignored in MDI mode: uses resources)
2049ba38 68#ifdef __WXMSW__
600683ca 69 frame->SetIcon(wxIcon(_T("joyicon")));
bbf1f0e5
KB
70#endif
71#ifdef __X__
600683ca 72 frame->SetIcon(wxIcon(_T("joyicon.xbm")));
bbf1f0e5
KB
73#endif
74
aec18ff7
MB
75 // Make a menubar
76 wxMenu *file_menu = new wxMenu;
bbf1f0e5 77
600683ca 78 file_menu->Append(JOYTEST_QUIT, _T("&Exit"));
bbf1f0e5 79
aec18ff7 80 wxMenuBar *menu_bar = new wxMenuBar;
bbf1f0e5 81
600683ca 82 menu_bar->Append(file_menu, _T("&File"));
bbf1f0e5 83
aec18ff7
MB
84 // Associate the menu bar with the frame
85 frame->SetMenuBar(menu_bar);
bbf1f0e5 86
8520f137 87#if wxUSE_STATUSBAR
aec18ff7 88 frame->CreateStatusBar();
65442ab6 89 frame->SetStatusText(wxString::Format(wxT("Device [%s] (PID:[%i] MID:[%i]) Ready... # of joysticks:[%i]"), stick.GetProductName().c_str(), stick.GetProductId(), stick.GetManufacturerId(), stick.GetNumberJoysticks()));
8520f137 90#endif // wxUSE_STATUSBAR
bbf1f0e5 91
aec18ff7 92 frame->CenterOnScreen();
45cbbbb3 93 frame->Show(true);
bbf1f0e5 94
aec18ff7 95 SetTopWindow(frame);
bbf1f0e5 96
45cbbbb3 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):
45cbbbb3 106 wxScrolledWindow(parent, wxID_ANY, pos, size, wxSUNKEN_BORDER)
bbf1f0e5 107{
a800dc50 108 m_stick = new wxJoystick(wxJOYSTICK1);
65442ab6 109 nButtons = m_stick->GetNumberButtons();
a800dc50 110 m_stick->SetCapture(this, 10);
bbf1f0e5
KB
111}
112
aec18ff7 113MyCanvas::~MyCanvas()
bbf1f0e5 114{
a800dc50
RD
115 m_stick->ReleaseCapture();
116 delete m_stick;
bbf1f0e5
KB
117}
118
119void MyCanvas::OnJoystickEvent(wxJoystickEvent& event)
120{
aec18ff7
MB
121 wxClientDC dc(this);
122
123 wxPoint pt(event.GetPosition());
124
a800dc50
RD
125 // if negative positions are possible then shift everything up
126 int xmin = wxGetApp().m_minX;
127 int xmax = wxGetApp().m_maxX;
128 int ymin = wxGetApp().m_minY;
129 int ymax = wxGetApp().m_maxY;
130 if (xmin < 0) {
131 xmax += abs(xmin);
132 pt.x += abs(xmin);
a800dc50
RD
133 }
134 if (ymin < 0) {
135 ymax += abs(ymin);
136 pt.y += abs(ymin);
a800dc50 137 }
925e9792 138
aec18ff7
MB
139 // Scale to canvas size
140 int cw, ch;
141 GetSize(&cw, &ch);
bbf1f0e5 142
a800dc50
RD
143 pt.x = (long) (((double)pt.x/(double)xmax) * cw);
144 pt.y = (long) (((double)pt.y/(double)ymax) * ch);
bbf1f0e5 145
aec18ff7
MB
146 if (xpos > -1 && ypos > -1 && event.IsMove() && event.ButtonIsDown())
147 {
148 dc.SetPen(*wxBLACK_PEN);
149 dc.DrawLine(xpos, ypos, pt.x, pt.y);
150 }
bbf1f0e5 151
aec18ff7
MB
152 xpos = pt.x;
153 ypos = pt.y;
bbf1f0e5 154
8520f137 155#if wxUSE_STATUSBAR
600683ca 156 wxString buf;
aec18ff7 157 if (event.ButtonDown())
65442ab6 158 buf.Printf(_T("Joystick (%d, %d) #%i Fire!"), pt.x, pt.y, event.GetButtonChange());
aec18ff7 159 else
65442ab6 160 buf.Printf(_T("Joystick (%d, %d) "), pt.x, pt.y);
bbf1f0e5 161
65442ab6
RN
162/*
163 for(int i = 0; i < nButtons; ++i)
164 {
165 buf += wxString(wxT("[")) +
166 ((event.GetButtonState() & (1 << i)) ? wxT("Y") : wxT("N")) + wxString(wxT("]"));
167 }
168*/
169
aec18ff7 170 frame->SetStatusText(buf);
8520f137 171#endif // wxUSE_STATUSBAR
bbf1f0e5 172
d93966b9 173#if wxUSE_SOUND
aec18ff7
MB
174 if (event.ButtonDown() && wxGetApp().m_fire.IsOk())
175 {
176 wxGetApp().m_fire.Play();
177 }
d93966b9 178#endif // wxUSE_SOUND
bbf1f0e5
KB
179}
180
181BEGIN_EVENT_TABLE(MyFrame, wxFrame)
aec18ff7 182 EVT_MENU(JOYTEST_QUIT, MyFrame::OnQuit)
bbf1f0e5
KB
183END_EVENT_TABLE()
184
aec18ff7
MB
185MyFrame::MyFrame(wxFrame *parent, const wxString& title, const wxPoint& pos,
186 const wxSize& size, const long style)
45cbbbb3 187 : wxFrame(parent, wxID_ANY, title, pos, size, style)
bbf1f0e5 188{
aec18ff7 189 canvas = new MyCanvas(this);
bbf1f0e5
KB
190}
191
87728739 192void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
bbf1f0e5 193{
45cbbbb3 194 Close(true);
bbf1f0e5
KB
195}
196
197void MyFrame::OnActivate(wxActivateEvent& event)
198{
aec18ff7
MB
199 if (event.GetActive() && canvas)
200 canvas->SetFocus();
bbf1f0e5 201}