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