]>
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 # error You must set wxUSE_STATUSBAR to 1 in setup.h
32 #include "wx/joystick.h"
36 MyFrame
*frame
= NULL
;
40 // For drawing lines in a canvas
46 // Initialise this in OnInit, not statically
49 wxJoystick
stick(wxJOYSTICK1
);
52 wxMessageBox(_T("No joystick detected!"));
57 m_fire
.Create(_T("gun.wav"));
60 m_maxX
= stick
.GetXMax();
61 m_maxY
= stick
.GetYMax();
63 // Create the main frame window
65 frame
= new MyFrame(NULL
, _T("Joystick Demo"), wxDefaultPosition
,
66 wxSize(500, 400), wxDEFAULT_FRAME_STYLE
| wxHSCROLL
| wxVSCROLL
);
68 // Give it an icon (this is ignored in MDI mode: uses resources)
70 frame
->SetIcon(wxIcon(_T("joyicon")));
73 frame
->SetIcon(wxIcon(_T("joyicon.xbm")));
77 wxMenu
*file_menu
= new wxMenu
;
79 file_menu
->Append(JOYTEST_QUIT
, _T("&Exit"));
81 wxMenuBar
*menu_bar
= new wxMenuBar
;
83 menu_bar
->Append(file_menu
, _T("&File"));
85 // Associate the menu bar with the frame
86 frame
->SetMenuBar(menu_bar
);
88 frame
->CreateStatusBar();
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
, -1, pos
, size
, wxSUNKEN_BORDER
)
106 wxJoystick
joystick(wxJOYSTICK1
);
107 joystick
.SetCapture(this);
110 MyCanvas::~MyCanvas()
112 wxJoystick
joystick(wxJOYSTICK1
);
113 joystick
.ReleaseCapture();
116 void MyCanvas::OnJoystickEvent(wxJoystickEvent
& event
)
120 wxPoint
pt(event
.GetPosition());
122 // Scale to canvas size
126 pt
.x
= (long) (((double)pt
.x
/(double)wxGetApp().m_maxX
) * cw
);
127 pt
.y
= (long) (((double)pt
.y
/(double)wxGetApp().m_maxY
) * ch
);
129 if (xpos
> -1 && ypos
> -1 && event
.IsMove() && event
.ButtonIsDown())
131 dc
.SetPen(*wxBLACK_PEN
);
132 dc
.DrawLine(xpos
, ypos
, pt
.x
, pt
.y
);
139 if (event
.ButtonDown())
140 buf
.Printf(_T("Joystick (%d, %d) Fire!"), pt
.x
, pt
.y
);
142 buf
.Printf(_T("Joystick (%d, %d)"), pt
.x
, pt
.y
);
144 frame
->SetStatusText(buf
);
147 if (event
.ButtonDown() && wxGetApp().m_fire
.IsOk())
149 wxGetApp().m_fire
.Play();
154 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
155 EVT_MENU(JOYTEST_QUIT
, MyFrame::OnQuit
)
158 MyFrame::MyFrame(wxFrame
*parent
, const wxString
& title
, const wxPoint
& pos
,
159 const wxSize
& size
, const long style
)
160 : wxFrame(parent
, -1, title
, pos
, size
, style
)
162 canvas
= new MyCanvas(this);
170 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
175 void MyFrame::OnActivate(wxActivateEvent
& event
)
177 if (event
.GetActive() && canvas
)