Revisão | 7f27942eebd505baf462bc2385bfe617d14944ad (tree) |
---|---|
Hora | 2020-03-03 06:26:38 |
Autor | David Ludwig <dludwig@pobo...> |
Commiter | David Ludwig |
misc code cleanups
@@ -42,6 +42,20 @@ | ||
42 | 42 | static Uint64 timeFrequency = 0; |
43 | 43 | static bool didCopyMapping = false; |
44 | 44 | |
45 | +#if DEV_DAVIDL | |
46 | + const int sysWindowWidth = 1100; | |
47 | + const int sysWindowHeight = 700; | |
48 | + const int sysWindowX = 1900; | |
49 | + const int sysWindowY = 450; | |
50 | + const Uint32 sysWindowFlags = SDL_WINDOW_RESIZABLE | SDL_WINDOW_MAXIMIZED; | |
51 | +#else | |
52 | + const int sysWindowWidth = 1100; | |
53 | + const int sysWindowHeight = 700; | |
54 | + const int sysWindowX = SDL_WINDOWPOS_CENTERED; | |
55 | + const int sysWindowY = SDL_WINDOWPOS_CENTERED; | |
56 | + const Uint32 sysWindowFlags = SDL_WINDOW_RESIZABLE; | |
57 | +#endif | |
58 | + | |
45 | 59 | // TODO: persist these on-disk |
46 | 60 | static bool autoOpenJoysticks = false; |
47 | 61 | static bool autoOpenGameControllers = true; |
@@ -51,7 +65,67 @@ | ||
51 | 65 | static bool showWindowGameControllers = true; |
52 | 66 | |
53 | 67 | |
54 | -extern bool ImGui_ImplSDL2_ProcessEvent(const SDL_Event* event); | |
68 | +struct NamedColumn { | |
69 | + float width; | |
70 | + const char *name; | |
71 | +}; | |
72 | + | |
73 | +template <typename Collection> | |
74 | +constexpr float TotalWidth(const Collection & aCollection) { | |
75 | + float total = 0.f; | |
76 | + for (const auto & it : aCollection) { | |
77 | + total += it.width; | |
78 | + } | |
79 | + return total; | |
80 | +} | |
81 | + | |
82 | +const NamedColumn joystickWindowColumns[] = { | |
83 | + { 35.f, "" }, | |
84 | + { 60.f, "Actions" }, | |
85 | + { 55.f, "Device\nIndex" }, | |
86 | + { 250.f, "Name" }, | |
87 | + { 60.f, "Player\nIndex" }, | |
88 | + { 265.f, "Device\nGUID" }, | |
89 | + { 118.f, "Type" }, | |
90 | + { 70.f, "Instance\nID" }, | |
91 | + { 70.f, "Virtual?" }, | |
92 | + { 110.f, "Game\nController?"}, | |
93 | +}; | |
94 | +const float joystickWindowMargins = 1.f; | |
95 | +const ImVec2 joystickWindowPos = { | |
96 | + joystickWindowMargins, | |
97 | + 20.f + joystickWindowMargins | |
98 | +}; | |
99 | +const ImVec2 joystickWindowSize = { | |
100 | + TotalWidth(joystickWindowColumns), | |
101 | + 650.f | |
102 | +}; | |
103 | + | |
104 | +const NamedColumn gameControllersWindowColumns[] = { | |
105 | + { 35.f, "" }, | |
106 | + { 60.f, "Actions" }, | |
107 | + { 70.f, "Joystick\nDevice\nIndex" }, | |
108 | + { 250.f, "Name" }, | |
109 | + { 60.f, "Player\nIndex" }, | |
110 | + { 150.f, "Type" }, | |
111 | + { 95.f, "Has Mapping?" }, | |
112 | + { 55.f, "Vendor" }, | |
113 | + { 60.f, "Product" }, | |
114 | + { 65.f, "Product\nVersion" }, | |
115 | + { 75.f, "Attached?" }, | |
116 | +}; | |
117 | +const float gameControllersWindowMargins = 1.f; | |
118 | +const ImVec2 gameControllersWindowPos = { | |
119 | + 20.f + gameControllersWindowMargins, | |
120 | + 150.f + gameControllersWindowMargins | |
121 | +}; | |
122 | +const ImVec2 gameControllersWindowSize = { | |
123 | + TotalWidth(gameControllersWindowColumns), | |
124 | + 550.f | |
125 | +}; | |
126 | + | |
127 | + | |
128 | +extern bool ImGui_ImplSDL2_ProcessEvent(const SDL_Event *event); | |
55 | 129 | |
56 | 130 | void ImGui_TextGUID(SDL_JoystickGUID guid) |
57 | 131 | { |
@@ -101,12 +175,14 @@ | ||
101 | 175 | return SDL_arraysize(EnumTypeInfo<EnumType>::infos); |
102 | 176 | } |
103 | 177 | |
178 | +#define ENUM_TYPE_INFO(TYPE, DEFAULT_VALUE) \ | |
179 | + template <> \ | |
180 | + const TYPE EnumTypeInfo<TYPE>::defaultValue = DEFAULT_VALUE; \ | |
181 | + template <> \ | |
182 | + const EnumTypeInfo<TYPE> EnumTypeInfo<TYPE>::infos[] = | |
104 | 183 | |
105 | -template <> | |
106 | -const SDL_JoystickType EnumTypeInfo<SDL_JoystickType>::defaultValue = SDL_JOYSTICK_TYPE_UNKNOWN; | |
107 | 184 | |
108 | -template <> | |
109 | -const EnumTypeInfo<SDL_JoystickType> EnumTypeInfo<SDL_JoystickType>::infos[] = { | |
185 | +ENUM_TYPE_INFO(SDL_JoystickType, SDL_JOYSTICK_TYPE_UNKNOWN) { | |
110 | 186 | { SDL_JOYSTICK_TYPE_UNKNOWN, "Unknown" }, |
111 | 187 | { SDL_JOYSTICK_TYPE_GAMECONTROLLER, "Game controller" }, |
112 | 188 | { SDL_JOYSTICK_TYPE_WHEEL, "Wheel" }, |
@@ -119,12 +195,7 @@ | ||
119 | 195 | { SDL_JOYSTICK_TYPE_THROTTLE, "Throttle" } |
120 | 196 | }; |
121 | 197 | |
122 | - | |
123 | -template <> | |
124 | -const SDL_GameControllerType EnumTypeInfo<SDL_GameControllerType>::defaultValue = SDL_CONTROLLER_TYPE_UNKNOWN; | |
125 | - | |
126 | -template <> | |
127 | -const EnumTypeInfo<SDL_GameControllerType> EnumTypeInfo<SDL_GameControllerType>::infos[] = { | |
198 | +ENUM_TYPE_INFO(SDL_GameControllerType, SDL_CONTROLLER_TYPE_UNKNOWN) { | |
128 | 199 | { SDL_CONTROLLER_TYPE_UNKNOWN, "Unknown" }, |
129 | 200 | { SDL_CONTROLLER_TYPE_XBOX360, "XBox 360" }, |
130 | 201 | { SDL_CONTROLLER_TYPE_XBOXONE, "XBox One" }, |
@@ -136,12 +207,7 @@ | ||
136 | 207 | #endif |
137 | 208 | }; |
138 | 209 | |
139 | - | |
140 | -template <> | |
141 | -const SDL_GameControllerButton EnumTypeInfo<SDL_GameControllerButton>::defaultValue = SDL_CONTROLLER_BUTTON_INVALID; | |
142 | - | |
143 | -template <> | |
144 | -const EnumTypeInfo<SDL_GameControllerButton> EnumTypeInfo<SDL_GameControllerButton>::infos[] = { | |
210 | +ENUM_TYPE_INFO(SDL_GameControllerButton, SDL_CONTROLLER_BUTTON_INVALID) { | |
145 | 211 | { SDL_CONTROLLER_BUTTON_INVALID, "Invalid" }, |
146 | 212 | { SDL_CONTROLLER_BUTTON_A, "A" }, |
147 | 213 | { SDL_CONTROLLER_BUTTON_B, "B" }, |
@@ -161,12 +227,7 @@ | ||
161 | 227 | // { SDL_CONTROLLER_BUTTON_MAX, "Max" }, // don't convert this to a value |
162 | 228 | }; |
163 | 229 | |
164 | - | |
165 | -template <> | |
166 | -const SDL_GameControllerAxis EnumTypeInfo<SDL_GameControllerAxis>::defaultValue = SDL_CONTROLLER_AXIS_INVALID; | |
167 | - | |
168 | -template <> | |
169 | -const EnumTypeInfo<SDL_GameControllerAxis> EnumTypeInfo<SDL_GameControllerAxis>::infos[] = { | |
230 | +ENUM_TYPE_INFO(SDL_GameControllerAxis, SDL_CONTROLLER_AXIS_INVALID) { | |
170 | 231 | { SDL_CONTROLLER_AXIS_INVALID, "Invalid" }, |
171 | 232 | { SDL_CONTROLLER_AXIS_LEFTX, "Left X" }, |
172 | 233 | { SDL_CONTROLLER_AXIS_LEFTY, "Left Y" }, |
@@ -178,36 +239,11 @@ | ||
178 | 239 | }; |
179 | 240 | |
180 | 241 | |
181 | -void ShowJoystickSubWindow(int device_index); | |
182 | - | |
183 | -void ShowJoystickMainWindow() | |
242 | +void ShowJoystickChildWindow(int device_index); | |
243 | +void ShowJoysticksWindow() | |
184 | 244 | { |
185 | - struct { | |
186 | - float width; | |
187 | - const char *name; | |
188 | - } main_columns[] = { | |
189 | - { 35.f, "" }, | |
190 | - { 60.f, "Actions" }, | |
191 | - { 55.f, "Device\nIndex" }, | |
192 | - { 250.f, "Name" }, | |
193 | - { 60.f, "Player\nIndex" }, | |
194 | - { 265.f, "Device\nGUID" }, | |
195 | - { 118.f, "Type" }, | |
196 | - { 70.f, "Instance\nID" }, | |
197 | - { 70.f, "Virtual?" }, | |
198 | - { 110.f, "Game\nController?"}, | |
199 | - }; | |
200 | - float total_width = 0.f; | |
201 | - for (const auto & col : main_columns) { | |
202 | - total_width += col.width; | |
203 | - } | |
204 | - | |
205 | - const float window_margins = 1; | |
206 | - // const float main_window_height = 480.f; | |
207 | - const float main_window_height = 650.f; | |
208 | - | |
209 | - ImGui::SetNextWindowPos(ImVec2(window_margins, 20.f + window_margins), ImGuiCond_Once); | |
210 | - ImGui::SetNextWindowSize(ImVec2(total_width, main_window_height), ImGuiCond_Once); | |
245 | + ImGui::SetNextWindowPos(joystickWindowPos, ImGuiCond_Once); | |
246 | + ImGui::SetNextWindowSize(joystickWindowSize, ImGuiCond_Once); | |
211 | 247 | ImGui::Begin("Joysticks", NULL, 0); |
212 | 248 | |
213 | 249 | #if SDL_JOYSTICK_VIRTUAL |
@@ -281,12 +317,12 @@ | ||
281 | 317 | ImGui::Separator(); |
282 | 318 | ImGui::Separator(); |
283 | 319 | |
284 | - ImGui::Columns(SDL_arraysize(main_columns), "JoystickList", true); | |
285 | - for (int i = 0; i < SDL_arraysize(main_columns); ++i) { | |
286 | - ImGui::SetColumnWidth(i, main_columns[i].width); | |
320 | + ImGui::Columns(SDL_arraysize(joystickWindowColumns), "JoystickList", true); | |
321 | + for (int i = 0; i < SDL_arraysize(joystickWindowColumns); ++i) { | |
322 | + ImGui::SetColumnWidth(i, joystickWindowColumns[i].width); | |
287 | 323 | } |
288 | - for (int i = 0; i < SDL_arraysize(main_columns); ++i) { | |
289 | - ImGui::Text("%s", main_columns[i].name); | |
324 | + for (int i = 0; i < SDL_arraysize(joystickWindowColumns); ++i) { | |
325 | + ImGui::Text("%s", joystickWindowColumns[i].name); | |
290 | 326 | ImGui::NextColumn(); |
291 | 327 | } |
292 | 328 |
@@ -372,13 +408,13 @@ | ||
372 | 408 | ImGui::Columns(1); |
373 | 409 | |
374 | 410 | // show sub-content |
375 | - ShowJoystickSubWindow(device_index); | |
411 | + ShowJoystickChildWindow(device_index); | |
376 | 412 | |
377 | 413 | // restore state |
378 | 414 | ImGui::TreePop(); |
379 | - ImGui::Columns(SDL_arraysize(main_columns), "JoystickList", true); | |
380 | - for (int i = 0; i < SDL_arraysize(main_columns); ++i) { | |
381 | - ImGui::SetColumnWidth(i, main_columns[i].width); | |
415 | + ImGui::Columns(SDL_arraysize(joystickWindowColumns), "JoystickList", true); | |
416 | + for (int i = 0; i < SDL_arraysize(joystickWindowColumns); ++i) { | |
417 | + ImGui::SetColumnWidth(i, joystickWindowColumns[i].width); | |
382 | 418 | } |
383 | 419 | ImGui::PopID(); |
384 | 420 | } |
@@ -388,7 +424,7 @@ | ||
388 | 424 | ImGui::End(); |
389 | 425 | } |
390 | 426 | |
391 | -void ShowJoystickSubWindow(int device_index) | |
427 | +void ShowJoystickChildWindow(int device_index) | |
392 | 428 | { |
393 | 429 | SDL_JoystickID device_instance_id = SDL_JoystickGetDeviceInstanceID(device_index); |
394 | 430 | SDL_Joystick * joystick = SDL_JoystickFromInstanceID(device_instance_id); |
@@ -706,36 +742,12 @@ | ||
706 | 742 | ImGui::EndChild(); |
707 | 743 | } |
708 | 744 | |
709 | -static void ShowGameControllerSubWindow(int device_index); | |
745 | +static void ShowGameControllerChildWindow(int device_index); | |
710 | 746 | |
711 | -static void ShowGameControllersMainWindow() | |
747 | +static void ShowGameControllersWindow() | |
712 | 748 | { |
713 | - struct { | |
714 | - float width; | |
715 | - const char *name; | |
716 | - } main_columns[] = { | |
717 | - { 35.f, "" }, | |
718 | - { 60.f, "Actions" }, // OK | |
719 | - { 70.f, "Joystick\nDevice\nIndex" }, // OK | |
720 | - { 250.f, "Name" }, // ok | |
721 | - { 60.f, "Player\nIndex" }, // ok | |
722 | - { 150.f, "Type" }, // ok | |
723 | - { 95.f, "Has Mapping?" }, | |
724 | - { 55.f, "Vendor" }, | |
725 | - { 60.f, "Product" }, | |
726 | - { 65.f, "Product\nVersion" }, | |
727 | - { 75.f, "Attached?" }, | |
728 | - }; | |
729 | - float total_width = 0.f; | |
730 | - for (const auto & col : main_columns) { | |
731 | - total_width += col.width; | |
732 | - } | |
733 | - | |
734 | - const float window_margins = 1; | |
735 | - const float main_window_height = 550.f; | |
736 | - | |
737 | - ImGui::SetNextWindowPos(ImVec2(window_margins + 20.f, 150.f + window_margins), ImGuiCond_Once); | |
738 | - ImGui::SetNextWindowSize(ImVec2(total_width, main_window_height), ImGuiCond_Once); | |
749 | + ImGui::SetNextWindowPos(gameControllersWindowPos, ImGuiCond_Once); | |
750 | + ImGui::SetNextWindowSize(gameControllersWindowSize, ImGuiCond_Once); | |
739 | 751 | ImGui::Begin("Game Controllers", NULL, 0); |
740 | 752 | |
741 | 753 | // if (ImGui::Button("Add Virtual Game Controller")) { |
@@ -757,12 +769,12 @@ | ||
757 | 769 | ImGui::Separator(); |
758 | 770 | ImGui::Separator(); |
759 | 771 | |
760 | - ImGui::Columns(SDL_arraysize(main_columns), "ControllerList", true); | |
761 | - for (int i = 0; i < SDL_arraysize(main_columns); ++i) { | |
762 | - ImGui::SetColumnWidth(i, main_columns[i].width); | |
772 | + ImGui::Columns(SDL_arraysize(gameControllersWindowColumns), "ControllerList", true); | |
773 | + for (int i = 0; i < SDL_arraysize(gameControllersWindowColumns); ++i) { | |
774 | + ImGui::SetColumnWidth(i, gameControllersWindowColumns[i].width); | |
763 | 775 | } |
764 | - for (int i = 0; i < SDL_arraysize(main_columns); ++i) { | |
765 | - ImGui::Text("%s", main_columns[i].name); | |
776 | + for (int i = 0; i < SDL_arraysize(gameControllersWindowColumns); ++i) { | |
777 | + ImGui::Text("%s", gameControllersWindowColumns[i].name); | |
766 | 778 | ImGui::NextColumn(); |
767 | 779 | } |
768 | 780 |
@@ -914,13 +926,13 @@ | ||
914 | 926 | ImGui::Columns(1); |
915 | 927 | |
916 | 928 | // show sub-content |
917 | - ShowGameControllerSubWindow(device_index); | |
929 | + ShowGameControllerChildWindow(device_index); | |
918 | 930 | |
919 | 931 | // restore state |
920 | 932 | ImGui::TreePop(); |
921 | - ImGui::Columns(SDL_arraysize(main_columns), "JoystickList", true); | |
922 | - for (int i = 0; i < SDL_arraysize(main_columns); ++i) { | |
923 | - ImGui::SetColumnWidth(i, main_columns[i].width); | |
933 | + ImGui::Columns(SDL_arraysize(gameControllersWindowColumns), "JoystickList", true); | |
934 | + for (int i = 0; i < SDL_arraysize(gameControllersWindowColumns); ++i) { | |
935 | + ImGui::SetColumnWidth(i, gameControllersWindowColumns[i].width); | |
924 | 936 | } |
925 | 937 | ImGui::PopID(); |
926 | 938 | } |
@@ -931,7 +943,7 @@ | ||
931 | 943 | ImGui::End(); |
932 | 944 | } |
933 | 945 | |
934 | -void ShowGameControllerSubWindow(int device_index) | |
946 | +void ShowGameControllerChildWindow(int device_index) | |
935 | 947 | { |
936 | 948 | SDL_JoystickID device_instance_id = SDL_JoystickGetDeviceInstanceID(device_index); |
937 | 949 | SDL_GameController * controller = SDL_GameControllerFromInstanceID(device_instance_id); |
@@ -1136,10 +1148,10 @@ | ||
1136 | 1148 | ImGui::ShowDemoWindow(); |
1137 | 1149 | } |
1138 | 1150 | if (showWindowJoysticks) { |
1139 | - ShowJoystickMainWindow(); | |
1151 | + ShowJoysticksWindow(); | |
1140 | 1152 | } |
1141 | 1153 | if (showWindowGameControllers) { |
1142 | - ShowGameControllersMainWindow(); | |
1154 | + ShowGameControllersWindow(); | |
1143 | 1155 | } |
1144 | 1156 | |
1145 | 1157 | // Tell ImGui + SDL that we're done drawing, for now |
@@ -1153,18 +1165,16 @@ | ||
1153 | 1165 | int main() |
1154 | 1166 | { |
1155 | 1167 | // Initialize SDL and ImGui |
1156 | - const int sysWindowWidth = 1100; | |
1157 | - const int sysWindowHeight = 700; | |
1158 | 1168 | SDL_Init(SDL_INIT_EVERYTHING); |
1169 | + | |
1159 | 1170 | SDL_Window * window = SDL_CreateWindow( |
1160 | 1171 | "SDL Test Panel", |
1161 | -#if DEV_DAVIDL | |
1162 | - 1900, 450, | |
1163 | -#else | |
1164 | - SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, | |
1165 | -#endif | |
1166 | - sysWindowWidth, sysWindowHeight, | |
1167 | - SDL_WINDOW_RESIZABLE); | |
1172 | + sysWindowX, | |
1173 | + sysWindowY, | |
1174 | + sysWindowWidth, | |
1175 | + sysWindowHeight, | |
1176 | + sysWindowFlags | |
1177 | + ); | |
1168 | 1178 | renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); |
1169 | 1179 | ImGui::CreateContext(); |
1170 | 1180 | ImGuiSDL::Initialize(renderer, sysWindowWidth, sysWindowHeight); |