ImGuiInputMonitor.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #ifdef IMGUI_ENABLED
  9. #include <AzCore/std/sort.h>
  10. #include <AzFramework/Input/Buses/Requests/InputDeviceRequestBus.h>
  11. #include <AzFramework/Input/Devices/InputDevice.h>
  12. #include "ImGuiInputMonitor.h"
  13. namespace ImGui
  14. {
  15. ImGuiInputMonitor::ImGuiInputMonitor()
  16. : m_enabled(false)
  17. {
  18. }
  19. void ImGuiInputMonitor::Initialize()
  20. {
  21. }
  22. void ImGuiInputMonitor::Shutdown()
  23. {
  24. }
  25. void ImGuiInputMonitor::ImGuiUpdate()
  26. {
  27. // Manage main window visibility
  28. if (m_enabled)
  29. {
  30. if (ImGui::Begin("Input Monitor", &m_enabled, ImGuiWindowFlags_MenuBar|ImGuiWindowFlags_HorizontalScrollbar|ImGuiWindowFlags_NoSavedSettings))
  31. {
  32. // Draw the Entire Main Menu Window Area
  33. ImGuiUpdate_DrawMenu();
  34. }
  35. ImGui::End();
  36. }
  37. }
  38. void ImGuiInputMonitor::ImGuiUpdate_DrawMenu()
  39. {
  40. using namespace ImGui;
  41. if (CollapsingHeader("Input Monitor", ImGuiTreeNodeFlags_DefaultOpen | ImGuiTreeNodeFlags_Framed))
  42. {
  43. using deviceBus = AzFramework::InputDeviceRequestBus;
  44. using deviceBusEvents = AzFramework::InputDeviceRequestBus::Events;
  45. AzFramework::InputDeviceRequests::InputDeviceIdSet deviceIds;
  46. deviceBus::Broadcast(&deviceBusEvents::GetInputDeviceIds, deviceIds);
  47. for (auto deviceId : deviceIds)
  48. {
  49. const AzFramework::InputDevice* device = nullptr;
  50. deviceBus::EventResult(device, deviceId, &deviceBusEvents::GetInputDevice);
  51. if (device)
  52. {
  53. const auto& inputChannelsById = device->GetInputChannelsById();
  54. AZStd::string childText = AZStd::string::format("Index: %d - Name '%s' - %s - player: %d - %d input channels",
  55. device->GetInputDeviceId().GetIndex(),
  56. device->GetInputDeviceId().GetName(),
  57. device->IsConnected() ? "CONNECTED" : "NOT CONNECTED",
  58. device->GetAssignedLocalUserId(),
  59. static_cast<int>(inputChannelsById.size()));
  60. if (inputChannelsById.size() == 0)
  61. {
  62. Text("%s", childText.c_str());
  63. }
  64. else if (TreeNodeEx(childText.c_str(), 0))
  65. {
  66. AZStd::vector<AzFramework::InputChannelId> channelsSortedByName;
  67. for (const auto& inputChannelById : inputChannelsById)
  68. {
  69. channelsSortedByName.push_back(inputChannelById.first);
  70. }
  71. auto sortFn = [](const AzFramework::InputChannelId& lhs, const AzFramework::InputChannelId& rhs)
  72. {
  73. return azstricmp(lhs.GetName(), rhs.GetName()) < 0;
  74. };
  75. AZStd::sort(channelsSortedByName.begin(), channelsSortedByName.end(), sortFn);
  76. if (BeginTable("InputChannels", 5, ImGuiTableFlags_Borders))
  77. {
  78. TableSetupColumn("Channel");
  79. TableSetupColumn("Value");
  80. TableSetupColumn("Delta");
  81. TableSetupColumn("State");
  82. TableSetupColumn("Active");
  83. TableHeadersRow();
  84. for (const auto& inputChannelId : channelsSortedByName)
  85. {
  86. auto foundChannel = inputChannelsById.find(inputChannelId);
  87. if (foundChannel == inputChannelsById.end())
  88. {
  89. continue;
  90. }
  91. const AzFramework::InputChannel* channel = foundChannel->second;
  92. if (channel)
  93. {
  94. TableNextColumn();
  95. Text("%s", channel->GetInputChannelId().GetName());
  96. TableNextColumn();
  97. Text("%0.4f", channel->GetValue());
  98. TableNextColumn();
  99. Text("%0.4f", channel->GetDelta());
  100. TableNextColumn();
  101. Text("%s%s%s%s",
  102. channel->IsStateIdle() ? "IDLE" : "",
  103. channel->IsStateBegan() ? "BEGAN" : "",
  104. channel->IsStateUpdated() ? "UPDATED" : "",
  105. channel->IsStateEnded() ? "ENDED" : "");
  106. TableNextColumn();
  107. Text("%s", channel->IsActive() ? "ACTIVE" : "INACTIVE");
  108. }
  109. }
  110. EndTable();
  111. }
  112. TreePop(); // End Tree
  113. }
  114. }
  115. }
  116. }
  117. }
  118. } // namespace ImGui
  119. #endif // IMGUI_ENABLED