]>
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
42 // Initialise this in OnInit, not statically
45 wxJoystick
stick(wxJOYSTICK1
);
48 wxMessageBox(_T("No joystick detected!"));
53 m_fire
.Create(_T("gun.wav"));
56 m_minX
= stick
.GetXMin();
57 m_minY
= stick
.GetYMin();
58 m_maxX
= stick
.GetXMax();
59 m_maxY
= stick
.GetYMax();
61 // Create the main frame window
63 frame
= new MyFrame(NULL
, _T("Joystick Demo"), wxDefaultPosition
,
64 wxSize(500, 400), wxDEFAULT_FRAME_STYLE
| wxHSCROLL
| wxVSCROLL
);
66 // Give it an icon (this is ignored in MDI mode: uses resources)
68 frame
->SetIcon(wxIcon(_T("joyicon")));
71 frame
->SetIcon(wxIcon(_T("joyicon.xbm")));
75 wxMenu
*file_menu
= new wxMenu
;
77 file_menu
->Append(JOYTEST_QUIT
, _T("&Exit"));
79 wxMenuBar
*menu_bar
= new wxMenuBar
;
81 menu_bar
->Append(file_menu
, _T("&File"));
83 // Associate the menu bar with the frame
84 frame
->SetMenuBar(menu_bar
);
87 frame
->CreateStatusBar();
88 #endif // wxUSE_STATUSBAR
90 frame
->CenterOnScreen();
98 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
99 EVT_JOYSTICK_EVENTS(MyCanvas::OnJoystickEvent
)
102 // Define a constructor for my canvas
103 MyCanvas::MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
):
104 wxScrolledWindow(parent
, wxID_ANY
, pos
, size
, wxSUNKEN_BORDER
)
106 m_stick
= new wxJoystick(wxJOYSTICK1
);
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(_T("Joystick (%d, %d) Fire!"), pt
.x
, pt
.y
);
157 buf
.Printf(_T("Joystick (%d, %d)"), pt
.x
, pt
.y
);
159 frame
->SetStatusText(buf
);
160 #endif // wxUSE_STATUSBAR
163 if (event
.ButtonDown() && wxGetApp().m_fire
.IsOk())
165 wxGetApp().m_fire
.Play();
167 #endif // wxUSE_SOUND
170 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
171 EVT_MENU(JOYTEST_QUIT
, MyFrame::OnQuit
)
174 MyFrame::MyFrame(wxFrame
*parent
, const wxString
& title
, const wxPoint
& pos
,
175 const wxSize
& size
, const long style
)
176 : wxFrame(parent
, wxID_ANY
, title
, pos
, size
, style
)
178 canvas
= new MyCanvas(this);
181 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
186 void MyFrame::OnActivate(wxActivateEvent
& event
)
188 if (event
.GetActive() && canvas
)