X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Fclient%2Fjoystick_controller.cpp;h=919db531598cd3ade2591a944d591c210c3e195a;hb=4419e311a96821d12e64073f342877327c655dca;hp=311cd22fb3803a368ac9d4410c019949603897dc;hpb=bce0d458d8cda70c10d78ea9ec476474f0a6f01a;p=minetest.git diff --git a/src/client/joystick_controller.cpp b/src/client/joystick_controller.cpp index 311cd22fb..919db5315 100644 --- a/src/client/joystick_controller.cpp +++ b/src/client/joystick_controller.cpp @@ -22,7 +22,8 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "keys.h" #include "settings.h" #include "gettime.h" -#include "../util/string.h" +#include "porting.h" +#include "util/string.h" bool JoystickButtonCmb::isTriggered(const irr::SEvent::SJoystickEvent &ev) const { @@ -36,18 +37,18 @@ 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 -#define JLO_B_PB(A, B, C) jlo.button_keys.push_back(JoystickButtonCmb(A, B, C)) -#define JLO_A_PB(A, B, C, D) jlo.axis_keys.push_back(JoystickAxisCmb(A, B, C, D)) +#define JLO_B_PB(A, B, C) jlo.button_keys.emplace_back(A, B, C) +#define JLO_A_PB(A, B, C, D) jlo.axis_keys.emplace_back(A, B, C, D) 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 @@ -73,18 +74,18 @@ JoystickLayout create_default_layout() // Accessible without four modifier button pressed // regardless whether start is pressed or not - JLO_B_PB(KeyType::MOUSE_L, fb | 1 << 4, 1 << 4); - JLO_B_PB(KeyType::MOUSE_R, fb | 1 << 5, 1 << 5); + JLO_B_PB(KeyType::DIG, fb | 1 << 4, 1 << 4); + JLO_B_PB(KeyType::PLACE, fb | 1 << 5, 1 << 5); // Accessible without any modifier pressed JLO_B_PB(KeyType::JUMP, bm | 1 << 0, 1 << 0); - JLO_B_PB(KeyType::SPECIAL1, bm | 1 << 1, 1 << 1); + JLO_B_PB(KeyType::AUX1, bm | 1 << 1, 1 << 1); // Accessible with start button not pressed, but four pressed // TODO find usage for button 0 - JLO_B_PB(KeyType::DROP, bm | 1 << 1, fb | 1 << 1); - JLO_B_PB(KeyType::SCROLL_UP, bm | 1 << 4, fb | 1 << 4); - JLO_B_PB(KeyType::SCROLL_DOWN,bm | 1 << 5, fb | 1 << 5); + JLO_B_PB(KeyType::DROP, bm | 1 << 1, fb | 1 << 1); + JLO_B_PB(KeyType::HOTBAR_PREV, bm | 1 << 4, fb | 1 << 4); + JLO_B_PB(KeyType::HOTBAR_NEXT, bm | 1 << 5, fb | 1 << 5); // Accessible with start button and four pressed // TODO find usage for buttons 0, 1 and 4, 5 @@ -92,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::SCROLL_UP, 2, -1, 1024); - JLO_A_PB(KeyType::SCROLL_DOWN, 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; } @@ -108,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 @@ -125,18 +126,18 @@ JoystickLayout create_xbox_layout() // 4 Buttons JLO_B_PB(KeyType::JUMP, 1 << 0, 1 << 0); // A/green JLO_B_PB(KeyType::ESC, 1 << 1, 1 << 1); // B/red - JLO_B_PB(KeyType::SPECIAL1, 1 << 2, 1 << 2); // X/blue + JLO_B_PB(KeyType::AUX1, 1 << 2, 1 << 2); // X/blue JLO_B_PB(KeyType::INVENTORY, 1 << 3, 1 << 3); // Y/yellow // Analog Sticks - JLO_B_PB(KeyType::SPECIAL1, 1 << 11, 1 << 11); // left + JLO_B_PB(KeyType::AUX1, 1 << 11, 1 << 11); // left JLO_B_PB(KeyType::SNEAK, 1 << 12, 1 << 12); // right // Triggers - JLO_B_PB(KeyType::MOUSE_L, 1 << 6, 1 << 6); // lt - JLO_B_PB(KeyType::MOUSE_R, 1 << 7, 1 << 7); // rt - JLO_B_PB(KeyType::SCROLL_UP, 1 << 4, 1 << 4); // lb - JLO_B_PB(KeyType::SCROLL_DOWN, 1 << 5, 1 << 5); // rb + JLO_B_PB(KeyType::DIG, 1 << 6, 1 << 6); // lt + JLO_B_PB(KeyType::PLACE, 1 << 7, 1 << 7); // rt + JLO_B_PB(KeyType::HOTBAR_PREV, 1 << 4, 1 << 4); // lb + JLO_B_PB(KeyType::HOTBAR_NEXT, 1 << 5, 1 << 5); // rb // D-PAD JLO_B_PB(KeyType::ZOOM, 1 << 15, 1 << 15); // up @@ -145,22 +146,19 @@ 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; } -JoystickController::JoystickController() +JoystickController::JoystickController() : + doubling_dtime(g_settings->getFloat("repeat_joystick_button_time")) { - m_joystick_id = 0; - - doubling_dtime = g_settings->getFloat("repeat_joystick_button_time"); - - for (size_t i = 0; i < KeyType::INTERNAL_ENUM_COUNT; i++) { - m_past_pressed_time[i] = 0; + for (float &i : m_past_pressed_time) { + i = 0; } clear(); } @@ -170,12 +168,12 @@ void JoystickController::onJoystickConnect(const std::vector s32 id = g_settings->getS32("joystick_id"); std::string layout = g_settings->get("joystick_type"); - if (id < 0 || id >= joystick_infos.size()) { + if (id < 0 || (u16)id >= joystick_infos.size()) { // TODO: auto detection id = 0; } - if (id >= 0 && id < joystick_infos.size()) { + if (id >= 0 && (u16)id < joystick_infos.size()) { if (layout.empty() || layout == "auto") setLayoutFromControllerName(joystick_infos[id].Name.c_str()); else @@ -185,8 +183,9 @@ void JoystickController::onJoystickConnect(const std::vector m_joystick_id = id; } -void JoystickController::setLayoutFromControllerName(std::string name) { - if (lowercase(name).find("xbox") >= 0) { +void JoystickController::setLayoutFromControllerName(const std::string &name) +{ + if (lowercase(name).find("xbox") != std::string::npos) { m_layout = create_xbox_layout(); } else { m_layout = create_default_layout(); @@ -198,21 +197,21 @@ bool JoystickController::handleEvent(const irr::SEvent::SJoystickEvent &ev) if (ev.Joystick != m_joystick_id) return false; - m_internal_time = getTimeMs() / 1000.f; + m_internal_time = porting::getTimeMs() / 1000.f; std::bitset keys_pressed; // First generate a list of keys pressed - for (size_t i = 0; i < m_layout.button_keys.size(); i++) { - if (m_layout.button_keys[i].isTriggered(ev)) { - keys_pressed.set(m_layout.button_keys[i].key); + for (const auto &button_key : m_layout.button_keys) { + if (button_key.isTriggered(ev)) { + keys_pressed.set(button_key.key); } } - for (size_t i = 0; i < m_layout.axis_keys.size(); i++) { - if (m_layout.axis_keys[i].isTriggered(ev)) { - keys_pressed.set(m_layout.axis_keys[i].key); + for (const auto &axis_key : m_layout.axis_keys) { + if (axis_key.isTriggered(ev)) { + keys_pressed.set(axis_key.key); } } @@ -220,16 +219,19 @@ 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++) { @@ -237,23 +239,22 @@ bool JoystickController::handleEvent(const irr::SEvent::SJoystickEvent &ev) 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; }