]>
git.saurik.com Git - wxWidgets.git/blob - samples/joytest/joytest.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Joystick sample
4 // Author: Julian Smart
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx/wx.h".
12 #include "wx/wxprec.h"
23 # error You must set wxUSE_JOYSTICK to 1 in setup.h
27 #include "wx/joystick.h"
31 // the application icon (under Windows and OS/2 it is in resources and even
32 // though we could still include the XPM here it would be unused)
33 #ifndef wxHAS_IMAGES_IN_RESOURCES
34 #include "../sample.xpm"
37 MyFrame
*frame
= NULL
;
41 // For drawing lines in a canvas
48 // Initialise this in OnInit, not statically
51 if ( !wxApp::OnInit() )
54 wxJoystick
stick(wxJOYSTICK1
);
57 wxMessageBox(wxT("No joystick detected!"));
62 m_fire
.Create(wxT("buttonpress.wav"));
65 m_minX
= stick
.GetXMin();
66 m_minY
= stick
.GetYMin();
67 m_maxX
= stick
.GetXMax();
68 m_maxY
= stick
.GetYMax();
70 // Create the main frame window
72 frame
= new MyFrame(NULL
, wxT("Joystick Demo"), wxDefaultPosition
,
73 wxSize(500, 400), wxDEFAULT_FRAME_STYLE
| wxHSCROLL
| wxVSCROLL
);
75 frame
->SetIcon(wxICON(sample
));
78 wxMenu
*file_menu
= new wxMenu
;
80 file_menu
->Append(JOYTEST_QUIT
, wxT("&Exit"));
82 wxMenuBar
*menu_bar
= new wxMenuBar
;
84 menu_bar
->Append(file_menu
, wxT("&File"));
86 // Associate the menu bar with the frame
87 frame
->SetMenuBar(menu_bar
);
90 frame
->CreateStatusBar();
91 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()));
92 #endif // wxUSE_STATUSBAR
94 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
)
121 // We don't have valid (x, y) coordinates for z-move events.
122 if ( !event
.IsZMove() )
126 wxPoint
pt(event
.GetPosition());
128 // if negative positions are possible then shift everything up
129 int xmin
= wxGetApp().m_minX
;
130 int xmax
= wxGetApp().m_maxX
;
131 int ymin
= wxGetApp().m_minY
;
132 int ymax
= wxGetApp().m_maxY
;
142 // Scale to canvas size
146 pt
.x
= (long) (((double)pt
.x
/(double)xmax
) * cw
);
147 pt
.y
= (long) (((double)pt
.y
/(double)ymax
) * ch
);
149 if (xpos
> -1 && ypos
> -1 && event
.IsMove() && event
.ButtonIsDown())
151 dc
.SetPen(*wxBLACK_PEN
);
152 dc
.DrawLine(xpos
, ypos
, pt
.x
, pt
.y
);
161 if (event
.ButtonDown())
162 buf
.Printf(wxT("Joystick (%d, %d) #%i Fire!"), xpos
, ypos
, event
.GetButtonChange());
164 buf
.Printf(wxT("Joystick (%d, %d) "), xpos
, ypos
);
167 for(int i = 0; i < nButtons; ++i)
169 buf += wxString(wxT("[")) +
170 ((event.GetButtonState() & (1 << i)) ? wxT("Y") : wxT("N")) + wxString(wxT("]"));
174 frame
->SetStatusText(buf
);
175 #endif // wxUSE_STATUSBAR
178 if (event
.ButtonDown() && wxGetApp().m_fire
.IsOk())
180 wxGetApp().m_fire
.Play();
182 #endif // wxUSE_SOUND
185 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
186 EVT_MENU(JOYTEST_QUIT
, MyFrame::OnQuit
)
189 MyFrame::MyFrame(wxFrame
*parent
, const wxString
& title
, const wxPoint
& pos
,
190 const wxSize
& size
, const long style
)
191 : wxFrame(parent
, wxID_ANY
, title
, pos
, size
, style
)
193 canvas
= new MyCanvas(this);
196 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
201 void MyFrame::OnActivate(wxActivateEvent
& event
)
203 if (event
.GetActive() && canvas
)