]> git.saurik.com Git - wxWidgets.git/blame - samples/joytest/joytest.cpp
HP-UX fixes: use INSTALL_DIR as install doesn't have -d option; fixes for threads...
[wxWidgets.git] / samples / joytest / joytest.cpp
CommitLineData
bbf1f0e5
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: joytest.cpp
3// Purpose: Joystick sample
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart and Markus Holzem
2f6c54eb 9// Licence: wxWindows license
bbf1f0e5
KB
10/////////////////////////////////////////////////////////////////////////////
11
12// For compilers that support precompilation, includes "wx/wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
16#pragma hdrstop
17#endif
18
19#ifndef WX_PRECOMP
20#include "wx/wx.h"
21#endif
22
9832ce0b 23#if !wxUSE_JOYSTICK
aec18ff7
MB
24# error You must set wxUSE_JOYSTICK to 1 in setup.h
25#endif
26
27#if !wxUSE_STATUSBAR
28# error You must set wxUSE_STATUSBAR to 1 in setup.h
9832ce0b
GD
29#endif
30
31#include "wx/wave.h"
32#include "wx/joystick.h"
bbf1f0e5
KB
33
34#include "joytest.h"
35
36MyFrame *frame = NULL;
37
38IMPLEMENT_APP(MyApp)
39
40// For drawing lines in a canvas
41long xpos = -1;
42long ypos = -1;
43
44int winNumber = 1;
45
46// Initialise this in OnInit, not statically
aec18ff7 47bool MyApp::OnInit()
bbf1f0e5 48{
aec18ff7
MB
49 wxJoystick stick(wxJOYSTICK1);
50 if (!stick.IsOk())
51 {
600683ca 52 wxMessageBox(_T("No joystick detected!"));
aec18ff7
MB
53 return FALSE;
54 }
55
4c05b807 56#if wxUSE_WAVE
600683ca 57 m_fire.Create(_T("gun.wav"));
4c05b807 58#endif // wxUSE_WAVE
bbf1f0e5 59
aec18ff7
MB
60 m_maxX = stick.GetXMax();
61 m_maxY = stick.GetYMax();
bbf1f0e5 62
aec18ff7 63 // Create the main frame window
bbf1f0e5 64
600683ca 65 frame = new MyFrame(NULL, _T("Joystick Demo"), wxDefaultPosition,
aec18ff7 66 wxSize(500, 400), wxDEFAULT_FRAME_STYLE | wxHSCROLL | wxVSCROLL);
bbf1f0e5
KB
67
68 // Give it an icon (this is ignored in MDI mode: uses resources)
2049ba38 69#ifdef __WXMSW__
600683ca 70 frame->SetIcon(wxIcon(_T("joyicon")));
bbf1f0e5
KB
71#endif
72#ifdef __X__
600683ca 73 frame->SetIcon(wxIcon(_T("joyicon.xbm")));
bbf1f0e5
KB
74#endif
75
aec18ff7
MB
76 // Make a menubar
77 wxMenu *file_menu = new wxMenu;
bbf1f0e5 78
600683ca 79 file_menu->Append(JOYTEST_QUIT, _T("&Exit"));
bbf1f0e5 80
aec18ff7 81 wxMenuBar *menu_bar = new wxMenuBar;
bbf1f0e5 82
600683ca 83 menu_bar->Append(file_menu, _T("&File"));
bbf1f0e5 84
aec18ff7
MB
85 // Associate the menu bar with the frame
86 frame->SetMenuBar(menu_bar);
bbf1f0e5 87
aec18ff7 88 frame->CreateStatusBar();
bbf1f0e5 89
aec18ff7
MB
90 frame->CenterOnScreen();
91 frame->Show(TRUE);
bbf1f0e5 92
aec18ff7 93 SetTopWindow(frame);
bbf1f0e5 94
aec18ff7 95 return TRUE;
bbf1f0e5
KB
96}
97
98BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
2f6c54eb 99 EVT_JOYSTICK_EVENTS(MyCanvas::OnJoystickEvent)
bbf1f0e5
KB
100END_EVENT_TABLE()
101
102// Define a constructor for my canvas
103MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size):
aec18ff7 104 wxScrolledWindow(parent, -1, pos, size, wxSUNKEN_BORDER)
bbf1f0e5
KB
105{
106 wxJoystick joystick(wxJOYSTICK1);
107 joystick.SetCapture(this);
108}
109
aec18ff7 110MyCanvas::~MyCanvas()
bbf1f0e5
KB
111{
112 wxJoystick joystick(wxJOYSTICK1);
113 joystick.ReleaseCapture();
114}
115
116void MyCanvas::OnJoystickEvent(wxJoystickEvent& event)
117{
aec18ff7
MB
118 wxClientDC dc(this);
119
120 wxPoint pt(event.GetPosition());
121
122 // Scale to canvas size
123 int cw, ch;
124 GetSize(&cw, &ch);
bbf1f0e5 125
aec18ff7
MB
126 pt.x = (long) (((double)pt.x/(double)wxGetApp().m_maxX) * cw);
127 pt.y = (long) (((double)pt.y/(double)wxGetApp().m_maxY) * ch);
bbf1f0e5 128
aec18ff7
MB
129 if (xpos > -1 && ypos > -1 && event.IsMove() && event.ButtonIsDown())
130 {
131 dc.SetPen(*wxBLACK_PEN);
132 dc.DrawLine(xpos, ypos, pt.x, pt.y);
133 }
bbf1f0e5 134
aec18ff7
MB
135 xpos = pt.x;
136 ypos = pt.y;
bbf1f0e5 137
600683ca 138 wxString buf;
aec18ff7 139 if (event.ButtonDown())
600683ca 140 buf.Printf(_T("Joystick (%d, %d) Fire!"), pt.x, pt.y);
aec18ff7 141 else
600683ca 142 buf.Printf(_T("Joystick (%d, %d)"), pt.x, pt.y);
bbf1f0e5 143
aec18ff7 144 frame->SetStatusText(buf);
bbf1f0e5 145
4c05b807 146#if wxUSE_WAVE
aec18ff7
MB
147 if (event.ButtonDown() && wxGetApp().m_fire.IsOk())
148 {
149 wxGetApp().m_fire.Play();
150 }
4c05b807 151#endif // wxUSE_WAVE
bbf1f0e5
KB
152}
153
154BEGIN_EVENT_TABLE(MyFrame, wxFrame)
aec18ff7 155 EVT_MENU(JOYTEST_QUIT, MyFrame::OnQuit)
bbf1f0e5
KB
156END_EVENT_TABLE()
157
aec18ff7
MB
158MyFrame::MyFrame(wxFrame *parent, const wxString& title, const wxPoint& pos,
159 const wxSize& size, const long style)
160 : wxFrame(parent, -1, title, pos, size, style)
bbf1f0e5 161{
aec18ff7 162 canvas = new MyCanvas(this);
bbf1f0e5
KB
163}
164
aec18ff7 165MyFrame::~MyFrame()
bbf1f0e5 166{
aec18ff7 167 // Empty
bbf1f0e5
KB
168}
169
170void MyFrame::OnQuit(wxCommandEvent& event)
171{
aec18ff7 172 Close(TRUE);
bbf1f0e5
KB
173}
174
175void MyFrame::OnActivate(wxActivateEvent& event)
176{
aec18ff7
MB
177 if (event.GetActive() && canvas)
178 canvas->SetFocus();
bbf1f0e5 179}