Mercurial
comparison previous.c @ 111:48f260576059
[PostDog] Rewriting it from scratch as it is unreadable for me.
| author | June Park <parkjune1995@gmail.com> |
|---|---|
| date | Sun, 04 Jan 2026 06:39:16 -0800 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 110:99c4530e4629 | 111:48f260576059 |
|---|---|
| 1 char urlInput[1024] = "https://httpbin.org/get"; | |
| 2 bool urlEditMode = false; | |
| 3 | |
| 4 char jsonInput[JSON_INPUT_BUFFER_LEN] = "{\"key\":\"value\"}"; | |
| 5 bool jsonEditMode = false; | |
| 6 | |
| 7 char headersInput[HEADER_INPUT_BUFFER_LEN] = "Content-Type: application/json"; | |
| 8 bool headersEditMode = false; | |
| 9 | |
| 10 char responseText[16384] = "Response will appear here...\n\nTry the default URL or enter your own!"; | |
| 11 | |
| 12 char paramsInput[PARAM_INPUT_BUFFER_LEN] = "key1=value1\nkey2=value2"; | |
| 13 bool paramsEditMode = false; | |
| 14 | |
| 15 ActiveTab activeTab = ActiveTab_JSON; // 0 = JSON, 1 = Headers, 2 = Params | |
| 16 | |
| 17 // HTTP method selection | |
| 18 int methodActive = 0; | |
| 19 bool methodDropdown = false; | |
| 20 const char *methods[] = { "GET", "POST", "PUT", "DELETE" }; | |
| 21 | |
| 22 // Scroll support | |
| 23 Vector2 jsonScroll = { 0, 0 }; | |
| 24 Vector2 headersScroll = { 0, 0 }; | |
| 25 Vector2 paramsScroll = { 0, 0 }; | |
| 26 Vector2 responseScroll = { 0, 0 }; | |
| 27 Vector2 historyScroll = { 0, 0 }; | |
| 28 | |
| 29 // History | |
| 30 HistoryItem historyItems[MAX_HISTORY_ITEMS]; | |
| 31 int historyCount = 0; | |
| 32 int selectedHistoryIndex = -1; | |
| 33 | |
| 34 // Load initial history | |
| 35 historyCount = PostDog_HistoryDistory_ItemsLoad(historyItems, MAX_HISTORY_ITEMS); | |
| 36 | |
| 37 while (!WindowShouldClose()) | |
| 38 { | |
| 39 // Get current window dimensions for responsive layout | |
| 40 int screenWidth = GetScreenWidth(); | |
| 41 int screenHeight = GetScreenHeight(); | |
| 42 | |
| 43 // Layout calculations | |
| 44 Rectangle sidebar = { 0, 10, SIDEBAR_WIDTH, screenHeight }; | |
| 45 | |
| 46 float mainX = SIDEBAR_WIDTH + GENERIC_PADDING; | |
| 47 float mainWidth = screenWidth - SIDEBAR_WIDTH - GENERIC_PADDING * 2; | |
| 48 | |
| 49 // URL input box - leave space for SEND button on the right | |
| 50 Rectangle urlBox = { | |
| 51 mainX, | |
| 52 GENERIC_PADDING, | |
| 53 mainWidth - SEND_BUTTON_WIDTH - GENERIC_PADDING, | |
| 54 URL_INPUT_HEIGHT | |
| 55 }; | |
| 56 | |
| 57 // SEND button positioned beside URL input | |
| 58 Rectangle sendButton = { | |
| 59 urlBox.x + urlBox.width + GENERIC_PADDING, | |
| 60 GENERIC_PADDING, | |
| 61 SEND_BUTTON_WIDTH, | |
| 62 SEND_BUTTON_HEIGHT | |
| 63 }; | |
| 64 | |
| 65 // Method dropdown below URL | |
| 66 Rectangle methodButton = { | |
| 67 mainX, | |
| 68 urlBox.y + urlBox.height + GENERIC_PADDING, | |
| 69 METHOD_BUTTON_WIDTH, | |
| 70 METHOD_BUTTON_HEIGHT | |
| 71 }; | |
| 72 | |
| 73 float tabHeight = 30; | |
| 74 float contentY = methodButton.y + methodButton.height + GENERIC_PADDING + tabHeight; | |
| 75 float contentHeight = screenHeight - contentY - GENERIC_PADDING; | |
| 76 | |
| 77 float panelWidth = (mainWidth - GENERIC_PADDING) / 2; | |
| 78 | |
| 79 Rectangle tabBar = { | |
| 80 mainX, | |
| 81 methodButton.y + methodButton.height + GENERIC_PADDING, | |
| 82 panelWidth, | |
| 83 tabHeight | |
| 84 }; | |
| 85 | |
| 86 Rectangle requestPanel = { | |
| 87 mainX, | |
| 88 contentY, | |
| 89 panelWidth, | |
| 90 contentHeight | |
| 91 }; | |
| 92 | |
| 93 Rectangle responsePanel = { | |
| 94 mainX + panelWidth + GENERIC_PADDING, | |
| 95 contentY, | |
| 96 panelWidth, | |
| 97 contentHeight | |
| 98 }; | |
| 99 | |
| 100 BeginDrawing(); | |
| 101 ClearBackground(GetColor(GuiGetStyle(DEFAULT, BACKGROUND_COLOR))); | |
| 102 | |
| 103 // --- Sidebar Component--- | |
| 104 DrawRectangleRec(sidebar, Fade(GRAY, 0.1f)); | |
| 105 GuiGroupBox(sidebar, "HISTORY"); | |
| 106 | |
| 107 Rectangle refreshBtn = { | |
| 108 sidebar.x + SIDEBAR_PADDING_GENERAL, | |
| 109 sidebar.y + SIDEBAR_PADDING_GENERAL, | |
| 110 SIDEBAR_REFERSH_BUTTON_WIDTH, | |
| 111 SIDEBAR_REFERSH_BUTTON_HEIGHT | |
| 112 }; | |
| 113 if (GuiButton(refreshBtn, "Refresh")) | |
| 114 { | |
| 115 historyCount = PostDog_HistoryDistory_ItemsLoad(historyItems, MAX_HISTORY_ITEMS); | |
| 116 } | |
| 117 | |
| 118 Rectangle historyArea = { | |
| 119 sidebar.x + SIDEBAR_PADDING_GENERAL, | |
| 120 sidebar.y + SIDEBAR_AREA_PADDING_Y, | |
| 121 sidebar.width - SIDEBAR_AREA_PADDING_X, | |
| 122 sidebar.height - SIDEBAR_AREA_PADDING_Y | |
| 123 }; | |
| 124 if (CheckCollisionPointRec(GetMousePosition(), historyArea)) | |
| 125 { | |
| 126 float wheel = GetMouseWheelMove(); | |
| 127 historyScroll.y += wheel * 20; | |
| 128 if (historyScroll.y < 0) historyScroll.y = 0; | |
| 129 } | |
| 130 | |
| 131 BeginScissorMode(historyArea.x, historyArea.y, historyArea.width, historyArea.height); | |
| 132 | |
| 133 if (historyCount == 0) | |
| 134 { | |
| 135 DrawText("No requests yet", historyArea.x + 5, historyArea.y + 25, 10, DARKGRAY); | |
| 136 } | |
| 137 else | |
| 138 { | |
| 139 int item_y_position = historyArea.y + SIDEBAR_AREA_PADDING_Y + 5 - (int)historyScroll.y; | |
| 140 for ( | |
| 141 int current_history_item_number = 0; | |
| 142 current_history_item_number < historyCount; | |
| 143 current_history_item_number++ | |
| 144 ) | |
| 145 { | |
| 146 if (item_y_position > historyArea.y - SIDEBAR_HISTORY_ITEM_HEIGHT && item_y_position < historyArea.y + historyArea.height) | |
| 147 { | |
| 148 Rectangle itemRect = { historyArea.x, item_y_position, historyArea.width, SIDEBAR_HISTORY_ITEM_HEIGHT - 2 }; | |
| 149 | |
| 150 // Draw button for history item | |
| 151 Color bgColor = (selectedHistoryIndex == current_history_item_number) ? Fade(BLUE, 0.3f) : Fade(LIGHTGRAY, 0.5f); | |
| 152 if (CheckCollisionPointRec(GetMousePosition(), itemRect) && IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) | |
| 153 { | |
| 154 // TODO: This is cringe as fuck probably should just have a strucut that we assign and then zero out lol | |
| 155 char tempUrl[1024], tempMethod[16], tempHeaders[HEADER_INPUT_BUFFER_LEN], tempBody[JSON_INPUT_BUFFER_LEN]; | |
| 156 | |
| 157 if (PostDog_HistoryDirectory_LoadRequest( | |
| 158 historyItems[current_history_item_number].filename, tempUrl, tempMethod, tempHeaders, tempBody) == 0) | |
| 159 { | |
| 160 strncpy(urlInput, tempUrl, sizeof(urlInput) - 1); | |
| 161 strncpy(headersInput, tempHeaders, sizeof(headersInput) - 1); | |
| 162 strncpy(jsonInput, tempBody, sizeof(jsonInput) - 1); | |
| 163 | |
| 164 // Set method | |
| 165 for (int m = 0; m < 4; m++) | |
| 166 { | |
| 167 if (strcmp(methods[m], tempMethod) == 0) | |
| 168 { | |
| 169 methodActive = m; | |
| 170 break; | |
| 171 } | |
| 172 } | |
| 173 | |
| 174 selectedHistoryIndex = current_history_item_number; | |
| 175 strcpy(responseText, "Request loaded from history. Click SEND to execute."); | |
| 176 } | |
| 177 } | |
| 178 | |
| 179 DrawRectangleRec(itemRect, bgColor); | |
| 180 DrawRectangleLinesEx(itemRect, 1, GRAY); | |
| 181 | |
| 182 // Draw method badge | |
| 183 DrawText(historyItems[current_history_item_number].method, itemRect.x + 5, item_y_position + 5, 10, BLACK); | |
| 184 | |
| 185 // Draw timestamp (date only) | |
| 186 char dateStr[16] = ""; | |
| 187 if (strlen(historyItems[current_history_item_number].filename) >= 8) | |
| 188 { | |
| 189 snprintf(dateStr, sizeof(dateStr), "%.4s-%.2s-%.2s", | |
| 190 historyItems[current_history_item_number].filename, historyItems[current_history_item_number].filename + 4, historyItems[current_history_item_number].filename + 6); | |
| 191 } | |
| 192 DrawText(dateStr, itemRect.x + 5, item_y_position + 18, 8, DARKGRAY); | |
| 193 | |
| 194 // Draw time | |
| 195 char timeStr[16] = ""; | |
| 196 if (strlen(historyItems[current_history_item_number].filename) >= 15) { | |
| 197 snprintf(timeStr, sizeof(timeStr), "%.2s:%.2s:%.2s", | |
| 198 historyItems[current_history_item_number].filename + 9, historyItems[current_history_item_number].filename + 11, historyItems[current_history_item_number].filename + 13); | |
| 199 } | |
| 200 DrawText(timeStr, itemRect.x + 5, item_y_position + 28, 8, DARKGRAY); | |
| 201 } | |
| 202 | |
| 203 item_y_position += SIDEBAR_HISTORY_ITEM_HEIGHT; | |
| 204 } | |
| 205 } | |
| 206 | |
| 207 EndScissorMode(); | |
| 208 | |
| 209 // --- URL Input Component --- | |
| 210 GuiLabel((Rectangle){ mainX, GENERIC_PADDING - 15, 100, 20 }, "URL:"); | |
| 211 if (GuiTextBox(urlBox, urlInput, 1024, urlEditMode)) | |
| 212 { | |
| 213 urlEditMode = !urlEditMode; | |
| 214 } | |
| 215 if (urlEditMode && (IsKeyDown(KEY_LEFT_SUPER) || IsKeyDown(KEY_LEFT_CONTROL)) && IsKeyPressed(KEY_C)) | |
| 216 { | |
| 217 SetClipboardText(urlInput); | |
| 218 } | |
| 219 | |
| 220 // Send button (beside URL) | |
| 221 if (GuiButton(sendButton, "SEND") || (urlEditMode && IsKeyDown(KEY_ENTER))) | |
| 222 { | |
| 223 strcpy(responseText, "Sending request...\n"); | |
| 224 | |
| 225 // Make the actual HTTP request | |
| 226 char tempResponse[16384]; | |
| 227 const char *selectedMethod = methods[methodActive]; | |
| 228 | |
| 229 // Use JSON body for POST/PUT, otherwise use empty body | |
| 230 const char *requestBody = (methodActive == 1 || methodActive == 2) ? jsonInput : NULL; | |
| 231 const char *requestHeaders = headersInput; | |
| 232 | |
| 233 // Save request to history | |
| 234 Postdog_SaveRequestToHistory(urlInput, selectedMethod, requestHeaders, requestBody); | |
| 235 | |
| 236 // Reload history to show the new request | |
| 237 historyCount = PostDog_HistoryDistory_ItemsLoad(historyItems, MAX_HISTORY_ITEMS); | |
| 238 | |
| 239 // Making the requests. | |
| 240 PostDog_Make_HttpRequest(urlInput, selectedMethod, requestHeaders, | |
| 241 requestBody, tempResponse, sizeof(tempResponse)); | |
| 242 strncpy(responseText, tempResponse, sizeof(responseText) - 1); | |
| 243 responseText[sizeof(responseText) - 1] = '\0'; | |
| 244 } | |
| 245 | |
| 246 // Tab toggle (3 tabs now) | |
| 247 float tabWidth = tabBar.width / 3; | |
| 248 Rectangle jsonTab = { tabBar.x, tabBar.y - 10, tabWidth, tabBar.height }; | |
| 249 Rectangle headersTab = { tabBar.x + tabWidth, tabBar.y - 10, tabWidth, tabBar.height }; | |
| 250 Rectangle paramsTab = { tabBar.x + tabWidth * 2, tabBar.y - 10, tabWidth, tabBar.height }; | |
| 251 | |
| 252 if (GuiButton(jsonTab, activeTab == ActiveTab_JSON ? "#191#Body" : "Body")) | |
| 253 { | |
| 254 activeTab = ActiveTab_JSON; | |
| 255 } | |
| 256 | |
| 257 if (GuiButton(headersTab, activeTab == ActiveTab_Headers ? "#191#Headers" : "Headers")) | |
| 258 { | |
| 259 activeTab = ActiveTab_Headers; | |
| 260 } | |
| 261 | |
| 262 if (GuiButton(paramsTab, activeTab == ActiveTab_Params ? "#191#Params" : "Params")) | |
| 263 { | |
| 264 activeTab = ActiveTab_Params; | |
| 265 } | |
| 266 | |
| 267 const char *panelTitle; | |
| 268 switch(activeTab) { | |
| 269 case ActiveTab_JSON: | |
| 270 { | |
| 271 panelTitle = "Request Body (JSON)"; | |
| 272 break; | |
| 273 } | |
| 274 case ActiveTab_Headers: | |
| 275 { | |
| 276 panelTitle = "Request Headers"; | |
| 277 break; | |
| 278 } | |
| 279 case ActiveTab_Params: | |
| 280 { | |
| 281 panelTitle = "Query Parameters"; | |
| 282 break; | |
| 283 } | |
| 284 } | |
| 285 | |
| 286 // Panel title | |
| 287 GuiGroupBox(requestPanel, panelTitle); | |
| 288 Rectangle textArea = { | |
| 289 requestPanel.x + 10, | |
| 290 requestPanel.y + 30, | |
| 291 requestPanel.width - 20, | |
| 292 requestPanel.height - 40 | |
| 293 }; | |
| 294 | |
| 295 // Handle click outside to disable edit mode | |
| 296 if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) | |
| 297 { | |
| 298 if (!CheckCollisionPointRec(GetMousePosition(), textArea)) | |
| 299 { | |
| 300 jsonEditMode = false; | |
| 301 headersEditMode = false; | |
| 302 paramsEditMode = false; | |
| 303 } | |
| 304 } | |
| 305 | |
| 306 // Draw border for text area | |
| 307 DrawRectangleLinesEx(textArea, 1, GRAY); | |
| 308 | |
| 309 // Manual scroll handling with mouse wheel | |
| 310 if (CheckCollisionPointRec(GetMousePosition(), textArea)) | |
| 311 { | |
| 312 float wheel = GetMouseWheelMove(); | |
| 313 switch(activeTab) | |
| 314 { | |
| 315 case ActiveTab_JSON: | |
| 316 { | |
| 317 jsonScroll.y += wheel * 20; | |
| 318 if (jsonScroll.y < 0) jsonScroll.y = 0; | |
| 319 } | |
| 320 case ActiveTab_Headers: | |
| 321 { | |
| 322 headersScroll.y += wheel * 20; | |
| 323 if (headersScroll.y < 0) headersScroll.y = 0; | |
| 324 } | |
| 325 case ActiveTab_Params: | |
| 326 { | |
| 327 paramsScroll.y += wheel * 20; | |
| 328 if (paramsScroll.y < 0) paramsScroll.y = 0; | |
| 329 } | |
| 330 } | |
| 331 } | |
| 332 | |
| 333 char *copyFromInput; | |
| 334 bool *currentMode; | |
| 335 switch(activeTab) | |
| 336 { | |
| 337 case ActiveTab_JSON: | |
| 338 { | |
| 339 PostDog_Render_TextWithScroll(textArea, jsonScroll, jsonInput); | |
| 340 copyFromInput = jsonInput; | |
| 341 currentMode = &jsonEditMode; | |
| 342 break; | |
| 343 } | |
| 344 case ActiveTab_Headers: | |
| 345 { | |
| 346 PostDog_Render_TextWithScroll(textArea, headersScroll, headersInput); | |
| 347 copyFromInput = headersInput; | |
| 348 currentMode = &headersEditMode; | |
| 349 break; | |
| 350 } | |
| 351 case ActiveTab_Params: | |
| 352 { | |
| 353 PostDog_Render_TextWithScroll(textArea, paramsScroll, paramsInput); | |
| 354 copyFromInput = paramsInput; | |
| 355 currentMode = ¶msEditMode; | |
| 356 | |
| 357 Rectangle updateUrlBtn = { textArea.x + 30, textArea.y + textArea.height - 10, 120, 20 }; | |
| 358 // TODO: Automatic update | |
| 359 if (GuiButton(updateUrlBtn, "Update URL")) | |
| 360 { | |
| 361 char tempUrl[1024]; | |
| 362 strncpy(tempUrl, urlInput, sizeof(tempUrl) - 1); | |
| 363 | |
| 364 // Remove existing params if any | |
| 365 char *questionMark = strchr(tempUrl, '?'); | |
| 366 if (questionMark) *questionMark = '\0'; | |
| 367 | |
| 368 Postdog_UpdateUrlWithParams(urlInput, sizeof(urlInput), tempUrl, paramsInput); | |
| 369 } | |
| 370 break; | |
| 371 } | |
| 372 } | |
| 373 | |
| 374 if ((IsKeyDown(KEY_LEFT_SUPER) || IsKeyDown(KEY_LEFT_CONTROL)) && IsKeyPressed(KEY_C)) | |
| 375 { | |
| 376 SetClipboardText(copyFromInput); | |
| 377 } | |
| 378 | |
| 379 Rectangle editBtn = { textArea.x + textArea.width - 60, textArea.y + textArea.height - 10, 50, 20 }; | |
| 380 if (GuiButton(editBtn, "Edit")) | |
| 381 { | |
| 382 *currentMode = !(*currentMode); | |
| 383 } | |
| 384 | |
| 385 // Response Panel with scroll | |
| 386 GuiGroupBox(responsePanel, "Response"); | |
| 387 | |
| 388 Rectangle responseArea = { | |
| 389 responsePanel.x + 10, | |
| 390 responsePanel.y + 30, | |
| 391 responsePanel.width - 20, | |
| 392 responsePanel.height - 40 | |
| 393 }; | |
| 394 | |
| 395 // Manual scroll for response | |
| 396 if (CheckCollisionPointRec(GetMousePosition(), responseArea)) | |
| 397 { | |
| 398 float wheel = GetMouseWheelMove(); | |
| 399 responseScroll.y += wheel * 20; | |
| 400 if (responseScroll.y < 0) responseScroll.y = 0; | |
| 401 } | |
| 402 | |
| 403 // Draw border | |
| 404 DrawRectangleLinesEx(responseArea, 1, GRAY); | |
| 405 | |
| 406 // Draw response text with scroll | |
| 407 PostDog_Render_TextWithScroll(responseArea, responseScroll, responseText); | |
| 408 | |
| 409 if ((IsKeyDown(KEY_LEFT_SUPER) || IsKeyDown(KEY_LEFT_CONTROL)) && IsKeyPressed(KEY_C)) | |
| 410 { | |
| 411 if (CheckCollisionPointRec(GetMousePosition(), responseArea)) { | |
| 412 SetClipboardText(responseText); | |
| 413 } | |
| 414 } | |
| 415 | |
| 416 // --- Edit modal ---- | |
| 417 if (jsonEditMode) | |
| 418 { | |
| 419 GuiTextBox( | |
| 420 (Rectangle){ screenWidth/2 - 300, screenHeight/2 - 200, 600, 400 }, | |
| 421 jsonInput, JSON_INPUT_BUFFER_LEN, true); | |
| 422 } | |
| 423 | |
| 424 if (headersEditMode) | |
| 425 { | |
| 426 GuiTextBox( | |
| 427 (Rectangle){ screenWidth/2 - 300, screenHeight/2 - 200, 600, 400 }, | |
| 428 headersInput, HEADER_INPUT_BUFFER_LEN, true); | |
| 429 } | |
| 430 | |
| 431 if (paramsEditMode) | |
| 432 { | |
| 433 GuiTextBox( | |
| 434 (Rectangle){ screenWidth/2 - 300, screenHeight/2 - 200, 600, 400 }, | |
| 435 paramsInput, PARAM_INPUT_BUFFER_LEN, true); | |
| 436 } | |
| 437 | |
| 438 if (GuiDropdownBox(methodButton, "GET;POST;PUT;DELETE", &methodActive, methodDropdown)) | |
| 439 { | |
| 440 methodDropdown = !methodDropdown; | |
| 441 } | |
| 442 | |
| 443 | |
| 444 EndDrawing(); | |
| 445 } | |
| 446 | |
| 447 |