joytest.cpp 5.0 KB

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