| 7 | #include <QSettings> |
| 8 | |
| 9 | SDLControllerManager::SDLControllerManager(QObject *parent) |
| 10 | : QObject(parent), pollTimer(new QTimer(this)) { |
| 11 | |
| 12 | // Load saved controller mappings or use defaults |
| 13 | loadControllerMappings(); |
| 14 | |
| 15 | connect(pollTimer, &QTimer::timeout, this, [=]() { |
| 16 | SDL_PumpEvents(); // ✅ Update SDL input state |
| 17 | |
| 18 | // 🔁 Poll for axis movement (left stick) |
| 19 | if (joystick) { |
| 20 | // Use joystick axis instead of controller axis |
| 21 | int x = SDL_JoystickGetAxis(joystick, 0); // Axis 0 = X |
| 22 | int y = SDL_JoystickGetAxis(joystick, 1); // Axis 1 = Y |
| 23 | |
| 24 | const int DEADZONE = 16000; |
| 25 | if (qAbs(x) >= DEADZONE || qAbs(y) >= DEADZONE) { |
| 26 | float fx = x / 32768.0f; |
| 27 | float fy = y / 32768.0f; |
| 28 | |
| 29 | float angle = qAtan2(-fy, fx) * 180.0 / M_PI; |
| 30 | if (angle < 0) angle += 360; |
| 31 | |
| 32 | int angleInt = static_cast<int>(angle); |
| 33 | |
| 34 | // ✅ Invert angle for dial so clockwise = clockwise |
| 35 | angleInt = (360 - angleInt) % 360; |
| 36 | |
| 37 | if (qAbs(angleInt - lastAngle) > 3) { |
| 38 | lastAngle = angleInt; |
| 39 | emit leftStickAngleChanged(angleInt); |
| 40 | } |
| 41 | leftStickActive = true; // ✅ Mark stick as active |
| 42 | } |
| 43 | else { |
| 44 | // Stick is in deadzone (center) |
| 45 | if (leftStickActive) { |
| 46 | // ✅ Was active → now released → emit release signal once |
| 47 | emit leftStickReleased(); |
| 48 | leftStickActive = false; |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | SDL_PumpEvents(); |
| 54 | |
| 55 | // Check hold timer |
| 56 | for (auto it = buttonPressTime.begin(); it != buttonPressTime.end(); ++it) { |
| 57 | QString btn = it.key(); |
| 58 | quint32 pressTime = it.value(); |
| 59 | quint32 now = SDL_GetTicks(); |
| 60 | |
| 61 | if (!buttonHeldEmitted.value(btn, false) && (now - pressTime) >= HOLD_THRESHOLD) { |
| 62 | emit buttonHeld(btn); |
| 63 | buttonHeldEmitted[btn] = true; |
| 64 | } |
| 65 | } |
| 66 | |