/
Nunchuk.cpp
221 lines (186 loc) · 6.32 KB
/
Nunchuk.cpp
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
// Copyright 2010 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#include "Core/HW/WiimoteEmu/Extension/Nunchuk.h"
#include <algorithm>
#include <array>
#include <cassert>
#include <cstring>
#include "Common/BitUtils.h"
#include "Common/Common.h"
#include "Common/CommonTypes.h"
#include "Common/MathUtil.h"
#include "Core/HW/Wiimote.h"
#include "Core/HW/WiimoteEmu/WiimoteEmu.h"
#include "InputCommon/ControllerEmu/Control/Input.h"
#include "InputCommon/ControllerEmu/ControlGroup/AnalogStick.h"
#include "InputCommon/ControllerEmu/ControlGroup/Buttons.h"
#include "InputCommon/ControllerEmu/ControlGroup/ControlGroup.h"
#include "InputCommon/ControllerEmu/ControlGroup/Force.h"
#include "InputCommon/ControllerEmu/ControlGroup/IMUAccelerometer.h"
#include "InputCommon/ControllerEmu/ControlGroup/Tilt.h"
namespace WiimoteEmu
{
constexpr std::array<u8, 6> nunchuk_id{{0x00, 0x00, 0xa4, 0x20, 0x00, 0x00}};
constexpr std::array<u8, 2> nunchuk_button_bitmasks{{
Nunchuk::BUTTON_C,
Nunchuk::BUTTON_Z,
}};
Nunchuk::Nunchuk() : Extension1stParty(_trans("Nunchuk"))
{
// buttons
groups.emplace_back(m_buttons = new ControllerEmu::Buttons(_trans("Buttons")));
m_buttons->AddInput(ControllerEmu::DoNotTranslate, "C");
m_buttons->AddInput(ControllerEmu::DoNotTranslate, "Z");
// stick
constexpr auto gate_radius = ControlState(STICK_GATE_RADIUS) / STICK_RADIUS;
groups.emplace_back(m_stick =
new ControllerEmu::OctagonAnalogStick(_trans("Stick"), gate_radius));
// swing
groups.emplace_back(m_swing = new ControllerEmu::Force(_trans("Swing")));
// tilt
groups.emplace_back(m_tilt = new ControllerEmu::Tilt(_trans("Tilt")));
// Shake
// Inverse the default intensity so shake is opposite that of wiimote.
// This is needed by DKCR for proper shake action detection.
groups.emplace_back(m_shake = new ControllerEmu::Shake(_trans("Shake"), -1));
// accelerometer
groups.emplace_back(m_imu_accelerometer = new ControllerEmu::IMUAccelerometer(
"IMUAccelerometer", _trans("Accelerometer")));
}
void Nunchuk::Update()
{
DataFormat nc_data = {};
// stick
const ControllerEmu::AnalogStick::StateData stick_state = m_stick->GetState();
nc_data.jx = u8(STICK_CENTER + stick_state.x * STICK_RADIUS);
nc_data.jy = u8(STICK_CENTER + stick_state.y * STICK_RADIUS);
// Some terribly coded games check whether to move with a check like
//
// if (x != 0 && y != 0)
// do_movement(x, y);
//
// With keyboard controls, these games break if you simply hit
// of the axes. Adjust this if you're hitting one of the axes so that
// we slightly tweak the other axis.
if (nc_data.jx != STICK_CENTER || nc_data.jy != STICK_CENTER)
{
if (nc_data.jx == STICK_CENTER)
++nc_data.jx;
if (nc_data.jy == STICK_CENTER)
++nc_data.jy;
}
// buttons
u8 buttons = 0;
m_buttons->GetState(&buttons, nunchuk_button_bitmasks.data());
nc_data.SetButtons(buttons);
// Acceleration data:
EmulateSwing(&m_swing_state, m_swing, 1.f / ::Wiimote::UPDATE_FREQ);
EmulateTilt(&m_tilt_state, m_tilt, 1.f / ::Wiimote::UPDATE_FREQ);
EmulateShake(&m_shake_state, m_shake, 1.f / ::Wiimote::UPDATE_FREQ);
const auto transformation =
GetRotationalMatrix(-m_tilt_state.angle) * GetRotationalMatrix(-m_swing_state.angle);
Common::Vec3 accel =
transformation *
(m_swing_state.acceleration +
m_imu_accelerometer->GetState().value_or(Common::Vec3(0, 0, float(GRAVITY_ACCELERATION))));
// shake
accel += m_shake_state.acceleration;
// Calibration values are 8-bit but we want 10-bit precision, so << 2.
const auto acc = ConvertAccelData(accel, ACCEL_ZERO_G << 2, ACCEL_ONE_G << 2);
nc_data.SetAccel(acc.value);
Common::BitCastPtr<DataFormat>(&m_reg.controller_data) = nc_data;
}
bool Nunchuk::IsButtonPressed() const
{
u8 buttons = 0;
m_buttons->GetState(&buttons, nunchuk_button_bitmasks.data());
return buttons != 0;
}
void Nunchuk::Reset()
{
EncryptedExtension::Reset();
m_reg.identifier = nunchuk_id;
m_swing_state = {};
m_tilt_state = {};
// Build calibration data:
m_reg.calibration = {{
// Accel Zero X,Y,Z:
ACCEL_ZERO_G,
ACCEL_ZERO_G,
ACCEL_ZERO_G,
// Possibly LSBs of zero values:
0x00,
// Accel 1G X,Y,Z:
ACCEL_ONE_G,
ACCEL_ONE_G,
ACCEL_ONE_G,
// Possibly LSBs of 1G values:
0x00,
// Stick X max,min,center:
STICK_CENTER + STICK_GATE_RADIUS,
STICK_CENTER - STICK_GATE_RADIUS,
STICK_CENTER,
// Stick Y max,min,center:
STICK_CENTER + STICK_GATE_RADIUS,
STICK_CENTER - STICK_GATE_RADIUS,
STICK_CENTER,
// 2 checksum bytes calculated below:
0x00,
0x00,
}};
UpdateCalibrationDataChecksum(m_reg.calibration, CALIBRATION_CHECKSUM_BYTES);
}
ControllerEmu::ControlGroup* Nunchuk::GetGroup(NunchukGroup group)
{
switch (group)
{
case NunchukGroup::Buttons:
return m_buttons;
case NunchukGroup::Stick:
return m_stick;
case NunchukGroup::Tilt:
return m_tilt;
case NunchukGroup::Swing:
return m_swing;
case NunchukGroup::Shake:
return m_shake;
case NunchukGroup::IMUAccelerometer:
return m_imu_accelerometer;
default:
assert(false);
return nullptr;
}
}
void Nunchuk::DoState(PointerWrap& p)
{
EncryptedExtension::DoState(p);
p.Do(m_swing_state);
p.Do(m_tilt_state);
p.Do(m_shake_state);
}
void Nunchuk::LoadDefaults(const ControllerInterface& ciface)
{
// Stick
m_stick->SetControlExpression(0, "W"); // up
m_stick->SetControlExpression(1, "S"); // down
m_stick->SetControlExpression(2, "A"); // left
m_stick->SetControlExpression(3, "D"); // right
// Because our defaults use keyboard input, set calibration shape to a square.
m_stick->SetCalibrationFromGate(ControllerEmu::SquareStickGate(1.0));
// Buttons
#ifdef _WIN32
m_buttons->SetControlExpression(0, "LCONTROL"); // C
m_buttons->SetControlExpression(1, "LSHIFT"); // Z
#elif __APPLE__
m_buttons->SetControlExpression(0, "Left Control"); // C
m_buttons->SetControlExpression(1, "Left Shift"); // Z
#else
m_buttons->SetControlExpression(0, "Control_L"); // C
m_buttons->SetControlExpression(1, "Shift_L"); // Z
#endif
// Shake
for (int i = 0; i < 3; ++i)
m_shake->SetControlExpression(i, "Click 2");
}
} // namespace WiimoteEmu