Skip to content

Commit

Permalink
Fix some more joystick issues (#10624)
Browse files Browse the repository at this point in the history
  • Loading branch information
m42uko committed Dec 19, 2020
1 parent 5066fe7 commit af22dd8
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 48 deletions.
3 changes: 3 additions & 0 deletions builtin/settingtypes.txt
Expand Up @@ -152,6 +152,9 @@ joystick_type (Joystick type) enum auto auto,generic,xbox
# when holding down a joystick button combination.
repeat_joystick_button_time (Joystick button repetition interval) float 0.17 0.001

# The deadzone of the joystick
joystick_deadzone (Joystick deadzone) int 2048

# The sensitivity of the joystick axes for moving the
# ingame view frustum around.
joystick_frustum_sensitivity (Joystick frustum sensitivity) float 170
Expand Down
3 changes: 3 additions & 0 deletions minetest.conf.example
Expand Up @@ -129,6 +129,9 @@
# type: float min: 0.001
# repeat_joystick_button_time = 0.17

# The deadzone of the joystick
# joystick_deadzone = 2048

# The sensitivity of the joystick axes for moving the
# ingame view frustum around.
# type: float
Expand Down
4 changes: 2 additions & 2 deletions src/client/game.cpp
Expand Up @@ -3139,8 +3139,8 @@ void Game::processPlayerInteraction(f32 dtime, bool show_hud, bool show_debug)
wasKeyDown(KeyType::DIG);
wasKeyDown(KeyType::PLACE);

input->joystick.clearWasKeyDown(KeyType::DIG);
input->joystick.clearWasKeyDown(KeyType::PLACE);
input->joystick.clearWasKeyPressed(KeyType::DIG);
input->joystick.clearWasKeyPressed(KeyType::PLACE);

input->joystick.clearWasKeyReleased(KeyType::DIG);
input->joystick.clearWasKeyReleased(KeyType::PLACE);
Expand Down
2 changes: 1 addition & 1 deletion src/client/inputhandler.h
Expand Up @@ -279,7 +279,7 @@ class RealInputHandler : public InputHandler
}
virtual bool wasKeyPressed(GameKeyType k)
{
return m_receiver->WasKeyPressed(keycache.key[k]) || joystick.wasKeyReleased(k);
return m_receiver->WasKeyPressed(keycache.key[k]) || joystick.wasKeyPressed(k);
}
virtual bool wasKeyReleased(GameKeyType k)
{
Expand Down
50 changes: 26 additions & 24 deletions src/client/joystick_controller.cpp
Expand Up @@ -37,7 +37,7 @@ bool JoystickAxisCmb::isTriggered(const irr::SEvent::SJoystickEvent &ev) const
{
s16 ax_val = ev.Axis[axis_to_compare];

return (ax_val * direction < 0) && (thresh * direction > ax_val * direction);
return (ax_val * direction < -thresh);
}

// spares many characters
Expand All @@ -48,7 +48,7 @@ JoystickLayout create_default_layout()
{
JoystickLayout jlo;

jlo.axes_dead_border = 1024;
jlo.axes_deadzone = g_settings->getU16("joystick_deadzone");

const JoystickAxisLayout axes[JA_COUNT] = {
{0, 1}, // JA_SIDEWARD_MOVE
Expand Down Expand Up @@ -93,14 +93,14 @@ JoystickLayout create_default_layout()
// Now about the buttons simulated by the axes

// Movement buttons, important for vessels
JLO_A_PB(KeyType::FORWARD, 1, 1, 1024);
JLO_A_PB(KeyType::BACKWARD, 1, -1, 1024);
JLO_A_PB(KeyType::LEFT, 0, 1, 1024);
JLO_A_PB(KeyType::RIGHT, 0, -1, 1024);
JLO_A_PB(KeyType::FORWARD, 1, 1, jlo.axes_deadzone);
JLO_A_PB(KeyType::BACKWARD, 1, -1, jlo.axes_deadzone);
JLO_A_PB(KeyType::LEFT, 0, 1, jlo.axes_deadzone);
JLO_A_PB(KeyType::RIGHT, 0, -1, jlo.axes_deadzone);

// Scroll buttons
JLO_A_PB(KeyType::HOTBAR_PREV, 2, -1, 1024);
JLO_A_PB(KeyType::HOTBAR_NEXT, 5, -1, 1024);
JLO_A_PB(KeyType::HOTBAR_PREV, 2, -1, jlo.axes_deadzone);
JLO_A_PB(KeyType::HOTBAR_NEXT, 5, -1, jlo.axes_deadzone);

return jlo;
}
Expand All @@ -109,7 +109,7 @@ JoystickLayout create_xbox_layout()
{
JoystickLayout jlo;

jlo.axes_dead_border = 7000;
jlo.axes_deadzone = 7000;

const JoystickAxisLayout axes[JA_COUNT] = {
{0, 1}, // JA_SIDEWARD_MOVE
Expand Down Expand Up @@ -146,10 +146,10 @@ JoystickLayout create_xbox_layout()
JLO_B_PB(KeyType::FREEMOVE, 1 << 16, 1 << 16); // down

// Movement buttons, important for vessels
JLO_A_PB(KeyType::FORWARD, 1, 1, 1024);
JLO_A_PB(KeyType::BACKWARD, 1, -1, 1024);
JLO_A_PB(KeyType::LEFT, 0, 1, 1024);
JLO_A_PB(KeyType::RIGHT, 0, -1, 1024);
JLO_A_PB(KeyType::FORWARD, 1, 1, jlo.axes_deadzone);
JLO_A_PB(KeyType::BACKWARD, 1, -1, jlo.axes_deadzone);
JLO_A_PB(KeyType::LEFT, 0, 1, jlo.axes_deadzone);
JLO_A_PB(KeyType::RIGHT, 0, -1, jlo.axes_deadzone);

return jlo;
}
Expand Down Expand Up @@ -219,40 +219,42 @@ bool JoystickController::handleEvent(const irr::SEvent::SJoystickEvent &ev)

for (size_t i = 0; i < KeyType::INTERNAL_ENUM_COUNT; i++) {
if (keys_pressed[i]) {
if (!m_past_pressed_keys[i] &&
if (!m_past_keys_pressed[i] &&
m_past_pressed_time[i] < m_internal_time - doubling_dtime) {
m_past_pressed_keys[i] = true;
m_past_keys_pressed[i] = true;
m_past_pressed_time[i] = m_internal_time;
}
} else if (m_pressed_keys[i]) {
m_past_released_keys[i] = true;
} else if (m_keys_down[i]) {
m_keys_released[i] = true;
}

m_pressed_keys[i] = keys_pressed[i];
if (keys_pressed[i] && !(m_keys_down[i]))
m_keys_pressed[i] = true;

m_keys_down[i] = keys_pressed[i];
}

for (size_t i = 0; i < JA_COUNT; i++) {
const JoystickAxisLayout &ax_la = m_layout.axes[i];
m_axes_vals[i] = ax_la.invert * ev.Axis[ax_la.axis_id];
}


return true;
}

void JoystickController::clear()
{
m_pressed_keys.reset();
m_past_pressed_keys.reset();
m_past_released_keys.reset();
m_keys_pressed.reset();
m_keys_down.reset();
m_past_keys_pressed.reset();
m_keys_released.reset();
memset(m_axes_vals, 0, sizeof(m_axes_vals));
}

s16 JoystickController::getAxisWithoutDead(JoystickAxis axis)
{
s16 v = m_axes_vals[axis];
if (((v > 0) && (v < m_layout.axes_dead_border)) ||
((v < 0) && (v > -m_layout.axes_dead_border)))
if (abs(v) < m_layout.axes_deadzone)
return 0;
return v;
}
38 changes: 17 additions & 21 deletions src/client/joystick_controller.h
Expand Up @@ -96,7 +96,7 @@ struct JoystickLayout {
std::vector<JoystickButtonCmb> button_keys;
std::vector<JoystickAxisCmb> axis_keys;
JoystickAxisLayout axes[JA_COUNT];
s16 axes_dead_border;
s16 axes_deadzone;
};

class JoystickController {
Expand All @@ -111,37 +111,32 @@ class JoystickController {

bool wasKeyDown(GameKeyType b)
{
bool r = m_past_pressed_keys[b];
m_past_pressed_keys[b] = false;
bool r = m_past_keys_pressed[b];
m_past_keys_pressed[b] = false;
return r;
}
bool getWasKeyDown(GameKeyType b)

bool wasKeyReleased(GameKeyType b)
{
return m_past_pressed_keys[b];
return m_keys_released[b];
}
void clearWasKeyDown(GameKeyType b)
void clearWasKeyReleased(GameKeyType b)
{
m_past_pressed_keys[b] = false;
m_keys_released[b] = false;
}

bool wasKeyReleased(GameKeyType b)
bool wasKeyPressed(GameKeyType b)
{
bool r = m_past_released_keys[b];
m_past_released_keys[b] = false;
return r;
}
bool getWasKeyReleased(GameKeyType b)
{
return m_past_pressed_keys[b];
return m_keys_pressed[b];
}
void clearWasKeyReleased(GameKeyType b)
void clearWasKeyPressed(GameKeyType b)
{
m_past_pressed_keys[b] = false;
m_keys_pressed[b] = false;
}

bool isKeyDown(GameKeyType b)
{
return m_pressed_keys[b];
return m_keys_down[b];
}

s16 getAxis(JoystickAxis axis)
Expand All @@ -162,12 +157,13 @@ class JoystickController {

u8 m_joystick_id = 0;

std::bitset<KeyType::INTERNAL_ENUM_COUNT> m_pressed_keys;
std::bitset<KeyType::INTERNAL_ENUM_COUNT> m_keys_down;
std::bitset<KeyType::INTERNAL_ENUM_COUNT> m_keys_pressed;

f32 m_internal_time;

f32 m_past_pressed_time[KeyType::INTERNAL_ENUM_COUNT];

std::bitset<KeyType::INTERNAL_ENUM_COUNT> m_past_pressed_keys;
std::bitset<KeyType::INTERNAL_ENUM_COUNT> m_past_released_keys;
std::bitset<KeyType::INTERNAL_ENUM_COUNT> m_past_keys_pressed;
std::bitset<KeyType::INTERNAL_ENUM_COUNT> m_keys_released;
};
1 change: 1 addition & 0 deletions src/defaultsettings.cpp
Expand Up @@ -279,6 +279,7 @@ void set_default_settings(Settings *settings)
settings->setDefault("joystick_type", "");
settings->setDefault("repeat_joystick_button_time", "0.17");
settings->setDefault("joystick_frustum_sensitivity", "170");
settings->setDefault("joystick_deadzone", "2048");

// Main menu
settings->setDefault("main_menu_path", "");
Expand Down

0 comments on commit af22dd8

Please sign in to comment.