]>
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 licence
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 // the application icon (under Windows and OS/2 it is in resources and even
33 // though we could still include the XPM here it would be unused)
34 #if !defined(__WXMSW__) && !defined(__WXPM__)
35 #include "../sample.xpm"
38 MyFrame
*frame
= NULL
;
42 // For drawing lines in a canvas
49 // Initialise this in OnInit, not statically
52 if ( !wxApp::OnInit() )
55 wxJoystick
stick(wxJOYSTICK1
);
58 wxMessageBox(wxT("No joystick detected!"));
63 m_fire
.Create(wxT("buttonpress.wav"));
66 m_minX
= stick
.GetXMin();
67 m_minY
= stick
.GetYMin();
68 m_maxX
= stick
.GetXMax();
69 m_maxY
= stick
.GetYMax();
71 // Create the main frame window
73 frame
= new MyFrame(NULL
, wxT("Joystick Demo"), wxDefaultPosition
,
74 wxSize(500, 400), wxDEFAULT_FRAME_STYLE
| wxHSCROLL
| wxVSCROLL
);
76 frame
->SetIcon(wxICON(sample
));
79 wxMenu
*file_menu
= new wxMenu
;
81 file_menu
->Append(JOYTEST_QUIT
, wxT("&Exit"));
83 wxMenuBar
*menu_bar
= new wxMenuBar
;
85 menu_bar
->Append(file_menu
, wxT("&File"));
87 // Associate the menu bar with the frame
88 frame
->SetMenuBar(menu_bar
);
91 frame
->CreateStatusBar();
92 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()));
93 #endif // wxUSE_STATUSBAR
95 frame
->CenterOnScreen();
101 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
102 EVT_JOYSTICK_EVENTS(MyCanvas::OnJoystickEvent
)
105 // Define a constructor for my canvas
106 MyCanvas::MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
):
107 wxScrolledWindow(parent
, wxID_ANY
, pos
, size
, wxSUNKEN_BORDER
)
109 m_stick
= new wxJoystick(wxJOYSTICK1
);
110 nButtons
= m_stick
->GetNumberButtons();
111 m_stick
->SetCapture(this, 10);
114 MyCanvas::~MyCanvas()
116 m_stick
->ReleaseCapture();
120 void MyCanvas::OnJoystickEvent(wxJoystickEvent
& event
)
124 wxPoint
pt(event
.GetPosition());
126 // if negative positions are possible then shift everything up
127 int xmin
= wxGetApp().m_minX
;
128 int xmax
= wxGetApp().m_maxX
;
129 int ymin
= wxGetApp().m_minY
;
130 int ymax
= wxGetApp().m_maxY
;
140 // Scale to canvas size
144 pt
.x
= (long) (((double)pt
.x
/(double)xmax
) * cw
);
145 pt
.y
= (long) (((double)pt
.y
/(double)ymax
) * ch
);
147 if (xpos
> -1 && ypos
> -1 && event
.IsMove() && event
.ButtonIsDown())
149 dc
.SetPen(*wxBLACK_PEN
);
150 dc
.DrawLine(xpos
, ypos
, pt
.x
, pt
.y
);
158 if (event
.ButtonDown())
159 buf
.Printf(wxT("Joystick (%d, %d) #%i Fire!"), pt
.x
, pt
.y
, event
.GetButtonChange());
161 buf
.Printf(wxT("Joystick (%d, %d) "), pt
.x
, pt
.y
);
164 for(int i = 0; i < nButtons; ++i)
166 buf += wxString(wxT("[")) +
167 ((event.GetButtonState() & (1 << i)) ? wxT("Y") : wxT("N")) + wxString(wxT("]"));
171 frame
->SetStatusText(buf
);
172 #endif // wxUSE_STATUSBAR
175 if (event
.ButtonDown() && wxGetApp().m_fire
.IsOk())
177 wxGetApp().m_fire
.Play();
179 #endif // wxUSE_SOUND
182 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
183 EVT_MENU(JOYTEST_QUIT
, MyFrame::OnQuit
)
186 MyFrame::MyFrame(wxFrame
*parent
, const wxString
& title
, const wxPoint
& pos
,
187 const wxSize
& size
, const long style
)
188 : wxFrame(parent
, wxID_ANY
, title
, pos
, size
, style
)
190 canvas
= new MyCanvas(this);
193 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
198 void MyFrame::OnActivate(wxActivateEvent
& event
)
200 if (event
.GetActive() && canvas
)