]>
git.saurik.com Git - wxWidgets.git/blob - samples/joytest/joytest.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Joystick sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
23 #include <wx/msw/wave.h>
24 #include <wx/msw/joystick.h>
28 MyFrame
*frame
= NULL
;
32 // For drawing lines in a canvas
38 // Initialise this in OnInit, not statically
39 bool MyApp::OnInit(void)
41 wxJoystick
stick(wxJOYSTICK1
);
44 wxMessageBox("No joystick detected!");
47 m_fire
.Create("gun.wav");
49 m_maxX
= stick
.GetXMax();
50 m_maxY
= stick
.GetYMax();
52 // Create the main frame window
54 frame
= new MyFrame(NULL
, "Joystick Demo", wxPoint(0, 0), wxSize(500, 400),
55 wxDEFAULT_FRAME_STYLE
| wxHSCROLL
| wxVSCROLL
);
57 // Give it an icon (this is ignored in MDI mode: uses resources)
59 frame
->SetIcon(wxIcon("joyicon"));
62 frame
->SetIcon(wxIcon("joyicon.xbm"));
66 wxMenu
*file_menu
= new wxMenu
;
68 file_menu
->Append(JOYTEST_QUIT
, "&Exit");
70 wxMenu
*help_menu
= new wxMenu
;
71 help_menu
->Append(JOYTEST_ABOUT
, "&About");
73 wxMenuBar
*menu_bar
= new wxMenuBar
;
75 menu_bar
->Append(file_menu
, "&File");
76 menu_bar
->Append(help_menu
, "&Help");
78 // Associate the menu bar with the frame
79 frame
->SetMenuBar(menu_bar
);
81 frame
->CreateStatusBar();
90 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
91 EVT_JOYSTICK_EVENTS(MyCanvas::OnJoystickEvent
)
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
)
98 wxJoystick
joystick(wxJOYSTICK1
);
99 joystick
.SetCapture(this);
102 MyCanvas::~MyCanvas(void)
104 wxJoystick
joystick(wxJOYSTICK1
);
105 joystick
.ReleaseCapture();
108 void MyCanvas::OnJoystickEvent(wxJoystickEvent
& event
)
112 wxPoint
pt(event
.GetPosition());
114 // Scale to canvas size
118 pt
.x
= (long) (((double)pt
.x
/(double)wxGetApp().m_maxX
) * cw
);
119 pt
.y
= (long) (((double)pt
.y
/(double)wxGetApp().m_maxY
) * ch
);
121 if (xpos
> -1 && ypos
> -1 && event
.IsMove() && event
.ButtonIsDown())
123 dc
.SetPen(*wxBLACK_PEN
);
124 dc
.DrawLine(xpos
, ypos
, pt
.x
, pt
.y
);
130 if (event
.ButtonDown())
131 sprintf(buf
, "Joystick (%ld, %ld) Fire!", pt
.x
, pt
.y
);
133 sprintf(buf
, "Joystick (%ld, %ld)", pt
.x
, pt
.y
);
134 frame
->SetStatusText(buf
);
136 if (event
.ButtonDown() && wxGetApp().m_fire
.IsOk())
138 wxGetApp().m_fire
.Play();
142 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
143 EVT_MENU(JOYTEST_QUIT
, MyFrame::OnQuit
)
146 MyFrame::MyFrame(wxFrame
*parent
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
,
148 wxFrame(parent
, -1, title
, pos
, size
, style
)
150 canvas
= new MyCanvas(this);
153 MyFrame::~MyFrame(void)
157 void MyFrame::OnQuit(wxCommandEvent
& event
)
162 void MyFrame::OnActivate(wxActivateEvent
& event
)
164 if (event
.GetActive() && canvas
)