Skip to content
This repository has been archived by the owner on Jan 10, 2025. It is now read-only.

Commit

Permalink
Add Dpad support
Browse files Browse the repository at this point in the history
  • Loading branch information
ikt32 committed Sep 17, 2016
1 parent d687a30 commit 235e9c7
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 29 deletions.
37 changes: 20 additions & 17 deletions Gears.ini
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

[OPTIONS]
Enable = 1
EnableH = 0
EnableH = 1

RealReverse = 1
SimpleBike = 1
Expand Down Expand Up @@ -99,13 +99,6 @@ HN = 0x69
Enable = 1
WheelRange = 900

DisableDpad = 0
; Hardcoded buttons/functions:
; Dpad Left - Radio Previous
; Dpad Right - Radio Next
; Dpad Down - Handbrake
; Throttle - Restart engine

; "A" on shifter
Toggle = 17

Expand All @@ -127,11 +120,11 @@ H5 = 12
H6 = 13
HR = 14

; Center Right / 19
Handbrake = -1
; Dpad Down
Handbrake = 18000

; Bottom Right / 21
Engine = -1
; Dpad Up
Engine = 3600

; Top Left
Lights = 7
Expand All @@ -146,16 +139,16 @@ LookBack = 22
Camera = 0

; 2nd red @ shifter
RadioNext = -1
RadioNext = 3600

; 3rd red @ shifter
RadioPrev = -1

; Center right
IndicatorLeft = 19
; Dpad Left
IndicatorLeft = 27000

; Bottom Right
IndicatorRight = 21
; Dpad Right
IndicatorRight = 9000

; Shifter "Y"-Button
IndicatorHazard = 15
Expand Down Expand Up @@ -189,6 +182,16 @@ ClutchDisable = 0
[DEBUG]
Info = 1

; To use your Dpad on your wheel, the following numbers apply
; N 3600
; NE 4500
; E 9000
; SE 13500
; S 18000
; SW 22500
; W 27000
; NW 31500

; 8 input axes are supported by DirectInput, use the one applicable to your device
; lX
; lY
Expand Down
70 changes: 64 additions & 6 deletions Gears/WheelDirectInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,45 @@ bool WheelDirectInput::IsConnected() const {
return false;
}

// Mental note: buttonType in these args means physical button number
// like how they are in DirectInput.
// If it matches the cardinal stuff the button is a POV hat thing

bool WheelDirectInput::IsButtonPressed(int buttonType) {
if (buttonType > 127) {
switch (buttonType) {
case N:
if (JoyState.rgdwPOV[0] == 0)
return true;
case NE:
case E:
case SE:
case S:
case SW:
case W:
case NW:
if (buttonType == JoyState.rgdwPOV[0])
return true;
default:
return false;
}
}
if (JoyState.rgbButtons[buttonType]) {
return true;
}
return false;
}

bool WheelDirectInput::IsButtonJustPressed(int buttonType) {
if (buttonType > 127) { // POV
povButtonCurr[buttonType] = IsButtonPressed(buttonType);

// raising edge
if (povButtonCurr[buttonType] && !povButtonPrev[buttonType]) {
return true;
}
return false;
}
rgbButtonCurr[buttonType] = IsButtonPressed(buttonType);

// raising edge
Expand All @@ -124,6 +155,15 @@ bool WheelDirectInput::IsButtonJustPressed(int buttonType) {
}

bool WheelDirectInput::IsButtonJustReleased(int buttonType) {
if (buttonType > 127) { // POV
povButtonCurr[buttonType] = IsButtonPressed(buttonType);

// falling edge
if (!povButtonCurr[buttonType] && povButtonPrev[buttonType]) {
return true;
}
return false;
}
rgbButtonCurr[buttonType] = IsButtonPressed(buttonType);

// falling edge
Expand All @@ -134,16 +174,31 @@ bool WheelDirectInput::IsButtonJustReleased(int buttonType) {
}

bool WheelDirectInput::WasButtonHeldForMs(int buttonType, int millis) {
if (buttonType > 127) { // POV
if (IsButtonJustPressed(buttonType)) {
povPressTime[buttonType] = milliseconds_now();
}
if (IsButtonJustReleased(buttonType)) {
povReleaseTime[buttonType] = milliseconds_now();
}

if ((povReleaseTime[buttonType] - povPressTime[buttonType]) >= millis) {
povPressTime[buttonType] = 0;
povReleaseTime[buttonType] = 0;
return true;
}
return false;
}
if (IsButtonJustPressed(buttonType)) {
pressTime[buttonType] = milliseconds_now();
rgbPressTime[buttonType] = milliseconds_now();
}
if (IsButtonJustReleased(buttonType)) {
releaseTime[buttonType] = milliseconds_now();
rgbReleaseTime[buttonType] = milliseconds_now();
}

if ((releaseTime[buttonType] - pressTime[buttonType]) >= millis) {
pressTime[buttonType] = 0;
releaseTime[buttonType] = 0;
if ((rgbReleaseTime[buttonType] - rgbPressTime[buttonType]) >= millis) {
rgbPressTime[buttonType] = 0;
rgbReleaseTime[buttonType] = 0;
return true;
}
return false;
Expand All @@ -153,6 +208,9 @@ void WheelDirectInput::UpdateButtonChangeStates() {
for (int i = 0; i < MAX_RGBBUTTONS; i++) {
rgbButtonPrev[i] = rgbButtonCurr[i];
}
for (int i = 0; i < SIZEOF_POV; i++) {
povButtonPrev[i] = povButtonCurr[i];
}
}

bool WheelDirectInput::CreateConstantForceEffect(std::string &axis) {
Expand Down Expand Up @@ -248,7 +306,7 @@ WheelDirectInput::DIAxis WheelDirectInput::StringToAxis(std::string& axisString)
return static_cast<DIAxis>(i);
}
}
return UNKNOWN;
return UNKNOWN_AXIS;
}


Expand Down
30 changes: 25 additions & 5 deletions Gears/WheelDirectInput.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class WheelDirectInput {
lRz,
rglSlider0,
rglSlider1,
UNKNOWN,
UNKNOWN_AXIS,
SIZEOF_DIAxis
};

Expand All @@ -35,7 +35,20 @@ class WheelDirectInput {
"lRz",
"rglSlider0",
"rglSlider1",
"UNKNOWN"
"UNKNOWN_AXIS"
};

enum POV {
N = 3600,
NE = 4500,
E = 9000,
SE = 13500,
S = 18000,
SW = 22500,
W = 27000,
NW = 31500,
UNKNOWN_POV,
SIZEOF_POV
};

public:
Expand All @@ -47,7 +60,6 @@ class WheelDirectInput {
void UpdateState();

bool IsConnected() const;

bool IsButtonPressed(int btn);
bool IsButtonJustPressed(int btn);
bool IsButtonJustReleased(int btn);
Expand All @@ -69,11 +81,19 @@ class WheelDirectInput {
LPDIRECTINPUTEFFECT pCFEffect;
LPDIRECTINPUTEFFECT pFREffect;
bool CreateConstantForceEffect(std::string& axis);
std::array<__int64, MAX_RGBBUTTONS> pressTime;
std::array<__int64, MAX_RGBBUTTONS> releaseTime;
std::array<__int64, MAX_RGBBUTTONS> rgbPressTime;
std::array<__int64, MAX_RGBBUTTONS> rgbReleaseTime;
std::array<bool, MAX_RGBBUTTONS> rgbButtonCurr;
std::array<bool, MAX_RGBBUTTONS> rgbButtonPrev;

// hooooo boi a big array I only use 8 values of
std::array<__int64, SIZEOF_POV> povPressTime;
std::array<__int64, SIZEOF_POV> povReleaseTime;
std::array<bool, SIZEOF_POV> povButtonCurr;
std::array<bool, SIZEOF_POV> povButtonPrev;



int prevPosition = 0;
long long prevTime = 0;
std::array<float, SAMPLES> samples = {};
Expand Down
1 change: 0 additions & 1 deletion Gears/script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,6 @@ void handleVehicleButtons() {
if (controls.ButtonIn(ScriptControls::WheelControlType::Handbrake)) {
//VEHICLE::SET_VEHICLE_HANDBRAKE(vehicle, true);
CONTROLS::_SET_CONTROL_NORMAL(0, ControlVehicleHandbrake, 1.0f);
showText(0.5, 0.5, 5.0, "E");
}
if (controls.ButtonIn(ScriptControls::WheelControlType::Horn)) {
CONTROLS::_SET_CONTROL_NORMAL(0, ControlVehicleHorn, 1.0f);
Expand Down

0 comments on commit 235e9c7

Please sign in to comment.