| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: joytest.cpp |
| 3 | // Purpose: Joystick sample |
| 4 | // Author: Julian Smart |
| 5 | // Modified by: |
| 6 | // Created: 04/01/98 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) Julian Smart |
| 9 | // Licence: wxWindows licence |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | // For compilers that support precompilation, includes "wx/wx.h". |
| 13 | #include "wx/wxprec.h" |
| 14 | |
| 15 | #ifdef __BORLANDC__ |
| 16 | #pragma hdrstop |
| 17 | #endif |
| 18 | |
| 19 | #ifndef WX_PRECOMP |
| 20 | #include "wx/wx.h" |
| 21 | #endif |
| 22 | |
| 23 | #if !wxUSE_JOYSTICK |
| 24 | # error You must set wxUSE_JOYSTICK to 1 in setup.h |
| 25 | #endif |
| 26 | |
| 27 | #include "wx/sound.h" |
| 28 | #include "wx/joystick.h" |
| 29 | |
| 30 | #include "joytest.h" |
| 31 | |
| 32 | // the application icon (under Windows and OS/2 it is in resources and even |
| 33 | // though we could still include the XPM here it would be unused) |
| 34 | #ifndef wxHAS_IMAGES_IN_RESOURCES |
| 35 | #include "../sample.xpm" |
| 36 | #endif |
| 37 | |
| 38 | MyFrame *frame = NULL; |
| 39 | |
| 40 | IMPLEMENT_APP(MyApp) |
| 41 | |
| 42 | // For drawing lines in a canvas |
| 43 | long xpos = -1; |
| 44 | long ypos = -1; |
| 45 | |
| 46 | int winNumber = 1; |
| 47 | |
| 48 | int nButtons = 0; |
| 49 | // Initialise this in OnInit, not statically |
| 50 | bool MyApp::OnInit() |
| 51 | { |
| 52 | if ( !wxApp::OnInit() ) |
| 53 | return false; |
| 54 | |
| 55 | wxJoystick stick(wxJOYSTICK1); |
| 56 | if (!stick.IsOk()) |
| 57 | { |
| 58 | wxMessageBox(wxT("No joystick detected!")); |
| 59 | return false; |
| 60 | } |
| 61 | |
| 62 | #if wxUSE_SOUND |
| 63 | m_fire.Create(wxT("buttonpress.wav")); |
| 64 | #endif // wxUSE_SOUND |
| 65 | |
| 66 | m_minX = stick.GetXMin(); |
| 67 | m_minY = stick.GetYMin(); |
| 68 | m_maxX = stick.GetXMax(); |
| 69 | m_maxY = stick.GetYMax(); |
| 70 | |
| 71 | // Create the main frame window |
| 72 | |
| 73 | frame = new MyFrame(NULL, wxT("Joystick Demo"), wxDefaultPosition, |
| 74 | wxSize(500, 400), wxDEFAULT_FRAME_STYLE | wxHSCROLL | wxVSCROLL); |
| 75 | |
| 76 | frame->SetIcon(wxICON(sample)); |
| 77 | |
| 78 | // Make a menubar |
| 79 | wxMenu *file_menu = new wxMenu; |
| 80 | |
| 81 | file_menu->Append(JOYTEST_QUIT, wxT("&Exit")); |
| 82 | |
| 83 | wxMenuBar *menu_bar = new wxMenuBar; |
| 84 | |
| 85 | menu_bar->Append(file_menu, wxT("&File")); |
| 86 | |
| 87 | // Associate the menu bar with the frame |
| 88 | frame->SetMenuBar(menu_bar); |
| 89 | |
| 90 | #if wxUSE_STATUSBAR |
| 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 |
| 94 | |
| 95 | frame->CenterOnScreen(); |
| 96 | frame->Show(true); |
| 97 | |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow) |
| 102 | EVT_JOYSTICK_EVENTS(MyCanvas::OnJoystickEvent) |
| 103 | END_EVENT_TABLE() |
| 104 | |
| 105 | // Define a constructor for my canvas |
| 106 | MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size): |
| 107 | wxScrolledWindow(parent, wxID_ANY, pos, size, wxSUNKEN_BORDER) |
| 108 | { |
| 109 | m_stick = new wxJoystick(wxJOYSTICK1); |
| 110 | nButtons = m_stick->GetNumberButtons(); |
| 111 | m_stick->SetCapture(this, 10); |
| 112 | } |
| 113 | |
| 114 | MyCanvas::~MyCanvas() |
| 115 | { |
| 116 | m_stick->ReleaseCapture(); |
| 117 | delete m_stick; |
| 118 | } |
| 119 | |
| 120 | void MyCanvas::OnJoystickEvent(wxJoystickEvent& event) |
| 121 | { |
| 122 | // We don't have valid (x, y) coordinates for z-move events. |
| 123 | if ( !event.IsZMove() ) |
| 124 | { |
| 125 | wxClientDC dc(this); |
| 126 | |
| 127 | wxPoint pt(event.GetPosition()); |
| 128 | |
| 129 | // if negative positions are possible then shift everything up |
| 130 | int xmin = wxGetApp().m_minX; |
| 131 | int xmax = wxGetApp().m_maxX; |
| 132 | int ymin = wxGetApp().m_minY; |
| 133 | int ymax = wxGetApp().m_maxY; |
| 134 | if (xmin < 0) { |
| 135 | xmax += abs(xmin); |
| 136 | pt.x += abs(xmin); |
| 137 | } |
| 138 | if (ymin < 0) { |
| 139 | ymax += abs(ymin); |
| 140 | pt.y += abs(ymin); |
| 141 | } |
| 142 | |
| 143 | // Scale to canvas size |
| 144 | int cw, ch; |
| 145 | GetSize(&cw, &ch); |
| 146 | |
| 147 | pt.x = (long) (((double)pt.x/(double)xmax) * cw); |
| 148 | pt.y = (long) (((double)pt.y/(double)ymax) * ch); |
| 149 | |
| 150 | if (xpos > -1 && ypos > -1 && event.IsMove() && event.ButtonIsDown()) |
| 151 | { |
| 152 | dc.SetPen(*wxBLACK_PEN); |
| 153 | dc.DrawLine(xpos, ypos, pt.x, pt.y); |
| 154 | } |
| 155 | |
| 156 | xpos = pt.x; |
| 157 | ypos = pt.y; |
| 158 | } |
| 159 | |
| 160 | #if wxUSE_STATUSBAR |
| 161 | wxString buf; |
| 162 | if (event.ButtonDown()) |
| 163 | buf.Printf(wxT("Joystick (%d, %d) #%i Fire!"), xpos, ypos, event.GetButtonChange()); |
| 164 | else |
| 165 | buf.Printf(wxT("Joystick (%d, %d) "), xpos, ypos); |
| 166 | |
| 167 | /* |
| 168 | for(int i = 0; i < nButtons; ++i) |
| 169 | { |
| 170 | buf += wxString(wxT("[")) + |
| 171 | ((event.GetButtonState() & (1 << i)) ? wxT("Y") : wxT("N")) + wxString(wxT("]")); |
| 172 | } |
| 173 | */ |
| 174 | |
| 175 | frame->SetStatusText(buf); |
| 176 | #endif // wxUSE_STATUSBAR |
| 177 | |
| 178 | #if wxUSE_SOUND |
| 179 | if (event.ButtonDown() && wxGetApp().m_fire.IsOk()) |
| 180 | { |
| 181 | wxGetApp().m_fire.Play(); |
| 182 | } |
| 183 | #endif // wxUSE_SOUND |
| 184 | } |
| 185 | |
| 186 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) |
| 187 | EVT_MENU(JOYTEST_QUIT, MyFrame::OnQuit) |
| 188 | END_EVENT_TABLE() |
| 189 | |
| 190 | MyFrame::MyFrame(wxFrame *parent, const wxString& title, const wxPoint& pos, |
| 191 | const wxSize& size, const long style) |
| 192 | : wxFrame(parent, wxID_ANY, title, pos, size, style) |
| 193 | { |
| 194 | canvas = new MyCanvas(this); |
| 195 | } |
| 196 | |
| 197 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) |
| 198 | { |
| 199 | Close(true); |
| 200 | } |
| 201 | |
| 202 | void MyFrame::OnActivate(wxActivateEvent& event) |
| 203 | { |
| 204 | if (event.GetActive() && canvas) |
| 205 | canvas->SetFocus(); |
| 206 | } |