forked from bridgecommand/bc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyEventReceiver.cpp
More file actions
313 lines (267 loc) · 12.1 KB
/
MyEventReceiver.cpp
File metadata and controls
313 lines (267 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/* Bridge Command 5.0 Ship Simulator
Copyright (C) 2014 James Packer
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY Or FITNESS For A PARTICULAR PURPOSE. See the
GNU General Public License For more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
#include "MyEventReceiver.hpp"
#include <iostream>
#include "GUIMain.hpp"
#include "SimulationModel.hpp"
using namespace irr;
MyEventReceiver::MyEventReceiver(irr::IrrlichtDevice* dev, SimulationModel* model, GUIMain* gui, irr::u32 portJoystickAxis, irr::u32 stbdJoystickAxis, irr::u32 rudderJoystickAxis) //Constructor
{
this->model = model; //Link to the model
this->gui = gui; //Link to GUI (Not currently used!)
scrollBarPosSpeed = 0;
scrollBarPosHeading = 0;
//set up joystick if present
dev->activateJoysticks(joystickInfo);
previousJoystickLoaded = false;
this->portJoystickAxis=portJoystickAxis;
this->stbdJoystickAxis=stbdJoystickAxis;
this->rudderJoystickAxis=rudderJoystickAxis;
}
bool MyEventReceiver::OnEvent(const SEvent& event)
{
if (event.EventType == EET_GUI_EVENT)
{
s32 id = event.GUIEvent.Caller->getID();
if (event.GUIEvent.EventType==gui::EGET_SCROLL_BAR_CHANGED)
{
if (id == GUIMain::GUI_ID_HEADING_SCROLL_BAR)
{
scrollBarPosHeading = ((gui::IGUIScrollBar*)event.GUIEvent.Caller)->getPos();
model->setHeading(scrollBarPosHeading);
}
if (id == GUIMain::GUI_ID_SPEED_SCROLL_BAR)
{
scrollBarPosSpeed = ((gui::IGUIScrollBar*)event.GUIEvent.Caller)->getPos();
model->setSpeed(scrollBarPosSpeed);
}
if (id == GUIMain::GUI_ID_STBD_SCROLL_BAR)
{
model->setStbdEngine(((gui::IGUIScrollBar*)event.GUIEvent.Caller)->getPos()/-100.0); //Convert to from +-100 to +-1, and invert up/down
}
if (id == GUIMain::GUI_ID_PORT_SCROLL_BAR)
{
model->setPortEngine(((gui::IGUIScrollBar*)event.GUIEvent.Caller)->getPos()/-100.0); //Convert to from +-100 to +-1, and invert up/down
}
if (id == GUIMain::GUI_ID_RUDDER_SCROLL_BAR)
{
model->setRudder(((gui::IGUIScrollBar*)event.GUIEvent.Caller)->getPos());
}
if (id == GUIMain::GUI_ID_RADAR_GAIN_SCROLL_BAR)
{
model->setRadarGain(((gui::IGUIScrollBar*)event.GUIEvent.Caller)->getPos());
}
if (id == GUIMain::GUI_ID_RADAR_CLUTTER_SCROLL_BAR)
{
model->setRadarClutter(((gui::IGUIScrollBar*)event.GUIEvent.Caller)->getPos());
}
if (id == GUIMain::GUI_ID_RADAR_RAIN_SCROLL_BAR)
{
model->setRadarRain(((gui::IGUIScrollBar*)event.GUIEvent.Caller)->getPos());
}
if (id == GUIMain::GUI_ID_WEATHER_SCROLL_BAR)
{
model->setWeather(((gui::IGUIScrollBar*)event.GUIEvent.Caller)->getPos()/10.0); //Scroll bar 0-120, weather 0-12
}
if (id == GUIMain::GUI_ID_RAIN_SCROLL_BAR)
{
model->setRain(((gui::IGUIScrollBar*)event.GUIEvent.Caller)->getPos()/10.0); //Scroll bar 0-100, rain 0-10
}
}
if (event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED) {
if (id == GUIMain::GUI_ID_START_BUTTON)
{
model->setAccelerator(1.0);
}
if (id == GUIMain::GUI_ID_RADAR_INCREASE_BUTTON)
{
model->increaseRadarRange();
}
if (id == GUIMain::GUI_ID_RADAR_DECREASE_BUTTON)
{
model->decreaseRadarRange();
}
if (id == GUIMain::GUI_ID_SHOW_INTERFACE_BUTTON)
{
gui->show2dInterface();
}
if (id == GUIMain::GUI_ID_HIDE_INTERFACE_BUTTON)
{
gui->hide2dInterface();
}
if (id == GUIMain::GUI_ID_BINOS_INTERFACE_BUTTON)
{
model->toggleZoom();
}
if (id == GUIMain::GUI_ID_RADAR_EBL_LEFT_BUTTON)
{
model->decreaseRadarEBLBrg();
}
if (id == GUIMain::GUI_ID_RADAR_EBL_RIGHT_BUTTON)
{
model->increaseRadarEBLBrg();
}
if (id == GUIMain::GUI_ID_RADAR_EBL_UP_BUTTON)
{
model->increaseRadarEBLRange();
}
if (id == GUIMain::GUI_ID_RADAR_EBL_DOWN_BUTTON)
{
model->decreaseRadarEBLRange();
}
} //Button clicked
} //GUI Event
//From keyboard
if (event.EventType == EET_KEY_INPUT_EVENT && event.KeyInput.PressedDown)
{
if (event.KeyInput.Shift) {
//Shift down
} else if (event.KeyInput.Control) {
//Ctrl down
switch(event.KeyInput.Key)
{
//Camera look
case KEY_UP:
model->lookAhead();
break;
case KEY_DOWN:
model->lookAstern();
break;
case KEY_LEFT:
model->lookPort();
break;
case KEY_RIGHT:
model->lookStbd();
break;
default:
//don't do anything
break;
}
} else {
//Shift and Ctrl not down
switch(event.KeyInput.Key)
{
//Accelerator
case KEY_KEY_0:
model->setAccelerator(0.0);
break;
case KEY_RETURN:
model->setAccelerator(1.0);
break;
case KEY_KEY_1:
model->setAccelerator(1.0);
break;
case KEY_KEY_2:
model->setAccelerator(2.0);
break;
case KEY_KEY_3:
model->setAccelerator(5.0);
break;
case KEY_KEY_4:
model->setAccelerator(15.0);
break;
case KEY_KEY_5:
model->setAccelerator(30.0);
break;
case KEY_KEY_6:
model->setAccelerator(60.0);
break;
case KEY_KEY_7:
model->setAccelerator(3600.0);
break;
//Camera look
case KEY_UP:
model->lookUp();
break;
case KEY_DOWN:
model->lookDown();
break;
case KEY_LEFT:
model->lookLeft();
break;
case KEY_RIGHT:
model->lookRight();
break;
case KEY_SPACE:
model->changeView();
break;
//toggle full screen 3d
case KEY_KEY_F:
gui->toggleShow2dInterface();
break;
case KEY_KEY_Q:
gui->quitSim();
break;
case KEY_KEY_S:
// Sink the ship here
model->sinkOwnShip();
break;
default:
//don't do anything
break;
}
}
}
//From joystick (actually polled, once per run():
if (event.EventType == EET_JOYSTICK_INPUT_EVENT) {
u32 numberOfAxes = event.JoystickEvent.NUMBER_OF_AXES;
//Do joystick stuff here
//Todo: track joystick changes, so if not changing, the GUI inputs are used - partially implemented but need to check for jitter etc
//Todo: Also implement multiplier/offset and joystick map.
//FIXME: Note that Irrlicht does not have joystick handling on MacOS
if (numberOfAxes>portJoystickAxis && numberOfAxes>stbdJoystickAxis && numberOfAxes>rudderJoystickAxis ) { //check required axes exist on this joystick
if (!previousJoystickLoaded) { //Load initial joystick values, so we can detect a change
previousJoystickPort = event.JoystickEvent.Axis[portJoystickAxis]/32768.0;
previousJoystickStbd = event.JoystickEvent.Axis[stbdJoystickAxis]/32768.0;
previousJoystickRudder = 30.0*event.JoystickEvent.Axis[rudderJoystickAxis]/32768.0;
previousJoystickLoaded=true;
} else { //Normal running
//Get current joystick inputs
f32 newJoystickPort = event.JoystickEvent.Axis[portJoystickAxis]/32768.0;//+-1 from axis range -32768 to 32767
f32 newJoystickStbd = event.JoystickEvent.Axis[stbdJoystickAxis]/32768.0;//+-1
f32 newJoystickRudder = 30.0*event.JoystickEvent.Axis[rudderJoystickAxis]/32768.0;//+-30 from axis range -32768 to 32767
//check if any have changed
bool joystickChanged = false;
f32 portChange = fabs(newJoystickPort - previousJoystickPort);
f32 stbdChange = fabs(newJoystickStbd - previousJoystickStbd);
f32 rudderChange = fabs(newJoystickRudder - previousJoystickRudder);
if (portChange > 0.01 || stbdChange > 0.01 || rudderChange > 0.01)
{
joystickChanged = true;
}
//If any have changed, use all
if (joystickChanged) {
model->setPortEngine(newJoystickPort);
model->setStbdEngine(newJoystickStbd);
model->setRudder(newJoystickRudder);
previousJoystickPort=newJoystickPort;
previousJoystickStbd=newJoystickStbd;
previousJoystickRudder=newJoystickRudder;
}
}
} else {
std::cout << "Trying to use non-existent joystick axis." << std::endl;
}
}
return false;
}
/*
s32 MyEventReceiver::GetScrollBarPosSpeed() const
{
return scrollBarPosSpeed;
}
s32 MyEventReceiver::GetScrollBarPosHeading() const
{
return scrollBarPosHeading;
}
*/