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
|
|
|
|
|
|
|
|
#if !wxUSE_STATUSBAR
|
|
|
|
# error You must set wxUSE_STATUSBAR to 1 in setup.h
|
2001-11-26 01:47:54 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "wx/wave.h"
|
|
|
|
#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;
|
|
|
|
|
|
|
|
// 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!"));
|
2002-08-01 15:12:24 -04:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2001-07-02 06:12:40 -04:00
|
|
|
#if wxUSE_WAVE
|
2002-12-15 12:25:36 -05:00
|
|
|
m_fire.Create(_T("gun.wav"));
|
2001-07-02 06:12:40 -04:00
|
|
|
#endif // wxUSE_WAVE
|
1998-05-20 10:21:00 -04:00
|
|
|
|
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
|
|
|
|
2002-08-01 15:12:24 -04:00
|
|
|
frame->CreateStatusBar();
|
1998-05-20 10:21:00 -04:00
|
|
|
|
2002-08-01 15:12:24 -04:00
|
|
|
frame->CenterOnScreen();
|
|
|
|
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
|
|
|
|
2002-08-01 15:12:24 -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):
|
2002-08-01 15:12:24 -04:00
|
|
|
wxScrolledWindow(parent, -1, pos, size, wxSUNKEN_BORDER)
|
1998-05-20 10:21:00 -04:00
|
|
|
{
|
|
|
|
wxJoystick joystick(wxJOYSTICK1);
|
|
|
|
joystick.SetCapture(this);
|
|
|
|
}
|
|
|
|
|
2002-08-01 15:12:24 -04:00
|
|
|
MyCanvas::~MyCanvas()
|
1998-05-20 10:21:00 -04:00
|
|
|
{
|
|
|
|
wxJoystick joystick(wxJOYSTICK1);
|
|
|
|
joystick.ReleaseCapture();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyCanvas::OnJoystickEvent(wxJoystickEvent& event)
|
|
|
|
{
|
2002-08-01 15:12:24 -04:00
|
|
|
wxClientDC dc(this);
|
|
|
|
|
|
|
|
wxPoint pt(event.GetPosition());
|
|
|
|
|
|
|
|
// Scale to canvas size
|
|
|
|
int cw, ch;
|
|
|
|
GetSize(&cw, &ch);
|
1998-05-20 10:21:00 -04:00
|
|
|
|
2002-08-01 15:12:24 -04:00
|
|
|
pt.x = (long) (((double)pt.x/(double)wxGetApp().m_maxX) * cw);
|
|
|
|
pt.y = (long) (((double)pt.y/(double)wxGetApp().m_maxY) * 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
|
|
|
|
2002-12-15 12:25:36 -05:00
|
|
|
wxString buf;
|
2002-08-01 15:12:24 -04:00
|
|
|
if (event.ButtonDown())
|
2002-12-15 12:25:36 -05:00
|
|
|
buf.Printf(_T("Joystick (%d, %d) Fire!"), pt.x, pt.y);
|
2002-08-01 15:12:24 -04:00
|
|
|
else
|
2002-12-15 12:25:36 -05:00
|
|
|
buf.Printf(_T("Joystick (%d, %d)"), pt.x, pt.y);
|
1998-05-20 10:21:00 -04:00
|
|
|
|
2002-08-01 15:12:24 -04:00
|
|
|
frame->SetStatusText(buf);
|
1998-05-20 10:21:00 -04:00
|
|
|
|
2001-07-02 06:12:40 -04:00
|
|
|
#if wxUSE_WAVE
|
2002-08-01 15:12:24 -04:00
|
|
|
if (event.ButtonDown() && wxGetApp().m_fire.IsOk())
|
|
|
|
{
|
|
|
|
wxGetApp().m_fire.Play();
|
|
|
|
}
|
2001-07-02 06:12:40 -04:00
|
|
|
#endif // wxUSE_WAVE
|
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)
|
|
|
|
: wxFrame(parent, -1, 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
|
|
|
}
|
|
|
|
|
2002-08-01 15:12:24 -04:00
|
|
|
MyFrame::~MyFrame()
|
1998-05-20 10:21:00 -04:00
|
|
|
{
|
2002-08-01 15:12:24 -04:00
|
|
|
// Empty
|
1998-05-20 10:21:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void MyFrame::OnQuit(wxCommandEvent& event)
|
|
|
|
{
|
2002-08-01 15:12:24 -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
|
|
|
}
|