]>
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_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
, _T("Joystick Demo"), wxDefaultPosition
,
68 wxSize(500, 400), wxDEFAULT_FRAME_STYLE
| wxHSCROLL
| wxVSCROLL
);
70 // Give it an icon (this is ignored in MDI mode: uses resources)
72 frame
->SetIcon(wxIcon(_T("joyicon")));
75 frame
->SetIcon(wxIcon(_T("joyicon.xbm")));
79 wxMenu
*file_menu
= new wxMenu
;
81 file_menu
->Append(JOYTEST_QUIT
, _T("&Exit"));
83 wxMenuBar
*menu_bar
= new wxMenuBar
;
85 menu_bar
->Append(file_menu
, _T("&File"));
87 // Associate the menu bar with the frame
88 frame
->SetMenuBar(menu_bar
);
90 frame
->CreateStatusBar();
92 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
, -1, pos
, size
, wxSUNKEN_BORDER
)
108 m_stick
= new wxJoystick(wxJOYSTICK1
);
109 m_stick
->SetCapture(this, 10);
112 MyCanvas::~MyCanvas()
114 m_stick
->ReleaseCapture();
118 void MyCanvas::OnJoystickEvent(wxJoystickEvent
& event
)
122 wxPoint
pt(event
.GetPosition());
124 // if negative positions are possible then shift everything up
125 int xmin
= wxGetApp().m_minX
;
126 int xmax
= wxGetApp().m_maxX
;
127 int ymin
= wxGetApp().m_minY
;
128 int ymax
= wxGetApp().m_maxY
;
140 // Scale to canvas size
144 pt
.x
= (long) (((double)pt
.x
/(double)xmax
) * cw
);
145 pt
.y
= (long) (((double)pt
.y
/(double)ymax
) * ch
);
147 if (xpos
> -1 && ypos
> -1 && event
.IsMove() && event
.ButtonIsDown())
149 dc
.SetPen(*wxBLACK_PEN
);
150 dc
.DrawLine(xpos
, ypos
, pt
.x
, pt
.y
);
157 if (event
.ButtonDown())
158 buf
.Printf(_T("Joystick (%d, %d) Fire!"), pt
.x
, pt
.y
);
160 buf
.Printf(_T("Joystick (%d, %d)"), pt
.x
, pt
.y
);
162 frame
->SetStatusText(buf
);
165 if (event
.ButtonDown() && wxGetApp().m_fire
.IsOk())
167 wxGetApp().m_fire
.Play();
169 #endif // wxUSE_SOUND
172 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
173 EVT_MENU(JOYTEST_QUIT
, MyFrame::OnQuit
)
176 MyFrame::MyFrame(wxFrame
*parent
, const wxString
& title
, const wxPoint
& pos
,
177 const wxSize
& size
, const long style
)
178 : wxFrame(parent
, -1, title
, pos
, size
, style
)
180 canvas
= new MyCanvas(this);
188 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
193 void MyFrame::OnActivate(wxActivateEvent
& event
)
195 if (event
.GetActive() && canvas
)