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