Skip to content

Commit

Permalink
Using also keypad buttons.
Browse files Browse the repository at this point in the history
Allow to toggle off button when pressing them again
  • Loading branch information
S-Dafarra committed Apr 4, 2024
1 parent 336e7d4 commit 5c258c3
Showing 1 changed file with 40 additions and 20 deletions.
60 changes: 40 additions & 20 deletions src/devices/keyboard-joypad/KeyboardJoypad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

struct ButtonState {
std::string alias;
ImGuiKey key = ImGuiKey_COUNT;
std::vector<ImGuiKey> keys;
int col = 0;
int sign = 1;
size_t index = 0;
Expand Down Expand Up @@ -403,18 +403,19 @@ class yarp::dev::KeyboardJoypad::Impl

if (button.size() == 1 && button[0] >= 'A' && button[0] <= 'Z')
{
newButton.key = static_cast<ImGuiKey>(ImGuiKey_A + button[0] - 'A');
newButton.keys.push_back(static_cast<ImGuiKey>(ImGuiKey_A + button[0] - 'A'));
}
else if (button.size() && button[0] >= '0' && button[0] <= '9')
{
newButton.key = static_cast<ImGuiKey>(ImGuiKey_0 + button[0] - '0');
newButton.keys.push_back(static_cast<ImGuiKey>(ImGuiKey_0 + button[0] - '0'));
newButton.keys.push_back(static_cast<ImGuiKey>(ImGuiKey_Keypad0 + button[0] - '0'));
}
else if (supportedButtons.find(button) != supportedButtons.end())
{
newButton.key = supportedButtons[button];
newButton.keys.push_back(supportedButtons[button]);
}

if (newButton.key != ImGuiKey_COUNT && have_alias)
if (!newButton.keys.empty() && have_alias)
{
alias += " (" + button + ")";
}
Expand Down Expand Up @@ -451,24 +452,43 @@ class yarp::dev::KeyboardJoypad::Impl
for (auto& row : buttons_table.rows)
{
ImGui::TableNextRow();

for (auto& button : row)
{
ImGui::TableSetColumnIndex(button.col);

if (button.key != ImGuiKey_COUNT)
bool anyKeyPressed = false;
bool anyKeyReleased = false;
for (ImGuiKey key : button.keys)
{
if (ImGui::IsKeyPressed(key))
{
anyKeyPressed = true;
}
if (ImGui::IsKeyReleased(key))
{
anyKeyReleased = true;
}
}

if (anyKeyPressed)
{
if (ImGui::IsKeyPressed(button.key))
button.buttonPressed = true;
if (!hold_button)
{
button.buttonPressed = true;
button.active = true;
}
else if (button.buttonPressed && ImGui::IsKeyReleased(button.key))
else
{
button.buttonPressed = false;
if (!hold_button)
button.active = false;
button.active = !button.active;
}
}
else if (button.buttonPressed && anyKeyReleased)
{
button.buttonPressed = false;
if (!hold_button)
button.active = false;
}

ImGuiStyle& style = ImGui::GetStyle();
ImVec4& buttonColor = button.active ? button_active_color : button_inactive_color;
Expand Down Expand Up @@ -633,7 +653,7 @@ bool yarp::dev::KeyboardJoypad::threadInit()
AxisSettings& ws_settings = m_pimpl->axes_settings.axes[Axis::WS];
int sign = ws_settings.sign;
size_t index = ws_settings.index;
wasd.rows.push_back({{.alias = "W", .key = ImGuiKey_W, .col = ad, .sign = -sign, .index = index}});
wasd.rows.push_back({{.alias = "W", .keys = {ImGuiKey_W}, .col = ad, .sign = -sign, .index = index}});
}
if (ad)
{
Expand All @@ -642,8 +662,8 @@ bool yarp::dev::KeyboardJoypad::threadInit()
size_t index = ws_settings.index;
m_pimpl->sticks_to_axes.back().push_back(index);
m_pimpl->sticks_values.back().push_back(0);
wasd.rows.push_back({{.alias = "A", .key = ImGuiKey_A, .col = 0, .sign = -sign, .index = index},
{.alias = "D", .key = ImGuiKey_D, .col = 2, .sign = sign, .index = index}});
wasd.rows.push_back({{.alias = "A", .keys = {ImGuiKey_A}, .col = 0, .sign = -sign, .index = index},
{.alias = "D", .keys = {ImGuiKey_D}, .col = 2, .sign = sign, .index = index}});
}
else
{
Expand All @@ -656,7 +676,7 @@ bool yarp::dev::KeyboardJoypad::threadInit()
size_t index = ws_settings.index;
m_pimpl->sticks_to_axes.back().push_back(index);
m_pimpl->sticks_values.back().push_back(0);
wasd.rows.push_back({{.alias = "S", .key = ImGuiKey_S, .col = ad, .sign = sign, .index = index}});
wasd.rows.push_back({{.alias = "S", .keys = {ImGuiKey_S}, .col = ad, .sign = sign, .index = index}});
}
}

Expand All @@ -672,7 +692,7 @@ bool yarp::dev::KeyboardJoypad::threadInit()
AxisSettings& ws_settings = m_pimpl->axes_settings.axes[Axis::UP_DOWN];
int sign = ws_settings.sign;
size_t index = ws_settings.index;
arrows.rows.push_back({{.alias = "top", .key = ImGuiKey_UpArrow, .col = left_right, .sign = -sign, .index = index}});
arrows.rows.push_back({{.alias = "top", .keys = {ImGuiKey_UpArrow}, .col = left_right, .sign = -sign, .index = index}});
}
if (left_right)
{
Expand All @@ -681,8 +701,8 @@ bool yarp::dev::KeyboardJoypad::threadInit()
size_t index = ws_settings.index;
m_pimpl->sticks_to_axes.back().push_back(index);
m_pimpl->sticks_values.back().push_back(0);
arrows.rows.push_back({{.alias = "left", .key = ImGuiKey_LeftArrow, .col = 0, .sign = -sign, .index = index},
{.alias = "right", .key = ImGuiKey_RightArrow, .col = 2, .sign = sign, .index = index}});
arrows.rows.push_back({{.alias = "left", .keys = {ImGuiKey_LeftArrow}, .col = 0, .sign = -sign, .index = index},
{.alias = "right", .keys = {ImGuiKey_RightArrow}, .col = 2, .sign = sign, .index = index}});
}
else
{
Expand All @@ -695,7 +715,7 @@ bool yarp::dev::KeyboardJoypad::threadInit()
size_t index = ws_settings.index;
m_pimpl->sticks_to_axes.back().push_back(index);
m_pimpl->sticks_values.back().push_back(0);
arrows.rows.push_back({{.alias = "bottom", .key = ImGuiKey_DownArrow, .col = left_right, .sign = sign, .index = index}});
arrows.rows.push_back({{.alias = "bottom", .keys = {ImGuiKey_DownArrow}, .col = left_right, .sign = sign, .index = index}});
}
}

Expand Down

0 comments on commit 5c258c3

Please sign in to comment.