diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 000000000000..0c09116f2ad1 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,21 @@ +# USE_NATIVE_IMGUI_INCLUDE_PATHS is set to true by default include direct path to imgui headers +# User can set it to false to create a custom way of including imgui to their project +option(USE_NATIVE_IMGUI_INCLUDE_PATHS "Set this to false if you want to define your own target_include_directories function for imgui." true) + +add_library(imgui STATIC) + +target_sources(imgui +PRIVATE + imgui.h + imconfig.h + imgui_demo.cpp + imgui_draw.cpp + imgui_internal.h + imgui_widgets.cpp + imgui.cpp + imgui_tables.cpp +) + +if(USE_NATIVE_IMGUI_INCLUDE_PATHS) + target_include_directories(imgui PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) +endif() diff --git a/imconfig.h b/imconfig.h index 769e73915582..4c7357d1788c 100644 --- a/imconfig.h +++ b/imconfig.h @@ -43,7 +43,7 @@ //#define IMGUI_DISABLE_DEFAULT_FORMAT_FUNCTIONS // Don't implement ImFormatString/ImFormatStringV so you can implement them yourself (e.g. if you don't want to link with vsnprintf) //#define IMGUI_DISABLE_DEFAULT_MATH_FUNCTIONS // Don't implement ImFabs/ImSqrt/ImPow/ImFmod/ImCos/ImSin/ImAcos/ImAtan2 so you can implement them yourself. //#define IMGUI_DISABLE_FILE_FUNCTIONS // Don't implement ImFileOpen/ImFileClose/ImFileRead/ImFileWrite and ImFileHandle at all (replace them with dummies) -//#define IMGUI_DISABLE_DEFAULT_FILE_FUNCTIONS // Don't implement ImFileOpen/ImFileClose/ImFileRead/ImFileWrite and ImFileHandle so you can implement them yourself if you don't want to link with fopen/fclose/fread/fwrite. This will also disable the LogToTTY() function. +#define IMGUI_DISABLE_DEFAULT_FILE_FUNCTIONS // Don't implement ImFileOpen/ImFileClose/ImFileRead/ImFileWrite so you can implement them yourself if you don't want to link with fopen/fclose/fread/fwrite. This will also disable the LogToTTY() function. //#define IMGUI_DISABLE_DEFAULT_ALLOCATORS // Don't implement default allocators calling malloc()/free() to avoid linking with them. You will need to call ImGui::SetAllocatorFunctions(). //#define IMGUI_DISABLE_SSE // Disable use of SSE intrinsics even if available diff --git a/imgui.cpp b/imgui.cpp index daf21721833a..2e1b7810cac5 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -1565,7 +1565,49 @@ bool ImFileClose(ImFileHandle f) { return fclose(f) == 0; } ImU64 ImFileGetSize(ImFileHandle f) { long off = 0, sz = 0; return ((off = ftell(f)) != -1 && !fseek(f, 0, SEEK_END) && (sz = ftell(f)) != -1 && !fseek(f, off, SEEK_SET)) ? (ImU64)sz : (ImU64)-1; } ImU64 ImFileRead(void* data, ImU64 sz, ImU64 count, ImFileHandle f) { return fread(data, (size_t)sz, (size_t)count, f); } ImU64 ImFileWrite(const void* data, ImU64 sz, ImU64 count, ImFileHandle f) { return fwrite(data, (size_t)sz, (size_t)count, f); } -#endif // #ifndef IMGUI_DISABLE_DEFAULT_FILE_FUNCTIONS +#else +//Functions to redirect to our hook object function calls if it exists +ImFileHandle ImFileOpen(const char* filename, const char* mode) { + if (auto context = ImGui::GetCurrentContext()) { + if (ImIFileHelper* fileHelperPtr = context->FileHelper) { + return fileHelperPtr->ImFileOpen(filename, mode); + } + } + return nullptr; +} +bool ImFileClose(ImFileHandle file) { + if (auto context = ImGui::GetCurrentContext()) { + if (ImIFileHelper* fileHelperPtr = context->FileHelper) { + return fileHelperPtr->ImFileClose(file); + } + } + return false; +} +ImU64 ImFileGetSize(ImFileHandle file) { + if (auto context = ImGui::GetCurrentContext()) { + if (ImIFileHelper* fileHelperPtr = context->FileHelper) { + return fileHelperPtr->ImFileGetSize(file); + } + } + return 0; +} +ImU64 ImFileRead(void* data, ImU64 size, ImU64 count, ImFileHandle file) { + if (auto context = ImGui::GetCurrentContext()) { + if (ImIFileHelper* fileHelperPtr = context->FileHelper) { + return fileHelperPtr->ImFileRead(data, size, count, file); + } + } + return 0; +} +ImU64 ImFileWrite(const void* data, ImU64 size, ImU64 count, ImFileHandle file) { + if (auto context = ImGui::GetCurrentContext()) { + if (ImIFileHelper* fileHelperPtr = context->FileHelper) { + return fileHelperPtr->ImFileWrite(data, size, count, file); + } + } + return 0; +} +#endif // Helper: Load file content into memory // Memory allocated with IM_ALLOC(), must be freed by user using IM_FREE() == ImGui::MemFree() diff --git a/imgui_internal.h b/imgui_internal.h index 7fe1ab3f5712..828f9e31928f 100644 --- a/imgui_internal.h +++ b/imgui_internal.h @@ -372,8 +372,25 @@ IMGUI_API bool ImFileClose(ImFileHandle file); IMGUI_API ImU64 ImFileGetSize(ImFileHandle file); IMGUI_API ImU64 ImFileRead(void* data, ImU64 size, ImU64 count, ImFileHandle file); IMGUI_API ImU64 ImFileWrite(const void* data, ImU64 size, ImU64 count, ImFileHandle file); -#else +#else //Bedrock File Handler Hook so we can use our own internal file handling #define IMGUI_DISABLE_TTY_FUNCTIONS // Can't use stdout, fflush if we are not using default file functions +//We'll cast this to our own internal type as needed +typedef void* ImFileHandle; +//Helper struct for hooking file operations to external systems +struct ImIFileHelper { + virtual ~ImIFileHelper() = default; + virtual ImFileHandle ImFileOpen(const char* filename, const char* mode) = 0; + virtual bool ImFileClose(ImFileHandle file) = 0; + virtual ImU64 ImFileGetSize(ImFileHandle file) = 0; + virtual ImU64 ImFileRead(void* data, ImU64 size, ImU64 count, ImFileHandle file) = 0; + virtual ImU64 ImFileWrite(const void* data, ImU64 size, ImU64 count, ImFileHandle file) = 0; +}; + +IMGUI_API ImFileHandle ImFileOpen(const char* filename, const char* mode); +IMGUI_API bool ImFileClose(ImFileHandle file); +IMGUI_API ImU64 ImFileGetSize(ImFileHandle file); +IMGUI_API ImU64 ImFileRead(void* data, ImU64 size, ImU64 count, ImFileHandle file); +IMGUI_API ImU64 ImFileWrite(const void* data, ImU64 size, ImU64 count, ImFileHandle file); #endif IMGUI_API void* ImFileLoadToMemory(const char* filename, const char* mode, size_t* out_file_size = NULL, int padding_bytes = 0); @@ -1598,6 +1615,9 @@ struct ImGuiContext ImChunkStream SettingsTables; // ImGuiTable .ini settings entries ImVector Hooks; // Hooks for extensions (e.g. test engine) ImGuiID HookIdNext; // Next available HookId +#ifdef IMGUI_DISABLE_DEFAULT_FILE_FUNCTIONS + ImIFileHelper* FileHelper = nullptr; // Hook object for external source to implement their own fileIO +#endif // Capture/Logging bool LogEnabled; // Currently capturing