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