]>
git.saurik.com Git - wxWidgets.git/blob - samples/joytest/joytest.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Joystick sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
24 # error You must set wxUSE_JOYSTICK to 1 in setup.h
28 #include "wx/joystick.h"
32 MyFrame
*frame
= NULL
;
36 // For drawing lines in a canvas
43 // Initialise this in OnInit, not statically
46 wxJoystick
stick(wxJOYSTICK1
);
49 wxMessageBox(_T("No joystick detected!"));
54 m_fire
.Create(_T("gun.wav"));
57 m_minX
= stick
.GetXMin();
58 m_minY
= stick
.GetYMin();
59 m_maxX
= stick
.GetXMax();
60 m_maxY
= stick
.GetYMax();
62 // Create the main frame window
64 frame
= new MyFrame(NULL
, _T("Joystick Demo"), wxDefaultPosition
,
65 wxSize(500, 400), wxDEFAULT_FRAME_STYLE
| wxHSCROLL
| wxVSCROLL
);
67 // Give it an icon (this is ignored in MDI mode: uses resources)
69 frame
->SetIcon(wxIcon(_T("joyicon")));
72 frame
->SetIcon(wxIcon(_T("joyicon.xbm")));
76 wxMenu
*file_menu
= new wxMenu
;
78 file_menu
->Append(JOYTEST_QUIT
, _T("&Exit"));
80 wxMenuBar
*menu_bar
= new wxMenuBar
;
82 menu_bar
->Append(file_menu
, _T("&File"));
84 // Associate the menu bar with the frame
85 frame
->SetMenuBar(menu_bar
);
88 frame
->CreateStatusBar();
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()));
90 #endif // wxUSE_STATUSBAR
92 frame
->CenterOnScreen();
100 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
101 EVT_JOYSTICK_EVENTS(MyCanvas::OnJoystickEvent
)
104 // Define a constructor for my canvas
105 MyCanvas::MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
):
106 wxScrolledWindow(parent
, wxID_ANY
, pos
, size
, wxSUNKEN_BORDER
)
108 m_stick
= new wxJoystick(wxJOYSTICK1
);
109 nButtons
= m_stick
->GetNumberButtons();
110 m_stick
->SetCapture(this, 10);
113 MyCanvas::~MyCanvas()
115 m_stick
->ReleaseCapture();
119 void MyCanvas::OnJoystickEvent(wxJoystickEvent
& event
)
123 wxPoint
pt(event
.GetPosition());
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
;
139 // Scale to canvas size
143 pt
.x
= (long) (((double)pt
.x
/(double)xmax
) * cw
);
144 pt
.y
= (long) (((double)pt
.y
/(double)ymax
) * ch
);
146 if (xpos
> -1 && ypos
> -1 && event
.IsMove() && event
.ButtonIsDown())
148 dc
.SetPen(*wxBLACK_PEN
);
149 dc
.DrawLine(xpos
, ypos
, pt
.x
, pt
.y
);
157 if (event
.ButtonDown())
158 buf
.Printf(_T("Joystick (%d, %d) #%i Fire!"), pt
.x
, pt
.y
, event
.GetButtonChange());
160 buf
.Printf(_T("Joystick (%d, %d) "), pt
.x
, pt
.y
);
163 for(int i = 0; i < nButtons; ++i)
165 buf += wxString(wxT("[")) +
166 ((event.GetButtonState() & (1 << i)) ? wxT("Y") : wxT("N")) + wxString(wxT("]"));
170 frame
->SetStatusText(buf
);
171 #endif // wxUSE_STATUSBAR
174 if (event
.ButtonDown() && wxGetApp().m_fire
.IsOk())
176 wxGetApp().m_fire
.Play();
178 #endif // wxUSE_SOUND
181 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
182 EVT_MENU(JOYTEST_QUIT
, MyFrame::OnQuit
)
185 MyFrame::MyFrame(wxFrame
*parent
, const wxString
& title
, const wxPoint
& pos
,
186 const wxSize
& size
, const long style
)
187 : wxFrame(parent
, wxID_ANY
, title
, pos
, size
, style
)
189 canvas
= new MyCanvas(this);
192 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
197 void MyFrame::OnActivate(wxActivateEvent
& event
)
199 if (event
.GetActive() && canvas
)