]> git.saurik.com Git - wxWidgets.git/blame - samples/joytest/joytest.cpp
implemented 'shaft scrolling' of children in wxUnivWindow::ScrollWindow
[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
9// Licence: wxWindows license
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
GD
23#if !wxUSE_JOYSTICK
24#error You must set wxUSE_JOYSTICK to 1 in setup.h!
25#endif
26
27#include "wx/wave.h"
28#include "wx/joystick.h"
bbf1f0e5
KB
29
30#include "joytest.h"
31
32MyFrame *frame = NULL;
33
34IMPLEMENT_APP(MyApp)
35
36// For drawing lines in a canvas
37long xpos = -1;
38long ypos = -1;
39
40int winNumber = 1;
41
42// Initialise this in OnInit, not statically
43bool MyApp::OnInit(void)
44{
45 wxJoystick stick(wxJOYSTICK1);
46 if (!stick.IsOk())
47 {
48 wxMessageBox("No joystick detected!");
49 return FALSE;
50 }
4c05b807 51#if wxUSE_WAVE
bbf1f0e5 52 m_fire.Create("gun.wav");
4c05b807 53#endif // wxUSE_WAVE
bbf1f0e5
KB
54
55 m_maxX = stick.GetXMax();
56 m_maxY = stick.GetYMax();
57
58 // Create the main frame window
59
60 frame = new MyFrame(NULL, "Joystick Demo", wxPoint(0, 0), wxSize(500, 400),
5f4987fe 61 wxDEFAULT_FRAME_STYLE | wxHSCROLL | wxVSCROLL);
bbf1f0e5
KB
62
63 // Give it an icon (this is ignored in MDI mode: uses resources)
2049ba38 64#ifdef __WXMSW__
bbf1f0e5
KB
65 frame->SetIcon(wxIcon("joyicon"));
66#endif
67#ifdef __X__
68 frame->SetIcon(wxIcon("joyicon.xbm"));
69#endif
70
71 // Make a menubar
72 wxMenu *file_menu = new wxMenu;
73
74 file_menu->Append(JOYTEST_QUIT, "&Exit");
75
76 wxMenu *help_menu = new wxMenu;
77 help_menu->Append(JOYTEST_ABOUT, "&About");
78
79 wxMenuBar *menu_bar = new wxMenuBar;
80
81 menu_bar->Append(file_menu, "&File");
82 menu_bar->Append(help_menu, "&Help");
83
84 // Associate the menu bar with the frame
85 frame->SetMenuBar(menu_bar);
86
87 frame->CreateStatusBar();
88
89 frame->Show(TRUE);
90
91 SetTopWindow(frame);
92
93 return TRUE;
94}
95
96BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
97 EVT_JOYSTICK_EVENTS(MyCanvas::OnJoystickEvent)
98END_EVENT_TABLE()
99
100// Define a constructor for my canvas
101MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size):
102 wxScrolledWindow(parent, -1, pos, size, wxSUNKEN_BORDER)
103{
104 wxJoystick joystick(wxJOYSTICK1);
105 joystick.SetCapture(this);
106}
107
108MyCanvas::~MyCanvas(void)
109{
110 wxJoystick joystick(wxJOYSTICK1);
111 joystick.ReleaseCapture();
112}
113
114void MyCanvas::OnJoystickEvent(wxJoystickEvent& event)
115{
116 wxClientDC dc(this);
117
118 wxPoint pt(event.GetPosition());
119
120 // Scale to canvas size
121 int cw, ch;
122 GetSize(&cw, &ch);
123
124 pt.x = (long) (((double)pt.x/(double)wxGetApp().m_maxX) * cw);
125 pt.y = (long) (((double)pt.y/(double)wxGetApp().m_maxY) * ch);
126
127 if (xpos > -1 && ypos > -1 && event.IsMove() && event.ButtonIsDown())
128 {
129 dc.SetPen(*wxBLACK_PEN);
130 dc.DrawLine(xpos, ypos, pt.x, pt.y);
131 }
132 xpos = pt.x;
133 ypos = pt.y;
134
135 char buf[100];
136 if (event.ButtonDown())
ee993c74 137 sprintf(buf, "Joystick (%d, %d) Fire!", pt.x, pt.y);
bbf1f0e5 138 else
ee993c74 139 sprintf(buf, "Joystick (%d, %d)", pt.x, pt.y);
bbf1f0e5
KB
140 frame->SetStatusText(buf);
141
4c05b807 142#if wxUSE_WAVE
bbf1f0e5
KB
143 if (event.ButtonDown() && wxGetApp().m_fire.IsOk())
144 {
145 wxGetApp().m_fire.Play();
146 }
4c05b807 147#endif // wxUSE_WAVE
bbf1f0e5
KB
148}
149
150BEGIN_EVENT_TABLE(MyFrame, wxFrame)
151 EVT_MENU(JOYTEST_QUIT, MyFrame::OnQuit)
152END_EVENT_TABLE()
153
154MyFrame::MyFrame(wxFrame *parent, const wxString& title, const wxPoint& pos, const wxSize& size,
155const long style):
156 wxFrame(parent, -1, title, pos, size, style)
157{
158 canvas = new MyCanvas(this);
159}
160
161MyFrame::~MyFrame(void)
162{
163}
164
165void MyFrame::OnQuit(wxCommandEvent& event)
166{
167 Close(TRUE);
168}
169
170void MyFrame::OnActivate(wxActivateEvent& event)
171{
172 if (event.GetActive() && canvas)
173 canvas->SetFocus();
174}