]>
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
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 // Give it an icon (this is ignored in MDI mode: uses resources)
72 frame
->SetIcon(wxIcon(wxT("joyicon")));
75 frame
->SetIcon(wxIcon(wxT("joyicon.xbm")));
79 wxMenu
*file_menu
= new wxMenu
;
81 file_menu
->Append(JOYTEST_QUIT
, wxT("&Exit"));
83 wxMenuBar
*menu_bar
= new wxMenuBar
;
85 menu_bar
->Append(file_menu
, wxT("&File"));
87 // Associate the menu bar with the frame
88 frame
->SetMenuBar(menu_bar
);
91 frame
->CreateStatusBar();
92 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()));
93 #endif // wxUSE_STATUSBAR
95 frame
->CenterOnScreen();
103 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
104 EVT_JOYSTICK_EVENTS(MyCanvas::OnJoystickEvent
)
107 // Define a constructor for my canvas
108 MyCanvas::MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
):
109 wxScrolledWindow(parent
, wxID_ANY
, pos
, size
, wxSUNKEN_BORDER
)
111 m_stick
= new wxJoystick(wxJOYSTICK1
);
112 nButtons
= m_stick
->GetNumberButtons();
113 m_stick
->SetCapture(this, 10);
116 MyCanvas::~MyCanvas()
118 m_stick
->ReleaseCapture();
122 void MyCanvas::OnJoystickEvent(wxJoystickEvent
& event
)
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
);
160 if (event
.ButtonDown())
161 buf
.Printf(wxT("Joystick (%d, %d) #%i Fire!"), pt
.x
, pt
.y
, event
.GetButtonChange());
163 buf
.Printf(wxT("Joystick (%d, %d) "), pt
.x
, pt
.y
);
166 for(int i = 0; i < nButtons; ++i)
168 buf += wxString(wxT("[")) +
169 ((event.GetButtonState() & (1 << i)) ? wxT("Y") : wxT("N")) + wxString(wxT("]"));
173 frame
->SetStatusText(buf
);
174 #endif // wxUSE_STATUSBAR
177 if (event
.ButtonDown() && wxGetApp().m_fire
.IsOk())
179 wxGetApp().m_fire
.Play();
181 #endif // wxUSE_SOUND
184 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
185 EVT_MENU(JOYTEST_QUIT
, MyFrame::OnQuit
)
188 MyFrame::MyFrame(wxFrame
*parent
, const wxString
& title
, const wxPoint
& pos
,
189 const wxSize
& size
, const long style
)
190 : wxFrame(parent
, wxID_ANY
, title
, pos
, size
, style
)
192 canvas
= new MyCanvas(this);
195 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
200 void MyFrame::OnActivate(wxActivateEvent
& event
)
202 if (event
.GetActive() && canvas
)