9#define GL_GLEXT_PROTOTYPES
11#define GL_SILENCE_DEPRECATION
21#include <GLFW/glfw3.h>
24#include <imgui_impl_glfw.h>
25#include <imgui_impl_opengl3.h>
30#include <unordered_map>
60 std::vector<ImGuiKey>
keys;
75 void render(
const ImVec4& button_active_color,
const ImVec4& button_inactive_color,
76 const ImVec2& buttonSize,
bool hold_active,
double joypadDeadzone,
77 const std::vector<float>& joypadAxisValues,
const std::vector<bool>& joypadButtonValues,
78 std::vector<double>& outputValues)
82 bool anyKeyPressed =
false;
83 bool anyKeyReleased =
false;
84 for (ImGuiKey key :
keys)
86 if (ImGui::IsKeyPressed(key))
90 if (ImGui::IsKeyReleased(key))
92 anyKeyReleased =
true;
98 if (i >= 0 && i < joypadButtonValues.size())
100 if (joypadButtonValues[
static_cast<size_t>(i)])
102 anyKeyPressed =
true;
106 anyKeyReleased =
true;
109 else if (i >= 0 && joypadButtonValues.size() > 0)
115 float valueFromJoypadAxes = 0.0;
118 if (axis.index >= 0 && axis.index < joypadAxisValues.size())
120 valueFromJoypadAxes +=
deadzone(axis.sign * joypadAxisValues[
static_cast<size_t>(axis.index)],
static_cast<float>(joypadDeadzone));
122 else if (axis.index >= 0 && joypadAxisValues.size() > 0)
132 if (toggleButton || (regularButton && !hold_active))
144 if (toggleButton || (regularButton && !hold_active))
148 ImGuiStyle& style = ImGui::GetStyle();
149 const ImVec4& buttonColor =
active || valueFromJoypadAxes > 0 ? button_active_color : button_inactive_color;
150 style.Colors[ImGuiCol_Button] = buttonColor;
151 style.Colors[ImGuiCol_ButtonHovered] = buttonColor;
152 style.Colors[ImGuiCol_ButtonActive] = buttonColor;
155 bool buttonReleased = ImGui::Button(
alias.c_str(), buttonSize);
156 bool buttonKeptPressed = ImGui::IsItemActive();
158 if (buttonReleased && (toggleButton || (regularButton && hold_active)))
162 else if (regularButton && buttonKeptPressed && !hold_active)
171 for (
auto& value :
values)
173 outputValues[value.index] += value.sign * (
active + valueFromJoypadAxes);
180 std::vector<std::vector<ButtonState>>
rows;
190 <<
"Using the default value:" << value;
194 if (!cfg.
find(key).isFloat64() && !cfg.
find(key).isInt64() && !cfg.
find(key).isInt32())
199 float input =
static_cast<float>(cfg.
find(key).asFloat64());
200 if (input < min_value || input > max_value)
202 yCError(
KEYBOARDJOYPAD) <<
"The value of " << key <<
" is out of range. It should be between" << min_value <<
"and" << max_value;
214 <<
"Using the default value:" << value;
218 if (!cfg.
find(key).isInt64() && !cfg.
find(key).isInt32())
223 int input =
static_cast<int>(cfg.
find(key).asInt64());
224 if (input < min_value || input > max_value)
226 yCError(
KEYBOARDJOYPAD) <<
"The value of " << key <<
" is out of range. It should be between" << min_value <<
"and" << max_value;
312 if (cfg.
check(
"allow_window_closing"))
318 yCInfo(
KEYBOARDJOYPAD) <<
"The key \"allow_window_closing\" is not present in the configuration file."
325 yCWarning(
KEYBOARDJOYPAD) <<
"In macOS the GUI thread should be the main thread. Hence, we are using true as default for \"no_gui_thread\"";
330 if (cfg.
check(
"no_gui_thread"))
342 yCError(
KEYBOARDJOYPAD) <<
"The configuration file is invalid. The keys \"no_gui_thread\" and \"allow_window_closing\" cannot be both true.";
346 if (cfg.
check(
"joypad_indices"))
351 int joypad_index =
static_cast<int>(joypadsValue.
asInt64());
352 if (joypad_index > GLFW_JOYSTICK_LAST)
355 <<
"It should be between" << GLFW_JOYSTICK_1 <<
"and" << GLFW_JOYSTICK_LAST;
358 if (joypad_index >= GLFW_JOYSTICK_1)
363 else if (!joypadsValue.
isList())
371 for (
size_t i = 0; i < joypads_index_list->
size(); i++)
375 yCError(
KEYBOARDJOYPAD) <<
"The value at index" << i <<
"of the \"joypad_indices\" list is not an integer.";
379 int joypad_index =
static_cast<int>(joypads_index_list->
get(i).
asInt64());
380 if (joypad_index < GLFW_JOYSTICK_1 || joypad_index > GLFW_JOYSTICK_LAST)
383 <<
"It should be between" << GLFW_JOYSTICK_1 <<
"and" << GLFW_JOYSTICK_LAST;
393 <<
"Using only the joypad with index 0 (if present).";
417 std::unordered_map<Axis, std::vector<AxisSettings>>
axes;
429 if (!cfg.
check(
"axes"))
431 yCInfo(
KEYBOARDJOYPAD) <<
"The key \"axes\" is not present in the configuration file. Enabling both wasd and the arrows.";
440 if (!cfg.
find(
"axes").isList())
448 for (
size_t i = 0; i < axes_list->
size(); i++)
460 if (axis[0] ==
'-' || axis[0] ==
'+')
462 sign = axis[0] ==
'-' ? -1 : +1;
463 axis = axis.substr(1);
466 std::transform(axis.begin(), axis.end(), axis.begin(), ::tolower);
472 else if (axis ==
"ad")
476 else if (axis ==
"up_down")
480 else if (axis ==
"left_right")
484 else if (axis !=
"" && axis !=
"none")
487 <<
"Allowed values(\"ws\", \"ad\", \"up_down\", \"left_right\","
488 <<
"eventually with a + or - as prefix, \"none\" and \"\")";
495 if (cfg.
check(
"wasd_label"))
505 if (cfg.
check(
"arrows_label"))
515 std::vector<std::pair<std::string, int&>> joypad_axis_index = { {
"ad_joypad_axis_index",
ad_joypad_axis_index},
520 for (
auto& [name, index] : joypad_axis_index)
522 if (!
parseInt(cfg, name, -1, 100, index))
578 static void GLMessageCallback(GLenum source, GLenum type, GLuint
id, GLenum severity, GLsizei,
const GLchar* message,
const void*) {
579 yCError(
KEYBOARDJOYPAD,
"GL CALLBACK: %s source = 0x%x, type = 0x%x, id = 0x%x, severity = 0x%x, message = %s",
580 (type == GL_DEBUG_TYPE_ERROR ?
"** GL ERROR **" :
""),
581 source, type,
id, severity, message);
591 if (!cfg.
check(
"buttons"))
593 yCInfo(
KEYBOARDJOYPAD) <<
"The key \"buttons\" is not present in the configuration file. No buttons will be created.";
597 if (!cfg.
find(
"buttons").isList())
605 std::unordered_map<std::string, std::pair<size_t, size_t>> buttons_map;
608 for (
size_t i = 0; i < buttons_list->
size(); i++)
610 std::string buttons_with_alias;
615 buttons_with_alias = std::to_string(buttons_list->
get(i).
asInt64());
625 buttons_with_alias = buttons_list->
get(i).
asString();
628 if (buttons_with_alias ==
"" || buttons_with_alias ==
"none")
633 std::string buttons_keys = buttons_with_alias;
634 std::string alias =
"";
636 size_t pos = buttons_with_alias.find(
':');
637 bool have_alias =
false;
638 if (pos != std::string::npos)
640 buttons_keys = buttons_with_alias.substr(0, pos);
641 alias = buttons_with_alias.substr(pos + 1);
646 alias = buttons_keys;
649 std::transform(buttons_keys.begin(), buttons_keys.end(), buttons_keys.begin(), ::toupper);
651 std::vector<std::string> buttons_key_list;
652 std::string delimiter =
"-";
653 pos = buttons_keys.find(delimiter);
654 while (pos != std::string::npos)
656 buttons_key_list.push_back(buttons_keys.substr(0, pos));
657 buttons_keys.erase(0, pos + delimiter.length());
658 pos = buttons_keys.find(delimiter);
660 buttons_key_list.push_back(buttons_keys);
663 newButton.
values.push_back({ .sign = 1, .index = i });
665 std::unordered_map<std::string, ImGuiKey> supportedButtons = {
666 {
"SPACE", ImGuiKey_Space},
667 {
"ENTER", ImGuiKey_Enter},
668 {
"ESCAPE", ImGuiKey_Escape},
669 {
"BACKSPACE", ImGuiKey_Backspace},
670 {
"DELETE", ImGuiKey_Delete},
671 {
"LEFT", ImGuiKey_LeftArrow},
672 {
"RIGHT", ImGuiKey_RightArrow},
673 {
"UP", ImGuiKey_UpArrow},
674 {
"DOWN", ImGuiKey_DownArrow},
675 {
"TAB", ImGuiKey_Tab}
678 std::string parsedButtons;
679 for (
auto& button : buttons_key_list)
682 if (button.size() == 1 && button[0] >=
'A' && button[0] <=
'Z')
684 newButton.
keys.push_back(
static_cast<ImGuiKey
>(ImGuiKey_A + button[0] -
'A'));
686 else if (button.size() && button[0] >=
'0' && button[0] <=
'9')
688 newButton.
keys.push_back(
static_cast<ImGuiKey
>(ImGuiKey_0 + button[0] -
'0'));
689 newButton.
keys.push_back(
static_cast<ImGuiKey
>(ImGuiKey_Keypad0 + button[0] -
'0'));
691 else if (button.size() > 1 && button[0] ==
'J' && std::find_if(button.begin() + 1,
692 button.end(), [](
unsigned char c) { return !std::isdigit(c); }) == button.end())
694 int joypad_button = std::stoi(button.substr(1));
697 else if (supportedButtons.find(button) != supportedButtons.end())
699 newButton.
keys.push_back(supportedButtons[button]);
708 if (!parsedButtons.empty())
710 parsedButtons +=
", " + button;
714 parsedButtons = button;
719 if (!parsedButtons.empty() && have_alias)
721 alias +=
" (" + parsedButtons +
")";
723 else if (!parsedButtons.empty() && !have_alias)
725 alias = parsedButtons;
728 newButton.
alias = alias;
730 if (buttons_map.find(newButton.
alias) != buttons_map.end())
732 size_t button_row = buttons_map[newButton.
alias].first;
733 size_t button_col = buttons_map[newButton.
alias].second;
743 newButton.
col = col++;
760 ImGui::SetNextWindowPos(position, ImGuiCond_FirstUseEver);
761 ImGui::Begin(name.c_str(), 0, ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoSavedSettings);
766 const std::vector<float>& joypadAxisValues,
const std::vector<bool>& joypadButtonValues,
767 std::vector<double>& values)
const
773 ImGui::BeginTable(buttons_table.
name.c_str(), n_cols, ImGuiTableFlags_NoSavedSettings | ImGuiTableFlags_SizingMask_);
775 for (
auto& row : buttons_table.
rows)
777 ImGui::TableNextRow();
779 for (
auto& button : row)
781 ImGui::TableSetColumnIndex(button.col);
787 ImGui::TableSetColumnIndex(0);
788 ImGui::Dummy(buttonSize);
802 this->window = glfwCreateWindow(this->settings.
window_width, this->settings.window_height,
803 "YARP Keyboard as Joypad Device Window",
nullptr,
nullptr);
809 glfwMakeContextCurrent(this->window);
814 glewExperimental = GL_TRUE;
815 GLenum err = glewInit();
816 if (err != GLEW_OK) {
822 glDebugMessageControl(GL_DEBUG_SOURCE_API, GL_DEBUG_TYPE_OTHER, GL_DEBUG_SEVERITY_NOTIFICATION, 0, NULL, GL_FALSE);
825 glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
826 glEnable(GL_DEBUG_OUTPUT);
830 IMGUI_CHECKVERSION();
831 ImGui::CreateContext();
832 ImGuiIO& io = ImGui::GetIO();
833 io.ConfigFlags |= ImGuiConfigFlags_NavNoCaptureKeyboard;
836 ImGui::StyleColorsDark();
839 ImGui_ImplGlfw_InitForOpenGL(this->window,
true);
840 ImGui_ImplOpenGL3_Init();
842 for (
int i = GLFW_JOYSTICK_1; i <= GLFW_JOYSTICK_LAST; ++i) {
843 if (glfwJoystickPresent(i)) {
844 int axes_count, button_count;
845 glfwGetJoystickAxes(i, &axes_count);
846 glfwGetJoystickButtons(i, &button_count);
847 std::string name {glfwGetJoystickName(i)};
848 this->joypads.push_back({ .name = name, .index = i, .axes = axes_count, .buttons = button_count });
850 <<
"axes =" << axes_count <<
"buttons = " << button_count <<
").";
854 if (!this->joypads.size()) {
858 size_t axes_offset = 0;
859 size_t buttons_offset = 0;
863 if (joypad_index >= this->joypads.size())
865 yCWarning(
KEYBOARDJOYPAD) <<
"The joypad with index" << joypad_index <<
"is not available. It will be skipped";
868 this->joypads[
static_cast<size_t>(joypad_index)].axes_offset = axes_offset;
869 this->joypads[
static_cast<size_t>(joypad_index)].buttons_offset = buttons_offset;
870 this->joypads[
static_cast<size_t>(joypad_index)].active =
true;
871 axes_offset += this->joypads[
static_cast<size_t>(joypad_index)].axes;
872 buttons_offset += this->joypads[
static_cast<size_t>(joypad_index)].
buttons;
873 this->using_joypad =
true;
875 this->joypad_axis_values.resize(axes_offset, 0.0);
876 this->joypad_button_values.resize(buttons_offset,
false);
878 this->button_inactive_color = ImGui::GetStyle().Colors[ImGuiCol_Button];
879 this->button_active_color = ImVec4(0.7f, 0.5f, 0.3f, 1.0f);
881 this->gui_thread_id = std::this_thread::get_id();
892 ImGui_ImplOpenGL3_NewFrame();
893 ImGui_ImplGlfw_NewFrame();
916 for (
size_t i = 0; i < this->joypad_button_values.size(); ++i)
918 this->joypad_button_values[i] =
false;
925 if (!this->
initialized || this->gui_thread_id != std::this_thread::get_id())
932 if (this->using_joypad)
934 for (
auto& joypad : this->
joypads)
936 if (!glfwJoystickPresent(joypad.index) || !joypad.active)
941 int new_axes, new_buttons;
942 const float* axes = glfwGetJoystickAxes(joypad.index, &new_axes);
943 const unsigned char*
buttons = glfwGetJoystickButtons(joypad.index, &new_buttons);
945 for (
size_t i = 0; i < std::min(joypad.axes, new_axes); ++i)
947 this->joypad_axis_values[joypad.axes_offset + i] = axes[i];
950 for (
size_t i = 0; i < std::min(joypad.buttons, new_buttons); ++i)
952 this->joypad_button_values[joypad.buttons_offset + i] =
buttons[i] == GLFW_PRESS;
957 ImVec2 position(this->settings.
padding, this->settings.padding);
958 float button_table_height = position.y;
959 for (
auto& stick : this->
sticks)
961 position.y = this->settings.
padding;
964 this->joypad_axis_values, this->joypad_button_values,this->axes_values);
966 position.x += stick.numberOfColumns * this->settings.
button_size + this->settings.
padding;
967 position.y += stick.rows.size() * this->settings.
button_size + this->settings.
padding;
968 button_table_height = std::max(button_table_height, position.y);
978 else if (axis_value < -1)
985 for (
size_t i = 0; i < this->sticks_to_axes.size(); ++i)
987 for (
size_t j = 0; j < this->sticks_to_axes[i].size(); j++)
989 this->sticks_values[i][j] = this->axes_values[this->sticks_to_axes[i][j]];
993 if (!this->buttons.
rows.empty())
995 position.y = this->settings.
padding;
997 ImGui::BeginTable(
"Buttons_layout", 1, ImGuiTableFlags_NoSavedSettings | ImGuiTableFlags_SizingMask_ | ImGuiTableFlags_BordersInner);
998 ImGui::TableNextRow();
999 ImGui::TableSetColumnIndex(0);
1000 this->ctrl_button.
render(this->button_active_color, this->button_inactive_color, ImVec2(this->settings.
button_size, this->settings.button_size),
false, this->settings.deadzone,
1001 this->joypad_axis_values, this->joypad_button_values, this->ctrl_value);
1002 ImGui::TableNextRow();
1003 ImGui::TableSetColumnIndex(0);
1004 bool hold_active = this->ctrl_value.front() > 0;
1006 this->joypad_axis_values, this->joypad_button_values, this->buttons_values);
1014 if (button_value > 0)
1024 position.x = this->settings.
padding;
1025 position.y = button_table_height;
1028 ImGuiIO& io = ImGui::GetIO();
1029 ImGui::Text(
"Application average %.1f ms/frame (%.1f FPS)", io.DeltaTime * 1000.0f, io.Framerate);
1032 glfwGetWindowSize(this->window, &width, &height);
1034 ImGui::Text(
"Window size: %d x %d", width, height);
1035 ImGui::SliderFloat(
"Button size", &this->settings.
button_size, this->settings.min_button_size, this->settings.max_button_size);
1036 ImGui::SliderFloat(
"Font multiplier", &this->settings.
font_multiplier, this->settings.min_font_multiplier, this->settings.max_font_multiplier);
1037 if (this->using_joypad)
1039 ImGui::SliderFloat(
"Joypad deadzone", &this->settings.
deadzone, 0.0, 1.0);
1041 std::string connectedJoypads =
"Connected joypads: ";
1042 for (
size_t i = 0; i < this->joypads.size(); ++i)
1044 connectedJoypads += this->joypads[i].name;
1045 if (i != this->joypads.size() - 1)
1047 connectedJoypads +=
", ";
1051 ImGui::Text(connectedJoypads.c_str());
1053 for (
size_t i = 0; i < this->joypad_axis_values.size(); ++i)
1056 std::stringstream stream;
1057 stream << std::fixed << std::setprecision(2) << this->joypad_axis_values[i];
1058 std::string sign = this->joypad_axis_values[i] >= 0 ?
"+" :
"";
1059 axes_values +=
"<" + std::to_string(i) +
"> " + sign + stream.str();
1060 if (i != this->joypad_axis_values.size() - 1)
1067 for (
size_t i = 0; i < this->joypad_button_values.size(); ++i)
1069 buttons_values +=
"<" + std::to_string(i) +
"> " + (this->joypad_button_values[i] ?
"1" :
"0");
1070 if (i != this->joypad_button_values.size() - 1)
1078 std::string output_axes_values =
"Output axes values: ";
1079 for (
size_t i = 0; i < this->axes_values.size(); ++i)
1082 std::stringstream stream;
1083 stream << std::fixed << std::setprecision(2) << this->axes_values[i];
1084 std::string sign = this->axes_values[i] >= 0 ?
"+" :
"";
1085 output_axes_values +=
"<" + std::to_string(i) +
"> " + sign + stream.str();
1086 if (i != this->axes_values.size() - 1)
1088 output_axes_values +=
", ";
1091 ImGui::Text(output_axes_values.c_str());
1092 std::string output_buttons_values =
"Output buttons values: ";
1093 if (this->buttons_values.empty())
1095 output_buttons_values +=
"None";
1097 for (
size_t i = 0; i < this->buttons_values.size(); ++i)
1099 std::stringstream stream;
1100 stream << std::fixed << std::setprecision(1) << this->buttons_values[i];
1101 output_buttons_values +=
"<" + std::to_string(i) +
"> " + stream.str();
1102 if (i != this->buttons_values.size() - 1)
1104 output_buttons_values +=
", ";
1107 ImGui::Text(output_buttons_values.c_str());
1112 int display_w, display_h;
1113 glfwGetFramebufferSize(this->window, &display_w, &display_h);
1114 glViewport(0, 0, display_w, display_h);
1115 glClearColor(this->clear_color.x * this->clear_color.w, this->clear_color.y * this->clear_color.w, this->clear_color.z * this->clear_color.w, this->clear_color.w);
1116 glClear(GL_COLOR_BUFFER_BIT);
1117 ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
1119 glfwSwapBuffers(this->window);
1138 ImGui_ImplOpenGL3_Shutdown();
1139 ImGui_ImplGlfw_Shutdown();
1140 ImGui::DestroyContext();
1144 glfwDestroyWindow(this->window);
1146 this->window =
nullptr;
1158 yarp::os::PeriodicThread(0.033,
yarp::os::ShouldUseSystemClock::Yes)
1160 m_pimpl = std::make_unique<Impl>();
1171 if (!m_pimpl->settings.parseFromConfigFile(cfg))
1176 if (!m_pimpl->axes_settings.parseFromConfigFile(cfg))
1181 m_pimpl->buttons.numberOfColumns = m_pimpl->settings.buttons_per_row;
1183 if (!m_pimpl->parseButtonsSettings(cfg))
1188 int ws = m_pimpl->axes_settings.axes.find(
Axis::WS) != m_pimpl->axes_settings.axes.end();
1189 int ad = m_pimpl->axes_settings.axes.find(
Axis::AD) != m_pimpl->axes_settings.axes.end();
1190 int up_down = m_pimpl->axes_settings.axes.find(
Axis::UP_DOWN) != m_pimpl->axes_settings.axes.end();
1191 int left_right = m_pimpl->axes_settings.axes.find(
Axis::LEFT_RIGHT) != m_pimpl->axes_settings.axes.end();
1193 m_pimpl->axes_values.resize(
static_cast<size_t>(m_pimpl->axes_settings.number_of_axes), 0.0);
1194 m_pimpl->sticks_to_axes.clear();
1198 m_pimpl->sticks_to_axes.emplace_back();
1199 m_pimpl->sticks_values.emplace_back();
1201 wasd.
name = m_pimpl->axes_settings.wasd_label;
1205 std::vector<ButtonValue> values;
1208 values.push_back({ .sign = -ws_settings.sign, .index = ws_settings.index });
1211 .joypadAxisInputs = {{.sign = -1, .index =
static_cast<size_t>(m_pimpl->axes_settings.ws_joypad_axis_index)}},
1216 std::vector<ButtonValue> a_values;
1217 std::vector<ButtonValue> d_values;
1220 a_values.push_back({ .sign = -ws_settings.sign, .index = ws_settings.index });
1221 d_values.push_back({ .sign = ws_settings.sign, .index = ws_settings.index });
1223 if (a_values.size() > 0)
1225 m_pimpl->sticks_to_axes.back().push_back(a_values.front().index);
1226 m_pimpl->sticks_values.back().push_back(0);
1229 wasd.
rows.push_back({ {.alias =
"A", .type =
ButtonType::TOGGLE, .keys = {ImGuiKey_A}, .values = a_values,
1230 .joypadAxisInputs = {{.sign = -1, .index =
static_cast<size_t>(m_pimpl->axes_settings.ad_joypad_axis_index)}},
1233 .joypadAxisInputs = {{.sign = +1, .index =
static_cast<size_t>(m_pimpl->axes_settings.ad_joypad_axis_index)}},
1238 wasd.
rows.emplace_back();
1242 std::vector<ButtonValue> values;
1245 values.push_back({ .sign = ws_settings.sign, .index = ws_settings.index });
1247 if (values.size() > 0)
1249 m_pimpl->sticks_to_axes.back().push_back(values.front().index);
1250 m_pimpl->sticks_values.back().push_back(0);
1254 .joypadAxisInputs = {{.sign = +1, .index =
static_cast<size_t>(m_pimpl->axes_settings.ws_joypad_axis_index)}},
1259 if (up_down || left_right)
1261 m_pimpl->sticks_to_axes.emplace_back();
1262 m_pimpl->sticks_values.emplace_back();
1264 arrows.
name = m_pimpl->axes_settings.arrows_label;
1268 std::vector<ButtonValue> values;
1271 values.push_back({ .sign = -ws_settings.sign, .index = ws_settings.index });
1273 arrows.
rows.push_back({ {.alias =
"top", .type =
ButtonType::TOGGLE, .keys = {ImGuiKey_UpArrow}, .values = values,
1274 .joypadAxisInputs = {{.sign = -1, .index =
static_cast<size_t>(m_pimpl->axes_settings.up_down_joypad_axis_index)}},
1275 .col = left_right} });
1279 std::vector<ButtonValue> l_values;
1280 std::vector<ButtonValue> r_values;
1283 l_values.push_back({ .sign = -ws_settings.sign, .index = ws_settings.index });
1284 r_values.push_back({ .sign = ws_settings.sign, .index = ws_settings.index });
1286 if (l_values.size() > 0)
1288 m_pimpl->sticks_to_axes.back().push_back(l_values.front().index);
1289 m_pimpl->sticks_values.back().push_back(0);
1291 arrows.
rows.push_back({ {.alias =
"left", .type =
ButtonType::TOGGLE, .keys = {ImGuiKey_LeftArrow}, .values = l_values,
1292 .joypadAxisInputs = {{.sign = -1, .index =
static_cast<size_t>(m_pimpl->axes_settings.left_right_joypad_axis_index)}},
1294 {.alias =
"right", .type =
ButtonType::TOGGLE, .keys = {ImGuiKey_RightArrow}, .values = r_values,
1295 .joypadAxisInputs = {{.sign = +1, .index =
static_cast<size_t>(m_pimpl->axes_settings.left_right_joypad_axis_index)}},
1300 arrows.
rows.emplace_back();
1304 std::vector<ButtonValue> values;
1307 values.push_back({ .sign = ws_settings.sign, .index = ws_settings.index });
1309 if (values.size() > 0)
1311 m_pimpl->sticks_to_axes.back().push_back(values.front().index);
1312 m_pimpl->sticks_values.back().push_back(0);
1314 arrows.
rows.push_back({ {.alias =
"bottom", .type =
ButtonType::TOGGLE, .keys = {ImGuiKey_DownArrow}, .values = values,
1315 .joypadAxisInputs = {{.sign = +1, .index =
static_cast<size_t>(m_pimpl->axes_settings.up_down_joypad_axis_index)}},
1316 .col = left_right} });
1320 if (m_pimpl->settings.single_threaded)
1327 this->setPeriod(m_pimpl->settings.gui_period);
1330 if (!this->start()) {
1344 if (m_pimpl->settings.single_threaded)
1353 if (m_pimpl->closed || m_pimpl->settings.single_threaded)
1358 std::lock_guard<std::mutex> lock(m_pimpl->mutex);
1360 return m_pimpl->initialize();
1365 if (m_pimpl->closed || m_pimpl->settings.single_threaded)
1370 std::lock_guard<std::mutex> lock(m_pimpl->mutex);
1377 if (m_pimpl->closed || m_pimpl->settings.single_threaded)
1382 std::lock_guard<std::mutex> lock(m_pimpl->mutex);
1383 if (m_pimpl->settings.allow_window_closing)
1385 m_pimpl->need_to_close = glfwWindowShouldClose(m_pimpl->window);
1389 double desired_period = 0;
1391 if (!m_pimpl->need_to_close)
1393 std::lock_guard<std::mutex> lock(m_pimpl->mutex);
1397 desired_period = getPeriod();
1398 period = getEstimatedUsed();
1406 if (period > desired_period)
1424 if (m_pimpl->settings.single_threaded && !m_pimpl->closed)
1426 std::lock_guard<std::mutex> lock(m_pimpl->mutex);
1427 if (!m_pimpl->initialized)
1429 if (!m_pimpl->initialize())
1436 return !m_pimpl->closed;
1442 return this->close();
1447 std::lock_guard<std::mutex> lock(m_pimpl->mutex);
1448 axis_count =
static_cast<unsigned int>(m_pimpl->axes_values.size());
1454 std::lock_guard<std::mutex> lock(m_pimpl->mutex);
1455 button_count =
static_cast<unsigned int>(m_pimpl->buttons_values.size());
1461 trackball_count = 0;
1479 std::lock_guard<std::mutex> lock(m_pimpl->mutex);
1480 stick_count =
static_cast<unsigned int>(m_pimpl->sticks_to_axes.size());
1486 std::lock_guard<std::mutex> lock(m_pimpl->mutex);
1487 if (stick_id >= m_pimpl->sticks_to_axes.size())
1493 dof =
static_cast<unsigned int>(m_pimpl->sticks_to_axes[stick_id].size());
1500 std::lock_guard<std::mutex> lock(m_pimpl->mutex);
1501 if (!m_pimpl->initialized)
1503 if (!m_pimpl->initialize())
1508 if (m_pimpl->settings.single_threaded && m_pimpl->needUpdate())
1512 if (button_id >= m_pimpl->buttons_values.size())
1517 value =
static_cast<float>(m_pimpl->buttons_values[button_id]);
1535 std::lock_guard<std::mutex> lock(m_pimpl->mutex);
1536 if (!m_pimpl->initialized)
1538 if (!m_pimpl->initialize())
1543 if (m_pimpl->settings.single_threaded && m_pimpl->needUpdate())
1547 if (axis_id >= m_pimpl->axes_values.size())
1552 value = m_pimpl->axes_values[axis_id];
1558 std::lock_guard<std::mutex> lock(m_pimpl->mutex);
1559 if (!m_pimpl->initialized)
1561 if (!m_pimpl->initialize())
1566 if (m_pimpl->settings.single_threaded && m_pimpl->needUpdate())
1570 if (stick_id >= m_pimpl->sticks_values.size())
1576 value.
resize(m_pimpl->sticks_values[stick_id].size());
1578 for (
size_t i = 0; i < value.
size(); i++)
1580 value[i] = m_pimpl->sticks_values[stick_id][i];
1583 if (value.
size() != 2)
1588 if (coordinate_mode == JoypadCtrl_coordinateMode::JypCtrlcoord_POLAR)
1590 double norm = sqrt(value[0] * value[0] + value[1] * value[1]);
1591 double angle = atan2(value[1], value[0]);
const yarp::os::LogComponent & KEYBOARDJOYPAD()
static bool parseFloat(yarp::os::Searchable &cfg, const std::string &key, float min_value, float max_value, float &value)
static bool parseInt(yarp::os::Searchable &cfg, const std::string &key, int min_value, int max_value, int &value)
Interface implemented by all device drivers.
JoypadCtrl_coordinateMode
void prepareWindow(const ImVec2 &position, const std::string &name)
std::vector< double > axes_values
AxesSettings axes_settings
ImVec4 button_inactive_color
double last_gui_update_time
static void glfwErrorCallback(int error, const char *description)
std::vector< double > ctrl_value
std::vector< bool > joypad_button_values
ImVec4 button_active_color
bool parseButtonsSettings(yarp::os::Searchable &cfg)
void renderButtonsTable(ButtonsTable &buttons_table, bool hold_active, double joypadDeadzone, const std::vector< float > &joypadAxisValues, const std::vector< bool > &joypadButtonValues, std::vector< double > &values) const
std::vector< float > joypad_axis_values
std::vector< ButtonsTable > sticks
std::atomic_bool need_to_close
std::atomic_bool initialized
std::vector< double > buttons_values
std::vector< JoypadInfo > joypads
std::vector< std::vector< double > > sticks_values
std::thread::id gui_thread_id
std::vector< std::vector< size_t > > sticks_to_axes
static void GLMessageCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei, const GLchar *message, const void *)
virtual bool close() override
Close the DeviceDriver.
virtual bool getButtonCount(unsigned int &button_count) override
virtual bool getStickCount(unsigned int &stick_count) override
virtual ~KeyboardJoypad()
virtual bool startService() override
Initiate the service, whatever it is.
virtual bool getAxis(unsigned int axis_id, double &value) override
virtual bool getTrackballCount(unsigned int &trackball_count) override
virtual bool getButton(unsigned int button_id, float &value) override
virtual bool open(yarp::os::Searchable &cfg) override
Open the DeviceDriver.
virtual bool getTouchSurfaceCount(unsigned int &touch_count) override
virtual bool getStick(unsigned int stick_id, yarp::sig::Vector &value, JoypadCtrl_coordinateMode coordinate_mode) override
virtual void run() override
Loop function.
virtual bool getStickDoF(unsigned int stick_id, unsigned int &dof) override
virtual bool getHat(unsigned int hat_id, unsigned char &value) override
virtual void threadRelease() override
Release method.
virtual bool getAxisCount(unsigned int &axis_count) override
virtual bool stopService() override
Shut down the service, whatever it is.
virtual bool threadInit() override
Initialization method.
virtual bool getTrackball(unsigned int trackball_id, yarp::sig::Vector &value) override
virtual bool getTouch(unsigned int touch_id, yarp::sig::Vector &value) override
virtual bool updateService() override
Give the service the chance to run for a while.
virtual bool getHatCount(unsigned int &hat_count) override
A simple collection of objects that can be described and transmitted in a portable way.
size_type size() const
Gets the number of elements in the bottle.
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
A base class for nested structures that can be searched.
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
virtual Value & find(const std::string &key) const =0
Gets a value corresponding to a given keyword.
A single value (typically within a Bottle).
virtual bool isString() const
Checks if value is a string.
virtual std::int64_t asInt64() const
Get 64-bit integer value.
virtual bool isList() const
Checks if value is a list.
virtual Bottle * asList() const
Get list value.
virtual bool isInt32() const
Checks if value is a 32-bit integer.
virtual bool isInt64() const
Checks if value is a 64-bit integer.
virtual std::string asString() const
Get string value.
size_t size() const
Returns the number of elements in the vector.
void resize(size_t size)
Resize the vector.
#define yCInfo(component,...)
#define yCError(component,...)
#define yCWarning(component,...)
#define yCErrorOnce(component,...)
#define yCWarningThrottle(component, period,...)
double now()
Return the current time in seconds, relative to an arbitrary starting point.
void delay(double seconds)
Wait for a certain number of seconds.
The main, catch-all namespace for YARP.
bool parseFromConfigFile(yarp::os::Searchable &cfg)
std::unordered_map< Axis, std::vector< AxisSettings > > axes
int up_down_joypad_axis_index
int left_right_joypad_axis_index
float min_font_multiplier
std::atomic< bool > single_threaded
std::vector< int > joypad_indices
bool parseFromConfigFile(yarp::os::Searchable &cfg)
bool allow_window_closing
float max_font_multiplier