]>
git.saurik.com Git - wxWidgets.git/blob - samples/joytest/joytest.cpp
729073fe04c4539fd8b838c4ca9932b9ee55876f
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();
95 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
96 EVT_JOYSTICK_EVENTS(MyCanvas::OnJoystickEvent
)
99 // Define a constructor for my canvas
100 MyCanvas::MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
):
101 wxScrolledWindow(parent
, wxID_ANY
, pos
, size
, wxSUNKEN_BORDER
)
103 m_stick
= new wxJoystick(wxJOYSTICK1
);
104 nButtons
= m_stick
->GetNumberButtons();
105 m_stick
->SetCapture(this, 10);
108 MyCanvas::~MyCanvas()
110 m_stick
->ReleaseCapture();
114 void MyCanvas::OnJoystickEvent(wxJoystickEvent
& event
)
118 wxPoint
pt(event
.GetPosition());
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
;
134 // Scale to canvas size
138 pt
.x
= (long) (((double)pt
.x
/(double)xmax
) * cw
);
139 pt
.y
= (long) (((double)pt
.y
/(double)ymax
) * ch
);
141 if (xpos
> -1 && ypos
> -1 && event
.IsMove() && event
.ButtonIsDown())
143 dc
.SetPen(*wxBLACK_PEN
);
144 dc
.DrawLine(xpos
, ypos
, pt
.x
, pt
.y
);
152 if (event
.ButtonDown())
153 buf
.Printf(wxT("Joystick (%d, %d) #%i Fire!"), pt
.x
, pt
.y
, event
.GetButtonChange());
155 buf
.Printf(wxT("Joystick (%d, %d) "), pt
.x
, pt
.y
);
158 for(int i = 0; i < nButtons; ++i)
160 buf += wxString(wxT("[")) +
161 ((event.GetButtonState() & (1 << i)) ? wxT("Y") : wxT("N")) + wxString(wxT("]"));
165 frame
->SetStatusText(buf
);
166 #endif // wxUSE_STATUSBAR
169 if (event
.ButtonDown() && wxGetApp().m_fire
.IsOk())
171 wxGetApp().m_fire
.Play();
173 #endif // wxUSE_SOUND
176 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
177 EVT_MENU(JOYTEST_QUIT
, MyFrame::OnQuit
)
180 MyFrame::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
)
184 canvas
= new MyCanvas(this);
187 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
192 void MyFrame::OnActivate(wxActivateEvent
& event
)
194 if (event
.GetActive() && canvas
)