]>
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 MyFrame
*frame
= NULL
;
36 // For drawing lines in a canvas
43 // Initialise this in OnInit, not statically
46 if ( !wxApp::OnInit() )
49 wxJoystick
stick(wxJOYSTICK1
);
52 wxMessageBox(wxT("No joystick detected!"));
57 m_fire
.Create(wxT("buttonpress.wav"));
60 m_minX
= stick
.GetXMin();
61 m_minY
= stick
.GetYMin();
62 m_maxX
= stick
.GetXMax();
63 m_maxY
= stick
.GetYMax();
65 // Create the main frame window
67 frame
= new MyFrame(NULL
, wxT("Joystick Demo"), wxDefaultPosition
,
68 wxSize(500, 400), wxDEFAULT_FRAME_STYLE
| wxHSCROLL
| wxVSCROLL
);
70 frame
->SetIcon(wxICON(sample
));
73 wxMenu
*file_menu
= new wxMenu
;
75 file_menu
->Append(JOYTEST_QUIT
, wxT("&Exit"));
77 wxMenuBar
*menu_bar
= new wxMenuBar
;
79 menu_bar
->Append(file_menu
, wxT("&File"));
81 // Associate the menu bar with the frame
82 frame
->SetMenuBar(menu_bar
);
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
89 frame
->CenterOnScreen();
97 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
98 EVT_JOYSTICK_EVENTS(MyCanvas::OnJoystickEvent
)
101 // Define a constructor for my canvas
102 MyCanvas::MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
):
103 wxScrolledWindow(parent
, wxID_ANY
, pos
, size
, wxSUNKEN_BORDER
)
105 m_stick
= new wxJoystick(wxJOYSTICK1
);
106 nButtons
= m_stick
->GetNumberButtons();
107 m_stick
->SetCapture(this, 10);
110 MyCanvas::~MyCanvas()
112 m_stick
->ReleaseCapture();
116 void MyCanvas::OnJoystickEvent(wxJoystickEvent
& event
)
120 wxPoint
pt(event
.GetPosition());
122 // if negative positions are possible then shift everything up
123 int xmin
= wxGetApp().m_minX
;
124 int xmax
= wxGetApp().m_maxX
;
125 int ymin
= wxGetApp().m_minY
;
126 int ymax
= wxGetApp().m_maxY
;
136 // Scale to canvas size
140 pt
.x
= (long) (((double)pt
.x
/(double)xmax
) * cw
);
141 pt
.y
= (long) (((double)pt
.y
/(double)ymax
) * ch
);
143 if (xpos
> -1 && ypos
> -1 && event
.IsMove() && event
.ButtonIsDown())
145 dc
.SetPen(*wxBLACK_PEN
);
146 dc
.DrawLine(xpos
, ypos
, pt
.x
, pt
.y
);
154 if (event
.ButtonDown())
155 buf
.Printf(wxT("Joystick (%d, %d) #%i Fire!"), pt
.x
, pt
.y
, event
.GetButtonChange());
157 buf
.Printf(wxT("Joystick (%d, %d) "), pt
.x
, pt
.y
);
160 for(int i = 0; i < nButtons; ++i)
162 buf += wxString(wxT("[")) +
163 ((event.GetButtonState() & (1 << i)) ? wxT("Y") : wxT("N")) + wxString(wxT("]"));
167 frame
->SetStatusText(buf
);
168 #endif // wxUSE_STATUSBAR
171 if (event
.ButtonDown() && wxGetApp().m_fire
.IsOk())
173 wxGetApp().m_fire
.Play();
175 #endif // wxUSE_SOUND
178 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
179 EVT_MENU(JOYTEST_QUIT
, MyFrame::OnQuit
)
182 MyFrame::MyFrame(wxFrame
*parent
, const wxString
& title
, const wxPoint
& pos
,
183 const wxSize
& size
, const long style
)
184 : wxFrame(parent
, wxID_ANY
, title
, pos
, size
, style
)
186 canvas
= new MyCanvas(this);
189 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
194 void MyFrame::OnActivate(wxActivateEvent
& event
)
196 if (event
.GetActive() && canvas
)