From 564f84093615011ca525f395569fba333ec0f136 Mon Sep 17 00:00:00 2001 From: OugonNoHane Date: Thu, 27 Oct 2022 17:01:21 +0200 Subject: [PATCH] Lectura directorio escindida main --- main.cpp | 109 +++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 74 insertions(+), 35 deletions(-) diff --git a/main.cpp b/main.cpp index 8ed8b64..520d1e1 100644 --- a/main.cpp +++ b/main.cpp @@ -7,7 +7,10 @@ #define GL_SILENCE_DEPRECATION #include "imgui/misc/single_file/imgui_single_file.h" #include +#include #include +#include +#include // [Win32] Our example includes a copy of glfw3.lib pre-compiled with VS2010 to maximize ease of testing and compatibility with old VS compilers. // To link with VS2010-era libraries, VS2015+ requires linking with legacy_stdio_definitions.lib, which we do using this pragma. @@ -20,6 +23,52 @@ static void glfw_error_callback(int error, const char* description) fprintf(stderr, "Glfw Error %d: %s\n", error, description); } +bool retrieveCurrentDirectory(char** currentPath){ + if(GetCurrentDirectory(MAX_PATH, *currentPath)) return true; + return false; +} + +int listDirectory(std::string path, std::vector *directoryContents){ + HANDLE hFind = INVALID_HANDLE_VALUE; + WIN32_FIND_DATA ffd; + LARGE_INTEGER filesize; + int numFiles = 0; + + if (path.length() > (MAX_PATH - 3)) return -1; + path = path + "\\*"; + hFind = FindFirstFile(path.c_str(), &ffd); + if (INVALID_HANDLE_VALUE == hFind) return -2; + do { + char* itemPath; + if (directoryContents->size() <= numFiles){ + itemPath = (char*)calloc(1, MAX_PATH * sizeof(WCHAR)); + directoryContents->push_back(itemPath); + } else { + itemPath = directoryContents->at(numFiles); + } + + uint64_t idx = 0; + do { + itemPath[idx] = ffd.cFileName[idx]; + idx++; + } while (ffd.cFileName[idx]); + + if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { + strcat(itemPath, " "); + } else { + filesize.QuadPart = ((ffd.nFileSizeHigh * (MAXDWORD+1)) + ffd.nFileSizeLow); + char buf[16]; buf[0] = ' '; + itoa(filesize.QuadPart, buf + 1 , 10 ); + strcat(itemPath, buf); + } + + numFiles++; + } while (FindNextFile(hFind, &ffd) != 0); + FindClose(hFind); + return numFiles; +} + + int main(int, char**) @@ -94,7 +143,7 @@ int main(int, char**) bool show_demo_window = true; bool show_another_window = false; ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); - + // Main loop while (!glfwWindowShouldClose(window)) { // Poll and handle events (inputs, window resize, etc.) @@ -139,52 +188,42 @@ int main(int, char**) { ImGui::Begin("File Picker in 4K"); - //TODO Win32 - HANDLE hFind = INVALID_HANDLE_VALUE; - WIN32_FIND_DATA ffd; - LARGE_INTEGER filesize; - static char* debug32; - // + //cursed C momento + static char currentPath[MAX_PATH]; + char* ptr = ¤tPath[0]; + static int currentItemIdx = 0; - ImGui::Text("Select a file:"); - hFind = FindFirstFile("C:\\Users\\Fabio\\Downloads\\Street Fighter 6 - Closed Beta\\*", &ffd); - if (INVALID_HANDLE_VALUE != hFind) { - if (ImGui::BeginListBox("fpLb", ImVec2(FLT_MIN, 25 * ImGui::GetTextLineHeightWithSpacing()))){ - for (int n = 0; FindNextFile(hFind, &ffd) != 0; n++) { - static std::string seleciable; - if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { - seleciable = " " + std::string(ffd.cFileName) + ""; - ImGui::Selectable(seleciable.c_str(), false); - } else { - filesize.QuadPart = (long long)(ffd.nFileSizeHigh * (MAXDWORD+1)) + ffd.nFileSizeLow; - seleciable = " " + std::string(ffd.cFileName) + " " + std::to_string(filesize.QuadPart); - ImGui::Selectable(seleciable.c_str(), false); - } - - //const bool isSelected = (currentItemIdx == n); - //if (ImGui::Selectable(items[n], is_selected)) - // item_current_idx = n; + std::vector directoryContents; + + if(!retrieveCurrentDirectory(&ptr)) goto filepickerFailure; + + if(int numFiles = (listDirectory(".", &directoryContents))){ + ImGui::Text("Select a file:"); + if (ImGui::BeginListBox("fpLb", ImVec2(-FLT_MIN, 25 * ImGui::GetTextLineHeightWithSpacing()))){ + for (int i = 0; i < numFiles; i++) { + // ImGui::Selectable(directoryContents.at(i), false); + + const bool isSelected = (currentItemIdx == i); + if (ImGui::Selectable(directoryContents.at(i), isSelected)) + currentItemIdx = i; - // Set the initial focus when opening the combo (scrolling + keyboard navigation focus) - //if (is_selected) - // ImGui::SetItemDefaultFocus(); - - // Set the initial focus when opening the combo (scrolling + keyboard navigation focus) - //if (is_selected) ImGui::SetItemDefaultFocus(); + //Set the initial focus when opening the combo (scrolling + keyboard navigation focus) + if (isSelected) + ImGui::SetItemDefaultFocus(); + } - FindClose(hFind); - ImGui::EndListBox(); } } else { ImGui::Text("cagaste"); } //ImGui::TreePop(); - + filepickerFailure: ImGui::End(); } - + + // 3. Show another simple window. if (show_another_window) {