Commit MetaInfo

Revision7f27942eebd505baf462bc2385bfe617d14944ad (tree)
Time2020-03-03 06:26:38
AuthorDavid Ludwig <dludwig@pobo...>
CommiterDavid Ludwig

Log Message

misc code cleanups

Change Summary

Incremental Difference

diff -r 6c97cb93d91f -r 7f27942eebd5 main.cpp
--- a/main.cpp Sun Mar 01 15:41:36 2020 -0500
+++ b/main.cpp Mon Mar 02 16:26:38 2020 -0500
@@ -42,6 +42,20 @@
4242 static Uint64 timeFrequency = 0;
4343 static bool didCopyMapping = false;
4444
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+
4559 // TODO: persist these on-disk
4660 static bool autoOpenJoysticks = false;
4761 static bool autoOpenGameControllers = true;
@@ -51,7 +65,67 @@
5165 static bool showWindowGameControllers = true;
5266
5367
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);
55129
56130 void ImGui_TextGUID(SDL_JoystickGUID guid)
57131 {
@@ -101,12 +175,14 @@
101175 return SDL_arraysize(EnumTypeInfo<EnumType>::infos);
102176 }
103177
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[] =
104183
105-template <>
106-const SDL_JoystickType EnumTypeInfo<SDL_JoystickType>::defaultValue = SDL_JOYSTICK_TYPE_UNKNOWN;
107184
108-template <>
109-const EnumTypeInfo<SDL_JoystickType> EnumTypeInfo<SDL_JoystickType>::infos[] = {
185+ENUM_TYPE_INFO(SDL_JoystickType, SDL_JOYSTICK_TYPE_UNKNOWN) {
110186 { SDL_JOYSTICK_TYPE_UNKNOWN, "Unknown" },
111187 { SDL_JOYSTICK_TYPE_GAMECONTROLLER, "Game controller" },
112188 { SDL_JOYSTICK_TYPE_WHEEL, "Wheel" },
@@ -119,12 +195,7 @@
119195 { SDL_JOYSTICK_TYPE_THROTTLE, "Throttle" }
120196 };
121197
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) {
128199 { SDL_CONTROLLER_TYPE_UNKNOWN, "Unknown" },
129200 { SDL_CONTROLLER_TYPE_XBOX360, "XBox 360" },
130201 { SDL_CONTROLLER_TYPE_XBOXONE, "XBox One" },
@@ -136,12 +207,7 @@
136207 #endif
137208 };
138209
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) {
145211 { SDL_CONTROLLER_BUTTON_INVALID, "Invalid" },
146212 { SDL_CONTROLLER_BUTTON_A, "A" },
147213 { SDL_CONTROLLER_BUTTON_B, "B" },
@@ -161,12 +227,7 @@
161227 // { SDL_CONTROLLER_BUTTON_MAX, "Max" }, // don't convert this to a value
162228 };
163229
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) {
170231 { SDL_CONTROLLER_AXIS_INVALID, "Invalid" },
171232 { SDL_CONTROLLER_AXIS_LEFTX, "Left X" },
172233 { SDL_CONTROLLER_AXIS_LEFTY, "Left Y" },
@@ -178,36 +239,11 @@
178239 };
179240
180241
181-void ShowJoystickSubWindow(int device_index);
182-
183-void ShowJoystickMainWindow()
242+void ShowJoystickChildWindow(int device_index);
243+void ShowJoysticksWindow()
184244 {
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);
211247 ImGui::Begin("Joysticks", NULL, 0);
212248
213249 #if SDL_JOYSTICK_VIRTUAL
@@ -281,12 +317,12 @@
281317 ImGui::Separator();
282318 ImGui::Separator();
283319
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);
287323 }
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);
290326 ImGui::NextColumn();
291327 }
292328
@@ -372,13 +408,13 @@
372408 ImGui::Columns(1);
373409
374410 // show sub-content
375- ShowJoystickSubWindow(device_index);
411+ ShowJoystickChildWindow(device_index);
376412
377413 // restore state
378414 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);
382418 }
383419 ImGui::PopID();
384420 }
@@ -388,7 +424,7 @@
388424 ImGui::End();
389425 }
390426
391-void ShowJoystickSubWindow(int device_index)
427+void ShowJoystickChildWindow(int device_index)
392428 {
393429 SDL_JoystickID device_instance_id = SDL_JoystickGetDeviceInstanceID(device_index);
394430 SDL_Joystick * joystick = SDL_JoystickFromInstanceID(device_instance_id);
@@ -706,36 +742,12 @@
706742 ImGui::EndChild();
707743 }
708744
709-static void ShowGameControllerSubWindow(int device_index);
745+static void ShowGameControllerChildWindow(int device_index);
710746
711-static void ShowGameControllersMainWindow()
747+static void ShowGameControllersWindow()
712748 {
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);
739751 ImGui::Begin("Game Controllers", NULL, 0);
740752
741753 // if (ImGui::Button("Add Virtual Game Controller")) {
@@ -757,12 +769,12 @@
757769 ImGui::Separator();
758770 ImGui::Separator();
759771
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);
763775 }
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);
766778 ImGui::NextColumn();
767779 }
768780
@@ -914,13 +926,13 @@
914926 ImGui::Columns(1);
915927
916928 // show sub-content
917- ShowGameControllerSubWindow(device_index);
929+ ShowGameControllerChildWindow(device_index);
918930
919931 // restore state
920932 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);
924936 }
925937 ImGui::PopID();
926938 }
@@ -931,7 +943,7 @@
931943 ImGui::End();
932944 }
933945
934-void ShowGameControllerSubWindow(int device_index)
946+void ShowGameControllerChildWindow(int device_index)
935947 {
936948 SDL_JoystickID device_instance_id = SDL_JoystickGetDeviceInstanceID(device_index);
937949 SDL_GameController * controller = SDL_GameControllerFromInstanceID(device_instance_id);
@@ -1136,10 +1148,10 @@
11361148 ImGui::ShowDemoWindow();
11371149 }
11381150 if (showWindowJoysticks) {
1139- ShowJoystickMainWindow();
1151+ ShowJoysticksWindow();
11401152 }
11411153 if (showWindowGameControllers) {
1142- ShowGameControllersMainWindow();
1154+ ShowGameControllersWindow();
11431155 }
11441156
11451157 // Tell ImGui + SDL that we're done drawing, for now
@@ -1153,18 +1165,16 @@
11531165 int main()
11541166 {
11551167 // Initialize SDL and ImGui
1156- const int sysWindowWidth = 1100;
1157- const int sysWindowHeight = 700;
11581168 SDL_Init(SDL_INIT_EVERYTHING);
1169+
11591170 SDL_Window * window = SDL_CreateWindow(
11601171 "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+ );
11681178 renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
11691179 ImGui::CreateContext();
11701180 ImGuiSDL::Initialize(renderer, sysWindowWidth, sysWindowHeight);
Show on old repository browser