]>
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"
24 #include <wx/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!");
48 m_fire
.Create("gun.wav");
51 m_maxX
= stick
.GetXMax();
52 m_maxY
= stick
.GetYMax();
54 // Create the main frame window
56 frame
= new MyFrame(NULL
, "Joystick Demo", wxPoint(0, 0), wxSize(500, 400),
57 wxDEFAULT_FRAME_STYLE
| wxHSCROLL
| wxVSCROLL
);
59 // Give it an icon (this is ignored in MDI mode: uses resources)
61 frame
->SetIcon(wxIcon("joyicon"));
64 frame
->SetIcon(wxIcon("joyicon.xbm"));
68 wxMenu
*file_menu
= new wxMenu
;
70 file_menu
->Append(JOYTEST_QUIT
, "&Exit");
72 wxMenu
*help_menu
= new wxMenu
;
73 help_menu
->Append(JOYTEST_ABOUT
, "&About");
75 wxMenuBar
*menu_bar
= new wxMenuBar
;
77 menu_bar
->Append(file_menu
, "&File");
78 menu_bar
->Append(help_menu
, "&Help");
80 // Associate the menu bar with the frame
81 frame
->SetMenuBar(menu_bar
);
83 frame
->CreateStatusBar();
92 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
93 EVT_JOYSTICK_EVENTS(MyCanvas::OnJoystickEvent
)
96 // Define a constructor for my canvas
97 MyCanvas::MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
):
98 wxScrolledWindow(parent
, -1, pos
, size
, wxSUNKEN_BORDER
)
100 wxJoystick
joystick(wxJOYSTICK1
);
101 joystick
.SetCapture(this);
104 MyCanvas::~MyCanvas(void)
106 wxJoystick
joystick(wxJOYSTICK1
);
107 joystick
.ReleaseCapture();
110 void MyCanvas::OnJoystickEvent(wxJoystickEvent
& event
)
114 wxPoint
pt(event
.GetPosition());
116 // Scale to canvas size
120 pt
.x
= (long) (((double)pt
.x
/(double)wxGetApp().m_maxX
) * cw
);
121 pt
.y
= (long) (((double)pt
.y
/(double)wxGetApp().m_maxY
) * ch
);
123 if (xpos
> -1 && ypos
> -1 && event
.IsMove() && event
.ButtonIsDown())
125 dc
.SetPen(*wxBLACK_PEN
);
126 dc
.DrawLine(xpos
, ypos
, pt
.x
, pt
.y
);
132 if (event
.ButtonDown())
133 sprintf(buf
, "Joystick (%d, %d) Fire!", pt
.x
, pt
.y
);
135 sprintf(buf
, "Joystick (%d, %d)", pt
.x
, pt
.y
);
136 frame
->SetStatusText(buf
);
139 if (event
.ButtonDown() && wxGetApp().m_fire
.IsOk())
141 wxGetApp().m_fire
.Play();
146 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
147 EVT_MENU(JOYTEST_QUIT
, MyFrame::OnQuit
)
150 MyFrame::MyFrame(wxFrame
*parent
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
,
152 wxFrame(parent
, -1, title
, pos
, size
, style
)
154 canvas
= new MyCanvas(this);
157 MyFrame::~MyFrame(void)
161 void MyFrame::OnQuit(wxCommandEvent
& event
)
166 void MyFrame::OnActivate(wxActivateEvent
& event
)
168 if (event
.GetActive() && canvas
)