-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUI.cpp
More file actions
352 lines (273 loc) · 14.5 KB
/
GUI.cpp
File metadata and controls
352 lines (273 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#include "GUI.h"
#include "controller_icon.h"
static const unsigned char* controller_map = assets_noun_xbox_controller_7424807_png;
static const unsigned int controller_map_len = assets_noun_xbox_controller_7424807_png_len;
static void helpMarker(const char* desc)
{
ImGui::TextDisabled("(?)");
if (ImGui::IsItemHovered(ImGuiHoveredFlags_DelayShort))
{
ImGui::BeginTooltip();
ImGui::PushTextWrapPos(ImGui::GetFontSize() * 35.0f);
ImGui::TextUnformatted(desc);
ImGui::PopTextWrapPos();
ImGui::EndTooltip();
}
}
GUI::GUI(Window& window, GamepadManager& gamepadManager) : m_window(window), m_gamepad(gamepadManager)
{
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGui::StyleColorsDark();
// Initialize Backends
ImGui_ImplSDL3_InitForSDLRenderer(m_window.getWindow(), m_window.getRenderer());
ImGui_ImplSDLRenderer3_Init(m_window.getRenderer());
m_texture = TextureLoader::loadFromMemory(
m_window.getRenderer(),
controller_map,
controller_map_len
);
}
GUI::~GUI()
{
ImGui_ImplSDLRenderer3_Shutdown();
ImGui_ImplSDL3_Shutdown();
ImGui::DestroyContext();
}
void GUI::newFrame()
{
ImGuiIO& io = ImGui::GetIO();
io.IniFilename = nullptr;
// 1. Get window size and renderer info
int w, h;
int bw, bh;
SDL_GetWindowSize(m_window.getWindow(), &w, &h);
SDL_GetRenderOutputSize(m_window.getRenderer(), &bw, &bh);
// 2. Prevent crash: manually set display size and scale if they are 0
io.DisplaySize = ImVec2((float)w, (float)h);
if (w > 0 && h > 0) {
io.DisplayFramebufferScale = ImVec2((float)bw / w, (float)bh / h);
} else {
io.DisplayFramebufferScale = ImVec2(1.0f, 1.0f);
}
// 3. Check for the specific Viewport crash condition
ImGuiViewport* main_viewport = ImGui::GetMainViewport();
if (main_viewport) {
if (main_viewport->FramebufferScale.x <= 0) main_viewport->FramebufferScale.x = 1.0f;
if (main_viewport->FramebufferScale.y <= 0) main_viewport->FramebufferScale.y = 1.0f;
}
ImGui_ImplSDLRenderer3_NewFrame();
ImGui_ImplSDL3_NewFrame();
ImGui::NewFrame();
}
void GUI::updateUI()
{
ImVec2 image_display_size(600, 600 * 0.75f);
int w, h;
SDL_GetWindowSize(m_window.getWindow(), &w, &h);
ImGui::SetNextWindowPos(ImVec2(0, 0));
ImGui::SetNextWindowSize(ImVec2((float)w, (float)h));
ImGuiWindowFlags flags = ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize |
ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoCollapse;
ImGui::Begin("MainUI", nullptr, flags);
ImGui::Text("Gamepad Diagnostic Tool v" GAMEPAD_TESTER_VERSION);
ImGui::Separator();
if (m_gamepad.isConnected()) {
// --- 1. Controller Visualization ---
// --- CLICK TRICK START ---
// if (ImGui::IsItemHovered() && ImGui::IsMouseClicked(ImGuiMouseButton_Left)) {
// ImVec2 mousePos = ImGui::GetMousePos(); // Global screen position of the mouse
// ImVec2 anchor = ImGui::GetItemRectMin(); // Global screen position of the image top-left corner
//
//
// float relativeX = mousePos.x - anchor.x;
// float relativeY = mousePos.y - anchor.y;
//
//
// std::cout << "Click at: X=" << std::fixed << std::setprecision(1)
// << relativeX << ", Y=" << relativeY << std::endl;
// }
// --- CLICK TRICK END ---
// --- 1. Display connected device info at the top ---
ImGui::Text("Connected Device: %s", m_gamepad.getGamepadName());
ImGui::Separator();
// --- 2. Main Layout Table ---
if (ImGui::BeginTable("MainLayout", 2, ImGuiTableFlags_NoSavedSettings))
{
// Column 0: Controller Outline & Triggers
ImGui::TableSetupColumn("VisualizerColumn", ImGuiTableColumnFlags_WidthFixed, 600.0f);
// Column 1: Statistics and Stick Squares
ImGui::TableSetupColumn("DataColumn", ImGuiTableColumnFlags_WidthStretch);
ImGui::TableNextRow();
ImGui::TableNextColumn();
// ImVec2 anchor = ImGui::GetCursorScreenPos();
// Get the bounding box of the image to use as a coordinate anchor
ImVec2 anchor = ImGui::GetItemRectMin();
ImDrawList* draw_list = ImGui::GetWindowDrawList();
// Function to draw a rectangle for D-pad directions
auto drawDPad = [&](SDL_GamepadButton btn, float xOffset, float yOffset, float w, float h) {
if (m_gamepad.isButtonPressed(btn)) {
// TODO: properly update the points. This is a quick fix.
ImVec2 posTopLeft = ImVec2(anchor.x + xOffset, anchor.y + yOffset - 50);
ImVec2 posBottomRight = ImVec2(posTopLeft.x + w, posTopLeft.y + h);
draw_list->AddRectFilled(posTopLeft, posBottomRight, IM_COL32(255, 0, 0, 200));
}
};
drawDPad(SDL_GAMEPAD_BUTTON_DPAD_UP, 217.0f, 264.0f, 24.0f, 28.0f);
drawDPad(SDL_GAMEPAD_BUTTON_DPAD_DOWN, 217.0f, 321.0f, 24.0f, 28.0f);
drawDPad(SDL_GAMEPAD_BUTTON_DPAD_LEFT, 178.0f, 294.0f, 28.0f, 24.0f);
drawDPad(SDL_GAMEPAD_BUTTON_DPAD_RIGHT, 247.0f, 294.0f, 28.0f, 24.0f);
// Helper function to draw custom shaped buttons (LB/RB)
auto drawShapedBtn = [&](SDL_GamepadButton btn, const std::vector<ImVec2>& relativePoints) {
if (m_gamepad.isButtonPressed(btn)) {
ImDrawList* draw_list = ImGui::GetWindowDrawList();
// Convert relative points to absolute screen positions
std::vector<ImVec2> absPoints;
// TODO: properly update the points. This is a quick fix
for (const auto& p : relativePoints) {
absPoints.push_back(ImVec2(anchor.x + p.x, anchor.y + p.y - 50));
}
// Draw the filled shape
draw_list->AddConvexPolyFilled(absPoints.data(), (int)absPoints.size(), IM_COL32(255, 0, 0, 200));
}
};
// LB Points (Tracing the left shoulder button)
std::vector<ImVec2> lbPoints = {
{99, 124}, {162, 103}, {173, 109}, {96, 131}
};
drawShapedBtn(SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, lbPoints);
// RB Points (Tracing the right shoulder button)
std::vector<ImVec2> rbPoints = {
{503, 124}, {438, 103}, {426, 109}, {503, 131}
};
drawShapedBtn(SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER, rbPoints);
ImGui::Spacing(); // Add some vertical space after the image
// --- DRAW CONTROLLER OUTLINE HERE ---
if (m_texture) {
ImGui::Image(
(ImTextureID)m_texture,
image_display_size,
ImVec2(0, 0.1f), // UV0 (Top Left)
ImVec2(1, 0.85f), // UV1 (Bottom Right)
ImVec4(1, 1, 1, 1), // Tint Color (White)
ImVec4(0, 0, 0, 0) // Border Color (Transparent/None)
);
std::vector<ImVec2> lbPoints = {{99, 124}, {162, 103}, {173, 109}, {96, 131}};
drawShapedBtn(SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, lbPoints);
std::vector<ImVec2> rbPoints = {{503, 124}, {438, 103}, {426, 109}, {503, 131}};
drawShapedBtn(SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER, rbPoints);
for (const auto& element : CONTROLLER_MAP) {
if (m_gamepad.isButtonPressed(element.button)) {
draw_list->AddCircleFilled(
ImVec2(anchor.x + element.x, anchor.y + element.y),
element.radius,
IM_COL32(255, 0, 0, 200) // Red with some transparency
);
}
}
// if (ImGui::IsItemClicked()) {
// ImVec2 mousePos = ImGui::GetMousePos();
// // Calculate relative coordinates from the top-left of the image
// float relativeX = mousePos.x - anchor.x;
// float relativeY = mousePos.y - anchor.y;
//
// // Output to terminal/console
// printf("Clicked at: { %f, %f }\n", relativeX, relativeY);
// }
}
// (Ensure 'anchor' is defined relative to the current cursor position)
// [Insert your AddImage or AddLine calls for the controller outline here]
ImGui::Spacing();
// --- 3. Stick Visualizers (Side-by-Side) ---
auto drawStickVisualizer = [&](const char* label, SDL_GamepadAxis axisX, SDL_GamepadAxis axisY) {
ImGui::BeginGroup();
ImDrawList* draw_list = ImGui::GetWindowDrawList();
ImVec2 size(160, 160);
ImVec2 pos = ImGui::GetCursorScreenPos();
ImVec2 center = ImVec2(pos.x + size.x / 2.0f, pos.y + size.y / 2.0f);
draw_list->AddRect(pos, ImVec2(pos.x + size.x, pos.y + size.y), IM_COL32(150, 150, 150, 255), 0, 0, 1.5f);
draw_list->AddLine(ImVec2(center.x, pos.y), ImVec2(center.x, pos.y + size.y), IM_COL32(80, 80, 80, 255));
draw_list->AddLine(ImVec2(pos.x, center.y), ImVec2(pos.x + size.x, center.y), IM_COL32(80, 80, 80, 255));
draw_list->AddCircle(center, size.x / 2.0f, IM_COL32(0, 255, 0, 100), 64);
float x = m_gamepad.getAxis(axisX);
float y = m_gamepad.getAxis(axisY);
ImVec2 dotPos = ImVec2(center.x + x * (size.x / 2.0f), center.y + y * (size.y / 2.0f));
draw_list->AddCircleFilled(dotPos, 5.0f, IM_COL32(255, 0, 0, 255));
ImGui::Dummy(size);
ImGui::Text("%s: X:%.2f Y:%.2f", label, x, y);
ImGui::EndGroup();
};
ImGui::BeginGroup();
drawStickVisualizer("Left", SDL_GAMEPAD_AXIS_LEFTX, SDL_GAMEPAD_AXIS_LEFTY);
ImGui::SameLine();
drawStickVisualizer("Right", SDL_GAMEPAD_AXIS_RIGHTX, SDL_GAMEPAD_AXIS_RIGHTY);
ImGui::EndGroup();
ImGui::Dummy(ImVec2(600, 20));
// --- Switch to Right Column for Data ---
ImGui::TableNextColumn();
// --- TRIGGER BARS RENDERER ---
auto drawTrigger = [&](SDL_GamepadAxis axis, float xOffset, const char* label) {
ImDrawList* draw_list = ImGui::GetWindowDrawList();
float val = m_gamepad.getAxis(axis); // Current pressure (0.0 to 1.0)
float barWidth = 20.0f;
float barHeight = 200.0f;
// Position: Right edge of the image + additional X offset
ImVec2 barPosTopLeft = ImVec2(anchor.x + image_display_size.x + xOffset, anchor.y + 50);
ImVec2 barPosBottomRight = ImVec2(barPosTopLeft.x + barWidth, barPosTopLeft.y + barHeight);
draw_list->AddRectFilled(barPosTopLeft, barPosBottomRight, IM_COL32(40, 40, 40, 255));
// Draw Filling (from top to bottom)
// Calculating the bottom Y coordinate of the red area based on axis value
float fillBottomY = barPosTopLeft.y + (barHeight * val);
draw_list->AddRectFilled(barPosTopLeft, ImVec2(barPosBottomRight.x, fillBottomY), IM_COL32(255, 0, 0, 255));
draw_list->AddRect(barPosTopLeft, barPosBottomRight, IM_COL32(200, 200, 200, 255));
draw_list->AddText(ImVec2(barPosTopLeft.x, barPosTopLeft.y - 20), IM_COL32(255, 255, 255, 255), label);
ImGui::Dummy(ImVec2(barWidth, barHeight));
};
// Draw both triggers to the right of the image
ImGui::BeginGroup();
drawTrigger(SDL_GAMEPAD_AXIS_LEFT_TRIGGER, 30.0f, "L2");
drawTrigger(SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, 70.0f, "R2");
ImGui::EndGroup();
// --- 4. Performance Metrics ---
ImGui::SameLine(150.0f);
ImGui::BeginGroup();
ImGui::Dummy(ImVec2(0, 50)); // Align with the top of the trigger bars
ImGui::Text("Performance Metrics");
const GamepadStats& stats = m_gamepad.getStats();
ImVec4 hzColor = (stats.polling_rate_hz >= 500) ? ImVec4(0, 1, 0, 1) : (stats.polling_rate_hz >= 240) ? ImVec4(1, 1, 0, 1) : ImVec4(1, 0, 0, 1);
ImGui::Text("Polling rate:"); ImGui::SameLine();
ImGui::TextColored(hzColor, "%.0f Hz", stats.polling_rate_hz);
ImGui::SameLine();
helpMarker("How many times per second the controller sends data to the PC.\n\n"
"Higher values (e.g., 1000Hz) mean smoother movement and lower input delay. "
"To measure accurately rapidly move your sticks back and forth. "
"\nModern controllers have a power-saving feature that lowers the polling rate according to what's needed to accurately depict the movement.");
ImGui::Text("Latency: "); ImGui::SameLine();
ImGui::TextColored(ImVec4(0, 0.8f, 1, 1), "%.2f ms", stats.avg_latency_ms);
ImGui::SameLine();
helpMarker("The estimated time it takes for an input to be processed by the software.\n\n"
"Lower is better. This value depends on the polling rate and the system's processing speed.");
ImGui::Spacing(); ImGui::Separator(); ImGui::Spacing();
// --- 5. Rumble Testing ---
ImGui::Text("Haptics / Rumble");
float itemWidth = ImGui::GetContentRegionAvail().x - 5.0f;
ImGui::PushItemWidth(itemWidth);
ImGui::SliderFloat("##Weak", &m_weakValue, 0.0f, 1.0f, "Weak: %.2f");
ImGui::SliderFloat("##Strong", &m_strongValue, 0.0f, 1.0f, "Strong: %.2f");
if (ImGui::Button("Test Rumble", ImVec2(itemWidth, 40))) {
m_gamepad.rumble(m_weakValue, m_strongValue, 1000);
}
ImGui::PopItemWidth();
ImGui::EndGroup();
ImGui::EndTable();
}
} else {
ImGui::TextColored(ImVec4(1, 0, 0, 1), "No Gamepad Connected");
}
ImGui::End();
}
void GUI::render()
{
ImGui::Render();
ImGui_ImplSDLRenderer3_RenderDrawData(ImGui::GetDrawData(), m_window.getRenderer());
}