Commit MetaInfo

Revisão2e785b10556b7e10e7e52edf2cb7a380f31dd2df (tree)
Hora2020-03-01 07:44:40
AutorDavid Ludwig <dludwig@pobo...>
CommiterDavid Ludwig

Mensagem de Log

misc game-controller improvements

Mudança Sumário

Diff

diff -r cec9252a158c -r 2e785b10556b main.cpp
--- a/main.cpp Fri Feb 28 13:39:53 2020 -0500
+++ b/main.cpp Sat Feb 29 17:44:40 2020 -0500
@@ -36,6 +36,12 @@
3636 #include "emscripten.h"
3737 #endif
3838
39+
40+// TODO: persist these on-disk
41+static bool autoOpenJoysticks = false;
42+static bool autoOpenGameControllers = true;
43+
44+
3945 extern bool ImGui_ImplSDL2_ProcessEvent(const SDL_Event* event);
4046
4147 void ImGui_TextGUID(SDL_JoystickGUID guid)
@@ -108,6 +114,67 @@
108114 return ControllerTypeName(SDL_CONTROLLER_TYPE_UNKNOWN);
109115 }
110116
117+struct GameControllerButtonInfo {
118+ SDL_GameControllerButton button;
119+ const char *name;
120+};
121+static const GameControllerButtonInfo controller_button_type_infos[] = {
122+ { SDL_CONTROLLER_BUTTON_INVALID, "Invalid" },
123+ { SDL_CONTROLLER_BUTTON_A, "A" },
124+ { SDL_CONTROLLER_BUTTON_B, "B" },
125+ { SDL_CONTROLLER_BUTTON_X, "X" },
126+ { SDL_CONTROLLER_BUTTON_Y, "Y" },
127+ { SDL_CONTROLLER_BUTTON_BACK, "Back" },
128+ { SDL_CONTROLLER_BUTTON_GUIDE, "Guide" },
129+ { SDL_CONTROLLER_BUTTON_START, "Start" },
130+ { SDL_CONTROLLER_BUTTON_LEFTSTICK, "Left Stick" },
131+ { SDL_CONTROLLER_BUTTON_RIGHTSTICK, "Right Stick" },
132+ { SDL_CONTROLLER_BUTTON_LEFTSHOULDER, "Left Shoulder" },
133+ { SDL_CONTROLLER_BUTTON_RIGHTSHOULDER, "Right Shoulder" },
134+ { SDL_CONTROLLER_BUTTON_DPAD_UP, "DPad Up" },
135+ { SDL_CONTROLLER_BUTTON_DPAD_DOWN, "DPad Down" },
136+ { SDL_CONTROLLER_BUTTON_DPAD_LEFT, "DPad Left" },
137+ { SDL_CONTROLLER_BUTTON_DPAD_RIGHT, "DPad Right" },
138+ // { SDL_CONTROLLER_BUTTON_MAX, "Max" }, // don't convert this to a value
139+};
140+
141+constexpr const char * GameControllerButtonName(SDL_GameControllerButton button)
142+{
143+ for (int i = 0; i < SDL_arraysize(controller_button_type_infos); ++i) {
144+ if (button == controller_button_type_infos[i].button) {
145+ return controller_button_type_infos[i].name;
146+ }
147+ }
148+ return GameControllerButtonName(SDL_CONTROLLER_BUTTON_INVALID);
149+}
150+
151+
152+struct GameControllerAxisInfo {
153+ SDL_GameControllerAxis axis;
154+ const char *name;
155+};
156+static const GameControllerAxisInfo controller_axis_type_infos[] = {
157+ { SDL_CONTROLLER_AXIS_INVALID, "Invalid" },
158+ { SDL_CONTROLLER_AXIS_LEFTX, "Left X" },
159+ { SDL_CONTROLLER_AXIS_LEFTY, "Left Y" },
160+ { SDL_CONTROLLER_AXIS_RIGHTX, "Right X" },
161+ { SDL_CONTROLLER_AXIS_RIGHTY, "Right Y" },
162+ { SDL_CONTROLLER_AXIS_TRIGGERLEFT, "Trigger Left" },
163+ { SDL_CONTROLLER_AXIS_TRIGGERRIGHT, "Trigger Right" },
164+ // { SDL_CONTROLLER_AXIS_MAX, "MAX" }, // don't convert this to a value
165+};
166+
167+constexpr const char * GameControllerAxisName(SDL_GameControllerAxis axis)
168+{
169+ for (int i = 0; i < SDL_arraysize(controller_axis_type_infos); ++i) {
170+ if (axis == controller_axis_type_infos[i].axis) {
171+ return controller_axis_type_infos[i].name;
172+ }
173+ }
174+ return GameControllerAxisName(SDL_CONTROLLER_AXIS_INVALID);
175+}
176+
177+
111178 void ShowJoystickSubWindow(int device_index);
112179
113180 void ShowJoystickMainWindow()
@@ -200,7 +267,6 @@
200267 ImGui::SameLine();
201268 const int num_joysticks = SDL_NumJoysticks();
202269 ImGui::Text("Joysticks, Total-Count: %d", num_joysticks);
203- static bool autoOpenJoysticks = false; // TODO: persist this on-disk
204270 // ImGui::SameLine();
205271 // ImGui::Dummy(ImVec2(32,0));
206272 // ImGui::SameLine();
@@ -620,6 +686,8 @@
620686 ImGui::EndChild();
621687 }
622688
689+static void ShowGameControllerSubWindow(int device_index);
690+
623691 static void ShowGameControllersMainWindow()
624692 {
625693 struct {
@@ -644,9 +712,9 @@
644712 }
645713
646714 const float window_margins = 1;
647- const float main_window_height = 480.f;
715+ const float main_window_height = 550.f;
648716
649- ImGui::SetNextWindowPos(ImVec2(window_margins + 20.f, 200.f + window_margins), ImGuiCond_Once);
717+ ImGui::SetNextWindowPos(ImVec2(window_margins + 20.f, 150.f + window_margins), ImGuiCond_Once);
650718 ImGui::SetNextWindowSize(ImVec2(total_width, main_window_height), ImGuiCond_Once);
651719 ImGui::Begin("Game Controllers", NULL, 0);
652720
@@ -688,9 +756,9 @@
688756 // SDL_Joystick * joystick = SDL_JoystickFromInstanceID(device_instance_id);
689757
690758 ImGuiTreeNodeFlags tree_node_flags = 0;
691- // if (joystick) {
692- // tree_node_flags |= ImGuiTreeNodeFlags_DefaultOpen;
693- // }
759+ if (controller) {
760+ tree_node_flags |= ImGuiTreeNodeFlags_DefaultOpen;
761+ }
694762 const bool tree_node_is_open = ImGui::TreeNodeEx((void*)(intptr_t)device_index, tree_node_flags, ""); //, "Joystick #%d", device_index);
695763 ImGui::NextColumn();
696764
@@ -701,7 +769,7 @@
701769 controller = nullptr;
702770 }
703771 } else {
704- if (ImGui::Button("Open")) {
772+ if (ImGui::Button("Open") || autoOpenGameControllers) {
705773 controller = SDL_GameControllerOpen(device_index);
706774 }
707775 }
@@ -828,7 +896,7 @@
828896 ImGui::Columns(1);
829897
830898 // show sub-content
831- // ShowJoystickSubWindow(device_index);
899+ ShowGameControllerSubWindow(device_index);
832900
833901 // restore state
834902 ImGui::TreePop();
@@ -845,6 +913,132 @@
845913 ImGui::End();
846914 }
847915
916+void ShowGameControllerSubWindow(int device_index)
917+{
918+ SDL_JoystickID device_instance_id = SDL_JoystickGetDeviceInstanceID(device_index);
919+ SDL_GameController * controller = SDL_GameControllerFromInstanceID(device_instance_id);
920+ if (!controller) {
921+ return;
922+ }
923+
924+ char window_name[64];
925+ SDL_snprintf(window_name, SDL_arraysize(window_name), "Controller #%d", device_index);
926+ ImGui::BeginChild(window_name, ImVec2(0,0), true, 0);
927+
928+ // Basics
929+ {
930+ ImGui::SetCursorPos(ImVec2(8,8));
931+ ImGui::BeginChild("Basics", ImVec2(400,160), true);
932+ ImGui::Text("Basics:");
933+ ImGui::Separator();
934+ ImGui::Columns(2);
935+ ImGui::SetColumnWidth(0, 170.f);
936+ ImGui::SetColumnWidth(1, 200.f);
937+
938+ ImGui::Text("Name");
939+ ImGui::NextColumn();
940+ ImGui::Text("%s", SDL_GameControllerName(controller));
941+ ImGui::NextColumn();
942+
943+ ImGui::Text("Type");
944+ ImGui::NextColumn();
945+ ImGui::Text("%s", ControllerTypeName(SDL_GameControllerGetType(controller)));
946+ ImGui::NextColumn();
947+
948+ ImGui::Text("Player Index");
949+ ImGui::NextColumn();
950+ ImGui::Text("%d", SDL_GameControllerGetPlayerIndex(controller));
951+ ImGui::NextColumn();
952+
953+ ImGui::Text("Vendor");
954+ ImGui::NextColumn();
955+ ImGui::Text("0x%04x", (int)SDL_GameControllerGetVendor(controller));
956+ ImGui::NextColumn();
957+
958+ ImGui::Text("Product");
959+ ImGui::NextColumn();
960+ ImGui::Text("0x%04x", (int)SDL_GameControllerGetProduct(controller));
961+ ImGui::NextColumn();
962+
963+ ImGui::Text("Product Version");
964+ ImGui::NextColumn();
965+ ImGui::Text("0x%04x", (int)SDL_GameControllerGetProductVersion(controller));
966+ ImGui::NextColumn();
967+
968+ ImGui::Text("Attached?");
969+ ImGui::NextColumn();
970+ ImGui::Text("%s", (SDL_GameControllerGetAttached(controller) == SDL_TRUE ? "YES" : "NO"));
971+ ImGui::NextColumn();
972+
973+ ImGui::EndChild();
974+ }
975+
976+ // Buttons
977+ {
978+ ImGui::SetCursorPos(ImVec2(416,8));
979+ ImGui::BeginChild("Buttons", ImVec2(250,400), true);
980+ ImGui::Text("Buttons:");
981+ ImGui::Separator();
982+ ImGui::Columns(3);
983+ ImGui::SetColumnWidth(0, 170.f);
984+ ImGui::SetColumnWidth(1, 32.f);
985+ ImGui::SetColumnWidth(2, 64.f);
986+ for (int i = 0; i < (int)SDL_CONTROLLER_BUTTON_MAX; ++i) {
987+ ImGui::PushID(i);
988+ const SDL_GameControllerButton button = (SDL_GameControllerButton)i;
989+ const Uint8 buttonState = SDL_GameControllerGetButton(controller, button);
990+ ImGui::PushStyleVar(ImGuiStyleVar_FrameBorderSize, 1.f);
991+ if (buttonState != 0) {
992+ ImGui::PushStyleColor(ImGuiCol_Text, (ImVec4)ImColor(255,255,0));
993+ ImGui::PushStyleColor(ImGuiCol_Button, (ImVec4)ImColor(255,0,0));
994+ } else {
995+ ImGui::PushStyleColor(ImGuiCol_Text, (ImVec4)ImColor(255,255,255));
996+ ImGui::PushStyleColor(ImGuiCol_Button, (ImVec4)ImColor(0,0,0));
997+ }
998+ ImGui::PushStyleColor(ImGuiCol_Border, (ImVec4)ImColor(255,255,255));
999+ ImGui::Text("Button, %s", GameControllerButtonName(button));
1000+ ImGui::NextColumn();
1001+ ImGui::Text("%d", (int)buttonState);
1002+ ImGui::NextColumn();
1003+ ImGui::Button("", ImVec2(20,20));
1004+ ImGui::PopStyleColor(3);
1005+ ImGui::PopStyleVar(1);
1006+ ImGui::NextColumn();
1007+ ImGui::PopID();
1008+ }
1009+ ImGui::EndChild();
1010+ }
1011+
1012+ // Axes
1013+ {
1014+ ImGui::SetCursorPos(ImVec2(8,180));
1015+ ImGui::BeginChild("Axes", ImVec2(400,200), true);
1016+ ImGui::Text("Axes:");
1017+ ImGui::Separator();
1018+ ImGui::Columns(3);
1019+ ImGui::SetColumnWidth(0, 150.f);
1020+ ImGui::SetColumnWidth(1, 60.f);
1021+ ImGui::SetColumnWidth(2, 250.f);
1022+ for (int i = 0; i < (int)SDL_CONTROLLER_AXIS_MAX; ++i) {
1023+ ImGui::PushID(i);
1024+ const SDL_GameControllerAxis key = (SDL_GameControllerAxis)i;
1025+ Sint16 value = SDL_GameControllerGetAxis(controller, key);
1026+ ImGui::Text("Axis, %s", GameControllerAxisName(key));
1027+ ImGui::NextColumn();
1028+ ImGui::Text("%d", (int)value);
1029+ ImGui::NextColumn();
1030+ const Sint16 s16_min = std::numeric_limits<Sint16>::min();
1031+ const Sint16 s16_max = std::numeric_limits<Sint16>::max();
1032+ ImGui::SliderScalar("", ImGuiDataType_S16, &value, &s16_min, &s16_max, "");
1033+ ImGui::NextColumn();
1034+ ImGui::PopID();
1035+ }
1036+ ImGui::EndChild();
1037+ }
1038+
1039+ ImGui::EndChild();
1040+}
1041+
8481042 static bool isRunning = true;
8491043 static SDL_Renderer * renderer = nullptr;
8501044
@@ -911,7 +1105,7 @@
9111105 // Layout this frame's ImGui
9121106 static bool show_window_imgui_demo = false;
9131107 static bool show_window_joysticks = true;
914- static bool show_window_game_controllers = false;
1108+ static bool show_window_game_controllers = true;
9151109 if (ImGui::BeginMainMenuBar()) {
9161110 if (ImGui::BeginMenu("View")) {
9171111 ImGui::MenuItem("Game Controllers...", NULL, &show_window_game_controllers);
Show on old repository browser