1998-05-20 14:21:00 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: joytest.cpp
|
|
|
|
// Purpose: Joystick sample
|
|
|
|
// Author: Julian Smart
|
|
|
|
// Modified by:
|
|
|
|
// Created: 04/01/98
|
2003-03-17 11:55:54 +00:00
|
|
|
// Copyright: (c) Julian Smart
|
2010-07-13 13:29:13 +00:00
|
|
|
// Licence: wxWindows licence
|
1998-05-20 14:21:00 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// For compilers that support precompilation, includes "wx/wx.h".
|
|
|
|
#include "wx/wxprec.h"
|
|
|
|
|
|
|
|
#ifdef __BORLANDC__
|
|
|
|
#pragma hdrstop
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef WX_PRECOMP
|
|
|
|
#include "wx/wx.h"
|
|
|
|
#endif
|
|
|
|
|
2001-11-26 06:47:54 +00:00
|
|
|
#if !wxUSE_JOYSTICK
|
2002-08-01 19:12:24 +00:00
|
|
|
# error You must set wxUSE_JOYSTICK to 1 in setup.h
|
|
|
|
#endif
|
|
|
|
|
2004-02-02 13:03:23 +00:00
|
|
|
#include "wx/sound.h"
|
2001-11-26 06:47:54 +00:00
|
|
|
#include "wx/joystick.h"
|
1998-05-20 14:21:00 +00:00
|
|
|
|
|
|
|
#include "joytest.h"
|
|
|
|
|
2015-11-28 22:54:31 +00:00
|
|
|
// the application icon (under Windows it is in resources and even
|
2011-01-07 04:51:21 +00:00
|
|
|
// though we could still include the XPM here it would be unused)
|
2012-03-04 00:28:58 +00:00
|
|
|
#ifndef wxHAS_IMAGES_IN_RESOURCES
|
2011-01-07 04:51:21 +00:00
|
|
|
#include "../sample.xpm"
|
|
|
|
#endif
|
|
|
|
|
1998-05-20 14:21:00 +00:00
|
|
|
MyFrame *frame = NULL;
|
|
|
|
|
2015-04-23 11:49:01 +00:00
|
|
|
wxIMPLEMENT_APP(MyApp);
|
1998-05-20 14:21:00 +00:00
|
|
|
|
|
|
|
// For drawing lines in a canvas
|
|
|
|
long xpos = -1;
|
|
|
|
long ypos = -1;
|
|
|
|
|
|
|
|
int winNumber = 1;
|
|
|
|
|
2005-02-14 20:12:11 +00:00
|
|
|
int nButtons = 0;
|
1998-05-20 14:21:00 +00:00
|
|
|
// Initialise this in OnInit, not statically
|
2002-08-01 19:12:24 +00:00
|
|
|
bool MyApp::OnInit()
|
1998-05-20 14:21:00 +00:00
|
|
|
{
|
2007-02-04 00:34:18 +00:00
|
|
|
if ( !wxApp::OnInit() )
|
|
|
|
return false;
|
|
|
|
|
2002-08-01 19:12:24 +00:00
|
|
|
wxJoystick stick(wxJOYSTICK1);
|
|
|
|
if (!stick.IsOk())
|
|
|
|
{
|
2009-07-23 20:30:22 +00:00
|
|
|
wxMessageBox(wxT("No joystick detected!"));
|
2004-05-25 20:01:09 +00:00
|
|
|
return false;
|
2002-08-01 19:12:24 +00:00
|
|
|
}
|
|
|
|
|
2004-02-02 13:03:23 +00:00
|
|
|
#if wxUSE_SOUND
|
2009-07-23 20:30:22 +00:00
|
|
|
m_fire.Create(wxT("buttonpress.wav"));
|
2004-02-02 13:03:23 +00:00
|
|
|
#endif // wxUSE_SOUND
|
1998-05-20 14:21:00 +00:00
|
|
|
|
2004-05-14 02:52:05 +00:00
|
|
|
m_minX = stick.GetXMin();
|
|
|
|
m_minY = stick.GetYMin();
|
2002-08-01 19:12:24 +00:00
|
|
|
m_maxX = stick.GetXMax();
|
|
|
|
m_maxY = stick.GetYMax();
|
1998-05-20 14:21:00 +00:00
|
|
|
|
2002-08-01 19:12:24 +00:00
|
|
|
// Create the main frame window
|
1998-05-20 14:21:00 +00:00
|
|
|
|
2009-07-23 20:30:22 +00:00
|
|
|
frame = new MyFrame(NULL, wxT("Joystick Demo"), wxDefaultPosition,
|
2002-08-01 19:12:24 +00:00
|
|
|
wxSize(500, 400), wxDEFAULT_FRAME_STYLE | wxHSCROLL | wxVSCROLL);
|
1998-05-20 14:21:00 +00:00
|
|
|
|
2010-06-20 17:42:33 +00:00
|
|
|
frame->SetIcon(wxICON(sample));
|
1998-05-20 14:21:00 +00:00
|
|
|
|
2002-08-01 19:12:24 +00:00
|
|
|
// Make a menubar
|
|
|
|
wxMenu *file_menu = new wxMenu;
|
1998-05-20 14:21:00 +00:00
|
|
|
|
2009-07-23 20:30:22 +00:00
|
|
|
file_menu->Append(JOYTEST_QUIT, wxT("&Exit"));
|
1998-05-20 14:21:00 +00:00
|
|
|
|
2002-08-01 19:12:24 +00:00
|
|
|
wxMenuBar *menu_bar = new wxMenuBar;
|
1998-05-20 14:21:00 +00:00
|
|
|
|
2009-07-23 20:30:22 +00:00
|
|
|
menu_bar->Append(file_menu, wxT("&File"));
|
1998-05-20 14:21:00 +00:00
|
|
|
|
2002-08-01 19:12:24 +00:00
|
|
|
// Associate the menu bar with the frame
|
|
|
|
frame->SetMenuBar(menu_bar);
|
1998-05-20 14:21:00 +00:00
|
|
|
|
2004-07-19 15:36:01 +00:00
|
|
|
#if wxUSE_STATUSBAR
|
2002-08-01 19:12:24 +00:00
|
|
|
frame->CreateStatusBar();
|
2005-11-29 12:50:13 +00:00
|
|
|
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()));
|
2004-07-19 15:36:01 +00:00
|
|
|
#endif // wxUSE_STATUSBAR
|
1998-05-20 14:21:00 +00:00
|
|
|
|
2002-08-01 19:12:24 +00:00
|
|
|
frame->CenterOnScreen();
|
2004-05-25 20:01:09 +00:00
|
|
|
frame->Show(true);
|
1998-05-20 14:21:00 +00:00
|
|
|
|
2004-05-25 20:01:09 +00:00
|
|
|
return true;
|
1998-05-20 14:21:00 +00:00
|
|
|
}
|
|
|
|
|
2014-03-30 07:07:55 +00:00
|
|
|
wxBEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
|
2002-03-17 14:16:03 +00:00
|
|
|
EVT_JOYSTICK_EVENTS(MyCanvas::OnJoystickEvent)
|
2014-03-30 07:07:55 +00:00
|
|
|
wxEND_EVENT_TABLE()
|
1998-05-20 14:21:00 +00:00
|
|
|
|
|
|
|
// Define a constructor for my canvas
|
|
|
|
MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size):
|
2004-05-25 20:01:09 +00:00
|
|
|
wxScrolledWindow(parent, wxID_ANY, pos, size, wxSUNKEN_BORDER)
|
1998-05-20 14:21:00 +00:00
|
|
|
{
|
2004-05-14 02:52:05 +00:00
|
|
|
m_stick = new wxJoystick(wxJOYSTICK1);
|
2005-02-14 20:12:11 +00:00
|
|
|
nButtons = m_stick->GetNumberButtons();
|
2004-05-14 02:52:05 +00:00
|
|
|
m_stick->SetCapture(this, 10);
|
1998-05-20 14:21:00 +00:00
|
|
|
}
|
|
|
|
|
2002-08-01 19:12:24 +00:00
|
|
|
MyCanvas::~MyCanvas()
|
1998-05-20 14:21:00 +00:00
|
|
|
{
|
2004-05-14 02:52:05 +00:00
|
|
|
m_stick->ReleaseCapture();
|
|
|
|
delete m_stick;
|
1998-05-20 14:21:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MyCanvas::OnJoystickEvent(wxJoystickEvent& event)
|
|
|
|
{
|
2012-01-07 15:09:29 +00:00
|
|
|
// We don't have valid (x, y) coordinates for z-move events.
|
|
|
|
if ( !event.IsZMove() )
|
2002-08-01 19:12:24 +00:00
|
|
|
{
|
2012-01-07 15:09:29 +00:00
|
|
|
wxClientDC dc(this);
|
|
|
|
|
|
|
|
wxPoint pt(event.GetPosition());
|
|
|
|
|
|
|
|
// if negative positions are possible then shift everything up
|
|
|
|
int xmin = wxGetApp().m_minX;
|
|
|
|
int xmax = wxGetApp().m_maxX;
|
|
|
|
int ymin = wxGetApp().m_minY;
|
|
|
|
int ymax = wxGetApp().m_maxY;
|
|
|
|
if (xmin < 0) {
|
|
|
|
xmax += abs(xmin);
|
|
|
|
pt.x += abs(xmin);
|
|
|
|
}
|
|
|
|
if (ymin < 0) {
|
|
|
|
ymax += abs(ymin);
|
|
|
|
pt.y += abs(ymin);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scale to canvas size
|
|
|
|
int cw, ch;
|
|
|
|
GetSize(&cw, &ch);
|
|
|
|
|
|
|
|
pt.x = (long) (((double)pt.x/(double)xmax) * cw);
|
|
|
|
pt.y = (long) (((double)pt.y/(double)ymax) * ch);
|
|
|
|
|
|
|
|
if (xpos > -1 && ypos > -1 && event.IsMove() && event.ButtonIsDown())
|
|
|
|
{
|
|
|
|
dc.SetPen(*wxBLACK_PEN);
|
|
|
|
dc.DrawLine(xpos, ypos, pt.x, pt.y);
|
|
|
|
}
|
1998-05-20 14:21:00 +00:00
|
|
|
|
2012-01-07 16:35:02 +00:00
|
|
|
xpos = pt.x;
|
|
|
|
ypos = pt.y;
|
|
|
|
}
|
1998-05-20 14:21:00 +00:00
|
|
|
|
2004-07-19 15:36:01 +00:00
|
|
|
#if wxUSE_STATUSBAR
|
2002-12-15 17:25:36 +00:00
|
|
|
wxString buf;
|
2002-08-01 19:12:24 +00:00
|
|
|
if (event.ButtonDown())
|
2013-11-21 00:07:30 +00:00
|
|
|
buf.Printf(wxT("Joystick (%ld, %ld) #%i Fire!"), xpos, ypos, event.GetButtonChange());
|
2002-08-01 19:12:24 +00:00
|
|
|
else
|
2013-11-21 00:07:30 +00:00
|
|
|
buf.Printf(wxT("Joystick (%ld, %ld) "), xpos, ypos);
|
1998-05-20 14:21:00 +00:00
|
|
|
|
2005-02-14 20:12:11 +00:00
|
|
|
/*
|
|
|
|
for(int i = 0; i < nButtons; ++i)
|
|
|
|
{
|
2005-03-28 13:43:20 +00:00
|
|
|
buf += wxString(wxT("[")) +
|
2005-02-14 20:12:11 +00:00
|
|
|
((event.GetButtonState() & (1 << i)) ? wxT("Y") : wxT("N")) + wxString(wxT("]"));
|
|
|
|
}
|
|
|
|
*/
|
2005-03-28 13:43:20 +00:00
|
|
|
|
2002-08-01 19:12:24 +00:00
|
|
|
frame->SetStatusText(buf);
|
2004-07-19 15:36:01 +00:00
|
|
|
#endif // wxUSE_STATUSBAR
|
1998-05-20 14:21:00 +00:00
|
|
|
|
2004-02-02 13:03:23 +00:00
|
|
|
#if wxUSE_SOUND
|
2002-08-01 19:12:24 +00:00
|
|
|
if (event.ButtonDown() && wxGetApp().m_fire.IsOk())
|
|
|
|
{
|
|
|
|
wxGetApp().m_fire.Play();
|
|
|
|
}
|
2004-02-02 13:03:23 +00:00
|
|
|
#endif // wxUSE_SOUND
|
1998-05-20 14:21:00 +00:00
|
|
|
}
|
|
|
|
|
2014-03-30 07:07:55 +00:00
|
|
|
wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
|
2002-08-01 19:12:24 +00:00
|
|
|
EVT_MENU(JOYTEST_QUIT, MyFrame::OnQuit)
|
2014-03-30 07:07:55 +00:00
|
|
|
wxEND_EVENT_TABLE()
|
1998-05-20 14:21:00 +00:00
|
|
|
|
2002-08-01 19:12:24 +00:00
|
|
|
MyFrame::MyFrame(wxFrame *parent, const wxString& title, const wxPoint& pos,
|
|
|
|
const wxSize& size, const long style)
|
2004-05-25 20:01:09 +00:00
|
|
|
: wxFrame(parent, wxID_ANY, title, pos, size, style)
|
1998-05-20 14:21:00 +00:00
|
|
|
{
|
2002-08-01 19:12:24 +00:00
|
|
|
canvas = new MyCanvas(this);
|
1998-05-20 14:21:00 +00:00
|
|
|
}
|
|
|
|
|
2003-09-09 20:24:34 +00:00
|
|
|
void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
|
1998-05-20 14:21:00 +00:00
|
|
|
{
|
2004-05-25 20:01:09 +00:00
|
|
|
Close(true);
|
1998-05-20 14:21:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MyFrame::OnActivate(wxActivateEvent& event)
|
|
|
|
{
|
2002-08-01 19:12:24 +00:00
|
|
|
if (event.GetActive() && canvas)
|
|
|
|
canvas->SetFocus();
|
1998-05-20 14:21:00 +00:00
|
|
|
}
|