1998-05-20 10:21:00 -04:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: joytest.cpp
|
|
|
|
// Purpose: Joystick sample
|
|
|
|
// Author: Julian Smart
|
|
|
|
// Modified by:
|
|
|
|
// Created: 04/01/98
|
|
|
|
// RCS-ID: $Id$
|
2003-03-17 06:55:54 -05:00
|
|
|
// Copyright: (c) Julian Smart
|
2002-03-17 09:16:03 -05:00
|
|
|
// Licence: wxWindows license
|
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"
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2002-08-01 15:12:24 -04:00
|
|
|
wxJoystick stick(wxJOYSTICK1);
|
|
|
|
if (!stick.IsOk())
|
|
|
|
{
|
2002-12-15 12:25:36 -05:00
|
|
|
wxMessageBox(_T("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
|
2005-03-28 08:43:20 -05:00
|
|
|
m_fire.Create(_T("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
|
|
|
|
2002-12-15 12:25:36 -05:00
|
|
|
frame = new MyFrame(NULL, _T("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
|
|
|
|
|
|
|
// Give it an icon (this is ignored in MDI mode: uses resources)
|
1998-07-10 10:15:17 -04:00
|
|
|
#ifdef __WXMSW__
|
2002-12-15 12:25:36 -05:00
|
|
|
frame->SetIcon(wxIcon(_T("joyicon")));
|
1998-05-20 10:21:00 -04:00
|
|
|
#endif
|
|
|
|
#ifdef __X__
|
2002-12-15 12:25:36 -05:00
|
|
|
frame->SetIcon(wxIcon(_T("joyicon.xbm")));
|
1998-05-20 10:21:00 -04:00
|
|
|
#endif
|
|
|
|
|
2002-08-01 15:12:24 -04:00
|
|
|
// Make a menubar
|
|
|
|
wxMenu *file_menu = new wxMenu;
|
1998-05-20 10:21:00 -04:00
|
|
|
|
2002-12-15 12:25:36 -05:00
|
|
|
file_menu->Append(JOYTEST_QUIT, _T("&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
|
|
|
|
2002-12-15 12:25:36 -05:00
|
|
|
menu_bar->Append(file_menu, _T("&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
|
|
|
|
2002-08-01 15:12:24 -04:00
|
|
|
SetTopWindow(frame);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
|
2002-03-17 09:16:03 -05:00
|
|
|
EVT_JOYSTICK_EVENTS(MyCanvas::OnJoystickEvent)
|
1998-05-20 10:21:00 -04:00
|
|
|
END_EVENT_TABLE()
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
{
|
2002-08-01 15:12:24 -04:00
|
|
|
wxClientDC dc(this);
|
|
|
|
|
|
|
|
wxPoint pt(event.GetPosition());
|
|
|
|
|
2004-05-13 22:52:05 -04:00
|
|
|
// 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);
|
|
|
|
}
|
2004-10-06 16:54:57 -04:00
|
|
|
|
2002-08-01 15:12:24 -04:00
|
|
|
// Scale to canvas size
|
|
|
|
int cw, ch;
|
|
|
|
GetSize(&cw, &ch);
|
1998-05-20 10:21:00 -04:00
|
|
|
|
2004-05-13 22:52:05 -04:00
|
|
|
pt.x = (long) (((double)pt.x/(double)xmax) * cw);
|
|
|
|
pt.y = (long) (((double)pt.y/(double)ymax) * ch);
|
1998-05-20 10:21:00 -04:00
|
|
|
|
2002-08-01 15:12:24 -04:00
|
|
|
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
|
|
|
|
2002-08-01 15:12:24 -04: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())
|
2005-02-14 15:12:11 -05:00
|
|
|
buf.Printf(_T("Joystick (%d, %d) #%i Fire!"), pt.x, pt.y, event.GetButtonChange());
|
2002-08-01 15:12:24 -04:00
|
|
|
else
|
2005-02-14 15:12:11 -05:00
|
|
|
buf.Printf(_T("Joystick (%d, %d) "), pt.x, pt.y);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
|
2002-08-01 15:12:24 -04:00
|
|
|
EVT_MENU(JOYTEST_QUIT, MyFrame::OnQuit)
|
1998-05-20 10:21:00 -04:00
|
|
|
END_EVENT_TABLE()
|
|
|
|
|
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
|
|
|
}
|