diff --git a/.github/ISSUE_TEMPLATE/issue_template.yml b/.github/ISSUE_TEMPLATE/issue_template.yml
index 46ed826d5..6ed62493a 100644
--- a/.github/ISSUE_TEMPLATE/issue_template.yml
+++ b/.github/ISSUE_TEMPLATE/issue_template.yml
@@ -4,7 +4,7 @@ body:
- type: markdown
attributes:
value: |
- FOR FIRST-TIME USERS ISSUES COMPILING/LINKING/RUNNING or LOADING FONTS, please use [GitHub Discussions](https://github.com/ocornut/imgui/discussions)
+ FOR FIRST-TIME USERS ISSUES COMPILING/LINKING/RUNNING, please use [GitHub Discussions](https://github.com/ocornut/imgui/discussions)
For anything else: **we are happy to use 'GitHub Issues' for many types of open-ended questions**. We are encouraging 'Issues' becoming a large, centralized, tagged, cross-referenced database of Dear ImGui contents.
Be mindful that messages are being sent to the e-mail box of "Watching" users. Try to proof-read your messages before sending them. Edits are not seen by those users.
diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md
index 638545bd6..796ec0b9e 100644
--- a/.github/pull_request_template.md
+++ b/.github/pull_request_template.md
@@ -2,5 +2,7 @@
1. PLEASE CAREFULLY READ: [Contributing Guidelines](https://github.com/ocornut/imgui/blob/master/docs/CONTRIBUTING.md)
-2. Clear this template before submitting your PR.
+2. Make sure you're using a special branch just for this pull request. (Sometimes people unknowingly use a default branch, then later update that branch, which updates the pull request with the other changes if it hasn't been merged yet.)
+
+3. Clear this template before submitting your PR.
diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index f95ff43c8..92d41dac1 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -17,31 +17,37 @@ on:
jobs:
Windows:
- runs-on: windows-2019
+ runs-on: windows-2025
env:
- VS_PATH: C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\
- MSBUILD_PATH: C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\
+ VS_PATH: C:\Program Files\Microsoft Visual Studio\2022\Enterprise\
+ MSBUILD_PATH: C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\Bin\
steps:
- uses: actions/checkout@v4
+ # The VulkanSDK libs for Windows is manually generated using build_windows_vulkan_libs.ps1 + attached to issue #8925.
+ # (we have a .yml workflow in commit history if it becomes ever useful to create this on CI too)
- name: Install Dependencies
shell: powershell
run: |
- Invoke-WebRequest -Uri "https://www.libsdl.org/release/SDL2-devel-2.26.3-VC.zip" -OutFile "SDL2-devel-2.26.3-VC.zip"
- Expand-Archive -Path SDL2-devel-2.26.3-VC.zip
- echo "SDL2_DIR=$(pwd)\SDL2-devel-2.26.3-VC\SDL2-2.26.3\" >>${env:GITHUB_ENV}
+ Invoke-WebRequest -Uri "https://www.libsdl.org/release/SDL2-devel-2.32.8-VC.zip" -OutFile "SDL2-devel-2.32.8-VC.zip"
+ Expand-Archive -Path SDL2-devel-2.32.8-VC.zip
+ echo "SDL2_DIR=$(pwd)\SDL2-devel-2.32.8-VC\SDL2-2.32.8\" >>${env:GITHUB_ENV}
- Invoke-WebRequest -Uri "https://github.com/ocornut/imgui/files/3789205/vulkan-sdk-1.1.121.2.zip" -OutFile vulkan-sdk-1.1.121.2.zip
- Expand-Archive -Path vulkan-sdk-1.1.121.2.zip
- echo "VULKAN_SDK=$(pwd)\vulkan-sdk-1.1.121.2\" >>${env:GITHUB_ENV}
+ Invoke-WebRequest -Uri "https://www.libsdl.org/release/SDL3-devel-3.2.18-VC.zip" -OutFile "SDL3-devel-3.2.18-VC.zip"
+ Expand-Archive -Path SDL3-devel-3.2.18-VC.zip
+ echo "SDL3_DIR=$(pwd)\SDL3-devel-3.2.18-VC\SDL3-3.2.18\" >>${env:GITHUB_ENV}
+
+ Invoke-WebRequest -Uri "https://github.com/user-attachments/files/22464296/vulkan_windows_libs_1.4.326.zip" -OutFile vulkan_windows_libs_1.4.326.zip
+ Expand-Archive -Path vulkan_windows_libs_1.4.326.zip
+ echo "VULKAN_SDK=$(pwd)\vulkan_windows_libs_1.4.326\" >>${env:GITHUB_ENV}
- name: Fix Projects
shell: powershell
run: |
# CI workers do not supporter older Visual Studio versions. Fix projects to target newer available version.
gci -recurse -filter "*.vcxproj" | ForEach-Object {
- (Get-Content $_.FullName) -Replace "v\d{3}","v142" | Set-Content -Path $_.FullName
- (Get-Content $_.FullName) -Replace "[\d\.]+","10.0.18362.0" | Set-Content -Path $_.FullName
+ (Get-Content $_.FullName) -Replace "v\d{3}","v143" | Set-Content -Path $_.FullName
+ (Get-Content $_.FullName) -Replace "[\d\.]+",'$(LatestTargetPlatformVersion)' | Set-Content -Path $_.FullName
}
# Not using matrix here because it would inflate job count too much. Check out and setup is done for every job and that makes build times way too long.
@@ -51,15 +57,11 @@ jobs:
- name: Build example_null (mingw 64-bit, as DLL)
shell: bash
run: |
- echo '#ifdef _EXPORT' > example_single_file.cpp
- echo '# define IMGUI_API __declspec(dllexport)' >> example_single_file.cpp
- echo '#else' >> example_single_file.cpp
- echo '# define IMGUI_API __declspec(dllimport)' >> example_single_file.cpp
- echo '#endif' >> example_single_file.cpp
+ echo '#define IMGUI_API __declspec(dllexport)' > example_single_file.cpp
echo '#define IMGUI_IMPLEMENTATION' >> example_single_file.cpp
echo '#include "misc/single_file/imgui_single_file.h"' >> example_single_file.cpp
- g++ -I. -Wall -Wformat -D_EXPORT -shared -o libimgui.dll -Wl,--out-implib,libimgui.a example_single_file.cpp -limm32
- g++ -I. -Wall -Wformat -o example_null.exe examples/example_null/main.cpp -L. -limgui
+ g++ -I. -Wall -Wformat -shared -o libimgui.dll -Wl,--out-implib,libimgui.a example_single_file.cpp -limm32
+ g++ -I. -Wall -Wformat -DIMGUI_API='__declspec(dllimport)' -o example_null.exe examples/example_null/main.cpp -L. -limgui
rm -f example_null.exe libimgui.* example_single_file.*
- name: Build example_null (extra warnings, msvc 64-bit)
@@ -99,20 +101,19 @@ jobs:
run: |
call "%VS_PATH%\VC\Auxiliary\Build\vcvars64.bat"
- echo #ifdef _EXPORT > example_single_file.cpp
- echo # define IMGUI_API __declspec(dllexport) >> example_single_file.cpp
- echo #else >> example_single_file.cpp
- echo # define IMGUI_API __declspec(dllimport) >> example_single_file.cpp
- echo #endif >> example_single_file.cpp
+ echo #define IMGUI_API __declspec(dllexport) > example_single_file.cpp
echo #define IMGUI_IMPLEMENTATION >> example_single_file.cpp
echo #include "misc/single_file/imgui_single_file.h" >> example_single_file.cpp
- cl.exe /D_USRDLL /D_WINDLL /D_EXPORT /I. example_single_file.cpp /LD /FeImGui.dll /link
- cl.exe /I. ImGui.lib /Feexample_null.exe examples/example_null/main.cpp
+ cl.exe /D_USRDLL /D_WINDLL /I. example_single_file.cpp /LD /FeImGui.dll /link
+ cl.exe /DIMGUI_API=__declspec(dllimport) /I. ImGui.lib /Feexample_null.exe examples/example_null/main.cpp
+ # Win64 examples are more frequently compilted than the Win32 examples.
+ # More of the Win32 examples requires 'workflow_run' to reduce waste.
- name: Build Win32 example_glfw_opengl2
shell: cmd
run: '"%MSBUILD_PATH%\MSBuild.exe" examples/example_glfw_opengl2/example_glfw_opengl2.vcxproj /p:Platform=Win32 /p:Configuration=Release'
+ if: github.event_name == 'workflow_run'
- name: Build Win32 example_glfw_opengl3
shell: cmd
@@ -148,77 +149,116 @@ jobs:
run: '"%MSBUILD_PATH%\MSBuild.exe" examples/example_sdl2_directx11/example_sdl2_directx11.vcxproj /p:Platform=Win32 /p:Configuration=Release'
if: github.event_name == 'workflow_run'
+ - name: Build Win32 example_sdl3_opengl3
+ shell: cmd
+ run: '"%MSBUILD_PATH%\MSBuild.exe" examples/example_sdl3_opengl3/example_sdl3_opengl3.vcxproj /p:Platform=Win32 /p:Configuration=Release'
+
+ - name: Build Win32 example_sdl3_sdlgpu3
+ shell: cmd
+ run: '"%MSBUILD_PATH%\MSBuild.exe" examples/example_sdl3_sdlgpu3/example_sdl3_sdlgpu3.vcxproj /p:Platform=Win32 /p:Configuration=Release'
+ if: github.event_name == 'workflow_run'
+
+ - name: Build Win32 example_sdl3_sdlrenderer3
+ shell: cmd
+ run: '"%MSBUILD_PATH%\MSBuild.exe" examples/example_sdl3_sdlrenderer3/example_sdl3_sdlrenderer3.vcxproj /p:Platform=Win32 /p:Configuration=Release'
+ if: github.event_name == 'workflow_run'
+
+ - name: Build Win32 example_sdl3_vulkan
+ shell: cmd
+ run: '"%MSBUILD_PATH%\MSBuild.exe" examples/example_sdl3_vulkan/example_sdl3_vulkan.vcxproj /p:Platform=Win32 /p:Configuration=Release'
+ if: github.event_name == 'workflow_run'
+
- name: Build Win32 example_win32_directx9
shell: cmd
run: '"%MSBUILD_PATH%\MSBuild.exe" examples/example_win32_directx9/example_win32_directx9.vcxproj /p:Platform=Win32 /p:Configuration=Release'
+ if: github.event_name == 'workflow_run'
- name: Build Win32 example_win32_directx10
shell: cmd
run: '"%MSBUILD_PATH%\MSBuild.exe" examples/example_win32_directx10/example_win32_directx10.vcxproj /p:Platform=Win32 /p:Configuration=Release'
+ if: github.event_name == 'workflow_run'
- name: Build Win32 example_win32_directx11
shell: cmd
run: '"%MSBUILD_PATH%\MSBuild.exe" examples/example_win32_directx11/example_win32_directx11.vcxproj /p:Platform=Win32 /p:Configuration=Release'
if: github.event_name == 'workflow_run'
- - name: Build x64 example_glfw_opengl2
+ # Windows 64-bits
+ - name: Build Win64 example_glfw_opengl2
shell: cmd
run: '"%MSBUILD_PATH%\MSBuild.exe" examples/example_glfw_opengl2/example_glfw_opengl2.vcxproj /p:Platform=x64 /p:Configuration=Release'
if: github.event_name == 'workflow_run'
- - name: Build x64 example_glfw_opengl3
+ - name: Build Win64 example_glfw_opengl3
shell: cmd
run: '"%MSBUILD_PATH%\MSBuild.exe" examples/example_glfw_opengl3/example_glfw_opengl3.vcxproj /p:Platform=x64 /p:Configuration=Release'
- - name: Build x64 example_glfw_vulkan
+ - name: Build Win64 example_glfw_vulkan
shell: cmd
run: '"%MSBUILD_PATH%\MSBuild.exe" examples/example_glfw_vulkan/example_glfw_vulkan.vcxproj /p:Platform=x64 /p:Configuration=Release'
- - name: Build x64 example_sdl2_sdlrenderer2
+ - name: Build Win64 example_sdl2_sdlrenderer2
shell: cmd
run: '"%MSBUILD_PATH%\MSBuild.exe" examples/example_sdl2_sdlrenderer2/example_sdl2_sdlrenderer2.vcxproj /p:Platform=x64 /p:Configuration=Release'
if: github.event_name == 'workflow_run'
- - name: Build x64 example_sdl2_vulkan
+ - name: Build Win64 example_sdl2_vulkan
shell: cmd
run: '"%MSBUILD_PATH%\MSBuild.exe" examples/example_sdl2_vulkan/example_sdl2_vulkan.vcxproj /p:Platform=x64 /p:Configuration=Release'
if: github.event_name == 'workflow_run'
- - name: Build x64 example_sdl2_opengl2
+ - name: Build Win64 example_sdl2_opengl2
shell: cmd
run: '"%MSBUILD_PATH%\MSBuild.exe" examples/example_sdl2_opengl2/example_sdl2_opengl2.vcxproj /p:Platform=x64 /p:Configuration=Release'
if: github.event_name == 'workflow_run'
- - name: Build x64 example_sdl2_opengl3
+ - name: Build Win64 example_sdl2_opengl3
shell: cmd
run: '"%MSBUILD_PATH%\MSBuild.exe" examples/example_sdl2_opengl3/example_sdl2_opengl3.vcxproj /p:Platform=x64 /p:Configuration=Release'
if: github.event_name == 'workflow_run'
- - name: Build x64 example_sdl2_directx11
+ - name: Build Win64 example_sdl2_directx11
shell: cmd
run: '"%MSBUILD_PATH%\MSBuild.exe" examples/example_sdl2_directx11/example_sdl2_directx11.vcxproj /p:Platform=x64 /p:Configuration=Release'
- - name: Build x64 example_win32_directx9
+ - name: Build Win64 example_sdl3_opengl3
+ shell: cmd
+ run: '"%MSBUILD_PATH%\MSBuild.exe" examples/example_sdl3_opengl3/example_sdl3_opengl3.vcxproj /p:Platform=x64 /p:Configuration=Release'
+ if: github.event_name == 'workflow_run'
+
+ - name: Build Win64 example_sdl3_sdlgpu3
+ shell: cmd
+ run: '"%MSBUILD_PATH%\MSBuild.exe" examples/example_sdl3_sdlgpu3/example_sdl3_sdlgpu3.vcxproj /p:Platform=x64 /p:Configuration=Release'
+
+ - name: Build Win64 example_sdl3_sdlrenderer3
+ shell: cmd
+ run: '"%MSBUILD_PATH%\MSBuild.exe" examples/example_sdl3_sdlrenderer3/example_sdl3_sdlrenderer3.vcxproj /p:Platform=x64 /p:Configuration=Release'
+
+ - name: Build Win64 example_sdl3_vulkan
+ shell: cmd
+ run: '"%MSBUILD_PATH%\MSBuild.exe" examples/example_sdl3_vulkan/example_sdl3_vulkan.vcxproj /p:Platform=x64 /p:Configuration=Release'
+ if: github.event_name == 'workflow_run'
+
+ - name: Build Win64 example_win32_directx9
shell: cmd
run: '"%MSBUILD_PATH%\MSBuild.exe" examples/example_win32_directx9/example_win32_directx9.vcxproj /p:Platform=x64 /p:Configuration=Release'
if: github.event_name == 'workflow_run'
- - name: Build x64 example_win32_directx10
+ - name: Build Win64 example_win32_directx10
shell: cmd
run: '"%MSBUILD_PATH%\MSBuild.exe" examples/example_win32_directx10/example_win32_directx10.vcxproj /p:Platform=x64 /p:Configuration=Release'
if: github.event_name == 'workflow_run'
- - name: Build x64 example_win32_directx11
+ - name: Build Win64 example_win32_directx11
shell: cmd
run: '"%MSBUILD_PATH%\MSBuild.exe" examples/example_win32_directx11/example_win32_directx11.vcxproj /p:Platform=x64 /p:Configuration=Release'
- if: github.event_name == 'workflow_run'
- - name: Build x64 example_win32_directx12
+ - name: Build Win64 example_win32_directx12
shell: cmd
run: '"%MSBUILD_PATH%\MSBuild.exe" examples/example_win32_directx12/example_win32_directx12.vcxproj /p:Platform=x64 /p:Configuration=Release'
Linux:
- runs-on: ubuntu-24.04
+ runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
@@ -441,7 +481,7 @@ jobs:
- name: Install Dependencies
run: |
- brew install glfw3 sdl2
+ brew install glfw3 sdl2 sdl3
- name: Build example_null (extra warnings, clang 64-bit)
run: make -C examples/example_null WITH_EXTRA_WARNINGS=1
@@ -499,6 +539,9 @@ jobs:
- name: Build example_sdl2_opengl3
run: make -C examples/example_sdl2_opengl3
+ - name: Build example_sdl3_opengl3
+ run: make -C examples/example_sdl3_opengl3
+
- name: Build example_apple_metal
run: xcodebuild -project examples/example_apple_metal/example_apple_metal.xcodeproj -target example_apple_metal_macos
@@ -506,7 +549,7 @@ jobs:
run: xcodebuild -project examples/example_apple_opengl2/example_apple_opengl2.xcodeproj -target example_osx_opengl2
iOS:
- runs-on: macos-latest
+ runs-on: macos-14
steps:
- uses: actions/checkout@v4
@@ -516,7 +559,7 @@ jobs:
xcodebuild -project examples/example_apple_metal/example_apple_metal.xcodeproj -target example_apple_metal_ios CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED=NO CODE_SIGNING_ALLOWED=NO
Emscripten:
- runs-on: ubuntu-24.04
+ runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
@@ -557,7 +600,7 @@ jobs:
cmake --build build
Android:
- runs-on: ubuntu-24.04
+ runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
diff --git a/.github/workflows/build_windows_vulkan_libs.ps1 b/.github/workflows/build_windows_vulkan_libs.ps1
new file mode 100644
index 000000000..6658f5b5b
--- /dev/null
+++ b/.github/workflows/build_windows_vulkan_libs.ps1
@@ -0,0 +1,38 @@
+# This is current meant to be run manually, occasionally:
+# - Run then zip the contents of vulkanArtifact/ into e.g. vulkan_windows_libs_1.4.326
+# - Upload as an attachment to https://github.com/ocornut/imgui/pull/8925 then change filename in build.yml
+# - There is a build_windows_vulkan_libs.yml in commit history that we removed thinking this is run so rarely we don't need to pollute CI UI with it.
+
+# Set default vulkan version if none provided
+if (-not $env:VULKAN_TAG) { $env:VULKAN_TAG = "1.4.326" }
+
+# Create output folder
+mkdir vulkanArtifact
+
+# Download Vulkan Headers
+
+Invoke-WebRequest -Uri "https://github.com/KhronosGroup/Vulkan-Headers/archive/refs/tags/v$($env:VULKAN_TAG).zip" -OutFile Vulkan-Headers-$($env:VULKAN_TAG).zip
+Expand-Archive -Path Vulkan-Headers-$($env:VULKAN_TAG).zip
+
+# Copy Vulkan Headers to artifact folder
+
+cp -R Vulkan-Headers-$($env:VULKAN_TAG)\Vulkan-Headers-$($env:VULKAN_TAG)\include vulkanArtifact\Include
+
+# Download Vulkan Loader
+
+Invoke-WebRequest -Uri "https://github.com/KhronosGroup/Vulkan-Loader/archive/refs/tags/v$($env:VULKAN_TAG).zip" -OutFile Vulkan-Loader-$($env:VULKAN_TAG).zip
+Expand-Archive -Path Vulkan-Loader-$($env:VULKAN_TAG).zip
+
+# Build Vulkan Loader x64
+
+cmake -S Vulkan-Loader-$($env:VULKAN_TAG)\Vulkan-Loader-$($env:VULKAN_TAG) -B VulkanLoader-build64 -D UPDATE_DEPS=On -A x64
+cmake --build VulkanLoader-build64
+mkdir vulkanArtifact\Lib
+copy VulkanLoader-build64\loader\Debug\vulkan-1.lib vulkanArtifact\Lib
+
+# Build Vulkan Loader win32
+
+cmake -S Vulkan-Loader-$($env:VULKAN_TAG)\Vulkan-Loader-$($env:VULKAN_TAG) -B VulkanLoader-build32 -D UPDATE_DEPS=On -A Win32
+cmake --build VulkanLoader-build32
+mkdir vulkanArtifact\Lib32
+copy VulkanLoader-build32\loader\Debug\vulkan-1.lib vulkanArtifact\Lib32
diff --git a/.github/workflows/static-analysis.yml b/.github/workflows/static-analysis.yml
index 69df5cdf8..a69c5110c 100644
--- a/.github/workflows/static-analysis.yml
+++ b/.github/workflows/static-analysis.yml
@@ -10,7 +10,7 @@ on:
jobs:
PVS-Studio:
- runs-on: ubuntu-22.04
+ runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
@@ -42,5 +42,5 @@ jobs:
fi
cd examples/example_null
pvs-studio-analyzer trace -- make WITH_EXTRA_WARNINGS=1
- pvs-studio-analyzer analyze -e ../../imstb_rectpack.h -e ../../imstb_textedit.h -e ../../imstb_truetype.h -l ../../pvs-studio.lic -o pvs-studio.log
+ pvs-studio-analyzer analyze --disableLicenseExpirationCheck -e ../../imstb_rectpack.h -e ../../imstb_textedit.h -e ../../imstb_truetype.h -l ../../pvs-studio.lic -o pvs-studio.log
plog-converter -a 'GA:1,2;OP:1' -d V1071 -t errorfile -w pvs-studio.log
diff --git a/.gitignore b/.gitignore
index 15a908273..a3faf78bb 100644
--- a/.gitignore
+++ b/.gitignore
@@ -29,12 +29,14 @@ ipch
## Getting files created in JSON/Schemas/Catalog/ from a VS2022 update
JSON/
-## Commonly used CMake directories
+## Commonly used CMake directories & CMake CPM cache
build*/
+.cache
-## Xcode artifacts
+## Xcode & macOS artifacts
project.xcworkspace
xcuserdata
+examples/*/*.dSYM
## Emscripten artifacts
examples/*.o.tmp
@@ -53,12 +55,21 @@ cmake-build-*
.vscode
## Unix executables from our example Makefiles
+examples/example_apple_metal/example_apple_metal
+examples/example_apple_opengl2/example_apple_opengl2
examples/example_glfw_metal/example_glfw_metal
examples/example_glfw_opengl2/example_glfw_opengl2
examples/example_glfw_opengl3/example_glfw_opengl3
+examples/example_glfw_vulkan/example_glfw_vulkan
examples/example_glut_opengl2/example_glut_opengl2
examples/example_null/example_null
examples/example_sdl2_metal/example_sdl2_metal
examples/example_sdl2_opengl2/example_sdl2_opengl2
examples/example_sdl2_opengl3/example_sdl2_opengl3
-examples/example_sdl2_sdlrenderer/example_sdl2_sdlrenderer
+examples/example_sdl2_sdlrenderer2/example_sdl2_sdlrenderer2
+examples/example_sdl2_vulkan/example_sdl2_vulkan
+examples/example_sdl3_metal/example_sdl3_metal
+examples/example_sdl3_opengl3/example_sdl3_opengl3
+examples/example_sdl3_sdlgpu3/example_sdl3_sdlgpu3
+examples/example_sdl3_sdlrenderer3/example_sdl3_sdlrenderer3
+examples/example_sdl3_vulkan/example_sdl3_vulkan
diff --git a/backends/imgui_impl_allegro5.cpp b/backends/imgui_impl_allegro5.cpp
index b990aa8f6..4ce8084cf 100644
--- a/backends/imgui_impl_allegro5.cpp
+++ b/backends/imgui_impl_allegro5.cpp
@@ -2,7 +2,8 @@
// (Info: Allegro 5 is a cross-platform general purpose library for handling windows, inputs, graphics, etc.)
// Implemented features:
-// [X] Renderer: User texture binding. Use 'ALLEGRO_BITMAP*' as ImTextureID. Read the FAQ about ImTextureID!
+// [X] Renderer: User texture binding. Use 'ALLEGRO_BITMAP*' as texture identifier. Read the FAQ about ImTextureID/ImTextureRef!
+// [X] Renderer: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures).
// [X] Platform: Keyboard support. Since 1.87 we are using the io.AddKeyEvent() function. Pass ImGuiKey values to all key functions e.g. ImGui::IsKeyPressed(ImGuiKey_Space). [Legacy ALLEGRO_KEY_* values are obsolete since 1.87 and not supported since 1.91.5]
// [X] Platform: Clipboard support (from Allegro 5.1.12).
// [X] Platform: Mouse cursor shape and visibility (ImGuiBackendFlags_HasMouseCursors). Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'.
@@ -21,6 +22,11 @@
// CHANGELOG
// (minor and older changes stripped away, please see git history for details)
+// 2025-09-18: Call platform_io.ClearRendererHandlers() and platform_io.ClearPlatformHandlers() on shutdown.
+// 2025-08-12: Inputs: fixed missing support for ImGuiKey_PrintScreen under Windows, as raw Allegro 5 does not receive it.
+// 2025-08-12: Added ImGui_ImplAllegro5_SetDisplay() function to change current ALLEGRO_DISPLAY, as Allegro applications often need to do that.
+// 2025-07-07: Fixed texture update broken on some platforms where ALLEGRO_LOCK_WRITEONLY needed all texels to be rewritten.
+// 2025-06-11: Added support for ImGuiBackendFlags_RendererHasTextures, for dynamic font atlas. Removed ImGui_ImplSDLGPU3_CreateFontsTexture() and ImGui_ImplSDLGPU3_DestroyFontsTexture().
// 2025-02-18: Added ImGuiMouseCursor_Wait and ImGuiMouseCursor_Progress mouse cursor support.
// 2025-01-06: Avoid calling al_set_mouse_cursor() repeatedly since it appears to leak on on X11 (#8256).
// 2024-08-22: moved some OS/backend related function pointers from ImGuiIO to ImGuiPlatformIO:
@@ -138,6 +144,13 @@ void ImGui_ImplAllegro5_RenderDrawData(ImDrawData* draw_data)
if (draw_data->DisplaySize.x <= 0.0f || draw_data->DisplaySize.y <= 0.0f)
return;
+ // Catch up with texture updates. Most of the times, the list will have 1 element with an OK status, aka nothing to do.
+ // (This almost always points to ImGui::GetPlatformIO().Textures[] but is part of ImDrawData to allow overriding or disabling texture updates).
+ if (draw_data->Textures != nullptr)
+ for (ImTextureData* tex : *draw_data->Textures)
+ if (tex->Status != ImTextureStatus_OK)
+ ImGui_ImplAllegro5_UpdateTexture(tex);
+
// Backup Allegro state that will be modified
ImGui_ImplAllegro5_Data* bd = ImGui_ImplAllegro5_GetBackendData();
ALLEGRO_TRANSFORM last_transform = *al_get_current_transform();
@@ -151,10 +164,8 @@ void ImGui_ImplAllegro5_RenderDrawData(ImDrawData* draw_data)
ImGui_ImplAllegro5_SetupRenderState(draw_data);
// Render command lists
- for (int n = 0; n < draw_data->CmdListsCount; n++)
+ for (const ImDrawList* draw_list : draw_data->CmdLists)
{
- const ImDrawList* draw_list = draw_data->CmdLists[n];
-
ImVector& vertices = bd->BufVertices;
#if ALLEGRO_HAS_DRAW_INDEXED_PRIM
vertices.resize(draw_list->VtxBuffer.Size);
@@ -233,43 +244,7 @@ void ImGui_ImplAllegro5_RenderDrawData(ImDrawData* draw_data)
bool ImGui_ImplAllegro5_CreateDeviceObjects()
{
- // Build texture atlas
ImGui_ImplAllegro5_Data* bd = ImGui_ImplAllegro5_GetBackendData();
- ImGuiIO& io = ImGui::GetIO();
- unsigned char* pixels;
- int width, height;
- io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
-
- // Create texture
- // (Bilinear sampling is required by default. Set 'io.Fonts->Flags |= ImFontAtlasFlags_NoBakedLines' or 'style.AntiAliasedLinesUseTex = false' to allow point/nearest sampling)
- int flags = al_get_new_bitmap_flags();
- int fmt = al_get_new_bitmap_format();
- al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP | ALLEGRO_MIN_LINEAR | ALLEGRO_MAG_LINEAR);
- al_set_new_bitmap_format(ALLEGRO_PIXEL_FORMAT_ABGR_8888_LE);
- ALLEGRO_BITMAP* img = al_create_bitmap(width, height);
- al_set_new_bitmap_flags(flags);
- al_set_new_bitmap_format(fmt);
- if (!img)
- return false;
-
- ALLEGRO_LOCKED_REGION* locked_img = al_lock_bitmap(img, al_get_bitmap_format(img), ALLEGRO_LOCK_WRITEONLY);
- if (!locked_img)
- {
- al_destroy_bitmap(img);
- return false;
- }
- memcpy(locked_img->data, pixels, sizeof(int) * width * height);
- al_unlock_bitmap(img);
-
- // Convert software texture to hardware texture.
- ALLEGRO_BITMAP* cloned_img = al_clone_bitmap(img);
- al_destroy_bitmap(img);
- if (!cloned_img)
- return false;
-
- // Store our identifier
- io.Fonts->SetTexID((ImTextureID)(intptr_t)cloned_img);
- bd->Texture = cloned_img;
// Create an invisible mouse cursor
// Because al_hide_mouse_cursor() seems to mess up with the actual inputs..
@@ -280,16 +255,82 @@ bool ImGui_ImplAllegro5_CreateDeviceObjects()
return true;
}
+void ImGui_ImplAllegro5_UpdateTexture(ImTextureData* tex)
+{
+ if (tex->Status == ImTextureStatus_WantCreate)
+ {
+ // Create and upload new texture to graphics system
+ //IMGUI_DEBUG_LOG("UpdateTexture #%03d: WantCreate %dx%d\n", tex->UniqueID, tex->Width, tex->Height);
+ IM_ASSERT(tex->TexID == ImTextureID_Invalid && tex->BackendUserData == nullptr);
+ IM_ASSERT(tex->Format == ImTextureFormat_RGBA32);
+
+ // Create texture
+ // (Bilinear sampling is required by default. Set 'io.Fonts->Flags |= ImFontAtlasFlags_NoBakedLines' or 'style.AntiAliasedLinesUseTex = false' to allow point/nearest sampling)
+ const int new_bitmap_flags = al_get_new_bitmap_flags();
+ int new_bitmap_format = al_get_new_bitmap_format();
+ al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP | ALLEGRO_MIN_LINEAR | ALLEGRO_MAG_LINEAR);
+ al_set_new_bitmap_format(ALLEGRO_PIXEL_FORMAT_ABGR_8888_LE);
+ ALLEGRO_BITMAP* cpu_bitmap = al_create_bitmap(tex->Width, tex->Height);
+ IM_ASSERT(cpu_bitmap != nullptr && "Backend failed to create texture!");
+
+ // Upload pixels
+ ALLEGRO_LOCKED_REGION* locked_region = al_lock_bitmap(cpu_bitmap, al_get_bitmap_format(cpu_bitmap), ALLEGRO_LOCK_WRITEONLY);
+ IM_ASSERT(locked_region != nullptr && "Backend failed to create texture!");
+ memcpy(locked_region->data, tex->GetPixels(), tex->GetSizeInBytes());
+ al_unlock_bitmap(cpu_bitmap);
+
+ // Convert software texture to hardware texture.
+ al_set_new_bitmap_flags(ALLEGRO_VIDEO_BITMAP);
+ al_set_new_bitmap_format(ALLEGRO_PIXEL_FORMAT_ANY_32_WITH_ALPHA);
+ ALLEGRO_BITMAP* gpu_bitmap = al_clone_bitmap(cpu_bitmap);
+ al_destroy_bitmap(cpu_bitmap);
+ IM_ASSERT(gpu_bitmap != nullptr && "Backend failed to create texture!");
+
+ al_set_new_bitmap_flags(new_bitmap_flags);
+ al_set_new_bitmap_format(new_bitmap_format);
+
+ // Store identifiers
+ tex->SetTexID((ImTextureID)(intptr_t)gpu_bitmap);
+ tex->SetStatus(ImTextureStatus_OK);
+ }
+ else if (tex->Status == ImTextureStatus_WantUpdates)
+ {
+ // Update selected blocks. We only ever write to textures regions which have never been used before!
+ // This backend choose to use tex->Updates[] but you can use tex->UpdateRect to upload a single region.
+ ImTextureRect r = tex->UpdateRect; // Bounding box encompassing all individual updates
+ ALLEGRO_BITMAP* gpu_bitmap = (ALLEGRO_BITMAP*)(intptr_t)tex->TexID;
+ ALLEGRO_LOCKED_REGION* locked_region = al_lock_bitmap_region(gpu_bitmap, r.x, r.y, r.w, r.h, al_get_bitmap_format(gpu_bitmap), ALLEGRO_LOCK_WRITEONLY);
+ IM_ASSERT(locked_region && "Backend failed to update texture!");
+ for (int y = 0; y < r.h; y++)
+ memcpy((unsigned char*)locked_region->data + locked_region->pitch * y, tex->GetPixelsAt(r.x, r.y + y), r.w * tex->BytesPerPixel); // dst, src, block pitch
+ al_unlock_bitmap(gpu_bitmap);
+ tex->SetStatus(ImTextureStatus_OK);
+ }
+ else if (tex->Status == ImTextureStatus_WantDestroy)
+ {
+ ALLEGRO_BITMAP* backend_tex = (ALLEGRO_BITMAP*)(intptr_t)tex->TexID;
+ if (backend_tex)
+ al_destroy_bitmap(backend_tex);
+
+ // Clear identifiers and mark as destroyed (in order to allow e.g. calling InvalidateDeviceObjects while running)
+ tex->SetTexID(ImTextureID_Invalid);
+ tex->SetStatus(ImTextureStatus_Destroyed);
+ }
+}
+
void ImGui_ImplAllegro5_InvalidateDeviceObjects()
{
- ImGuiIO& io = ImGui::GetIO();
ImGui_ImplAllegro5_Data* bd = ImGui_ImplAllegro5_GetBackendData();
- if (bd->Texture)
- {
- io.Fonts->SetTexID(0);
- al_destroy_bitmap(bd->Texture);
- bd->Texture = nullptr;
- }
+
+ // Destroy all textures
+ for (ImTextureData* tex : ImGui::GetPlatformIO().Textures)
+ if (tex->RefCount == 1)
+ {
+ tex->SetStatus(ImTextureStatus_WantDestroy);
+ ImGui_ImplAllegro5_UpdateTexture(tex);
+ }
+
+ // Destroy mouse cursor
if (bd->MouseCursorInvisible)
{
al_destroy_mouse_cursor(bd->MouseCursorInvisible);
@@ -440,21 +481,11 @@ bool ImGui_ImplAllegro5_Init(ALLEGRO_DISPLAY* display)
io.BackendPlatformUserData = (void*)bd;
io.BackendPlatformName = io.BackendRendererName = "imgui_impl_allegro5";
io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional)
+ io.BackendFlags |= ImGuiBackendFlags_RendererHasTextures; // We can honor ImGuiPlatformIO::Textures[] requests during render.
- bd->Display = display;
bd->LastCursor = ALLEGRO_SYSTEM_MOUSE_CURSOR_NONE;
- // Create custom vertex declaration.
- // Unfortunately Allegro doesn't support 32-bit packed colors so we have to convert them to 4 floats.
- // We still use a custom declaration to use 'ALLEGRO_PRIM_TEX_COORD' instead of 'ALLEGRO_PRIM_TEX_COORD_PIXEL' else we can't do a reliable conversion.
- ALLEGRO_VERTEX_ELEMENT elems[] =
- {
- { ALLEGRO_PRIM_POSITION, ALLEGRO_PRIM_FLOAT_2, offsetof(ImDrawVertAllegro, pos) },
- { ALLEGRO_PRIM_TEX_COORD, ALLEGRO_PRIM_FLOAT_2, offsetof(ImDrawVertAllegro, uv) },
- { ALLEGRO_PRIM_COLOR_ATTR, 0, offsetof(ImDrawVertAllegro, col) },
- { 0, 0, 0 }
- };
- bd->VertexDecl = al_create_vertex_decl(elems, sizeof(ImDrawVertAllegro));
+ ImGui_ImplAllegro5_SetDisplay(display);
#if ALLEGRO_HAS_CLIPBOARD
ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
@@ -470,6 +501,7 @@ void ImGui_ImplAllegro5_Shutdown()
ImGui_ImplAllegro5_Data* bd = ImGui_ImplAllegro5_GetBackendData();
IM_ASSERT(bd != nullptr && "No platform backend to shutdown, or already shutdown?");
ImGuiIO& io = ImGui::GetIO();
+ ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
ImGui_ImplAllegro5_InvalidateDeviceObjects();
if (bd->VertexDecl)
@@ -479,10 +511,39 @@ void ImGui_ImplAllegro5_Shutdown()
io.BackendPlatformName = io.BackendRendererName = nullptr;
io.BackendPlatformUserData = nullptr;
- io.BackendFlags &= ~ImGuiBackendFlags_HasMouseCursors;
+ io.BackendFlags &= ~(ImGuiBackendFlags_HasMouseCursors | ImGuiBackendFlags_RendererHasTextures);
+ platform_io.ClearRendererHandlers();
+ platform_io.ClearPlatformHandlers();
IM_DELETE(bd);
}
+void ImGui_ImplAllegro5_SetDisplay(ALLEGRO_DISPLAY* display)
+{
+ ImGui_ImplAllegro5_Data* bd = ImGui_ImplAllegro5_GetBackendData();
+ bd->Display = display;
+
+ if (bd->VertexDecl)
+ {
+ al_destroy_vertex_decl(bd->VertexDecl);
+ bd->VertexDecl = NULL;
+ }
+
+ if (bd->Display && !bd->VertexDecl)
+ {
+ // Create custom vertex declaration.
+ // Unfortunately Allegro doesn't support 32-bits packed colors so we have to convert them to 4 floats.
+ // We still use a custom declaration to use 'ALLEGRO_PRIM_TEX_COORD' instead of 'ALLEGRO_PRIM_TEX_COORD_PIXEL' else we can't do a reliable conversion.
+ ALLEGRO_VERTEX_ELEMENT elems[] =
+ {
+ { ALLEGRO_PRIM_POSITION, ALLEGRO_PRIM_FLOAT_2, offsetof(ImDrawVertAllegro, pos) },
+ { ALLEGRO_PRIM_TEX_COORD, ALLEGRO_PRIM_FLOAT_2, offsetof(ImDrawVertAllegro, uv) },
+ { ALLEGRO_PRIM_COLOR_ATTR, 0, offsetof(ImDrawVertAllegro, col) },
+ { 0, 0, 0 }
+ };
+ bd->VertexDecl = al_create_vertex_decl(elems, sizeof(ImDrawVertAllegro));
+ }
+}
+
// ev->keyboard.modifiers seems always zero so using that...
static void ImGui_ImplAllegro5_UpdateKeyModifiers()
{
@@ -609,12 +670,11 @@ void ImGui_ImplAllegro5_NewFrame()
ImGui_ImplAllegro5_Data* bd = ImGui_ImplAllegro5_GetBackendData();
IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplAllegro5_Init()?");
- if (!bd->Texture)
+ if (!bd->MouseCursorInvisible)
ImGui_ImplAllegro5_CreateDeviceObjects();
- ImGuiIO& io = ImGui::GetIO();
-
// Setup display size (every frame to accommodate for window resizing)
+ ImGuiIO& io = ImGui::GetIO();
int w, h;
w = al_get_display_width(bd->Display);
h = al_get_display_height(bd->Display);
@@ -625,6 +685,11 @@ void ImGui_ImplAllegro5_NewFrame()
io.DeltaTime = bd->Time > 0.0 ? (float)(current_time - bd->Time) : (float)(1.0f / 60.0f);
bd->Time = current_time;
+ // Allegro 5 doesn't receive PrintScreen under Windows
+#ifdef _WIN32
+ io.AddKeyEvent(ImGuiKey_PrintScreen, (::GetAsyncKeyState(VK_SNAPSHOT) & 0x8000) != 0);
+#endif
+
// Setup mouse cursor shape
ImGui_ImplAllegro5_UpdateMouseCursor();
}
diff --git a/backends/imgui_impl_allegro5.h b/backends/imgui_impl_allegro5.h
index e67ffe509..a6e93ecf0 100644
--- a/backends/imgui_impl_allegro5.h
+++ b/backends/imgui_impl_allegro5.h
@@ -2,7 +2,8 @@
// (Info: Allegro 5 is a cross-platform general purpose library for handling windows, inputs, graphics, etc.)
// Implemented features:
-// [X] Renderer: User texture binding. Use 'ALLEGRO_BITMAP*' as ImTextureID. Read the FAQ about ImTextureID!
+// [X] Renderer: User texture binding. Use 'ALLEGRO_BITMAP*' as texture identifier. Read the FAQ about ImTextureID/ImTextureRef!
+// [X] Renderer: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures).
// [X] Platform: Keyboard support. Since 1.87 we are using the io.AddKeyEvent() function. Pass ImGuiKey values to all key functions e.g. ImGui::IsKeyPressed(ImGuiKey_Space). [Legacy ALLEGRO_KEY_* values are obsolete since 1.87 and not supported since 1.91.5]
// [X] Platform: Clipboard support (from Allegro 5.1.12).
// [X] Platform: Mouse cursor shape and visibility (ImGuiBackendFlags_HasMouseCursors). Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'.
@@ -32,9 +33,13 @@ IMGUI_IMPL_API void ImGui_ImplAllegro5_Shutdown();
IMGUI_IMPL_API void ImGui_ImplAllegro5_NewFrame();
IMGUI_IMPL_API void ImGui_ImplAllegro5_RenderDrawData(ImDrawData* draw_data);
IMGUI_IMPL_API bool ImGui_ImplAllegro5_ProcessEvent(ALLEGRO_EVENT* event);
+IMGUI_IMPL_API void ImGui_ImplAllegro5_SetDisplay(ALLEGRO_DISPLAY* display);
// Use if you want to reset your rendering device without losing Dear ImGui state.
IMGUI_IMPL_API bool ImGui_ImplAllegro5_CreateDeviceObjects();
IMGUI_IMPL_API void ImGui_ImplAllegro5_InvalidateDeviceObjects();
+// (Advanced) Use e.g. if you need to precisely control the timing of texture updates (e.g. for staged rendering), by setting ImDrawData::Textures = NULL to handle this manually.
+IMGUI_IMPL_API void ImGui_ImplAllegro5_UpdateTexture(ImTextureData* tex);
+
#endif // #ifndef IMGUI_DISABLE
diff --git a/backends/imgui_impl_dx10.cpp b/backends/imgui_impl_dx10.cpp
index dd2e600be..376ed38b1 100644
--- a/backends/imgui_impl_dx10.cpp
+++ b/backends/imgui_impl_dx10.cpp
@@ -2,8 +2,9 @@
// This needs to be used along with a Platform Backend (e.g. Win32)
// Implemented features:
-// [X] Renderer: User texture binding. Use 'ID3D10ShaderResourceView*' as ImTextureID. Read the FAQ about ImTextureID!
+// [X] Renderer: User texture binding. Use 'ID3D10ShaderResourceView*' as texture identifier. Read the FAQ about ImTextureID/ImTextureRef!
// [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset).
+// [X] Renderer: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures).
// [X] Renderer: Multi-viewport support (multiple windows). Enable with 'io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable'.
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
@@ -17,6 +18,9 @@
// CHANGELOG
// (minor and older changes stripped away, please see git history for details)
// 2025-XX-XX: Platform: Added support for multiple windows via the ImGuiPlatformIO interface.
+// 2025-09-18: Call platform_io.ClearRendererHandlers() on shutdown.
+// 2025-06-11: DirectX10: Added support for ImGuiBackendFlags_RendererHasTextures, for dynamic font atlas.
+// 2025-05-07: DirectX10: Honor draw_data->FramebufferScale to allow for custom backends and experiment using it (consistently with other renderer backends, even though in normal condition it is not set under Windows).
// 2025-01-06: DirectX10: Expose selected render state in ImGui_ImplDX10_RenderState, which you can access in 'void* platform_io.Renderer_RenderState' during draw callbacks.
// 2024-10-07: DirectX10: Changed default texture sampler to Clamp instead of Repeat/Wrap.
// 2022-10-11: Using 'nullptr' instead of 'NULL' as per our switch to C++11.
@@ -49,7 +53,19 @@
#pragma comment(lib, "d3dcompiler") // Automatically link with d3dcompiler.lib as we are using D3DCompile() below.
#endif
+// Clang/GCC warnings with -Weverything
+#if defined(__clang__)
+#pragma clang diagnostic ignored "-Wold-style-cast" // warning: use of old-style cast // yes, they are more terse.
+#pragma clang diagnostic ignored "-Wsign-conversion" // warning: implicit conversion changes signedness
+#endif
+
// DirectX10 data
+struct ImGui_ImplDX10_Texture
+{
+ ID3D10Texture2D* pTexture;
+ ID3D10ShaderResourceView* pTextureView;
+};
+
struct ImGui_ImplDX10_Data
{
ID3D10Device* pd3dDevice;
@@ -60,8 +76,7 @@ struct ImGui_ImplDX10_Data
ID3D10InputLayout* pInputLayout;
ID3D10Buffer* pVertexConstantBuffer;
ID3D10PixelShader* pPixelShader;
- ID3D10SamplerState* pFontSampler;
- ID3D10ShaderResourceView* pFontTextureView;
+ ID3D10SamplerState* pTexSamplerLinear;
ID3D10RasterizerState* pRasterizerState;
ID3D10BlendState* pBlendState;
ID3D10DepthStencilState* pDepthStencilState;
@@ -94,8 +109,8 @@ static void ImGui_ImplDX10_SetupRenderState(ImDrawData* draw_data, ID3D10Device*
// Setup viewport
D3D10_VIEWPORT vp = {};
- vp.Width = (UINT)draw_data->DisplaySize.x;
- vp.Height = (UINT)draw_data->DisplaySize.y;
+ vp.Width = (UINT)(draw_data->DisplaySize.x * draw_data->FramebufferScale.x);
+ vp.Height = (UINT)(draw_data->DisplaySize.y * draw_data->FramebufferScale.y);
vp.MinDepth = 0.0f;
vp.MaxDepth = 1.0f;
vp.TopLeftX = vp.TopLeftY = 0;
@@ -132,7 +147,7 @@ static void ImGui_ImplDX10_SetupRenderState(ImDrawData* draw_data, ID3D10Device*
device->VSSetShader(bd->pVertexShader);
device->VSSetConstantBuffers(0, 1, &bd->pVertexConstantBuffer);
device->PSSetShader(bd->pPixelShader);
- device->PSSetSamplers(0, 1, &bd->pFontSampler);
+ device->PSSetSamplers(0, 1, &bd->pTexSamplerLinear);
device->GSSetShader(nullptr);
// Setup render state
@@ -152,6 +167,13 @@ void ImGui_ImplDX10_RenderDrawData(ImDrawData* draw_data)
ImGui_ImplDX10_Data* bd = ImGui_ImplDX10_GetBackendData();
ID3D10Device* device = bd->pd3dDevice;
+ // Catch up with texture updates. Most of the times, the list will have 1 element with an OK status, aka nothing to do.
+ // (This almost always points to ImGui::GetPlatformIO().Textures[] but is part of ImDrawData to allow overriding or disabling texture updates).
+ if (draw_data->Textures != nullptr)
+ for (ImTextureData* tex : *draw_data->Textures)
+ if (tex->Status != ImTextureStatus_OK)
+ ImGui_ImplDX10_UpdateTexture(tex);
+
// Create and grow vertex/index buffers if needed
if (!bd->pVB || bd->VertexBufferSize < draw_data->TotalVtxCount)
{
@@ -185,9 +207,8 @@ void ImGui_ImplDX10_RenderDrawData(ImDrawData* draw_data)
ImDrawIdx* idx_dst = nullptr;
bd->pVB->Map(D3D10_MAP_WRITE_DISCARD, 0, (void**)&vtx_dst);
bd->pIB->Map(D3D10_MAP_WRITE_DISCARD, 0, (void**)&idx_dst);
- for (int n = 0; n < draw_data->CmdListsCount; n++)
+ for (const ImDrawList* draw_list : draw_data->CmdLists)
{
- const ImDrawList* draw_list = draw_data->CmdLists[n];
memcpy(vtx_dst, draw_list->VtxBuffer.Data, draw_list->VtxBuffer.Size * sizeof(ImDrawVert));
memcpy(idx_dst, draw_list->IdxBuffer.Data, draw_list->IdxBuffer.Size * sizeof(ImDrawIdx));
vtx_dst += draw_list->VtxBuffer.Size;
@@ -243,7 +264,7 @@ void ImGui_ImplDX10_RenderDrawData(ImDrawData* draw_data)
ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
ImGui_ImplDX10_RenderState render_state;
render_state.Device = bd->pd3dDevice;
- render_state.SamplerDefault = bd->pFontSampler;
+ render_state.SamplerDefault = bd->pTexSamplerLinear;
render_state.VertexConstantBuffer = bd->pVertexConstantBuffer;
platform_io.Renderer_RenderState = &render_state;
@@ -252,9 +273,9 @@ void ImGui_ImplDX10_RenderDrawData(ImDrawData* draw_data)
int global_vtx_offset = 0;
int global_idx_offset = 0;
ImVec2 clip_off = draw_data->DisplayPos;
- for (int n = 0; n < draw_data->CmdListsCount; n++)
+ ImVec2 clip_scale = draw_data->FramebufferScale;
+ for (const ImDrawList* draw_list : draw_data->CmdLists)
{
- const ImDrawList* draw_list = draw_data->CmdLists[n];
for (int cmd_i = 0; cmd_i < draw_list->CmdBuffer.Size; cmd_i++)
{
const ImDrawCmd* pcmd = &draw_list->CmdBuffer[cmd_i];
@@ -270,8 +291,8 @@ void ImGui_ImplDX10_RenderDrawData(ImDrawData* draw_data)
else
{
// Project scissor/clipping rectangles into framebuffer space
- ImVec2 clip_min(pcmd->ClipRect.x - clip_off.x, pcmd->ClipRect.y - clip_off.y);
- ImVec2 clip_max(pcmd->ClipRect.z - clip_off.x, pcmd->ClipRect.w - clip_off.y);
+ ImVec2 clip_min((pcmd->ClipRect.x - clip_off.x) * clip_scale.x, (pcmd->ClipRect.y - clip_off.y) * clip_scale.y);
+ ImVec2 clip_max((pcmd->ClipRect.z - clip_off.x) * clip_scale.x, (pcmd->ClipRect.w - clip_off.y) * clip_scale.y);
if (clip_max.x <= clip_min.x || clip_max.y <= clip_min.y)
continue;
@@ -308,21 +329,39 @@ void ImGui_ImplDX10_RenderDrawData(ImDrawData* draw_data)
device->IASetInputLayout(old.InputLayout); if (old.InputLayout) old.InputLayout->Release();
}
-static void ImGui_ImplDX10_CreateFontsTexture()
+static void ImGui_ImplDX10_DestroyTexture(ImTextureData* tex)
{
- // Build texture atlas
- ImGui_ImplDX10_Data* bd = ImGui_ImplDX10_GetBackendData();
- ImGuiIO& io = ImGui::GetIO();
- unsigned char* pixels;
- int width, height;
- io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
-
- // Upload texture to graphics system
+ if (ImGui_ImplDX10_Texture* backend_tex = (ImGui_ImplDX10_Texture*)tex->BackendUserData)
{
+ IM_ASSERT(backend_tex->pTextureView == (ID3D10ShaderResourceView*)(intptr_t)tex->TexID);
+ backend_tex->pTextureView->Release();
+ backend_tex->pTexture->Release();
+ IM_DELETE(backend_tex);
+
+ // Clear identifiers and mark as destroyed (in order to allow e.g. calling InvalidateDeviceObjects while running)
+ tex->SetTexID(ImTextureID_Invalid);
+ tex->BackendUserData = nullptr;
+ }
+ tex->SetStatus(ImTextureStatus_Destroyed);
+}
+
+void ImGui_ImplDX10_UpdateTexture(ImTextureData* tex)
+{
+ ImGui_ImplDX10_Data* bd = ImGui_ImplDX10_GetBackendData();
+ if (tex->Status == ImTextureStatus_WantCreate)
+ {
+ // Create and upload new texture to graphics system
+ //IMGUI_DEBUG_LOG("UpdateTexture #%03d: WantCreate %dx%d\n", tex->UniqueID, tex->Width, tex->Height);
+ IM_ASSERT(tex->TexID == ImTextureID_Invalid && tex->BackendUserData == nullptr);
+ IM_ASSERT(tex->Format == ImTextureFormat_RGBA32);
+ unsigned int* pixels = (unsigned int*)tex->GetPixels();
+ ImGui_ImplDX10_Texture* backend_tex = IM_NEW(ImGui_ImplDX10_Texture)();
+
+ // Create texture
D3D10_TEXTURE2D_DESC desc;
ZeroMemory(&desc, sizeof(desc));
- desc.Width = width;
- desc.Height = height;
+ desc.Width = (UINT)tex->Width;
+ desc.Height = (UINT)tex->Height;
desc.MipLevels = 1;
desc.ArraySize = 1;
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
@@ -331,13 +370,12 @@ static void ImGui_ImplDX10_CreateFontsTexture()
desc.BindFlags = D3D10_BIND_SHADER_RESOURCE;
desc.CPUAccessFlags = 0;
- ID3D10Texture2D* pTexture = nullptr;
D3D10_SUBRESOURCE_DATA subResource;
subResource.pSysMem = pixels;
subResource.SysMemPitch = desc.Width * 4;
subResource.SysMemSlicePitch = 0;
- bd->pd3dDevice->CreateTexture2D(&desc, &subResource, &pTexture);
- IM_ASSERT(pTexture != nullptr);
+ bd->pd3dDevice->CreateTexture2D(&desc, &subResource, &backend_tex->pTexture);
+ IM_ASSERT(backend_tex->pTexture != nullptr && "Backend failed to create texture!");
// Create texture view
D3D10_SHADER_RESOURCE_VIEW_DESC srv_desc;
@@ -346,23 +384,29 @@ static void ImGui_ImplDX10_CreateFontsTexture()
srv_desc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURE2D;
srv_desc.Texture2D.MipLevels = desc.MipLevels;
srv_desc.Texture2D.MostDetailedMip = 0;
- bd->pd3dDevice->CreateShaderResourceView(pTexture, &srv_desc, &bd->pFontTextureView);
- pTexture->Release();
+ bd->pd3dDevice->CreateShaderResourceView(backend_tex->pTexture, &srv_desc, &backend_tex->pTextureView);
+ IM_ASSERT(backend_tex->pTextureView != nullptr && "Backend failed to create texture!");
+
+ // Store identifiers
+ tex->SetTexID((ImTextureID)(intptr_t)backend_tex->pTextureView);
+ tex->SetStatus(ImTextureStatus_OK);
+ tex->BackendUserData = backend_tex;
}
-
- // Store our identifier
- io.Fonts->SetTexID((ImTextureID)bd->pFontTextureView);
-}
-
-static void ImGui_ImplDX10_DestroyFontsTexture()
-{
- ImGui_ImplDX10_Data* bd = ImGui_ImplDX10_GetBackendData();
- if (bd->pFontTextureView)
+ else if (tex->Status == ImTextureStatus_WantUpdates)
{
- bd->pFontTextureView->Release();
- bd->pFontTextureView = nullptr;
- ImGui::GetIO().Fonts->SetTexID(0); // We copied bd->pFontTextureView to io.Fonts->TexID so let's clear that as well.
+ // Update selected blocks. We only ever write to textures regions which have never been used before!
+ // This backend choose to use tex->Updates[] but you can use tex->UpdateRect to upload a single region.
+ ImGui_ImplDX10_Texture* backend_tex = (ImGui_ImplDX10_Texture*)tex->BackendUserData;
+ IM_ASSERT(backend_tex->pTextureView == (ID3D10ShaderResourceView*)(intptr_t)tex->TexID);
+ for (ImTextureRect& r : tex->Updates)
+ {
+ D3D10_BOX box = { (UINT)r.x, (UINT)r.y, (UINT)0, (UINT)(r.x + r.w), (UINT)(r.y + r.h), (UINT)1 };
+ bd->pd3dDevice->UpdateSubresource(backend_tex->pTexture, 0, &box, tex->GetPixelsAt(r.x, r.y), (UINT)tex->GetPitch(), 0);
+ }
+ tex->SetStatus(ImTextureStatus_OK);
}
+ if (tex->Status == ImTextureStatus_WantDestroy && tex->UnusedFrames > 0)
+ ImGui_ImplDX10_DestroyTexture(tex);
}
bool ImGui_ImplDX10_CreateDeviceObjects()
@@ -370,8 +414,7 @@ bool ImGui_ImplDX10_CreateDeviceObjects()
ImGui_ImplDX10_Data* bd = ImGui_ImplDX10_GetBackendData();
if (!bd->pd3dDevice)
return false;
- if (bd->pFontSampler)
- ImGui_ImplDX10_InvalidateDeviceObjects();
+ ImGui_ImplDX10_InvalidateDeviceObjects();
// By using D3DCompile() from / d3dcompiler.lib, we introduce a dependency to a given version of d3dcompiler_XX.dll (see D3DCOMPILER_DLL_A)
// If you would like to use this DX10 sample code but remove this dependency you can:
@@ -527,11 +570,9 @@ bool ImGui_ImplDX10_CreateDeviceObjects()
desc.ComparisonFunc = D3D10_COMPARISON_ALWAYS;
desc.MinLOD = 0.f;
desc.MaxLOD = 0.f;
- bd->pd3dDevice->CreateSamplerState(&desc, &bd->pFontSampler);
+ bd->pd3dDevice->CreateSamplerState(&desc, &bd->pTexSamplerLinear);
}
- ImGui_ImplDX10_CreateFontsTexture();
-
return true;
}
@@ -541,9 +582,11 @@ void ImGui_ImplDX10_InvalidateDeviceObjects()
if (!bd->pd3dDevice)
return;
- ImGui_ImplDX10_DestroyFontsTexture();
-
- if (bd->pFontSampler) { bd->pFontSampler->Release(); bd->pFontSampler = nullptr; }
+ // Destroy all textures
+ for (ImTextureData* tex : ImGui::GetPlatformIO().Textures)
+ if (tex->RefCount == 1)
+ ImGui_ImplDX10_DestroyTexture(tex);
+ if (bd->pTexSamplerLinear) { bd->pTexSamplerLinear->Release(); bd->pTexSamplerLinear = nullptr; }
if (bd->pIB) { bd->pIB->Release(); bd->pIB = nullptr; }
if (bd->pVB) { bd->pVB->Release(); bd->pVB = nullptr; }
if (bd->pBlendState) { bd->pBlendState->Release(); bd->pBlendState = nullptr; }
@@ -566,8 +609,12 @@ bool ImGui_ImplDX10_Init(ID3D10Device* device)
io.BackendRendererUserData = (void*)bd;
io.BackendRendererName = "imgui_impl_dx10";
io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes.
+ io.BackendFlags |= ImGuiBackendFlags_RendererHasTextures; // We can honor ImGuiPlatformIO::Textures[] requests during render.
io.BackendFlags |= ImGuiBackendFlags_RendererHasViewports; // We can create multi-viewports on the Renderer side (optional)
+ ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
+ platform_io.Renderer_TextureMaxWidth = platform_io.Renderer_TextureMaxHeight = D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION;
+
// Get factory from device
IDXGIDevice* pDXGIDevice = nullptr;
IDXGIAdapter* pDXGIAdapter = nullptr;
@@ -593,14 +640,17 @@ void ImGui_ImplDX10_Shutdown()
ImGui_ImplDX10_Data* bd = ImGui_ImplDX10_GetBackendData();
IM_ASSERT(bd != nullptr && "No renderer backend to shutdown, or already shutdown?");
ImGuiIO& io = ImGui::GetIO();
+ ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
ImGui_ImplDX10_ShutdownMultiViewportSupport();
ImGui_ImplDX10_InvalidateDeviceObjects();
if (bd->pFactory) { bd->pFactory->Release(); }
if (bd->pd3dDevice) { bd->pd3dDevice->Release(); }
+
io.BackendRendererName = nullptr;
io.BackendRendererUserData = nullptr;
- io.BackendFlags &= ~(ImGuiBackendFlags_RendererHasVtxOffset | ImGuiBackendFlags_RendererHasViewports);
+ io.BackendFlags &= ~(ImGuiBackendFlags_RendererHasVtxOffset | ImGuiBackendFlags_RendererHasTextures | ImGuiBackendFlags_RendererHasViewports);
+ platform_io.ClearRendererHandlers();
IM_DELETE(bd);
}
@@ -610,7 +660,8 @@ void ImGui_ImplDX10_NewFrame()
IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplDX10_Init()?");
if (!bd->pVertexShader)
- ImGui_ImplDX10_CreateDeviceObjects();
+ if (!ImGui_ImplDX10_CreateDeviceObjects())
+ IM_ASSERT(0 && "ImGui_ImplDX10_CreateDeviceObjects() failed!");
}
//--------------------------------------------------------------------------------------------------------
@@ -657,6 +708,7 @@ static void ImGui_ImplDX10_CreateWindow(ImGuiViewport* viewport)
IM_ASSERT(vd->SwapChain == nullptr && vd->RTView == nullptr);
bd->pFactory->CreateSwapChain(bd->pd3dDevice, &sd, &vd->SwapChain);
+ bd->pFactory->MakeWindowAssociation(hwnd, DXGI_MWA_NO_ALT_ENTER | DXGI_MWA_NO_WINDOW_CHANGES); // Disable e.g. Alt+Enter
// Create the render target
if (vd->SwapChain)
diff --git a/backends/imgui_impl_dx10.h b/backends/imgui_impl_dx10.h
index 07bbd9ffd..815113120 100644
--- a/backends/imgui_impl_dx10.h
+++ b/backends/imgui_impl_dx10.h
@@ -2,8 +2,9 @@
// This needs to be used along with a Platform Backend (e.g. Win32)
// Implemented features:
-// [X] Renderer: User texture binding. Use 'ID3D10ShaderResourceView*' as ImTextureID. Read the FAQ about ImTextureID!
+// [X] Renderer: User texture binding. Use 'ID3D10ShaderResourceView*' as texture identifier. Read the FAQ about ImTextureID/ImTextureRef!
// [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset).
+// [X] Renderer: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures).
// [X] Renderer: Multi-viewport support (multiple windows). Enable with 'io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable'.
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
@@ -32,6 +33,9 @@ IMGUI_IMPL_API void ImGui_ImplDX10_RenderDrawData(ImDrawData* draw_data);
IMGUI_IMPL_API bool ImGui_ImplDX10_CreateDeviceObjects();
IMGUI_IMPL_API void ImGui_ImplDX10_InvalidateDeviceObjects();
+// (Advanced) Use e.g. if you need to precisely control the timing of texture updates (e.g. for staged rendering), by setting ImDrawData::Textures = NULL to handle this manually.
+IMGUI_IMPL_API void ImGui_ImplDX10_UpdateTexture(ImTextureData* tex);
+
// [BETA] Selected render state data shared with callbacks.
// This is temporarily stored in GetPlatformIO().Renderer_RenderState during the ImGui_ImplDX10_RenderDrawData() call.
// (Please open an issue if you feel you need access to more data)
diff --git a/backends/imgui_impl_dx11.cpp b/backends/imgui_impl_dx11.cpp
index 2cc98f174..33751bfeb 100644
--- a/backends/imgui_impl_dx11.cpp
+++ b/backends/imgui_impl_dx11.cpp
@@ -2,8 +2,9 @@
// This needs to be used along with a Platform Backend (e.g. Win32)
// Implemented features:
-// [X] Renderer: User texture binding. Use 'ID3D11ShaderResourceView*' as ImTextureID. Read the FAQ about ImTextureID!
+// [X] Renderer: User texture binding. Use 'ID3D11ShaderResourceView*' as texture identifier. Read the FAQ about ImTextureID/ImTextureRef!
// [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset).
+// [X] Renderer: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures).
// [X] Renderer: Expose selected render state for draw callbacks to use. Access in '(ImGui_ImplXXXX_RenderState*)GetPlatformIO().Renderer_RenderState'.
// [X] Renderer: Multi-viewport support (multiple windows). Enable with 'io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable'.
@@ -18,6 +19,9 @@
// CHANGELOG
// (minor and older changes stripped away, please see git history for details)
// 2025-XX-XX: Platform: Added support for multiple windows via the ImGuiPlatformIO interface.
+// 2025-09-18: Call platform_io.ClearRendererHandlers() on shutdown.
+// 2025-06-11: DirectX11: Added support for ImGuiBackendFlags_RendererHasTextures, for dynamic font atlas.
+// 2025-05-07: DirectX11: Honor draw_data->FramebufferScale to allow for custom backends and experiment using it (consistently with other renderer backends, even though in normal condition it is not set under Windows).
// 2025-02-24: [Docking] Added undocumented ImGui_ImplDX11_SetSwapChainDescs() to configure swap chain creation for secondary viewports.
// 2025-01-06: DirectX11: Expose VertexConstantBuffer in ImGui_ImplDX11_RenderState. Reset projection matrix in ImDrawCallback_ResetRenderState handler.
// 2024-10-07: DirectX11: Changed default texture sampler to Clamp instead of Repeat/Wrap.
@@ -52,7 +56,19 @@
#pragma comment(lib, "d3dcompiler") // Automatically link with d3dcompiler.lib as we are using D3DCompile() below.
#endif
+// Clang/GCC warnings with -Weverything
+#if defined(__clang__)
+#pragma clang diagnostic ignored "-Wold-style-cast" // warning: use of old-style cast // yes, they are more terse.
+#pragma clang diagnostic ignored "-Wsign-conversion" // warning: implicit conversion changes signedness
+#endif
+
// DirectX11 data
+struct ImGui_ImplDX11_Texture
+{
+ ID3D11Texture2D* pTexture;
+ ID3D11ShaderResourceView* pTextureView;
+};
+
struct ImGui_ImplDX11_Data
{
ID3D11Device* pd3dDevice;
@@ -64,8 +80,7 @@ struct ImGui_ImplDX11_Data
ID3D11InputLayout* pInputLayout;
ID3D11Buffer* pVertexConstantBuffer;
ID3D11PixelShader* pPixelShader;
- ID3D11SamplerState* pFontSampler;
- ID3D11ShaderResourceView* pFontTextureView;
+ ID3D11SamplerState* pTexSamplerLinear;
ID3D11RasterizerState* pRasterizerState;
ID3D11BlendState* pBlendState;
ID3D11DepthStencilState* pDepthStencilState;
@@ -93,14 +108,14 @@ static void ImGui_ImplDX11_InitMultiViewportSupport();
static void ImGui_ImplDX11_ShutdownMultiViewportSupport();
// Functions
-static void ImGui_ImplDX11_SetupRenderState(ImDrawData* draw_data, ID3D11DeviceContext* device_ctx)
+static void ImGui_ImplDX11_SetupRenderState(const ImDrawData* draw_data, ID3D11DeviceContext* device_ctx)
{
ImGui_ImplDX11_Data* bd = ImGui_ImplDX11_GetBackendData();
// Setup viewport
D3D11_VIEWPORT vp = {};
- vp.Width = draw_data->DisplaySize.x;
- vp.Height = draw_data->DisplaySize.y;
+ vp.Width = draw_data->DisplaySize.x * draw_data->FramebufferScale.x;
+ vp.Height = draw_data->DisplaySize.y * draw_data->FramebufferScale.y;
vp.MinDepth = 0.0f;
vp.MaxDepth = 1.0f;
vp.TopLeftX = vp.TopLeftY = 0;
@@ -137,7 +152,7 @@ static void ImGui_ImplDX11_SetupRenderState(ImDrawData* draw_data, ID3D11DeviceC
device_ctx->VSSetShader(bd->pVertexShader, nullptr, 0);
device_ctx->VSSetConstantBuffers(0, 1, &bd->pVertexConstantBuffer);
device_ctx->PSSetShader(bd->pPixelShader, nullptr, 0);
- device_ctx->PSSetSamplers(0, 1, &bd->pFontSampler);
+ device_ctx->PSSetSamplers(0, 1, &bd->pTexSamplerLinear);
device_ctx->GSSetShader(nullptr, nullptr, 0);
device_ctx->HSSetShader(nullptr, nullptr, 0); // In theory we should backup and restore this as well.. very infrequently used..
device_ctx->DSSetShader(nullptr, nullptr, 0); // In theory we should backup and restore this as well.. very infrequently used..
@@ -160,6 +175,13 @@ void ImGui_ImplDX11_RenderDrawData(ImDrawData* draw_data)
ImGui_ImplDX11_Data* bd = ImGui_ImplDX11_GetBackendData();
ID3D11DeviceContext* device = bd->pd3dDeviceContext;
+ // Catch up with texture updates. Most of the times, the list will have 1 element with an OK status, aka nothing to do.
+ // (This almost always points to ImGui::GetPlatformIO().Textures[] but is part of ImDrawData to allow overriding or disabling texture updates).
+ if (draw_data->Textures != nullptr)
+ for (ImTextureData* tex : *draw_data->Textures)
+ if (tex->Status != ImTextureStatus_OK)
+ ImGui_ImplDX11_UpdateTexture(tex);
+
// Create and grow vertex/index buffers if needed
if (!bd->pVB || bd->VertexBufferSize < draw_data->TotalVtxCount)
{
@@ -195,9 +217,8 @@ void ImGui_ImplDX11_RenderDrawData(ImDrawData* draw_data)
return;
ImDrawVert* vtx_dst = (ImDrawVert*)vtx_resource.pData;
ImDrawIdx* idx_dst = (ImDrawIdx*)idx_resource.pData;
- for (int n = 0; n < draw_data->CmdListsCount; n++)
+ for (const ImDrawList* draw_list : draw_data->CmdLists)
{
- const ImDrawList* draw_list = draw_data->CmdLists[n];
memcpy(vtx_dst, draw_list->VtxBuffer.Data, draw_list->VtxBuffer.Size * sizeof(ImDrawVert));
memcpy(idx_dst, draw_list->IdxBuffer.Data, draw_list->IdxBuffer.Size * sizeof(ImDrawIdx));
vtx_dst += draw_list->VtxBuffer.Size;
@@ -259,7 +280,7 @@ void ImGui_ImplDX11_RenderDrawData(ImDrawData* draw_data)
ImGui_ImplDX11_RenderState render_state;
render_state.Device = bd->pd3dDevice;
render_state.DeviceContext = bd->pd3dDeviceContext;
- render_state.SamplerDefault = bd->pFontSampler;
+ render_state.SamplerDefault = bd->pTexSamplerLinear;
render_state.VertexConstantBuffer = bd->pVertexConstantBuffer;
platform_io.Renderer_RenderState = &render_state;
@@ -268,9 +289,9 @@ void ImGui_ImplDX11_RenderDrawData(ImDrawData* draw_data)
int global_idx_offset = 0;
int global_vtx_offset = 0;
ImVec2 clip_off = draw_data->DisplayPos;
- for (int n = 0; n < draw_data->CmdListsCount; n++)
+ ImVec2 clip_scale = draw_data->FramebufferScale;
+ for (const ImDrawList* draw_list : draw_data->CmdLists)
{
- const ImDrawList* draw_list = draw_data->CmdLists[n];
for (int cmd_i = 0; cmd_i < draw_list->CmdBuffer.Size; cmd_i++)
{
const ImDrawCmd* pcmd = &draw_list->CmdBuffer[cmd_i];
@@ -286,8 +307,8 @@ void ImGui_ImplDX11_RenderDrawData(ImDrawData* draw_data)
else
{
// Project scissor/clipping rectangles into framebuffer space
- ImVec2 clip_min(pcmd->ClipRect.x - clip_off.x, pcmd->ClipRect.y - clip_off.y);
- ImVec2 clip_max(pcmd->ClipRect.z - clip_off.x, pcmd->ClipRect.w - clip_off.y);
+ ImVec2 clip_min((pcmd->ClipRect.x - clip_off.x) * clip_scale.x, (pcmd->ClipRect.y - clip_off.y) * clip_scale.y);
+ ImVec2 clip_max((pcmd->ClipRect.z - clip_off.x) * clip_scale.x, (pcmd->ClipRect.w - clip_off.y) * clip_scale.y);
if (clip_max.x <= clip_min.x || clip_max.y <= clip_min.y)
continue;
@@ -326,21 +347,39 @@ void ImGui_ImplDX11_RenderDrawData(ImDrawData* draw_data)
device->IASetInputLayout(old.InputLayout); if (old.InputLayout) old.InputLayout->Release();
}
-static void ImGui_ImplDX11_CreateFontsTexture()
+static void ImGui_ImplDX11_DestroyTexture(ImTextureData* tex)
{
- // Build texture atlas
- ImGuiIO& io = ImGui::GetIO();
- ImGui_ImplDX11_Data* bd = ImGui_ImplDX11_GetBackendData();
- unsigned char* pixels;
- int width, height;
- io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
-
- // Upload texture to graphics system
+ if (ImGui_ImplDX11_Texture* backend_tex = (ImGui_ImplDX11_Texture*)tex->BackendUserData)
{
+ IM_ASSERT(backend_tex->pTextureView == (ID3D11ShaderResourceView*)(intptr_t)tex->TexID);
+ backend_tex->pTextureView->Release();
+ backend_tex->pTexture->Release();
+ IM_DELETE(backend_tex);
+
+ // Clear identifiers and mark as destroyed (in order to allow e.g. calling InvalidateDeviceObjects while running)
+ tex->SetTexID(ImTextureID_Invalid);
+ tex->BackendUserData = nullptr;
+ }
+ tex->SetStatus(ImTextureStatus_Destroyed);
+}
+
+void ImGui_ImplDX11_UpdateTexture(ImTextureData* tex)
+{
+ ImGui_ImplDX11_Data* bd = ImGui_ImplDX11_GetBackendData();
+ if (tex->Status == ImTextureStatus_WantCreate)
+ {
+ // Create and upload new texture to graphics system
+ //IMGUI_DEBUG_LOG("UpdateTexture #%03d: WantCreate %dx%d\n", tex->UniqueID, tex->Width, tex->Height);
+ IM_ASSERT(tex->TexID == ImTextureID_Invalid && tex->BackendUserData == nullptr);
+ IM_ASSERT(tex->Format == ImTextureFormat_RGBA32);
+ unsigned int* pixels = (unsigned int*)tex->GetPixels();
+ ImGui_ImplDX11_Texture* backend_tex = IM_NEW(ImGui_ImplDX11_Texture)();
+
+ // Create texture
D3D11_TEXTURE2D_DESC desc;
ZeroMemory(&desc, sizeof(desc));
- desc.Width = width;
- desc.Height = height;
+ desc.Width = (UINT)tex->Width;
+ desc.Height = (UINT)tex->Height;
desc.MipLevels = 1;
desc.ArraySize = 1;
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
@@ -348,14 +387,12 @@ static void ImGui_ImplDX11_CreateFontsTexture()
desc.Usage = D3D11_USAGE_DEFAULT;
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
desc.CPUAccessFlags = 0;
-
- ID3D11Texture2D* pTexture = nullptr;
D3D11_SUBRESOURCE_DATA subResource;
subResource.pSysMem = pixels;
subResource.SysMemPitch = desc.Width * 4;
subResource.SysMemSlicePitch = 0;
- bd->pd3dDevice->CreateTexture2D(&desc, &subResource, &pTexture);
- IM_ASSERT(pTexture != nullptr);
+ bd->pd3dDevice->CreateTexture2D(&desc, &subResource, &backend_tex->pTexture);
+ IM_ASSERT(backend_tex->pTexture != nullptr && "Backend failed to create texture!");
// Create texture view
D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc;
@@ -364,23 +401,29 @@ static void ImGui_ImplDX11_CreateFontsTexture()
srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
srvDesc.Texture2D.MipLevels = desc.MipLevels;
srvDesc.Texture2D.MostDetailedMip = 0;
- bd->pd3dDevice->CreateShaderResourceView(pTexture, &srvDesc, &bd->pFontTextureView);
- pTexture->Release();
+ bd->pd3dDevice->CreateShaderResourceView(backend_tex->pTexture, &srvDesc, &backend_tex->pTextureView);
+ IM_ASSERT(backend_tex->pTextureView != nullptr && "Backend failed to create texture!");
+
+ // Store identifiers
+ tex->SetTexID((ImTextureID)(intptr_t)backend_tex->pTextureView);
+ tex->SetStatus(ImTextureStatus_OK);
+ tex->BackendUserData = backend_tex;
}
-
- // Store our identifier
- io.Fonts->SetTexID((ImTextureID)bd->pFontTextureView);
-}
-
-static void ImGui_ImplDX11_DestroyFontsTexture()
-{
- ImGui_ImplDX11_Data* bd = ImGui_ImplDX11_GetBackendData();
- if (bd->pFontTextureView)
+ else if (tex->Status == ImTextureStatus_WantUpdates)
{
- bd->pFontTextureView->Release();
- bd->pFontTextureView = nullptr;
- ImGui::GetIO().Fonts->SetTexID(0); // We copied data->pFontTextureView to io.Fonts->TexID so let's clear that as well.
+ // Update selected blocks. We only ever write to textures regions which have never been used before!
+ // This backend choose to use tex->Updates[] but you can use tex->UpdateRect to upload a single region.
+ ImGui_ImplDX11_Texture* backend_tex = (ImGui_ImplDX11_Texture*)tex->BackendUserData;
+ IM_ASSERT(backend_tex->pTextureView == (ID3D11ShaderResourceView*)(intptr_t)tex->TexID);
+ for (ImTextureRect& r : tex->Updates)
+ {
+ D3D11_BOX box = { (UINT)r.x, (UINT)r.y, (UINT)0, (UINT)(r.x + r.w), (UINT)(r.y + r .h), (UINT)1 };
+ bd->pd3dDeviceContext->UpdateSubresource(backend_tex->pTexture, 0, &box, tex->GetPixelsAt(r.x, r.y), (UINT)tex->GetPitch(), 0);
+ }
+ tex->SetStatus(ImTextureStatus_OK);
}
+ if (tex->Status == ImTextureStatus_WantDestroy && tex->UnusedFrames > 0)
+ ImGui_ImplDX11_DestroyTexture(tex);
}
bool ImGui_ImplDX11_CreateDeviceObjects()
@@ -388,8 +431,7 @@ bool ImGui_ImplDX11_CreateDeviceObjects()
ImGui_ImplDX11_Data* bd = ImGui_ImplDX11_GetBackendData();
if (!bd->pd3dDevice)
return false;
- if (bd->pFontSampler)
- ImGui_ImplDX11_InvalidateDeviceObjects();
+ ImGui_ImplDX11_InvalidateDeviceObjects();
// By using D3DCompile() from / d3dcompiler.lib, we introduce a dependency to a given version of d3dcompiler_XX.dll (see D3DCOMPILER_DLL_A)
// If you would like to use this DX11 sample code but remove this dependency you can:
@@ -545,11 +587,9 @@ bool ImGui_ImplDX11_CreateDeviceObjects()
desc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
desc.MinLOD = 0.f;
desc.MaxLOD = 0.f;
- bd->pd3dDevice->CreateSamplerState(&desc, &bd->pFontSampler);
+ bd->pd3dDevice->CreateSamplerState(&desc, &bd->pTexSamplerLinear);
}
- ImGui_ImplDX11_CreateFontsTexture();
-
return true;
}
@@ -559,9 +599,12 @@ void ImGui_ImplDX11_InvalidateDeviceObjects()
if (!bd->pd3dDevice)
return;
- ImGui_ImplDX11_DestroyFontsTexture();
+ // Destroy all textures
+ for (ImTextureData* tex : ImGui::GetPlatformIO().Textures)
+ if (tex->RefCount == 1)
+ ImGui_ImplDX11_DestroyTexture(tex);
- if (bd->pFontSampler) { bd->pFontSampler->Release(); bd->pFontSampler = nullptr; }
+ if (bd->pTexSamplerLinear) { bd->pTexSamplerLinear->Release(); bd->pTexSamplerLinear = nullptr; }
if (bd->pIB) { bd->pIB->Release(); bd->pIB = nullptr; }
if (bd->pVB) { bd->pVB->Release(); bd->pVB = nullptr; }
if (bd->pBlendState) { bd->pBlendState->Release(); bd->pBlendState = nullptr; }
@@ -584,8 +627,12 @@ bool ImGui_ImplDX11_Init(ID3D11Device* device, ID3D11DeviceContext* device_co
io.BackendRendererUserData = (void*)bd;
io.BackendRendererName = "imgui_impl_dx11";
io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes.
+ io.BackendFlags |= ImGuiBackendFlags_RendererHasTextures; // We can honor ImGuiPlatformIO::Textures[] requests during render.
io.BackendFlags |= ImGuiBackendFlags_RendererHasViewports; // We can create multi-viewports on the Renderer side (optional)
+ ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
+ platform_io.Renderer_TextureMaxWidth = platform_io.Renderer_TextureMaxHeight = D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION;
+
// Get factory from device
IDXGIDevice* pDXGIDevice = nullptr;
IDXGIAdapter* pDXGIAdapter = nullptr;
@@ -614,15 +661,18 @@ void ImGui_ImplDX11_Shutdown()
ImGui_ImplDX11_Data* bd = ImGui_ImplDX11_GetBackendData();
IM_ASSERT(bd != nullptr && "No renderer backend to shutdown, or already shutdown?");
ImGuiIO& io = ImGui::GetIO();
+ ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
ImGui_ImplDX11_ShutdownMultiViewportSupport();
ImGui_ImplDX11_InvalidateDeviceObjects();
if (bd->pFactory) { bd->pFactory->Release(); }
if (bd->pd3dDevice) { bd->pd3dDevice->Release(); }
if (bd->pd3dDeviceContext) { bd->pd3dDeviceContext->Release(); }
+
io.BackendRendererName = nullptr;
io.BackendRendererUserData = nullptr;
- io.BackendFlags &= ~(ImGuiBackendFlags_RendererHasVtxOffset | ImGuiBackendFlags_RendererHasViewports);
+ io.BackendFlags &= ~(ImGuiBackendFlags_RendererHasVtxOffset | ImGuiBackendFlags_RendererHasTextures | ImGuiBackendFlags_RendererHasViewports);
+ platform_io.ClearRendererHandlers();
IM_DELETE(bd);
}
@@ -631,8 +681,9 @@ void ImGui_ImplDX11_NewFrame()
ImGui_ImplDX11_Data* bd = ImGui_ImplDX11_GetBackendData();
IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplDX11_Init()?");
- if (!bd->pFontSampler)
- ImGui_ImplDX11_CreateDeviceObjects();
+ if (!bd->pVertexShader)
+ if (!ImGui_ImplDX11_CreateDeviceObjects())
+ IM_ASSERT(0 && "ImGui_ImplDX11_CreateDeviceObjects() failed!");
}
//--------------------------------------------------------------------------------------------------------
@@ -687,6 +738,7 @@ static void ImGui_ImplDX11_CreateWindow(ImGuiViewport* viewport)
break;
}
IM_ASSERT(SUCCEEDED(hr));
+ bd->pFactory->MakeWindowAssociation(hwnd, DXGI_MWA_NO_ALT_ENTER | DXGI_MWA_NO_WINDOW_CHANGES); // Disable e.g. Alt+Enter
// Create the render target
if (vd->SwapChain != nullptr)
diff --git a/backends/imgui_impl_dx11.h b/backends/imgui_impl_dx11.h
index acdd62cc5..6f7798d76 100644
--- a/backends/imgui_impl_dx11.h
+++ b/backends/imgui_impl_dx11.h
@@ -2,8 +2,9 @@
// This needs to be used along with a Platform Backend (e.g. Win32)
// Implemented features:
-// [X] Renderer: User texture binding. Use 'ID3D11ShaderResourceView*' as ImTextureID. Read the FAQ about ImTextureID!
+// [X] Renderer: User texture binding. Use 'ID3D11ShaderResourceView*' as texture identifier. Read the FAQ about ImTextureID/ImTextureRef!
// [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset).
+// [X] Renderer: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures).
// [X] Renderer: Expose selected render state for draw callbacks to use. Access in '(ImGui_ImplXXXX_RenderState*)GetPlatformIO().Renderer_RenderState'.
// [X] Renderer: Multi-viewport support (multiple windows). Enable with 'io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable'.
@@ -34,6 +35,9 @@ IMGUI_IMPL_API void ImGui_ImplDX11_RenderDrawData(ImDrawData* draw_data);
IMGUI_IMPL_API bool ImGui_ImplDX11_CreateDeviceObjects();
IMGUI_IMPL_API void ImGui_ImplDX11_InvalidateDeviceObjects();
+// (Advanced) Use e.g. if you need to precisely control the timing of texture updates (e.g. for staged rendering), by setting ImDrawData::Textures = NULL to handle this manually.
+IMGUI_IMPL_API void ImGui_ImplDX11_UpdateTexture(ImTextureData* tex);
+
// [BETA] Selected render state data shared with callbacks.
// This is temporarily stored in GetPlatformIO().Renderer_RenderState during the ImGui_ImplDX11_RenderDrawData() call.
// (Please open an issue if you feel you need access to more data)
diff --git a/backends/imgui_impl_dx12.cpp b/backends/imgui_impl_dx12.cpp
index 72ec30956..931e8e2fb 100644
--- a/backends/imgui_impl_dx12.cpp
+++ b/backends/imgui_impl_dx12.cpp
@@ -2,8 +2,9 @@
// This needs to be used along with a Platform Backend (e.g. Win32)
// Implemented features:
-// [X] Renderer: User texture binding. Use 'D3D12_GPU_DESCRIPTOR_HANDLE' as ImTextureID. Read the FAQ about ImTextureID!
+// [X] Renderer: User texture binding. Use 'D3D12_GPU_DESCRIPTOR_HANDLE' as texture identifier. Read the FAQ about ImTextureID/ImTextureRef!
// [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset).
+// [X] Renderer: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures).
// [X] Renderer: Expose selected render state for draw callbacks to use. Access in '(ImGui_ImplXXXX_RenderState*)GetPlatformIO().Renderer_RenderState'.
// [X] Renderer: Multi-viewport support (multiple windows). Enable with 'io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable'.
// FIXME: The transition from removing a viewport and moving the window in an existing hosted viewport tends to flicker.
@@ -22,6 +23,13 @@
// CHANGELOG
// (minor and older changes stripped away, please see git history for details)
// 2025-XX-XX: Platform: Added support for multiple windows via the ImGuiPlatformIO interface.
+// 2025-09-29: DirectX12: Rework synchronization logic. (#8961)
+// 2025-09-29: DirectX12: Enable swapchain tearing to eliminate viewports framerate throttling. (#8965)
+// 2025-09-29: DirectX12: Reuse a command list and allocator for texture uploads instead of recreating them each time. (#8963)
+// 2025-09-18: Call platform_io.ClearRendererHandlers() on shutdown.
+// 2025-06-19: Fixed build on MinGW. (#8702, #4594)
+// 2025-06-11: DirectX12: Added support for ImGuiBackendFlags_RendererHasTextures, for dynamic font atlas.
+// 2025-05-07: DirectX12: Honor draw_data->FramebufferScale to allow for custom backends and experiment using it (consistently with other renderer backends, even though in normal condition it is not set under Windows).
// 2025-02-24: DirectX12: Fixed an issue where ImGui_ImplDX12_Init() signature change from 2024-11-15 combined with change from 2025-01-15 made legacy ImGui_ImplDX12_Init() crash. (#8429)
// 2025-01-15: DirectX12: Texture upload use the command queue provided in ImGui_ImplDX12_InitInfo instead of creating its own.
// 2024-12-09: DirectX12: Let user specifies the DepthStencilView format by setting ImGui_ImplDX12_InitInfo::DSVFormat.
@@ -55,12 +63,21 @@
// DirectX
#include
-#include
+#include
#include
#ifdef _MSC_VER
#pragma comment(lib, "d3dcompiler") // Automatically link with d3dcompiler.lib as we are using D3DCompile() below.
#endif
+// Clang/GCC warnings with -Weverything
+#if defined(__clang__)
+#pragma clang diagnostic ignored "-Wold-style-cast" // warning: use of old-style cast // yes, they are more terse.
+#pragma clang diagnostic ignored "-Wsign-conversion" // warning: implicit conversion changes signedness
+#endif
+
+// MinGW workaround, see #4594
+typedef decltype(D3D12SerializeRootSignature) *_PFN_D3D12_SERIALIZE_ROOT_SIGNATURE;
+
// DirectX12 data
struct ImGui_ImplDX12_RenderBuffers;
@@ -76,6 +93,7 @@ struct ImGui_ImplDX12_Texture
struct ImGui_ImplDX12_Data
{
ImGui_ImplDX12_InitInfo InitInfo;
+ IDXGIFactory5* pdxgiFactory;
ID3D12Device* pd3dDevice;
ID3D12RootSignature* pRootSignature;
ID3D12PipelineState* pPipelineState;
@@ -84,10 +102,19 @@ struct ImGui_ImplDX12_Data
DXGI_FORMAT RTVFormat;
DXGI_FORMAT DSVFormat;
ID3D12DescriptorHeap* pd3dSrvDescHeap;
+ ID3D12Fence* Fence;
+ UINT64 FenceLastSignaledValue;
+ HANDLE FenceEvent;
UINT numFramesInFlight;
- ImGui_ImplDX12_Texture FontTexture;
+ bool tearingSupport;
bool LegacySingleDescriptorUsed;
+ ID3D12CommandAllocator* pTexCmdAllocator;
+ ID3D12GraphicsCommandList* pTexCmdList;
+
+ ImGui_ImplDX12_RenderBuffers* pFrameResources;
+ UINT frameIndex;
+
ImGui_ImplDX12_Data() { memset((void*)this, 0, sizeof(*this)); }
};
@@ -110,6 +137,7 @@ struct ImGui_ImplDX12_RenderBuffers
// Buffers used for secondary viewports created by the multi-viewports systems
struct ImGui_ImplDX12_FrameContext
{
+ UINT64 FenceValue;
ID3D12CommandAllocator* CommandAllocator;
ID3D12Resource* RenderTarget;
D3D12_CPU_DESCRIPTOR_HANDLE RenderTargetCpuDescriptors;
@@ -125,9 +153,7 @@ struct ImGui_ImplDX12_ViewportData
ID3D12GraphicsCommandList* CommandList;
ID3D12DescriptorHeap* RtvDescHeap;
IDXGISwapChain3* SwapChain;
- ID3D12Fence* Fence;
- UINT64 FenceSignaledValue;
- HANDLE FenceEvent;
+ HANDLE SwapChainWaitableObject;
UINT NumFramesInFlight;
ImGui_ImplDX12_FrameContext* FrameCtx;
@@ -141,16 +167,15 @@ struct ImGui_ImplDX12_ViewportData
CommandList = nullptr;
RtvDescHeap = nullptr;
SwapChain = nullptr;
- Fence = nullptr;
- FenceSignaledValue = 0;
- FenceEvent = nullptr;
+ SwapChainWaitableObject = nullptr;
NumFramesInFlight = num_frames_in_flight;
FrameCtx = new ImGui_ImplDX12_FrameContext[NumFramesInFlight];
- FrameIndex = UINT_MAX;
+ FrameIndex = 0;
FrameRenderBuffers = new ImGui_ImplDX12_RenderBuffers[NumFramesInFlight];
for (UINT i = 0; i < NumFramesInFlight; ++i)
{
+ FrameCtx[i].FenceValue = 0;
FrameCtx[i].CommandAllocator = nullptr;
FrameCtx[i].RenderTarget = nullptr;
@@ -166,8 +191,7 @@ struct ImGui_ImplDX12_ViewportData
IM_ASSERT(CommandQueue == nullptr && CommandList == nullptr);
IM_ASSERT(RtvDescHeap == nullptr);
IM_ASSERT(SwapChain == nullptr);
- IM_ASSERT(Fence == nullptr);
- IM_ASSERT(FenceEvent == nullptr);
+ IM_ASSERT(SwapChainWaitableObject == nullptr);
for (UINT i = 0; i < NumFramesInFlight; ++i)
{
@@ -186,8 +210,8 @@ struct VERTEX_CONSTANT_BUFFER_DX12
};
// Forward Declarations
-static void ImGui_ImplDX12_InitPlatformInterface();
-static void ImGui_ImplDX12_ShutdownPlatformInterface();
+static void ImGui_ImplDX12_InitMultiViewportSupport();
+static void ImGui_ImplDX12_ShutdownMultiViewportSupport();
// Functions
static void ImGui_ImplDX12_SetupRenderState(ImDrawData* draw_data, ID3D12GraphicsCommandList* command_list, ImGui_ImplDX12_RenderBuffers* fr)
@@ -214,8 +238,8 @@ static void ImGui_ImplDX12_SetupRenderState(ImDrawData* draw_data, ID3D12Graphic
// Setup viewport
D3D12_VIEWPORT vp = {};
- vp.Width = draw_data->DisplaySize.x;
- vp.Height = draw_data->DisplaySize.y;
+ vp.Width = draw_data->DisplaySize.x * draw_data->FramebufferScale.x;
+ vp.Height = draw_data->DisplaySize.y * draw_data->FramebufferScale.y;
vp.MinDepth = 0.0f;
vp.MaxDepth = 1.0f;
vp.TopLeftX = vp.TopLeftY = 0.0f;
@@ -259,6 +283,13 @@ void ImGui_ImplDX12_RenderDrawData(ImDrawData* draw_data, ID3D12GraphicsCommandL
if (draw_data->DisplaySize.x <= 0.0f || draw_data->DisplaySize.y <= 0.0f)
return;
+ // Catch up with texture updates. Most of the times, the list will have 1 element with an OK status, aka nothing to do.
+ // (This almost always points to ImGui::GetPlatformIO().Textures[] but is part of ImDrawData to allow overriding or disabling texture updates).
+ if (draw_data->Textures != nullptr)
+ for (ImTextureData* tex : *draw_data->Textures)
+ if (tex->Status != ImTextureStatus_OK)
+ ImGui_ImplDX12_UpdateTexture(tex);
+
// FIXME: We are assuming that this only gets called once per frame!
ImGui_ImplDX12_Data* bd = ImGui_ImplDX12_GetBackendData();
ImGui_ImplDX12_ViewportData* vd = (ImGui_ImplDX12_ViewportData*)draw_data->OwnerViewport->RendererUserData;
@@ -319,9 +350,8 @@ void ImGui_ImplDX12_RenderDrawData(ImDrawData* draw_data, ID3D12GraphicsCommandL
return;
ImDrawVert* vtx_dst = (ImDrawVert*)vtx_resource;
ImDrawIdx* idx_dst = (ImDrawIdx*)idx_resource;
- for (int n = 0; n < draw_data->CmdListsCount; n++)
+ for (const ImDrawList* draw_list : draw_data->CmdLists)
{
- const ImDrawList* draw_list = draw_data->CmdLists[n];
memcpy(vtx_dst, draw_list->VtxBuffer.Data, draw_list->VtxBuffer.Size * sizeof(ImDrawVert));
memcpy(idx_dst, draw_list->IdxBuffer.Data, draw_list->IdxBuffer.Size * sizeof(ImDrawIdx));
vtx_dst += draw_list->VtxBuffer.Size;
@@ -351,9 +381,9 @@ void ImGui_ImplDX12_RenderDrawData(ImDrawData* draw_data, ID3D12GraphicsCommandL
int global_vtx_offset = 0;
int global_idx_offset = 0;
ImVec2 clip_off = draw_data->DisplayPos;
- for (int n = 0; n < draw_data->CmdListsCount; n++)
+ ImVec2 clip_scale = draw_data->FramebufferScale;
+ for (const ImDrawList* draw_list : draw_data->CmdLists)
{
- const ImDrawList* draw_list = draw_data->CmdLists[n];
for (int cmd_i = 0; cmd_i < draw_list->CmdBuffer.Size; cmd_i++)
{
const ImDrawCmd* pcmd = &draw_list->CmdBuffer[cmd_i];
@@ -369,8 +399,8 @@ void ImGui_ImplDX12_RenderDrawData(ImDrawData* draw_data, ID3D12GraphicsCommandL
else
{
// Project scissor/clipping rectangles into framebuffer space
- ImVec2 clip_min(pcmd->ClipRect.x - clip_off.x, pcmd->ClipRect.y - clip_off.y);
- ImVec2 clip_max(pcmd->ClipRect.z - clip_off.x, pcmd->ClipRect.w - clip_off.y);
+ ImVec2 clip_min((pcmd->ClipRect.x - clip_off.x) * clip_scale.x, (pcmd->ClipRect.y - clip_off.y) * clip_scale.y);
+ ImVec2 clip_max((pcmd->ClipRect.z - clip_off.x) * clip_scale.x, (pcmd->ClipRect.w - clip_off.y) * clip_scale.y);
if (clip_max.x <= clip_min.x || clip_max.y <= clip_min.y)
continue;
@@ -391,18 +421,39 @@ void ImGui_ImplDX12_RenderDrawData(ImDrawData* draw_data, ID3D12GraphicsCommandL
platform_io.Renderer_RenderState = nullptr;
}
-static void ImGui_ImplDX12_CreateFontsTexture()
+static void ImGui_ImplDX12_DestroyTexture(ImTextureData* tex)
{
- // Build texture atlas
- ImGuiIO& io = ImGui::GetIO();
- ImGui_ImplDX12_Data* bd = ImGui_ImplDX12_GetBackendData();
- unsigned char* pixels;
- int width, height;
- io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
-
- // Upload texture to graphics system
- ImGui_ImplDX12_Texture* font_tex = &bd->FontTexture;
+ if (ImGui_ImplDX12_Texture* backend_tex = (ImGui_ImplDX12_Texture*)tex->BackendUserData)
{
+ IM_ASSERT(backend_tex->hFontSrvGpuDescHandle.ptr == (UINT64)tex->TexID);
+ ImGui_ImplDX12_Data* bd = ImGui_ImplDX12_GetBackendData();
+ bd->InitInfo.SrvDescriptorFreeFn(&bd->InitInfo, backend_tex->hFontSrvCpuDescHandle, backend_tex->hFontSrvGpuDescHandle);
+ SafeRelease(backend_tex->pTextureResource);
+ backend_tex->hFontSrvCpuDescHandle.ptr = 0;
+ backend_tex->hFontSrvGpuDescHandle.ptr = 0;
+ IM_DELETE(backend_tex);
+
+ // Clear identifiers and mark as destroyed (in order to allow e.g. calling InvalidateDeviceObjects while running)
+ tex->SetTexID(ImTextureID_Invalid);
+ tex->BackendUserData = nullptr;
+ }
+ tex->SetStatus(ImTextureStatus_Destroyed);
+}
+
+void ImGui_ImplDX12_UpdateTexture(ImTextureData* tex)
+{
+ ImGui_ImplDX12_Data* bd = ImGui_ImplDX12_GetBackendData();
+ bool need_barrier_before_copy = true; // Do we need a resource barrier before we copy new data in?
+
+ if (tex->Status == ImTextureStatus_WantCreate)
+ {
+ // Create and upload new texture to graphics system
+ //IMGUI_DEBUG_LOG("UpdateTexture #%03d: WantCreate %dx%d\n", tex->UniqueID, tex->Width, tex->Height);
+ IM_ASSERT(tex->TexID == ImTextureID_Invalid && tex->BackendUserData == nullptr);
+ IM_ASSERT(tex->Format == ImTextureFormat_RGBA32);
+ ImGui_ImplDX12_Texture* backend_tex = IM_NEW(ImGui_ImplDX12_Texture)();
+ bd->InitInfo.SrvDescriptorAllocFn(&bd->InitInfo, &backend_tex->hFontSrvCpuDescHandle, &backend_tex->hFontSrvGpuDescHandle); // Allocate a desctriptor handle
+
D3D12_HEAP_PROPERTIES props = {};
props.Type = D3D12_HEAP_TYPE_DEFAULT;
props.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
@@ -412,8 +463,8 @@ static void ImGui_ImplDX12_CreateFontsTexture()
ZeroMemory(&desc, sizeof(desc));
desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
desc.Alignment = 0;
- desc.Width = width;
- desc.Height = height;
+ desc.Width = tex->Width;
+ desc.Height = tex->Height;
desc.DepthOrArraySize = 1;
desc.MipLevels = 1;
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
@@ -426,8 +477,47 @@ static void ImGui_ImplDX12_CreateFontsTexture()
bd->pd3dDevice->CreateCommittedResource(&props, D3D12_HEAP_FLAG_NONE, &desc,
D3D12_RESOURCE_STATE_COPY_DEST, nullptr, IID_PPV_ARGS(&pTexture));
- UINT upload_pitch = (width * 4 + D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1u) & ~(D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1u);
- UINT upload_size = height * upload_pitch;
+ // Create SRV
+ D3D12_SHADER_RESOURCE_VIEW_DESC srvDesc;
+ ZeroMemory(&srvDesc, sizeof(srvDesc));
+ srvDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
+ srvDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
+ srvDesc.Texture2D.MipLevels = desc.MipLevels;
+ srvDesc.Texture2D.MostDetailedMip = 0;
+ srvDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
+ bd->pd3dDevice->CreateShaderResourceView(pTexture, &srvDesc, backend_tex->hFontSrvCpuDescHandle);
+ SafeRelease(backend_tex->pTextureResource);
+ backend_tex->pTextureResource = pTexture;
+
+ // Store identifiers
+ tex->SetTexID((ImTextureID)backend_tex->hFontSrvGpuDescHandle.ptr);
+ tex->BackendUserData = backend_tex;
+ need_barrier_before_copy = false; // Because this is a newly-created texture it will be in D3D12_RESOURCE_STATE_COMMON and thus we don't need a barrier
+ // We don't set tex->Status to ImTextureStatus_OK to let the code fallthrough below.
+ }
+
+ if (tex->Status == ImTextureStatus_WantCreate || tex->Status == ImTextureStatus_WantUpdates)
+ {
+ ImGui_ImplDX12_Texture* backend_tex = (ImGui_ImplDX12_Texture*)tex->BackendUserData;
+ IM_ASSERT(tex->Format == ImTextureFormat_RGBA32);
+
+ // We could use the smaller rect on _WantCreate but using the full rect allows us to clear the texture.
+ // FIXME-OPT: Uploading single box even when using ImTextureStatus_WantUpdates. Could use tex->Updates[]
+ // - Copy all blocks contiguously in upload buffer.
+ // - Barrier before copy, submit all CopyTextureRegion(), barrier after copy.
+ const int upload_x = (tex->Status == ImTextureStatus_WantCreate) ? 0 : tex->UpdateRect.x;
+ const int upload_y = (tex->Status == ImTextureStatus_WantCreate) ? 0 : tex->UpdateRect.y;
+ const int upload_w = (tex->Status == ImTextureStatus_WantCreate) ? tex->Width : tex->UpdateRect.w;
+ const int upload_h = (tex->Status == ImTextureStatus_WantCreate) ? tex->Height : tex->UpdateRect.h;
+
+ // Update full texture or selected blocks. We only ever write to textures regions which have never been used before!
+ // This backend choose to use tex->UpdateRect but you can use tex->Updates[] to upload individual regions.
+ UINT upload_pitch_src = upload_w * tex->BytesPerPixel;
+ UINT upload_pitch_dst = (upload_pitch_src + D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1u) & ~(D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1u);
+ UINT upload_size = upload_pitch_dst * upload_h;
+
+ D3D12_RESOURCE_DESC desc;
+ ZeroMemory(&desc, sizeof(desc));
desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
desc.Alignment = 0;
desc.Width = upload_size;
@@ -440,97 +530,91 @@ static void ImGui_ImplDX12_CreateFontsTexture()
desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
desc.Flags = D3D12_RESOURCE_FLAG_NONE;
+ D3D12_HEAP_PROPERTIES props;
+ memset(&props, 0, sizeof(D3D12_HEAP_PROPERTIES));
props.Type = D3D12_HEAP_TYPE_UPLOAD;
props.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
props.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
+ // FIXME-OPT: Could upload buffer be kept around, reused, and grown only when needed? Would that be worth it?
ID3D12Resource* uploadBuffer = nullptr;
HRESULT hr = bd->pd3dDevice->CreateCommittedResource(&props, D3D12_HEAP_FLAG_NONE, &desc,
D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, IID_PPV_ARGS(&uploadBuffer));
IM_ASSERT(SUCCEEDED(hr));
+ bd->pTexCmdAllocator->Reset();
+ bd->pTexCmdList->Reset(bd->pTexCmdAllocator, nullptr);
+ ID3D12GraphicsCommandList* cmdList = bd->pTexCmdList;
+
+ // Copy to upload buffer
void* mapped = nullptr;
D3D12_RANGE range = { 0, upload_size };
hr = uploadBuffer->Map(0, &range, &mapped);
IM_ASSERT(SUCCEEDED(hr));
- for (int y = 0; y < height; y++)
- memcpy((void*) ((uintptr_t) mapped + y * upload_pitch), pixels + y * width * 4, width * 4);
+ for (int y = 0; y < upload_h; y++)
+ memcpy((void*)((uintptr_t)mapped + y * upload_pitch_dst), tex->GetPixelsAt(upload_x, upload_y + y), upload_pitch_src);
uploadBuffer->Unmap(0, &range);
+ if (need_barrier_before_copy)
+ {
+ D3D12_RESOURCE_BARRIER barrier = {};
+ barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
+ barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
+ barrier.Transition.pResource = backend_tex->pTextureResource;
+ barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
+ barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE;
+ barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_COPY_DEST;
+ cmdList->ResourceBarrier(1, &barrier);
+ }
+
D3D12_TEXTURE_COPY_LOCATION srcLocation = {};
D3D12_TEXTURE_COPY_LOCATION dstLocation = {};
{
srcLocation.pResource = uploadBuffer;
srcLocation.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
srcLocation.PlacedFootprint.Footprint.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
- srcLocation.PlacedFootprint.Footprint.Width = width;
- srcLocation.PlacedFootprint.Footprint.Height = height;
+ srcLocation.PlacedFootprint.Footprint.Width = upload_w;
+ srcLocation.PlacedFootprint.Footprint.Height = upload_h;
srcLocation.PlacedFootprint.Footprint.Depth = 1;
- srcLocation.PlacedFootprint.Footprint.RowPitch = upload_pitch;
-
- dstLocation.pResource = pTexture;
+ srcLocation.PlacedFootprint.Footprint.RowPitch = upload_pitch_dst;
+ dstLocation.pResource = backend_tex->pTextureResource;
dstLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
dstLocation.SubresourceIndex = 0;
}
+ cmdList->CopyTextureRegion(&dstLocation, upload_x, upload_y, 0, &srcLocation, nullptr);
- D3D12_RESOURCE_BARRIER barrier = {};
- barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
- barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
- barrier.Transition.pResource = pTexture;
- barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
- barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_COPY_DEST;
- barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE;
-
- ID3D12Fence* fence = nullptr;
- hr = bd->pd3dDevice->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence));
- IM_ASSERT(SUCCEEDED(hr));
-
- HANDLE event = ::CreateEvent(0, 0, 0, 0);
- IM_ASSERT(event != nullptr);
-
- ID3D12CommandAllocator* cmdAlloc = nullptr;
- hr = bd->pd3dDevice->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&cmdAlloc));
- IM_ASSERT(SUCCEEDED(hr));
-
- ID3D12GraphicsCommandList* cmdList = nullptr;
- hr = bd->pd3dDevice->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, cmdAlloc, nullptr, IID_PPV_ARGS(&cmdList));
- IM_ASSERT(SUCCEEDED(hr));
-
- cmdList->CopyTextureRegion(&dstLocation, 0, 0, 0, &srcLocation, nullptr);
- cmdList->ResourceBarrier(1, &barrier);
+ {
+ D3D12_RESOURCE_BARRIER barrier = {};
+ barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
+ barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
+ barrier.Transition.pResource = backend_tex->pTextureResource;
+ barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
+ barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_COPY_DEST;
+ barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE;
+ cmdList->ResourceBarrier(1, &barrier);
+ }
hr = cmdList->Close();
IM_ASSERT(SUCCEEDED(hr));
ID3D12CommandQueue* cmdQueue = bd->pCommandQueue;
cmdQueue->ExecuteCommandLists(1, (ID3D12CommandList* const*)&cmdList);
- hr = cmdQueue->Signal(fence, 1);
+ hr = cmdQueue->Signal(bd->Fence, ++bd->FenceLastSignaledValue);
IM_ASSERT(SUCCEEDED(hr));
- fence->SetEventOnCompletion(1, event);
- ::WaitForSingleObject(event, INFINITE);
+ // FIXME-OPT: Suboptimal?
+ // - To remove this may need to create NumFramesInFlight x ImGui_ImplDX12_FrameContext in backend data (mimick docking version)
+ // - Store per-frame in flight: upload buffer?
+ // - Where do cmdList and cmdAlloc fit?
+ bd->Fence->SetEventOnCompletion(bd->FenceLastSignaledValue, bd->FenceEvent);
+ ::WaitForSingleObject(bd->FenceEvent, INFINITE);
- cmdList->Release();
- cmdAlloc->Release();
- ::CloseHandle(event);
- fence->Release();
uploadBuffer->Release();
-
- // Create texture view
- D3D12_SHADER_RESOURCE_VIEW_DESC srvDesc;
- ZeroMemory(&srvDesc, sizeof(srvDesc));
- srvDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
- srvDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
- srvDesc.Texture2D.MipLevels = desc.MipLevels;
- srvDesc.Texture2D.MostDetailedMip = 0;
- srvDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
- bd->pd3dDevice->CreateShaderResourceView(pTexture, &srvDesc, font_tex->hFontSrvCpuDescHandle);
- SafeRelease(font_tex->pTextureResource);
- font_tex->pTextureResource = pTexture;
+ tex->SetStatus(ImTextureStatus_OK);
}
- // Store our identifier
- io.Fonts->SetTexID((ImTextureID)font_tex->hFontSrvGpuDescHandle.ptr);
+ if (tex->Status == ImTextureStatus_WantDestroy && tex->UnusedFrames >= (int)bd->numFramesInFlight)
+ ImGui_ImplDX12_DestroyTexture(tex);
}
bool ImGui_ImplDX12_CreateDeviceObjects()
@@ -541,6 +625,13 @@ bool ImGui_ImplDX12_CreateDeviceObjects()
if (bd->pPipelineState)
ImGui_ImplDX12_InvalidateDeviceObjects();
+ HRESULT hr = ::CreateDXGIFactory1(IID_PPV_ARGS(&bd->pdxgiFactory));
+ IM_ASSERT(hr == S_OK);
+
+ BOOL allow_tearing = FALSE;
+ bd->pdxgiFactory->CheckFeatureSupport(DXGI_FEATURE_PRESENT_ALLOW_TEARING, &allow_tearing, sizeof(allow_tearing));
+ bd->tearingSupport = (allow_tearing == TRUE);
+
// Create the root signature
{
D3D12_DESCRIPTOR_RANGE descRange = {};
@@ -564,26 +655,26 @@ bool ImGui_ImplDX12_CreateDeviceObjects()
param[1].ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL;
// Bilinear sampling is required by default. Set 'io.Fonts->Flags |= ImFontAtlasFlags_NoBakedLines' or 'style.AntiAliasedLinesUseTex = false' to allow point/nearest sampling.
- D3D12_STATIC_SAMPLER_DESC staticSampler = {};
- staticSampler.Filter = D3D12_FILTER_MIN_MAG_MIP_LINEAR;
- staticSampler.AddressU = D3D12_TEXTURE_ADDRESS_MODE_CLAMP;
- staticSampler.AddressV = D3D12_TEXTURE_ADDRESS_MODE_CLAMP;
- staticSampler.AddressW = D3D12_TEXTURE_ADDRESS_MODE_CLAMP;
- staticSampler.MipLODBias = 0.f;
- staticSampler.MaxAnisotropy = 0;
- staticSampler.ComparisonFunc = D3D12_COMPARISON_FUNC_ALWAYS;
- staticSampler.BorderColor = D3D12_STATIC_BORDER_COLOR_TRANSPARENT_BLACK;
- staticSampler.MinLOD = 0.f;
- staticSampler.MaxLOD = 0.f;
- staticSampler.ShaderRegister = 0;
- staticSampler.RegisterSpace = 0;
- staticSampler.ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL;
+ D3D12_STATIC_SAMPLER_DESC staticSampler[1] = {};
+ staticSampler[0].Filter = D3D12_FILTER_MIN_MAG_MIP_LINEAR;
+ staticSampler[0].AddressU = D3D12_TEXTURE_ADDRESS_MODE_CLAMP;
+ staticSampler[0].AddressV = D3D12_TEXTURE_ADDRESS_MODE_CLAMP;
+ staticSampler[0].AddressW = D3D12_TEXTURE_ADDRESS_MODE_CLAMP;
+ staticSampler[0].MipLODBias = 0.f;
+ staticSampler[0].MaxAnisotropy = 0;
+ staticSampler[0].ComparisonFunc = D3D12_COMPARISON_FUNC_ALWAYS;
+ staticSampler[0].BorderColor = D3D12_STATIC_BORDER_COLOR_TRANSPARENT_BLACK;
+ staticSampler[0].MinLOD = 0.f;
+ staticSampler[0].MaxLOD = D3D12_FLOAT32_MAX;
+ staticSampler[0].ShaderRegister = 0;
+ staticSampler[0].RegisterSpace = 0;
+ staticSampler[0].ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL;
D3D12_ROOT_SIGNATURE_DESC desc = {};
desc.NumParameters = _countof(param);
desc.pParameters = param;
desc.NumStaticSamplers = 1;
- desc.pStaticSamplers = &staticSampler;
+ desc.pStaticSamplers = &staticSampler[0];
desc.Flags =
D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT |
D3D12_ROOT_SIGNATURE_FLAG_DENY_HULL_SHADER_ROOT_ACCESS |
@@ -612,7 +703,7 @@ bool ImGui_ImplDX12_CreateDeviceObjects()
return false;
}
- PFN_D3D12_SERIALIZE_ROOT_SIGNATURE D3D12SerializeRootSignatureFn = (PFN_D3D12_SERIALIZE_ROOT_SIGNATURE)::GetProcAddress(d3d12_dll, "D3D12SerializeRootSignature");
+ _PFN_D3D12_SERIALIZE_ROOT_SIGNATURE D3D12SerializeRootSignatureFn = (_PFN_D3D12_SERIALIZE_ROOT_SIGNATURE)(void*)::GetProcAddress(d3d12_dll, "D3D12SerializeRootSignature");
if (D3D12SerializeRootSignatureFn == nullptr)
return false;
@@ -762,7 +853,19 @@ bool ImGui_ImplDX12_CreateDeviceObjects()
if (result_pipeline_state != S_OK)
return false;
- ImGui_ImplDX12_CreateFontsTexture();
+ // Create command allocator and command list for ImGui_ImplDX12_UpdateTexture()
+ hr = bd->pd3dDevice->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&bd->pTexCmdAllocator));
+ IM_ASSERT(SUCCEEDED(hr));
+ hr = bd->pd3dDevice->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, bd->pTexCmdAllocator, nullptr, IID_PPV_ARGS(&bd->pTexCmdList));
+ IM_ASSERT(SUCCEEDED(hr));
+ hr = bd->pTexCmdList->Close();
+ IM_ASSERT(SUCCEEDED(hr));
+
+ // Create fence.
+ hr = bd->pd3dDevice->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&bd->Fence));
+ IM_ASSERT(hr == S_OK);
+ bd->FenceEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr);
+ IM_ASSERT(bd->FenceEvent != nullptr);
return true;
}
@@ -780,20 +883,46 @@ void ImGui_ImplDX12_InvalidateDeviceObjects()
if (!bd || !bd->pd3dDevice)
return;
+ SafeRelease(bd->pdxgiFactory);
if (bd->commandQueueOwned)
SafeRelease(bd->pCommandQueue);
bd->commandQueueOwned = false;
SafeRelease(bd->pRootSignature);
SafeRelease(bd->pPipelineState);
+ SafeRelease(bd->pTexCmdList);
+ SafeRelease(bd->pTexCmdAllocator);
+ SafeRelease(bd->Fence);
+ CloseHandle(bd->FenceEvent);
+ bd->FenceEvent = nullptr;
- // Free SRV descriptor used by texture
- ImGuiIO& io = ImGui::GetIO();
- ImGui_ImplDX12_Texture* font_tex = &bd->FontTexture;
- bd->InitInfo.SrvDescriptorFreeFn(&bd->InitInfo, font_tex->hFontSrvCpuDescHandle, font_tex->hFontSrvGpuDescHandle);
- SafeRelease(font_tex->pTextureResource);
- io.Fonts->SetTexID(0); // We copied bd->hFontSrvGpuDescHandle to io.Fonts->TexID so let's clear that as well.
+ // Destroy all textures
+ for (ImTextureData* tex : ImGui::GetPlatformIO().Textures)
+ if (tex->RefCount == 1)
+ ImGui_ImplDX12_DestroyTexture(tex);
}
+#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
+static void ImGui_ImplDX12_InitLegacySingleDescriptorMode(ImGui_ImplDX12_InitInfo* init_info)
+{
+ // Wrap legacy behavior of passing space for a single descriptor
+ IM_ASSERT(init_info->LegacySingleSrvCpuDescriptor.ptr != 0 && init_info->LegacySingleSrvGpuDescriptor.ptr != 0);
+ init_info->SrvDescriptorAllocFn = [](ImGui_ImplDX12_InitInfo*, D3D12_CPU_DESCRIPTOR_HANDLE* out_cpu_handle, D3D12_GPU_DESCRIPTOR_HANDLE* out_gpu_handle)
+ {
+ ImGui_ImplDX12_Data* bd = ImGui_ImplDX12_GetBackendData();
+ IM_ASSERT(bd->LegacySingleDescriptorUsed == false && "Only 1 simultaneous texture allowed with legacy ImGui_ImplDX12_Init() signature!");
+ *out_cpu_handle = bd->InitInfo.LegacySingleSrvCpuDescriptor;
+ *out_gpu_handle = bd->InitInfo.LegacySingleSrvGpuDescriptor;
+ bd->LegacySingleDescriptorUsed = true;
+ };
+ init_info->SrvDescriptorFreeFn = [](ImGui_ImplDX12_InitInfo*, D3D12_CPU_DESCRIPTOR_HANDLE, D3D12_GPU_DESCRIPTOR_HANDLE)
+ {
+ ImGui_ImplDX12_Data* bd = ImGui_ImplDX12_GetBackendData();
+ IM_ASSERT(bd->LegacySingleDescriptorUsed == true);
+ bd->LegacySingleDescriptorUsed = false;
+ };
+}
+#endif
+
bool ImGui_ImplDX12_Init(ImGui_ImplDX12_InitInfo* init_info)
{
ImGuiIO& io = ImGui::GetIO();
@@ -812,13 +941,16 @@ bool ImGui_ImplDX12_Init(ImGui_ImplDX12_InitInfo* init_info)
bd->DSVFormat = init_info->DSVFormat;
bd->numFramesInFlight = init_info->NumFramesInFlight;
bd->pd3dSrvDescHeap = init_info->SrvDescriptorHeap;
+ bd->tearingSupport = false;
io.BackendRendererUserData = (void*)bd;
io.BackendRendererName = "imgui_impl_dx12";
io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes.
+ io.BackendFlags |= ImGuiBackendFlags_RendererHasTextures; // We can honor ImGuiPlatformIO::Textures[] requests during render.
io.BackendFlags |= ImGuiBackendFlags_RendererHasViewports; // We can create multi-viewports on the Renderer side (optional)
+
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
- ImGui_ImplDX12_InitPlatformInterface();
+ ImGui_ImplDX12_InitMultiViewportSupport();
// Create a dummy ImGui_ImplDX12_ViewportData holder for the main viewport,
// Since this is created and managed by the application, we will only use the ->Resources[] fields.
@@ -827,29 +959,9 @@ bool ImGui_ImplDX12_Init(ImGui_ImplDX12_InitInfo* init_info)
#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
if (init_info->SrvDescriptorAllocFn == nullptr)
- {
- // Wrap legacy behavior of passing space for a single descriptor
- IM_ASSERT(init_info->LegacySingleSrvCpuDescriptor.ptr != 0 && init_info->LegacySingleSrvGpuDescriptor.ptr != 0);
- init_info->SrvDescriptorAllocFn = [](ImGui_ImplDX12_InitInfo*, D3D12_CPU_DESCRIPTOR_HANDLE* out_cpu_handle, D3D12_GPU_DESCRIPTOR_HANDLE* out_gpu_handle)
- {
- ImGui_ImplDX12_Data* bd = ImGui_ImplDX12_GetBackendData();
- IM_ASSERT(bd->LegacySingleDescriptorUsed == false && "Only 1 simultaneous texture allowed with legacy ImGui_ImplDX12_Init() signature!");
- *out_cpu_handle = bd->InitInfo.LegacySingleSrvCpuDescriptor;
- *out_gpu_handle = bd->InitInfo.LegacySingleSrvGpuDescriptor;
- bd->LegacySingleDescriptorUsed = true;
- };
- init_info->SrvDescriptorFreeFn = [](ImGui_ImplDX12_InitInfo*, D3D12_CPU_DESCRIPTOR_HANDLE, D3D12_GPU_DESCRIPTOR_HANDLE)
- {
- ImGui_ImplDX12_Data* bd = ImGui_ImplDX12_GetBackendData();
- IM_ASSERT(bd->LegacySingleDescriptorUsed == true);
- bd->LegacySingleDescriptorUsed = false;
- };
- }
+ ImGui_ImplDX12_InitLegacySingleDescriptorMode(init_info);
#endif
-
- // Allocate 1 SRV descriptor for the font texture
IM_ASSERT(init_info->SrvDescriptorAllocFn != nullptr && init_info->SrvDescriptorFreeFn != nullptr);
- init_info->SrvDescriptorAllocFn(&bd->InitInfo, &bd->FontTexture.hFontSrvCpuDescHandle, &bd->FontTexture.hFontSrvGpuDescHandle);
return true;
}
@@ -877,6 +989,9 @@ bool ImGui_ImplDX12_Init(ID3D12Device* device, int num_frames_in_flight, DXGI_FO
bool ret = ImGui_ImplDX12_Init(&init_info);
ImGui_ImplDX12_Data* bd = ImGui_ImplDX12_GetBackendData();
bd->commandQueueOwned = true;
+ ImGuiIO& io = ImGui::GetIO();
+ io.BackendFlags &= ~ImGuiBackendFlags_RendererHasTextures; // Using legacy ImGui_ImplDX12_Init() call with 1 SRV descriptor we cannot support multiple textures.
+
return ret;
}
#endif
@@ -886,6 +1001,7 @@ void ImGui_ImplDX12_Shutdown()
ImGui_ImplDX12_Data* bd = ImGui_ImplDX12_GetBackendData();
IM_ASSERT(bd != nullptr && "No renderer backend to shutdown, or already shutdown?");
ImGuiIO& io = ImGui::GetIO();
+ ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
// Manually delete main viewport render resources in-case we haven't initialized for viewports
ImGuiViewport* main_viewport = ImGui::GetMainViewport();
@@ -898,13 +1014,13 @@ void ImGui_ImplDX12_Shutdown()
main_viewport->RendererUserData = nullptr;
}
- // Clean up windows and device objects
- ImGui_ImplDX12_ShutdownPlatformInterface();
+ ImGui_ImplDX12_ShutdownMultiViewportSupport();
ImGui_ImplDX12_InvalidateDeviceObjects();
io.BackendRendererName = nullptr;
io.BackendRendererUserData = nullptr;
- io.BackendFlags &= ~(ImGuiBackendFlags_RendererHasVtxOffset | ImGuiBackendFlags_RendererHasViewports);
+ io.BackendFlags &= ~(ImGuiBackendFlags_RendererHasVtxOffset | ImGuiBackendFlags_RendererHasTextures | ImGuiBackendFlags_RendererHasViewports);
+ platform_io.ClearRendererHandlers();
IM_DELETE(bd);
}
@@ -914,7 +1030,8 @@ void ImGui_ImplDX12_NewFrame()
IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplDX12_Init()?");
if (!bd->pPipelineState)
- ImGui_ImplDX12_CreateDeviceObjects();
+ if (!ImGui_ImplDX12_CreateDeviceObjects())
+ IM_ASSERT(0 && "ImGui_ImplDX12_CreateDeviceObjects() failed!");
}
//--------------------------------------------------------------------------------------------------------
@@ -934,18 +1051,12 @@ static void ImGui_ImplDX12_CreateWindow(ImGuiViewport* viewport)
HWND hwnd = viewport->PlatformHandleRaw ? (HWND)viewport->PlatformHandleRaw : (HWND)viewport->PlatformHandle;
IM_ASSERT(hwnd != 0);
- vd->FrameIndex = UINT_MAX;
-
- // Create command queue.
- D3D12_COMMAND_QUEUE_DESC queue_desc = {};
- queue_desc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE;
- queue_desc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
-
- HRESULT res = S_OK;
- res = bd->pd3dDevice->CreateCommandQueue(&queue_desc, IID_PPV_ARGS(&vd->CommandQueue));
- IM_ASSERT(res == S_OK);
+ // Use shared command queue from init info
+ vd->FrameIndex = 0;
+ vd->CommandQueue = bd->pCommandQueue;
// Create command allocator.
+ HRESULT res = S_OK;
for (UINT i = 0; i < bd->numFramesInFlight; ++i)
{
res = bd->pd3dDevice->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&vd->FrameCtx[i].CommandAllocator));
@@ -957,13 +1068,6 @@ static void ImGui_ImplDX12_CreateWindow(ImGuiViewport* viewport)
IM_ASSERT(res == S_OK);
vd->CommandList->Close();
- // Create fence.
- res = bd->pd3dDevice->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&vd->Fence));
- IM_ASSERT(res == S_OK);
-
- vd->FenceEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr);
- IM_ASSERT(vd->FenceEvent != nullptr);
-
// Create swap chain
// FIXME-VIEWPORT: May want to copy/inherit swap chain settings from the user/application.
DXGI_SWAP_CHAIN_DESC1 sd1;
@@ -979,23 +1083,23 @@ static void ImGui_ImplDX12_CreateWindow(ImGuiViewport* viewport)
sd1.AlphaMode = DXGI_ALPHA_MODE_UNSPECIFIED;
sd1.Scaling = DXGI_SCALING_NONE;
sd1.Stereo = FALSE;
+ sd1.Flags = DXGI_SWAP_CHAIN_FLAG_FRAME_LATENCY_WAITABLE_OBJECT;
- IDXGIFactory4* dxgi_factory = nullptr;
- res = ::CreateDXGIFactory1(IID_PPV_ARGS(&dxgi_factory));
- IM_ASSERT(res == S_OK);
+ if (bd->tearingSupport)
+ sd1.Flags |= DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING;
IDXGISwapChain1* swap_chain = nullptr;
- res = dxgi_factory->CreateSwapChainForHwnd(vd->CommandQueue, hwnd, &sd1, nullptr, nullptr, &swap_chain);
+ res = bd->pdxgiFactory->CreateSwapChainForHwnd(vd->CommandQueue, hwnd, &sd1, nullptr, nullptr, &swap_chain);
+ IM_ASSERT(res == S_OK);
+ res = bd->pdxgiFactory->MakeWindowAssociation(hwnd, DXGI_MWA_NO_ALT_ENTER | DXGI_MWA_NO_WINDOW_CHANGES); // Disable e.g. Alt+Enter
IM_ASSERT(res == S_OK);
-
- dxgi_factory->Release();
// Or swapChain.As(&mSwapChain)
IM_ASSERT(vd->SwapChain == nullptr);
swap_chain->QueryInterface(IID_PPV_ARGS(&vd->SwapChain));
swap_chain->Release();
- // Create the render targets
+ // Create the render targets and waitable object
if (vd->SwapChain)
{
D3D12_DESCRIPTOR_HEAP_DESC desc = {};
@@ -1023,6 +1127,10 @@ static void ImGui_ImplDX12_CreateWindow(ImGuiViewport* viewport)
bd->pd3dDevice->CreateRenderTargetView(back_buffer, nullptr, vd->FrameCtx[i].RenderTargetCpuDescriptors);
vd->FrameCtx[i].RenderTarget = back_buffer;
}
+
+ hr = vd->SwapChain->SetMaximumFrameLatency(bd->numFramesInFlight);
+ IM_ASSERT(hr == S_OK);
+ vd->SwapChainWaitableObject = vd->SwapChain->GetFrameLatencyWaitableObject();
}
for (UINT i = 0; i < bd->numFramesInFlight; i++)
@@ -1031,16 +1139,31 @@ static void ImGui_ImplDX12_CreateWindow(ImGuiViewport* viewport)
static void ImGui_WaitForPendingOperations(ImGui_ImplDX12_ViewportData* vd)
{
- HRESULT hr = S_FALSE;
- if (vd && vd->CommandQueue && vd->Fence && vd->FenceEvent)
+ ImGui_ImplDX12_Data* bd = ImGui_ImplDX12_GetBackendData();
+ HRESULT hr = vd->CommandQueue->Signal(bd->Fence, ++bd->FenceLastSignaledValue);
+ IM_ASSERT(hr == S_OK);
+
+ hr = bd->Fence->SetEventOnCompletion(bd->FenceLastSignaledValue, bd->FenceEvent);
+ IM_ASSERT(hr == S_OK);
+ ::WaitForSingleObject(bd->FenceEvent, INFINITE);
+}
+
+static ImGui_ImplDX12_FrameContext* ImGui_WaitForNextFrameContext(ImGui_ImplDX12_ViewportData* vd)
+{
+ ImGui_ImplDX12_Data* bd = ImGui_ImplDX12_GetBackendData();
+ ImGui_ImplDX12_FrameContext* frame_context = &vd->FrameCtx[vd->FrameIndex % vd->NumFramesInFlight];
+ if (bd->Fence->GetCompletedValue() < frame_context->FenceValue)
{
- hr = vd->CommandQueue->Signal(vd->Fence, ++vd->FenceSignaledValue);
+ HRESULT hr = bd->Fence->SetEventOnCompletion(frame_context->FenceValue, bd->FenceEvent);
IM_ASSERT(hr == S_OK);
- ::WaitForSingleObject(vd->FenceEvent, 0); // Reset any forgotten waits
- hr = vd->Fence->SetEventOnCompletion(vd->FenceSignaledValue, vd->FenceEvent);
- IM_ASSERT(hr == S_OK);
- ::WaitForSingleObject(vd->FenceEvent, INFINITE);
+ HANDLE waitableObjects[] = { vd->SwapChainWaitableObject, bd->FenceEvent };
+ ::WaitForMultipleObjects(2, waitableObjects, TRUE, INFINITE);
}
+ else
+ {
+ ::WaitForSingleObject(vd->SwapChainWaitableObject, INFINITE);
+ }
+ return frame_context;
}
static void ImGui_ImplDX12_DestroyWindow(ImGuiViewport* viewport)
@@ -1051,13 +1174,12 @@ static void ImGui_ImplDX12_DestroyWindow(ImGuiViewport* viewport)
{
ImGui_WaitForPendingOperations(vd);
- SafeRelease(vd->CommandQueue);
+ vd->CommandQueue = nullptr;
+ ::CloseHandle(vd->SwapChainWaitableObject);
+ vd->SwapChainWaitableObject = nullptr;
SafeRelease(vd->CommandList);
SafeRelease(vd->SwapChain);
SafeRelease(vd->RtvDescHeap);
- SafeRelease(vd->Fence);
- ::CloseHandle(vd->FenceEvent);
- vd->FenceEvent = nullptr;
for (UINT i = 0; i < bd->numFramesInFlight; i++)
{
@@ -1083,7 +1205,9 @@ static void ImGui_ImplDX12_SetWindowSize(ImGuiViewport* viewport, ImVec2 size)
if (vd->SwapChain)
{
ID3D12Resource* back_buffer = nullptr;
- vd->SwapChain->ResizeBuffers(0, (UINT)size.x, (UINT)size.y, DXGI_FORMAT_UNKNOWN, 0);
+ DXGI_SWAP_CHAIN_DESC1 desc = {};
+ vd->SwapChain->GetDesc1(&desc);
+ vd->SwapChain->ResizeBuffers(0, (UINT)size.x, (UINT)size.y, desc.Format, desc.Flags);
for (UINT i = 0; i < bd->numFramesInFlight; i++)
{
vd->SwapChain->GetBuffer(i, IID_PPV_ARGS(&back_buffer));
@@ -1098,7 +1222,7 @@ static void ImGui_ImplDX12_RenderWindow(ImGuiViewport* viewport, void*)
ImGui_ImplDX12_Data* bd = ImGui_ImplDX12_GetBackendData();
ImGui_ImplDX12_ViewportData* vd = (ImGui_ImplDX12_ViewportData*)viewport->RendererUserData;
- ImGui_ImplDX12_FrameContext* frame_context = &vd->FrameCtx[vd->FrameIndex % bd->numFramesInFlight];
+ ImGui_ImplDX12_FrameContext* frame_context = ImGui_WaitForNextFrameContext(vd);
UINT back_buffer_idx = vd->SwapChain->GetCurrentBackBufferIndex();
const ImVec4 clear_color = ImVec4(0.0f, 0.0f, 0.0f, 1.0f);
@@ -1118,7 +1242,7 @@ static void ImGui_ImplDX12_RenderWindow(ImGuiViewport* viewport, void*)
cmd_list->ResourceBarrier(1, &barrier);
cmd_list->OMSetRenderTargets(1, &vd->FrameCtx[back_buffer_idx].RenderTargetCpuDescriptors, FALSE, nullptr);
if (!(viewport->Flags & ImGuiViewportFlags_NoRendererClear))
- cmd_list->ClearRenderTargetView(vd->FrameCtx[back_buffer_idx].RenderTargetCpuDescriptors, (float*)&clear_color, 0, nullptr);
+ cmd_list->ClearRenderTargetView(vd->FrameCtx[back_buffer_idx].RenderTargetCpuDescriptors, (const float*)&clear_color, 0, nullptr);
cmd_list->SetDescriptorHeaps(1, &bd->pd3dSrvDescHeap);
ImGui_ImplDX12_RenderDrawData(viewport->DrawData, cmd_list);
@@ -1128,21 +1252,23 @@ static void ImGui_ImplDX12_RenderWindow(ImGuiViewport* viewport, void*)
cmd_list->ResourceBarrier(1, &barrier);
cmd_list->Close();
- vd->CommandQueue->Wait(vd->Fence, vd->FenceSignaledValue);
vd->CommandQueue->ExecuteCommandLists(1, (ID3D12CommandList* const*)&cmd_list);
- vd->CommandQueue->Signal(vd->Fence, ++vd->FenceSignaledValue);
+
+ HRESULT hr = vd->CommandQueue->Signal(bd->Fence, ++bd->FenceLastSignaledValue);
+ IM_ASSERT(hr == S_OK);
+ frame_context->FenceValue = bd->FenceLastSignaledValue;
}
static void ImGui_ImplDX12_SwapBuffers(ImGuiViewport* viewport, void*)
{
+ ImGui_ImplDX12_Data* bd = ImGui_ImplDX12_GetBackendData();
ImGui_ImplDX12_ViewportData* vd = (ImGui_ImplDX12_ViewportData*)viewport->RendererUserData;
- vd->SwapChain->Present(0, 0);
- while (vd->Fence->GetCompletedValue() < vd->FenceSignaledValue)
- ::SwitchToThread();
+ vd->SwapChain->Present(0, bd->tearingSupport ? DXGI_PRESENT_ALLOW_TEARING : 0);
+ vd->FrameIndex++;
}
-void ImGui_ImplDX12_InitPlatformInterface()
+void ImGui_ImplDX12_InitMultiViewportSupport()
{
ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
platform_io.Renderer_CreateWindow = ImGui_ImplDX12_CreateWindow;
@@ -1152,7 +1278,7 @@ void ImGui_ImplDX12_InitPlatformInterface()
platform_io.Renderer_SwapBuffers = ImGui_ImplDX12_SwapBuffers;
}
-void ImGui_ImplDX12_ShutdownPlatformInterface()
+void ImGui_ImplDX12_ShutdownMultiViewportSupport()
{
ImGui::DestroyPlatformWindows();
}
diff --git a/backends/imgui_impl_dx12.h b/backends/imgui_impl_dx12.h
index c82514c59..7d4c04378 100644
--- a/backends/imgui_impl_dx12.h
+++ b/backends/imgui_impl_dx12.h
@@ -2,8 +2,9 @@
// This needs to be used along with a Platform Backend (e.g. Win32)
// Implemented features:
-// [X] Renderer: User texture binding. Use 'D3D12_GPU_DESCRIPTOR_HANDLE' as ImTextureID. Read the FAQ about ImTextureID!
+// [X] Renderer: User texture binding. Use 'D3D12_GPU_DESCRIPTOR_HANDLE' as texture identifier. Read the FAQ about ImTextureID/ImTextureRef!
// [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset).
+// [X] Renderer: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures).
// [X] Renderer: Expose selected render state for draw callbacks to use. Access in '(ImGui_ImplXXXX_RenderState*)GetPlatformIO().Renderer_RenderState'.
// [X] Renderer: Multi-viewport support (multiple windows). Enable with 'io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable'.
@@ -64,6 +65,9 @@ IMGUI_IMPL_API bool ImGui_ImplDX12_Init(ID3D12Device* device, int num_frames
IMGUI_IMPL_API bool ImGui_ImplDX12_CreateDeviceObjects();
IMGUI_IMPL_API void ImGui_ImplDX12_InvalidateDeviceObjects();
+// (Advanced) Use e.g. if you need to precisely control the timing of texture updates (e.g. for staged rendering), by setting ImDrawData::Textures = NULL to handle this manually.
+IMGUI_IMPL_API void ImGui_ImplDX12_UpdateTexture(ImTextureData* tex);
+
// [BETA] Selected render state data shared with callbacks.
// This is temporarily stored in GetPlatformIO().Renderer_RenderState during the ImGui_ImplDX12_RenderDrawData() call.
// (Please open an issue if you feel you need access to more data)
diff --git a/backends/imgui_impl_dx9.cpp b/backends/imgui_impl_dx9.cpp
index ef2ba13e3..d2a3567b6 100644
--- a/backends/imgui_impl_dx9.cpp
+++ b/backends/imgui_impl_dx9.cpp
@@ -2,8 +2,9 @@
// This needs to be used along with a Platform Backend (e.g. Win32)
// Implemented features:
-// [X] Renderer: User texture binding. Use 'LPDIRECT3DTEXTURE9' as ImTextureID. Read the FAQ about ImTextureID!
+// [X] Renderer: User texture binding. Use 'LPDIRECT3DTEXTURE9' as texture identifier. Read the FAQ about ImTextureID/ImTextureRef!
// [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset).
+// [X] Renderer: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures).
// [X] Renderer: IMGUI_USE_BGRA_PACKED_COLOR support, as this is the optimal color encoding for DirectX9.
// [X] Renderer: Multi-viewport support (multiple windows). Enable with 'io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable'.
@@ -18,6 +19,8 @@
// CHANGELOG
// (minor and older changes stripped away, please see git history for details)
// 2025-XX-XX: Platform: Added support for multiple windows via the ImGuiPlatformIO interface.
+// 2025-09-18: Call platform_io.ClearRendererHandlers() on shutdown.
+// 2025-06-11: DirectX9: Added support for ImGuiBackendFlags_RendererHasTextures, for dynamic font atlas.
// 2024-10-07: DirectX9: Changed default texture sampler to Clamp instead of Repeat/Wrap.
// 2024-02-12: DirectX9: Using RGBA format when supported by the driver to avoid CPU side conversion. (#6575)
// 2022-10-11: Using 'nullptr' instead of 'NULL' as per our switch to C++11.
@@ -46,13 +49,18 @@
// DirectX
#include
+// Clang/GCC warnings with -Weverything
+#if defined(__clang__)
+#pragma clang diagnostic ignored "-Wold-style-cast" // warning: use of old-style cast // yes, they are more terse.
+#pragma clang diagnostic ignored "-Wsign-conversion" // warning: implicit conversion changes signedness
+#endif
+
// DirectX data
struct ImGui_ImplDX9_Data
{
LPDIRECT3DDEVICE9 pd3dDevice;
LPDIRECT3DVERTEXBUFFER9 pVB;
LPDIRECT3DINDEXBUFFER9 pIB;
- LPDIRECT3DTEXTURE9 FontTexture;
int VertexBufferSize;
int IndexBufferSize;
bool HasRgbaSupport;
@@ -171,6 +179,13 @@ void ImGui_ImplDX9_RenderDrawData(ImDrawData* draw_data)
ImGui_ImplDX9_Data* bd = ImGui_ImplDX9_GetBackendData();
LPDIRECT3DDEVICE9 device = bd->pd3dDevice;
+ // Catch up with texture updates. Most of the times, the list will have 1 element with an OK status, aka nothing to do.
+ // (This almost always points to ImGui::GetPlatformIO().Textures[] but is part of ImDrawData to allow overriding or disabling texture updates).
+ if (draw_data->Textures != nullptr)
+ for (ImTextureData* tex : *draw_data->Textures)
+ if (tex->Status != ImTextureStatus_OK)
+ ImGui_ImplDX9_UpdateTexture(tex);
+
// Create and grow buffers if needed
if (!bd->pVB || bd->VertexBufferSize < draw_data->TotalVtxCount)
{
@@ -222,9 +237,8 @@ void ImGui_ImplDX9_RenderDrawData(ImDrawData* draw_data)
// FIXME-OPT: This is a minor waste of resource, the ideal is to use imconfig.h and
// 1) to avoid repacking colors: #define IMGUI_USE_BGRA_PACKED_COLOR
// 2) to avoid repacking vertices: #define IMGUI_OVERRIDE_DRAWVERT_STRUCT_LAYOUT struct ImDrawVert { ImVec2 pos; float z; ImU32 col; ImVec2 uv; }
- for (int n = 0; n < draw_data->CmdListsCount; n++)
+ for (const ImDrawList* draw_list : draw_data->CmdLists)
{
- const ImDrawList* draw_list = draw_data->CmdLists[n];
const ImDrawVert* vtx_src = draw_list->VtxBuffer.Data;
for (int i = 0; i < draw_list->VtxBuffer.Size; i++)
{
@@ -254,9 +268,8 @@ void ImGui_ImplDX9_RenderDrawData(ImDrawData* draw_data)
int global_vtx_offset = 0;
int global_idx_offset = 0;
ImVec2 clip_off = draw_data->DisplayPos;
- for (int n = 0; n < draw_data->CmdListsCount; n++)
+ for (const ImDrawList* draw_list : draw_data->CmdLists)
{
- const ImDrawList* draw_list = draw_data->CmdLists[n];
for (int cmd_i = 0; cmd_i < draw_list->CmdBuffer.Size; cmd_i++)
{
const ImDrawCmd* pcmd = &draw_list->CmdBuffer[cmd_i];
@@ -335,8 +348,12 @@ bool ImGui_ImplDX9_Init(IDirect3DDevice9* device)
io.BackendRendererUserData = (void*)bd;
io.BackendRendererName = "imgui_impl_dx9";
io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes.
+ io.BackendFlags |= ImGuiBackendFlags_RendererHasTextures; // We can honor ImGuiPlatformIO::Textures[] requests during render.
io.BackendFlags |= ImGuiBackendFlags_RendererHasViewports; // We can create multi-viewports on the Renderer side (optional)
+ ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
+ platform_io.Renderer_TextureMaxWidth = platform_io.Renderer_TextureMaxHeight = 4096;
+
bd->pd3dDevice = device;
bd->pd3dDevice->AddRef();
bd->HasRgbaSupport = ImGui_ImplDX9_CheckFormatSupport(bd->pd3dDevice, D3DFMT_A8B8G8R8);
@@ -351,18 +368,21 @@ void ImGui_ImplDX9_Shutdown()
ImGui_ImplDX9_Data* bd = ImGui_ImplDX9_GetBackendData();
IM_ASSERT(bd != nullptr && "No renderer backend to shutdown, or already shutdown?");
ImGuiIO& io = ImGui::GetIO();
+ ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
ImGui_ImplDX9_ShutdownMultiViewportSupport();
ImGui_ImplDX9_InvalidateDeviceObjects();
if (bd->pd3dDevice) { bd->pd3dDevice->Release(); }
+
io.BackendRendererName = nullptr;
io.BackendRendererUserData = nullptr;
- io.BackendFlags &= ~(ImGuiBackendFlags_RendererHasVtxOffset | ImGuiBackendFlags_RendererHasViewports);
+ io.BackendFlags &= ~(ImGuiBackendFlags_RendererHasVtxOffset | ImGuiBackendFlags_RendererHasTextures | ImGuiBackendFlags_RendererHasViewports);
+ platform_io.ClearRendererHandlers();
IM_DELETE(bd);
}
// Convert RGBA32 to BGRA32 (because RGBA32 is not well supported by DX9 devices)
-static void ImGui_ImplDX9_CopyTextureRegion(bool tex_use_colors, ImU32* src, int src_pitch, ImU32* dst, int dst_pitch, int w, int h)
+static void ImGui_ImplDX9_CopyTextureRegion(bool tex_use_colors, const ImU32* src, int src_pitch, ImU32* dst, int dst_pitch, int w, int h)
{
#ifndef IMGUI_USE_BGRA_PACKED_COLOR
ImGui_ImplDX9_Data* bd = ImGui_ImplDX9_GetBackendData();
@@ -373,8 +393,8 @@ static void ImGui_ImplDX9_CopyTextureRegion(bool tex_use_colors, ImU32* src, int
#endif
for (int y = 0; y < h; y++)
{
- ImU32* src_p = (ImU32*)((unsigned char*)src + src_pitch * y);
- ImU32* dst_p = (ImU32*)((unsigned char*)dst + dst_pitch * y);
+ const ImU32* src_p = (const ImU32*)(const void*)((const unsigned char*)src + src_pitch * y);
+ ImU32* dst_p = (ImU32*)(void*)((unsigned char*)dst + dst_pitch * y);
if (convert_rgba_to_bgra)
for (int x = w; x > 0; x--, src_p++, dst_p++) // Convert copy
*dst_p = IMGUI_COL_TO_DX9_ARGB(*src_p);
@@ -383,28 +403,61 @@ static void ImGui_ImplDX9_CopyTextureRegion(bool tex_use_colors, ImU32* src, int
}
}
-static bool ImGui_ImplDX9_CreateFontsTexture()
+void ImGui_ImplDX9_UpdateTexture(ImTextureData* tex)
{
- // Build texture atlas
- ImGuiIO& io = ImGui::GetIO();
ImGui_ImplDX9_Data* bd = ImGui_ImplDX9_GetBackendData();
- unsigned char* pixels;
- int width, height, bytes_per_pixel;
- io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height, &bytes_per_pixel);
- // Upload texture to graphics system
- bd->FontTexture = nullptr;
- if (bd->pd3dDevice->CreateTexture(width, height, 1, D3DUSAGE_DYNAMIC, bd->HasRgbaSupport ? D3DFMT_A8B8G8R8 : D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &bd->FontTexture, nullptr) < 0)
- return false;
- D3DLOCKED_RECT tex_locked_rect;
- if (bd->FontTexture->LockRect(0, &tex_locked_rect, nullptr, 0) != D3D_OK)
- return false;
- ImGui_ImplDX9_CopyTextureRegion(io.Fonts->TexPixelsUseColors, (ImU32*)pixels, width * bytes_per_pixel, (ImU32*)tex_locked_rect.pBits, (int)tex_locked_rect.Pitch, width, height);
- bd->FontTexture->UnlockRect(0);
+ if (tex->Status == ImTextureStatus_WantCreate)
+ {
+ // Create and upload new texture to graphics system
+ //IMGUI_DEBUG_LOG("UpdateTexture #%03d: WantCreate %dx%d\n", tex->UniqueID, tex->Width, tex->Height);
+ IM_ASSERT(tex->TexID == ImTextureID_Invalid && tex->BackendUserData == nullptr);
+ IM_ASSERT(tex->Format == ImTextureFormat_RGBA32);
+ LPDIRECT3DTEXTURE9 dx_tex = nullptr;
+ HRESULT hr = bd->pd3dDevice->CreateTexture(tex->Width, tex->Height, 1, D3DUSAGE_DYNAMIC, bd->HasRgbaSupport ? D3DFMT_A8B8G8R8 : D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &dx_tex, nullptr);
+ if (hr < 0)
+ {
+ IM_ASSERT(hr >= 0 && "Backend failed to create texture!");
+ return;
+ }
- // Store our identifier
- io.Fonts->SetTexID((ImTextureID)bd->FontTexture);
- return true;
+ D3DLOCKED_RECT locked_rect;
+ if (dx_tex->LockRect(0, &locked_rect, nullptr, 0) == D3D_OK)
+ {
+ ImGui_ImplDX9_CopyTextureRegion(tex->UseColors, (ImU32*)tex->GetPixels(), tex->Width * 4, (ImU32*)locked_rect.pBits, (ImU32)locked_rect.Pitch, tex->Width, tex->Height);
+ dx_tex->UnlockRect(0);
+ }
+
+ // Store identifiers
+ tex->SetTexID((ImTextureID)(intptr_t)dx_tex);
+ tex->SetStatus(ImTextureStatus_OK);
+ }
+ else if (tex->Status == ImTextureStatus_WantUpdates)
+ {
+ // Update selected blocks. We only ever write to textures regions which have never been used before!
+ // This backend choose to use tex->Updates[] but you can use tex->UpdateRect to upload a single region.
+ LPDIRECT3DTEXTURE9 backend_tex = (LPDIRECT3DTEXTURE9)(intptr_t)tex->TexID;
+ RECT update_rect = { (LONG)tex->UpdateRect.x, (LONG)tex->UpdateRect.y, (LONG)(tex->UpdateRect.x + tex->UpdateRect.w), (LONG)(tex->UpdateRect.y + tex->UpdateRect.h) };
+ D3DLOCKED_RECT locked_rect;
+ if (backend_tex->LockRect(0, &locked_rect, &update_rect, 0) == D3D_OK)
+ for (ImTextureRect& r : tex->Updates)
+ ImGui_ImplDX9_CopyTextureRegion(tex->UseColors, (ImU32*)tex->GetPixelsAt(r.x, r.y), tex->Width * 4,
+ (ImU32*)locked_rect.pBits + (r.x - update_rect.left) + (r.y - update_rect.top) * (locked_rect.Pitch / 4), (int)locked_rect.Pitch, r.w, r.h);
+ backend_tex->UnlockRect(0);
+ tex->SetStatus(ImTextureStatus_OK);
+ }
+ else if (tex->Status == ImTextureStatus_WantDestroy)
+ {
+ if (LPDIRECT3DTEXTURE9 backend_tex = (LPDIRECT3DTEXTURE9)tex->TexID)
+ {
+ IM_ASSERT(tex->TexID == (ImTextureID)(intptr_t)backend_tex);
+ backend_tex->Release();
+
+ // Clear identifiers and mark as destroyed (in order to allow e.g. calling InvalidateDeviceObjects while running)
+ tex->SetTexID(ImTextureID_Invalid);
+ }
+ tex->SetStatus(ImTextureStatus_Destroyed);
+ }
}
bool ImGui_ImplDX9_CreateDeviceObjects()
@@ -412,8 +465,6 @@ bool ImGui_ImplDX9_CreateDeviceObjects()
ImGui_ImplDX9_Data* bd = ImGui_ImplDX9_GetBackendData();
if (!bd || !bd->pd3dDevice)
return false;
- if (!ImGui_ImplDX9_CreateFontsTexture())
- return false;
ImGui_ImplDX9_CreateDeviceObjectsForPlatformWindows();
return true;
}
@@ -423,9 +474,16 @@ void ImGui_ImplDX9_InvalidateDeviceObjects()
ImGui_ImplDX9_Data* bd = ImGui_ImplDX9_GetBackendData();
if (!bd || !bd->pd3dDevice)
return;
+
+ // Destroy all textures
+ for (ImTextureData* tex : ImGui::GetPlatformIO().Textures)
+ if (tex->RefCount == 1)
+ {
+ tex->SetStatus(ImTextureStatus_WantDestroy);
+ ImGui_ImplDX9_UpdateTexture(tex);
+ }
if (bd->pVB) { bd->pVB->Release(); bd->pVB = nullptr; }
if (bd->pIB) { bd->pIB->Release(); bd->pIB = nullptr; }
- if (bd->FontTexture) { bd->FontTexture->Release(); bd->FontTexture = nullptr; ImGui::GetIO().Fonts->SetTexID(0); } // We copied bd->pFontTextureView to io.Fonts->TexID so let's clear that as well.
ImGui_ImplDX9_InvalidateDeviceObjectsForPlatformWindows();
}
@@ -433,9 +491,7 @@ void ImGui_ImplDX9_NewFrame()
{
ImGui_ImplDX9_Data* bd = ImGui_ImplDX9_GetBackendData();
IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplDX9_Init()?");
-
- if (!bd->FontTexture)
- ImGui_ImplDX9_CreateDeviceObjects();
+ IM_UNUSED(bd);
}
//--------------------------------------------------------------------------------------------------------
diff --git a/backends/imgui_impl_dx9.h b/backends/imgui_impl_dx9.h
index 983d956e9..09f1d30de 100644
--- a/backends/imgui_impl_dx9.h
+++ b/backends/imgui_impl_dx9.h
@@ -2,8 +2,9 @@
// This needs to be used along with a Platform Backend (e.g. Win32)
// Implemented features:
-// [X] Renderer: User texture binding. Use 'LPDIRECT3DTEXTURE9' as ImTextureID. Read the FAQ about ImTextureID!
+// [X] Renderer: User texture binding. Use 'LPDIRECT3DTEXTURE9' as texture identifier. Read the FAQ about ImTextureID/ImTextureRef!
// [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset).
+// [X] Renderer: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures).
// [X] Renderer: IMGUI_USE_BGRA_PACKED_COLOR support, as this is the optimal color encoding for DirectX9.
// [X] Renderer: Multi-viewport support (multiple windows). Enable with 'io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable'.
@@ -31,4 +32,7 @@ IMGUI_IMPL_API void ImGui_ImplDX9_RenderDrawData(ImDrawData* draw_data);
IMGUI_IMPL_API bool ImGui_ImplDX9_CreateDeviceObjects();
IMGUI_IMPL_API void ImGui_ImplDX9_InvalidateDeviceObjects();
+// (Advanced) Use e.g. if you need to precisely control the timing of texture updates (e.g. for staged rendering), by setting ImDrawData::Textures = NULL to handle this manually.
+IMGUI_IMPL_API void ImGui_ImplDX9_UpdateTexture(ImTextureData* tex);
+
#endif // #ifndef IMGUI_DISABLE
diff --git a/backends/imgui_impl_glfw.cpp b/backends/imgui_impl_glfw.cpp
index 0d4c8759f..b204eef37 100644
--- a/backends/imgui_impl_glfw.cpp
+++ b/backends/imgui_impl_glfw.cpp
@@ -10,10 +10,11 @@
// [X] Platform: Gamepad support. Enable with 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad'.
// [X] Platform: Mouse cursor shape and visibility (ImGuiBackendFlags_HasMouseCursors). Resizing cursors requires GLFW 3.4+! Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'.
// [X] Platform: Multi-viewport support (multiple windows). Enable with 'io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable'.
+// [X] Multiple Dear ImGui contexts support.
// Missing features or Issues:
-// [ ] Touch events are only correctly identified as Touch on Windows. This create issues with some interactions. GLFW doesn't provide a way to identify touch inputs from mouse inputs, we cannot call io.AddMouseSourceEvent() to identify the source. We provide a Windows-specific workaround.
-// [ ] Missing ImGuiMouseCursor_Wait and ImGuiMouseCursor_Progress cursors.
-// [ ] Multi-viewport: ParentViewportID not honored, and so io.ConfigViewportsNoDefaultParent has no effect (minor).
+// [ ] Platform: Touch events are only correctly identified as Touch on Windows. This create issues with some interactions. GLFW doesn't provide a way to identify touch inputs from mouse inputs, we cannot call io.AddMouseSourceEvent() to identify the source. We provide a Windows-specific workaround.
+// [ ] Platform: Missing ImGuiMouseCursor_Wait and ImGuiMouseCursor_Progress cursors.
+// [ ] Platform: Multi-viewport: Missing ImGuiBackendFlags_HasParentViewport support. The viewport->ParentViewportID field is ignored, and therefore io.ConfigViewportsNoDefaultParent has no effect either.
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
@@ -31,6 +32,14 @@
// CHANGELOG
// (minor and older changes stripped away, please see git history for details)
// 2025-XX-XX: Platform: Added support for multiple windows via the ImGuiPlatformIO interface.
+// 2025-09-18: Call platform_io.ClearPlatformHandlers() on shutdown.
+// 2025-09-15: Content Scales are always reported as 1.0 on Wayland. FramebufferScale are always reported as 1.0 on X11. (#8920, #8921)
+// 2025-09-10: [Docking] Improve multi-viewport behavior in tiling WMs on X11 via the ImGui_ImplGlfw_SetWindowFloating() function. Note: using GLFW backend on Linux/BSD etc. requires linking with -lX11. (#8884, #8474, #8289)
+// 2025-07-08: Made ImGui_ImplGlfw_GetContentScaleForWindow(), ImGui_ImplGlfw_GetContentScaleForMonitor() helpers return 1.0f on Emscripten and Android platforms, matching macOS logic. (#8742, #8733)
+// 2025-06-18: Added support for multiple Dear ImGui contexts. (#8676, #8239, #8069)
+// 2025-06-11: Added ImGui_ImplGlfw_GetContentScaleForWindow(GLFWwindow* window) and ImGui_ImplGlfw_GetContentScaleForMonitor(GLFWmonitor* monitor) helper to facilitate making DPI-aware apps.
+// 2025-05-15: [Docking] Add Platform_GetWindowFramebufferScale() handler, to allow varying Retina display density on multiple monitors.
+// 2025-04-26: [Docking] Disable multi-viewports under Wayland. (#8587)
// 2025-03-10: Map GLFW_KEY_WORLD_1 and GLFW_KEY_WORLD_2 into ImGuiKey_Oem102.
// 2025-03-03: Fixed clipboard handler assertion when using GLFW <= 3.2.1 compiled with asserts enabled.
// 2025-02-21: [Docking] Update monitors and work areas information every frame, as the later may change regardless of monitor changes. (#8415)
@@ -102,27 +111,44 @@
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wold-style-cast" // warning: use of old-style cast
#pragma clang diagnostic ignored "-Wsign-conversion" // warning: implicit conversion changes signedness
+#elif defined(__GNUC__)
+#pragma GCC diagnostic ignored "-Wfloat-equal" // warning: comparing floating-point with '==' or '!=' is unsafe
#endif
// GLFW
+#if defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
+#define GLFW_HAS_X11_OR_WAYLAND 1
+#else
+#define GLFW_HAS_X11_OR_WAYLAND 0
+#endif
#include
-
#ifdef _WIN32
#undef APIENTRY
-#ifndef GLFW_EXPOSE_NATIVE_WIN32
+#ifndef GLFW_EXPOSE_NATIVE_WIN32 // for glfwGetWin32Window()
#define GLFW_EXPOSE_NATIVE_WIN32
#endif
-#include // for glfwGetWin32Window()
-#endif
-#ifdef __APPLE__
-#ifndef GLFW_EXPOSE_NATIVE_COCOA
+#include
+#elif defined(__APPLE__)
+#ifndef GLFW_EXPOSE_NATIVE_COCOA // for glfwGetCocoaWindow()
#define GLFW_EXPOSE_NATIVE_COCOA
#endif
-#include // for glfwGetCocoaWindow()
+#include
+#elif GLFW_HAS_X11_OR_WAYLAND
+#ifndef GLFW_EXPOSE_NATIVE_X11 // for glfwGetX11Display(), glfwGetX11Window() on Freedesktop (Linux, BSD, etc.)
+#define GLFW_EXPOSE_NATIVE_X11
+#include
#endif
+#ifndef GLFW_EXPOSE_NATIVE_WAYLAND
+#define GLFW_EXPOSE_NATIVE_WAYLAND
+#endif
+#include
+#undef Status // X11 headers are leaking this.
+#endif
+
#ifndef _WIN32
#include // for usleep()
#endif
+#include // for snprintf()
#ifdef __EMSCRIPTEN__
#include
@@ -162,17 +188,29 @@
#define GLFW_HAS_GAMEPAD_API (GLFW_VERSION_COMBINED >= 3300) // 3.3+ glfwGetGamepadState() new api
#define GLFW_HAS_GETKEYNAME (GLFW_VERSION_COMBINED >= 3200) // 3.2+ glfwGetKeyName()
#define GLFW_HAS_GETERROR (GLFW_VERSION_COMBINED >= 3300) // 3.3+ glfwGetError()
+#define GLFW_HAS_GETPLATFORM (GLFW_VERSION_COMBINED >= 3400) // 3.4+ glfwGetPlatform()
+
+// Map GLFWWindow* to ImGuiContext*.
+// - Would be simpler if we could use glfwSetWindowUserPointer()/glfwGetWindowUserPointer(), but this is a single and shared resource.
+// - Would be simpler if we could use e.g. std::map<> as well. But we don't.
+// - This is not particularly optimized as we expect size to be small and queries to be rare.
+struct ImGui_ImplGlfw_WindowToContext { GLFWwindow* Window; ImGuiContext* Context; };
+static ImVector g_ContextMap;
+static void ImGui_ImplGlfw_ContextMap_Add(GLFWwindow* window, ImGuiContext* ctx) { g_ContextMap.push_back(ImGui_ImplGlfw_WindowToContext{ window, ctx }); }
+static void ImGui_ImplGlfw_ContextMap_Remove(GLFWwindow* window) { for (ImGui_ImplGlfw_WindowToContext& entry : g_ContextMap) if (entry.Window == window) { g_ContextMap.erase_unsorted(&entry); return; } }
+static ImGuiContext* ImGui_ImplGlfw_ContextMap_Get(GLFWwindow* window) { for (ImGui_ImplGlfw_WindowToContext& entry : g_ContextMap) if (entry.Window == window) return entry.Context; return nullptr; }
-// GLFW data
enum GlfwClientApi
{
- GlfwClientApi_Unknown,
GlfwClientApi_OpenGL,
GlfwClientApi_Vulkan,
+ GlfwClientApi_Unknown, // Anything else fits here.
};
+// GLFW data
struct ImGui_ImplGlfw_Data
{
+ ImGuiContext* Context;
GLFWwindow* Window;
GlfwClientApi ClientApi;
double Time;
@@ -182,8 +220,10 @@ struct ImGui_ImplGlfw_Data
bool MouseIgnoreButtonUp;
ImVec2 LastValidMousePos;
GLFWwindow* KeyOwnerWindows[GLFW_KEY_LAST];
+ bool IsWayland;
bool InstalledCallbacks;
bool CallbacksChainForAllWindows;
+ char BackendPlatformName[32];
#ifdef EMSCRIPTEN_USE_EMBEDDED_GLFW3
const char* CanvasSelector;
#endif
@@ -211,10 +251,18 @@ struct ImGui_ImplGlfw_Data
// (passing install_callbacks=false in ImGui_ImplGlfw_InitXXX functions), set the current dear imgui context and then call our callbacks.
// - Otherwise we may need to store a GLFWWindow* -> ImGuiContext* map and handle this in the backend, adding a little bit of extra complexity to it.
// FIXME: some shared resources (mouse cursor shape, gamepad) are mishandled when using multi-context.
+namespace ImGui { extern ImGuiIO& GetIO(ImGuiContext*); }
static ImGui_ImplGlfw_Data* ImGui_ImplGlfw_GetBackendData()
{
+ // Get data for current context
return ImGui::GetCurrentContext() ? (ImGui_ImplGlfw_Data*)ImGui::GetIO().BackendPlatformUserData : nullptr;
}
+static ImGui_ImplGlfw_Data* ImGui_ImplGlfw_GetBackendData(GLFWwindow* window)
+{
+ // Get data for a given GLFW window, regardless of current context (since GLFW events are sent together)
+ ImGuiContext* ctx = ImGui_ImplGlfw_ContextMap_Get(window);
+ return (ImGui_ImplGlfw_Data*)ImGui::GetIO(ctx).BackendPlatformUserData;
+}
// Forward Declarations
static void ImGui_ImplGlfw_UpdateMonitors();
@@ -222,6 +270,23 @@ static void ImGui_ImplGlfw_InitMultiViewportSupport();
static void ImGui_ImplGlfw_ShutdownMultiViewportSupport();
// Functions
+static bool ImGui_ImplGlfw_IsWayland()
+{
+#if !GLFW_HAS_X11_OR_WAYLAND
+ return false;
+#elif GLFW_HAS_GETPLATFORM
+ return glfwGetPlatform() == GLFW_PLATFORM_WAYLAND;
+#else
+ const char* version = glfwGetVersionString();
+ if (strstr(version, "Wayland") == NULL) // e.g. Ubuntu 22.04 ships with GLFW 3.3.6 compiled without Wayland
+ return false;
+#ifdef GLFW_EXPOSE_NATIVE_X11
+ if (glfwGetX11Display() != NULL)
+ return false;
+#endif
+ return true;
+#endif
+}
// Not static to allow third-party code to use that if they want to (but undocumented)
ImGuiKey ImGui_ImplGlfw_KeyToImGuiKey(int keycode, int scancode);
@@ -355,42 +420,39 @@ ImGuiKey ImGui_ImplGlfw_KeyToImGuiKey(int keycode, int scancode)
// X11 does not include current pressed/released modifier key in 'mods' flags submitted by GLFW
// See https://github.com/ocornut/imgui/issues/6034 and https://github.com/glfw/glfw/issues/1630
-static void ImGui_ImplGlfw_UpdateKeyModifiers(GLFWwindow* window)
+static void ImGui_ImplGlfw_UpdateKeyModifiers(ImGuiIO& io, GLFWwindow* window)
{
- ImGuiIO& io = ImGui::GetIO();
io.AddKeyEvent(ImGuiMod_Ctrl, (glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS) || (glfwGetKey(window, GLFW_KEY_RIGHT_CONTROL) == GLFW_PRESS));
io.AddKeyEvent(ImGuiMod_Shift, (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) || (glfwGetKey(window, GLFW_KEY_RIGHT_SHIFT) == GLFW_PRESS));
io.AddKeyEvent(ImGuiMod_Alt, (glfwGetKey(window, GLFW_KEY_LEFT_ALT) == GLFW_PRESS) || (glfwGetKey(window, GLFW_KEY_RIGHT_ALT) == GLFW_PRESS));
io.AddKeyEvent(ImGuiMod_Super, (glfwGetKey(window, GLFW_KEY_LEFT_SUPER) == GLFW_PRESS) || (glfwGetKey(window, GLFW_KEY_RIGHT_SUPER) == GLFW_PRESS));
}
-static bool ImGui_ImplGlfw_ShouldChainCallback(GLFWwindow* window)
+static bool ImGui_ImplGlfw_ShouldChainCallback(ImGui_ImplGlfw_Data* bd, GLFWwindow* window)
{
- ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
return bd->CallbacksChainForAllWindows ? true : (window == bd->Window);
}
void ImGui_ImplGlfw_MouseButtonCallback(GLFWwindow* window, int button, int action, int mods)
{
- ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
- if (bd->PrevUserCallbackMousebutton != nullptr && ImGui_ImplGlfw_ShouldChainCallback(window))
+ ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(window);
+ if (bd->PrevUserCallbackMousebutton != nullptr && ImGui_ImplGlfw_ShouldChainCallback(bd, window))
bd->PrevUserCallbackMousebutton(window, button, action, mods);
// Workaround for Linux: ignore mouse up events which are following an focus loss following a viewport creation
if (bd->MouseIgnoreButtonUp && action == GLFW_RELEASE)
return;
- ImGui_ImplGlfw_UpdateKeyModifiers(window);
-
- ImGuiIO& io = ImGui::GetIO();
+ ImGuiIO& io = ImGui::GetIO(bd->Context);
+ ImGui_ImplGlfw_UpdateKeyModifiers(io, window);
if (button >= 0 && button < ImGuiMouseButton_COUNT)
io.AddMouseButtonEvent(button, action == GLFW_PRESS);
}
void ImGui_ImplGlfw_ScrollCallback(GLFWwindow* window, double xoffset, double yoffset)
{
- ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
- if (bd->PrevUserCallbackScroll != nullptr && ImGui_ImplGlfw_ShouldChainCallback(window))
+ ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(window);
+ if (bd->PrevUserCallbackScroll != nullptr && ImGui_ImplGlfw_ShouldChainCallback(bd, window))
bd->PrevUserCallbackScroll(window, xoffset, yoffset);
#ifdef EMSCRIPTEN_USE_EMBEDDED_GLFW3
@@ -398,7 +460,7 @@ void ImGui_ImplGlfw_ScrollCallback(GLFWwindow* window, double xoffset, double yo
return;
#endif
- ImGuiIO& io = ImGui::GetIO();
+ ImGuiIO& io = ImGui::GetIO(bd->Context);
io.AddMouseWheelEvent((float)xoffset, (float)yoffset);
}
@@ -438,21 +500,21 @@ static int ImGui_ImplGlfw_TranslateUntranslatedKey(int key, int scancode)
void ImGui_ImplGlfw_KeyCallback(GLFWwindow* window, int keycode, int scancode, int action, int mods)
{
- ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
- if (bd->PrevUserCallbackKey != nullptr && ImGui_ImplGlfw_ShouldChainCallback(window))
+ ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(window);
+ if (bd->PrevUserCallbackKey != nullptr && ImGui_ImplGlfw_ShouldChainCallback(bd, window))
bd->PrevUserCallbackKey(window, keycode, scancode, action, mods);
if (action != GLFW_PRESS && action != GLFW_RELEASE)
return;
- ImGui_ImplGlfw_UpdateKeyModifiers(window);
+ ImGuiIO& io = ImGui::GetIO(bd->Context);
+ ImGui_ImplGlfw_UpdateKeyModifiers(io, window);
if (keycode >= 0 && keycode < IM_ARRAYSIZE(bd->KeyOwnerWindows))
bd->KeyOwnerWindows[keycode] = (action == GLFW_PRESS) ? window : nullptr;
keycode = ImGui_ImplGlfw_TranslateUntranslatedKey(keycode, scancode);
- ImGuiIO& io = ImGui::GetIO();
ImGuiKey imgui_key = ImGui_ImplGlfw_KeyToImGuiKey(keycode, scancode);
io.AddKeyEvent(imgui_key, (action == GLFW_PRESS));
io.SetKeyEventNativeData(imgui_key, keycode, scancode); // To support legacy indexing (<1.87 user code)
@@ -460,25 +522,25 @@ void ImGui_ImplGlfw_KeyCallback(GLFWwindow* window, int keycode, int scancode, i
void ImGui_ImplGlfw_WindowFocusCallback(GLFWwindow* window, int focused)
{
- ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
- if (bd->PrevUserCallbackWindowFocus != nullptr && ImGui_ImplGlfw_ShouldChainCallback(window))
+ ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(window);
+ if (bd->PrevUserCallbackWindowFocus != nullptr && ImGui_ImplGlfw_ShouldChainCallback(bd, window))
bd->PrevUserCallbackWindowFocus(window, focused);
// Workaround for Linux: when losing focus with MouseIgnoreButtonUpWaitForFocusLoss set, we will temporarily ignore subsequent Mouse Up events
bd->MouseIgnoreButtonUp = (bd->MouseIgnoreButtonUpWaitForFocusLoss && focused == 0);
bd->MouseIgnoreButtonUpWaitForFocusLoss = false;
- ImGuiIO& io = ImGui::GetIO();
+ ImGuiIO& io = ImGui::GetIO(bd->Context);
io.AddFocusEvent(focused != 0);
}
void ImGui_ImplGlfw_CursorPosCallback(GLFWwindow* window, double x, double y)
{
- ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
- if (bd->PrevUserCallbackCursorPos != nullptr && ImGui_ImplGlfw_ShouldChainCallback(window))
+ ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(window);
+ if (bd->PrevUserCallbackCursorPos != nullptr && ImGui_ImplGlfw_ShouldChainCallback(bd, window))
bd->PrevUserCallbackCursorPos(window, x, y);
- ImGuiIO& io = ImGui::GetIO();
+ ImGuiIO& io = ImGui::GetIO(bd->Context);
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{
int window_x, window_y;
@@ -494,11 +556,11 @@ void ImGui_ImplGlfw_CursorPosCallback(GLFWwindow* window, double x, double y)
// so we back it up and restore on Leave/Enter (see https://github.com/ocornut/imgui/issues/4984)
void ImGui_ImplGlfw_CursorEnterCallback(GLFWwindow* window, int entered)
{
- ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
- if (bd->PrevUserCallbackCursorEnter != nullptr && ImGui_ImplGlfw_ShouldChainCallback(window))
+ ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(window);
+ if (bd->PrevUserCallbackCursorEnter != nullptr && ImGui_ImplGlfw_ShouldChainCallback(bd, window))
bd->PrevUserCallbackCursorEnter(window, entered);
- ImGuiIO& io = ImGui::GetIO();
+ ImGuiIO& io = ImGui::GetIO(bd->Context);
if (entered)
{
bd->MouseWindow = window;
@@ -514,11 +576,11 @@ void ImGui_ImplGlfw_CursorEnterCallback(GLFWwindow* window, int entered)
void ImGui_ImplGlfw_CharCallback(GLFWwindow* window, unsigned int c)
{
- ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
- if (bd->PrevUserCallbackChar != nullptr && ImGui_ImplGlfw_ShouldChainCallback(window))
+ ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(window);
+ if (bd->PrevUserCallbackChar != nullptr && ImGui_ImplGlfw_ShouldChainCallback(bd, window))
bd->PrevUserCallbackChar(window, c);
- ImGuiIO& io = ImGui::GetIO();
+ ImGuiIO& io = ImGui::GetIO(bd->Context);
io.AddInputCharacter(c);
}
@@ -528,17 +590,18 @@ void ImGui_ImplGlfw_MonitorCallback(GLFWmonitor*, int)
}
#ifdef EMSCRIPTEN_USE_EMBEDDED_GLFW3
-static EM_BOOL ImGui_ImplEmscripten_WheelCallback(int, const EmscriptenWheelEvent* ev, void*)
+static EM_BOOL ImGui_ImplEmscripten_WheelCallback(int, const EmscriptenWheelEvent* ev, void* user_data)
{
// Mimic Emscripten_HandleWheel() in SDL.
// Corresponding equivalent in GLFW JS emulation layer has incorrect quantizing preventing small values. See #6096
+ ImGui_ImplGlfw_Data* bd = (ImGui_ImplGlfw_Data*)user_data;
float multiplier = 0.0f;
if (ev->deltaMode == DOM_DELTA_PIXEL) { multiplier = 1.0f / 100.0f; } // 100 pixels make up a step.
else if (ev->deltaMode == DOM_DELTA_LINE) { multiplier = 1.0f / 3.0f; } // 3 lines make up a step.
else if (ev->deltaMode == DOM_DELTA_PAGE) { multiplier = 80.0f; } // A page makes up 80 steps.
float wheel_x = ev->deltaX * -multiplier;
float wheel_y = ev->deltaY * -multiplier;
- ImGuiIO& io = ImGui::GetIO();
+ ImGuiIO& io = ImGui::GetIO(bd->Context);
io.AddMouseWheelEvent(wheel_x, wheel_y);
//IMGUI_DEBUG_LOG("[Emsc] mode %d dx: %.2f, dy: %.2f, dz: %.2f --> feed %.2f %.2f\n", (int)ev->deltaMode, ev->deltaX, ev->deltaY, ev->deltaZ, wheel_x, wheel_y);
return EM_TRUE;
@@ -551,7 +614,7 @@ static LRESULT CALLBACK ImGui_ImplGlfw_WndProc(HWND hWnd, UINT msg, WPARAM wPara
void ImGui_ImplGlfw_InstallCallbacks(GLFWwindow* window)
{
- ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
+ ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(window);
IM_ASSERT(bd->InstalledCallbacks == false && "Callbacks already installed!");
IM_ASSERT(bd->Window == window);
@@ -568,7 +631,7 @@ void ImGui_ImplGlfw_InstallCallbacks(GLFWwindow* window)
void ImGui_ImplGlfw_RestoreCallbacks(GLFWwindow* window)
{
- ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
+ ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(window);
IM_ASSERT(bd->InstalledCallbacks == true && "Callbacks not installed!");
IM_ASSERT(bd->Window == window);
@@ -591,7 +654,7 @@ void ImGui_ImplGlfw_RestoreCallbacks(GLFWwindow* window)
bd->PrevUserCallbackMonitor = nullptr;
}
-// Set to 'true' to enable chaining installed callbacks for all windows (including secondary viewports created by backends or by user.
+// Set to 'true' to enable chaining installed callbacks for all windows (including secondary viewports created by backends or by user).
// This is 'false' by default meaning we only chain callbacks for the main viewport.
// We cannot set this to 'true' by default because user callbacks code may be not testing the 'window' parameter of their callback.
// If you set this to 'true' your user callback code will need to make sure you are testing the 'window' parameter.
@@ -618,19 +681,31 @@ static bool ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks, Glfw
// Setup backend capabilities flags
ImGui_ImplGlfw_Data* bd = IM_NEW(ImGui_ImplGlfw_Data)();
+ snprintf(bd->BackendPlatformName, sizeof(bd->BackendPlatformName), "imgui_impl_glfw (%d)", GLFW_VERSION_COMBINED);
io.BackendPlatformUserData = (void*)bd;
- io.BackendPlatformName = "imgui_impl_glfw";
+ io.BackendPlatformName = bd->BackendPlatformName;
io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional)
io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used)
+
+ bool has_viewports = false;
#ifndef __EMSCRIPTEN__
- io.BackendFlags |= ImGuiBackendFlags_PlatformHasViewports; // We can create multi-viewports on the Platform side (optional)
+ has_viewports = true;
+#if GLFW_HAS_GETPLATFORM
+ if (glfwGetPlatform() == GLFW_PLATFORM_WAYLAND)
+ has_viewports = false;
+#endif
+ if (has_viewports)
+ io.BackendFlags |= ImGuiBackendFlags_PlatformHasViewports; // We can create multi-viewports on the Platform side (optional)
#endif
#if GLFW_HAS_MOUSE_PASSTHROUGH || GLFW_HAS_WINDOW_HOVERED
io.BackendFlags |= ImGuiBackendFlags_HasMouseHoveredViewport; // We can call io.AddMouseViewportEvent() with correct data (optional)
#endif
+ bd->Context = ImGui::GetCurrentContext();
bd->Window = window;
bd->Time = 0.0;
+ bd->IsWayland = ImGui_ImplGlfw_IsWayland();
+ ImGui_ImplGlfw_ContextMap_Add(window, bd->Context);
ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
#if GLFW_VERSION_COMBINED < 3300
@@ -690,11 +765,14 @@ static bool ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks, Glfw
#else
IM_UNUSED(main_viewport);
#endif
- ImGui_ImplGlfw_InitMultiViewportSupport();
+ if (has_viewports)
+ ImGui_ImplGlfw_InitMultiViewportSupport();
// Windows: register a WndProc hook so we can intercept some messages.
#ifdef _WIN32
- bd->PrevWndProc = (WNDPROC)::GetWindowLongPtrW((HWND)main_viewport->PlatformHandleRaw, GWLP_WNDPROC);
+ HWND hwnd = (HWND)main_viewport->PlatformHandleRaw;
+ ::SetPropA(hwnd, "IMGUI_BACKEND_DATA", bd);
+ bd->PrevWndProc = (WNDPROC)::GetWindowLongPtrW(hwnd, GWLP_WNDPROC);
IM_ASSERT(bd->PrevWndProc != nullptr);
::SetWindowLongPtrW((HWND)main_viewport->PlatformHandleRaw, GWLP_WNDPROC, (LONG_PTR)ImGui_ImplGlfw_WndProc);
#endif
@@ -705,7 +783,7 @@ static bool ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks, Glfw
#if EMSCRIPTEN_USE_PORT_CONTRIB_GLFW3 >= 34020240817
if (emscripten::glfw3::IsRuntimePlatformApple())
{
- ImGui::GetIO().ConfigMacOSXBehaviors = true;
+ io.ConfigMacOSXBehaviors = true;
// Due to how the browser (poorly) handles the Meta Key, this line essentially disables repeats when used.
// This means that Meta + V only registers a single key-press, even if the keys are held.
@@ -739,10 +817,11 @@ void ImGui_ImplGlfw_Shutdown()
{
ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
IM_ASSERT(bd != nullptr && "No platform backend to shutdown, or already shutdown?");
+
ImGuiIO& io = ImGui::GetIO();
+ ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
ImGui_ImplGlfw_ShutdownMultiViewportSupport();
-
if (bd->InstalledCallbacks)
ImGui_ImplGlfw_RestoreCallbacks(bd->Window);
#ifdef EMSCRIPTEN_USE_EMBEDDED_GLFW3
@@ -756,6 +835,7 @@ void ImGui_ImplGlfw_Shutdown()
// Windows: restore our WndProc hook
#ifdef _WIN32
ImGuiViewport* main_viewport = ImGui::GetMainViewport();
+ ::SetPropA((HWND)main_viewport->PlatformHandleRaw, "IMGUI_BACKEND_DATA", nullptr);
::SetWindowLongPtrW((HWND)main_viewport->PlatformHandleRaw, GWLP_WNDPROC, (LONG_PTR)bd->PrevWndProc);
bd->PrevWndProc = nullptr;
#endif
@@ -763,6 +843,8 @@ void ImGui_ImplGlfw_Shutdown()
io.BackendPlatformName = nullptr;
io.BackendPlatformUserData = nullptr;
io.BackendFlags &= ~(ImGuiBackendFlags_HasMouseCursors | ImGuiBackendFlags_HasSetMousePos | ImGuiBackendFlags_HasGamepad | ImGuiBackendFlags_PlatformHasViewports | ImGuiBackendFlags_HasMouseHoveredViewport);
+ platform_io.ClearPlatformHandlers();
+ ImGui_ImplGlfw_ContextMap_Remove(bd->Window);
IM_DELETE(bd);
}
@@ -946,33 +1028,78 @@ static void ImGui_ImplGlfw_UpdateMonitors()
monitor.WorkSize = ImVec2((float)w, (float)h);
}
#endif
-#if GLFW_HAS_PER_MONITOR_DPI
- // Warning: the validity of monitor DPI information on Windows depends on the application DPI awareness settings, which generally needs to be set in the manifest or at runtime.
- float x_scale, y_scale;
- glfwGetMonitorContentScale(glfw_monitors[n], &x_scale, &y_scale);
- if (x_scale == 0.0f)
+ float scale = ImGui_ImplGlfw_GetContentScaleForMonitor(glfw_monitors[n]);
+ if (scale == 0.0f)
continue; // Some accessibility applications are declaring virtual monitors with a DPI of 0, see #7902.
- monitor.DpiScale = x_scale;
-#endif
+ monitor.DpiScale = scale;
monitor.PlatformHandle = (void*)glfw_monitors[n]; // [...] GLFW doc states: "guaranteed to be valid only until the monitor configuration changes"
platform_io.Monitors.push_back(monitor);
}
}
+// - On Windows the process needs to be marked DPI-aware!! SDL2 doesn't do it by default. You can call ::SetProcessDPIAware() or call ImGui_ImplWin32_EnableDpiAwareness() from Win32 backend.
+// - Apple platforms use FramebufferScale so we always return 1.0f.
+// - Some accessibility applications are declaring virtual monitors with a DPI of 0.0f, see #7902. We preserve this value for caller to handle.
+float ImGui_ImplGlfw_GetContentScaleForWindow(GLFWwindow* window)
+{
+#if GLFW_HAS_X11_OR_WAYLAND
+ if (ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(window))
+ if (bd->IsWayland)
+ return 1.0f;
+#endif
+#if GLFW_HAS_PER_MONITOR_DPI && !(defined(__APPLE__) || defined(__EMSCRIPTEN__) || defined(__ANDROID__))
+ float x_scale, y_scale;
+ glfwGetWindowContentScale(window, &x_scale, &y_scale);
+ return x_scale;
+#else
+ IM_UNUSED(window);
+ return 1.0f;
+#endif
+}
+
+float ImGui_ImplGlfw_GetContentScaleForMonitor(GLFWmonitor* monitor)
+{
+#if GLFW_HAS_X11_OR_WAYLAND
+ if (ImGui_ImplGlfw_IsWayland()) // We can't access our bd->IsWayland cache for a monitor.
+ return 1.0f;
+#endif
+#if GLFW_HAS_PER_MONITOR_DPI && !(defined(__APPLE__) || defined(__EMSCRIPTEN__) || defined(__ANDROID__))
+ float x_scale, y_scale;
+ glfwGetMonitorContentScale(monitor, &x_scale, &y_scale);
+ return x_scale;
+#else
+ IM_UNUSED(monitor);
+ return 1.0f;
+#endif
+}
+
+static void ImGui_ImplGlfw_GetWindowSizeAndFramebufferScale(GLFWwindow* window, ImVec2* out_size, ImVec2* out_framebuffer_scale)
+{
+ int w, h;
+ int display_w, display_h;
+ glfwGetWindowSize(window, &w, &h);
+ glfwGetFramebufferSize(window, &display_w, &display_h);
+ float fb_scale_x = (w > 0) ? (float)display_w / w : 1.0f;
+ float fb_scale_y = (h > 0) ? (float)display_h / h : 1.0f;
+#if GLFW_HAS_X11_OR_WAYLAND
+ ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(window);
+ if (!bd->IsWayland)
+ fb_scale_x = fb_scale_y = 1.0f;
+#endif
+ if (out_size != nullptr)
+ *out_size = ImVec2((float)w, (float)h);
+ if (out_framebuffer_scale != nullptr)
+ *out_framebuffer_scale = ImVec2(fb_scale_x, fb_scale_y);
+}
+
void ImGui_ImplGlfw_NewFrame()
{
ImGuiIO& io = ImGui::GetIO();
ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplGlfw_InitForXXX()?");
- // Setup display size (every frame to accommodate for window resizing)
- int w, h;
- int display_w, display_h;
- glfwGetWindowSize(bd->Window, &w, &h);
- glfwGetFramebufferSize(bd->Window, &display_w, &display_h);
- io.DisplaySize = ImVec2((float)w, (float)h);
- if (w > 0 && h > 0)
- io.DisplayFramebufferScale = ImVec2((float)display_w / (float)w, (float)display_h / (float)h);
+ // Setup main viewport size (every frame to accommodate for window resizing)
+ ImGui_ImplGlfw_GetWindowSizeAndFramebufferScale(bd->Window, &io.DisplaySize, &io.DisplayFramebufferScale);
ImGui_ImplGlfw_UpdateMonitors();
// Setup time step
@@ -1038,7 +1165,7 @@ void ImGui_ImplGlfw_InstallEmscriptenCallbacks(GLFWwindow*, const char* canvas_s
// Register Emscripten Wheel callback to workaround issue in Emscripten GLFW Emulation (#6096)
// We intentionally do not check 'if (install_callbacks)' here, as some users may set it to false and call GLFW callback themselves.
// FIXME: May break chaining in case user registered their own Emscripten callback?
- emscripten_set_wheel_callback(bd->CanvasSelector, nullptr, false, ImGui_ImplEmscripten_WheelCallback);
+ emscripten_set_wheel_callback(bd->CanvasSelector, bd, false, ImGui_ImplEmscripten_WheelCallback);
}
#elif defined(EMSCRIPTEN_USE_PORT_CONTRIB_GLFW3)
// When using --use-port=contrib.glfw3 for the GLFW implementation, you can override the behavior of this call
@@ -1117,6 +1244,30 @@ static void ImGui_ImplGlfw_WindowSizeCallback(GLFWwindow* window, int, int)
}
}
+#if !defined(__APPLE__) && !defined(_WIN32) && !defined(__EMSCRIPTEN__) && GLFW_HAS_GETPLATFORM
+#define IMGUI_GLFW_HAS_SETWINDOWFLOATING
+static void ImGui_ImplGlfw_SetWindowFloating(GLFWwindow* window)
+{
+#ifdef GLFW_EXPOSE_NATIVE_X11
+ if (glfwGetPlatform() == GLFW_PLATFORM_X11)
+ {
+ Display* display = glfwGetX11Display();
+ Window xwindow = glfwGetX11Window(window);
+ Atom wm_type = XInternAtom(display, "_NET_WM_WINDOW_TYPE", False);
+ Atom wm_type_dialog = XInternAtom(display, "_NET_WM_WINDOW_TYPE_DIALOG", False);
+ XChangeProperty(display, xwindow, wm_type, XA_ATOM, 32, PropModeReplace, (unsigned char*)&wm_type_dialog, 1);
+ XSetWindowAttributes attrs;
+ attrs.override_redirect = False;
+ XChangeWindowAttributes(display, xwindow, CWOverrideRedirect, &attrs);
+ XFlush(display);
+ }
+#endif // GLFW_EXPOSE_NATIVE_X11
+#ifdef GLFW_EXPOSE_NATIVE_WAYLAND
+ // FIXME: Help needed, see #8884, #8474 for discussions about this.
+#endif // GLFW_EXPOSE_NATIVE_X11
+}
+#endif // IMGUI_GLFW_HAS_SETWINDOWFLOATING
+
static void ImGui_ImplGlfw_CreateWindow(ImGuiViewport* viewport)
{
ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
@@ -1134,7 +1285,7 @@ static void ImGui_ImplGlfw_CreateWindow(ImGuiViewport* viewport)
glfwWindowHint(GLFW_FOCUSED, false);
#if GLFW_HAS_FOCUS_ON_SHOW
glfwWindowHint(GLFW_FOCUS_ON_SHOW, false);
- #endif
+#endif
glfwWindowHint(GLFW_DECORATED, (viewport->Flags & ImGuiViewportFlags_NoDecoration) ? false : true);
#if GLFW_HAS_WINDOW_TOPMOST
glfwWindowHint(GLFW_FLOATING, (viewport->Flags & ImGuiViewportFlags_TopMost) ? true : false);
@@ -1142,9 +1293,14 @@ static void ImGui_ImplGlfw_CreateWindow(ImGuiViewport* viewport)
GLFWwindow* share_window = (bd->ClientApi == GlfwClientApi_OpenGL) ? bd->Window : nullptr;
vd->Window = glfwCreateWindow((int)viewport->Size.x, (int)viewport->Size.y, "No Title Yet", nullptr, share_window);
vd->WindowOwned = true;
+ ImGui_ImplGlfw_ContextMap_Add(vd->Window, bd->Context);
viewport->PlatformHandle = (void*)vd->Window;
+#ifdef IMGUI_GLFW_HAS_SETWINDOWFLOATING
+ ImGui_ImplGlfw_SetWindowFloating(vd->Window);
+#endif
#ifdef _WIN32
viewport->PlatformHandleRaw = glfwGetWin32Window(vd->Window);
+ ::SetPropA((HWND)viewport->PlatformHandleRaw, "IMGUI_BACKEND_DATA", bd);
#elif defined(__APPLE__)
viewport->PlatformHandleRaw = (void*)glfwGetCocoaWindow(vd->Window);
#endif
@@ -1186,6 +1342,7 @@ static void ImGui_ImplGlfw_DestroyWindow(ImGuiViewport* viewport)
if (bd->KeyOwnerWindows[i] == vd->Window)
ImGui_ImplGlfw_KeyCallback(vd->Window, i, 0, GLFW_RELEASE, 0); // Later params are only used for main viewport, on which this function is never called.
+ ImGui_ImplGlfw_ContextMap_Remove(vd->Window);
glfwDestroyWindow(vd->Window);
}
vd->Window = nullptr;
@@ -1209,12 +1366,10 @@ static void ImGui_ImplGlfw_ShowWindow(ImGuiViewport* viewport)
::SetWindowLong(hwnd, GWL_EXSTYLE, ex_style);
}
- // GLFW hack: install hook for WM_NCHITTEST message handler
-#if !GLFW_HAS_MOUSE_PASSTHROUGH && GLFW_HAS_WINDOW_HOVERED && defined(_WIN32)
+ // GLFW hack: install WndProc for mouse source event and WM_NCHITTEST message handler.
::SetPropA(hwnd, "IMGUI_VIEWPORT", viewport);
vd->PrevWndProc = (WNDPROC)::GetWindowLongPtrW(hwnd, GWLP_WNDPROC);
::SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (LONG_PTR)ImGui_ImplGlfw_WndProc);
-#endif
#if !GLFW_HAS_FOCUS_ON_SHOW
// GLFW hack: GLFW 3.2 has a bug where glfwShowWindow() also activates/focus the window.
@@ -1258,7 +1413,7 @@ static ImVec2 ImGui_ImplGlfw_GetWindowSize(ImGuiViewport* viewport)
static void ImGui_ImplGlfw_SetWindowSize(ImGuiViewport* viewport, ImVec2 size)
{
ImGui_ImplGlfw_ViewportData* vd = (ImGui_ImplGlfw_ViewportData*)viewport->PlatformUserData;
-#if __APPLE__ && !GLFW_HAS_OSX_WINDOW_POS_FIX
+#if defined(__APPLE__) && !GLFW_HAS_OSX_WINDOW_POS_FIX
// Native OS windows are positioned from the bottom-left corner on macOS, whereas on other platforms they are
// positioned from the upper-left corner. GLFW makes an effort to convert macOS style coordinates, however it
// doesn't handle it when changing size. We are manually moving the window in order for changes of size to be based
@@ -1272,6 +1427,14 @@ static void ImGui_ImplGlfw_SetWindowSize(ImGuiViewport* viewport, ImVec2 size)
glfwSetWindowSize(vd->Window, (int)size.x, (int)size.y);
}
+static ImVec2 ImGui_ImplGlfw_GetWindowFramebufferScale(ImGuiViewport* viewport)
+{
+ ImGui_ImplGlfw_ViewportData* vd = (ImGui_ImplGlfw_ViewportData*)viewport->PlatformUserData;
+ ImVec2 framebuffer_scale;
+ ImGui_ImplGlfw_GetWindowSizeAndFramebufferScale(vd->Window, nullptr, &framebuffer_scale);
+ return framebuffer_scale;
+}
+
static void ImGui_ImplGlfw_SetWindowTitle(ImGuiViewport* viewport, const char* title)
{
ImGui_ImplGlfw_ViewportData* vd = (ImGui_ImplGlfw_ViewportData*)viewport->PlatformUserData;
@@ -1370,6 +1533,7 @@ static void ImGui_ImplGlfw_InitMultiViewportSupport()
platform_io.Platform_GetWindowPos = ImGui_ImplGlfw_GetWindowPos;
platform_io.Platform_SetWindowSize = ImGui_ImplGlfw_SetWindowSize;
platform_io.Platform_GetWindowSize = ImGui_ImplGlfw_GetWindowSize;
+ platform_io.Platform_GetWindowFramebufferScale = ImGui_ImplGlfw_GetWindowFramebufferScale;
platform_io.Platform_SetWindowFocus = ImGui_ImplGlfw_SetWindowFocus;
platform_io.Platform_GetWindowFocus = ImGui_ImplGlfw_GetWindowFocus;
platform_io.Platform_GetWindowMinimized = ImGui_ImplGlfw_GetWindowMinimized;
@@ -1413,7 +1577,9 @@ static ImGuiMouseSource GetMouseSourceFromMessageExtraInfo()
}
static LRESULT CALLBACK ImGui_ImplGlfw_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
- ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
+ ImGui_ImplGlfw_Data* bd = (ImGui_ImplGlfw_Data*)::GetPropA(hWnd, "IMGUI_BACKEND_DATA");
+ ImGuiIO& io = ImGui::GetIO(bd->Context);
+
WNDPROC prev_wndproc = bd->PrevWndProc;
ImGuiViewport* viewport = (ImGuiViewport*)::GetPropA(hWnd, "IMGUI_VIEWPORT");
if (viewport != NULL)
@@ -1429,7 +1595,7 @@ static LRESULT CALLBACK ImGui_ImplGlfw_WndProc(HWND hWnd, UINT msg, WPARAM wPara
case WM_RBUTTONDOWN: case WM_RBUTTONDBLCLK: case WM_RBUTTONUP:
case WM_MBUTTONDOWN: case WM_MBUTTONDBLCLK: case WM_MBUTTONUP:
case WM_XBUTTONDOWN: case WM_XBUTTONDBLCLK: case WM_XBUTTONUP:
- ImGui::GetIO().AddMouseSourceEvent(GetMouseSourceFromMessageExtraInfo());
+ io.AddMouseSourceEvent(GetMouseSourceFromMessageExtraInfo());
break;
// We have submitted https://github.com/glfw/glfw/pull/1568 to allow GLFW to support "transparent inputs".
@@ -1446,6 +1612,7 @@ static LRESULT CALLBACK ImGui_ImplGlfw_WndProc(HWND hWnd, UINT msg, WPARAM wPara
break;
}
#endif
+ default: break;
}
return ::CallWindowProcW(prev_wndproc, hWnd, msg, wParam, lParam);
}
diff --git a/backends/imgui_impl_glfw.h b/backends/imgui_impl_glfw.h
index fb6f3118a..e4325d8b2 100644
--- a/backends/imgui_impl_glfw.h
+++ b/backends/imgui_impl_glfw.h
@@ -10,10 +10,11 @@
// [X] Platform: Gamepad support. Enable with 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad'.
// [X] Platform: Mouse cursor shape and visibility (ImGuiBackendFlags_HasMouseCursors). Resizing cursors requires GLFW 3.4+! Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'.
// [X] Platform: Multi-viewport support (multiple windows). Enable with 'io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable'.
+// [X] Multiple Dear ImGui contexts support.
// Missing features or Issues:
-// [ ] Touch events are only correctly identified as Touch on Windows. This create issues with some interactions. GLFW doesn't provide a way to identify touch inputs from mouse inputs, we cannot call io.AddMouseSourceEvent() to identify the source. We provide a Windows-specific workaround.
-// [ ] Missing ImGuiMouseCursor_Wait and ImGuiMouseCursor_Progress cursors.
-// [ ] Multi-viewport: ParentViewportID not honored, and so io.ConfigViewportsNoDefaultParent has no effect (minor).
+// [ ] Platform: Touch events are only correctly identified as Touch on Windows. This create issues with some interactions. GLFW doesn't provide a way to identify touch inputs from mouse inputs, we cannot call io.AddMouseSourceEvent() to identify the source. We provide a Windows-specific workaround.
+// [ ] Platform: Missing ImGuiMouseCursor_Wait and ImGuiMouseCursor_Progress cursors.
+// [ ] Platform: Multi-viewport: Missing ImGuiBackendFlags_HasParentViewport support. The viewport->ParentViewportID field is ignored, and therefore io.ConfigViewportsNoDefaultParent has no effect either.
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
@@ -65,5 +66,8 @@ IMGUI_IMPL_API void ImGui_ImplGlfw_MonitorCallback(GLFWmonitor* monitor, int
// GLFW helpers
IMGUI_IMPL_API void ImGui_ImplGlfw_Sleep(int milliseconds);
+IMGUI_IMPL_API float ImGui_ImplGlfw_GetContentScaleForWindow(GLFWwindow* window);
+IMGUI_IMPL_API float ImGui_ImplGlfw_GetContentScaleForMonitor(GLFWmonitor* monitor);
+
#endif // #ifndef IMGUI_DISABLE
diff --git a/backends/imgui_impl_metal.h b/backends/imgui_impl_metal.h
index f2a8be7d3..430bba8f0 100644
--- a/backends/imgui_impl_metal.h
+++ b/backends/imgui_impl_metal.h
@@ -2,8 +2,9 @@
// This needs to be used along with a Platform Backend (e.g. OSX)
// Implemented features:
-// [X] Renderer: User texture binding. Use 'MTLTexture' as ImTextureID. Read the FAQ about ImTextureID!
+// [X] Renderer: User texture binding. Use 'MTLTexture' as texture identifier. Read the FAQ about ImTextureID/ImTextureRef!
// [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset).
+// [X] Renderer: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures).
// [X] Renderer: Multi-viewport support (multiple windows). Enable with 'io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable'.
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
@@ -36,11 +37,12 @@ IMGUI_IMPL_API void ImGui_ImplMetal_RenderDrawData(ImDrawData* drawData,
id commandEncoder);
// Called by Init/NewFrame/Shutdown
-IMGUI_IMPL_API bool ImGui_ImplMetal_CreateFontsTexture(id device);
-IMGUI_IMPL_API void ImGui_ImplMetal_DestroyFontsTexture();
IMGUI_IMPL_API bool ImGui_ImplMetal_CreateDeviceObjects(id device);
IMGUI_IMPL_API void ImGui_ImplMetal_DestroyDeviceObjects();
+// (Advanced) Use e.g. if you need to precisely control the timing of texture updates (e.g. for staged rendering), by setting ImDrawData::Textures = NULL to handle this manually.
+IMGUI_IMPL_API void ImGui_ImplMetal_UpdateTexture(ImTextureData* tex);
+
#endif
//-----------------------------------------------------------------------------
@@ -63,11 +65,12 @@ IMGUI_IMPL_API void ImGui_ImplMetal_RenderDrawData(ImDrawData* draw_data,
MTL::RenderCommandEncoder* commandEncoder);
// Called by Init/NewFrame/Shutdown
-IMGUI_IMPL_API bool ImGui_ImplMetal_CreateFontsTexture(MTL::Device* device);
-IMGUI_IMPL_API void ImGui_ImplMetal_DestroyFontsTexture();
IMGUI_IMPL_API bool ImGui_ImplMetal_CreateDeviceObjects(MTL::Device* device);
IMGUI_IMPL_API void ImGui_ImplMetal_DestroyDeviceObjects();
+// (Advanced) Use e.g. if you need to precisely control the timing of texture updates (e.g. for staged rendering), by setting ImDrawData::Textures = NULL to handle this manually.
+IMGUI_IMPL_API void ImGui_ImplMetal_UpdateTexture(ImTextureData* tex);
+
#endif
#endif
diff --git a/backends/imgui_impl_metal.mm b/backends/imgui_impl_metal.mm
index 9ab4a342a..2ce95223f 100644
--- a/backends/imgui_impl_metal.mm
+++ b/backends/imgui_impl_metal.mm
@@ -2,8 +2,9 @@
// This needs to be used along with a Platform Backend (e.g. OSX)
// Implemented features:
-// [X] Renderer: User texture binding. Use 'MTLTexture' as ImTextureID. Read the FAQ about ImTextureID!
+// [X] Renderer: User texture binding. Use 'MTLTexture' as texture identifier. Read the FAQ about ImTextureID/ImTextureRef!
// [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset).
+// [X] Renderer: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures).
// [X] Renderer: Multi-viewport support (multiple windows). Enable with 'io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable'.
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
@@ -17,6 +18,8 @@
// CHANGELOG
// (minor and older changes stripped away, please see git history for details)
// 2025-XX-XX: Metal: Added support for multiple windows via the ImGuiPlatformIO interface.
+// 2025-09-18: Call platform_io.ClearRendererHandlers() on shutdown.
+// 2025-06-11: Added support for ImGuiBackendFlags_RendererHasTextures, for dynamic font atlas. Removed ImGui_ImplMetal_CreateFontsTexture() and ImGui_ImplMetal_DestroyFontsTexture().
// 2025-02-03: Metal: Crash fix. (#8367)
// 2025-01-08: Metal: Fixed memory leaks when using metal-cpp (#8276, #8166) or when using multiple contexts (#7419).
// 2022-08-23: Metal: Update deprecated property 'sampleCount'->'rasterSampleCount'.
@@ -67,6 +70,11 @@ static void ImGui_ImplMetal_InvalidateDeviceObjectsForPlatformWindows();
- (instancetype)initWithRenderPassDescriptor:(MTLRenderPassDescriptor*)renderPassDescriptor;
@end
+@interface MetalTexture : NSObject
+@property (nonatomic, strong) id metalTexture;
+- (instancetype)initWithTexture:(id)metalTexture;
+@end
+
// A singleton that stores long-lived objects that are needed by the Metal
// renderer backend. Stores the render pipeline state cache and the default
// font texture, and manages the reusable buffer cache.
@@ -75,7 +83,6 @@ static void ImGui_ImplMetal_InvalidateDeviceObjectsForPlatformWindows();
@property (nonatomic, strong) id depthStencilState;
@property (nonatomic, strong) FramebufferDescriptor* framebufferDescriptor; // framebuffer descriptor for current frame; transient
@property (nonatomic, strong) NSMutableDictionary* renderPipelineStateCache; // pipeline cache; keyed on framebuffer descriptors
-@property (nonatomic, strong, nullable) id fontTexture;
@property (nonatomic, strong) NSMutableArray* bufferCache;
@property (nonatomic, assign) double lastBufferCachePurge;
- (MetalBuffer*)dequeueReusableBufferOfLength:(NSUInteger)length device:(id)device;
@@ -118,11 +125,6 @@ void ImGui_ImplMetal_RenderDrawData(ImDrawData* draw_data,
}
-bool ImGui_ImplMetal_CreateFontsTexture(MTL::Device* device)
-{
- return ImGui_ImplMetal_CreateFontsTexture((__bridge id)(device));
-}
-
bool ImGui_ImplMetal_CreateDeviceObjects(MTL::Device* device)
{
return ImGui_ImplMetal_CreateDeviceObjects((__bridge id)(device));
@@ -142,6 +144,7 @@ bool ImGui_ImplMetal_Init(id device)
io.BackendRendererUserData = (void*)bd;
io.BackendRendererName = "imgui_impl_metal";
io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes.
+ io.BackendFlags |= ImGuiBackendFlags_RendererHasTextures; // We can honor ImGuiPlatformIO::Textures[] requests during render.
io.BackendFlags |= ImGuiBackendFlags_RendererHasViewports; // We can create multi-viewports on the Renderer side (optional)
bd->SharedMetalContext = [[MetalContext alloc] init];
@@ -157,14 +160,17 @@ void ImGui_ImplMetal_Shutdown()
ImGui_ImplMetal_Data* bd = ImGui_ImplMetal_GetBackendData();
IM_UNUSED(bd);
IM_ASSERT(bd != nullptr && "No renderer backend to shutdown, or already shutdown?");
+ ImGuiIO& io = ImGui::GetIO();
+ ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
+
ImGui_ImplMetal_ShutdownMultiViewportSupport();
ImGui_ImplMetal_DestroyDeviceObjects();
ImGui_ImplMetal_DestroyBackendData();
- ImGuiIO& io = ImGui::GetIO();
io.BackendRendererName = nullptr;
io.BackendRendererUserData = nullptr;
- io.BackendFlags &= ~(ImGuiBackendFlags_RendererHasVtxOffset | ImGuiBackendFlags_RendererHasViewports);
+ io.BackendFlags &= ~(ImGuiBackendFlags_RendererHasVtxOffset | ImGuiBackendFlags_RendererHasTextures | ImGuiBackendFlags_RendererHasViewports);
+ platform_io.ClearRendererHandlers();
}
void ImGui_ImplMetal_NewFrame(MTLRenderPassDescriptor* renderPassDescriptor)
@@ -180,7 +186,7 @@ void ImGui_ImplMetal_NewFrame(MTLRenderPassDescriptor* renderPassDescriptor)
ImGui_ImplMetal_CreateDeviceObjects(bd->SharedMetalContext.device);
}
-static void ImGui_ImplMetal_SetupRenderState(ImDrawData* drawData, id commandBuffer,
+static void ImGui_ImplMetal_SetupRenderState(ImDrawData* draw_data, id commandBuffer,
id commandEncoder, id renderPipelineState,
MetalBuffer* vertexBuffer, size_t vertexBufferOffset)
{
@@ -196,17 +202,17 @@ static void ImGui_ImplMetal_SetupRenderState(ImDrawData* drawData, idDisplaySize.x * drawData->FramebufferScale.x),
- .height = (double)(drawData->DisplaySize.y * drawData->FramebufferScale.y),
+ .width = (double)(draw_data->DisplaySize.x * draw_data->FramebufferScale.x),
+ .height = (double)(draw_data->DisplaySize.y * draw_data->FramebufferScale.y),
.znear = 0.0,
.zfar = 1.0
};
[commandEncoder setViewport:viewport];
- float L = drawData->DisplayPos.x;
- float R = drawData->DisplayPos.x + drawData->DisplaySize.x;
- float T = drawData->DisplayPos.y;
- float B = drawData->DisplayPos.y + drawData->DisplaySize.y;
+ float L = draw_data->DisplayPos.x;
+ float R = draw_data->DisplayPos.x + draw_data->DisplaySize.x;
+ float T = draw_data->DisplayPos.y;
+ float B = draw_data->DisplayPos.y + draw_data->DisplaySize.y;
float N = (float)viewport.znear;
float F = (float)viewport.zfar;
const float ortho_projection[4][4] =
@@ -225,17 +231,24 @@ static void ImGui_ImplMetal_SetupRenderState(ImDrawData* drawData, id commandBuffer, id commandEncoder)
+void ImGui_ImplMetal_RenderDrawData(ImDrawData* draw_data, id commandBuffer, id commandEncoder)
{
ImGui_ImplMetal_Data* bd = ImGui_ImplMetal_GetBackendData();
MetalContext* ctx = bd->SharedMetalContext;
// Avoid rendering when minimized, scale coordinates for retina displays (screen coordinates != framebuffer coordinates)
- int fb_width = (int)(drawData->DisplaySize.x * drawData->FramebufferScale.x);
- int fb_height = (int)(drawData->DisplaySize.y * drawData->FramebufferScale.y);
- if (fb_width <= 0 || fb_height <= 0 || drawData->CmdListsCount == 0)
+ int fb_width = (int)(draw_data->DisplaySize.x * draw_data->FramebufferScale.x);
+ int fb_height = (int)(draw_data->DisplaySize.y * draw_data->FramebufferScale.y);
+ if (fb_width <= 0 || fb_height <= 0 || draw_data->CmdLists.Size == 0)
return;
+ // Catch up with texture updates. Most of the times, the list will have 1 element with an OK status, aka nothing to do.
+ // (This almost always points to ImGui::GetPlatformIO().Textures[] but is part of ImDrawData to allow overriding or disabling texture updates).
+ if (draw_data->Textures != nullptr)
+ for (ImTextureData* tex : *draw_data->Textures)
+ if (tex->Status != ImTextureStatus_OK)
+ ImGui_ImplMetal_UpdateTexture(tex);
+
// Try to retrieve a render pipeline state that is compatible with the framebuffer config for this frame
// The hit rate for this cache should be very near 100%.
id renderPipelineState = ctx.renderPipelineStateCache[ctx.framebufferDescriptor];
@@ -248,24 +261,22 @@ void ImGui_ImplMetal_RenderDrawData(ImDrawData* drawData, id c
ctx.renderPipelineStateCache[ctx.framebufferDescriptor] = renderPipelineState;
}
- size_t vertexBufferLength = (size_t)drawData->TotalVtxCount * sizeof(ImDrawVert);
- size_t indexBufferLength = (size_t)drawData->TotalIdxCount * sizeof(ImDrawIdx);
+ size_t vertexBufferLength = (size_t)draw_data->TotalVtxCount * sizeof(ImDrawVert);
+ size_t indexBufferLength = (size_t)draw_data->TotalIdxCount * sizeof(ImDrawIdx);
MetalBuffer* vertexBuffer = [ctx dequeueReusableBufferOfLength:vertexBufferLength device:commandBuffer.device];
MetalBuffer* indexBuffer = [ctx dequeueReusableBufferOfLength:indexBufferLength device:commandBuffer.device];
- ImGui_ImplMetal_SetupRenderState(drawData, commandBuffer, commandEncoder, renderPipelineState, vertexBuffer, 0);
+ ImGui_ImplMetal_SetupRenderState(draw_data, commandBuffer, commandEncoder, renderPipelineState, vertexBuffer, 0);
// Will project scissor/clipping rectangles into framebuffer space
- ImVec2 clip_off = drawData->DisplayPos; // (0,0) unless using multi-viewports
- ImVec2 clip_scale = drawData->FramebufferScale; // (1,1) unless using retina display which are often (2,2)
+ ImVec2 clip_off = draw_data->DisplayPos; // (0,0) unless using multi-viewports
+ ImVec2 clip_scale = draw_data->FramebufferScale; // (1,1) unless using retina display which are often (2,2)
// Render command lists
size_t vertexBufferOffset = 0;
size_t indexBufferOffset = 0;
- for (int n = 0; n < drawData->CmdListsCount; n++)
+ for (const ImDrawList* draw_list : draw_data->CmdLists)
{
- const ImDrawList* draw_list = drawData->CmdLists[n];
-
memcpy((char*)vertexBuffer.buffer.contents + vertexBufferOffset, draw_list->VtxBuffer.Data, (size_t)draw_list->VtxBuffer.Size * sizeof(ImDrawVert));
memcpy((char*)indexBuffer.buffer.contents + indexBufferOffset, draw_list->IdxBuffer.Data, (size_t)draw_list->IdxBuffer.Size * sizeof(ImDrawIdx));
@@ -277,7 +288,7 @@ void ImGui_ImplMetal_RenderDrawData(ImDrawData* drawData, id c
// User callback, registered via ImDrawList::AddCallback()
// (ImDrawCallback_ResetRenderState is a special callback value used by the user to request the renderer to reset render state.)
if (pcmd->UserCallback == ImDrawCallback_ResetRenderState)
- ImGui_ImplMetal_SetupRenderState(drawData, commandBuffer, commandEncoder, renderPipelineState, vertexBuffer, vertexBufferOffset);
+ ImGui_ImplMetal_SetupRenderState(draw_data, commandBuffer, commandEncoder, renderPipelineState, vertexBuffer, vertexBufferOffset);
else
pcmd->UserCallback(draw_list, pcmd);
}
@@ -337,42 +348,71 @@ void ImGui_ImplMetal_RenderDrawData(ImDrawData* drawData, id c
}];
}
-bool ImGui_ImplMetal_CreateFontsTexture(id device)
+static void ImGui_ImplMetal_DestroyTexture(ImTextureData* tex)
{
- ImGui_ImplMetal_Data* bd = ImGui_ImplMetal_GetBackendData();
- ImGuiIO& io = ImGui::GetIO();
+ if (MetalTexture* backend_tex = (__bridge_transfer MetalTexture*)(tex->BackendUserData))
+ {
+ IM_ASSERT(backend_tex.metalTexture == (__bridge id)(void*)(intptr_t)tex->TexID);
+ backend_tex.metalTexture = nil;
- // We are retrieving and uploading the font atlas as a 4-channels RGBA texture here.
- // In theory we could call GetTexDataAsAlpha8() and upload a 1-channel texture to save on memory access bandwidth.
- // However, using a shader designed for 1-channel texture would make it less obvious to use the ImTextureID facility to render users own textures.
- // You can make that change in your implementation.
- unsigned char* pixels;
- int width, height;
- io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
- MTLTextureDescriptor* textureDescriptor = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatRGBA8Unorm
- width:(NSUInteger)width
- height:(NSUInteger)height
- mipmapped:NO];
- textureDescriptor.usage = MTLTextureUsageShaderRead;
-#if TARGET_OS_OSX || TARGET_OS_MACCATALYST
- textureDescriptor.storageMode = MTLStorageModeManaged;
-#else
- textureDescriptor.storageMode = MTLStorageModeShared;
-#endif
- id texture = [device newTextureWithDescriptor:textureDescriptor];
- [texture replaceRegion:MTLRegionMake2D(0, 0, (NSUInteger)width, (NSUInteger)height) mipmapLevel:0 withBytes:pixels bytesPerRow:(NSUInteger)width * 4];
- bd->SharedMetalContext.fontTexture = texture;
- io.Fonts->SetTexID((ImTextureID)(intptr_t)(__bridge void*)bd->SharedMetalContext.fontTexture); // ImTextureID == ImU64
-
- return (bd->SharedMetalContext.fontTexture != nil);
+ // Clear identifiers and mark as destroyed (in order to allow e.g. calling InvalidateDeviceObjects while running)
+ tex->SetTexID(ImTextureID_Invalid);
+ tex->BackendUserData = nullptr;
+ }
+ tex->SetStatus(ImTextureStatus_Destroyed);
}
-void ImGui_ImplMetal_DestroyFontsTexture()
+void ImGui_ImplMetal_UpdateTexture(ImTextureData* tex)
{
ImGui_ImplMetal_Data* bd = ImGui_ImplMetal_GetBackendData();
- ImGuiIO& io = ImGui::GetIO();
- bd->SharedMetalContext.fontTexture = nil;
- io.Fonts->SetTexID(0);
+ if (tex->Status == ImTextureStatus_WantCreate)
+ {
+ // Create and upload new texture to graphics system
+ //IMGUI_DEBUG_LOG("UpdateTexture #%03d: WantCreate %dx%d\n", tex->UniqueID, tex->Width, tex->Height);
+ IM_ASSERT(tex->TexID == ImTextureID_Invalid && tex->BackendUserData == nullptr);
+ IM_ASSERT(tex->Format == ImTextureFormat_RGBA32);
+
+ // We are retrieving and uploading the font atlas as a 4-channels RGBA texture here.
+ // In theory we could call GetTexDataAsAlpha8() and upload a 1-channel texture to save on memory access bandwidth.
+ // However, using a shader designed for 1-channel texture would make it less obvious to use the ImTextureID facility to render users own textures.
+ // You can make that change in your implementation.
+ MTLTextureDescriptor* textureDescriptor = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatRGBA8Unorm
+ width:(NSUInteger)tex->Width
+ height:(NSUInteger)tex->Height
+ mipmapped:NO];
+ textureDescriptor.usage = MTLTextureUsageShaderRead;
+ #if TARGET_OS_OSX || TARGET_OS_MACCATALYST
+ textureDescriptor.storageMode = MTLStorageModeManaged;
+ #else
+ textureDescriptor.storageMode = MTLStorageModeShared;
+ #endif
+ id texture = [bd->SharedMetalContext.device newTextureWithDescriptor:textureDescriptor];
+ [texture replaceRegion:MTLRegionMake2D(0, 0, (NSUInteger)tex->Width, (NSUInteger)tex->Height) mipmapLevel:0 withBytes:tex->Pixels bytesPerRow:(NSUInteger)tex->Width * 4];
+ MetalTexture* backend_tex = [[MetalTexture alloc] initWithTexture:texture];
+
+ // Store identifiers
+ tex->SetTexID((ImTextureID)(intptr_t)texture);
+ tex->SetStatus(ImTextureStatus_OK);
+ tex->BackendUserData = (__bridge_retained void*)(backend_tex);
+ }
+ else if (tex->Status == ImTextureStatus_WantUpdates)
+ {
+ // Update selected blocks. We only ever write to textures regions which have never been used before!
+ // This backend choose to use tex->Updates[] but you can use tex->UpdateRect to upload a single region.
+ MetalTexture* backend_tex = (__bridge MetalTexture*)(tex->BackendUserData);
+ for (ImTextureRect& r : tex->Updates)
+ {
+ [backend_tex.metalTexture replaceRegion:MTLRegionMake2D((NSUInteger)r.x, (NSUInteger)r.y, (NSUInteger)r.w, (NSUInteger)r.h)
+ mipmapLevel:0
+ withBytes:tex->GetPixelsAt(r.x, r.y)
+ bytesPerRow:(NSUInteger)tex->Width * 4];
+ }
+ tex->SetStatus(ImTextureStatus_OK);
+ }
+ else if (tex->Status == ImTextureStatus_WantDestroy && tex->UnusedFrames > 0)
+ {
+ ImGui_ImplMetal_DestroyTexture(tex);
+ }
}
bool ImGui_ImplMetal_CreateDeviceObjects(id device)
@@ -386,14 +426,19 @@ bool ImGui_ImplMetal_CreateDeviceObjects(id device)
#ifdef IMGUI_IMPL_METAL_CPP
[depthStencilDescriptor release];
#endif
- ImGui_ImplMetal_CreateFontsTexture(device);
+
return true;
}
void ImGui_ImplMetal_DestroyDeviceObjects()
{
ImGui_ImplMetal_Data* bd = ImGui_ImplMetal_GetBackendData();
- ImGui_ImplMetal_DestroyFontsTexture();
+
+ // Destroy all textures
+ for (ImTextureData* tex : ImGui::GetPlatformIO().Textures)
+ if (tex->RefCount == 1)
+ ImGui_ImplMetal_DestroyTexture(tex);
+
ImGui_ImplMetal_InvalidateDeviceObjectsForPlatformWindows();
[bd->SharedMetalContext.renderPipelineStateCache removeAllObjects];
}
@@ -485,13 +530,12 @@ static void ImGui_ImplMetal_RenderWindow(ImGuiViewport* viewport, void*)
}
data->FirstFrame = false;
- viewport->DpiScale = (float)window.backingScaleFactor;
- if (data->MetalLayer.contentsScale != viewport->DpiScale)
+ float fb_scale = (float)window.backingScaleFactor;
+ if (data->MetalLayer.contentsScale != fb_scale)
{
- data->MetalLayer.contentsScale = viewport->DpiScale;
- data->MetalLayer.drawableSize = MakeScaledSize(window.frame.size, viewport->DpiScale);
+ data->MetalLayer.contentsScale = fb_scale;
+ data->MetalLayer.drawableSize = MakeScaledSize(window.frame.size, fb_scale);
}
- viewport->DrawData->FramebufferScale = ImVec2(viewport->DpiScale, viewport->DpiScale);
#endif
id drawable = [data->MetalLayer nextDrawable];
@@ -605,6 +649,18 @@ static void ImGui_ImplMetal_InvalidateDeviceObjectsForPlatformWindows()
@end
+#pragma mark - MetalTexture implementation
+
+@implementation MetalTexture
+- (instancetype)initWithTexture:(id)metalTexture
+{
+ if ((self = [super init]))
+ self.metalTexture = metalTexture;
+ return self;
+}
+
+@end
+
#pragma mark - MetalContext implementation
@implementation MetalContext
diff --git a/backends/imgui_impl_opengl2.cpp b/backends/imgui_impl_opengl2.cpp
index 122d1edf4..c89b83c97 100644
--- a/backends/imgui_impl_opengl2.cpp
+++ b/backends/imgui_impl_opengl2.cpp
@@ -2,7 +2,8 @@
// This needs to be used along with a Platform Backend (e.g. GLFW, SDL, Win32, custom..)
// Implemented features:
-// [X] Renderer: User texture binding. Use 'GLuint' OpenGL texture identifier as void*/ImTextureID. Read the FAQ about ImTextureID!
+// [X] Renderer: User texture binding. Use 'GLuint' OpenGL texture as texture identifier. Read the FAQ about ImTextureID/ImTextureRef!
+// [X] Renderer: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures).
// [X] Renderer: Multi-viewport support (multiple windows). Enable with 'io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable'.
// Missing features or Issues:
// [ ] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset).
@@ -26,6 +27,9 @@
// CHANGELOG
// (minor and older changes stripped away, please see git history for details)
// 2025-XX-XX: Platform: Added support for multiple windows via the ImGuiPlatformIO interface.
+// 2025-09-18: Call platform_io.ClearRendererHandlers() on shutdown.
+// 2025-07-15: OpenGL: Set GL_UNPACK_ALIGNMENT to 1 before updating textures. (#8802)
+// 2025-06-11: OpenGL: Added support for ImGuiBackendFlags_RendererHasTextures, for dynamic font atlas. Removed ImGui_ImplOpenGL2_CreateFontsTexture() and ImGui_ImplOpenGL2_DestroyFontsTexture().
// 2024-10-07: OpenGL: Changed default texture sampler to Clamp instead of Repeat/Wrap.
// 2024-06-28: OpenGL: ImGui_ImplOpenGL2_NewFrame() recreates font texture if it has been destroyed by ImGui_ImplOpenGL2_DestroyFontsTexture(). (#7748)
// 2022-10-11: Using 'nullptr' instead of 'NULL' as per our switch to C++11.
@@ -83,8 +87,6 @@
// OpenGL data
struct ImGui_ImplOpenGL2_Data
{
- GLuint FontTexture;
-
ImGui_ImplOpenGL2_Data() { memset((void*)this, 0, sizeof(*this)); }
};
@@ -110,7 +112,8 @@ bool ImGui_ImplOpenGL2_Init()
ImGui_ImplOpenGL2_Data* bd = IM_NEW(ImGui_ImplOpenGL2_Data)();
io.BackendRendererUserData = (void*)bd;
io.BackendRendererName = "imgui_impl_opengl2";
- io.BackendFlags |= ImGuiBackendFlags_RendererHasViewports; // We can create multi-viewports on the Renderer side (optional)
+ io.BackendFlags |= ImGuiBackendFlags_RendererHasTextures; // We can honor ImGuiPlatformIO::Textures[] requests during render.
+ io.BackendFlags |= ImGuiBackendFlags_RendererHasViewports; // We can create multi-viewports on the Renderer side (optional)
ImGui_ImplOpenGL2_InitMultiViewportSupport();
@@ -122,12 +125,15 @@ void ImGui_ImplOpenGL2_Shutdown()
ImGui_ImplOpenGL2_Data* bd = ImGui_ImplOpenGL2_GetBackendData();
IM_ASSERT(bd != nullptr && "No renderer backend to shutdown, or already shutdown?");
ImGuiIO& io = ImGui::GetIO();
+ ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
ImGui_ImplOpenGL2_ShutdownMultiViewportSupport();
ImGui_ImplOpenGL2_DestroyDeviceObjects();
+
io.BackendRendererName = nullptr;
io.BackendRendererUserData = nullptr;
- io.BackendFlags &= ~ImGuiBackendFlags_RendererHasViewports;
+ io.BackendFlags &= ~(ImGuiBackendFlags_RendererHasTextures | ImGuiBackendFlags_RendererHasViewports);
+ platform_io.ClearRendererHandlers();
IM_DELETE(bd);
}
@@ -135,11 +141,7 @@ void ImGui_ImplOpenGL2_NewFrame()
{
ImGui_ImplOpenGL2_Data* bd = ImGui_ImplOpenGL2_GetBackendData();
IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplOpenGL2_Init()?");
-
- if (!bd->FontTexture)
- ImGui_ImplOpenGL2_CreateDeviceObjects();
- if (!bd->FontTexture)
- ImGui_ImplOpenGL2_CreateFontsTexture();
+ IM_UNUSED(bd);
}
static void ImGui_ImplOpenGL2_SetupRenderState(ImDrawData* draw_data, int fb_width, int fb_height)
@@ -197,6 +199,13 @@ void ImGui_ImplOpenGL2_RenderDrawData(ImDrawData* draw_data)
if (fb_width == 0 || fb_height == 0)
return;
+ // Catch up with texture updates. Most of the times, the list will have 1 element with an OK status, aka nothing to do.
+ // (This almost always points to ImGui::GetPlatformIO().Textures[] but is part of ImDrawData to allow overriding or disabling texture updates).
+ if (draw_data->Textures != nullptr)
+ for (ImTextureData* tex : *draw_data->Textures)
+ if (tex->Status != ImTextureStatus_OK)
+ ImGui_ImplOpenGL2_UpdateTexture(tex);
+
// Backup GL state
GLint last_texture; glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);
GLint last_polygon_mode[2]; glGetIntegerv(GL_POLYGON_MODE, last_polygon_mode);
@@ -214,9 +223,8 @@ void ImGui_ImplOpenGL2_RenderDrawData(ImDrawData* draw_data)
ImVec2 clip_scale = draw_data->FramebufferScale; // (1,1) unless using retina display which are often (2,2)
// Render command lists
- for (int n = 0; n < draw_data->CmdListsCount; n++)
+ for (const ImDrawList* draw_list : draw_data->CmdLists)
{
- const ImDrawList* draw_list = draw_data->CmdLists[n];
const ImDrawVert* vtx_buffer = draw_list->VtxBuffer.Data;
const ImDrawIdx* idx_buffer = draw_list->IdxBuffer.Data;
glVertexPointer(2, GL_FLOAT, sizeof(ImDrawVert), (const GLvoid*)((const char*)vtx_buffer + offsetof(ImDrawVert, pos)));
@@ -270,57 +278,79 @@ void ImGui_ImplOpenGL2_RenderDrawData(ImDrawData* draw_data)
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, last_tex_env_mode);
}
-bool ImGui_ImplOpenGL2_CreateFontsTexture()
+void ImGui_ImplOpenGL2_UpdateTexture(ImTextureData* tex)
{
- // Build texture atlas
- ImGuiIO& io = ImGui::GetIO();
- ImGui_ImplOpenGL2_Data* bd = ImGui_ImplOpenGL2_GetBackendData();
- unsigned char* pixels;
- int width, height;
- io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); // Load as RGBA 32-bit (75% of the memory is wasted, but default font is so small) because it is more likely to be compatible with user's existing shaders. If your ImTextureId represent a higher-level concept than just a GL texture id, consider calling GetTexDataAsAlpha8() instead to save on GPU memory.
-
- // Upload texture to graphics system
- // (Bilinear sampling is required by default. Set 'io.Fonts->Flags |= ImFontAtlasFlags_NoBakedLines' or 'style.AntiAliasedLinesUseTex = false' to allow point/nearest sampling)
- GLint last_texture;
- glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);
- glGenTextures(1, &bd->FontTexture);
- glBindTexture(GL_TEXTURE_2D, bd->FontTexture);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
- glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
-
- // Store our identifier
- io.Fonts->SetTexID((ImTextureID)(intptr_t)bd->FontTexture);
-
- // Restore state
- glBindTexture(GL_TEXTURE_2D, last_texture);
-
- return true;
-}
-
-void ImGui_ImplOpenGL2_DestroyFontsTexture()
-{
- ImGuiIO& io = ImGui::GetIO();
- ImGui_ImplOpenGL2_Data* bd = ImGui_ImplOpenGL2_GetBackendData();
- if (bd->FontTexture)
+ if (tex->Status == ImTextureStatus_WantCreate)
{
- glDeleteTextures(1, &bd->FontTexture);
- io.Fonts->SetTexID(0);
- bd->FontTexture = 0;
+ // Create and upload new texture to graphics system
+ //IMGUI_DEBUG_LOG("UpdateTexture #%03d: WantCreate %dx%d\n", tex->UniqueID, tex->Width, tex->Height);
+ IM_ASSERT(tex->TexID == 0 && tex->BackendUserData == nullptr);
+ IM_ASSERT(tex->Format == ImTextureFormat_RGBA32);
+ const void* pixels = tex->GetPixels();
+ GLuint gl_texture_id = 0;
+
+ // Upload texture to graphics system
+ // (Bilinear sampling is required by default. Set 'io.Fonts->Flags |= ImFontAtlasFlags_NoBakedLines' or 'style.AntiAliasedLinesUseTex = false' to allow point/nearest sampling)
+ GLint last_texture;
+ GL_CALL(glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture));
+ GL_CALL(glGenTextures(1, &gl_texture_id));
+ GL_CALL(glBindTexture(GL_TEXTURE_2D, gl_texture_id));
+ GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
+ GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
+ GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP));
+ GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP));
+ GL_CALL(glPixelStorei(GL_UNPACK_ROW_LENGTH, 0));
+ GL_CALL(glPixelStorei(GL_UNPACK_ALIGNMENT, 1));
+ GL_CALL(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex->Width, tex->Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels));
+
+ // Store identifiers
+ tex->SetTexID((ImTextureID)(intptr_t)gl_texture_id);
+ tex->SetStatus(ImTextureStatus_OK);
+
+ // Restore state
+ GL_CALL(glBindTexture(GL_TEXTURE_2D, last_texture));
+ }
+ else if (tex->Status == ImTextureStatus_WantUpdates)
+ {
+ // Update selected blocks. We only ever write to textures regions which have never been used before!
+ // This backend choose to use tex->Updates[] but you can use tex->UpdateRect to upload a single region.
+ GLint last_texture;
+ GL_CALL(glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture));
+
+ GLuint gl_tex_id = (GLuint)(intptr_t)tex->TexID;
+ GL_CALL(glBindTexture(GL_TEXTURE_2D, gl_tex_id));
+ GL_CALL(glPixelStorei(GL_UNPACK_ROW_LENGTH, tex->Width));
+ GL_CALL(glPixelStorei(GL_UNPACK_ALIGNMENT, 1));
+ for (ImTextureRect& r : tex->Updates)
+ GL_CALL(glTexSubImage2D(GL_TEXTURE_2D, 0, r.x, r.y, r.w, r.h, GL_RGBA, GL_UNSIGNED_BYTE, tex->GetPixelsAt(r.x, r.y)));
+ GL_CALL(glPixelStorei(GL_UNPACK_ROW_LENGTH, 0));
+ GL_CALL(glBindTexture(GL_TEXTURE_2D, last_texture)); // Restore state
+ tex->SetStatus(ImTextureStatus_OK);
+ }
+ else if (tex->Status == ImTextureStatus_WantDestroy)
+ {
+ GLuint gl_tex_id = (GLuint)(intptr_t)tex->TexID;
+ glDeleteTextures(1, &gl_tex_id);
+
+ // Clear identifiers and mark as destroyed (in order to allow e.g. calling InvalidateDeviceObjects while running)
+ tex->SetTexID(ImTextureID_Invalid);
+ tex->SetStatus(ImTextureStatus_Destroyed);
}
}
bool ImGui_ImplOpenGL2_CreateDeviceObjects()
{
- return ImGui_ImplOpenGL2_CreateFontsTexture();
+ return true;
}
void ImGui_ImplOpenGL2_DestroyDeviceObjects()
{
- ImGui_ImplOpenGL2_DestroyFontsTexture();
+ for (ImTextureData* tex : ImGui::GetPlatformIO().Textures)
+ if (tex->RefCount == 1)
+ {
+ tex->SetStatus(ImTextureStatus_WantDestroy);
+ ImGui_ImplOpenGL2_UpdateTexture(tex);
+ }
}
diff --git a/backends/imgui_impl_opengl2.h b/backends/imgui_impl_opengl2.h
index c7b40e077..def65c8d9 100644
--- a/backends/imgui_impl_opengl2.h
+++ b/backends/imgui_impl_opengl2.h
@@ -2,7 +2,8 @@
// This needs to be used along with a Platform Backend (e.g. GLFW, SDL, Win32, custom..)
// Implemented features:
-// [X] Renderer: User texture binding. Use 'GLuint' OpenGL texture identifier as void*/ImTextureID. Read the FAQ about ImTextureID!
+// [X] Renderer: User texture binding. Use 'GLuint' OpenGL texture as texture identifier. Read the FAQ about ImTextureID/ImTextureRef!
+// [X] Renderer: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures).
// [X] Renderer: Multi-viewport support (multiple windows). Enable with 'io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable'.
// Missing features or Issues:
// [ ] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset).
@@ -34,9 +35,10 @@ IMGUI_IMPL_API void ImGui_ImplOpenGL2_NewFrame();
IMGUI_IMPL_API void ImGui_ImplOpenGL2_RenderDrawData(ImDrawData* draw_data);
// Called by Init/NewFrame/Shutdown
-IMGUI_IMPL_API bool ImGui_ImplOpenGL2_CreateFontsTexture();
-IMGUI_IMPL_API void ImGui_ImplOpenGL2_DestroyFontsTexture();
IMGUI_IMPL_API bool ImGui_ImplOpenGL2_CreateDeviceObjects();
IMGUI_IMPL_API void ImGui_ImplOpenGL2_DestroyDeviceObjects();
+// (Advanced) Use e.g. if you need to precisely control the timing of texture updates (e.g. for staged rendering), by setting ImDrawData::Textures = NULL to handle this manually.
+IMGUI_IMPL_API void ImGui_ImplOpenGL2_UpdateTexture(ImTextureData* tex);
+
#endif // #ifndef IMGUI_DISABLE
diff --git a/backends/imgui_impl_opengl3.cpp b/backends/imgui_impl_opengl3.cpp
index 2ed1c5413..4ef9fb3d0 100644
--- a/backends/imgui_impl_opengl3.cpp
+++ b/backends/imgui_impl_opengl3.cpp
@@ -4,8 +4,9 @@
// This needs to be used along with a Platform Backend (e.g. GLFW, SDL, Win32, custom..)
// Implemented features:
-// [X] Renderer: User texture binding. Use 'GLuint' OpenGL texture identifier as void*/ImTextureID. Read the FAQ about ImTextureID!
+// [X] Renderer: User texture binding. Use 'GLuint' OpenGL texture as texture identifier. Read the FAQ about ImTextureID/ImTextureRef!
// [x] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset) [Desktop OpenGL only!]
+// [X] Renderer: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures).
// [X] Renderer: Multi-viewport support (multiple windows). Enable with 'io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable'.
// About WebGL/ES:
@@ -24,6 +25,11 @@
// CHANGELOG
// (minor and older changes stripped away, please see git history for details)
// 2025-XX-XX: Platform: Added support for multiple windows via the ImGuiPlatformIO interface.
+// 2025-09-18: Call platform_io.ClearRendererHandlers() on shutdown.
+// 2025-07-22: OpenGL: Add and call embedded loader shutdown during ImGui_ImplOpenGL3_Shutdown() to facilitate multiple init/shutdown cycles in same process. (#8792)
+// 2025-07-15: OpenGL: Set GL_UNPACK_ALIGNMENT to 1 before updating textures (#8802) + restore non-WebGL/ES update path that doesn't require a CPU-side copy.
+// 2025-06-11: OpenGL: Added support for ImGuiBackendFlags_RendererHasTextures, for dynamic font atlas. Removed ImGui_ImplOpenGL3_CreateFontsTexture() and ImGui_ImplOpenGL3_DestroyFontsTexture().
+// 2025-06-04: OpenGL: Made GLES 3.20 contexts not access GL_CONTEXT_PROFILE_MASK nor GL_PRIMITIVE_RESTART. (#8664)
// 2025-02-18: OpenGL: Lazily reinitialize embedded GL loader for when calling backend from e.g. other DLL boundaries. (#8406)
// 2024-10-07: OpenGL: Changed default texture sampler to Clamp instead of Repeat/Wrap.
// 2024-06-28: OpenGL: ImGui_ImplOpenGL3_NewFrame() recreates font texture if it has been destroyed by ImGui_ImplOpenGL3_DestroyFontsTexture(). (#7748)
@@ -232,7 +238,7 @@ struct ImGui_ImplOpenGL3_Data
bool GlProfileIsES3;
bool GlProfileIsCompat;
GLint GlProfileMask;
- GLuint FontTexture;
+ GLint MaxTextureSize;
GLuint ShaderHandle;
GLint AttribLocationTex; // Uniforms location
GLint AttribLocationProjMtx;
@@ -243,8 +249,10 @@ struct ImGui_ImplOpenGL3_Data
GLsizeiptr VertexBufferSize;
GLsizeiptr IndexBufferSize;
bool HasPolygonMode;
+ bool HasBindSampler;
bool HasClipOrigin;
bool UseBufferSubData;
+ ImVector TempBuffer;
ImGui_ImplOpenGL3_Data() { memset((void*)this, 0, sizeof(*this)); }
};
@@ -331,11 +339,7 @@ bool ImGui_ImplOpenGL3_Init(const char* glsl_version)
if (major == 0 && minor == 0)
sscanf(gl_version_str, "%d.%d", &major, &minor); // Query GL_VERSION in desktop GL 2.x, the string will start with "."
bd->GlVersion = (GLuint)(major * 100 + minor * 10);
-#if defined(GL_CONTEXT_PROFILE_MASK)
- if (bd->GlVersion >= 320)
- glGetIntegerv(GL_CONTEXT_PROFILE_MASK, &bd->GlProfileMask);
- bd->GlProfileIsCompat = (bd->GlProfileMask & GL_CONTEXT_COMPATIBILITY_PROFILE_BIT) != 0;
-#endif
+ glGetIntegerv(GL_MAX_TEXTURE_SIZE, &bd->MaxTextureSize);
#if defined(IMGUI_IMPL_OPENGL_ES3)
bd->GlProfileIsES3 = true;
@@ -344,6 +348,12 @@ bool ImGui_ImplOpenGL3_Init(const char* glsl_version)
bd->GlProfileIsES3 = true;
#endif
+#if defined(GL_CONTEXT_PROFILE_MASK)
+ if (!bd->GlProfileIsES3 && bd->GlVersion >= 320)
+ glGetIntegerv(GL_CONTEXT_PROFILE_MASK, &bd->GlProfileMask);
+ bd->GlProfileIsCompat = (bd->GlProfileMask & GL_CONTEXT_COMPATIBILITY_PROFILE_BIT) != 0;
+#endif
+
bd->UseBufferSubData = false;
/*
// Query vendor to enable glBufferSubData kludge
@@ -363,7 +373,11 @@ bool ImGui_ImplOpenGL3_Init(const char* glsl_version)
if (bd->GlVersion >= 320)
io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes.
#endif
- io.BackendFlags |= ImGuiBackendFlags_RendererHasViewports; // We can create multi-viewports on the Renderer side (optional)
+ io.BackendFlags |= ImGuiBackendFlags_RendererHasTextures; // We can honor ImGuiPlatformIO::Textures[] requests during render.
+ io.BackendFlags |= ImGuiBackendFlags_RendererHasViewports; // We can create multi-viewports on the Renderer side (optional)
+
+ ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
+ platform_io.Renderer_TextureMaxWidth = platform_io.Renderer_TextureMaxHeight = (int)bd->MaxTextureSize;
// Store GLSL version string so we can refer to it later in case we recreate shaders.
// Note: GLSL version is NOT the same as GL version. Leave this to nullptr if unsure.
@@ -391,6 +405,9 @@ bool ImGui_ImplOpenGL3_Init(const char* glsl_version)
// Detect extensions we support
#ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_POLYGON_MODE
bd->HasPolygonMode = (!bd->GlProfileIsES2 && !bd->GlProfileIsES3);
+#endif
+#ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_BIND_SAMPLER
+ bd->HasBindSampler = (bd->GlVersion >= 330 || bd->GlProfileIsES3);
#endif
bd->HasClipOrigin = (bd->GlVersion >= 450);
#ifdef IMGUI_IMPL_OPENGL_HAS_EXTENSIONS
@@ -414,13 +431,20 @@ void ImGui_ImplOpenGL3_Shutdown()
ImGui_ImplOpenGL3_Data* bd = ImGui_ImplOpenGL3_GetBackendData();
IM_ASSERT(bd != nullptr && "No renderer backend to shutdown, or already shutdown?");
ImGuiIO& io = ImGui::GetIO();
+ ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
ImGui_ImplOpenGL3_ShutdownMultiViewportSupport();
ImGui_ImplOpenGL3_DestroyDeviceObjects();
+
io.BackendRendererName = nullptr;
io.BackendRendererUserData = nullptr;
- io.BackendFlags &= ~(ImGuiBackendFlags_RendererHasVtxOffset | ImGuiBackendFlags_RendererHasViewports);
+ io.BackendFlags &= ~(ImGuiBackendFlags_RendererHasVtxOffset | ImGuiBackendFlags_RendererHasTextures | ImGuiBackendFlags_RendererHasViewports);
+ platform_io.ClearRendererHandlers();
IM_DELETE(bd);
+
+#ifdef IMGUI_IMPL_OPENGL_LOADER_IMGL3W
+ imgl3wShutdown();
+#endif
}
void ImGui_ImplOpenGL3_NewFrame()
@@ -431,9 +455,8 @@ void ImGui_ImplOpenGL3_NewFrame()
ImGui_ImplOpenGL3_InitLoader(); // Lazily init loader if not already done for e.g. DLL boundaries.
if (!bd->ShaderHandle)
- ImGui_ImplOpenGL3_CreateDeviceObjects();
- if (!bd->FontTexture)
- ImGui_ImplOpenGL3_CreateFontsTexture();
+ if (!ImGui_ImplOpenGL3_CreateDeviceObjects())
+ IM_ASSERT(0 && "ImGui_ImplOpenGL3_CreateDeviceObjects() failed!");
}
static void ImGui_ImplOpenGL3_SetupRenderState(ImDrawData* draw_data, int fb_width, int fb_height, GLuint vertex_array_object)
@@ -449,7 +472,7 @@ static void ImGui_ImplOpenGL3_SetupRenderState(ImDrawData* draw_data, int fb_wid
glDisable(GL_STENCIL_TEST);
glEnable(GL_SCISSOR_TEST);
#ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_PRIMITIVE_RESTART
- if (bd->GlVersion >= 310)
+ if (!bd->GlProfileIsES3 && bd->GlVersion >= 310)
glDisable(GL_PRIMITIVE_RESTART);
#endif
#ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_POLYGON_MODE
@@ -490,7 +513,7 @@ static void ImGui_ImplOpenGL3_SetupRenderState(ImDrawData* draw_data, int fb_wid
glUniformMatrix4fv(bd->AttribLocationProjMtx, 1, GL_FALSE, &ortho_projection[0][0]);
#ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_BIND_SAMPLER
- if (bd->GlVersion >= 330 || bd->GlProfileIsES3)
+ if (bd->HasBindSampler)
glBindSampler(0, 0); // We use combined texture/sampler state. Applications using GL 3.3 and GL ES 3.0 may set that otherwise.
#endif
@@ -525,13 +548,20 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data)
ImGui_ImplOpenGL3_Data* bd = ImGui_ImplOpenGL3_GetBackendData();
+ // Catch up with texture updates. Most of the times, the list will have 1 element with an OK status, aka nothing to do.
+ // (This almost always points to ImGui::GetPlatformIO().Textures[] but is part of ImDrawData to allow overriding or disabling texture updates).
+ if (draw_data->Textures != nullptr)
+ for (ImTextureData* tex : *draw_data->Textures)
+ if (tex->Status != ImTextureStatus_OK)
+ ImGui_ImplOpenGL3_UpdateTexture(tex);
+
// Backup GL state
GLenum last_active_texture; glGetIntegerv(GL_ACTIVE_TEXTURE, (GLint*)&last_active_texture);
glActiveTexture(GL_TEXTURE0);
GLuint last_program; glGetIntegerv(GL_CURRENT_PROGRAM, (GLint*)&last_program);
GLuint last_texture; glGetIntegerv(GL_TEXTURE_BINDING_2D, (GLint*)&last_texture);
#ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_BIND_SAMPLER
- GLuint last_sampler; if (bd->GlVersion >= 330 || bd->GlProfileIsES3) { glGetIntegerv(GL_SAMPLER_BINDING, (GLint*)&last_sampler); } else { last_sampler = 0; }
+ GLuint last_sampler; if (bd->HasBindSampler) { glGetIntegerv(GL_SAMPLER_BINDING, (GLint*)&last_sampler); } else { last_sampler = 0; }
#endif
GLuint last_array_buffer; glGetIntegerv(GL_ARRAY_BUFFER_BINDING, (GLint*)&last_array_buffer);
#ifndef IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY
@@ -561,7 +591,7 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data)
GLboolean last_enable_stencil_test = glIsEnabled(GL_STENCIL_TEST);
GLboolean last_enable_scissor_test = glIsEnabled(GL_SCISSOR_TEST);
#ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_PRIMITIVE_RESTART
- GLboolean last_enable_primitive_restart = (bd->GlVersion >= 310) ? glIsEnabled(GL_PRIMITIVE_RESTART) : GL_FALSE;
+ GLboolean last_enable_primitive_restart = (!bd->GlProfileIsES3 && bd->GlVersion >= 310) ? glIsEnabled(GL_PRIMITIVE_RESTART) : GL_FALSE;
#endif
// Setup desired GL state
@@ -578,10 +608,8 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data)
ImVec2 clip_scale = draw_data->FramebufferScale; // (1,1) unless using retina display which are often (2,2)
// Render command lists
- for (int n = 0; n < draw_data->CmdListsCount; n++)
+ for (const ImDrawList* draw_list : draw_data->CmdLists)
{
- const ImDrawList* draw_list = draw_data->CmdLists[n];
-
// Upload vertex/index buffers
// - OpenGL drivers are in a very sorry state nowadays....
// During 2021 we attempted to switch from glBufferData() to orphaning+glBufferSubData() following reports
@@ -658,7 +686,7 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data)
if (last_program == 0 || glIsProgram(last_program)) glUseProgram(last_program);
glBindTexture(GL_TEXTURE_2D, last_texture);
#ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_BIND_SAMPLER
- if (bd->GlVersion >= 330 || bd->GlProfileIsES3)
+ if (bd->HasBindSampler)
glBindSampler(0, last_sampler);
#endif
glActiveTexture(last_active_texture);
@@ -680,7 +708,7 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data)
if (last_enable_stencil_test) glEnable(GL_STENCIL_TEST); else glDisable(GL_STENCIL_TEST);
if (last_enable_scissor_test) glEnable(GL_SCISSOR_TEST); else glDisable(GL_SCISSOR_TEST);
#ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_PRIMITIVE_RESTART
- if (bd->GlVersion >= 310) { if (last_enable_primitive_restart) glEnable(GL_PRIMITIVE_RESTART); else glDisable(GL_PRIMITIVE_RESTART); }
+ if (!bd->GlProfileIsES3 && bd->GlVersion >= 310) { if (last_enable_primitive_restart) glEnable(GL_PRIMITIVE_RESTART); else glDisable(GL_PRIMITIVE_RESTART); }
#endif
#ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_POLYGON_MODE
@@ -693,50 +721,90 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data)
(void)bd; // Not all compilation paths use this
}
-bool ImGui_ImplOpenGL3_CreateFontsTexture()
+static void ImGui_ImplOpenGL3_DestroyTexture(ImTextureData* tex)
{
- ImGuiIO& io = ImGui::GetIO();
- ImGui_ImplOpenGL3_Data* bd = ImGui_ImplOpenGL3_GetBackendData();
+ GLuint gl_tex_id = (GLuint)(intptr_t)tex->TexID;
+ glDeleteTextures(1, &gl_tex_id);
- // Build texture atlas
- unsigned char* pixels;
- int width, height;
- io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); // Load as RGBA 32-bit (75% of the memory is wasted, but default font is so small) because it is more likely to be compatible with user's existing shaders. If your ImTextureId represent a higher-level concept than just a GL texture id, consider calling GetTexDataAsAlpha8() instead to save on GPU memory.
-
- // Upload texture to graphics system
- // (Bilinear sampling is required by default. Set 'io.Fonts->Flags |= ImFontAtlasFlags_NoBakedLines' or 'style.AntiAliasedLinesUseTex = false' to allow point/nearest sampling)
- GLint last_texture;
- GL_CALL(glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture));
- GL_CALL(glGenTextures(1, &bd->FontTexture));
- GL_CALL(glBindTexture(GL_TEXTURE_2D, bd->FontTexture));
- GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
- GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
- GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
- GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
-#ifdef GL_UNPACK_ROW_LENGTH // Not on WebGL/ES
- GL_CALL(glPixelStorei(GL_UNPACK_ROW_LENGTH, 0));
-#endif
- GL_CALL(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels));
-
- // Store identifier
- io.Fonts->SetTexID((ImTextureID)(intptr_t)bd->FontTexture);
-
- // Restore state
- GL_CALL(glBindTexture(GL_TEXTURE_2D, last_texture));
-
- return true;
+ // Clear identifiers and mark as destroyed (in order to allow e.g. calling InvalidateDeviceObjects while running)
+ tex->SetTexID(ImTextureID_Invalid);
+ tex->SetStatus(ImTextureStatus_Destroyed);
}
-void ImGui_ImplOpenGL3_DestroyFontsTexture()
+void ImGui_ImplOpenGL3_UpdateTexture(ImTextureData* tex)
{
- ImGuiIO& io = ImGui::GetIO();
- ImGui_ImplOpenGL3_Data* bd = ImGui_ImplOpenGL3_GetBackendData();
- if (bd->FontTexture)
+ // FIXME: Consider backing up and restoring
+ if (tex->Status == ImTextureStatus_WantCreate || tex->Status == ImTextureStatus_WantUpdates)
{
- glDeleteTextures(1, &bd->FontTexture);
- io.Fonts->SetTexID(0);
- bd->FontTexture = 0;
+#ifdef GL_UNPACK_ROW_LENGTH // Not on WebGL/ES
+ GL_CALL(glPixelStorei(GL_UNPACK_ROW_LENGTH, 0));
+#endif
+#ifdef GL_UNPACK_ALIGNMENT
+ GL_CALL(glPixelStorei(GL_UNPACK_ALIGNMENT, 1));
+#endif
}
+
+ if (tex->Status == ImTextureStatus_WantCreate)
+ {
+ // Create and upload new texture to graphics system
+ //IMGUI_DEBUG_LOG("UpdateTexture #%03d: WantCreate %dx%d\n", tex->UniqueID, tex->Width, tex->Height);
+ IM_ASSERT(tex->TexID == 0 && tex->BackendUserData == nullptr);
+ IM_ASSERT(tex->Format == ImTextureFormat_RGBA32);
+ const void* pixels = tex->GetPixels();
+ GLuint gl_texture_id = 0;
+
+ // Upload texture to graphics system
+ // (Bilinear sampling is required by default. Set 'io.Fonts->Flags |= ImFontAtlasFlags_NoBakedLines' or 'style.AntiAliasedLinesUseTex = false' to allow point/nearest sampling)
+ GLint last_texture;
+ GL_CALL(glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture));
+ GL_CALL(glGenTextures(1, &gl_texture_id));
+ GL_CALL(glBindTexture(GL_TEXTURE_2D, gl_texture_id));
+ GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
+ GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
+ GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
+ GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
+ GL_CALL(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex->Width, tex->Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels));
+
+ // Store identifiers
+ tex->SetTexID((ImTextureID)(intptr_t)gl_texture_id);
+ tex->SetStatus(ImTextureStatus_OK);
+
+ // Restore state
+ GL_CALL(glBindTexture(GL_TEXTURE_2D, last_texture));
+ }
+ else if (tex->Status == ImTextureStatus_WantUpdates)
+ {
+ // Update selected blocks. We only ever write to textures regions which have never been used before!
+ // This backend choose to use tex->Updates[] but you can use tex->UpdateRect to upload a single region.
+ GLint last_texture;
+ GL_CALL(glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture));
+
+ GLuint gl_tex_id = (GLuint)(intptr_t)tex->TexID;
+ GL_CALL(glBindTexture(GL_TEXTURE_2D, gl_tex_id));
+#if GL_UNPACK_ROW_LENGTH // Not on WebGL/ES
+ GL_CALL(glPixelStorei(GL_UNPACK_ROW_LENGTH, tex->Width));
+ for (ImTextureRect& r : tex->Updates)
+ GL_CALL(glTexSubImage2D(GL_TEXTURE_2D, 0, r.x, r.y, r.w, r.h, GL_RGBA, GL_UNSIGNED_BYTE, tex->GetPixelsAt(r.x, r.y)));
+ GL_CALL(glPixelStorei(GL_UNPACK_ROW_LENGTH, 0));
+#else
+ // GL ES doesn't have GL_UNPACK_ROW_LENGTH, so we need to (A) copy to a contiguous buffer or (B) upload line by line.
+ ImGui_ImplOpenGL3_Data* bd = ImGui_ImplOpenGL3_GetBackendData();
+ for (ImTextureRect& r : tex->Updates)
+ {
+ const int src_pitch = r.w * tex->BytesPerPixel;
+ bd->TempBuffer.resize(r.h * src_pitch);
+ char* out_p = bd->TempBuffer.Data;
+ for (int y = 0; y < r.h; y++, out_p += src_pitch)
+ memcpy(out_p, tex->GetPixelsAt(r.x, r.y + y), src_pitch);
+ IM_ASSERT(out_p == bd->TempBuffer.end());
+ GL_CALL(glTexSubImage2D(GL_TEXTURE_2D, 0, r.x, r.y, r.w, r.h, GL_RGBA, GL_UNSIGNED_BYTE, bd->TempBuffer.Data));
+ }
+#endif
+ tex->SetStatus(ImTextureStatus_OK);
+ GL_CALL(glBindTexture(GL_TEXTURE_2D, last_texture)); // Restore state
+ }
+ else if (tex->Status == ImTextureStatus_WantDestroy && tex->UnusedFrames > 0)
+ ImGui_ImplOpenGL3_DestroyTexture(tex);
}
// If you get an error please report on github. You may try different GL context version or GLSL version. See GL<>GLSL version table at the top of this file.
@@ -928,21 +996,24 @@ bool ImGui_ImplOpenGL3_CreateDeviceObjects()
GL_CALL(vert_handle = glCreateShader(GL_VERTEX_SHADER));
glShaderSource(vert_handle, 2, vertex_shader_with_version, nullptr);
glCompileShader(vert_handle);
- CheckShader(vert_handle, "vertex shader");
+ if (!CheckShader(vert_handle, "vertex shader"))
+ return false;
const GLchar* fragment_shader_with_version[2] = { bd->GlslVersionString, fragment_shader };
GLuint frag_handle;
GL_CALL(frag_handle = glCreateShader(GL_FRAGMENT_SHADER));
glShaderSource(frag_handle, 2, fragment_shader_with_version, nullptr);
glCompileShader(frag_handle);
- CheckShader(frag_handle, "fragment shader");
+ if (!CheckShader(frag_handle, "fragment shader"))
+ return false;
// Link
bd->ShaderHandle = glCreateProgram();
glAttachShader(bd->ShaderHandle, vert_handle);
glAttachShader(bd->ShaderHandle, frag_handle);
glLinkProgram(bd->ShaderHandle);
- CheckProgram(bd->ShaderHandle, "shader program");
+ if (!CheckProgram(bd->ShaderHandle, "shader program"))
+ return false;
glDetachShader(bd->ShaderHandle, vert_handle);
glDetachShader(bd->ShaderHandle, frag_handle);
@@ -959,8 +1030,6 @@ bool ImGui_ImplOpenGL3_CreateDeviceObjects()
glGenBuffers(1, &bd->VboHandle);
glGenBuffers(1, &bd->ElementsHandle);
- ImGui_ImplOpenGL3_CreateFontsTexture();
-
// Restore modified GL state
glBindTexture(GL_TEXTURE_2D, last_texture);
glBindBuffer(GL_ARRAY_BUFFER, last_array_buffer);
@@ -980,7 +1049,11 @@ void ImGui_ImplOpenGL3_DestroyDeviceObjects()
if (bd->VboHandle) { glDeleteBuffers(1, &bd->VboHandle); bd->VboHandle = 0; }
if (bd->ElementsHandle) { glDeleteBuffers(1, &bd->ElementsHandle); bd->ElementsHandle = 0; }
if (bd->ShaderHandle) { glDeleteProgram(bd->ShaderHandle); bd->ShaderHandle = 0; }
- ImGui_ImplOpenGL3_DestroyFontsTexture();
+
+ // Destroy all textures
+ for (ImTextureData* tex : ImGui::GetPlatformIO().Textures)
+ if (tex->RefCount == 1)
+ ImGui_ImplOpenGL3_DestroyTexture(tex);
}
//--------------------------------------------------------------------------------------------------------
diff --git a/backends/imgui_impl_opengl3.h b/backends/imgui_impl_opengl3.h
index d56213044..9495d4e97 100644
--- a/backends/imgui_impl_opengl3.h
+++ b/backends/imgui_impl_opengl3.h
@@ -4,8 +4,9 @@
// This needs to be used along with a Platform Backend (e.g. GLFW, SDL, Win32, custom..)
// Implemented features:
-// [X] Renderer: User texture binding. Use 'GLuint' OpenGL texture identifier as void*/ImTextureID. Read the FAQ about ImTextureID!
+// [X] Renderer: User texture binding. Use 'GLuint' OpenGL texture as texture identifier. Read the FAQ about ImTextureID/ImTextureRef!
// [x] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset) [Desktop OpenGL only!]
+// [X] Renderer: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures).
// [X] Renderer: Multi-viewport support (multiple windows). Enable with 'io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable'.
// About WebGL/ES:
@@ -37,11 +38,12 @@ IMGUI_IMPL_API void ImGui_ImplOpenGL3_NewFrame();
IMGUI_IMPL_API void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data);
// (Optional) Called by Init/NewFrame/Shutdown
-IMGUI_IMPL_API bool ImGui_ImplOpenGL3_CreateFontsTexture();
-IMGUI_IMPL_API void ImGui_ImplOpenGL3_DestroyFontsTexture();
IMGUI_IMPL_API bool ImGui_ImplOpenGL3_CreateDeviceObjects();
IMGUI_IMPL_API void ImGui_ImplOpenGL3_DestroyDeviceObjects();
+// (Advanced) Use e.g. if you need to precisely control the timing of texture updates (e.g. for staged rendering), by setting ImDrawData::Textures = NULL to handle this manually.
+IMGUI_IMPL_API void ImGui_ImplOpenGL3_UpdateTexture(ImTextureData* tex);
+
// Configuration flags to add in your imconfig file:
//#define IMGUI_IMPL_OPENGL_ES2 // Enable ES 2 (Auto-detected on Emscripten)
//#define IMGUI_IMPL_OPENGL_ES3 // Enable ES 3 (Auto-detected on iOS/Android)
diff --git a/backends/imgui_impl_opengl3_loader.h b/backends/imgui_impl_opengl3_loader.h
index d6ffa5a2d..2c80cc598 100644
--- a/backends/imgui_impl_opengl3_loader.h
+++ b/backends/imgui_impl_opengl3_loader.h
@@ -166,7 +166,9 @@ typedef khronos_uint8_t GLubyte;
#define GL_SCISSOR_BOX 0x0C10
#define GL_SCISSOR_TEST 0x0C11
#define GL_UNPACK_ROW_LENGTH 0x0CF2
+#define GL_UNPACK_ALIGNMENT 0x0CF5
#define GL_PACK_ALIGNMENT 0x0D05
+#define GL_MAX_TEXTURE_SIZE 0x0D33
#define GL_TEXTURE_2D 0x0DE1
#define GL_UNSIGNED_BYTE 0x1401
#define GL_UNSIGNED_SHORT 0x1403
@@ -178,6 +180,7 @@ typedef khronos_uint8_t GLubyte;
#define GL_RENDERER 0x1F01
#define GL_VERSION 0x1F02
#define GL_EXTENSIONS 0x1F03
+#define GL_NEAREST 0x2600
#define GL_LINEAR 0x2601
#define GL_TEXTURE_MAG_FILTER 0x2800
#define GL_TEXTURE_MIN_FILTER 0x2801
@@ -224,11 +227,13 @@ typedef khronos_float_t GLclampf;
typedef double GLclampd;
#define GL_TEXTURE_BINDING_2D 0x8069
typedef void (APIENTRYP PFNGLDRAWELEMENTSPROC) (GLenum mode, GLsizei count, GLenum type, const void *indices);
+typedef void (APIENTRYP PFNGLTEXSUBIMAGE2DPROC) (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const void *pixels);
typedef void (APIENTRYP PFNGLBINDTEXTUREPROC) (GLenum target, GLuint texture);
typedef void (APIENTRYP PFNGLDELETETEXTURESPROC) (GLsizei n, const GLuint *textures);
typedef void (APIENTRYP PFNGLGENTEXTURESPROC) (GLsizei n, GLuint *textures);
#ifdef GL_GLEXT_PROTOTYPES
GLAPI void APIENTRY glDrawElements (GLenum mode, GLsizei count, GLenum type, const void *indices);
+GLAPI void APIENTRY glTexSubImage2D (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const void *pixels);
GLAPI void APIENTRY glBindTexture (GLenum target, GLuint texture);
GLAPI void APIENTRY glDeleteTextures (GLsizei n, const GLuint *textures);
GLAPI void APIENTRY glGenTextures (GLsizei n, GLuint *textures);
@@ -396,9 +401,15 @@ GLAPI void APIENTRY glDrawElementsBaseVertex (GLenum mode, GLsizei count, GLenum
#ifndef GL_VERSION_3_3
#define GL_VERSION_3_3 1
#define GL_SAMPLER_BINDING 0x8919
+typedef void (APIENTRYP PFNGLGENSAMPLERSPROC) (GLsizei count, GLuint *samplers);
+typedef void (APIENTRYP PFNGLDELETESAMPLERSPROC) (GLsizei count, const GLuint *samplers);
typedef void (APIENTRYP PFNGLBINDSAMPLERPROC) (GLuint unit, GLuint sampler);
+typedef void (APIENTRYP PFNGLSAMPLERPARAMETERIPROC) (GLuint sampler, GLenum pname, GLint param);
#ifdef GL_GLEXT_PROTOTYPES
+GLAPI void APIENTRY glGenSamplers (GLsizei count, GLuint *samplers);
+GLAPI void APIENTRY glDeleteSamplers (GLsizei count, const GLuint *samplers);
GLAPI void APIENTRY glBindSampler (GLuint unit, GLuint sampler);
+GLAPI void APIENTRY glSamplerParameteri (GLuint sampler, GLenum pname, GLint param);
#endif
#endif /* GL_VERSION_3_3 */
#ifndef GL_VERSION_4_1
@@ -473,12 +484,13 @@ typedef GL3WglProc (*GL3WGetProcAddressProc)(const char *proc);
/* gl3w api */
GL3W_API int imgl3wInit(void);
GL3W_API int imgl3wInit2(GL3WGetProcAddressProc proc);
+GL3W_API void imgl3wShutdown(void);
GL3W_API int imgl3wIsSupported(int major, int minor);
GL3W_API GL3WglProc imgl3wGetProcAddress(const char *proc);
/* gl3w internal state */
union ImGL3WProcs {
- GL3WglProc ptr[59];
+ GL3WglProc ptr[63];
struct {
PFNGLACTIVETEXTUREPROC ActiveTexture;
PFNGLATTACHSHADERPROC AttachShader;
@@ -498,6 +510,7 @@ union ImGL3WProcs {
PFNGLCREATESHADERPROC CreateShader;
PFNGLDELETEBUFFERSPROC DeleteBuffers;
PFNGLDELETEPROGRAMPROC DeleteProgram;
+ PFNGLDELETESAMPLERSPROC DeleteSamplers;
PFNGLDELETESHADERPROC DeleteShader;
PFNGLDELETETEXTURESPROC DeleteTextures;
PFNGLDELETEVERTEXARRAYSPROC DeleteVertexArrays;
@@ -510,6 +523,7 @@ union ImGL3WProcs {
PFNGLENABLEVERTEXATTRIBARRAYPROC EnableVertexAttribArray;
PFNGLFLUSHPROC Flush;
PFNGLGENBUFFERSPROC GenBuffers;
+ PFNGLGENSAMPLERSPROC GenSamplers;
PFNGLGENTEXTURESPROC GenTextures;
PFNGLGENVERTEXARRAYSPROC GenVertexArrays;
PFNGLGETATTRIBLOCATIONPROC GetAttribLocation;
@@ -530,10 +544,12 @@ union ImGL3WProcs {
PFNGLPIXELSTOREIPROC PixelStorei;
PFNGLPOLYGONMODEPROC PolygonMode;
PFNGLREADPIXELSPROC ReadPixels;
+ PFNGLSAMPLERPARAMETERIPROC SamplerParameteri;
PFNGLSCISSORPROC Scissor;
PFNGLSHADERSOURCEPROC ShaderSource;
PFNGLTEXIMAGE2DPROC TexImage2D;
PFNGLTEXPARAMETERIPROC TexParameteri;
+ PFNGLTEXSUBIMAGE2DPROC TexSubImage2D;
PFNGLUNIFORM1IPROC Uniform1i;
PFNGLUNIFORMMATRIX4FVPROC UniformMatrix4fv;
PFNGLUSEPROGRAMPROC UseProgram;
@@ -563,6 +579,7 @@ GL3W_API extern union ImGL3WProcs imgl3wProcs;
#define glCreateShader imgl3wProcs.gl.CreateShader
#define glDeleteBuffers imgl3wProcs.gl.DeleteBuffers
#define glDeleteProgram imgl3wProcs.gl.DeleteProgram
+#define glDeleteSamplers imgl3wProcs.gl.DeleteSamplers
#define glDeleteShader imgl3wProcs.gl.DeleteShader
#define glDeleteTextures imgl3wProcs.gl.DeleteTextures
#define glDeleteVertexArrays imgl3wProcs.gl.DeleteVertexArrays
@@ -575,6 +592,7 @@ GL3W_API extern union ImGL3WProcs imgl3wProcs;
#define glEnableVertexAttribArray imgl3wProcs.gl.EnableVertexAttribArray
#define glFlush imgl3wProcs.gl.Flush
#define glGenBuffers imgl3wProcs.gl.GenBuffers
+#define glGenSamplers imgl3wProcs.gl.GenSamplers
#define glGenTextures imgl3wProcs.gl.GenTextures
#define glGenVertexArrays imgl3wProcs.gl.GenVertexArrays
#define glGetAttribLocation imgl3wProcs.gl.GetAttribLocation
@@ -595,10 +613,12 @@ GL3W_API extern union ImGL3WProcs imgl3wProcs;
#define glPixelStorei imgl3wProcs.gl.PixelStorei
#define glPolygonMode imgl3wProcs.gl.PolygonMode
#define glReadPixels imgl3wProcs.gl.ReadPixels
+#define glSamplerParameteri imgl3wProcs.gl.SamplerParameteri
#define glScissor imgl3wProcs.gl.Scissor
#define glShaderSource imgl3wProcs.gl.ShaderSource
#define glTexImage2D imgl3wProcs.gl.TexImage2D
#define glTexParameteri imgl3wProcs.gl.TexParameteri
+#define glTexSubImage2D imgl3wProcs.gl.TexSubImage2D
#define glUniform1i imgl3wProcs.gl.Uniform1i
#define glUniformMatrix4fv imgl3wProcs.gl.UniformMatrix4fv
#define glUseProgram imgl3wProcs.gl.UseProgram
@@ -626,7 +646,7 @@ extern "C" {
#endif
#include
-static HMODULE libgl;
+static HMODULE libgl = NULL;
typedef PROC(__stdcall* GL3WglGetProcAddr)(LPCSTR);
static GL3WglGetProcAddr wgl_get_proc_address;
@@ -639,7 +659,7 @@ static int open_libgl(void)
return GL3W_OK;
}
-static void close_libgl(void) { FreeLibrary(libgl); }
+static void close_libgl(void) { FreeLibrary(libgl); libgl = NULL; }
static GL3WglProc get_proc(const char *proc)
{
GL3WglProc res;
@@ -651,7 +671,7 @@ static GL3WglProc get_proc(const char *proc)
#elif defined(__APPLE__)
#include
-static void *libgl;
+static void *libgl = NULL;
static int open_libgl(void)
{
libgl = dlopen("/System/Library/Frameworks/OpenGL.framework/OpenGL", RTLD_LAZY | RTLD_LOCAL);
@@ -660,7 +680,7 @@ static int open_libgl(void)
return GL3W_OK;
}
-static void close_libgl(void) { dlclose(libgl); }
+static void close_libgl(void) { dlclose(libgl); libgl = NULL; }
static GL3WglProc get_proc(const char *proc)
{
@@ -694,7 +714,11 @@ static void close_libgl(void)
static int is_library_loaded(const char* name, void** lib)
{
+#if defined(__HAIKU__)
+ *lib = NULL; // no support for RTLD_NOLOAD on Haiku.
+#else
*lib = dlopen(name, RTLD_LAZY | RTLD_LOCAL | RTLD_NOLOAD);
+#endif
return *lib != NULL;
}
@@ -828,6 +852,11 @@ int imgl3wInit2(GL3WGetProcAddressProc proc)
return parse_version();
}
+void imgl3wShutdown(void)
+{
+ close_libgl();
+}
+
int imgl3wIsSupported(int major, int minor)
{
if (major < 2)
@@ -858,6 +887,7 @@ static const char *proc_names[] = {
"glCreateShader",
"glDeleteBuffers",
"glDeleteProgram",
+ "glDeleteSamplers",
"glDeleteShader",
"glDeleteTextures",
"glDeleteVertexArrays",
@@ -870,6 +900,7 @@ static const char *proc_names[] = {
"glEnableVertexAttribArray",
"glFlush",
"glGenBuffers",
+ "glGenSamplers",
"glGenTextures",
"glGenVertexArrays",
"glGetAttribLocation",
@@ -890,10 +921,12 @@ static const char *proc_names[] = {
"glPixelStorei",
"glPolygonMode",
"glReadPixels",
+ "glSamplerParameteri",
"glScissor",
"glShaderSource",
"glTexImage2D",
"glTexParameteri",
+ "glTexSubImage2D",
"glUniform1i",
"glUniformMatrix4fv",
"glUseProgram",
diff --git a/backends/imgui_impl_osx.h b/backends/imgui_impl_osx.h
index 10812a108..d944f3e2f 100644
--- a/backends/imgui_impl_osx.h
+++ b/backends/imgui_impl_osx.h
@@ -12,9 +12,8 @@
// [X] Platform: IME support.
// [x] Platform: Multi-viewport / platform windows.
// Missing features or Issues:
-// [ ] Missing ImGuiMouseCursor_Wait and ImGuiMouseCursor_Progress cursors.
-// [ ] Multi-viewport: Window size not correctly reported when enabling io.ConfigViewportsNoDecoration
-// [ ] Multi-viewport: ParentViewportID not honored, and so io.ConfigViewportsNoDefaultParent has no effect (minor).
+// [ ] Platform: Multi-viewport: Window size not correctly reported when enabling io.ConfigViewportsNoDecoration
+// [ ] Platform: Multi-viewport: Missing ImGuiBackendFlags_HasParentViewport support. The viewport->ParentViewportID field is ignored, and therefore io.ConfigViewportsNoDefaultParent has no effect either.
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
diff --git a/backends/imgui_impl_osx.mm b/backends/imgui_impl_osx.mm
index e08b00e38..f55b9c080 100644
--- a/backends/imgui_impl_osx.mm
+++ b/backends/imgui_impl_osx.mm
@@ -12,9 +12,8 @@
// [X] Platform: IME support.
// [x] Platform: Multi-viewport / platform windows.
// Missing features or Issues:
-// [ ] Missing ImGuiMouseCursor_Wait and ImGuiMouseCursor_Progress cursors.
-// [ ] Multi-viewport: Window size not correctly reported when enabling io.ConfigViewportsNoDecoration
-// [ ] Multi-viewport: ParentViewportID not honored, and so io.ConfigViewportsNoDefaultParent has no effect (minor).
+// [ ] Platform: Multi-viewport: Window size not correctly reported when enabling io.ConfigViewportsNoDecoration
+// [ ] Platform: Multi-viewport: Missing ImGuiBackendFlags_HasParentViewport support. The viewport->ParentViewportID field is ignored, and therefore io.ConfigViewportsNoDefaultParent has no effect either.
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
@@ -35,6 +34,11 @@
// CHANGELOG
// (minor and older changes stripped away, please see git history for details)
// 2025-XX-XX: Added support for multiple windows via the ImGuiPlatformIO interface.
+// 2025-09-18: Call platform_io.ClearPlatformHandlers() on shutdown.
+// 2025-07-08: [Docking] Fixed multi-viewport handling broken on 2025-06-02. (#8644, #8777)
+// 2025-06-27: Added ImGuiMouseCursor_Wait and ImGuiMouseCursor_Progress mouse cursor support.
+// 2025-06-12: ImGui_ImplOSX_HandleEvent() only process event for window containing our view. (#8644)
+// 2025-05-15: [Docking] Add Platform_GetWindowFramebufferScale() handler, to allow varying Retina display density on multiple monitors.
// 2025-03-21: Fill gamepad inputs and set ImGuiBackendFlags_HasGamepad regardless of ImGuiConfigFlags_NavEnableGamepad being set.
// 2025-01-20: Removed notification observer when shutting down. (#8331)
// 2024-08-22: moved some OS/backend related function pointers from ImGuiIO to ImGuiPlatformIO:
@@ -116,6 +120,7 @@ static bool ImGui_ImplOSX_HandleEvent(NSEvent* event, NSView* view);
+ (id)_windowResizeNorthEastSouthWestCursor;
+ (id)_windowResizeNorthSouthCursor;
+ (id)_windowResizeEastWestCursor;
++ (id)busyButClickableCursor;
@end
/**
@@ -463,6 +468,7 @@ bool ImGui_ImplOSX_Init(NSView* view)
bd->MouseCursors[ImGuiMouseCursor_ResizeNESW] = [NSCursor respondsToSelector:@selector(_windowResizeNorthEastSouthWestCursor)] ? [NSCursor _windowResizeNorthEastSouthWestCursor] : [NSCursor closedHandCursor];
bd->MouseCursors[ImGuiMouseCursor_ResizeNWSE] = [NSCursor respondsToSelector:@selector(_windowResizeNorthWestSouthEastCursor)] ? [NSCursor _windowResizeNorthWestSouthEastCursor] : [NSCursor closedHandCursor];
bd->MouseCursors[ImGuiMouseCursor_Hand] = [NSCursor pointingHandCursor];
+ bd->MouseCursors[ImGuiMouseCursor_Wait] = bd->MouseCursors[ImGuiMouseCursor_Progress] = [NSCursor respondsToSelector:@selector(busyButClickableCursor)] ? [NSCursor busyButClickableCursor] : [NSCursor arrowCursor];
bd->MouseCursors[ImGuiMouseCursor_NotAllowed] = [NSCursor operationNotAllowedCursor];
// Note that imgui.cpp also include default OSX clipboard handlers which can be enabled
@@ -510,7 +516,7 @@ bool ImGui_ImplOSX_Init(NSView* view)
[view addSubview:bd->KeyEventResponder];
ImGui_ImplOSX_AddTrackingArea(view);
- platform_io.Platform_SetImeDataFn = [](ImGuiContext*, ImGuiViewport* viewport, ImGuiPlatformImeData* data) -> void
+ platform_io.Platform_SetImeDataFn = [](ImGuiContext*, ImGuiViewport*, ImGuiPlatformImeData* data) -> void
{
ImGui_ImplOSX_Data* bd = ImGui_ImplOSX_GetBackendData();
if (data->WantVisible)
@@ -545,9 +551,12 @@ void ImGui_ImplOSX_Shutdown()
ImGui_ImplOSX_ShutdownMultiViewportSupport();
ImGui_ImplOSX_DestroyBackendData();
ImGuiIO& io = ImGui::GetIO();
+ ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
+
io.BackendPlatformName = nullptr;
io.BackendPlatformUserData = nullptr;
io.BackendFlags &= ~(ImGuiBackendFlags_HasMouseCursors | ImGuiBackendFlags_HasGamepad | ImGuiBackendFlags_PlatformHasViewports);
+ platform_io.ClearPlatformHandlers();
}
static void ImGui_ImplOSX_UpdateMouseCursor()
@@ -656,9 +665,9 @@ void ImGui_ImplOSX_NewFrame(NSView* view)
// Setup display size
if (view)
{
- const float dpi = (float)[view.window backingScaleFactor];
+ const float fb_scale = (float)[view.window backingScaleFactor];
io.DisplaySize = ImVec2((float)view.bounds.size.width, (float)view.bounds.size.height);
- io.DisplayFramebufferScale = ImVec2(dpi, dpi);
+ io.DisplayFramebufferScale = ImVec2(fb_scale, fb_scale);
}
// Setup time step
@@ -696,8 +705,11 @@ static ImGuiMouseSource GetMouseSource(NSEvent* event)
static bool ImGui_ImplOSX_HandleEvent(NSEvent* event, NSView* view)
{
- ImGuiIO& io = ImGui::GetIO();
+ // Only process events from the window containing ImGui view
+ if (!ImGui::FindViewportByPlatformHandle((__bridge void*)event.window))
+ return false;
+ ImGuiIO& io = ImGui::GetIO();
if (event.type == NSEventTypeLeftMouseDown || event.type == NSEventTypeRightMouseDown || event.type == NSEventTypeOtherMouseDown)
{
int button = (int)[event buttonNumber];
@@ -830,8 +842,6 @@ static bool ImGui_ImplOSX_HandleEvent(NSEvent* event, NSView* view)
default:
return io.WantCaptureKeyboard;
}
-
- NSEventModifierFlags modifier_flags = [event modifierFlags];
io.AddKeyEvent(key, (modifier_flags & mask) != 0);
io.SetKeyEventNativeData(key, key_code, -1); // To support legacy indexing (<1.87 user code)
}
@@ -872,13 +882,13 @@ static void ImGui_ImplOSX_AddTrackingArea(NSView* _Nonnull view)
// If you are new to dear imgui or creating a new binding for dear imgui, it is recommended that you completely ignore this section first..
//--------------------------------------------------------------------------------------------------------
-struct ImGuiViewportDataOSX
+struct ImGui_ImplOSX_ViewportData
{
- NSWindow* Window;
- bool WindowOwned;
+ NSWindow* Window;
+ bool WindowOwned;
- ImGuiViewportDataOSX() { WindowOwned = false; }
- ~ImGuiViewportDataOSX() { IM_ASSERT(Window == nil); }
+ ImGui_ImplOSX_ViewportData() { WindowOwned = false; }
+ ~ImGui_ImplOSX_ViewportData() { IM_ASSERT(Window == nil); }
};
@interface ImGui_ImplOSX_Window: NSWindow
@@ -903,8 +913,8 @@ static void ConvertNSRect(NSRect* r)
static void ImGui_ImplOSX_CreateWindow(ImGuiViewport* viewport)
{
ImGui_ImplOSX_Data* bd = ImGui_ImplOSX_GetBackendData();
- ImGuiViewportDataOSX* data = IM_NEW(ImGuiViewportDataOSX)();
- viewport->PlatformUserData = data;
+ ImGui_ImplOSX_ViewportData* vd = IM_NEW(ImGui_ImplOSX_ViewportData)();
+ viewport->PlatformUserData = vd;
NSScreen* screen = bd->Window.screen;
NSRect rect = NSMakeRect(viewport->Pos.x, viewport->Pos.y, viewport->Size.x, viewport->Size.y);
@@ -933,51 +943,48 @@ static void ImGui_ImplOSX_CreateWindow(ImGuiViewport* viewport)
window.contentView = view;
- data->Window = window;
- data->WindowOwned = true;
+ vd->Window = window;
+ vd->WindowOwned = true;
viewport->PlatformRequestResize = false;
viewport->PlatformHandle = viewport->PlatformHandleRaw = (__bridge_retained void*)window;
}
static void ImGui_ImplOSX_DestroyWindow(ImGuiViewport* viewport)
{
- NSWindow* window = (__bridge_transfer NSWindow*)viewport->PlatformHandleRaw;
- window = nil;
-
- if (ImGuiViewportDataOSX* data = (ImGuiViewportDataOSX*)viewport->PlatformUserData)
+ if (ImGui_ImplOSX_ViewportData* vd = (ImGui_ImplOSX_ViewportData*)viewport->PlatformUserData)
{
- NSWindow* window = data->Window;
- if (window != nil && data->WindowOwned)
+ NSWindow* window = vd->Window;
+ if (window != nil && vd->WindowOwned)
{
window.contentView = nil;
window.contentViewController = nil;
[window orderOut:nil];
}
- data->Window = nil;
- IM_DELETE(data);
+ vd->Window = nil;
+ IM_DELETE(vd);
}
viewport->PlatformUserData = viewport->PlatformHandle = viewport->PlatformHandleRaw = nullptr;
}
static void ImGui_ImplOSX_ShowWindow(ImGuiViewport* viewport)
{
- ImGuiViewportDataOSX* data = (ImGuiViewportDataOSX*)viewport->PlatformUserData;
- IM_ASSERT(data->Window != 0);
+ ImGui_ImplOSX_ViewportData* vd = (ImGui_ImplOSX_ViewportData*)viewport->PlatformUserData;
+ IM_ASSERT(vd->Window != 0);
if (viewport->Flags & ImGuiViewportFlags_NoFocusOnAppearing)
- [data->Window orderFront:nil];
+ [vd->Window orderFront:nil];
else
- [data->Window makeKeyAndOrderFront:nil];
+ [vd->Window makeKeyAndOrderFront:nil];
- [data->Window setIsVisible:YES];
+ [vd->Window setIsVisible:YES];
}
static ImVec2 ImGui_ImplOSX_GetWindowPos(ImGuiViewport* viewport)
{
- ImGuiViewportDataOSX* data = (ImGuiViewportDataOSX*)viewport->PlatformUserData;
- IM_ASSERT(data->Window != 0);
+ ImGui_ImplOSX_ViewportData* vd = (ImGui_ImplOSX_ViewportData*)viewport->PlatformUserData;
+ IM_ASSERT(vd->Window != 0);
- NSWindow* window = data->Window;
+ NSWindow* window = vd->Window;
NSRect frame = window.frame;
NSRect contentRect = window.contentLayoutRect;
if (window.styleMask & NSWindowStyleMaskFullSizeContentView) // No title bar windows should be considered.
@@ -989,10 +996,10 @@ static ImVec2 ImGui_ImplOSX_GetWindowPos(ImGuiViewport* viewport)
static void ImGui_ImplOSX_SetWindowPos(ImGuiViewport* viewport, ImVec2 pos)
{
- ImGuiViewportDataOSX* data = (ImGuiViewportDataOSX*)viewport->PlatformUserData;
- IM_ASSERT(data->Window != 0);
+ ImGui_ImplOSX_ViewportData* vd = (ImGui_ImplOSX_ViewportData*)viewport->PlatformUserData;
+ IM_ASSERT(vd->Window != 0);
- NSWindow* window = data->Window;
+ NSWindow* window = vd->Window;
NSSize size = window.frame.size;
NSRect r = NSMakeRect(pos.x, pos.y, size.width, size.height);
@@ -1002,20 +1009,20 @@ static void ImGui_ImplOSX_SetWindowPos(ImGuiViewport* viewport, ImVec2 pos)
static ImVec2 ImGui_ImplOSX_GetWindowSize(ImGuiViewport* viewport)
{
- ImGuiViewportDataOSX* data = (ImGuiViewportDataOSX*)viewport->PlatformUserData;
- IM_ASSERT(data->Window != 0);
+ ImGui_ImplOSX_ViewportData* vd = (ImGui_ImplOSX_ViewportData*)viewport->PlatformUserData;
+ IM_ASSERT(vd->Window != 0);
- NSWindow* window = data->Window;
+ NSWindow* window = vd->Window;
NSSize size = window.contentLayoutRect.size;
return ImVec2(size.width, size.height);
}
static void ImGui_ImplOSX_SetWindowSize(ImGuiViewport* viewport, ImVec2 size)
{
- ImGuiViewportDataOSX* data = (ImGuiViewportDataOSX*)viewport->PlatformUserData;
- IM_ASSERT(data->Window != 0);
+ ImGui_ImplOSX_ViewportData* vd = (ImGui_ImplOSX_ViewportData*)viewport->PlatformUserData;
+ IM_ASSERT(vd->Window != 0);
- NSWindow* window = data->Window;
+ NSWindow* window = vd->Window;
NSRect rect = window.frame;
rect.origin.y -= (size.y - rect.size.height);
rect.size.width = size.x;
@@ -1023,53 +1030,61 @@ static void ImGui_ImplOSX_SetWindowSize(ImGuiViewport* viewport, ImVec2 size)
[window setFrame:rect display:YES];
}
+static ImVec2 ImGui_ImplOSX_GetWindowFramebufferScale(ImGuiViewport* viewport)
+{
+ ImGui_ImplOSX_ViewportData* vd = (ImGui_ImplOSX_ViewportData*)viewport->PlatformUserData;
+ NSWindow* window = vd->Window;
+ const float fb_scale = (float)[window backingScaleFactor];
+ return ImVec2(fb_scale, fb_scale);
+}
+
static void ImGui_ImplOSX_SetWindowFocus(ImGuiViewport* viewport)
{
ImGui_ImplOSX_Data* bd = ImGui_ImplOSX_GetBackendData();
- ImGuiViewportDataOSX* data = (ImGuiViewportDataOSX*)viewport->PlatformUserData;
- IM_ASSERT(data->Window != 0);
- [data->Window makeKeyAndOrderFront:bd->Window];
+ ImGui_ImplOSX_ViewportData* vd = (ImGui_ImplOSX_ViewportData*)viewport->PlatformUserData;
+ IM_ASSERT(vd->Window != 0);
+ [vd->Window makeKeyAndOrderFront:bd->Window];
}
static bool ImGui_ImplOSX_GetWindowFocus(ImGuiViewport* viewport)
{
- ImGuiViewportDataOSX* data = (ImGuiViewportDataOSX*)viewport->PlatformUserData;
- IM_ASSERT(data->Window != 0);
+ ImGui_ImplOSX_ViewportData* vd = (ImGui_ImplOSX_ViewportData*)viewport->PlatformUserData;
+ IM_ASSERT(vd->Window != 0);
- return data->Window.isKeyWindow;
+ return vd->Window.isKeyWindow;
}
static bool ImGui_ImplOSX_GetWindowMinimized(ImGuiViewport* viewport)
{
- ImGuiViewportDataOSX* data = (ImGuiViewportDataOSX*)viewport->PlatformUserData;
- IM_ASSERT(data->Window != 0);
+ ImGui_ImplOSX_ViewportData* vd = (ImGui_ImplOSX_ViewportData*)viewport->PlatformUserData;
+ IM_ASSERT(vd->Window != 0);
- return data->Window.isMiniaturized;
+ return vd->Window.isMiniaturized;
}
static void ImGui_ImplOSX_SetWindowTitle(ImGuiViewport* viewport, const char* title)
{
- ImGuiViewportDataOSX* data = (ImGuiViewportDataOSX*)viewport->PlatformUserData;
- IM_ASSERT(data->Window != 0);
+ ImGui_ImplOSX_ViewportData* vd = (ImGui_ImplOSX_ViewportData*)viewport->PlatformUserData;
+ IM_ASSERT(vd->Window != 0);
- data->Window.title = [NSString stringWithUTF8String:title];
+ vd->Window.title = [NSString stringWithUTF8String:title];
}
static void ImGui_ImplOSX_SetWindowAlpha(ImGuiViewport* viewport, float alpha)
{
- ImGuiViewportDataOSX* data = (ImGuiViewportDataOSX*)viewport->PlatformUserData;
- IM_ASSERT(data->Window != 0);
+ ImGui_ImplOSX_ViewportData* vd = (ImGui_ImplOSX_ViewportData*)viewport->PlatformUserData;
+ IM_ASSERT(vd->Window != 0);
IM_ASSERT(alpha >= 0.0f && alpha <= 1.0f);
- data->Window.alphaValue = alpha;
+ vd->Window.alphaValue = alpha;
}
static float ImGui_ImplOSX_GetWindowDpiScale(ImGuiViewport* viewport)
{
- ImGuiViewportDataOSX* data = (ImGuiViewportDataOSX*)viewport->PlatformUserData;
- IM_ASSERT(data->Window != 0);
+ ImGui_ImplOSX_ViewportData* vd = (ImGui_ImplOSX_ViewportData*)viewport->PlatformUserData;
+ IM_ASSERT(vd->Window != 0);
- return data->Window.backingScaleFactor;
+ return vd->Window.backingScaleFactor;
}
static void ImGui_ImplOSX_UpdateMonitors()
@@ -1112,6 +1127,7 @@ static void ImGui_ImplOSX_InitMultiViewportSupport()
platform_io.Platform_GetWindowPos = ImGui_ImplOSX_GetWindowPos;
platform_io.Platform_SetWindowSize = ImGui_ImplOSX_SetWindowSize;
platform_io.Platform_GetWindowSize = ImGui_ImplOSX_GetWindowSize;
+ platform_io.Platform_GetWindowFramebufferScale = ImGui_ImplOSX_GetWindowFramebufferScale;
platform_io.Platform_SetWindowFocus = ImGui_ImplOSX_SetWindowFocus;
platform_io.Platform_GetWindowFocus = ImGui_ImplOSX_GetWindowFocus;
platform_io.Platform_GetWindowMinimized = ImGui_ImplOSX_GetWindowMinimized;
@@ -1121,10 +1137,10 @@ static void ImGui_ImplOSX_InitMultiViewportSupport()
// Register main window handle (which is owned by the main application, not by us)
ImGuiViewport* main_viewport = ImGui::GetMainViewport();
- ImGuiViewportDataOSX* data = IM_NEW(ImGuiViewportDataOSX)();
- data->Window = bd->Window;
- data->WindowOwned = false;
- main_viewport->PlatformUserData = data;
+ ImGui_ImplOSX_ViewportData* vd = IM_NEW(ImGui_ImplOSX_ViewportData)();
+ vd->Window = bd->Window;
+ vd->WindowOwned = false;
+ main_viewport->PlatformUserData = vd;
main_viewport->PlatformHandle = (__bridge void*)bd->Window;
[NSNotificationCenter.defaultCenter addObserver:bd->Observer
@@ -1148,8 +1164,8 @@ static void ImGui_ImplOSX_ShutdownMultiViewportSupport()
}
ImGuiViewport* main_viewport = ImGui::GetMainViewport();
- ImGuiViewportDataOSX* data = (ImGuiViewportDataOSX*)main_viewport->PlatformUserData;
- IM_DELETE(data);
+ ImGui_ImplOSX_ViewportData* vd = (ImGui_ImplOSX_ViewportData*)main_viewport->PlatformUserData;
+ IM_DELETE(vd);
main_viewport->PlatformUserData = nullptr;
ImGui::DestroyPlatformWindows();
}
diff --git a/backends/imgui_impl_sdl2.cpp b/backends/imgui_impl_sdl2.cpp
index cb3ff4d29..f0b8b2bb4 100644
--- a/backends/imgui_impl_sdl2.cpp
+++ b/backends/imgui_impl_sdl2.cpp
@@ -13,7 +13,7 @@
// [X] Platform: Multi-viewport support (multiple windows). Enable with 'io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable'.
// Missing features or Issues:
// [ ] Platform: Multi-viewport: Minimized windows seems to break mouse wheel events (at least under Windows).
-// [ ] Platform: Multi-viewport: ParentViewportID not honored, and so io.ConfigViewportsNoDefaultParent has no effect (minor).
+// [ ] Platform: Multi-viewport: Missing ImGuiBackendFlags_HasParentViewport support. The viewport->ParentViewportID field is ignored, and therefore io.ConfigViewportsNoDefaultParent has no effect either.
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
@@ -26,6 +26,14 @@
// CHANGELOG
// (minor and older changes stripped away, please see git history for details)
// 2025-XX-XX: Platform: Added support for multiple windows via the ImGuiPlatformIO interface.
+// 2025-09-24: Skip using the SDL_GetGlobalMouseState() state when one of our window is hovered, as the SDL_MOUSEMOTION data is reliable. Fix macOS notch mouse coordinates issue in fullscreen mode + better perf on X11. (#7919, #7786)
+// 2025-09-18: Call platform_io.ClearPlatformHandlers() on shutdown.
+// 2025-09-15: Content Scales are always reported as 1.0 on Wayland. (#8921)
+// 2025-07-08: Made ImGui_ImplSDL2_GetContentScaleForWindow(), ImGui_ImplSDL2_GetContentScaleForDisplay() helpers return 1.0f on Emscripten and Android platforms, matching macOS logic. (#8742, #8733)
+// 2025-06-11: Added ImGui_ImplSDL2_GetContentScaleForWindow(SDL_Window* window) and ImGui_ImplSDL2_GetContentScaleForDisplay(int display_index) helper to facilitate making DPI-aware apps.
+// 2025-05-15: [Docking] Add Platform_GetWindowFramebufferScale() handler, to allow varying Retina display density on multiple monitors.
+// 2025-04-09: [Docking] Revert update monitors and work areas information every frame. Only do it on Windows. (#8415, #8558)
+// 2025-04-09: Don't attempt to call SDL_CaptureMouse() on drivers where we don't call SDL_GetGlobalMouseState(). (#8561)
// 2025-03-21: Fill gamepad inputs and set ImGuiBackendFlags_HasGamepad regardless of ImGuiConfigFlags_NavEnableGamepad being set.
// 2025-03-10: When dealing with OEM keys, use scancodes instead of translated keycodes to choose ImGuiKey values. (#7136, #7201, #7206, #7306, #7670, #7672, #8468)
// 2025-02-26: Only start SDL_CaptureMouse() when mouse is being dragged, to mitigate issues with e.g.Linux debuggers not claiming capture back. (#6410, #3650)
@@ -109,6 +117,7 @@
// Clang warnings with -Weverything
#if defined(__clang__)
#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wold-style-cast" // warning: use of old-style cast
#pragma clang diagnostic ignored "-Wimplicit-int-float-conversion" // warning: implicit conversion from 'xxx' to 'float' may lose precision
#endif
@@ -116,12 +125,14 @@
// (the multi-viewports feature requires SDL features supported from SDL 2.0.4+. SDL 2.0.5+ is highly recommended)
#include
#include
+#include // for snprintf()
#ifdef __APPLE__
#include
#endif
#ifdef __EMSCRIPTEN__
#include
#endif
+#undef Status // X11 headers are leaking this.
#if SDL_VERSION_ATLEAST(2,0,4) && !defined(__EMSCRIPTEN__) && !defined(__ANDROID__) && !(defined(__APPLE__) && TARGET_OS_IOS) && !defined(__amigaos4__)
#define SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE 1
@@ -150,7 +161,9 @@ struct ImGui_ImplSDL2_Data
SDL_Renderer* Renderer;
Uint64 Time;
char* ClipboardTextData;
+ char BackendPlatformName[48];
bool UseVulkan;
+ bool WantUpdateMonitors;
// Mouse handling
Uint32 MouseWindowID;
@@ -159,6 +172,7 @@ struct ImGui_ImplSDL2_Data
SDL_Cursor* MouseLastCursor;
int MouseLastLeaveFrame;
bool MouseCanUseGlobalState;
+ bool MouseCanUseCapture;
bool MouseCanReportHoveredViewport; // This is hard to use/unreliable on SDL so we'll set ImGuiBackendFlags_HasMouseHoveredViewport dynamically based on state.
// Gamepad handling
@@ -449,16 +463,26 @@ bool ImGui_ImplSDL2_ProcessEvent(const SDL_Event* event)
case SDL_KEYDOWN:
case SDL_KEYUP:
{
- if (ImGui_ImplSDL2_GetViewportForWindowID(event->key.windowID) == nullptr)
+ ImGuiViewport* viewport = ImGui_ImplSDL2_GetViewportForWindowID(event->key.windowID);
+ if (viewport == nullptr)
return false;
+ //IMGUI_DEBUG_LOG("SDL_KEY%s : key=0x%08X ('%s'), scancode=%d ('%s'), mod=%X, windowID=%d, viewport=%08X\n",
+ // (event->type == SDL_KEYDOWN) ? "DOWN" : "UP ", event->key.keysym.sym, SDL_GetKeyName(event->key.keysym.sym), event->key.keysym.scancode, SDL_GetScancodeName(event->key.keysym.scancode), event->key.keysym.mod, event->key.windowID, viewport ? viewport->ID : 0);
ImGui_ImplSDL2_UpdateKeyModifiers((SDL_Keymod)event->key.keysym.mod);
- //IMGUI_DEBUG_LOG("SDL_KEY_%s : key=%d ('%s'), scancode=%d ('%s'), mod=%X\n",
- // (event->type == SDL_KEYDOWN) ? "DOWN" : "UP ", event->key.keysym.sym, SDL_GetKeyName(event->key.keysym.sym), event->key.keysym.scancode, SDL_GetScancodeName(event->key.keysym.scancode), event->key.keysym.mod);
ImGuiKey key = ImGui_ImplSDL2_KeyEventToImGuiKey(event->key.keysym.sym, event->key.keysym.scancode);
io.AddKeyEvent(key, (event->type == SDL_KEYDOWN));
- io.SetKeyEventNativeData(key, event->key.keysym.sym, event->key.keysym.scancode, event->key.keysym.scancode); // To support legacy indexing (<1.87 user code). Legacy backend uses SDLK_*** as indices to IsKeyXXX() functions.
+ io.SetKeyEventNativeData(key, (int)event->key.keysym.sym, (int)event->key.keysym.scancode, (int)event->key.keysym.scancode); // To support legacy indexing (<1.87 user code). Legacy backend uses SDLK_*** as indices to IsKeyXXX() functions.
return true;
}
+#if SDL_HAS_DISPLAY_EVENT
+ case SDL_DISPLAYEVENT:
+ {
+ // 2.0.26 has SDL_DISPLAYEVENT_CONNECTED/SDL_DISPLAYEVENT_DISCONNECTED/SDL_DISPLAYEVENT_ORIENTATION,
+ // so change of DPI/Scaling are not reflected in this event. (SDL3 has it)
+ bd->WantUpdateMonitors = true;
+ return true;
+ }
+#endif
case SDL_WINDOWEVENT:
{
ImGuiViewport* viewport = ImGui_ImplSDL2_GetViewportForWindowID(event->window.windowID);
@@ -478,15 +502,17 @@ bool ImGui_ImplSDL2_ProcessEvent(const SDL_Event* event)
}
if (window_event == SDL_WINDOWEVENT_LEAVE)
bd->MouseLastLeaveFrame = ImGui::GetFrameCount() + 1;
+ //if (window_event == SDL_WINDOWEVENT_FOCUS_GAINED) { IMGUI_DEBUG_LOG("SDL_WINDOWEVENT_FOCUS_GAINED: windowId %d, viewport: %08X\n", event->window.windowID, viewport ? viewport->ID : 0); }
+ //if (window_event == SDL_WINDOWEVENT_FOCUS_LOST) { IMGUI_DEBUG_LOG("SDL_WINDOWEVENT_FOCUS_LOST: windowId %d, viewport: %08X\n", event->window.windowID, viewport ? viewport->ID : 0); }
if (window_event == SDL_WINDOWEVENT_FOCUS_GAINED)
io.AddFocusEvent(true);
- else if (window_event == SDL_WINDOWEVENT_FOCUS_LOST)
+ if (window_event == SDL_WINDOWEVENT_FOCUS_LOST)
io.AddFocusEvent(false);
- else if (window_event == SDL_WINDOWEVENT_CLOSE)
+ if (window_event == SDL_WINDOWEVENT_CLOSE)
viewport->PlatformRequestClose = true;
- else if (window_event == SDL_WINDOWEVENT_MOVED)
+ if (window_event == SDL_WINDOWEVENT_MOVED)
viewport->PlatformRequestMove = true;
- else if (window_event == SDL_WINDOWEVENT_RESIZED)
+ if (window_event == SDL_WINDOWEVENT_RESIZED)
viewport->PlatformRequestResize = true;
return true;
}
@@ -496,6 +522,8 @@ bool ImGui_ImplSDL2_ProcessEvent(const SDL_Event* event)
bd->WantUpdateGamepadsList = true;
return true;
}
+ default:
+ break;
}
return false;
}
@@ -509,26 +537,23 @@ static bool ImGui_ImplSDL2_Init(SDL_Window* window, SDL_Renderer* renderer, void
ImGuiIO& io = ImGui::GetIO();
IMGUI_CHECKVERSION();
IM_ASSERT(io.BackendPlatformUserData == nullptr && "Already initialized a platform backend!");
+ //SDL_SetHint(SDL_HINT_EVENT_LOGGING, "2");
- // Check and store if we are on a SDL backend that supports global mouse position
- // ("wayland" and "rpi" don't support it, but we chose to use a white-list instead of a black-list)
- bool mouse_can_use_global_state = false;
-#if SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE
- const char* sdl_backend = SDL_GetCurrentVideoDriver();
- const char* global_mouse_whitelist[] = { "windows", "cocoa", "x11", "DIVE", "VMAN" };
- for (int n = 0; n < IM_ARRAYSIZE(global_mouse_whitelist); n++)
- if (strncmp(sdl_backend, global_mouse_whitelist[n], strlen(global_mouse_whitelist[n])) == 0)
- mouse_can_use_global_state = true;
-#endif
+ // Obtain compiled and runtime versions
+ SDL_version ver_compiled;
+ SDL_version ver_runtime;
+ SDL_VERSION(&ver_compiled);
+ SDL_GetVersion(&ver_runtime);
// Setup backend capabilities flags
ImGui_ImplSDL2_Data* bd = IM_NEW(ImGui_ImplSDL2_Data)();
+ snprintf(bd->BackendPlatformName, sizeof(bd->BackendPlatformName), "imgui_impl_sdl2 (%u.%u.%u, %u.%u.%u)",
+ ver_compiled.major, ver_compiled.minor, ver_compiled.patch, ver_runtime.major, ver_runtime.minor, ver_runtime.patch);
io.BackendPlatformUserData = (void*)bd;
- io.BackendPlatformName = "imgui_impl_sdl2";
+ io.BackendPlatformName = bd->BackendPlatformName;
io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional)
io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used)
- if (mouse_can_use_global_state)
- io.BackendFlags |= ImGuiBackendFlags_PlatformHasViewports; // We can create multi-viewports on the Platform side (optional)
+ // (ImGuiBackendFlags_PlatformHasViewports may be set just below)
bd->Window = window;
bd->WindowID = SDL_GetWindowID(window);
@@ -536,13 +561,26 @@ static bool ImGui_ImplSDL2_Init(SDL_Window* window, SDL_Renderer* renderer, void
// SDL on Linux/OSX doesn't report events for unfocused windows (see https://github.com/ocornut/imgui/issues/4960)
// We will use 'MouseCanReportHoveredViewport' to set 'ImGuiBackendFlags_HasMouseHoveredViewport' dynamically each frame.
- bd->MouseCanUseGlobalState = mouse_can_use_global_state;
#ifndef __APPLE__
bd->MouseCanReportHoveredViewport = bd->MouseCanUseGlobalState;
#else
bd->MouseCanReportHoveredViewport = false;
#endif
+ // Check and store if we are on a SDL backend that supports SDL_GetGlobalMouseState() and SDL_CaptureMouse()
+ // ("wayland" and "rpi" don't support it, but we chose to use a white-list instead of a black-list)
+ bd->MouseCanUseGlobalState = false;
+ bd->MouseCanUseCapture = false;
+#if SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE
+ const char* sdl_backend = SDL_GetCurrentVideoDriver();
+ const char* capture_and_global_state_whitelist[] = { "windows", "cocoa", "x11", "DIVE", "VMAN" };
+ for (const char* item : capture_and_global_state_whitelist)
+ if (strncmp(sdl_backend, item, strlen(item)) == 0)
+ bd->MouseCanUseGlobalState = bd->MouseCanUseCapture = true;
+#endif
+ if (bd->MouseCanUseGlobalState)
+ io.BackendFlags |= ImGuiBackendFlags_PlatformHasViewports; // We can create multi-viewports on the Platform side (optional)
+
ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
platform_io.Platform_SetClipboardTextFn = ImGui_ImplSDL2_SetClipboardText;
platform_io.Platform_GetClipboardTextFn = ImGui_ImplSDL2_GetClipboardText;
@@ -666,9 +704,9 @@ void ImGui_ImplSDL2_Shutdown()
ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData();
IM_ASSERT(bd != nullptr && "No platform backend to shutdown, or already shutdown?");
ImGuiIO& io = ImGui::GetIO();
+ ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
ImGui_ImplSDL2_ShutdownMultiViewportSupport();
-
if (bd->ClipboardTextData)
SDL_free(bd->ClipboardTextData);
for (ImGuiMouseCursor cursor_n = 0; cursor_n < ImGuiMouseCursor_COUNT; cursor_n++)
@@ -678,6 +716,7 @@ void ImGui_ImplSDL2_Shutdown()
io.BackendPlatformName = nullptr;
io.BackendPlatformUserData = nullptr;
io.BackendFlags &= ~(ImGuiBackendFlags_HasMouseCursors | ImGuiBackendFlags_HasSetMousePos | ImGuiBackendFlags_HasGamepad | ImGuiBackendFlags_PlatformHasViewports | ImGuiBackendFlags_HasMouseHoveredViewport);
+ platform_io.ClearPlatformHandlers();
IM_DELETE(bd);
}
@@ -690,12 +729,15 @@ static void ImGui_ImplSDL2_UpdateMouseData()
// We forward mouse input when hovered or captured (via SDL_MOUSEMOTION) or when focused (below)
#if SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE
// - SDL_CaptureMouse() let the OS know e.g. that our drags can extend outside of parent boundaries (we want updated position) and shouldn't trigger other operations outside.
- // - Debuggers under Linux tends to leave captured mouse on break, which may be very inconvenient, so to migitate the issue we wait until mouse has moved to begin capture.
- bool want_capture = false;
- for (int button_n = 0; button_n < ImGuiMouseButton_COUNT && !want_capture; button_n++)
- if (ImGui::IsMouseDragging(button_n, 1.0f))
- want_capture = true;
- SDL_CaptureMouse(want_capture ? SDL_TRUE : SDL_FALSE);
+ // - Debuggers under Linux tends to leave captured mouse on break, which may be very inconvenient, so to mitigate the issue we wait until mouse has moved to begin capture.
+ if (bd->MouseCanUseCapture)
+ {
+ bool want_capture = false;
+ for (int button_n = 0; button_n < ImGuiMouseButton_COUNT && !want_capture; button_n++)
+ if (ImGui::IsMouseDragging(button_n, 1.0f))
+ want_capture = true;
+ SDL_CaptureMouse(want_capture ? SDL_TRUE : SDL_FALSE);
+ }
SDL_Window* focused_window = SDL_GetKeyboardFocus();
const bool is_app_focused = (focused_window && (bd->Window == focused_window || ImGui_ImplSDL2_GetViewportForWindowID(SDL_GetWindowID(focused_window)) != NULL));
@@ -717,13 +759,16 @@ static void ImGui_ImplSDL2_UpdateMouseData()
SDL_WarpMouseInWindow(bd->Window, (int)io.MousePos.x, (int)io.MousePos.y);
}
- // (Optional) Fallback to provide mouse position when focused (SDL_MOUSEMOTION already provides this when hovered or captured)
+ // (Optional) Fallback to provide unclamped mouse position when focused but not hovered (SDL_MOUSEMOTION already provides this when hovered or captured)
+ // Note that SDL_GetGlobalMouseState() is in theory slow on X11, but this only runs on rather specific cases. If a problem we may provide a way to opt-out this feature.
+ SDL_Window* hovered_window = SDL_GetMouseFocus();
const bool is_relative_mouse_mode = SDL_GetRelativeMouseMode() != 0;
- if (bd->MouseCanUseGlobalState && bd->MouseButtonsDown == 0 && !is_relative_mouse_mode)
+ if (hovered_window == NULL && bd->MouseCanUseGlobalState && bd->MouseButtonsDown == 0 && !is_relative_mouse_mode)
{
// Single-viewport mode: mouse position in client window coordinates (io.MousePos is (0,0) when the mouse is on the upper-left corner of the app window)
// Multi-viewport mode: mouse position in OS absolute coordinates (io.MousePos is (0,0) when the mouse is on the upper-left of the primary monitor)
- int mouse_x, mouse_y, window_x, window_y;
+ int mouse_x, mouse_y;
+ int window_x, window_y;
SDL_GetGlobalMouseState(&mouse_x, &mouse_y);
if (!(io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable))
{
@@ -777,6 +822,30 @@ static void ImGui_ImplSDL2_UpdateMouseCursor()
}
}
+// - On Windows the process needs to be marked DPI-aware!! SDL2 doesn't do it by default. You can call ::SetProcessDPIAware() or call ImGui_ImplWin32_EnableDpiAwareness() from Win32 backend.
+// - Apple platforms use FramebufferScale so we always return 1.0f.
+// - Some accessibility applications are declaring virtual monitors with a DPI of 0.0f, see #7902. We preserve this value for caller to handle.
+float ImGui_ImplSDL2_GetContentScaleForWindow(SDL_Window* window)
+{
+ return ImGui_ImplSDL2_GetContentScaleForDisplay(SDL_GetWindowDisplayIndex(window));
+}
+
+float ImGui_ImplSDL2_GetContentScaleForDisplay(int display_index)
+{
+ const char* sdl_driver = SDL_GetCurrentVideoDriver();
+ if (sdl_driver && strcmp(sdl_driver, "wayland") == 0)
+ return 1.0f;
+#if SDL_HAS_PER_MONITOR_DPI
+#if !defined(__APPLE__) && !defined(__EMSCRIPTEN__) && !defined(__ANDROID__)
+ float dpi = 0.0f;
+ if (SDL_GetDisplayDPI(display_index, &dpi, nullptr, nullptr) == 0)
+ return dpi / 96.0f;
+#endif
+#endif
+ IM_UNUSED(display_index);
+ return 1.0f;
+}
+
static void ImGui_ImplSDL2_CloseGamepads()
{
ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData();
@@ -882,8 +951,10 @@ static void ImGui_ImplSDL2_UpdateGamepads()
// FIXME: Note that doesn't update with DPI/Scaling change only as SDL2 doesn't have an event for it (SDL3 has).
static void ImGui_ImplSDL2_UpdateMonitors()
{
+ ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData();
ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
platform_io.Monitors.resize(0);
+ bd->WantUpdateMonitors = false;
int display_count = SDL_GetNumVideoDisplays();
for (int n = 0; n < display_count; n++)
{
@@ -900,48 +971,51 @@ static void ImGui_ImplSDL2_UpdateMonitors()
monitor.WorkSize = ImVec2((float)r.w, (float)r.h);
}
#endif
-#if SDL_HAS_PER_MONITOR_DPI
- // FIXME-VIEWPORT: On MacOS SDL reports actual monitor DPI scale, ignoring OS configuration. We may want to set
- // DpiScale to cocoa_window.backingScaleFactor here.
- float dpi = 0.0f;
- if (!SDL_GetDisplayDPI(n, &dpi, nullptr, nullptr))
- {
- if (dpi <= 0.0f)
- continue; // Some accessibility applications are declaring virtual monitors with a DPI of 0, see #7902.
- monitor.DpiScale = dpi / 96.0f;
- }
-#endif
+ float dpi_scale = ImGui_ImplSDL2_GetContentScaleForDisplay(n);
+ if (dpi_scale <= 0.0f)
+ continue; // Some accessibility applications are declaring virtual monitors with a DPI of 0, see #7902.
+ monitor.DpiScale = dpi_scale;
monitor.PlatformHandle = (void*)(intptr_t)n;
platform_io.Monitors.push_back(monitor);
}
}
+static void ImGui_ImplSDL2_GetWindowSizeAndFramebufferScale(SDL_Window* window, SDL_Renderer* renderer, ImVec2* out_size, ImVec2* out_framebuffer_scale)
+{
+ int w, h;
+ int display_w, display_h;
+ SDL_GetWindowSize(window, &w, &h);
+ if (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED)
+ w = h = 0;
+ if (renderer != nullptr)
+ SDL_GetRendererOutputSize(renderer, &display_w, &display_h);
+#if SDL_HAS_VULKAN
+ else if (SDL_GetWindowFlags(window) & SDL_WINDOW_VULKAN)
+ SDL_Vulkan_GetDrawableSize(window, &display_w, &display_h);
+#endif
+ else
+ SDL_GL_GetDrawableSize(window, &display_w, &display_h);
+ if (out_size != nullptr)
+ *out_size = ImVec2((float)w, (float)h);
+ if (out_framebuffer_scale != nullptr)
+ *out_framebuffer_scale = (w > 0 && h > 0) ? ImVec2((float)display_w / w, (float)display_h / h) : ImVec2(1.0f, 1.0f);
+}
+
void ImGui_ImplSDL2_NewFrame()
{
ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData();
IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplSDL2_Init()?");
ImGuiIO& io = ImGui::GetIO();
- // Setup display size (every frame to accommodate for window resizing)
- int w, h;
- int display_w, display_h;
- SDL_GetWindowSize(bd->Window, &w, &h);
- if (SDL_GetWindowFlags(bd->Window) & SDL_WINDOW_MINIMIZED)
- w = h = 0;
- if (bd->Renderer != nullptr)
- SDL_GetRendererOutputSize(bd->Renderer, &display_w, &display_h);
-#if SDL_HAS_VULKAN
- else if (SDL_GetWindowFlags(bd->Window) & SDL_WINDOW_VULKAN)
- SDL_Vulkan_GetDrawableSize(bd->Window, &display_w, &display_h);
-#endif
- else
- SDL_GL_GetDrawableSize(bd->Window, &display_w, &display_h);
- io.DisplaySize = ImVec2((float)w, (float)h);
- if (w > 0 && h > 0)
- io.DisplayFramebufferScale = ImVec2((float)display_w / w, (float)display_h / h);
+ // Setup main viewport size (every frame to accommodate for window resizing)
+ ImGui_ImplSDL2_GetWindowSizeAndFramebufferScale(bd->Window, bd->Renderer, &io.DisplaySize, &io.DisplayFramebufferScale);
// Update monitors
- ImGui_ImplSDL2_UpdateMonitors();
+#ifdef WIN32
+ bd->WantUpdateMonitors = true; // Keep polling under Windows to handle changes of work area when resizing task-bar (#8415)
+#endif
+ if (bd->WantUpdateMonitors)
+ ImGui_ImplSDL2_UpdateMonitors();
// Setup time step (we don't use SDL_GetTicks() because it is using millisecond resolution)
// (Accept SDL_GetPerformanceCounter() not returning a monotonically increasing value. Happens in VMs and Emscripten, see #6189, #6114, #3644)
@@ -1065,7 +1139,7 @@ static void ImGui_ImplSDL2_DestroyWindow(ImGuiViewport* viewport)
static void ImGui_ImplSDL2_ShowWindow(ImGuiViewport* viewport)
{
ImGui_ImplSDL2_ViewportData* vd = (ImGui_ImplSDL2_ViewportData*)viewport->PlatformUserData;
-#if defined(_WIN32) && !(defined(WINAPI_FAMILY) && (WINAPI_FAMILY == WINAPI_FAMILY_APP || WINAPI_FAMILY == WINAPI_FAMILY_GAMES))
+#if defined(_WIN32) && !(defined(WINAPI_FAMILY) && ((defined(WINAPI_FAMILY_APP) && WINAPI_FAMILY == WINAPI_FAMILY_APP) || (defined(WINAPI_FAMILY_GAMES) && WINAPI_FAMILY == WINAPI_FAMILY_GAMES)))
HWND hwnd = (HWND)viewport->PlatformHandleRaw;
// SDL hack: Hide icon from task bar
@@ -1120,6 +1194,15 @@ static void ImGui_ImplSDL2_SetWindowSize(ImGuiViewport* viewport, ImVec2 size)
SDL_SetWindowSize(vd->Window, (int)size.x, (int)size.y);
}
+static ImVec2 ImGui_ImplSDL2_GetWindowFramebufferScale(ImGuiViewport* viewport)
+{
+ // FIXME: SDL_Renderer does not support multi-viewport.
+ ImGui_ImplSDL2_ViewportData* vd = (ImGui_ImplSDL2_ViewportData*)viewport->PlatformUserData;
+ ImVec2 framebuffer_scale;
+ ImGui_ImplSDL2_GetWindowSizeAndFramebufferScale(vd->Window, nullptr, nullptr, &framebuffer_scale);
+ return framebuffer_scale;
+}
+
static void ImGui_ImplSDL2_SetWindowTitle(ImGuiViewport* viewport, const char* title)
{
ImGui_ImplSDL2_ViewportData* vd = (ImGui_ImplSDL2_ViewportData*)viewport->PlatformUserData;
@@ -1193,6 +1276,7 @@ static void ImGui_ImplSDL2_InitMultiViewportSupport(SDL_Window* window, void* sd
platform_io.Platform_GetWindowPos = ImGui_ImplSDL2_GetWindowPos;
platform_io.Platform_SetWindowSize = ImGui_ImplSDL2_SetWindowSize;
platform_io.Platform_GetWindowSize = ImGui_ImplSDL2_GetWindowSize;
+ platform_io.Platform_GetWindowFramebufferScale = ImGui_ImplSDL2_GetWindowFramebufferScale;
platform_io.Platform_SetWindowFocus = ImGui_ImplSDL2_SetWindowFocus;
platform_io.Platform_GetWindowFocus = ImGui_ImplSDL2_GetWindowFocus;
platform_io.Platform_GetWindowMinimized = ImGui_ImplSDL2_GetWindowMinimized;
diff --git a/backends/imgui_impl_sdl2.h b/backends/imgui_impl_sdl2.h
index 908c84878..1ad836544 100644
--- a/backends/imgui_impl_sdl2.h
+++ b/backends/imgui_impl_sdl2.h
@@ -12,7 +12,7 @@
// [X] Platform: Multi-viewport support (multiple windows). Enable with 'io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable'.
// Missing features or Issues:
// [ ] Platform: Multi-viewport: Minimized windows seems to break mouse wheel events (at least under Windows).
-// [ ] Platform: Multi-viewport: ParentViewportID not honored, and so io.ConfigViewportsNoDefaultParent has no effect (minor).
+// [ ] Platform: Multi-viewport: Missing ImGuiBackendFlags_HasParentViewport support. The viewport->ParentViewportID field is ignored, and therefore io.ConfigViewportsNoDefaultParent has no effect either.
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
@@ -42,6 +42,10 @@ IMGUI_IMPL_API void ImGui_ImplSDL2_Shutdown();
IMGUI_IMPL_API void ImGui_ImplSDL2_NewFrame();
IMGUI_IMPL_API bool ImGui_ImplSDL2_ProcessEvent(const SDL_Event* event);
+// DPI-related helpers (optional)
+IMGUI_IMPL_API float ImGui_ImplSDL2_GetContentScaleForWindow(SDL_Window* window);
+IMGUI_IMPL_API float ImGui_ImplSDL2_GetContentScaleForDisplay(int display_index);
+
// Gamepad selection automatically starts in AutoFirst mode, picking first available SDL_Gamepad. You may override this.
// When using manual mode, caller is responsible for opening/closing gamepad.
enum ImGui_ImplSDL2_GamepadMode { ImGui_ImplSDL2_GamepadMode_AutoFirst, ImGui_ImplSDL2_GamepadMode_AutoAll, ImGui_ImplSDL2_GamepadMode_Manual };
diff --git a/backends/imgui_impl_sdl3.cpp b/backends/imgui_impl_sdl3.cpp
index 5d139edaf..e475f3014 100644
--- a/backends/imgui_impl_sdl3.cpp
+++ b/backends/imgui_impl_sdl3.cpp
@@ -24,6 +24,15 @@
// CHANGELOG
// (minor and older changes stripped away, please see git history for details)
// 2025-XX-XX: Platform: Added support for multiple windows via the ImGuiPlatformIO interface.
+// 2025-09-24: Skip using the SDL_GetGlobalMouseState() state when one of our window is hovered, as the SDL_EVENT_MOUSE_MOTION data is reliable. Fix macOS notch mouse coordinates issue in fullscreen mode + better perf on X11. (#7919, #7786)
+// 2025-09-18: Call platform_io.ClearPlatformHandlers() on shutdown.
+// 2025-09-15: Use SDL_GetWindowDisplayScale() on Mac to output DisplayFrameBufferScale. The function is more reliable during resolution changes e.g. going fullscreen. (#8703, #4414)
+// 2025-06-27: IME: avoid calling SDL_StartTextInput() again if already active. (#8727)
+// 2025-05-15: [Docking] Add Platform_GetWindowFramebufferScale() handler, to allow varying Retina display density on multiple monitors.
+// 2025-05-06: [Docking] macOS: fixed secondary viewports not appearing on other monitors before of parenting.
+// 2025-04-09: [Docking] Revert update monitors and work areas information every frame. Only do it on Windows. (#8415, #8558)
+// 2025-04-22: IME: honor ImGuiPlatformImeData->WantTextInput as an alternative way to call SDL_StartTextInput(), without IME being necessarily visible.
+// 2025-04-09: Don't attempt to call SDL_CaptureMouse() on drivers where we don't call SDL_GetGlobalMouseState(). (#8561)
// 2025-03-30: Update for SDL3 api changes: Revert SDL_GetClipboardText() memory ownership change. (#8530, #7801)
// 2025-03-21: Fill gamepad inputs and set ImGuiBackendFlags_HasGamepad regardless of ImGuiConfigFlags_NavEnableGamepad being set.
// 2025-03-10: When dealing with OEM keys, use scancodes instead of translated keycodes to choose ImGuiKey values. (#7136, #7201, #7206, #7306, #7670, #7672, #8468)
@@ -69,11 +78,13 @@
// Clang warnings with -Weverything
#if defined(__clang__)
#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wold-style-cast" // warning: use of old-style cast
#pragma clang diagnostic ignored "-Wimplicit-int-float-conversion" // warning: implicit conversion from 'xxx' to 'float' may lose precision
#endif
// SDL
#include
+#include // for snprintf()
#if defined(__APPLE__)
#include
#endif
@@ -106,7 +117,9 @@ struct ImGui_ImplSDL3_Data
SDL_Renderer* Renderer;
Uint64 Time;
char* ClipboardTextData;
+ char BackendPlatformName[48];
bool UseVulkan;
+ bool WantUpdateMonitors;
// IME handling
SDL_Window* ImeWindow;
@@ -118,6 +131,7 @@ struct ImGui_ImplSDL3_Data
SDL_Cursor* MouseLastCursor;
int MousePendingLeaveFrame;
bool MouseCanUseGlobalState;
+ bool MouseCanUseCapture;
bool MouseCanReportHoveredViewport; // This is hard to use/unreliable on SDL so we'll set ImGuiBackendFlags_HasMouseHoveredViewport dynamically based on state.
// Gamepad handling
@@ -162,7 +176,7 @@ static void ImGui_ImplSDL3_PlatformSetImeData(ImGuiContext*, ImGuiViewport* view
ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
SDL_WindowID window_id = (SDL_WindowID)(intptr_t)viewport->PlatformHandle;
SDL_Window* window = SDL_GetWindowFromID(window_id);
- if ((data->WantVisible == false || bd->ImeWindow != window) && bd->ImeWindow != nullptr)
+ if ((!(data->WantVisible || data->WantTextInput) || bd->ImeWindow != window) && bd->ImeWindow != nullptr)
{
SDL_StopTextInput(bd->ImeWindow);
bd->ImeWindow = nullptr;
@@ -175,9 +189,10 @@ static void ImGui_ImplSDL3_PlatformSetImeData(ImGuiContext*, ImGuiViewport* view
r.w = 1;
r.h = (int)data->InputLineHeight;
SDL_SetTextInputArea(window, &r, 0);
- SDL_StartTextInput(window);
bd->ImeWindow = window;
}
+ if (!SDL_TextInputActive(window) && (data->WantVisible || data->WantTextInput))
+ SDL_StartTextInput(window);
}
// Not static to allow third-party code to use that if they want to (but undocumented)
@@ -414,14 +429,24 @@ bool ImGui_ImplSDL3_ProcessEvent(const SDL_Event* event)
case SDL_EVENT_KEY_DOWN:
case SDL_EVENT_KEY_UP:
{
- if (ImGui_ImplSDL3_GetViewportForWindowID(event->key.windowID) == nullptr)
+ ImGuiViewport* viewport = ImGui_ImplSDL3_GetViewportForWindowID(event->key.windowID);
+ if (viewport == nullptr)
return false;
+ //IMGUI_DEBUG_LOG("SDL_EVENT_KEY_%s : key=0x%08X ('%s'), scancode=%d ('%s'), mod=%X, windowID=%d, viewport=%08X\n",
+ // (event->type == SDL_EVENT_KEY_DOWN) ? "DOWN" : "UP ", event->key.key, SDL_GetKeyName(event->key.key), event->key.scancode, SDL_GetScancodeName(event->key.scancode), event->key.mod, event->key.windowID, viewport ? viewport->ID : 0);
ImGui_ImplSDL3_UpdateKeyModifiers((SDL_Keymod)event->key.mod);
- //IMGUI_DEBUG_LOG("SDL_EVENT_KEY_%s : key=%d ('%s'), scancode=%d ('%s'), mod=%X\n",
- // (event->type == SDL_EVENT_KEY_DOWN) ? "DOWN" : "UP ", event->key.key, SDL_GetKeyName(event->key.key), event->key.scancode, SDL_GetScancodeName(event->key.scancode), event->key.mod);
ImGuiKey key = ImGui_ImplSDL3_KeyEventToImGuiKey(event->key.key, event->key.scancode);
io.AddKeyEvent(key, (event->type == SDL_EVENT_KEY_DOWN));
- io.SetKeyEventNativeData(key, event->key.key, event->key.scancode, event->key.scancode); // To support legacy indexing (<1.87 user code). Legacy backend uses SDLK_*** as indices to IsKeyXXX() functions.
+ io.SetKeyEventNativeData(key, (int)event->key.key, (int)event->key.scancode, (int)event->key.scancode); // To support legacy indexing (<1.87 user code). Legacy backend uses SDLK_*** as indices to IsKeyXXX() functions.
+ return true;
+ }
+ case SDL_EVENT_DISPLAY_ORIENTATION:
+ case SDL_EVENT_DISPLAY_ADDED:
+ case SDL_EVENT_DISPLAY_REMOVED:
+ case SDL_EVENT_DISPLAY_MOVED:
+ case SDL_EVENT_DISPLAY_CONTENT_SCALE_CHANGED:
+ {
+ bd->WantUpdateMonitors = true;
return true;
}
case SDL_EVENT_WINDOW_MOUSE_ENTER:
@@ -446,8 +471,10 @@ bool ImGui_ImplSDL3_ProcessEvent(const SDL_Event* event)
case SDL_EVENT_WINDOW_FOCUS_GAINED:
case SDL_EVENT_WINDOW_FOCUS_LOST:
{
- if (ImGui_ImplSDL3_GetViewportForWindowID(event->window.windowID) == nullptr)
+ ImGuiViewport* viewport = ImGui_ImplSDL3_GetViewportForWindowID(event->window.windowID);
+ if (viewport == nullptr)
return false;
+ //IMGUI_DEBUG_LOG("%s: windowId %d, viewport: %08X\n", (event->type == SDL_EVENT_WINDOW_FOCUS_GAINED) ? "SDL_EVENT_WINDOW_FOCUS_GAINED" : "SDL_WINDOWEVENT_FOCUS_LOST", event->window.windowID, viewport ? viewport->ID : 0);
io.AddFocusEvent(event->type == SDL_EVENT_WINDOW_FOCUS_GAINED);
return true;
}
@@ -472,6 +499,8 @@ bool ImGui_ImplSDL3_ProcessEvent(const SDL_Event* event)
bd->WantUpdateGamepadsList = true;
return true;
}
+ default:
+ break;
}
return false;
}
@@ -482,7 +511,7 @@ static void ImGui_ImplSDL3_SetupPlatformHandles(ImGuiViewport* viewport, SDL_Win
viewport->PlatformHandleRaw = nullptr;
#if defined(_WIN32) && !defined(__WINRT__)
viewport->PlatformHandleRaw = (HWND)SDL_GetPointerProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_WIN32_HWND_POINTER, nullptr);
-#elif defined(__APPLE__) && defined(SDL_VIDEO_DRIVER_COCOA)
+#elif defined(__APPLE__)
viewport->PlatformHandleRaw = SDL_GetPointerProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_COCOA_WINDOW_POINTER, nullptr);
#endif
}
@@ -493,26 +522,20 @@ static bool ImGui_ImplSDL3_Init(SDL_Window* window, SDL_Renderer* renderer, void
IMGUI_CHECKVERSION();
IM_ASSERT(io.BackendPlatformUserData == nullptr && "Already initialized a platform backend!");
IM_UNUSED(sdl_gl_context); // Unused in this branch
+ //SDL_SetHint(SDL_HINT_EVENT_LOGGING, "2");
- // Check and store if we are on a SDL backend that supports global mouse position
- // ("wayland" and "rpi" don't support it, but we chose to use a white-list instead of a black-list)
- bool mouse_can_use_global_state = false;
-#if SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE
- const char* sdl_backend = SDL_GetCurrentVideoDriver();
- const char* global_mouse_whitelist[] = { "windows", "cocoa", "x11", "DIVE", "VMAN" };
- for (int n = 0; n < IM_ARRAYSIZE(global_mouse_whitelist); n++)
- if (strncmp(sdl_backend, global_mouse_whitelist[n], strlen(global_mouse_whitelist[n])) == 0)
- mouse_can_use_global_state = true;
-#endif
+ const int ver_linked = SDL_GetVersion();
// Setup backend capabilities flags
ImGui_ImplSDL3_Data* bd = IM_NEW(ImGui_ImplSDL3_Data)();
+ snprintf(bd->BackendPlatformName, sizeof(bd->BackendPlatformName), "imgui_impl_sdl3 (%d.%d.%d; %d.%d.%d)",
+ SDL_MAJOR_VERSION, SDL_MINOR_VERSION, SDL_MICRO_VERSION, SDL_VERSIONNUM_MAJOR(ver_linked), SDL_VERSIONNUM_MINOR(ver_linked), SDL_VERSIONNUM_MICRO(ver_linked));
io.BackendPlatformUserData = (void*)bd;
- io.BackendPlatformName = "imgui_impl_sdl3";
+ io.BackendPlatformName = bd->BackendPlatformName;
io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional)
io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used)
- if (mouse_can_use_global_state)
- io.BackendFlags |= ImGuiBackendFlags_PlatformHasViewports; // We can create multi-viewports on the Platform side (optional)
+ // (ImGuiBackendFlags_PlatformHasViewports and ImGuiBackendFlags_HasParentViewport may be set just below)
+ // (ImGuiBackendFlags_HasMouseHoveredViewport is set dynamically in our _NewFrame function)
bd->Window = window;
bd->WindowID = SDL_GetWindowID(window);
@@ -520,13 +543,29 @@ static bool ImGui_ImplSDL3_Init(SDL_Window* window, SDL_Renderer* renderer, void
// SDL on Linux/OSX doesn't report events for unfocused windows (see https://github.com/ocornut/imgui/issues/4960)
// We will use 'MouseCanReportHoveredViewport' to set 'ImGuiBackendFlags_HasMouseHoveredViewport' dynamically each frame.
- bd->MouseCanUseGlobalState = mouse_can_use_global_state;
#ifndef __APPLE__
bd->MouseCanReportHoveredViewport = bd->MouseCanUseGlobalState;
#else
bd->MouseCanReportHoveredViewport = false;
#endif
+ // Check and store if we are on a SDL backend that supports SDL_GetGlobalMouseState() and SDL_CaptureMouse()
+ // ("wayland" and "rpi" don't support it, but we chose to use a white-list instead of a black-list)
+ bd->MouseCanUseGlobalState = false;
+ bd->MouseCanUseCapture = false;
+#if SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE
+ const char* sdl_backend = SDL_GetCurrentVideoDriver();
+ const char* capture_and_global_state_whitelist[] = { "windows", "cocoa", "x11", "DIVE", "VMAN" };
+ for (const char* item : capture_and_global_state_whitelist)
+ if (strncmp(sdl_backend, item, strlen(item)) == 0)
+ bd->MouseCanUseGlobalState = bd->MouseCanUseCapture = true;
+#endif
+ if (bd->MouseCanUseGlobalState)
+ {
+ io.BackendFlags |= ImGuiBackendFlags_PlatformHasViewports; // We can create multi-viewports on the Platform side (optional)
+ io.BackendFlags |= ImGuiBackendFlags_HasParentViewport; // We can honor viewport->ParentViewportId by applying the corresponding parent/child relationship at platform level (optional)
+ }
+
ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
platform_io.Platform_SetClipboardTextFn = ImGui_ImplSDL3_SetClipboardText;
platform_io.Platform_GetClipboardTextFn = ImGui_ImplSDL3_GetClipboardText;
@@ -562,7 +601,7 @@ static bool ImGui_ImplSDL3_Init(SDL_Window* window, SDL_Renderer* renderer, void
// Without this, when clicking to gain focus, our widgets wouldn't activate even though they showed as hovered.
// (This is unfortunately a global SDL setting, so enabling it might have a side-effect on your application.
// It is unlikely to make a difference, but if your app absolutely needs to ignore the initial on-focus click:
- // you can ignore SDL_EVENT_MOUSE_BUTTON_DOWN events coming right after a SDL_WINDOWEVENT_FOCUS_GAINED)
+ // you can ignore SDL_EVENT_MOUSE_BUTTON_DOWN events coming right after a SDL_EVENT_WINDOW_FOCUS_GAINED)
SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1");
// From 2.0.22: Disable auto-capture, this is preventing drag and drop across multiple windows (see #5710)
@@ -629,9 +668,9 @@ void ImGui_ImplSDL3_Shutdown()
ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
IM_ASSERT(bd != nullptr && "No platform backend to shutdown, or already shutdown?");
ImGuiIO& io = ImGui::GetIO();
+ ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
ImGui_ImplSDL3_ShutdownMultiViewportSupport();
-
if (bd->ClipboardTextData)
SDL_free(bd->ClipboardTextData);
for (ImGuiMouseCursor cursor_n = 0; cursor_n < ImGuiMouseCursor_COUNT; cursor_n++)
@@ -640,7 +679,8 @@ void ImGui_ImplSDL3_Shutdown()
io.BackendPlatformName = nullptr;
io.BackendPlatformUserData = nullptr;
- io.BackendFlags &= ~(ImGuiBackendFlags_HasMouseCursors | ImGuiBackendFlags_HasSetMousePos | ImGuiBackendFlags_HasGamepad | ImGuiBackendFlags_PlatformHasViewports | ImGuiBackendFlags_HasMouseHoveredViewport);
+ io.BackendFlags &= ~(ImGuiBackendFlags_HasMouseCursors | ImGuiBackendFlags_HasSetMousePos | ImGuiBackendFlags_HasGamepad | ImGuiBackendFlags_PlatformHasViewports | ImGuiBackendFlags_HasMouseHoveredViewport | ImGuiBackendFlags_HasParentViewport);
+ platform_io.ClearPlatformHandlers();
IM_DELETE(bd);
}
@@ -653,12 +693,15 @@ static void ImGui_ImplSDL3_UpdateMouseData()
// We forward mouse input when hovered or captured (via SDL_EVENT_MOUSE_MOTION) or when focused (below)
#if SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE
// - SDL_CaptureMouse() let the OS know e.g. that our drags can extend outside of parent boundaries (we want updated position) and shouldn't trigger other operations outside.
- // - Debuggers under Linux tends to leave captured mouse on break, which may be very inconvenient, so to migitate the issue we wait until mouse has moved to begin capture.
- bool want_capture = false;
- for (int button_n = 0; button_n < ImGuiMouseButton_COUNT && !want_capture; button_n++)
- if (ImGui::IsMouseDragging(button_n, 1.0f))
- want_capture = true;
- SDL_CaptureMouse(want_capture);
+ // - Debuggers under Linux tends to leave captured mouse on break, which may be very inconvenient, so to mitigate the issue we wait until mouse has moved to begin capture.
+ if (bd->MouseCanUseCapture)
+ {
+ bool want_capture = false;
+ for (int button_n = 0; button_n < ImGuiMouseButton_COUNT && !want_capture; button_n++)
+ if (ImGui::IsMouseDragging(button_n, 1.0f))
+ want_capture = true;
+ SDL_CaptureMouse(want_capture);
+ }
SDL_Window* focused_window = SDL_GetKeyboardFocus();
const bool is_app_focused = (focused_window && (bd->Window == focused_window || ImGui_ImplSDL3_GetViewportForWindowID(SDL_GetWindowID(focused_window)) != NULL));
@@ -679,9 +722,11 @@ static void ImGui_ImplSDL3_UpdateMouseData()
SDL_WarpMouseInWindow(bd->Window, io.MousePos.x, io.MousePos.y);
}
- // (Optional) Fallback to provide mouse position when focused (SDL_EVENT_MOUSE_MOTION already provides this when hovered or captured)
+ // (Optional) Fallback to provide unclamped mouse position when focused but not hovered (SDL_EVENT_MOUSE_MOTION already provides this when hovered or captured)
+ // Note that SDL_GetGlobalMouseState() is in theory slow on X11, but this only runs on rather specific cases. If a problem we may provide a way to opt-out this feature.
+ SDL_Window* hovered_window = SDL_GetMouseFocus();
const bool is_relative_mouse_mode = SDL_GetWindowRelativeMouseMode(bd->Window);
- if (bd->MouseCanUseGlobalState && bd->MouseButtonsDown == 0 && !is_relative_mouse_mode)
+ if (hovered_window == NULL && bd->MouseCanUseGlobalState && bd->MouseButtonsDown == 0 && !is_relative_mouse_mode)
{
// Single-viewport mode: mouse position in client window coordinates (io.MousePos is (0,0) when the mouse is on the upper-left corner of the app window)
// Multi-viewport mode: mouse position in OS absolute coordinates (io.MousePos is (0,0) when the mouse is on the upper-left of the primary monitor)
@@ -694,7 +739,7 @@ static void ImGui_ImplSDL3_UpdateMouseData()
mouse_x -= window_x;
mouse_y -= window_y;
}
- io.AddMousePosEvent((float)mouse_x, (float)mouse_y);
+ io.AddMousePosEvent(mouse_x, mouse_y);
}
}
@@ -845,8 +890,10 @@ static void ImGui_ImplSDL3_UpdateGamepads()
static void ImGui_ImplSDL3_UpdateMonitors()
{
+ ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
platform_io.Monitors.resize(0);
+ bd->WantUpdateMonitors = false;
int display_count;
SDL_DisplayID* displays = SDL_GetDisplays(&display_count);
@@ -864,9 +911,7 @@ static void ImGui_ImplSDL3_UpdateMonitors()
monitor.WorkPos = ImVec2((float)r.x, (float)r.y);
monitor.WorkSize = ImVec2((float)r.w, (float)r.h);
}
- // FIXME-VIEWPORT: On MacOS SDL reports actual monitor DPI scale, ignoring OS configuration. We may want to set
- // DpiScale to cocoa_window.backingScaleFactor here.
- monitor.DpiScale = SDL_GetDisplayContentScale(display_id);
+ monitor.DpiScale = SDL_GetDisplayContentScale(display_id); // See https://wiki.libsdl.org/SDL3/README-highdpi for details.
monitor.PlatformHandle = (void*)(intptr_t)n;
if (monitor.DpiScale <= 0.0f)
continue; // Some accessibility applications are declaring virtual monitors with a DPI of 0, see #7902.
@@ -875,27 +920,46 @@ static void ImGui_ImplSDL3_UpdateMonitors()
SDL_free(displays);
}
+static void ImGui_ImplSDL3_GetWindowSizeAndFramebufferScale(SDL_Window* window, ImVec2* out_size, ImVec2* out_framebuffer_scale)
+{
+ int w, h;
+ SDL_GetWindowSize(window, &w, &h);
+ if (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED)
+ w = h = 0;
+
+#if defined(__APPLE__)
+ float fb_scale_x = SDL_GetWindowDisplayScale(window); // Seems more reliable during resolution change (#8703)
+ float fb_scale_y = fb_scale_x;
+#else
+ int display_w, display_h;
+ SDL_GetWindowSizeInPixels(window, &display_w, &display_h);
+ float fb_scale_x = (w > 0) ? (float)display_w / w : 1.0f;
+ float fb_scale_y = (h > 0) ? (float)display_h / h : 1.0f;
+#endif
+
+ if (out_size != nullptr)
+ *out_size = ImVec2((float)w, (float)h);
+ if (out_framebuffer_scale != nullptr)
+ *out_framebuffer_scale = ImVec2(fb_scale_x, fb_scale_y);
+}
+
void ImGui_ImplSDL3_NewFrame()
{
ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplSDL3_Init()?");
ImGuiIO& io = ImGui::GetIO();
- // Setup display size (every frame to accommodate for window resizing)
- int w, h;
- int display_w, display_h;
- SDL_GetWindowSize(bd->Window, &w, &h);
- if (SDL_GetWindowFlags(bd->Window) & SDL_WINDOW_MINIMIZED)
- w = h = 0;
- SDL_GetWindowSizeInPixels(bd->Window, &display_w, &display_h);
- io.DisplaySize = ImVec2((float)w, (float)h);
- if (w > 0 && h > 0)
- io.DisplayFramebufferScale = ImVec2((float)display_w / w, (float)display_h / h);
+ // Setup main viewport size (every frame to accommodate for window resizing)
+ ImGui_ImplSDL3_GetWindowSizeAndFramebufferScale(bd->Window, &io.DisplaySize, &io.DisplayFramebufferScale);
// Update monitors
- ImGui_ImplSDL3_UpdateMonitors();
+#ifdef WIN32
+ bd->WantUpdateMonitors = true; // Keep polling under Windows to handle changes of work area when resizing task-bar (#8415)
+#endif
+ if (bd->WantUpdateMonitors)
+ ImGui_ImplSDL3_UpdateMonitors();
- // Setup time step (we don't use SDL_GetTicks() because it is using millisecond resolution)
+ // Setup time step (we could also use SDL_GetTicksNS() available since SDL3)
// (Accept SDL_GetPerformanceCounter() not returning a monotonically increasing value. Happens in VMs and Emscripten, see #6189, #6114, #3644)
static Uint64 frequency = SDL_GetPerformanceFrequency();
Uint64 current_time = SDL_GetPerformanceCounter();
@@ -944,14 +1008,13 @@ struct ImGui_ImplSDL3_ViewportData
~ImGui_ImplSDL3_ViewportData() { IM_ASSERT(Window == nullptr && GLContext == nullptr); }
};
-static SDL_Window* ImGui_ImplSDL3_GetSDLWindowFromViewportID(ImGuiID viewport_id)
+static SDL_Window* ImGui_ImplSDL3_GetSDLWindowFromViewport(ImGuiViewport* viewport)
{
- if (viewport_id != 0)
- if (ImGuiViewport* viewport = ImGui::FindViewportByID(viewport_id))
- {
- SDL_WindowID window_id = (SDL_WindowID)(intptr_t)viewport->PlatformHandle;
- return SDL_GetWindowFromID(window_id);
- }
+ if (viewport != nullptr)
+ {
+ SDL_WindowID window_id = (SDL_WindowID)(intptr_t)viewport->PlatformHandle;
+ return SDL_GetWindowFromID(window_id);
+ }
return nullptr;
}
@@ -961,7 +1024,7 @@ static void ImGui_ImplSDL3_CreateWindow(ImGuiViewport* viewport)
ImGui_ImplSDL3_ViewportData* vd = IM_NEW(ImGui_ImplSDL3_ViewportData)();
viewport->PlatformUserData = vd;
- vd->ParentWindow = ImGui_ImplSDL3_GetSDLWindowFromViewportID(viewport->ParentViewportId);
+ vd->ParentWindow = ImGui_ImplSDL3_GetSDLWindowFromViewport(viewport->ParentViewport);
ImGuiViewport* main_viewport = ImGui::GetMainViewport();
ImGui_ImplSDL3_ViewportData* main_viewport_data = (ImGui_ImplSDL3_ViewportData*)main_viewport->PlatformUserData;
@@ -985,7 +1048,9 @@ static void ImGui_ImplSDL3_CreateWindow(ImGuiViewport* viewport)
sdl_flags |= (viewport->Flags & ImGuiViewportFlags_NoTaskBarIcon) ? SDL_WINDOW_UTILITY : 0;
sdl_flags |= (viewport->Flags & ImGuiViewportFlags_TopMost) ? SDL_WINDOW_ALWAYS_ON_TOP : 0;
vd->Window = SDL_CreateWindow("No Title Yet", (int)viewport->Size.x, (int)viewport->Size.y, sdl_flags);
+#ifndef __APPLE__ // On Mac, SDL3 Parenting appears to prevent viewport from appearing in another monitor
SDL_SetWindowParent(vd->Window, vd->ParentWindow);
+#endif
SDL_SetWindowPosition(vd->Window, (int)viewport->Pos.x, (int)viewport->Pos.y);
vd->WindowOwned = true;
if (use_opengl)
@@ -1017,7 +1082,7 @@ static void ImGui_ImplSDL3_DestroyWindow(ImGuiViewport* viewport)
static void ImGui_ImplSDL3_ShowWindow(ImGuiViewport* viewport)
{
ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData;
-#if defined(_WIN32) && !(defined(WINAPI_FAMILY) && (WINAPI_FAMILY == WINAPI_FAMILY_APP || WINAPI_FAMILY == WINAPI_FAMILY_GAMES))
+#if defined(_WIN32) && !(defined(WINAPI_FAMILY) && ((defined(WINAPI_FAMILY_APP) && WINAPI_FAMILY == WINAPI_FAMILY_APP) || (defined(WINAPI_FAMILY_GAMES) && WINAPI_FAMILY == WINAPI_FAMILY_GAMES)))
HWND hwnd = (HWND)viewport->PlatformHandleRaw;
// SDL hack: Show icon in task bar (#7989)
@@ -1032,22 +1097,29 @@ static void ImGui_ImplSDL3_ShowWindow(ImGuiViewport* viewport)
}
#endif
+#ifdef __APPLE__
+ SDL_SetHint(SDL_HINT_WINDOW_ACTIVATE_WHEN_SHOWN, "1"); // Otherwise new window appear under
+#else
SDL_SetHint(SDL_HINT_WINDOW_ACTIVATE_WHEN_SHOWN, (viewport->Flags & ImGuiViewportFlags_NoFocusOnAppearing) ? "0" : "1");
+#endif
SDL_ShowWindow(vd->Window);
}
static void ImGui_ImplSDL3_UpdateWindow(ImGuiViewport* viewport)
{
ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData;
+ IM_UNUSED(vd);
+#ifndef __APPLE__ // On Mac, SDL3 Parenting appears to prevent viewport from appearing in another monitor
// Update SDL3 parent if it changed _after_ creation.
// This is for advanced apps that are manipulating ParentViewportID manually.
- SDL_Window* new_parent = ImGui_ImplSDL3_GetSDLWindowFromViewportID(viewport->ParentViewportId);
+ SDL_Window* new_parent = ImGui_ImplSDL3_GetSDLWindowFromViewport(viewport->ParentViewport);
if (new_parent != vd->ParentWindow)
{
vd->ParentWindow = new_parent;
SDL_SetWindowParent(vd->Window, vd->ParentWindow);
}
+#endif
}
static ImVec2 ImGui_ImplSDL3_GetWindowPos(ImGuiViewport* viewport)
@@ -1078,6 +1150,14 @@ static void ImGui_ImplSDL3_SetWindowSize(ImGuiViewport* viewport, ImVec2 size)
SDL_SetWindowSize(vd->Window, (int)size.x, (int)size.y);
}
+static ImVec2 ImGui_ImplSDL3_GetWindowFramebufferScale(ImGuiViewport* viewport)
+{
+ ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData;
+ ImVec2 framebuffer_scale;
+ ImGui_ImplSDL3_GetWindowSizeAndFramebufferScale(vd->Window, nullptr, &framebuffer_scale);
+ return framebuffer_scale;
+}
+
static void ImGui_ImplSDL3_SetWindowTitle(ImGuiViewport* viewport, const char* title)
{
ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData;
@@ -1132,7 +1212,7 @@ static int ImGui_ImplSDL3_CreateVkSurface(ImGuiViewport* viewport, ImU64 vk_inst
{
ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData;
(void)vk_allocator;
- bool ret = SDL_Vulkan_CreateSurface(vd->Window, (VkInstance)vk_instance, (VkAllocationCallbacks*)vk_allocator, (VkSurfaceKHR*)out_vk_surface);
+ bool ret = SDL_Vulkan_CreateSurface(vd->Window, (VkInstance)vk_instance, (const VkAllocationCallbacks*)vk_allocator, (VkSurfaceKHR*)out_vk_surface);
return ret ? 0 : 1; // ret ? VK_SUCCESS : VK_NOT_READY
}
@@ -1148,6 +1228,7 @@ static void ImGui_ImplSDL3_InitMultiViewportSupport(SDL_Window* window, void* sd
platform_io.Platform_GetWindowPos = ImGui_ImplSDL3_GetWindowPos;
platform_io.Platform_SetWindowSize = ImGui_ImplSDL3_SetWindowSize;
platform_io.Platform_GetWindowSize = ImGui_ImplSDL3_GetWindowSize;
+ platform_io.Platform_GetWindowFramebufferScale = ImGui_ImplSDL3_GetWindowFramebufferScale;
platform_io.Platform_SetWindowFocus = ImGui_ImplSDL3_SetWindowFocus;
platform_io.Platform_GetWindowFocus = ImGui_ImplSDL3_GetWindowFocus;
platform_io.Platform_GetWindowMinimized = ImGui_ImplSDL3_GetWindowMinimized;
diff --git a/backends/imgui_impl_sdlgpu3.cpp b/backends/imgui_impl_sdlgpu3.cpp
index 4ad480bf9..846c2c319 100644
--- a/backends/imgui_impl_sdlgpu3.cpp
+++ b/backends/imgui_impl_sdlgpu3.cpp
@@ -2,10 +2,10 @@
// This needs to be used along with the SDL3 Platform Backend
// Implemented features:
-// [X] Renderer: User texture binding. Use simply cast a reference to your SDL_GPUTextureSamplerBinding to ImTextureID.
-// [X] Renderer: Large meshes support (64k+ vertices) with 16-bit indices.
-// Missing features:
-// [ ] Renderer: Multi-viewport support (multiple windows).
+// [X] Renderer: User texture binding. Use 'SDL_GPUTexture*' as texture identifier. Read the FAQ about ImTextureID/ImTextureRef! **IMPORTANT** Before 2025/08/08, ImTextureID was a reference to a SDL_GPUTextureSamplerBinding struct.
+// [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset).
+// [X] Renderer: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures).
+// [X] Renderer: Multi-viewport support (multiple windows). Enable with 'io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable'.
// The aim of imgui_impl_sdlgpu3.h/.cpp is to be usable in your engine without any modification.
// IF YOU FEEL YOU NEED TO MAKE ANY CHANGE TO THIS CODE, please share them and your feedback at https://github.com/ocornut/imgui/
@@ -23,6 +23,15 @@
// Calling the function is MANDATORY, otherwise the ImGui will not upload neither the vertex nor the index buffer for the GPU. See imgui_impl_sdlgpu3.cpp for more info.
// CHANGELOG
+// 2025-XX-XX: Platform: Added support for multiple windows via the ImGuiPlatformIO interface.
+// 2025-09-18: Call platform_io.ClearRendererHandlers() on shutdown.
+// 2025-08-20: Added ImGui_ImplSDLGPU3_InitInfo::SwapchainComposition and ImGui_ImplSDLGPU3_InitInfo::PresentMode to configure how secondary viewports are created.
+// 2025-08-08: *BREAKING* Changed ImTextureID type from SDL_GPUTextureSamplerBinding* to SDL_GPUTexture*, which is more natural and easier for user to manage. If you need to change the current sampler, you can access the ImGui_ImplSDLGPU3_RenderState struct. (#8866, #8163, #7998, #7988)
+// 2025-08-08: Expose SamplerDefault and SamplerCurrent in ImGui_ImplSDLGPU3_RenderState. Allow callback to change sampler.
+// 2025-06-25: Mapping transfer buffer for texture update use cycle=true. Fixes artifacts e.g. on Metal backend.
+// 2025-06-11: Added support for ImGuiBackendFlags_RendererHasTextures, for dynamic font atlas. Removed ImGui_ImplSDLGPU3_CreateFontsTexture() and ImGui_ImplSDLGPU3_DestroyFontsTexture().
+// 2025-04-28: Added support for special ImDrawCallback_ResetRenderState callback to reset render state.
+// 2025-03-30: Made ImGui_ImplSDLGPU3_PrepareDrawData() reuse GPU Transfer Buffers which were unusually slow to recreate every frame. Much faster now.
// 2025-03-21: Fixed typo in function name Imgui_ImplSDLGPU3_PrepareDrawData() -> ImGui_ImplSDLGPU3_PrepareDrawData().
// 2025-01-16: Renamed ImGui_ImplSDLGPU3_InitInfo::GpuDevice to Device.
// 2025-01-09: SDL_GPU: Added the SDL_GPU3 backend.
@@ -37,10 +46,12 @@
// Reusable buffers used for rendering 1 current in-flight frame, for ImGui_ImplSDLGPU3_RenderDrawData()
struct ImGui_ImplSDLGPU3_FrameData
{
- SDL_GPUBuffer* VertexBuffer = nullptr;
- SDL_GPUBuffer* IndexBuffer = nullptr;
- uint32_t VertexBufferSize = 0;
- uint32_t IndexBufferSize = 0;
+ SDL_GPUBuffer* VertexBuffer = nullptr;
+ SDL_GPUTransferBuffer* VertexTransferBuffer = nullptr;
+ uint32_t VertexBufferSize = 0;
+ SDL_GPUBuffer* IndexBuffer = nullptr;
+ SDL_GPUTransferBuffer* IndexTransferBuffer = nullptr;
+ uint32_t IndexBufferSize = 0;
};
struct ImGui_ImplSDLGPU3_Data
@@ -48,14 +59,12 @@ struct ImGui_ImplSDLGPU3_Data
ImGui_ImplSDLGPU3_InitInfo InitInfo;
// Graphics pipeline & shaders
- SDL_GPUShader* VertexShader = nullptr;
- SDL_GPUShader* FragmentShader = nullptr;
- SDL_GPUGraphicsPipeline* Pipeline = nullptr;
-
- // Font data
- SDL_GPUSampler* FontSampler = nullptr;
- SDL_GPUTexture* FontTexture = nullptr;
- SDL_GPUTextureSamplerBinding FontBinding = { nullptr, nullptr };
+ SDL_GPUShader* VertexShader = nullptr;
+ SDL_GPUShader* FragmentShader = nullptr;
+ SDL_GPUGraphicsPipeline* Pipeline = nullptr;
+ SDL_GPUSampler* TexSamplerLinear = nullptr;
+ SDL_GPUTransferBuffer* TexTransferBuffer = nullptr;
+ uint32_t TexTransferBufferSize = 0;
// Frame data for main window
ImGui_ImplSDLGPU3_FrameData MainWindowFrameData;
@@ -76,12 +85,13 @@ static ImGui_ImplSDLGPU3_Data* ImGui_ImplSDLGPU3_GetBackendData()
return ImGui::GetCurrentContext() ? (ImGui_ImplSDLGPU3_Data*)ImGui::GetIO().BackendRendererUserData : nullptr;
}
-static void ImGui_ImplSDLGPU3_SetupRenderState(ImDrawData* draw_data, SDL_GPUGraphicsPipeline* pipeline, SDL_GPUCommandBuffer* command_buffer, SDL_GPURenderPass * render_pass, ImGui_ImplSDLGPU3_FrameData* fd, uint32_t fb_width, uint32_t fb_height)
+static void ImGui_ImplSDLGPU3_SetupRenderState(ImDrawData* draw_data, ImGui_ImplSDLGPU3_RenderState* render_state, SDL_GPUGraphicsPipeline* pipeline, SDL_GPUCommandBuffer* command_buffer, SDL_GPURenderPass* render_pass, ImGui_ImplSDLGPU3_FrameData* fd, uint32_t fb_width, uint32_t fb_height)
{
- //ImGui_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData();
+ ImGui_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData();
+ render_state->SamplerCurrent = bd->TexSamplerLinear;
// Bind graphics pipeline
- SDL_BindGPUGraphicsPipeline(render_pass,pipeline);
+ SDL_BindGPUGraphicsPipeline(render_pass, pipeline);
// Bind Vertex And Index Buffers
if (draw_data->TotalVtxCount > 0)
@@ -116,14 +126,15 @@ static void ImGui_ImplSDLGPU3_SetupRenderState(ImDrawData* draw_data, SDL_GPUGra
SDL_PushGPUVertexUniformData(command_buffer, 0, &ubo, sizeof(UBO));
}
-static void CreateOrResizeBuffer(SDL_GPUBuffer** buffer, uint32_t* old_size, uint32_t new_size, SDL_GPUBufferUsageFlags usage)
+static void CreateOrResizeBuffers(SDL_GPUBuffer** buffer, SDL_GPUTransferBuffer** transferbuffer, uint32_t* old_size, uint32_t new_size, SDL_GPUBufferUsageFlags usage)
{
ImGui_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData();
ImGui_ImplSDLGPU3_InitInfo* v = &bd->InitInfo;
- // Even though this is fairly rarely called.
+ // FIXME-OPT: Not optimal, but this is fairly rarely called.
SDL_WaitForGPUIdle(v->Device);
SDL_ReleaseGPUBuffer(v->Device, *buffer);
+ SDL_ReleaseGPUTransferBuffer(v->Device, *transferbuffer);
SDL_GPUBufferCreateInfo buffer_info = {};
buffer_info.usage = usage;
@@ -132,6 +143,12 @@ static void CreateOrResizeBuffer(SDL_GPUBuffer** buffer, uint32_t* old_size, uin
*buffer = SDL_CreateGPUBuffer(v->Device, &buffer_info);
*old_size = new_size;
IM_ASSERT(*buffer != nullptr && "Failed to create GPU Buffer, call SDL_GetError() for more information");
+
+ SDL_GPUTransferBufferCreateInfo transferbuffer_info = {};
+ transferbuffer_info.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD;
+ transferbuffer_info.size = new_size;
+ *transferbuffer = SDL_CreateGPUTransferBuffer(v->Device, &transferbuffer_info);
+ IM_ASSERT(*transferbuffer != nullptr && "Failed to create GPU Transfer Buffer, call SDL_GetError() for more information");
}
// SDL_GPU doesn't allow copy passes to occur while a render or compute pass is bound!
@@ -145,6 +162,13 @@ void ImGui_ImplSDLGPU3_PrepareDrawData(ImDrawData* draw_data, SDL_GPUCommandBuff
if (fb_width <= 0 || fb_height <= 0 || draw_data->TotalVtxCount <= 0)
return;
+ // Catch up with texture updates. Most of the times, the list will have 1 element with an OK status, aka nothing to do.
+ // (This almost always points to ImGui::GetPlatformIO().Textures[] but is part of ImDrawData to allow overriding or disabling texture updates).
+ if (draw_data->Textures != nullptr)
+ for (ImTextureData* tex : *draw_data->Textures)
+ if (tex->Status != ImTextureStatus_OK)
+ ImGui_ImplSDLGPU3_UpdateTexture(tex);
+
ImGui_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData();
ImGui_ImplSDLGPU3_InitInfo* v = &bd->InitInfo;
ImGui_ImplSDLGPU3_FrameData* fd = &bd->MainWindowFrameData;
@@ -152,42 +176,28 @@ void ImGui_ImplSDLGPU3_PrepareDrawData(ImDrawData* draw_data, SDL_GPUCommandBuff
uint32_t vertex_size = draw_data->TotalVtxCount * sizeof(ImDrawVert);
uint32_t index_size = draw_data->TotalIdxCount * sizeof(ImDrawIdx);
if (fd->VertexBuffer == nullptr || fd->VertexBufferSize < vertex_size)
- CreateOrResizeBuffer(&fd->VertexBuffer, &fd->VertexBufferSize, vertex_size, SDL_GPU_BUFFERUSAGE_VERTEX);
+ CreateOrResizeBuffers(&fd->VertexBuffer, &fd->VertexTransferBuffer, &fd->VertexBufferSize, vertex_size, SDL_GPU_BUFFERUSAGE_VERTEX);
if (fd->IndexBuffer == nullptr || fd->IndexBufferSize < index_size)
- CreateOrResizeBuffer(&fd->IndexBuffer, &fd->IndexBufferSize, index_size, SDL_GPU_BUFFERUSAGE_INDEX);
+ CreateOrResizeBuffers(&fd->IndexBuffer, &fd->IndexTransferBuffer, &fd->IndexBufferSize, index_size, SDL_GPU_BUFFERUSAGE_INDEX);
- // FIXME: It feels like more code could be shared there.
- SDL_GPUTransferBufferCreateInfo vertex_transferbuffer_info = {};
- vertex_transferbuffer_info.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD;
- vertex_transferbuffer_info.size = vertex_size;
- SDL_GPUTransferBufferCreateInfo index_transferbuffer_info = {};
- index_transferbuffer_info.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD;
- index_transferbuffer_info.size = index_size;
-
- SDL_GPUTransferBuffer* vertex_transferbuffer = SDL_CreateGPUTransferBuffer(v->Device, &vertex_transferbuffer_info);
- IM_ASSERT(vertex_transferbuffer != nullptr && "Failed to create the vertex transfer buffer, call SDL_GetError() for more information");
- SDL_GPUTransferBuffer* index_transferbuffer = SDL_CreateGPUTransferBuffer(v->Device, &index_transferbuffer_info);
- IM_ASSERT(index_transferbuffer != nullptr && "Failed to create the index transfer buffer, call SDL_GetError() for more information");
-
- ImDrawVert* vtx_dst = (ImDrawVert*)SDL_MapGPUTransferBuffer(v->Device, vertex_transferbuffer, true);
- ImDrawIdx* idx_dst = (ImDrawIdx*)SDL_MapGPUTransferBuffer(v->Device, index_transferbuffer, true);
- for (int n = 0; n < draw_data->CmdListsCount; n++)
+ ImDrawVert* vtx_dst = (ImDrawVert*)SDL_MapGPUTransferBuffer(v->Device, fd->VertexTransferBuffer, true);
+ ImDrawIdx* idx_dst = (ImDrawIdx*)SDL_MapGPUTransferBuffer(v->Device, fd->IndexTransferBuffer, true);
+ for (const ImDrawList* draw_list : draw_data->CmdLists)
{
- const ImDrawList* draw_list = draw_data->CmdLists[n];
memcpy(vtx_dst, draw_list->VtxBuffer.Data, draw_list->VtxBuffer.Size * sizeof(ImDrawVert));
memcpy(idx_dst, draw_list->IdxBuffer.Data, draw_list->IdxBuffer.Size * sizeof(ImDrawIdx));
vtx_dst += draw_list->VtxBuffer.Size;
idx_dst += draw_list->IdxBuffer.Size;
}
- SDL_UnmapGPUTransferBuffer(v->Device, vertex_transferbuffer);
- SDL_UnmapGPUTransferBuffer(v->Device, index_transferbuffer);
+ SDL_UnmapGPUTransferBuffer(v->Device, fd->VertexTransferBuffer);
+ SDL_UnmapGPUTransferBuffer(v->Device, fd->IndexTransferBuffer);
SDL_GPUTransferBufferLocation vertex_buffer_location = {};
vertex_buffer_location.offset = 0;
- vertex_buffer_location.transfer_buffer = vertex_transferbuffer;
+ vertex_buffer_location.transfer_buffer = fd->VertexTransferBuffer;
SDL_GPUTransferBufferLocation index_buffer_location = {};
index_buffer_location.offset = 0;
- index_buffer_location.transfer_buffer = index_transferbuffer;
+ index_buffer_location.transfer_buffer = fd->IndexTransferBuffer;
SDL_GPUBufferRegion vertex_buffer_region = {};
vertex_buffer_region.buffer = fd->VertexBuffer;
@@ -203,8 +213,6 @@ void ImGui_ImplSDLGPU3_PrepareDrawData(ImDrawData* draw_data, SDL_GPUCommandBuff
SDL_UploadToGPUBuffer(copy_pass, &vertex_buffer_location, &vertex_buffer_region, true);
SDL_UploadToGPUBuffer(copy_pass, &index_buffer_location, &index_buffer_region, true);
SDL_EndGPUCopyPass(copy_pass);
- SDL_ReleaseGPUTransferBuffer(v->Device, index_transferbuffer);
- SDL_ReleaseGPUTransferBuffer(v->Device, vertex_transferbuffer);
}
void ImGui_ImplSDLGPU3_RenderDrawData(ImDrawData* draw_data, SDL_GPUCommandBuffer* command_buffer, SDL_GPURenderPass* render_pass, SDL_GPUGraphicsPipeline* pipeline)
@@ -221,25 +229,36 @@ void ImGui_ImplSDLGPU3_RenderDrawData(ImDrawData* draw_data, SDL_GPUCommandBuffe
if (pipeline == nullptr)
pipeline = bd->Pipeline;
- ImGui_ImplSDLGPU3_SetupRenderState(draw_data, pipeline, command_buffer, render_pass, fd, fb_width, fb_height);
-
// Will project scissor/clipping rectangles into framebuffer space
ImVec2 clip_off = draw_data->DisplayPos; // (0,0) unless using multi-viewports
ImVec2 clip_scale = draw_data->FramebufferScale; // (1,1) unless using retina display which are often (2,2)
+ // Setup render state structure (for callbacks and custom texture bindings)
+ ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
+ ImGui_ImplSDLGPU3_RenderState render_state;
+ render_state.Device = bd->InitInfo.Device;
+ render_state.SamplerDefault = render_state.SamplerCurrent = bd->TexSamplerLinear;
+ platform_io.Renderer_RenderState = &render_state;
+
+ ImGui_ImplSDLGPU3_SetupRenderState(draw_data, &render_state, pipeline, command_buffer, render_pass, fd, fb_width, fb_height);
+
// Render command lists
// (Because we merged all buffers into a single one, we maintain our own offset into them)
int global_vtx_offset = 0;
int global_idx_offset = 0;
- for (int n = 0; n < draw_data->CmdListsCount; n++)
+ for (const ImDrawList* draw_list : draw_data->CmdLists)
{
- const ImDrawList* draw_list = draw_data->CmdLists[n];
for (int cmd_i = 0; cmd_i < draw_list->CmdBuffer.Size; cmd_i++)
{
const ImDrawCmd* pcmd = &draw_list->CmdBuffer[cmd_i];
if (pcmd->UserCallback != nullptr)
{
- pcmd->UserCallback(draw_list, pcmd);
+ // User callback, registered via ImDrawList::AddCallback()
+ // (ImDrawCallback_ResetRenderState is a special callback value used by the user to request the renderer to reset render state.)
+ if (pcmd->UserCallback == ImDrawCallback_ResetRenderState)
+ ImGui_ImplSDLGPU3_SetupRenderState(draw_data, &render_state, pipeline, command_buffer, render_pass, fd, fb_width, fb_height);
+ else
+ pcmd->UserCallback(draw_list, pcmd);
}
else
{
@@ -264,9 +283,14 @@ void ImGui_ImplSDLGPU3_RenderDrawData(ImDrawData* draw_data, SDL_GPUCommandBuffe
SDL_SetGPUScissor(render_pass,&scissor_rect);
// Bind DescriptorSet with font or user texture
- SDL_BindGPUFragmentSamplers(render_pass, 0, (SDL_GPUTextureSamplerBinding*)pcmd->GetTexID(), 1);
+ SDL_GPUTextureSamplerBinding texture_sampler_binding;
+ texture_sampler_binding.texture = (SDL_GPUTexture*)(intptr_t)pcmd->GetTexID();
+ texture_sampler_binding.sampler = render_state.SamplerCurrent;
+ SDL_BindGPUFragmentSamplers(render_pass, 0, &texture_sampler_binding, 1);
// Draw
+ // **IF YOU GET A CRASH HERE** In 1.92.2 on 2025/08/08 we have changed ImTextureID to store 'SDL_GPUTexture*' instead of storing 'SDL_GPUTextureSamplerBinding'.
+ // Any code loading custom texture using this backend needs to be updated.
SDL_DrawGPUIndexedPrimitives(render_pass, pcmd->ElemCount, 1, pcmd->IdxOffset + global_idx_offset, pcmd->VtxOffset + global_vtx_offset, 0);
}
}
@@ -282,91 +306,107 @@ void ImGui_ImplSDLGPU3_RenderDrawData(ImDrawData* draw_data, SDL_GPUCommandBuffe
SDL_SetGPUScissor(render_pass, &scissor_rect);
}
-void ImGui_ImplSDLGPU3_CreateFontsTexture()
+static void ImGui_ImplSDLGPU3_DestroyTexture(ImTextureData* tex)
+{
+ ImGui_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData();
+ if (SDL_GPUTexture* raw_tex = (SDL_GPUTexture*)(intptr_t)tex->GetTexID())
+ SDL_ReleaseGPUTexture(bd->InitInfo.Device, raw_tex);
+
+ // Clear identifiers and mark as destroyed (in order to allow e.g. calling InvalidateDeviceObjects while running)
+ tex->SetTexID(ImTextureID_Invalid);
+ tex->SetStatus(ImTextureStatus_Destroyed);
+}
+
+void ImGui_ImplSDLGPU3_UpdateTexture(ImTextureData* tex)
{
- ImGuiIO& io = ImGui::GetIO();
ImGui_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData();
ImGui_ImplSDLGPU3_InitInfo* v = &bd->InitInfo;
- // Destroy existing texture (if any)
- if (bd->FontTexture)
+ if (tex->Status == ImTextureStatus_WantCreate)
{
- SDL_WaitForGPUIdle(v->Device);
- ImGui_ImplSDLGPU3_DestroyFontsTexture();
- }
+ // Create and upload new texture to graphics system
+ //IMGUI_DEBUG_LOG("UpdateTexture #%03d: WantCreate %dx%d\n", tex->UniqueID, tex->Width, tex->Height);
+ IM_ASSERT(tex->TexID == ImTextureID_Invalid && tex->BackendUserData == nullptr);
+ IM_ASSERT(tex->Format == ImTextureFormat_RGBA32);
- unsigned char* pixels;
- int width, height;
- io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
- uint32_t upload_size = width * height * 4 * sizeof(char);
-
- // Create the Image:
- {
+ // Create texture
SDL_GPUTextureCreateInfo texture_info = {};
- texture_info.type = SDL_GPU_TEXTURETYPE_2D;
+ texture_info.type = SDL_GPU_TEXTURETYPE_2D;
texture_info.format = SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM;
texture_info.usage = SDL_GPU_TEXTUREUSAGE_SAMPLER;
- texture_info.width = width;
- texture_info.height = height;
+ texture_info.width = tex->Width;
+ texture_info.height = tex->Height;
texture_info.layer_count_or_depth = 1;
texture_info.num_levels = 1;
texture_info.sample_count = SDL_GPU_SAMPLECOUNT_1;
- bd->FontTexture = SDL_CreateGPUTexture(v->Device, &texture_info);
- IM_ASSERT(bd->FontTexture && "Failed to create font texture, call SDL_GetError() for more info");
+ SDL_GPUTexture* raw_tex = SDL_CreateGPUTexture(v->Device, &texture_info);
+ IM_ASSERT(raw_tex != nullptr && "Failed to create texture, call SDL_GetError() for more info");
+
+ // Store identifiers
+ tex->SetTexID((ImTextureID)(intptr_t)raw_tex);
}
- // Assign the texture to the TextureSamplerBinding
- bd->FontBinding.texture = bd->FontTexture;
-
- // Create all the upload structures and upload:
+ if (tex->Status == ImTextureStatus_WantCreate || tex->Status == ImTextureStatus_WantUpdates)
{
- SDL_GPUTransferBufferCreateInfo transferbuffer_info = {};
- transferbuffer_info.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD;
- transferbuffer_info.size = upload_size;
+ SDL_GPUTexture* raw_tex = (SDL_GPUTexture*)(intptr_t)tex->GetTexID();
+ IM_ASSERT(tex->Format == ImTextureFormat_RGBA32);
- SDL_GPUTransferBuffer* transferbuffer = SDL_CreateGPUTransferBuffer(v->Device, &transferbuffer_info);
- IM_ASSERT(transferbuffer != nullptr && "Failed to create font transfer buffer, call SDL_GetError() for more information");
+ // Update full texture or selected blocks. We only ever write to textures regions which have never been used before!
+ // This backend choose to use tex->UpdateRect but you can use tex->Updates[] to upload individual regions.
+ // We could use the smaller rect on _WantCreate but using the full rect allows us to clear the texture.
+ const int upload_x = (tex->Status == ImTextureStatus_WantCreate) ? 0 : tex->UpdateRect.x;
+ const int upload_y = (tex->Status == ImTextureStatus_WantCreate) ? 0 : tex->UpdateRect.y;
+ const int upload_w = (tex->Status == ImTextureStatus_WantCreate) ? tex->Width : tex->UpdateRect.w;
+ const int upload_h = (tex->Status == ImTextureStatus_WantCreate) ? tex->Height : tex->UpdateRect.h;
+ uint32_t upload_pitch = upload_w * tex->BytesPerPixel;
+ uint32_t upload_size = upload_w * upload_h * tex->BytesPerPixel;
- void* texture_ptr = SDL_MapGPUTransferBuffer(v->Device, transferbuffer, false);
- memcpy(texture_ptr, pixels, upload_size);
- SDL_UnmapGPUTransferBuffer(v->Device, transferbuffer);
+ // Create transfer buffer
+ if (bd->TexTransferBufferSize < upload_size)
+ {
+ SDL_ReleaseGPUTransferBuffer(v->Device, bd->TexTransferBuffer);
+ SDL_GPUTransferBufferCreateInfo transferbuffer_info = {};
+ transferbuffer_info.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD;
+ transferbuffer_info.size = upload_size + 1024;
+ bd->TexTransferBufferSize = upload_size + 1024;
+ bd->TexTransferBuffer = SDL_CreateGPUTransferBuffer(v->Device, &transferbuffer_info);
+ IM_ASSERT(bd->TexTransferBuffer != nullptr && "Failed to create transfer buffer, call SDL_GetError() for more information");
+ }
+
+ // Copy to transfer buffer
+ {
+ void* texture_ptr = SDL_MapGPUTransferBuffer(v->Device, bd->TexTransferBuffer, true);
+ for (int y = 0; y < upload_h; y++)
+ memcpy((void*)((uintptr_t)texture_ptr + y * upload_pitch), tex->GetPixelsAt(upload_x, upload_y + y), upload_pitch);
+ SDL_UnmapGPUTransferBuffer(v->Device, bd->TexTransferBuffer);
+ }
SDL_GPUTextureTransferInfo transfer_info = {};
transfer_info.offset = 0;
- transfer_info.transfer_buffer = transferbuffer;
+ transfer_info.transfer_buffer = bd->TexTransferBuffer;
SDL_GPUTextureRegion texture_region = {};
- texture_region.texture = bd->FontTexture;
- texture_region.w = width;
- texture_region.h = height;
+ texture_region.texture = raw_tex;
+ texture_region.x = (Uint32)upload_x;
+ texture_region.y = (Uint32)upload_y;
+ texture_region.w = (Uint32)upload_w;
+ texture_region.h = (Uint32)upload_h;
texture_region.d = 1;
- SDL_GPUCommandBuffer* cmd = SDL_AcquireGPUCommandBuffer(v->Device);
- SDL_GPUCopyPass* copy_pass = SDL_BeginGPUCopyPass(cmd);
- SDL_UploadToGPUTexture(copy_pass, &transfer_info, &texture_region, false);
- SDL_EndGPUCopyPass(copy_pass);
- SDL_SubmitGPUCommandBuffer(cmd);
- SDL_ReleaseGPUTransferBuffer(v->Device, transferbuffer);
- }
+ // Upload
+ {
+ SDL_GPUCommandBuffer* cmd = SDL_AcquireGPUCommandBuffer(v->Device);
+ SDL_GPUCopyPass* copy_pass = SDL_BeginGPUCopyPass(cmd);
+ SDL_UploadToGPUTexture(copy_pass, &transfer_info, &texture_region, false);
+ SDL_EndGPUCopyPass(copy_pass);
+ SDL_SubmitGPUCommandBuffer(cmd);
+ }
- // Store our identifier
- io.Fonts->SetTexID((ImTextureID)&bd->FontBinding);
-}
-
-// You probably never need to call this, as it is called by ImGui_ImplSDLGPU3_CreateFontsTexture() and ImGui_ImplSDLGPU3_Shutdown().
-void ImGui_ImplSDLGPU3_DestroyFontsTexture()
-{
- ImGuiIO& io = ImGui::GetIO();
- ImGui_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData();
- ImGui_ImplSDLGPU3_InitInfo* v = &bd->InitInfo;
- if (bd->FontTexture)
- {
- SDL_ReleaseGPUTexture(v->Device, bd->FontTexture);
- bd->FontBinding.texture = nullptr;
- bd->FontTexture = nullptr;
+ tex->SetStatus(ImTextureStatus_OK);
}
- io.Fonts->SetTexID(0);
+ if (tex->Status == ImTextureStatus_WantDestroy && tex->UnusedFrames > 0)
+ ImGui_ImplSDLGPU3_DestroyTexture(tex);
}
static void ImGui_ImplSDLGPU3_CreateShaders()
@@ -518,7 +558,9 @@ void ImGui_ImplSDLGPU3_CreateDeviceObjects()
ImGui_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData();
ImGui_ImplSDLGPU3_InitInfo* v = &bd->InitInfo;
- if (!bd->FontSampler)
+ ImGui_ImplSDLGPU3_DestroyDeviceObjects();
+
+ if (bd->TexSamplerLinear == nullptr)
{
// Bilinear sampling is required by default. Set 'io.Fonts->Flags |= ImFontAtlasFlags_NoBakedLines' or 'style.AntiAliasedLinesUseTex = false' to allow point/nearest sampling.
SDL_GPUSamplerCreateInfo sampler_info = {};
@@ -535,13 +577,11 @@ void ImGui_ImplSDLGPU3_CreateDeviceObjects()
sampler_info.max_anisotropy = 1.0f;
sampler_info.enable_compare = false;
- bd->FontSampler = SDL_CreateGPUSampler(v->Device, &sampler_info);
- bd->FontBinding.sampler = bd->FontSampler;
- IM_ASSERT(bd->FontSampler != nullptr && "Failed to create font sampler, call SDL_GetError() for more information");
+ bd->TexSamplerLinear = SDL_CreateGPUSampler(v->Device, &sampler_info);
+ IM_ASSERT(bd->TexSamplerLinear != nullptr && "Failed to create sampler, call SDL_GetError() for more information");
}
ImGui_ImplSDLGPU3_CreateGraphicsPipeline();
- ImGui_ImplSDLGPU3_CreateFontsTexture();
}
void ImGui_ImplSDLGPU3_DestroyFrameData()
@@ -549,12 +589,14 @@ void ImGui_ImplSDLGPU3_DestroyFrameData()
ImGui_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData();
ImGui_ImplSDLGPU3_InitInfo* v = &bd->InitInfo;
- SDL_ReleaseGPUBuffer(v->Device, bd->MainWindowFrameData.VertexBuffer);
- SDL_ReleaseGPUBuffer(v->Device, bd->MainWindowFrameData.IndexBuffer);
- bd->MainWindowFrameData.VertexBuffer = nullptr;
- bd->MainWindowFrameData.IndexBuffer = nullptr;
- bd->MainWindowFrameData.VertexBufferSize = 0;
- bd->MainWindowFrameData.IndexBufferSize = 0;
+ ImGui_ImplSDLGPU3_FrameData* fd = &bd->MainWindowFrameData;
+ SDL_ReleaseGPUBuffer(v->Device, fd->VertexBuffer);
+ SDL_ReleaseGPUBuffer(v->Device, fd->IndexBuffer);
+ SDL_ReleaseGPUTransferBuffer(v->Device, fd->VertexTransferBuffer);
+ SDL_ReleaseGPUTransferBuffer(v->Device, fd->IndexTransferBuffer);
+ fd->VertexBuffer = fd->IndexBuffer = nullptr;
+ fd->VertexTransferBuffer = fd->IndexTransferBuffer = nullptr;
+ fd->VertexBufferSize = fd->IndexBufferSize = 0;
}
void ImGui_ImplSDLGPU3_DestroyDeviceObjects()
@@ -563,14 +605,21 @@ void ImGui_ImplSDLGPU3_DestroyDeviceObjects()
ImGui_ImplSDLGPU3_InitInfo* v = &bd->InitInfo;
ImGui_ImplSDLGPU3_DestroyFrameData();
- ImGui_ImplSDLGPU3_DestroyFontsTexture();
- if (bd->VertexShader) { SDL_ReleaseGPUShader(v->Device, bd->VertexShader); bd->VertexShader = nullptr;}
- if (bd->FragmentShader) { SDL_ReleaseGPUShader(v->Device, bd->FragmentShader); bd->FragmentShader = nullptr;}
- if (bd->FontSampler) { SDL_ReleaseGPUSampler(v->Device, bd->FontSampler); bd->FontSampler = nullptr;}
- if (bd->Pipeline) { SDL_ReleaseGPUGraphicsPipeline(v->Device, bd->Pipeline); bd->Pipeline = nullptr;}
+ // Destroy all textures
+ for (ImTextureData* tex : ImGui::GetPlatformIO().Textures)
+ if (tex->RefCount == 1)
+ ImGui_ImplSDLGPU3_DestroyTexture(tex);
+ if (bd->TexTransferBuffer) { SDL_ReleaseGPUTransferBuffer(v->Device, bd->TexTransferBuffer); bd->TexTransferBuffer = nullptr; }
+ if (bd->VertexShader) { SDL_ReleaseGPUShader(v->Device, bd->VertexShader); bd->VertexShader = nullptr; }
+ if (bd->FragmentShader) { SDL_ReleaseGPUShader(v->Device, bd->FragmentShader); bd->FragmentShader = nullptr; }
+ if (bd->TexSamplerLinear) { SDL_ReleaseGPUSampler(v->Device, bd->TexSamplerLinear); bd->TexSamplerLinear = nullptr; }
+ if (bd->Pipeline) { SDL_ReleaseGPUGraphicsPipeline(v->Device, bd->Pipeline); bd->Pipeline = nullptr; }
}
+static void ImGui_ImplSDLGPU3_InitMultiViewportSupport();
+static void ImGui_ImplSDLGPU3_ShutdownMultiViewportSupport();
+
bool ImGui_ImplSDLGPU3_Init(ImGui_ImplSDLGPU3_InitInfo* info)
{
ImGuiIO& io = ImGui::GetIO();
@@ -582,13 +631,15 @@ bool ImGui_ImplSDLGPU3_Init(ImGui_ImplSDLGPU3_InitInfo* info)
io.BackendRendererUserData = (void*)bd;
io.BackendRendererName = "imgui_impl_sdlgpu3";
io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes.
+ io.BackendFlags |= ImGuiBackendFlags_RendererHasTextures; // We can honor ImGuiPlatformIO::Textures[] requests during render.
+ io.BackendFlags |= ImGuiBackendFlags_RendererHasViewports; // We can create multi-viewports on the Renderer side (optional)
IM_ASSERT(info->Device != nullptr);
IM_ASSERT(info->ColorTargetFormat != SDL_GPU_TEXTUREFORMAT_INVALID);
bd->InitInfo = *info;
- ImGui_ImplSDLGPU3_CreateDeviceObjects();
+ ImGui_ImplSDLGPU3_InitMultiViewportSupport();
return true;
}
@@ -598,11 +649,15 @@ void ImGui_ImplSDLGPU3_Shutdown()
ImGui_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData();
IM_ASSERT(bd != nullptr && "No renderer backend to shutdown, or already shutdown?");
ImGuiIO& io = ImGui::GetIO();
+ ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
+ ImGui_ImplSDLGPU3_ShutdownMultiViewportSupport();
ImGui_ImplSDLGPU3_DestroyDeviceObjects();
+
io.BackendRendererName = nullptr;
io.BackendRendererUserData = nullptr;
- io.BackendFlags &= ~ImGuiBackendFlags_RendererHasVtxOffset;
+ io.BackendFlags &= ~(ImGuiBackendFlags_RendererHasVtxOffset | ImGuiBackendFlags_RendererHasTextures | ImGuiBackendFlags_RendererHasViewports);
+ platform_io.ClearRendererHandlers();
IM_DELETE(bd);
}
@@ -611,8 +666,80 @@ void ImGui_ImplSDLGPU3_NewFrame()
ImGui_ImplSDLGPU3_Data* bd = ImGui_ImplSDLGPU3_GetBackendData();
IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplSDLGPU3_Init()?");
- if (!bd->FontTexture)
- ImGui_ImplSDLGPU3_CreateFontsTexture();
+ if (!bd->TexSamplerLinear)
+ ImGui_ImplSDLGPU3_CreateDeviceObjects();
}
+//--------------------------------------------------------------------------------------------------------
+// MULTI-VIEWPORT / PLATFORM INTERFACE SUPPORT
+// This is an _advanced_ and _optional_ feature, allowing the backend to create and handle multiple viewports simultaneously.
+// If you are new to dear imgui or creating a new binding for dear imgui, it is recommended that you completely ignore this section first..
+//--------------------------------------------------------------------------------------------------------
+
+static void ImGui_ImplSDLGPU3_CreateWindow(ImGuiViewport* viewport)
+{
+ ImGui_ImplSDLGPU3_Data* data = ImGui_ImplSDLGPU3_GetBackendData();
+ SDL_Window* window = SDL_GetWindowFromID((SDL_WindowID)(intptr_t)viewport->PlatformHandle);
+ SDL_ClaimWindowForGPUDevice(data->InitInfo.Device, window);
+ SDL_SetGPUSwapchainParameters(data->InitInfo.Device, window, data->InitInfo.SwapchainComposition, data->InitInfo.PresentMode);
+ viewport->RendererUserData = (void*)1;
+}
+
+static void ImGui_ImplSDLGPU3_RenderWindow(ImGuiViewport* viewport, void*)
+{
+ ImGui_ImplSDLGPU3_Data* data = ImGui_ImplSDLGPU3_GetBackendData();
+ SDL_Window* window = SDL_GetWindowFromID((SDL_WindowID)(intptr_t)viewport->PlatformHandle);
+
+ ImDrawData* draw_data = viewport->DrawData;
+
+ SDL_GPUCommandBuffer* command_buffer = SDL_AcquireGPUCommandBuffer(data->InitInfo.Device);
+
+ SDL_GPUTexture* swapchain_texture;
+ SDL_AcquireGPUSwapchainTexture(command_buffer, window, &swapchain_texture, nullptr, nullptr);
+
+ if (swapchain_texture != nullptr)
+ {
+ ImGui_ImplSDLGPU3_PrepareDrawData(draw_data, command_buffer); // FIXME-OPT: Not optimal, may this be done earlier?
+ SDL_GPUColorTargetInfo target_info = {};
+ target_info.texture = swapchain_texture;
+ target_info.clear_color = SDL_FColor{ 0.0f,0.0f,0.0f,1.0f };
+ target_info.load_op = SDL_GPU_LOADOP_CLEAR;
+ target_info.store_op = SDL_GPU_STOREOP_STORE;
+ target_info.mip_level = 0;
+ target_info.layer_or_depth_plane = 0;
+ target_info.cycle = false;
+ SDL_GPURenderPass* render_pass = SDL_BeginGPURenderPass(command_buffer, &target_info, 1, nullptr);
+ ImGui_ImplSDLGPU3_RenderDrawData(draw_data, command_buffer, render_pass);
+ SDL_EndGPURenderPass(render_pass);
+ }
+
+ SDL_SubmitGPUCommandBuffer(command_buffer);
+}
+
+static void ImGui_ImplSDLGPU3_DestroyWindow(ImGuiViewport* viewport)
+{
+ ImGui_ImplSDLGPU3_Data* data = ImGui_ImplSDLGPU3_GetBackendData();
+ if (viewport->RendererUserData)
+ {
+ SDL_Window* window = SDL_GetWindowFromID((SDL_WindowID)(intptr_t)viewport->PlatformHandle);
+ SDL_ReleaseWindowFromGPUDevice(data->InitInfo.Device, window);
+ }
+ viewport->RendererUserData = nullptr;
+}
+
+static void ImGui_ImplSDLGPU3_InitMultiViewportSupport()
+{
+ ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
+ platform_io.Renderer_RenderWindow = ImGui_ImplSDLGPU3_RenderWindow;
+ platform_io.Renderer_CreateWindow = ImGui_ImplSDLGPU3_CreateWindow;
+ platform_io.Renderer_DestroyWindow = ImGui_ImplSDLGPU3_DestroyWindow;
+}
+
+static void ImGui_ImplSDLGPU3_ShutdownMultiViewportSupport()
+{
+ ImGui::DestroyPlatformWindows();
+}
+
+//-----------------------------------------------------------------------------
+
#endif // #ifndef IMGUI_DISABLE
diff --git a/backends/imgui_impl_sdlgpu3.h b/backends/imgui_impl_sdlgpu3.h
index 63691dd65..6789da8f1 100644
--- a/backends/imgui_impl_sdlgpu3.h
+++ b/backends/imgui_impl_sdlgpu3.h
@@ -2,10 +2,10 @@
// This needs to be used along with the SDL3 Platform Backend
// Implemented features:
-// [X] Renderer: User texture binding. Use simply cast a reference to your SDL_GPUTextureSamplerBinding to ImTextureID.
-// [X] Renderer: Large meshes support (64k+ vertices) with 16-bit indices.
-// Missing features:
-// [ ] Renderer: Multi-viewport support (multiple windows).
+// [X] Renderer: User texture binding. Use 'SDL_GPUTexture*' as texture identifier. Read the FAQ about ImTextureID/ImTextureRef! **IMPORTANT** Before 2025/08/08, ImTextureID was a reference to a SDL_GPUTextureSamplerBinding struct.
+// [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset).
+// [X] Renderer: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures).
+// [X] Renderer: Multi-viewport support (multiple windows). Enable with 'io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable'.
// The aim of imgui_impl_sdlgpu3.h/.cpp is to be usable in your engine without any modification.
// IF YOU FEEL YOU NEED TO MAKE ANY CHANGE TO THIS CODE, please share them and your feedback at https://github.com/ocornut/imgui/
@@ -28,12 +28,14 @@
#include
// Initialization data, for ImGui_ImplSDLGPU_Init()
-// - Remember to set ColorTargetFormat to the correct format. If you're rendering to the swapchain, call SDL_GetGPUSwapchainTextureFormat to query the right value
+// - Remember to set ColorTargetFormat to the correct format. If you're rendering to the swapchain, call SDL_GetGPUSwapchainTextureFormat() to query the right value
struct ImGui_ImplSDLGPU3_InitInfo
{
- SDL_GPUDevice* Device = nullptr;
- SDL_GPUTextureFormat ColorTargetFormat = SDL_GPU_TEXTUREFORMAT_INVALID;
- SDL_GPUSampleCount MSAASamples = SDL_GPU_SAMPLECOUNT_1;
+ SDL_GPUDevice* Device = nullptr;
+ SDL_GPUTextureFormat ColorTargetFormat = SDL_GPU_TEXTUREFORMAT_INVALID;
+ SDL_GPUSampleCount MSAASamples = SDL_GPU_SAMPLECOUNT_1;
+ SDL_GPUSwapchainComposition SwapchainComposition = SDL_GPU_SWAPCHAINCOMPOSITION_SDR; // Only used in multi-viewports mode.
+ SDL_GPUPresentMode PresentMode = SDL_GPU_PRESENTMODE_VSYNC; // Only used in multi-viewports mode.
};
// Follow "Getting Started" link and check examples/ folder to learn about using backends!
@@ -43,9 +45,21 @@ IMGUI_IMPL_API void ImGui_ImplSDLGPU3_NewFrame();
IMGUI_IMPL_API void ImGui_ImplSDLGPU3_PrepareDrawData(ImDrawData* draw_data, SDL_GPUCommandBuffer* command_buffer);
IMGUI_IMPL_API void ImGui_ImplSDLGPU3_RenderDrawData(ImDrawData* draw_data, SDL_GPUCommandBuffer* command_buffer, SDL_GPURenderPass* render_pass, SDL_GPUGraphicsPipeline* pipeline = nullptr);
+// Use if you want to reset your rendering device without losing Dear ImGui state.
IMGUI_IMPL_API void ImGui_ImplSDLGPU3_CreateDeviceObjects();
IMGUI_IMPL_API void ImGui_ImplSDLGPU3_DestroyDeviceObjects();
-IMGUI_IMPL_API void ImGui_ImplSDLGPU3_CreateFontsTexture();
-IMGUI_IMPL_API void ImGui_ImplSDLGPU3_DestroyFontsTexture();
+
+// (Advanced) Use e.g. if you need to precisely control the timing of texture updates (e.g. for staged rendering), by setting ImDrawData::Textures = NULL to handle this manually.
+IMGUI_IMPL_API void ImGui_ImplSDLGPU3_UpdateTexture(ImTextureData* tex);
+
+// [BETA] Selected render state data shared with callbacks.
+// This is temporarily stored in GetPlatformIO().Renderer_RenderState during the ImGui_ImplSDLGPU3_RenderDrawData() call.
+// (Please open an issue if you feel you need access to more data)
+struct ImGui_ImplSDLGPU3_RenderState
+{
+ SDL_GPUDevice* Device;
+ SDL_GPUSampler* SamplerDefault; // Default sampler (bilinear filtering)
+ SDL_GPUSampler* SamplerCurrent; // Current sampler (may be changed by callback)
+};
#endif // #ifndef IMGUI_DISABLE
diff --git a/backends/imgui_impl_sdlrenderer2.cpp b/backends/imgui_impl_sdlrenderer2.cpp
index 05b994128..c923058e9 100644
--- a/backends/imgui_impl_sdlrenderer2.cpp
+++ b/backends/imgui_impl_sdlrenderer2.cpp
@@ -10,8 +10,9 @@
// and it might be difficult to step out of those boundaries.
// Implemented features:
-// [X] Renderer: User texture binding. Use 'SDL_Texture*' as ImTextureID. Read the FAQ about ImTextureID!
+// [X] Renderer: User texture binding. Use 'SDL_Texture*' as texture identifier. Read the FAQ about ImTextureID/ImTextureRef!
// [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset).
+// [X] Renderer: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures).
// [X] Renderer: Expose selected render state for draw callbacks to use. Access in '(ImGui_ImplXXXX_RenderState*)GetPlatformIO().Renderer_RenderState'.
// Missing features:
// [ ] Renderer: Multi-viewport support (multiple windows).
@@ -25,6 +26,8 @@
// - Introduction, links and more at the top of imgui.cpp
// CHANGELOG
+// 2025-09-18: Call platform_io.ClearRendererHandlers() on shutdown.
+// 2025-06-11: Added support for ImGuiBackendFlags_RendererHasTextures, for dynamic font atlas. Removed ImGui_ImplSDLRenderer2_CreateFontsTexture() and ImGui_ImplSDLRenderer2_DestroyFontsTexture().
// 2025-01-18: Use endian-dependent RGBA32 texture format, to match SDL_Color.
// 2024-10-09: Expose selected render state in ImGui_ImplSDLRenderer2_RenderState, which you can access in 'void* platform_io.Renderer_RenderState' during draw callbacks.
// 2024-05-14: *BREAKING CHANGE* ImGui_ImplSDLRenderer3_RenderDrawData() requires SDL_Renderer* passed as parameter.
@@ -44,6 +47,8 @@
#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wsign-conversion" // warning: implicit conversion changes signedness
+#elif defined(__GNUC__)
+#pragma GCC diagnostic ignored "-Wfloat-equal" // warning: comparing floating-point with '==' or '!=' is unsafe
#endif
// SDL
@@ -56,7 +61,7 @@
struct ImGui_ImplSDLRenderer2_Data
{
SDL_Renderer* Renderer; // Main viewport's renderer
- SDL_Texture* FontTexture;
+
ImGui_ImplSDLRenderer2_Data() { memset((void*)this, 0, sizeof(*this)); }
};
@@ -80,6 +85,7 @@ bool ImGui_ImplSDLRenderer2_Init(SDL_Renderer* renderer)
io.BackendRendererUserData = (void*)bd;
io.BackendRendererName = "imgui_impl_sdlrenderer2";
io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes.
+ io.BackendFlags |= ImGuiBackendFlags_RendererHasTextures; // We can honor ImGuiPlatformIO::Textures[] requests during render.
bd->Renderer = renderer;
@@ -91,12 +97,14 @@ void ImGui_ImplSDLRenderer2_Shutdown()
ImGui_ImplSDLRenderer2_Data* bd = ImGui_ImplSDLRenderer2_GetBackendData();
IM_ASSERT(bd != nullptr && "No renderer backend to shutdown, or already shutdown?");
ImGuiIO& io = ImGui::GetIO();
+ ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
ImGui_ImplSDLRenderer2_DestroyDeviceObjects();
io.BackendRendererName = nullptr;
io.BackendRendererUserData = nullptr;
- io.BackendFlags &= ~ImGuiBackendFlags_RendererHasVtxOffset;
+ io.BackendFlags &= ~(ImGuiBackendFlags_RendererHasVtxOffset | ImGuiBackendFlags_RendererHasTextures);
+ platform_io.ClearRendererHandlers();
IM_DELETE(bd);
}
@@ -112,9 +120,7 @@ void ImGui_ImplSDLRenderer2_NewFrame()
{
ImGui_ImplSDLRenderer2_Data* bd = ImGui_ImplSDLRenderer2_GetBackendData();
IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplSDLRenderer2_Init()?");
-
- if (!bd->FontTexture)
- ImGui_ImplSDLRenderer2_CreateDeviceObjects();
+ IM_UNUSED(bd);
}
void ImGui_ImplSDLRenderer2_RenderDrawData(ImDrawData* draw_data, SDL_Renderer* renderer)
@@ -135,6 +141,13 @@ void ImGui_ImplSDLRenderer2_RenderDrawData(ImDrawData* draw_data, SDL_Renderer*
if (fb_width == 0 || fb_height == 0)
return;
+ // Catch up with texture updates. Most of the times, the list will have 1 element with an OK status, aka nothing to do.
+ // (This almost always points to ImGui::GetPlatformIO().Textures[] but is part of ImDrawData to allow overriding or disabling texture updates).
+ if (draw_data->Textures != nullptr)
+ for (ImTextureData* tex : *draw_data->Textures)
+ if (tex->Status != ImTextureStatus_OK)
+ ImGui_ImplSDLRenderer2_UpdateTexture(tex);
+
// Backup SDL_Renderer state that will be modified to restore it afterwards
struct BackupSDLRendererState
{
@@ -161,9 +174,8 @@ void ImGui_ImplSDLRenderer2_RenderDrawData(ImDrawData* draw_data, SDL_Renderer*
ImVec2 clip_scale = render_scale;
// Render command lists
- for (int n = 0; n < draw_data->CmdListsCount; n++)
+ for (const ImDrawList* draw_list : draw_data->CmdLists)
{
- const ImDrawList* draw_list = draw_data->CmdLists[n];
const ImDrawVert* vtx_buffer = draw_list->VtxBuffer.Data;
const ImDrawIdx* idx_buffer = draw_list->IdxBuffer.Data;
@@ -220,55 +232,65 @@ void ImGui_ImplSDLRenderer2_RenderDrawData(ImDrawData* draw_data, SDL_Renderer*
SDL_RenderSetClipRect(renderer, old.ClipEnabled ? &old.ClipRect : nullptr);
}
-// Called by Init/NewFrame/Shutdown
-bool ImGui_ImplSDLRenderer2_CreateFontsTexture()
+void ImGui_ImplSDLRenderer2_UpdateTexture(ImTextureData* tex)
{
- ImGuiIO& io = ImGui::GetIO();
ImGui_ImplSDLRenderer2_Data* bd = ImGui_ImplSDLRenderer2_GetBackendData();
- // Build texture atlas
- unsigned char* pixels;
- int width, height;
- io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); // Load as RGBA 32-bit (75% of the memory is wasted, but default font is so small) because it is more likely to be compatible with user's existing shaders. If your ImTextureId represent a higher-level concept than just a GL texture id, consider calling GetTexDataAsAlpha8() instead to save on GPU memory.
-
- // Upload texture to graphics system
- // (Bilinear sampling is required by default. Set 'io.Fonts->Flags |= ImFontAtlasFlags_NoBakedLines' or 'style.AntiAliasedLinesUseTex = false' to allow point/nearest sampling)
- bd->FontTexture = SDL_CreateTexture(bd->Renderer, SDL_PIXELFORMAT_RGBA32, SDL_TEXTUREACCESS_STATIC, width, height);
- if (bd->FontTexture == nullptr)
+ if (tex->Status == ImTextureStatus_WantCreate)
{
- SDL_Log("error creating texture");
- return false;
+ // Create and upload new texture to graphics system
+ //IMGUI_DEBUG_LOG("UpdateTexture #%03d: WantCreate %dx%d\n", tex->UniqueID, tex->Width, tex->Height);
+ IM_ASSERT(tex->TexID == ImTextureID_Invalid && tex->BackendUserData == nullptr);
+ IM_ASSERT(tex->Format == ImTextureFormat_RGBA32);
+
+ // Create texture
+ // (Bilinear sampling is required by default. Set 'io.Fonts->Flags |= ImFontAtlasFlags_NoBakedLines' or 'style.AntiAliasedLinesUseTex = false' to allow point/nearest sampling)
+ SDL_Texture* sdl_texture = SDL_CreateTexture(bd->Renderer, SDL_PIXELFORMAT_RGBA32, SDL_TEXTUREACCESS_STATIC, tex->Width, tex->Height);
+ IM_ASSERT(sdl_texture != nullptr && "Backend failed to create texture!");
+ SDL_UpdateTexture(sdl_texture, nullptr, tex->GetPixels(), tex->GetPitch());
+ SDL_SetTextureBlendMode(sdl_texture, SDL_BLENDMODE_BLEND);
+ SDL_SetTextureScaleMode(sdl_texture, SDL_ScaleModeLinear);
+
+ // Store identifiers
+ tex->SetTexID((ImTextureID)(intptr_t)sdl_texture);
+ tex->SetStatus(ImTextureStatus_OK);
}
- SDL_UpdateTexture(bd->FontTexture, nullptr, pixels, 4 * width);
- SDL_SetTextureBlendMode(bd->FontTexture, SDL_BLENDMODE_BLEND);
- SDL_SetTextureScaleMode(bd->FontTexture, SDL_ScaleModeLinear);
-
- // Store our identifier
- io.Fonts->SetTexID((ImTextureID)(intptr_t)bd->FontTexture);
-
- return true;
-}
-
-void ImGui_ImplSDLRenderer2_DestroyFontsTexture()
-{
- ImGuiIO& io = ImGui::GetIO();
- ImGui_ImplSDLRenderer2_Data* bd = ImGui_ImplSDLRenderer2_GetBackendData();
- if (bd->FontTexture)
+ else if (tex->Status == ImTextureStatus_WantUpdates)
{
- io.Fonts->SetTexID(0);
- SDL_DestroyTexture(bd->FontTexture);
- bd->FontTexture = nullptr;
+ // Update selected blocks. We only ever write to textures regions which have never been used before!
+ // This backend choose to use tex->Updates[] but you can use tex->UpdateRect to upload a single region.
+ SDL_Texture* sdl_texture = (SDL_Texture*)(intptr_t)tex->TexID;
+ for (ImTextureRect& r : tex->Updates)
+ {
+ SDL_Rect sdl_r = { r.x, r.y, r.w, r.h };
+ SDL_UpdateTexture(sdl_texture, &sdl_r, tex->GetPixelsAt(r.x, r.y), tex->GetPitch());
+ }
+ tex->SetStatus(ImTextureStatus_OK);
+ }
+ else if (tex->Status == ImTextureStatus_WantDestroy)
+ {
+ if (SDL_Texture* sdl_texture = (SDL_Texture*)(intptr_t)tex->TexID)
+ SDL_DestroyTexture(sdl_texture);
+
+ // Clear identifiers and mark as destroyed (in order to allow e.g. calling InvalidateDeviceObjects while running)
+ tex->SetTexID(ImTextureID_Invalid);
+ tex->SetStatus(ImTextureStatus_Destroyed);
}
}
-bool ImGui_ImplSDLRenderer2_CreateDeviceObjects()
+void ImGui_ImplSDLRenderer2_CreateDeviceObjects()
{
- return ImGui_ImplSDLRenderer2_CreateFontsTexture();
}
void ImGui_ImplSDLRenderer2_DestroyDeviceObjects()
{
- ImGui_ImplSDLRenderer2_DestroyFontsTexture();
+ // Destroy all textures
+ for (ImTextureData* tex : ImGui::GetPlatformIO().Textures)
+ if (tex->RefCount == 1)
+ {
+ tex->SetStatus(ImTextureStatus_WantDestroy);
+ ImGui_ImplSDLRenderer2_UpdateTexture(tex);
+ }
}
//-----------------------------------------------------------------------------
diff --git a/backends/imgui_impl_sdlrenderer2.h b/backends/imgui_impl_sdlrenderer2.h
index f5b8172a4..3d8de5fad 100644
--- a/backends/imgui_impl_sdlrenderer2.h
+++ b/backends/imgui_impl_sdlrenderer2.h
@@ -10,8 +10,9 @@
// and it might be difficult to step out of those boundaries.
// Implemented features:
-// [X] Renderer: User texture binding. Use 'SDL_Texture*' as ImTextureID. Read the FAQ about ImTextureID!
+// [X] Renderer: User texture binding. Use 'SDL_Texture*' as texture identifier. Read the FAQ about ImTextureID/ImTextureRef!
// [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset).
+// [X] Renderer: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures).
// [X] Renderer: Expose selected render state for draw callbacks to use. Access in '(ImGui_ImplXXXX_RenderState*)GetPlatformIO().Renderer_RenderState'.
// Missing features:
// [ ] Renderer: Multi-viewport support (multiple windows).
@@ -37,11 +38,12 @@ IMGUI_IMPL_API void ImGui_ImplSDLRenderer2_NewFrame();
IMGUI_IMPL_API void ImGui_ImplSDLRenderer2_RenderDrawData(ImDrawData* draw_data, SDL_Renderer* renderer);
// Called by Init/NewFrame/Shutdown
-IMGUI_IMPL_API bool ImGui_ImplSDLRenderer2_CreateFontsTexture();
-IMGUI_IMPL_API void ImGui_ImplSDLRenderer2_DestroyFontsTexture();
-IMGUI_IMPL_API bool ImGui_ImplSDLRenderer2_CreateDeviceObjects();
+IMGUI_IMPL_API void ImGui_ImplSDLRenderer2_CreateDeviceObjects();
IMGUI_IMPL_API void ImGui_ImplSDLRenderer2_DestroyDeviceObjects();
+// (Advanced) Use e.g. if you need to precisely control the timing of texture updates (e.g. for staged rendering), by setting ImDrawData::Textures = NULL to handle this manually.
+IMGUI_IMPL_API void ImGui_ImplSDLRenderer2_UpdateTexture(ImTextureData* tex);
+
// [BETA] Selected render state data shared with callbacks.
// This is temporarily stored in GetPlatformIO().Renderer_RenderState during the ImGui_ImplSDLRenderer2_RenderDrawData() call.
// (Please open an issue if you feel you need access to more data)
diff --git a/backends/imgui_impl_sdlrenderer3.cpp b/backends/imgui_impl_sdlrenderer3.cpp
index 4bbf7289f..64b6c5eef 100644
--- a/backends/imgui_impl_sdlrenderer3.cpp
+++ b/backends/imgui_impl_sdlrenderer3.cpp
@@ -10,8 +10,9 @@
// and it might be difficult to step out of those boundaries.
// Implemented features:
-// [X] Renderer: User texture binding. Use 'SDL_Texture*' as ImTextureID. Read the FAQ about ImTextureID!
+// [X] Renderer: User texture binding. Use 'SDL_Texture*' as texture identifier. Read the FAQ about ImTextureID/ImTextureRef!
// [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset).
+// [X] Renderer: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures).
// [X] Renderer: Expose selected render state for draw callbacks to use. Access in '(ImGui_ImplXXXX_RenderState*)GetPlatformIO().Renderer_RenderState'.
// Missing features:
// [ ] Renderer: Multi-viewport support (multiple windows).
@@ -25,6 +26,8 @@
// - Introduction, links and more at the top of imgui.cpp
// CHANGELOG
+// 2025-09-18: Call platform_io.ClearRendererHandlers() on shutdown.
+// 2025-06-11: Added support for ImGuiBackendFlags_RendererHasTextures, for dynamic font atlas. Removed ImGui_ImplSDLRenderer3_CreateFontsTexture() and ImGui_ImplSDLRenderer3_DestroyFontsTexture().
// 2025-01-18: Use endian-dependent RGBA32 texture format, to match SDL_Color.
// 2024-10-09: Expose selected render state in ImGui_ImplSDLRenderer3_RenderState, which you can access in 'void* platform_io.Renderer_RenderState' during draw callbacks.
// 2024-07-01: Update for SDL3 api changes: SDL_RenderGeometryRaw() uint32 version was removed (SDL#9009).
@@ -41,6 +44,8 @@
#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wsign-conversion" // warning: implicit conversion changes signedness
+#elif defined(__GNUC__)
+#pragma GCC diagnostic ignored "-Wfloat-equal" // warning: comparing floating-point with '==' or '!=' is unsafe
#endif
// SDL
@@ -53,7 +58,6 @@
struct ImGui_ImplSDLRenderer3_Data
{
SDL_Renderer* Renderer; // Main viewport's renderer
- SDL_Texture* FontTexture;
ImVector ColorBuffer;
ImGui_ImplSDLRenderer3_Data() { memset((void*)this, 0, sizeof(*this)); }
@@ -79,6 +83,7 @@ bool ImGui_ImplSDLRenderer3_Init(SDL_Renderer* renderer)
io.BackendRendererUserData = (void*)bd;
io.BackendRendererName = "imgui_impl_sdlrenderer3";
io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes.
+ io.BackendFlags |= ImGuiBackendFlags_RendererHasTextures; // We can honor ImGuiPlatformIO::Textures[] requests during render.
bd->Renderer = renderer;
@@ -90,12 +95,14 @@ void ImGui_ImplSDLRenderer3_Shutdown()
ImGui_ImplSDLRenderer3_Data* bd = ImGui_ImplSDLRenderer3_GetBackendData();
IM_ASSERT(bd != nullptr && "No renderer backend to shutdown, or already shutdown?");
ImGuiIO& io = ImGui::GetIO();
+ ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
ImGui_ImplSDLRenderer3_DestroyDeviceObjects();
io.BackendRendererName = nullptr;
io.BackendRendererUserData = nullptr;
- io.BackendFlags &= ~ImGuiBackendFlags_RendererHasVtxOffset;
+ io.BackendFlags &= ~(ImGuiBackendFlags_RendererHasVtxOffset | ImGuiBackendFlags_RendererHasTextures);
+ platform_io.ClearRendererHandlers();
IM_DELETE(bd);
}
@@ -111,9 +118,7 @@ void ImGui_ImplSDLRenderer3_NewFrame()
{
ImGui_ImplSDLRenderer3_Data* bd = ImGui_ImplSDLRenderer3_GetBackendData();
IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplSDLRenderer3_Init()?");
-
- if (!bd->FontTexture)
- ImGui_ImplSDLRenderer3_CreateDeviceObjects();
+ IM_UNUSED(bd);
}
// https://github.com/libsdl-org/SDL/issues/9009
@@ -154,6 +159,13 @@ void ImGui_ImplSDLRenderer3_RenderDrawData(ImDrawData* draw_data, SDL_Renderer*
if (fb_width == 0 || fb_height == 0)
return;
+ // Catch up with texture updates. Most of the times, the list will have 1 element with an OK status, aka nothing to do.
+ // (This almost always points to ImGui::GetPlatformIO().Textures[] but is part of ImDrawData to allow overriding or disabling texture updates).
+ if (draw_data->Textures != nullptr)
+ for (ImTextureData* tex : *draw_data->Textures)
+ if (tex->Status != ImTextureStatus_OK)
+ ImGui_ImplSDLRenderer3_UpdateTexture(tex);
+
// Backup SDL_Renderer state that will be modified to restore it afterwards
struct BackupSDLRendererState
{
@@ -182,9 +194,8 @@ void ImGui_ImplSDLRenderer3_RenderDrawData(ImDrawData* draw_data, SDL_Renderer*
ImVec2 clip_scale = render_scale;
// Render command lists
- for (int n = 0; n < draw_data->CmdListsCount; n++)
+ for (const ImDrawList* draw_list : draw_data->CmdLists)
{
- const ImDrawList* draw_list = draw_data->CmdLists[n];
const ImDrawVert* vtx_buffer = draw_list->VtxBuffer.Data;
const ImDrawIdx* idx_buffer = draw_list->IdxBuffer.Data;
@@ -237,55 +248,65 @@ void ImGui_ImplSDLRenderer3_RenderDrawData(ImDrawData* draw_data, SDL_Renderer*
SDL_SetRenderClipRect(renderer, old.ClipEnabled ? &old.ClipRect : nullptr);
}
-// Called by Init/NewFrame/Shutdown
-bool ImGui_ImplSDLRenderer3_CreateFontsTexture()
+void ImGui_ImplSDLRenderer3_UpdateTexture(ImTextureData* tex)
{
- ImGuiIO& io = ImGui::GetIO();
ImGui_ImplSDLRenderer3_Data* bd = ImGui_ImplSDLRenderer3_GetBackendData();
- // Build texture atlas
- unsigned char* pixels;
- int width, height;
- io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); // Load as RGBA 32-bit (75% of the memory is wasted, but default font is so small) because it is more likely to be compatible with user's existing shaders. If your ImTextureId represent a higher-level concept than just a GL texture id, consider calling GetTexDataAsAlpha8() instead to save on GPU memory.
-
- // Upload texture to graphics system
- // (Bilinear sampling is required by default. Set 'io.Fonts->Flags |= ImFontAtlasFlags_NoBakedLines' or 'style.AntiAliasedLinesUseTex = false' to allow point/nearest sampling)
- bd->FontTexture = SDL_CreateTexture(bd->Renderer, SDL_PIXELFORMAT_RGBA32, SDL_TEXTUREACCESS_STATIC, width, height);
- if (bd->FontTexture == nullptr)
+ if (tex->Status == ImTextureStatus_WantCreate)
{
- SDL_Log("error creating texture");
- return false;
+ // Create and upload new texture to graphics system
+ //IMGUI_DEBUG_LOG("UpdateTexture #%03d: WantCreate %dx%d\n", tex->UniqueID, tex->Width, tex->Height);
+ IM_ASSERT(tex->TexID == 0 && tex->BackendUserData == nullptr);
+ IM_ASSERT(tex->Format == ImTextureFormat_RGBA32);
+
+ // Create texture
+ // (Bilinear sampling is required by default. Set 'io.Fonts->Flags |= ImFontAtlasFlags_NoBakedLines' or 'style.AntiAliasedLinesUseTex = false' to allow point/nearest sampling)
+ SDL_Texture* sdl_texture = SDL_CreateTexture(bd->Renderer, SDL_PIXELFORMAT_RGBA32, SDL_TEXTUREACCESS_STATIC, tex->Width, tex->Height);
+ IM_ASSERT(sdl_texture != nullptr && "Backend failed to create texture!");
+ SDL_UpdateTexture(sdl_texture, nullptr, tex->GetPixels(), tex->GetPitch());
+ SDL_SetTextureBlendMode(sdl_texture, SDL_BLENDMODE_BLEND);
+ SDL_SetTextureScaleMode(sdl_texture, SDL_SCALEMODE_LINEAR);
+
+ // Store identifiers
+ tex->SetTexID((ImTextureID)(intptr_t)sdl_texture);
+ tex->SetStatus(ImTextureStatus_OK);
}
- SDL_UpdateTexture(bd->FontTexture, nullptr, pixels, 4 * width);
- SDL_SetTextureBlendMode(bd->FontTexture, SDL_BLENDMODE_BLEND);
- SDL_SetTextureScaleMode(bd->FontTexture, SDL_SCALEMODE_LINEAR);
-
- // Store our identifier
- io.Fonts->SetTexID((ImTextureID)(intptr_t)bd->FontTexture);
-
- return true;
-}
-
-void ImGui_ImplSDLRenderer3_DestroyFontsTexture()
-{
- ImGuiIO& io = ImGui::GetIO();
- ImGui_ImplSDLRenderer3_Data* bd = ImGui_ImplSDLRenderer3_GetBackendData();
- if (bd->FontTexture)
+ else if (tex->Status == ImTextureStatus_WantUpdates)
{
- io.Fonts->SetTexID(0);
- SDL_DestroyTexture(bd->FontTexture);
- bd->FontTexture = nullptr;
+ // Update selected blocks. We only ever write to textures regions which have never been used before!
+ // This backend choose to use tex->Updates[] but you can use tex->UpdateRect to upload a single region.
+ SDL_Texture* sdl_texture = (SDL_Texture*)(intptr_t)tex->TexID;
+ for (ImTextureRect& r : tex->Updates)
+ {
+ SDL_Rect sdl_r = { r.x, r.y, r.w, r.h };
+ SDL_UpdateTexture(sdl_texture, &sdl_r, tex->GetPixelsAt(r.x, r.y), tex->GetPitch());
+ }
+ tex->SetStatus(ImTextureStatus_OK);
+ }
+ else if (tex->Status == ImTextureStatus_WantDestroy)
+ {
+ if (SDL_Texture* sdl_texture = (SDL_Texture*)(intptr_t)tex->TexID)
+ SDL_DestroyTexture(sdl_texture);
+
+ // Clear identifiers and mark as destroyed (in order to allow e.g. calling InvalidateDeviceObjects while running)
+ tex->SetTexID(ImTextureID_Invalid);
+ tex->SetStatus(ImTextureStatus_Destroyed);
}
}
-bool ImGui_ImplSDLRenderer3_CreateDeviceObjects()
+void ImGui_ImplSDLRenderer3_CreateDeviceObjects()
{
- return ImGui_ImplSDLRenderer3_CreateFontsTexture();
}
void ImGui_ImplSDLRenderer3_DestroyDeviceObjects()
{
- ImGui_ImplSDLRenderer3_DestroyFontsTexture();
+ // Destroy all textures
+ for (ImTextureData* tex : ImGui::GetPlatformIO().Textures)
+ if (tex->RefCount == 1)
+ {
+ tex->SetStatus(ImTextureStatus_WantDestroy);
+ ImGui_ImplSDLRenderer3_UpdateTexture(tex);
+ }
}
//-----------------------------------------------------------------------------
diff --git a/backends/imgui_impl_sdlrenderer3.h b/backends/imgui_impl_sdlrenderer3.h
index f79aa1bc4..4faaf92b6 100644
--- a/backends/imgui_impl_sdlrenderer3.h
+++ b/backends/imgui_impl_sdlrenderer3.h
@@ -10,8 +10,9 @@
// and it might be difficult to step out of those boundaries.
// Implemented features:
-// [X] Renderer: User texture binding. Use 'SDL_Texture*' as ImTextureID. Read the FAQ about ImTextureID!
+// [X] Renderer: User texture binding. Use 'SDL_Texture*' as texture identifier. Read the FAQ about ImTextureID/ImTextureRef!
// [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset).
+// [X] Renderer: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures).
// [X] Renderer: Expose selected render state for draw callbacks to use. Access in '(ImGui_ImplXXXX_RenderState*)GetPlatformIO().Renderer_RenderState'.
// Missing features:
// [ ] Renderer: Multi-viewport support (multiple windows).
@@ -37,11 +38,12 @@ IMGUI_IMPL_API void ImGui_ImplSDLRenderer3_NewFrame();
IMGUI_IMPL_API void ImGui_ImplSDLRenderer3_RenderDrawData(ImDrawData* draw_data, SDL_Renderer* renderer);
// Called by Init/NewFrame/Shutdown
-IMGUI_IMPL_API bool ImGui_ImplSDLRenderer3_CreateFontsTexture();
-IMGUI_IMPL_API void ImGui_ImplSDLRenderer3_DestroyFontsTexture();
-IMGUI_IMPL_API bool ImGui_ImplSDLRenderer3_CreateDeviceObjects();
+IMGUI_IMPL_API void ImGui_ImplSDLRenderer3_CreateDeviceObjects();
IMGUI_IMPL_API void ImGui_ImplSDLRenderer3_DestroyDeviceObjects();
+// (Advanced) Use e.g. if you need to precisely control the timing of texture updates (e.g. for staged rendering), by setting ImDrawData::Textures = NULL to handle this manually.
+IMGUI_IMPL_API void ImGui_ImplSDLRenderer3_UpdateTexture(ImTextureData* tex);
+
// [BETA] Selected render state data shared with callbacks.
// This is temporarily stored in GetPlatformIO().Renderer_RenderState during the ImGui_ImplSDLRenderer3_RenderDrawData() call.
// (Please open an issue if you feel you need access to more data)
diff --git a/backends/imgui_impl_vulkan.cpp b/backends/imgui_impl_vulkan.cpp
index bdb4c43d9..045f2c6c2 100644
--- a/backends/imgui_impl_vulkan.cpp
+++ b/backends/imgui_impl_vulkan.cpp
@@ -2,8 +2,9 @@
// This needs to be used along with a Platform Backend (e.g. GLFW, SDL, Win32, custom..)
// Implemented features:
-// [!] Renderer: User texture binding. Use 'VkDescriptorSet' as ImTextureID. Call ImGui_ImplVulkan_AddTexture() to register one. Read the FAQ about ImTextureID! See https://github.com/ocornut/imgui/pull/914 for discussions.
+// [!] Renderer: User texture binding. Use 'VkDescriptorSet' as texture identifier. Call ImGui_ImplVulkan_AddTexture() to register one. Read the FAQ about ImTextureID/ImTextureRef + https://github.com/ocornut/imgui/pull/914 for discussions.
// [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset).
+// [X] Renderer: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures).
// [X] Renderer: Expose selected render state for draw callbacks to use. Access in '(ImGui_ImplXXXX_RenderState*)GetPlatformIO().Renderer_RenderState'.
// [x] Renderer: Multi-viewport / platform windows. With issues (flickering when creating a new viewport).
@@ -28,6 +29,20 @@
// CHANGELOG
// (minor and older changes stripped away, please see git history for details)
// 2025-XX-XX: Platform: Added support for multiple windows via the ImGuiPlatformIO interface.
+// 2025-09-26: *BREAKING CHANGE*: moved some fields in ImGui_ImplVulkan_InitInfo: init_info.RenderPass --> init_info.PipelineInfoMain.RenderPass, init_info.Subpass --> init_info.PipelineInfoMain.Subpass, init_info.MSAASamples --> init_info.PipelineInfoMain.MSAASamples, init_info.PipelineRenderingCreateInfo --> init_info.PipelineInfoMain.PipelineRenderingCreateInfo.
+// 2025-09-26: *BREAKING CHANGE*: renamed ImGui_ImplVulkan_MainPipelineCreateInfo to ImGui_ImplVulkan_PipelineInfo. Introduced very recently so shouldn't affect many users.
+// 2025-09-26: *BREAKING CHANGE*: helper ImGui_ImplVulkanH_CreateOrResizeWindow() added a VkImageUsageFlags image_usage` argument, default to VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT if 0.
+// 2025-09-26: Vulkan: Added a way to customize shaders by filling ImGui_ImplVulkan_InitInfo::CustomShaderVertCreateInfo/CustomShaderFragCreateInfo. (#8585)
+// 2025-09-22: [Docking] Added ImGui_ImplVulkanH_GetWindowDataFromViewport() accessor/helper. (#8946, #8940)
+// 2025-09-18: Call platform_io.ClearRendererHandlers() on shutdown.
+// 2025-09-04: Vulkan: Added ImGui_ImplVulkan_CreateMainPipeline(). (#8110, #8111)
+// 2025-07-27: Vulkan: Fixed texture update corruption introduced on 2025-06-11. (#8801, #8755, #8840)
+// 2025-07-07: Vulkan: Fixed texture synchronization issue introduced on 2025-06-11. (#8772)
+// 2025-06-27: Vulkan: Fixed validation errors during texture upload/update by aligning upload size to 'nonCoherentAtomSize'. (#8743, #8744)
+// 2025-06-11: Vulkan: Added support for ImGuiBackendFlags_RendererHasTextures, for dynamic font atlas. Removed ImGui_ImplVulkan_CreateFontsTexture() and ImGui_ImplVulkan_DestroyFontsTexture().
+// 2025-05-07: Vulkan: Fixed validation errors during window detach in multi-viewport mode. (#8600, #8176)
+// 2025-05-07: Vulkan: Load dynamic rendering functions using vkGetDeviceProcAddr() + try both non-KHR and KHR versions. (#8600, #8326, #8365)
+// 2025-04-07: Vulkan: Deep-copy ImGui_ImplVulkan_InitInfo::PipelineRenderingCreateInfo's pColorAttachmentFormats buffer when set, in order to reduce common user-error of specifying a pointer to data that gets out of scope. (#8282)
// 2025-02-14: *BREAKING CHANGE*: Added uint32_t api_version to ImGui_ImplVulkan_LoadFunctions().
// 2025-02-13: Vulkan: Added ApiVersion field in ImGui_ImplVulkan_InitInfo. Default to header version if unspecified. Dynamic rendering path loads "vkCmdBeginRendering/vkCmdEndRendering" (without -KHR suffix) on API 1.3. (#8326)
// 2025-01-09: Vulkan: Added IMGUI_IMPL_VULKAN_MINIMUM_IMAGE_SAMPLER_POOL_SIZE to clarify how many image sampler descriptors are expected to be available in descriptor pool. (#6642)
@@ -93,6 +108,7 @@
#ifndef IM_MAX
#define IM_MAX(A, B) (((A) >= (B)) ? (A) : (B))
#endif
+#undef Status // X11 headers are leaking this.
// Visual Studio warnings
#ifdef _MSC_VER
@@ -109,11 +125,11 @@ void ImGui_ImplVulkan_DestroyWindowRenderBuffers(VkDevice device, ImGui_ImplVulk
void ImGui_ImplVulkanH_DestroyFrame(VkDevice device, ImGui_ImplVulkanH_Frame* fd, const VkAllocationCallbacks* allocator);
void ImGui_ImplVulkanH_DestroyFrameSemaphores(VkDevice device, ImGui_ImplVulkanH_FrameSemaphores* fsd, const VkAllocationCallbacks* allocator);
void ImGui_ImplVulkanH_DestroyAllViewportsRenderBuffers(VkDevice device, const VkAllocationCallbacks* allocator);
-void ImGui_ImplVulkanH_CreateWindowSwapChain(VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_Window* wd, const VkAllocationCallbacks* allocator, int w, int h, uint32_t min_image_count);
+void ImGui_ImplVulkanH_CreateWindowSwapChain(VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_Window* wd, const VkAllocationCallbacks* allocator, int w, int h, uint32_t min_image_count, VkImageUsageFlags image_usage);
void ImGui_ImplVulkanH_CreateWindowCommandBuffers(VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_Window* wd, uint32_t queue_family, const VkAllocationCallbacks* allocator);
// Vulkan prototypes for use with custom loaders
-// (see description of IMGUI_IMPL_VULKAN_NO_PROTOTYPES in imgui_impl_vulkan.h
+// (see description of IMGUI_IMPL_VULKAN_NO_PROTOTYPES in imgui_impl_vulkan.h)
#if defined(VK_NO_PROTOTYPES) && !defined(VOLK_H_)
#define IMGUI_IMPL_VULKAN_USE_LOADER
static bool g_FunctionsLoaded = false;
@@ -180,6 +196,7 @@ static bool g_FunctionsLoaded = true;
IMGUI_VULKAN_FUNC_MAP_MACRO(vkFreeDescriptorSets) \
IMGUI_VULKAN_FUNC_MAP_MACRO(vkFreeMemory) \
IMGUI_VULKAN_FUNC_MAP_MACRO(vkGetBufferMemoryRequirements) \
+ IMGUI_VULKAN_FUNC_MAP_MACRO(vkGetDeviceQueue) \
IMGUI_VULKAN_FUNC_MAP_MACRO(vkGetImageMemoryRequirements) \
IMGUI_VULKAN_FUNC_MAP_MACRO(vkGetPhysicalDeviceProperties) \
IMGUI_VULKAN_FUNC_MAP_MACRO(vkGetPhysicalDeviceMemoryProperties) \
@@ -260,6 +277,7 @@ struct ImGui_ImplVulkan_Data
{
ImGui_ImplVulkan_InitInfo VulkanInitInfo;
VkDeviceSize BufferMemoryAlignment;
+ VkDeviceSize NonCoherentAtomSize;
VkPipelineCreateFlags PipelineCreateFlags;
VkDescriptorSetLayout DescriptorSetLayout;
VkPipelineLayout PipelineLayout;
@@ -268,10 +286,10 @@ struct ImGui_ImplVulkan_Data
VkShaderModule ShaderModuleVert;
VkShaderModule ShaderModuleFrag;
VkDescriptorPool DescriptorPool;
+ ImVector PipelineRenderingCreateInfoColorAttachmentFormats; // Deep copy of format array
// Texture management
- ImGui_ImplVulkan_Texture FontTexture;
- VkSampler TexSampler;
+ VkSampler TexSamplerLinear;
VkCommandPool TexCommandPool;
VkCommandBuffer TexCommandBuffer;
@@ -282,6 +300,7 @@ struct ImGui_ImplVulkan_Data
{
memset((void*)this, 0, sizeof(*this));
BufferMemoryAlignment = 256;
+ NonCoherentAtomSize = 64;
}
};
@@ -525,6 +544,13 @@ void ImGui_ImplVulkan_RenderDrawData(ImDrawData* draw_data, VkCommandBuffer comm
if (fb_width <= 0 || fb_height <= 0)
return;
+ // Catch up with texture updates. Most of the times, the list will have 1 element with an OK status, aka nothing to do.
+ // (This almost always points to ImGui::GetPlatformIO().Textures[] but is part of ImDrawData to allow overriding or disabling texture updates).
+ if (draw_data->Textures != nullptr)
+ for (ImTextureData* tex : *draw_data->Textures)
+ if (tex->Status != ImTextureStatus_OK)
+ ImGui_ImplVulkan_UpdateTexture(tex);
+
ImGui_ImplVulkan_Data* bd = ImGui_ImplVulkan_GetBackendData();
ImGui_ImplVulkan_InitInfo* v = &bd->VulkanInitInfo;
if (pipeline == VK_NULL_HANDLE)
@@ -562,9 +588,8 @@ void ImGui_ImplVulkan_RenderDrawData(ImDrawData* draw_data, VkCommandBuffer comm
check_vk_result(err);
err = vkMapMemory(v->Device, rb->IndexBufferMemory, 0, index_size, 0, (void**)&idx_dst);
check_vk_result(err);
- for (int n = 0; n < draw_data->CmdListsCount; n++)
+ for (const ImDrawList* draw_list : draw_data->CmdLists)
{
- const ImDrawList* draw_list = draw_data->CmdLists[n];
memcpy(vtx_dst, draw_list->VtxBuffer.Data, draw_list->VtxBuffer.Size * sizeof(ImDrawVert));
memcpy(idx_dst, draw_list->IdxBuffer.Data, draw_list->IdxBuffer.Size * sizeof(ImDrawIdx));
vtx_dst += draw_list->VtxBuffer.Size;
@@ -600,11 +625,11 @@ void ImGui_ImplVulkan_RenderDrawData(ImDrawData* draw_data, VkCommandBuffer comm
// Render command lists
// (Because we merged all buffers into a single one, we maintain our own offset into them)
+ VkDescriptorSet last_desc_set = VK_NULL_HANDLE;
int global_vtx_offset = 0;
int global_idx_offset = 0;
- for (int n = 0; n < draw_data->CmdListsCount; n++)
+ for (const ImDrawList* draw_list : draw_data->CmdLists)
{
- const ImDrawList* draw_list = draw_data->CmdLists[n];
for (int cmd_i = 0; cmd_i < draw_list->CmdBuffer.Size; cmd_i++)
{
const ImDrawCmd* pcmd = &draw_list->CmdBuffer[cmd_i];
@@ -616,6 +641,7 @@ void ImGui_ImplVulkan_RenderDrawData(ImDrawData* draw_data, VkCommandBuffer comm
ImGui_ImplVulkan_SetupRenderState(draw_data, pipeline, command_buffer, rb, fb_width, fb_height);
else
pcmd->UserCallback(draw_list, pcmd);
+ last_desc_set = VK_NULL_HANDLE;
}
else
{
@@ -641,7 +667,9 @@ void ImGui_ImplVulkan_RenderDrawData(ImDrawData* draw_data, VkCommandBuffer comm
// Bind DescriptorSet with font or user texture
VkDescriptorSet desc_set = (VkDescriptorSet)pcmd->GetTexID();
- vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, bd->PipelineLayout, 0, 1, &desc_set, 0, nullptr);
+ if (desc_set != last_desc_set)
+ vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, bd->PipelineLayout, 0, 1, &desc_set, 0, nullptr);
+ last_desc_set = desc_set;
// Draw
vkCmdDrawIndexed(command_buffer, pcmd->ElemCount, 1, pcmd->IdxOffset + global_idx_offset, pcmd->VtxOffset + global_vtx_offset, 0);
@@ -663,247 +691,265 @@ void ImGui_ImplVulkan_RenderDrawData(ImDrawData* draw_data, VkCommandBuffer comm
vkCmdSetScissor(command_buffer, 0, 1, &scissor);
}
-bool ImGui_ImplVulkan_CreateFontsTexture()
+static void ImGui_ImplVulkan_DestroyTexture(ImTextureData* tex)
{
- ImGuiIO& io = ImGui::GetIO();
+ if (ImGui_ImplVulkan_Texture* backend_tex = (ImGui_ImplVulkan_Texture*)tex->BackendUserData)
+ {
+ IM_ASSERT(backend_tex->DescriptorSet == (VkDescriptorSet)tex->TexID);
+ ImGui_ImplVulkan_Data* bd = ImGui_ImplVulkan_GetBackendData();
+ ImGui_ImplVulkan_InitInfo* v = &bd->VulkanInitInfo;
+ ImGui_ImplVulkan_RemoveTexture(backend_tex->DescriptorSet);
+ vkDestroyImageView(v->Device, backend_tex->ImageView, v->Allocator);
+ vkDestroyImage(v->Device, backend_tex->Image, v->Allocator);
+ vkFreeMemory(v->Device, backend_tex->Memory, v->Allocator);
+ IM_DELETE(backend_tex);
+
+ // Clear identifiers and mark as destroyed (in order to allow e.g. calling InvalidateDeviceObjects while running)
+ tex->SetTexID(ImTextureID_Invalid);
+ tex->BackendUserData = nullptr;
+ }
+ tex->SetStatus(ImTextureStatus_Destroyed);
+}
+
+void ImGui_ImplVulkan_UpdateTexture(ImTextureData* tex)
+{
+ if (tex->Status == ImTextureStatus_OK)
+ return;
ImGui_ImplVulkan_Data* bd = ImGui_ImplVulkan_GetBackendData();
ImGui_ImplVulkan_InitInfo* v = &bd->VulkanInitInfo;
VkResult err;
- // Destroy existing texture (if any)
- if (bd->FontTexture.DescriptorSet)
+ if (tex->Status == ImTextureStatus_WantCreate)
{
- vkQueueWaitIdle(v->Queue);
- ImGui_ImplVulkan_DestroyFontsTexture();
+ // Create and upload new texture to graphics system
+ //IMGUI_DEBUG_LOG("UpdateTexture #%03d: WantCreate %dx%d\n", tex->UniqueID, tex->Width, tex->Height);
+ IM_ASSERT(tex->TexID == ImTextureID_Invalid && tex->BackendUserData == nullptr);
+ IM_ASSERT(tex->Format == ImTextureFormat_RGBA32);
+ ImGui_ImplVulkan_Texture* backend_tex = IM_NEW(ImGui_ImplVulkan_Texture)();
+
+ // Create the Image:
+ {
+ VkImageCreateInfo info = {};
+ info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
+ info.imageType = VK_IMAGE_TYPE_2D;
+ info.format = VK_FORMAT_R8G8B8A8_UNORM;
+ info.extent.width = tex->Width;
+ info.extent.height = tex->Height;
+ info.extent.depth = 1;
+ info.mipLevels = 1;
+ info.arrayLayers = 1;
+ info.samples = VK_SAMPLE_COUNT_1_BIT;
+ info.tiling = VK_IMAGE_TILING_OPTIMAL;
+ info.usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
+ info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
+ info.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
+ err = vkCreateImage(v->Device, &info, v->Allocator, &backend_tex->Image);
+ check_vk_result(err);
+ VkMemoryRequirements req;
+ vkGetImageMemoryRequirements(v->Device, backend_tex->Image, &req);
+ VkMemoryAllocateInfo alloc_info = {};
+ alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
+ alloc_info.allocationSize = IM_MAX(v->MinAllocationSize, req.size);
+ alloc_info.memoryTypeIndex = ImGui_ImplVulkan_MemoryType(VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, req.memoryTypeBits);
+ err = vkAllocateMemory(v->Device, &alloc_info, v->Allocator, &backend_tex->Memory);
+ check_vk_result(err);
+ err = vkBindImageMemory(v->Device, backend_tex->Image, backend_tex->Memory, 0);
+ check_vk_result(err);
+ }
+
+ // Create the Image View:
+ {
+ VkImageViewCreateInfo info = {};
+ info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
+ info.image = backend_tex->Image;
+ info.viewType = VK_IMAGE_VIEW_TYPE_2D;
+ info.format = VK_FORMAT_R8G8B8A8_UNORM;
+ info.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
+ info.subresourceRange.levelCount = 1;
+ info.subresourceRange.layerCount = 1;
+ err = vkCreateImageView(v->Device, &info, v->Allocator, &backend_tex->ImageView);
+ check_vk_result(err);
+ }
+
+ // Create the Descriptor Set
+ backend_tex->DescriptorSet = ImGui_ImplVulkan_AddTexture(bd->TexSamplerLinear, backend_tex->ImageView, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
+
+ // Store identifiers
+ tex->SetTexID((ImTextureID)backend_tex->DescriptorSet);
+ tex->BackendUserData = backend_tex;
}
- // Create command pool/buffer
- if (bd->TexCommandPool == VK_NULL_HANDLE)
+ if (tex->Status == ImTextureStatus_WantCreate || tex->Status == ImTextureStatus_WantUpdates)
{
- VkCommandPoolCreateInfo info = {};
- info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
- info.flags = 0;
- info.queueFamilyIndex = v->QueueFamily;
- vkCreateCommandPool(v->Device, &info, v->Allocator, &bd->TexCommandPool);
- }
- if (bd->TexCommandBuffer == VK_NULL_HANDLE)
- {
- VkCommandBufferAllocateInfo info = {};
- info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
- info.commandPool = bd->TexCommandPool;
- info.commandBufferCount = 1;
- err = vkAllocateCommandBuffers(v->Device, &info, &bd->TexCommandBuffer);
+ ImGui_ImplVulkan_Texture* backend_tex = (ImGui_ImplVulkan_Texture*)tex->BackendUserData;
+
+ // Update full texture or selected blocks. We only ever write to textures regions which have never been used before!
+ // This backend choose to use tex->UpdateRect but you can use tex->Updates[] to upload individual regions.
+ // We could use the smaller rect on _WantCreate but using the full rect allows us to clear the texture.
+ const int upload_x = (tex->Status == ImTextureStatus_WantCreate) ? 0 : tex->UpdateRect.x;
+ const int upload_y = (tex->Status == ImTextureStatus_WantCreate) ? 0 : tex->UpdateRect.y;
+ const int upload_w = (tex->Status == ImTextureStatus_WantCreate) ? tex->Width : tex->UpdateRect.w;
+ const int upload_h = (tex->Status == ImTextureStatus_WantCreate) ? tex->Height : tex->UpdateRect.h;
+
+ // Create the Upload Buffer:
+ VkDeviceMemory upload_buffer_memory;
+
+ VkBuffer upload_buffer;
+ VkDeviceSize upload_pitch = upload_w * tex->BytesPerPixel;
+ VkDeviceSize upload_size = AlignBufferSize(upload_h * upload_pitch, bd->NonCoherentAtomSize);
+ {
+ VkBufferCreateInfo buffer_info = {};
+ buffer_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
+ buffer_info.size = upload_size;
+ buffer_info.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
+ buffer_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
+ err = vkCreateBuffer(v->Device, &buffer_info, v->Allocator, &upload_buffer);
+ check_vk_result(err);
+ VkMemoryRequirements req;
+ vkGetBufferMemoryRequirements(v->Device, upload_buffer, &req);
+ bd->BufferMemoryAlignment = (bd->BufferMemoryAlignment > req.alignment) ? bd->BufferMemoryAlignment : req.alignment;
+ VkMemoryAllocateInfo alloc_info = {};
+ alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
+ alloc_info.allocationSize = IM_MAX(v->MinAllocationSize, req.size);
+ alloc_info.memoryTypeIndex = ImGui_ImplVulkan_MemoryType(VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, req.memoryTypeBits);
+ err = vkAllocateMemory(v->Device, &alloc_info, v->Allocator, &upload_buffer_memory);
+ check_vk_result(err);
+ err = vkBindBufferMemory(v->Device, upload_buffer, upload_buffer_memory, 0);
+ check_vk_result(err);
+ }
+
+ // Upload to Buffer:
+ {
+ char* map = nullptr;
+ err = vkMapMemory(v->Device, upload_buffer_memory, 0, upload_size, 0, (void**)(&map));
+ check_vk_result(err);
+ for (int y = 0; y < upload_h; y++)
+ memcpy(map + upload_pitch * y, tex->GetPixelsAt(upload_x, upload_y + y), (size_t)upload_pitch);
+ VkMappedMemoryRange range[1] = {};
+ range[0].sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
+ range[0].memory = upload_buffer_memory;
+ range[0].size = upload_size;
+ err = vkFlushMappedMemoryRanges(v->Device, 1, range);
+ check_vk_result(err);
+ vkUnmapMemory(v->Device, upload_buffer_memory);
+ }
+
+ // Start command buffer
+ {
+ err = vkResetCommandPool(v->Device, bd->TexCommandPool, 0);
+ check_vk_result(err);
+ VkCommandBufferBeginInfo begin_info = {};
+ begin_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
+ begin_info.flags |= VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
+ err = vkBeginCommandBuffer(bd->TexCommandBuffer, &begin_info);
+ check_vk_result(err);
+ }
+
+ // Copy to Image:
+ {
+ VkBufferMemoryBarrier upload_barrier[1] = {};
+ upload_barrier[0].sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER;
+ upload_barrier[0].srcAccessMask = VK_ACCESS_HOST_WRITE_BIT;
+ upload_barrier[0].dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
+ upload_barrier[0].srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
+ upload_barrier[0].dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
+ upload_barrier[0].buffer = upload_buffer;
+ upload_barrier[0].offset = 0;
+ upload_barrier[0].size = upload_size;
+
+ VkImageMemoryBarrier copy_barrier[1] = {};
+ copy_barrier[0].sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
+ copy_barrier[0].dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
+ copy_barrier[0].oldLayout = (tex->Status == ImTextureStatus_WantCreate) ? VK_IMAGE_LAYOUT_UNDEFINED : VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
+ copy_barrier[0].newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
+ copy_barrier[0].srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
+ copy_barrier[0].dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
+ copy_barrier[0].image = backend_tex->Image;
+ copy_barrier[0].subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
+ copy_barrier[0].subresourceRange.levelCount = 1;
+ copy_barrier[0].subresourceRange.layerCount = 1;
+ vkCmdPipelineBarrier(bd->TexCommandBuffer, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT | VK_PIPELINE_STAGE_HOST_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 1, upload_barrier, 1, copy_barrier);
+
+ VkBufferImageCopy region = {};
+ region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
+ region.imageSubresource.layerCount = 1;
+ region.imageExtent.width = upload_w;
+ region.imageExtent.height = upload_h;
+ region.imageExtent.depth = 1;
+ region.imageOffset.x = upload_x;
+ region.imageOffset.y = upload_y;
+ vkCmdCopyBufferToImage(bd->TexCommandBuffer, upload_buffer, backend_tex->Image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ®ion);
+
+ VkImageMemoryBarrier use_barrier[1] = {};
+ use_barrier[0].sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
+ use_barrier[0].srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
+ use_barrier[0].dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
+ use_barrier[0].oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
+ use_barrier[0].newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
+ use_barrier[0].srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
+ use_barrier[0].dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
+ use_barrier[0].image = backend_tex->Image;
+ use_barrier[0].subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
+ use_barrier[0].subresourceRange.levelCount = 1;
+ use_barrier[0].subresourceRange.layerCount = 1;
+ vkCmdPipelineBarrier(bd->TexCommandBuffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, 0, 0, nullptr, 0, nullptr, 1, use_barrier);
+ }
+
+ // End command buffer
+ {
+ VkSubmitInfo end_info = {};
+ end_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
+ end_info.commandBufferCount = 1;
+ end_info.pCommandBuffers = &bd->TexCommandBuffer;
+ err = vkEndCommandBuffer(bd->TexCommandBuffer);
+ check_vk_result(err);
+ err = vkQueueSubmit(v->Queue, 1, &end_info, VK_NULL_HANDLE);
+ check_vk_result(err);
+ }
+
+ err = vkQueueWaitIdle(v->Queue); // FIXME-OPT: Suboptimal!
check_vk_result(err);
+ vkDestroyBuffer(v->Device, upload_buffer, v->Allocator);
+ vkFreeMemory(v->Device, upload_buffer_memory, v->Allocator);
+
+ tex->SetStatus(ImTextureStatus_OK);
}
- // Start command buffer
- {
- err = vkResetCommandPool(v->Device, bd->TexCommandPool, 0);
- check_vk_result(err);
- VkCommandBufferBeginInfo begin_info = {};
- begin_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
- begin_info.flags |= VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
- err = vkBeginCommandBuffer(bd->TexCommandBuffer, &begin_info);
- check_vk_result(err);
- }
-
- unsigned char* pixels;
- int width, height;
- io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
- size_t upload_size = width * height * 4 * sizeof(char);
-
- // Create the Image:
- ImGui_ImplVulkan_Texture* backend_tex = &bd->FontTexture;
- {
- VkImageCreateInfo info = {};
- info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
- info.imageType = VK_IMAGE_TYPE_2D;
- info.format = VK_FORMAT_R8G8B8A8_UNORM;
- info.extent.width = width;
- info.extent.height = height;
- info.extent.depth = 1;
- info.mipLevels = 1;
- info.arrayLayers = 1;
- info.samples = VK_SAMPLE_COUNT_1_BIT;
- info.tiling = VK_IMAGE_TILING_OPTIMAL;
- info.usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
- info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
- info.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
- err = vkCreateImage(v->Device, &info, v->Allocator, &backend_tex->Image);
- check_vk_result(err);
- VkMemoryRequirements req;
- vkGetImageMemoryRequirements(v->Device, backend_tex->Image, &req);
- VkMemoryAllocateInfo alloc_info = {};
- alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
- alloc_info.allocationSize = IM_MAX(v->MinAllocationSize, req.size);
- alloc_info.memoryTypeIndex = ImGui_ImplVulkan_MemoryType(VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, req.memoryTypeBits);
- err = vkAllocateMemory(v->Device, &alloc_info, v->Allocator, &backend_tex->Memory);
- check_vk_result(err);
- err = vkBindImageMemory(v->Device, backend_tex->Image, backend_tex->Memory, 0);
- check_vk_result(err);
- }
-
- // Create the Image View:
- {
- VkImageViewCreateInfo info = {};
- info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
- info.image = backend_tex->Image;
- info.viewType = VK_IMAGE_VIEW_TYPE_2D;
- info.format = VK_FORMAT_R8G8B8A8_UNORM;
- info.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
- info.subresourceRange.levelCount = 1;
- info.subresourceRange.layerCount = 1;
- err = vkCreateImageView(v->Device, &info, v->Allocator, &backend_tex->ImageView);
- check_vk_result(err);
- }
-
- // Create the Descriptor Set:
- backend_tex->DescriptorSet = ImGui_ImplVulkan_AddTexture(bd->TexSampler, backend_tex->ImageView, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
-
- // Create the Upload Buffer:
- VkDeviceMemory upload_buffer_memory;
- VkBuffer upload_buffer;
- {
- VkBufferCreateInfo buffer_info = {};
- buffer_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
- buffer_info.size = upload_size;
- buffer_info.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
- buffer_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
- err = vkCreateBuffer(v->Device, &buffer_info, v->Allocator, &upload_buffer);
- check_vk_result(err);
- VkMemoryRequirements req;
- vkGetBufferMemoryRequirements(v->Device, upload_buffer, &req);
- bd->BufferMemoryAlignment = (bd->BufferMemoryAlignment > req.alignment) ? bd->BufferMemoryAlignment : req.alignment;
- VkMemoryAllocateInfo alloc_info = {};
- alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
- alloc_info.allocationSize = IM_MAX(v->MinAllocationSize, req.size);
- alloc_info.memoryTypeIndex = ImGui_ImplVulkan_MemoryType(VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, req.memoryTypeBits);
- err = vkAllocateMemory(v->Device, &alloc_info, v->Allocator, &upload_buffer_memory);
- check_vk_result(err);
- err = vkBindBufferMemory(v->Device, upload_buffer, upload_buffer_memory, 0);
- check_vk_result(err);
- }
-
- // Upload to Buffer:
- {
- char* map = nullptr;
- err = vkMapMemory(v->Device, upload_buffer_memory, 0, upload_size, 0, (void**)(&map));
- check_vk_result(err);
- memcpy(map, pixels, upload_size);
- VkMappedMemoryRange range[1] = {};
- range[0].sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
- range[0].memory = upload_buffer_memory;
- range[0].size = upload_size;
- err = vkFlushMappedMemoryRanges(v->Device, 1, range);
- check_vk_result(err);
- vkUnmapMemory(v->Device, upload_buffer_memory);
- }
-
- // Copy to Image:
- {
- VkImageMemoryBarrier copy_barrier[1] = {};
- copy_barrier[0].sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
- copy_barrier[0].dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
- copy_barrier[0].oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
- copy_barrier[0].newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
- copy_barrier[0].srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
- copy_barrier[0].dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
- copy_barrier[0].image = backend_tex->Image;
- copy_barrier[0].subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
- copy_barrier[0].subresourceRange.levelCount = 1;
- copy_barrier[0].subresourceRange.layerCount = 1;
- vkCmdPipelineBarrier(bd->TexCommandBuffer, VK_PIPELINE_STAGE_HOST_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr, 1, copy_barrier);
-
- VkBufferImageCopy region = {};
- region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
- region.imageSubresource.layerCount = 1;
- region.imageExtent.width = width;
- region.imageExtent.height = height;
- region.imageExtent.depth = 1;
- vkCmdCopyBufferToImage(bd->TexCommandBuffer, upload_buffer, backend_tex->Image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ®ion);
-
- VkImageMemoryBarrier use_barrier[1] = {};
- use_barrier[0].sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
- use_barrier[0].srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
- use_barrier[0].dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
- use_barrier[0].oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
- use_barrier[0].newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
- use_barrier[0].srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
- use_barrier[0].dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
- use_barrier[0].image = backend_tex->Image;
- use_barrier[0].subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
- use_barrier[0].subresourceRange.levelCount = 1;
- use_barrier[0].subresourceRange.layerCount = 1;
- vkCmdPipelineBarrier(bd->TexCommandBuffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, 0, 0, nullptr, 0, nullptr, 1, use_barrier);
- }
-
- // Store our identifier
- io.Fonts->SetTexID((ImTextureID)backend_tex->DescriptorSet);
-
- // End command buffer
- VkSubmitInfo end_info = {};
- end_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
- end_info.commandBufferCount = 1;
- end_info.pCommandBuffers = &bd->TexCommandBuffer;
- err = vkEndCommandBuffer(bd->TexCommandBuffer);
- check_vk_result(err);
- err = vkQueueSubmit(v->Queue, 1, &end_info, VK_NULL_HANDLE);
- check_vk_result(err);
-
- err = vkQueueWaitIdle(v->Queue);
- check_vk_result(err);
-
- vkDestroyBuffer(v->Device, upload_buffer, v->Allocator);
- vkFreeMemory(v->Device, upload_buffer_memory, v->Allocator);
-
- return true;
-}
-
-// You probably never need to call this, as it is called by ImGui_ImplVulkan_CreateFontsTexture() and ImGui_ImplVulkan_Shutdown().
-void ImGui_ImplVulkan_DestroyFontsTexture()
-{
- ImGuiIO& io = ImGui::GetIO();
- ImGui_ImplVulkan_Data* bd = ImGui_ImplVulkan_GetBackendData();
- ImGui_ImplVulkan_InitInfo* v = &bd->VulkanInitInfo;
-
- ImGui_ImplVulkan_Texture* backend_tex = &bd->FontTexture;
-
- if (backend_tex->DescriptorSet)
- {
- ImGui_ImplVulkan_RemoveTexture(backend_tex->DescriptorSet);
- backend_tex->DescriptorSet = VK_NULL_HANDLE;
- io.Fonts->SetTexID(0);
- }
- if (backend_tex->ImageView) { vkDestroyImageView(v->Device, backend_tex->ImageView, v->Allocator); backend_tex->ImageView = VK_NULL_HANDLE; }
- if (backend_tex->Image) { vkDestroyImage(v->Device, backend_tex->Image, v->Allocator); backend_tex->Image = VK_NULL_HANDLE; }
- if (backend_tex->Memory) { vkFreeMemory(v->Device, backend_tex->Memory, v->Allocator); backend_tex->Memory = VK_NULL_HANDLE; }
+ if (tex->Status == ImTextureStatus_WantDestroy && tex->UnusedFrames >= (int)bd->VulkanInitInfo.ImageCount)
+ ImGui_ImplVulkan_DestroyTexture(tex);
}
static void ImGui_ImplVulkan_CreateShaderModules(VkDevice device, const VkAllocationCallbacks* allocator)
{
- // Create the shader modules
ImGui_ImplVulkan_Data* bd = ImGui_ImplVulkan_GetBackendData();
+ ImGui_ImplVulkan_InitInfo* v = &bd->VulkanInitInfo;
if (bd->ShaderModuleVert == VK_NULL_HANDLE)
{
- VkShaderModuleCreateInfo vert_info = {};
- vert_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
- vert_info.codeSize = sizeof(__glsl_shader_vert_spv);
- vert_info.pCode = (uint32_t*)__glsl_shader_vert_spv;
- VkResult err = vkCreateShaderModule(device, &vert_info, allocator, &bd->ShaderModuleVert);
+ VkShaderModuleCreateInfo default_vert_info = {};
+ default_vert_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
+ default_vert_info.codeSize = sizeof(__glsl_shader_vert_spv);
+ default_vert_info.pCode = (uint32_t*)__glsl_shader_vert_spv;
+ VkShaderModuleCreateInfo* p_vert_info = (v->CustomShaderVertCreateInfo.sType == VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO) ? &v->CustomShaderVertCreateInfo : &default_vert_info;
+ VkResult err = vkCreateShaderModule(device, p_vert_info, allocator, &bd->ShaderModuleVert);
check_vk_result(err);
}
if (bd->ShaderModuleFrag == VK_NULL_HANDLE)
{
- VkShaderModuleCreateInfo frag_info = {};
- frag_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
- frag_info.codeSize = sizeof(__glsl_shader_frag_spv);
- frag_info.pCode = (uint32_t*)__glsl_shader_frag_spv;
- VkResult err = vkCreateShaderModule(device, &frag_info, allocator, &bd->ShaderModuleFrag);
+ VkShaderModuleCreateInfo default_frag_info = {};
+ default_frag_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
+ default_frag_info.codeSize = sizeof(__glsl_shader_frag_spv);
+ default_frag_info.pCode = (uint32_t*)__glsl_shader_frag_spv;
+ VkShaderModuleCreateInfo* p_frag_info = (v->CustomShaderFragCreateInfo.sType == VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO) ? &v->CustomShaderFragCreateInfo : &default_frag_info;
+ VkResult err = vkCreateShaderModule(device, p_frag_info, allocator, &bd->ShaderModuleFrag);
check_vk_result(err);
}
}
-static void ImGui_ImplVulkan_CreatePipeline(VkDevice device, const VkAllocationCallbacks* allocator, VkPipelineCache pipelineCache, VkRenderPass renderPass, VkSampleCountFlagBits MSAASamples, VkPipeline* pipeline, uint32_t subpass)
+#if !defined(IMGUI_IMPL_VULKAN_HAS_DYNAMIC_RENDERING) && !(defined(VK_VERSION_1_3) || defined(VK_KHR_dynamic_rendering))
+typedef void VkPipelineRenderingCreateInfoKHR;
+#endif
+
+static VkPipeline ImGui_ImplVulkan_CreatePipeline(VkDevice device, const VkAllocationCallbacks* allocator, VkPipelineCache pipelineCache, const ImGui_ImplVulkan_PipelineInfo* info)
{
ImGui_ImplVulkan_Data* bd = ImGui_ImplVulkan_GetBackendData();
ImGui_ImplVulkan_CreateShaderModules(device, allocator);
@@ -961,7 +1007,7 @@ static void ImGui_ImplVulkan_CreatePipeline(VkDevice device, const VkAllocationC
VkPipelineMultisampleStateCreateInfo ms_info = {};
ms_info.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
- ms_info.rasterizationSamples = (MSAASamples != 0) ? MSAASamples : VK_SAMPLE_COUNT_1_BIT;
+ ms_info.rasterizationSamples = (info->MSAASamples != 0) ? info->MSAASamples : VK_SAMPLE_COUNT_1_BIT;
VkPipelineColorBlendAttachmentState color_attachment[1] = {};
color_attachment[0].blendEnable = VK_TRUE;
@@ -987,35 +1033,36 @@ static void ImGui_ImplVulkan_CreatePipeline(VkDevice device, const VkAllocationC
dynamic_state.dynamicStateCount = (uint32_t)IM_ARRAYSIZE(dynamic_states);
dynamic_state.pDynamicStates = dynamic_states;
- VkGraphicsPipelineCreateInfo info = {};
- info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
- info.flags = bd->PipelineCreateFlags;
- info.stageCount = 2;
- info.pStages = stage;
- info.pVertexInputState = &vertex_info;
- info.pInputAssemblyState = &ia_info;
- info.pViewportState = &viewport_info;
- info.pRasterizationState = &raster_info;
- info.pMultisampleState = &ms_info;
- info.pDepthStencilState = &depth_info;
- info.pColorBlendState = &blend_info;
- info.pDynamicState = &dynamic_state;
- info.layout = bd->PipelineLayout;
- info.renderPass = renderPass;
- info.subpass = subpass;
+ VkGraphicsPipelineCreateInfo create_info = {};
+ create_info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
+ create_info.flags = bd->PipelineCreateFlags;
+ create_info.stageCount = 2;
+ create_info.pStages = stage;
+ create_info.pVertexInputState = &vertex_info;
+ create_info.pInputAssemblyState = &ia_info;
+ create_info.pViewportState = &viewport_info;
+ create_info.pRasterizationState = &raster_info;
+ create_info.pMultisampleState = &ms_info;
+ create_info.pDepthStencilState = &depth_info;
+ create_info.pColorBlendState = &blend_info;
+ create_info.pDynamicState = &dynamic_state;
+ create_info.layout = bd->PipelineLayout;
+ create_info.renderPass = info->RenderPass;
+ create_info.subpass = info->Subpass;
#ifdef IMGUI_IMPL_VULKAN_HAS_DYNAMIC_RENDERING
if (bd->VulkanInitInfo.UseDynamicRendering)
{
- IM_ASSERT(bd->VulkanInitInfo.PipelineRenderingCreateInfo.sType == VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO_KHR && "PipelineRenderingCreateInfo sType must be VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO_KHR");
- IM_ASSERT(bd->VulkanInitInfo.PipelineRenderingCreateInfo.pNext == nullptr && "PipelineRenderingCreateInfo pNext must be nullptr");
- info.pNext = &bd->VulkanInitInfo.PipelineRenderingCreateInfo;
- info.renderPass = VK_NULL_HANDLE; // Just make sure it's actually nullptr.
+ IM_ASSERT(info->PipelineRenderingCreateInfo.sType == VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO_KHR && "PipelineRenderingCreateInfo::sType must be VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO_KHR");
+ IM_ASSERT(info->PipelineRenderingCreateInfo.pNext == nullptr && "PipelineRenderingCreateInfo::pNext must be nullptr");
+ create_info.pNext = &info->PipelineRenderingCreateInfo;
+ create_info.renderPass = VK_NULL_HANDLE; // Just make sure it's actually nullptr.
}
#endif
-
- VkResult err = vkCreateGraphicsPipelines(device, pipelineCache, 1, &info, allocator, pipeline);
+ VkPipeline pipeline;
+ VkResult err = vkCreateGraphicsPipelines(device, pipelineCache, 1, &create_info, allocator, &pipeline);
check_vk_result(err);
+ return pipeline;
}
bool ImGui_ImplVulkan_CreateDeviceObjects()
@@ -1024,7 +1071,7 @@ bool ImGui_ImplVulkan_CreateDeviceObjects()
ImGui_ImplVulkan_InitInfo* v = &bd->VulkanInitInfo;
VkResult err;
- if (!bd->TexSampler)
+ if (!bd->TexSamplerLinear)
{
// Bilinear sampling is required by default. Set 'io.Fonts->Flags |= ImFontAtlasFlags_NoBakedLines' or 'style.AntiAliasedLinesUseTex = false' to allow point/nearest sampling.
VkSamplerCreateInfo info = {};
@@ -1038,7 +1085,7 @@ bool ImGui_ImplVulkan_CreateDeviceObjects()
info.minLod = -1000;
info.maxLod = 1000;
info.maxAnisotropy = 1.0f;
- err = vkCreateSampler(v->Device, &info, v->Allocator, &bd->TexSampler);
+ err = vkCreateSampler(v->Device, &info, v->Allocator, &bd->TexSamplerLinear);
check_vk_result(err);
}
@@ -1058,7 +1105,7 @@ bool ImGui_ImplVulkan_CreateDeviceObjects()
if (v->DescriptorPoolSize != 0)
{
- IM_ASSERT(v->DescriptorPoolSize > IMGUI_IMPL_VULKAN_MINIMUM_IMAGE_SAMPLER_POOL_SIZE);
+ IM_ASSERT(v->DescriptorPoolSize >= IMGUI_IMPL_VULKAN_MINIMUM_IMAGE_SAMPLER_POOL_SIZE);
VkDescriptorPoolSize pool_size = { VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, v->DescriptorPoolSize };
VkDescriptorPoolCreateInfo pool_info = {};
pool_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
@@ -1089,21 +1136,79 @@ bool ImGui_ImplVulkan_CreateDeviceObjects()
check_vk_result(err);
}
- ImGui_ImplVulkan_CreatePipeline(v->Device, v->Allocator, v->PipelineCache, v->RenderPass, v->MSAASamples, &bd->Pipeline, v->Subpass);
+ // Create pipeline
+ bool create_main_pipeline = (v->PipelineInfoMain.RenderPass != VK_NULL_HANDLE);
+#ifdef IMGUI_IMPL_VULKAN_HAS_DYNAMIC_RENDERING
+ create_main_pipeline |= (v->UseDynamicRendering && v->PipelineInfoMain.PipelineRenderingCreateInfo.sType == VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO_KHR);
+#endif
+ if (create_main_pipeline)
+ ImGui_ImplVulkan_CreateMainPipeline(&v->PipelineInfoMain);
+
+ // Create command pool/buffer for texture upload
+ if (!bd->TexCommandPool)
+ {
+ VkCommandPoolCreateInfo info = {};
+ info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
+ info.flags = 0;
+ info.queueFamilyIndex = v->QueueFamily;
+ err = vkCreateCommandPool(v->Device, &info, v->Allocator, &bd->TexCommandPool);
+ check_vk_result(err);
+ }
+ if (!bd->TexCommandBuffer)
+ {
+ VkCommandBufferAllocateInfo info = {};
+ info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
+ info.commandPool = bd->TexCommandPool;
+ info.commandBufferCount = 1;
+ err = vkAllocateCommandBuffers(v->Device, &info, &bd->TexCommandBuffer);
+ check_vk_result(err);
+ }
return true;
}
+void ImGui_ImplVulkan_CreateMainPipeline(const ImGui_ImplVulkan_PipelineInfo* pipeline_info_in)
+{
+ ImGui_ImplVulkan_Data* bd = ImGui_ImplVulkan_GetBackendData();
+ ImGui_ImplVulkan_InitInfo* v = &bd->VulkanInitInfo;
+ if (bd->Pipeline)
+ {
+ vkDestroyPipeline(v->Device, bd->Pipeline, v->Allocator);
+ bd->Pipeline = VK_NULL_HANDLE;
+ }
+ ImGui_ImplVulkan_PipelineInfo* pipeline_info = &v->PipelineInfoMain;
+ if (pipeline_info != pipeline_info_in)
+ *pipeline_info = *pipeline_info_in;
+
+#ifdef IMGUI_IMPL_VULKAN_HAS_DYNAMIC_RENDERING
+ VkPipelineRenderingCreateInfoKHR* pipeline_rendering_create_info = &pipeline_info->PipelineRenderingCreateInfo;
+ if (v->UseDynamicRendering && pipeline_rendering_create_info->pColorAttachmentFormats != NULL)
+ {
+ // Deep copy buffer to reduce error-rate for end user (#8282)
+ ImVector formats;
+ formats.resize((int)pipeline_rendering_create_info->colorAttachmentCount);
+ memcpy(formats.Data, pipeline_rendering_create_info->pColorAttachmentFormats, (size_t)formats.size_in_bytes());
+ formats.swap(bd->PipelineRenderingCreateInfoColorAttachmentFormats);
+ pipeline_rendering_create_info->pColorAttachmentFormats = bd->PipelineRenderingCreateInfoColorAttachmentFormats.Data;
+ }
+#endif
+ bd->Pipeline = ImGui_ImplVulkan_CreatePipeline(v->Device, v->Allocator, v->PipelineCache, pipeline_info);
+}
+
void ImGui_ImplVulkan_DestroyDeviceObjects()
{
ImGui_ImplVulkan_Data* bd = ImGui_ImplVulkan_GetBackendData();
ImGui_ImplVulkan_InitInfo* v = &bd->VulkanInitInfo;
ImGui_ImplVulkanH_DestroyAllViewportsRenderBuffers(v->Device, v->Allocator);
- ImGui_ImplVulkan_DestroyFontsTexture();
+
+ // Destroy all textures
+ for (ImTextureData* tex : ImGui::GetPlatformIO().Textures)
+ if (tex->RefCount == 1)
+ ImGui_ImplVulkan_DestroyTexture(tex);
if (bd->TexCommandBuffer) { vkFreeCommandBuffers(v->Device, bd->TexCommandPool, 1, &bd->TexCommandBuffer); bd->TexCommandBuffer = VK_NULL_HANDLE; }
if (bd->TexCommandPool) { vkDestroyCommandPool(v->Device, bd->TexCommandPool, v->Allocator); bd->TexCommandPool = VK_NULL_HANDLE; }
- if (bd->TexSampler) { vkDestroySampler(v->Device, bd->TexSampler, v->Allocator); bd->TexSampler = VK_NULL_HANDLE; }
+ if (bd->TexSamplerLinear) { vkDestroySampler(v->Device, bd->TexSamplerLinear, v->Allocator); bd->TexSamplerLinear = VK_NULL_HANDLE; }
if (bd->ShaderModuleVert) { vkDestroyShaderModule(v->Device, bd->ShaderModuleVert, v->Allocator); bd->ShaderModuleVert = VK_NULL_HANDLE; }
if (bd->ShaderModuleFrag) { vkDestroyShaderModule(v->Device, bd->ShaderModuleFrag, v->Allocator); bd->ShaderModuleFrag = VK_NULL_HANDLE; }
if (bd->DescriptorSetLayout) { vkDestroyDescriptorSetLayout(v->Device, bd->DescriptorSetLayout, v->Allocator); bd->DescriptorSetLayout = VK_NULL_HANDLE; }
@@ -1116,9 +1221,19 @@ void ImGui_ImplVulkan_DestroyDeviceObjects()
#ifdef IMGUI_IMPL_VULKAN_HAS_DYNAMIC_RENDERING
static void ImGui_ImplVulkan_LoadDynamicRenderingFunctions(uint32_t api_version, PFN_vkVoidFunction(*loader_func)(const char* function_name, void* user_data), void* user_data)
{
- // Manually load those two (see #5446, #8326, #8365)
- ImGuiImplVulkanFuncs_vkCmdBeginRenderingKHR = reinterpret_cast(loader_func(api_version < VK_API_VERSION_1_3 ? "vkCmdBeginRenderingKHR" : "vkCmdBeginRendering", user_data));
- ImGuiImplVulkanFuncs_vkCmdEndRenderingKHR = reinterpret_cast(loader_func(api_version < VK_API_VERSION_1_3 ? "vkCmdEndRenderingKHR" : "vkCmdEndRendering", user_data));
+ IM_UNUSED(api_version);
+
+ // Manually load those two (see #5446, #8326, #8365, #8600)
+ // - Try loading core (non-KHR) versions first (this will work for Vulkan 1.3+ and the device supports dynamic rendering)
+ ImGuiImplVulkanFuncs_vkCmdBeginRenderingKHR = reinterpret_cast(loader_func("vkCmdBeginRendering", user_data));
+ ImGuiImplVulkanFuncs_vkCmdEndRenderingKHR = reinterpret_cast(loader_func("vkCmdEndRendering", user_data));
+
+ // - Fallback to KHR versions if core not available (this will work if KHR extension is available and enabled and also the device supports dynamic rendering)
+ if (ImGuiImplVulkanFuncs_vkCmdBeginRenderingKHR == nullptr || ImGuiImplVulkanFuncs_vkCmdEndRenderingKHR == nullptr)
+ {
+ ImGuiImplVulkanFuncs_vkCmdBeginRenderingKHR = reinterpret_cast(loader_func("vkCmdBeginRenderingKHR", user_data));
+ ImGuiImplVulkanFuncs_vkCmdEndRenderingKHR = reinterpret_cast(loader_func("vkCmdEndRenderingKHR", user_data));
+ }
}
#endif
@@ -1173,7 +1288,7 @@ bool ImGui_ImplVulkan_Init(ImGui_ImplVulkan_InitInfo* info)
{
#ifdef IMGUI_IMPL_VULKAN_HAS_DYNAMIC_RENDERING
#ifndef IMGUI_IMPL_VULKAN_USE_LOADER
- ImGui_ImplVulkan_LoadDynamicRenderingFunctions(info->ApiVersion, [](const char* function_name, void* user_data) { return vkGetInstanceProcAddr((VkInstance)user_data, function_name); }, (void*)info->Instance);
+ ImGui_ImplVulkan_LoadDynamicRenderingFunctions(info->ApiVersion, [](const char* function_name, void* user_data) { return vkGetDeviceProcAddr((VkDevice)user_data, function_name); }, (void*)info->Device);
#endif
IM_ASSERT(ImGuiImplVulkanFuncs_vkCmdBeginRenderingKHR != nullptr);
IM_ASSERT(ImGuiImplVulkanFuncs_vkCmdEndRenderingKHR != nullptr);
@@ -1191,24 +1306,31 @@ bool ImGui_ImplVulkan_Init(ImGui_ImplVulkan_InitInfo* info)
io.BackendRendererUserData = (void*)bd;
io.BackendRendererName = "imgui_impl_vulkan";
io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes.
+ io.BackendFlags |= ImGuiBackendFlags_RendererHasTextures; // We can honor ImGuiPlatformIO::Textures[] requests during render.
io.BackendFlags |= ImGuiBackendFlags_RendererHasViewports; // We can create multi-viewports on the Renderer side (optional)
+ // Sanity checks
IM_ASSERT(info->Instance != VK_NULL_HANDLE);
IM_ASSERT(info->PhysicalDevice != VK_NULL_HANDLE);
IM_ASSERT(info->Device != VK_NULL_HANDLE);
IM_ASSERT(info->Queue != VK_NULL_HANDLE);
+ IM_ASSERT(info->MinImageCount >= 2);
+ IM_ASSERT(info->ImageCount >= info->MinImageCount);
if (info->DescriptorPool != VK_NULL_HANDLE) // Either DescriptorPool or DescriptorPoolSize must be set, not both!
IM_ASSERT(info->DescriptorPoolSize == 0);
else
IM_ASSERT(info->DescriptorPoolSize > 0);
- IM_ASSERT(info->MinImageCount >= 2);
- IM_ASSERT(info->ImageCount >= info->MinImageCount);
- if (info->UseDynamicRendering == false)
- IM_ASSERT(info->RenderPass != VK_NULL_HANDLE);
+ if (info->UseDynamicRendering)
+ IM_ASSERT(info->PipelineInfoMain.RenderPass == VK_NULL_HANDLE && info->PipelineInfoForViewports.RenderPass == VK_NULL_HANDLE);
bd->VulkanInitInfo = *info;
- ImGui_ImplVulkan_CreateDeviceObjects();
+ VkPhysicalDeviceProperties properties;
+ vkGetPhysicalDeviceProperties(info->PhysicalDevice, &properties);
+ bd->NonCoherentAtomSize = properties.limits.nonCoherentAtomSize;
+
+ if (!ImGui_ImplVulkan_CreateDeviceObjects())
+ IM_ASSERT(0 && "ImGui_ImplVulkan_CreateDeviceObjects() failed!"); // <- Can't be hit yet.
// Our render function expect RendererUserData to be storing the window render buffer we need (for the main viewport we won't use ->Window)
ImGuiViewport* main_viewport = ImGui::GetMainViewport();
@@ -1224,6 +1346,7 @@ void ImGui_ImplVulkan_Shutdown()
ImGui_ImplVulkan_Data* bd = ImGui_ImplVulkan_GetBackendData();
IM_ASSERT(bd != nullptr && "No renderer backend to shutdown, or already shutdown?");
ImGuiIO& io = ImGui::GetIO();
+ ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
// First destroy objects in all viewports
ImGui_ImplVulkan_DestroyDeviceObjects();
@@ -1239,7 +1362,8 @@ void ImGui_ImplVulkan_Shutdown()
io.BackendRendererName = nullptr;
io.BackendRendererUserData = nullptr;
- io.BackendFlags &= ~(ImGuiBackendFlags_RendererHasVtxOffset | ImGuiBackendFlags_RendererHasViewports);
+ io.BackendFlags &= ~(ImGuiBackendFlags_RendererHasVtxOffset | ImGuiBackendFlags_RendererHasTextures | ImGuiBackendFlags_RendererHasViewports);
+ platform_io.ClearRendererHandlers();
IM_DELETE(bd);
}
@@ -1247,9 +1371,7 @@ void ImGui_ImplVulkan_NewFrame()
{
ImGui_ImplVulkan_Data* bd = ImGui_ImplVulkan_GetBackendData();
IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplVulkan_Init()?");
-
- if (!bd->FontTexture.DescriptorSet)
- ImGui_ImplVulkan_CreateFontsTexture();
+ IM_UNUSED(bd);
}
void ImGui_ImplVulkan_SetMinImageCount(uint32_t min_image_count)
@@ -1334,7 +1456,7 @@ void ImGui_ImplVulkan_DestroyWindowRenderBuffers(VkDevice device, ImGui_ImplVulk
//-------------------------------------------------------------------------
// Internal / Miscellaneous Vulkan Helpers
-// (Used by example's main.cpp. Used by multi-viewport features. PROBABLY NOT used by your own app.)
+// (Used by example's main.cpp. Used by multi-viewport features. PROBABLY NOT used by your own engine/app.)
//-------------------------------------------------------------------------
// You probably do NOT need to use or care about those functions.
// Those functions only exist because:
@@ -1344,7 +1466,7 @@ void ImGui_ImplVulkan_DestroyWindowRenderBuffers(VkDevice device, ImGui_ImplVulk
// but it is too much code to duplicate everywhere so we exceptionally expose them.
//
// Your engine/app will likely _already_ have code to setup all that stuff (swap chain, render pass, frame buffers, etc.).
-// You may read this code to learn about Vulkan, but it is recommended you use you own custom tailored code to do equivalent work.
+// You may read this code to learn about Vulkan, but it is recommended you use your own custom tailored code to do equivalent work.
// (The ImGui_ImplVulkanH_XXX functions do not interact with any of the state used by the regular ImGui_ImplVulkan_XXX functions)
//-------------------------------------------------------------------------
@@ -1522,7 +1644,7 @@ int ImGui_ImplVulkanH_GetMinImageCountFromPresentMode(VkPresentModeKHR present_m
}
// Also destroy old swap chain and in-flight frames data, if any.
-void ImGui_ImplVulkanH_CreateWindowSwapChain(VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_Window* wd, const VkAllocationCallbacks* allocator, int w, int h, uint32_t min_image_count)
+void ImGui_ImplVulkanH_CreateWindowSwapChain(VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_Window* wd, const VkAllocationCallbacks* allocator, int w, int h, uint32_t min_image_count, VkImageUsageFlags image_usage)
{
VkResult err;
VkSwapchainKHR old_swapchain = wd->Swapchain;
@@ -1559,7 +1681,7 @@ void ImGui_ImplVulkanH_CreateWindowSwapChain(VkPhysicalDevice physical_device, V
info.imageFormat = wd->SurfaceFormat.format;
info.imageColorSpace = wd->SurfaceFormat.colorSpace;
info.imageArrayLayers = 1;
- info.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
+ info.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | image_usage;
info.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE; // Assume that graphics family == present family
info.preTransform = (cap.supportedTransforms & VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) ? VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR : cap.currentTransform;
info.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
@@ -1687,13 +1809,89 @@ void ImGui_ImplVulkanH_CreateWindowSwapChain(VkPhysicalDevice physical_device, V
}
// Create or resize window
-void ImGui_ImplVulkanH_CreateOrResizeWindow(VkInstance instance, VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_Window* wd, uint32_t queue_family, const VkAllocationCallbacks* allocator, int width, int height, uint32_t min_image_count)
+// - 2025/09/26: v1.92.4 added a trailing 'VkImageUsageFlags image_usage' parameter which is usually VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT.
+void ImGui_ImplVulkanH_CreateOrResizeWindow(VkInstance instance, VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_Window* wd, uint32_t queue_family, const VkAllocationCallbacks* allocator, int width, int height, uint32_t min_image_count, VkImageUsageFlags image_usage)
{
IM_ASSERT(g_FunctionsLoaded && "Need to call ImGui_ImplVulkan_LoadFunctions() if IMGUI_IMPL_VULKAN_NO_PROTOTYPES or VK_NO_PROTOTYPES are set!");
(void)instance;
- ImGui_ImplVulkanH_CreateWindowSwapChain(physical_device, device, wd, allocator, width, height, min_image_count);
- //ImGui_ImplVulkan_CreatePipeline(device, allocator, VK_NULL_HANDLE, wd->RenderPass, VK_SAMPLE_COUNT_1_BIT, &wd->Pipeline, g_VulkanInitInfo.Subpass);
+ ImGui_ImplVulkanH_CreateWindowSwapChain(physical_device, device, wd, allocator, width, height, min_image_count, image_usage);
ImGui_ImplVulkanH_CreateWindowCommandBuffers(physical_device, device, wd, queue_family, allocator);
+
+ // FIXME: to submit the command buffer, we need a queue. In the examples folder, the ImGui_ImplVulkanH_CreateOrResizeWindow function is called
+ // before the ImGui_ImplVulkan_Init function, so we don't have access to the queue yet. Here we have the queue_family that we can use to grab
+ // a queue from the device and submit the command buffer. It would be better to have access to the queue as suggested in the FIXME below.
+ VkCommandPool command_pool;
+ VkCommandPoolCreateInfo pool_info = {};
+ pool_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
+ pool_info.queueFamilyIndex = queue_family;
+ pool_info.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
+ VkResult err = vkCreateCommandPool(device, &pool_info, allocator, &command_pool);
+ check_vk_result(err);
+
+ VkFenceCreateInfo fence_info = {};
+ fence_info.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
+ VkFence fence;
+ err = vkCreateFence(device, &fence_info, allocator, &fence);
+ check_vk_result(err);
+
+ VkCommandBufferAllocateInfo alloc_info = {};
+ alloc_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
+ alloc_info.commandPool = command_pool;
+ alloc_info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
+ alloc_info.commandBufferCount = 1;
+ VkCommandBuffer command_buffer;
+ err = vkAllocateCommandBuffers(device, &alloc_info, &command_buffer);
+ check_vk_result(err);
+
+ VkCommandBufferBeginInfo begin_info = {};
+ begin_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
+ begin_info.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
+ err = vkBeginCommandBuffer(command_buffer, &begin_info);
+ check_vk_result(err);
+
+ // Transition the images to the correct layout for rendering
+ for (uint32_t i = 0; i < wd->ImageCount; i++)
+ {
+ VkImageMemoryBarrier barrier = {};
+ barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
+ barrier.image = wd->Frames[i].Backbuffer;
+ barrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
+ barrier.newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
+ barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
+ barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
+ barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
+ barrier.subresourceRange.levelCount = 1;
+ barrier.subresourceRange.layerCount = 1;
+ vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, 0, 0, nullptr, 0, nullptr, 1, &barrier);
+ }
+
+ err = vkEndCommandBuffer(command_buffer);
+ check_vk_result(err);
+ VkSubmitInfo submit_info = {};
+ submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
+ submit_info.commandBufferCount = 1;
+ submit_info.pCommandBuffers = &command_buffer;
+
+ VkQueue queue;
+ vkGetDeviceQueue(device, queue_family, 0, &queue);
+ err = vkQueueSubmit(queue, 1, &submit_info, fence);
+ check_vk_result(err);
+ err = vkWaitForFences(device, 1, &fence, VK_TRUE, UINT64_MAX);
+ check_vk_result(err);
+ err = vkResetFences(device, 1, &fence);
+ check_vk_result(err);
+
+ err = vkResetCommandPool(device, command_pool, 0);
+ check_vk_result(err);
+
+ // Destroy command buffer and fence and command pool
+ vkFreeCommandBuffers(device, command_pool, 1, &command_buffer);
+ vkDestroyCommandPool(device, command_pool, allocator);
+ vkDestroyFence(device, fence, allocator);
+ command_pool = VK_NULL_HANDLE;
+ command_buffer = VK_NULL_HANDLE;
+ fence = VK_NULL_HANDLE;
+ queue = VK_NULL_HANDLE;
}
void ImGui_ImplVulkanH_DestroyWindow(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_Window* wd, const VkAllocationCallbacks* allocator)
@@ -1742,6 +1940,12 @@ void ImGui_ImplVulkanH_DestroyAllViewportsRenderBuffers(VkDevice device, const V
ImGui_ImplVulkan_DestroyWindowRenderBuffers(device, &vd->RenderBuffers, allocator);
}
+ImGui_ImplVulkanH_Window* ImGui_ImplVulkanH_GetWindowDataFromViewport(ImGuiViewport* viewport)
+{
+ ImGui_ImplVulkan_ViewportData* vd = (ImGui_ImplVulkan_ViewportData*)viewport->RendererUserData;
+ return vd ? &vd->Window : nullptr;
+}
+
//--------------------------------------------------------------------------------------------------------
// MULTI-VIEWPORT / PLATFORM INTERFACE SUPPORT
// This is an _advanced_ and _optional_ feature, allowing the backend to create and handle multiple viewports simultaneously.
@@ -1771,10 +1975,11 @@ static void ImGui_ImplVulkan_CreateWindow(ImGuiViewport* viewport)
}
// Select Surface Format
+ ImGui_ImplVulkan_PipelineInfo* pipeline_info = &v->PipelineInfoForViewports;
ImVector requestSurfaceImageFormats;
#ifdef IMGUI_IMPL_VULKAN_HAS_DYNAMIC_RENDERING
- for (uint32_t n = 0; n < v->PipelineRenderingCreateInfo.colorAttachmentCount; n++)
- requestSurfaceImageFormats.push_back(v->PipelineRenderingCreateInfo.pColorAttachmentFormats[n]);
+ for (uint32_t n = 0; n < pipeline_info->PipelineRenderingCreateInfo.colorAttachmentCount; n++)
+ requestSurfaceImageFormats.push_back(pipeline_info->PipelineRenderingCreateInfo.pColorAttachmentFormats[n]);
#endif
const VkFormat defaultFormats[] = { VK_FORMAT_B8G8R8A8_UNORM, VK_FORMAT_R8G8B8A8_UNORM, VK_FORMAT_B8G8R8_UNORM, VK_FORMAT_R8G8B8_UNORM };
for (VkFormat format : defaultFormats)
@@ -1792,12 +1997,26 @@ static void ImGui_ImplVulkan_CreateWindow(ImGuiViewport* viewport)
// Create SwapChain, RenderPass, Framebuffer, etc.
wd->ClearEnable = (viewport->Flags & ImGuiViewportFlags_NoRendererClear) ? false : true;
wd->UseDynamicRendering = v->UseDynamicRendering;
- ImGui_ImplVulkanH_CreateOrResizeWindow(v->Instance, v->PhysicalDevice, v->Device, wd, v->QueueFamily, v->Allocator, (int)viewport->Size.x, (int)viewport->Size.y, v->MinImageCount);
+ ImGui_ImplVulkanH_CreateOrResizeWindow(v->Instance, v->PhysicalDevice, v->Device, wd, v->QueueFamily, v->Allocator, (int)viewport->Size.x, (int)viewport->Size.y, v->MinImageCount, pipeline_info->SwapChainImageUsage);
vd->WindowOwned = true;
// Create pipeline (shared by all secondary viewports)
if (bd->PipelineForViewports == VK_NULL_HANDLE)
- ImGui_ImplVulkan_CreatePipeline(v->Device, v->Allocator, VK_NULL_HANDLE, wd->RenderPass, VK_SAMPLE_COUNT_1_BIT, &bd->PipelineForViewports, 0);
+ {
+#ifdef IMGUI_IMPL_VULKAN_HAS_DYNAMIC_RENDERING
+ if (wd->UseDynamicRendering)
+ {
+ pipeline_info->PipelineRenderingCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO;
+ pipeline_info->PipelineRenderingCreateInfo.colorAttachmentCount = 1;
+ pipeline_info->PipelineRenderingCreateInfo.pColorAttachmentFormats = &wd->SurfaceFormat.format;
+ }
+ else
+ {
+ pipeline_info->RenderPass = wd->RenderPass;
+ }
+#endif
+ bd->PipelineForViewports = ImGui_ImplVulkan_CreatePipeline(v->Device, v->Allocator, VK_NULL_HANDLE, &v->PipelineInfoForViewports);
+ }
}
static void ImGui_ImplVulkan_DestroyWindow(ImGuiViewport* viewport)
@@ -1823,7 +2042,7 @@ static void ImGui_ImplVulkan_SetWindowSize(ImGuiViewport* viewport, ImVec2 size)
return;
ImGui_ImplVulkan_InitInfo* v = &bd->VulkanInitInfo;
vd->Window.ClearEnable = (viewport->Flags & ImGuiViewportFlags_NoRendererClear) ? false : true;
- ImGui_ImplVulkanH_CreateOrResizeWindow(v->Instance, v->PhysicalDevice, v->Device, &vd->Window, v->QueueFamily, v->Allocator, (int)size.x, (int)size.y, v->MinImageCount);
+ ImGui_ImplVulkanH_CreateOrResizeWindow(v->Instance, v->PhysicalDevice, v->Device, &vd->Window, v->QueueFamily, v->Allocator, (int)size.x, (int)size.y, v->MinImageCount, v->PipelineInfoForViewports.SwapChainImageUsage);
}
static void ImGui_ImplVulkan_RenderWindow(ImGuiViewport* viewport, void*)
@@ -1836,7 +2055,7 @@ static void ImGui_ImplVulkan_RenderWindow(ImGuiViewport* viewport, void*)
if (vd->SwapChainNeedRebuild || vd->SwapChainSuboptimal)
{
- ImGui_ImplVulkanH_CreateOrResizeWindow(v->Instance, v->PhysicalDevice, v->Device, wd, v->QueueFamily, v->Allocator, (int)viewport->Size.x, (int)viewport->Size.y, v->MinImageCount);
+ ImGui_ImplVulkanH_CreateOrResizeWindow(v->Instance, v->PhysicalDevice, v->Device, wd, v->QueueFamily, v->Allocator, (int)viewport->Size.x, (int)viewport->Size.y, v->MinImageCount, v->PipelineInfoForViewports.SwapChainImageUsage);
vd->SwapChainNeedRebuild = vd->SwapChainSuboptimal = false;
}
@@ -1883,13 +2102,13 @@ static void ImGui_ImplVulkan_RenderWindow(ImGuiViewport* viewport, void*)
VkImageMemoryBarrier barrier = {};
barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
barrier.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
- barrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
+ barrier.oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
barrier.newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
barrier.image = fd->Backbuffer;
barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
barrier.subresourceRange.levelCount = 1;
barrier.subresourceRange.layerCount = 1;
- vkCmdPipelineBarrier(fd->CommandBuffer, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, 0, 0, nullptr, 0, nullptr, 1, &barrier);
+ vkCmdPipelineBarrier(fd->CommandBuffer, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | VK_PIPELINE_STAGE_NONE, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, 0, 0, nullptr, 0, nullptr, 1, &barrier);
VkRenderingAttachmentInfo attachmentInfo = {};
attachmentInfo.sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO_KHR;
@@ -2010,7 +2229,7 @@ static void ImGui_ImplVulkan_SwapBuffers(ImGuiViewport* viewport, void*)
void ImGui_ImplVulkan_InitMultiViewportSupport()
{
ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
- if (ImGui::GetIO().ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
+ if (ImGui::GetIO().BackendFlags & ImGuiBackendFlags_PlatformHasViewports)
IM_ASSERT(platform_io.Platform_CreateVkSurface != nullptr && "Platform needs to setup the CreateVkSurface handler.");
platform_io.Renderer_CreateWindow = ImGui_ImplVulkan_CreateWindow;
platform_io.Renderer_DestroyWindow = ImGui_ImplVulkan_DestroyWindow;
diff --git a/backends/imgui_impl_vulkan.h b/backends/imgui_impl_vulkan.h
index 2bb3d007e..ad89cb9e4 100644
--- a/backends/imgui_impl_vulkan.h
+++ b/backends/imgui_impl_vulkan.h
@@ -2,8 +2,9 @@
// This needs to be used along with a Platform Backend (e.g. GLFW, SDL, Win32, custom..)
// Implemented features:
-// [!] Renderer: User texture binding. Use 'VkDescriptorSet' as ImTextureID. Call ImGui_ImplVulkan_AddTexture() to register one. Read the FAQ about ImTextureID! See https://github.com/ocornut/imgui/pull/914 for discussions.
+// [!] Renderer: User texture binding. Use 'VkDescriptorSet' as texture identifier. Call ImGui_ImplVulkan_AddTexture() to register one. Read the FAQ about ImTextureID/ImTextureRef + https://github.com/ocornut/imgui/pull/914 for discussions.
// [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset).
+// [X] Renderer: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures).
// [X] Renderer: Expose selected render state for draw callbacks to use. Access in '(ImGui_ImplXXXX_RenderState*)GetPlatformIO().Renderer_RenderState'.
// [x] Renderer: Multi-viewport / platform windows. With issues (flickering when creating a new viewport).
@@ -62,9 +63,25 @@
#define IMGUI_IMPL_VULKAN_HAS_DYNAMIC_RENDERING
#endif
-// Current version of the backend use 1 descriptor for the font atlas + as many as additional calls done to ImGui_ImplVulkan_AddTexture().
-// It is expected that as early as Q1 2025 the backend will use a few more descriptors. Use this value + number of desired calls to ImGui_ImplVulkan_AddTexture().
-#define IMGUI_IMPL_VULKAN_MINIMUM_IMAGE_SAMPLER_POOL_SIZE (1) // Minimum per atlas
+// Backend uses a small number of descriptors per font atlas + as many as additional calls done to ImGui_ImplVulkan_AddTexture().
+#define IMGUI_IMPL_VULKAN_MINIMUM_IMAGE_SAMPLER_POOL_SIZE (8) // Minimum per atlas
+
+// Specify settings to create pipeline and swapchain
+struct ImGui_ImplVulkan_PipelineInfo
+{
+ // For Main viewport only
+ VkRenderPass RenderPass; // Ignored if using dynamic rendering
+
+ // For Main and Secondary viewports
+ uint32_t Subpass; //
+ VkSampleCountFlagBits MSAASamples = {}; // 0 defaults to VK_SAMPLE_COUNT_1_BIT
+#ifdef IMGUI_IMPL_VULKAN_HAS_DYNAMIC_RENDERING
+ VkPipelineRenderingCreateInfoKHR PipelineRenderingCreateInfo; // Optional, valid if .sType == VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO_KHR
+#endif
+
+ // For Secondary viewports only (created/managed by backend)
+ VkImageUsageFlags SwapChainImageUsage; // Extra flags for vkCreateSwapchainKHR() calls for secondary viewports. We automatically add VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT. You can add e.g. VK_IMAGE_USAGE_TRANSFER_SRC_BIT if you need to capture from viewports.
+};
// Initialization data, for ImGui_ImplVulkan_Init()
// [Please zero-clear before use!]
@@ -83,29 +100,33 @@ struct ImGui_ImplVulkan_InitInfo
uint32_t QueueFamily;
VkQueue Queue;
VkDescriptorPool DescriptorPool; // See requirements in note above; ignored if using DescriptorPoolSize > 0
- VkRenderPass RenderPass; // Ignored if using dynamic rendering
+ uint32_t DescriptorPoolSize; // Optional: set to create internal descriptor pool automatically instead of using DescriptorPool.
uint32_t MinImageCount; // >= 2
uint32_t ImageCount; // >= MinImageCount
- VkSampleCountFlagBits MSAASamples; // 0 defaults to VK_SAMPLE_COUNT_1_BIT
+ VkPipelineCache PipelineCache; // Optional
- // (Optional)
- VkPipelineCache PipelineCache;
- uint32_t Subpass;
-
- // (Optional) Set to create internal descriptor pool instead of using DescriptorPool
- uint32_t DescriptorPoolSize;
+ // Pipeline
+ ImGui_ImplVulkan_PipelineInfo PipelineInfoMain; // Infos for Main Viewport (created by app/user)
+ ImGui_ImplVulkan_PipelineInfo PipelineInfoForViewports; // Infos for Secondary Viewports (created by backend)
+ //VkRenderPass RenderPass; // --> Since 2025/09/26: set 'PipelineInfoMain.RenderPass' instead
+ //uint32_t Subpass; // --> Since 2025/09/26: set 'PipelineInfoMain.Subpass' instead
+ //VkSampleCountFlagBits MSAASamples; // --> Since 2025/09/26: set 'PipelineInfoMain.MSAASamples' instead
+ //VkPipelineRenderingCreateInfoKHR PipelineRenderingCreateInfo; // Since 2025/09/26: set 'PipelineInfoMain.PipelineRenderingCreateInfo' instead
// (Optional) Dynamic Rendering
- // Need to explicitly enable VK_KHR_dynamic_rendering extension to use this, even for Vulkan 1.3.
+ // Need to explicitly enable VK_KHR_dynamic_rendering extension to use this, even for Vulkan 1.3 + setup PipelineInfoMain.PipelineRenderingCreateInfo and PipelineInfoViewports.PipelineRenderingCreateInfo.
bool UseDynamicRendering;
-#ifdef IMGUI_IMPL_VULKAN_HAS_DYNAMIC_RENDERING
- VkPipelineRenderingCreateInfoKHR PipelineRenderingCreateInfo;
-#endif
// (Optional) Allocation, Debugging
const VkAllocationCallbacks* Allocator;
void (*CheckVkResultFn)(VkResult err);
VkDeviceSize MinAllocationSize; // Minimum allocation size. Set to 1024*1024 to satisfy zealous best practices validation layer and waste a little memory.
+
+ // (Optional) Customize default vertex/fragment shaders.
+ // - if .sType == VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO we use specified structs, otherwise we use defaults.
+ // - Shader inputs/outputs need to match ours. Code/data pointed to by the structure needs to survive for whole during of backend usage.
+ VkShaderModuleCreateInfo CustomShaderVertCreateInfo;
+ VkShaderModuleCreateInfo CustomShaderFragCreateInfo;
};
// Follow "Getting Started" link and check examples/ folder to learn about using backends!
@@ -113,10 +134,16 @@ IMGUI_IMPL_API bool ImGui_ImplVulkan_Init(ImGui_ImplVulkan_InitInfo*
IMGUI_IMPL_API void ImGui_ImplVulkan_Shutdown();
IMGUI_IMPL_API void ImGui_ImplVulkan_NewFrame();
IMGUI_IMPL_API void ImGui_ImplVulkan_RenderDrawData(ImDrawData* draw_data, VkCommandBuffer command_buffer, VkPipeline pipeline = VK_NULL_HANDLE);
-IMGUI_IMPL_API bool ImGui_ImplVulkan_CreateFontsTexture();
-IMGUI_IMPL_API void ImGui_ImplVulkan_DestroyFontsTexture();
IMGUI_IMPL_API void ImGui_ImplVulkan_SetMinImageCount(uint32_t min_image_count); // To override MinImageCount after initialization (e.g. if swap chain is recreated)
+// (Advanced) Use e.g. if you need to recreate pipeline without reinitializing the backend (see #8110, #8111)
+// The main window pipeline will be created by ImGui_ImplVulkan_Init() if possible (== RenderPass xor (UseDynamicRendering && PipelineRenderingCreateInfo->sType == VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO_KHR))
+// Else, the pipeline can be created, or re-created, using ImGui_ImplVulkan_CreateMainPipeline() before rendering.
+IMGUI_IMPL_API void ImGui_ImplVulkan_CreateMainPipeline(const ImGui_ImplVulkan_PipelineInfo* info);
+
+// (Advanced) Use e.g. if you need to precisely control the timing of texture updates (e.g. for staged rendering), by setting ImDrawData::Textures = NULL to handle this manually.
+IMGUI_IMPL_API void ImGui_ImplVulkan_UpdateTexture(ImTextureData* tex);
+
// Register a texture (VkDescriptorSet == ImTextureID)
// FIXME: This is experimental in the sense that we are unsure how to best design/tackle this problem
// Please post to https://github.com/ocornut/imgui/pull/914 if you have suggestions.
@@ -151,25 +178,26 @@ struct ImGui_ImplVulkan_RenderState
//
// Your engine/app will likely _already_ have code to setup all that stuff (swap chain,
// render pass, frame buffers, etc.). You may read this code if you are curious, but
-// it is recommended you use you own custom tailored code to do equivalent work.
+// it is recommended you use your own custom tailored code to do equivalent work.
//
// We don't provide a strong guarantee that we won't change those functions API.
//
// The ImGui_ImplVulkanH_XXX functions should NOT interact with any of the state used
-// by the regular ImGui_ImplVulkan_XXX functions).
+// by the regular ImGui_ImplVulkan_XXX functions.
//-------------------------------------------------------------------------
struct ImGui_ImplVulkanH_Frame;
struct ImGui_ImplVulkanH_Window;
// Helpers
-IMGUI_IMPL_API void ImGui_ImplVulkanH_CreateOrResizeWindow(VkInstance instance, VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_Window* wd, uint32_t queue_family, const VkAllocationCallbacks* allocator, int w, int h, uint32_t min_image_count);
+IMGUI_IMPL_API void ImGui_ImplVulkanH_CreateOrResizeWindow(VkInstance instance, VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_Window* wd, uint32_t queue_family, const VkAllocationCallbacks* allocator, int w, int h, uint32_t min_image_count, VkImageUsageFlags image_usage);
IMGUI_IMPL_API void ImGui_ImplVulkanH_DestroyWindow(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_Window* wd, const VkAllocationCallbacks* allocator);
IMGUI_IMPL_API VkSurfaceFormatKHR ImGui_ImplVulkanH_SelectSurfaceFormat(VkPhysicalDevice physical_device, VkSurfaceKHR surface, const VkFormat* request_formats, int request_formats_count, VkColorSpaceKHR request_color_space);
IMGUI_IMPL_API VkPresentModeKHR ImGui_ImplVulkanH_SelectPresentMode(VkPhysicalDevice physical_device, VkSurfaceKHR surface, const VkPresentModeKHR* request_modes, int request_modes_count);
IMGUI_IMPL_API VkPhysicalDevice ImGui_ImplVulkanH_SelectPhysicalDevice(VkInstance instance);
IMGUI_IMPL_API uint32_t ImGui_ImplVulkanH_SelectQueueFamilyIndex(VkPhysicalDevice physical_device);
IMGUI_IMPL_API int ImGui_ImplVulkanH_GetMinImageCountFromPresentMode(VkPresentModeKHR present_mode);
+IMGUI_IMPL_API ImGui_ImplVulkanH_Window* ImGui_ImplVulkanH_GetWindowDataFromViewport(ImGuiViewport* viewport); // Access to Vulkan objects associated with a viewport (e.g to export a screenshot)
// Helper structure to hold the data needed by one rendering frame
// (Used by example's main.cpp. Used by multi-viewport features. Probably NOT used by your own engine/app.)
diff --git a/backends/imgui_impl_wgpu.cpp b/backends/imgui_impl_wgpu.cpp
index 5072e96e2..668509052 100644
--- a/backends/imgui_impl_wgpu.cpp
+++ b/backends/imgui_impl_wgpu.cpp
@@ -3,11 +3,12 @@
// (Please note that WebGPU is currently experimental, will not run on non-beta browsers, and may break.)
// Implemented features:
-// [X] Renderer: User texture binding. Use 'WGPUTextureView' as ImTextureID. Read the FAQ about ImTextureID!
+// [X] Renderer: User texture binding. Use 'WGPUTextureView' as ImTextureID. Read the FAQ about ImTextureID/ImTextureRef!
// [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset).
// [X] Renderer: Expose selected render state for draw callbacks to use. Access in '(ImGui_ImplXXXX_RenderState*)GetPlatformIO().Renderer_RenderState'.
-// Missing features:
-// [ ] Renderer: Multi-viewport support (multiple windows). Not meaningful on the web.
+// [X] Renderer: Texture updates support for dynamic font system (ImGuiBackendFlags_RendererHasTextures).
+// Missing features or Issues:
+// [ ] Renderer: Multi-viewport support (multiple windows), useful for desktop.
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
@@ -19,6 +20,8 @@
// CHANGELOG
// (minor and older changes stripped away, please see git history for details)
+// 2025-09-18: Call platform_io.ClearRendererHandlers() on shutdown.
+// 2025-06-12: Added support for ImGuiBackendFlags_RendererHasTextures, for dynamic font atlas. (#8465)
// 2025-02-26: Recreate image bind groups during render. (#8426, #8046, #7765, #8027) + Update for latest webgpu-native changes.
// 2024-10-14: Update Dawn support for change of string usages. (#8082, #8083)
// 2024-10-07: Expose selected render state in ImGui_ImplWGPU_RenderState, which you can access in 'void* platform_io.Renderer_RenderState' during draw callbacks.
@@ -73,11 +76,15 @@ extern ImGuiID ImHashData(const void* data_p, size_t data_size, ImU32 seed);
#define MEMALIGN(_SIZE,_ALIGN) (((_SIZE) + ((_ALIGN) - 1)) & ~((_ALIGN) - 1)) // Memory align (copied from IM_ALIGN() macro).
// WebGPU data
+struct ImGui_ImplWGPU_Texture
+{
+ WGPUTexture Texture = nullptr;
+ WGPUTextureView TextureView = nullptr;
+};
+
struct RenderResources
{
- WGPUTexture FontTexture = nullptr; // Font texture
- WGPUTextureView FontTextureView = nullptr; // Texture view for font texture
- WGPUSampler Sampler = nullptr; // Sampler for the font texture
+ WGPUSampler Sampler = nullptr; // Sampler for textures
WGPUBuffer Uniforms = nullptr; // Shader uniforms
WGPUBindGroup CommonBindGroup = nullptr; // Resources bind-group to bind the common resources to pipeline
ImGuiStorage ImageBindGroups; // Resources bind-group to bind the font/image resources to pipeline (this is a key->value map)
@@ -234,23 +241,8 @@ static void SafeRelease(WGPUShaderModule& res)
wgpuShaderModuleRelease(res);
res = nullptr;
}
-static void SafeRelease(WGPUTextureView& res)
-{
- if (res)
- wgpuTextureViewRelease(res);
- res = nullptr;
-}
-static void SafeRelease(WGPUTexture& res)
-{
- if (res)
- wgpuTextureRelease(res);
- res = nullptr;
-}
-
static void SafeRelease(RenderResources& res)
{
- SafeRelease(res.FontTexture);
- SafeRelease(res.FontTextureView);
SafeRelease(res.Sampler);
SafeRelease(res.Uniforms);
SafeRelease(res.CommonBindGroup);
@@ -378,9 +370,16 @@ void ImGui_ImplWGPU_RenderDrawData(ImDrawData* draw_data, WGPURenderPassEncoder
// Avoid rendering when minimized
int fb_width = (int)(draw_data->DisplaySize.x * draw_data->FramebufferScale.x);
int fb_height = (int)(draw_data->DisplaySize.y * draw_data->FramebufferScale.y);
- if (fb_width <= 0 || fb_height <= 0 || draw_data->CmdListsCount == 0)
+ if (fb_width <= 0 || fb_height <= 0 || draw_data->CmdLists.Size == 0)
return;
+ // Catch up with texture updates. Most of the times, the list will have 1 element with an OK status, aka nothing to do.
+ // (This almost always points to ImGui::GetPlatformIO().Textures[] but is part of ImDrawData to allow overriding or disabling texture updates).
+ if (draw_data->Textures != nullptr)
+ for (ImTextureData* tex : *draw_data->Textures)
+ if (tex->Status != ImTextureStatus_OK)
+ ImGui_ImplWGPU_UpdateTexture(tex);
+
// FIXME: Assuming that this only gets called once per frame!
// If not, we can't just re-allocate the IB or VB, we'll have to do a proper allocator.
ImGui_ImplWGPU_Data* bd = ImGui_ImplWGPU_GetBackendData();
@@ -446,9 +445,8 @@ void ImGui_ImplWGPU_RenderDrawData(ImDrawData* draw_data, WGPURenderPassEncoder
// Upload vertex/index data into a single contiguous GPU buffer
ImDrawVert* vtx_dst = (ImDrawVert*)fr->VertexBufferHost;
ImDrawIdx* idx_dst = (ImDrawIdx*)fr->IndexBufferHost;
- for (int n = 0; n < draw_data->CmdListsCount; n++)
+ for (const ImDrawList* draw_list : draw_data->CmdLists)
{
- const ImDrawList* draw_list = draw_data->CmdLists[n];
memcpy(vtx_dst, draw_list->VtxBuffer.Data, draw_list->VtxBuffer.Size * sizeof(ImDrawVert));
memcpy(idx_dst, draw_list->IdxBuffer.Data, draw_list->IdxBuffer.Size * sizeof(ImDrawIdx));
vtx_dst += draw_list->VtxBuffer.Size;
@@ -475,9 +473,8 @@ void ImGui_ImplWGPU_RenderDrawData(ImDrawData* draw_data, WGPURenderPassEncoder
int global_idx_offset = 0;
ImVec2 clip_scale = draw_data->FramebufferScale;
ImVec2 clip_off = draw_data->DisplayPos;
- for (int n = 0; n < draw_data->CmdListsCount; n++)
+ for (const ImDrawList* draw_list : draw_data->CmdLists)
{
- const ImDrawList* draw_list = draw_data->CmdLists[n];
for (int cmd_i = 0; cmd_i < draw_list->CmdBuffer.Size; cmd_i++)
{
const ImDrawCmd* pcmd = &draw_list->CmdBuffer[cmd_i];
@@ -536,33 +533,51 @@ void ImGui_ImplWGPU_RenderDrawData(ImDrawData* draw_data, WGPURenderPassEncoder
platform_io.Renderer_RenderState = nullptr;
}
-static void ImGui_ImplWGPU_CreateFontsTexture()
+static void ImGui_ImplWGPU_DestroyTexture(ImTextureData* tex)
{
- // Build texture atlas
- ImGui_ImplWGPU_Data* bd = ImGui_ImplWGPU_GetBackendData();
- ImGuiIO& io = ImGui::GetIO();
- unsigned char* pixels;
- int width, height, size_pp;
- io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height, &size_pp);
-
- // Upload texture to graphics system
+ if (ImGui_ImplWGPU_Texture* backend_tex = (ImGui_ImplWGPU_Texture*)tex->BackendUserData)
{
+ IM_ASSERT(backend_tex->TextureView == (WGPUTextureView)(intptr_t)tex->TexID);
+ wgpuTextureViewRelease(backend_tex->TextureView);
+ wgpuTextureRelease(backend_tex->Texture);
+ IM_DELETE(backend_tex);
+
+ // Clear identifiers and mark as destroyed (in order to allow e.g. calling InvalidateDeviceObjects while running)
+ tex->SetTexID(ImTextureID_Invalid);
+ tex->BackendUserData = nullptr;
+ }
+ tex->SetStatus(ImTextureStatus_Destroyed);
+}
+
+void ImGui_ImplWGPU_UpdateTexture(ImTextureData* tex)
+{
+ ImGui_ImplWGPU_Data* bd = ImGui_ImplWGPU_GetBackendData();
+ if (tex->Status == ImTextureStatus_WantCreate)
+ {
+ // Create and upload new texture to graphics system
+ //IMGUI_DEBUG_LOG("UpdateTexture #%03d: WantCreate %dx%d\n", tex->UniqueID, tex->Width, tex->Height);
+ IM_ASSERT(tex->TexID == ImTextureID_Invalid && tex->BackendUserData == nullptr);
+ IM_ASSERT(tex->Format == ImTextureFormat_RGBA32);
+ ImGui_ImplWGPU_Texture* backend_tex = IM_NEW(ImGui_ImplWGPU_Texture)();
+
+ // Create texture
WGPUTextureDescriptor tex_desc = {};
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN) || defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU)
- tex_desc.label = { "Dear ImGui Font Texture", WGPU_STRLEN };
+ tex_desc.label = { "Dear ImGui Texture", WGPU_STRLEN };
#else
- tex_desc.label = "Dear ImGui Font Texture";
+ tex_desc.label = "Dear ImGui Texture";
#endif
tex_desc.dimension = WGPUTextureDimension_2D;
- tex_desc.size.width = width;
- tex_desc.size.height = height;
+ tex_desc.size.width = tex->Width;
+ tex_desc.size.height = tex->Height;
tex_desc.size.depthOrArrayLayers = 1;
tex_desc.sampleCount = 1;
tex_desc.format = WGPUTextureFormat_RGBA8Unorm;
tex_desc.mipLevelCount = 1;
tex_desc.usage = WGPUTextureUsage_CopyDst | WGPUTextureUsage_TextureBinding;
- bd->renderResources.FontTexture = wgpuDeviceCreateTexture(bd->wgpuDevice, &tex_desc);
+ backend_tex->Texture = wgpuDeviceCreateTexture(bd->wgpuDevice, &tex_desc);
+ // Create texture view
WGPUTextureViewDescriptor tex_view_desc = {};
tex_view_desc.format = WGPUTextureFormat_RGBA8Unorm;
tex_view_desc.dimension = WGPUTextureViewDimension_2D;
@@ -571,19 +586,35 @@ static void ImGui_ImplWGPU_CreateFontsTexture()
tex_view_desc.baseArrayLayer = 0;
tex_view_desc.arrayLayerCount = 1;
tex_view_desc.aspect = WGPUTextureAspect_All;
- bd->renderResources.FontTextureView = wgpuTextureCreateView(bd->renderResources.FontTexture, &tex_view_desc);
+ backend_tex->TextureView = wgpuTextureCreateView(backend_tex->Texture, &tex_view_desc);
+
+ // Store identifiers
+ tex->SetTexID((ImTextureID)(intptr_t)backend_tex->TextureView);
+ tex->BackendUserData = backend_tex;
+ // We don't set tex->Status to ImTextureStatus_OK to let the code fallthrough below.
}
- // Upload texture data
+ if (tex->Status == ImTextureStatus_WantCreate || tex->Status == ImTextureStatus_WantUpdates)
{
+ ImGui_ImplWGPU_Texture* backend_tex = (ImGui_ImplWGPU_Texture*)tex->BackendUserData;
+ IM_ASSERT(tex->Format == ImTextureFormat_RGBA32);
+
+ // We could use the smaller rect on _WantCreate but using the full rect allows us to clear the texture.
+ const int upload_x = (tex->Status == ImTextureStatus_WantCreate) ? 0 : tex->UpdateRect.x;
+ const int upload_y = (tex->Status == ImTextureStatus_WantCreate) ? 0 : tex->UpdateRect.y;
+ const int upload_w = (tex->Status == ImTextureStatus_WantCreate) ? tex->Width : tex->UpdateRect.w;
+ const int upload_h = (tex->Status == ImTextureStatus_WantCreate) ? tex->Height : tex->UpdateRect.h;
+
+ // Update full texture or selected blocks. We only ever write to textures regions which have never been used before!
+ // This backend choose to use tex->UpdateRect but you can use tex->Updates[] to upload individual regions.
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN) || defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU)
WGPUTexelCopyTextureInfo dst_view = {};
#else
WGPUImageCopyTexture dst_view = {};
#endif
- dst_view.texture = bd->renderResources.FontTexture;
+ dst_view.texture = backend_tex->Texture;
dst_view.mipLevel = 0;
- dst_view.origin = { 0, 0, 0 };
+ dst_view.origin = { (uint32_t)upload_x, (uint32_t)upload_y, 0 };
dst_view.aspect = WGPUTextureAspect_All;
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN) || defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU)
WGPUTexelCopyBufferLayout layout = {};
@@ -591,29 +622,14 @@ static void ImGui_ImplWGPU_CreateFontsTexture()
WGPUTextureDataLayout layout = {};
#endif
layout.offset = 0;
- layout.bytesPerRow = width * size_pp;
- layout.rowsPerImage = height;
- WGPUExtent3D size = { (uint32_t)width, (uint32_t)height, 1 };
- wgpuQueueWriteTexture(bd->defaultQueue, &dst_view, pixels, (uint32_t)(width * size_pp * height), &layout, &size);
+ layout.bytesPerRow = tex->Width * tex->BytesPerPixel;
+ layout.rowsPerImage = upload_h;
+ WGPUExtent3D write_size = { (uint32_t)upload_w, (uint32_t)upload_h, 1 };
+ wgpuQueueWriteTexture(bd->defaultQueue, &dst_view, tex->GetPixelsAt(upload_x, upload_y), (uint32_t)(tex->Width * upload_h * tex->BytesPerPixel), &layout, &write_size);
+ tex->SetStatus(ImTextureStatus_OK);
}
-
- // Create the associated sampler
- // (Bilinear sampling is required by default. Set 'io.Fonts->Flags |= ImFontAtlasFlags_NoBakedLines' or 'style.AntiAliasedLinesUseTex = false' to allow point/nearest sampling)
- {
- WGPUSamplerDescriptor sampler_desc = {};
- sampler_desc.minFilter = WGPUFilterMode_Linear;
- sampler_desc.magFilter = WGPUFilterMode_Linear;
- sampler_desc.mipmapFilter = WGPUMipmapFilterMode_Linear;
- sampler_desc.addressModeU = WGPUAddressMode_ClampToEdge;
- sampler_desc.addressModeV = WGPUAddressMode_ClampToEdge;
- sampler_desc.addressModeW = WGPUAddressMode_ClampToEdge;
- sampler_desc.maxAnisotropy = 1;
- bd->renderResources.Sampler = wgpuDeviceCreateSampler(bd->wgpuDevice, &sampler_desc);
- }
-
- // Store our identifier
- static_assert(sizeof(ImTextureID) >= sizeof(bd->renderResources.FontTexture), "Can't pack descriptor handle into TexID, 32-bit not supported yet.");
- io.Fonts->SetTexID((ImTextureID)bd->renderResources.FontTextureView);
+ if (tex->Status == ImTextureStatus_WantDestroy && tex->UnusedFrames > 0)
+ ImGui_ImplWGPU_DestroyTexture(tex);
}
static void ImGui_ImplWGPU_CreateUniformBuffer()
@@ -757,16 +773,26 @@ bool ImGui_ImplWGPU_CreateDeviceObjects()
bd->pipelineState = wgpuDeviceCreateRenderPipeline(bd->wgpuDevice, &graphics_pipeline_desc);
- ImGui_ImplWGPU_CreateFontsTexture();
ImGui_ImplWGPU_CreateUniformBuffer();
+ // Create sampler
+ // (Bilinear sampling is required by default. Set 'io.Fonts->Flags |= ImFontAtlasFlags_NoBakedLines' or 'style.AntiAliasedLinesUseTex = false' to allow point/nearest sampling)
+ WGPUSamplerDescriptor sampler_desc = {};
+ sampler_desc.minFilter = WGPUFilterMode_Linear;
+ sampler_desc.magFilter = WGPUFilterMode_Linear;
+ sampler_desc.mipmapFilter = WGPUMipmapFilterMode_Linear;
+ sampler_desc.addressModeU = WGPUAddressMode_ClampToEdge;
+ sampler_desc.addressModeV = WGPUAddressMode_ClampToEdge;
+ sampler_desc.addressModeW = WGPUAddressMode_ClampToEdge;
+ sampler_desc.maxAnisotropy = 1;
+ bd->renderResources.Sampler = wgpuDeviceCreateSampler(bd->wgpuDevice, &sampler_desc);
+
// Create resource bind group
WGPUBindGroupEntry common_bg_entries[] =
{
{ nullptr, 0, bd->renderResources.Uniforms, 0, MEMALIGN(sizeof(Uniforms), 16), 0, 0 },
{ nullptr, 1, 0, 0, 0, bd->renderResources.Sampler, 0 },
};
-
WGPUBindGroupDescriptor common_bg_descriptor = {};
common_bg_descriptor.layout = bg_layouts[0];
common_bg_descriptor.entryCount = sizeof(common_bg_entries) / sizeof(WGPUBindGroupEntry);
@@ -791,8 +817,10 @@ void ImGui_ImplWGPU_InvalidateDeviceObjects()
SafeRelease(bd->pipelineState);
SafeRelease(bd->renderResources);
- ImGuiIO& io = ImGui::GetIO();
- io.Fonts->SetTexID(0); // We copied g_pFontTextureView to io.Fonts->TexID so let's clear that as well.
+ // Destroy all textures
+ for (ImTextureData* tex : ImGui::GetPlatformIO().Textures)
+ if (tex->RefCount == 1)
+ ImGui_ImplWGPU_DestroyTexture(tex);
for (unsigned int i = 0; i < bd->numFramesInFlight; i++)
SafeRelease(bd->pFrameResources[i]);
@@ -817,6 +845,7 @@ bool ImGui_ImplWGPU_Init(ImGui_ImplWGPU_InitInfo* init_info)
io.BackendRendererName = "imgui_impl_webgpu";
#endif
io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes.
+ io.BackendFlags |= ImGuiBackendFlags_RendererHasTextures; // We can honor ImGuiPlatformIO::Textures[] requests during render.
bd->initInfo = *init_info;
bd->wgpuDevice = init_info->Device;
@@ -826,8 +855,6 @@ bool ImGui_ImplWGPU_Init(ImGui_ImplWGPU_InitInfo* init_info)
bd->numFramesInFlight = init_info->NumFramesInFlight;
bd->frameIndex = UINT_MAX;
- bd->renderResources.FontTexture = nullptr;
- bd->renderResources.FontTextureView = nullptr;
bd->renderResources.Sampler = nullptr;
bd->renderResources.Uniforms = nullptr;
bd->renderResources.CommonBindGroup = nullptr;
@@ -855,6 +882,7 @@ void ImGui_ImplWGPU_Shutdown()
ImGui_ImplWGPU_Data* bd = ImGui_ImplWGPU_GetBackendData();
IM_ASSERT(bd != nullptr && "No renderer backend to shutdown, or already shutdown?");
ImGuiIO& io = ImGui::GetIO();
+ ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
ImGui_ImplWGPU_InvalidateDeviceObjects();
delete[] bd->pFrameResources;
@@ -866,7 +894,8 @@ void ImGui_ImplWGPU_Shutdown()
io.BackendRendererName = nullptr;
io.BackendRendererUserData = nullptr;
- io.BackendFlags &= ~ImGuiBackendFlags_RendererHasVtxOffset;
+ io.BackendFlags &= ~(ImGuiBackendFlags_RendererHasVtxOffset | ImGuiBackendFlags_RendererHasTextures);
+ platform_io.ClearRendererHandlers();
IM_DELETE(bd);
}
@@ -874,7 +903,8 @@ void ImGui_ImplWGPU_NewFrame()
{
ImGui_ImplWGPU_Data* bd = ImGui_ImplWGPU_GetBackendData();
if (!bd->pipelineState)
- ImGui_ImplWGPU_CreateDeviceObjects();
+ if (!ImGui_ImplWGPU_CreateDeviceObjects())
+ IM_ASSERT(0 && "ImGui_ImplWGPU_CreateDeviceObjects() failed!");
}
//-----------------------------------------------------------------------------
diff --git a/backends/imgui_impl_wgpu.h b/backends/imgui_impl_wgpu.h
index 3262ac5ea..5474f3c9c 100644
--- a/backends/imgui_impl_wgpu.h
+++ b/backends/imgui_impl_wgpu.h
@@ -10,11 +10,12 @@
//#define IMGUI_IMPL_WEBGPU_BACKEND_WGPU
// Implemented features:
-// [X] Renderer: User texture binding. Use 'WGPUTextureView' as ImTextureID. Read the FAQ about ImTextureID!
+// [X] Renderer: User texture binding. Use 'WGPUTextureView' as ImTextureID. Read the FAQ about ImTextureID/ImTextureRef!
// [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset).
// [X] Renderer: Expose selected render state for draw callbacks to use. Access in '(ImGui_ImplXXXX_RenderState*)GetPlatformIO().Renderer_RenderState'.
-// Missing features:
-// [ ] Renderer: Multi-viewport support (multiple windows). Not meaningful on the web.
+// [X] Renderer: Texture updates support for dynamic font system (ImGuiBackendFlags_RendererHasTextures).
+// Missing features or Issues:
+// [ ] Renderer: Multi-viewport support (multiple windows), useful for desktop.
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
@@ -57,6 +58,9 @@ IMGUI_IMPL_API void ImGui_ImplWGPU_RenderDrawData(ImDrawData* draw_data, WGPURen
IMGUI_IMPL_API bool ImGui_ImplWGPU_CreateDeviceObjects();
IMGUI_IMPL_API void ImGui_ImplWGPU_InvalidateDeviceObjects();
+// (Advanced) Use e.g. if you need to precisely control the timing of texture updates (e.g. for staged rendering), by setting ImDrawData::Textures = NULL to handle this manually.
+IMGUI_IMPL_API void ImGui_ImplWGPU_UpdateTexture(ImTextureData* tex);
+
// [BETA] Selected render state data shared with callbacks.
// This is temporarily stored in GetPlatformIO().Renderer_RenderState during the ImGui_ImplWGPU_RenderDrawData() call.
// (Please open an issue if you feel you need access to more data)
diff --git a/backends/imgui_impl_win32.cpp b/backends/imgui_impl_win32.cpp
index 14354ade0..e7ba085bb 100644
--- a/backends/imgui_impl_win32.cpp
+++ b/backends/imgui_impl_win32.cpp
@@ -23,6 +23,10 @@
// CHANGELOG
// (minor and older changes stripped away, please see git history for details)
// 2025-XX-XX: Platform: Added support for multiple windows via the ImGuiPlatformIO interface.
+// 2025-09-23: Inputs: Minor optimization not submitting gamepad input if packet number has not changed.
+// 2025-09-18: Call platform_io.ClearPlatformHandlers() on shutdown.
+// 2025-06-02: [Docking] WM_DPICHANGED also apply io.ConfigDpiScaleViewports for main viewport instead of letting it be done by application code.
+// 2025-04-30: Inputs: Fixed an issue where externally losing mouse capture (due to e.g. focus loss) would fail to claim it again the next subsequent click. (#8594)
// 2025-03-26: [Docking] Viewports: fixed an issue when closing a window from the OS close button (with io.ConfigViewportsNoDecoration = false) while user code was discarding the 'bool* p_open = false' output from Begin(). Because we allowed the Win32 window to close early, Windows destroyed it and our imgui window became not visible even though user code was still submitting it.
// 2025-03-10: When dealing with OEM keys, use scancodes instead of translated keycodes to choose ImGuiKey values. (#7136, #7201, #7206, #7306, #7670, #7672, #8468)
// 2025-02-21: [Docking] WM_SETTINGCHANGE's SPI_SETWORKAREA message also triggers a refresh of monitor list. (#8415)
@@ -135,6 +139,7 @@ struct ImGui_ImplWin32_Data
HMODULE XInputDLL;
PFN_XInputGetCapabilities XInputGetCapabilities;
PFN_XInputGetState XInputGetState;
+ DWORD XInputPacketNumber;
#endif
ImGui_ImplWin32_Data() { memset((void*)this, 0, sizeof(*this)); }
@@ -184,6 +189,7 @@ static bool ImGui_ImplWin32_InitEx(void* hwnd, bool platform_has_own_dc)
io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used)
io.BackendFlags |= ImGuiBackendFlags_PlatformHasViewports; // We can create multi-viewports on the Platform side (optional)
io.BackendFlags |= ImGuiBackendFlags_HasMouseHoveredViewport; // We can call io.AddMouseViewportEvent() with correct data (optional)
+ io.BackendFlags |= ImGuiBackendFlags_HasParentViewport; // We can honor viewport->ParentViewportId by applying the corresponding parent/child relationship at platform levle (optional)
bd->hWnd = (HWND)hwnd;
bd->TicksPerSecond = perf_frequency;
@@ -243,6 +249,7 @@ void ImGui_ImplWin32_Shutdown()
ImGui_ImplWin32_Data* bd = ImGui_ImplWin32_GetBackendData();
IM_ASSERT(bd != nullptr && "No platform backend to shutdown, or already shutdown?");
ImGuiIO& io = ImGui::GetIO();
+ ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
::SetPropA(bd->hWnd, "IMGUI_CONTEXT", nullptr);
ImGui_ImplWin32_ShutdownMultiViewportSupport();
@@ -255,7 +262,8 @@ void ImGui_ImplWin32_Shutdown()
io.BackendPlatformName = nullptr;
io.BackendPlatformUserData = nullptr;
- io.BackendFlags &= ~(ImGuiBackendFlags_HasMouseCursors | ImGuiBackendFlags_HasSetMousePos | ImGuiBackendFlags_HasGamepad | ImGuiBackendFlags_PlatformHasViewports | ImGuiBackendFlags_HasMouseHoveredViewport);
+ io.BackendFlags &= ~(ImGuiBackendFlags_HasMouseCursors | ImGuiBackendFlags_HasSetMousePos | ImGuiBackendFlags_HasGamepad | ImGuiBackendFlags_PlatformHasViewports | ImGuiBackendFlags_HasMouseHoveredViewport | ImGuiBackendFlags_HasParentViewport);
+ platform_io.ClearPlatformHandlers();
IM_DELETE(bd);
}
@@ -413,6 +421,9 @@ static void ImGui_ImplWin32_UpdateGamepads(ImGuiIO& io)
if (!bd->HasGamepad || bd->XInputGetState == nullptr || bd->XInputGetState(0, &xinput_state) != ERROR_SUCCESS)
return;
io.BackendFlags |= ImGuiBackendFlags_HasGamepad;
+ if (bd->XInputPacketNumber != 0 && bd->XInputPacketNumber == xinput_state.dwPacketNumber)
+ return;
+ bd->XInputPacketNumber = xinput_state.dwPacketNumber;
#define IM_SATURATE(V) (V < 0.0f ? 0.0f : V > 1.0f ? 1.0f : V)
#define MAP_BUTTON(KEY_NO, BUTTON_ENUM) { io.AddKeyEvent(KEY_NO, (gamepad.wButtons & BUTTON_ENUM) != 0); }
@@ -666,6 +677,7 @@ ImGuiKey ImGui_ImplWin32_KeyEventToImGuiKey(WPARAM wParam, LPARAM lParam)
case 51: return ImGuiKey_Comma;
case 52: return ImGuiKey_Period;
case 53: return ImGuiKey_Slash;
+ default: break;
}
return ImGuiKey_None;
@@ -704,6 +716,10 @@ static ImGuiMouseSource ImGui_ImplWin32_GetMouseSourceFromMessageExtraInfo()
extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam); // Use ImGui::GetCurrentContext()
extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandlerEx(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, ImGuiIO& io); // Doesn't use ImGui::GetCurrentContext()
+#ifndef WM_DPICHANGED
+#define WM_DPICHANGED 0x02E0 // From Windows SDK 8.1+ headers
+#endif
+
IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
// Most backends don't have silent checks like this one, but we need it because WndProc are called early in CreateWindow().
@@ -781,7 +797,10 @@ IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandlerEx(HWND hwnd, UINT msg, WPA
if (msg == WM_RBUTTONDOWN || msg == WM_RBUTTONDBLCLK) { button = 1; }
if (msg == WM_MBUTTONDOWN || msg == WM_MBUTTONDBLCLK) { button = 2; }
if (msg == WM_XBUTTONDOWN || msg == WM_XBUTTONDBLCLK) { button = (GET_XBUTTON_WPARAM(wParam) == XBUTTON1) ? 3 : 4; }
- if (bd->MouseButtonsDown == 0 && ::GetCapture() == nullptr)
+ HWND hwnd_with_capture = ::GetCapture();
+ if (bd->MouseButtonsDown != 0 && hwnd_with_capture != hwnd) // Did we externally lost capture?
+ bd->MouseButtonsDown = 0;
+ if (bd->MouseButtonsDown == 0 && hwnd_with_capture == nullptr)
::SetCapture(hwnd); // Allow us to read mouse coordinates when dragging mouse outside of our window bounds.
bd->MouseButtonsDown |= 1 << button;
io.AddMouseSourceEvent(mouse_source);
@@ -895,6 +914,13 @@ IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandlerEx(HWND hwnd, UINT msg, WPA
if (wParam == SPI_SETWORKAREA)
bd->WantUpdateMonitors = true;
return 0;
+ case WM_DPICHANGED:
+ {
+ const RECT* suggested_rect = (RECT*)lParam;
+ if (io.ConfigDpiScaleViewports)
+ ::SetWindowPos(hwnd, nullptr, suggested_rect->left, suggested_rect->top, suggested_rect->right - suggested_rect->left, suggested_rect->bottom - suggested_rect->top, SWP_NOZORDER | SWP_NOACTIVATE);
+ return 0;
+ }
}
return 0;
}
@@ -1097,11 +1123,10 @@ static void ImGui_ImplWin32_GetWin32StyleFromViewportFlags(ImGuiViewportFlags fl
*out_ex_style |= WS_EX_TOPMOST;
}
-static HWND ImGui_ImplWin32_GetHwndFromViewportID(ImGuiID viewport_id)
+static HWND ImGui_ImplWin32_GetHwndFromViewport(ImGuiViewport* viewport)
{
- if (viewport_id != 0)
- if (ImGuiViewport* viewport = ImGui::FindViewportByID(viewport_id))
- return (HWND)viewport->PlatformHandle;
+ if (viewport != nullptr)
+ return (HWND)viewport->PlatformHandle;
return nullptr;
}
@@ -1112,7 +1137,7 @@ static void ImGui_ImplWin32_CreateWindow(ImGuiViewport* viewport)
// Select style and parent window
ImGui_ImplWin32_GetWin32StyleFromViewportFlags(viewport->Flags, &vd->DwStyle, &vd->DwExStyle);
- vd->HwndParent = ImGui_ImplWin32_GetHwndFromViewportID(viewport->ParentViewportId);
+ vd->HwndParent = ImGui_ImplWin32_GetHwndFromViewport(viewport->ParentViewport);
// Create window
RECT rect = { (LONG)viewport->Pos.x, (LONG)viewport->Pos.y, (LONG)(viewport->Pos.x + viewport->Size.x), (LONG)(viewport->Pos.y + viewport->Size.y) };
@@ -1175,7 +1200,7 @@ static void ImGui_ImplWin32_UpdateWindow(ImGuiViewport* viewport)
// Update Win32 parent if it changed _after_ creation
// Unlike style settings derived from configuration flags, this is more likely to change for advanced apps that are manipulating ParentViewportID manually.
- HWND new_parent = ImGui_ImplWin32_GetHwndFromViewportID(viewport->ParentViewportId);
+ HWND new_parent = ImGui_ImplWin32_GetHwndFromViewport(viewport->ParentViewport);
if (new_parent != vd->HwndParent)
{
// Win32 windows can either have a "Parent" (for WS_CHILD window) or an "Owner" (which among other thing keeps window above its owner).
diff --git a/docs/BACKENDS.md b/docs/BACKENDS.md
index de5cd6697..d50130f2a 100644
--- a/docs/BACKENDS.md
+++ b/docs/BACKENDS.md
@@ -1,18 +1,33 @@
_(You may browse this at https://github.com/ocornut/imgui/blob/master/docs/BACKENDS.md or view this file with any Markdown viewer)_
-## Dear ImGui: Backends
+# Dear ImGui: Backends
-### Integrating backends
+## Index
+
+- [Introduction](#introduction)
+ - [Getting Started](#getting-started)
+ - [What are Backends?](#what-are-backends)
+- [Using standard Backends](#using-standard-backends)
+- [Using third-party Backends](#using-third-party-backends)
+- [Writing your own Backend](#writing-your-own-backend)
+ - [Using a custom engine?](#using-a-custom-engine)
+ - [Platform: Implementing your Platform Backend](#platform-implementing-your-platform-backend)
+ - [Rendering: Implementing your RenderDrawData function](#rendering-implementing-your-renderdrawdata-function)
+ - [Rendering: Adding support for `ImGuiBackendFlags_RendererHasTextures` (1.92+)](#rendering-adding-support-for-imguibackendflags_rendererhastextures-192)
+
+## Introduction
+
+### Getting Started
💡 The **[Getting Started](https://github.com/ocornut/imgui/wiki/Getting-Started) wiki guide** has examples of how to integrate Dear ImGui into an existing application.
The [EXAMPLES.MD](https://github.com/ocornut/imgui/blob/master/docs/EXAMPLES.md) documentation may also be worth a read.
-### What are backends?
+### What are Backends?
Dear ImGui is highly portable and only requires a few things to run and render, typically:
- Required: providing mouse/keyboard inputs (fed into the `ImGuiIO` structure).
- - Required: uploading the font atlas texture into graphics memory.
+ - Required: creating, updating and destroying textures.
- Required: rendering indexed textured triangles with a clipping rectangle.
Extra features are opt-in, our backends try to support as many as possible:
@@ -34,13 +49,13 @@ and the backends which we are describing here (backends/ folder).
- You should be able to write backends for pretty much any platform and any 3D graphics API.
e.g. you can get creative and use software rendering or render remotely on a different machine.
-### Standard backends
+## Using standard Backends
**The [backends/](https://github.com/ocornut/imgui/blob/master/backends) folder contains backends for popular platforms/graphics API, which you can use in
your application or engine to easily integrate Dear ImGui.** Each backend is typically self-contained in a pair of files: imgui_impl_XXXX.cpp + imgui_impl_XXXX.h.
- The 'Platform' backends are in charge of: mouse/keyboard/gamepad inputs, cursor shape, timing, and windowing.
- e.g. Windows ([imgui_impl_win32.cpp](https://github.com/ocornut/imgui/blob/master/backends/imgui_impl_win32.cpp)), GLFW ([imgui_impl_glfw.cpp](https://github.com/ocornut/imgui/blob/master/backends/imgui_impl_glfw.cpp)), SDL2 ([imgui_impl_sdl2.cpp](https://github.com/ocornut/imgui/blob/master/backends/imgui_impl_sdl2.cpp)), etc.
+ e.g. Windows ([imgui_impl_win32.cpp](https://github.com/ocornut/imgui/blob/master/backends/imgui_impl_win32.cpp)), SDL3 ([imgui_impl_sdl3.cpp](https://github.com/ocornut/imgui/blob/master/backends/imgui_impl_sdl3.cpp)), GLFW ([imgui_impl_glfw.cpp](https://github.com/ocornut/imgui/blob/master/backends/imgui_impl_glfw.cpp)), etc.
- The 'Renderer' backends are in charge of: creating atlas texture, and rendering imgui draw data.
e.g. DirectX11 ([imgui_impl_dx11.cpp](https://github.com/ocornut/imgui/blob/master/backends/imgui_impl_dx11.cpp)), OpenGL/WebGL ([imgui_impl_opengl3.cpp](https://github.com/ocornut/imgui/blob/master/backends/imgui_impl_opengl3.cpp)), Vulkan ([imgui_impl_vulkan.cpp](https://github.com/ocornut/imgui/blob/master/backends/imgui_impl_vulkan.cpp)), etc.
@@ -53,44 +68,41 @@ For example, the [example_win32_directx11](https://github.com/ocornut/imgui/tree
**Once Dear ImGui is setup and running, run and refer to `ImGui::ShowDemoWindow()` in imgui_demo.cpp for usage of the end-user API.**
-### List of backends
+### List of standard Backends
In the [backends/](https://github.com/ocornut/imgui/blob/master/backends) folder:
List of Platforms Backends:
- imgui_impl_android.cpp ; Android native app API
- imgui_impl_glfw.cpp ; GLFW (Windows, macOS, Linux, etc.) http://www.glfw.org/
- imgui_impl_osx.mm ; macOS native API (not as feature complete as glfw/sdl backends)
- imgui_impl_sdl2.cpp ; SDL2 (Windows, macOS, Linux, iOS, Android) https://www.libsdl.org
- imgui_impl_sdl3.cpp ; SDL3 (Windows, macOS, Linux, iOS, Android) https://www.libsdl.org (*EXPERIMENTAL UNTIL SDL3 IS RELEASED*)
- imgui_impl_win32.cpp ; Win32 native API (Windows)
- imgui_impl_glut.cpp ; GLUT/FreeGLUT (this is prehistoric software and absolutely not recommended today!)
+ imgui_impl_android.cpp ; Android native app API
+ imgui_impl_glfw.cpp ; GLFW (Windows, macOS, Linux, etc.) http://www.glfw.org/
+ imgui_impl_osx.mm ; macOS native API (not as feature complete as glfw/sdl backends)
+ imgui_impl_sdl2.cpp ; SDL2 (Windows, macOS, Linux, iOS, Android) https://www.libsdl.org
+ imgui_impl_sdl3.cpp ; SDL3 (Windows, macOS, Linux, iOS, Android) https://www.libsdl.org
+ imgui_impl_win32.cpp ; Win32 native API (Windows)
+ imgui_impl_glut.cpp ; GLUT/FreeGLUT (this is prehistoric software and absolutely not recommended today!)
List of Renderer Backends:
- imgui_impl_dx9.cpp ; DirectX9
- imgui_impl_dx10.cpp ; DirectX10
- imgui_impl_dx11.cpp ; DirectX11
- imgui_impl_dx12.cpp ; DirectX12
- imgui_impl_metal.mm ; Metal (with ObjC)
- imgui_impl_opengl2.cpp ; OpenGL 2 (legacy, fixed pipeline <- don't use with modern OpenGL context)
- imgui_impl_opengl3.cpp ; OpenGL 3/4, OpenGL ES 2, OpenGL ES 3 (modern programmable pipeline)
+ imgui_impl_dx9.cpp ; DirectX9
+ imgui_impl_dx10.cpp ; DirectX10
+ imgui_impl_dx11.cpp ; DirectX11
+ imgui_impl_dx12.cpp ; DirectX12
+ imgui_impl_metal.mm ; Metal (ObjC or C++)
+ imgui_impl_opengl2.cpp ; OpenGL 2 (legacy fixed pipeline. Don't use with modern OpenGL code!)
+ imgui_impl_opengl3.cpp ; OpenGL 3/4, OpenGL ES 2/3, WebGL
+ imgui_impl_sdlgpu3.cpp ; SDL_GPU (portable 3D graphics API of SDL3)
imgui_impl_sdlrenderer2.cpp ; SDL_Renderer (optional component of SDL2 available from SDL 2.0.18+)
- imgui_impl_sdlrenderer3.cpp ; SDL_Renderer (optional component of SDL3 available from SDL 3.0.0+)
- imgui_impl_vulkan.cpp ; Vulkan
- imgui_impl_wgpu.cpp ; WebGPU (web and desktop)
+ imgui_impl_sdlrenderer3.cpp ; SDL_Renderer (optional component of SDL3. Prefer using SDL_GPU!).
+ imgui_impl_vulkan.cpp ; Vulkan
+ imgui_impl_wgpu.cpp ; WebGPU (web + desktop)
List of high-level Frameworks Backends (combining Platform + Renderer):
imgui_impl_allegro5.cpp
Emscripten is also supported!
-The SDL+GL, GLFW+GL and GLFW+WebGPU examples are all ready to build and run with Emscripten.
-
-### Backends for third-party frameworks, graphics API or other languages
-
-See https://github.com/ocornut/imgui/wiki/Bindings for the full list (e.g. Adventure Game Studio, Cinder, Cocos2d-x, Game Maker Studio2, Godot, LÖVE+LUA, Magnum, Monogame, Ogre, openFrameworks, OpenSceneGraph, SFML, Sokol, Unity, Unreal Engine and many others).
+The SDL2+GL, SDL3+GL, GLFW+GL and GLFW+WebGPU examples are all ready to build and run with Emscripten.
### Recommended Backends
@@ -98,18 +110,32 @@ If you are not sure which backend to use, the recommended platform/frameworks fo
|Library |Website |Backend |Note |
|--------|--------|--------|-----|
-| GLFW | https://github.com/glfw/glfw | imgui_impl_glfw.cpp | |
+| SDL3 | https://www.libsdl.org | imgui_impl_sdl3.cpp | Recommended |
| SDL2 | https://www.libsdl.org | imgui_impl_sdl2.cpp | |
+| GLFW | https://github.com/glfw/glfw | imgui_impl_glfw.cpp | |
| Sokol | https://github.com/floooh/sokol | [util/sokol_imgui.h](https://github.com/floooh/sokol/blob/master/util/sokol_imgui.h) | Lower-level than GLFW/SDL |
+If your application runs on Windows or if you are using multi-viewport, the win32 backend handles some details a little better than other backends.
+
+## Using third-party Backends
+
+See https://github.com/ocornut/imgui/wiki/Bindings for the full list (e.g. Adventure Game Studio, Cinder, Cocos2d-x, Game Maker Studio2, Godot, LÖVE+LUA, Magnum, Monogame, Ogre, openFrameworks, OpenSceneGraph, SFML, Sokol, Unity, Unreal Engine and many others).
+
+## Writing your own Backend
### Using a custom engine?
You will likely be tempted to start by rewrite your own backend using your own custom/high-level facilities...
Think twice!
-If you are new to Dear ImGui, first try using the existing backends as-is.
+TL;DR;
+- Writing your own Renderer Backend is easy.
+- Writing your own Platform Backend is harder and you are more likely to introduce bugs.
+- **It is unlikely you will add value to your project by creating your own backend.**
+
+**Consider using the existing backends as-is**.
You will save lots of time integrating the library.
+Standard backends are battle-tested and handle subtleties that you are likely to implement incorrectly.
You can LATER decide to rewrite yourself a custom backend if you really need to.
In most situations, custom backends have fewer features and more bugs than the standard backends we provide.
If you want portability, you can use multiple backends and choose between them either at compile time
@@ -131,16 +157,222 @@ Suggestion: try using a non-portable backend first (e.g. win32 + underlying grap
your desktop builds working first. This will get you running faster and get your acquainted with
how Dear ImGui works and is setup. You can then rewrite a custom backend using your own engine API...
-Generally:
-It is unlikely you will add value to your project by creating your own backend.
-
Also:
The [multi-viewports feature](https://github.com/ocornut/imgui/wiki/Multi-Viewports) of the 'docking' branch allows
Dear ImGui windows to be seamlessly detached from the main application window. This is achieved using an
extra layer to the Platform and Renderer backends, which allows Dear ImGui to communicate platform-specific
requests such as: "create an additional OS window", "create a render context", "get the OS position of this
-window" etc. See 'ImGuiPlatformIO' for details.
+window", but some things are more difficult "find OS window under mouse position BUT with some windows marked as passthrough". See 'ImGuiPlatformIO' for details.
Supporting the multi-viewports feature correctly using 100% of your own abstractions is more difficult
than supporting single-viewport.
If you decide to use unmodified imgui_impl_XXXX.cpp files, you can automatically benefit from
improvements and fixes related to viewports and platform windows without extra work on your side.
+
+### Platform: Implementing your Platform Backend
+
+The Platform backends in impl_impl_XXX.cpp files contain many implementations.
+
+**In your `ImGui_ImplXXX_Init()` function:**
+- You can allocate your backend data and use `io.BackendPlatformUserData` to store/retrieve it later.
+- Set `io.BackendPlatformName` to a name `"imgui_impl_xxxx"` which will be available in e.g. About box.
+- Set `io.BackendPlatformUserData` to your backend data.
+- Set `io.BackendFlags` with supported optional features:
+ - `ImGuiBackendFlags_HasGamepad`: supports gamepad and currently has one connected.
+ - `ImGuiBackendFlags_HasMouseCursors`: supports honoring GetMouseCursor() value to change the OS cursor shape.
+ - `ImGuiBackendFlags_HasSetMousePos`: supports io.WantSetMousePos requests to reposition the OS mouse position (only used if io.ConfigNavMoveSetMousePos is set).
+ - `ImGuiBackendFlags_PlatformHasViewports` supports multiple viewports. (multi-viewports only)
+ - `ImGuiBackendFlags_HasMouseHoveredViewport` supports calling io.AddMouseViewportEvent() with the viewport under the mouse. IF POSSIBLE, ignore viewports with the ImGuiViewportFlags_NoInputs flag. If this cannot be done, Dear ImGui needs to use a flawed heuristic to find the viewport under mouse position, as it doesn't know about foreign windows. (multi-viewports only)
+ - `ImGuiBackendFlags_HasParentViewport` supports honoring viewport->ParentViewportId value, by applying the corresponding parent/child relation at the Platform level.
+
+**In your `ImGui_ImplXXX_NewFrame()` function:**
+- Set `io.DeltaTime` to the time elapsed (in seconds) since last frame.
+- Set `io.DisplaySize` to your window size.
+- Set `io.DisplayFrameBufferSize` to your window pixel density (macOS/iOS only).
+- Update mouse cursor shape is supported.
+
+**In your `ImGui_ImplXXX_NewFrame()` function or event handlers:**
+- **Mouse Support**
+ - Use `io.AddMousePosEvent()`, `io.AddMouseButtonEvent()`, `io.AddMouseWheelEvent()` to pass mouse events.
+ - Use `io.AddMouseSourceEvent()` if you are able to distinguish Mouse from TouchScreen from Pen inputs. TouchScreen and Pen inputs requires different logic for some Dear ImGui features.
+ - Use `io.AddMouseViewportEvent()` to specify which viewport/OS window is being hovered by the mouse. Read instructions carefully as this is not as simple as it seems! (multi-viewports only)
+- **Keyboard Support**
+ - Use `io.AddKeyEvent()` to pass key events.
+ - Use `io.AddInputCharacter()` to pass text/character events.
+- **Gamepad Support**
+ - Use `io.AddKeyEvent()` and `io.AddKeyAnalogEvent()` to pass gamepad events, using `ImGuiKey_GamepadXXX` values.
+- **Miscellaneous**
+ - Clipboard Support: setup `Platform_GetClipboardTextFn()`, `Platform_SetClipboardTextFn()` handlers in `ImGuiPlatformIO`.
+ - Open in Shell support: setup `Platform_OpenInShellFn()` handler in `ImGuiPlatformIO`.
+ - IME Support: setup `Platform_SetImeDataFn()` handler in `ImGuiPlatformIO`.
+ - Use `io.AddFocusEvent()` to notify when application window gets focused/unfocused.
+- **Multi-viewport Support**
+ - Update monitor list if supported.
+ - Setup all required handlers in `ImGuiPlatformIO` to create/destroy/move/resize/title/focus/etc. windows.
+
+### Rendering: Implementing your RenderDrawData function
+
+Note: set `ImGuiBackendFlags_RendererHasVtxOffset` to signify your backend can handle rendering with a vertex offset (`ImDrawCmd::VtxOffset` field).
+Otherwise, rendering will be limited to 64K vertices per window, which may be limiting for advanced plot.
+As an alternative, you may also use `#define ImDrawIdx unsigned int` in your `imconfig.h` file to support 32-bit indices.
+
+```cpp
+void MyImGuiBackend_RenderDrawData(ImDrawData* draw_data)
+{
+ // TODO: Update textures.
+ // - Most of the times, the list will have 1 element with an OK status, aka nothing to do.
+ // - This almost always points to ImGui::GetPlatformIO().Textures[] but is part of ImDrawData to allow overriding or disabling texture updates.
+ if (draw_data->Textures != nullptr)
+ for (ImTextureData* tex : *draw_data->Textures)
+ if (tex->Status != ImTextureStatus_OK)
+ MyImGuiBackend_UpdateTexture(tex);
+
+ // TODO: Setup render state:
+ // - Alpha-blending enabled
+ // - No backface culling
+ // - No depth testing, no depth writing
+ // - Scissor enabled
+ MyEngineSetupenderState();
+
+ // TODO: Setup texture sampling state
+ // - Sample with bilinear filtering (NOT point/nearest filtering).
+ // - Use 'io.Fonts->Flags |= ImFontAtlasFlags_NoBakedLines;' to allow point/nearest filtering.
+
+ // TODO: Setup viewport covering draw_data->DisplayPos to draw_data->DisplayPos + draw_data->DisplaySize
+
+ // TODO: Setup orthographic projection matrix cover draw_data->DisplayPos to draw_data->DisplayPos + draw_data->DisplaySize
+
+ // TODO: Setup shader: vertex { float2 pos, float2 uv, u32 color }, fragment shader sample color from 1 texture, multiply by vertex color.
+
+ // Render command lists
+ ImVec2 clip_off = draw_data->DisplayPos;
+ ImVec2 clip_scale = draw_data->FramebufferScale;
+ for (const ImDrawList* draw_list : draw_data->CmdLists)
+ {
+ const ImDrawVert* vtx_buffer = cmd_list->VtxBuffer.Data; // vertex buffer generated by Dear ImGui
+ const ImDrawIdx* idx_buffer = cmd_list->IdxBuffer.Data; // index buffer generated by Dear ImGui
+ for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++)
+ {
+ const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
+ if (pcmd->UserCallback)
+ {
+ if (pcmd->UserCallback == ImDrawCallback_ResetRenderState)
+ MyEngineSetupenderState();
+ else
+ pcmd->UserCallback(cmd_list, pcmd);
+ }
+ else
+ {
+ // Project scissor/clipping rectangles into framebuffer space
+ // - Clipping coordinates are provided in imgui coordinates space:
+ // - For a given viewport, draw_data->DisplayPos == viewport->Pos and draw_data->DisplaySize == viewport->Size
+ // - In a single viewport application, draw_data->DisplayPos == (0,0) and draw_data->DisplaySize == io.DisplaySize, but always use GetMainViewport()->Pos/Size instead of hardcoding those values.
+ // - In the interest of supporting multi-viewport applications (see 'docking' branch on github),
+ // always subtract draw_data->DisplayPos from clipping bounds to convert them to your viewport space.
+ // - Note that pcmd->ClipRect contains Min+Max bounds. Some graphics API may use Min+Max, other may use Min+Size (size being Max-Min)
+ ImVec2 clip_min((pcmd->ClipRect.x - clip_off.x) * clip_scale.x, (pcmd->ClipRect.y - clip_off.y) * clip_scale.y);
+ ImVec2 clip_max((pcmd->ClipRect.z - clip_off.x) * clip_scale.x, (pcmd->ClipRect.w - clip_off.y) * clip_scale.y);
+ if (clip_max.x <= clip_min.x || clip_max.y <= clip_min.y)
+ continue;
+
+ // We are using scissoring to clip some objects. All low-level graphics API should support it.
+ // - If your engine doesn't support scissoring yet, you may ignore this at first. You will get some small glitches
+ // (some elements visible outside their bounds) but you can fix that once everything else works!
+ MyEngineSetScissor(clip_min.x, clip_min.y, clip_max.x, clip_max.y);
+
+ // The texture for the draw call is specified by pcmd->GetTexID().
+ // The vast majority of draw calls will use the Dear ImGui texture atlas, which value you have set yourself during initialization.
+ MyEngineBindTexture((MyTexture*)pcmd->GetTexID());
+
+ // Render 'pcmd->ElemCount/3' indexed triangles.
+ // By default the indices ImDrawIdx are 16-bit, you can change them to 32-bit in imconfig.h if your engine doesn't support 16-bit indices.
+ MyEngineDrawIndexedTriangles(pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, idx_buffer + pcmd->IdxOffset, vtx_buffer, pcmd->VtxOffset);
+ }
+ }
+ }
+}
+```
+
+### Rendering: Adding support for `ImGuiBackendFlags_RendererHasTextures` (1.92+)
+
+Version [1.92.0](https://github.com/ocornut/imgui/releases/tag/v1.92.0) (June 2025), added texture support in Rendering Backends, which is the backbone for supporting dynamic font scaling among other things.
+
+**In order to move forward and take advantage of all new features, support for `ImGuiBackendFlags_RendererHasTextures` will likely be REQUIRED for all backends before June 2026.**
+
+`ImFontAtlas` functions such as `Build()`, `GetTexDataAsRGBA32()`, `GetTexDataAsAlpha8()`, `SetTexID()`, `IsBuilt()` were obsoleted in favor if iterating a `Textures[]` array and updating their state when requested by Dear ImGui.
+
+**TD;DR: List of commits which added support for `ImGuiBackendFlags_RendererHasTextures` in standard backends:**
+
+- Allegro5: [ee8941e](https://github.com/ocornut/imgui/commit/ee8941e) (+35 lines)
+- DirectX9: [75efba7](https://github.com/ocornut/imgui/commit/75efba7) (+48 lines)
+- DirectX10: [2d2b1bc](https://github.com/ocornut/imgui/commit/2d2b1bc) (+40 lines)
+- DirectX11: [372fd27](https://github.com/ocornut/imgui/commit/372fd27) (+40 lines)
+- DirectX12: [eefe5d5](https://github.com/ocornut/imgui/commit/eefe5d5) (+87 lines)
+- Metal: [26c017d](https://github.com/ocornut/imgui/commit/26c017d) (+55 lines)
+- OpenGL Legacy: [0430c55](https://github.com/ocornut/imgui/commit/0430c55) (+25 lines)
+- OpenGL3/WebGL/ES: [dbb91a5](https://github.com/ocornut/imgui/commit/dbb91a5) (+47 lines)
+- SDL_Renderer2: [9fa65cd](https://github.com/ocornut/imgui/commit/9fa65cd) (+20 lines)
+- SDL_Renderer3: [e538883](https://github.com/ocornut/imgui/commit/e538883) (+19 lines)
+- SDL_GPU: [16fe666](https://github.com/ocornut/imgui/commit/16fe666) (+41 lines)
+- Vulkan: [abe294b](https://github.com/ocornut/imgui/commit/abe294b) (+33 lines)
+- WGPU: [571dae9](https://github.com/ocornut/imgui/commit/571dae9) (+30 lines)
+
+**Instructions:**
+
+- Set `ImGuiBackendFlags_RendererHasTextures` to signify your backend can handle the feature.
+- During rendering, e.g. in your RenderDrawData function, iterate `ImDrawData->Textures` array and process all textures.
+- During shutdown, iterate the `ImGui::GetPlatformIO().Textures` and destroy all textures.
+- (Both arrays are `ImVector`. They are only in different location because: to allow advanced users to perform multi-threaded rendering, we store a pointer to the texture list in ImDrawData, with the aim that multi-threaded rendering users replace it with their own pointer.)
+
+Pseudo-code for processing a texture:
+```cpp
+if (draw_data->Textures != nullptr)
+ for (ImTextureData* tex : *draw_data->Textures)
+ if (tex->Status != ImTextureStatus_OK)
+ MyImGuiBackend_UpdateTexture(tex);
+```
+```cpp
+void MyImGuiBackend_UpdateTexture(ImTextureData* tex)
+{
+ if (tex->Status == ImTextureStatus_WantCreate)
+ {
+ // Create texture based on tex->Width, tex->Height.
+ // - Most backends only support tex->Format == ImTextureFormat_RGBA32.
+ // - Backends for particularly memory constrainted platforms may support tex->Format == ImTextureFormat_Alpha8.
+
+ // Upload all texture pixels
+ // - Read from our CPU-side copy of the texture and copy to your graphics API.
+ // - Use tex->Width, tex->Height, tex->GetPixels(), tex->GetPixelsAt(), tex->GetPitch() as needed.
+
+ // Store your data, and acknowledge creation.
+ tex->SetTexID(xxxx); // Specify backend-specific ImTextureID identifier which will be stored in ImDrawCmd.
+ tex->SetStatus(ImTextureStatus_OK);
+ tex->BackendUserData = xxxx; // Store more backend data if needed (most backend allocate a small texture to store data in there)
+ }
+ if (tex->Status == ImTextureStatus_WantUpdates)
+ {
+ // Upload a rectangle of pixels to the existing texture
+ // - We only ever write to textures regions which have never been used before!
+ // - Use tex->TexID or tex->BackendUserData to retrieve your stored data.
+ // - Use tex->UpdateRect.x/y, tex->UpdateRect.w/h to obtain the block position and size.
+ // - Use tex->Updates[] to obtain individual sub-regions within tex->UpdateRect. Not recommended.
+ // - Read from our CPU-side copy of the texture and copy to your graphics API.
+ // - Use tex->Width, tex->Height, tex->GetPixels(), tex->GetPixelsAt(), tex->GetPitch() as needed.
+
+ // Acknowledge update
+ tex->SetStatus(ImTextureStatus_OK);
+ }
+ if (tex->Status == ImTextureStatus_WantDestroy && tex->UnusedFrames > 0)
+ {
+ // If you use staged rendering and have in-flight renders, changed tex->UnusedFrames > 0 check to higher count as needed e.g. > 2
+
+ // Destroy texture
+ // - Use tex->TexID or tex->BackendUserData to retrieve your stored data.
+ // - Destroy texture in your graphics API.
+
+ // Acknowledge destruction
+ tex->SetTexID(ImTextureID_Invalid);
+ tex->SetStatus(ImTextureStatus_Destroyed);
+ }
+}
+```
+Refer to "List of commits which added support for `ImGuiBackendFlags_RendererHasTextures` in standard backends" above for concrete examples of this.
diff --git a/docs/CHANGELOG.txt b/docs/CHANGELOG.txt
index f722da802..3cc0d2a1b 100644
--- a/docs/CHANGELOG.txt
+++ b/docs/CHANGELOG.txt
@@ -36,42 +36,907 @@ HOW TO UPDATE?
- Please report any issue!
-----------------------------------------------------------------------
- VERSION 1.92.0 WIP (In Progress)
+ VERSION 1.92.4 WIP (In Progress)
-----------------------------------------------------------------------
-Breaking changes:
+Breaking Changes:
-- Backends: SDL3: Fixed casing typo in function name: (#8509, #8163, #7998, #7988) [@puugz]
- - Imgui_ImplSDLGPU3_PrepareDrawData() -> ImGui_ImplSDLGPU3_PrepareDrawData()
+- Viewports: for consistency with other config flags, renamed
+ io.ConfigViewportPlatformFocusSetsImGuiFocus
+ to io.ConfigViewportsPlatformFocusSetsImGuiFocus. (#6299, #6462)
+ It was really a typo in the first place, and introduced in 1.92.2.
+- Backends: Vulkan: moved some fields in ImGui_ImplVulkan_InitInfo:
+ init_info.RenderPass --> init_info.PipelineInfoMain.RenderPass
+ init_info.Subpass --> init_info.PipelineInfoMain.Subpass
+ init_info.MSAASamples --> init_info.PipelineInfoMain.MSAASamples
+ init_info.PipelineRenderingCreateInfo --> init_info.PipelineInfoMain.PipelineRenderingCreateInfo
+ It makes things more consistent and was desirable to introduce new settings for
+ secondary viewports. (#8946, #8110, #8111, #8686) [@ocornut, @SuperRonan, @sylmroz]
+- Backends: Vulkan: renamed ImGui_ImplVulkan_MainPipelineCreateInfo --> ImGui_ImplVulkan_PipelineInfo
+ (introduced very recently and only used by `ImGui_ImplVulkan_CreateMainPipeline()`
+ so it should not affect many users). (#8110, #8111)
+- Backends: Vulkan: helper ImGui_ImplVulkanH_CreateOrResizeWindow() added a
+ `VkImageUsageFlags image_usage` argument to specific extra flags for calls to
+ vkCreateSwapchainKHR() done for secondary viewports. We automatically add
+ `VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT`. In theory the function is an internal
+ helper but since it's used by our examples some may have used it. (#8946, #8111, #8686)
-Other changes:
+Other Changes:
-- IO: variations in analog-only components of gamepad events do not interfere
- with trickling of mouse position events (#4921, #8508)
-- Windows: fixed SetNextWindowCollapsed()/SetWindowCollapsed() breaking
- codepath that preserve last contents size when collapsed, resulting in
- programmatically uncollapsing auto-sizing windows having them flicker size
- for a frame. (#7691) [@achabense]
-- Nav: fixed assertion when holding gamepad FaceLeft/West button to open
- CTRL+Tab windowing + pressing a keyboard key. (#8525)
-- Error Handling: added better error report and recovery for extraneous
- EndPopup() call. (#1651, #8499)
-- Style, InputText: added ImGuiCol_InputTextCursor to configure color of
- the InputText cursor/caret. (#7031)
-- Misc: added extra operators to ImVec4 in IMGUI_DEFINE_MATH_OPERATORS block. (#8510) [@gan74]
-- Backends: SDL2, SDL3, OSX: Fill gamepad inputs and set ImGuiBackendFlags_HasGamepad
- regardless of ImGuiConfigFlags_NavEnableGamepad being set. (#8508)
-- Backends: SDL3: Update for SDL3 api changes: revert SDL_GetClipboardText()
- memory ownership change. (#8530, #7801) [@Green-Sky]
+- Windows: added lower-right resize grip on child windows using both
+ ImGuiChildFlags_ResizeX and ImGuiChildFlags_ResizeY flags. (#8501) [@aleksijuvani]
+ The grip is not visible before hovering to reduce clutter.
+- InputText: fixed single-line InputText() not applying fine character clipping
+ properly (regression in 1.92.3). (#8967) [@Cyphall]
+- IO: added ImGuiPlatformIO::ClearPlatformHandlers(), ClearRendererHandlers()
+ helpers to null all handlers. (#8945, #2769)
+- Misc: Debuggers: added type formatters for the LLDB debuggers (e.g. Xcode,
+ Android Studio & more) to provide nicer display for ImVec2, ImVec4, ImVector etc.
+ See misc/debuggers/ for details. (#8950) [@mentlerd]
+- Textures: fixed a crash if texture status is set to _WantDestroy by a backend after
+ it had already been destroyed. This would typically happen when calling backend's
+ ImGui_ImplXXXX_InvalidateDeviceObjects() helpers twice in a row. (#8977, #8811)
+- Textures: allowed backend to destroy texture while inside the NewFrame/EndFrame
+ scope. Basically if a backend decide to destroy a texture that we didn't request
+ to destroy (for e.g. freeing resources) the texture is immediately set to
+ a _WantCreate status again. (#8811)
+- Textures: fixed an issue preventing multi-contexts sharing a ImFontAtlas from
+ being possible to destroy in any order.
+- Textures: fixed not updating ImTextureData's RefCount when destroying a context
+ using a shared ImFontAtlas, leading standard backends to not properly free
+ texture resources. (#8975) [@icrashstuff]
+- CI: Updates Windows CI scripts to generate/use VulkanSDK. (#8925, #8778) [@yaz0r]
+- Backends: all backends call ImGuiPlatformIO::ClearPlatformHandlers() and
+ ClearRendererHandlers() on shutdown, so as not to leave function pointers
+ which may be dangling when using backend in e.g. DLL. (#8945, #2769)
+- Docs: updated FAQ with new "What is the difference between Dear ImGui and
+ traditional UI toolkits?" entry. (#8862)
+- Backends: DirectX12: reuse a command list and allocator for texture uploads instead
+ of recreating them each time. (#8963, #8465) [@RT2Code]
+- Backends: DirectX12: Rework synchronization logic. (#8961) [@RT2Code]
+ (presumably fixes old hard-to-repro crash issues such as #3463, #5018)
+- Backends: DirectX12: Enable swapchain tearing if available. (#8965) [@RT2Code]
+- Backends: OpenGL3: fixed GL loader to work on Haiku OS which does not support
+ `RTLD_NOLOAD`. (#8952) [@Xottab-DUTY, @threedeyes]
+- Backends: GLFW: fixed build on platform that are neither Windows, macOS or
+ known Unixes (Regression in 1.92.3). (#8969, #8920, #8921) [@oktonion]
+- Backends: SDL2,SDL3: avoid using the SDL_GetGlobalMouseState() path when one of our
+ window is hovered, as the event data is reliable and enough in this case.
+ - Fix mouse coordinates issue in fullscreen apps with macOS notch. (#7919, #7786)
+ - Essentially a working for SDL3 bug which will be fixed in SDL 3.3.0.
+ - Better perf on X11 as querying global position requires a round trip to X11 server.
+- Backends: Win32: minor optimization not submitting gamepad io again if
+ XInput's dwPacketNumber has not changed. (#8556) [@MidTerm-CN]
+- Backends: Vulkan: added a way to specify custom shaders by filling init fields
+ CustomShaderVertCreateInfo and CustomShaderFragCreateInfo. (#8585, #8271) [@johan0A]
+- Backends: DX9,DX10,DX11,DX12,Metal,Vulkan,WGPU,SDLRenderer2,SDLRenderer3:
+ ensure that a texture in _WantDestroy state always turn to _Destroyed even
+ if your underlying graphics data was already destroyed. (#8977)
+- Examples: SDL2+DirectX11: Try WARP software driver if hardware driver is
+ not available. (#5924, #5562)
+- Examples: SDL3+DirectX11: Added SDL3+DirectX11 example. (#8956, #8957) [@tomaz82]
+- Examples: Win32+DirectX12: Rework synchronization logic. (#8961) [@RT2Code]
+- Examples: made examples's main.cpp consistent with returning 1 on error.
Docking+Viewports Branch:
-- Backends: Win32: Viewports: fixed an issue when closing a window from
- the OS close button (with io.ConfigViewportsNoDecoration=false) while
- user code is discarding the 'bool *p_open=false output' from Begin().
+- Nav: fixed a crash that could occur when opening a popup following the processing
+ of a global shortcut while no windows were focused (the fix done in 1.92.3 was
+ incomplete for docking branch).
+- Viewports: added ImGuiBackendFlags_HasParentViewport backend flag for
+ backend to specify if it can honor the viewport->ParentViewportId value by
+ applying the corresponding parent/child relation at the Platform level. (#8948)
+ - SDL3, Win32 backends: supported.
+ - SDL2, GLFW, OSX backends: unsupported.
+- Viewports: fixed a bug where ImGuiWindowFlags_NoBringToFrontOnFocus would effectivey
+ be ignored when windows first appear and viewports are enabled. (#7008) [@jshofmann]
+- Viewports: changed default value of io.ConfigViewportsNoDefaultParent to true. (#8948)
+- Viewports: fixed an issue inferring Z-order when attempting to merge a viewport
+ back in the the main/hosting viewport. (#8948)
+ Note that for GLFW/SDL2/OSX backends, which do not support honoring ParentViewportID.
+ setting io.ConfigViewportsNoDefaultParent=true will align imgui's expectation
+ with what the backend does.
+- Viewports: storing `ImGuiViewport* ParentViewport` pointer along with ParentViewportID.
+- Viewports: DestroyContext() does not call DestroyPlatformWindows() anymore at
+ it assumed to be unnecessary as backensd should have done it and we check that
+ backends have been shutdown since 1.90.4. Changed into asserts. (#7175, #8945)
+- Backends: DX10, DX11, DX12: Disabled DXGI's Alt+Enter default behavior on secondary
+ viewports managed by the backend. (#4350) [@PathogenDavid]
+- Backends: Vulkan: Added a way to configure secondary viewport pipelinen creation
+ by setting init_info.PipelineInfoForViewports fields. (#8946, #8110, #8111, #8686)
+- Backends: Vulkan: Added a way to configure secondary viewport swapchain VkImageUsageFlags
+ to e.g. capture rendering. (#8946, #8940) [@olivier-gerard, @ocornut]
+ Usage example:
+ `init_info.PipelineInfoForViewports.SwapChainImageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT;`
+- Backends: Vulkan: pipeline created for secondary viewport automatically match
+ surface format. (#8686) [@sylmroz]
+- Backends: Vulkan: Added ImGui_ImplVulkanH_GetWindowDataFromViewport() accessor/helper.
+ (#8946, #8940) [@olivier-gerard]
+- Examples: DX10, DX11: Disabled DXGI's Alt+Enter default behavior in examples.
+ Applications are free to leave this enabled, but it does not work properly with
+ multiple viewports. (#4350) [@PathogenDavid]
+
+
+-----------------------------------------------------------------------
+ VERSION 1.92.3 (Released 2025-09-17)
+-----------------------------------------------------------------------
+
+Decorated log and release notes: https://github.com/ocornut/imgui/releases/tag/v1.92.3
+
+Other Changes:
+
+- Fonts: fixed merging a font and specifying a font target in DstFont
+ that's not the last added font (regression in 1.92). (#8912)
+- Fonts: fixed an assertion failure when a rectangle entry has been reused
+ 1024 times (e.g. due to constant change of font size). (#8906) [@cfillion]
+- Clipper, Tables: added ImGuiListClipperFlags_NoSetTableRowCounters as a way to
+ disable the assumption that 1 clipper item == 1 table row, which breaks when
+ e.g. using clipper with ItemsHeight=1 in order to clip in pixel units. (#8886)
+- Scrollbar, Style: added configurable style.ScrollbarPadding value and corresponding
+ ImGuiStyleVar_ScrollbarPadding enum, instead of an hard-coded computed default. (#8895)
+- Nav: fixed Ctrl+Tab window appearing as empty when the sole active and focused
+ window has the ImGuiWindowFlags_NoNavFocus flag. (#8914)
+- Nav: fixed a crash that could occur when opening a popup following the processing
+ of a global shortcut while no windows were focused.
+- Bullet: fixed tessellation which looked out of place in very large sizes.
+- InputText: added ImGuiInputTextFlags_WordWrap flag to word-wrap multi-line buffers.
+ (#3237, #952, #1062, #7363). Current caveats:
+ - This is marked as beta because not being tested enough.
+ Please report any incorrect cursor movement, selection behavior etc. bug to #3237.
+ - Wrapping style is not ideal. Wrapping of long words/sections (e.g. words
+ larger than total available width) may be particularly unpleasing.
+ - Wrapping width needs to always account for the possibility of a vertical scrollbar.
+ - It is currently much slower than regular text fields:
+ - Ballpark estimate of cost on my 2019 desktop PC:
+ For a 100 KB text buffer: +~0.3 ms/+~1.0 ms (Optimized vs Debug builds).
+ - The CPU cost is very roughly proportional to text length, so a 10 KB buffer
+ should cost about ten times less.
+- InputText, InputInt, InputFloat: fixed an issue where using Escape to revert
+ would not write back the reverted value during the IsItemDeactivatedAfterEdit()
+ frame if the provided input buffer doesn't store temporary edits.
+ (regression in 1.91.7) (#8915, #8273)
+- InputText: fixed an issue where using Escape with ImGuiInputTextFlags_EscapeClearsAll
+ would not write back the cleared value during the IsItemDeactivatedAfterEdit()
+ frame if the provided input buffer doesn't store temporary edits. (#8915, #8273)
+- InputText: allow passing an empty string with buf_size==0. (#8907)
+ In theory the buffer size should always account for a zero-terminator, but idioms
+ such as using InputTextMultiline() with ImGuiInputTextFlags_ReadOnly to display
+ a text blob are facilitated by allowing this.
+- InputText: refactored internals to simplify and optimizing rendering of selection.
+ Very large selection (e.g. 1 MB) now take less overhead.
+- InputText: revert a change in 1.79 where pressing Down or PageDown on the last line
+ of a multi-line buffer without a trailing carriage return would keep the cursor
+ unmoved. We revert back to move to the end of line in this situation.
+- InputText: fixed pressing End (without Shift) in a multi-line selection from
+ mistakenly moving cursor based on selection start.
+- Focus, InputText: fixed an issue where SetKeyboardFocusHere() did not work
+ on InputTextMultiline() fields with ImGuiInputTextFlags_AllowTabInput, since
+ they normally inhibit activation to allow tabbing through multiple items. (#8928)
+- Selectable: added ImGuiSelectableFlags_SelectOnNav to auto-select an item when
+ moved into, unless Ctrl is held. (automatic when in a BeginMultiSelect() block).
+- TabBar: fixed an issue were forcefully selecting a tab using internal API would
+ be ignored on first/appearing frame before tabs are submitted (#8929, #6681)
+- DrawList: fixed CloneOutput() unnecessarily taking a copy of the ImDrawListSharedData
+ pointer, which could to issue when deleting the cloned list. (#8894, #1860)
+- DrawList: made AddCallback() assert when passing a null callback.
+- Debug Tools: ID Stack Tool: fixed using fixed-size buffers preventing long identifiers
+ from being displayed in the tool. (#8905, #4631)
+- Debug Tools: ID Stack Tool: when ### is used, uncontributing prefix before the ###
+ is now skipped. (#8904, #4631)
+- Debug Tools: ID Stack Tool: added option to hex-encode non-ASCII characters in
+ output path. (#8904, #4631)
+- Debug Tools: ID Stack Tool: fixed a crash when using PushOverrideID(0) during
+ a query. (#8937, #4631)
+- Debug Tools: Fixed assertion failure when opening a combo box while using
+ io.ConfigDebugBeginReturnValueOnce/ConfigDebugBeginReturnValueLoop. (#8931) [@harrymander]
+- Demo: tweaked ShowFontSelector() and ShowStyleSelector() to update selection
+ while navigating and to not close popup automatically.
+- CI: Updates Windows CI to use a more recent VulkanSDK. (#8925, #8778) [@yaz0r]
+- Examples: Android: Android+OpenGL3: update Gradle project (#8888, #8878) [@scribam]
+- Examples: GLFW+OpenGL2, GLFW+Vulkan, GLFW+Metal, Win32+Vulkan: Fixed not applying
+ content scale consistently with other examples. (#8921, #8756)
+- Backends: GLFW: distinguish X11 vs Wayland to fix various scaling issues.
+ (#8920, #8921) [@TheBrokenRail, @pthom, @ocornut]
+ - window/monitor content scales are always reported as 1.0 on Wayland.
+ - framebuffer scales are always reported as 1.0 on X11.
+- Backends: SDL2: window/monitor content scales are always reported as 1.0 on Wayland.
+ (#8920, #8921) [@TheBrokenRail, @pthom, @ocornut]
+- Backends: SDL3: use SDL_GetWindowDisplayScale() on Mac to obtain DisplayFrameBufferScale,
+ fixing incorrect values during resolution changes e.g. going fullscreen.
+ (#8703, #4414) [@jclounge]
+- Backends: SDL_GPU: Added ImGui_ImplSDLGPU3_InitInfo::SwapchainComposition and
+ PresentMode to configure how secondary viewports are created. Currently only used
+ multi-viewport mode. (#8892) [@PTSVU]
+- Backends: Vulkan: added ImGui_ImplVulkan_CreateMainPipeline() to recreate pipeline
+ without reinitializing backend. (#8110, #8111) [@SuperRonan]
+
+Docking+Viewports Branch:
+
+- DPI: fixed io.ConfigDpiScaleFonts from ever working since 1.92 (the irony
+ being that it is when it started to make sense!). As a reminder, the option
+ automatically copy the _current_ viewport DpiScale to style.FontScaleDpi.
+ This is why we separated the style.FontScaleMain and style.FontScaleDpi scaling
+ factor, as the later is meant to be overwritten. (#8832, #8465)
+- DPI: Fixed obsoleted ImGuiConfigFlags_DpiEnableScaleFonts/_DpiEnableScaleViewports
+ names from setting the equivalent io.ConfigDpiScaleFonts/io.ConfigDpiScaleViewports
+ flag correctly (regression in 1.92).
+- Docking, Style: added style.DockingNodeHasCloseButton option to hide the
+ Close Button attached to each docking node. (#8933)
+- Backends: GLFW: improve multi-viewport behavior in tiling WMs on X11.
+ Note: using GLFW backend on Linux/BSD etc. requires linking with `-lX11`.
+ (#8884, #8474, #8289, #2117) [@Ikos3k, @Madman10K]
+
+
+-----------------------------------------------------------------------
+ VERSION 1.92.2b (Released 2025-08-13)
+-----------------------------------------------------------------------
+
+Decorated log and release notes: https://github.com/ocornut/imgui/releases/tag/v1.92.2b
+
+Changes:
+
+- Fixed IsItemHovered() failing on disabled items and items that have no
+ identifier (e.g. Text() calls) when holding mouse button. (#8877, #8883)
+ [Regression in 1.92.2].
+- Made IsItemHovered() on holding mouse button down on disabled items not
+ leak between items when the window cannot be moved.
+- Backends: Allegro5: Fixed texture format setup which didn't work on all
+ setups/drivers. (#8770, #8465)
+- Backends: Allegro5: Added ImGui_ImplAllegro5_SetDisplay() function to
+ change current ALLEGRO_DISPLAY, as Allegro applications often need to do that.
+- Backends: Allegro5: Fixed missing support for ImGuiKey_PrintScreen
+ under Windows, as raw Allegro 5 does not receive it.
+
+Docking+Viewports Branch:
+
+- Fixed a bug where closing a viewport using OS facility (e.g. ALT+F4, Close Button)
+ would erroneously close all windows located in the viewport, even ones docked
+ into nested dockspaces. Only top-most windows should be closed. (#8887) [@lailoken]
+
+
+-----------------------------------------------------------------------
+ VERSION 1.92.2 (Released 2025-08-11)
+-----------------------------------------------------------------------
+
+Decorated log and release notes: https://github.com/ocornut/imgui/releases/tag/v1.92.2
+
+Breaking Changes:
+
+- Tabs: Renamed ImGuiTabBarFlags_FittingPolicyResizeDown to ImGuiTabBarFlags_FittingPolicyShrink.
+ Kept inline redirection enum (will obsolete). (#261, #351)
+- Backends: SDL_GPU3: changed ImTextureID type from SDL_GPUTextureSamplerBinding* to SDL_GPUTexture*,
+ which is more natural and easier for user to manage. If you need to change the current sampler,
+ you can access the ImGui_ImplSDLGPU3_RenderState struct. (#8866, #8163, #7998, #7988)
+
+Other Changes:
+
+- Fixed an old inconsistency between IsItemHovered() and internal hovering check,
+ where IsItemHovered() would return true to mouse was first clicked on the
+ background of a non-moveable window then moved over the item or button.
+ Note that while it is consistent with other logic, there is a possibility
+ that some third-party code may accidentally relied on this. One can always use
+ ImGuiHoveredFlags_AllowWhenBlockedByActiveItem to bypass the active id check.
+ (#8877) [@achabense, @ocornut]
+- Fonts: fixed an issue when a font using MergeMode has a reference size
+ specified but the target font doesn't. Usually either all fonts should
+ have a reference size (only required when specifying e.g. GlyphOffset),
+ or none should have a reference size.
+- Fonts: fixed a crash when changing texture format when using a legacy
+ backend. Most commonly would happen when calling GetTexDataAsRGBA32()
+ then immediately calling GetTexDataAsAlpha8(). (#8824)
+- Windows: fixed an issue where resizable child windows would emit border
+ logic when hidden/non-visible (e.g. when in a docked window that is not
+ selected), impacting code not checking for BeginChild() return value. (#8815)
+- Textures: Fixed support for `#define ImTextureID_Invalid` to non-zero value:
+ ImFontAtlas() was incorrectly cleared with zeroes. (#8860, #8745) [@cfillion]
+- Tables: fixed TableGetRowIndex() which never correctly worked when using
+ a clipper (it exists for consistency but is almost never used, as it is
+ often more convenient to use index in caller-code, whereas TableGetRowIndex()
+ includes header rows).
+- Tables: fixed imgui_internal.h's TableGetHoveredRow() the same way. (#7350, #6588, #6250)
+- Tabs: added new fitting policy ImGuiTabBarFlags_FittingPolicyMixed
+ and made it the default. This policy shrink tab width down to a given amount,
+ and then beyond that it enable scrolling buttons. (#3421, #8800)
+- Tabs: added style.TabMinWidthShrink, ImGuiStyleVar_TabMinWidthShrink to
+ control the width to shrink to in ImGuiTabBarFlags_FittingPolicyMixed mode.
+ (#3421, #8800).
+- Tabs: when scrolling is enabled, track selected tabs when resizing down
+ parent container. This does not prevent to horizontally scroll it out of
+ view during normal operations. (#3421, #8800)
+- Tabs: added style.TabMinWidthBase, ImGuiStyleVar_TabMinWidthBase to control
+ the base minimum width of a tab (default to 1.0f). This is the size before
+ any potential shrinking is applied.
+- Tabs: fixed tab bar underline not drawing below scroll buttons, when
+ they are enabled (minor regression from 1.90). (#6820, #4859, #5022, #5239)
+- Tabs: made scrolling buttons never keyboard/gamepad navigation candidates.
+- Nav, Tables: fixed navigation within scrolling tables when item boundaries
+ goes beyond columns limits. The fix done in 1.89.6 didn't work correctly
+ on scrolling windows. (#8816, #2221)
+- Nav: fixed a bug where ImGuiKey_NavGamepadMenu (==ImGuiKey_GamepadFaceLeft)
+ button couldn't toggle between main and menu layers while navigating a Modal
+ window. (#8834)
+- Error Handling: minor improvements to error handling for TableGetSortSpecs()
+ and TableSetBgColor() calls. (#1651, #8499)
+- Misc: fixed building with IMGUI_DISABLE_DEBUG_TOOLS only. (#8796)
+- Misc: fixed building with IMGUI_DISABLE_STB_TRUETYPE_IMPLEMENTATION. (#8794)
+- Misc: removed more redundant inline static linkage from imgui_internal.h to
+ facilitate using in C++ modules. (#8813, #8682, #8358) [@stripe2933]
+- Misc: ImVector: skip memcpy in operator= if `Data` isn't initialized in order
+ to play nice with -fsanitize=undefined. (#8874) [@i25e]
+- CI: Added SDL3 builds to MacOS and Windows. (#8819, #8778) [@scribam]
+- CI: Updated Windows CI to use a more recent SDL2. (#8819, #8778) [@scribam]
+- Examples: SDL3+Metal: added SDL3+Metal example. (#8827, #8825) [@shi-yan]
+- Examples: SDL3+SDL_GPU: use SDL_WaitAndAcquireGPUSwapchainTexture() instead
+ of SDL_AcquireGPUSwapchainTexture(). (#8830) [@itsdanott]
+- Examples: SDL3+SDL_GPU: use SDL_GPU_PRESENTMODE_VSYNC present mode.
+- Backends: OpenGL3: add and call embedded loader shutdown in ImGui_ImplOpenGL3_Shutdown()
+ to facilitate multiple init/shutdown cycles in same process. (#8792) [@tim-rex]
+- Backends: OpenGL2, OpenGL3: set GL_UNPACK_ALIGNMENT to 1 before updating
+ textures. (#8802) [@Daandelange]
+- Backends: SDL_GPU3: expose current SDL_GPUSampler* in the ImGui_ImplSDLGPU3_RenderState
+ struct. (#8866, #8163, #7998, #7988)
+- Backends: Vulkan: Fixed texture update corruption introduced in 1.92.0,
+ affecting some drivers/setups. (#8801, #8755, #8840) [@Retro52, @Miolith]
+- Backends: Vulkan: Avoid calling vkCmdBindDescriptorSets() when texture
+ has not changed. (#8666) [@micb25]
+
+Docking+Viewports Branch:
+
+- Windows, Viewport: fixed an issue where interrupting a viewport move with
+ e.g. a ClearActiveID() call would leave the dragged viewport with the
+ normally temporary ImGuiViewportFlags_NoInputs flag, preventing further
+ interactions with the viewport. (#5324) (thanks @mdelaharpe)
+- Viewports: added io.ConfigViewportPlatformFocusSetsImGuiFocus to opt-out
+ of focusing imgui windows When a platform window is focused (e.g. using Alt+Tab,
+ clicking Platform Title Bar). In principle this is better enabled but we
+ provide an opt-out because some Linux window managers tend to eagerly focus
+ windows (on e.g. mouse hover, or even on a simple window pos/size change).
+ (#6299, #6462)
+
+
+-----------------------------------------------------------------------
+ VERSION 1.92.1 (Released 2025-07-09)
+-----------------------------------------------------------------------
+
+Decorated log and release notes: https://github.com/ocornut/imgui/releases/tag/v1.92.1
+
+Changes:
+
+- Fonts: added ImFontAtlas::SetFontLoader() to dynamically change font
+ loader at runtime without using internal API. (#8752, #8465)
+- Fonts: fixed a bug where dynamically changing font loader would lose
+ the Fallback and Ellipsis glyphs under some circumstance. (#8763)
+- Fonts: for large size fonts, layout/size calculation only load glyphs metrics.
+ Actual glyphs are renderer+packed when used by drawing functions. (#8758, #8465)
+- Fonts: set a maximum font size of 512.0f at ImGui:: API level to reduce
+ edge cases (e.g. out of memory errors). ImDrawList:: API doesn't have the
+ constraint. (#8758)
+- Fonts: Restore ImFontConfig::FontNo being a 32-bits value as this is needed
+ to pass full range of information into e.g. FreeType's face_index, as higher
+ bits are used from FreeType 2.6.1. (#8775) [@Valakor]
+ (the field has been erroneously reduced from 32-bits to 8-bit in 1.92.0)
+- Fonts, Tables: fixed PushFont() having no effect when called after submitting
+ a hidden column. (#8865)
+- Textures: Fixed support for `#define ImTextureID_Invalid` to non-zero value:
+ ImTextureData() was incorrectly cleared with zeroes. (#8745) [@rachit7645]
+- Demo: Added "Text -> Font Size" demo section. (#8738) [@Demonese]
+- CI: Fixed dllimport/dllexport tests. (#8757) [@AidanSun05]
+- CI: Updated to use latest Windows image + VS2022.
+- Debug Tools: added IMGUI_DEBUG_HIGHLIGHT_ALL_ID_CONFLICTS to detect
+ id conflicts _before_ hovering. This is very slow and should only be used
+ temporarily. (#8651, #7961, #7669)
+- Examples: GLFW+OpenGL3, GLFW+WGPU: Emscripten Makefiles uses GLFW port
+ 'contrib.glfw3' which offers better HiDPI support. (#8742) [@pthom]
+- Backends: GLFW, SDL2 made ImGui_ImplGLFW_GetContentScaleXXX() and
+ ImGui_ImplSDL2_GetContentScaleXXXX() helpers return 1.0f on Emscripten
+ and Android platforms, matching macOS logic. (#8742, #8733) [@pthom]
+- Backends: SDL3: avoid calling SDL_StartTextInput() again if already active.
+ (fixes e.g.: an issue on iOS where the keyboard animation will popup every
+ time the user types a key + probably other things) (#8727) [@morrazzzz]
+- Backends: OSX: added ImGuiMouseCursor_Wait and ImGuiMouseCursor_Progress
+ mouse cursor support. (#8739) [@cfillion]
+- Backends: Allegro5: fixed texture update broken on some platforms where
+ ALLEGRO_LOCK_WRITEONLY needed all texels to be rewritten. (#8770)
+- Backends: Vulkan: use nonCoherentAtomSize to align upload_size, fixing
+ validation error on some setups. (#8743, #8744) [@tquante]
+- Backends: Vulkan: fixed texture synchronization issue introduced in 1.92.0,
+ leading to validation layers reacting. (#8772) [@Majora320]
+
+Docking+Viewports Branch:
+
+- Backends: OSX: Fixed multi-viewport handling broken in 1.92.0. (#8644, #8777) [@cfillion]
+
+
+-----------------------------------------------------------------------
+ VERSION 1.92.0 (Released 2025-06-25)
+-----------------------------------------------------------------------
+
+Decorated log and release notes: https://github.com/ocornut/imgui/releases/tag/v1.92.0
+
+THIS VERSION CONTAINS THE LARGEST AMOUNT OF BREAKING CHANGES SINCE 2015!
+I TRIED REALLY HARD TO KEEP THEM TO A MINIMUM, REDUCE THE AMOUNT OF INTERFERENCE,
+BUT INEVITABLY SOME USERS OR THIRD-PARTY EXTENSIONS WILL BE AFFECTED.
+
+For instructions to upgrade your custom backend:
+--> See https://github.com/ocornut/imgui/blob/master/docs/BACKENDS.md
+
+IN ORDER TO HELP US IMPROVE THE TRANSITION PROCESS, INCL. DOCUMENTATION AND COMMENTS,
+PLEASE REPORT **ANY** DOUBT, CONFUSION, QUESTIONS, FEEDBACK TO:
+ https://github.com/ocornut/imgui/issues/
+If you are using custom widgets, internals or third-party extension that are somehow
+breaking and aren't obvious how to solve, please post in Issues so we can gather
+data and share solutions that may help others.
+
+As part of the plan to reduce impact of API breaking changes, several unfinished
+changes/features/refactors related to font and text systems and scaling will be
+part of subsequent releases (1.92.1+).
+
+If you are updating from an old version, and expecting a massive or difficult update,
+consider first updating to 1.91.9 to reduce the amount of changes.
+
+Breaking changes:
+
+- Fonts: **IMPORTANT**: if your app was solving the OSX/iOS Retina screen specific
+ logical vs display scale problem by setting io.DisplayFramebufferScale (e.g. to 2.0f)
+ + setting io.FontGlobalScale (e.g. to 1.0f/2.0f) + loading fonts at scaled sizes (e.g. size X * 2.0f):
+ - This WILL NOT map correctly to the new system! Because font will rasterize as requested size.
+ - With a legacy backend (< 1.92):
+ - Instead of setting io.FontGlobalScale = 1.0f/N -> set ImFontCfg::RasterizerDensity = N.
+ - This already worked before, but is now pretty much required.
+ - With a new backend (1.92+),
+ - This should be all automatic.
+ - FramebufferScale is automatically used to set current font RasterizerDensity.
+ - FramebufferScale is a per-viewport property provided by backend through the
+ Platform_GetWindowFramebufferScale() handler in 'docking' branch.
+- Fonts: **IMPORTANT** on Font Sizing:
+ - Before 1.92, fonts were of a single size. They can now be dynamically sized.
+ - PushFont() API now has a REQUIRED size parameter.
+ void PushFont(ImFont* font) --> void PushFont(ImFont* font, float size);
+ - PushFont(font, 0.0f) // Change font and keep current size
+ - PushFont(NULL, 20.0f) // Keep font and change current size
+ - PushFont(font, 20.0f) // Change font and set size to 20.0f
+ - PushFont(font, style.FontSizeBase * 2.0f) // Change font and set size to be twice bigger than current size.
+ - PushFont(font, font->LegacySize) // Change font and set size to size passed to AddFontXXX() function. Same as pre-1.92 behavior, for fixed size fonts.
+ - To use old behavior use 'ImGui::PushFont(font, font->LegacySize)' at call site.
+ We intentionally didn't add a default parameter because it would make the long-term
+ transition more difficult.
+ - Kept inline redirection font. Will obsolete.
+ - Global scale factors may be applied over the provided size.
+ This is why it is called 'FontSizeBase' in the style structure.
+ - Global scale factors are: 'style.FontScaleMain', 'style.FontScaleDpi' and maybe more.
+ - ImFont::FontSize was removed and does not make sense anymore.
+ - ImFont::LegacySize is the size passed to AddFont().
+ - Removed support for old PushFont(NULL) which was a shortcut for "revert to default font".
+ `PushFont(NULL, some_size)` now keeps current change and changes size.
+ - Renamed/moved 'io.FontGlobalScale' to 'style.FontScaleMain'.
+- Fonts: **IMPORTANT** on Font Merging:
+ - When searching for a glyph in multiple merged fonts: we search for the FIRST font source
+ which contains the desired glyph. Because the user doesn't need to provide glyph ranges
+ any more, it is possible that a glyph that you expected to fetch from a secondary/merged
+ icon font may be erroneously fetched from the primary font.
+ - We added `ImFontConfig::GlyphExcludeRanges[]` to specify ranges to exclude from a given font source:
+ // Add Font Source 1 but ignore ICON_MIN_FA..ICON_MAX_FA range
+ static ImWchar exclude_ranges[] = { ICON_MIN_FA, ICON_MAX_FA, 0 };
+ ImFontConfig cfg1;
+ cfg1.GlyphExcludeRanges = exclude_ranges;
+ io.Fonts->AddFontFromFileTTF("segoeui.ttf", 0.0f, &cfg1);
+ // Add Font Source 2, which expects to use the range above
+ ImFontConfig cfg2;
+ cfg2.MergeMode = true;
+ io.Fonts->AddFontFromFileTTF("FontAwesome4.ttf", 0.0f, &cfg2);
+ - You can use `Metrics/Debugger->Fonts->Font->Input Glyphs Overlap Detection Tool` to
+ see list of glyphs available in multiple font sources. This can facilitate understanding
+ which font input is providing which glyph.
+- Fonts: **IMPORTANT** on Thread Safety:
+ - A few functions such as font->CalcTextSizeA() were by sheer luck (== accidentally)
+ thread-safe even thou we had never provided that guarantee before. They are
+ definitively not thread-safe anymore as new glyphs may be loaded.
+
+- Textures:
+ - All API functions taking a 'ImTextureID' parameter are now taking a 'ImTextureRef':
+ - ImTextureRef ais small composite structure which may be constructed from a ImTextureID.
+ (or constructed from a ImTextureData* which represent a texture which will generally
+ be ready by the time of rendering).
+ - Affected functions are:
+ - ImGui::Image(), ImGui::ImageWithBg(), ImGui::ImageButton(),
+ - ImDrawList::AddImage(), ImDrawList::AddImageQuad(), ImDrawList::AddImageRounded().
+ - We suggest that C users and any higher-level language bindings generators may
+ facilitate converting this in some way, aka emulating the trivial C++ constructor.
+- Fonts: obsoleted ImFontAtlas::GetTexDataAsRGBA32(), GetTexDataAsAlpha8(), Build(), SetTexID()
+ and IsBuilt() functions. The new protocol for backends to handle textures doesn't need them.
+ Kept redirection functions (will obsolete).
+ - A majority of old backends should still work with new code (behaving like they did before).
+ - For instructions to upgrade your custom backend:
+ https://github.com/ocornut/imgui/blob/master/docs/BACKENDS.md
+ - Calling ImFontAtlas::Build() before initializing new backends will erroneously trigger
+ preloading all glyphs. Will be detected with an assertion after the backend is initialized.
+- Fonts: ImFontConfig::OversampleH/OversampleV default to automatic (== 0)
+ since v1.91.8. It is quite important you keep it automatic until we decide if we want
+ to provide a way to express finer policy, otherwise you will likely waste texture space
+ when using large glyphs. Note that the imgui_freetype backend doesn't use and does not
+ need oversampling.
+- Fonts: specifying glyph ranges is now unnecessary.
+ - The value of ImFontConfig::GlyphRanges[] is only useful for legacy backends.
+ - All GetGlyphRangesXXXX() functions are now marked obsolete:
+ - GetGlyphRangesDefault(), GetGlyphRangesGreek(), GetGlyphRangesKorean(),
+ GetGlyphRangesJapanese(), GetGlyphRangesChineseSimplifiedCommon(),
+ GetGlyphRangesChineseFull(), GetGlyphRangesCyrillic(),
+ GetGlyphRangesThai(), GetGlyphRangesVietnamese().
+- Fonts: removed ImFontAtlas::TexDesiredWidth to enforce a texture width. (#327)
+ (it vaguely made sense with the old system as if unspecified textures width maxed up
+ to 4096 but that limit isn't necessary anymore, and Renderer_TextureMaxWidth covers this)
+ However you may set TexMinWidth = TexMaxWidth for the same effect.
+- Fonts: if you create and manage ImFontAtlas instances yourself (instead of relying on
+ ImGuiContext to create one), you'll need to call ImFontAtlasUpdateNewFrame() yourself.
+ An assert will trigger if you don't.
+- Fonts: obsoleted ImGui::SetWindowFontScale() which is not useful anymore. Prefer using
+ PushFont(NULL, style.FontSizeBase * factor) or to manipulate other scaling factors.
+- Fonts: obsoleted ImFont::Scale which is not useful anymore.
+- Fonts: changed ImFont::CalcWordWrapPositionA() to ImFont::CalcWordWrapPosition():
+ - old: const char* CalcWordWrapPositionA(float scale, const char* text, ....);
+ - new: const char* CalcWordWrapPosition (float size, const char* text, ....);
+ The leading 'float scale' parameters was changed to 'float size'.
+ This was necessary as 'scale' is assuming a unique font size.
+ Kept inline redirection function assuming using font->LegacySize.
+- Fonts: generally reworked Internals of ImFontAtlas and ImFont.
+ While in theory a vast majority of users shouldn't be affected, some use cases or
+ extensions might be. Among other things:
+ - ImDrawCmd::TextureId has been changed to ImDrawCmd::TexRef.
+ - ImFontAtlas::TexID has been changed to ImFontAtlas::TexRef.
+ - ImFontAtlas::ConfigData[] has been renamed to ImFontAtlas::Sources[].
+ - ImFont::ConfigData[], ConfigDataCount has been renamed to Sources[], SourceCount.
+ - Each ImFont has a number of ImFontBaked instances corresponding to actively used
+ sizes. ImFont::GetFontBaked(size) retrieves the one for a given size.
+ - Fields moved from ImFont to ImFontBaked:
+ - ImFont::IndexAdvanceX[] -> ImFontBaked::IndexAdvanceX[]
+ - ImFont::Glyphs[] -> ImFontBaked::Glyphs[]
+ - ImFont::Ascent, Descent -> ImFontBaked::Ascent, Descent
+ - ImFont::FindGlyph() -> ImFontBaked::FindGlyph()
+ - ImFont::FindGlyphNoFallback() -> ImFontBaked::FindGlyphNoFallback()
+ - ImFont::GetCharAdvance() -> ImFontBaked::GetCharAdvance()
+ - Widget code may use ImGui::GetFontBaked() instead of ImGui::GetFont() to
+ access font data for current font at current font size.
+ (and you may use font->GetFontBaked(size) to access it for any other size.)
+ g.Font == ImGui::GetFont()
+ g.FontSize == ImGui::GetFontSize()
+ g.FontBaked == ImGui::GetFontBaked() == ImGui::GetFont()->GetFontBaked(ImGui::GetFontSize())
+ - Fields moved from ImFontAtlas to ImTextureData
+ - ImFontAtlas->TexWidth -> ImFontAtlas->TexData->Width
+ - ImFontAtlas->TexHeight -> ImFontAtlas->TexData->Height
+ - ImFontAtlas->TexPixelsAlpha8 -> ImFontAtlas->TexData->GetPixels() (when ImFontAtlas::TexDesiredFormat == ImTextureFormat_Alpha8)
+ - ImFontAtlas->TexPixelsRGBA32 -> ImFontAtlas->TexData->GetPixels() (when ImFontAtlas::TexDesiredFormat == ImTextureFormat_RGBA32)
+ Please report if you are affected!
+- Fonts: (users of imgui_freetype)
+ - renamed ImFontAtlas::FontBuilderFlags to ImFontAtlas::FontLoaderFlags.
+ - renamed ImFontConfig::FontBuilderFlags to ImFontConfig::FontLoaderFlags.
+ - renamed ImGuiFreeTypeBuilderFlags to ImGuiFreeTypeLoaderFlags.
+ - if you used runtime imgui_freetype selection rather than the default compile-time
+ option provided by IMGUI_ENABLE_FREETYPE:
+ - renamed/reworked ImFontBuilderIO into ImFontLoader,
+ - renamed ImGuiFreeType::GetBuilderForFreeType() to ImGuiFreeType::GetFontLoader()
+ - old: io.Fonts->FontBuilderIO = ImGuiFreeType::GetBuilderForFreeType()
+ - new: io.Fonts->FontLoader = ImGuiFreeType::GetFontLoader()
+- DrawList: Renamed ImDrawList::PushTextureID()/PopTextureID() to PushTexture()/PopTexture().
+- Fonts: (users of custom rectangles)
+ - Renamed AddCustomRectRegular() to AddCustomRect(). (#8466)
+ - Added GetCustomRect() as a replacement for GetCustomRectByIndex() + CalcCustomRectUV(). (#8466)
+ - The output type of GetCustomRect() is now ImFontAtlasRect, which include UV coordinates.
+ - ImFontAtlasCustomRect::X --> ImFontAtlasRect::x
+ - ImFontAtlasCustomRect::Y --> ImFontAtlasRect::y
+ - ImFontAtlasCustomRect::Width --> ImFontAtlasRect::w
+ - ImFontAtlasCustomRect::Height --> ImFontAtlasRect::h
+ Before:
+ const ImFontAtlasCustomRect* r = atlas->GetCustomRectByIndex(custom_rect_id);
+ ImVec2 uv0, uv1;
+ atlas->GetCustomRectUV(r, &uv0, &uv1);
+ ImGui::Image(atlas->TexRef, ImVec2(r->w, r->h), uv0, uv1);
+ After:
+ ImFontAtlasRect r;
+ atlas->GetCustomRect(custom_rect_id, &r);
+ ImGui::Image(atlas->TexRef, ImVec2(r.w, r.h), r.uv0, r.uv1);
+ We added a redirecting typedef but haven't attempted to magically redirect
+ the field names, as this API is rarely used and the fix is simple.
+ - Obsoleted AddCustomRectFontGlyph() as the API does not make sense for scalable fonts:
+ - Kept existing function which uses the font "default size" (Sources[0]->LegacySize).
+ - Added a helper AddCustomRectFontGlyphForSize() which is immediately marked obsolete,
+ but can facilitate transitioning old code.
+ - Prefer adding a font source (ImFontConfig) using a custom/procedural loader.
+- Backends: removed ImGui_ImplXXXX_CreateFontsTexture()/ImGui_ImplXXXX_DestroyFontsTexture()
+ for all backends that had them. They should not be necessary any more.
+ - removed ImGui_ImplMetal_CreateFontsTexture(), ImGui_ImplMetal_DestroyFontsTexture().
+ - removed ImGui_ImplOpenGL2_CreateFontsTexture(), ImGui_ImplOpenGL2_DestroyFontsTexture().
+ - removed ImGui_ImplOpenGL3_CreateFontsTexture(), ImGui_ImplOpenGL3_DestroyFontsTexture().
+ - removed ImGui_ImplSDLGPU3_CreateFontsTexture(), ImGui_ImplSDLGPU3_DestroyFontsTexture().
+ - removed ImGui_ImplSDLRenderer2_CreateFontsTexture(), ImGui_ImplSDLRenderer2_DestroyFontsTexture().
+ - removed ImGui_ImplSDLRenderer3_CreateFontsTexture(), ImGui_ImplSDLRenderer3_DestroyFontsTexture().
+ - removed ImGui_ImplVulkan_CreateFontsTexture(), ImGui_ImplVulkan_DestroyFontsTexture().
+- Layout: commented out legacy ErrorCheckUsingSetCursorPosToExtendParentBoundaries() fallback
+ obsoleted in 1.89 (August 2022) which allowed a SetCursorPos()/SetCursorScreenPos() call WITHOUT AN ITEM
+ to extend parent window/cell boundaries. Replaced with assert/tooltip that would already happens if
+ previously using IMGUI_DISABLE_OBSOLETE_FUNCTIONS. (#5548, #4510, #3355, #1760, #1490, #4152, #150)
+ - Incorrect way to make a window content size 200x200:
+ Begin(...) + SetCursorScreenPos(GetCursorScreenPos() + ImVec2(200,200)) + End();
+ - Correct ways to make a window content size 200x200:
+ Begin(...) + SetCursorScreenPos(GetCursorScreenPos() + ImVec2(200,200)) + Dummy(ImVec2(0,0)) + End();
+ Begin(...) + Dummy(ImVec2(200,200)) + End();
+ - TL;DR; if the assert triggers, you can add a Dummy({0,0}) call to validate extending parent boundaries.
+- TreeNode: renamed ImGuiTreeNodeFlags_NavLeftJumpsBackHere to ImGuiTreeNodeFlags_NavLeftJumpsToParent
+ for clarity. Kept inline redirection enum (will obsolete). (#1079, #8639)
+- Commented out PushAllowKeyboardFocus()/PopAllowKeyboardFocus() which was obsoleted
+ in 1.89.4 (March 2023). (#3092)
+ - PushAllowKeyboardFocus(bool tab_stop) --> PushItemFlag(ImGuiItemFlags_NoTabStop, !tab_stop);
+ - PopAllowKeyboardFocus() --> PopItemFlag().
+- Commented out ImGuiListClipper::ForceDisplayRangeByIndices() which was obsoleted
+ in 1.89.6 (June 2023).
+ - ForceDisplayRangeByIndices() --> IncludeItemsByIndex()
+- Backends: SDL3: Fixed casing typo in function name: (#8509, #8163, #7998, #7988) [@puugz]
+ - Imgui_ImplSDLGPU3_PrepareDrawData() --> ImGui_ImplSDLGPU3_PrepareDrawData()
+- Internals: RenderTextEllipsis() function removed the 'float clip_max_x' parameter directly
+ preceding 'float ellipsis_max_x'. Values were identical for a vast majority of users. (#8387)
+- [Docking] renamed/moved ImGuiConfigFlags_DpiEnableScaleFonts -> bool io.ConfigDpiScaleFonts.
+- [Docking] renamed/moved ImGuiConfigFlags_DpiEnableScaleViewports -> bool io.ConfigDpiScaleViewports.
+ **Neither of those flags are very useful in current code. They will be useful once we merge font changes.**
+
+Non-breaking Fonts/Textures related changes:
+
+- Textures: added partial texture update protocol. (#8465, #3761)
+ - The Renderer Backend needs to set io.BackendFlags |= ImGuiBackendFlags_RendererHasTextures
+ and handle texture updates requests.
+ - New structs: ImTextureData, ImTextureRect.
+ - New enums: ImTextureStatus, ImTextureFormat.
+ - During its ImGui_ImplXXXX_RenderDrawData() call, the backend can now access a texture list
+ in ImDrawData::Textures[]. Textures may have four distinct states:
+ - ImTextureStatus_WantCreate: requesting backend to create a texture.
+ - ImTextureStatus_WantUpdates: requesting backend to copy given blocks from the CPU side
+ copy of the texture to your graphics pipeline.
+ A 'tex->Updates[]' list of update is provided as well as a single 'tex->UpdatesRect' bounding box.
+ - ImTextureStatus_WantDestroy: requesting backend to destroy the texture.
+ - A 'int UnusedFrames' value is provided to conveniently defer destroying.
+ - Backend is generally free to destroy textures whenever they like.
+ - ImTextureStatus_OK: nothing to do.
+ - Almost all standard backends have been updated to support this.
+ - Backends have allowed to destroy textures at any time if they desire so.
+ A list is available in platform_io.Textures[] for this purpose and for backend shutdown.
+ - Both stb_truetype and FreeType backends have been updated to work with the new
+ system, and they now share more code than before.
+ - Added '#define IMGUI_HAS_TEXTURES' to facilitate compile-time checks for third-party
+ extensions until this is merged with a definitive version number to check.
+- Fonts: font backend/loader may easily be changed dynamically, allowing users to compare
+ rasterizers outputs and features. imgui_freetype is generally beneficial.
+- Fonts: ImFontAtlas::AddFontXXX() functions may be called at any time during the frame.
+- Fonts: ImFontAtlas::AddFontXXX() can fail more gracefully if error handling is configured
+ to not assert (this will be better exposed via future font flags).
+- Fonts: added style.FontScaleBase scaling factor (previously called io.FontGlobalScale).
+- Fonts: added style.FontScaleDpi scaling factor. This is designed to be be changed on
+ per-monitor/per-viewport basis, which `io.ConfigDpiScaleFonts` does automatically.
+ (which is why it is separate from FontScaleBase).
+- Fonts: added optional font_size parameter to ImGui::PushFont() function.
+- Fonts: added ImFontAtlas::RemoveFont() function.
+- Fonts: added ImFontAtlas::CompactCache() function.
+- Fonts: added ImFontAtlas::TexDesiredFormat field (default to ImTextureFormat_RGBA32,
+ can be changed to ImTextureFormat_Alpha8).
+- Fonts: added ImFontAtlas::TexMinWidth, TexMinHeight, TexMaxWidth, TexMaxHeight fields.
+- Fonts: added ImFontConfig::PixelSnapV to align scaled GlyphOffset.y to pixel boundaries.
+- Fonts: added ImFontConfig::GlyphExcludeRanges[], which behave similarly to
+ ImFontConfig::GlyphRanges[], but has the opposite meaning. It is tailored to situations
+ where merged fonts have overlapping characters.
+- Fonts: added "Input Glyphs Overlap Detection Tool" which dumps a list of glyphs
+ provided by merged sources, which may help setting up a GlyphExcludeRanges[] filter.
+- Fonts: added ImFontAtlas::FontBackendName (which is surfaced in the "About Dear ImGui"
+ window and other locations).
+- Fonts: added ImFontFlags (currently needs to be passed through ImFontConfig until
+ we revamp font loading API):
+ - ImFontFlags_NoLoadError: disable erroring/assert when calling AddFontXXX()
+ with missing file/data. Calling code is expected to check AddFontXXX() return value.
+ - ImFontFlags_NoLoadGlyphs: disable loading new glyphs.
+ - ImFontFlags_LockBakedSizes: disable loading new baked sizes, disable garbage
+ collecting current ones. e.g. if you want to lock a font to a single size.
+- Fonts: the general design has changed toward meaning that a ImFont doesn't have
+ have a specific size: it may be bound and drawn with any size.
+ - We store ImFontBaked structures internally, which are a cache of information
+ for a given size being drawn. You should not need to deal with ImFontBaked directly.
+ - ImFontBaked structures may be cleaned up between frames when unused, pointers
+ to them are only valid for the current frame.
+ - Added ImFontBaked::IsGlyphLoaded() function.
+- Fonts: Custom Rect packing has generally been reworked. (#8107, #7962, #1282)
+ - ImFontAtlas::AddCustomRect() (previously AddCustomRectRegular()/AddCustomRectFontGlyph())
+ functions will immediately return a packed rectangle identifier, and you can write your
+ pixels immediately - previously had to wait for Build() to be called.
+ This is also the case when using a legacy backend.
+ - Custom packed rectangles may be moved during a texture change, aka practically anytime.
+ Always refer to latest uvs/position returned by GetCustomRect().
+ - AddCustomRect() returns ImFontAtlasRectId_Invalid on failure.
+ - Added ImFontAtlas::RemoveCustomRect() function.
+ - GetCustomRect() can safely return false and not crash when passed an invalid or removed id.
+- Fonts: texture is now stored in a single format CPU side (save ~25% when using RGBA).
+- Fonts: changing current font to one from a different atlas is supported. (#8080)
+- Fonts: automatic baking of an "..." ellipsis works better with monospace fonts.
+- Fonts: each ImFontConfig font source may provide a custom backend/loader.
+- Fonts: added platform_io.Renderer_TextureMaxWidth/Renderer_TextureMaxHeight fields
+ for Renderer Backend to specify if there is a maximum accepted texture size (not yet used).
+- Fonts: added compile-time overridable '#define ImTextureID_Invalid 0' if you need 0
+ to be a valid low-level texture identifier.
+- Fonts: reworked text ellipsis logic to ensure a "..." is always displayed instead
+ of a single character. (#7024)
+- Fonts: word-wrapping code handle ideographic comma & full stop (U+3001, U+3002). (#8540)
+- Fonts: fixed CalcWordWrapPosition() fallback when width is too small to wrap:
+ would use a +1 offset instead of advancing to the next UTF-8 codepoint. (#8540)
+- Debug Tools: Fonts section: add font preview, add "Remove" button, add "Load all glyphs"
+ button, add selection to change font backend when both are compiled in.
+- Renderer Backends:
+ - Backends: DX9/DX10/DX11/DX12, Vulkan, OpenGL2/3, Metal, SDLGPU3, SDLRenderer2/3, WebGPU, Allegro5:
+ - Added ImGuiBackendFlags_RendererHasTextures support for all backends. (#8465, #3761, #3471)
+ [@ocornut, @ShironekoBen, @thedmd]
+ - Added ImGui_ImplXXXX_UpdateTexture(ImTextureData* tex) functions for all backends.
+ Available if you want to start uploading textures right after ImGui::Render() and without
+ waiting for the call to ImGui_ImplXXXX_RenderDrawData(). Also useful if you use a staged or
+ multi-threaded rendering schemes, where you might want to set ImDrawData::Textures = NULL. (#8597, #1860)
+- Special thanks for fonts/texture related feedback to: @thedmd, @ShironekoBen, @rodrigorc,
+ @pathogendavid, @itamago, @rokups, @DucaRii, @Aarkham, @cyfewlp.
+
+Other Changes:
+
+- IO: variations in analog-only components of gamepad events do not interfere
+ with trickling of mouse position events (#4921, #8508)
+- Windows: fixed SetNextWindowCollapsed()/SetWindowCollapsed() bypassing the
+ codepath that preserve last contents size when collapsed, resulting in
+ programmatically uncollapsing auto-sizing windows having them flicker size
+ for a frame. (#7691) [@achabense]
+- Windows: clicking on a window close button doesn't claim focus and bring to front. (#8683)
+- Windows: loosened code to allow hovering of resize grips, borders, and table
+ borders while hovering a sibling child window, so that the code in master matches
+ one in docking (they accidentally diverged). (#8554)
+- Windows: BeginChild(): fixed being unable to combine manual resize on one axis
+ and automatic resize on the other axis. (#8690)
+ e.g. neither ImGuiChildFlags_ResizeX | ImGuiChildFlags_AutoResizeY
+ or ImGuiChildFlags_ResizeY | ImGuiChildFlags_AutoResizeX worked before.
+- TreeNode: added experimental flags to draw tree hierarchy outlines linking
+ parent and tree nodes: (#2920)
+ - ImGuiTreeNodeFlags_DrawLinesNone: No lines drawn (default value in style.TreeLinesFlags).
+ - ImGuiTreeNodeFlags_DrawLinesFull: Horizontal lines to child nodes. Vertical line drawn down to TreePop() position: cover full contents.
+ - ImGuiTreeNodeFlags_DrawLinesToNodes: Horizontal lines to child nodes. Vertical line drawn down to bottom-most child node.
+ - Added style.TreeLinesFlags which stores the default setting,
+ which may be overridden in individual TreeNode() calls.
+ - Added style.TreeLinesSize (default to 1.0f).
+ - Added style.TreeLinesRadius (default to 0.0f).
+ - Added ImGuiCol_TreeLines (in default styles this is the same as ImGuiCol_Border).
+ - Caveats:
+ - Tree nodes may be used in many creative ways (manually positioning openable
+ nodes in unusual ways, using indent to create tree-looking structures, etc.)
+ and the feature may not accurately represent them in every cases.
+ - The feature adds a little cost as extra data needs to be stored.
+ (ImGuiTreeNodeFlags_DrawLinesToNodes is slower than ImGuiTreeNodeFlags_DrawLinesFull
+ which may be meaningful on very large trees, as it needs to record bottom-most
+ Y position even for clipped nodes).
+ - The feature is unlikely to ever work properly when using a coarse clipper
+ such as ImGuiListClipper.
+- TreeNode: fixed incorrect clipping of arrow/bullet when using ImGuiTreeNodeFlags_SpanAllColumns.
+- InputText: fixed cursor positioning issue using up/down keys near end of lines while
+ editing non-ASCII text. (Regression from 1.91.2) (#8635, #7925)
+- InputText: fixed a buffer overrun that could happen when using dynamically resizing
+ buffers (e.g. imgui_stdlib.cpp for std::string, or ImGuiInputTextFlags_CallbackRezize)
+ and programmatically making an insertion. (#8689) [@ocornut, @m9710797]
+- Tables: fixed TableHeader() eager vertical clipping of text which may be noticeable
+ with FramePadding.y was too small. (#6236)
+- Tables: fixed an assert when combining Tables, Frozen Rows, Clipper and BeginMultiSelect()
+ in a certain order. (#8595, #8250)
+- Tabs: fixes small issues with how "..." ellipsis moved depending on visibility
+ of Close Button or Unsaved Document marker. (#8387)
+- Tooltips: tooltips have a maximum size corresponding to host display/monitor size,
+ which mitigates edge case issues in multi-viewport scenarios where abnormally large
+ windows (e.g. determined programmatically) can lead to renderer backend trying to
+ create abnormally large framebuffers.
+- TextLinkOpenURL(): added bool return value on click. (#8645, #8451, #7660)
+- Scroll: fixed contents size, scrollbar visibility and scrolling resetting issues
+ with abnormally large contents ranges. (#3609, #8215)
+- Clipper: some mitigation/improvements for abnormally large contents ranges. (#3609, #8215)
+- Nav: fixed assertion when holding gamepad FaceLeft/West button to open
+ CTRL+Tab windowing + pressing a keyboard key. (#8525)
+- Nav: fixed scroll fallback (when there are no interactive widgets to jump to) not
+ being enabled on windows with menu or title bar.
+- Nav: fixed an issue handling PageUp/PageDown on windows with abnormally large contents
+ range which could lead to clipper requesting very large ranges.
+- Error Handling: added better error report and recovery for extraneous
+ EndPopup() call. (#1651, #8499)
+- Error Handling: added better error report and recovery when calling EndFrame()
+ or Render() without NewFrame(). Was previously only an assert.
+- Style, InputText: added ImGuiCol_InputTextCursor to configure color of
+ the InputText cursor/caret. (#7031)
+- Platform IME: added ImGuiPlatformImeData::ViewportId info (backported from Docking branch).
+- Platform IME: added ImGuiPlatformImeData::WantTextInput which might set independently
+ of WantVisible. This is set in the same structure because activating text input generally
+ requires providing a window to the backend. (#8584, #6341)
+- DrawList: Fixed a regression from 1.91.1 where a Begin()/PushFont()/AddImage() sequence
+ would not restore the correct atlas Texture Identifier when the PushFont() call used
+ a font from a different atlas. (#8694, caused by #3224, #3875, #6398, #7903)
+- Misc: added extra operators to ImVec4 in IMGUI_DEFINE_MATH_OPERATORS block. (#8510) [@gan74]
+- Misc: removed static linkage from operators to facilitate using in C++ modules. (#8682, #8358) [@johmani]
+- Demo: changed default framed item width to use Min(GetFontSize() * 12, GetContentRegionAvail().x * 0.40f).
+- Renderer Backends:
+ - Backends: SDLGPU3: Fixed creating atlas texture earlier than other backends, preventing
+ to load fonts between the Init and NewFrames calls.
+ - Backends: SDLGPU3: Made ImGui_ImplSDLGPU3_PrepareDrawData() reuse GPU Transfer Buffers which
+ were unusually slow to recreate every frame. Much faster now. (#8534) [@ocornut, @TheMode]
+ - Backends: SDLGPU3: added support for ImDrawCallback_ResetRenderState. (#8599)
+ - Backends: OpenGL3: made GLES 3.20 contexts not access GL_CONTEXT_PROFILE_MASK nor
+ GL_PRIMITIVE_RESTART. (#8664) [@DyXel]
+ - Backends: DirectX12: Fixed build on MinGW. (#8702, #4594) [@playday3008]
+ - Backends: DirectX10, DirectX11, DirectX12: Honor FramebufferScale to allow for custom
+ platform backends and experiments using it (consistently with other renderer backends,
+ even though in normal condition it is not set under Windows). (#8412) [@WSSDude]
+ - Backends: Vulkan: Deep-copy ImGui_ImplVulkan_InitInfo::PipelineRenderingCreateInfo's
+ pColorAttachmentFormats buffer when set, in order to reduce common user-error of
+ specifying a pointer to data that gets out of scope. (#8282)
+ - Backends: Vulkan: Load dynamic rendering functions using vkGetDeviceProcAddr()
+ + try both non-KHR and KHR versions. (#8600, #8326, #8365) [@ChrisTom-94]
+ - Backends: Vulkan: fixed validation errors in window create/resize helpers used by examples
+ and by multi-viewports implementation, which would typically trigger errors while detaching
+ secondary viewports. (#8600, #8176) [@ChrisTom-94]
+- Platform Backends:
+ - Backends: GLFW: added ImGui_ImplGlfw_GetContentScaleForMonitor(), ImGui_ImplGlfw_GetContentScaleForWindow()
+ helpers. They are wrappers to glfwGetMonitorContentScale()/glfwGetWindowContentScale(), with compile-time
+ GLFW version checks + returning 1.0f on Apple platform.
+ - Backends: GLFW: Added support for multiple Dear ImGui contexts. (#8676, #8239, #8069)
+ - Backends: SDL2: added ImGui_ImplSDL2_GetDpiScaleForDisplay() and ImGui_ImplSDL2_GetContentScaleForWindow()
+ helpers. They are wrappers to SDL_GetDisplayDPI(), with compile-time SDL version checks + returning 1.0f
+ on Apple platforms. SDL3 already does this by default.
+ - Backends: Win32: Fixed an issue where externally losing mouse capture (due to e.g. focus loss)
+ would fail to claim it again the next subsequent click. (#8594)
+ - Backends: SDL2, SDL3, OSX: Fill gamepad inputs and set ImGuiBackendFlags_HasGamepad
+ regardless of ImGuiConfigFlags_NavEnableGamepad being set. (#8508)
+ - Backends: SDL2, SDL3: don't attempt to call SDL_CaptureMouse() on drivers where we don't
+ call SDL_GetGlobalMouseState(). This is specifically for Wayland but we currently use
+ the same white-list as SDL_GetGlobalMouseState(). (#8561) [@vs49688]
+ - Backends: GLFW, SDL2, SDL3: include GLFW/SDL version number in io.BackendPlatformName.
+ - Backends: SDL3: Update for SDL3 api changes: revert SDL_GetClipboardText()
+ memory ownership change. (#8530, #7801) [@Green-Sky]
+ - Backends: SDL3: honor ImGuiPlatformImeData::WantTextInput as an alternative
+ way to call SDL_StartTextInput(), without IME being necessarily visible. (#8584)
+ - Backends: SDL3: fixed pulling SDL_PROP_WINDOW_COCOA_WINDOW_POINTER into
+ viewport->PlatformHandleRaw. (#8725, #8726) [@eertbleyen]
+ - Backends: OSX: ImGui_ImplOSX_HandleEvent() only process event for window containing
+ our view. (#8644) [@BingoXuan]
+- Examples:
+ - Examples: Made many examples DPI aware by default.
+ The single-viewport is basically:
+ - Query monitor DPI scale. Helpers are provided in some backends.
+ - Call style.ScaleAllSizes() and set style.FontScaleDpi with this factor.
+ Multi-viewport applications may set both of those flags:
+ - io.ConfigDpiScaleFonts = true;
+ - io.ConfigDpiScaleViewports = true;
+ Which will scale fonts but NOT style padding/spacings/thicknesses yet.
+ - Examples: Apple+Metal, Apple+OpenGL: add Makefile (CLion opens them nicely). (#8637) [@pthom]
+ - Examples: DirectX12+Win32: also test for IsIconic() for sleeping since we don't seem to
+ get a DXGI_STATUS_OCCLUDED signal when minimized. (#8603) [@dooann]
+
+Docking+Viewports Branch:
+
+- Viewports: added per-viewport FramebufferScale for Retina display multi-monitor support.
+ Backend must provide platform_io.platform_io.Platform_GetWindowFramebufferScale handler.
+ This effectively fixes clipping/rendering on multi-monitors with varying Retina scale.
+ (#1065, #1542, #1676, #1786, #2826, #3757, #5081, #5580, #5592, #6465, #7273, #7779 etc.)
+- Viewports: fixed handling of simultaneous move + resize (e.g. toggling maximized)
+ when ImGuiConfigFlags_DpiEnableScaleViewports is enabled.
+- Backends: Win32: Viewports: fixed an issue when closing a window from
+ the OS close button (with io.ConfigViewportsNoDecoration=false) while
+ user code is discarding the 'bool *p_open=false output' from Begin().
Because we allowed the Win32 window to close early, Windows destroyed
it and our imgui window became not visible even though user code was
- still submitting it.
+ still submitting it. (#8670)
+- Backends: Win32: Viewports: handle WM_DPICHANGED in backend when
+ ImGuiConfigFlags_DpiEnableScaleViewports is enabled.
+- Backends: GLFW, SDL2, SDL3, Apple: provide Platform_GetWindowFramebufferScale handler,
+ (#1065, #1542, #1676, #1786, #2826, #3757, #5081, #5580, #5592, #6465, #7273, #7779 etc.)
+- Backends: SDLGPU3 for SDL3: added multi-viewport support. (#8573) [@Lekoopapaul]
+- Backends: SDL2, SDL3: revert updating monitors and work areas info every
+ frame. Only do it on Windows to detect task-bar resize until we get an
+ adequate event for it. (#8415, #8558)
+- Backends: SDL3: macOS: Fixed secondary-viewports not appearing on a different
+ monitor than the main viewport. Because SDL_SetWindowParent() seems to restrict it.
+- Backends: GLFW: Disable multi-viewports under Wayland (require GLFW 3.4). (#8587)
+- Backends: GLFW: Fixed an issue where Win32 specific WndProc hook was not installed
+ for all GLFW version, preventing mouse vs touch detection from working on
+ secondary viewports.
-----------------------------------------------------------------------
@@ -178,6 +1043,7 @@ Other changes:
- Debug Tools: Added io.ConfigDebugHighlightIdConflictsShowItemPicker (defaults to true)
to allow disabled Item Picker suggestion in user facing builds. (#7961, #7669)
- Debug Tools: Tweaked layout of ID Stack Tool and always display full path. (#4631)
+- imgui_freetype: update lunasvg API to support v3.0+. (#8656, #6842, #6591) [@moretromain]
- Misc: Various zealous warning fixes for newer version of Clang.
- Misc: Added ImGuiMouseCursor_Wait and ImGuiMouseCursor_Progress mouse cursors
(busy/wait/hourglass shape, with or without an arrow cursor).
@@ -204,7 +1070,7 @@ Other changes:
with asserts enabled. (#8452)
- Backends: SDL2, SDL3: Using SDL_OpenURL() in platform_io.Platform_OpenInShellFn
handler. (#7660) [@achabense]
-- Backends: SDL2, SDL3: Use display bounds when SDL_GetDisplayUsableBounds()
+- Backends: SDL2, SDL3: Use display bounds when SDL_GetDisplayUsableBounds()
fails or return a zero size. (#8415, #3457)
- Backends: SDL2, SDL3, Win32, Allegro5: Added support for ImGuiMouseCursor_Wait
and ImGuiMouseCursor_Progress cursors.
@@ -232,9 +1098,9 @@ Docking+Viewports Branch:
- Docking: Removed legacy assert preventing to call DockBuilderSplitNode() on an existing
split node. This makes using DockBuilder a little more flexible and bearable! (#8472) [@MegaMech]
-- Viewports: fixed an issue where in certain cases, a window repositioning leading
- to a monitor change could have the window incorrectly get clamped within the boundaries
- of its previous monitor. Would happen e.g. when loading .ini data during runtime. (#8484)
+- Viewports: fixed an issue where in certain cases, a window repositioning leading
+ to a monitor change could have the window incorrectly get clamped within the boundaries
+ of its previous monitor. Would happen e.g. when loading .ini data during runtime. (#8484)
- Viewports: fixed an assert when a window load settings with a position outside
monitor bounds, when there are multiple monitors. (#8393, #8385) [@gaborodriguez]
- Viewports + Backends: Win32: Fixed setting title bar text when application
@@ -559,7 +1425,7 @@ Breaking changes:
allows casting any pointer/integer type without warning:
- May warn: ImGui::Image((void*)MyTextureData, ...);
- May warn: ImGui::Image((void*)(intptr_t)MyTextureData, ...);
- - Won't warn: ImGui::Image((ImTextureID)(intptr_t)MyTextureData), ...);
+ - Won't warn: ImGui::Image((ImTextureID)(intptr_t)MyTextureData, ...);
- Note that you can always define ImTextureID to be your own high-level structures
(with dedicated constructors and extra render parameters) if you like.
- IO: moved ImGuiConfigFlags_NavEnableSetMousePos to standalone io.ConfigNavMoveSetMousePos bool.
@@ -581,7 +1447,7 @@ Other changes:
- Set io.ConfigNavCursorVisibleAuto = true (default) to enable automatic toggling
of cursor visibility (mouse click hide the cursor, arrow keys makes it visible).
- Set io.ConfigNavCursorVisibleAlways to keep cursor always visible.
- - Nav: added NavSetCursorVisible(bool visible) function to manipulate visibility of
+ - Nav: added SetNavCursorVisible(bool visible) function to manipulate visibility of
navigation cursor (e.g. set default state, or after some actions). (#1074, #2048, #7237, #8059)
- Nav: added io.ConfigNavEscapeClearFocusItem and io.ConfigNavEscapeClearFocusWindow to change
how pressing Escape affects navigation. (#8059, #2048, #1074, #3200)
@@ -4369,7 +5235,7 @@ Breaking Changes:
- ShowTestWindow() -> use ShowDemoWindow()
- IsRootWindowFocused() -> use IsWindowFocused(ImGuiFocusedFlags_RootWindow)
- IsRootWindowOrAnyChildFocused() -> use IsWindowFocused(ImGuiFocusedFlags_RootAndChildWindows)
- - SetNextWindowContentWidth(w) -> use SetNextWindowContentSize(ImVec2(w, 0.0f)
+ - SetNextWindowContentWidth(w) -> use SetNextWindowContentSize(ImVec2(w, 0.0f))
- GetItemsLineHeightWithSpacing() -> use GetFrameHeightWithSpacing()
- ImGuiCol_ChildWindowBg -> use ImGuiCol_ChildBg
- ImGuiStyleVar_ChildWindowRounding -> use ImGuiStyleVar_ChildRounding
diff --git a/docs/EXAMPLES.md b/docs/EXAMPLES.md
index 0df73059b..f61297fd1 100644
--- a/docs/EXAMPLES.md
+++ b/docs/EXAMPLES.md
@@ -3,7 +3,7 @@ _(You may browse this at https://github.com/ocornut/imgui/blob/master/docs/EXAMP
## Dear ImGui: Examples
**The [examples/](https://github.com/ocornut/imgui/blob/master/examples) folder example applications (standalone, ready-to-build) for variety of
-platforms and graphics APIs.** They all use standard backends from the [backends/](https://github.com/ocornut/imgui/blob/master/backends) folder (see [BACKENDS.md](https://github.com/ocornut/imgui/blob/master/docs/BACKENDS.md)).
+platforms and graphics APIs.** They all use standard backends from the [backends/](https://github.com/ocornut/imgui/blob/master/backends) folder (see [docs/BACKENDS.md](https://github.com/ocornut/imgui/blob/master/docs/BACKENDS.md)).
The purpose of Examples is to showcase integration with backends, let you try Dear ImGui, and guide you toward
integrating Dear ImGui in your own application/game/engine.
@@ -58,7 +58,7 @@ Allegro 5 example.
Android + OpenGL3 (ES) example.
= main.cpp + imgui_impl_android.cpp + imgui_impl_opengl3.cpp
-[example_apple_metal/](https://github.com/ocornut/imgui/blob/master/examples/example_metal/)
+[example_apple_metal/](https://github.com/ocornut/imgui/tree/master/examples/example_apple_metal/)
OSX & iOS + Metal example.
= main.m + imgui_impl_osx.mm + imgui_impl_metal.mm
It is based on the "cross-platform" game template provided with Xcode as of Xcode 9.
@@ -149,6 +149,14 @@ SDL2 (Win32, Mac, Linux, etc.) + Vulkan example.
This is quite long and tedious, because: Vulkan.
For this example, the main.cpp file exceptionally use helpers function from imgui_impl_vulkan.h/cpp.
+[example_sdl3_directx11/](https://github.com/ocornut/imgui/blob/master/examples/example_sdl3_directx11/)
+SDL3 + DirectX11 examples, Windows only.
+= main.cpp + imgui_impl_sdl3.cpp + imgui_impl_dx11.cpp
+
+[example_sdl3_metal/](https://github.com/ocornut/imgui/blob/master/examples/example_sdl3_metal/)
+SDL3 + Metal example, Mac only.
+= main.cpp + imgui_impl_sdl3.cpp + imgui_impl_metal.mm
+
[example_sdl3_opengl3/](https://github.com/ocornut/imgui/blob/master/examples/example_sdl3_opengl3/)
SDL3 (Win32, Mac, Linux, etc.) + OpenGL3+/ES2/ES3 example.
= main.cpp + imgui_impl_sdl3.cpp + imgui_impl_opengl3.cpp
diff --git a/docs/FAQ.md b/docs/FAQ.md
index 8c08b4dd7..e72f63f74 100644
--- a/docs/FAQ.md
+++ b/docs/FAQ.md
@@ -13,18 +13,20 @@ or view this file with any Markdown viewer.
:---------------------------------------------------------- |
| [Where is the documentation?](#q-where-is-the-documentation) |
| [What is this library called?](#q-what-is-this-library-called) |
+| [What is the difference between Dear ImGui and traditional UI toolkits?](#q-what-is-the-difference-between-dear-imgui-and-traditional-ui-toolkits) |
| [Which version should I get?](#q-which-version-should-i-get) |
| **Q&A: Integration** |
| **[How to get started?](#q-how-to-get-started)** |
| **[How can I tell whether to dispatch mouse/keyboard to Dear ImGui or my application?](#q-how-can-i-tell-whether-to-dispatch-mousekeyboard-to-dear-imgui-or-my-application)** |
| [How can I enable keyboard or gamepad controls?](#q-how-can-i-enable-keyboard-or-gamepad-controls) |
| [How can I use this on a machine without mouse, keyboard or screen? (input share, remote display)](#q-how-can-i-use-this-on-a-machine-without-mouse-keyboard-or-screen-input-share-remote-display) |
+| [How can I create my own backend?](#q-how-can-i-create-my-own-backend)
| [I integrated Dear ImGui in my engine and little squares are showing instead of text...](#q-i-integrated-dear-imgui-in-my-engine-and-little-squares-are-showing-instead-of-text) |
| [I integrated Dear ImGui in my engine and some elements are clipping or disappearing when I move windows around...](#q-i-integrated-dear-imgui-in-my-engine-and-some-elements-are-clipping-or-disappearing-when-i-move-windows-around) |
| [I integrated Dear ImGui in my engine and some elements are displaying outside their expected windows boundaries...](#q-i-integrated-dear-imgui-in-my-engine-and-some-elements-are-displaying-outside-their-expected-windows-boundaries) |
| **Q&A: Usage** |
| **[About the ID Stack system.. Why is my widget not reacting when I click on it? Why is the wrong widget reacting when I click on one? How can I have widgets with an empty label? How can I have multiple widgets with the same label? How can I have multiple windows with the same label?](#q-about-the-id-stack-system)** |
-| [How can I display an image? What is ImTextureID, how does it work?](#q-how-can-i-display-an-image-what-is-imtextureid-how-does-it-work)|
+| [How can I display an image?](#q-how-can-i-display-an-image) [What are ImTextureID/ImTextureRef?](#q-what-are-imtextureidimtextureref)|
| [How can I use maths operators with ImVec2?](#q-how-can-i-use-maths-operators-with-imvec2) |
| [How can I use my own maths types instead of ImVec2/ImVec4?](#q-how-can-i-use-my-own-maths-types-instead-of-imvec2imvec4) |
| [How can I interact with standard C++ types (such as std::string and std::vector)?](#q-how-can-i-interact-with-standard-c-types-such-as-stdstring-and-stdvector) |
@@ -74,6 +76,60 @@ or view this file with any Markdown viewer.
---
+### Q: What is the difference between Dear ImGui and traditional UI toolkits?
+
+Here's a very simplified comparison between the approach taken by Dear ImGui vs traditional toolkits:
+
+| Dear ImGui | Qt/GTK/WPF... |
+|--------------------------|--------------------------|
+| UI fully issued on every update. | UI issued once then later modified. |
+| UI layout is fully dynamic and can change at any time. UI is generally emitted programmatically, which empowers reflecting a dynamic set of data. | UI layout is mostly static. UI may be emitted programmatically or from data created by offline tools. UI need extra code to evolve, which is often tedious and error-prone if it needs to be reflecting dynamic data and systems. |
+| Application can submit UI based on arbitrary logic and then forget about it. | Application needs more bookkeeping of UI elements. |
+| UI library stores minimal amounts of data. At one point in time, it typically doesn't know or remember which other widgets are displayed and which widgets are coming next. As a result, certain layout features (alignment, resizing) are not as easy to implement or require ad-hoc code. | UI library stores entire widgets tree and state. UI library can use this retained data to easily layout things. |
+| UI code may be added anywhere. You can even create UI to edit a local variable! | UI code needs to be added in dedicated spots. |
+| UI layout/logic/action/data bindings are all nearby in the code. | UI layout/logic/action/data bindings in distinct functions, files or formats. |
+| Data is naturally always synchronized. | Use callback/signal/slot for synchronizing data (error-prone). |
+| API is simple and easy to learn. In particular, doing basic things is very easy. | API is more complex and specialized. |
+| API is low-level (raw language types). | API are higher-level (more abstractions, advanced language features). |
+| Less fancy look and feel. | Standard look and feel. |
+| Compile yourself. Easy to debug, hack, modify, study. | Mostly use precompiled libraries. Compiling, modifying or studying is daunting if not impossible. |
+| Run on every platform. | Run on limited desktop platforms. |
+
+Idiomatic Dear ImGui code:
+```cpp
+if (ImGui::Button("Save"))
+ MySaveFunction();
+
+ImGui::SliderFloat("Slider", &m_MyValue, 0.0f, 1.0f);
+```
+Idiomatic code with traditional toolkit:
+```cpp
+UiButton* button = new UiButton("Save");
+button->OnClick = &MySaveFunction;
+parent->Add(button);
+
+UiSlider* slider = new UiSlider("Slider");
+slider->SetRange(0.0f, 1.0f);
+slider->BindData(&m_MyValue);
+parent->Add(slider);
+```
+This is only meant to give you a intuitive feeling of the main differences, but pros & cons go deeper than that.
+
+Some of those properties are typically associated to the umbrella term "IMGUI", but the term has no simple and well-agreed definition. There are many erroneous statements and misunderstandings with what IMGUI means. It is partly caused by the fact that most popular IMGUI implementations (including Dear ImGui) have originated from game industry needs and have targeted specific use cases, causing people to conflate IMGUI properties with what a specific library does. However, it is perfectly possible to implement an IMGUI library that would have very different properties than e.g. Dear ImGui. My take on defining what an IMGUI is:
+
+**IMGUI refers to the API: literally the interface between the application and the UI system.**
+- An IMGUI API favors the application code owning its data and being the single source of truth for it.
+- An IMGUI API tries to minimize the application having to retain/manage data related to the UI system.
+- An IMGUI API tries to minimize the UI system having to retain/manage data related to the application.
+- Synchronization between application data and UI data is natural and less error-prone.
+
+**IMGUI does NOT refer to the implementation. Whatever happens inside the UI library code doesn't matter.**
+ Also see: [Links to many articles about the IMGUI paradigm](https://github.com/ocornut/imgui/wiki/#about-the-imgui-paradigm).
+
+##### [Return to Index](#index)
+
+---
+
### Q: Which version should I get?
I occasionally tag [Releases](https://github.com/ocornut/imgui/releases) but it is generally safe and recommended to sync to master/latest. The library is fairly stable and regressions tend to be fixed fast when reported.
@@ -92,8 +148,8 @@ Many projects are using this branch and it is kept in sync with master regularly
### Q: How to get started?
Read [Getting Started](https://github.com/ocornut/imgui/wiki/Getting-Started).
-Read [EXAMPLES.md](https://github.com/ocornut/imgui/blob/master/docs/EXAMPLES.md).
-Read [BACKENDS.md](https://github.com/ocornut/imgui/blob/master/docs/BACKENDS.md).
+Read [docs/EXAMPLES.md](https://github.com/ocornut/imgui/blob/master/docs/EXAMPLES.md).
+Read [docs/BACKENDS.md](https://github.com/ocornut/imgui/blob/master/docs/BACKENDS.md).
Read `PROGRAMMER GUIDE` section of [imgui.cpp](https://github.com/ocornut/imgui/blob/master/imgui.cpp).
The [Wiki](https://github.com/ocornut/imgui/wiki) is a hub to many resources and links.
@@ -159,9 +215,18 @@ Console SDK also sometimes provide equivalent tooling or wrapper for Synergy-lik
---
+### Q: How can I create my own backend?
+- See [docs/BACKENDS.md](https://github.com/ocornut/imgui/blob/master/docs/BACKENDS.md).
+- See Documentation at the top of imgui.cpp.
+
+##### [Return to Index](#index)
+
+---
+
### Q: I integrated Dear ImGui in my engine and little squares are showing instead of text...
Your renderer backend is not using the font texture correctly or it hasn't been uploaded to the GPU.
-- If this happens using the standard backends: A) have you modified the font atlas after `ImGui_ImplXXX_NewFrame()`? B) maybe the texture failed to upload, which **can if your texture atlas is too big**. Also see [docs/FONTS.md](https://github.com/ocornut/imgui/blob/master/docs/FONTS.md).
+- If this happens using standard backends (before 1.92): A) have you modified the font atlas after `ImGui_ImplXXX_NewFrame()`? B) maybe the texture failed to upload, which **can if your texture atlas is too big**. Also see [docs/FONTS.md](https://github.com/ocornut/imgui/blob/master/docs/FONTS.md).
+- If this happens using standard backends (after 1.92): please report.
- If this happens with a custom backend: make sure you have uploaded the font texture to the GPU, that all shaders are rendering states are setup properly (e.g. texture is bound). Compare your code to existing backends and use a graphics debugger such as [RenderDoc](https://renderdoc.org) to debug your rendering states.
##### [Return to Index](#index)
@@ -375,23 +440,73 @@ node open/closed state differently. See what makes more sense in your situation!
---
-### Q: How can I display an image? What is ImTextureID, how does it work?
+### Q: How can I display an image?
+### Q: What are ImTextureID/ImTextureRef?
-Short explanation:
-- Refer to [Image Loading and Displaying Examples](https://github.com/ocornut/imgui/wiki/Image-Loading-and-Displaying-Examples) on the [Wiki](https://github.com/ocornut/imgui/wiki).
+**Short explanation:**
- You may use functions such as `ImGui::Image()`, `ImGui::ImageButton()` or lower-level `ImDrawList::AddImage()` to emit draw calls that will use your own textures.
+- To load and display your own textures, refer to [Image Loading and Displaying Examples](https://github.com/ocornut/imgui/wiki/Image-Loading-and-Displaying-Examples) on the [Wiki](https://github.com/ocornut/imgui/wiki).
- Actual textures are identified in a way that is up to the user/engine. Those identifiers are stored and passed as an opaque ImTextureID value.
- By default ImTextureID can store up to 64-bits. You may `#define` it to a custom type/structure if you need.
- Loading image files from the disk and turning them into a texture is not within the scope of Dear ImGui (for a good reason), but the examples linked above may be useful references.
+**Details:**
+
+1.92 introduced `ImTextureRef` in June 2025.
+- All drawing functions using `ImTextureID` were changed to use `ImTextureRef`.
+- You can trivially create a `ImTextureRef` from a `ImTextureID`.
+- **If you use Image functions with textures that you have loaded/created yourself, you will mostly likely only ever store/manipulate `ImTextureID` and then pass them as `ImTextureRef`.**
+- You only NEED to manipulate `ImTextureRef` when dealing with textures managed by the backend itself, aka mainly the atlas texture for now.
+- We intentionally do not provide an implicit `ImTextureRef` -> `ImTextureID` cast operator because it is technically lossy to convert ImTextureRef to ImTextureID before rendering.
+
+**ImTextureID = backend specific, low-level identifier for a texture uploaded in GPU/graphics system.**
+```cpp
+#ifndef ImTextureID
+typedef ImU64 ImTextureID; // Default: store up to 64-bits (any pointer or integer). A majority of backends are ok with that.
+#endif
+```
+- When a Rendered Backend creates a texture, it store its native identifier into a ImTextureID value (e.g. Used by DX11 backend to a `ID3D11ShaderResourceView*`; Used by OpenGL backends to store `GLuint`; Used by SDLGPU backend to store a `SDL_GPUTextureSamplerBinding*`, etc.).
+- User may submit their own textures to e.g. `ImGui::Image()` function by passing this value.
+- During the rendering loop, the Renderer Backend retrieve the ImTextureID, which stored inside a ImTextureRef, which is stored inside ImDrawCmd.
+- Compile-time type configuration:
+ - To use something other than a 64-bit value: add '#define ImTextureID MyTextureType*' in your imconfig.h file.
+ - This can be whatever to you want it to be! read the FAQ entry about textures for details.
+ - You may decide to store a higher-level structure containing texture, sampler, shader etc. with various constructors if you like. You will need to implement ==/!= operators.
+
+**ImTextureRef = higher-level identifier for a texture.**
+```cpp
+// Store a ImTextureID _or_ a ImTextureData*.
+struct ImTextureRef
+{
+ ImTextureRef() { _TexData = NULL; _TexID = ImTextureID_Invalid; }
+ ImTextureRef(ImTextureID tex_id) { _TexData = NULL; _TexID = tex_id; }
+ inline ImTextureID GetTexID() const { return _TexData ? _TexData->TexID : _TexID; }
+
+ // Members (either are set, never both!)
+ ImTextureData* _TexData; // A texture, generally owned by a ImFontAtlas. Will convert to ImTextureID during render loop, after texture has been uploaded.
+ ImTextureID _TexID; // _OR_ Low-level backend texture identifier, if already uploaded or created by user/app. Generally provided to e.g. ImGui::Image() calls.
+};
+```
+- The identifier is valid even before the texture has been uploaded to the GPU/graphics system.
+- This is what gets passed to functions such as `ImGui::Image()`, `ImDrawList::AddImage()`.
+- This is what gets stored in draw commands (`ImDrawCmd`) to identify a texture during rendering.
+ - When a texture is created by user code (e.g. custom images), we directly store the low-level `ImTextureID`.
+ - Because of this, when displaying your own texture you are likely to ever only manage ImTextureID values on your side.
+ - When a texture is created by the backend, we store a `ImTextureData*` which becomes an indirection to extract the `ImTextureID` value during rendering, after texture upload has happened.
+ - To create a `ImTextureRef` from a `ImTextureData*` you can use `ImTextureData::GetTexRef()`.
+ We intentionally do not provide an `ImTextureRef` constructor for this: we don't expect this to be frequently useful to the end-user, and it would be erroneously called by many legacy code.
+ - There is no constructor to create a `ImTextureRef` from a `ImTextureData*` as we don't expect this to be useful to the end-user, and it would be erroneously called by many legacy code.
+ - If you want to bind the current atlas when using custom rectangles, you can use `io.Fonts->TexRef`.
+ - Binding generators for languages such as C (which don't have constructors), should provide a helper, e.g. `inline ImTextureRef ImTextureRefFromID(ImTextureID tex_id) { ImTextureRef tex_ref = { ._TexData = NULL, .TexID = tex_id }; return tex_ref; }`
+
**Please read documentations or tutorials on your graphics API to understand how to display textures on the screen before moving onward.**
Long explanation:
- Dear ImGui's job is to create "meshes", defined in a renderer-agnostic format made of draw commands and vertices. At the end of the frame, those meshes (ImDrawList) will be displayed by your rendering function. They are made up of textured polygons and the code to render them is generally fairly short (a few dozen lines). In the examples/ folder, we provide functions for popular graphics APIs (OpenGL, DirectX, etc.).
- Each rendering function decides on a data type to represent "textures". The concept of what is a "texture" is entirely tied to your underlying engine/graphics API.
- We carry the information to identify a "texture" in the ImTextureID type.
+We carry the information to identify a "texture" in the ImTextureID type, which itself tends to be stored inside a ImTextureRef.
ImTextureID default to ImU64 aka 8 bytes worth of data: just enough to store one pointer or integer of your choice.
-Dear ImGui doesn't know or understand what you are storing in ImTextureID, it merely passes ImTextureID values until they reach your rendering function.
+Dear ImGui doesn't know or understand what you are storing in ImTextureID, it merely passes values until they reach your rendering function.
- In the [examples/](https://github.com/ocornut/imgui/tree/master/examples) backends, for each graphics API we decided on a type that is likely to be a good representation for specifying an image from the end-user perspective. This is what the _examples_ rendering functions are using:
```cpp
OpenGL:
@@ -539,30 +654,49 @@ ImGui::End();
### Q: How should I handle DPI in my application?
-The short answer is: obtain the desired DPI scale, load your fonts resized with that scale (always round down fonts size to the nearest integer), and scale your Style structure accordingly using `style.ScaleAllSizes()`.
+Since 1.92 (June 2025) fonts may be dynamically used at any size.
-Your application may want to detect DPI change and reload the fonts and reset style between frames.
+**Scaling fonts**
+
+To change font size:
+```cpp
+ImGui::PushFont(NULL, 42.0f);
+```
+To change font and font size:
+```cpp
+ImGui::PushFont(new_font, 42.0f);
+```
+To scale all fonts:
+```cpp
+style.FontScaleDpi = 2.0f;
+```
+In `docking` branch or with multi-viewports:
+```cpp
+io.ConfigDpiScaleFonts = true; // [Experimental] Automatically overwrite style.FontScaleDpi in Begin() when Monitor DPI changes. This will scale fonts but _NOT_ scale sizes/padding for now.
+io.ConfigDpiScaleViewports = true; // [Experimental] Scale Dear ImGui and Platform Windows when Monitor DPI changes.
+```
+
+**Scaling style** (paddings, spacings, thicknesses)
+
+This is still massively work in progress, expect turbulence.
+Style values are currently not easily scalable dynamically.
+For single viewport application you can call once:
+```cpp
+style.ScaleAllSizes(factor); // call once!
+```
+If you need to change the scaling factor, it is currently most practical to reset the style and call this again with a new value.
Your UI code should avoid using hardcoded constants for size and positioning. Prefer to express values as multiple of reference values such as `ImGui::GetFontSize()` or `ImGui::GetFrameHeight()`. So e.g. instead of seeing a hardcoded height of 500 for a given item/window, you may want to use `30*ImGui::GetFontSize()` instead.
-Down the line Dear ImGui will provide a variety of standardized reference values to facilitate using this.
+Down the line Dear ImGui will provide a variety of standardized reference values to facilitate using this. This is expected to happen during subsequent 1.92.x releases.
-Applications in the `examples/` folder are not DPI aware partly because they are unable to load a custom font from the file-system (may change that in the future).
+Applications in the `examples/` folder are partly DPI aware but they are unable to load a custom font from the file-system, so they look ugly (may change that in the future).
-The reason DPI is not auto-magically solved in stock examples is that we don't yet have a satisfying solution for the "multi-dpi" problem (using the `docking` branch: when multiple viewport windows are over multiple monitors using different DPI scales). The current way to handle this on the application side is:
-- Create and maintain one font atlas per active DPI scale (e.g. by iterating `platform_io.Monitors[]` before `NewFrame()`).
-- Hook `platform_io.OnChangedViewport()` to detect when a `Begin()` call makes a Dear ImGui window change monitor (and therefore DPI).
-- In the hook: swap atlas, swap style with correctly sized one, and remap the current font from one atlas to the other (you may need to maintain a remapping table of your fonts at varying DPI scales).
+The reason DPI is not auto-magically solved in stock examples is that we don't yet have a satisfying solution for the "multi-dpi" problem (using the `docking` branch: when multiple viewport windows are over multiple monitors using different DPI scales) specifically for the `ImGuiStyle` structure. Fonts are however now perfectly scalable.
-This approach is relatively easy and functional but comes with two issues:
-- It's not possibly to reliably size or position a window ahead of `Begin()` without knowing on which monitor it'll land.
-- Style override may be lost during the `Begin()` call crossing monitor boundaries. You may need to do some custom scaling mumbo-jumbo if you want your `OnChangedViewport()` handler to preserve style overrides.
-
-Please note that if you are not using multi-viewports with multi-monitors using different DPI scales, you can ignore that and use the simpler technique recommended at the top.
-
-On Windows, in addition to scaling the font size (make sure to round to an integer) and using `style.ScaleAllSizes()`, you will need to inform Windows that your application is DPI aware. If this is not done, Windows will scale the application window and the UI text will be blurry. Potential solutions to indicate DPI awareness on Windows are:
-
-- For SDL2: the flag `SDL_WINDOW_ALLOW_HIGHDPI` needs to be passed to `SDL_CreateWindow()`.
+**On Windows, you need to inform Windows that your application is DPI aware!**
+If this is not done, Windows will scale the application window and the UI text will be blurry. Potential solutions to indicate DPI awareness on Windows are:
+- For SDL2: the flag `SDL_WINDOW_ALLOW_HIGHDPI` needs to be passed to `SDL_CreateWindow()` + call `::SetProcessDPIAware()`.
- For SDL3: the flag `SDL_WINDOW_HIGH_PIXEL_DENSITY` needs to be passed to `SDL_CreateWindow()`.
- For GLFW: this is done automatically.
- For other Windows projects with other backends, or wrapper projects:
@@ -615,9 +749,12 @@ Use the font atlas to pack them into a single texture. Read [docs/FONTS.md](http
---
### Q: How can I display and input non-Latin characters such as Chinese, Japanese, Korean, Cyrillic?
-When loading a font, pass custom Unicode ranges to specify the glyphs to load.
+Since 1.92 (June 2025) and with an updated backend, it is not necessary to specify glyph ranges at all.
+
+Before 1.92, when loading a font, pass custom Unicode ranges to specify the glyphs to load.
```cpp
+// [BEFORE 1.92]
// Add default Japanese ranges
io.Fonts->AddFontFromFileTTF("myfontfile.ttf", size_in_pixels, nullptr, io.Fonts->GetGlyphRangesJapanese());
@@ -641,8 +778,8 @@ Text input: it is up to your application to pass the right character code by cal
The applications in examples/ are doing that.
Windows: you can use the WM_CHAR or WM_UNICHAR or WM_IME_CHAR message (depending if your app is built using Unicode or MultiByte mode).
You may also use `MultiByteToWideChar()` or `ToUnicode()` to retrieve Unicode codepoints from MultiByte characters or keyboard state.
-Windows: if your language is relying on an Input Method Editor (IME), you can write your HWND to ImGui::GetMainViewport()->PlatformHandleRaw
-for the default implementation of GetPlatformIO().Platform_SetImeDataFn() to set your Microsoft IME position correctly.
+Windows: if your language is relying on an Input Method Editor (IME), you can write your HWND to `ImGui::GetMainViewport()->PlatformHandleRaw`
+for the default implementation of `GetPlatformIO().Platform_SetImeDataFn()` to set your Microsoft IME position correctly.
##### [Return to Index](#index)
diff --git a/docs/FONTS.md b/docs/FONTS.md
index e9fbcbe42..072ca9762 100644
--- a/docs/FONTS.md
+++ b/docs/FONTS.md
@@ -2,7 +2,7 @@ _(You may browse this at https://github.com/ocornut/imgui/blob/master/docs/FONTS
## Dear ImGui: Using Fonts
-The code in imgui.cpp embeds a copy of 'ProggyClean.ttf' (by Tristan Grimmer),
+The code in imgui.cpp embeds a copy of [ProggyClean.ttf](http://proggyfonts.net) (by Tristan Grimmer),
a 13 pixels high, pixel-perfect font used by default. We embed it in the source code so you can use Dear ImGui without any file system access. ProggyClean does not scale smoothly, therefore it is recommended that you load your own file when using Dear ImGui in an application aiming to look nice and wanting to support multiple resolutions.
You may also load external .TTF/.OTF files.
@@ -12,11 +12,13 @@ In the [misc/fonts/](https://github.com/ocornut/imgui/tree/master/misc/fonts) fo
## Index
- [Troubleshooting](#troubleshooting)
+- [New! Dynamic Fonts system in 1.92 (June 2025)](#new-dynamic-fonts-system-in-192-june-2025)
- [How should I handle DPI in my application?](#how-should-i-handle-dpi-in-my-application)
- [Fonts Loading Instructions](#fonts-loading-instructions)
- [Loading Font Data from Memory](#loading-font-data-from-memory)
- [Loading Font Data Embedded In Source Code](#loading-font-data-embedded-in-source-code)
- [Using Icon Fonts](#using-icon-fonts)
+ - [Excluding Overlapping Ranges](#excluding-overlapping-ranges)
- [Using FreeType Rasterizer (imgui_freetype)](#using-freetype-rasterizer-imgui_freetype)
- [Using Colorful Glyphs/Emojis](#using-colorful-glyphsemojis)
- [Using Custom Glyph Ranges](#using-custom-glyph-ranges)
@@ -35,7 +37,7 @@ In the [misc/fonts/](https://github.com/ocornut/imgui/tree/master/misc/fonts) fo
### (1) Invalid filename due to use of `\` or unexpected working directory.
-See [About Filenames](#about-filenames). AddFontXXX functions should assert if the filename is incorrect.
+See [About Filenames](#about-filenames). AddFontXXX() functions should assert if the filename is incorrect.
### (2) Invalid UTF-8 encoding of your non-ASCII strings.
@@ -43,14 +45,18 @@ See [About UTF-8 Encoding](#about-utf-8-encoding). Use the encoding viewer to co
### (3) Missing glyph ranges.
-You need to load a font with explicit glyph ranges if you want to use non-ASCII characters. See [Fonts Loading Instructions](#fonts-loading-instructions). Use [Debug Tools](#debug-tools) confirm loaded fonts and loaded glyph ranges.
+🆕 **Since 1.92, with an up to date backend: specifying glyph ranges is unnecessary.**
-This is a current constraint of Dear ImGui (which we will lift in the future): when loading a font you need to specify which characters glyphs to load.
+⏪ Before 1.92: you need to load a font with explicit glyph ranges if you want to use non-ASCII characters. See [Fonts Loading Instructions](#fonts-loading-instructions). Use [Debug Tools](#debug-tools) confirm loaded fonts and loaded glyph ranges.
+
+This was a previous constraint of Dear ImGui (lifted in 1.92): when loading a font you need to specify which characters glyphs to load.
All loaded fonts glyphs are rendered into a single texture atlas ahead of time. Calling either of `io.Fonts->GetTexDataAsAlpha8()`, `io.Fonts->GetTexDataAsRGBA32()` or `io.Fonts->Build()` will build the atlas. This is generally called by the Renderer backend, e.g. `ImGui_ImplDX11_NewFrame()` calls it. **If you use custom glyphs ranges, make sure the array is persistent** and available during the calls to `GetTexDataAsAlpha8()/GetTexDataAsRGBA32()/Build()`.
### (4) Font atlas texture fails to upload to GPU.
-This is often of byproduct of point 3. If you have large number of glyphs or multiple fonts, the texture may become too big for your graphics API. **The typical result of failing to upload a texture is if every glyph or everything appears as empty white rectangles.** Mind the fact that some graphics drivers have texture size limitation. If you are building a PC application, mind the fact that your users may use hardware with lower limitations than yours.
+🆕 **Since 1.92, with an up to date backend: atlas is built incrementally and dynamically resized, this is less likely to happen**
+
+:rewind: This is often of byproduct of point 3. If you have large number of glyphs or multiple fonts, the texture may become too big for your graphics API. **The typical result of failing to upload a texture is if every glyph or everything appears as empty white rectangles.** Mind the fact that some graphics drivers have texture size limitation. If you are building a PC application, mind the fact that your users may use hardware with lower limitations than yours.

@@ -60,9 +66,22 @@ Some solutions:
- Reduce glyphs ranges by calculating them from source localization data.
You can use the `ImFontGlyphRangesBuilder` for this purpose and rebuilding your atlas between frames when new characters are needed. This will be the biggest win!
- Set `io.Fonts.Flags |= ImFontAtlasFlags_NoPowerOfTwoHeight;` to disable rounding the texture height to the next power of two.
-- Set `io.Fonts.TexDesiredWidth` to specify a texture width to reduce maximum texture height (see comment in `ImFontAtlas::Build()` function).
-Future versions of Dear ImGui should solve this problem.
+##### [Return to Index](#index)
+
+---------------------------------------
+
+## New! Dynamic Fonts system in 1.92 (June 2025)
+
+v1.92 introduces a newer, dynamic font system. It requires backend to support the `ImGuiBackendFlags_HasTextures` feature:
+- Users of icons, Asian and non-English languages do not need to pre-build all glyphs ahead of time. Saving on loading time, memory, and also reducing issues with missing glyphs. Specifying glyph ranges is not needed anymore.
+- `PushFont(NULL, new_size)` may be used anytime to change font size.
+- Packing custom rectangles is more convenient as pixels may be written to immediately.
+- Any update to fonts previously required backend specific calls to re-upload the texture, and said calls were not portable across backends. It is now possible to scale fonts etc. in a way that doesn't require you to make backend-specific calls.
+- It is possible to plug a custom loader/backend to any font source.
+
+See [#8465](https://github.com/ocornut/imgui/issues/8465) for more details.
+
##### [Return to Index](#index)
@@ -85,6 +104,13 @@ io.Fonts->AddFontDefault();
```
**Load .TTF/.OTF file with:**
+
+🆕 **Since 1.92, with an up to date backend: passing a size is not necessary**
+```cpp
+ImGuiIO& io = ImGui::GetIO();
+io.Fonts->AddFontFromFileTTF("font.ttf");
+```
+:rewind: **Before 1.92, or without an up to date backend:**
```cpp
ImGuiIO& io = ImGui::GetIO();
io.Fonts->AddFontFromFileTTF("font.ttf", size_pixels);
@@ -95,14 +121,14 @@ If you get an assert stating "Could not load font file!", your font filename is
```cpp
// Init
ImGuiIO& io = ImGui::GetIO();
-ImFont* font1 = io.Fonts->AddFontFromFileTTF("font.ttf", size_pixels);
-ImFont* font2 = io.Fonts->AddFontFromFileTTF("anotherfont.otf", size_pixels);
+ImFont* font1 = io.Fonts->AddFontFromFileTTF("font.ttf",);
+ImFont* font2 = io.Fonts->AddFontFromFileTTF("anotherfont.otf");
```
In your application loop, select which font to use:
```cpp
ImGui::Text("Hello"); // use the default font (which is the first loaded font)
-ImGui::PushFont(font2);
+ImGui::PushFont(font2, 0.0f); // change font, keep current size
ImGui::Text("Hello with another font");
ImGui::PopFont();
```
@@ -110,11 +136,22 @@ ImGui::PopFont();
**For advanced options create a ImFontConfig structure and pass it to the AddFont() function (it will be copied internally):**
```cpp
ImFontConfig config;
-config.RasterizerDensity = 2.0f;
+config.OversampleH = 1.0f;
ImFont* font = io.Fonts->AddFontFromFileTTF("font.ttf", size_pixels, &config);
```
**Combine multiple fonts into one:**
+
+🆕 **Since 1.92, with an up to date backend: specifying glyph ranges is unnecessary.**
+```cpp
+// Load a first font
+ImFont* font = io.Fonts->AddFontDefault();
+ImFontConfig config;
+config.MergeMode = true;
+io.Fonts->AddFontFromFileTTF("DroidSans.ttf", 0.0f, &config); // Merge into first font to add e.g. Asian characters
+io.Fonts->AddFontFromFileTTF("fontawesome-webfont.ttf", 0.0f, &config); // Merge into first font to add Icons
+```
+:rewind: **Before 1.92, or without an up to date backend:**
```cpp
// Load a first font
ImFont* font = io.Fonts->AddFontDefault();
@@ -132,6 +169,7 @@ io.Fonts->Build();
**Add a fourth parameter to bake specific font ranges only:**
+🆕 **Since 1.92, with an up to date backend: specifying glyph ranges is unnecessary. All the GetGlyphRangesXXX() functions are marked obsolete.**
```cpp
// Basic Latin, Extended Latin
io.Fonts->AddFontFromFileTTF("font.ttf", size_pixels, nullptr, io.Fonts->GetGlyphRangesDefault());
@@ -146,10 +184,18 @@ See [Using Custom Glyph Ranges](#using-custom-glyph-ranges) section to create yo
**Example loading and using a Japanese font:**
+🆕 **Since 1.92, with an up to date backend:**
+```cpp
+ImGuiIO& io = ImGui::GetIO();
+io.Fonts->AddFontFromFileTTF("NotoSansCJKjp-Medium.otf");
+```
+
+:rewind: **Before 1.92, or without an up to date backend:**
```cpp
ImGuiIO& io = ImGui::GetIO();
io.Fonts->AddFontFromFileTTF("NotoSansCJKjp-Medium.otf", 20.0f, nullptr, io.Fonts->GetGlyphRangesJapanese());
```
+
```cpp
ImGui::Text(u8"こんにちは!テスト %d", 123);
if (ImGui::Button(u8"ロード"))
@@ -216,12 +262,24 @@ To refer to the icon UTF-8 codepoints from your C++ code, you may use those head
So you can use `ICON_FA_SEARCH` as a string that will render as a "Search" icon.
+🆕 **Since 1.92, with an up to date backend: specifying glyph ranges is unnecessary. You can omit this parameter.**
Example Setup:
```cpp
// Merge icons into default tool font
#include "IconsFontAwesome.h"
ImGuiIO& io = ImGui::GetIO();
io.Fonts->AddFontDefault();
+ImFontConfig config;
+config.MergeMode = true;
+config.GlyphMinAdvanceX = 13.0f; // Use if you want to make the icon monospaced
+io.Fonts->AddFontFromFileTTF("fonts/fontawesome-webfont.ttf", 13.0f, &config);
+```
+:rewind: **Before 1.92:**
+```cpp
+// Merge icons into default tool font
+#include "IconsFontAwesome.h"
+ImGuiIO& io = ImGui::GetIO();
+io.Fonts->AddFontDefault();
ImFontConfig config;
config.MergeMode = true;
@@ -241,7 +299,8 @@ See Links below for other icons fonts and related tools.
**Monospace Icons?**
-To make your icon look more monospace and facilitate alignment, you may want to set the ImFontConfig::GlyphMinAdvanceX value when loading an icon font.
+To make your icon look more monospace and facilitate alignment, you may want to set the `ImFontConfig::GlyphMinAdvanceX` value when loading an icon font.
+If you `GlyphMinAdvanceX` you need to pass a `font_size` to `AddFontXXX()` calls, as the MinAdvanceX value will be specified for the given size and scaled otherwise.
**Screenshot**
@@ -252,11 +311,49 @@ Here's an application using icons ("Avoyd", https://www.avoyd.com):
---------------------------------------
+### Excluding Overlapping Ranges
+
+🆕 **Since 1.92, with an up to date backend: glyphs ranges are ignored**: when loading a glyph, input fonts in the merge list are queried in order. The first font which has the glyph loads it.
+ ‼️ **If you are merging several fonts, you may have undesirable overlapping ranges.** You can use `ImFontConfig::GlyphExcludeRanges[] `to specify ranges to ignore in a given Input.
+
+```cpp
+// Add Font Source 1 but ignore ICON_MIN_FA..ICON_MAX_FA range
+static ImWchar exclude_ranges[] = { ICON_MIN_FA, ICON_MAX_FA, 0 };
+ImFontConfig cfg1;
+cfg1.GlyphExcludeRanges = exclude_ranges;
+io.Fonts->AddFontFromFileTTF("segoeui.ttf", 0.0f, &cfg1);
+
+// Add Font Source 2, which expects to use the range above
+ImFontConfig cfg2;
+cfg2.MergeMode = true;
+io.Fonts->AddFontFromFileTTF("FontAwesome4.ttf", 0.0f, &cfg2);
+```
+Another (silly) example:
+```cpp
+// Remove 'A'-'Z' from first font
+static ImWchar exclude_ranges[] = { 'A', 'Z', 0 };
+ImFontConfig cfg1;
+cfg1.GlyphExcludeRanges = exclude_ranges;
+io.Fonts->AddFontFromFileTTF("segoeui.ttf", 0.0f, &cfg1);
+
+// Load another font to fill the gaps
+ImFontConfig cfg2;
+cfg2.MergeMode = true;
+io.Fonts->AddFontFromFileTTF("Roboto-Medium.ttf", 0.0f, &cfg2);
+```
+
+
+You can use `Metrics/Debugger->Fonts->Font->Input Glyphs Overlap Detection Tool` to see list of glyphs available in multiple font sources. This can facilitate understanding which font input is providing which glyph.
+
+##### [Return to Index](#index)
+
+---------------------------------------
+
## Using FreeType Rasterizer (imgui_freetype)
-- Dear ImGui uses imstb\_truetype.h to rasterize fonts (with optional oversampling). This technique and its implementation are not ideal for fonts rendered at small sizes, which may appear a little blurry or hard to read.
-- There is an implementation of the ImFontAtlas builder using FreeType that you can use in the [misc/freetype/](https://github.com/ocornut/imgui/tree/master/misc/freetype) folder.
-- FreeType supports auto-hinting which tends to improve the readability of small fonts.
+- Dear ImGui uses [stb_truetype.h](https://github.com/nothings/stb/) to rasterize fonts (with optional oversampling). This technique and its implementation are not ideal for fonts rendered at small sizes, which may appear a little blurry or hard to read.
+- You can however use `imgui_freetype.cpp` from the [misc/freetype/](https://github.com/ocornut/imgui/tree/master/misc/freetype) folder. Compile with this file and add `#define IMGUI_ENABLE_FREETYPE` to your imconfig.h file or build system to automatically activate it.
+- FreeType supports auto-hinting which tends to improve the readability of small fonts. It makes a big difference especially at smaller resolutions.
- Read documentation in the [misc/freetype/](https://github.com/ocornut/imgui/tree/master/misc/freetype) folder.
- Correct sRGB space blending will have an important effect on your font rendering quality.
@@ -278,10 +375,9 @@ Here's an application using icons ("Avoyd", https://www.avoyd.com):
io.Fonts->AddFontFromFileTTF("../../../imgui_dev/data/fonts/NotoSans-Regular.ttf", 16.0f);
static ImWchar ranges[] = { 0x1, 0x1FFFF, 0 };
static ImFontConfig cfg;
-cfg.OversampleH = cfg.OversampleV = 1;
cfg.MergeMode = true;
-cfg.FontBuilderFlags |= ImGuiFreeTypeBuilderFlags_LoadColor;
-io.Fonts->AddFontFromFileTTF("C:\\Windows\\Fonts\\seguiemj.ttf", 16.0f, &cfg, ranges);
+cfg.FontLoaderFlags |= ImGuiFreeTypeLoaderFlags_LoadColor;
+io.Fonts->AddFontFromFileTTF("C:\\Windows\\Fonts\\seguiemj.ttf", 16.0f, &cfg);
```
##### [Return to Index](#index)
@@ -290,7 +386,9 @@ io.Fonts->AddFontFromFileTTF("C:\\Windows\\Fonts\\seguiemj.ttf", 16.0f, &cfg, ra
## Using Custom Glyph Ranges
-You can use the `ImFontGlyphRangesBuilder` helper to create glyph ranges based on text input. For example: for a game where your script is known, if you can feed your entire script to it and only build the characters the game needs.
+🆕 **Since 1.92, with an up to date backend: specifying glyph ranges is unnecessary. Therefore this is not really useful any more.**
+
+:rewind: You can use the `ImFontGlyphRangesBuilder` helper to create glyph ranges based on text input. For example: for a game where your script is known, if you can feed your entire script to it and only build the characters the game needs.
```cpp
ImVector ranges;
ImFontGlyphRangesBuilder builder;
@@ -309,10 +407,19 @@ io.Fonts->Build(); // Build the atlas while
## Using Custom Colorful Icons
+🆕 **Since 1.92, with an up to date backend: this system has been revamped.**
+
+TL;DR; With the new system, it is recommended that you create a custom `ImFontLoader` and register your fonts with it.
+`AddCustomRectFontGlyph()` has been obsoleted because its API does not make much sense with resizable fonts.
+
+You can ask questions in [#8466](https://github.com/ocornut/imgui/issues/8466).
+
+:rewind: **Before 1.92:**
+
As an alternative to rendering colorful glyphs using imgui_freetype with `ImGuiFreeTypeBuilderFlags_LoadColor`, you may allocate your own space in the texture atlas and write yourself into it. **(This is a BETA api, use if you are familiar with dear imgui and with your rendering backend)**
- You can use the `ImFontAtlas::AddCustomRect()` and `ImFontAtlas::AddCustomRectFontGlyph()` api to register rectangles that will be packed into the font atlas texture. Register them before building the atlas, then call Build()`.
-- You can then use `ImFontAtlas::GetCustomRectByIndex(int)` to query the position/size of your rectangle within the texture, and blit/copy any graphics data of your choice into those rectangles.
+- You can then use `ImFontAtlas::GetCustomRect(int)` to query the position/size of your rectangle within the texture, and blit/copy any graphics data of your choice into those rectangles.
- This API is beta because it is likely to change in order to support multi-dpi (multiple viewports on multiple monitors with varying DPI scale).
#### Pseudo-code:
@@ -332,9 +439,7 @@ int tex_width, tex_height;
io.Fonts->GetTexDataAsRGBA32(&tex_pixels, &tex_width, &tex_height);
for (int rect_n = 0; rect_n < IM_ARRAYSIZE(rect_ids); rect_n++)
-{
- int rect_id = rect_ids[rect_n];
- if (const ImFontAtlasCustomRect* rect = io.Fonts->GetCustomRectByIndex(rect_id))
+ if (const ImTextureRect* rect = io.Fonts->GetCustomRect(rect_ids[rect_n]))
{
// Fill the custom rectangle with red pixels (in reality you would draw/copy your bitmap data here!)
for (int y = 0; y < rect->Height; y++)
@@ -344,7 +449,6 @@ for (int rect_n = 0; rect_n < IM_ARRAYSIZE(rect_ids); rect_n++)
*p++ = IM_COL32(255, 0, 0, 255);
}
}
-}
```
##### [Return to Index](#index)
@@ -483,15 +587,18 @@ Some fonts files are available in the `misc/fonts/` folder:
#### MONOSPACE FONTS
+
+
Pixel Perfect:
- Proggy Fonts, by Tristan Grimmer http://www.proggyfonts.net or http://upperboundsinteractive.com/fonts.php
- Sweet16, Sweet16 Mono, by Martin Sedlak (Latin + Supplemental + Extended A) https://github.com/kmar/Sweet16Font (also include an .inl file to use directly in dear imgui.)
Regular:
-- Google Noto Mono Fonts https://www.google.com/get/noto/
-- Typefaces for source code beautification https://github.com/chrissimpkins/codeface
-- Programmation fonts http://s9w.github.io/font_compare/
-- Inconsolata http://www.levien.com/type/myfonts/inconsolata.html
+- ProggyVector if you want the old school Dear ImGui font to scale: https://github.com/bluescan/proggyfonts
+- Google Noto Mono Fonts: https://www.google.com/get/noto/
+- Typefaces for source code beautification: https://github.com/chrissimpkins/codeface
+- Programmation fonts: http://s9w.github.io/font_compare/
+- Inconsolata: http://www.levien.com/type/myfonts/inconsolata.html
- Adobe Source Code Pro: Monospaced font family for ui & coding environments https://github.com/adobe-fonts/source-code-pro
- Monospace/Fixed Width Programmer's Fonts http://www.lowing.org/fonts/
diff --git a/docs/README.md b/docs/README.md
index 9b437311a..45977e74f 100644
--- a/docs/README.md
+++ b/docs/README.md
@@ -16,7 +16,7 @@ Businesses: support continued development and maintenance via invoiced sponsorin
| [The Pitch](#the-pitch) - [Usage](#usage) - [How it works](#how-it-works) - [Releases & Changelogs](#releases--changelogs) - [Demo](#demo) - [Getting Started & Integration](#getting-started--integration) |
:----------------------------------------------------------: |
| [Gallery](#gallery) - [Support, FAQ](#support-frequently-asked-questions-faq) - [How to help](#how-to-help) - **[Funding & Sponsors](https://github.com/ocornut/imgui/wiki/Funding)** - [Credits](#credits) - [License](#license) |
-| [Wiki](https://github.com/ocornut/imgui/wiki) - [Extensions](https://github.com/ocornut/imgui/wiki/Useful-Extensions) - [Languages bindings & frameworks backends](https://github.com/ocornut/imgui/wiki/Bindings) - [Software using Dear ImGui](https://github.com/ocornut/imgui/wiki/Software-using-dear-imgui) - [User quotes](https://github.com/ocornut/imgui/wiki/Quotes) |
+| [Wiki](https://github.com/ocornut/imgui/wiki) - [Extensions](https://github.com/ocornut/imgui/wiki/Useful-Extensions) - [Language bindings & framework backends](https://github.com/ocornut/imgui/wiki/Bindings) - [Software using Dear ImGui](https://github.com/ocornut/imgui/wiki/Software-using-dear-imgui) - [User quotes](https://github.com/ocornut/imgui/wiki/Quotes) |
### The Pitch
@@ -107,12 +107,12 @@ Reading the changelogs is a good way to keep up to date with the things Dear ImG
### Demo
-Calling the `ImGui::ShowDemoWindow()` function will create a demo window showcasing a variety of features and examples. The code is always available for reference in `imgui_demo.cpp`. [Here's how the demo looks](https://raw.githubusercontent.com/wiki/ocornut/imgui/web/v167/v167-misc.png).
+Calling the `ImGui::ShowDemoWindow()` function will create a demo window showcasing a variety of features and examples. The code is always available for reference in `imgui_demo.cpp`.
+- [Web version of the demo](https://pthom.github.io/imgui_manual_online/manual/imgui_manual.html) courtesy of [@pthom](https://github.com/pthom).
+- [Screenshot of the demo](https://raw.githubusercontent.com/wiki/ocornut/imgui/web/v167/v167-misc.png).
You should be able to build the examples from sources. If you don't, let us know! If you want to have a quick look at some Dear ImGui features, you can download Windows binaries of the demo app here:
-- [imgui-demo-binaries-20241211.zip](https://www.dearimgui.com/binaries/imgui-demo-binaries-20241211.zip) (Windows, 1.91.6, built 2024/11/11, master) or [older binaries](https://www.dearimgui.com/binaries).
-
-The demo applications are not DPI aware so expect some blurriness on a 4K screen. For DPI awareness in your application, you can load/reload your font at a different scale and scale your style with `style.ScaleAllSizes()` (see [FAQ](https://www.dearimgui.com/faq)).
+- [imgui-demo-binaries-20250625.zip](https://www.dearimgui.com/binaries/imgui-demo-binaries-20250625.zip) (Windows, 1.92.0, built 2025/06/25, master) or [older binaries](https://www.dearimgui.com/binaries).
### Getting Started & Integration
@@ -120,9 +120,13 @@ See the [Getting Started](https://github.com/ocornut/imgui/wiki/Getting-Started)
On most platforms and when using C++, **you should be able to use a combination of the [imgui_impl_xxxx](https://github.com/ocornut/imgui/tree/master/backends) backends without modification** (e.g. `imgui_impl_win32.cpp` + `imgui_impl_dx11.cpp`). If your engine supports multiple platforms, consider using more imgui_impl_xxxx files instead of rewriting them: this will be less work for you, and you can get Dear ImGui running immediately. You can _later_ decide to rewrite a custom backend using your custom engine functions if you wish so.
-Integrating Dear ImGui within your custom engine is a matter of 1) wiring mouse/keyboard/gamepad inputs 2) uploading a texture to your GPU/render engine 3) providing a render function that can bind textures and render textured triangles, which is essentially what Backends are doing. The [examples/](https://github.com/ocornut/imgui/tree/master/examples) folder is populated with applications doing just that: setting up a window and using backends. If you follow the [Getting Started](https://github.com/ocornut/imgui/wiki/Getting-Started) guide it should in theory takes you less than an hour to integrate Dear ImGui. **Make sure to spend time reading the [FAQ](https://www.dearimgui.com/faq), comments, and the examples applications!**
+Integrating Dear ImGui within your custom engine is a matter of mainly 1) wiring mouse/keyboard/gamepad inputs 2) uploading a texture to your GPU/render engine 3) providing a render function that can create/update textures and render textured triangles. This is exactly what backends are doing.
+- The [examples/](https://github.com/ocornut/imgui/tree/master/examples) folder is populated with applications setting up a window and using standard backends.
+- The [Getting Started](https://github.com/ocornut/imgui/wiki/Getting-Started) guide has instructions to integrate imgui into an existing application using standard backends. It should in theory take you less than an hour to integrate Dear ImGui into your existing codebase where support libraries are linked. Less if you read carefully.
+- The [Backends](https://github.com/ocornut/imgui/blob/master/docs/BACKENDS.md) guide explains what backends are doing, and has instructions to implement a custom backend. You can also refer to the source code of our ~20 backends to understand how they work.
+- Generally, **make sure to spend time reading the [FAQ](https://www.dearimgui.com/faq), comments, and the examples applications!**
-Officially maintained backends/bindings (in repository):
+Officially maintained backends (in repository):
- Renderers: DirectX9, DirectX10, DirectX11, DirectX12, Metal, OpenGL/ES/ES2, SDL_GPU, SDL_Renderer2/3, Vulkan, WebGPU.
- Platforms: GLFW, SDL2/SDL3, Win32, Glut, OSX, Android.
- Frameworks: Allegro5, Emscripten.
@@ -130,7 +134,7 @@ Officially maintained backends/bindings (in repository):
[Third-party backends/bindings](https://github.com/ocornut/imgui/wiki/Bindings) wiki page:
- Languages: C, C# and: Beef, ChaiScript, CovScript, Crystal, D, Go, Haskell, Haxe/hxcpp, Java, JavaScript, Julia, Kotlin, Lobster, Lua, Nim, Odin, Pascal, PureBasic, Python, ReaScript, Ruby, Rust, Swift, Zig...
- Frameworks: AGS/Adventure Game Studio, Amethyst, Blender, bsf, Cinder, Cocos2d-x, Defold, Diligent Engine, Ebiten, Flexium, GML/Game Maker Studio, GLEQ, Godot, GTK3, Irrlicht Engine, JUCE, LÖVE+LUA, Mach Engine, Magnum, Marmalade, Monogame, NanoRT, nCine, Nim Game Lib, Nintendo 3DS/Switch/WiiU (homebrew), Ogre, openFrameworks, OSG/OpenSceneGraph, Orx, Photoshop, px_render, Qt/QtDirect3D, raylib, SFML, Sokol, Unity, Unreal Engine 4/5, UWP, vtk, VulkanHpp, VulkanSceneGraph, Win32 GDI, WxWidgets.
-- Many bindings are auto-generated (by good old [cimgui](https://github.com/cimgui/cimgui) or newer/experimental [dear_bindings](https://github.com/dearimgui/dear_bindings)), you can use their metadata output to generate bindings for other languages.
+- Many bindings are auto-generated (by good old [cimgui](https://github.com/cimgui/cimgui) or our newer [dear_bindings](https://github.com/dearimgui/dear_bindings)), you can use their metadata output to generate bindings for other languages.
[Useful Extensions/Widgets](https://github.com/ocornut/imgui/wiki/Useful-Extensions) wiki page:
- Automation/testing, Text editors, node editors, timeline editors, plotting, software renderers, remote network access, memory editors, gizmos, etc. Notable and well supported extensions include [ImPlot](https://github.com/epezent/implot) and [Dear ImGui Test Engine](https://github.com/ocornut/imgui_test_engine).
@@ -184,7 +188,7 @@ How to help
- See [GitHub Forum/Issues](https://github.com/ocornut/imgui/issues).
- You may help with development and submit pull requests! Please understand that by submitting a PR you are also submitting a request for the maintainer to review your code and then take over its maintenance forever. PR should be crafted both in the interest of the end-users and also to ease the maintainer into understanding and accepting it.
- See [Help wanted](https://github.com/ocornut/imgui/wiki/Help-Wanted) on the [Wiki](https://github.com/ocornut/imgui/wiki/) for some more ideas.
-- Be a [Funding Supporter](https://github.com/ocornut/imgui/wiki/Funding)! Have your company financially support this project via invoiced sponsors/maintenance or by buying a license for [Dear ImGui Test Engine](https://github.com/ocornut/imgui_test_engine) (please reach out: omar AT dearimgui DOT com).
+- Be a [Funding Supporter](https://github.com/ocornut/imgui/wiki/Funding)! Have your company financially support this project via invoiced sponsors/maintenance or by buying a license for [Dear ImGui Test Engine](https://github.com/ocornut/imgui_test_engine) (please reach out: contact AT dearimgui DOT com).
Sponsors
--------
@@ -214,7 +218,7 @@ Omar: "I first discovered the IMGUI paradigm at [Q-Games](https://www.q-games.co
Embeds [ProggyClean.ttf](https://www.proggyfonts.net) font by Tristan Grimmer (MIT license).
Embeds [stb_textedit.h, stb_truetype.h, stb_rect_pack.h](https://github.com/nothings/stb/) by Sean Barrett (public domain).
-Inspiration, feedback, and testing for early versions: Casey Muratori, Atman Binstock, Mikko Mononen, Emmanuel Briney, Stefan Kamoda, Anton Mikhailov, Matt Willis. Also thank you to everyone posting feedback, questions and patches on GitHub.
+Inspiration, feedback, and testing for early versions: Casey Muratori, Atman Binstock, Mikko Mononen, Emmanuel Briney, Stefan Kamoda, Anton Mikhailov, Matt Willis. Special thanks to Alex Evans, Patrick Doane, Marco Koegler for kindly helping. Also thank you to everyone posting feedback, questions and patches on GitHub.
License
-------
diff --git a/docs/TODO.txt b/docs/TODO.txt
index 6a420e0b0..dbeee1db4 100644
--- a/docs/TODO.txt
+++ b/docs/TODO.txt
@@ -26,9 +26,6 @@ It's mostly a bunch of personal notes, probably incomplete. Feel free to query i
- window/size: manually triggered auto-fit (double-click on grip) shouldn't resize window down to viewport size?
- window/size: how to allow to e.g. auto-size vertically to fit contents, but be horizontally resizable? Assuming SetNextWindowSize() is modified to treat -1.0f on each axis as "keep as-is" (would be good but might break erroneous code): Problem is UpdateWindowManualResize() and lots of code treat (window->AutoFitFramesX > 0 || window->AutoFitFramesY > 0) together.
- window/opt: freeze window flag: if not focused/hovered, return false, render with previous ImDrawList. and/or reduce refresh rate. -> this may require enforcing that it is illegal to submit contents if Begin returns false.
- - window/child: background options for child windows, border option (disable rounding).
- - window/child: allow resizing of child windows (possibly given min/max for each axis?.)
- - window/child: allow SetNextWindowContentSize() to work on child windows.
- window/clipping: some form of clipping when DisplaySize (or corresponding viewport) is zero.
- window/tabbing: add a way to signify that a window or docked window requires attention (e.g. blinking title bar, trying to click behind a modal).
- window/id_stack: add e.g. window->GetIDFromPath() with support for leading / and ../ (#1390, #331) -> model from test engine.
@@ -90,7 +87,6 @@ It's mostly a bunch of personal notes, probably incomplete. Feel free to query i
- input text multi-line: line numbers? status bar? (follow up on #200)
- input text multi-line: behave better when user changes input buffer while editing is active (even though it is illegal behavior). namely, the change of buffer can create a scrollbar glitch (#725)
- input text multi-line: better horizontal scrolling support (#383, #1224)
- - input text multi-line: single call to AddText() should be coarse clipped on InputTextEx() end.
- input number: optional range min/max for Input*() functions
- input number: holding [-]/[+] buttons could increase the step speed non-linearly (or user-controlled)
- input number: use mouse wheel to step up/down
@@ -272,22 +268,14 @@ It's mostly a bunch of personal notes, probably incomplete. Feel free to query i
- font: arbitrary line spacing. (#2945)
- font: MergeMode: flags to select overwriting or not (this is now very easy with refactored ImFontAtlasBuildWithStbTruetype)
- - font: free the Alpha buffer if user only requested RGBA.
-!- font: better CalcTextSizeA() API, at least for simple use cases. current one is horrible (perhaps have simple vs extended versions).
+ - font: better CalcTextSizeA() API, at least for simple use cases. current one is horrible (perhaps have simple vs extended versions).
- font: for the purpose of RenderTextEllipsis(), it might be useful that CalcTextSizeA() can ignore the trailing padding?
- font: a CalcTextHeight() helper could run faster than CalcTextSize().y
- font: enforce monospace through ImFontConfig (for icons?) + create dual ImFont output from same input, reusing rasterized data but with different glyphs/AdvanceX
- - font: finish CustomRectRegister() to allow mapping Unicode codepoint to custom texture data
- - font: remove ID from CustomRect registration, it seems unnecessary!
- font: make it easier to submit own bitmap font (same texture, another texture?). (#2127, #2575)
- - font: PushFontSize API (#1018)
- font: MemoryTTF taking ownership confusing/not obvious, maybe default should be opposite?
- font: storing MinAdvanceX per font would allow us to skip calculating line width (under a threshold of character count) in loops looking for block width
- - font/demo: add tools to show glyphs used by a text blob, display U16 value, list missing glyphs.
- font/demo: demonstrate use of ImFontGlyphRangesBuilder.
- - font/atlas: add a missing Glyphs.reserve()
- - font/atlas: incremental updates
- - font/atlas: dynamic font atlas to avoid baking huge ranges into bitmap and make scaling easier.
- font/draw: vertical and/or rotated text renderer (#705) - vertical is easier clipping wise
- font/draw: need to be able to specify wrap start position.
- font/draw: better reserve policy for large horizontal block of text (shouldn't reserve for all clipped lines). also see #3349.
@@ -296,7 +284,6 @@ It's mostly a bunch of personal notes, probably incomplete. Feel free to query i
- font: optimization: for monospace font (like the default one) we can trim IndexXAdvance as long as trailing value is == FallbackXAdvance (need to make sure TAB is still correct), would save on cache line.
- font: add support for kerning, probably optional. A) perhaps default to (32..128)^2 matrix ~ 9K entries = 36KB, then hash for non-ascii?. B) or sparse lookup into per-char list?
- font: add a simpler CalcTextSizeA() api? current one ok but not welcome if user needs to call it directly (without going through ImGui::CalcTextSize)
- - font: fix AddRemapChar() to work before atlas has been built.
- font: (api breaking) remove "TTF" from symbol names. also because it now supports OTF.
- font/opt: Considering storing standalone AdvanceX table as 16-bit fixed point integer?
- font/opt: Glyph currently 40 bytes (2+9*4). Consider storing UV as 16-bits integer? (->32 bytes). X0/Y0/X1/Y1 as 16 fixed-point integers? Or X0/Y0 as float and X1/Y1 as fixed8_8?
@@ -327,7 +314,7 @@ It's mostly a bunch of personal notes, probably incomplete. Feel free to query i
- nav/windowing: Resizing window will currently fail with certain types of resizing constraints/callback applied
- focus: preserve ActiveId/focus stack state, e.g. when opening a menu and close it, previously selected InputText() focus gets restored (#622)
- - viewport: make it possible to have no main/hosting viewport
+ - viewport: make it possible to have no main/hosting viewport (see #8268)
- viewport: We set ImGuiViewportFlags_NoFocusOnAppearing in a way that is required for GLFW/SDL binding, but could be handled better without
on a custom e.g. Win32 bindings. It prevents newly dragged-out viewports from taking the focus, which makes ALT+F4 more ambiguous.
- viewport: not focusing newly undocked viewport means clicking back on previous one doesn't bring OS window to front.
@@ -336,6 +323,7 @@ It's mostly a bunch of personal notes, probably incomplete. Feel free to query i
- viewport: store per-viewport/monitor DPI in .ini file so an application reload or main window changing DPI on reload can be properly patched for.
- viewport: implicit/fallback Debug window can hog a zombie viewport (harmless, noisy?) > could at least clear out the reference on a per session basis?
- viewport: need to clarify how to use GetMousePos() from a user point of view.
+ - viewport: could apply monitor boundaries to cliprect to coarse clip things out of monitor? (#8698)
- platform: glfw: no support for ImGuiBackendFlags_HasMouseHoveredViewport.
- platform: sdl: no support for ImGuiBackendFlags_HasMouseHoveredViewport. maybe we could use SDL_GetMouseFocus() / SDL_WINDOW_MOUSE_FOCUS if imgui could fallback on its heuristic when NoInputs is set
- platform: sdl: no refresh of monitor/display (SDL doesn't seem to have an event for it).
diff --git a/examples/example_allegro5/main.cpp b/examples/example_allegro5/main.cpp
index 298c8451d..8476599d3 100644
--- a/examples/example_allegro5/main.cpp
+++ b/examples/example_allegro5/main.cpp
@@ -28,7 +28,7 @@ int main(int, char**)
al_install_mouse();
al_init_primitives_addon();
al_set_new_display_flags(ALLEGRO_RESIZABLE);
- ALLEGRO_DISPLAY* display = al_create_display(1280, 720);
+ ALLEGRO_DISPLAY* display = al_create_display(1280, 800);
al_set_window_title(display, "Dear ImGui Allegro 5 example");
ALLEGRO_EVENT_QUEUE* queue = al_create_event_queue();
al_register_event_source(queue, al_get_display_event_source(display));
@@ -53,16 +53,16 @@ int main(int, char**)
// - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
// - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
// - If the file cannot be loaded, the function will return a nullptr. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
- // - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
// - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use Freetype for higher quality font rendering.
- // - Read 'docs/FONTS.md' for more instructions and details.
+ // - Read 'docs/FONTS.md' for more instructions and details. If you like the default font but want it to scale better, consider using the 'ProggyVector' from the same author!
// - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
+ //style.FontSizeBase = 20.0f;
//io.Fonts->AddFontDefault();
- //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf", 18.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
- //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, nullptr, io.Fonts->GetGlyphRangesJapanese());
+ //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf");
+ //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf");
//IM_ASSERT(font != nullptr);
bool show_demo_window = true;
diff --git a/examples/example_android_opengl3/android/.gitignore b/examples/example_android_opengl3/android/.gitignore
index 3c7a61910..41afcd57c 100644
--- a/examples/example_android_opengl3/android/.gitignore
+++ b/examples/example_android_opengl3/android/.gitignore
@@ -1,12 +1,14 @@
-.cxx
-.externalNativeBuild
build/
*.iml
-
-.idea
.gradle
+.idea
+.DS_Store
+/captures
+.externalNativeBuild
+.cxx
local.properties
# Android Studio puts a Gradle wrapper here, that we don't want:
gradle/
+!gradle/libs.versions.toml
gradlew*
diff --git a/examples/example_android_opengl3/android/app/build.gradle b/examples/example_android_opengl3/android/app/build.gradle
index 3a68c8371..e142f1917 100644
--- a/examples/example_android_opengl3/android/app/build.gradle
+++ b/examples/example_android_opengl3/android/app/build.gradle
@@ -1,16 +1,16 @@
-apply plugin: 'com.android.application'
-apply plugin: 'kotlin-android'
+plugins {
+ alias(libs.plugins.android.application)
+ alias(libs.plugins.kotlin.android)
+}
android {
- compileSdkVersion 33
- buildToolsVersion "33.0.2"
- ndkVersion "25.2.9519653"
+ namespace 'imgui.example.android'
+ compileSdk 36
defaultConfig {
applicationId "imgui.example.android"
- namespace "imgui.example.android"
- minSdkVersion 24
- targetSdkVersion 33
+ minSdk 24
+ targetSdk 36
versionCode 1
versionName "1.0"
}
@@ -21,26 +21,17 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt')
}
}
-
compileOptions {
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
-
kotlinOptions {
- jvmTarget="11"
+ jvmTarget = '11'
}
-
externalNativeBuild {
cmake {
- path "../../CMakeLists.txt"
+ path file('../../CMakeLists.txt')
version '3.22.1'
}
}
}
-repositories {
- mavenCentral()
-}
-dependencies {
- implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
-}
diff --git a/examples/example_android_opengl3/android/app/src/main/AndroidManifest.xml b/examples/example_android_opengl3/android/app/src/main/AndroidManifest.xml
index 5a1e2d9e5..6410b5a13 100644
--- a/examples/example_android_opengl3/android/app/src/main/AndroidManifest.xml
+++ b/examples/example_android_opengl3/android/app/src/main/AndroidManifest.xml
@@ -1,5 +1,6 @@
-
+AddFontFromMemoryTTF(font_data, font_data_size, 15.0f);
//IM_ASSERT(font != nullptr);
//font_data_size = GetAssetData("ArialUni.ttf", &font_data);
- //font = io.Fonts->AddFontFromMemoryTTF(font_data, font_data_size, 18.0f, nullptr, io.Fonts->GetGlyphRangesJapanese());
+ //font = io.Fonts->AddFontFromMemoryTTF(font_data, font_data_size, 18.0f);
//IM_ASSERT(font != nullptr);
// Arbitrary scale-up
diff --git a/examples/example_apple_metal/Makefile b/examples/example_apple_metal/Makefile
new file mode 100644
index 000000000..9412c9b53
--- /dev/null
+++ b/examples/example_apple_metal/Makefile
@@ -0,0 +1,21 @@
+# Makefile for example_apple_metal, for macOS only (**not iOS**)
+CXX = clang++
+EXE = example_apple_metal
+IMGUI_DIR = ../../
+SOURCES = main.mm
+SOURCES += $(IMGUI_DIR)/imgui.cpp $(IMGUI_DIR)/imgui_demo.cpp $(IMGUI_DIR)/imgui_draw.cpp $(IMGUI_DIR)/imgui_tables.cpp $(IMGUI_DIR)/imgui_widgets.cpp
+SOURCES += $(IMGUI_DIR)/backends/imgui_impl_osx.mm $(IMGUI_DIR)/backends/imgui_impl_metal.mm
+
+CXXFLAGS = -std=c++11 -ObjC++ -fobjc-arc -Wall -Wextra -I$(IMGUI_DIR) -I$(IMGUI_DIR)/backends
+FRAMEWORKS = -framework AppKit -framework Metal -framework MetalKit -framework QuartzCore -framework GameController
+
+all: $(EXE)
+
+$(EXE): $(SOURCES)
+ $(CXX) $(CXXFLAGS) $^ $(FRAMEWORKS) -o $@
+
+run: all
+ ./$(EXE)
+
+clean:
+ rm -f $(EXE) *.o
diff --git a/examples/example_apple_metal/example_apple_metal.xcodeproj/project.pbxproj b/examples/example_apple_metal/example_apple_metal.xcodeproj/project.pbxproj
index 3ebf9ccf9..ab181bb46 100644
--- a/examples/example_apple_metal/example_apple_metal.xcodeproj/project.pbxproj
+++ b/examples/example_apple_metal/example_apple_metal.xcodeproj/project.pbxproj
@@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
- objectVersion = 48;
+ objectVersion = 54;
objects = {
/* Begin PBXBuildFile section */
@@ -222,7 +222,8 @@
8307E7B620E9F9C700473790 /* Project object */ = {
isa = PBXProject;
attributes = {
- LastUpgradeCheck = 1200;
+ BuildIndependentTargetsInParallel = YES;
+ LastUpgradeCheck = 1530;
ORGANIZATIONNAME = "Warren Moore";
TargetAttributes = {
8307E7C320E9F9C900473790 = {
@@ -340,9 +341,11 @@
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COPY_PHASE_STRIP = NO;
+ DEAD_CODE_STRIPPING = YES;
DEBUG_INFORMATION_FORMAT = dwarf;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
+ ENABLE_USER_SCRIPT_SANDBOXING = YES;
GCC_C_LANGUAGE_STANDARD = gnu11;
GCC_DYNAMIC_NO_PIC = NO;
GCC_NO_COMMON_BLOCKS = YES;
@@ -396,9 +399,11 @@
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COPY_PHASE_STRIP = NO;
+ DEAD_CODE_STRIPPING = YES;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
ENABLE_NS_ASSERTIONS = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
+ ENABLE_USER_SCRIPT_SANDBOXING = YES;
GCC_C_LANGUAGE_STANDARD = gnu11;
GCC_NO_COMMON_BLOCKS = YES;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
@@ -418,8 +423,11 @@
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = "";
INFOPLIST_FILE = "$(SRCROOT)/iOS/Info-iOS.plist";
- IPHONEOS_DEPLOYMENT_TARGET = 10.0;
- LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
+ IPHONEOS_DEPLOYMENT_TARGET = 12.0;
+ LD_RUNPATH_SEARCH_PATHS = (
+ "$(inherited)",
+ "@executable_path/Frameworks",
+ );
PRODUCT_BUNDLE_IDENTIFIER = "org.imgui.example.apple-metal-ios";
PRODUCT_NAME = example_apple_metal;
SDKROOT = iphoneos;
@@ -435,8 +443,11 @@
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = "";
INFOPLIST_FILE = "$(SRCROOT)/iOS/Info-iOS.plist";
- IPHONEOS_DEPLOYMENT_TARGET = 10.0;
- LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
+ IPHONEOS_DEPLOYMENT_TARGET = 12.0;
+ LD_RUNPATH_SEARCH_PATHS = (
+ "$(inherited)",
+ "@executable_path/Frameworks",
+ );
PRODUCT_BUNDLE_IDENTIFIER = "org.imgui.example.apple-metal-ios";
PRODUCT_NAME = example_apple_metal;
SDKROOT = iphoneos;
@@ -452,10 +463,14 @@
CODE_SIGN_IDENTITY = "-";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
+ DEAD_CODE_STRIPPING = YES;
DEVELOPMENT_TEAM = "";
INFOPLIST_FILE = "$(SRCROOT)/macOS/Info-macOS.plist";
- LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";
- MACOSX_DEPLOYMENT_TARGET = 10.12;
+ LD_RUNPATH_SEARCH_PATHS = (
+ "$(inherited)",
+ "@executable_path/../Frameworks",
+ );
+ MACOSX_DEPLOYMENT_TARGET = "$(RECOMMENDED_MACOSX_DEPLOYMENT_TARGET)";
PRODUCT_BUNDLE_IDENTIFIER = "org.imgui.example.apple-metal-macos";
PRODUCT_NAME = example_apple_metal;
SDKROOT = macosx;
@@ -469,10 +484,14 @@
CODE_SIGN_IDENTITY = "-";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
+ DEAD_CODE_STRIPPING = YES;
DEVELOPMENT_TEAM = "";
INFOPLIST_FILE = "$(SRCROOT)/macOS/Info-macOS.plist";
- LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";
- MACOSX_DEPLOYMENT_TARGET = 10.12;
+ LD_RUNPATH_SEARCH_PATHS = (
+ "$(inherited)",
+ "@executable_path/../Frameworks",
+ );
+ MACOSX_DEPLOYMENT_TARGET = "$(RECOMMENDED_MACOSX_DEPLOYMENT_TARGET)";
PRODUCT_BUNDLE_IDENTIFIER = "org.imgui.example.apple-metal-macos";
PRODUCT_NAME = example_apple_metal;
SDKROOT = macosx;
diff --git a/examples/example_apple_metal/main.mm b/examples/example_apple_metal/main.mm
index 006a0f162..68bd2bba6 100644
--- a/examples/example_apple_metal/main.mm
+++ b/examples/example_apple_metal/main.mm
@@ -82,16 +82,16 @@
// - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
// - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
// - If the file cannot be loaded, the function will return a nullptr. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
- // - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
// - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use Freetype for higher quality font rendering.
- // - Read 'docs/FONTS.md' for more instructions and details.
+ // - Read 'docs/FONTS.md' for more instructions and details. If you like the default font but want it to scale better, consider using the 'ProggyVector' from the same author!
// - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
+ //style.FontSizeBase = 20.0f;
//io.Fonts->AddFontDefault();
- //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf", 18.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
- //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, nullptr, io.Fonts->GetGlyphRangesJapanese());
+ //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf");
+ //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf");
//IM_ASSERT(font != nullptr);
return self;
@@ -104,7 +104,7 @@
-(void)loadView
{
- self.view = [[MTKView alloc] initWithFrame:CGRectMake(0, 0, 1200, 720)];
+ self.view = [[MTKView alloc] initWithFrame:CGRectMake(0, 0, 1200, 800)];
}
-(void)viewDidLoad
@@ -336,9 +336,20 @@
#if TARGET_OS_OSX
-int main(int argc, const char * argv[])
+int main(int, const char**)
{
- return NSApplicationMain(argc, argv);
+ @autoreleasepool
+ {
+ [NSApplication sharedApplication];
+ [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
+
+ AppDelegate *appDelegate = [[AppDelegate alloc] init]; // creates window
+ [NSApp setDelegate:appDelegate];
+
+ [NSApp activateIgnoringOtherApps:YES];
+ [NSApp run];
+ }
+ return 0;
}
#else
diff --git a/examples/example_apple_opengl2/Makefile b/examples/example_apple_opengl2/Makefile
new file mode 100644
index 000000000..4ad5fa692
--- /dev/null
+++ b/examples/example_apple_opengl2/Makefile
@@ -0,0 +1,21 @@
+# Makefile for example_apple_metal, for macOS only (**not iOS**)
+CXX = clang++
+EXE = example_apple_opengl2
+IMGUI_DIR = ../../
+SOURCES = main.mm
+SOURCES += $(IMGUI_DIR)/imgui.cpp $(IMGUI_DIR)/imgui_demo.cpp $(IMGUI_DIR)/imgui_draw.cpp $(IMGUI_DIR)/imgui_tables.cpp $(IMGUI_DIR)/imgui_widgets.cpp
+SOURCES += $(IMGUI_DIR)/backends/imgui_impl_osx.mm $(IMGUI_DIR)/backends/imgui_impl_opengl2.cpp
+
+CXXFLAGS = -std=c++11 -ObjC++ -fobjc-arc -Wall -Wextra -I$(IMGUI_DIR) -I$(IMGUI_DIR)/backends
+FRAMEWORKS = -framework Cocoa -framework OpenGL -framework GameController
+
+all: $(EXE)
+
+$(EXE): $(SOURCES)
+ $(CXX) $(CXXFLAGS) $(SOURCES) -o $(EXE) $(FRAMEWORKS)
+
+run: all
+ ./$(EXE)
+
+clean:
+ rm -f $(EXE) *.o
diff --git a/examples/example_apple_opengl2/example_apple_opengl2.xcodeproj/project.pbxproj b/examples/example_apple_opengl2/example_apple_opengl2.xcodeproj/project.pbxproj
index a168373d4..9770e4396 100644
--- a/examples/example_apple_opengl2/example_apple_opengl2.xcodeproj/project.pbxproj
+++ b/examples/example_apple_opengl2/example_apple_opengl2.xcodeproj/project.pbxproj
@@ -289,7 +289,7 @@
isa = XCBuildConfiguration;
buildSettings = {
CODE_SIGN_STYLE = Automatic;
- MACOSX_DEPLOYMENT_TARGET = 10.12;
+ MACOSX_DEPLOYMENT_TARGET = 10.13;
PRODUCT_NAME = "$(TARGET_NAME)";
USER_HEADER_SEARCH_PATHS = ../..;
};
@@ -299,7 +299,7 @@
isa = XCBuildConfiguration;
buildSettings = {
CODE_SIGN_STYLE = Automatic;
- MACOSX_DEPLOYMENT_TARGET = 10.12;
+ MACOSX_DEPLOYMENT_TARGET = 10.13;
PRODUCT_NAME = "$(TARGET_NAME)";
USER_HEADER_SEARCH_PATHS = ../..;
};
diff --git a/examples/example_apple_opengl2/main.mm b/examples/example_apple_opengl2/main.mm
index 11829a820..25121b053 100644
--- a/examples/example_apple_opengl2/main.mm
+++ b/examples/example_apple_opengl2/main.mm
@@ -6,6 +6,8 @@
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
// - Introduction, links and more at the top of imgui.cpp
+// FIXME: Multi-viewports is not yet functional in this example. May need backend rework/coordination.
+
#import
#import
#import
@@ -70,16 +72,16 @@
// - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
// - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
// - If the file cannot be loaded, the function will return a nullptr. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
- // - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
// - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use Freetype for higher quality font rendering.
- // - Read 'docs/FONTS.md' for more instructions and details.
+ // - Read 'docs/FONTS.md' for more instructions and details. If you like the default font but want it to scale better, consider using the 'ProggyVector' from the same author!
// - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
+ //style.FontSizeBase = 20.0f;
//io.Fonts->AddFontDefault();
- //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf", 18.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
- //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, nullptr, io.Fonts->GetGlyphRangesJapanese());
+ //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf");
+ //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf");
//IM_ASSERT(font != nullptr);
}
@@ -188,7 +190,7 @@
if (_window != nil)
return (_window);
- NSRect viewRect = NSMakeRect(100.0, 100.0, 100.0 + 1280.0, 100 + 720.0);
+ NSRect viewRect = NSMakeRect(100.0, 100.0, 100.0 + 1280.0, 100 + 800.0);
_window = [[NSWindow alloc] initWithContentRect:viewRect styleMask:NSWindowStyleMaskTitled|NSWindowStyleMaskMiniaturizable|NSWindowStyleMaskResizable|NSWindowStyleMaskClosable backing:NSBackingStoreBuffered defer:YES];
[_window setTitle:@"Dear ImGui OSX+OpenGL2 Example"];
diff --git a/examples/example_glfw_metal/main.mm b/examples/example_glfw_metal/main.mm
index 2f346ffb8..bcb2c4a70 100644
--- a/examples/example_glfw_metal/main.mm
+++ b/examples/example_glfw_metal/main.mm
@@ -27,61 +27,67 @@ static void glfw_error_callback(int error, const char* description)
int main(int, char**)
{
+ glfwSetErrorCallback(glfw_error_callback);
+ if (!glfwInit())
+ return 1;
+
+ // Create window with graphics context
+ float main_scale = ImGui_ImplGlfw_GetContentScaleForMonitor(glfwGetPrimaryMonitor()); // Valid on GLFW 3.3+ only
+ glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
+ GLFWwindow* window = glfwCreateWindow((int)(1280 * main_scale), (int)(800 * main_scale), "Dear ImGui GLFW+Metal example", nullptr, nullptr);
+ if (window == nullptr)
+ return 1;
+
// Setup Dear ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
- io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
- io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
- io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking
- io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows
+ io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
+ io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
+ io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking
+ io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows
- // Setup style
+ // Setup Dear ImGui style
ImGui::StyleColorsDark();
//ImGui::StyleColorsLight();
- // When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
+ // Setup scaling
ImGuiStyle& style = ImGui::GetStyle();
+ style.ScaleAllSizes(main_scale); // Bake a fixed style scale. (until we have a solution for dynamic style scaling, changing this requires resetting Style + calling this again)
+ style.FontScaleDpi = main_scale; // Set initial font scale. (using io.ConfigDpiScaleFonts=true makes this unnecessary. We leave both here for documentation purpose)
+ io.ConfigDpiScaleFonts = true; // [Experimental] Automatically overwrite style.FontScaleDpi in Begin() when Monitor DPI changes. This will scale fonts but _NOT_ scale sizes/padding for now.
+ io.ConfigDpiScaleViewports = true; // [Experimental] Scale Dear ImGui and Platform Windows when Monitor DPI changes.
+
+ // When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{
style.WindowRounding = 0.0f;
style.Colors[ImGuiCol_WindowBg].w = 1.0f;
}
- // Load Fonts
- // - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
- // - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
- // - If the file cannot be loaded, the function will return a nullptr. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
- // - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
- // - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use Freetype for higher quality font rendering.
- // - Read 'docs/FONTS.md' for more instructions and details.
- // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
- //io.Fonts->AddFontDefault();
- //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf", 18.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
- //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, nullptr, io.Fonts->GetGlyphRangesJapanese());
- //IM_ASSERT(font != nullptr);
-
- // Setup window
- glfwSetErrorCallback(glfw_error_callback);
- if (!glfwInit())
- return 1;
-
- // Create window with graphics context
- glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
- GLFWwindow* window = glfwCreateWindow(1280, 720, "Dear ImGui GLFW+Metal example", nullptr, nullptr);
- if (window == nullptr)
- return 1;
-
id device = MTLCreateSystemDefaultDevice();
id commandQueue = [device newCommandQueue];
// Setup Platform/Renderer backends
- ImGui_ImplGlfw_InitForOther(window, true);
+ ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplMetal_Init(device);
+ // Load Fonts
+ // - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
+ // - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
+ // - If the file cannot be loaded, the function will return a nullptr. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
+ // - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use Freetype for higher quality font rendering.
+ // - Read 'docs/FONTS.md' for more instructions and details. If you like the default font but want it to scale better, consider using the 'ProggyVector' from the same author!
+ // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
+ //style.FontSizeBase = 20.0f;
+ //io.Fonts->AddFontDefault();
+ //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf");
+ //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf");
+ //IM_ASSERT(font != nullptr);
+
NSWindow *nswin = glfwGetCocoaWindow(window);
CAMetalLayer *layer = [CAMetalLayer layer];
layer.device = device;
diff --git a/examples/example_glfw_opengl2/Makefile b/examples/example_glfw_opengl2/Makefile
index 1f15c15c0..ecfdec7b1 100644
--- a/examples/example_glfw_opengl2/Makefile
+++ b/examples/example_glfw_opengl2/Makefile
@@ -21,6 +21,7 @@ SOURCES += $(IMGUI_DIR)/imgui.cpp $(IMGUI_DIR)/imgui_demo.cpp $(IMGUI_DIR)/imgui
SOURCES += $(IMGUI_DIR)/backends/imgui_impl_glfw.cpp $(IMGUI_DIR)/backends/imgui_impl_opengl2.cpp
OBJS = $(addsuffix .o, $(basename $(notdir $(SOURCES))))
UNAME_S := $(shell uname -s)
+LINUX_GL_LIBS = -lGL
CXXFLAGS = -std=c++11 -I$(IMGUI_DIR) -I$(IMGUI_DIR)/backends
CXXFLAGS += -g -Wall -Wformat
@@ -32,7 +33,7 @@ LIBS =
ifeq ($(UNAME_S), Linux) #LINUX
ECHO_MESSAGE = "Linux"
- LIBS += -lGL `pkg-config --static --libs glfw3`
+ LIBS += $(LINUX_GL_LIBS) -lX11 `pkg-config --static --libs glfw3`
CXXFLAGS += `pkg-config --cflags glfw3`
CFLAGS = $(CXXFLAGS)
diff --git a/examples/example_glfw_opengl2/main.cpp b/examples/example_glfw_opengl2/main.cpp
index df14dca69..aa6816f4e 100644
--- a/examples/example_glfw_opengl2/main.cpp
+++ b/examples/example_glfw_opengl2/main.cpp
@@ -40,7 +40,8 @@ int main(int, char**)
return 1;
// Create window with graphics context
- GLFWwindow* window = glfwCreateWindow(1280, 720, "Dear ImGui GLFW+OpenGL2 example", nullptr, nullptr);
+ float main_scale = ImGui_ImplGlfw_GetContentScaleForMonitor(glfwGetPrimaryMonitor()); // Valid on GLFW 3.3+ only
+ GLFWwindow* window = glfwCreateWindow((int)(1280 * main_scale), (int)(800 * main_scale), "Dear ImGui GLFW+OpenGL2 example", nullptr, nullptr);
if (window == nullptr)
return 1;
glfwMakeContextCurrent(window);
@@ -61,8 +62,14 @@ int main(int, char**)
ImGui::StyleColorsDark();
//ImGui::StyleColorsLight();
- // When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
+ // Setup scaling
ImGuiStyle& style = ImGui::GetStyle();
+ style.ScaleAllSizes(main_scale); // Bake a fixed style scale. (until we have a solution for dynamic style scaling, changing this requires resetting Style + calling this again)
+ style.FontScaleDpi = main_scale; // Set initial font scale. (using io.ConfigDpiScaleFonts=true makes this unnecessary. We leave both here for documentation purpose)
+ io.ConfigDpiScaleFonts = true; // [Experimental] Automatically overwrite style.FontScaleDpi in Begin() when Monitor DPI changes. This will scale fonts but _NOT_ scale sizes/padding for now.
+ io.ConfigDpiScaleViewports = true; // [Experimental] Scale Dear ImGui and Platform Windows when Monitor DPI changes.
+
+ // When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{
style.WindowRounding = 0.0f;
@@ -77,16 +84,16 @@ int main(int, char**)
// - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
// - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
// - If the file cannot be loaded, the function will return a nullptr. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
- // - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
// - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use Freetype for higher quality font rendering.
- // - Read 'docs/FONTS.md' for more instructions and details.
+ // - Read 'docs/FONTS.md' for more instructions and details. If you like the default font but want it to scale better, consider using the 'ProggyVector' from the same author!
// - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
+ //style.FontSizeBase = 20.0f;
//io.Fonts->AddFontDefault();
- //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf", 18.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
- //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, nullptr, io.Fonts->GetGlyphRangesJapanese());
+ //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf");
+ //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf");
//IM_ASSERT(font != nullptr);
// Our state
diff --git a/examples/example_glfw_opengl3/Makefile b/examples/example_glfw_opengl3/Makefile
index 252ce5714..4c2dc523f 100644
--- a/examples/example_glfw_opengl3/Makefile
+++ b/examples/example_glfw_opengl3/Makefile
@@ -41,7 +41,7 @@ LIBS =
ifeq ($(UNAME_S), Linux) #LINUX
ECHO_MESSAGE = "Linux"
- LIBS += $(LINUX_GL_LIBS) `pkg-config --static --libs glfw3`
+ LIBS += $(LINUX_GL_LIBS) -lX11 `pkg-config --static --libs glfw3`
CXXFLAGS += `pkg-config --cflags glfw3`
CFLAGS = $(CXXFLAGS)
diff --git a/examples/example_glfw_opengl3/Makefile.emscripten b/examples/example_glfw_opengl3/Makefile.emscripten
index bba4ac9dc..8d2f6e7bd 100644
--- a/examples/example_glfw_opengl3/Makefile.emscripten
+++ b/examples/example_glfw_opengl3/Makefile.emscripten
@@ -32,8 +32,9 @@ EMS =
##---------------------------------------------------------------------
# ("EMS" options gets added to both CPPFLAGS and LDFLAGS, whereas some options are for linker only)
-EMS += -s DISABLE_EXCEPTION_CATCHING=1
-LDFLAGS += -s USE_GLFW=3 -s WASM=1 -s ALLOW_MEMORY_GROWTH=1 -s NO_EXIT_RUNTIME=0 -s ASSERTIONS=1
+# Note: For glfw, we use emscripten-glfw port (contrib.glfw3) instead of ('-s USE_GLFW=3' in LDFLAGS) to get a better support for High DPI displays.
+EMS += -s DISABLE_EXCEPTION_CATCHING=1 --use-port=contrib.glfw3
+LDFLAGS += -s WASM=1 -s ALLOW_MEMORY_GROWTH=1 -s NO_EXIT_RUNTIME=0 -s ASSERTIONS=1
# Build as single file (binary text encoded in .html file)
#LDFLAGS += -sSINGLE_FILE
diff --git a/examples/example_glfw_opengl3/main.cpp b/examples/example_glfw_opengl3/main.cpp
index d02bb092e..e1b37d43a 100644
--- a/examples/example_glfw_opengl3/main.cpp
+++ b/examples/example_glfw_opengl3/main.cpp
@@ -71,7 +71,8 @@ int main(int, char**)
#endif
// Create window with graphics context
- GLFWwindow* window = glfwCreateWindow(1280, 720, "Dear ImGui GLFW+OpenGL3 example", nullptr, nullptr);
+ float main_scale = ImGui_ImplGlfw_GetContentScaleForMonitor(glfwGetPrimaryMonitor()); // Valid on GLFW 3.3+ only
+ GLFWwindow* window = glfwCreateWindow((int)(1280 * main_scale), (int)(800 * main_scale), "Dear ImGui GLFW+OpenGL3 example", nullptr, nullptr);
if (window == nullptr)
return 1;
glfwMakeContextCurrent(window);
@@ -92,8 +93,16 @@ int main(int, char**)
ImGui::StyleColorsDark();
//ImGui::StyleColorsLight();
- // When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
+ // Setup scaling
ImGuiStyle& style = ImGui::GetStyle();
+ style.ScaleAllSizes(main_scale); // Bake a fixed style scale. (until we have a solution for dynamic style scaling, changing this requires resetting Style + calling this again)
+ style.FontScaleDpi = main_scale; // Set initial font scale. (using io.ConfigDpiScaleFonts=true makes this unnecessary. We leave both here for documentation purpose)
+#if GLFW_VERSION_MAJOR >= 3 && GLFW_VERSION_MINOR >= 3
+ io.ConfigDpiScaleFonts = true; // [Experimental] Automatically overwrite style.FontScaleDpi in Begin() when Monitor DPI changes. This will scale fonts but _NOT_ scale sizes/padding for now.
+ io.ConfigDpiScaleViewports = true; // [Experimental] Scale Dear ImGui and Platform Windows when Monitor DPI changes.
+#endif
+
+ // When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{
style.WindowRounding = 0.0f;
@@ -111,17 +120,17 @@ int main(int, char**)
// - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
// - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
// - If the file cannot be loaded, the function will return a nullptr. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
- // - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
// - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use Freetype for higher quality font rendering.
- // - Read 'docs/FONTS.md' for more instructions and details.
+ // - Read 'docs/FONTS.md' for more instructions and details. If you like the default font but want it to scale better, consider using the 'ProggyVector' from the same author!
// - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
// - Our Emscripten build process allows embedding fonts to be accessible at runtime from the "fonts/" folder. See Makefile.emscripten for details.
+ //style.FontSizeBase = 20.0f;
//io.Fonts->AddFontDefault();
- //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf", 18.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
- //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, nullptr, io.Fonts->GetGlyphRangesJapanese());
+ //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf");
+ //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf");
//IM_ASSERT(font != nullptr);
// Our state
diff --git a/examples/example_glfw_vulkan/CMakeLists.txt b/examples/example_glfw_vulkan/CMakeLists.txt
index 75475dbae..fae580bfc 100644
--- a/examples/example_glfw_vulkan/CMakeLists.txt
+++ b/examples/example_glfw_vulkan/CMakeLists.txt
@@ -36,6 +36,7 @@ find_package(Vulkan REQUIRED)
#NAMES vulkan vulkan-1)
#set(LIBRARIES "glfw;${VULKAN_LIBRARY}")
set(LIBRARIES "glfw;Vulkan::Vulkan")
+# FIXME: Linux needs linking with "X11" as well?
# Use vulkan headers from glfw:
include_directories(${GLFW_DIR}/deps)
diff --git a/examples/example_glfw_vulkan/Makefile b/examples/example_glfw_vulkan/Makefile
new file mode 100644
index 000000000..f2a9ab518
--- /dev/null
+++ b/examples/example_glfw_vulkan/Makefile
@@ -0,0 +1,83 @@
+#
+# Cross Platform Makefile
+# Compatible with MSYS2/MINGW, Ubuntu 14.04.1 and Mac OS X
+#
+# You will need GLFW (http://www.glfw.org):
+# Linux:
+# apt-get install libglfw-dev
+# Mac OS X:
+# brew install glfw
+# MSYS2:
+# pacman -S --noconfirm --needed mingw-w64-x86_64-toolchain mingw-w64-x86_64-glfw
+#
+
+#CXX = g++
+#CXX = clang++
+
+EXE = example_glfw_vulkan
+IMGUI_DIR = ../..
+SOURCES = main.cpp
+SOURCES += $(IMGUI_DIR)/imgui.cpp $(IMGUI_DIR)/imgui_demo.cpp $(IMGUI_DIR)/imgui_draw.cpp $(IMGUI_DIR)/imgui_tables.cpp $(IMGUI_DIR)/imgui_widgets.cpp
+SOURCES += $(IMGUI_DIR)/backends/imgui_impl_glfw.cpp $(IMGUI_DIR)/backends/imgui_impl_vulkan.cpp
+OBJS = $(addsuffix .o, $(basename $(notdir $(SOURCES))))
+UNAME_S := $(shell uname -s)
+LINUX_GL_LIBS = -lGL
+
+CXXFLAGS = -std=c++11 -I$(IMGUI_DIR) -I$(IMGUI_DIR)/backends
+CXXFLAGS += -g -Wall -Wformat
+LIBS =
+
+##---------------------------------------------------------------------
+## BUILD FLAGS PER PLATFORM
+##---------------------------------------------------------------------
+
+ifeq ($(UNAME_S), Linux) #LINUX
+ ECHO_MESSAGE = "Linux"
+ LIBS += $(LINUX_GL_LIBS) -lX11 `pkg-config --static --libs glfw3 vulkan`
+
+ CXXFLAGS += `pkg-config --cflags glfw3 vulkan`
+ CFLAGS = $(CXXFLAGS)
+endif
+
+ifeq ($(UNAME_S), Darwin) #APPLE
+ ECHO_MESSAGE = "Mac OS X"
+ LIBS += -framework Cocoa -framework IOKit -framework CoreVideo
+ LIBS += `pkg-config --libs glfw3 vulkan`
+ LIBS += -L/usr/local/lib -L/opt/local/lib -L/opt/homebrew/lib
+ #LIBS += -lglfw3
+
+ LIBS += `pkg-config --cflags glfw3 vulkan`
+ CXXFLAGS += -I/usr/local/include -I/opt/local/include -I/opt/homebrew/include
+ CFLAGS = $(CXXFLAGS)
+endif
+
+ifeq ($(OS), Windows_NT)
+ ECHO_MESSAGE = "MinGW"
+ LIBS += -lgdi32 -limm32
+ LIBS += `pkg-config --libs glfw3 vulkan`
+
+ CXXFLAGS += `pkg-config --cflags glfw3 vulkan`
+ CFLAGS = $(CXXFLAGS)
+endif
+
+##---------------------------------------------------------------------
+## BUILD RULES
+##---------------------------------------------------------------------
+
+%.o:%.cpp
+ $(CXX) $(CXXFLAGS) -c -o $@ $<
+
+%.o:$(IMGUI_DIR)/%.cpp
+ $(CXX) $(CXXFLAGS) -c -o $@ $<
+
+%.o:$(IMGUI_DIR)/backends/%.cpp
+ $(CXX) $(CXXFLAGS) -c -o $@ $<
+
+all: $(EXE)
+ @echo Build complete for $(ECHO_MESSAGE)
+
+$(EXE): $(OBJS)
+ $(CXX) -o $@ $^ $(CXXFLAGS) $(LIBS)
+
+clean:
+ rm -f $(EXE) $(OBJS)
diff --git a/examples/example_glfw_vulkan/main.cpp b/examples/example_glfw_vulkan/main.cpp
index d6139676c..01e47fdad 100644
--- a/examples/example_glfw_vulkan/main.cpp
+++ b/examples/example_glfw_vulkan/main.cpp
@@ -38,6 +38,7 @@
//#define APP_USE_UNLIMITED_FRAME_RATE
#ifdef _DEBUG
#define APP_USE_VULKAN_DEBUG_REPORT
+static VkDebugReportCallbackEXT g_DebugReport = VK_NULL_HANDLE;
#endif
// Data
@@ -47,7 +48,6 @@ static VkPhysicalDevice g_PhysicalDevice = VK_NULL_HANDLE;
static VkDevice g_Device = VK_NULL_HANDLE;
static uint32_t g_QueueFamily = (uint32_t)-1;
static VkQueue g_Queue = VK_NULL_HANDLE;
-static VkDebugReportCallbackEXT g_DebugReport = VK_NULL_HANDLE;
static VkPipelineCache g_PipelineCache = VK_NULL_HANDLE;
static VkDescriptorPool g_DescriptorPool = VK_NULL_HANDLE;
@@ -239,7 +239,7 @@ static void SetupVulkanWindow(ImGui_ImplVulkanH_Window* wd, VkSurfaceKHR surface
// Create SwapChain, RenderPass, Framebuffer, etc.
IM_ASSERT(g_MinImageCount >= 2);
- ImGui_ImplVulkanH_CreateOrResizeWindow(g_Instance, g_PhysicalDevice, g_Device, wd, g_QueueFamily, g_Allocator, width, height, g_MinImageCount);
+ ImGui_ImplVulkanH_CreateOrResizeWindow(g_Instance, g_PhysicalDevice, g_Device, wd, g_QueueFamily, g_Allocator, width, height, g_MinImageCount, 0);
}
static void CleanupVulkan()
@@ -357,7 +357,8 @@ int main(int, char**)
// Create window with Vulkan context
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
- GLFWwindow* window = glfwCreateWindow(1280, 720, "Dear ImGui GLFW+Vulkan example", nullptr, nullptr);
+ float main_scale = ImGui_ImplGlfw_GetContentScaleForMonitor(glfwGetPrimaryMonitor()); // Valid on GLFW 3.3+ only
+ GLFWwindow* window = glfwCreateWindow((int)(1280 * main_scale), (int)(800 * main_scale), "Dear ImGui GLFW+Vulkan example", nullptr, nullptr);
if (!glfwVulkanSupported())
{
printf("GLFW: Vulkan Not Supported\n");
@@ -397,8 +398,14 @@ int main(int, char**)
ImGui::StyleColorsDark();
//ImGui::StyleColorsLight();
- // When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
+ // Setup scaling
ImGuiStyle& style = ImGui::GetStyle();
+ style.ScaleAllSizes(main_scale); // Bake a fixed style scale. (until we have a solution for dynamic style scaling, changing this requires resetting Style + calling this again)
+ style.FontScaleDpi = main_scale; // Set initial font scale. (using io.ConfigDpiScaleFonts=true makes this unnecessary. We leave both here for documentation purpose)
+ io.ConfigDpiScaleFonts = true; // [Experimental] Automatically overwrite style.FontScaleDpi in Begin() when Monitor DPI changes. This will scale fonts but _NOT_ scale sizes/padding for now.
+ io.ConfigDpiScaleViewports = true; // [Experimental] Scale Dear ImGui and Platform Windows when Monitor DPI changes.
+
+ // When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{
style.WindowRounding = 0.0f;
@@ -416,12 +423,12 @@ int main(int, char**)
init_info.Queue = g_Queue;
init_info.PipelineCache = g_PipelineCache;
init_info.DescriptorPool = g_DescriptorPool;
- init_info.RenderPass = wd->RenderPass;
- init_info.Subpass = 0;
init_info.MinImageCount = g_MinImageCount;
init_info.ImageCount = wd->ImageCount;
- init_info.MSAASamples = VK_SAMPLE_COUNT_1_BIT;
init_info.Allocator = g_Allocator;
+ init_info.PipelineInfoMain.RenderPass = wd->RenderPass;
+ init_info.PipelineInfoMain.Subpass = 0;
+ init_info.PipelineInfoMain.MSAASamples = VK_SAMPLE_COUNT_1_BIT;
init_info.CheckVkResultFn = check_vk_result;
ImGui_ImplVulkan_Init(&init_info);
@@ -429,16 +436,16 @@ int main(int, char**)
// - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
// - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
// - If the file cannot be loaded, the function will return a nullptr. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
- // - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
// - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use Freetype for higher quality font rendering.
- // - Read 'docs/FONTS.md' for more instructions and details.
+ // - Read 'docs/FONTS.md' for more instructions and details. If you like the default font but want it to scale better, consider using the 'ProggyVector' from the same author!
// - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
+ //style.FontSizeBase = 20.0f;
//io.Fonts->AddFontDefault();
- //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf", 18.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
- //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, nullptr, io.Fonts->GetGlyphRangesJapanese());
+ //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf");
+ //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf");
//IM_ASSERT(font != nullptr);
// Our state
@@ -462,7 +469,7 @@ int main(int, char**)
if (fb_width > 0 && fb_height > 0 && (g_SwapChainRebuild || g_MainWindowData.Width != fb_width || g_MainWindowData.Height != fb_height))
{
ImGui_ImplVulkan_SetMinImageCount(g_MinImageCount);
- ImGui_ImplVulkanH_CreateOrResizeWindow(g_Instance, g_PhysicalDevice, g_Device, &g_MainWindowData, g_QueueFamily, g_Allocator, fb_width, fb_height, g_MinImageCount);
+ ImGui_ImplVulkanH_CreateOrResizeWindow(g_Instance, g_PhysicalDevice, g_Device, wd, g_QueueFamily, g_Allocator, fb_width, fb_height, g_MinImageCount, 0);
g_MainWindowData.FrameIndex = 0;
g_SwapChainRebuild = false;
}
diff --git a/examples/example_glfw_wgpu/Makefile.emscripten b/examples/example_glfw_wgpu/Makefile.emscripten
index 78d64b4d3..8ee398bc8 100644
--- a/examples/example_glfw_wgpu/Makefile.emscripten
+++ b/examples/example_glfw_wgpu/Makefile.emscripten
@@ -32,8 +32,9 @@ EMS =
##---------------------------------------------------------------------
# ("EMS" options gets added to both CPPFLAGS and LDFLAGS, whereas some options are for linker only)
-EMS += -s DISABLE_EXCEPTION_CATCHING=1
-LDFLAGS += -s USE_GLFW=3 -s USE_WEBGPU=1
+# Note: For glfw, we use emscripten-glfw port (contrib.glfw3) instead of (-s USE_GLFW=3) to get a better support for High DPI displays.
+EMS += -s DISABLE_EXCEPTION_CATCHING=1 --use-port=contrib.glfw3
+LDFLAGS += -s USE_WEBGPU=1
LDFLAGS += -s WASM=1 -s ALLOW_MEMORY_GROWTH=1 -s NO_EXIT_RUNTIME=0 -s ASSERTIONS=1
# Build as single file (binary text encoded in .html file)
diff --git a/examples/example_glfw_wgpu/main.cpp b/examples/example_glfw_wgpu/main.cpp
index 723b69da4..5eec5009d 100644
--- a/examples/example_glfw_wgpu/main.cpp
+++ b/examples/example_glfw_wgpu/main.cpp
@@ -37,7 +37,7 @@ static WGPUSurface wgpu_surface = nullptr;
static WGPUTextureFormat wgpu_preferred_fmt = WGPUTextureFormat_RGBA8Unorm;
static WGPUSwapChain wgpu_swap_chain = nullptr;
static int wgpu_swap_chain_width = 1280;
-static int wgpu_swap_chain_height = 720;
+static int wgpu_swap_chain_height = 800;
// Forward declarations
static bool InitWGPU(GLFWwindow* window);
@@ -115,19 +115,19 @@ int main(int, char**)
// - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
// - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
// - If the file cannot be loaded, the function will return a nullptr. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
- // - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
// - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use Freetype for higher quality font rendering.
- // - Read 'docs/FONTS.md' for more instructions and details.
+ // - Read 'docs/FONTS.md' for more instructions and details. If you like the default font but want it to scale better, consider using the 'ProggyVector' from the same author!
// - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
// - Emscripten allows preloading a file or folder to be accessible at runtime. See Makefile for details.
//io.Fonts->AddFontDefault();
+ //style.FontSizeBase = 20.0f;
#ifndef IMGUI_DISABLE_FILE_FUNCTIONS
- //io.Fonts->AddFontFromFileTTF("fonts/segoeui.ttf", 18.0f);
- //io.Fonts->AddFontFromFileTTF("fonts/DroidSans.ttf", 16.0f);
- //io.Fonts->AddFontFromFileTTF("fonts/Roboto-Medium.ttf", 16.0f);
- //io.Fonts->AddFontFromFileTTF("fonts/Cousine-Regular.ttf", 15.0f);
- //io.Fonts->AddFontFromFileTTF("fonts/ProggyTiny.ttf", 10.0f);
- //ImFont* font = io.Fonts->AddFontFromFileTTF("fonts/ArialUni.ttf", 18.0f, nullptr, io.Fonts->GetGlyphRangesJapanese());
+ //io.Fonts->AddFontFromFileTTF("fonts/segoeui.ttf");
+ //io.Fonts->AddFontFromFileTTF("fonts/DroidSans.ttf");
+ //io.Fonts->AddFontFromFileTTF("fonts/Roboto-Medium.ttf");
+ //io.Fonts->AddFontFromFileTTF("fonts/Cousine-Regular.ttf");
+ //io.Fonts->AddFontFromFileTTF("fonts/ProggyTiny.ttf");
+ //ImFont* font = io.Fonts->AddFontFromFileTTF("fonts/ArialUni.ttf");
//IM_ASSERT(font != nullptr);
#endif
diff --git a/examples/example_glut_opengl2/main.cpp b/examples/example_glut_opengl2/main.cpp
index 4edae6f19..ad7f24830 100644
--- a/examples/example_glut_opengl2/main.cpp
+++ b/examples/example_glut_opengl2/main.cpp
@@ -48,7 +48,7 @@ int main(int argc, char** argv)
glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_GLUTMAINLOOP_RETURNS);
#endif
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_MULTISAMPLE);
- glutInitWindowSize(1280, 720);
+ glutInitWindowSize(1280, 800);
glutCreateWindow("Dear ImGui GLUT+OpenGL2 Example");
// Setup GLUT display function
@@ -83,16 +83,16 @@ int main(int argc, char** argv)
// - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
// - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
// - If the file cannot be loaded, the function will return a nullptr. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
- // - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
// - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use Freetype for higher quality font rendering.
- // - Read 'docs/FONTS.md' for more instructions and details.
+ // - Read 'docs/FONTS.md' for more instructions and details. If you like the default font but want it to scale better, consider using the 'ProggyVector' from the same author!
// - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
+ //style.FontSizeBase = 20.0f;
//io.Fonts->AddFontDefault();
- //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf", 18.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
- //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, nullptr, io.Fonts->GetGlyphRangesJapanese());
+ //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf");
+ //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf");
//IM_ASSERT(font != nullptr);
// Main loop
diff --git a/examples/example_null/main.cpp b/examples/example_null/main.cpp
index f7153cc48..460f33cab 100644
--- a/examples/example_null/main.cpp
+++ b/examples/example_null/main.cpp
@@ -11,9 +11,10 @@ int main(int, char**)
ImGuiIO& io = ImGui::GetIO();
// Build atlas
- unsigned char* tex_pixels = nullptr;
- int tex_w, tex_h;
- io.Fonts->GetTexDataAsRGBA32(&tex_pixels, &tex_w, &tex_h);
+ //unsigned char* tex_pixels = nullptr;
+ //int tex_w, tex_h;
+ //io.Fonts->GetTexDataAsRGBA32(&tex_pixels, &tex_w, &tex_h);
+ io.BackendFlags |= ImGuiBackendFlags_RendererHasTextures;
for (int n = 0; n < 20; n++)
{
diff --git a/examples/example_sdl2_directx11/main.cpp b/examples/example_sdl2_directx11/main.cpp
index 7b7de21c6..bbabf55c0 100644
--- a/examples/example_sdl2_directx11/main.cpp
+++ b/examples/example_sdl2_directx11/main.cpp
@@ -33,10 +33,13 @@ int main(int, char**)
// Setup SDL
// (Some versions of SDL before <2.0.10 appears to have performance/stalling issues on a minority of Windows systems,
// depending on whether SDL_INIT_GAMECONTROLLER is enabled or disabled.. updating to the latest version of SDL is recommended!)
+#ifdef _WIN32
+ ::SetProcessDPIAware();
+#endif
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0)
{
printf("Error: %s\n", SDL_GetError());
- return -1;
+ return 1;
}
// From 2.0.18: Enable native IME.
@@ -45,12 +48,13 @@ int main(int, char**)
#endif
// Setup window
+ float main_scale = ImGui_ImplSDL2_GetContentScaleForDisplay(0);
SDL_WindowFlags window_flags = (SDL_WindowFlags)(SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
- SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL2+DirectX11 example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, window_flags);
+ SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL2+DirectX11 example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, (int)(1280 * main_scale), (int)(800 * main_scale), window_flags);
if (window == nullptr)
{
printf("Error: SDL_CreateWindow(): %s\n", SDL_GetError());
- return -1;
+ return 1;
}
SDL_SysWMinfo wmInfo;
@@ -80,8 +84,14 @@ int main(int, char**)
ImGui::StyleColorsDark();
//ImGui::StyleColorsLight();
- // When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
+ // Setup scaling
ImGuiStyle& style = ImGui::GetStyle();
+ style.ScaleAllSizes(main_scale); // Bake a fixed style scale. (until we have a solution for dynamic style scaling, changing this requires resetting Style + calling this again)
+ style.FontScaleDpi = main_scale; // Set initial font scale. (using io.ConfigDpiScaleFonts=true makes this unnecessary. We leave both here for documentation purpose)
+ io.ConfigDpiScaleFonts = true; // [Experimental] Automatically overwrite style.FontScaleDpi in Begin() when Monitor DPI changes. This will scale fonts but _NOT_ scale sizes/padding for now.
+ io.ConfigDpiScaleViewports = true; // [Experimental] Scale Dear ImGui and Platform Windows when Monitor DPI changes.
+
+ // When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{
style.WindowRounding = 0.0f;
@@ -96,16 +106,16 @@ int main(int, char**)
// - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
// - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
// - If the file cannot be loaded, the function will return a nullptr. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
- // - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
// - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use Freetype for higher quality font rendering.
- // - Read 'docs/FONTS.md' for more instructions and details.
+ // - Read 'docs/FONTS.md' for more instructions and details. If you like the default font but want it to scale better, consider using the 'ProggyVector' from the same author!
// - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
+ //style.FontSizeBase = 20.0f;
//io.Fonts->AddFontDefault();
- //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf", 18.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
- //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, nullptr, io.Fonts->GetGlyphRangesJapanese());
+ //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf");
+ //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf");
//IM_ASSERT(font != nullptr);
// Our state
@@ -240,9 +250,19 @@ bool CreateDeviceD3D(HWND hWnd)
//createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
D3D_FEATURE_LEVEL featureLevel;
const D3D_FEATURE_LEVEL featureLevelArray[2] = { D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_0, };
- if (D3D11CreateDeviceAndSwapChain(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, createDeviceFlags, featureLevelArray, 2, D3D11_SDK_VERSION, &sd, &g_pSwapChain, &g_pd3dDevice, &featureLevel, &g_pd3dDeviceContext) != S_OK)
+ HRESULT res = D3D11CreateDeviceAndSwapChain(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, createDeviceFlags, featureLevelArray, 2, D3D11_SDK_VERSION, &sd, &g_pSwapChain, &g_pd3dDevice, &featureLevel, &g_pd3dDeviceContext);
+ if (res == DXGI_ERROR_UNSUPPORTED) // Try high-performance WARP software driver if hardware is not available.
+ res = D3D11CreateDeviceAndSwapChain(nullptr, D3D_DRIVER_TYPE_WARP, nullptr, createDeviceFlags, featureLevelArray, 2, D3D11_SDK_VERSION, &sd, &g_pSwapChain, &g_pd3dDevice, &featureLevel, &g_pd3dDeviceContext);
+ if (res != S_OK)
return false;
+ // Disable DXGI's default Alt+Enter fullscreen behavior.
+ // - You are free to leave this enabled, but it will not work properly with multiple viewports.
+ // - This must be done for all windows associated to the device. Our DX11 backend does this automatically for secondary viewports that it creates.
+ IDXGIFactory* pSwapChainFactory;
+ if (SUCCEEDED(g_pSwapChain->GetParent(IID_PPV_ARGS(&pSwapChainFactory))))
+ pSwapChainFactory->MakeWindowAssociation(hWnd, DXGI_MWA_NO_ALT_ENTER);
+
CreateRenderTarget();
return true;
}
diff --git a/examples/example_sdl2_metal/main.mm b/examples/example_sdl2_metal/main.mm
index 5cd704382..08f8a23a6 100644
--- a/examples/example_sdl2_metal/main.mm
+++ b/examples/example_sdl2_metal/main.mm
@@ -43,16 +43,16 @@ int main(int, char**)
// - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
// - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
// - If the file cannot be loaded, the function will return a nullptr. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
- // - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
// - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use Freetype for higher quality font rendering.
- // - Read 'docs/FONTS.md' for more instructions and details.
+ // - Read 'docs/FONTS.md' for more instructions and details. If you like the default font but want it to scale better, consider using the 'ProggyVector' from the same author!
// - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
+ //style.FontSizeBase = 20.0f;
//io.Fonts->AddFontDefault();
- //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf", 18.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
- //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, nullptr, io.Fonts->GetGlyphRangesJapanese());
+ //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf");
+ //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf");
//IM_ASSERT(font != nullptr);
// Setup SDL
@@ -61,7 +61,7 @@ int main(int, char**)
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0)
{
printf("Error: %s\n", SDL_GetError());
- return -1;
+ return 1;
}
// Inform SDL that we will be using metal for rendering. Without this hint initialization of metal renderer may fail.
@@ -70,7 +70,7 @@ int main(int, char**)
// Enable native IME.
SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1");
- SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL+Metal example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
+ SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL+Metal example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 800, SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
if (window == nullptr)
{
printf("Error creating window: %s\n", SDL_GetError());
diff --git a/examples/example_sdl2_opengl2/main.cpp b/examples/example_sdl2_opengl2/main.cpp
index 4c537c376..0e77fa7cf 100644
--- a/examples/example_sdl2_opengl2/main.cpp
+++ b/examples/example_sdl2_opengl2/main.cpp
@@ -17,15 +17,21 @@
#include
#include
#include
+#ifdef _WIN32
+#include // SetProcessDPIAware()
+#endif
// Main code
int main(int, char**)
{
// Setup SDL
+#ifdef _WIN32
+ ::SetProcessDPIAware();
+#endif
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0)
{
printf("Error: %s\n", SDL_GetError());
- return -1;
+ return 1;
}
// From 2.0.18: Enable native IME.
@@ -39,12 +45,13 @@ int main(int, char**)
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
+ float main_scale = ImGui_ImplSDL2_GetContentScaleForDisplay(0);
SDL_WindowFlags window_flags = (SDL_WindowFlags)(SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
- SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL2+OpenGL example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, window_flags);
+ SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL2+OpenGL example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, (int)(1280 * main_scale), (int)(800 * main_scale), window_flags);
if (window == nullptr)
{
printf("Error: SDL_CreateWindow(): %s\n", SDL_GetError());
- return -1;
+ return 1;
}
SDL_GLContext gl_context = SDL_GL_CreateContext(window);
@@ -66,8 +73,14 @@ int main(int, char**)
ImGui::StyleColorsDark();
//ImGui::StyleColorsLight();
- // When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
+ // Setup scaling
ImGuiStyle& style = ImGui::GetStyle();
+ style.ScaleAllSizes(main_scale); // Bake a fixed style scale. (until we have a solution for dynamic style scaling, changing this requires resetting Style + calling this again)
+ style.FontScaleDpi = main_scale; // Set initial font scale. (using io.ConfigDpiScaleFonts=true makes this unnecessary. We leave both here for documentation purpose)
+ io.ConfigDpiScaleFonts = true; // [Experimental] Automatically overwrite style.FontScaleDpi in Begin() when Monitor DPI changes. This will scale fonts but _NOT_ scale sizes/padding for now.
+ io.ConfigDpiScaleViewports = true; // [Experimental] Scale Dear ImGui and Platform Windows when Monitor DPI changes.
+
+ // When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{
style.WindowRounding = 0.0f;
@@ -82,16 +95,16 @@ int main(int, char**)
// - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
// - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
// - If the file cannot be loaded, the function will return a nullptr. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
- // - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
// - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use Freetype for higher quality font rendering.
- // - Read 'docs/FONTS.md' for more instructions and details.
+ // - Read 'docs/FONTS.md' for more instructions and details. If you like the default font but want it to scale better, consider using the 'ProggyVector' from the same author!
// - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
+ //style.FontSizeBase = 20.0f;
//io.Fonts->AddFontDefault();
- //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf", 18.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
- //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, nullptr, io.Fonts->GetGlyphRangesJapanese());
+ //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf");
+ //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf");
//IM_ASSERT(font != nullptr);
// Our state
diff --git a/examples/example_sdl2_opengl3/main.cpp b/examples/example_sdl2_opengl3/main.cpp
index 4fc75c33b..f80ff96e8 100644
--- a/examples/example_sdl2_opengl3/main.cpp
+++ b/examples/example_sdl2_opengl3/main.cpp
@@ -17,6 +17,9 @@
#else
#include
#endif
+#ifdef _WIN32
+#include // SetProcessDPIAware()
+#endif
// This example can also compile and run with Emscripten! See 'Makefile.emscripten' for details.
#ifdef __EMSCRIPTEN__
@@ -27,10 +30,13 @@
int main(int, char**)
{
// Setup SDL
+#ifdef _WIN32
+ ::SetProcessDPIAware();
+#endif
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0)
{
printf("Error: %s\n", SDL_GetError());
- return -1;
+ return 1;
}
// Decide GL+GLSL versions
@@ -73,19 +79,20 @@ int main(int, char**)
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
+ float main_scale = ImGui_ImplSDL2_GetContentScaleForDisplay(0);
SDL_WindowFlags window_flags = (SDL_WindowFlags)(SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
- SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL2+OpenGL3 example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, window_flags);
+ SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL2+OpenGL3 example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, (int)(1280 * main_scale), (int)(800 * main_scale), window_flags);
if (window == nullptr)
{
printf("Error: SDL_CreateWindow(): %s\n", SDL_GetError());
- return -1;
+ return 1;
}
SDL_GLContext gl_context = SDL_GL_CreateContext(window);
if (gl_context == nullptr)
{
printf("Error: SDL_GL_CreateContext(): %s\n", SDL_GetError());
- return -1;
+ return 1;
}
SDL_GL_MakeCurrent(window, gl_context);
@@ -106,8 +113,14 @@ int main(int, char**)
ImGui::StyleColorsDark();
//ImGui::StyleColorsLight();
- // When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
+ // Setup scaling
ImGuiStyle& style = ImGui::GetStyle();
+ style.ScaleAllSizes(main_scale); // Bake a fixed style scale. (until we have a solution for dynamic style scaling, changing this requires resetting Style + calling this again)
+ style.FontScaleDpi = main_scale; // Set initial font scale. (using io.ConfigDpiScaleFonts=true makes this unnecessary. We leave both here for documentation purpose)
+ io.ConfigDpiScaleFonts = true; // [Experimental] Automatically overwrite style.FontScaleDpi in Begin() when Monitor DPI changes. This will scale fonts but _NOT_ scale sizes/padding for now.
+ io.ConfigDpiScaleViewports = true; // [Experimental] Scale Dear ImGui and Platform Windows when Monitor DPI changes.
+
+ // When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{
style.WindowRounding = 0.0f;
@@ -122,17 +135,17 @@ int main(int, char**)
// - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
// - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
// - If the file cannot be loaded, the function will return a nullptr. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
- // - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
// - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use Freetype for higher quality font rendering.
- // - Read 'docs/FONTS.md' for more instructions and details.
+ // - Read 'docs/FONTS.md' for more instructions and details. If you like the default font but want it to scale better, consider using the 'ProggyVector' from the same author!
// - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
// - Our Emscripten build process allows embedding fonts to be accessible at runtime from the "fonts/" folder. See Makefile.emscripten for details.
+ //style.FontSizeBase = 20.0f;
//io.Fonts->AddFontDefault();
- //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf", 18.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
- //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, nullptr, io.Fonts->GetGlyphRangesJapanese());
+ //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf");
+ //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf");
//IM_ASSERT(font != nullptr);
// Our state
diff --git a/examples/example_sdl2_sdlrenderer2/main.cpp b/examples/example_sdl2_sdlrenderer2/main.cpp
index e922873ad..da8596286 100644
--- a/examples/example_sdl2_sdlrenderer2/main.cpp
+++ b/examples/example_sdl2_sdlrenderer2/main.cpp
@@ -15,6 +15,9 @@
#include "imgui_impl_sdlrenderer2.h"
#include
#include
+#ifdef _WIN32
+#include // SetProcessDPIAware()
+#endif
#if !SDL_VERSION_ATLEAST(2,0,17)
#error This backend requires SDL 2.0.17+ because of SDL_RenderGeometry() function
@@ -24,10 +27,13 @@
int main(int, char**)
{
// Setup SDL
+#ifdef _WIN32
+ ::SetProcessDPIAware();
+#endif
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0)
{
printf("Error: %s\n", SDL_GetError());
- return -1;
+ return 1;
}
// From 2.0.18: Enable native IME.
@@ -36,18 +42,19 @@ int main(int, char**)
#endif
// Create window with SDL_Renderer graphics context
+ float main_scale = ImGui_ImplSDL2_GetContentScaleForDisplay(0);
SDL_WindowFlags window_flags = (SDL_WindowFlags)(SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
- SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL2+SDL_Renderer example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, window_flags);
+ SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL2+SDL_Renderer example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, (int)(1280 * main_scale), (int)(800 * main_scale), window_flags);
if (window == nullptr)
{
printf("Error: SDL_CreateWindow(): %s\n", SDL_GetError());
- return -1;
+ return 1;
}
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED);
if (renderer == nullptr)
{
SDL_Log("Error creating SDL_Renderer!");
- return -1;
+ return 1;
}
//SDL_RendererInfo info;
//SDL_GetRendererInfo(renderer, &info);
@@ -65,6 +72,13 @@ int main(int, char**)
ImGui::StyleColorsDark();
//ImGui::StyleColorsLight();
+ // Setup scaling
+ ImGuiStyle& style = ImGui::GetStyle();
+ style.ScaleAllSizes(main_scale); // Bake a fixed style scale. (until we have a solution for dynamic style scaling, changing this requires resetting Style + calling this again)
+ style.FontScaleDpi = main_scale; // Set initial font scale. (using io.ConfigDpiScaleFonts=true makes this unnecessary. We leave both here for documentation purpose)
+ io.ConfigDpiScaleFonts = true; // [Experimental] Automatically overwrite style.FontScaleDpi in Begin() when Monitor DPI changes. This will scale fonts but _NOT_ scale sizes/padding for now.
+ io.ConfigDpiScaleViewports = true; // [Experimental] Scale Dear ImGui and Platform Windows when Monitor DPI changes.
+
// Setup Platform/Renderer backends
ImGui_ImplSDL2_InitForSDLRenderer(window, renderer);
ImGui_ImplSDLRenderer2_Init(renderer);
@@ -73,16 +87,16 @@ int main(int, char**)
// - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
// - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
// - If the file cannot be loaded, the function will return a nullptr. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
- // - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
// - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use Freetype for higher quality font rendering.
- // - Read 'docs/FONTS.md' for more instructions and details.
+ // - Read 'docs/FONTS.md' for more instructions and details. If you like the default font but want it to scale better, consider using the 'ProggyVector' from the same author!
// - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
+ //style.FontSizeBase = 20.0f;
//io.Fonts->AddFontDefault();
- //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf", 18.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
- //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, nullptr, io.Fonts->GetGlyphRangesJapanese());
+ //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf");
+ //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf");
//IM_ASSERT(font != nullptr);
// Our state
diff --git a/examples/example_sdl2_vulkan/Makefile b/examples/example_sdl2_vulkan/Makefile
new file mode 100644
index 000000000..e722ab035
--- /dev/null
+++ b/examples/example_sdl2_vulkan/Makefile
@@ -0,0 +1,80 @@
+#
+# Cross Platform Makefile
+# Compatible with MSYS2/MINGW, Ubuntu 14.04.1 and Mac OS X
+#
+# You will need SDL2 (http://www.libsdl.org):
+# Linux:
+# apt-get install libsdl2-dev
+# Mac OS X:
+# brew install sdl2
+# MSYS2:
+# pacman -S mingw-w64-i686-SDL2
+#
+
+#CXX = g++
+#CXX = clang++
+
+EXE = example_sdl2_vulkan
+IMGUI_DIR = ../..
+SOURCES = main.cpp
+SOURCES += $(IMGUI_DIR)/imgui.cpp $(IMGUI_DIR)/imgui_demo.cpp $(IMGUI_DIR)/imgui_draw.cpp $(IMGUI_DIR)/imgui_tables.cpp $(IMGUI_DIR)/imgui_widgets.cpp
+SOURCES += $(IMGUI_DIR)/backends/imgui_impl_sdl2.cpp $(IMGUI_DIR)/backends/imgui_impl_vulkan.cpp
+OBJS = $(addsuffix .o, $(basename $(notdir $(SOURCES))))
+UNAME_S := $(shell uname -s)
+
+CXXFLAGS = -std=c++11 -I$(IMGUI_DIR) -I$(IMGUI_DIR)/backends
+CXXFLAGS += -g -Wall -Wformat
+LIBS =
+
+##---------------------------------------------------------------------
+## BUILD FLAGS PER PLATFORM
+##---------------------------------------------------------------------
+
+ifeq ($(UNAME_S), Linux) #LINUX
+ ECHO_MESSAGE = "Linux"
+ LIBS += -lGL -ldl
+ LIBS += `pkg-config --libs sdl2 vulkan`
+ CXXFLAGS += `pkg-config --cflags sdl2 vulkan`
+ CFLAGS = $(CXXFLAGS)
+endif
+
+ifeq ($(UNAME_S), Darwin) #APPLE
+ ECHO_MESSAGE = "Mac OS X"
+ LIBS += -framework Cocoa -framework IOKit -framework CoreVideo
+ LIBS += `pkg-config --libs sdl2 vulkan`
+ LIBS += -L/usr/local/lib -L/opt/local/lib
+
+ CXXFLAGS += `pkg-config --cflags sdl2 vulkan`
+ CXXFLAGS += -I/usr/local/include -I/opt/local/include
+ CFLAGS = $(CXXFLAGS)
+endif
+
+ifeq ($(OS), Windows_NT)
+ ECHO_MESSAGE = "MinGW"
+ LIBS += -lgdi32 -lopengl32 -limm32 `pkg-config --static --libs sdl2 vulkan`
+
+ CXXFLAGS += `pkg-config --cflags sdl2 vulkan`
+ CFLAGS = $(CXXFLAGS)
+endif
+
+##---------------------------------------------------------------------
+## BUILD RULES
+##---------------------------------------------------------------------
+
+%.o:%.cpp
+ $(CXX) $(CXXFLAGS) -c -o $@ $<
+
+%.o:$(IMGUI_DIR)/%.cpp
+ $(CXX) $(CXXFLAGS) -c -o $@ $<
+
+%.o:$(IMGUI_DIR)/backends/%.cpp
+ $(CXX) $(CXXFLAGS) -c -o $@ $<
+
+all: $(EXE)
+ @echo Build complete for $(ECHO_MESSAGE)
+
+$(EXE): $(OBJS)
+ $(CXX) -o $@ $^ $(CXXFLAGS) $(LIBS)
+
+clean:
+ rm -f $(EXE) $(OBJS)
diff --git a/examples/example_sdl2_vulkan/main.cpp b/examples/example_sdl2_vulkan/main.cpp
index 99cc8121e..52ebe6dd9 100644
--- a/examples/example_sdl2_vulkan/main.cpp
+++ b/examples/example_sdl2_vulkan/main.cpp
@@ -20,6 +20,9 @@
#include // abort
#include
#include
+#ifdef _WIN32
+#include // SetProcessDPIAware()
+#endif
// Volk headers
#ifdef IMGUI_IMPL_VULKAN_USE_VOLK
@@ -30,6 +33,7 @@
//#define APP_USE_UNLIMITED_FRAME_RATE
#ifdef _DEBUG
#define APP_USE_VULKAN_DEBUG_REPORT
+static VkDebugReportCallbackEXT g_DebugReport = VK_NULL_HANDLE;
#endif
// Data
@@ -39,7 +43,6 @@ static VkPhysicalDevice g_PhysicalDevice = VK_NULL_HANDLE;
static VkDevice g_Device = VK_NULL_HANDLE;
static uint32_t g_QueueFamily = (uint32_t)-1;
static VkQueue g_Queue = VK_NULL_HANDLE;
-static VkDebugReportCallbackEXT g_DebugReport = VK_NULL_HANDLE;
static VkPipelineCache g_PipelineCache = VK_NULL_HANDLE;
static VkDescriptorPool g_DescriptorPool = VK_NULL_HANDLE;
@@ -227,7 +230,7 @@ static void SetupVulkanWindow(ImGui_ImplVulkanH_Window* wd, VkSurfaceKHR surface
// Create SwapChain, RenderPass, Framebuffer, etc.
IM_ASSERT(g_MinImageCount >= 2);
- ImGui_ImplVulkanH_CreateOrResizeWindow(g_Instance, g_PhysicalDevice, g_Device, wd, g_QueueFamily, g_Allocator, width, height, g_MinImageCount);
+ ImGui_ImplVulkanH_CreateOrResizeWindow(g_Instance, g_PhysicalDevice, g_Device, wd, g_QueueFamily, g_Allocator, width, height, g_MinImageCount, 0);
}
static void CleanupVulkan()
@@ -340,10 +343,13 @@ static void FramePresent(ImGui_ImplVulkanH_Window* wd)
int main(int, char**)
{
// Setup SDL
+#ifdef _WIN32
+ ::SetProcessDPIAware();
+#endif
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0)
{
printf("Error: %s\n", SDL_GetError());
- return -1;
+ return 1;
}
// From 2.0.18: Enable native IME.
@@ -352,12 +358,13 @@ int main(int, char**)
#endif
// Create window with Vulkan graphics context
+ float main_scale = ImGui_ImplSDL2_GetContentScaleForDisplay(0);
SDL_WindowFlags window_flags = (SDL_WindowFlags)(SDL_WINDOW_VULKAN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
- SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL2+Vulkan example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, window_flags);
+ SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL2+Vulkan example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, (int)(1280 * main_scale), (int)(800 * main_scale), window_flags);
if (window == nullptr)
{
printf("Error: SDL_CreateWindow(): %s\n", SDL_GetError());
- return -1;
+ return 1;
}
ImVector extensions;
@@ -397,8 +404,14 @@ int main(int, char**)
ImGui::StyleColorsDark();
//ImGui::StyleColorsLight();
- // When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
+ // Setup scaling
ImGuiStyle& style = ImGui::GetStyle();
+ style.ScaleAllSizes(main_scale); // Bake a fixed style scale. (until we have a solution for dynamic style scaling, changing this requires resetting Style + calling this again)
+ style.FontScaleDpi = main_scale; // Set initial font scale. (using io.ConfigDpiScaleFonts=true makes this unnecessary. We leave both here for documentation purpose)
+ io.ConfigDpiScaleFonts = true; // [Experimental] Automatically overwrite style.FontScaleDpi in Begin() when Monitor DPI changes. This will scale fonts but _NOT_ scale sizes/padding for now.
+ io.ConfigDpiScaleViewports = true; // [Experimental] Scale Dear ImGui and Platform Windows when Monitor DPI changes.
+
+ // When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{
style.WindowRounding = 0.0f;
@@ -416,12 +429,12 @@ int main(int, char**)
init_info.Queue = g_Queue;
init_info.PipelineCache = g_PipelineCache;
init_info.DescriptorPool = g_DescriptorPool;
- init_info.RenderPass = wd->RenderPass;
- init_info.Subpass = 0;
init_info.MinImageCount = g_MinImageCount;
init_info.ImageCount = wd->ImageCount;
- init_info.MSAASamples = VK_SAMPLE_COUNT_1_BIT;
init_info.Allocator = g_Allocator;
+ init_info.PipelineInfoMain.RenderPass = wd->RenderPass;
+ init_info.PipelineInfoMain.Subpass = 0;
+ init_info.PipelineInfoMain.MSAASamples = VK_SAMPLE_COUNT_1_BIT;
init_info.CheckVkResultFn = check_vk_result;
ImGui_ImplVulkan_Init(&init_info);
@@ -429,16 +442,16 @@ int main(int, char**)
// - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
// - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
// - If the file cannot be loaded, the function will return a nullptr. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
- // - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
// - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use Freetype for higher quality font rendering.
- // - Read 'docs/FONTS.md' for more instructions and details.
+ // - Read 'docs/FONTS.md' for more instructions and details. If you like the default font but want it to scale better, consider using the 'ProggyVector' from the same author!
// - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
+ //style.FontSizeBase = 20.0f;
//io.Fonts->AddFontDefault();
- //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf", 18.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
- //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, nullptr, io.Fonts->GetGlyphRangesJapanese());
+ //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf");
+ //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf");
//IM_ASSERT(font != nullptr);
// Our state
@@ -476,7 +489,7 @@ int main(int, char**)
if (fb_width > 0 && fb_height > 0 && (g_SwapChainRebuild || g_MainWindowData.Width != fb_width || g_MainWindowData.Height != fb_height))
{
ImGui_ImplVulkan_SetMinImageCount(g_MinImageCount);
- ImGui_ImplVulkanH_CreateOrResizeWindow(g_Instance, g_PhysicalDevice, g_Device, &g_MainWindowData, g_QueueFamily, g_Allocator, fb_width, fb_height, g_MinImageCount);
+ ImGui_ImplVulkanH_CreateOrResizeWindow(g_Instance, g_PhysicalDevice, g_Device, wd, g_QueueFamily, g_Allocator, fb_width, fb_height, g_MinImageCount, 0);
g_MainWindowData.FrameIndex = 0;
g_SwapChainRebuild = false;
}
diff --git a/examples/example_sdl3_directx11/build_win32.bat b/examples/example_sdl3_directx11/build_win32.bat
new file mode 100644
index 000000000..b84b01f1a
--- /dev/null
+++ b/examples/example_sdl3_directx11/build_win32.bat
@@ -0,0 +1,8 @@
+@REM Build for Visual Studio compiler. Run your copy of vcvars32.bat or vcvarsall.bat to setup command-line compiler.
+@set OUT_DIR=Debug
+@set OUT_EXE=example_sdl3_directx11
+@set INCLUDES=/I..\.. /I..\..\backends /I%SDL3_DIR%\include /I "%WindowsSdkDir%Include\um" /I "%WindowsSdkDir%Include\shared" /I "%DXSDK_DIR%Include"
+@set SOURCES=main.cpp ..\..\backends\imgui_impl_sdl3.cpp ..\..\backends\imgui_impl_dx11.cpp ..\..\imgui*.cpp
+@set LIBS=/LIBPATH:%SDL3_DIR%\lib\x86 SDL3.lib /LIBPATH:"%DXSDK_DIR%/Lib/x86" d3d11.lib d3dcompiler.lib shell32.lib
+mkdir %OUT_DIR%
+cl /nologo /Zi /MD /utf-8 %INCLUDES% %SOURCES% /Fe%OUT_DIR%/%OUT_EXE%.exe /Fo%OUT_DIR%/ /link %LIBS% /subsystem:console
diff --git a/examples/example_sdl3_directx11/example_sdl3_directx11.vcxproj b/examples/example_sdl3_directx11/example_sdl3_directx11.vcxproj
new file mode 100644
index 000000000..340901c3d
--- /dev/null
+++ b/examples/example_sdl3_directx11/example_sdl3_directx11.vcxproj
@@ -0,0 +1,187 @@
+
+
+
+
+ Debug
+ Win32
+
+
+ Debug
+ x64
+
+
+ Release
+ Win32
+
+
+ Release
+ x64
+
+
+
+ {009DAC16-1A9C-47BE-9770-A30A046E8090}
+ example_sdl3_directx11
+ 8.1
+ example_sdl3_directx11
+
+
+
+ Application
+ true
+ MultiByte
+ v140
+
+
+ Application
+ true
+ MultiByte
+ v140
+
+
+ Application
+ false
+ true
+ MultiByte
+ v140
+
+
+ Application
+ false
+ true
+ MultiByte
+ v140
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ $(ProjectDir)$(Configuration)\
+ $(ProjectDir)$(Configuration)\
+ $(IncludePath)
+
+
+ $(ProjectDir)$(Configuration)\
+ $(ProjectDir)$(Configuration)\
+ $(IncludePath)
+
+
+ $(ProjectDir)$(Configuration)\
+ $(ProjectDir)$(Configuration)\
+ $(IncludePath)
+
+
+ $(ProjectDir)$(Configuration)\
+ $(ProjectDir)$(Configuration)\
+ $(IncludePath)
+
+
+
+ Level4
+ Disabled
+ ..\..;..\..\backends;%SDL3_DIR%\include;$(VcpkgCurrentInstalledDir)include;%(AdditionalIncludeDirectories)
+ /utf-8 %(AdditionalOptions)
+
+
+ true
+ %SDL3_DIR%\lib\x86;$(DXSDK_DIR)/Lib/x86;%(AdditionalLibraryDirectories)
+ SDL3.lib;d3d11.lib;d3dcompiler.lib;dxgi.lib;%(AdditionalDependencies)
+ Console
+ msvcrt.lib
+
+
+
+
+ Level4
+ Disabled
+ ..\..;..\..\backends;%SDL3_DIR%\include;$(VcpkgCurrentInstalledDir)include;%(AdditionalIncludeDirectories)
+ /utf-8 %(AdditionalOptions)
+
+
+ true
+ %SDL3_DIR%\lib\x64;$(DXSDK_DIR)/Lib/x64;%(AdditionalLibraryDirectories)
+ SDL3.lib;d3d11.lib;d3dcompiler.lib;dxgi.lib;%(AdditionalDependencies)
+ Console
+ msvcrt.lib
+
+
+
+
+ Level4
+ MaxSpeed
+ true
+ true
+ ..\..;..\..\backends;%SDL3_DIR%\include;$(VcpkgCurrentInstalledDir)include;%(AdditionalIncludeDirectories)
+ false
+ /utf-8 %(AdditionalOptions)
+
+
+ true
+ true
+ true
+ %SDL3_DIR%\lib\x86;$(DXSDK_DIR)/Lib/x86;%(AdditionalLibraryDirectories)
+ SDL3.lib;d3d11.lib;d3dcompiler.lib;dxgi.lib;%(AdditionalDependencies)
+ Console
+
+
+
+
+
+
+ Level4
+ MaxSpeed
+ true
+ true
+ ..\..;..\..\backends;%SDL3_DIR%\include;$(VcpkgCurrentInstalledDir)include;%(AdditionalIncludeDirectories)
+ false
+ /utf-8 %(AdditionalOptions)
+
+
+ true
+ true
+ true
+ %SDL3_DIR%\lib\x64;$(DXSDK_DIR)/Lib/x64;%(AdditionalLibraryDirectories)
+ SDL3.lib;d3d11.lib;d3dcompiler.lib;dxgi.lib;%(AdditionalDependencies)
+ Console
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/examples/example_sdl3_directx11/example_sdl3_directx11.vcxproj.filters b/examples/example_sdl3_directx11/example_sdl3_directx11.vcxproj.filters
new file mode 100644
index 000000000..0579b7a64
--- /dev/null
+++ b/examples/example_sdl3_directx11/example_sdl3_directx11.vcxproj.filters
@@ -0,0 +1,63 @@
+
+
+
+
+ {0587d7a3-f2ce-4d56-b84f-a0005d3bfce6}
+
+
+ {08e36723-ce4f-4cff-9662-c40801cf1acf}
+
+
+
+
+ imgui
+
+
+ imgui
+
+
+ imgui
+
+
+ sources
+
+
+ sources
+
+
+
+
+ imgui
+
+
+ sources
+
+
+ imgui
+
+
+ imgui
+
+
+ imgui
+
+
+ imgui
+
+
+ sources
+
+
+ sources
+
+
+
+
+
+ imgui
+
+
+ imgui
+
+
+
\ No newline at end of file
diff --git a/examples/example_sdl3_directx11/main.cpp b/examples/example_sdl3_directx11/main.cpp
new file mode 100644
index 000000000..f0940ae00
--- /dev/null
+++ b/examples/example_sdl3_directx11/main.cpp
@@ -0,0 +1,284 @@
+// Dear ImGui: standalone example application for SDL3 + DirectX 11
+// (SDL is a cross-platform general purpose library for handling windows, inputs, OpenGL/Vulkan/Metal graphics context creation, etc.)
+
+// Learn about Dear ImGui:
+// - FAQ https://dearimgui.com/faq
+// - Getting Started https://dearimgui.com/getting-started
+// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
+// - Introduction, links and more at the top of imgui.cpp
+
+#include "imgui.h"
+#include "imgui_impl_sdl3.h"
+#include "imgui_impl_dx11.h"
+#include
+#include // printf
+#include
+
+// Data
+static ID3D11Device* g_pd3dDevice = nullptr;
+static ID3D11DeviceContext* g_pd3dDeviceContext = nullptr;
+static IDXGISwapChain* g_pSwapChain = nullptr;
+static ID3D11RenderTargetView* g_mainRenderTargetView = nullptr;
+
+// Forward declarations of helper functions
+bool CreateDeviceD3D(HWND hWnd);
+void CleanupDeviceD3D();
+void CreateRenderTarget();
+void CleanupRenderTarget();
+
+// Main code
+int main(int, char**)
+{
+ // Setup SDL
+ // [If using SDL_MAIN_USE_CALLBACKS: all code below until the main loop starts would likely be your SDL_AppInit() function]
+ if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMEPAD))
+ {
+ printf("Error: SDL_Init(): %s\n", SDL_GetError());
+ return 1;
+ }
+
+ // Setup window
+ float main_scale = SDL_GetDisplayContentScale(SDL_GetPrimaryDisplay());
+ SDL_WindowFlags window_flags = SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIDDEN | SDL_WINDOW_HIGH_PIXEL_DENSITY;
+ SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL3+DirectX11 example", (int)(1280 * main_scale), (int)(800 * main_scale), window_flags);
+ if (window == nullptr)
+ {
+ printf("Error: SDL_CreateWindow(): %s\n", SDL_GetError());
+ return 1;
+ }
+
+ SDL_PropertiesID props = SDL_GetWindowProperties(window);
+ HWND hwnd = (HWND)SDL_GetPointerProperty(props, SDL_PROP_WINDOW_WIN32_HWND_POINTER, NULL);
+
+ // Initialize Direct3D
+ if (!CreateDeviceD3D(hwnd))
+ {
+ CleanupDeviceD3D();
+ return 1;
+ }
+
+ SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
+ SDL_ShowWindow(window);
+
+ // Setup Dear ImGui context
+ IMGUI_CHECKVERSION();
+ ImGui::CreateContext();
+ ImGuiIO& io = ImGui::GetIO(); (void)io;
+ io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
+ io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
+ io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking
+ io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows
+ //io.ConfigViewportsNoAutoMerge = true;
+ //io.ConfigViewportsNoTaskBarIcon = true;
+
+ // Setup Dear ImGui style
+ ImGui::StyleColorsDark();
+ //ImGui::StyleColorsLight();
+
+ // Setup scaling
+ ImGuiStyle& style = ImGui::GetStyle();
+ style.ScaleAllSizes(main_scale); // Bake a fixed style scale. (until we have a solution for dynamic style scaling, changing this requires resetting Style + calling this again)
+ style.FontScaleDpi = main_scale; // Set initial font scale. (using io.ConfigDpiScaleFonts=true makes this unnecessary. We leave both here for documentation purpose)
+ io.ConfigDpiScaleFonts = true; // [Experimental] Automatically overwrite style.FontScaleDpi in Begin() when Monitor DPI changes. This will scale fonts but _NOT_ scale sizes/padding for now.
+ io.ConfigDpiScaleViewports = true; // [Experimental] Scale Dear ImGui and Platform Windows when Monitor DPI changes.
+
+ // When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
+ if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
+ {
+ style.WindowRounding = 0.0f;
+ style.Colors[ImGuiCol_WindowBg].w = 1.0f;
+ }
+
+ // Setup Platform/Renderer backends
+ ImGui_ImplSDL3_InitForD3D(window);
+ ImGui_ImplDX11_Init(g_pd3dDevice, g_pd3dDeviceContext);
+
+ // Load Fonts
+ // - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
+ // - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
+ // - If the file cannot be loaded, the function will return a nullptr. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
+ // - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use Freetype for higher quality font rendering.
+ // - Read 'docs/FONTS.md' for more instructions and details. If you like the default font but want it to scale better, consider using the 'ProggyVector' from the same author!
+ // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
+ //style.FontSizeBase = 20.0f;
+ //io.Fonts->AddFontDefault();
+ //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf");
+ //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf");
+ //IM_ASSERT(font != nullptr);
+
+ // Our state
+ bool show_demo_window = true;
+ bool show_another_window = false;
+ ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
+
+ // Main loop
+ bool done = false;
+ while (!done)
+ {
+ // Poll and handle events (inputs, window resize, etc.)
+ // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
+ // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data.
+ // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data.
+ // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
+ // [If using SDL_MAIN_USE_CALLBACKS: call ImGui_ImplSDL3_ProcessEvent() from your SDL_AppEvent() function]
+ SDL_Event event;
+ while (SDL_PollEvent(&event))
+ {
+ ImGui_ImplSDL3_ProcessEvent(&event);
+ if (event.type == SDL_EVENT_QUIT)
+ done = true;
+ if (event.type == SDL_EVENT_WINDOW_CLOSE_REQUESTED && event.window.windowID == SDL_GetWindowID(window))
+ done = true;
+ if (event.type == SDL_EVENT_WINDOW_RESIZED && event.window.windowID == SDL_GetWindowID(window))
+ {
+ // Release all outstanding references to the swap chain's buffers before resizing.
+ CleanupRenderTarget();
+ g_pSwapChain->ResizeBuffers(0, 0, 0, DXGI_FORMAT_UNKNOWN, 0);
+ CreateRenderTarget();
+ }
+ }
+
+ // [If using SDL_MAIN_USE_CALLBACKS: all code below would likely be your SDL_AppIterate() function]
+ if (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED)
+ {
+ SDL_Delay(10);
+ continue;
+ }
+
+ // Start the Dear ImGui frame
+ ImGui_ImplDX11_NewFrame();
+ ImGui_ImplSDL3_NewFrame();
+ ImGui::NewFrame();
+
+ // 1. Show the big demo window (Most of the sample code is in ImGui::ShowDemoWindow()! You can browse its code to learn more about Dear ImGui!).
+ if (show_demo_window)
+ ImGui::ShowDemoWindow(&show_demo_window);
+
+ // 2. Show a simple window that we create ourselves. We use a Begin/End pair to create a named window.
+ {
+ static float f = 0.0f;
+ static int counter = 0;
+
+ ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it.
+
+ ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too)
+ ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state
+ ImGui::Checkbox("Another Window", &show_another_window);
+
+ ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
+ ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
+
+ if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
+ counter++;
+ ImGui::SameLine();
+ ImGui::Text("counter = %d", counter);
+
+ ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);
+ ImGui::End();
+ }
+
+ // 3. Show another simple window.
+ if (show_another_window)
+ {
+ ImGui::Begin("Another Window", &show_another_window); // Pass a pointer to our bool variable (the window will have a closing button that will clear the bool when clicked)
+ ImGui::Text("Hello from another window!");
+ if (ImGui::Button("Close Me"))
+ show_another_window = false;
+ ImGui::End();
+ }
+
+ // Rendering
+ ImGui::Render();
+ const float clear_color_with_alpha[4] = { clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w };
+ g_pd3dDeviceContext->OMSetRenderTargets(1, &g_mainRenderTargetView, nullptr);
+ g_pd3dDeviceContext->ClearRenderTargetView(g_mainRenderTargetView, clear_color_with_alpha);
+ ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
+
+ // Update and Render additional Platform Windows
+ if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
+ {
+ ImGui::UpdatePlatformWindows();
+ ImGui::RenderPlatformWindowsDefault();
+ }
+
+ g_pSwapChain->Present(1, 0); // Present with vsync
+ //g_pSwapChain->Present(0, 0); // Present without vsync
+ }
+
+ // Cleanup
+ // [If using SDL_MAIN_USE_CALLBACKS: all code below would likely be your SDL_AppQuit() function]
+ ImGui_ImplDX11_Shutdown();
+ ImGui_ImplSDL3_Shutdown();
+ ImGui::DestroyContext();
+
+ CleanupDeviceD3D();
+ SDL_DestroyWindow(window);
+ SDL_Quit();
+
+ return 0;
+}
+
+// Helper functions to use DirectX11
+bool CreateDeviceD3D(HWND hWnd)
+{
+ // Setup swap chain
+ DXGI_SWAP_CHAIN_DESC sd;
+ ZeroMemory(&sd, sizeof(sd));
+ sd.BufferCount = 2;
+ sd.BufferDesc.Width = 0;
+ sd.BufferDesc.Height = 0;
+ sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
+ sd.BufferDesc.RefreshRate.Numerator = 60;
+ sd.BufferDesc.RefreshRate.Denominator = 1;
+ sd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
+ sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
+ sd.OutputWindow = hWnd;
+ sd.SampleDesc.Count = 1;
+ sd.SampleDesc.Quality = 0;
+ sd.Windowed = TRUE;
+ sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
+
+ UINT createDeviceFlags = 0;
+ //createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
+ D3D_FEATURE_LEVEL featureLevel;
+ const D3D_FEATURE_LEVEL featureLevelArray[2] = { D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_0, };
+ HRESULT res = D3D11CreateDeviceAndSwapChain(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, createDeviceFlags, featureLevelArray, 2, D3D11_SDK_VERSION, &sd, &g_pSwapChain, &g_pd3dDevice, &featureLevel, &g_pd3dDeviceContext);
+ if (res == DXGI_ERROR_UNSUPPORTED) // Try high-performance WARP software driver if hardware is not available.
+ res = D3D11CreateDeviceAndSwapChain(nullptr, D3D_DRIVER_TYPE_WARP, nullptr, createDeviceFlags, featureLevelArray, 2, D3D11_SDK_VERSION, &sd, &g_pSwapChain, &g_pd3dDevice, &featureLevel, &g_pd3dDeviceContext);
+ if (res != S_OK)
+ return false;
+
+ // Disable DXGI's default Alt+Enter fullscreen behavior.
+ // - You are free to leave this enabled, but it will not work properly with multiple viewports.
+ // - This must be done for all windows associated to the device. Our DX11 backend does this automatically for secondary viewports that it creates.
+ IDXGIFactory* pSwapChainFactory;
+ if (SUCCEEDED(g_pSwapChain->GetParent(IID_PPV_ARGS(&pSwapChainFactory))))
+ pSwapChainFactory->MakeWindowAssociation(hWnd, DXGI_MWA_NO_ALT_ENTER);
+
+ CreateRenderTarget();
+ return true;
+}
+
+void CleanupDeviceD3D()
+{
+ CleanupRenderTarget();
+ if (g_pSwapChain) { g_pSwapChain->Release(); g_pSwapChain = nullptr; }
+ if (g_pd3dDeviceContext) { g_pd3dDeviceContext->Release(); g_pd3dDeviceContext = nullptr; }
+ if (g_pd3dDevice) { g_pd3dDevice->Release(); g_pd3dDevice = nullptr; }
+}
+
+void CreateRenderTarget()
+{
+ ID3D11Texture2D* pBackBuffer;
+ g_pSwapChain->GetBuffer(0, IID_PPV_ARGS(&pBackBuffer));
+ g_pd3dDevice->CreateRenderTargetView(pBackBuffer, nullptr, &g_mainRenderTargetView);
+ pBackBuffer->Release();
+}
+
+void CleanupRenderTarget()
+{
+ if (g_mainRenderTargetView) { g_mainRenderTargetView->Release(); g_mainRenderTargetView = nullptr; }
+}
diff --git a/examples/example_sdl3_metal/Makefile b/examples/example_sdl3_metal/Makefile
new file mode 100644
index 000000000..9093ae133
--- /dev/null
+++ b/examples/example_sdl3_metal/Makefile
@@ -0,0 +1,48 @@
+#
+# You will need SDL3 (http://www.libsdl.org):
+# brew install sdl3
+#
+
+#CXX = g++
+#CXX = clang++
+
+EXE = example_sdl3_metal
+IMGUI_DIR = ../..
+SOURCES = main.mm
+SOURCES += $(IMGUI_DIR)/imgui.cpp $(IMGUI_DIR)/imgui_demo.cpp $(IMGUI_DIR)/imgui_draw.cpp $(IMGUI_DIR)/imgui_tables.cpp $(IMGUI_DIR)/imgui_widgets.cpp
+SOURCES += $(IMGUI_DIR)/backends/imgui_impl_sdl3.cpp $(IMGUI_DIR)/backends/imgui_impl_metal.mm
+OBJS = $(addsuffix .o, $(basename $(notdir $(SOURCES))))
+
+LIBS = -framework Metal -framework MetalKit -framework Cocoa -framework IOKit -framework CoreVideo -framework QuartzCore
+LIBS += `pkg-config --libs sdl3`
+LIBS += -L/usr/local/lib -L/opt/local/lib
+
+CXXFLAGS += `pkg-config --cflags sdl3`
+CXXFLAGS += -I/usr/local/include -I/opt/local/include
+CXXFLAGS += -std=c++11 -I$(IMGUI_DIR) -I$(IMGUI_DIR)/backends
+CXXFLAGS += -Wall -Wformat
+CFLAGS = $(CXXFLAGS)
+
+%.o:%.cpp
+ $(CXX) $(CXXFLAGS) -c -o $@ $<
+
+%.o:$(IMGUI_DIR)/%.cpp
+ $(CXX) $(CXXFLAGS) -c -o $@ $<
+
+%.o:$(IMGUI_DIR)/backends/%.cpp
+ $(CXX) $(CXXFLAGS) -c -o $@ $<
+
+%.o:%.mm
+ $(CXX) $(CXXFLAGS) -ObjC++ -fobjc-weak -fobjc-arc -c -o $@ $<
+
+%.o:$(IMGUI_DIR)/backends/%.mm
+ $(CXX) $(CXXFLAGS) -ObjC++ -fobjc-weak -fobjc-arc -c -o $@ $<
+
+all: $(EXE)
+ @echo Build complete
+
+$(EXE): $(OBJS)
+ $(CXX) -o $@ $^ $(CXXFLAGS) $(LIBS)
+
+clean:
+ rm -f $(EXE) $(OBJS)
diff --git a/examples/example_sdl3_metal/main.mm b/examples/example_sdl3_metal/main.mm
new file mode 100644
index 000000000..1dd80d010
--- /dev/null
+++ b/examples/example_sdl3_metal/main.mm
@@ -0,0 +1,208 @@
+// Dear ImGui: standalone example application for SDL3 + Metal
+// (SDL is a cross-platform general purpose library for handling windows, inputs, OpenGL/Vulkan/Metal graphics context creation, etc.)
+
+// Learn about Dear ImGui:
+// - FAQ https://dearimgui.com/faq
+// - Getting Started https://dearimgui.com/getting-started
+// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
+// - Introduction, links and more at the top of imgui.cpp
+
+#include "imgui.h"
+#include "imgui_impl_sdl3.h"
+#include "imgui_impl_metal.h"
+#include // printf, fprintf
+#include
+
+#import
+#import
+
+// Main code
+int main(int, char**)
+{
+ // Setup SDL
+ // [If using SDL_MAIN_USE_CALLBACKS: all code below until the main loop starts would likely be your SDL_AppInit() function]
+ if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMEPAD))
+ {
+ printf("Error: SDL_Init(): %s\n", SDL_GetError());
+ return 1;
+ }
+
+ // Create SDL window graphics context
+ float main_scale = SDL_GetDisplayContentScale(SDL_GetPrimaryDisplay());
+ SDL_WindowFlags window_flags = SDL_WINDOW_METAL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIDDEN | SDL_WINDOW_HIGH_PIXEL_DENSITY;
+ SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL3+Metal example", (int)(1280 * main_scale), (int)(800 * main_scale), window_flags);
+ if (window == nullptr)
+ {
+ printf("Error: SDL_CreateWindow(): %s\n", SDL_GetError());
+ return 1;
+ }
+ SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
+ SDL_ShowWindow(window);
+
+ // Create Metal device _before_ creating the view/layer
+ id metalDevice = MTLCreateSystemDefaultDevice();
+ if (!metalDevice)
+ {
+ printf("Error: failed to create Metal device.\n");
+ SDL_DestroyWindow(window);
+ SDL_Quit();
+ return 1;
+ }
+ SDL_MetalView view = SDL_Metal_CreateView(window);
+ CAMetalLayer* layer = (__bridge CAMetalLayer*)SDL_Metal_GetLayer(view);
+ layer.device = metalDevice;
+ layer.pixelFormat = MTLPixelFormatBGRA8Unorm;
+
+ id commandQueue = [layer.device newCommandQueue];
+ MTLRenderPassDescriptor* renderPassDescriptor = [MTLRenderPassDescriptor new];
+
+ // Setup Dear ImGui context
+ IMGUI_CHECKVERSION();
+ ImGui::CreateContext();
+ ImGuiIO& io = ImGui::GetIO(); (void)io;
+ io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
+ io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
+
+ // Setup Dear ImGui style
+ ImGui::StyleColorsDark();
+ //ImGui::StyleColorsLight();
+
+ // Setup scaling
+ ImGuiStyle& style = ImGui::GetStyle();
+ style.ScaleAllSizes(main_scale); // Bake a fixed style scale. (until we have a solution for dynamic style scaling, changing this requires resetting Style + calling this again)
+ style.FontScaleDpi = main_scale; // Set initial font scale. (using io.ConfigDpiScaleFonts=true makes this unnecessary. We leave both here for documentation purpose)
+
+ // Setup Platform/Renderer backends
+ ImGui_ImplMetal_Init(layer.device);
+ ImGui_ImplSDL3_InitForMetal(window);
+
+ // Load Fonts
+ // - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
+ // - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
+ // - If the file cannot be loaded, the function will return a nullptr. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
+ // - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use Freetype for higher quality font rendering.
+ // - Read 'docs/FONTS.md' for more instructions and details. If you like the default font but want it to scale better, consider using the 'ProggyVector' from the same author!
+ // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
+ //style.FontSizeBase = 20.0f;
+ //io.Fonts->AddFontDefault();
+ //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf");
+ //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf");
+ //IM_ASSERT(font != nullptr);
+
+ // Our state
+ bool show_demo_window = true;
+ bool show_another_window = false;
+ float clear_color[4] = { 0.45f, 0.55f, 0.60f, 1.00f };
+
+ // Main loop
+ bool done = false;
+ while (!done)
+ {
+ @autoreleasepool
+ {
+ // Poll and handle events (inputs, window resize, etc.)
+ // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
+ // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data.
+ // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data.
+ // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
+ // [If using SDL_MAIN_USE_CALLBACKS: call ImGui_ImplSDL3_ProcessEvent() from your SDL_AppEvent() function]
+ SDL_Event event;
+ while (SDL_PollEvent(&event))
+ {
+ ImGui_ImplSDL3_ProcessEvent(&event);
+ if (event.type == SDL_EVENT_QUIT)
+ done = true;
+ if (event.type == SDL_EVENT_WINDOW_CLOSE_REQUESTED && event.window.windowID == SDL_GetWindowID(window))
+ done = true;
+ }
+
+ // [If using SDL_MAIN_USE_CALLBACKS: all code below would likely be your SDL_AppIterate() function]
+ if (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED)
+ {
+ SDL_Delay(10);
+ continue;
+ }
+
+ int width, height;
+ SDL_GetWindowSizeInPixels(window, &width, &height);
+
+ layer.drawableSize = CGSizeMake(width, height);
+ id drawable = [layer nextDrawable];
+
+ id commandBuffer = [commandQueue commandBuffer];
+ renderPassDescriptor.colorAttachments[0].clearColor = MTLClearColorMake(clear_color[0] * clear_color[3], clear_color[1] * clear_color[3], clear_color[2] * clear_color[3], clear_color[3]);
+ renderPassDescriptor.colorAttachments[0].texture = drawable.texture;
+ renderPassDescriptor.colorAttachments[0].loadAction = MTLLoadActionClear;
+ renderPassDescriptor.colorAttachments[0].storeAction = MTLStoreActionStore;
+ id renderEncoder = [commandBuffer renderCommandEncoderWithDescriptor:renderPassDescriptor];
+ [renderEncoder pushDebugGroup:@"ImGui demo"];
+
+ // Start the Dear ImGui frame
+ ImGui_ImplMetal_NewFrame(renderPassDescriptor);
+ ImGui_ImplSDL3_NewFrame();
+ ImGui::NewFrame();
+
+ // 1. Show the big demo window (Most of the sample code is in ImGui::ShowDemoWindow()! You can browse its code to learn more about Dear ImGui!).
+ if (show_demo_window)
+ ImGui::ShowDemoWindow(&show_demo_window);
+
+ // 2. Show a simple window that we create ourselves. We use a Begin/End pair to create a named window.
+ {
+ static float f = 0.0f;
+ static int counter = 0;
+
+ ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it.
+
+ ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too)
+ ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state
+ ImGui::Checkbox("Another Window", &show_another_window);
+
+ ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
+ ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
+
+ if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
+ counter++;
+ ImGui::SameLine();
+ ImGui::Text("counter = %d", counter);
+
+ ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);
+ ImGui::End();
+ }
+
+ // 3. Show another simple window.
+ if (show_another_window)
+ {
+ ImGui::Begin("Another Window", &show_another_window); // Pass a pointer to our bool variable (the window will have a closing button that will clear the bool when clicked)
+ ImGui::Text("Hello from another window!");
+ if (ImGui::Button("Close Me"))
+ show_another_window = false;
+ ImGui::End();
+ }
+
+ // Rendering
+ ImGui::Render();
+ ImDrawData* draw_data = ImGui::GetDrawData();
+ ImGui_ImplMetal_RenderDrawData(draw_data, commandBuffer, renderEncoder);
+
+ [renderEncoder popDebugGroup];
+ [renderEncoder endEncoding];
+
+ [commandBuffer presentDrawable:drawable];
+ [commandBuffer commit];
+ }
+ }
+
+ // Cleanup
+ // [If using SDL_MAIN_USE_CALLBACKS: all code below would likely be your SDL_AppQuit() function]
+ ImGui_ImplMetal_Shutdown();
+ ImGui_ImplSDL3_Shutdown();
+ ImGui::DestroyContext();
+
+ SDL_DestroyWindow(window);
+ SDL_Quit();
+
+ return 0;
+}
diff --git a/examples/example_sdl3_opengl3/Makefile b/examples/example_sdl3_opengl3/Makefile
index c2ef3ba55..d9c6eac9e 100644
--- a/examples/example_sdl3_opengl3/Makefile
+++ b/examples/example_sdl3_opengl3/Makefile
@@ -45,10 +45,11 @@ endif
ifeq ($(UNAME_S), Darwin) #APPLE
ECHO_MESSAGE = "Mac OS X"
- LIBS += -framework OpenGL -framework Cocoa -framework IOKit -framework CoreVideo `sdl3-config --libs`
+ LIBS += -framework OpenGL -framework Cocoa -framework IOKit -framework CoreVideo
+ LIBS += `pkg-config --libs sdl3`
LIBS += -L/usr/local/lib -L/opt/local/lib
- CXXFLAGS += `pkg-config sdl3 --cflags`
+ CXXFLAGS += `pkg-config --cflags sdl3`
CXXFLAGS += -I/usr/local/include -I/opt/local/include
CFLAGS = $(CXXFLAGS)
endif
diff --git a/examples/example_sdl3_opengl3/main.cpp b/examples/example_sdl3_opengl3/main.cpp
index e38b41408..706f2db8a 100644
--- a/examples/example_sdl3_opengl3/main.cpp
+++ b/examples/example_sdl3_opengl3/main.cpp
@@ -30,7 +30,7 @@ int main(int, char**)
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMEPAD))
{
printf("Error: SDL_Init(): %s\n", SDL_GetError());
- return -1;
+ return 1;
}
// Decide GL+GLSL versions
@@ -68,23 +68,24 @@ int main(int, char**)
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
- Uint32 window_flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIDDEN;
- SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL3+OpenGL3 example", 1280, 720, window_flags);
+ float main_scale = SDL_GetDisplayContentScale(SDL_GetPrimaryDisplay());
+ SDL_WindowFlags window_flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIDDEN | SDL_WINDOW_HIGH_PIXEL_DENSITY;
+ SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL3+OpenGL3 example", (int)(1280 * main_scale), (int)(800 * main_scale), window_flags);
if (window == nullptr)
{
printf("Error: SDL_CreateWindow(): %s\n", SDL_GetError());
- return -1;
+ return 1;
}
- SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
SDL_GLContext gl_context = SDL_GL_CreateContext(window);
if (gl_context == nullptr)
{
printf("Error: SDL_GL_CreateContext(): %s\n", SDL_GetError());
- return -1;
+ return 1;
}
SDL_GL_MakeCurrent(window, gl_context);
SDL_GL_SetSwapInterval(1); // Enable vsync
+ SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
SDL_ShowWindow(window);
// Setup Dear ImGui context
@@ -102,8 +103,14 @@ int main(int, char**)
ImGui::StyleColorsDark();
//ImGui::StyleColorsLight();
- // When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
+ // Setup scaling
ImGuiStyle& style = ImGui::GetStyle();
+ style.ScaleAllSizes(main_scale); // Bake a fixed style scale. (until we have a solution for dynamic style scaling, changing this requires resetting Style + calling this again)
+ style.FontScaleDpi = main_scale; // Set initial font scale. (using io.ConfigDpiScaleFonts=true makes this unnecessary. We leave both here for documentation purpose)
+ io.ConfigDpiScaleFonts = true; // [Experimental] Automatically overwrite style.FontScaleDpi in Begin() when Monitor DPI changes. This will scale fonts but _NOT_ scale sizes/padding for now.
+ io.ConfigDpiScaleViewports = true; // [Experimental] Scale Dear ImGui and Platform Windows when Monitor DPI changes.
+
+ // When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{
style.WindowRounding = 0.0f;
@@ -118,17 +125,17 @@ int main(int, char**)
// - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
// - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
// - If the file cannot be loaded, the function will return a nullptr. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
- // - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
// - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use Freetype for higher quality font rendering.
- // - Read 'docs/FONTS.md' for more instructions and details.
+ // - Read 'docs/FONTS.md' for more instructions and details. If you like the default font but want it to scale better, consider using the 'ProggyVector' from the same author!
// - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
// - Our Emscripten build process allows embedding fonts to be accessible at runtime from the "fonts/" folder. See Makefile.emscripten for details.
+ //style.FontSizeBase = 20.0f;
//io.Fonts->AddFontDefault();
- //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf", 18.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
- //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, nullptr, io.Fonts->GetGlyphRangesJapanese());
+ //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf");
+ //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf");
//IM_ASSERT(font != nullptr);
// Our state
diff --git a/examples/example_sdl3_sdlgpu3/main.cpp b/examples/example_sdl3_sdlgpu3/main.cpp
index 3deeeafd5..cde27bd51 100644
--- a/examples/example_sdl3_sdlgpu3/main.cpp
+++ b/examples/example_sdl3_sdlgpu3/main.cpp
@@ -1,4 +1,5 @@
// Dear ImGui: standalone example application for SDL3 + SDL_GPU
+// (SDL is a cross-platform general purpose library for handling windows, inputs, OpenGL/Vulkan/Metal graphics context creation, etc.)
// Learn about Dear ImGui:
// - FAQ https://dearimgui.com/faq
@@ -30,32 +31,36 @@ int main(int, char**)
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMEPAD))
{
printf("Error: SDL_Init(): %s\n", SDL_GetError());
- return -1;
+ return 1;
}
// Create SDL window graphics context
- SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL3+SDL_GPU example", 1280, 720, SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIGH_PIXEL_DENSITY);
+ float main_scale = SDL_GetDisplayContentScale(SDL_GetPrimaryDisplay());
+ SDL_WindowFlags window_flags = SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIDDEN | SDL_WINDOW_HIGH_PIXEL_DENSITY;
+ SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL3+SDL_GPU example", (int)(1280 * main_scale), (int)(800 * main_scale), window_flags);
if (window == nullptr)
{
printf("Error: SDL_CreateWindow(): %s\n", SDL_GetError());
- return -1;
+ return 1;
}
+ SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
+ SDL_ShowWindow(window);
// Create GPU Device
SDL_GPUDevice* gpu_device = SDL_CreateGPUDevice(SDL_GPU_SHADERFORMAT_SPIRV | SDL_GPU_SHADERFORMAT_DXIL | SDL_GPU_SHADERFORMAT_METALLIB,true,nullptr);
if (gpu_device == nullptr)
{
printf("Error: SDL_CreateGPUDevice(): %s\n", SDL_GetError());
- return -1;
+ return 1;
}
// Claim window for GPU Device
if (!SDL_ClaimWindowForGPUDevice(gpu_device, window))
{
printf("Error: SDL_ClaimWindowForGPUDevice(): %s\n", SDL_GetError());
- return -1;
+ return 1;
}
- SDL_SetGPUSwapchainParameters(gpu_device, window, SDL_GPU_SWAPCHAINCOMPOSITION_SDR, SDL_GPU_PRESENTMODE_MAILBOX);
+ SDL_SetGPUSwapchainParameters(gpu_device, window, SDL_GPU_SWAPCHAINCOMPOSITION_SDR, SDL_GPU_PRESENTMODE_VSYNC);
// Setup Dear ImGui context
IMGUI_CHECKVERSION();
@@ -63,33 +68,51 @@ int main(int, char**)
ImGuiIO& io = ImGui::GetIO(); (void)io;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
+ io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking
+ io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows
// Setup Dear ImGui style
ImGui::StyleColorsDark();
//ImGui::StyleColorsLight();
+ // Setup scaling
+ ImGuiStyle& style = ImGui::GetStyle();
+ style.ScaleAllSizes(main_scale); // Bake a fixed style scale. (until we have a solution for dynamic style scaling, changing this requires resetting Style + calling this again)
+ style.FontScaleDpi = main_scale; // Set initial font scale. (using io.ConfigDpiScaleFonts=true makes this unnecessary. We leave both here for documentation purpose)
+ io.ConfigDpiScaleFonts = true; // [Experimental] Automatically overwrite style.FontScaleDpi in Begin() when Monitor DPI changes. This will scale fonts but _NOT_ scale sizes/padding for now.
+ io.ConfigDpiScaleViewports = true; // [Experimental] Scale Dear ImGui and Platform Windows when Monitor DPI changes.
+
+ // When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
+ if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
+ {
+ style.WindowRounding = 0.0f;
+ style.Colors[ImGuiCol_WindowBg].w = 1.0f;
+ }
+
// Setup Platform/Renderer backends
ImGui_ImplSDL3_InitForSDLGPU(window);
ImGui_ImplSDLGPU3_InitInfo init_info = {};
init_info.Device = gpu_device;
init_info.ColorTargetFormat = SDL_GetGPUSwapchainTextureFormat(gpu_device, window);
- init_info.MSAASamples = SDL_GPU_SAMPLECOUNT_1;
+ init_info.MSAASamples = SDL_GPU_SAMPLECOUNT_1; // Only used in multi-viewports mode.
+ init_info.SwapchainComposition = SDL_GPU_SWAPCHAINCOMPOSITION_SDR; // Only used in multi-viewports mode.
+ init_info.PresentMode = SDL_GPU_PRESENTMODE_VSYNC;
ImGui_ImplSDLGPU3_Init(&init_info);
// Load Fonts
// - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
// - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
// - If the file cannot be loaded, the function will return a nullptr. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
- // - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
// - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use Freetype for higher quality font rendering.
- // - Read 'docs/FONTS.md' for more instructions and details.
+ // - Read 'docs/FONTS.md' for more instructions and details. If you like the default font but want it to scale better, consider using the 'ProggyVector' from the same author!
// - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
+ //style.FontSizeBase = 20.0f;
//io.Fonts->AddFontDefault();
- //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf", 18.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
- //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, nullptr, io.Fonts->GetGlyphRangesJapanese());
+ //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf");
+ //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf");
//IM_ASSERT(font != nullptr);
// Our state
@@ -145,7 +168,7 @@ int main(int, char**)
ImGui::Checkbox("Another Window", &show_another_window);
ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
- ImGui::ColorEdit4("clear color", (float*)&clear_color); // Edit 3 floats representing a color
+ ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
counter++;
@@ -174,7 +197,7 @@ int main(int, char**)
SDL_GPUCommandBuffer* command_buffer = SDL_AcquireGPUCommandBuffer(gpu_device); // Acquire a GPU command buffer
SDL_GPUTexture* swapchain_texture;
- SDL_AcquireGPUSwapchainTexture(command_buffer, window, &swapchain_texture, nullptr, nullptr); // Acquire a swapchain texture
+ SDL_WaitAndAcquireGPUSwapchainTexture(command_buffer, window, &swapchain_texture, nullptr, nullptr); // Acquire a swapchain texture
if (swapchain_texture != nullptr && !is_minimized)
{
@@ -198,6 +221,13 @@ int main(int, char**)
SDL_EndGPURenderPass(render_pass);
}
+ // Update and Render additional Platform Windows
+ if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
+ {
+ ImGui::UpdatePlatformWindows();
+ ImGui::RenderPlatformWindowsDefault();
+ }
+
// Submit the command buffer
SDL_SubmitGPUCommandBuffer(command_buffer);
}
diff --git a/examples/example_sdl3_sdlrenderer3/Makefile b/examples/example_sdl3_sdlrenderer3/Makefile
index 1976fa924..c278f7830 100644
--- a/examples/example_sdl3_sdlrenderer3/Makefile
+++ b/examples/example_sdl3_sdlrenderer3/Makefile
@@ -26,7 +26,7 @@ LIBS =
ifeq ($(UNAME_S), Linux) #LINUX
ECHO_MESSAGE = "Linux"
- LIBS += -ldl `pkg-config sdl3 --libs`
+ LIBS += -ldl `pkg-config sdl3 --libs`
CXXFLAGS += `pkg-config sdl3 --cflags`
CFLAGS = $(CXXFLAGS)
@@ -34,10 +34,11 @@ endif
ifeq ($(UNAME_S), Darwin) #APPLE
ECHO_MESSAGE = "Mac OS X"
- LIBS += -framework Cocoa -framework IOKit -framework CoreVideo `sdl3-config --libs`
+ LIBS += -framework Cocoa -framework IOKit -framework CoreVideo
+ LIBS += `pkg-config --libs sdl3`
LIBS += -L/usr/local/lib -L/opt/local/lib
- CXXFLAGS += `pkg-config sdl3 --cflags`
+ CXXFLAGS += `pkg-config --cflags sdl3`
CXXFLAGS += -I/usr/local/include -I/opt/local/include
CFLAGS = $(CXXFLAGS)
endif
diff --git a/examples/example_sdl3_sdlrenderer3/main.cpp b/examples/example_sdl3_sdlrenderer3/main.cpp
index 6cc8bf522..4d082f7a6 100644
--- a/examples/example_sdl3_sdlrenderer3/main.cpp
+++ b/examples/example_sdl3_sdlrenderer3/main.cpp
@@ -28,23 +28,24 @@ int main(int, char**)
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMEPAD))
{
printf("Error: SDL_Init(): %s\n", SDL_GetError());
- return -1;
+ return 1;
}
// Create window with SDL_Renderer graphics context
- Uint32 window_flags = SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIDDEN;
- SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL3+SDL_Renderer example", 1280, 720, window_flags);
+ float main_scale = SDL_GetDisplayContentScale(SDL_GetPrimaryDisplay());
+ SDL_WindowFlags window_flags = SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIDDEN | SDL_WINDOW_HIGH_PIXEL_DENSITY;
+ SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL3+SDL_Renderer example", (int)(1280 * main_scale), (int)(800 * main_scale), window_flags);
if (window == nullptr)
{
printf("Error: SDL_CreateWindow(): %s\n", SDL_GetError());
- return -1;
+ return 1;
}
SDL_Renderer* renderer = SDL_CreateRenderer(window, nullptr);
SDL_SetRenderVSync(renderer, 1);
if (renderer == nullptr)
{
SDL_Log("Error: SDL_CreateRenderer(): %s\n", SDL_GetError());
- return -1;
+ return 1;
}
SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
SDL_ShowWindow(window);
@@ -61,6 +62,13 @@ int main(int, char**)
ImGui::StyleColorsDark();
//ImGui::StyleColorsLight();
+ // Setup scaling
+ ImGuiStyle& style = ImGui::GetStyle();
+ style.ScaleAllSizes(main_scale); // Bake a fixed style scale. (until we have a solution for dynamic style scaling, changing this requires resetting Style + calling this again)
+ style.FontScaleDpi = main_scale; // Set initial font scale. (using io.ConfigDpiScaleFonts=true makes this unnecessary. We leave both here for documentation purpose)
+ //io.ConfigDpiScaleFonts = true; // [Experimental] Automatically overwrite style.FontScaleDpi in Begin() when Monitor DPI changes. This will scale fonts but _NOT_ scale sizes/padding for now.
+ //io.ConfigDpiScaleViewports = true; // [Experimental] Scale Dear ImGui and Platform Windows when Monitor DPI changes.
+
// Setup Platform/Renderer backends
ImGui_ImplSDL3_InitForSDLRenderer(window, renderer);
ImGui_ImplSDLRenderer3_Init(renderer);
@@ -69,17 +77,17 @@ int main(int, char**)
// - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
// - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
// - If the file cannot be loaded, the function will return a nullptr. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
- // - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
// - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use Freetype for higher quality font rendering.
- // - Read 'docs/FONTS.md' for more instructions and details.
+ // - Read 'docs/FONTS.md' for more instructions and details. If you like the default font but want it to scale better, consider using the 'ProggyVector' from the same author!
// - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
// - Our Emscripten build process allows embedding fonts to be accessible at runtime from the "fonts/" folder. See Makefile.emscripten for details.
+ //style.FontSizeBase = 20.0f;
//io.Fonts->AddFontDefault();
- //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf", 18.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
- //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, nullptr, io.Fonts->GetGlyphRangesJapanese());
+ //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf");
+ //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf");
//IM_ASSERT(font != nullptr);
// Our state
@@ -165,7 +173,7 @@ int main(int, char**)
// Rendering
ImGui::Render();
- //SDL_RenderSetScale(renderer, io.DisplayFramebufferScale.x, io.DisplayFramebufferScale.y);
+ SDL_SetRenderScale(renderer, io.DisplayFramebufferScale.x, io.DisplayFramebufferScale.y);
SDL_SetRenderDrawColorFloat(renderer, clear_color.x, clear_color.y, clear_color.z, clear_color.w);
SDL_RenderClear(renderer);
ImGui_ImplSDLRenderer3_RenderDrawData(ImGui::GetDrawData(), renderer);
diff --git a/examples/example_sdl3_vulkan/Makefile b/examples/example_sdl3_vulkan/Makefile
new file mode 100644
index 000000000..e1956ff67
--- /dev/null
+++ b/examples/example_sdl3_vulkan/Makefile
@@ -0,0 +1,77 @@
+#
+# Cross Platform Makefile
+# Compatible with MSYS2/MINGW, Ubuntu 14.04.1 and Mac OS X
+#
+# You will need SDL3 (http://www.libsdl.org) which is still unreleased/unpackaged.
+# Mac OS X:
+# brew install sdl3
+
+#CXX = g++
+#CXX = clang++
+
+EXE = example_sdl3_vulkan
+IMGUI_DIR = ../..
+SOURCES = main.cpp
+SOURCES += $(IMGUI_DIR)/imgui.cpp $(IMGUI_DIR)/imgui_demo.cpp $(IMGUI_DIR)/imgui_draw.cpp $(IMGUI_DIR)/imgui_tables.cpp $(IMGUI_DIR)/imgui_widgets.cpp
+SOURCES += $(IMGUI_DIR)/backends/imgui_impl_sdl3.cpp $(IMGUI_DIR)/backends/imgui_impl_vulkan.cpp
+OBJS = $(addsuffix .o, $(basename $(notdir $(SOURCES))))
+UNAME_S := $(shell uname -s)
+
+CXXFLAGS = -std=c++11 -I$(IMGUI_DIR) -I$(IMGUI_DIR)/backends
+CXXFLAGS += -g -Wall -Wformat
+LIBS =
+
+
+##---------------------------------------------------------------------
+## BUILD FLAGS PER PLATFORM
+##---------------------------------------------------------------------
+
+ifeq ($(UNAME_S), Linux) #LINUX
+ ECHO_MESSAGE = "Linux"
+ LIBS += -ldl
+ LIBS += `pkg-config --libs sdl3 vulkan`
+
+ CXXFLAGS += `pkg-config --cflags sdl3 vulkan`
+ CFLAGS = $(CXXFLAGS)
+endif
+
+ifeq ($(UNAME_S), Darwin) #APPLE
+ ECHO_MESSAGE = "Mac OS X"
+ LIBS += -framework Cocoa -framework IOKit -framework CoreVideo
+ LIBS += `pkg-config --libs sdl3 vulkan`
+ LIBS += -L/usr/local/lib -L/opt/local/lib
+
+ CXXFLAGS += `pkg-config --cflags sdl3 vulkan`
+ CXXFLAGS += -I/usr/local/include -I/opt/local/include
+ CFLAGS = $(CXXFLAGS)
+endif
+
+ifeq ($(OS), Windows_NT)
+ ECHO_MESSAGE = "MinGW"
+ LIBS += -lgdi32 -limm32 `pkg-config --static --libs sdl3 vulkan`
+
+ CXXFLAGS += `pkg-config --cflags sdl3 vulkan`
+ CFLAGS = $(CXXFLAGS)
+endif
+
+##---------------------------------------------------------------------
+## BUILD RULES
+##---------------------------------------------------------------------
+
+%.o:%.cpp
+ $(CXX) $(CXXFLAGS) -c -o $@ $<
+
+%.o:$(IMGUI_DIR)/%.cpp
+ $(CXX) $(CXXFLAGS) -c -o $@ $<
+
+%.o:$(IMGUI_DIR)/backends/%.cpp
+ $(CXX) $(CXXFLAGS) -c -o $@ $<
+
+all: $(EXE)
+ @echo Build complete for $(ECHO_MESSAGE)
+
+$(EXE): $(OBJS)
+ $(CXX) -o $@ $^ $(CXXFLAGS) $(LIBS)
+
+clean:
+ rm -f $(EXE) $(OBJS)
diff --git a/examples/example_sdl3_vulkan/main.cpp b/examples/example_sdl3_vulkan/main.cpp
index 552e9a898..c2b7f793b 100644
--- a/examples/example_sdl3_vulkan/main.cpp
+++ b/examples/example_sdl3_vulkan/main.cpp
@@ -35,6 +35,7 @@
//#define APP_USE_UNLIMITED_FRAME_RATE
#ifdef _DEBUG
#define APP_USE_VULKAN_DEBUG_REPORT
+static VkDebugReportCallbackEXT g_DebugReport = VK_NULL_HANDLE;
#endif
// Data
@@ -44,7 +45,6 @@ static VkPhysicalDevice g_PhysicalDevice = VK_NULL_HANDLE;
static VkDevice g_Device = VK_NULL_HANDLE;
static uint32_t g_QueueFamily = (uint32_t)-1;
static VkQueue g_Queue = VK_NULL_HANDLE;
-static VkDebugReportCallbackEXT g_DebugReport = VK_NULL_HANDLE;
static VkPipelineCache g_PipelineCache = VK_NULL_HANDLE;
static VkDescriptorPool g_DescriptorPool = VK_NULL_HANDLE;
@@ -232,7 +232,7 @@ static void SetupVulkanWindow(ImGui_ImplVulkanH_Window* wd, VkSurfaceKHR surface
// Create SwapChain, RenderPass, Framebuffer, etc.
IM_ASSERT(g_MinImageCount >= 2);
- ImGui_ImplVulkanH_CreateOrResizeWindow(g_Instance, g_PhysicalDevice, g_Device, wd, g_QueueFamily, g_Allocator, width, height, g_MinImageCount);
+ ImGui_ImplVulkanH_CreateOrResizeWindow(g_Instance, g_PhysicalDevice, g_Device, wd, g_QueueFamily, g_Allocator, width, height, g_MinImageCount, 0);
}
static void CleanupVulkan()
@@ -349,16 +349,17 @@ int main(int, char**)
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMEPAD))
{
printf("Error: SDL_Init(): %s\n", SDL_GetError());
- return -1;
+ return 1;
}
// Create window with Vulkan graphics context
- SDL_WindowFlags window_flags = (SDL_WindowFlags)(SDL_WINDOW_VULKAN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIGH_PIXEL_DENSITY | SDL_WINDOW_HIDDEN);
- SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL3+Vulkan example", 1280, 720, window_flags);
+ float main_scale = SDL_GetDisplayContentScale(SDL_GetPrimaryDisplay());
+ SDL_WindowFlags window_flags = SDL_WINDOW_VULKAN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIDDEN | SDL_WINDOW_HIGH_PIXEL_DENSITY;
+ SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL3+Vulkan example", (int)(1280 * main_scale), (int)(800 * main_scale), window_flags);
if (window == nullptr)
{
printf("Error: SDL_CreateWindow(): %s\n", SDL_GetError());
- return -1;
+ return 1;
}
ImVector extensions;
@@ -402,8 +403,14 @@ int main(int, char**)
ImGui::StyleColorsDark();
//ImGui::StyleColorsLight();
- // When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
+ // Setup scaling
ImGuiStyle& style = ImGui::GetStyle();
+ style.ScaleAllSizes(main_scale); // Bake a fixed style scale. (until we have a solution for dynamic style scaling, changing this requires resetting Style + calling this again)
+ style.FontScaleDpi = main_scale; // Set initial font scale. (using io.ConfigDpiScaleFonts=true makes this unnecessary. We leave both here for documentation purpose)
+ io.ConfigDpiScaleFonts = true; // [Experimental] Automatically overwrite style.FontScaleDpi in Begin() when Monitor DPI changes. This will scale fonts but _NOT_ scale sizes/padding for now.
+ io.ConfigDpiScaleViewports = true; // [Experimental] Scale Dear ImGui and Platform Windows when Monitor DPI changes.
+
+ // When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{
style.WindowRounding = 0.0f;
@@ -421,12 +428,12 @@ int main(int, char**)
init_info.Queue = g_Queue;
init_info.PipelineCache = g_PipelineCache;
init_info.DescriptorPool = g_DescriptorPool;
- init_info.RenderPass = wd->RenderPass;
- init_info.Subpass = 0;
init_info.MinImageCount = g_MinImageCount;
init_info.ImageCount = wd->ImageCount;
- init_info.MSAASamples = VK_SAMPLE_COUNT_1_BIT;
init_info.Allocator = g_Allocator;
+ init_info.PipelineInfoMain.RenderPass = wd->RenderPass;
+ init_info.PipelineInfoMain.Subpass = 0;
+ init_info.PipelineInfoMain.MSAASamples = VK_SAMPLE_COUNT_1_BIT;
init_info.CheckVkResultFn = check_vk_result;
ImGui_ImplVulkan_Init(&init_info);
@@ -434,16 +441,16 @@ int main(int, char**)
// - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
// - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
// - If the file cannot be loaded, the function will return a nullptr. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
- // - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
// - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use Freetype for higher quality font rendering.
- // - Read 'docs/FONTS.md' for more instructions and details.
+ // - Read 'docs/FONTS.md' for more instructions and details. If you like the default font but want it to scale better, consider using the 'ProggyVector' from the same author!
// - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
+ //style.FontSizeBase = 20.0f;
//io.Fonts->AddFontDefault();
- //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf", 18.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
- //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, nullptr, io.Fonts->GetGlyphRangesJapanese());
+ //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf");
+ //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf");
//IM_ASSERT(font != nullptr);
// Our state
@@ -484,7 +491,7 @@ int main(int, char**)
if (fb_width > 0 && fb_height > 0 && (g_SwapChainRebuild || g_MainWindowData.Width != fb_width || g_MainWindowData.Height != fb_height))
{
ImGui_ImplVulkan_SetMinImageCount(g_MinImageCount);
- ImGui_ImplVulkanH_CreateOrResizeWindow(g_Instance, g_PhysicalDevice, g_Device, &g_MainWindowData, g_QueueFamily, g_Allocator, fb_width, fb_height, g_MinImageCount);
+ ImGui_ImplVulkanH_CreateOrResizeWindow(g_Instance, g_PhysicalDevice, g_Device, wd, g_QueueFamily, g_Allocator, fb_height, fb_height, g_MinImageCount, 0);
g_MainWindowData.FrameIndex = 0;
g_SwapChainRebuild = false;
}
diff --git a/examples/example_win32_directx10/main.cpp b/examples/example_win32_directx10/main.cpp
index a9e5e31fb..504502dac 100644
--- a/examples/example_win32_directx10/main.cpp
+++ b/examples/example_win32_directx10/main.cpp
@@ -1,4 +1,4 @@
-// Dear ImGui: standalone example application for DirectX 10
+// Dear ImGui: standalone example application for Windows API + DirectX 10
// Learn about Dear ImGui:
// - FAQ https://dearimgui.com/faq
@@ -30,11 +30,14 @@ LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
// Main code
int main(int, char**)
{
+ // Make process DPI aware and obtain main monitor scale
+ ImGui_ImplWin32_EnableDpiAwareness();
+ float main_scale = ImGui_ImplWin32_GetDpiScaleForMonitor(::MonitorFromPoint(POINT{ 0, 0 }, MONITOR_DEFAULTTOPRIMARY));
+
// Create application window
- //ImGui_ImplWin32_EnableDpiAwareness();
WNDCLASSEXW wc = { sizeof(wc), CS_CLASSDC, WndProc, 0L, 0L, GetModuleHandle(nullptr), nullptr, nullptr, nullptr, nullptr, L"ImGui Example", nullptr };
::RegisterClassExW(&wc);
- HWND hwnd = ::CreateWindowW(wc.lpszClassName, L"Dear ImGui DirectX10 Example", WS_OVERLAPPEDWINDOW, 100, 100, 1280, 800, nullptr, nullptr, wc.hInstance, nullptr);
+ HWND hwnd = ::CreateWindowW(wc.lpszClassName, L"Dear ImGui DirectX10 Example", WS_OVERLAPPEDWINDOW, 100, 100, (int)(1280 * main_scale), (int)(800 * main_scale), nullptr, nullptr, wc.hInstance, nullptr);
// Initialize Direct3D
if (!CreateDeviceD3D(hwnd))
@@ -63,8 +66,14 @@ int main(int, char**)
ImGui::StyleColorsDark();
//ImGui::StyleColorsLight();
- // When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
+ // Setup scaling
ImGuiStyle& style = ImGui::GetStyle();
+ style.ScaleAllSizes(main_scale); // Bake a fixed style scale. (until we have a solution for dynamic style scaling, changing this requires resetting Style + calling this again)
+ style.FontScaleDpi = main_scale; // Set initial font scale. (using io.ConfigDpiScaleFonts=true makes this unnecessary. We leave both here for documentation purpose)
+ io.ConfigDpiScaleFonts = true; // [Experimental] Automatically overwrite style.FontScaleDpi in Begin() when Monitor DPI changes. This will scale fonts but _NOT_ scale sizes/padding for now.
+ io.ConfigDpiScaleViewports = true; // [Experimental] Scale Dear ImGui and Platform Windows when Monitor DPI changes.
+
+ // When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{
style.WindowRounding = 0.0f;
@@ -79,16 +88,16 @@ int main(int, char**)
// - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
// - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
// - If the file cannot be loaded, the function will return a nullptr. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
- // - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
// - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use Freetype for higher quality font rendering.
- // - Read 'docs/FONTS.md' for more instructions and details.
+ // - Read 'docs/FONTS.md' for more instructions and details. If you like the default font but want it to scale better, consider using the 'ProggyVector' from the same author!
// - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
+ //style.FontSizeBase = 20.0f;
//io.Fonts->AddFontDefault();
- //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf", 18.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
- //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, nullptr, io.Fonts->GetGlyphRangesJapanese());
+ //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf");
+ //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf");
//IM_ASSERT(font != nullptr);
// Our state
@@ -232,6 +241,13 @@ bool CreateDeviceD3D(HWND hWnd)
if (res != S_OK)
return false;
+ // Disable DXGI's default Alt+Enter fullscreen behavior.
+ // - You are free to leave this enabled, but it will not work properly with multiple viewports.
+ // - This must be done for all windows associated to the device. Our DX11 backend does this automatically for secondary viewports that it creates.
+ IDXGIFactory* pSwapChainFactory;
+ if (SUCCEEDED(g_pSwapChain->GetParent(IID_PPV_ARGS(&pSwapChainFactory))))
+ pSwapChainFactory->MakeWindowAssociation(hWnd, DXGI_MWA_NO_ALT_ENTER);
+
CreateRenderTarget();
return true;
}
diff --git a/examples/example_win32_directx11/main.cpp b/examples/example_win32_directx11/main.cpp
index d25213bfd..16b7e2cf2 100644
--- a/examples/example_win32_directx11/main.cpp
+++ b/examples/example_win32_directx11/main.cpp
@@ -1,4 +1,4 @@
-// Dear ImGui: standalone example application for DirectX 11
+// Dear ImGui: standalone example application for Windows API + DirectX 11
// Learn about Dear ImGui:
// - FAQ https://dearimgui.com/faq
@@ -30,11 +30,14 @@ LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
// Main code
int main(int, char**)
{
+ // Make process DPI aware and obtain main monitor scale
+ ImGui_ImplWin32_EnableDpiAwareness();
+ float main_scale = ImGui_ImplWin32_GetDpiScaleForMonitor(::MonitorFromPoint(POINT{ 0, 0 }, MONITOR_DEFAULTTOPRIMARY));
+
// Create application window
- //ImGui_ImplWin32_EnableDpiAwareness();
WNDCLASSEXW wc = { sizeof(wc), CS_CLASSDC, WndProc, 0L, 0L, GetModuleHandle(nullptr), nullptr, nullptr, nullptr, nullptr, L"ImGui Example", nullptr };
::RegisterClassExW(&wc);
- HWND hwnd = ::CreateWindowW(wc.lpszClassName, L"Dear ImGui DirectX11 Example", WS_OVERLAPPEDWINDOW, 100, 100, 1280, 800, nullptr, nullptr, wc.hInstance, nullptr);
+ HWND hwnd = ::CreateWindowW(wc.lpszClassName, L"Dear ImGui DirectX11 Example", WS_OVERLAPPEDWINDOW, 100, 100, (int)(1280 * main_scale), (int)(800 * main_scale), nullptr, nullptr, wc.hInstance, nullptr);
// Initialize Direct3D
if (!CreateDeviceD3D(hwnd))
@@ -58,18 +61,21 @@ int main(int, char**)
io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows
//io.ConfigViewportsNoAutoMerge = true;
//io.ConfigViewportsNoTaskBarIcon = true;
- //io.ConfigViewportsNoDefaultParent = true;
//io.ConfigDockingAlwaysTabBar = true;
//io.ConfigDockingTransparentPayload = true;
- //io.ConfigFlags |= ImGuiConfigFlags_DpiEnableScaleFonts; // FIXME-DPI: Experimental. THIS CURRENTLY DOESN'T WORK AS EXPECTED. DON'T USE IN USER APP!
- //io.ConfigFlags |= ImGuiConfigFlags_DpiEnableScaleViewports; // FIXME-DPI: Experimental.
// Setup Dear ImGui style
ImGui::StyleColorsDark();
//ImGui::StyleColorsLight();
- // When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
+ // Setup scaling
ImGuiStyle& style = ImGui::GetStyle();
+ style.ScaleAllSizes(main_scale); // Bake a fixed style scale. (until we have a solution for dynamic style scaling, changing this requires resetting Style + calling this again)
+ style.FontScaleDpi = main_scale; // Set initial font scale. (using io.ConfigDpiScaleFonts=true makes this unnecessary. We leave both here for documentation purpose)
+ io.ConfigDpiScaleFonts = true; // [Experimental] Automatically overwrite style.FontScaleDpi in Begin() when Monitor DPI changes. This will scale fonts but _NOT_ scale sizes/padding for now.
+ io.ConfigDpiScaleViewports = true; // [Experimental] Scale Dear ImGui and Platform Windows when Monitor DPI changes.
+
+ // When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{
style.WindowRounding = 0.0f;
@@ -84,16 +90,16 @@ int main(int, char**)
// - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
// - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
// - If the file cannot be loaded, the function will return a nullptr. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
- // - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
// - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use Freetype for higher quality font rendering.
- // - Read 'docs/FONTS.md' for more instructions and details.
+ // - Read 'docs/FONTS.md' for more instructions and details. If you like the default font but want it to scale better, consider using the 'ProggyVector' from the same author!
// - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
+ //style.FontSizeBase = 20.0f;
//io.Fonts->AddFontDefault();
- //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf", 18.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
- //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, nullptr, io.Fonts->GetGlyphRangesJapanese());
+ //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf");
+ //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf");
//IM_ASSERT(font != nullptr);
// Our state
@@ -239,6 +245,13 @@ bool CreateDeviceD3D(HWND hWnd)
if (res != S_OK)
return false;
+ // Disable DXGI's default Alt+Enter fullscreen behavior.
+ // - You are free to leave this enabled, but it will not work properly with multiple viewports.
+ // - This must be done for all windows associated to the device. Our DX11 backend does this automatically for secondary viewports that it creates.
+ IDXGIFactory* pSwapChainFactory;
+ if (SUCCEEDED(g_pSwapChain->GetParent(IID_PPV_ARGS(&pSwapChainFactory))))
+ pSwapChainFactory->MakeWindowAssociation(hWnd, DXGI_MWA_NO_ALT_ENTER);
+
CreateRenderTarget();
return true;
}
@@ -264,10 +277,6 @@ void CleanupRenderTarget()
if (g_mainRenderTargetView) { g_mainRenderTargetView->Release(); g_mainRenderTargetView = nullptr; }
}
-#ifndef WM_DPICHANGED
-#define WM_DPICHANGED 0x02E0 // From Windows SDK 8.1+ headers
-#endif
-
// Forward declare message handler from imgui_impl_win32.cpp
extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
@@ -296,15 +305,6 @@ LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
case WM_DESTROY:
::PostQuitMessage(0);
return 0;
- case WM_DPICHANGED:
- if (ImGui::GetIO().ConfigFlags & ImGuiConfigFlags_DpiEnableScaleViewports)
- {
- //const int dpi = HIWORD(wParam);
- //printf("WM_DPICHANGED to %d (%.0f%%)\n", dpi, (float)dpi / 96.0f * 100.0f);
- const RECT* suggested_rect = (RECT*)lParam;
- ::SetWindowPos(hWnd, nullptr, suggested_rect->left, suggested_rect->top, suggested_rect->right - suggested_rect->left, suggested_rect->bottom - suggested_rect->top, SWP_NOZORDER | SWP_NOACTIVATE);
- }
- break;
}
return ::DefWindowProcW(hWnd, msg, wParam, lParam);
}
diff --git a/examples/example_win32_directx12/main.cpp b/examples/example_win32_directx12/main.cpp
index 5f22a4dad..e94ccaee1 100644
--- a/examples/example_win32_directx12/main.cpp
+++ b/examples/example_win32_directx12/main.cpp
@@ -1,4 +1,4 @@
-// Dear ImGui: standalone example application for DirectX 12
+// Dear ImGui: standalone example application for Windows API + DirectX 12
// Learn about Dear ImGui:
// - FAQ https://dearimgui.com/faq
@@ -10,7 +10,7 @@
#include "imgui_impl_win32.h"
#include "imgui_impl_dx12.h"
#include
-#include
+#include
#include
#ifdef _DEBUG
@@ -92,6 +92,7 @@ static ID3D12Fence* g_fence = nullptr;
static HANDLE g_fenceEvent = nullptr;
static UINT64 g_fenceLastSignaledValue = 0;
static IDXGISwapChain3* g_pSwapChain = nullptr;
+static bool g_SwapChainTearingSupport = false;
static bool g_SwapChainOccluded = false;
static HANDLE g_hSwapChainWaitableObject = nullptr;
static ID3D12Resource* g_mainRenderTargetResource[APP_NUM_BACK_BUFFERS] = {};
@@ -102,18 +103,21 @@ bool CreateDeviceD3D(HWND hWnd);
void CleanupDeviceD3D();
void CreateRenderTarget();
void CleanupRenderTarget();
-void WaitForLastSubmittedFrame();
-FrameContext* WaitForNextFrameResources();
+void WaitForPendingOperations();
+FrameContext* WaitForNextFrameContext();
LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
// Main code
int main(int, char**)
{
+ // Make process DPI aware and obtain main monitor scale
+ ImGui_ImplWin32_EnableDpiAwareness();
+ float main_scale = ImGui_ImplWin32_GetDpiScaleForMonitor(::MonitorFromPoint(POINT{ 0, 0 }, MONITOR_DEFAULTTOPRIMARY));
+
// Create application window
- //ImGui_ImplWin32_EnableDpiAwareness();
WNDCLASSEXW wc = { sizeof(wc), CS_CLASSDC, WndProc, 0L, 0L, GetModuleHandle(nullptr), nullptr, nullptr, nullptr, nullptr, L"ImGui Example", nullptr };
::RegisterClassExW(&wc);
- HWND hwnd = ::CreateWindowW(wc.lpszClassName, L"Dear ImGui DirectX12 Example", WS_OVERLAPPEDWINDOW, 100, 100, 1280, 800, nullptr, nullptr, wc.hInstance, nullptr);
+ HWND hwnd = ::CreateWindowW(wc.lpszClassName, L"Dear ImGui DirectX12 Example", WS_OVERLAPPEDWINDOW, 100, 100, (int)(1280 * main_scale), (int)(800 * main_scale), nullptr, nullptr, wc.hInstance, nullptr);
// Initialize Direct3D
if (!CreateDeviceD3D(hwnd))
@@ -142,8 +146,14 @@ int main(int, char**)
ImGui::StyleColorsDark();
//ImGui::StyleColorsLight();
- // When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
+ // Setup scaling
ImGuiStyle& style = ImGui::GetStyle();
+ style.ScaleAllSizes(main_scale); // Bake a fixed style scale. (until we have a solution for dynamic style scaling, changing this requires resetting Style + calling this again)
+ style.FontScaleDpi = main_scale; // Set initial font scale. (using io.ConfigDpiScaleFonts=true makes this unnecessary. We leave both here for documentation purpose)
+ io.ConfigDpiScaleFonts = true; // [Experimental] Automatically overwrite style.FontScaleDpi in Begin() when Monitor DPI changes. This will scale fonts but _NOT_ scale sizes/padding for now.
+ io.ConfigDpiScaleViewports = true; // [Experimental] Scale Dear ImGui and Platform Windows when Monitor DPI changes.
+
+ // When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{
style.WindowRounding = 0.0f;
@@ -173,16 +183,16 @@ int main(int, char**)
// - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
// - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
// - If the file cannot be loaded, the function will return a nullptr. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
- // - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
// - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use Freetype for higher quality font rendering.
- // - Read 'docs/FONTS.md' for more instructions and details.
+ // - Read 'docs/FONTS.md' for more instructions and details. If you like the default font but want it to scale better, consider using the 'ProggyVector' from the same author!
// - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
+ //style.FontSizeBase = 20.0f;
//io.Fonts->AddFontDefault();
- //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf", 18.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
- //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, nullptr, io.Fonts->GetGlyphRangesJapanese());
+ //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf");
+ //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf");
//IM_ASSERT(font != nullptr);
// Our state
@@ -208,7 +218,7 @@ int main(int, char**)
break;
// Handle window screen locked
- if (g_SwapChainOccluded && g_pSwapChain->Present(0, DXGI_PRESENT_TEST) == DXGI_STATUS_OCCLUDED)
+ if ((g_SwapChainOccluded && g_pSwapChain->Present(0, DXGI_PRESENT_TEST) == DXGI_STATUS_OCCLUDED) || ::IsIconic(hwnd))
{
::Sleep(10);
continue;
@@ -260,7 +270,7 @@ int main(int, char**)
// Rendering
ImGui::Render();
- FrameContext* frameCtx = WaitForNextFrameResources();
+ FrameContext* frameCtx = WaitForNextFrameContext();
UINT backBufferIdx = g_pSwapChain->GetCurrentBackBufferIndex();
frameCtx->CommandAllocator->Reset();
@@ -294,18 +304,17 @@ int main(int, char**)
ImGui::RenderPlatformWindowsDefault();
}
+ g_pd3dCommandQueue->Signal(g_fence, ++g_fenceLastSignaledValue);
+ frameCtx->FenceValue = g_fenceLastSignaledValue;
+
// Present
HRESULT hr = g_pSwapChain->Present(1, 0); // Present with vsync
- //HRESULT hr = g_pSwapChain->Present(0, 0); // Present without vsync
+ //HRESULT hr = g_pSwapChain->Present(0, g_SwapChainTearingSupport ? DXGI_PRESENT_ALLOW_TEARING : 0); // Present without vsync
g_SwapChainOccluded = (hr == DXGI_STATUS_OCCLUDED);
-
- UINT64 fenceValue = g_fenceLastSignaledValue + 1;
- g_pd3dCommandQueue->Signal(g_fence, fenceValue);
- g_fenceLastSignaledValue = fenceValue;
- frameCtx->FenceValue = fenceValue;
+ g_frameIndex++;
}
- WaitForLastSubmittedFrame();
+ WaitForPendingOperations();
// Cleanup
ImGui_ImplDX12_Shutdown();
@@ -419,14 +428,24 @@ bool CreateDeviceD3D(HWND hWnd)
return false;
{
- IDXGIFactory4* dxgiFactory = nullptr;
+ IDXGIFactory5* dxgiFactory = nullptr;
IDXGISwapChain1* swapChain1 = nullptr;
if (CreateDXGIFactory1(IID_PPV_ARGS(&dxgiFactory)) != S_OK)
return false;
+
+ BOOL allow_tearing = FALSE;
+ dxgiFactory->CheckFeatureSupport(DXGI_FEATURE_PRESENT_ALLOW_TEARING, &allow_tearing, sizeof(allow_tearing));
+ g_SwapChainTearingSupport = (allow_tearing == TRUE);
+ if (g_SwapChainTearingSupport)
+ sd.Flags |= DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING;
+
if (dxgiFactory->CreateSwapChainForHwnd(g_pd3dCommandQueue, hWnd, &sd, nullptr, nullptr, &swapChain1) != S_OK)
return false;
if (swapChain1->QueryInterface(IID_PPV_ARGS(&g_pSwapChain)) != S_OK)
return false;
+ if (g_SwapChainTearingSupport)
+ dxgiFactory->MakeWindowAssociation(hWnd, DXGI_MWA_NO_ALT_ENTER);
+
swapChain1->Release();
dxgiFactory->Release();
g_pSwapChain->SetMaximumFrameLatency(APP_NUM_BACK_BUFFERS);
@@ -475,49 +494,33 @@ void CreateRenderTarget()
void CleanupRenderTarget()
{
- WaitForLastSubmittedFrame();
+ WaitForPendingOperations();
for (UINT i = 0; i < APP_NUM_BACK_BUFFERS; i++)
if (g_mainRenderTargetResource[i]) { g_mainRenderTargetResource[i]->Release(); g_mainRenderTargetResource[i] = nullptr; }
}
-void WaitForLastSubmittedFrame()
+void WaitForPendingOperations()
{
- FrameContext* frameCtx = &g_frameContext[g_frameIndex % APP_NUM_FRAMES_IN_FLIGHT];
+ g_pd3dCommandQueue->Signal(g_fence, ++g_fenceLastSignaledValue);
- UINT64 fenceValue = frameCtx->FenceValue;
- if (fenceValue == 0)
- return; // No fence was signaled
-
- frameCtx->FenceValue = 0;
- if (g_fence->GetCompletedValue() >= fenceValue)
- return;
-
- g_fence->SetEventOnCompletion(fenceValue, g_fenceEvent);
- WaitForSingleObject(g_fenceEvent, INFINITE);
+ g_fence->SetEventOnCompletion(g_fenceLastSignaledValue, g_fenceEvent);
+ ::WaitForSingleObject(g_fenceEvent, INFINITE);
}
-FrameContext* WaitForNextFrameResources()
+FrameContext* WaitForNextFrameContext()
{
- UINT nextFrameIndex = g_frameIndex + 1;
- g_frameIndex = nextFrameIndex;
-
- HANDLE waitableObjects[] = { g_hSwapChainWaitableObject, nullptr };
- DWORD numWaitableObjects = 1;
-
- FrameContext* frameCtx = &g_frameContext[nextFrameIndex % APP_NUM_FRAMES_IN_FLIGHT];
- UINT64 fenceValue = frameCtx->FenceValue;
- if (fenceValue != 0) // means no fence was signaled
+ FrameContext* frame_context = &g_frameContext[g_frameIndex % APP_NUM_FRAMES_IN_FLIGHT];
+ if (g_fence->GetCompletedValue() < frame_context->FenceValue)
{
- frameCtx->FenceValue = 0;
- g_fence->SetEventOnCompletion(fenceValue, g_fenceEvent);
- waitableObjects[1] = g_fenceEvent;
- numWaitableObjects = 2;
+ g_fence->SetEventOnCompletion(frame_context->FenceValue, g_fenceEvent);
+ HANDLE waitableObjects[] = { g_hSwapChainWaitableObject, g_fenceEvent };
+ ::WaitForMultipleObjects(2, waitableObjects, TRUE, INFINITE);
}
+ else
+ ::WaitForSingleObject(g_hSwapChainWaitableObject, INFINITE);
- WaitForMultipleObjects(numWaitableObjects, waitableObjects, TRUE, INFINITE);
-
- return frameCtx;
+ return frame_context;
}
// Forward declare message handler from imgui_impl_win32.cpp
@@ -538,9 +541,10 @@ LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
case WM_SIZE:
if (g_pd3dDevice != nullptr && wParam != SIZE_MINIMIZED)
{
- WaitForLastSubmittedFrame();
CleanupRenderTarget();
- HRESULT result = g_pSwapChain->ResizeBuffers(0, (UINT)LOWORD(lParam), (UINT)HIWORD(lParam), DXGI_FORMAT_UNKNOWN, DXGI_SWAP_CHAIN_FLAG_FRAME_LATENCY_WAITABLE_OBJECT);
+ DXGI_SWAP_CHAIN_DESC1 desc = {};
+ g_pSwapChain->GetDesc1(&desc);
+ HRESULT result = g_pSwapChain->ResizeBuffers(0, (UINT)LOWORD(lParam), (UINT)HIWORD(lParam), desc.Format, desc.Flags);
assert(SUCCEEDED(result) && "Failed to resize swapchain.");
CreateRenderTarget();
}
diff --git a/examples/example_win32_directx9/main.cpp b/examples/example_win32_directx9/main.cpp
index 8063636d7..e1cd0fee6 100644
--- a/examples/example_win32_directx9/main.cpp
+++ b/examples/example_win32_directx9/main.cpp
@@ -1,4 +1,4 @@
-// Dear ImGui: standalone example application for DirectX 9
+// Dear ImGui: standalone example application for Windows API + DirectX 9
// Learn about Dear ImGui:
// - FAQ https://dearimgui.com/faq
@@ -28,11 +28,14 @@ LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
// Main code
int main(int, char**)
{
+ // Make process DPI aware and obtain main monitor scale
+ ImGui_ImplWin32_EnableDpiAwareness();
+ float main_scale = ImGui_ImplWin32_GetDpiScaleForMonitor(::MonitorFromPoint(POINT{ 0, 0 }, MONITOR_DEFAULTTOPRIMARY));
+
// Create application window
- //ImGui_ImplWin32_EnableDpiAwareness();
WNDCLASSEXW wc = { sizeof(wc), CS_CLASSDC, WndProc, 0L, 0L, GetModuleHandle(nullptr), nullptr, nullptr, nullptr, nullptr, L"ImGui Example", nullptr };
::RegisterClassExW(&wc);
- HWND hwnd = ::CreateWindowW(wc.lpszClassName, L"Dear ImGui DirectX9 Example", WS_OVERLAPPEDWINDOW, 100, 100, 1280, 800, nullptr, nullptr, wc.hInstance, nullptr);
+ HWND hwnd = ::CreateWindowW(wc.lpszClassName, L"Dear ImGui DirectX9 Example", WS_OVERLAPPEDWINDOW, 100, 100, (int)(1280 * main_scale), (int)(800 * main_scale), nullptr, nullptr, wc.hInstance, nullptr);
// Initialize Direct3D
if (!CreateDeviceD3D(hwnd))
@@ -61,8 +64,14 @@ int main(int, char**)
ImGui::StyleColorsDark();
//ImGui::StyleColorsLight();
- // When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
+ // Setup scaling
ImGuiStyle& style = ImGui::GetStyle();
+ style.ScaleAllSizes(main_scale); // Bake a fixed style scale. (until we have a solution for dynamic style scaling, changing this requires resetting Style + calling this again)
+ style.FontScaleDpi = main_scale; // Set initial font scale. (using io.ConfigDpiScaleFonts=true makes this unnecessary. We leave both here for documentation purpose)
+ io.ConfigDpiScaleFonts = true; // [Experimental] Automatically overwrite style.FontScaleDpi in Begin() when Monitor DPI changes. This will scale fonts but _NOT_ scale sizes/padding for now.
+ io.ConfigDpiScaleViewports = true; // [Experimental] Scale Dear ImGui and Platform Windows when Monitor DPI changes.
+
+ // When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{
style.WindowRounding = 0.0f;
@@ -77,16 +86,16 @@ int main(int, char**)
// - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
// - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
// - If the file cannot be loaded, the function will return a nullptr. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
- // - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
// - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use Freetype for higher quality font rendering.
- // - Read 'docs/FONTS.md' for more instructions and details.
+ // - Read 'docs/FONTS.md' for more instructions and details. If you like the default font but want it to scale better, consider using the 'ProggyVector' from the same author!
// - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
+ //style.FontSizeBase = 20.0f;
//io.Fonts->AddFontDefault();
- //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf", 18.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
- //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, nullptr, io.Fonts->GetGlyphRangesJapanese());
+ //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf");
+ //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf");
//IM_ASSERT(font != nullptr);
// Our state
@@ -250,10 +259,6 @@ void ResetDevice()
ImGui_ImplDX9_CreateDeviceObjects();
}
-#ifndef WM_DPICHANGED
-#define WM_DPICHANGED 0x02E0 // From Windows SDK 8.1+ headers
-#endif
-
// Forward declare message handler from imgui_impl_win32.cpp
extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
@@ -282,15 +287,6 @@ LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
case WM_DESTROY:
::PostQuitMessage(0);
return 0;
- case WM_DPICHANGED:
- if (ImGui::GetIO().ConfigFlags & ImGuiConfigFlags_DpiEnableScaleViewports)
- {
- //const int dpi = HIWORD(wParam);
- //printf("WM_DPICHANGED to %d (%.0f%%)\n", dpi, (float)dpi / 96.0f * 100.0f);
- const RECT* suggested_rect = (RECT*)lParam;
- ::SetWindowPos(hWnd, nullptr, suggested_rect->left, suggested_rect->top, suggested_rect->right - suggested_rect->left, suggested_rect->bottom - suggested_rect->top, SWP_NOZORDER | SWP_NOACTIVATE);
- }
- break;
}
return ::DefWindowProcW(hWnd, msg, wParam, lParam);
}
diff --git a/examples/example_win32_opengl3/main.cpp b/examples/example_win32_opengl3/main.cpp
index ffc6e3da0..97b876ac0 100644
--- a/examples/example_win32_opengl3/main.cpp
+++ b/examples/example_win32_opengl3/main.cpp
@@ -1,4 +1,4 @@
-// Dear ImGui: standalone example application for Win32 + OpenGL 3
+// Dear ImGui: standalone example application for Windows API + OpenGL
// Learn about Dear ImGui:
// - FAQ https://dearimgui.com/faq
@@ -15,7 +15,7 @@
#define WIN32_LEAN_AND_MEAN
#endif
#include
-#include
+#include
#include
// Data stored per platform window
@@ -72,11 +72,14 @@ static void Hook_Renderer_SwapBuffers(ImGuiViewport* viewport, void*)
// Main code
int main(int, char**)
{
+ // Make process DPI aware and obtain main monitor scale
+ //ImGui_ImplWin32_EnableDpiAwareness(); // FIXME: This somehow doesn't work in the Win32+OpenGL example. Why?
+ float main_scale = ImGui_ImplWin32_GetDpiScaleForMonitor(::MonitorFromPoint(POINT{ 0, 0 }, MONITOR_DEFAULTTOPRIMARY));
+
// Create application window
- //ImGui_ImplWin32_EnableDpiAwareness();
WNDCLASSEXW wc = { sizeof(wc), CS_OWNDC, WndProc, 0L, 0L, GetModuleHandle(nullptr), nullptr, nullptr, nullptr, nullptr, L"ImGui Example", nullptr };
::RegisterClassExW(&wc);
- HWND hwnd = ::CreateWindowW(wc.lpszClassName, L"Dear ImGui Win32+OpenGL3 Example", WS_OVERLAPPEDWINDOW, 100, 100, 1280, 800, nullptr, nullptr, wc.hInstance, nullptr);
+ HWND hwnd = ::CreateWindowW(wc.lpszClassName, L"Dear ImGui Win32+OpenGL3 Example", WS_OVERLAPPEDWINDOW, 100, 100, (int)(1280 * main_scale), (int)(800 * main_scale), nullptr, nullptr, wc.hInstance, nullptr);
// Initialize OpenGL
if (!CreateDeviceWGL(hwnd, &g_MainWindow))
@@ -105,8 +108,14 @@ int main(int, char**)
ImGui::StyleColorsDark();
//ImGui::StyleColorsClassic();
- // When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
+ // Setup scaling
ImGuiStyle& style = ImGui::GetStyle();
+ style.ScaleAllSizes(main_scale); // Bake a fixed style scale. (until we have a solution for dynamic style scaling, changing this requires resetting Style + calling this again)
+ style.FontScaleDpi = main_scale; // Set initial font scale. (using io.ConfigDpiScaleFonts=true makes this unnecessary. We leave both here for documentation purpose)
+ io.ConfigDpiScaleFonts = true; // [Experimental] Automatically overwrite style.FontScaleDpi in Begin() when Monitor DPI changes. This will scale fonts but _NOT_ scale sizes/padding for now.
+ io.ConfigDpiScaleViewports = true; // [Experimental] Scale Dear ImGui and Platform Windows when Monitor DPI changes.
+
+ // When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{
style.WindowRounding = 0.0f;
@@ -135,16 +144,16 @@ int main(int, char**)
// - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
// - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
// - If the file cannot be loaded, the function will return a nullptr. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
- // - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
// - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use Freetype for higher quality font rendering.
- // - Read 'docs/FONTS.md' for more instructions and details.
+ // - Read 'docs/FONTS.md' for more instructions and details. If you like the default font but want it to scale better, consider using the 'ProggyVector' from the same author!
// - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
+ //style.FontSizeBase = 20.0f;
//io.Fonts->AddFontDefault();
- //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf", 18.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
- //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, nullptr, io.Fonts->GetGlyphRangesJapanese());
+ //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf");
+ //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf");
//IM_ASSERT(font != nullptr);
// Our state
diff --git a/examples/example_win32_vulkan/main.cpp b/examples/example_win32_vulkan/main.cpp
index 6be7bc5a5..c8b455a47 100644
--- a/examples/example_win32_vulkan/main.cpp
+++ b/examples/example_win32_vulkan/main.cpp
@@ -1,4 +1,4 @@
-// Dear ImGui: standalone example application for Win32 + Vulkan
+// Dear ImGui: standalone example application for Windows API + Vulkan
// Learn about Dear ImGui:
// - FAQ https://dearimgui.com/faq
@@ -228,7 +228,7 @@ static void SetupVulkanWindow(ImGui_ImplVulkanH_Window* wd, VkSurfaceKHR surface
// Create SwapChain, RenderPass, Framebuffer, etc.
IM_ASSERT(g_MinImageCount >= 2);
- ImGui_ImplVulkanH_CreateOrResizeWindow(g_Instance, g_PhysicalDevice, g_Device, wd, g_QueueFamily, g_Allocator, width, height, g_MinImageCount);
+ ImGui_ImplVulkanH_CreateOrResizeWindow(g_Instance, g_PhysicalDevice, g_Device, wd, g_QueueFamily, g_Allocator, width, height, g_MinImageCount, 0);
}
static void CleanupVulkan()
@@ -352,11 +352,14 @@ static int ImGui_ImplWin32_CreateVkSurface(ImGuiViewport* viewport, ImU64 vk_ins
// Main code
int main(int, char**)
{
+ // Make process DPI aware and obtain main monitor scale
+ ImGui_ImplWin32_EnableDpiAwareness();
+ float main_scale = ImGui_ImplWin32_GetDpiScaleForMonitor(::MonitorFromPoint(POINT{ 0, 0 }, MONITOR_DEFAULTTOPRIMARY));
+
// Create application window
- //ImGui_ImplWin32_EnableDpiAwareness();
WNDCLASSEXW wc = { sizeof(wc), CS_CLASSDC, WndProc, 0L, 0L, GetModuleHandle(nullptr), nullptr, nullptr, nullptr, nullptr, L"ImGui Example", nullptr };
::RegisterClassExW(&wc);
- HWND hwnd = ::CreateWindowW(wc.lpszClassName, L"Dear ImGui Win32+Vulkan Example", WS_OVERLAPPEDWINDOW, 100, 100, 1280, 800, nullptr, nullptr, wc.hInstance, nullptr);
+ HWND hwnd = ::CreateWindowW(wc.lpszClassName, L"Dear ImGui Win32+Vulkan Example", WS_OVERLAPPEDWINDOW, 100, 100, (int)(1280 * main_scale), (int)(800 * main_scale), nullptr, nullptr, wc.hInstance, nullptr);
ImVector extensions;
extensions.push_back("VK_KHR_surface");
@@ -379,7 +382,7 @@ int main(int, char**)
// Show the window
// FIXME: Retrieve client size from window itself.
ImGui_ImplVulkanH_Window* wd = &g_MainWindowData;
- SetupVulkanWindow(wd, surface, 1280, 800);
+ SetupVulkanWindow(wd, surface, (int)(1280 * main_scale), (int)(800 * main_scale));
::ShowWindow(hwnd, SW_SHOWDEFAULT);
::UpdateWindow(hwnd);
@@ -398,8 +401,14 @@ int main(int, char**)
ImGui::StyleColorsDark();
//ImGui::StyleColorsLight();
- // When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
+ // Setup scaling
ImGuiStyle& style = ImGui::GetStyle();
+ style.ScaleAllSizes(main_scale); // Bake a fixed style scale. (until we have a solution for dynamic style scaling, changing this requires resetting Style + calling this again)
+ style.FontScaleDpi = main_scale; // Set initial font scale. (using io.ConfigDpiScaleFonts=true makes this unnecessary. We leave both here for documentation purpose)
+ io.ConfigDpiScaleFonts = true; // [Experimental] Automatically overwrite style.FontScaleDpi in Begin() when Monitor DPI changes. This will scale fonts but _NOT_ scale sizes/padding for now.
+ io.ConfigDpiScaleViewports = true; // [Experimental] Scale Dear ImGui and Platform Windows when Monitor DPI changes.
+
+ // When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{
style.WindowRounding = 0.0f;
@@ -419,12 +428,12 @@ int main(int, char**)
init_info.Queue = g_Queue;
init_info.PipelineCache = g_PipelineCache;
init_info.DescriptorPool = g_DescriptorPool;
- init_info.RenderPass = wd->RenderPass;
- init_info.Subpass = 0;
init_info.MinImageCount = g_MinImageCount;
init_info.ImageCount = wd->ImageCount;
- init_info.MSAASamples = VK_SAMPLE_COUNT_1_BIT;
init_info.Allocator = g_Allocator;
+ init_info.PipelineInfoMain.RenderPass = wd->RenderPass;
+ init_info.PipelineInfoMain.Subpass = 0;
+ init_info.PipelineInfoMain.MSAASamples = VK_SAMPLE_COUNT_1_BIT;
init_info.CheckVkResultFn = check_vk_result;
ImGui_ImplVulkan_Init(&init_info);
@@ -432,16 +441,16 @@ int main(int, char**)
// - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
// - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
// - If the file cannot be loaded, the function will return a nullptr. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
- // - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
// - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use Freetype for higher quality font rendering.
- // - Read 'docs/FONTS.md' for more instructions and details.
+ // - Read 'docs/FONTS.md' for more instructions and details. If you like the default font but want it to scale better, consider using the 'ProggyVector' from the same author!
// - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
+ //style.FontSizeBase = 20.0f;
//io.Fonts->AddFontDefault();
- //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf", 18.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
- //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
- //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, nullptr, io.Fonts->GetGlyphRangesJapanese());
+ //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf");
+ //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf");
+ //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf");
//IM_ASSERT(font != nullptr);
// Our state
@@ -573,7 +582,7 @@ LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
if (fb_width > 0 && fb_height > 0 && (g_SwapChainRebuild || g_MainWindowData.Width != fb_width || g_MainWindowData.Height != fb_height))
{
ImGui_ImplVulkan_SetMinImageCount(g_MinImageCount);
- ImGui_ImplVulkanH_CreateOrResizeWindow(g_Instance, g_PhysicalDevice, g_Device, &g_MainWindowData, g_QueueFamily, g_Allocator, fb_width, fb_height, g_MinImageCount);
+ ImGui_ImplVulkanH_CreateOrResizeWindow(g_Instance, g_PhysicalDevice, g_Device, &g_MainWindowData, g_QueueFamily, g_Allocator, fb_width, fb_height, g_MinImageCount, 0);
g_MainWindowData.FrameIndex = 0;
g_SwapChainRebuild = false;
}
diff --git a/examples/imgui_examples.sln b/examples/imgui_examples.sln
index cf1c5ad50..8ad1a2566 100644
--- a/examples/imgui_examples.sln
+++ b/examples/imgui_examples.sln
@@ -39,6 +39,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "example_win32_vulkan", "exa
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "example_sdl3_sdlgpu3", "example_sdl3_sdlgpu3\example_sdl3_sdlgpu3.vcxproj", "{C22CB6F8-39A5-4DDA-90ED-4ACA4E81E1E5}"
EndProject
+Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "example_sdl3_directx11", "example_sdl3_directx11\example_sdl3_directx11.vcxproj", "{009DAC16-1A9C-47BE-9770-A30A046E8090}"
+EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Win32 = Debug|Win32
@@ -191,6 +193,14 @@ Global
{C22CB6F8-39A5-4DDA-90ED-4ACA4E81E1E5}.Release|Win32.Build.0 = Release|Win32
{C22CB6F8-39A5-4DDA-90ED-4ACA4E81E1E5}.Release|x64.ActiveCfg = Release|x64
{C22CB6F8-39A5-4DDA-90ED-4ACA4E81E1E5}.Release|x64.Build.0 = Release|x64
+ {009DAC16-1A9C-47BE-9770-A30A046E8090}.Debug|Win32.ActiveCfg = Debug|Win32
+ {009DAC16-1A9C-47BE-9770-A30A046E8090}.Debug|Win32.Build.0 = Debug|Win32
+ {009DAC16-1A9C-47BE-9770-A30A046E8090}.Debug|x64.ActiveCfg = Debug|x64
+ {009DAC16-1A9C-47BE-9770-A30A046E8090}.Debug|x64.Build.0 = Debug|x64
+ {009DAC16-1A9C-47BE-9770-A30A046E8090}.Release|Win32.ActiveCfg = Release|Win32
+ {009DAC16-1A9C-47BE-9770-A30A046E8090}.Release|Win32.Build.0 = Release|Win32
+ {009DAC16-1A9C-47BE-9770-A30A046E8090}.Release|x64.ActiveCfg = Release|x64
+ {009DAC16-1A9C-47BE-9770-A30A046E8090}.Release|x64.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
diff --git a/imconfig.h b/imconfig.h
index a1e29e849..4dab1b604 100644
--- a/imconfig.h
+++ b/imconfig.h
@@ -129,6 +129,10 @@
//#define IM_DEBUG_BREAK IM_ASSERT(0)
//#define IM_DEBUG_BREAK __debugbreak()
+//---- Debug Tools: Enable highlight ID conflicts _before_ hovering items. When io.ConfigDebugHighlightIdConflicts is set.
+// (THIS WILL SLOW DOWN DEAR IMGUI. Only use occasionally and disable after use)
+//#define IMGUI_DEBUG_HIGHLIGHT_ALL_ID_CONFLICTS
+
//---- Debug Tools: Enable slower asserts
//#define IMGUI_DEBUG_PARANOID
diff --git a/imgui.cpp b/imgui.cpp
index 654b4dd2f..4a1e25cfe 100644
--- a/imgui.cpp
+++ b/imgui.cpp
@@ -1,4 +1,4 @@
-// dear imgui, v1.92.0 WIP
+// dear imgui, v1.92.4 WIP
// (main code and documentation)
// Help:
@@ -21,9 +21,10 @@
// - Issues & support ........... https://github.com/ocornut/imgui/issues
// - Test Engine & Automation ... https://github.com/ocornut/imgui_test_engine (test suite, test engine to automate your apps)
-// For first-time users having issues compiling/linking/running/loading fonts:
+// For first-time users having issues compiling/linking/running:
// please post in https://github.com/ocornut/imgui/discussions if you cannot find a solution in resources above.
// Everything else should be asked in 'Issues'! We are building a database of cross-linked knowledge there.
+// Since 1.92, we encourage font loading questions to also be posted in 'Issues'.
// Copyright (c) 2014-2025 Omar Cornut
// Developed by Omar Cornut and every direct or indirect contributors to the GitHub.
@@ -52,7 +53,7 @@ DOCUMENTATION
- HOW TO UPDATE TO A NEWER VERSION OF DEAR IMGUI
- GETTING STARTED WITH INTEGRATING DEAR IMGUI IN YOUR CODE/ENGINE
- HOW A SIMPLE APPLICATION MAY LOOK LIKE
- - HOW A SIMPLE RENDERING FUNCTION MAY LOOK LIKE
+ - USING CUSTOM BACKEND / CUSTOM ENGINE
- API BREAKING CHANGES (read me when you update!)
- FREQUENTLY ASKED QUESTIONS (FAQ)
- Read all answers online: https://www.dearimgui.com/faq, or in docs/FAQ.md (with a Markdown viewer)
@@ -77,6 +78,7 @@ CODE
// [SECTION] RENDER HELPERS
// [SECTION] INITIALIZATION, SHUTDOWN
// [SECTION] MAIN CODE (most of the code! lots of stuff, needs tidying up!)
+// [SECTION] FONTS, TEXTURES
// [SECTION] ID STACK
// [SECTION] INPUTS
// [SECTION] ERROR CHECKING, STATE RECOVERY
@@ -273,7 +275,8 @@ CODE
HOW A SIMPLE APPLICATION MAY LOOK LIKE
--------------------------------------
- EXHIBIT 1: USING THE EXAMPLE BACKENDS (= imgui_impl_XXX.cpp files from the backends/ folder).
+
+ USING THE EXAMPLE BACKENDS (= imgui_impl_XXX.cpp files from the backends/ folder).
The sub-folders in examples/ contain examples applications following this structure.
// Application init: create a dear imgui context, setup some options, load fonts
@@ -298,7 +301,7 @@ CODE
// Any application code here
ImGui::Text("Hello, world!");
- // Render dear imgui into screen
+ // Render dear imgui into framebuffer
ImGui::Render();
ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
g_pSwapChain->Present(1, 0);
@@ -309,26 +312,36 @@ CODE
ImGui_ImplWin32_Shutdown();
ImGui::DestroyContext();
- EXHIBIT 2: IMPLEMENTING CUSTOM BACKEND / CUSTOM ENGINE
+ To decide whether to dispatch mouse/keyboard inputs to Dear ImGui to the rest of your application,
+ you should read the 'io.WantCaptureMouse', 'io.WantCaptureKeyboard' and 'io.WantTextInput' flags!
+ Please read the FAQ entry "How can I tell whether to dispatch mouse/keyboard to Dear ImGui or my application?" about this.
- // Application init: create a dear imgui context, setup some options, load fonts
+
+USING CUSTOM BACKEND / CUSTOM ENGINE
+------------------------------------
+
+IMPLEMENTING YOUR PLATFORM BACKEND:
+ -> see https://github.com/ocornut/imgui/blob/master/docs/BACKENDS.md for basic instructions.
+ -> the Platform backends in impl_impl_XXX.cpp files contain many implementations.
+
+IMPLEMENTING YOUR RenderDrawData() function:
+ -> see https://github.com/ocornut/imgui/blob/master/docs/BACKENDS.md
+ -> the Renderer Backends in impl_impl_XXX.cpp files contain many implementations of a ImGui_ImplXXXX_RenderDrawData() function.
+
+IMPLEMENTING SUPPORT for ImGuiBackendFlags_RendererHasTextures:
+ -> see https://github.com/ocornut/imgui/blob/master/docs/BACKENDS.md
+ -> the Renderer Backends in impl_impl_XXX.cpp files contain many implementations of a ImGui_ImplXXXX_UpdateTexture() function.
+
+ Basic application/backend skeleton:
+
+ // Application init: create a Dear ImGui context, setup some options, load fonts
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
- // TODO: Set optional io.ConfigFlags values, e.g. 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard' to enable keyboard controls.
- // TODO: Fill optional fields of the io structure later.
+ // TODO: set io.ConfigXXX values, e.g.
+ io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable keyboard controls
+
// TODO: Load TTF/OTF fonts if you don't want to use the default font.
-
- // Build and load the texture atlas into a texture
- // (In the examples/ app this is usually done within the ImGui_ImplXXX_Init() function from one of the demo Renderer)
- int width, height;
- unsigned char* pixels = nullptr;
- io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
-
- // At this point you've got the texture data and you need to upload that to your graphic system:
- // After we have created the texture, store its pointer/identifier (_in whichever format your engine uses_) in 'io.Fonts->TexID'.
- // This will be passed back to your via the renderer. Basically ImTextureID == void*. Read FAQ for details about ImTextureID.
- MyTexture* texture = MyEngine::CreateTextureFromMemoryPixels(pixels, width, height, TEXTURE_TYPE_RGBA32)
- io.Fonts->SetTexID((void*)texture);
+ io.Fonts->AddFontFromFileTTF("NotoSans.ttf");
// Application main loop
while (true)
@@ -351,77 +364,25 @@ CODE
MyGameUpdate(); // may use any Dear ImGui functions, e.g. ImGui::Begin("My window"); ImGui::Text("Hello, world!"); ImGui::End();
MyGameRender(); // may use any Dear ImGui functions as well!
- // Render dear imgui, swap buffers
+ // End the dear imgui frame
// (You want to try calling EndFrame/Render as late as you can, to be able to use Dear ImGui in your own game rendering code)
- ImGui::EndFrame();
+ ImGui::EndFrame(); // this is automatically called by Render(), but available
ImGui::Render();
+
+ // Update textures
ImDrawData* draw_data = ImGui::GetDrawData();
- MyImGuiRenderFunction(draw_data);
+ for (ImTextureData* tex : *draw_data->Textures)
+ if (tex->Status != ImTextureStatus_OK)
+ MyImGuiBackend_UpdateTexture(tex);
+
+ // Render dear imgui contents, swap buffers
+ MyImGuiBackend_RenderDrawData(draw_data);
SwapBuffers();
}
// Shutdown
ImGui::DestroyContext();
- To decide whether to dispatch mouse/keyboard inputs to Dear ImGui to the rest of your application,
- you should read the 'io.WantCaptureMouse', 'io.WantCaptureKeyboard' and 'io.WantTextInput' flags!
- Please read the FAQ entry "How can I tell whether to dispatch mouse/keyboard to Dear ImGui or my application?" about this.
-
-
- HOW A SIMPLE RENDERING FUNCTION MAY LOOK LIKE
- ---------------------------------------------
- The backends in impl_impl_XXX.cpp files contain many working implementations of a rendering function.
-
- void MyImGuiRenderFunction(ImDrawData* draw_data)
- {
- // TODO: Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled
- // TODO: Setup texture sampling state: sample with bilinear filtering (NOT point/nearest filtering). Use 'io.Fonts->Flags |= ImFontAtlasFlags_NoBakedLines;' to allow point/nearest filtering.
- // TODO: Setup viewport covering draw_data->DisplayPos to draw_data->DisplayPos + draw_data->DisplaySize
- // TODO: Setup orthographic projection matrix cover draw_data->DisplayPos to draw_data->DisplayPos + draw_data->DisplaySize
- // TODO: Setup shader: vertex { float2 pos, float2 uv, u32 color }, fragment shader sample color from 1 texture, multiply by vertex color.
- ImVec2 clip_off = draw_data->DisplayPos;
- for (int n = 0; n < draw_data->CmdListsCount; n++)
- {
- const ImDrawList* cmd_list = draw_data->CmdLists[n];
- const ImDrawVert* vtx_buffer = cmd_list->VtxBuffer.Data; // vertex buffer generated by Dear ImGui
- const ImDrawIdx* idx_buffer = cmd_list->IdxBuffer.Data; // index buffer generated by Dear ImGui
- for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++)
- {
- const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
- if (pcmd->UserCallback)
- {
- pcmd->UserCallback(cmd_list, pcmd);
- }
- else
- {
- // Project scissor/clipping rectangles into framebuffer space
- ImVec2 clip_min(pcmd->ClipRect.x - clip_off.x, pcmd->ClipRect.y - clip_off.y);
- ImVec2 clip_max(pcmd->ClipRect.z - clip_off.x, pcmd->ClipRect.w - clip_off.y);
- if (clip_max.x <= clip_min.x || clip_max.y <= clip_min.y)
- continue;
-
- // We are using scissoring to clip some objects. All low-level graphics API should support it.
- // - If your engine doesn't support scissoring yet, you may ignore this at first. You will get some small glitches
- // (some elements visible outside their bounds) but you can fix that once everything else works!
- // - Clipping coordinates are provided in imgui coordinates space:
- // - For a given viewport, draw_data->DisplayPos == viewport->Pos and draw_data->DisplaySize == viewport->Size
- // - In a single viewport application, draw_data->DisplayPos == (0,0) and draw_data->DisplaySize == io.DisplaySize, but always use GetMainViewport()->Pos/Size instead of hardcoding those values.
- // - In the interest of supporting multi-viewport applications (see 'docking' branch on github),
- // always subtract draw_data->DisplayPos from clipping bounds to convert them to your viewport space.
- // - Note that pcmd->ClipRect contains Min+Max bounds. Some graphics API may use Min+Max, other may use Min+Size (size being Max-Min)
- MyEngineSetScissor(clip_min.x, clip_min.y, clip_max.x, clip_max.y);
-
- // The texture for the draw call is specified by pcmd->GetTexID().
- // The vast majority of draw calls will use the Dear ImGui texture atlas, which value you have set yourself during initialization.
- MyEngineBindTexture((MyTexture*)pcmd->GetTexID());
-
- // Render 'pcmd->ElemCount/3' indexed triangles.
- // By default the indices ImDrawIdx are 16-bit, you can change them to 32-bit in imconfig.h if your engine doesn't support 16-bit indices.
- MyEngineDrawIndexedTriangles(pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, idx_buffer + pcmd->IdxOffset, vtx_buffer, pcmd->VtxOffset);
- }
- }
- }
- }
API BREAKING CHANGES
@@ -439,7 +400,104 @@ CODE
- likewise io.MousePos and GetMousePos() will use OS coordinates.
If you query mouse positions to interact with non-imgui coordinates you will need to offset them, e.g. subtract GetWindowViewport()->Pos.
+ - 2025/09/22 (1.92.4) - Viewports: renamed io.ConfigViewportPlatformFocusSetsImGuiFocus to io.ConfigViewportsPlatformFocusSetsImGuiFocus. Was a typo in the first place. (#6299, #6462)
+ - 2025/08/08 (1.92.2) - Backends: SDL_GPU3: Changed ImTextureID type from SDL_GPUTextureSamplerBinding* to SDL_GPUTexture*, which is more natural and easier for user to manage. If you need to change the current sampler, you can access the ImGui_ImplSDLGPU3_RenderState struct. (#8866, #8163, #7998, #7988)
+ - 2025/07/31 (1.92.2) - Tabs: Renamed ImGuiTabBarFlags_FittingPolicyResizeDown to ImGuiTabBarFlags_FittingPolicyShrink. Kept inline redirection enum (will obsolete).
+ - 2025/06/25 (1.92.0) - Layout: commented out legacy ErrorCheckUsingSetCursorPosToExtendParentBoundaries() fallback obsoleted in 1.89 (August 2022) which allowed a SetCursorPos()/SetCursorScreenPos() call WITHOUT AN ITEM
+ to extend parent window/cell boundaries. Replaced with assert/tooltip that would already happens if previously using IMGUI_DISABLE_OBSOLETE_FUNCTIONS. (#5548, #4510, #3355, #1760, #1490, #4152, #150)
+ - Incorrect way to make a window content size 200x200:
+ Begin(...) + SetCursorScreenPos(GetCursorScreenPos() + ImVec2(200,200)) + End();
+ - Correct ways to make a window content size 200x200:
+ Begin(...) + SetCursorScreenPos(GetCursorScreenPos() + ImVec2(200,200)) + Dummy(ImVec2(0,0)) + End();
+ Begin(...) + Dummy(ImVec2(200,200)) + End();
+ - TL;DR; if the assert triggers, you can add a Dummy({0,0}) call to validate extending parent boundaries.
+ - 2025/06/11 (1.92.0) - Renamed/moved ImGuiConfigFlags_DpiEnableScaleFonts -> bool io.ConfigDpiScaleFonts.
+ - Renamed/moved ImGuiConfigFlags_DpiEnableScaleViewports -> bool io.ConfigDpiScaleViewports. **Neither of those flags are very useful in current code. They will be useful once we merge font changes.**
+ [there was a bug on 2025/06/12: when using the old config flags names, they were not imported correctly into the new ones, fixed on 2025/09/12]
+ - 2025/06/11 (1.92.0) - THIS VERSION CONTAINS THE LARGEST AMOUNT OF BREAKING CHANGES SINCE 2015! I TRIED REALLY HARD TO KEEP THEM TO A MINIMUM, REDUCE THE AMOUNT OF INTERFERENCES, BUT INEVITABLY SOME USERS WILL BE AFFECTED.
+ IN ORDER TO HELP US IMPROVE THE TRANSITION PROCESS, INCL. DOCUMENTATION AND COMMENTS, PLEASE REPORT **ANY** DOUBT, CONFUSION, QUESTIONS, FEEDBACK TO: https://github.com/ocornut/imgui/issues/
+ As part of the plan to reduce impact of API breaking changes, several unfinished changes/features/refactors related to font and text systems and scaling will be part of subsequent releases (1.92.1+).
+ If you are updating from an old version, and expecting a massive or difficult update, consider first updating to 1.91.9 to reduce the amount of changes.
+ - Hard to read? Refer to 'docs/Changelog.txt' for a less compact and more complete version of this!
+ - Fonts: **IMPORTANT**: if your app was solving the OSX/iOS Retina screen specific logical vs display scale problem by setting io.DisplayFramebufferScale (e.g. to 2.0f) + setting io.FontGlobalScale (e.g. to 1.0f/2.0f) + loading fonts at scaled sizes (e.g. size X * 2.0f):
+ This WILL NOT map correctly to the new system! Because font will rasterize as requested size.
+ - With a legacy backend (< 1.92): Instead of setting io.FontGlobalScale = 1.0f/N -> set ImFontCfg::RasterizerDensity = N. This already worked before, but is now pretty much required.
+ - With a new backend (1.92+): This should be all automatic. FramebufferScale is automatically used to set current font RasterizerDensity. FramebufferScale is a per-viewport property provided by backend through the Platform_GetWindowFramebufferScale() handler in 'docking' branch.
+ - Fonts: **IMPORTANT** on Font Sizing: Before 1.92, fonts were of a single size. They can now be dynamically sized.
+ - PushFont() API now has a REQUIRED size parameter.
+ - Before 1.92: PushFont() always used font "default" size specified in AddFont() call. It is equivalent to calling PushFont(font, font->LegacySize).
+ - Since 1.92: PushFont(font, 0.0f) preserve the current font size which is a shared value.
+ - To use old behavior: use 'ImGui::PushFont(font, font->LegacySize)' at call site.
+ - Kept inline single parameter function. Will obsolete.
+ - Fonts: **IMPORTANT** on Font Merging:
+ - When searching for a glyph in multiple merged fonts: we search for the FIRST font source which contains the desired glyph.
+ Because the user doesn't need to provide glyph ranges any more, it is possible that a glyph that you expected to fetch from a secondary/merged icon font may be erroneously fetched from the primary font.
+ - When searching for a glyph in multiple merged fonts: we now search for the FIRST font source which contains the desired glyph. This is technically a different behavior than before!
+ - e.g. If you are merging fonts you may have glyphs that you expected to load from Font Source 2 which exists in Font Source 1.
+ After the update and when using a new backend, those glyphs may now loaded from Font Source 1!
+ - We added `ImFontConfig::GlyphExcludeRanges[]` to specify ranges to exclude from a given font source:
+ // Add Font Source 1 but ignore ICON_MIN_FA..ICON_MAX_FA range
+ static ImWchar exclude_ranges[] = { ICON_MIN_FA, ICON_MAX_FA, 0 };
+ ImFontConfig cfg1;
+ cfg1.GlyphExcludeRanges = exclude_ranges;
+ io.Fonts->AddFontFromFileTTF("segoeui.ttf", 0.0f, &cfg1);
+ // Add Font Source 2, which expects to use the range above
+ ImFontConfig cfg2;
+ cfg2.MergeMode = true;
+ io.Fonts->AddFontFromFileTTF("FontAwesome4.ttf", 0.0f, &cfg2);
+ - You can use `Metrics/Debugger->Fonts->Font->Input Glyphs Overlap Detection Tool` to see list of glyphs available in multiple font sources. This can facilitate unde
+ - Fonts: **IMPORTANT** on Thread Safety:
+ - A few functions such as font->CalcTextSizeA() were, by sheer luck (== accidentally) thread-safe even thou we had never provided that guarantee. They are definitively not thread-safe anymore as new glyphs may be loaded.
+ - Fonts: ImFont::FontSize was removed and does not make sense anymore. ImFont::LegacySize is the size passed to AddFont().
+ - Fonts: Removed support for PushFont(NULL) which was a shortcut for "default font".
+ - Fonts: Renamed/moved 'io.FontGlobalScale' to 'style.FontScaleMain'.
+ - Textures: all API functions taking a 'ImTextureID' parameter are now taking a 'ImTextureRef'. Affected functions are: ImGui::Image(), ImGui::ImageWithBg(), ImGui::ImageButton(), ImDrawList::AddImage(), ImDrawList::AddImageQuad(), ImDrawList::AddImageRounded().
+ - Fonts: obsoleted ImFontAtlas::GetTexDataAsRGBA32(), GetTexDataAsAlpha8(), Build(), SetTexID(), IsBuilt() functions. The new protocol for backends to handle textures doesn't need them. Kept redirection functions (will obsolete).
+ - Fonts: ImFontConfig::OversampleH/OversampleV default to automatic (== 0) since v1.91.8. It is quite important you keep it automatic until we decide if we want to provide a way to express finer policy, otherwise you will likely waste texture space when using large glyphs. Note that the imgui_freetype backend doesn't use and does not need oversampling.
+ - Fonts: specifying glyph ranges is now unnecessary. The value of ImFontConfig::GlyphRanges[] is only useful for legacy backends. All GetGlyphRangesXXXX() functions are now marked obsolete: GetGlyphRangesDefault(), GetGlyphRangesGreek(), GetGlyphRangesKorean(), GetGlyphRangesJapanese(), GetGlyphRangesChineseSimplifiedCommon(), GetGlyphRangesChineseFull(), GetGlyphRangesCyrillic(), GetGlyphRangesThai(), GetGlyphRangesVietnamese().
+ - Fonts: removed ImFontAtlas::TexDesiredWidth to enforce a texture width. (#327)
+ - Fonts: if you create and manage ImFontAtlas instances yourself (instead of relying on ImGuiContext to create one), you'll need to call ImFontAtlasUpdateNewFrame() yourself. An assert will trigger if you don't.
+ - Fonts: obsolete ImGui::SetWindowFontScale() which is not useful anymore. Prefer using 'PushFont(NULL, style.FontSizeBase * factor)' or to manipulate other scaling factors.
+ - Fonts: obsoleted ImFont::Scale which is not useful anymore.
+ - Fonts: generally reworked Internals of ImFontAtlas and ImFont. While in theory a vast majority of users shouldn't be affected, some use cases or extensions might be. Among other things:
+ - ImDrawCmd::TextureId has been changed to ImDrawCmd::TexRef.
+ - ImFontAtlas::TexID has been changed to ImFontAtlas::TexRef.
+ - ImFontAtlas::ConfigData[] has been renamed to ImFontAtlas::Sources[]
+ - ImFont::ConfigData[], ConfigDataCount has been renamed to Sources[], SourceCount.
+ - Each ImFont has a number of ImFontBaked instances corresponding to actively used sizes. ImFont::GetFontBaked(size) retrieves the one for a given size.
+ - Fields moved from ImFont to ImFontBaked: IndexAdvanceX[], Glyphs[], Ascent, Descent, FindGlyph(), FindGlyphNoFallback(), GetCharAdvance().
+ - Fields moved from ImFontAtlas to ImFontAtlas->Tex: ImFontAtlas::TexWidth => TexData->Width, ImFontAtlas::TexHeight => TexData->Height, ImFontAtlas::TexPixelsAlpha8/TexPixelsRGBA32 => TexData->GetPixels().
+ - Widget code may use ImGui::GetFontBaked() instead of ImGui::GetFont() to access font data for current font at current font size (and you may use font->GetFontBaked(size) to access it for any other size.)
+ - Fonts: (users of imgui_freetype): renamed ImFontAtlas::FontBuilderFlags to ImFontAtlas::FontLoaderFlags. Renamed ImFontConfig::FontBuilderFlags to ImFontConfig::FontLoaderFlags. Renamed ImGuiFreeTypeBuilderFlags to ImGuiFreeTypeLoaderFlags.
+ If you used runtime imgui_freetype selection rather than the default IMGUI_ENABLE_FREETYPE compile-time option: Renamed/reworked ImFontBuilderIO into ImFontLoader. Renamed ImGuiFreeType::GetBuilderForFreeType() to ImGuiFreeType::GetFontLoader().
+ - old: io.Fonts->FontBuilderIO = ImGuiFreeType::GetBuilderForFreeType()
+ - new: io.Fonts->FontLoader = ImGuiFreeType::GetFontLoader()
+ - new: io.Fonts->SetFontLoader(ImGuiFreeType::GetFontLoader()) to change dynamically at runtime [from 1.92.1]
+ - Fonts: (users of custom rectangles, see #8466): Renamed AddCustomRectRegular() to AddCustomRect(). Added GetCustomRect() as a replacement for GetCustomRectByIndex() + CalcCustomRectUV().
+ - The output type of GetCustomRect() is now ImFontAtlasRect, which include UV coordinates. X->x, Y->y, Width->w, Height->h.
+ - old:
+ const ImFontAtlasCustomRect* r = atlas->GetCustomRectByIndex(custom_rect_id);
+ ImVec2 uv0, uv1;
+ atlas->GetCustomRectUV(r, &uv0, &uv1);
+ ImGui::Image(atlas->TexRef, ImVec2(r->w, r->h), uv0, uv1);
+ - new;
+ ImFontAtlasRect r;
+ atlas->GetCustomRect(custom_rect_id, &r);
+ ImGui::Image(atlas->TexRef, ImVec2(r.w, r.h), r.uv0, r.uv1);
+ - We added a redirecting typedef but haven't attempted to magically redirect the field names, as this API is rarely used and the fix is simple.
+ - Obsoleted AddCustomRectFontGlyph() as the API does not make sense for scalable fonts. Kept existing function which uses the font "default size" (Sources[0]->LegacySize). Added a helper AddCustomRectFontGlyphForSize() which is immediately marked obsolete, but can facilitate transitioning old code.
+ - Prefer adding a font source (ImFontConfig) using a custom/procedural loader.
+ - DrawList: Renamed ImDrawList::PushTextureID()/PopTextureID() to PushTexture()/PopTexture().
+ - Backends: removed ImGui_ImplXXXX_CreateFontsTexture()/ImGui_ImplXXXX_DestroyFontsTexture() for all backends that had them. They should not be necessary any more.
+ - 2025/05/23 (1.92.0) - Fonts: changed ImFont::CalcWordWrapPositionA() to ImFont::CalcWordWrapPosition()
+ - old: const char* ImFont::CalcWordWrapPositionA(float scale, const char* text, ....);
+ - new: const char* ImFont::CalcWordWrapPosition (float size, const char* text, ....);
+ The leading 'float scale' parameters was changed to 'float size'. This was necessary as 'scale' is assuming standard font size which is a concept we aim to eliminate in an upcoming update. Kept inline redirection function.
+ - 2025/05/15 (1.92.0) - TreeNode: renamed ImGuiTreeNodeFlags_NavLeftJumpsBackHere to ImGuiTreeNodeFlags_NavLeftJumpsToParent for clarity. Kept inline redirection enum (will obsolete).
+ - 2025/05/15 (1.92.0) - Commented out PushAllowKeyboardFocus()/PopAllowKeyboardFocus() which was obsoleted in 1.89.4. Use PushItemFlag(ImGuiItemFlags_NoTabStop, !tab_stop)/PopItemFlag() instead. (#3092)
+ - 2025/05/15 (1.92.0) - Commented out ImGuiListClipper::ForceDisplayRangeByIndices() which was obsoleted in 1.89.6. Use ImGuiListClipper::IncludeItemsByIndex() instead.
- 2025/03/05 (1.91.9) - BeginMenu(): Internals: reworked mangling of menu windows to use "###Menu_00" etc. instead of "##Menu_00", allowing them to also store the menu name before it. This shouldn't affect code unless directly accessing menu window from their mangled name.
+ - 2025/04/16 (1.91.9) - Internals: RenderTextEllipsis() function removed the 'float clip_max_x' parameter directly preceding 'float ellipsis_max_x'. Values were identical for a vast majority of users. (#8387)
- 2025/02/27 (1.91.9) - Image(): removed 'tint_col' and 'border_col' parameter from Image() function. Added ImageWithBg() replacement. (#8131, #8238)
- old: void Image (ImTextureID tex_id, ImVec2 image_size, ImVec2 uv0 = (0,0), ImVec2 uv1 = (1,1), ImVec4 tint_col = (1,1,1,1), ImVec4 border_col = (0,0,0,0));
- new: void Image (ImTextureID tex_id, ImVec2 image_size, ImVec2 uv0 = (0,0), ImVec2 uv1 = (1,1));
@@ -480,7 +538,7 @@ CODE
in doubt it is almost always better to do an intermediate intptr_t cast, since it allows casting any pointer/integer type without warning:
- May warn: ImGui::Image((void*)MyTextureData, ...);
- May warn: ImGui::Image((void*)(intptr_t)MyTextureData, ...);
- - Won't warn: ImGui::Image((ImTextureID)(intptr_t)MyTextureData), ...);
+ - Won't warn: ImGui::Image((ImTextureID)(intptr_t)MyTextureData, ...);
- note that you can always define ImTextureID to be your own high-level structures (with dedicated constructors) if you like.
- 2024/10/03 (1.91.3) - drags: treat v_min==v_max as a valid clamping range when != 0.0f. Zero is a still special value due to legacy reasons, unless using ImGuiSliderFlags_ClampZeroRange. (#7968, #3361, #76)
- drags: extended behavior of ImGuiSliderFlags_AlwaysClamp to include _ClampZeroRange. It considers v_min==v_max==0.0f as a valid clamping range (aka edits not allowed).
@@ -770,7 +828,7 @@ CODE
- ShowTestWindow() -> use ShowDemoWindow()
- IsRootWindowFocused() -> use IsWindowFocused(ImGuiFocusedFlags_RootWindow)
- IsRootWindowOrAnyChildFocused() -> use IsWindowFocused(ImGuiFocusedFlags_RootAndChildWindows)
- - SetNextWindowContentWidth(w) -> use SetNextWindowContentSize(ImVec2(w, 0.0f)
+ - SetNextWindowContentWidth(w) -> use SetNextWindowContentSize(ImVec2(w, 0.0f))
- GetItemsLineHeightWithSpacing() -> use GetFrameHeightWithSpacing()
- ImGuiCol_ChildWindowBg -> use ImGuiCol_ChildBg
- ImGuiStyleVar_ChildWindowRounding -> use ImGuiStyleVar_ChildRounding
@@ -1012,6 +1070,7 @@ CODE
associated with it.
Q: What is this library called?
+ Q: What is the difference between Dear ImGui and traditional UI toolkits?
Q: Which version should I get?
>> This library is called "Dear ImGui", please don't call it "ImGui" :)
>> See https://www.dearimgui.com/faq for details.
@@ -1189,6 +1248,9 @@ CODE
#define IMGUI_DEBUG_NAV_SCORING 0 // Display navigation scoring preview when hovering items. Hold CTRL to display for all candidates. CTRL+Arrow to change last direction.
#define IMGUI_DEBUG_NAV_RECTS 0 // Display the reference navigation rectangle for each window
+// Default font size if unspecified in both style.FontSizeBase and AddFontXXX() calls.
+static const float FONT_DEFAULT_SIZE = 20.0f;
+
// When using CTRL+TAB (or Gamepad Square+L/R) we delay the visual a little in order to reduce visual noise doing a fast switch.
static const float NAV_WINDOWING_HIGHLIGHT_DELAY = 0.20f; // Time before the highlight and screen dimming starts fading in
static const float NAV_WINDOWING_LIST_APPEAR_DELAY = 0.15f; // Time before the window list starts to appear
@@ -1248,7 +1310,7 @@ static float NavUpdatePageUpPageDown();
static inline void NavUpdateAnyRequestFlag();
static void NavUpdateCreateWrappingRequest();
static void NavEndFrame();
-static bool NavScoreItem(ImGuiNavItemData* result);
+static bool NavScoreItem(ImGuiNavItemData* result, const ImRect& nav_bb);
static void NavApplyItemToResult(ImGuiNavItemData* result);
static void NavProcessItem();
static void NavProcessItemForTabbingRequest(ImGuiID id, ImGuiItemFlags item_flags, ImGuiNavMoveFlags move_flags);
@@ -1274,6 +1336,10 @@ static void UpdateMouseWheel();
static void UpdateKeyRoutingTable(ImGuiKeyRoutingTable* rt);
// Misc
+static void UpdateFontsNewFrame();
+static void UpdateFontsEndFrame();
+static void UpdateTexturesNewFrame();
+static void UpdateTexturesEndFrame();
static void UpdateSettings();
static int UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& size_auto_fit, int* border_hovered, int* border_held, int resize_grip_count, ImU32 resize_grip_col[4], const ImRect& visibility_rect);
static void RenderWindowOuterBorders(ImGuiWindow* window);
@@ -1349,6 +1415,10 @@ static void* GImAllocatorUserData = NULL;
ImGuiStyle::ImGuiStyle()
{
+ FontSizeBase = 0.0f; // Will default to io.Fonts->Fonts[0] on first frame.
+ FontScaleMain = 1.0f; // Main scale factor. May be set by application once, or exposed to end-user.
+ FontScaleDpi = 1.0f; // Additional scale factor from viewport/monitor contents scale. When io.ConfigDpiScaleFonts is enabled, this is automatically overwritten when changing monitor DPI.
+
Alpha = 1.0f; // Global alpha applies to everything in Dear ImGui.
DisabledAlpha = 0.60f; // Additional alpha multiplier applied by BeginDisabled(). Multiply over current value of Alpha.
WindowPadding = ImVec2(8,8); // Padding within a window
@@ -1373,18 +1443,24 @@ ImGuiStyle::ImGuiStyle()
ColumnsMinSpacing = 6.0f; // Minimum horizontal spacing between two columns. Preferably > (FramePadding.x + 1).
ScrollbarSize = 14.0f; // Width of the vertical scrollbar, Height of the horizontal scrollbar
ScrollbarRounding = 9.0f; // Radius of grab corners rounding for scrollbar
+ ScrollbarPadding = 2.0f; // Padding of scrollbar grab within its frame (same for both axises)
GrabMinSize = 12.0f; // Minimum width/height of a grab box for slider/scrollbar
GrabRounding = 0.0f; // Radius of grabs corners rounding. Set to 0.0f to have rectangular slider grabs.
LogSliderDeadzone = 4.0f; // The size in pixels of the dead-zone around zero on logarithmic sliders that cross zero.
ImageBorderSize = 0.0f; // Thickness of border around tabs.
TabRounding = 5.0f; // Radius of upper corners of a tab. Set to 0.0f to have rectangular tabs.
TabBorderSize = 0.0f; // Thickness of border around tabs.
+ TabMinWidthBase = 1.0f; // Minimum tab width, to make tabs larger than their contents. TabBar buttons are not affected.
+ TabMinWidthShrink = 80.0f; // Minimum tab width after shrinking, when using ImGuiTabBarFlags_FittingPolicyMixed policy.
TabCloseButtonMinWidthSelected = -1.0f; // -1: always visible. 0.0f: visible when hovered. >0.0f: visible when hovered if minimum width.
TabCloseButtonMinWidthUnselected = 0.0f; // -1: always visible. 0.0f: visible when hovered. >0.0f: visible when hovered if minimum width. FLT_MAX: never show close button when unselected.
TabBarBorderSize = 1.0f; // Thickness of tab-bar separator, which takes on the tab active color to denote focus.
TabBarOverlineSize = 1.0f; // Thickness of tab-bar overline, which highlights the selected tab-bar.
TableAngledHeadersAngle = 35.0f * (IM_PI / 180.0f); // Angle of angled headers (supported values range from -50 degrees to +50 degrees).
TableAngledHeadersTextAlign = ImVec2(0.5f,0.0f);// Alignment of angled headers within the cell
+ TreeLinesFlags = ImGuiTreeNodeFlags_DrawLinesNone;
+ TreeLinesSize = 1.0f; // Thickness of outlines when using ImGuiTreeNodeFlags_DrawLines.
+ TreeLinesRounding = 0.0f; // Radius of lines connecting child nodes to the vertical line.
ColorButtonPosition = ImGuiDir_Right; // Side of the color button in the ColorEdit4 widget (left/right). Defaults to ImGuiDir_Right.
ButtonTextAlign = ImVec2(0.5f,0.5f);// Alignment of button text when button is larger than text.
SelectableTextAlign = ImVec2(0.0f,0.0f);// Alignment of selectable text. Defaults to (0.0f, 0.0f) (top-left aligned). It's generally important to keep this left-aligned if you want to lay multiple items on a same line.
@@ -1393,6 +1469,7 @@ ImGuiStyle::ImGuiStyle()
SeparatorTextPadding = ImVec2(20.0f,3.f);// Horizontal offset of text from each edge of the separator + spacing on other axis. Generally small values. .y is recommended to be == FramePadding.y.
DisplayWindowPadding = ImVec2(19,19); // Window position are clamped to be visible within the display area or monitors by at least this amount. Only applies to regular windows.
DisplaySafeAreaPadding = ImVec2(3,3); // If you cannot see the edge of your screen (e.g. on a TV) increase the safe area padding. Covers popups/tooltips as well regular windows.
+ DockingNodeHasCloseButton = true; // Docking nodes have their own CloseButton() to close all docked windows.
DockingSeparatorSize = 2.0f; // Thickness of resizing border between docked windows
MouseCursorScale = 1.0f; // Scale software rendered mouse cursor (when io.MouseDrawCursor is enabled). May be removed later.
AntiAliasedLines = true; // Enable anti-aliased lines/borders. Disable if you are really tight on CPU/GPU.
@@ -1408,14 +1485,20 @@ ImGuiStyle::ImGuiStyle()
HoverFlagsForTooltipMouse = ImGuiHoveredFlags_Stationary | ImGuiHoveredFlags_DelayShort | ImGuiHoveredFlags_AllowWhenDisabled; // Default flags when using IsItemHovered(ImGuiHoveredFlags_ForTooltip) or BeginItemTooltip()/SetItemTooltip() while using mouse.
HoverFlagsForTooltipNav = ImGuiHoveredFlags_NoSharedDelay | ImGuiHoveredFlags_DelayNormal | ImGuiHoveredFlags_AllowWhenDisabled; // Default flags when using IsItemHovered(ImGuiHoveredFlags_ForTooltip) or BeginItemTooltip()/SetItemTooltip() while using keyboard/gamepad.
+ // [Internal]
+ _MainScale = 1.0f;
+ _NextFrameFontSizeBase = 0.0f;
+
// Default theme
ImGui::StyleColorsDark(this);
}
-// To scale your entire UI (e.g. if you want your app to use High DPI or generally be DPI aware) you may use this helper function. Scaling the fonts is done separately and is up to you.
+
+// Scale all spacing/padding/thickness values. Do not scale fonts.
// Important: This operation is lossy because we round all sizes to integer. If you need to change your scale multiples, call this over a freshly initialized ImGuiStyle structure rather than scaling multiple times.
void ImGuiStyle::ScaleAllSizes(float scale_factor)
{
+ _MainScale *= scale_factor;
WindowPadding = ImTrunc(WindowPadding * scale_factor);
WindowRounding = ImTrunc(WindowRounding * scale_factor);
WindowMinSize = ImTrunc(WindowMinSize * scale_factor);
@@ -1432,14 +1515,18 @@ void ImGuiStyle::ScaleAllSizes(float scale_factor)
ColumnsMinSpacing = ImTrunc(ColumnsMinSpacing * scale_factor);
ScrollbarSize = ImTrunc(ScrollbarSize * scale_factor);
ScrollbarRounding = ImTrunc(ScrollbarRounding * scale_factor);
+ ScrollbarPadding = ImTrunc(ScrollbarPadding * scale_factor);
GrabMinSize = ImTrunc(GrabMinSize * scale_factor);
GrabRounding = ImTrunc(GrabRounding * scale_factor);
LogSliderDeadzone = ImTrunc(LogSliderDeadzone * scale_factor);
ImageBorderSize = ImTrunc(ImageBorderSize * scale_factor);
TabRounding = ImTrunc(TabRounding * scale_factor);
+ TabMinWidthBase = ImTrunc(TabMinWidthBase * scale_factor);
+ TabMinWidthShrink = ImTrunc(TabMinWidthShrink * scale_factor);
TabCloseButtonMinWidthSelected = (TabCloseButtonMinWidthSelected > 0.0f && TabCloseButtonMinWidthSelected != FLT_MAX) ? ImTrunc(TabCloseButtonMinWidthSelected * scale_factor) : TabCloseButtonMinWidthSelected;
TabCloseButtonMinWidthUnselected = (TabCloseButtonMinWidthUnselected > 0.0f && TabCloseButtonMinWidthUnselected != FLT_MAX) ? ImTrunc(TabCloseButtonMinWidthUnselected * scale_factor) : TabCloseButtonMinWidthUnselected;
TabBarOverlineSize = ImTrunc(TabBarOverlineSize * scale_factor);
+ TreeLinesRounding = ImTrunc(TreeLinesRounding * scale_factor);
SeparatorTextPadding = ImTrunc(SeparatorTextPadding * scale_factor);
DockingSeparatorSize = ImTrunc(DockingSeparatorSize * scale_factor);
DisplayWindowPadding = ImTrunc(DisplayWindowPadding * scale_factor);
@@ -1464,9 +1551,11 @@ ImGuiIO::ImGuiIO()
UserData = NULL;
Fonts = NULL;
- FontGlobalScale = 1.0f;
FontDefault = NULL;
FontAllowUserScaling = false;
+#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
+ FontGlobalScale = 1.0f; // Use style.FontScaleMain instead!
+#endif
DisplayFramebufferScale = ImVec2(1.0f, 1.0f);
// Keyboard/Gamepad Navigation options
@@ -1488,7 +1577,8 @@ ImGuiIO::ImGuiIO()
ConfigViewportsNoAutoMerge = false;
ConfigViewportsNoTaskBarIcon = false;
ConfigViewportsNoDecoration = true;
- ConfigViewportsNoDefaultParent = false;
+ ConfigViewportsNoDefaultParent = true;
+ ConfigViewportsPlatformFocusSetsImGuiFocus = true;
// Miscellaneous options
MouseDrawCursor = false;
@@ -2043,7 +2133,7 @@ void ImStrncpy(char* dst, const char* src, size_t count)
if (count < 1)
return;
if (count > 1)
- strncpy(dst, src, count - 1);
+ strncpy(dst, src, count - 1); // FIXME-OPT: strncpy not only doesn't guarantee 0-termination, it also always writes the whole array
dst[count - 1] = 0;
}
@@ -2054,6 +2144,12 @@ char* ImStrdup(const char* str)
return (char*)memcpy(buf, (const void*)str, len + 1);
}
+void* ImMemdup(const void* src, size_t size)
+{
+ void* dst = IM_ALLOC(size);
+ return memcpy(dst, src, size);
+}
+
char* ImStrdupcpy(char* dst, size_t* p_dst_size, const char* src)
{
size_t dst_buf_size = p_dst_size ? *p_dst_size : ImStrlen(dst) + 1;
@@ -2358,6 +2454,17 @@ ImGuiID ImHashStr(const char* data_p, size_t data_size, ImGuiID seed)
return ~crc;
}
+// Skip to the "###" marker if any. We don't skip past to match the behavior of GetID()
+// FIXME-OPT: This is not designed to be optimal. Use with care.
+const char* ImHashSkipUncontributingPrefix(const char* label)
+{
+ const char* result = label;
+ while (unsigned char c = *label++)
+ if (c == '#' && label[0] == '#' && label[1] == '#')
+ result = label - 1;
+ return result;
+}
+
//-----------------------------------------------------------------------------
// [SECTION] MISC HELPERS/UTILITIES (File functions)
//-----------------------------------------------------------------------------
@@ -2563,11 +2670,11 @@ static inline int ImTextCharToUtf8_inline(char* buf, int buf_size, unsigned int
return 0;
}
-const char* ImTextCharToUtf8(char out_buf[5], unsigned int c)
+int ImTextCharToUtf8(char out_buf[5], unsigned int c)
{
int count = ImTextCharToUtf8_inline(out_buf, 5, c);
out_buf[count] = 0;
- return out_buf;
+ return count;
}
// Not optimal but we very rarely use this function.
@@ -3034,19 +3141,21 @@ void ImGuiTextBuffer::appendfv(const char* fmt, va_list args)
va_end(args_copy);
}
+IM_MSVC_RUNTIME_CHECKS_OFF
void ImGuiTextIndex::append(const char* base, int old_size, int new_size)
{
IM_ASSERT(old_size >= 0 && new_size >= old_size && new_size >= EndOffset);
if (old_size == new_size)
return;
if (EndOffset == 0 || base[EndOffset - 1] == '\n')
- LineOffsets.push_back(EndOffset);
+ Offsets.push_back(EndOffset);
const char* base_end = base + new_size;
for (const char* p = base + old_size; (p = (const char*)ImMemchr(p, '\n', base_end - p)) != 0; )
if (++p < base_end) // Don't push a trailing offset on last \n
- LineOffsets.push_back((int)(intptr_t)(p - base));
+ Offsets.push_back((int)(intptr_t)(p - base));
EndOffset = ImMax(EndOffset, new_size);
}
+IM_MSVC_RUNTIME_CHECKS_RESTORE
//-----------------------------------------------------------------------------
// [SECTION] ImGuiListClipper
@@ -3084,7 +3193,7 @@ static void ImGuiListClipper_SortAndFuseRanges(ImVector&
}
}
-static void ImGuiListClipper_SeekCursorAndSetupPrevLine(float pos_y, float line_height)
+static void ImGuiListClipper_SeekCursorAndSetupPrevLine(ImGuiListClipper* clipper, float pos_y, float line_height)
{
// Set cursor position and a few other things so that SetScrollHereY() and Columns() can work when seeking cursor.
// FIXME: It is problematic that we have to do that here, because custom/equivalent end-user code would stumble on the same issue.
@@ -3102,10 +3211,13 @@ static void ImGuiListClipper_SeekCursorAndSetupPrevLine(float pos_y, float line_
{
if (table->IsInsideRow)
ImGui::TableEndRow(table);
- table->RowPosY2 = window->DC.CursorPos.y;
const int row_increase = (int)((off_y / line_height) + 0.5f);
- //table->CurrentRow += row_increase; // Can't do without fixing TableEndRow()
- table->RowBgColorCounter += row_increase;
+ if (row_increase > 0 && (clipper->Flags & ImGuiListClipperFlags_NoSetTableRowCounters) == 0) // If your clipper item height is != from actual table row height, consider using ImGuiListClipperFlags_NoSetTableRowCounters. See #8886.
+ {
+ table->CurrentRow += row_increase;
+ table->RowBgColorCounter += row_increase;
+ }
+ table->RowPosY2 = window->DC.CursorPos.y;
}
}
@@ -3188,7 +3300,7 @@ void ImGuiListClipper::SeekCursorForItem(int item_n)
// - StartPosY starts from ItemsFrozen, by adding SeekOffsetY we generally cancel that out (SeekOffsetY == LossynessOffset - ItemsFrozen * ItemsHeight).
// - The reason we store SeekOffsetY instead of inferring it, is because we want to allow user to perform Seek after the last step, where ImGuiListClipperData is already done.
float pos_y = (float)((double)StartPosY + StartSeekOffsetY + (double)item_n * ItemsHeight);
- ImGuiListClipper_SeekCursorAndSetupPrevLine(pos_y, ItemsHeight);
+ ImGuiListClipper_SeekCursorAndSetupPrevLine(this, pos_y, ItemsHeight);
}
static bool ImGuiListClipper_StepInternal(ImGuiListClipper* clipper)
@@ -3241,10 +3353,17 @@ static bool ImGuiListClipper_StepInternal(ImGuiListClipper* clipper)
if (table)
IM_ASSERT(table->RowPosY1 == clipper->StartPosY && table->RowPosY2 == window->DC.CursorPos.y);
- clipper->ItemsHeight = (window->DC.CursorPos.y - clipper->StartPosY) / (float)(clipper->DisplayEnd - clipper->DisplayStart);
- bool affected_by_floating_point_precision = ImIsFloatAboveGuaranteedIntegerPrecision(clipper->StartPosY) || ImIsFloatAboveGuaranteedIntegerPrecision(window->DC.CursorPos.y);
+ bool affected_by_floating_point_precision = ImIsFloatAboveGuaranteedIntegerPrecision((float)clipper->StartPosY) || ImIsFloatAboveGuaranteedIntegerPrecision(window->DC.CursorPos.y);
if (affected_by_floating_point_precision)
+ {
+ // Mitigation/hack for very large range: assume last time height constitute line height.
clipper->ItemsHeight = window->DC.PrevLineSize.y + g.Style.ItemSpacing.y; // FIXME: Technically wouldn't allow multi-line entries.
+ window->DC.CursorPos.y = (float)(clipper->StartPosY + clipper->ItemsHeight);
+ }
+ else
+ {
+ clipper->ItemsHeight = (float)(window->DC.CursorPos.y - clipper->StartPosY) / (float)(clipper->DisplayEnd - clipper->DisplayStart);
+ }
if (clipper->ItemsHeight == 0.0f && clipper->ItemsCount == INT_MAX) // Accept that no item have been submitted if in indeterminate mode.
return false;
IM_ASSERT(clipper->ItemsHeight > 0.0f && "Unable to calculate item height! First item hasn't moved the cursor vertically!");
@@ -3268,7 +3387,10 @@ static bool ImGuiListClipper_StepInternal(ImGuiListClipper* clipper)
// Add range selected to be included for navigation
const bool is_nav_request = (g.NavMoveScoringItems && g.NavWindow && g.NavWindow->RootWindowForNav == window->RootWindowForNav);
if (is_nav_request)
+ {
+ data->Ranges.push_back(ImGuiListClipperRange::FromPositions(g.NavScoringRect.Min.y, g.NavScoringRect.Max.y, 0, 0));
data->Ranges.push_back(ImGuiListClipperRange::FromPositions(g.NavScoringNoClipRect.Min.y, g.NavScoringNoClipRect.Max.y, 0, 0));
+ }
if (is_nav_request && (g.NavMoveFlags & ImGuiNavMoveFlags_IsTabbing) && g.NavTabbingDir == -1)
data->Ranges.push_back(ImGuiListClipperRange::FromIndices(clipper->ItemsCount - 1, clipper->ItemsCount));
@@ -3277,7 +3399,6 @@ static bool ImGuiListClipper_StepInternal(ImGuiListClipper* clipper)
if (g.NavId != 0 && window->NavLastIds[0] == g.NavId)
data->Ranges.push_back(ImGuiListClipperRange::FromPositions(nav_rect_abs.Min.y, nav_rect_abs.Max.y, 0, 0));
- // Add visible range
float min_y = window->ClipRect.Min.y;
float max_y = window->ClipRect.Max.y;
@@ -3296,6 +3417,7 @@ static bool ImGuiListClipper_StepInternal(ImGuiListClipper* clipper)
data->Ranges.push_back(ImGuiListClipperRange::FromPositions(bs->UnclipRect.Min.y, bs->UnclipRect.Max.y, 0, 0));
}
+ // Add main visible range
const int off_min = (is_nav_request && g.NavMoveClipDir == ImGuiDir_Up) ? -1 : 0;
const int off_max = (is_nav_request && g.NavMoveClipDir == ImGuiDir_Down) ? 1 : 0;
data->Ranges.push_back(ImGuiListClipperRange::FromPositions(min_y, max_y, off_min, off_max));
@@ -3361,6 +3483,13 @@ bool ImGuiListClipper::Step()
return ret;
}
+// Generic helper, equivalent to old ImGui::CalcListClipping() but statelesss
+void ImGui::CalcClipRectVisibleItemsY(const ImRect& clip_rect, const ImVec2& pos, float items_height, int* out_visible_start, int* out_visible_end)
+{
+ *out_visible_start = ImMax((int)((clip_rect.Min.y - pos.y) / items_height), 0);
+ *out_visible_end = ImMax((int)ImCeil((clip_rect.Max.y - pos.y) / items_height), *out_visible_start);
+}
+
//-----------------------------------------------------------------------------
// [SECTION] STYLING
//-----------------------------------------------------------------------------
@@ -3471,15 +3600,20 @@ static const ImGuiStyleVarInfo GStyleVarsInfo[] =
{ 2, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, CellPadding) }, // ImGuiStyleVar_CellPadding
{ 1, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, ScrollbarSize) }, // ImGuiStyleVar_ScrollbarSize
{ 1, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, ScrollbarRounding) }, // ImGuiStyleVar_ScrollbarRounding
+ { 1, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, ScrollbarPadding) }, // ImGuiStyleVar_ScrollbarPadding
{ 1, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, GrabMinSize) }, // ImGuiStyleVar_GrabMinSize
{ 1, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, GrabRounding) }, // ImGuiStyleVar_GrabRounding
{ 1, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, ImageBorderSize) }, // ImGuiStyleVar_ImageBorderSize
{ 1, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, TabRounding) }, // ImGuiStyleVar_TabRounding
{ 1, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, TabBorderSize) }, // ImGuiStyleVar_TabBorderSize
+ { 1, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, TabMinWidthBase) }, // ImGuiStyleVar_TabMinWidthBase
+ { 1, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, TabMinWidthShrink) }, // ImGuiStyleVar_TabMinWidthShrink
{ 1, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, TabBarBorderSize) }, // ImGuiStyleVar_TabBarBorderSize
{ 1, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, TabBarOverlineSize) }, // ImGuiStyleVar_TabBarOverlineSize
{ 1, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, TableAngledHeadersAngle)}, // ImGuiStyleVar_TableAngledHeadersAngle
{ 2, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, TableAngledHeadersTextAlign)},// ImGuiStyleVar_TableAngledHeadersTextAlign
+ { 1, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, TreeLinesSize)}, // ImGuiStyleVar_TreeLinesSize
+ { 1, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, TreeLinesRounding)}, // ImGuiStyleVar_TreeLinesRounding
{ 2, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, ButtonTextAlign) }, // ImGuiStyleVar_ButtonTextAlign
{ 2, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, SelectableTextAlign) }, // ImGuiStyleVar_SelectableTextAlign
{ 1, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, SeparatorTextBorderSize)}, // ImGuiStyleVar_SeparatorTextBorderSize
@@ -3631,6 +3765,7 @@ const char* ImGui::GetStyleColorName(ImGuiCol idx)
case ImGuiCol_TableRowBgAlt: return "TableRowBgAlt";
case ImGuiCol_TextLink: return "TextLink";
case ImGuiCol_TextSelectedBg: return "TextSelectedBg";
+ case ImGuiCol_TreeLines: return "TreeLines";
case ImGuiCol_DragDropTarget: return "DragDropTarget";
case ImGuiCol_NavCursor: return "NavCursor";
case ImGuiCol_NavWindowingHighlight: return "NavWindowingHighlight";
@@ -3752,18 +3887,19 @@ void ImGui::RenderTextClipped(const ImVec2& pos_min, const ImVec2& pos_max, cons
}
// Another overly complex function until we reorganize everything into a nice all-in-one helper.
-// This is made more complex because we have dissociated the layout rectangle (pos_min..pos_max) which define _where_ the ellipsis is, from actual clipping of text and limit of the ellipsis display.
+// This is made more complex because we have dissociated the layout rectangle (pos_min..pos_max) from 'ellipsis_max_x' which may be beyond it.
// This is because in the context of tabs we selectively hide part of the text when the Close Button appears, but we don't want the ellipsis to move.
-void ImGui::RenderTextEllipsis(ImDrawList* draw_list, const ImVec2& pos_min, const ImVec2& pos_max, float clip_max_x, float ellipsis_max_x, const char* text, const char* text_end_full, const ImVec2* text_size_if_known)
+// (BREAKING) On 2025/04/16 we removed the 'float clip_max_x' parameters which was preceding 'float ellipsis_max' and was the same value for 99% of users.
+void ImGui::RenderTextEllipsis(ImDrawList* draw_list, const ImVec2& pos_min, const ImVec2& pos_max, float ellipsis_max_x, const char* text, const char* text_end_full, const ImVec2* text_size_if_known)
{
ImGuiContext& g = *GImGui;
if (text_end_full == NULL)
text_end_full = FindRenderedTextEnd(text);
const ImVec2 text_size = text_size_if_known ? *text_size_if_known : CalcTextSize(text, text_end_full, false, 0.0f);
- //draw_list->AddLine(ImVec2(pos_max.x, pos_min.y - 4), ImVec2(pos_max.x, pos_max.y + 4), IM_COL32(0, 0, 255, 255));
- //draw_list->AddLine(ImVec2(ellipsis_max_x, pos_min.y-2), ImVec2(ellipsis_max_x, pos_max.y+2), IM_COL32(0, 255, 0, 255));
- //draw_list->AddLine(ImVec2(clip_max_x, pos_min.y), ImVec2(clip_max_x, pos_max.y), IM_COL32(255, 0, 0, 255));
+ //draw_list->AddLine(ImVec2(pos_max.x, pos_min.y - 4), ImVec2(pos_max.x, pos_max.y + 6), IM_COL32(0, 0, 255, 255));
+ //draw_list->AddLine(ImVec2(ellipsis_max_x, pos_min.y - 2), ImVec2(ellipsis_max_x, pos_max.y + 3), IM_COL32(0, 255, 0, 255));
+
// FIXME: We could technically remove (last_glyph->AdvanceX - last_glyph->X1) from text_size.x here and save a few pixels.
if (text_size.x > pos_max.x - pos_min.x)
{
@@ -3776,17 +3912,12 @@ void ImGui::RenderTextEllipsis(ImDrawList* draw_list, const ImVec2& pos_min, con
const float font_size = draw_list->_Data->FontSize;
const float font_scale = draw_list->_Data->FontScale;
const char* text_end_ellipsis = NULL;
- const float ellipsis_width = font->EllipsisWidth * font_scale;
+ ImFontBaked* baked = font->GetFontBaked(font_size);
+ const float ellipsis_width = baked->GetCharAdvance(font->EllipsisChar) * font_scale;
// We can now claim the space between pos_max.x and ellipsis_max.x
const float text_avail_width = ImMax((ImMax(pos_max.x, ellipsis_max_x) - ellipsis_width) - pos_min.x, 1.0f);
float text_size_clipped_x = font->CalcTextSizeA(font_size, text_avail_width, 0.0f, text, text_end_full, &text_end_ellipsis).x;
- if (text == text_end_ellipsis && text_end_ellipsis < text_end_full)
- {
- // Always display at least 1 character if there's no room for character + ellipsis
- text_end_ellipsis = text + ImTextCountUtf8BytesFromChar(text, text_end_full);
- text_size_clipped_x = font->CalcTextSizeA(font_size, FLT_MAX, 0.0f, text, text_end_ellipsis).x;
- }
while (text_end_ellipsis > text && ImCharIsBlankA(text_end_ellipsis[-1]))
{
// Trim trailing space before ellipsis (FIXME: Supporting non-ascii blanks would be nice, for this we need a function to backtrack in UTF-8 text)
@@ -3795,15 +3926,14 @@ void ImGui::RenderTextEllipsis(ImDrawList* draw_list, const ImVec2& pos_min, con
}
// Render text, render ellipsis
- RenderTextClippedEx(draw_list, pos_min, ImVec2(clip_max_x, pos_max.y), text, text_end_ellipsis, &text_size, ImVec2(0.0f, 0.0f));
+ RenderTextClippedEx(draw_list, pos_min, pos_max, text, text_end_ellipsis, &text_size, ImVec2(0.0f, 0.0f));
+ ImVec4 cpu_fine_clip_rect(pos_min.x, pos_min.y, pos_max.x, pos_max.y);
ImVec2 ellipsis_pos = ImTrunc(ImVec2(pos_min.x + text_size_clipped_x, pos_min.y));
- if (ellipsis_pos.x + ellipsis_width <= ellipsis_max_x)
- for (int i = 0; i < font->EllipsisCharCount; i++, ellipsis_pos.x += font->EllipsisCharStep * font_scale)
- font->RenderChar(draw_list, font_size, ellipsis_pos, GetColorU32(ImGuiCol_Text), font->EllipsisChar);
+ font->RenderChar(draw_list, font_size, ellipsis_pos, GetColorU32(ImGuiCol_Text), font->EllipsisChar, &cpu_fine_clip_rect);
}
else
{
- RenderTextClippedEx(draw_list, pos_min, ImVec2(clip_max_x, pos_max.y), text, text_end_full, &text_size, ImVec2(0.0f, 0.0f));
+ RenderTextClippedEx(draw_list, pos_min, pos_max, text, text_end_full, &text_size, ImVec2(0.0f, 0.0f));
}
if (g.LogEnabled)
@@ -3875,7 +4005,7 @@ void ImGui::RenderMouseCursor(ImVec2 base_pos, float base_scale, ImGuiMouseCurso
ImGuiContext& g = *GImGui;
if (mouse_cursor <= ImGuiMouseCursor_None || mouse_cursor >= ImGuiMouseCursor_COUNT) // We intentionally accept out of bound values.
mouse_cursor = ImGuiMouseCursor_Arrow;
- ImFontAtlas* font_atlas = g.DrawListSharedData.Font->ContainerAtlas;
+ ImFontAtlas* font_atlas = g.DrawListSharedData.FontAtlas;
for (ImGuiViewportP* viewport : g.Viewports)
{
// We scale cursor with current viewport/monitor, however Windows 10 for its own hardware cursor seems to be using a different scale factor.
@@ -3887,12 +4017,12 @@ void ImGui::RenderMouseCursor(ImVec2 base_pos, float base_scale, ImGuiMouseCurso
if (!viewport->GetMainRect().Overlaps(ImRect(pos, pos + ImVec2(size.x + 2, size.y + 2) * scale)))
continue;
ImDrawList* draw_list = GetForegroundDrawList(viewport);
- ImTextureID tex_id = font_atlas->TexID;
- draw_list->PushTextureID(tex_id);
- draw_list->AddImage(tex_id, pos + ImVec2(1, 0) * scale, pos + (ImVec2(1, 0) + size) * scale, uv[2], uv[3], col_shadow);
- draw_list->AddImage(tex_id, pos + ImVec2(2, 0) * scale, pos + (ImVec2(2, 0) + size) * scale, uv[2], uv[3], col_shadow);
- draw_list->AddImage(tex_id, pos, pos + size * scale, uv[2], uv[3], col_border);
- draw_list->AddImage(tex_id, pos, pos + size * scale, uv[0], uv[1], col_fill);
+ ImTextureRef tex_ref = font_atlas->TexRef;
+ draw_list->PushTexture(tex_ref);
+ draw_list->AddImage(tex_ref, pos + ImVec2(1, 0) * scale, pos + (ImVec2(1, 0) + size) * scale, uv[2], uv[3], col_shadow);
+ draw_list->AddImage(tex_ref, pos + ImVec2(2, 0) * scale, pos + (ImVec2(2, 0) + size) * scale, uv[2], uv[3], col_shadow);
+ draw_list->AddImage(tex_ref, pos, pos + size * scale, uv[2], uv[3], col_border);
+ draw_list->AddImage(tex_ref, pos, pos + size * scale, uv[0], uv[1], col_fill);
if (mouse_cursor == ImGuiMouseCursor_Wait || mouse_cursor == ImGuiMouseCursor_Progress)
{
float a_min = ImFmod((float)g.Time * 5.0f, 2.0f * IM_PI);
@@ -3900,7 +4030,7 @@ void ImGui::RenderMouseCursor(ImVec2 base_pos, float base_scale, ImGuiMouseCurso
draw_list->PathArcTo(pos + ImVec2(14, -1) * scale, 6.0f * scale, a_min, a_max);
draw_list->PathStroke(col_fill, ImDrawFlags_None, 3.0f * scale);
}
- draw_list->PopTextureID();
+ draw_list->PopTexture();
}
}
@@ -3986,10 +4116,14 @@ ImGuiContext::ImGuiContext(ImFontAtlas* shared_font_atlas)
Initialized = false;
ConfigFlagsCurrFrame = ConfigFlagsLastFrame = ImGuiConfigFlags_None;
- FontAtlasOwnedByContext = shared_font_atlas ? false : true;
+
Font = NULL;
- FontSize = FontBaseSize = FontScale = CurrentDpiScale = 0.0f;
+ FontBaked = NULL;
+ FontSize = FontSizeBase = FontBakedScale = CurrentDpiScale = 0.0f;
+ FontRasterizerDensity = 1.0f;
IO.Fonts = shared_font_atlas ? shared_font_atlas : IM_NEW(ImFontAtlas)();
+ if (shared_font_atlas == NULL)
+ IO.Fonts->OwnerContext = this;
Time = 0.0f;
FrameCount = 0;
FrameCountEnded = FrameCountPlatformEnded = FrameCountRendered = -1;
@@ -4014,8 +4148,8 @@ ImGuiContext::ImGuiContext(ImFontAtlas* shared_font_atlas)
WheelingWindowStartFrame = WheelingWindowScrolledFrame = -1;
WheelingWindowReleaseTimer = 0.0f;
- DebugDrawIdConflicts = 0;
- DebugHookIdInfo = 0;
+ DebugDrawIdConflictsId = 0;
+ DebugHookIdInfoId = 0;
HoveredId = HoveredIdPreviousFrame = 0;
HoveredIdPreviousFrameItemCount = 0;
HoveredIdAllowOverlap = false;
@@ -4035,6 +4169,7 @@ ImGuiContext::ImGuiContext(ImFontAtlas* shared_font_atlas)
ActiveIdClickOffset = ImVec2(-1, -1);
ActiveIdWindow = NULL;
ActiveIdSource = ImGuiInputSource_None;
+ ActiveIdDisabledId = 0;
ActiveIdMouseButton = -1;
ActiveIdPreviousFrame = 0;
memset(&DeactivatedItemData, 0, sizeof(DeactivatedItemData));
@@ -4110,6 +4245,7 @@ ImGuiContext::ImGuiContext(ImFontAtlas* shared_font_atlas)
DragDropSourceFrameCount = -1;
DragDropMouseButton = -1;
DragDropTargetId = 0;
+ DragDropTargetFullViewport = 0;
DragDropAcceptFlags = ImGuiDragDropFlags_None;
DragDropAcceptIdCurrRectSurface = 0.0f;
DragDropAcceptIdPrev = DragDropAcceptIdCurr = 0;
@@ -4131,6 +4267,7 @@ ImGuiContext::ImGuiContext(ImFontAtlas* shared_font_atlas)
MouseCursor = ImGuiMouseCursor_Arrow;
MouseStationaryTimer = 0.0f;
+ InputTextPasswordFontBackupFlags = ImFontFlags_None;
TempInputId = 0;
memset(&DataTypeZeroValue, 0, sizeof(DataTypeZeroValue));
BeginMenuDepth = BeginComboDepth = 0;
@@ -4154,7 +4291,6 @@ ImGuiContext::ImGuiContext(ImFontAtlas* shared_font_atlas)
PlatformImeData.InputPos = ImVec2(0.0f, 0.0f);
PlatformImeDataPrev.InputPos = ImVec2(-1.0f, -1.0f); // Different to ensure initial submission
- PlatformImeViewport = 0;
DockNodeWindowMenuHandler = NULL;
@@ -4261,6 +4397,18 @@ void ImGui::Initialize()
DockContextInitialize(&g);
#endif
+ // Print a debug message when running with debug feature IMGUI_DEBUG_HIGHLIGHT_ALL_ID_CONFLICTS because it is very slow.
+ // DO NOT COMMENT OUT THIS MESSAGE. IT IS DESIGNED TO REMIND YOU THAT IMGUI_DEBUG_HIGHLIGHT_ALL_ID_CONFLICTS SHOULD ONLY BE TEMPORARILY ENABLED.
+#ifdef IMGUI_DEBUG_HIGHLIGHT_ALL_ID_CONFLICTS
+ DebugLog("IMGUI_DEBUG_HIGHLIGHT_ALL_ID_CONFLICTS is enabled.\nMust disable after use! Otherwise Dear ImGui will run slower.\n");
+#endif
+
+ // ImDrawList/ImFontAtlas are designed to function without ImGui, and 99% of it works without an ImGui context.
+ // But this link allows us to facilitate/handle a few edge cases better.
+ ImFontAtlas* atlas = g.IO.Fonts;
+ g.DrawListSharedData.Context = &g;
+ RegisterFontAtlas(atlas);
+
g.Initialized = true;
}
@@ -4270,14 +4418,22 @@ void ImGui::Shutdown()
ImGuiContext& g = *GImGui;
IM_ASSERT_USER_ERROR(g.IO.BackendPlatformUserData == NULL, "Forgot to shutdown Platform backend?");
IM_ASSERT_USER_ERROR(g.IO.BackendRendererUserData == NULL, "Forgot to shutdown Renderer backend?");
+ for (ImGuiViewportP* viewport : g.Viewports)
+ {
+ IM_UNUSED(viewport);
+ IM_ASSERT_USER_ERROR(viewport->RendererUserData == NULL && viewport->PlatformUserData == NULL && viewport->PlatformHandle == NULL, "Backend or app forgot to call DestroyPlatformWindows()?");
+ }
// The fonts atlas can be used prior to calling NewFrame(), so we clear it even if g.Initialized is FALSE (which would happen if we never called NewFrame)
- if (g.IO.Fonts && g.FontAtlasOwnedByContext)
+ for (ImFontAtlas* atlas : g.FontAtlases)
{
- g.IO.Fonts->Locked = false;
- IM_DELETE(g.IO.Fonts);
+ UnregisterFontAtlas(atlas);
+ if (atlas->RefCount == 0)
+ {
+ atlas->Locked = false;
+ IM_DELETE(atlas);
+ }
}
- g.IO.Fonts = NULL;
g.DrawListSharedData.TempBuffer.clear();
// Cleanup of other data are conditional on actually having initialized Dear ImGui.
@@ -4288,9 +4444,6 @@ void ImGui::Shutdown()
if (g.SettingsLoaded && g.IO.IniFilename != NULL)
SaveIniSettingsToDisk(g.IO.IniFilename);
- // Destroy platform windows
- DestroyPlatformWindows();
-
// Shutdown extensions
DockContextShutdown(&g);
@@ -4336,6 +4489,7 @@ void ImGui::Shutdown()
g.ClipboardHandlerData.clear();
g.MenusIdSubmittedThisFrame.clear();
g.InputTextState.ClearFreeMemory();
+ g.InputTextLineIndex.clear();
g.InputTextDeactivatedState.ClearFreeMemory();
g.SettingsWindows.clear();
@@ -4413,12 +4567,12 @@ ImGuiWindow::ImGuiWindow(ImGuiContext* ctx, const char* name) : DrawListInst(NUL
LastFrameJustFocused = -1;
LastTimeActive = -1.0f;
FontRefSize = 0.0f;
- FontWindowScale = FontWindowScaleParents = FontDpiScale = 1.0f;
+ FontWindowScale = FontWindowScaleParents = 1.0f;
SettingsOffset = -1;
DockOrder = -1;
DrawList = &DrawListInst;
DrawList->_OwnerName = Name;
- DrawList->_Data = &Ctx->DrawListSharedData;
+ DrawList->_SetDrawListSharedData(&Ctx->DrawListSharedData);
NavPreferredScoringPosRel[0] = NavPreferredScoringPosRel[1] = ImVec2(FLT_MAX, FLT_MAX);
IM_PLACEMENT_NEW(&WindowClass) ImGuiWindowClass();
}
@@ -4438,8 +4592,15 @@ static void SetCurrentWindow(ImGuiWindow* window)
g.CurrentTable = window && window->DC.CurrentTableIdx != -1 ? g.Tables.GetByIndex(window->DC.CurrentTableIdx) : NULL;
if (window)
{
- g.FontSize = g.DrawListSharedData.FontSize = window->CalcFontSize();
- g.FontScale = g.DrawListSharedData.FontScale = g.FontSize / g.Font->FontSize;
+ bool backup_skip_items = window->SkipItems;
+ window->SkipItems = false;
+ if (g.IO.BackendFlags & ImGuiBackendFlags_RendererHasTextures)
+ {
+ ImGuiViewport* viewport = window->Viewport;
+ g.FontRasterizerDensity = (viewport->FramebufferScale.x != 0.0f) ? viewport->FramebufferScale.x : g.IO.DisplayFramebufferScale.x; // == SetFontRasterizerDensity()
+ }
+ ImGui::UpdateCurrentFontSize(0.0f);
+ window->SkipItems = backup_skip_items;
ImGui::NavUpdateCurrentWindowIsScrollPushableX();
}
}
@@ -4449,9 +4610,12 @@ void ImGui::GcCompactTransientMiscBuffers()
ImGuiContext& g = *GImGui;
g.ItemFlagsStack.clear();
g.GroupStack.clear();
+ g.InputTextLineIndex.clear();
g.MultiSelectTempDataStacked = 0;
g.MultiSelectTempData.clear_destruct();
TableGcCompactSettings();
+ for (ImFontAtlas* atlas : g.FontAtlases)
+ atlas->CompactCache();
}
// Free up/compact internal window buffers, we can use this when a window becomes unused.
@@ -4487,15 +4651,6 @@ void ImGui::SetActiveID(ImGuiID id, ImGuiWindow* window)
// Clear previous active id
if (g.ActiveId != 0)
{
- // While most behaved code would make an effort to not steal active id during window move/drag operations,
- // we at least need to be resilient to it. Canceling the move is rather aggressive and users of 'master' branch
- // may prefer the weird ill-defined half working situation ('docking' did assert), so may need to rework that.
- if (g.MovingWindow != NULL && g.ActiveId == g.MovingWindow->MoveId)
- {
- IMGUI_DEBUG_LOG_ACTIVEID("SetActiveID() cancel MovingWindow\n");
- g.MovingWindow = NULL;
- }
-
// Store deactivate data
ImGuiDeactivatedItemData* deactivated_data = &g.DeactivatedItemData;
deactivated_data->ID = g.ActiveId;
@@ -4508,6 +4663,15 @@ void ImGui::SetActiveID(ImGuiID id, ImGuiWindow* window)
// One common scenario leading to this is: pressing Key ->NavMoveRequestApplyResult() -> ClearActiveID()
if (g.InputTextState.ID == g.ActiveId)
InputTextDeactivateHook(g.ActiveId);
+
+ // While most behaved code would make an effort to not steal active id during window move/drag operations,
+ // we at least need to be resilient to it. Canceling the move is rather aggressive and users of 'master' branch
+ // may prefer the weird ill-defined half working situation ('docking' did assert), so may need to rework that.
+ if (g.MovingWindow != NULL && g.ActiveId == g.MovingWindow->MoveId)
+ {
+ IMGUI_DEBUG_LOG_ACTIVEID("SetActiveID() cancel MovingWindow\n");
+ StopMouseMovingWindow();
+ }
}
// Set active id
@@ -4531,6 +4695,7 @@ void ImGui::SetActiveID(ImGuiID id, ImGuiWindow* window)
g.ActiveIdWindow = window;
g.ActiveIdHasBeenEditedThisFrame = false;
g.ActiveIdFromShortcut = false;
+ g.ActiveIdDisabledId = 0;
if (id)
{
g.ActiveIdIsAlive = id;
@@ -4680,8 +4845,20 @@ bool ImGui::IsItemHovered(ImGuiHoveredFlags flags)
const ImGuiID id = g.LastItemData.ID;
if ((flags & ImGuiHoveredFlags_AllowWhenBlockedByActiveItem) == 0)
if (g.ActiveId != 0 && g.ActiveId != id && !g.ActiveIdAllowOverlap)
- if (g.ActiveId != window->MoveId && g.ActiveId != window->TabId)
+ {
+ // When ActiveId == MoveId it means that either:
+ // - (1) user clicked on void _or_ an item with no id, which triggers moving window (ActiveId is set even when window has _NoMove flag)
+ // - the (id == 0) test handles it, however, IsItemHovered() will leak between id==0 items (mostly visible when using _NoMove). // FIXME: May be fixed.
+ // - (2) user clicked a disabled item. UpdateMouseMovingWindowEndFrame() uses ActiveId == MoveId to avoid interference with item logic + sets ActiveIdDisabledId.
+ bool cancel_is_hovered = true;
+ if (g.ActiveId == window->MoveId && (id == 0 || g.ActiveIdDisabledId == id))
+ cancel_is_hovered = false;
+ // When ActiveId == TabId it means user clicked docking tab for the window.
+ if (g.ActiveId == window->TabId)
+ cancel_is_hovered = false;
+ if (cancel_is_hovered)
return false;
+ }
// Test if interactions on this window are blocked by an active popup or modal.
// The ImGuiHoveredFlags_AllowWhenBlockedByPopup flag will be tested here.
@@ -4728,7 +4905,8 @@ bool ImGui::IsItemHovered(ImGuiHoveredFlags flags)
return true;
}
-// Internal facing ItemHoverable() used when submitting widgets. Differs slightly from IsItemHovered().
+// Internal facing ItemHoverable() used when submitting widgets. THIS IS A SUBMISSION NOT A HOVER CHECK.
+// Returns whether the item was hovered, logic differs slightly from IsItemHovered().
// (this does not rely on LastItemData it can be called from a ButtonBehavior() call not following an ItemAdd() call)
// FIXME-LEGACY: the 'ImGuiItemFlags item_flags' parameter was added on 2023-06-28.
// If you used this in your legacy/custom widgets code:
@@ -4740,11 +4918,12 @@ bool ImGui::ItemHoverable(const ImRect& bb, ImGuiID id, ImGuiItemFlags item_flag
ImGuiWindow* window = g.CurrentWindow;
// Detect ID conflicts
+ // (this is specifically done here by comparing on hover because it allows us a detection of duplicates that is algorithmically extra cheap, 1 u32 compare per item. No O(log N) lookup whatsoever)
#ifndef IMGUI_DISABLE_DEBUG_TOOLS
if (id != 0 && g.HoveredIdPreviousFrame == id && (item_flags & ImGuiItemFlags_AllowDuplicateId) == 0)
{
g.HoveredIdPreviousFrameItemCount++;
- if (g.DebugDrawIdConflicts == id)
+ if (g.DebugDrawIdConflictsId == id)
window->DrawList->AddRect(bb.Min - ImVec2(1,1), bb.Max + ImVec2(1,1), IM_COL32(255, 0, 0, 255), 0.0f, ImDrawFlags_None, 2.0f);
}
#endif
@@ -4760,7 +4939,7 @@ bool ImGui::ItemHoverable(const ImRect& bb, ImGuiID id, ImGuiItemFlags item_flag
if (!g.ActiveIdFromShortcut)
return false;
- // Done with rectangle culling so we can perform heavier checks now.
+ // We are done with rectangle culling so we can perform heavier checks now.
if (!(item_flags & ImGuiItemFlags_NoWindowHoverableCheck) && !IsWindowContentHoverable(window, ImGuiHoveredFlags_None))
{
g.HoveredIdIsDisabled = true;
@@ -5013,7 +5192,7 @@ static ImDrawList* GetViewportBgFgDrawList(ImGuiViewportP* viewport, size_t draw
if (viewport->BgFgDrawListsLastFrame[drawlist_no] != g.FrameCount)
{
draw_list->_ResetForNewFrame();
- draw_list->PushTextureID(g.IO.Fonts->TexID);
+ draw_list->PushTexture(g.IO.Fonts->TexRef);
draw_list->PushClipRect(viewport->Pos, viewport->Pos + viewport->Size, false);
viewport->BgFgDrawListsLastFrame[drawlist_no] = g.FrameCount;
}
@@ -5086,6 +5265,35 @@ void ImGui::StartMouseMovingWindowOrNode(ImGuiWindow* window, ImGuiDockNode* nod
StartMouseMovingWindow(window);
}
+// This is not 100% symetric with StartMouseMovingWindow().
+// We do NOT clear ActiveID, because:
+// - It would lead to rather confusing recursive code paths. Caller can call ClearActiveID() if desired.
+// - Some code intentionally cancel moving but keep the ActiveID to lock inputs (e.g. code path taken when clicking a disabled item).
+void ImGui::StopMouseMovingWindow()
+{
+ ImGuiContext& g = *GImGui;
+ ImGuiWindow* window = g.MovingWindow;
+
+ // Ref commits 6b7766817, 36055213c for some partial history on checking if viewport != NULL.
+ if (window && window->Viewport)
+ {
+ // Try to merge the window back into the main viewport.
+ // This works because MouseViewport should be != MovingWindow->Viewport on release (as per code in UpdateViewports)
+ if (g.ConfigFlagsCurrFrame & ImGuiConfigFlags_ViewportsEnable)
+ UpdateTryMergeWindowIntoHostViewport(window, g.MouseViewport);
+
+ // Restore the mouse viewport so that we don't hover the viewport _under_ the moved window during the frame we released the mouse button.
+ if (!IsDragDropPayloadBeingAccepted())
+ g.MouseViewport = window->Viewport;
+
+ // Clear the NoInputs window flag set by the Viewport system in AddUpdateViewport()
+ const bool window_can_use_inputs = ((window->Flags & ImGuiWindowFlags_NoMouseInputs) && (window->Flags & ImGuiWindowFlags_NoNavInputs)) == false;
+ if (window_can_use_inputs)
+ window->Viewport->Flags &= ~ImGuiViewportFlags_NoInputs;
+ }
+ g.MovingWindow = NULL;
+}
+
// Handle mouse moving window
// Note: moving window with the navigation keys (Square + d-pad / CTRL+TAB + Arrows) are processed in NavUpdateWindowing()
// FIXME: We don't have strong guarantee that g.MovingWindow stay synced with g.ActiveId == g.MovingWindow->MoveId.
@@ -5103,8 +5311,8 @@ void ImGui::UpdateMouseMovingWindowNewFrame()
ImGuiWindow* moving_window = g.MovingWindow->RootWindowDockTree;
// When a window stop being submitted while being dragged, it may will its viewport until next Begin()
- const bool window_disappared = (!moving_window->WasActive && !moving_window->Active);
- if (g.IO.MouseDown[0] && IsMousePosValid(&g.IO.MousePos) && !window_disappared)
+ const bool window_disappeared = (!moving_window->WasActive && !moving_window->Active);
+ if (g.IO.MouseDown[0] && IsMousePosValid(&g.IO.MousePos) && !window_disappeared)
{
ImVec2 pos = g.IO.MousePos - g.ActiveIdClickOffset;
if (moving_window->Pos.x != pos.x || moving_window->Pos.y != pos.y)
@@ -5120,23 +5328,7 @@ void ImGui::UpdateMouseMovingWindowNewFrame()
}
else
{
- if (!window_disappared)
- {
- // Try to merge the window back into the main viewport.
- // This works because MouseViewport should be != MovingWindow->Viewport on release (as per code in UpdateViewports)
- if (g.ConfigFlagsCurrFrame & ImGuiConfigFlags_ViewportsEnable)
- UpdateTryMergeWindowIntoHostViewport(moving_window, g.MouseViewport);
-
- // Restore the mouse viewport so that we don't hover the viewport _under_ the moved window during the frame we released the mouse button.
- if (moving_window->Viewport && !IsDragDropPayloadBeingAccepted())
- g.MouseViewport = moving_window->Viewport;
-
- // Clear the NoInput window flag set by the Viewport system
- if (moving_window->Viewport)
- moving_window->Viewport->Flags &= ~ImGuiViewportFlags_NoInputs;
- }
-
- g.MovingWindow = NULL;
+ StopMouseMovingWindow();
ClearActiveID();
}
}
@@ -5178,6 +5370,9 @@ void ImGui::UpdateMouseMovingWindowEndFrame()
{
StartMouseMovingWindow(g.HoveredWindow); //-V595
+ // FIXME: In principal we might be able to call StopMouseMovingWindow() below.
+ // Please note how StartMouseMovingWindow() and StopMouseMovingWindow() and not entirely symetrical, at the later doesn't clear ActiveId.
+
// Cancel moving if clicked outside of title bar
if (g.IO.ConfigWindowsMoveFromTitleBarOnly)
if (!(root_window->Flags & ImGuiWindowFlags_NoTitleBar) || root_window->DockIsActive)
@@ -5185,9 +5380,12 @@ void ImGui::UpdateMouseMovingWindowEndFrame()
g.MovingWindow = NULL;
// Cancel moving if clicked over an item which was disabled or inhibited by popups
- // (when g.HoveredIdIsDisabled == true && g.HoveredId == 0 we are inhibited by popups, when g.HoveredIdIsDisabled == true && g.HoveredId != 0 we are over a disabled item)0 already)
+ // (when g.HoveredIdIsDisabled == true && g.HoveredId == 0 we are inhibited by popups, when g.HoveredIdIsDisabled == true && g.HoveredId != 0 we are over a disabled item)
if (g.HoveredIdIsDisabled)
+ {
g.MovingWindow = NULL;
+ g.ActiveIdDisabledId = g.HoveredId;
+ }
}
else if (root_window == NULL && g.NavWindow != NULL)
{
@@ -5368,7 +5566,6 @@ void ImGui::NewFrame()
UpdateSettings();
g.Time += g.IO.DeltaTime;
- g.WithinFrameScope = true;
g.FrameCount += 1;
g.TooltipOverrideCount = 0;
g.WindowsActiveCount = 0;
@@ -5388,12 +5585,14 @@ void ImGui::NewFrame()
// Update viewports (after processing input queue, so io.MouseHoveredViewport is set)
UpdateViewportsNewFrame();
+ // Update texture list (collect destroyed textures, etc.)
+ UpdateTexturesNewFrame();
+
// Setup current font and draw list shared data
- // FIXME-VIEWPORT: the concept of a single ClipRectFullscreen is not ideal!
- g.IO.Fonts->Locked = true;
SetupDrawListSharedData();
- SetCurrentFont(GetDefaultFont());
- IM_ASSERT(g.Font->IsLoaded());
+ UpdateFontsNewFrame();
+
+ g.WithinFrameScope = true;
// Mark rendering data as invalid to prevent user who may have a handle on it to use it.
for (ImGuiViewportP* viewport : g.Viewports)
@@ -5408,9 +5607,9 @@ void ImGui::NewFrame()
// [DEBUG]
if (!g.IO.ConfigDebugHighlightIdConflicts || !g.IO.KeyCtrl) // Count is locked while holding CTRL
- g.DebugDrawIdConflicts = 0;
+ g.DebugDrawIdConflictsId = 0;
if (g.IO.ConfigDebugHighlightIdConflicts && g.HoveredIdPreviousFrameItemCount > 1)
- g.DebugDrawIdConflicts = g.HoveredIdPreviousFrame;
+ g.DebugDrawIdConflictsId = g.HoveredIdPreviousFrame;
// Update HoveredId data
if (!g.HoveredIdPreviousFrame)
@@ -5550,7 +5749,7 @@ void ImGui::NewFrame()
// Platform IME data: reset for the frame
g.PlatformImeDataPrev = g.PlatformImeData;
- g.PlatformImeData.WantVisible = false;
+ g.PlatformImeData.WantVisible = g.PlatformImeData.WantTextInput = false;
// Mouse wheel scrolling, scale
UpdateMouseWheel();
@@ -5718,8 +5917,9 @@ static void InitViewportDrawData(ImGuiViewportP* viewport)
draw_data->TotalVtxCount = draw_data->TotalIdxCount = 0;
draw_data->DisplayPos = viewport->Pos;
draw_data->DisplaySize = is_minimized ? ImVec2(0.0f, 0.0f) : viewport->Size;
- draw_data->FramebufferScale = io.DisplayFramebufferScale; // FIXME-VIEWPORT: This may vary on a per-monitor/viewport basis?
+ draw_data->FramebufferScale = (viewport->FramebufferScale.x != 0.0f) ? viewport->FramebufferScale : io.DisplayFramebufferScale;
draw_data->OwnerViewport = viewport;
+ draw_data->Textures = &ImGui::GetPlatformIO().Textures;
}
// Push a clipping rectangle for both ImGui logic (hit-testing etc.) and low-level ImDrawList rendering.
@@ -5876,7 +6076,11 @@ void ImGui::EndFrame()
// Don't process EndFrame() multiple times.
if (g.FrameCountEnded == g.FrameCount)
return;
- IM_ASSERT(g.WithinFrameScope && "Forgot to call ImGui::NewFrame()?");
+ if (!g.WithinFrameScope)
+ {
+ IM_ASSERT_USER_ERROR(g.WithinFrameScope, "Forgot to call ImGui::NewFrame()?");
+ return;
+ }
CallContextHooks(&g, ImGuiContextHookType_EndFramePre);
@@ -5890,12 +6094,13 @@ void ImGui::EndFrame()
ImGuiPlatformImeData* ime_data = &g.PlatformImeData;
if (g.PlatformIO.Platform_SetImeDataFn != NULL && memcmp(ime_data, &g.PlatformImeDataPrev, sizeof(ImGuiPlatformImeData)) != 0)
{
- ImGuiViewport* viewport = FindViewportByID(g.PlatformImeViewport);
+ ImGuiViewport* viewport = FindViewportByID(ime_data->ViewportId);
IMGUI_DEBUG_LOG_IO("[io] Calling Platform_SetImeDataFn(): WantVisible: %d, InputPos (%.2f,%.2f)\n", ime_data->WantVisible, ime_data->InputPos.x, ime_data->InputPos.y);
if (viewport == NULL)
viewport = GetMainViewport();
g.PlatformIO.Platform_SetImeDataFn(&g, viewport, ime_data);
}
+ g.WantTextInputNextFrame = ime_data->WantTextInput ? 1 : 0;
// Hide implicit/fallback "Debug" window if it hasn't been used
g.WithinFrameScopeWithImplicitWindow = false;
@@ -5935,6 +6140,7 @@ void ImGui::EndFrame()
// End frame
g.WithinFrameScope = false;
g.FrameCountEnded = g.FrameCount;
+ UpdateFontsEndFrame();
// Initiate moving window + handle left-click and right-click focus
UpdateMouseMovingWindowEndFrame();
@@ -5958,8 +6164,11 @@ void ImGui::EndFrame()
g.Windows.swap(g.WindowsTempSortBuffer);
g.IO.MetricsActiveWindows = g.WindowsActiveCount;
+ UpdateTexturesEndFrame();
+
// Unlock font atlas
- g.IO.Fonts->Locked = false;
+ for (ImFontAtlas* atlas : g.FontAtlases)
+ atlas->Locked = false;
// Clear Input data for next frame
g.IO.MousePosPrev = g.IO.MousePos;
@@ -6036,6 +6245,12 @@ void ImGui::Render()
g.IO.MetricsRenderIndices += draw_data->TotalIdxCount;
}
+#ifndef IMGUI_DISABLE_DEBUG_TOOLS
+ if (g.IO.BackendFlags & ImGuiBackendFlags_RendererHasTextures)
+ for (ImFontAtlas* atlas : g.FontAtlases)
+ ImFontAtlasDebugLogTextureRequests(atlas);
+#endif
+
CallContextHooks(&g, ImGuiContextHookType_RenderPost);
}
@@ -6668,10 +6883,10 @@ static void CalcWindowContentSizes(ImGuiWindow* window, ImVec2* content_size_cur
return;
}
- content_size_current->x = (window->ContentSizeExplicit.x != 0.0f) ? window->ContentSizeExplicit.x : IM_TRUNC(window->DC.CursorMaxPos.x - window->DC.CursorStartPos.x);
- content_size_current->y = (window->ContentSizeExplicit.y != 0.0f) ? window->ContentSizeExplicit.y : IM_TRUNC(window->DC.CursorMaxPos.y - window->DC.CursorStartPos.y);
- content_size_ideal->x = (window->ContentSizeExplicit.x != 0.0f) ? window->ContentSizeExplicit.x : IM_TRUNC(ImMax(window->DC.CursorMaxPos.x, window->DC.IdealMaxPos.x) - window->DC.CursorStartPos.x);
- content_size_ideal->y = (window->ContentSizeExplicit.y != 0.0f) ? window->ContentSizeExplicit.y : IM_TRUNC(ImMax(window->DC.CursorMaxPos.y, window->DC.IdealMaxPos.y) - window->DC.CursorStartPos.y);
+ content_size_current->x = (window->ContentSizeExplicit.x != 0.0f) ? window->ContentSizeExplicit.x : ImTrunc64(window->DC.CursorMaxPos.x - window->DC.CursorStartPos.x);
+ content_size_current->y = (window->ContentSizeExplicit.y != 0.0f) ? window->ContentSizeExplicit.y : ImTrunc64(window->DC.CursorMaxPos.y - window->DC.CursorStartPos.y);
+ content_size_ideal->x = (window->ContentSizeExplicit.x != 0.0f) ? window->ContentSizeExplicit.x : ImTrunc64(ImMax(window->DC.CursorMaxPos.x, window->DC.IdealMaxPos.x) - window->DC.CursorStartPos.x);
+ content_size_ideal->y = (window->ContentSizeExplicit.y != 0.0f) ? window->ContentSizeExplicit.y : ImTrunc64(ImMax(window->DC.CursorMaxPos.y, window->DC.IdealMaxPos.y) - window->DC.CursorStartPos.y);
}
static ImVec2 CalcWindowAutoFitSize(ImGuiWindow* window, const ImVec2& size_contents)
@@ -6682,35 +6897,35 @@ static ImVec2 CalcWindowAutoFitSize(ImGuiWindow* window, const ImVec2& size_cont
const float decoration_h_without_scrollbars = window->DecoOuterSizeY1 + window->DecoOuterSizeY2 - window->ScrollbarSizes.y;
ImVec2 size_pad = window->WindowPadding * 2.0f;
ImVec2 size_desired = size_contents + size_pad + ImVec2(decoration_w_without_scrollbars, decoration_h_without_scrollbars);
+
+ // Determine maximum window size
+ // Child windows are layed within their parent (unless they are also popups/menus) and thus have no restriction
+ ImVec2 size_max = ImVec2(FLT_MAX, FLT_MAX);
+ if ((window->Flags & ImGuiWindowFlags_ChildWindow) == 0 || (window->Flags & ImGuiWindowFlags_Popup) != 0)
+ {
+ if (!window->ViewportOwned)
+ size_max = ImGui::GetMainViewport()->WorkSize - style.DisplaySafeAreaPadding * 2.0f;
+ const int monitor_idx = window->ViewportAllowPlatformMonitorExtend;
+ if (monitor_idx >= 0 && monitor_idx < g.PlatformIO.Monitors.Size)
+ size_max = g.PlatformIO.Monitors[monitor_idx].WorkSize - style.DisplaySafeAreaPadding * 2.0f;
+ }
+
if (window->Flags & ImGuiWindowFlags_Tooltip)
{
- // Tooltip always resize
- return size_desired;
+ // Tooltip always resize (up to maximum size)
+ return ImMin(size_desired, size_max);
}
else
{
- // Maximum window size is determined by the viewport size or monitor size
ImVec2 size_min = CalcWindowMinSize(window);
- ImVec2 size_max = ImVec2(FLT_MAX, FLT_MAX);
-
- // Child windows are layed within their parent (unless they are also popups/menus) and thus have no restriction
- if ((window->Flags & ImGuiWindowFlags_ChildWindow) == 0 || (window->Flags & ImGuiWindowFlags_Popup) != 0)
- {
- if (!window->ViewportOwned)
- size_max = ImGui::GetMainViewport()->WorkSize - style.DisplaySafeAreaPadding * 2.0f;
- const int monitor_idx = window->ViewportAllowPlatformMonitorExtend;
- if (monitor_idx >= 0 && monitor_idx < g.PlatformIO.Monitors.Size)
- size_max = g.PlatformIO.Monitors[monitor_idx].WorkSize - style.DisplaySafeAreaPadding * 2.0f;
- }
-
- ImVec2 size_auto_fit = ImClamp(size_desired, size_min, ImMax(size_min, size_max));
+ ImVec2 size_auto_fit = ImClamp(size_desired, ImMin(size_min, size_max), size_max);
// FIXME: CalcWindowAutoFitSize() doesn't take into account that only one axis may be auto-fit when calculating scrollbars,
// we may need to compute/store three variants of size_auto_fit, for x/y/xy.
// Here we implement a workaround for child windows only, but a full solution would apply to normal windows as well:
- if ((window->ChildFlags & ImGuiChildFlags_ResizeX) && !(window->ChildFlags & ImGuiChildFlags_ResizeY))
+ if ((window->ChildFlags & ImGuiChildFlags_ResizeX) && !(window->ChildFlags & (ImGuiChildFlags_ResizeY | ImGuiChildFlags_AutoResizeY)))
size_auto_fit.y = window->SizeFull.y;
- else if (!(window->ChildFlags & ImGuiChildFlags_ResizeX) && (window->ChildFlags & ImGuiChildFlags_ResizeY))
+ else if ((window->ChildFlags & ImGuiChildFlags_ResizeY) && !(window->ChildFlags & (ImGuiChildFlags_ResizeX | ImGuiChildFlags_AutoResizeX)))
size_auto_fit.x = window->SizeFull.x;
// When the window cannot fit all contents (either because of constraints, either because screen is too small),
@@ -6745,8 +6960,20 @@ static ImGuiCol GetWindowBgColorIdx(ImGuiWindow* window)
return ImGuiCol_WindowBg;
}
-static void CalcResizePosSizeFromAnyCorner(ImGuiWindow* window, const ImVec2& corner_target, const ImVec2& corner_norm, ImVec2* out_pos, ImVec2* out_size)
+static void CalcResizePosSizeFromAnyCorner(ImGuiWindow* window, const ImVec2& corner_target_arg, const ImVec2& corner_norm, ImVec2* out_pos, ImVec2* out_size)
{
+ ImVec2 corner_target = corner_target_arg;
+ if (window->Flags & ImGuiWindowFlags_ChildWindow) // Clamp resizing of childs within parent
+ {
+ ImGuiWindow* parent_window = window->ParentWindow;
+ ImGuiWindowFlags parent_flags = parent_window->Flags;
+ ImRect limit_rect = parent_window->InnerRect;
+ limit_rect.Expand(ImVec2(-ImMax(parent_window->WindowPadding.x, parent_window->WindowBorderSize), -ImMax(parent_window->WindowPadding.y, parent_window->WindowBorderSize)));
+ if ((parent_flags & (ImGuiWindowFlags_HorizontalScrollbar | ImGuiWindowFlags_AlwaysHorizontalScrollbar)) == 0 || (parent_flags & ImGuiWindowFlags_NoScrollbar))
+ corner_target.x = ImClamp(corner_target.x, limit_rect.Min.x, limit_rect.Max.x);
+ if (parent_flags & ImGuiWindowFlags_NoScrollbar)
+ corner_target.y = ImClamp(corner_target.y, limit_rect.Min.y, limit_rect.Max.y);
+ }
ImVec2 pos_min = ImLerp(corner_target, window->Pos, corner_norm); // Expected window upper-left
ImVec2 pos_max = ImLerp(window->Pos + window->Size, corner_target, corner_norm); // Expected window lower-right
ImVec2 size_expected = pos_max - pos_min;
@@ -6830,7 +7057,9 @@ static int ImGui::UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& si
ImGuiContext& g = *GImGui;
ImGuiWindowFlags flags = window->Flags;
- if ((flags & ImGuiWindowFlags_NoResize) || (flags & ImGuiWindowFlags_AlwaysAutoResize) || window->AutoFitFramesX > 0 || window->AutoFitFramesY > 0)
+ if ((flags & ImGuiWindowFlags_NoResize) || window->AutoFitFramesX > 0 || window->AutoFitFramesY > 0)
+ return false;
+ if ((flags & ImGuiWindowFlags_AlwaysAutoResize) && (window->ChildFlags & (ImGuiChildFlags_ResizeX | ImGuiChildFlags_ResizeY)) == 0)
return false;
if (window->WasActive == false) // Early out to avoid running this code for e.g. a hidden implicit/fallback Debug window.
return false;
@@ -6899,7 +7128,8 @@ static int ImGui::UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& si
}
// Only lower-left grip is visible before hovering/activating
- if (resize_grip_n == 0 || held || hovered)
+ const bool resize_grip_visible = held || hovered || (resize_grip_n == 0 && (window->Flags & ImGuiWindowFlags_ChildWindow) == 0);
+ if (resize_grip_visible)
resize_grip_col[resize_grip_n] = GetColorU32(held ? ImGuiCol_ResizeGripActive : hovered ? ImGuiCol_ResizeGripHovered : ImGuiCol_ResizeGrip);
}
@@ -6974,17 +7204,6 @@ static int ImGui::UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& si
ImVec2 clamp_min(border_n == ImGuiDir_Right ? clamp_rect.Min.x : -FLT_MAX, border_n == ImGuiDir_Down || (border_n == ImGuiDir_Up && window_move_from_title_bar) ? clamp_rect.Min.y : -FLT_MAX);
ImVec2 clamp_max(border_n == ImGuiDir_Left ? clamp_rect.Max.x : +FLT_MAX, border_n == ImGuiDir_Up ? clamp_rect.Max.y : +FLT_MAX);
border_target = ImClamp(border_target, clamp_min, clamp_max);
- if (flags & ImGuiWindowFlags_ChildWindow) // Clamp resizing of childs within parent
- {
- ImGuiWindow* parent_window = window->ParentWindow;
- ImGuiWindowFlags parent_flags = parent_window->Flags;
- ImRect border_limit_rect = parent_window->InnerRect;
- border_limit_rect.Expand(ImVec2(-ImMax(parent_window->WindowPadding.x, parent_window->WindowBorderSize), -ImMax(parent_window->WindowPadding.y, parent_window->WindowBorderSize)));
- if ((axis == ImGuiAxis_X) && ((parent_flags & (ImGuiWindowFlags_HorizontalScrollbar | ImGuiWindowFlags_AlwaysHorizontalScrollbar)) == 0 || (parent_flags & ImGuiWindowFlags_NoScrollbar)))
- border_target.x = ImClamp(border_target.x, border_limit_rect.Min.x, border_limit_rect.Max.x);
- if ((axis == ImGuiAxis_Y) && (parent_flags & ImGuiWindowFlags_NoScrollbar))
- border_target.y = ImClamp(border_target.y, border_limit_rect.Min.y, border_limit_rect.Max.y);
- }
if (!ignore_resize)
CalcResizePosSizeFromAnyCorner(window, border_target, ImMin(def.SegmentN1, def.SegmentN2), &pos_target, &size_target);
}
@@ -7293,8 +7512,13 @@ void ImGui::RenderWindowTitleBarContents(ImGuiWindow* window, const ImRect& titl
// Close button
if (has_close_button)
+ {
+ ImGuiItemFlags backup_item_flags = g.CurrentItemFlags;
+ g.CurrentItemFlags |= ImGuiItemFlags_NoFocus;
if (CloseButton(window->GetID("#CLOSE"), close_button_pos))
*p_open = false;
+ g.CurrentItemFlags = backup_item_flags;
+ }
window->DC.NavLayerCurrent = ImGuiNavLayer_Main;
g.CurrentItemFlags = item_flags_backup;
@@ -7553,7 +7777,7 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags)
}
// Process SetNextWindow***() calls
- // (FIXME: Consider splitting the HasXXX flags into X/Y components
+ // (FIXME: Consider splitting the HasXXX flags into X/Y components)
bool window_pos_set_by_api = false;
bool window_size_x_set_by_api = false, window_size_y_set_by_api = false;
if (g.NextWindowData.HasFlags & ImGuiNextWindowDataFlags_HasPos)
@@ -7695,7 +7919,6 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags)
WindowSelectViewport(window);
SetCurrentViewport(window, window->Viewport);
- window->FontDpiScale = (g.IO.ConfigFlags & ImGuiConfigFlags_DpiEnableScaleFonts) ? window->Viewport->DpiScale : 1.0f;
SetCurrentWindow(window);
flags = window->Flags;
@@ -7840,7 +8063,6 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags)
// FIXME-DPI
//IM_ASSERT(old_viewport->DpiScale == window->Viewport->DpiScale); // FIXME-DPI: Something went wrong
SetCurrentViewport(window, window->Viewport);
- window->FontDpiScale = (g.IO.ConfigFlags & ImGuiConfigFlags_DpiEnableScaleFonts) ? window->Viewport->DpiScale : 1.0f;
SetCurrentWindow(window);
}
@@ -7920,12 +8142,22 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags)
#endif
// Decide if we are going to handle borders and resize grips
- const bool handle_borders_and_resize_grips = (window->DockNodeAsHost || !window->DockIsActive);
+ // 'window->SkipItems' is not updated yet so for child windows we rely on ParentWindow to avoid submitting decorations. (#8815)
+ // Whenever we add support for full decorated child windows we will likely make this logic more general.
+ bool handle_borders_and_resize_grips = (window->DockNodeAsHost || !window->DockIsActive);
+ if ((flags & ImGuiWindowFlags_ChildWindow) && window->ParentWindow->SkipItems)
+ handle_borders_and_resize_grips = false;
// Handle manual resize: Resize Grips, Borders, Gamepad
+ // Child windows can only be resized when they have the flags set. The resize grip allows resizing in both directions, so it should appear only if both flags are set.
int border_hovered = -1, border_held = -1;
ImU32 resize_grip_col[4] = {};
- const int resize_grip_count = ((flags & ImGuiWindowFlags_ChildWindow) && !(flags & ImGuiWindowFlags_Popup)) ? 0 : g.IO.ConfigWindowsResizeFromEdges ? 2 : 1; // Allow resize from lower-left if we have the mouse cursor feedback for it.
+ int resize_grip_count;
+ if ((flags & ImGuiWindowFlags_ChildWindow) && !(flags & ImGuiWindowFlags_Popup))
+ resize_grip_count = ((window->ChildFlags & ImGuiChildFlags_ResizeX) && (window->ChildFlags & ImGuiChildFlags_ResizeY)) ? 1 : 0;
+ else
+ resize_grip_count = g.IO.ConfigWindowsResizeFromEdges ? 2 : 1; // Allow resize from lower-left if we have the mouse cursor feedback for it.
+
const float resize_grip_draw_size = IM_TRUNC(ImMax(g.FontSize * 1.10f, window->WindowRounding + 1.0f + g.FontSize * 0.2f));
if (handle_borders_and_resize_grips && !window->Collapsed)
if (int auto_fit_mask = UpdateWindowManualResize(window, size_auto_fit, &border_hovered, &border_held, resize_grip_count, &resize_grip_col[0], visibility_rect))
@@ -8039,12 +8271,6 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags)
window->InnerClipRect.Max.y = ImFloor(window->InnerRect.Max.y - window->WindowBorderSize * 0.5f);
window->InnerClipRect.ClipWithFull(host_rect);
- // Default item width. Make it proportional to window size if window manually resizes
- if (window->Size.x > 0.0f && !(flags & ImGuiWindowFlags_Tooltip) && !(flags & ImGuiWindowFlags_AlwaysAutoResize))
- window->ItemWidthDefault = ImTrunc(window->Size.x * 0.65f);
- else
- window->ItemWidthDefault = ImTrunc(g.FontSize * 16.0f);
-
// SCROLLING
// Lock down maximum scrolling
@@ -8062,7 +8288,7 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags)
// Setup draw list and outer clipping rectangle
IM_ASSERT(window->DrawList->CmdBuffer.Size == 1 && window->DrawList->CmdBuffer[0].ElemCount == 0);
- window->DrawList->PushTextureID(g.Font->ContainerAtlas->TexID);
+ window->DrawList->PushTexture(g.Font->ContainerAtlas->TexRef);
PushClipRect(host_rect.Min, host_rect.Max, false);
// Child windows can render their decoration (bg color, border, scrollbars, etc.) within their parent to save a draw call (since 1.71)
@@ -8151,13 +8377,18 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags)
window->DC.MenuBarAppending = false;
window->DC.MenuColumns.Update(style.ItemSpacing.x, window_just_activated_by_user);
window->DC.TreeDepth = 0;
- window->DC.TreeHasStackDataDepthMask = 0x00;
+ window->DC.TreeHasStackDataDepthMask = window->DC.TreeRecordsClippedNodesY2Mask = 0x00;
window->DC.ChildWindows.resize(0);
window->DC.StateStorage = &window->StateStorage;
window->DC.CurrentColumns = NULL;
window->DC.LayoutType = ImGuiLayoutType_Vertical;
window->DC.ParentLayoutType = parent_window ? parent_window->DC.LayoutType : ImGuiLayoutType_Vertical;
+ // Default item width. Make it proportional to window size if window manually resizes
+ if (window->Size.x > 0.0f && !(flags & ImGuiWindowFlags_Tooltip) && !(flags & ImGuiWindowFlags_AlwaysAutoResize))
+ window->ItemWidthDefault = ImTrunc(window->Size.x * 0.65f);
+ else
+ window->ItemWidthDefault = ImTrunc(g.FontSize * 16.0f);
window->DC.ItemWidth = window->ItemWidthDefault;
window->DC.TextWrapPos = -1.0f; // disabled
window->DC.ItemWidthStack.resize(0);
@@ -8183,12 +8414,14 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags)
NavInitWindow(window, false); // <-- this is in the way for us to be able to defer and sort reappearing FocusWindow() calls
// Close requested by platform window (apply to all windows in this viewport)
+ // FIXME: Investigate removing the 'window->Viewport != GetMainViewport()' test, which seems superfluous.
if (p_open != NULL && window->Viewport->PlatformRequestClose && window->Viewport != GetMainViewport())
- {
- IMGUI_DEBUG_LOG_VIEWPORT("[viewport] Window '%s' closed by PlatformRequestClose\n", window->Name);
- *p_open = false;
- g.NavWindowingToggleLayer = false; // Assume user mapped PlatformRequestClose on ALT-F4 so we disable ALT for menu toggle. False positive not an issue. // FIXME-NAV: Try removing.
- }
+ if (window->DockNode == NULL || (window->DockNode->MergedFlags & ImGuiDockNodeFlags_DockSpace) == 0)
+ {
+ IMGUI_DEBUG_LOG_VIEWPORT("[viewport] Window '%s' closed by PlatformRequestClose\n", window->Name);
+ *p_open = false;
+ g.NavWindowingToggleLayer = false; // Assume user mapped PlatformRequestClose on ALT-F4 so we disable ALT for menu toggle. False positive not an issue. // FIXME-NAV: Try removing.
+ }
// Pressing CTRL+C copy window content into the clipboard
// [EXPERIMENTAL] Breaks on nested Begin/End pairs. We need to work that out and add better logging scope.
@@ -8416,56 +8649,6 @@ void ImGui::End()
SetCurrentViewport(g.CurrentWindow, g.CurrentWindow->Viewport);
}
-// Important: this alone doesn't alter current ImDrawList state. This is called by PushFont/PopFont only.
-void ImGui::SetCurrentFont(ImFont* font)
-{
- ImGuiContext& g = *GImGui;
- IM_ASSERT(font && font->IsLoaded()); // Font Atlas not created. Did you call io.Fonts->GetTexDataAsRGBA32 / GetTexDataAsAlpha8 ?
- IM_ASSERT(font->Scale > 0.0f);
- g.Font = font;
- g.FontBaseSize = ImMax(1.0f, g.IO.FontGlobalScale * g.Font->FontSize * g.Font->Scale);
- g.FontSize = g.CurrentWindow ? g.CurrentWindow->CalcFontSize() : 0.0f;
- g.FontScale = g.FontSize / g.Font->FontSize;
-
- ImFontAtlas* atlas = g.Font->ContainerAtlas;
- g.DrawListSharedData.TexUvWhitePixel = atlas->TexUvWhitePixel;
- g.DrawListSharedData.TexUvLines = atlas->TexUvLines;
- g.DrawListSharedData.Font = g.Font;
- g.DrawListSharedData.FontSize = g.FontSize;
- g.DrawListSharedData.FontScale = g.FontScale;
-}
-
-// Use ImDrawList::_SetTextureID(), making our shared g.FontStack[] authoritative against window-local ImDrawList.
-// - Whereas ImDrawList::PushTextureID()/PopTextureID() is not to be used across Begin() calls.
-// - Note that we don't propagate current texture id when e.g. Begin()-ing into a new window, we never really did...
-// - Some code paths never really fully worked with multiple atlas textures.
-// - The right-ish solution may be to remove _SetTextureID() and make AddText/RenderText lazily call PushTextureID()/PopTextureID()
-// the same way AddImage() does, but then all other primitives would also need to? I don't think we should tackle this problem
-// because we have a concrete need and a test bed for multiple atlas textures.
-void ImGui::PushFont(ImFont* font)
-{
- ImGuiContext& g = *GImGui;
- if (font == NULL)
- font = GetDefaultFont();
- g.FontStack.push_back(font);
- SetCurrentFont(font);
- g.CurrentWindow->DrawList->_SetTextureID(font->ContainerAtlas->TexID);
-}
-
-void ImGui::PopFont()
-{
- ImGuiContext& g = *GImGui;
- if (g.FontStack.Size <= 0)
- {
- IM_ASSERT_USER_ERROR(0, "Calling PopFont() too many times!");
- return;
- }
- g.FontStack.pop_back();
- ImFont* font = g.FontStack.Size == 0 ? GetDefaultFont() : g.FontStack.back();
- SetCurrentFont(font);
- g.CurrentWindow->DrawList->_SetTextureID(font->ContainerAtlas->TexID);
-}
-
void ImGui::PushItemFlag(ImGuiItemFlags option, bool enabled)
{
ImGuiContext& g = *GImGui;
@@ -8885,7 +9068,7 @@ void ImGui::SetNextWindowSizeConstraints(const ImVec2& size_min, const ImVec2& s
}
// Content size = inner scrollable rectangle, padded with WindowPadding.
-// SetNextWindowContentSize(ImVec2(100,100) + ImGuiWindowFlags_AlwaysAutoResize will always allow submitting a 100x100 item.
+// SetNextWindowContentSize(ImVec2(100,100)) + ImGuiWindowFlags_AlwaysAutoResize will always allow submitting a 100x100 item.
void ImGui::SetNextWindowContentSize(const ImVec2& size)
{
ImGuiContext& g = *GImGui;
@@ -8971,6 +9154,14 @@ ImFont* ImGui::GetFont()
return GImGui->Font;
}
+ImFontBaked* ImGui::GetFontBaked()
+{
+ return GImGui->FontBaked;
+}
+
+// Get current font size (= height in pixels) of current font, with global scale factors applied.
+// - Use style.FontSizeBase to get value before global scale factors.
+// - recap: ImGui::GetFontSize() == style.FontSizeBase * (style.FontScaleMain * style.FontScaleDpi * other_scaling_factors)
float ImGui::GetFontSize()
{
return GImGui->FontSize;
@@ -8981,15 +9172,16 @@ ImVec2 ImGui::GetFontTexUvWhitePixel()
return GImGui->DrawListSharedData.TexUvWhitePixel;
}
+// Prefer using PushFont(NULL, style.FontSizeBase * factor), or use style.FontScaleMain to scale all windows.
+#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
void ImGui::SetWindowFontScale(float scale)
{
IM_ASSERT(scale > 0.0f);
- ImGuiContext& g = *GImGui;
ImGuiWindow* window = GetCurrentWindow();
window->FontWindowScale = scale;
- g.FontSize = g.DrawListSharedData.FontSize = window->CalcFontSize();
- g.FontScale = g.DrawListSharedData.FontScale = g.FontSize / g.Font->FontSize;
+ UpdateCurrentFontSize(0.0f);
}
+#endif
void ImGui::PushFocusScope(ImGuiID id)
{
@@ -9143,6 +9335,272 @@ bool ImGui::IsRectVisible(const ImVec2& rect_min, const ImVec2& rect_max)
return window->ClipRect.Overlaps(ImRect(rect_min, rect_max));
}
+//-----------------------------------------------------------------------------
+// [SECTION] FONTS, TEXTURES
+//-----------------------------------------------------------------------------
+// Most of the relevant font logic is in imgui_draw.cpp.
+// Those are high-level support functions.
+//-----------------------------------------------------------------------------
+// - UpdateTexturesNewFrame() [Internal]
+// - UpdateTexturesEndFrame() [Internal]
+// - UpdateFontsNewFrame() [Internal]
+// - UpdateFontsEndFrame() [Internal]
+// - GetDefaultFont() [Internal]
+// - RegisterUserTexture() [Internal]
+// - UnregisterUserTexture() [Internal]
+// - RegisterFontAtlas() [Internal]
+// - UnregisterFontAtlas() [Internal]
+// - SetCurrentFont() [Internal]
+// - UpdateCurrentFontSize() [Internal]
+// - SetFontRasterizerDensity() [Internal]
+// - PushFont()
+// - PopFont()
+//-----------------------------------------------------------------------------
+
+static void ImGui::UpdateTexturesNewFrame()
+{
+ // Cannot update every atlases based on atlas's FrameCount < g.FrameCount, because an atlas may be shared by multiple contexts with different frame count.
+ ImGuiContext& g = *GImGui;
+ const bool has_textures = (g.IO.BackendFlags & ImGuiBackendFlags_RendererHasTextures) != 0;
+ for (ImFontAtlas* atlas : g.FontAtlases)
+ {
+ if (atlas->OwnerContext == &g)
+ {
+ ImFontAtlasUpdateNewFrame(atlas, g.FrameCount, has_textures);
+ }
+ else
+ {
+ // (1) If you manage font atlases yourself, e.g. create a ImFontAtlas yourself you need to call ImFontAtlasUpdateNewFrame() on it.
+ // Otherwise, calling ImGui::CreateContext() without parameter will create an atlas owned by the context.
+ // (2) If you have multiple font atlases, make sure the 'atlas->RendererHasTextures' as specified in the ImFontAtlasUpdateNewFrame() call matches for that.
+ // (3) If you have multiple imgui contexts, they also need to have a matching value for ImGuiBackendFlags_RendererHasTextures.
+ IM_ASSERT(atlas->Builder != NULL && atlas->Builder->FrameCount != -1);
+ IM_ASSERT(atlas->RendererHasTextures == has_textures);
+ }
+ }
+}
+
+// Build a single texture list
+static void ImGui::UpdateTexturesEndFrame()
+{
+ ImGuiContext& g = *GImGui;
+ g.PlatformIO.Textures.resize(0);
+ for (ImFontAtlas* atlas : g.FontAtlases)
+ for (ImTextureData* tex : atlas->TexList)
+ {
+ // We provide this information so backends can decide whether to destroy textures.
+ // This means in practice that if N imgui contexts are created with a shared atlas, we assume all of them have a backend initialized.
+ tex->RefCount = (unsigned short)atlas->RefCount;
+ g.PlatformIO.Textures.push_back(tex);
+ }
+ for (ImTextureData* tex : g.UserTextures)
+ g.PlatformIO.Textures.push_back(tex);
+}
+
+void ImGui::UpdateFontsNewFrame()
+{
+ ImGuiContext& g = *GImGui;
+ if ((g.IO.BackendFlags & ImGuiBackendFlags_RendererHasTextures) == 0)
+ for (ImFontAtlas* atlas : g.FontAtlases)
+ atlas->Locked = true;
+
+ if (g.Style._NextFrameFontSizeBase != 0.0f)
+ {
+ g.Style.FontSizeBase = g.Style._NextFrameFontSizeBase;
+ g.Style._NextFrameFontSizeBase = 0.0f;
+ }
+
+ // Apply default font size the first time
+ ImFont* font = ImGui::GetDefaultFont();
+ if (g.Style.FontSizeBase <= 0.0f)
+ g.Style.FontSizeBase = (font->LegacySize > 0.0f ? font->LegacySize : FONT_DEFAULT_SIZE);
+
+ // Set initial font
+ g.Font = font;
+ g.FontSizeBase = g.Style.FontSizeBase;
+ g.FontSize = 0.0f;
+ ImFontStackData font_stack_data = { font, g.Style.FontSizeBase, g.Style.FontSizeBase }; // <--- Will restore FontSize
+ SetCurrentFont(font_stack_data.Font, font_stack_data.FontSizeBeforeScaling, 0.0f); // <--- but use 0.0f to enable scale
+ g.FontStack.push_back(font_stack_data);
+ IM_ASSERT(g.Font->IsLoaded());
+}
+
+void ImGui::UpdateFontsEndFrame()
+{
+ PopFont();
+}
+
+ImFont* ImGui::GetDefaultFont()
+{
+ ImGuiContext& g = *GImGui;
+ ImFontAtlas* atlas = g.IO.Fonts;
+ if (atlas->Builder == NULL || atlas->Fonts.Size == 0)
+ ImFontAtlasBuildMain(atlas);
+ return g.IO.FontDefault ? g.IO.FontDefault : atlas->Fonts[0];
+}
+
+// EXPERIMENTAL: DO NOT USE YET.
+void ImGui::RegisterUserTexture(ImTextureData* tex)
+{
+ ImGuiContext& g = *GImGui;
+ tex->RefCount++;
+ g.UserTextures.push_back(tex);
+}
+
+void ImGui::UnregisterUserTexture(ImTextureData* tex)
+{
+ ImGuiContext& g = *GImGui;
+ IM_ASSERT(tex->RefCount > 0);
+ tex->RefCount--;
+ g.UserTextures.find_erase(tex);
+}
+
+void ImGui::RegisterFontAtlas(ImFontAtlas* atlas)
+{
+ ImGuiContext& g = *GImGui;
+ if (g.FontAtlases.Size == 0)
+ IM_ASSERT(atlas == g.IO.Fonts);
+ atlas->RefCount++;
+ g.FontAtlases.push_back(atlas);
+ ImFontAtlasAddDrawListSharedData(atlas, &g.DrawListSharedData);
+ for (ImTextureData* tex : atlas->TexList)
+ tex->RefCount = (unsigned short)atlas->RefCount;
+}
+
+void ImGui::UnregisterFontAtlas(ImFontAtlas* atlas)
+{
+ ImGuiContext& g = *GImGui;
+ IM_ASSERT(atlas->RefCount > 0);
+ ImFontAtlasRemoveDrawListSharedData(atlas, &g.DrawListSharedData);
+ g.FontAtlases.find_erase(atlas);
+ atlas->RefCount--;
+ for (ImTextureData* tex : atlas->TexList)
+ tex->RefCount = (unsigned short)atlas->RefCount;
+}
+
+// Use ImDrawList::_SetTexture(), making our shared g.FontStack[] authoritative against window-local ImDrawList.
+// - Whereas ImDrawList::PushTexture()/PopTexture() is not to be used across Begin() calls.
+// - Note that we don't propagate current texture id when e.g. Begin()-ing into a new window, we never really did...
+// - Some code paths never really fully worked with multiple atlas textures.
+// - The right-ish solution may be to remove _SetTexture() and make AddText/RenderText lazily call PushTexture()/PopTexture()
+// the same way AddImage() does, but then all other primitives would also need to? I don't think we should tackle this problem
+// because we have a concrete need and a test bed for multiple atlas textures.
+// FIXME-NEWATLAS-V2: perhaps we can now leverage ImFontAtlasUpdateDrawListsTextures() ?
+void ImGui::SetCurrentFont(ImFont* font, float font_size_before_scaling, float font_size_after_scaling)
+{
+ ImGuiContext& g = *GImGui;
+ g.Font = font;
+ g.FontSizeBase = font_size_before_scaling;
+ UpdateCurrentFontSize(font_size_after_scaling);
+
+ if (font != NULL)
+ {
+ IM_ASSERT(font && font->IsLoaded()); // Font Atlas not created. Did you call io.Fonts->GetTexDataAsRGBA32 / GetTexDataAsAlpha8 ?
+#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
+ IM_ASSERT(font->Scale > 0.0f);
+#endif
+ ImFontAtlas* atlas = font->ContainerAtlas;
+ g.DrawListSharedData.FontAtlas = atlas;
+ g.DrawListSharedData.Font = font;
+ ImFontAtlasUpdateDrawListsSharedData(atlas);
+ if (g.CurrentWindow != NULL)
+ g.CurrentWindow->DrawList->_SetTexture(atlas->TexRef);
+ }
+}
+
+void ImGui::UpdateCurrentFontSize(float restore_font_size_after_scaling)
+{
+ ImGuiContext& g = *GImGui;
+ ImGuiWindow* window = g.CurrentWindow;
+
+ g.Style.FontSizeBase = g.FontSizeBase;
+
+ // Early out to avoid hidden window keeping bakes referenced and out of GC reach.
+ // However this would leave a pretty subtle and damning error surface area if g.FontBaked was mismatching.
+ // FIXME: perhaps g.FontSize should be updated?
+ if (window != NULL && window->SkipItems)
+ {
+ ImGuiTable* table = g.CurrentTable;
+ if (table == NULL || (table->CurrentColumn != -1 && table->Columns[table->CurrentColumn].IsSkipItems == false)) // See 8465#issuecomment-2951509561 and #8865. Ideally the SkipItems=true in tables would be amended with extra data.
+ return;
+ }
+
+ // Restoring is pretty much only used by PopFont()
+ float final_size = (restore_font_size_after_scaling > 0.0f) ? restore_font_size_after_scaling : 0.0f;
+ if (final_size == 0.0f)
+ {
+ final_size = g.FontSizeBase;
+
+ // Global scale factors
+ final_size *= g.Style.FontScaleMain; // Main global scale factor
+ final_size *= g.Style.FontScaleDpi; // Per-monitor/viewport DPI scale factor, automatically updated when io.ConfigDpiScaleFonts is enabled.
+
+ // Window scale (mostly obsolete now)
+ if (window != NULL)
+ final_size *= window->FontWindowScale;
+
+ // Legacy scale factors
+#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
+ final_size *= g.IO.FontGlobalScale; // Use style.FontScaleMain instead!
+ if (g.Font != NULL)
+ final_size *= g.Font->Scale; // Was never really useful.
+#endif
+ }
+
+ // Round font size
+ // - We started rounding in 1.90 WIP (18991) as our layout system currently doesn't support non-rounded font size well yet.
+ // - We may support it better later and remove this rounding.
+ final_size = GetRoundedFontSize(final_size);
+ final_size = ImClamp(final_size, 1.0f, IMGUI_FONT_SIZE_MAX);
+ if (g.Font != NULL && (g.IO.BackendFlags & ImGuiBackendFlags_RendererHasTextures))
+ g.Font->CurrentRasterizerDensity = g.FontRasterizerDensity;
+ g.FontSize = final_size;
+ g.FontBaked = (g.Font != NULL && window != NULL) ? g.Font->GetFontBaked(final_size) : NULL;
+ g.FontBakedScale = (g.Font != NULL && window != NULL) ? (g.FontSize / g.FontBaked->Size) : 0.0f;
+ g.DrawListSharedData.FontSize = g.FontSize;
+ g.DrawListSharedData.FontScale = g.FontBakedScale;
+}
+
+// Exposed in case user may want to override setting density.
+// IMPORTANT: Begin()/End() is overriding density. Be considerate of this you change it.
+void ImGui::SetFontRasterizerDensity(float rasterizer_density)
+{
+ ImGuiContext& g = *GImGui;
+ IM_ASSERT(g.IO.BackendFlags & ImGuiBackendFlags_RendererHasTextures);
+ if (g.FontRasterizerDensity == rasterizer_density)
+ return;
+ g.FontRasterizerDensity = rasterizer_density;
+ UpdateCurrentFontSize(0.0f);
+}
+
+// If you want to scale an existing font size! Read comments in imgui.h!
+void ImGui::PushFont(ImFont* font, float font_size_base)
+{
+ ImGuiContext& g = *GImGui;
+ if (font == NULL) // Before 1.92 (June 2025), PushFont(NULL) == PushFont(GetDefaultFont())
+ font = g.Font;
+ IM_ASSERT(font != NULL);
+ IM_ASSERT(font_size_base >= 0.0f);
+
+ g.FontStack.push_back({ g.Font, g.FontSizeBase, g.FontSize });
+ if (font_size_base == 0.0f)
+ font_size_base = g.FontSizeBase; // Keep current font size
+ SetCurrentFont(font, font_size_base, 0.0f);
+}
+
+void ImGui::PopFont()
+{
+ ImGuiContext& g = *GImGui;
+ if (g.FontStack.Size <= 0)
+ {
+ IM_ASSERT_USER_ERROR(0, "Calling PopFont() too many times!");
+ return;
+ }
+ ImFontStackData* font_stack_data = &g.FontStack.back();
+ SetCurrentFont(font_stack_data->Font, font_stack_data->FontSizeBeforeScaling, font_stack_data->FontSizeAfterScaling);
+ g.FontStack.pop_back();
+}
+
//-----------------------------------------------------------------------------
// [SECTION] ID STACK
//-----------------------------------------------------------------------------
@@ -9156,7 +9614,7 @@ ImGuiID ImGuiWindow::GetID(const char* str, const char* str_end)
ImGuiID id = ImHashStr(str, str_end ? (str_end - str) : 0, seed);
#ifndef IMGUI_DISABLE_DEBUG_TOOLS
ImGuiContext& g = *Ctx;
- if (g.DebugHookIdInfo == id)
+ if (g.DebugHookIdInfoId == id)
ImGui::DebugHookIdInfo(id, ImGuiDataType_String, str, str_end);
#endif
return id;
@@ -9168,7 +9626,7 @@ ImGuiID ImGuiWindow::GetID(const void* ptr)
ImGuiID id = ImHashData(&ptr, sizeof(void*), seed);
#ifndef IMGUI_DISABLE_DEBUG_TOOLS
ImGuiContext& g = *Ctx;
- if (g.DebugHookIdInfo == id)
+ if (g.DebugHookIdInfoId == id)
ImGui::DebugHookIdInfo(id, ImGuiDataType_Pointer, ptr, NULL);
#endif
return id;
@@ -9180,7 +9638,7 @@ ImGuiID ImGuiWindow::GetID(int n)
ImGuiID id = ImHashData(&n, sizeof(n), seed);
#ifndef IMGUI_DISABLE_DEBUG_TOOLS
ImGuiContext& g = *Ctx;
- if (g.DebugHookIdInfo == id)
+ if (g.DebugHookIdInfoId == id)
ImGui::DebugHookIdInfo(id, ImGuiDataType_S32, (void*)(intptr_t)n, NULL);
#endif
return id;
@@ -9243,7 +9701,7 @@ void ImGui::PushOverrideID(ImGuiID id)
ImGuiContext& g = *GImGui;
ImGuiWindow* window = g.CurrentWindow;
#ifndef IMGUI_DISABLE_DEBUG_TOOLS
- if (g.DebugHookIdInfo == id)
+ if (g.DebugHookIdInfoId == id)
DebugHookIdInfo(id, ImGuiDataType_ID, NULL, NULL);
#endif
window->IDStack.push_back(id);
@@ -9257,7 +9715,7 @@ ImGuiID ImGui::GetIDWithSeed(const char* str, const char* str_end, ImGuiID seed)
ImGuiID id = ImHashStr(str, str_end ? (str_end - str) : 0, seed);
#ifndef IMGUI_DISABLE_DEBUG_TOOLS
ImGuiContext& g = *GImGui;
- if (g.DebugHookIdInfo == id)
+ if (g.DebugHookIdInfoId == id)
DebugHookIdInfo(id, ImGuiDataType_String, str, str_end);
#endif
return id;
@@ -9268,7 +9726,7 @@ ImGuiID ImGui::GetIDWithSeed(int n, ImGuiID seed)
ImGuiID id = ImHashData(&n, sizeof(n), seed);
#ifndef IMGUI_DISABLE_DEBUG_TOOLS
ImGuiContext& g = *GImGui;
- if (g.DebugHookIdInfo == id)
+ if (g.DebugHookIdInfoId == id)
DebugHookIdInfo(id, ImGuiDataType_S32, (void*)(intptr_t)n, NULL);
#endif
return id;
@@ -10399,13 +10857,17 @@ void ImGui::SetNextFrameWantCaptureMouse(bool want_capture_mouse)
static const char* GetInputSourceName(ImGuiInputSource source)
{
const char* input_source_names[] = { "None", "Mouse", "Keyboard", "Gamepad" };
- IM_ASSERT(IM_ARRAYSIZE(input_source_names) == ImGuiInputSource_COUNT && source >= 0 && source < ImGuiInputSource_COUNT);
+ IM_ASSERT(IM_ARRAYSIZE(input_source_names) == ImGuiInputSource_COUNT);
+ if (source < 0 || source >= ImGuiInputSource_COUNT)
+ return "Unknown";
return input_source_names[source];
}
static const char* GetMouseSourceName(ImGuiMouseSource source)
{
const char* mouse_source_names[] = { "Mouse", "TouchScreen", "Pen" };
- IM_ASSERT(IM_ARRAYSIZE(mouse_source_names) == ImGuiMouseSource_COUNT && source >= 0 && source < ImGuiMouseSource_COUNT);
+ IM_ASSERT(IM_ARRAYSIZE(mouse_source_names) == ImGuiMouseSource_COUNT);
+ if (source < 0 || source >= ImGuiMouseSource_COUNT)
+ return "Unknown";
return mouse_source_names[source];
}
static void DebugPrintInputEvent(const char* prefix, const ImGuiInputEvent* e)
@@ -10804,36 +11266,44 @@ bool ImGui::DebugCheckVersionAndDataLayout(const char* version, size_t sz_io, si
return !error;
}
-// Until 1.89 (IMGUI_VERSION_NUM < 18814) it was legal to use SetCursorPos() to extend the boundary of a parent (e.g. window or table cell)
-// This is causing issues and ambiguity and we need to retire that.
-// See https://github.com/ocornut/imgui/issues/5548 for more details.
-// [Scenario 1]
+// Until 1.89 (August 2022, IMGUI_VERSION_NUM < 18814) it was legal to use SetCursorPos()/SetCursorScreenPos()
+// to extend contents size of our parent container (e.g. window contents size, which is used for auto-resizing
+// windows, table column contents size used for auto-resizing columns, group size).
+// This was causing issues and ambiguities and we needed to retire that.
+// From 1.89, extending contents size boundaries REQUIRES AN ITEM TO BE SUBMITTED.
+//
// Previously this would make the window content size ~200x200:
-// Begin(...) + SetCursorScreenPos(GetCursorScreenPos() + ImVec2(200,200)) + End(); // NOT OK
+// Begin(...) + SetCursorScreenPos(GetCursorScreenPos() + ImVec2(200,200)) + End(); // NOT OK ANYMORE
// Instead, please submit an item:
// Begin(...) + SetCursorScreenPos(GetCursorScreenPos() + ImVec2(200,200)) + Dummy(ImVec2(0,0)) + End(); // OK
// Alternative:
// Begin(...) + Dummy(ImVec2(200,200)) + End(); // OK
-// [Scenario 2]
-// For reference this is one of the issue what we aim to fix with this change:
-// BeginGroup() + SomeItem("foobar") + SetCursorScreenPos(GetCursorScreenPos()) + EndGroup()
-// The previous logic made SetCursorScreenPos(GetCursorScreenPos()) have a side-effect! It would erroneously incorporate ItemSpacing.y after the item into content size, making the group taller!
-// While this code is a little twisted, no-one would expect SetXXX(GetXXX()) to have a side-effect. Using vertical alignment patterns could trigger this issue.
+//
+// The assert below detects when the _last_ call in a window was a SetCursorPos() not followed by an Item,
+// and with a position that would grow the parent contents size.
+//
+// Advanced:
+// - For reference, old logic was causing issues because it meant that SetCursorScreenPos(GetCursorScreenPos())
+// had a side-effect on layout! In particular this caused problem to compute group boundaries.
+// e.g. BeginGroup() + SomeItem() + SetCursorScreenPos(GetCursorScreenPos()) + EndGroup() would cause the
+// group to be taller because auto-sizing generally adds padding on bottom and right side.
+// - While this code is a little twisted, no-one would expect SetXXX(GetXXX()) to have a side-effect.
+// Using vertical alignment patterns would frequently trigger this sorts of issue.
+// - See https://github.com/ocornut/imgui/issues/5548 for more details.
void ImGui::ErrorCheckUsingSetCursorPosToExtendParentBoundaries()
{
ImGuiContext& g = *GImGui;
ImGuiWindow* window = g.CurrentWindow;
IM_ASSERT(window->DC.IsSetPos);
window->DC.IsSetPos = false;
-#ifdef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
if (window->DC.CursorPos.x <= window->DC.CursorMaxPos.x && window->DC.CursorPos.y <= window->DC.CursorMaxPos.y)
return;
if (window->SkipItems)
return;
- IM_ASSERT(0 && "Code uses SetCursorPos()/SetCursorScreenPos() to extend window/parent boundaries. Please submit an item e.g. Dummy() to validate extent.");
-#else
- window->DC.CursorMaxPos = ImMax(window->DC.CursorMaxPos, window->DC.CursorPos);
-#endif
+ IM_ASSERT_USER_ERROR(0, "Code uses SetCursorPos()/SetCursorScreenPos() to extend window/parent boundaries.\nPlease submit an item e.g. Dummy() afterwards in order to grow window/parent boundaries.");
+
+ // For reference, the old behavior was essentially:
+ //window->DC.CursorMaxPos = ImMax(window->DC.CursorMaxPos, window->DC.CursorPos);
}
static void ImGui::ErrorCheckNewFrameSanityChecks()
@@ -10861,7 +11331,6 @@ static void ImGui::ErrorCheckNewFrameSanityChecks()
IM_ASSERT((g.IO.DeltaTime > 0.0f || g.FrameCount == 0) && "Need a positive DeltaTime!");
IM_ASSERT((g.FrameCount == 0 || g.FrameCountEnded == g.FrameCount) && "Forgot to call Render() or EndFrame() at the end of the previous frame?");
IM_ASSERT(g.IO.DisplaySize.x >= 0.0f && g.IO.DisplaySize.y >= 0.0f && "Invalid DisplaySize value!");
- IM_ASSERT(g.IO.Fonts->IsBuilt() && "Font Atlas not built! Make sure you called ImGui_ImplXXXX_NewFrame() function for renderer backend, which should call io.Fonts->GetTexDataAsRGBA32() / GetTexDataAsAlpha8()");
IM_ASSERT(g.Style.CurveTessellationTol > 0.0f && "Invalid style setting!");
IM_ASSERT(g.Style.CircleTessellationMaxError > 0.0f && "Invalid style setting!");
IM_ASSERT(g.Style.Alpha >= 0.0f && g.Style.Alpha <= 1.0f && "Invalid style setting!"); // Allows us to avoid a few clamps in color computations
@@ -10869,12 +11338,16 @@ static void ImGui::ErrorCheckNewFrameSanityChecks()
IM_ASSERT(g.Style.WindowBorderHoverPadding > 0.0f && "Invalid style setting!"); // Required otherwise cannot resize from borders.
IM_ASSERT(g.Style.WindowMenuButtonPosition == ImGuiDir_None || g.Style.WindowMenuButtonPosition == ImGuiDir_Left || g.Style.WindowMenuButtonPosition == ImGuiDir_Right);
IM_ASSERT(g.Style.ColorButtonPosition == ImGuiDir_Left || g.Style.ColorButtonPosition == ImGuiDir_Right);
+ IM_ASSERT(g.Style.TreeLinesFlags == ImGuiTreeNodeFlags_DrawLinesNone || g.Style.TreeLinesFlags == ImGuiTreeNodeFlags_DrawLinesFull || g.Style.TreeLinesFlags == ImGuiTreeNodeFlags_DrawLinesToNodes);
// Error handling: we do not accept 100% silent recovery! Please contact me if you feel this is getting in your way.
if (g.IO.ConfigErrorRecovery)
IM_ASSERT(g.IO.ConfigErrorRecoveryEnableAssert || g.IO.ConfigErrorRecoveryEnableDebugLog || g.IO.ConfigErrorRecoveryEnableTooltip || g.ErrorCallback != NULL);
#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
+ if (g.IO.FontGlobalScale > 1.0f)
+ IM_ASSERT(g.Style.FontScaleMain == 1.0f && "Since 1.92: use style.FontScaleMain instead of g.IO.FontGlobalScale!");
+
// Remap legacy names
if (g.IO.ConfigFlags & ImGuiConfigFlags_NavEnableSetMousePos)
{
@@ -10886,6 +11359,16 @@ static void ImGui::ErrorCheckNewFrameSanityChecks()
g.IO.ConfigNavCaptureKeyboard = false;
g.IO.ConfigFlags &= ~ImGuiConfigFlags_NavNoCaptureKeyboard;
}
+ if (g.IO.ConfigFlags & ImGuiConfigFlags_DpiEnableScaleFonts)
+ {
+ g.IO.ConfigDpiScaleFonts = true;
+ g.IO.ConfigFlags &= ~ImGuiConfigFlags_DpiEnableScaleFonts;
+ }
+ if (g.IO.ConfigFlags & ImGuiConfigFlags_DpiEnableScaleViewports)
+ {
+ g.IO.ConfigDpiScaleViewports = true;
+ g.IO.ConfigFlags &= ~ImGuiConfigFlags_DpiEnableScaleViewports;
+ }
// Remap legacy clipboard handlers (OBSOLETED in 1.91.1, August 2024)
if (g.IO.GetClipboardTextFn != NULL && (g.PlatformIO.Platform_GetClipboardTextFn == NULL || g.PlatformIO.Platform_GetClipboardTextFn == Platform_GetClipboardTextFn_DefaultImpl))
@@ -11144,9 +11627,9 @@ void ImGui::ErrorCheckEndFrameFinalizeErrorTooltip()
{
#ifndef IMGUI_DISABLE_DEBUG_TOOLS
ImGuiContext& g = *GImGui;
- if (g.DebugDrawIdConflicts != 0 && g.IO.KeyCtrl == false)
+ if (g.DebugDrawIdConflictsId != 0 && g.IO.KeyCtrl == false)
g.DebugDrawIdConflictsCount = g.HoveredIdPreviousFrameItemCount;
- if (g.DebugDrawIdConflicts != 0 && g.DebugItemPickerActive == false && BeginErrorTooltip())
+ if (g.DebugDrawIdConflictsId != 0 && g.DebugItemPickerActive == false && BeginErrorTooltip())
{
Text("Programmer error: %d visible items with conflicting ID!", g.DebugDrawIdConflictsCount);
BulletText("Code should use PushID()/PopID() in loops, or append \"##xx\" to same-label identifiers!");
@@ -11315,6 +11798,21 @@ bool ImGui::ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb_arg, ImGu
// Empty identifier are valid and useful in a small amount of cases, but 99.9% of the time you want to use "##something".
// READ THE FAQ: https://dearimgui.com/faq
IM_ASSERT(id != window->ID && "Cannot have an empty ID at the root of a window. If you need an empty label, use ## and read the FAQ about how the ID Stack works!");
+
+ // [DEBUG] Highlight all conflicts WITHOUT needing to hover. THIS WILL SLOW DOWN DEAR IMGUI. DON'T KEEP ACTIVATED.
+ // This will only work for items submitted with ItemAdd(). Some very rare/odd/unrecommended code patterns are calling ButtonBehavior() without ItemAdd().
+#ifdef IMGUI_DEBUG_HIGHLIGHT_ALL_ID_CONFLICTS
+ if ((g.LastItemData.ItemFlags & ImGuiItemFlags_AllowDuplicateId) == 0)
+ {
+ int* p_alive = g.DebugDrawIdConflictsAliveCount.GetIntRef(id, -1); // Could halve lookups if we knew ImGuiStorage can store 64-bit, or by storing FrameCount as 30-bits + highlight as 2-bits. But the point is that we should not pretend that this is fast.
+ int* p_highlight = g.DebugDrawIdConflictsHighlightSet.GetIntRef(id, -1);
+ if (*p_alive == g.FrameCount)
+ *p_highlight = g.FrameCount;
+ *p_alive = g.FrameCount;
+ if (*p_highlight >= g.FrameCount - 1)
+ window->DrawList->AddRect(bb.Min - ImVec2(1, 1), bb.Max + ImVec2(1, 1), IM_COL32(255, 0, 0, 255), 0.0f, ImDrawFlags_None, 2.0f);
+ }
+#endif
}
//if (g.IO.KeyAlt) window->DrawList->AddRect(bb.Min, bb.Max, IM_COL32(255,255,0,120)); // [DEBUG]
//if ((g.LastItemData.ItemFlags & ImGuiItemFlags_NoNav) == 0)
@@ -11796,7 +12294,7 @@ static ImVec2 CalcNextScrollFromScrollTargetAndClamp(ImGuiWindow* window)
}
scroll[axis] = scroll_target - center_ratio * (window->SizeFull[axis] - decoration_size[axis]);
}
- scroll[axis] = IM_ROUND(ImMax(scroll[axis], 0.0f));
+ scroll[axis] = ImRound64(ImMax(scroll[axis], 0.0f));
if (!window->Collapsed && !window->SkipItems)
scroll[axis] = ImMin(scroll[axis], window->ScrollMax[axis]);
}
@@ -12796,10 +13294,10 @@ bool ImGui::IsWindowFocused(ImGuiFocusedFlags flags)
IM_ASSERT(cur_window); // Not inside a Begin()/End()
const bool popup_hierarchy = (flags & ImGuiFocusedFlags_NoPopupHierarchy) == 0;
const bool dock_hierarchy = (flags & ImGuiFocusedFlags_DockHierarchy) != 0;
- if (flags & ImGuiHoveredFlags_RootWindow)
+ if (flags & ImGuiFocusedFlags_RootWindow)
cur_window = GetCombinedRootWindow(cur_window, popup_hierarchy, dock_hierarchy);
- if (flags & ImGuiHoveredFlags_ChildWindows)
+ if (flags & ImGuiFocusedFlags_ChildWindows)
return IsWindowChildOf(ref_window, cur_window, popup_hierarchy, dock_hierarchy);
else
return (ref_window == cur_window);
@@ -13136,7 +13634,7 @@ static float inline NavScoreItemDistInterval(float cand_min, float cand_max, flo
}
// Scoring function for keyboard/gamepad directional navigation. Based on https://gist.github.com/rygorous/6981057
-static bool ImGui::NavScoreItem(ImGuiNavItemData* result)
+static bool ImGui::NavScoreItem(ImGuiNavItemData* result, const ImRect& nav_bb)
{
ImGuiContext& g = *GImGui;
ImGuiWindow* window = g.CurrentWindow;
@@ -13144,7 +13642,7 @@ static bool ImGui::NavScoreItem(ImGuiNavItemData* result)
return false;
// FIXME: Those are not good variables names
- ImRect cand = g.LastItemData.NavRect; // Current item nav rectangle
+ ImRect cand = nav_bb; // Current item nav rectangle
const ImRect curr = g.NavScoringRect; // Current modified source rect (NB: we've applied Max.x = Min.x in NavUpdate() to inhibit the effect of having varied item width)
g.NavScoringDebugCount++;
@@ -13311,13 +13809,13 @@ static void ImGui::NavProcessItem()
const ImGuiID id = g.LastItemData.ID;
const ImGuiItemFlags item_flags = g.LastItemData.ItemFlags;
- // When inside a container that isn't scrollable with Left<>Right, clip NavRect accordingly (#2221)
+ // When inside a container that isn't scrollable with Left<>Right, clip NavRect accordingly (#2221, #8816)
+ ImRect nav_bb = g.LastItemData.NavRect;
if (window->DC.NavIsScrollPushableX == false)
{
- g.LastItemData.NavRect.Min.x = ImClamp(g.LastItemData.NavRect.Min.x, window->ClipRect.Min.x, window->ClipRect.Max.x);
- g.LastItemData.NavRect.Max.x = ImClamp(g.LastItemData.NavRect.Max.x, window->ClipRect.Min.x, window->ClipRect.Max.x);
+ nav_bb.Min.x = ImClamp(nav_bb.Min.x, window->ClipRect.Min.x, window->ClipRect.Max.x);
+ nav_bb.Max.x = ImClamp(nav_bb.Max.x, window->ClipRect.Min.x, window->ClipRect.Max.x);
}
- const ImRect nav_bb = g.LastItemData.NavRect;
// Process Init Request
if (g.NavInitRequest && g.NavLayer == window->DC.NavLayerCurrent && (item_flags & ImGuiItemFlags_Disabled) == 0)
@@ -13349,14 +13847,14 @@ static void ImGui::NavProcessItem()
else if (g.NavId != id || (g.NavMoveFlags & ImGuiNavMoveFlags_AllowCurrentNavId))
{
ImGuiNavItemData* result = (window == g.NavWindow) ? &g.NavMoveResultLocal : &g.NavMoveResultOther;
- if (NavScoreItem(result))
+ if (NavScoreItem(result, nav_bb))
NavApplyItemToResult(result);
// Features like PageUp/PageDown need to maintain a separate score for the visible set of items.
const float VISIBLE_RATIO = 0.70f;
if ((g.NavMoveFlags & ImGuiNavMoveFlags_AlsoScoreVisibleSet) && window->ClipRect.Overlaps(nav_bb))
if (ImClamp(nav_bb.Max.y, window->ClipRect.Min.y, window->ClipRect.Max.y) - ImClamp(nav_bb.Min.y, window->ClipRect.Min.y, window->ClipRect.Max.y) >= (nav_bb.Max.y - nav_bb.Min.y) * VISIBLE_RATIO)
- if (NavScoreItem(&g.NavMoveResultLocalVisible))
+ if (NavScoreItem(&g.NavMoveResultLocalVisible, nav_bb))
NavApplyItemToResult(&g.NavMoveResultLocalVisible);
}
}
@@ -13486,8 +13984,8 @@ void ImGui::NavMoveRequestResolveWithLastItem(ImGuiNavItemData* result)
NavUpdateAnyRequestFlag();
}
-// Called by TreePop() to implement ImGuiTreeNodeFlags_NavLeftJumpsBackHere
-void ImGui::NavMoveRequestResolveWithPastTreeNode(ImGuiNavItemData* result, ImGuiTreeNodeStackData* tree_node_data)
+// Called by TreePop() to implement ImGuiTreeNodeFlags_NavLeftJumpsToParent
+void ImGui::NavMoveRequestResolveWithPastTreeNode(ImGuiNavItemData* result, const ImGuiTreeNodeStackData* tree_node_data)
{
ImGuiContext& g = *GImGui;
g.NavMoveScoringItems = false;
@@ -13640,6 +14138,9 @@ static ImVec2 ImGui::NavCalcPreferredRefPos()
const bool activated_shortcut = g.ActiveId != 0 && g.ActiveIdFromShortcut && g.ActiveId == g.LastItemData.ID;
+ if (source != ImGuiInputSource_Mouse && !activated_shortcut && window == NULL)
+ source = ImGuiInputSource_Mouse;
+
// Testing for !activated_shortcut here could in theory be removed if we decided that activating a remote shortcut altered one of the g.NavDisableXXX flag.
if (source == ImGuiInputSource_Mouse)
{
@@ -13655,18 +14156,20 @@ static ImVec2 ImGui::NavCalcPreferredRefPos()
ImRect ref_rect;
if (activated_shortcut)
ref_rect = g.LastItemData.NavRect;
- else
+ else if (window != NULL)
ref_rect = WindowRectRelToAbs(window, window->NavRectRel[g.NavLayer]);
// Take account of upcoming scrolling (maybe set mouse pos should be done in EndFrame?)
- if (window->LastFrameActive != g.FrameCount && (window->ScrollTarget.x != FLT_MAX || window->ScrollTarget.y != FLT_MAX))
+ if (window != NULL && window->LastFrameActive != g.FrameCount && (window->ScrollTarget.x != FLT_MAX || window->ScrollTarget.y != FLT_MAX))
{
ImVec2 next_scroll = CalcNextScrollFromScrollTargetAndClamp(window);
ref_rect.Translate(window->Scroll - next_scroll);
}
ImVec2 pos = ImVec2(ref_rect.Min.x + ImMin(g.Style.FramePadding.x * 4, ref_rect.GetWidth()), ref_rect.Max.y - ImMin(g.Style.FramePadding.y, ref_rect.GetHeight()));
- ImGuiViewport* viewport = window->Viewport;
- return ImTrunc(ImClamp(pos, viewport->Pos, viewport->Pos + viewport->Size)); // ImTrunc() is important because non-integer mouse position application in backend might be lossy and result in undesirable non-zero delta.
+ if (window != NULL)
+ if (ImGuiViewport* viewport = window->Viewport)
+ pos = ImClamp(pos, viewport->Pos, viewport->Pos + viewport->Size);
+ return ImTrunc(pos); // ImTrunc() is important because non-integer mouse position application in backend might be lossy and result in undesirable non-zero delta.
}
}
@@ -14020,6 +14523,7 @@ void ImGui::NavUpdateCreateMoveRequest()
}
}
+ // Prepare scoring rectangle.
// For scoring we use a single segment on the left side our current item bounding box (not touching the edge to avoid box overlap with zero-spaced items)
ImRect scoring_rect;
if (window != NULL)
@@ -14034,7 +14538,7 @@ void ImGui::NavUpdateCreateMoveRequest()
//if (!g.NavScoringNoClipRect.IsInverted()) { GetForegroundDrawList()->AddRect(g.NavScoringNoClipRect.Min, g.NavScoringNoClipRect.Max, IM_COL32(255, 200, 0, 255)); } // [DEBUG]
}
g.NavScoringRect = scoring_rect;
- g.NavScoringNoClipRect.Add(scoring_rect);
+ //g.NavScoringNoClipRect.Add(scoring_rect);
}
void ImGui::NavUpdateCreateTabbingRequest()
@@ -14169,6 +14673,8 @@ void ImGui::NavMoveRequestApplyResult()
{
g.NavNextActivateId = result->ID;
g.NavNextActivateFlags = ImGuiActivateFlags_None;
+ if (g.NavMoveFlags & ImGuiNavMoveFlags_FocusApi)
+ g.NavNextActivateFlags |= ImGuiActivateFlags_FromFocusApi;
if (g.NavMoveFlags & ImGuiNavMoveFlags_IsTabbing)
g.NavNextActivateFlags |= ImGuiActivateFlags_PreferInput | ImGuiActivateFlags_TryToPreserveState | ImGuiActivateFlags_FromTabbing;
}
@@ -14253,7 +14759,7 @@ static float ImGui::NavUpdatePageUpPageDown()
if (g.NavLayer != ImGuiNavLayer_Main)
NavRestoreLayer(ImGuiNavLayer_Main);
- if (window->DC.NavLayersActiveMask == 0x00 && window->DC.NavWindowHasScrollY)
+ if ((window->DC.NavLayersActiveMask & (1 << ImGuiNavLayer_Main)) == 0 && window->DC.NavWindowHasScrollY)
{
// Fallback manual-scroll when window has no navigable item
if (IsKeyPressed(ImGuiKey_PageUp, ImGuiInputFlags_Repeat, ImGuiKeyOwner_NoOwner))
@@ -14433,6 +14939,7 @@ static void ImGui::NavUpdateWindowingApplyFocus(ImGuiWindow* apply_focus_window)
SetNavCursorVisibleAfterMove();
ClosePopupsOverWindow(apply_focus_window, false);
FocusWindow(apply_focus_window, ImGuiFocusRequestFlags_RestoreFocusedChild);
+ IM_ASSERT(g.NavWindow != NULL);
apply_focus_window = g.NavWindow;
if (apply_focus_window->NavLastIds[0] == 0)
NavInitWindow(apply_focus_window, false);
@@ -14485,17 +14992,23 @@ static void ImGui::NavUpdateWindowing()
const bool nav_keyboard_active = (io.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard) != 0;
const bool keyboard_next_window = allow_windowing && g.ConfigNavWindowingKeyNext && Shortcut(g.ConfigNavWindowingKeyNext, ImGuiInputFlags_Repeat | ImGuiInputFlags_RouteAlways, owner_id);
const bool keyboard_prev_window = allow_windowing && g.ConfigNavWindowingKeyPrev && Shortcut(g.ConfigNavWindowingKeyPrev, ImGuiInputFlags_Repeat | ImGuiInputFlags_RouteAlways, owner_id);
- const bool start_windowing_with_gamepad = allow_windowing && nav_gamepad_active && !g.NavWindowingTarget && Shortcut(ImGuiKey_NavGamepadMenu, ImGuiInputFlags_RouteAlways, owner_id);
+ const bool start_toggling_with_gamepad = nav_gamepad_active && !g.NavWindowingTarget && Shortcut(ImGuiKey_NavGamepadMenu, ImGuiInputFlags_RouteAlways, owner_id);
+ const bool start_windowing_with_gamepad = allow_windowing && start_toggling_with_gamepad;
const bool start_windowing_with_keyboard = allow_windowing && !g.NavWindowingTarget && (keyboard_next_window || keyboard_prev_window); // Note: enabled even without NavEnableKeyboard!
bool just_started_windowing_from_null_focus = false;
+ if (start_toggling_with_gamepad)
+ {
+ g.NavWindowingToggleLayer = true; // Gamepad starts toggling layer
+ g.NavWindowingToggleKey = ImGuiKey_NavGamepadMenu;
+ g.NavWindowingInputSource = g.NavInputSource = ImGuiInputSource_Gamepad;
+ }
if (start_windowing_with_gamepad || start_windowing_with_keyboard)
- if (ImGuiWindow* window = g.NavWindow ? g.NavWindow : FindWindowNavFocusable(g.WindowsFocusOrder.Size - 1, -INT_MAX, -1))
+ if (ImGuiWindow* window = (g.NavWindow && IsWindowNavFocusable(g.NavWindow)) ? g.NavWindow : FindWindowNavFocusable(g.WindowsFocusOrder.Size - 1, -INT_MAX, -1))
{
if (start_windowing_with_keyboard || g.ConfigNavWindowingWithGamepad)
g.NavWindowingTarget = g.NavWindowingTargetAnim = window->RootWindow; // Current location
g.NavWindowingTimer = g.NavWindowingHighlightAlpha = 0.0f;
g.NavWindowingAccumDeltaPos = g.NavWindowingAccumDeltaSize = ImVec2(0.0f, 0.0f);
- g.NavWindowingToggleLayer = start_windowing_with_gamepad ? true : false; // Gamepad starts toggling layer
g.NavWindowingInputSource = g.NavInputSource = start_windowing_with_keyboard ? ImGuiInputSource_Keyboard : ImGuiInputSource_Gamepad;
if (g.NavWindow == NULL)
just_started_windowing_from_null_focus = true;
@@ -14933,6 +15446,31 @@ bool ImGui::BeginDragDropTargetCustom(const ImRect& bb, ImGuiID id)
g.DragDropTargetRect = bb;
g.DragDropTargetClipRect = window->ClipRect; // May want to be overridden by user depending on use case?
g.DragDropTargetId = id;
+ g.DragDropTargetFullViewport = 0;
+ g.DragDropWithinTarget = true;
+ return true;
+}
+
+// Typical usage would be:
+// if (!ImGui::IsWindowHovered(ImGuiHoveredFlags_AnyWindow | ImGuiHoveredFlags_AllowWhenBlockedByActiveItem))
+// if (ImGui::BeginDragDropTargetViewport(ImGui::GetMainViewport(), NULL))
+// But we are leaving the hover test to the caller for maximum flexibility.
+bool ImGui::BeginDragDropTargetViewport(ImGuiViewport* viewport, const ImRect* p_bb)
+{
+ ImGuiContext& g = *GImGui;
+ if (!g.DragDropActive)
+ return false;
+
+ ImRect bb = p_bb ? *p_bb : ((ImGuiViewportP*)viewport)->GetWorkRect();
+ ImGuiID id = viewport->ID;
+ if (g.MouseViewport != viewport || !IsMouseHoveringRect(bb.Min, bb.Max, false) || (id == g.DragDropPayload.SourceId))
+ return false;
+
+ IM_ASSERT(g.DragDropWithinTarget == false && g.DragDropWithinSource == false); // Can't nest BeginDragDropSource() and BeginDragDropTarget()
+ g.DragDropTargetRect = bb;
+ g.DragDropTargetClipRect = bb;
+ g.DragDropTargetId = id;
+ g.DragDropTargetFullViewport = id;
g.DragDropWithinTarget = true;
return true;
}
@@ -15003,8 +15541,19 @@ const ImGuiPayload* ImGui::AcceptDragDropPayload(const char* type, ImGuiDragDrop
// Render default drop visuals
payload.Preview = was_accepted_previously;
flags |= (g.DragDropSourceFlags & ImGuiDragDropFlags_AcceptNoDrawDefaultRect); // Source can also inhibit the preview (useful for external sources that live for 1 frame)
- if (!(flags & ImGuiDragDropFlags_AcceptNoDrawDefaultRect) && payload.Preview)
- RenderDragDropTargetRect(r, g.DragDropTargetClipRect);
+ const bool draw_target_rect = payload.Preview && !(flags & ImGuiDragDropFlags_AcceptNoDrawDefaultRect);
+ if (draw_target_rect && g.DragDropTargetFullViewport != 0)
+ {
+ ImGuiViewport* viewport = FindViewportByID(g.DragDropTargetFullViewport);
+ IM_ASSERT(viewport != NULL);
+ ImRect bb = g.DragDropTargetRect;
+ bb.Expand(-3.5f);
+ RenderDragDropTargetRectEx(GetForegroundDrawList(viewport), bb);
+ }
+ else if (draw_target_rect)
+ {
+ RenderDragDropTargetRectForItem(r);
+ }
g.DragDropAcceptFrameCount = g.FrameCount;
if ((g.DragDropSourceFlags & ImGuiDragDropFlags_SourceExtern) && g.DragDropMouseButton == -1)
@@ -15020,21 +15569,26 @@ const ImGuiPayload* ImGui::AcceptDragDropPayload(const char* type, ImGuiDragDrop
}
// FIXME-STYLE FIXME-DRAGDROP: Settle on a proper default visuals for drop target.
-void ImGui::RenderDragDropTargetRect(const ImRect& bb, const ImRect& item_clip_rect)
+void ImGui::RenderDragDropTargetRectForItem(const ImRect& bb)
{
ImGuiContext& g = *GImGui;
ImGuiWindow* window = g.CurrentWindow;
ImRect bb_display = bb;
- bb_display.ClipWith(item_clip_rect); // Clip THEN expand so we have a way to visualize that target is not entirely visible.
+ bb_display.ClipWith(g.DragDropTargetClipRect); // Clip THEN expand so we have a way to visualize that target is not entirely visible.
bb_display.Expand(3.5f);
bool push_clip_rect = !window->ClipRect.Contains(bb_display);
if (push_clip_rect)
window->DrawList->PushClipRectFullScreen();
- window->DrawList->AddRect(bb_display.Min, bb_display.Max, GetColorU32(ImGuiCol_DragDropTarget), 0.0f, 0, 2.0f); // FIXME-DPI
+ RenderDragDropTargetRectEx(window->DrawList, bb_display);
if (push_clip_rect)
window->DrawList->PopClipRect();
}
+void ImGui::RenderDragDropTargetRectEx(ImDrawList* draw_list, const ImRect& bb)
+{
+ draw_list->AddRect(bb.Min, bb.Max, GetColorU32(ImGuiCol_DragDropTarget), 0.0f, 0, 2.0f); // FIXME-DPI
+}
+
const ImGuiPayload* ImGui::GetDragDropPayload()
{
ImGuiContext& g = *GImGui;
@@ -15514,13 +16068,9 @@ ImGuiWindowSettings* ImGui::CreateNewWindowSettings(const char* name)
{
ImGuiContext& g = *GImGui;
+ // Preserve the full string when ConfigDebugVerboseIniSettings is set to make .ini inspection easier.
if (g.IO.ConfigDebugIniSettings == false)
- {
- // Skip to the "###" marker if any. We don't skip past to match the behavior of GetID()
- // Preserve the full string when ConfigDebugVerboseIniSettings is set to make .ini inspection easier.
- if (const char* p = strstr(name, "###"))
- name = p;
- }
+ name = ImHashSkipUncontributingPrefix(name);
const size_t name_len = ImStrlen(name);
// Allocate chunk
@@ -15730,6 +16280,37 @@ void ImGui::LocalizeRegisterEntries(const ImGuiLocEntry* entries, int count)
// - DestroyPlatformWindows()
//-----------------------------------------------------------------------------
+void ImGuiPlatformIO::ClearPlatformHandlers()
+{
+ Platform_GetClipboardTextFn = NULL;
+ Platform_SetClipboardTextFn = NULL;
+ Platform_OpenInShellFn = NULL;
+ Platform_SetImeDataFn = NULL;
+ Platform_ClipboardUserData = Platform_OpenInShellUserData = Platform_ImeUserData = NULL;
+ Platform_CreateWindow = Platform_DestroyWindow = Platform_ShowWindow = NULL;
+ Platform_SetWindowPos = Platform_SetWindowSize = NULL;
+ Platform_GetWindowPos = Platform_GetWindowSize = Platform_GetWindowFramebufferScale = NULL;
+ Platform_SetWindowFocus = NULL;
+ Platform_GetWindowFocus = Platform_GetWindowMinimized = NULL;
+ Platform_SetWindowTitle = NULL;
+ Platform_SetWindowAlpha = NULL;
+ Platform_UpdateWindow = NULL;
+ Platform_RenderWindow = Platform_SwapBuffers = NULL;
+ Platform_GetWindowDpiScale = NULL;
+ Platform_OnChangedViewport = NULL;
+ Platform_GetWindowWorkAreaInsets = NULL;
+ Platform_CreateVkSurface = NULL;
+}
+
+void ImGuiPlatformIO::ClearRendererHandlers()
+{
+ Renderer_TextureMaxWidth = Renderer_TextureMaxHeight = 0;
+ Renderer_RenderState = NULL;
+ Renderer_CreateWindow = Renderer_DestroyWindow = NULL;
+ Renderer_SetWindowSize = NULL;
+ Renderer_RenderWindow = Renderer_SwapBuffers = NULL;
+}
+
ImGuiViewport* ImGui::GetMainViewport()
{
ImGuiContext& g = *GImGui;
@@ -15768,6 +16349,8 @@ void ImGui::SetCurrentViewport(ImGuiWindow* current_window, ImGuiViewportP* view
g.CurrentViewport = viewport;
IM_ASSERT(g.CurrentDpiScale > 0.0f && g.CurrentDpiScale < 99.0f); // Typical correct values would be between 1.0f and 4.0f
//IMGUI_DEBUG_LOG_VIEWPORT("[viewport] SetCurrentViewport changed '%s' 0x%08X\n", current_window ? current_window->Name : NULL, viewport ? viewport->ID : 0);
+ if (g.IO.ConfigDpiScaleFonts)
+ g.Style.FontScaleDpi = g.CurrentDpiScale;
// Notify platform layer of viewport changes
// FIXME-DPI: This is only currently used for experimenting with handling of multiple DPI
@@ -15799,6 +16382,29 @@ static bool ImGui::GetWindowAlwaysWantOwnViewport(ImGuiWindow* window)
return false;
}
+
+// Heuristic, see #8948: depends on how backends handle OS-level parenting.
+static bool IsViewportAbove(ImGuiViewportP* potential_above, ImGuiViewportP* potential_below)
+{
+ // If ImGuiBackendFlags_HasParentViewport if set, ->ParentViewport chain should be accurate.
+ ImGuiContext& g = *GImGui;
+ if (g.IO.BackendFlags & ImGuiBackendFlags_HasParentViewport)
+ {
+ for (ImGuiViewport* v = potential_above; v != NULL && v->ParentViewport; v = v->ParentViewport)
+ if (v->ParentViewport == potential_below)
+ return true;
+ }
+ else
+ {
+ if (potential_above->ParentViewport == potential_below)
+ return true;
+ }
+
+ if (potential_above->LastFocusedStampCount > potential_below->LastFocusedStampCount)
+ return true;
+ return false;
+}
+
static bool ImGui::UpdateTryMergeWindowIntoHostViewport(ImGuiWindow* window, ImGuiViewportP* viewport)
{
ImGuiContext& g = *GImGui;
@@ -15813,14 +16419,14 @@ static bool ImGui::UpdateTryMergeWindowIntoHostViewport(ImGuiWindow* window, ImG
if (GetWindowAlwaysWantOwnViewport(window))
return false;
- // FIXME: Can't use g.WindowsFocusOrder[] for root windows only as we care about Z order. If we maintained a DisplayOrder along with FocusOrder we could..
- for (ImGuiWindow* window_behind : g.Windows)
+ for (ImGuiViewportP* viewport_2 : g.Viewports)
{
- if (window_behind == window)
- break;
- if (window_behind->WasActive && window_behind->ViewportOwned && !(window_behind->Flags & ImGuiWindowFlags_ChildWindow))
- if (window_behind->Viewport->GetMainRect().Overlaps(window->Rect()))
- return false;
+ if (viewport_2 == viewport || viewport_2 == window->Viewport)
+ continue;
+ if (viewport_2->GetMainRect().Overlaps(window->Rect()))
+ if (IsViewportAbove(viewport_2, viewport))
+ if (window->Viewport == NULL || !IsViewportAbove(viewport_2, window->Viewport))
+ return false;
}
// Move to the existing viewport, Move child/hosted windows as well (FIXME-OPT: iterate child)
@@ -15830,7 +16436,8 @@ static bool ImGui::UpdateTryMergeWindowIntoHostViewport(ImGuiWindow* window, ImG
if (g.Windows[n]->Viewport == old_viewport)
SetWindowViewport(g.Windows[n], viewport);
SetWindowViewport(window, viewport);
- BringWindowToDisplayFront(window);
+ if ((window->Flags & ImGuiWindowFlags_NoBringToFrontOnFocus) == 0)
+ BringWindowToDisplayFront(window);
return true;
}
@@ -15847,6 +16454,7 @@ static bool ImGui::UpdateTryMergeWindowIntoHostViewports(ImGuiWindow* window)
void ImGui::TranslateWindowsInViewport(ImGuiViewportP* viewport, const ImVec2& old_pos, const ImVec2& new_pos, const ImVec2& old_size, const ImVec2& new_size)
{
ImGuiContext& g = *GImGui;
+ //IMGUI_DEBUG_LOG_VIEWPORT("[viewport] TranslateWindowsInViewport 0x%08X\n", viewport->ID);
IM_ASSERT(viewport->Window == NULL && (viewport->Flags & ImGuiViewportFlags_CanHostOtherWindows));
// 1) We test if ImGuiConfigFlags_ViewportsEnable was just toggled, which allows us to conveniently
@@ -15855,7 +16463,7 @@ void ImGui::TranslateWindowsInViewport(ImGuiViewportP* viewport, const ImVec2& o
// One problem with this is that most Win32 applications doesn't update their render while dragging,
// and so the window will appear to teleport when releasing the mouse.
const bool translate_all_windows = (g.ConfigFlagsCurrFrame & ImGuiConfigFlags_ViewportsEnable) != (g.ConfigFlagsLastFrame & ImGuiConfigFlags_ViewportsEnable);
- ImRect test_still_fit_rect(old_pos, old_pos + viewport->Size);
+ ImRect test_still_fit_rect(old_pos, old_pos + old_size);
ImVec2 delta_pos = new_pos - old_pos;
for (ImGuiWindow* window : g.Windows) // FIXME-OPT
if (translate_all_windows || (window->Viewport == viewport && (old_size == new_size || test_still_fit_rect.Contains(window->Rect()))))
@@ -15866,6 +16474,7 @@ void ImGui::TranslateWindowsInViewport(ImGuiViewportP* viewport, const ImVec2& o
void ImGui::ScaleWindowsInViewport(ImGuiViewportP* viewport, float scale)
{
ImGuiContext& g = *GImGui;
+ //IMGUI_DEBUG_LOG_VIEWPORT("[viewport] ScaleWindowsInViewport 0x%08X\n", viewport->ID);
if (viewport->Window)
{
ScaleWindow(viewport->Window, scale);
@@ -15934,7 +16543,7 @@ static void ImGui::UpdateViewportsNewFrame()
// Focused viewport has changed?
if (focused_viewport && g.PlatformLastFocusedViewportId != focused_viewport->ID)
{
- IMGUI_DEBUG_LOG_VIEWPORT("[viewport] Focused viewport changed %08X -> %08X, attempting to apply our focus.\n", g.PlatformLastFocusedViewportId, focused_viewport->ID);
+ IMGUI_DEBUG_LOG_VIEWPORT("[viewport] Focused viewport changed %08X -> %08X '%s', attempting to apply our focus.\n", g.PlatformLastFocusedViewportId, focused_viewport->ID, focused_viewport->Window ? focused_viewport->Window->Name : "n/a");
const ImGuiViewport* prev_focused_viewport = FindViewportByID(g.PlatformLastFocusedViewportId);
const bool prev_focused_has_been_destroyed = (prev_focused_viewport == NULL) || (prev_focused_viewport->PlatformWindowCreated == false);
@@ -15950,7 +16559,7 @@ static void ImGui::UpdateViewportsNewFrame()
// - if focus didn't happen because we destroyed another window (#6462)
// FIXME: perhaps 'FocusTopMostWindowUnderOne()' can handle the 'focused_window->Window != NULL' case as well.
const bool apply_imgui_focus_on_focused_viewport = !IsAnyMouseDown() && !prev_focused_has_been_destroyed;
- if (apply_imgui_focus_on_focused_viewport)
+ if (apply_imgui_focus_on_focused_viewport && g.IO.ConfigViewportsPlatformFocusSetsImGuiFocus)
{
focused_viewport->LastFocusedHadNavWindow |= (g.NavWindow != NULL) && (g.NavWindow->Viewport == focused_viewport); // Update so a window changing viewport won't lose focus.
ImGuiFocusRequestFlags focus_request_flags = ImGuiFocusRequestFlags_UnlessBelowModal | ImGuiFocusRequestFlags_RestoreFocusedChild;
@@ -15973,10 +16582,12 @@ static void ImGui::UpdateViewportsNewFrame()
IM_ASSERT(main_viewport->Window == NULL);
ImVec2 main_viewport_pos = viewports_enabled ? g.PlatformIO.Platform_GetWindowPos(main_viewport) : ImVec2(0.0f, 0.0f);
ImVec2 main_viewport_size = g.IO.DisplaySize;
+ ImVec2 main_viewport_framebuffer_scale = g.IO.DisplayFramebufferScale;
if (viewports_enabled && (main_viewport->Flags & ImGuiViewportFlags_IsMinimized))
{
- main_viewport_pos = main_viewport->Pos; // Preserve last pos/size when minimized (FIXME: We don't do the same for Size outside of the viewport path)
+ main_viewport_pos = main_viewport->Pos; // Preserve last pos/size when minimized (FIXME: We don't do the same for Size outside of the viewport path)
main_viewport_size = main_viewport->Size;
+ main_viewport_framebuffer_scale = main_viewport->FramebufferScale;
}
AddUpdateViewport(NULL, IMGUI_VIEWPORT_DEFAULT_ID, main_viewport_pos, main_viewport_size, ImGuiViewportFlags_OwnedByApp | ImGuiViewportFlags_CanHostOtherWindows);
@@ -16008,6 +16619,8 @@ static void ImGui::UpdateViewportsNewFrame()
viewport->Pos = viewport->LastPlatformPos = g.PlatformIO.Platform_GetWindowPos(viewport);
if (viewport->PlatformRequestResize)
viewport->Size = viewport->LastPlatformSize = g.PlatformIO.Platform_GetWindowSize(viewport);
+ if (g.PlatformIO.Platform_GetWindowFramebufferScale != NULL)
+ viewport->FramebufferScale = g.PlatformIO.Platform_GetWindowFramebufferScale(viewport);
}
}
@@ -16049,7 +16662,7 @@ static void ImGui::UpdateViewportsNewFrame()
if (viewport->DpiScale != 0.0f && new_dpi_scale != viewport->DpiScale)
{
float scale_factor = new_dpi_scale / viewport->DpiScale;
- if (g.IO.ConfigFlags & ImGuiConfigFlags_DpiEnableScaleViewports)
+ if (g.IO.ConfigDpiScaleViewports)
ScaleWindowsInViewport(viewport, scale_factor);
//if (viewport == GetMainViewport())
// g.PlatformInterface.SetWindowSize(viewport, viewport->Size * scale_factor);
@@ -16168,9 +16781,10 @@ ImGuiViewportP* ImGui::AddUpdateViewport(ImGuiWindow* window, ImGuiID id, const
flags |= ImGuiViewportFlags_IsPlatformWindow;
if (window != NULL)
{
+ const bool window_can_use_inputs = ((window->Flags & ImGuiWindowFlags_NoMouseInputs) && (window->Flags & ImGuiWindowFlags_NoNavInputs)) == false;
if (g.MovingWindow && g.MovingWindow->RootWindowDockTree == window)
flags |= ImGuiViewportFlags_NoInputs | ImGuiViewportFlags_NoFocusOnAppearing;
- if ((window->Flags & ImGuiWindowFlags_NoMouseInputs) && (window->Flags & ImGuiWindowFlags_NoNavInputs))
+ if (!window_can_use_inputs)
flags |= ImGuiViewportFlags_NoInputs;
if (window->Flags & ImGuiWindowFlags_NoFocusOnAppearing)
flags |= ImGuiViewportFlags_NoFocusOnAppearing;
@@ -16213,10 +16827,7 @@ ImGuiViewportP* ImGui::AddUpdateViewport(ImGuiWindow* window, ImGuiID id, const
// Store initial DpiScale before the OS platform window creation, based on expected monitor data.
// This is so we can select an appropriate font size on the first frame of our window lifetime
- if (viewport->PlatformMonitor != -1)
- viewport->DpiScale = g.PlatformIO.Monitors[viewport->PlatformMonitor].DpiScale;
- else
- viewport->DpiScale = 1.0f;
+ viewport->DpiScale = GetViewportPlatformMonitor(viewport)->DpiScale;
}
viewport->Window = window;
@@ -16473,11 +17084,22 @@ void ImGui::WindowSyncOwnedViewport(ImGuiWindow* window, ImGuiWindow* parent_win
// Update parent viewport ID
// (the !IsFallbackWindow test mimic the one done in WindowSelectViewport())
if (window->WindowClass.ParentViewportId != (ImGuiID)-1)
+ {
+ ImGuiID old_parent_viewport_id = window->Viewport->ParentViewportId;
window->Viewport->ParentViewportId = window->WindowClass.ParentViewportId;
+ if (window->Viewport->ParentViewportId != old_parent_viewport_id)
+ window->Viewport->ParentViewport = FindViewportByID(window->Viewport->ParentViewportId);
+ }
else if ((window_flags & (ImGuiWindowFlags_Popup | ImGuiWindowFlags_Tooltip)) && parent_window_in_stack && (!parent_window_in_stack->IsFallbackWindow || parent_window_in_stack->WasActive))
+ {
+ window->Viewport->ParentViewport = parent_window_in_stack->Viewport;
window->Viewport->ParentViewportId = parent_window_in_stack->Viewport->ID;
+ }
else
+ {
+ window->Viewport->ParentViewport = g.IO.ConfigViewportsNoDefaultParent ? NULL : GetMainViewport();
window->Viewport->ParentViewportId = g.IO.ConfigViewportsNoDefaultParent ? 0 : IMGUI_VIEWPORT_DEFAULT_ID;
+ }
}
// Called by user at the end of the main loop, after EndFrame()
@@ -17210,7 +17832,7 @@ static void ImGui::DockContextPruneUnusedSettingsNodes(ImGuiContext* ctx)
{
ImGuiDockNodeSettings* settings = &dc->NodesSettings[settings_n];
ImGuiDockContextPruneNodeData* data = pool.GetByKey(settings->ID);
- if (data->CountWindows > 1)
+ if (data == NULL || data->CountWindows > 1)
continue;
ImGuiDockContextPruneNodeData* data_root = (data->RootId == settings->ID) ? data : pool.GetByKey(data->RootId);
@@ -18155,7 +18777,7 @@ static void ImGui::DockNodeUpdate(ImGuiDockNode* node)
node->HasCloseButton |= window->HasCloseButton;
window->DockIsActive = (node->Windows.Size > 1);
}
- if (node_flags & ImGuiDockNodeFlags_NoCloseButton)
+ if ((node_flags & ImGuiDockNodeFlags_NoCloseButton) || !g.Style.DockingNodeHasCloseButton)
node->HasCloseButton = false;
// Bind or create host window
@@ -18473,8 +19095,6 @@ static void ImGui::DockNodeUpdateTabBar(ImGuiDockNode* node, ImGuiWindow* host_w
ImGuiStyle& style = g.Style;
const bool node_was_active = (node->LastFrameActive + 1 == g.FrameCount);
- const bool closed_all = node->WantCloseAll && node_was_active;
- const ImGuiID closed_one = node->WantCloseTabId && node_was_active;
node->WantCloseAll = false;
node->WantCloseTabId = 0;
@@ -18606,7 +19226,8 @@ static void ImGui::DockNodeUpdateTabBar(ImGuiDockNode* node, ImGuiWindow* host_w
// Begin tab bar
ImGuiTabBarFlags tab_bar_flags = ImGuiTabBarFlags_Reorderable | ImGuiTabBarFlags_AutoSelectNewTabs; // | ImGuiTabBarFlags_NoTabListScrollingButtons);
- tab_bar_flags |= ImGuiTabBarFlags_SaveSettings | ImGuiTabBarFlags_DockNode;// | ImGuiTabBarFlags_FittingPolicyScroll;
+ tab_bar_flags |= ImGuiTabBarFlags_SaveSettings | ImGuiTabBarFlags_DockNode;
+ tab_bar_flags |= ImGuiTabBarFlags_FittingPolicyMixed; // Enforce default policy. Since 1.92.2 this is now reasonable. May expose later if needed. (#8800, #3421)
tab_bar_flags |= ImGuiTabBarFlags_DrawSelectedOverline;
if (!host_window->Collapsed && is_focused)
tab_bar_flags |= ImGuiTabBarFlags_IsFocused;
@@ -18626,37 +19247,35 @@ static void ImGui::DockNodeUpdateTabBar(ImGuiDockNode* node, ImGuiWindow* host_w
for (int window_n = 0; window_n < node->Windows.Size; window_n++)
{
ImGuiWindow* window = node->Windows[window_n];
- if ((closed_all || closed_one == window->TabId) && window->HasCloseButton && !(window->Flags & ImGuiWindowFlags_UnsavedDocument))
- continue;
- if (window->LastFrameActive + 1 >= g.FrameCount || !node_was_active)
- {
- ImGuiTabItemFlags tab_item_flags = 0;
- tab_item_flags |= window->WindowClass.TabItemFlagsOverrideSet;
- if (window->Flags & ImGuiWindowFlags_UnsavedDocument)
- tab_item_flags |= ImGuiTabItemFlags_UnsavedDocument;
- if (tab_bar->Flags & ImGuiTabBarFlags_NoCloseWithMiddleMouseButton)
- tab_item_flags |= ImGuiTabItemFlags_NoCloseWithMiddleMouseButton;
+ if (window->LastFrameActive + 1 < g.FrameCount && node_was_active)
+ continue; // FIXME: Not sure if that's still taken/useful.
- // Apply stored style overrides for the window
- for (int color_n = 0; color_n < ImGuiWindowDockStyleCol_COUNT; color_n++)
- g.Style.Colors[GWindowDockStyleColors[color_n]] = ColorConvertU32ToFloat4(window->DockStyle.Colors[color_n]);
+ ImGuiTabItemFlags tab_item_flags = 0;
+ tab_item_flags |= window->WindowClass.TabItemFlagsOverrideSet;
+ if (window->Flags & ImGuiWindowFlags_UnsavedDocument)
+ tab_item_flags |= ImGuiTabItemFlags_UnsavedDocument;
+ if (tab_bar->Flags & ImGuiTabBarFlags_NoCloseWithMiddleMouseButton)
+ tab_item_flags |= ImGuiTabItemFlags_NoCloseWithMiddleMouseButton;
- // Note that TabItemEx() calls TabBarCalcTabID() so our tab item ID will ignore the current ID stack (rightly so)
- bool tab_open = true;
- TabItemEx(tab_bar, window->Name, window->HasCloseButton ? &tab_open : NULL, tab_item_flags, window);
- if (!tab_open)
- node->WantCloseTabId = window->TabId;
- if (tab_bar->VisibleTabId == window->TabId)
- node->VisibleWindow = window;
+ // Apply stored style overrides for the window
+ for (int color_n = 0; color_n < ImGuiWindowDockStyleCol_COUNT; color_n++)
+ g.Style.Colors[GWindowDockStyleColors[color_n]] = ColorConvertU32ToFloat4(window->DockStyle.Colors[color_n]);
- // Store last item data so it can be queried with IsItemXXX functions after the user Begin() call
- window->DC.DockTabItemStatusFlags = g.LastItemData.StatusFlags;
- window->DC.DockTabItemRect = g.LastItemData.Rect;
+ // Note that TabItemEx() calls TabBarCalcTabID() so our tab item ID will ignore the current ID stack (rightly so)
+ bool tab_open = true;
+ TabItemEx(tab_bar, window->Name, window->HasCloseButton ? &tab_open : NULL, tab_item_flags, window);
+ if (!tab_open)
+ node->WantCloseTabId = window->TabId;
+ if (tab_bar->VisibleTabId == window->TabId)
+ node->VisibleWindow = window;
- // Update navigation ID on menu layer
- if (g.NavWindow && g.NavWindow->RootWindow == window && (window->DC.NavLayersActiveMask & (1 << ImGuiNavLayer_Menu)) == 0)
- host_window->NavLastIds[1] = window->TabId;
- }
+ // Store last item data so it can be queried with IsItemXXX functions after the user Begin() call
+ window->DC.DockTabItemStatusFlags = g.LastItemData.StatusFlags;
+ window->DC.DockTabItemRect = g.LastItemData.Rect;
+
+ // Update navigation ID on menu layer
+ if (g.NavWindow && g.NavWindow->RootWindow == window && (window->DC.NavLayersActiveMask & (1 << ImGuiNavLayer_Menu)) == 0)
+ host_window->NavLastIds[1] = window->TabId;
}
// Restore style colors
@@ -20906,11 +21525,16 @@ static void Platform_SetImeDataFn_DefaultImpl(ImGuiContext*, ImGuiViewport*, ImG
//-----------------------------------------------------------------------------
// [SECTION] METRICS/DEBUGGER WINDOW
//-----------------------------------------------------------------------------
+// - MetricsHelpMarker() [Internal]
// - DebugRenderViewportThumbnail() [Internal]
// - RenderViewportsThumbnails() [Internal]
+// - DebugRenderKeyboardPreview() [Internal]
// - DebugTextEncoding()
-// - MetricsHelpMarker() [Internal]
-// - ShowFontAtlas() [Internal]
+// - DebugFlashStyleColorStop() [Internal]
+// - DebugFlashStyleColor()
+// - UpdateDebugToolFlashStyleColor() [Internal]
+// - ShowFontAtlas() [Internal but called by Demo!]
+// - DebugNodeTexture() [Internal]
// - ShowMetricsWindow()
// - DebugNodeColumns() [Internal]
// - DebugNodeDockNode() [Internal]
@@ -20925,8 +21549,24 @@ static void Platform_SetImeDataFn_DefaultImpl(ImGuiContext*, ImGuiViewport*, ImG
// - DebugNodeWindowSettings() [Internal]
// - DebugNodeWindowsList() [Internal]
// - DebugNodeWindowsListByBeginStackParent() [Internal]
+// - ShowFontSelector()
//-----------------------------------------------------------------------------
+#if !defined(IMGUI_DISABLE_DEMO_WINDOWS) || !defined(IMGUI_DISABLE_DEBUG_TOOLS)
+// Avoid naming collision with imgui_demo.cpp's HelpMarker() for unity builds.
+static void MetricsHelpMarker(const char* desc)
+{
+ ImGui::TextDisabled("(?)");
+ if (ImGui::BeginItemTooltip())
+ {
+ ImGui::PushTextWrapPos(ImGui::GetFontSize() * 35.0f);
+ ImGui::TextUnformatted(desc);
+ ImGui::PopTextWrapPos();
+ ImGui::EndTooltip();
+ }
+}
+#endif
+
#ifndef IMGUI_DISABLE_DEBUG_TOOLS
void ImGui::DebugRenderViewportThumbnail(ImDrawList* draw_list, ImGuiViewportP* viewport, const ImRect& bb)
@@ -21072,10 +21712,12 @@ void ImGui::DebugTextEncoding(const char* str)
Text("0x%02X", (int)(unsigned char)p[byte_index]);
}
TableNextColumn();
- if (GetFont()->FindGlyphNoFallback((ImWchar)c))
- TextUnformatted(p, p + c_utf8_len);
- else
- TextUnformatted((c == IM_UNICODE_CODEPOINT_INVALID) ? "[invalid]" : "[missing]");
+ TextUnformatted(p, p + c_utf8_len);
+ if (!GetFont()->IsGlyphInFont((ImWchar)c))
+ {
+ SameLine();
+ TextUnformatted("[missing]");
+ }
TableNextColumn();
Text("U+%04X", (int)c);
p += c_utf8_len;
@@ -21123,36 +21765,203 @@ static const char* FormatTextureIDForDebugDisplay(char* buf, int buf_size, ImTex
return buf;
}
-// Avoid naming collision with imgui_demo.cpp's HelpMarker() for unity builds.
-static void MetricsHelpMarker(const char* desc)
+static const char* FormatTextureRefForDebugDisplay(char* buf, int buf_size, ImTextureRef tex_ref)
{
- ImGui::TextDisabled("(?)");
- if (ImGui::BeginItemTooltip())
- {
- ImGui::PushTextWrapPos(ImGui::GetFontSize() * 35.0f);
- ImGui::TextUnformatted(desc);
- ImGui::PopTextWrapPos();
- ImGui::EndTooltip();
- }
+ char* buf_end = buf + buf_size;
+ if (tex_ref._TexData != NULL)
+ buf += ImFormatString(buf, buf_end - buf, "#%03d: ", tex_ref._TexData->UniqueID);
+ return FormatTextureIDForDebugDisplay(buf, (int)(buf_end - buf), tex_ref.GetTexID()); // Calling TexRef::GetTexID() to avoid assert of cmd->GetTexID()
}
+#ifdef IMGUI_ENABLE_FREETYPE
+namespace ImGuiFreeType { IMGUI_API const ImFontLoader* GetFontLoader(); IMGUI_API bool DebugEditFontLoaderFlags(unsigned int* p_font_builder_flags); }
+#endif
+
// [DEBUG] List fonts in a font atlas and display its texture
void ImGui::ShowFontAtlas(ImFontAtlas* atlas)
{
+ ImGuiContext& g = *GImGui;
+ ImGuiIO& io = g.IO;
+ ImGuiStyle& style = g.Style;
+
+ BeginDisabled();
+ CheckboxFlags("io.BackendFlags: RendererHasTextures", &io.BackendFlags, ImGuiBackendFlags_RendererHasTextures);
+ EndDisabled();
+ ShowFontSelector("Font");
+ //BeginDisabled((io.BackendFlags & ImGuiBackendFlags_RendererHasTextures) == 0);
+ if (DragFloat("FontSizeBase", &style.FontSizeBase, 0.20f, 5.0f, 100.0f, "%.0f"))
+ style._NextFrameFontSizeBase = style.FontSizeBase; // FIXME: Temporary hack until we finish remaining work.
+ SameLine(0.0f, 0.0f); Text(" (out %.2f)", GetFontSize());
+ SameLine(); MetricsHelpMarker("- This is scaling font only. General scaling will come later.");
+ DragFloat("FontScaleMain", &style.FontScaleMain, 0.02f, 0.5f, 4.0f);
+ //BeginDisabled(io.ConfigDpiScaleFonts);
+ DragFloat("FontScaleDpi", &style.FontScaleDpi, 0.02f, 0.5f, 4.0f);
+ //SetItemTooltip("When io.ConfigDpiScaleFonts is set, this value is automatically overwritten.");
+ //EndDisabled();
+ if ((io.BackendFlags & ImGuiBackendFlags_RendererHasTextures) == 0)
+ {
+ BulletText("Warning: Font scaling will NOT be smooth, because\nImGuiBackendFlags_RendererHasTextures is not set!");
+ BulletText("For instructions, see:");
+ SameLine();
+ TextLinkOpenURL("docs/BACKENDS.md", "https://github.com/ocornut/imgui/blob/master/docs/BACKENDS.md");
+ }
+ BulletText("Load a nice font for better results!");
+ BulletText("Please submit feedback:");
+ SameLine(); TextLinkOpenURL("#8465", "https://github.com/ocornut/imgui/issues/8465");
+ BulletText("Read FAQ for more details:");
+ SameLine(); TextLinkOpenURL("dearimgui.com/faq", "https://www.dearimgui.com/faq/");
+ //EndDisabled();
+
+ SeparatorText("Font List");
+
+ ImGuiMetricsConfig* cfg = &g.DebugMetricsConfig;
+ Checkbox("Show font preview", &cfg->ShowFontPreview);
+
+ // Font loaders
+ if (TreeNode("Loader", "Loader: \'%s\'", atlas->FontLoaderName ? atlas->FontLoaderName : "NULL"))
+ {
+ const ImFontLoader* loader_current = atlas->FontLoader;
+ BeginDisabled(!atlas->RendererHasTextures);
+#ifdef IMGUI_ENABLE_STB_TRUETYPE
+ const ImFontLoader* loader_stbtruetype = ImFontAtlasGetFontLoaderForStbTruetype();
+ if (RadioButton("stb_truetype", loader_current == loader_stbtruetype))
+ atlas->SetFontLoader(loader_stbtruetype);
+#else
+ BeginDisabled();
+ RadioButton("stb_truetype", false);
+ SetItemTooltip("Requires #define IMGUI_ENABLE_STB_TRUETYPE");
+ EndDisabled();
+#endif
+ SameLine();
+#ifdef IMGUI_ENABLE_FREETYPE
+ const ImFontLoader* loader_freetype = ImGuiFreeType::GetFontLoader();
+ if (RadioButton("FreeType", loader_current == loader_freetype))
+ atlas->SetFontLoader(loader_freetype);
+ if (loader_current == loader_freetype)
+ {
+ unsigned int loader_flags = atlas->FontLoaderFlags;
+ Text("Shared FreeType Loader Flags: 0x%08X", loader_flags);
+ if (ImGuiFreeType::DebugEditFontLoaderFlags(&loader_flags))
+ {
+ for (ImFont* font : atlas->Fonts)
+ ImFontAtlasFontDestroyOutput(atlas, font);
+ atlas->FontLoaderFlags = loader_flags;
+ for (ImFont* font : atlas->Fonts)
+ ImFontAtlasFontInitOutput(atlas, font);
+ }
+ }
+#else
+ BeginDisabled();
+ RadioButton("FreeType", false);
+ SetItemTooltip("Requires #define IMGUI_ENABLE_FREETYPE + imgui_freetype.cpp.");
+ EndDisabled();
+#endif
+ EndDisabled();
+ TreePop();
+ }
+
+ // Font list
for (ImFont* font : atlas->Fonts)
{
PushID(font);
DebugNodeFont(font);
PopID();
}
- if (TreeNode("Font Atlas", "Font Atlas (%dx%d pixels)", atlas->TexWidth, atlas->TexHeight))
+
+ SeparatorText("Font Atlas");
+ if (Button("Compact"))
+ atlas->CompactCache();
+ SameLine();
+ if (Button("Grow"))
+ ImFontAtlasTextureGrow(atlas);
+ SameLine();
+ if (Button("Clear All"))
+ ImFontAtlasBuildClear(atlas);
+ SetItemTooltip("Destroy cache and custom rectangles.");
+
+ for (int tex_n = 0; tex_n < atlas->TexList.Size; tex_n++)
{
- ImGuiContext& g = *GImGui;
- PushStyleVar(ImGuiStyleVar_ImageBorderSize, ImMax(1.0f, g.Style.ImageBorderSize));
- ImageWithBg(atlas->TexID, ImVec2((float)atlas->TexWidth, (float)atlas->TexHeight), ImVec2(0.0f, 0.0f), ImVec2(1.0f, 1.0f), ImVec4(0.0f, 0.0f, 0.0f, 1.0f));
+ ImTextureData* tex = atlas->TexList[tex_n];
+ if (tex_n > 0)
+ SameLine();
+ Text("Tex: %dx%d", tex->Width, tex->Height);
+ }
+ const int packed_surface_sqrt = (int)sqrtf((float)atlas->Builder->RectsPackedSurface);
+ const int discarded_surface_sqrt = (int)sqrtf((float)atlas->Builder->RectsDiscardedSurface);
+ Text("Packed rects: %d, area: about %d px ~%dx%d px", atlas->Builder->RectsPackedCount, atlas->Builder->RectsPackedSurface, packed_surface_sqrt, packed_surface_sqrt);
+ Text("incl. Discarded rects: %d, area: about %d px ~%dx%d px", atlas->Builder->RectsDiscardedCount, atlas->Builder->RectsDiscardedSurface, discarded_surface_sqrt, discarded_surface_sqrt);
+
+ ImFontAtlasRectId highlight_r_id = ImFontAtlasRectId_Invalid;
+ if (TreeNode("Rects Index", "Rects Index (%d)", atlas->Builder->RectsPackedCount)) // <-- Use count of used rectangles
+ {
+ PushStyleVar(ImGuiStyleVar_ImageBorderSize, 1.0f);
+ if (BeginTable("##table", 2, ImGuiTableFlags_RowBg | ImGuiTableFlags_Borders | ImGuiTableFlags_ScrollY, ImVec2(0.0f, GetTextLineHeightWithSpacing() * 12)))
+ {
+ for (const ImFontAtlasRectEntry& entry : atlas->Builder->RectsIndex)
+ if (entry.IsUsed)
+ {
+ ImFontAtlasRectId id = ImFontAtlasRectId_Make(atlas->Builder->RectsIndex.index_from_ptr(&entry), entry.Generation);
+ ImFontAtlasRect r = {};
+ atlas->GetCustomRect(id, &r);
+ const char* buf;
+ ImFormatStringToTempBuffer(&buf, NULL, "ID:%08X, used:%d, { w:%3d, h:%3d } { x:%4d, y:%4d }", id, entry.IsUsed, r.w, r.h, r.x, r.y);
+ TableNextColumn();
+ Selectable(buf);
+ if (IsItemHovered())
+ highlight_r_id = id;
+ TableNextColumn();
+ Image(atlas->TexRef, ImVec2(r.w, r.h), r.uv0, r.uv1);
+ }
+ EndTable();
+ }
PopStyleVar();
TreePop();
}
+
+ // Texture list
+ // (ensure the last texture always use the same ID, so we can keep it open neatly)
+ ImFontAtlasRect highlight_r;
+ if (highlight_r_id != ImFontAtlasRectId_Invalid)
+ atlas->GetCustomRect(highlight_r_id, &highlight_r);
+ for (int tex_n = 0; tex_n < atlas->TexList.Size; tex_n++)
+ {
+ if (tex_n == atlas->TexList.Size - 1)
+ SetNextItemOpen(true, ImGuiCond_Once);
+ DebugNodeTexture(atlas->TexList[tex_n], atlas->TexList.Size - 1 - tex_n, (highlight_r_id != ImFontAtlasRectId_Invalid) ? &highlight_r : NULL);
+ }
+}
+
+void ImGui::DebugNodeTexture(ImTextureData* tex, int int_id, const ImFontAtlasRect* highlight_rect)
+{
+ ImGuiContext& g = *GImGui;
+ PushID(int_id);
+ if (TreeNode("", "Texture #%03d (%dx%d pixels)", tex->UniqueID, tex->Width, tex->Height))
+ {
+ ImGuiMetricsConfig* cfg = &g.DebugMetricsConfig;
+ Checkbox("Show used rect", &cfg->ShowTextureUsedRect);
+ PushStyleVar(ImGuiStyleVar_ImageBorderSize, ImMax(1.0f, g.Style.ImageBorderSize));
+ ImVec2 p = GetCursorScreenPos();
+ if (tex->WantDestroyNextFrame)
+ Dummy(ImVec2((float)tex->Width, (float)tex->Height));
+ else
+ ImageWithBg(tex->GetTexRef(), ImVec2((float)tex->Width, (float)tex->Height), ImVec2(0.0f, 0.0f), ImVec2(1.0f, 1.0f), ImVec4(0.0f, 0.0f, 0.0f, 1.0f));
+ if (cfg->ShowTextureUsedRect)
+ GetWindowDrawList()->AddRect(ImVec2(p.x + tex->UsedRect.x, p.y + tex->UsedRect.y), ImVec2(p.x + tex->UsedRect.x + tex->UsedRect.w, p.y + tex->UsedRect.y + tex->UsedRect.h), IM_COL32(255, 0, 255, 255));
+ if (highlight_rect != NULL)
+ {
+ ImRect r_outer(p.x, p.y, p.x + tex->Width, p.y + tex->Height);
+ ImRect r_inner(p.x + highlight_rect->x, p.y + highlight_rect->y, p.x + highlight_rect->x + highlight_rect->w, p.y + highlight_rect->y + highlight_rect->h);
+ RenderRectFilledWithHole(GetWindowDrawList(), r_outer, r_inner, IM_COL32(0, 0, 0, 100), 0.0f);
+ GetWindowDrawList()->AddRect(r_inner.Min - ImVec2(1, 1), r_inner.Max + ImVec2(1, 1), IM_COL32(255, 255, 0, 255));
+ }
+ PopStyleVar();
+
+ char texid_desc[30];
+ Text("Status = %s (%d), Format = %s (%d), UseColors = %d", ImTextureDataGetStatusName(tex->Status), tex->Status, ImTextureDataGetFormatName(tex->Format), tex->Format, tex->UseColors);
+ Text("TexID = %s, BackendUserData = %p", FormatTextureRefForDebugDisplay(texid_desc, IM_ARRAYSIZE(texid_desc), tex->GetTexRef()), tex->BackendUserData);
+ TreePop();
+ }
+ PopID();
}
void ImGui::ShowMetricsWindow(bool* p_open)
@@ -21235,6 +22044,10 @@ void ImGui::ShowMetricsWindow(bool* p_open)
}
};
+#ifdef IMGUI_DEBUG_HIGHLIGHT_ALL_ID_CONFLICTS
+ TextColored(ImVec4(1.0f, 0.0f, 0.0f, 1.0f), "IMGUI_DEBUG_HIGHLIGHT_ALL_ID_CONFLICTS is enabled.\nMust disable after use! Otherwise Dear ImGui will run slower.\n");
+#endif
+
// Tools
if (TreeNode("Tools"))
{
@@ -21438,6 +22251,14 @@ void ImGui::ShowMetricsWindow(bool* p_open)
TreePop();
}
+ // Details for Fonts
+ for (ImFontAtlas* atlas : g.FontAtlases)
+ if (TreeNode((void*)atlas, "Fonts (%d), Textures (%d)", atlas->Fonts.Size, atlas->TexList.Size))
+ {
+ ShowFontAtlas(atlas);
+ TreePop();
+ }
+
// Details for Popups
if (TreeNode("Popups", "Popups (%d)", g.OpenPopupStack.Size))
{
@@ -21474,14 +22295,6 @@ void ImGui::ShowMetricsWindow(bool* p_open)
TreePop();
}
- // Details for Fonts
- ImFontAtlas* atlas = g.IO.Fonts;
- if (TreeNode("Fonts", "Fonts (%d)", atlas->Fonts.Size))
- {
- ShowFontAtlas(atlas);
- TreePop();
- }
-
// Details for InputText
if (TreeNode("InputText"))
{
@@ -22025,8 +22838,8 @@ void ImGui::DebugNodeDrawList(ImGuiWindow* window, ImGuiViewportP* viewport, con
continue;
}
- char texid_desc[20];
- FormatTextureIDForDebugDisplay(texid_desc, IM_ARRAYSIZE(texid_desc), pcmd->TextureId);
+ char texid_desc[30];
+ FormatTextureRefForDebugDisplay(texid_desc, IM_ARRAYSIZE(texid_desc), pcmd->TexRef);
char buf[300];
ImFormatString(buf, IM_ARRAYSIZE(buf), "DrawCmd:%5d tris, Tex %s, ClipRect (%4.0f,%4.0f)-(%4.0f,%4.0f)",
pcmd->ElemCount / 3, texid_desc, pcmd->ClipRect.x, pcmd->ClipRect.y, pcmd->ClipRect.z, pcmd->ClipRect.w);
@@ -22115,19 +22928,39 @@ void ImGui::DebugNodeDrawCmdShowMeshAndBoundingBox(ImDrawList* out_draw_list, co
out_draw_list->Flags = backup_flags;
}
+// [DEBUG] Compute mask of inputs with the same codepoint.
+static int CalcFontGlyphSrcOverlapMask(ImFontAtlas* atlas, ImFont* font, unsigned int codepoint)
+{
+ int mask = 0, count = 0;
+ for (int src_n = 0; src_n < font->Sources.Size; src_n++)
+ {
+ ImFontConfig* src = font->Sources[src_n];
+ if (!(src->FontLoader ? src->FontLoader : atlas->FontLoader)->FontSrcContainsGlyph(atlas, src, (ImWchar)codepoint))
+ continue;
+ mask |= (1 << src_n);
+ count++;
+ }
+ return count > 1 ? mask : 0;
+}
+
// [DEBUG] Display details for a single font, called by ShowStyleEditor().
void ImGui::DebugNodeFont(ImFont* font)
{
- bool opened = TreeNode(font, "Font: \"%s\": %.2f px, %d glyphs, %d sources(s)",
- font->Sources ? font->Sources[0].Name : "", font->FontSize, font->Glyphs.Size, font->SourcesCount);
+ ImGuiContext& g = *GImGui;
+ ImGuiMetricsConfig* cfg = &g.DebugMetricsConfig;
+ ImFontAtlas* atlas = font->ContainerAtlas;
+ bool opened = TreeNode(font, "Font: \"%s\": %d sources(s)", font->GetDebugName(), font->Sources.Size);
// Display preview text
if (!opened)
Indent();
Indent();
- PushFont(font);
- Text("The quick brown fox jumps over the lazy dog");
- PopFont();
+ if (cfg->ShowFontPreview)
+ {
+ PushFont(font, 0.0f);
+ Text("The quick brown fox jumps over the lazy dog");
+ PopFont();
+ }
if (!opened)
{
Unindent();
@@ -22136,90 +22969,182 @@ void ImGui::DebugNodeFont(ImFont* font)
}
if (SmallButton("Set as default"))
GetIO().FontDefault = font;
+ SameLine();
+ BeginDisabled(atlas->Fonts.Size <= 1 || atlas->Locked);
+ if (SmallButton("Remove"))
+ atlas->RemoveFont(font);
+ EndDisabled();
+ SameLine();
+ if (SmallButton("Clear bakes"))
+ ImFontAtlasFontDiscardBakes(atlas, font, 0);
+ SameLine();
+ if (SmallButton("Clear unused"))
+ ImFontAtlasFontDiscardBakes(atlas, font, 2);
// Display details
+#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
SetNextItemWidth(GetFontSize() * 8);
DragFloat("Font scale", &font->Scale, 0.005f, 0.3f, 2.0f, "%.1f");
- SameLine(); MetricsHelpMarker(
+ /*SameLine(); MetricsHelpMarker(
"Note that the default embedded font is NOT meant to be scaled.\n\n"
"Font are currently rendered into bitmaps at a given size at the time of building the atlas. "
"You may oversample them to get some flexibility with scaling. "
"You can also render at multiple sizes and select which one to use at runtime.\n\n"
- "(Glimmer of hope: the atlas system will be rewritten in the future to make scaling more flexible.)");
- Text("Ascent: %f, Descent: %f, Height: %f", font->Ascent, font->Descent, font->Ascent - font->Descent);
+ "(Glimmer of hope: the atlas system will be rewritten in the future to make scaling more flexible.)");*/
+#endif
+
char c_str[5];
- Text("Fallback character: '%s' (U+%04X)", ImTextCharToUtf8(c_str, font->FallbackChar), font->FallbackChar);
- Text("Ellipsis character: '%s' (U+%04X)", ImTextCharToUtf8(c_str, font->EllipsisChar), font->EllipsisChar);
- const int surface_sqrt = (int)ImSqrt((float)font->MetricsTotalSurface);
- Text("Texture Area: about %d px ~%dx%d px", font->MetricsTotalSurface, surface_sqrt, surface_sqrt);
- for (int config_i = 0; config_i < font->SourcesCount; config_i++)
- if (font->Sources)
- {
- const ImFontConfig* src = &font->Sources[config_i];
- int oversample_h, oversample_v;
- ImFontAtlasBuildGetOversampleFactors(src, &oversample_h, &oversample_v);
- BulletText("Input %d: \'%s\', Oversample: (%d=>%d,%d=>%d), PixelSnapH: %d, Offset: (%.1f,%.1f)",
- config_i, src->Name, src->OversampleH, oversample_h, src->OversampleV, oversample_v, src->PixelSnapH, src->GlyphOffset.x, src->GlyphOffset.y);
- }
+ ImTextCharToUtf8(c_str, font->FallbackChar);
+ Text("Fallback character: '%s' (U+%04X)", c_str, font->FallbackChar);
+ ImTextCharToUtf8(c_str, font->EllipsisChar);
+ Text("Ellipsis character: '%s' (U+%04X)", c_str, font->EllipsisChar);
- // Display all glyphs of the fonts in separate pages of 256 characters
+ for (int src_n = 0; src_n < font->Sources.Size; src_n++)
{
- if (TreeNode("Glyphs", "Glyphs (%d)", font->Glyphs.Size))
+ ImFontConfig* src = font->Sources[src_n];
+ if (TreeNode(src, "Input %d: \'%s\' [%d], Oversample: %d,%d, PixelSnapH: %d, Offset: (%.1f,%.1f)",
+ src_n, src->Name, src->FontNo, src->OversampleH, src->OversampleV, src->PixelSnapH, src->GlyphOffset.x, src->GlyphOffset.y))
{
- ImDrawList* draw_list = GetWindowDrawList();
- const ImU32 glyph_col = GetColorU32(ImGuiCol_Text);
- const float cell_size = font->FontSize * 1;
- const float cell_spacing = GetStyle().ItemSpacing.y;
- for (unsigned int base = 0; base <= IM_UNICODE_CODEPOINT_MAX; base += 256)
+ const ImFontLoader* loader = src->FontLoader ? src->FontLoader : atlas->FontLoader;
+ Text("Loader: '%s'", loader->Name ? loader->Name : "N/A");
+#ifdef IMGUI_ENABLE_FREETYPE
+ if (loader->Name != NULL && strcmp(loader->Name, "FreeType") == 0)
{
- // Skip ahead if a large bunch of glyphs are not present in the font (test in chunks of 4k)
- // This is only a small optimization to reduce the number of iterations when IM_UNICODE_MAX_CODEPOINT
- // is large // (if ImWchar==ImWchar32 we will do at least about 272 queries here)
- if (!(base & 8191) && font->IsGlyphRangeUnused(base, base + 8191))
+ unsigned int loader_flags = src->FontLoaderFlags;
+ Text("FreeType Loader Flags: 0x%08X", loader_flags);
+ if (ImGuiFreeType::DebugEditFontLoaderFlags(&loader_flags))
{
- base += 8192 - 256;
- continue;
+ ImFontAtlasFontDestroyOutput(atlas, font);
+ src->FontLoaderFlags = loader_flags;
+ ImFontAtlasFontInitOutput(atlas, font);
}
-
- int count = 0;
- for (unsigned int n = 0; n < 256; n++)
- if (font->FindGlyphNoFallback((ImWchar)(base + n)))
- count++;
- if (count <= 0)
- continue;
- if (!TreeNode((void*)(intptr_t)base, "U+%04X..U+%04X (%d %s)", base, base + 255, count, count > 1 ? "glyphs" : "glyph"))
- continue;
-
- // Draw a 16x16 grid of glyphs
- ImVec2 base_pos = GetCursorScreenPos();
- for (unsigned int n = 0; n < 256; n++)
- {
- // We use ImFont::RenderChar as a shortcut because we don't have UTF-8 conversion functions
- // available here and thus cannot easily generate a zero-terminated UTF-8 encoded string.
- ImVec2 cell_p1(base_pos.x + (n % 16) * (cell_size + cell_spacing), base_pos.y + (n / 16) * (cell_size + cell_spacing));
- ImVec2 cell_p2(cell_p1.x + cell_size, cell_p1.y + cell_size);
- const ImFontGlyph* glyph = font->FindGlyphNoFallback((ImWchar)(base + n));
- draw_list->AddRect(cell_p1, cell_p2, glyph ? IM_COL32(255, 255, 255, 100) : IM_COL32(255, 255, 255, 50));
- if (!glyph)
- continue;
- font->RenderChar(draw_list, cell_size, cell_p1, glyph_col, (ImWchar)(base + n));
- if (IsMouseHoveringRect(cell_p1, cell_p2) && BeginTooltip())
- {
- DebugNodeFontGlyph(font, glyph);
- EndTooltip();
- }
- }
- Dummy(ImVec2((cell_size + cell_spacing) * 16, (cell_size + cell_spacing) * 16));
- TreePop();
}
+#endif
TreePop();
}
}
+ if (font->Sources.Size > 1 && TreeNode("Input Glyphs Overlap Detection Tool"))
+ {
+ TextWrapped("- First Input that contains the glyph is used.\n"
+ "- Use ImFontConfig::GlyphExcludeRanges[] to specify ranges to ignore glyph in given Input.\n- Prefer using a small number of ranges as the list is scanned every time a new glyph is loaded,\n - e.g. GlyphExcludeRanges[] = { ICON_MIN_FA, ICON_MAX_FA, 0 };\n- This tool doesn't cache results and is slow, don't keep it open!");
+ if (BeginTable("table", 2))
+ {
+ for (unsigned int c = 0; c < 0x10000; c++)
+ if (int overlap_mask = CalcFontGlyphSrcOverlapMask(atlas, font, c))
+ {
+ unsigned int c_end = c + 1;
+ while (c_end < 0x10000 && CalcFontGlyphSrcOverlapMask(atlas, font, c_end) == overlap_mask)
+ c_end++;
+ if (TableNextColumn() && TreeNode((void*)(intptr_t)c, "U+%04X-U+%04X: %d codepoints in %d inputs", c, c_end - 1, c_end - c, ImCountSetBits(overlap_mask)))
+ {
+ char utf8_buf[5];
+ for (unsigned int n = c; n < c_end; n++)
+ {
+ ImTextCharToUtf8(utf8_buf, n);
+ BulletText("Codepoint U+%04X (%s)", n, utf8_buf);
+ }
+ TreePop();
+ }
+ TableNextColumn();
+ for (int src_n = 0; src_n < font->Sources.Size; src_n++)
+ if (overlap_mask & (1 << src_n))
+ {
+ Text("%d ", src_n);
+ SameLine();
+ }
+ c = c_end - 1;
+ }
+ EndTable();
+ }
+ TreePop();
+ }
+
+ // Display all glyphs of the fonts in separate pages of 256 characters
+ for (int baked_n = 0; baked_n < atlas->Builder->BakedPool.Size; baked_n++)
+ {
+ ImFontBaked* baked = &atlas->Builder->BakedPool[baked_n];
+ if (baked->ContainerFont != font)
+ continue;
+ PushID(baked_n);
+ if (TreeNode("Glyphs", "Baked at { %.2fpx, d.%.2f }: %d glyphs%s", baked->Size, baked->RasterizerDensity, baked->Glyphs.Size, (baked->LastUsedFrame < atlas->Builder->FrameCount - 1) ? " *Unused*" : ""))
+ {
+ if (SmallButton("Load all"))
+ for (unsigned int base = 0; base <= IM_UNICODE_CODEPOINT_MAX; base++)
+ baked->FindGlyph((ImWchar)base);
+
+ const int surface_sqrt = (int)ImSqrt((float)baked->MetricsTotalSurface);
+ Text("Ascent: %f, Descent: %f, Ascent-Descent: %f", baked->Ascent, baked->Descent, baked->Ascent - baked->Descent);
+ Text("Texture Area: about %d px ~%dx%d px", baked->MetricsTotalSurface, surface_sqrt, surface_sqrt);
+ for (int src_n = 0; src_n < font->Sources.Size; src_n++)
+ {
+ ImFontConfig* src = font->Sources[src_n];
+ int oversample_h, oversample_v;
+ ImFontAtlasBuildGetOversampleFactors(src, baked, &oversample_h, &oversample_v);
+ BulletText("Input %d: \'%s\', Oversample: (%d=>%d,%d=>%d), PixelSnapH: %d, Offset: (%.1f,%.1f)",
+ src_n, src->Name, src->OversampleH, oversample_h, src->OversampleV, oversample_v, src->PixelSnapH, src->GlyphOffset.x, src->GlyphOffset.y);
+ }
+
+ DebugNodeFontGlyphesForSrcMask(font, baked, ~0);
+ TreePop();
+ }
+ PopID();
+ }
TreePop();
Unindent();
}
-void ImGui::DebugNodeFontGlyph(ImFont*, const ImFontGlyph* glyph)
+void ImGui::DebugNodeFontGlyphesForSrcMask(ImFont* font, ImFontBaked* baked, int src_mask)
+{
+ ImDrawList* draw_list = GetWindowDrawList();
+ const ImU32 glyph_col = GetColorU32(ImGuiCol_Text);
+ const float cell_size = baked->Size * 1;
+ const float cell_spacing = GetStyle().ItemSpacing.y;
+ for (unsigned int base = 0; base <= IM_UNICODE_CODEPOINT_MAX; base += 256)
+ {
+ // Skip ahead if a large bunch of glyphs are not present in the font (test in chunks of 4k)
+ // This is only a small optimization to reduce the number of iterations when IM_UNICODE_MAX_CODEPOINT
+ // is large // (if ImWchar==ImWchar32 we will do at least about 272 queries here)
+ if (!(base & 8191) && font->IsGlyphRangeUnused(base, base + 8191))
+ {
+ base += 8192 - 256;
+ continue;
+ }
+
+ int count = 0;
+ for (unsigned int n = 0; n < 256; n++)
+ if (const ImFontGlyph* glyph = baked->IsGlyphLoaded((ImWchar)(base + n)) ? baked->FindGlyph((ImWchar)(base + n)) : NULL)
+ if (src_mask & (1 << glyph->SourceIdx))
+ count++;
+ if (count <= 0)
+ continue;
+ if (!TreeNode((void*)(intptr_t)base, "U+%04X..U+%04X (%d %s)", base, base + 255, count, count > 1 ? "glyphs" : "glyph"))
+ continue;
+
+ // Draw a 16x16 grid of glyphs
+ ImVec2 base_pos = GetCursorScreenPos();
+ for (unsigned int n = 0; n < 256; n++)
+ {
+ // We use ImFont::RenderChar as a shortcut because we don't have UTF-8 conversion functions
+ // available here and thus cannot easily generate a zero-terminated UTF-8 encoded string.
+ ImVec2 cell_p1(base_pos.x + (n % 16) * (cell_size + cell_spacing), base_pos.y + (n / 16) * (cell_size + cell_spacing));
+ ImVec2 cell_p2(cell_p1.x + cell_size, cell_p1.y + cell_size);
+ const ImFontGlyph* glyph = baked->IsGlyphLoaded((ImWchar)(base + n)) ? baked->FindGlyph((ImWchar)(base + n)) : NULL;
+ draw_list->AddRect(cell_p1, cell_p2, glyph ? IM_COL32(255, 255, 255, 100) : IM_COL32(255, 255, 255, 50));
+ if (!glyph || (src_mask & (1 << glyph->SourceIdx)) == 0)
+ continue;
+ font->RenderChar(draw_list, cell_size, cell_p1, glyph_col, (ImWchar)(base + n));
+ if (IsMouseHoveringRect(cell_p1, cell_p2) && BeginTooltip())
+ {
+ DebugNodeFontGlyph(font, glyph);
+ EndTooltip();
+ }
+ }
+ Dummy(ImVec2((cell_size + cell_spacing) * 16, (cell_size + cell_spacing) * 16));
+ TreePop();
+ }
+}
+
+void ImGui::DebugNodeFontGlyph(ImFont* font, const ImFontGlyph* glyph)
{
Text("Codepoint: U+%04X", glyph->Codepoint);
Separator();
@@ -22227,6 +23152,12 @@ void ImGui::DebugNodeFontGlyph(ImFont*, const ImFontGlyph* glyph)
Text("AdvanceX: %.1f", glyph->AdvanceX);
Text("Pos: (%.2f,%.2f)->(%.2f,%.2f)", glyph->X0, glyph->Y0, glyph->X1, glyph->Y1);
Text("UV: (%.3f,%.3f)->(%.3f,%.3f)", glyph->U0, glyph->V0, glyph->U1, glyph->V1);
+ if (glyph->PackId >= 0)
+ {
+ ImTextureRect* r = ImFontAtlasPackGetRect(font->ContainerAtlas, glyph->PackId);
+ Text("PackId: 0x%X (%dx%d rect at %d,%d)", glyph->PackId, r->w, r->h, r->x, r->y);
+ }
+ Text("SourceIdx: %d", glyph->SourceIdx);
}
// [DEBUG] Display contents of ImGuiStorage
@@ -22293,8 +23224,9 @@ void ImGui::DebugNodeViewport(ImGuiViewportP* viewport)
if (open)
{
ImGuiWindowFlags flags = viewport->Flags;
- BulletText("Main Pos: (%.0f,%.0f), Size: (%.0f,%.0f)\nWorkArea Inset Left: %.0f Top: %.0f, Right: %.0f, Bottom: %.0f\nMonitor: %d, DpiScale: %.0f%%",
+ BulletText("Main Pos: (%.0f,%.0f), Size: (%.0f,%.0f)\nFrameBufferScale: (%.2f,%.2f)\nWorkArea Inset Left: %.0f Top: %.0f, Right: %.0f, Bottom: %.0f\nMonitor: %d, DpiScale: %.0f%%",
viewport->Pos.x, viewport->Pos.y, viewport->Size.x, viewport->Size.y,
+ viewport->FramebufferScale.x, viewport->FramebufferScale.y,
viewport->WorkInsetMin.x, viewport->WorkInsetMin.y, viewport->WorkInsetMax.x, viewport->WorkInsetMax.y,
viewport->PlatformMonitor, viewport->DpiScale * 100.0f);
if (viewport->Idx > 0) { SameLine(); if (SmallButton("Reset Pos")) { viewport->Pos = ImVec2(200, 200); viewport->UpdateWorkRect(); if (viewport->Window) viewport->Window->Pos = viewport->Pos; } }
@@ -22380,7 +23312,7 @@ void ImGui::DebugNodeWindow(ImGuiWindow* window, const char* label)
}
const ImVec2* pr = window->NavPreferredScoringPosRel;
for (int layer = 0; layer < ImGuiNavLayer_COUNT; layer++)
- BulletText("NavPreferredScoringPosRel[%d] = {%.1f,%.1f)", layer, (pr[layer].x == FLT_MAX ? -99999.0f : pr[layer].x), (pr[layer].y == FLT_MAX ? -99999.0f : pr[layer].y)); // Display as 99999.0f so it looks neater.
+ BulletText("NavPreferredScoringPosRel[%d] = (%.1f,%.1f)", layer, (pr[layer].x == FLT_MAX ? -99999.0f : pr[layer].x), (pr[layer].y == FLT_MAX ? -99999.0f : pr[layer].y)); // Display as 99999.0f so it looks neater.
BulletText("NavLayersActiveMask: %X, NavLastChildNavWindow: %s", window->DC.NavLayersActiveMask, window->NavLastChildNavWindow ? window->NavLastChildNavWindow->Name : "NULL");
BulletText("Viewport: %d%s, ViewportId: 0x%08X, ViewportPos: (%.1f,%.1f)", window->Viewport ? window->Viewport->Idx : -1, window->ViewportOwned ? " (Owned)" : "", window->ViewportId, window->ViewportPos.x, window->ViewportPos.y);
@@ -22439,9 +23371,9 @@ void ImGui::DebugNodeWindowsListByBeginStackParent(ImGuiWindow** windows, int wi
ImFormatString(buf, IM_ARRAYSIZE(buf), "[%04d] Window", window->BeginOrderWithinContext);
//BulletText("[%04d] Window '%s'", window->BeginOrderWithinContext, window->Name);
DebugNodeWindow(window, buf);
- Indent();
+ TreePush(buf);
DebugNodeWindowsListByBeginStackParent(windows + i + 1, windows_size - i - 1, window);
- Unindent();
+ TreePop();
}
}
@@ -22534,7 +23466,7 @@ void ImGui::ShowDebugLogWindow(bool* p_open)
ShowDebugLogFlag("Docking", ImGuiDebugLogFlags_EventDocking);
ShowDebugLogFlag("Focus", ImGuiDebugLogFlags_EventFocus);
ShowDebugLogFlag("IO", ImGuiDebugLogFlags_EventIO);
- //ShowDebugLogFlag("Font", ImGuiDebugLogFlags_EventFont);
+ ShowDebugLogFlag("Font", ImGuiDebugLogFlags_EventFont);
ShowDebugLogFlag("Nav", ImGuiDebugLogFlags_EventNav);
ShowDebugLogFlag("Popup", ImGuiDebugLogFlags_EventPopup);
ShowDebugLogFlag("Selection", ImGuiDebugLogFlags_EventSelection);
@@ -22736,20 +23668,22 @@ void ImGui::UpdateDebugToolStackQueries()
ImGuiIDStackTool* tool = &g.DebugIDStackTool;
// Clear hook when id stack tool is not visible
- g.DebugHookIdInfo = 0;
+ g.DebugHookIdInfoId = 0;
+ tool->QueryHookActive = false;
if (g.FrameCount != tool->LastActiveFrame + 1)
return;
// Update queries. The steps are: -1: query Stack, >= 0: query each stack item
// We can only perform 1 ID Info query every frame. This is designed so the GetID() tests are cheap and constant-time
- const ImGuiID query_id = g.HoveredIdPreviousFrame ? g.HoveredIdPreviousFrame : g.ActiveId;
- if (tool->QueryId != query_id)
+ const ImGuiID query_main_id = g.HoveredIdPreviousFrame ? g.HoveredIdPreviousFrame : g.ActiveId;
+ if (tool->QueryMainId != query_main_id)
{
- tool->QueryId = query_id;
+ tool->QueryMainId = query_main_id;
tool->StackLevel = -1;
tool->Results.resize(0);
+ tool->ResultPathsBuf.resize(0);
}
- if (query_id == 0)
+ if (query_main_id == 0)
return;
// Advance to next stack level when we got our result, or after 2 frames (in case we never get a result)
@@ -22761,11 +23695,15 @@ void ImGui::UpdateDebugToolStackQueries()
// Update hook
stack_level = tool->StackLevel;
if (stack_level == -1)
- g.DebugHookIdInfo = query_id;
- if (stack_level >= 0 && stack_level < tool->Results.Size)
{
- g.DebugHookIdInfo = tool->Results[stack_level].ID;
+ g.DebugHookIdInfoId = query_main_id;
+ tool->QueryHookActive = true;
+ }
+ else if (stack_level >= 0 && stack_level < tool->Results.Size)
+ {
+ g.DebugHookIdInfoId = tool->Results[stack_level].ID;
tool->Results[stack_level].QueryFrameCount++;
+ tool->QueryHookActive = true;
}
}
@@ -22775,11 +23713,17 @@ void ImGui::DebugHookIdInfo(ImGuiID id, ImGuiDataType data_type, const void* dat
ImGuiContext& g = *GImGui;
ImGuiWindow* window = g.CurrentWindow;
ImGuiIDStackTool* tool = &g.DebugIDStackTool;
+ if (tool->QueryHookActive == false)
+ {
+ IM_ASSERT(id == 0);
+ return;
+ }
// Step 0: stack query
// This assumes that the ID was computed with the current ID stack, which tends to be the case for our widget.
if (tool->StackLevel == -1)
{
+ IM_ASSERT(tool->Results.Size == 0);
tool->StackLevel++;
tool->Results.resize(window->IDStack.Size + 1, ImGuiStackLevelInfo());
for (int n = 0; n < window->IDStack.Size + 1; n++)
@@ -22794,42 +23738,48 @@ void ImGui::DebugHookIdInfo(ImGuiID id, ImGuiDataType data_type, const void* dat
ImGuiStackLevelInfo* info = &tool->Results[tool->StackLevel];
IM_ASSERT(info->ID == id && info->QueryFrameCount > 0);
- switch (data_type)
+ if (info->DescOffset == -1)
{
- case ImGuiDataType_S32:
- ImFormatString(info->Desc, IM_ARRAYSIZE(info->Desc), "%d", (int)(intptr_t)data_id);
- break;
- case ImGuiDataType_String:
- ImFormatString(info->Desc, IM_ARRAYSIZE(info->Desc), "%.*s", data_id_end ? (int)((const char*)data_id_end - (const char*)data_id) : (int)ImStrlen((const char*)data_id), (const char*)data_id);
- break;
- case ImGuiDataType_Pointer:
- ImFormatString(info->Desc, IM_ARRAYSIZE(info->Desc), "(void*)0x%p", data_id);
- break;
- case ImGuiDataType_ID:
- if (info->Desc[0] != 0) // PushOverrideID() is often used to avoid hashing twice, which would lead to 2 calls to DebugHookIdInfo(). We prioritize the first one.
- return;
- ImFormatString(info->Desc, IM_ARRAYSIZE(info->Desc), "0x%08X [override]", id);
- break;
- default:
- IM_ASSERT(0);
+ const char* result = NULL;
+ const char* result_end = NULL;
+ switch (data_type)
+ {
+ case ImGuiDataType_S32:
+ ImFormatStringToTempBuffer(&result, &result_end, "%d", (int)(intptr_t)data_id);
+ break;
+ case ImGuiDataType_String:
+ ImFormatStringToTempBuffer(&result, &result_end, "%.*s", data_id_end ? (int)((const char*)data_id_end - (const char*)data_id) : (int)ImStrlen((const char*)data_id), (const char*)data_id);
+ break;
+ case ImGuiDataType_Pointer:
+ ImFormatStringToTempBuffer(&result, &result_end, "(void*)0x%p", data_id);
+ break;
+ case ImGuiDataType_ID:
+ // PushOverrideID() is often used to avoid hashing twice, which would lead to 2 calls to DebugHookIdInfo(). We prioritize the first one.
+ ImFormatStringToTempBuffer(&result, &result_end, "0x%08X [override]", id);
+ break;
+ default:
+ IM_ASSERT(0);
+ }
+ info->DescOffset = tool->ResultPathsBuf.size();
+ tool->ResultPathsBuf.append(result, result_end + 1); // Include zero terminator
}
info->QuerySuccess = true;
- info->DataType = data_type;
+ info->DataType = (ImS8)data_type;
}
static int StackToolFormatLevelInfo(ImGuiIDStackTool* tool, int n, bool format_for_ui, char* buf, size_t buf_size)
{
ImGuiStackLevelInfo* info = &tool->Results[n];
- ImGuiWindow* window = (info->Desc[0] == 0 && n == 0) ? ImGui::FindWindowByID(info->ID) : NULL;
+ ImGuiWindow* window = (info->DescOffset == -1 && n == 0) ? ImGui::FindWindowByID(info->ID) : NULL;
if (window) // Source: window name (because the root ID don't call GetID() and so doesn't get hooked)
- return ImFormatString(buf, buf_size, format_for_ui ? "\"%s\" [window]" : "%s", window->Name);
+ return ImFormatString(buf, buf_size, format_for_ui ? "\"%s\" [window]" : "%s", ImHashSkipUncontributingPrefix(window->Name));
if (info->QuerySuccess) // Source: GetID() hooks (prioritize over ItemInfo() because we frequently use patterns like: PushID(str), Button("") where they both have same id)
- return ImFormatString(buf, buf_size, (format_for_ui && info->DataType == ImGuiDataType_String) ? "\"%s\"" : "%s", info->Desc);
+ return ImFormatString(buf, buf_size, (format_for_ui && info->DataType == ImGuiDataType_String) ? "\"%s\"" : "%s", ImHashSkipUncontributingPrefix(&tool->ResultPathsBuf.Buf[info->DescOffset]));
if (tool->StackLevel < tool->Results.Size) // Only start using fallback below when all queries are done, so during queries we don't flickering ??? markers.
return (*buf = 0);
#ifdef IMGUI_ENABLE_TEST_ENGINE
if (const char* label = ImGuiTestEngine_FindItemDebugLabel(GImGui, info->ID)) // Source: ImGuiTestEngine's ItemInfo()
- return ImFormatString(buf, buf_size, format_for_ui ? "??? \"%s\"" : "%s", label);
+ return ImFormatString(buf, buf_size, format_for_ui ? "??? \"%s\"" : "%s", ImHashSkipUncontributingPrefix(label));
#endif
return ImFormatString(buf, buf_size, "???");
}
@@ -22850,38 +23800,47 @@ void ImGui::ShowIDStackToolWindow(bool* p_open)
ImGuiIDStackTool* tool = &g.DebugIDStackTool;
// Build and display path
- tool->ResultPathBuf.resize(0);
+ tool->ResultTempBuf.resize(0);
for (int stack_n = 0; stack_n < tool->Results.Size; stack_n++)
{
char level_desc[256];
StackToolFormatLevelInfo(tool, stack_n, false, level_desc, IM_ARRAYSIZE(level_desc));
- tool->ResultPathBuf.append(stack_n == 0 ? "//" : "/");
- for (int n = 0; level_desc[n]; n++)
+ tool->ResultTempBuf.append(stack_n == 0 ? "//" : "/");
+ for (const char* p = level_desc; *p != 0; )
{
- if (level_desc[n] == '/')
- tool->ResultPathBuf.append("\\");
- tool->ResultPathBuf.append(level_desc + n, level_desc + n + 1);
+ unsigned int c;
+ const char* p_next = p + ImTextCharFromUtf8(&c, p, NULL);
+ if (c == '/')
+ tool->ResultTempBuf.append("\\");
+ if (c < 256 || !tool->OptHexEncodeNonAsciiChars)
+ tool->ResultTempBuf.append(p, p_next);
+ else for (; p < p_next; p++)
+ tool->ResultTempBuf.appendf("\\x%02x", (unsigned char)*p);
+ p = p_next;
}
}
- Text("0x%08X", tool->QueryId);
+ Text("0x%08X", tool->QueryMainId);
SameLine();
MetricsHelpMarker("Hover an item with the mouse to display elements of the ID Stack leading to the item's final ID.\nEach level of the stack correspond to a PushID() call.\nAll levels of the stack are hashed together to make the final ID of a widget (ID displayed at the bottom level of the stack).\nRead FAQ entry about the ID stack for details.");
// CTRL+C to copy path
const float time_since_copy = (float)g.Time - tool->CopyToClipboardLastTime;
+ PushStyleVarY(ImGuiStyleVar_FramePadding, 0.0f);
+ Checkbox("Hex-encode non-ASCII", &tool->OptHexEncodeNonAsciiChars);
SameLine();
- PushStyleVarY(ImGuiStyleVar_FramePadding, 0.0f); Checkbox("Ctrl+C: copy path", &tool->CopyToClipboardOnCtrlC); PopStyleVar();
+ Checkbox("Ctrl+C: copy path", &tool->OptCopyToClipboardOnCtrlC);
+ PopStyleVar();
SameLine();
TextColored((time_since_copy >= 0.0f && time_since_copy < 0.75f && ImFmod(time_since_copy, 0.25f) < 0.25f * 0.5f) ? ImVec4(1.f, 1.f, 0.3f, 1.f) : ImVec4(), "*COPIED*");
- if (tool->CopyToClipboardOnCtrlC && Shortcut(ImGuiMod_Ctrl | ImGuiKey_C, ImGuiInputFlags_RouteGlobal | ImGuiInputFlags_RouteOverFocused))
+ if (tool->OptCopyToClipboardOnCtrlC && Shortcut(ImGuiMod_Ctrl | ImGuiKey_C, ImGuiInputFlags_RouteGlobal | ImGuiInputFlags_RouteOverFocused))
{
tool->CopyToClipboardLastTime = (float)g.Time;
- SetClipboardText(tool->ResultPathBuf.c_str());
+ SetClipboardText(tool->ResultTempBuf.c_str());
}
- Text("- Path \"%s\"", tool->ResultPathBuf.c_str());
+ Text("- Path \"%s\"", tool->ResultTempBuf.c_str());
#ifdef IMGUI_ENABLE_TEST_ENGINE
- Text("- Label \"%s\"", tool->QueryId ? ImGuiTestEngine_FindItemDebugLabel(&g, tool->QueryId) : "");
+ Text("- Label \"%s\"", tool->QueryMainId ? ImGuiTestEngine_FindItemDebugLabel(&g, tool->QueryMainId) : "");
#endif
Separator();
@@ -22921,6 +23880,7 @@ void ImGui::DebugNodeColumns(ImGuiOldColumns*) {}
void ImGui::DebugNodeDrawList(ImGuiWindow*, ImGuiViewportP*, const ImDrawList*, const char*) {}
void ImGui::DebugNodeDrawCmdShowMeshAndBoundingBox(ImDrawList*, const ImDrawList*, const ImDrawCmd*, bool, bool) {}
void ImGui::DebugNodeFont(ImFont*) {}
+void ImGui::DebugNodeFontGlyphesForSrcMask(ImFont*, ImFontBaked*, int) {}
void ImGui::DebugNodeStorage(ImGuiStorage*, const char*) {}
void ImGui::DebugNodeTabBar(ImGuiTabBar*, const char*) {}
void ImGui::DebugNodeWindow(ImGuiWindow*, const char*) {}
@@ -22935,6 +23895,40 @@ void ImGui::DebugHookIdInfo(ImGuiID, ImGuiDataType, const void*, const void*) {}
#endif // #ifndef IMGUI_DISABLE_DEBUG_TOOLS
+#if !defined(IMGUI_DISABLE_DEMO_WINDOWS) || !defined(IMGUI_DISABLE_DEBUG_TOOLS)
+// Demo helper function to select among loaded fonts.
+// Here we use the regular BeginCombo()/EndCombo() api which is the more flexible one.
+void ImGui::ShowFontSelector(const char* label)
+{
+ ImGuiIO& io = GetIO();
+ ImFont* font_current = GetFont();
+ if (BeginCombo(label, font_current->GetDebugName()))
+ {
+ for (ImFont* font : io.Fonts->Fonts)
+ {
+ PushID((void*)font);
+ if (Selectable(font->GetDebugName(), font == font_current, ImGuiSelectableFlags_SelectOnNav))
+ io.FontDefault = font;
+ if (font == font_current)
+ SetItemDefaultFocus();
+ PopID();
+ }
+ EndCombo();
+ }
+ SameLine();
+ if (io.BackendFlags & ImGuiBackendFlags_RendererHasTextures)
+ MetricsHelpMarker(
+ "- Load additional fonts with io.Fonts->AddFontXXX() functions.\n"
+ "- Read FAQ and docs/FONTS.md for more details.");
+ else
+ MetricsHelpMarker(
+ "- Load additional fonts with io.Fonts->AddFontXXX() functions.\n"
+ "- The font atlas is built when calling io.Fonts->GetTexDataAsXXXX() or io.Fonts->Build().\n"
+ "- Read FAQ and docs/FONTS.md for more details.\n"
+ "- If you need to add/remove fonts at runtime (e.g. for DPI change), do it before calling NewFrame().");
+}
+#endif // #if !defined(IMGUI_DISABLE_DEMO_WINDOWS) || !defined(IMGUI_DISABLE_DEBUG_TOOLS)
+
//-----------------------------------------------------------------------------
// Include imgui_user.inl at the end of imgui.cpp to access private data/functions that aren't exposed.
diff --git a/imgui.h b/imgui.h
index 6d5c8c1d8..251b016bf 100644
--- a/imgui.h
+++ b/imgui.h
@@ -1,4 +1,4 @@
-// dear imgui, v1.92.0 WIP
+// dear imgui, v1.92.4 WIP
// (headers)
// Help:
@@ -28,18 +28,19 @@
// Library Version
// (Integer encoded as XYYZZ for use in #if preprocessor conditionals, e.g. '#if IMGUI_VERSION_NUM >= 12345')
-#define IMGUI_VERSION "1.92.0 WIP"
-#define IMGUI_VERSION_NUM 19192
-#define IMGUI_HAS_TABLE
-#define IMGUI_HAS_VIEWPORT // Viewport WIP branch
-#define IMGUI_HAS_DOCK // Docking WIP branch
+#define IMGUI_VERSION "1.92.4 WIP"
+#define IMGUI_VERSION_NUM 19234
+#define IMGUI_HAS_TABLE // Added BeginTable() - from IMGUI_VERSION_NUM >= 18000
+#define IMGUI_HAS_TEXTURES // Added ImGuiBackendFlags_RendererHasTextures - from IMGUI_VERSION_NUM >= 19198
+#define IMGUI_HAS_VIEWPORT // In 'docking' WIP branch.
+#define IMGUI_HAS_DOCK // In 'docking' WIP branch.
/*
Index of this file:
// [SECTION] Header mess
// [SECTION] Forward declarations and basic types
-// [SECTION] Texture identifier (ImTextureID)
+// [SECTION] Texture identifiers (ImTextureID, ImTextureRef)
// [SECTION] Dear ImGui end-user API functions
// [SECTION] Flags & Enumerations
// [SECTION] Tables API flags and structures (ImGuiTableFlags, ImGuiTableColumnFlags, ImGuiTableRowFlags, ImGuiTableBgTarget, ImGuiTableSortSpecs, ImGuiTableColumnSortSpecs)
@@ -50,7 +51,8 @@ Index of this file:
// [SECTION] Helpers (ImGuiOnceUponAFrame, ImGuiTextFilter, ImGuiTextBuffer, ImGuiStorage, ImGuiListClipper, Math Operators, ImColor)
// [SECTION] Multi-Select API flags and structures (ImGuiMultiSelectFlags, ImGuiMultiSelectIO, ImGuiSelectionRequest, ImGuiSelectionBasicStorage, ImGuiSelectionExternalStorage)
// [SECTION] Drawing API (ImDrawCallback, ImDrawCmd, ImDrawIdx, ImDrawVert, ImDrawChannel, ImDrawListSplitter, ImDrawFlags, ImDrawListFlags, ImDrawList, ImDrawData)
-// [SECTION] Font API (ImFontConfig, ImFontGlyph, ImFontGlyphRangesBuilder, ImFontAtlasFlags, ImFontAtlas, ImFont)
+// [SECTION] Texture API (ImTextureFormat, ImTextureStatus, ImTextureRect, ImTextureData)
+// [SECTION] Font API (ImFontConfig, ImFontGlyph, ImFontGlyphRangesBuilder, ImFontAtlasFlags, ImFontAtlas, ImFontBaked, ImFont)
// [SECTION] Viewports (ImGuiViewportFlags, ImGuiViewport)
// [SECTION] ImGuiPlatformIO + other Platform Dependent Interfaces (ImGuiPlatformMonitor, ImGuiPlatformImeData)
// [SECTION] Obsolete functions and types
@@ -100,8 +102,10 @@ Index of this file:
#define IMGUI_CHECKVERSION() ImGui::DebugCheckVersionAndDataLayout(IMGUI_VERSION, sizeof(ImGuiIO), sizeof(ImGuiStyle), sizeof(ImVec2), sizeof(ImVec4), sizeof(ImDrawVert), sizeof(ImDrawIdx))
// Helper Macros - IM_FMTARGS, IM_FMTLIST: Apply printf-style warnings to our formatting functions.
-// (MSVC provides an equivalent mechanism via SAL Annotations but it would require the macros in a different
-// location. e.g. #include + void myprintf(_Printf_format_string_ const char* format, ...))
+// (MSVC provides an equivalent mechanism via SAL Annotations but it requires the macros in a different
+// location. e.g. #include + void myprintf(_Printf_format_string_ const char* format, ...),
+// and only works when using Code Analysis, rather than just normal compiling).
+// (see https://github.com/ocornut/imgui/issues/8871 for a patch to enable this for MSVC's Code Analysis)
#if !defined(IMGUI_USE_STB_SPRINTF) && defined(__MINGW32__) && !defined(__clang__)
#define IM_FMTARGS(FMT) __attribute__((format(gnu_printf, FMT, FMT+1)))
#define IM_FMTLIST(FMT) __attribute__((format(gnu_printf, FMT, 0)))
@@ -171,10 +175,15 @@ struct ImDrawListSplitter; // Helper to split a draw list into differen
struct ImDrawVert; // A single vertex (pos + uv + col = 20 bytes by default. Override layout with IMGUI_OVERRIDE_DRAWVERT_STRUCT_LAYOUT)
struct ImFont; // Runtime data for a single font within a parent ImFontAtlas
struct ImFontAtlas; // Runtime data for multiple fonts, bake multiple fonts into a single texture, TTF/OTF font loader
-struct ImFontBuilderIO; // Opaque interface to a font builder (stb_truetype or FreeType).
+struct ImFontAtlasBuilder; // Opaque storage for building a ImFontAtlas
+struct ImFontAtlasRect; // Output of ImFontAtlas::GetCustomRect() when using custom rectangles.
+struct ImFontBaked; // Baked data for a ImFont at a given size.
struct ImFontConfig; // Configuration data when adding a font or merging fonts
struct ImFontGlyph; // A single font glyph (code point + coordinates within in ImFontAtlas + offset)
struct ImFontGlyphRangesBuilder; // Helper to build glyph ranges from text/string data
+struct ImFontLoader; // Opaque interface to a font loading backend (stb_truetype, FreeType etc.).
+struct ImTextureData; // Specs and pixel storage for a texture used by Dear ImGui.
+struct ImTextureRect; // Coordinates of a rectangle within a texture.
struct ImColor; // Helper functions to create a color that can be converted to either u32 or float4 (*OBSOLETE* please avoid using)
// Forward declarations: ImGui layer
@@ -228,7 +237,9 @@ typedef int ImGuiTableBgTarget; // -> enum ImGuiTableBgTarget_ // Enum: A
// - In VS Code, CLion, etc.: CTRL+click can follow symbols inside comments.
typedef int ImDrawFlags; // -> enum ImDrawFlags_ // Flags: for ImDrawList functions
typedef int ImDrawListFlags; // -> enum ImDrawListFlags_ // Flags: for ImDrawList instance
-typedef int ImFontAtlasFlags; // -> enum ImFontAtlasFlags_ // Flags: for ImFontAtlas build
+typedef int ImDrawTextFlags; // -> enum ImDrawTextFlags_ // Internal, do not use!
+typedef int ImFontFlags; // -> enum ImFontFlags_ // Flags: for ImFont
+typedef int ImFontAtlasFlags; // -> enum ImFontAtlasFlags_ // Flags: for ImFontAtlas
typedef int ImGuiBackendFlags; // -> enum ImGuiBackendFlags_ // Flags: for io.BackendFlags
typedef int ImGuiButtonFlags; // -> enum ImGuiButtonFlags_ // Flags: for InvisibleButton()
typedef int ImGuiChildFlags; // -> enum ImGuiChildFlags_ // Flags: for BeginChild()
@@ -243,6 +254,7 @@ typedef int ImGuiInputFlags; // -> enum ImGuiInputFlags_ // Flags: f
typedef int ImGuiInputTextFlags; // -> enum ImGuiInputTextFlags_ // Flags: for InputText(), InputTextMultiline()
typedef int ImGuiItemFlags; // -> enum ImGuiItemFlags_ // Flags: for PushItemFlag(), shared by all items
typedef int ImGuiKeyChord; // -> ImGuiKey | ImGuiMod_XXX // Flags: for IsKeyChordPressed(), Shortcut() etc. an ImGuiKey optionally OR-ed with one or more ImGuiMod_XXX values.
+typedef int ImGuiListClipperFlags; // -> enum ImGuiListClipperFlags_// Flags: for ImGuiListClipper
typedef int ImGuiPopupFlags; // -> enum ImGuiPopupFlags_ // Flags: for OpenPopup*(), BeginPopupContext*(), IsPopupOpen()
typedef int ImGuiMultiSelectFlags; // -> enum ImGuiMultiSelectFlags_// Flags: for BeginMultiSelect()
typedef int ImGuiSelectableFlags; // -> enum ImGuiSelectableFlags_ // Flags: for Selectable()
@@ -306,18 +318,67 @@ struct ImVec4
IM_MSVC_RUNTIME_CHECKS_RESTORE
//-----------------------------------------------------------------------------
-// [SECTION] Texture identifier (ImTextureID)
+// [SECTION] Texture identifiers (ImTextureID, ImTextureRef)
//-----------------------------------------------------------------------------
-// ImTexture: user data for renderer backend to identify a texture [Compile-time configurable type]
-// - To use something else than an opaque void* pointer: override with e.g. '#define ImTextureID MyTextureType*' in your imconfig.h file.
-// - This can be whatever to you want it to be! read the FAQ about ImTextureID for details.
-// - You can make this a structure with various constructors if you need. You will have to implement ==/!= operators.
-// - (note: before v1.91.4 (2024/10/08) the default type for ImTextureID was void*. Use intermediary intptr_t cast and read FAQ if you have casting warnings)
+// ImTextureID = backend specific, low-level identifier for a texture uploaded in GPU/graphics system.
+// [Compile-time configurable type]
+// - When a Rendered Backend creates a texture, it store its native identifier into a ImTextureID value.
+// (e.g. Used by DX11 backend to a `ID3D11ShaderResourceView*`; Used by OpenGL backends to store `GLuint`;
+// Used by SDLGPU backend to store a `SDL_GPUTextureSamplerBinding*`, etc.).
+// - User may submit their own textures to e.g. ImGui::Image() function by passing this value.
+// - During the rendering loop, the Renderer Backend retrieve the ImTextureID, which stored inside a
+// ImTextureRef, which is stored inside a ImDrawCmd.
+// - Compile-time type configuration:
+// - To use something other than a 64-bit value: add '#define ImTextureID MyTextureType*' in your imconfig.h file.
+// - This can be whatever to you want it to be! read the FAQ entry about textures for details.
+// - You may decide to store a higher-level structure containing texture, sampler, shader etc. with various
+// constructors if you like. You will need to implement ==/!= operators.
+// History:
+// - In v1.91.4 (2024/10/08): the default type for ImTextureID was changed from 'void*' to 'ImU64'. This allowed backends requirig 64-bit worth of data to build on 32-bit architectures. Use intermediary intptr_t cast and read FAQ if you have casting warnings.
+// - In v1.92.0 (2025/06/11): added ImTextureRef which carry either a ImTextureID either a pointer to internal texture atlas. All user facing functions taking ImTextureID changed to ImTextureRef
#ifndef ImTextureID
-typedef ImU64 ImTextureID; // Default: store a pointer or an integer fitting in a pointer (most renderer backends are ok with that)
+typedef ImU64 ImTextureID; // Default: store up to 64-bits (any pointer or integer). A majority of backends are ok with that.
#endif
+// Define this if you need 0 to be a valid ImTextureID for your backend.
+#ifndef ImTextureID_Invalid
+#define ImTextureID_Invalid ((ImTextureID)0)
+#endif
+
+// ImTextureRef = higher-level identifier for a texture. Store a ImTextureID _or_ a ImTextureData*.
+// The identifier is valid even before the texture has been uploaded to the GPU/graphics system.
+// This is what gets passed to functions such as `ImGui::Image()`, `ImDrawList::AddImage()`.
+// This is what gets stored in draw commands (`ImDrawCmd`) to identify a texture during rendering.
+// - When a texture is created by user code (e.g. custom images), we directly stores the low-level ImTextureID.
+// Because of this, when displaying your own texture you are likely to ever only manage ImTextureID values on your side.
+// - When a texture is created by the backend, we stores a ImTextureData* which becomes an indirection
+// to extract the ImTextureID value during rendering, after texture upload has happened.
+// - To create a ImTextureRef from a ImTextureData you can use ImTextureData::GetTexRef().
+// We intentionally do not provide an ImTextureRef constructor for this: we don't expect this
+// to be frequently useful to the end-user, and it would be erroneously called by many legacy code.
+// - If you want to bind the current atlas when using custom rectangle, you can use io.Fonts->TexRef.
+// - Binding generators for languages such as C (which don't have constructors), should provide a helper, e.g.
+// inline ImTextureRef ImTextureRefFromID(ImTextureID tex_id) { ImTextureRef tex_ref = { ._TexData = NULL, .TexID = tex_id }; return tex_ref; }
+// In 1.92 we changed most drawing functions using ImTextureID to use ImTextureRef.
+// We intentionally do not provide an implicit ImTextureRef -> ImTextureID cast operator because it is technically lossy to convert ImTextureRef to ImTextureID before rendering.
+IM_MSVC_RUNTIME_CHECKS_OFF
+struct ImTextureRef
+{
+ ImTextureRef() { _TexData = NULL; _TexID = ImTextureID_Invalid; }
+ ImTextureRef(ImTextureID tex_id) { _TexData = NULL; _TexID = tex_id; }
+#if !defined(IMGUI_DISABLE_OBSOLETE_FUNCTIONS) && !defined(ImTextureID)
+ ImTextureRef(void* tex_id) { _TexData = NULL; _TexID = (ImTextureID)(size_t)tex_id; } // For legacy backends casting to ImTextureID
+#endif
+
+ inline ImTextureID GetTexID() const; // == (_TexData ? _TexData->TexID : _TexID) // Implemented below in the file.
+
+ // Members (either are set, never both!)
+ ImTextureData* _TexData; // A texture, generally owned by a ImFontAtlas. Will convert to ImTextureID during render loop, after texture has been uploaded.
+ ImTextureID _TexID; // _OR_ Low-level backend texture identifier, if already uploaded or created by user/app. Generally provided to e.g. ImGui::Image() calls.
+};
+IM_MSVC_RUNTIME_CHECKS_RESTORE
+
//-----------------------------------------------------------------------------
// [SECTION] Dear ImGui end-user API functions
// (Note that ImGui:: being a namespace, you can add extra ImGui:: functions in your own separate file. Please don't modify imgui source files!)
@@ -341,7 +402,7 @@ namespace ImGui
IMGUI_API void NewFrame(); // start a new Dear ImGui frame, you can submit any command from this point until Render()/EndFrame().
IMGUI_API void EndFrame(); // ends the Dear ImGui frame. automatically called by Render(). If you don't need to render data (skipping rendering) you may call EndFrame() without Render()... but you'll have wasted CPU already! If you don't need to render, better to not create any windows and not call NewFrame() at all!
IMGUI_API void Render(); // ends the Dear ImGui frame, finalize the draw data. You can then get call GetDrawData().
- IMGUI_API ImDrawData* GetDrawData(); // valid after Render() and until the next call to NewFrame(). this is what you have to render.
+ IMGUI_API ImDrawData* GetDrawData(); // valid after Render() and until the next call to NewFrame(). Call ImGui_ImplXXXX_RenderDrawData() function in your Renderer Backend to render.
// Demo, Debug, Information
IMGUI_API void ShowDemoWindow(bool* p_open = NULL); // create Demo window. demonstrate most ImGui features. call this to learn about the library! try to make it always available in your application!
@@ -426,7 +487,6 @@ namespace ImGui
IMGUI_API void SetWindowSize(const ImVec2& size, ImGuiCond cond = 0); // (not recommended) set current window size - call within Begin()/End(). set to ImVec2(0, 0) to force an auto-fit. prefer using SetNextWindowSize(), as this may incur tearing and minor side-effects.
IMGUI_API void SetWindowCollapsed(bool collapsed, ImGuiCond cond = 0); // (not recommended) set current window collapsed state. prefer using SetNextWindowCollapsed().
IMGUI_API void SetWindowFocus(); // (not recommended) set current window to be focused / top-most. prefer using SetNextWindowFocus().
- IMGUI_API void SetWindowFontScale(float scale); // [OBSOLETE] set font scale. Adjust IO.FontGlobalScale if you want to scale all windows. This is an old API! For correct scaling, prefer to reload font + rebuild ImFontAtlas + call style.ScaleAllSizes().
IMGUI_API void SetWindowPos(const char* name, const ImVec2& pos, ImGuiCond cond = 0); // set named window position.
IMGUI_API void SetWindowSize(const char* name, const ImVec2& size, ImGuiCond cond = 0); // set named window size. set axis to 0.0f to force an auto-fit on this axis.
IMGUI_API void SetWindowCollapsed(const char* name, bool collapsed, ImGuiCond cond = 0); // set named window collapsed state
@@ -446,9 +506,29 @@ namespace ImGui
IMGUI_API void SetScrollFromPosX(float local_x, float center_x_ratio = 0.5f); // adjust scrolling amount to make given position visible. Generally GetCursorStartPos() + offset to compute a valid position.
IMGUI_API void SetScrollFromPosY(float local_y, float center_y_ratio = 0.5f); // adjust scrolling amount to make given position visible. Generally GetCursorStartPos() + offset to compute a valid position.
- // Parameters stacks (shared)
- IMGUI_API void PushFont(ImFont* font); // use NULL as a shortcut to push default font
+ // Parameters stacks (font)
+ // - PushFont(font, 0.0f) // Change font and keep current size
+ // - PushFont(NULL, 20.0f) // Keep font and change current size
+ // - PushFont(font, 20.0f) // Change font and set size to 20.0f
+ // - PushFont(font, style.FontSizeBase * 2.0f) // Change font and set size to be twice bigger than current size.
+ // - PushFont(font, font->LegacySize) // Change font and set size to size passed to AddFontXXX() function. Same as pre-1.92 behavior.
+ // *IMPORTANT* before 1.92, fonts had a single size. They can now be dynamically be adjusted.
+ // - In 1.92 we have REMOVED the single parameter version of PushFont() because it seems like the easiest way to provide an error-proof transition.
+ // - PushFont(font) before 1.92 = PushFont(font, font->LegacySize) after 1.92 // Use default font size as passed to AddFontXXX() function.
+ // *IMPORTANT* global scale factors are applied over the provided size.
+ // - Global scale factors are: 'style.FontScaleMain', 'style.FontScaleDpi' and maybe more.
+ // - If you want to apply a factor to the _current_ font size:
+ // - CORRECT: PushFont(NULL, style.FontSizeBase) // use current unscaled size == does nothing
+ // - CORRECT: PushFont(NULL, style.FontSizeBase * 2.0f) // use current unscaled size x2 == make text twice bigger
+ // - INCORRECT: PushFont(NULL, GetFontSize()) // INCORRECT! using size after global factors already applied == GLOBAL SCALING FACTORS WILL APPLY TWICE!
+ // - INCORRECT: PushFont(NULL, GetFontSize() * 2.0f) // INCORRECT! using size after global factors already applied == GLOBAL SCALING FACTORS WILL APPLY TWICE!
+ IMGUI_API void PushFont(ImFont* font, float font_size_base_unscaled); // Use NULL as a shortcut to keep current font. Use 0.0f to keep current size.
IMGUI_API void PopFont();
+ IMGUI_API ImFont* GetFont(); // get current font
+ IMGUI_API float GetFontSize(); // get current scaled font size (= height in pixels). AFTER global scale factors applied. *IMPORTANT* DO NOT PASS THIS VALUE TO PushFont()! Use ImGui::GetStyle().FontSizeBase to get value before global scale factors.
+ IMGUI_API ImFontBaked* GetFontBaked(); // get current font bound at current size // == GetFont()->GetFontBaked(GetFontSize())
+
+ // Parameters stacks (shared)
IMGUI_API void PushStyleColor(ImGuiCol idx, ImU32 col); // modify a style color. always use this if you modify the style after NewFrame().
IMGUI_API void PushStyleColor(ImGuiCol idx, const ImVec4& col);
IMGUI_API void PopStyleColor(int count = 1);
@@ -470,8 +550,6 @@ namespace ImGui
// Style read access
// - Use the ShowStyleEditor() function to interactively see/edit the colors.
- IMGUI_API ImFont* GetFont(); // get current font
- IMGUI_API float GetFontSize(); // get current font size (= height in pixels) of current font with current scale applied
IMGUI_API ImVec2 GetFontTexUvWhitePixel(); // get UV coordinate for a white pixel, useful to draw custom shapes via the ImDrawList API
IMGUI_API ImU32 GetColorU32(ImGuiCol idx, float alpha_mul = 1.0f); // retrieve given style color with style alpha applied and optional extra alpha multiplier, packed as a 32-bit value suitable for ImDrawList
IMGUI_API ImU32 GetColorU32(const ImVec4& col); // retrieve given color with style alpha applied, packed as a 32-bit value suitable for ImDrawList
@@ -567,16 +645,17 @@ namespace ImGui
IMGUI_API void ProgressBar(float fraction, const ImVec2& size_arg = ImVec2(-FLT_MIN, 0), const char* overlay = NULL);
IMGUI_API void Bullet(); // draw a small circle + keep the cursor on the same line. advance cursor x position by GetTreeNodeToLabelSpacing(), same distance that TreeNode() uses
IMGUI_API bool TextLink(const char* label); // hyperlink text button, return true when clicked
- IMGUI_API void TextLinkOpenURL(const char* label, const char* url = NULL); // hyperlink text button, automatically open file/url when clicked
+ IMGUI_API bool TextLinkOpenURL(const char* label, const char* url = NULL); // hyperlink text button, automatically open file/url when clicked
// Widgets: Images
- // - Read about ImTextureID here: https://github.com/ocornut/imgui/wiki/Image-Loading-and-Displaying-Examples
+ // - Read about ImTextureID/ImTextureRef here: https://github.com/ocornut/imgui/wiki/Image-Loading-and-Displaying-Examples
// - 'uv0' and 'uv1' are texture coordinates. Read about them from the same link above.
// - Image() pads adds style.ImageBorderSize on each side, ImageButton() adds style.FramePadding on each side.
// - ImageButton() draws a background based on regular Button() color + optionally an inner background if specified.
- IMGUI_API void Image(ImTextureID user_texture_id, const ImVec2& image_size, const ImVec2& uv0 = ImVec2(0, 0), const ImVec2& uv1 = ImVec2(1, 1));
- IMGUI_API void ImageWithBg(ImTextureID user_texture_id, const ImVec2& image_size, const ImVec2& uv0 = ImVec2(0, 0), const ImVec2& uv1 = ImVec2(1, 1), const ImVec4& bg_col = ImVec4(0, 0, 0, 0), const ImVec4& tint_col = ImVec4(1, 1, 1, 1));
- IMGUI_API bool ImageButton(const char* str_id, ImTextureID user_texture_id, const ImVec2& image_size, const ImVec2& uv0 = ImVec2(0, 0), const ImVec2& uv1 = ImVec2(1, 1), const ImVec4& bg_col = ImVec4(0, 0, 0, 0), const ImVec4& tint_col = ImVec4(1, 1, 1, 1));
+ // - An obsolete version of Image(), before 1.91.9 (March 2025), had a 'tint_col' parameter which is now supported by the ImageWithBg() function.
+ IMGUI_API void Image(ImTextureRef tex_ref, const ImVec2& image_size, const ImVec2& uv0 = ImVec2(0, 0), const ImVec2& uv1 = ImVec2(1, 1));
+ IMGUI_API void ImageWithBg(ImTextureRef tex_ref, const ImVec2& image_size, const ImVec2& uv0 = ImVec2(0, 0), const ImVec2& uv1 = ImVec2(1, 1), const ImVec4& bg_col = ImVec4(0, 0, 0, 0), const ImVec4& tint_col = ImVec4(1, 1, 1, 1));
+ IMGUI_API bool ImageButton(const char* str_id, ImTextureRef tex_ref, const ImVec2& image_size, const ImVec2& uv0 = ImVec2(0, 0), const ImVec2& uv1 = ImVec2(1, 1), const ImVec4& bg_col = ImVec4(0, 0, 0, 0), const ImVec4& tint_col = ImVec4(1, 1, 1, 1));
// Widgets: Combo Box (Dropdown)
// - The BeginCombo()/EndCombo() api allows you to manage your contents and selection state however you want it, by creating e.g. Selectable() items.
@@ -849,7 +928,7 @@ namespace ImGui
IMGUI_API ImGuiTableSortSpecs* TableGetSortSpecs(); // get latest sort specs for the table (NULL if not sorting). Lifetime: don't hold on this pointer over multiple frames or past any subsequent call to BeginTable().
IMGUI_API int TableGetColumnCount(); // return number of columns (value passed to BeginTable)
IMGUI_API int TableGetColumnIndex(); // return current column index.
- IMGUI_API int TableGetRowIndex(); // return current row index.
+ IMGUI_API int TableGetRowIndex(); // return current row index (header rows are accounted for)
IMGUI_API const char* TableGetColumnName(int column_n = -1); // return "" if column didn't have a name declared by TableSetupColumn(). Pass -1 to use current column.
IMGUI_API ImGuiTableColumnFlags TableGetColumnFlags(int column_n = -1); // return column flags so you can query their Enabled/Visible/Sorted/Hovered status flags. Pass -1 to use current column.
IMGUI_API void TableSetColumnEnabled(int column_n, bool v);// change user accessible enabled/disabled state of a column. Set to false to hide the column. User can use the context menu to change this themselves (right-click in headers, or right-click in columns body with ImGuiTableFlags_ContextMenuInBody)
@@ -1006,8 +1085,8 @@ namespace ImGui
// Inputs Utilities: Shortcut Testing & Routing [BETA]
// - ImGuiKeyChord = a ImGuiKey + optional ImGuiMod_Alt/ImGuiMod_Ctrl/ImGuiMod_Shift/ImGuiMod_Super.
- // ImGuiKey_C // Accepted by functions taking ImGuiKey or ImGuiKeyChord arguments)
- // ImGuiMod_Ctrl | ImGuiKey_C // Accepted by functions taking ImGuiKeyChord arguments)
+ // ImGuiKey_C // Accepted by functions taking ImGuiKey or ImGuiKeyChord arguments
+ // ImGuiMod_Ctrl | ImGuiKey_C // Accepted by functions taking ImGuiKeyChord arguments
// only ImGuiMod_XXX values are legal to combine with an ImGuiKey. You CANNOT combine two ImGuiKey values.
// - The general idea is that several callers may register interest in a shortcut, and only one owner gets it.
// Parent -> call Shortcut(Ctrl+S) // When Parent is focused, Parent gets the shortcut.
@@ -1146,7 +1225,7 @@ enum ImGuiWindowFlags_
};
// Flags for ImGui::BeginChild()
-// (Legacy: bit 0 must always correspond to ImGuiChildFlags_Borders to be backward compatible with old API using 'bool border = false'.
+// (Legacy: bit 0 must always correspond to ImGuiChildFlags_Borders to be backward compatible with old API using 'bool border = false'.)
// About using AutoResizeX/AutoResizeY flags:
// - May be combined with SetNextWindowSizeConstraints() to set a min/max size for each axis (see "Demo->Child->Auto-resize with Constraints").
// - Size measurement for a given axis is only performed when the child window is within visible boundaries, or is just appearing.
@@ -1225,6 +1304,15 @@ enum ImGuiInputTextFlags_
ImGuiInputTextFlags_CallbackResize = 1 << 22, // Callback on buffer capacity changes request (beyond 'buf_size' parameter value), allowing the string to grow. Notify when the string wants to be resized (for string types which hold a cache of their Size). You will be provided a new BufSize in the callback and NEED to honor it. (see misc/cpp/imgui_stdlib.h for an example of using this)
ImGuiInputTextFlags_CallbackEdit = 1 << 23, // Callback on any edit. Note that InputText() already returns true on edit + you can always use IsItemEdited(). The callback is useful to manipulate the underlying buffer while focus is active.
+ // Multi-line Word-Wrapping [BETA]
+ // - Not well tested yet. Please report any incorrect cursor movement, selection behavior etc. bug to https://github.com/ocornut/imgui/issues/3237.
+ // - Wrapping style is not ideal. Wrapping of long words/sections (e.g. words larger than total available width) may be particularly unpleasing.
+ // - Wrapping width needs to always account for the possibility of a vertical scrollbar.
+ // - It is much slower than regular text fields.
+ // Ballpark estimate of cost on my 2019 desktop PC: for a 100 KB text buffer: +~0.3 ms (Optimized) / +~1.0 ms (Debug build).
+ // The CPU cost is very roughly proportional to text length, so a 10 KB buffer should cost about ten times less.
+ ImGuiInputTextFlags_WordWrap = 1 << 24, // InputTextMultine(): word-wrap lines that are too long.
+
// Obsolete names
//ImGuiInputTextFlags_AlwaysInsertMode = ImGuiInputTextFlags_AlwaysOverwrite // [renamed in 1.82] name was not matching behavior
};
@@ -1250,12 +1338,19 @@ enum ImGuiTreeNodeFlags_
ImGuiTreeNodeFlags_SpanAllColumns = 1 << 14, // Frame will span all columns of its container table (label will still fit in current column)
ImGuiTreeNodeFlags_LabelSpanAllColumns = 1 << 15, // Label will span all columns of its container table
//ImGuiTreeNodeFlags_NoScrollOnOpen = 1 << 16, // FIXME: TODO: Disable automatic scroll on TreePop() if node got just open and contents is not visible
- ImGuiTreeNodeFlags_NavLeftJumpsBackHere = 1 << 17, // (WIP) Nav: left direction may move to this TreeNode() from any of its child (items submitted between TreeNode and TreePop)
+ ImGuiTreeNodeFlags_NavLeftJumpsToParent = 1 << 17, // Nav: left arrow moves back to parent. This is processed in TreePop() when there's an unfullfilled Left nav request remaining.
ImGuiTreeNodeFlags_CollapsingHeader = ImGuiTreeNodeFlags_Framed | ImGuiTreeNodeFlags_NoTreePushOnOpen | ImGuiTreeNodeFlags_NoAutoOpenOnLog,
+ // [EXPERIMENTAL] Draw lines connecting TreeNode hierarchy. Discuss in GitHub issue #2920.
+ // Default value is pulled from style.TreeLinesFlags. May be overridden in TreeNode calls.
+ ImGuiTreeNodeFlags_DrawLinesNone = 1 << 18, // No lines drawn
+ ImGuiTreeNodeFlags_DrawLinesFull = 1 << 19, // Horizontal lines to child nodes. Vertical line drawn down to TreePop() position: cover full contents. Faster (for large trees).
+ ImGuiTreeNodeFlags_DrawLinesToNodes = 1 << 20, // Horizontal lines to child nodes. Vertical line drawn down to bottom-most child node. Slower (for large trees).
+
#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
- ImGuiTreeNodeFlags_AllowItemOverlap = ImGuiTreeNodeFlags_AllowOverlap, // Renamed in 1.89.7
- ImGuiTreeNodeFlags_SpanTextWidth = ImGuiTreeNodeFlags_SpanLabelWidth,// Renamed in 1.90.7
+ ImGuiTreeNodeFlags_NavLeftJumpsBackHere = ImGuiTreeNodeFlags_NavLeftJumpsToParent, // Renamed in 1.92.0
+ ImGuiTreeNodeFlags_SpanTextWidth = ImGuiTreeNodeFlags_SpanLabelWidth, // Renamed in 1.90.7
+ ImGuiTreeNodeFlags_AllowItemOverlap = ImGuiTreeNodeFlags_AllowOverlap, // Renamed in 1.89.7
#endif
};
@@ -1294,6 +1389,7 @@ enum ImGuiSelectableFlags_
ImGuiSelectableFlags_Disabled = 1 << 3, // Cannot be selected, display grayed out text
ImGuiSelectableFlags_AllowOverlap = 1 << 4, // (WIP) Hit testing to allow subsequent widgets to overlap this one
ImGuiSelectableFlags_Highlight = 1 << 5, // Make the item be displayed as if it is hovered
+ ImGuiSelectableFlags_SelectOnNav = 1 << 6, // Auto-select when moved into, unless Ctrl is held. Automatic when in a BeginMultiSelect() block.
#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
ImGuiSelectableFlags_DontClosePopups = ImGuiSelectableFlags_NoAutoClosePopups, // Renamed in 1.91.0
@@ -1327,10 +1423,17 @@ enum ImGuiTabBarFlags_
ImGuiTabBarFlags_NoTabListScrollingButtons = 1 << 4, // Disable scrolling buttons (apply when fitting policy is ImGuiTabBarFlags_FittingPolicyScroll)
ImGuiTabBarFlags_NoTooltip = 1 << 5, // Disable tooltips when hovering a tab
ImGuiTabBarFlags_DrawSelectedOverline = 1 << 6, // Draw selected overline markers over selected tab
- ImGuiTabBarFlags_FittingPolicyResizeDown = 1 << 7, // Resize tabs when they don't fit
- ImGuiTabBarFlags_FittingPolicyScroll = 1 << 8, // Add scroll buttons when tabs don't fit
- ImGuiTabBarFlags_FittingPolicyMask_ = ImGuiTabBarFlags_FittingPolicyResizeDown | ImGuiTabBarFlags_FittingPolicyScroll,
- ImGuiTabBarFlags_FittingPolicyDefault_ = ImGuiTabBarFlags_FittingPolicyResizeDown,
+
+ // Fitting/Resize policy
+ ImGuiTabBarFlags_FittingPolicyMixed = 1 << 7, // Shrink down tabs when they don't fit, until width is style.TabMinWidthShrink, then enable scrolling buttons.
+ ImGuiTabBarFlags_FittingPolicyShrink = 1 << 8, // Shrink down tabs when they don't fit
+ ImGuiTabBarFlags_FittingPolicyScroll = 1 << 9, // Enable scrolling buttons when tabs don't fit
+ ImGuiTabBarFlags_FittingPolicyMask_ = ImGuiTabBarFlags_FittingPolicyMixed | ImGuiTabBarFlags_FittingPolicyShrink | ImGuiTabBarFlags_FittingPolicyScroll,
+ ImGuiTabBarFlags_FittingPolicyDefault_ = ImGuiTabBarFlags_FittingPolicyMixed,
+
+#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
+ ImGuiTabBarFlags_FittingPolicyResizeDown = ImGuiTabBarFlags_FittingPolicyShrink, // Renamed in 1.92.2
+#endif
};
// Flags for ImGui::BeginTabItem()
@@ -1513,7 +1616,7 @@ enum ImGuiKey : int
ImGuiKey_Space,
ImGuiKey_Enter,
ImGuiKey_Escape,
- ImGuiKey_LeftCtrl, ImGuiKey_LeftShift, ImGuiKey_LeftAlt, ImGuiKey_LeftSuper,
+ ImGuiKey_LeftCtrl, ImGuiKey_LeftShift, ImGuiKey_LeftAlt, ImGuiKey_LeftSuper, // Also see ImGuiMod_Ctrl, ImGuiMod_Shift, ImGuiMod_Alt, ImGuiMod_Super below!
ImGuiKey_RightCtrl, ImGuiKey_RightShift, ImGuiKey_RightAlt, ImGuiKey_RightSuper,
ImGuiKey_Menu,
ImGuiKey_0, ImGuiKey_1, ImGuiKey_2, ImGuiKey_3, ImGuiKey_4, ImGuiKey_5, ImGuiKey_6, ImGuiKey_7, ImGuiKey_8, ImGuiKey_9,
@@ -1553,32 +1656,34 @@ enum ImGuiKey : int
ImGuiKey_AppForward,
ImGuiKey_Oem102, // Non-US backslash.
- // Gamepad (some of those are analog values, 0.0f to 1.0f) // NAVIGATION ACTION
+ // Gamepad
+ // (analog values are 0.0f to 1.0f)
// (download controller mapping PNG/PSD at http://dearimgui.com/controls_sheets)
- ImGuiKey_GamepadStart, // Menu (Xbox) + (Switch) Start/Options (PS)
- ImGuiKey_GamepadBack, // View (Xbox) - (Switch) Share (PS)
- ImGuiKey_GamepadFaceLeft, // X (Xbox) Y (Switch) Square (PS) // Tap: Toggle Menu. Hold: Windowing mode (Focus/Move/Resize windows)
- ImGuiKey_GamepadFaceRight, // B (Xbox) A (Switch) Circle (PS) // Cancel / Close / Exit
- ImGuiKey_GamepadFaceUp, // Y (Xbox) X (Switch) Triangle (PS) // Text Input / On-screen Keyboard
- ImGuiKey_GamepadFaceDown, // A (Xbox) B (Switch) Cross (PS) // Activate / Open / Toggle / Tweak
- ImGuiKey_GamepadDpadLeft, // D-pad Left // Move / Tweak / Resize Window (in Windowing mode)
- ImGuiKey_GamepadDpadRight, // D-pad Right // Move / Tweak / Resize Window (in Windowing mode)
- ImGuiKey_GamepadDpadUp, // D-pad Up // Move / Tweak / Resize Window (in Windowing mode)
- ImGuiKey_GamepadDpadDown, // D-pad Down // Move / Tweak / Resize Window (in Windowing mode)
- ImGuiKey_GamepadL1, // L Bumper (Xbox) L (Switch) L1 (PS) // Tweak Slower / Focus Previous (in Windowing mode)
- ImGuiKey_GamepadR1, // R Bumper (Xbox) R (Switch) R1 (PS) // Tweak Faster / Focus Next (in Windowing mode)
- ImGuiKey_GamepadL2, // L Trig. (Xbox) ZL (Switch) L2 (PS) [Analog]
- ImGuiKey_GamepadR2, // R Trig. (Xbox) ZR (Switch) R2 (PS) [Analog]
- ImGuiKey_GamepadL3, // L Stick (Xbox) L3 (Switch) L3 (PS)
- ImGuiKey_GamepadR3, // R Stick (Xbox) R3 (Switch) R3 (PS)
- ImGuiKey_GamepadLStickLeft, // [Analog] // Move Window (in Windowing mode)
- ImGuiKey_GamepadLStickRight, // [Analog] // Move Window (in Windowing mode)
- ImGuiKey_GamepadLStickUp, // [Analog] // Move Window (in Windowing mode)
- ImGuiKey_GamepadLStickDown, // [Analog] // Move Window (in Windowing mode)
- ImGuiKey_GamepadRStickLeft, // [Analog]
- ImGuiKey_GamepadRStickRight, // [Analog]
- ImGuiKey_GamepadRStickUp, // [Analog]
- ImGuiKey_GamepadRStickDown, // [Analog]
+ // // XBOX | SWITCH | PLAYSTA. | -> ACTION
+ ImGuiKey_GamepadStart, // Menu | + | Options |
+ ImGuiKey_GamepadBack, // View | - | Share |
+ ImGuiKey_GamepadFaceLeft, // X | Y | Square | Tap: Toggle Menu. Hold: Windowing mode (Focus/Move/Resize windows)
+ ImGuiKey_GamepadFaceRight, // B | A | Circle | Cancel / Close / Exit
+ ImGuiKey_GamepadFaceUp, // Y | X | Triangle | Text Input / On-screen Keyboard
+ ImGuiKey_GamepadFaceDown, // A | B | Cross | Activate / Open / Toggle / Tweak
+ ImGuiKey_GamepadDpadLeft, // D-pad Left | " | " | Move / Tweak / Resize Window (in Windowing mode)
+ ImGuiKey_GamepadDpadRight, // D-pad Right | " | " | Move / Tweak / Resize Window (in Windowing mode)
+ ImGuiKey_GamepadDpadUp, // D-pad Up | " | " | Move / Tweak / Resize Window (in Windowing mode)
+ ImGuiKey_GamepadDpadDown, // D-pad Down | " | " | Move / Tweak / Resize Window (in Windowing mode)
+ ImGuiKey_GamepadL1, // L Bumper | L | L1 | Tweak Slower / Focus Previous (in Windowing mode)
+ ImGuiKey_GamepadR1, // R Bumper | R | R1 | Tweak Faster / Focus Next (in Windowing mode)
+ ImGuiKey_GamepadL2, // L Trigger | ZL | L2 | [Analog]
+ ImGuiKey_GamepadR2, // R Trigger | ZR | R2 | [Analog]
+ ImGuiKey_GamepadL3, // L Stick | L3 | L3 |
+ ImGuiKey_GamepadR3, // R Stick | R3 | R3 |
+ ImGuiKey_GamepadLStickLeft, // | | | [Analog] Move Window (in Windowing mode)
+ ImGuiKey_GamepadLStickRight, // | | | [Analog] Move Window (in Windowing mode)
+ ImGuiKey_GamepadLStickUp, // | | | [Analog] Move Window (in Windowing mode)
+ ImGuiKey_GamepadLStickDown, // | | | [Analog] Move Window (in Windowing mode)
+ ImGuiKey_GamepadRStickLeft, // | | | [Analog]
+ ImGuiKey_GamepadRStickRight, // | | | [Analog]
+ ImGuiKey_GamepadRStickUp, // | | | [Analog]
+ ImGuiKey_GamepadRStickDown, // | | | [Analog]
// Aliases: Mouse Buttons (auto-submitted from AddMouseButtonEvent() calls)
// - This is mirroring the data also written to io.MouseDown[], io.MouseWheel, in a format allowing them to be accessed via standard key API.
@@ -1586,11 +1691,15 @@ enum ImGuiKey : int
// [Internal] Reserved for mod storage
ImGuiKey_ReservedForModCtrl, ImGuiKey_ReservedForModShift, ImGuiKey_ReservedForModAlt, ImGuiKey_ReservedForModSuper,
+
+ // [Internal] If you need to iterate all keys (for e.g. an input mapper) you may use ImGuiKey_NamedKey_BEGIN..ImGuiKey_NamedKey_END.
ImGuiKey_NamedKey_END,
+ ImGuiKey_NamedKey_COUNT = ImGuiKey_NamedKey_END - ImGuiKey_NamedKey_BEGIN,
// Keyboard Modifiers (explicitly submitted by backend via AddKeyEvent() calls)
- // - This is mirroring the data also written to io.KeyCtrl, io.KeyShift, io.KeyAlt, io.KeySuper, in a format allowing
- // them to be accessed via standard key API, allowing calls such as IsKeyPressed(), IsKeyReleased(), querying duration etc.
+ // - Any functions taking a ImGuiKeyChord parameter can binary-or those with regular keys, e.g. Shortcut(ImGuiMod_Ctrl | ImGuiKey_S).
+ // - Those are written back into io.KeyCtrl, io.KeyShift, io.KeyAlt, io.KeySuper for convenience,
+ // but may be accessed via standard key API such as IsKeyPressed(), IsKeyReleased(), querying duration etc.
// - Code polling every key (e.g. an interface to detect a key press for input mapping) might want to ignore those
// and prefer using the real keys (e.g. ImGuiKey_LeftCtrl, ImGuiKey_RightCtrl instead of ImGuiMod_Ctrl).
// - In theory the value of keyboard modifiers should be roughly equivalent to a logical or of the equivalent left/right keys.
@@ -1604,11 +1713,6 @@ enum ImGuiKey : int
ImGuiMod_Super = 1 << 15, // Windows/Super (non-macOS), Ctrl (macOS)
ImGuiMod_Mask_ = 0xF000, // 4-bits
- // [Internal] If you need to iterate all keys (for e.g. an input mapper) you may use ImGuiKey_NamedKey_BEGIN..ImGuiKey_NamedKey_END.
- ImGuiKey_NamedKey_COUNT = ImGuiKey_NamedKey_END - ImGuiKey_NamedKey_BEGIN,
- //ImGuiKey_KeysData_SIZE = ImGuiKey_NamedKey_COUNT, // Size of KeysData[]: only hold named keys
- //ImGuiKey_KeysData_OFFSET = ImGuiKey_NamedKey_BEGIN, // Accesses to io.KeysData[] must use (key - ImGuiKey_NamedKey_BEGIN) index.
-
#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
ImGuiKey_COUNT = ImGuiKey_NamedKey_END, // Obsoleted in 1.91.5 because it was extremely misleading (since named keys don't start at 0 anymore)
ImGuiMod_Shortcut = ImGuiMod_Ctrl, // Removed in 1.90.7, you can now simply use ImGuiMod_Ctrl
@@ -1658,8 +1762,6 @@ enum ImGuiConfigFlags_
// [BETA] Viewports
// When using viewports it is recommended that your default value for ImGuiCol_WindowBg is opaque (Alpha=1.0) so transition to a viewport won't be noticeable.
ImGuiConfigFlags_ViewportsEnable = 1 << 10, // Viewport enable flags (require both ImGuiBackendFlags_PlatformHasViewports + ImGuiBackendFlags_RendererHasViewports set by the respective backends)
- ImGuiConfigFlags_DpiEnableScaleViewports= 1 << 14, // [BETA: Don't use] FIXME-DPI: Reposition and resize imgui windows when the DpiScale of a viewport changed (mostly useful for the main viewport hosting other window). Note that resizing the main window itself is up to your application.
- ImGuiConfigFlags_DpiEnableScaleFonts = 1 << 15, // [BETA: Don't use] FIXME-DPI: Request bitmap-scaled fonts to match DpiScale. This is a very low-quality workaround. The correct way to handle DPI is _currently_ to replace the atlas and/or fonts in the Platform_OnChangedViewport callback, but this is all early work in progress.
// User storage (to allow your backend/engine to communicate to code that may be shared between multiple projects. Those flags are NOT used by core Dear ImGui)
ImGuiConfigFlags_IsSRGB = 1 << 20, // Application is SRGB-aware.
@@ -1668,6 +1770,8 @@ enum ImGuiConfigFlags_
#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
ImGuiConfigFlags_NavEnableSetMousePos = 1 << 2, // [moved/renamed in 1.91.4] -> use bool io.ConfigNavMoveSetMousePos
ImGuiConfigFlags_NavNoCaptureKeyboard = 1 << 3, // [moved/renamed in 1.91.4] -> use bool io.ConfigNavCaptureKeyboard
+ ImGuiConfigFlags_DpiEnableScaleFonts = 1 << 14, // [moved/renamed in 1.92.0] -> use bool io.ConfigDpiScaleFonts
+ ImGuiConfigFlags_DpiEnableScaleViewports= 1 << 15, // [moved/renamed in 1.92.0] -> use bool io.ConfigDpiScaleViewports
#endif
};
@@ -1679,11 +1783,13 @@ enum ImGuiBackendFlags_
ImGuiBackendFlags_HasMouseCursors = 1 << 1, // Backend Platform supports honoring GetMouseCursor() value to change the OS cursor shape.
ImGuiBackendFlags_HasSetMousePos = 1 << 2, // Backend Platform supports io.WantSetMousePos requests to reposition the OS mouse position (only used if io.ConfigNavMoveSetMousePos is set).
ImGuiBackendFlags_RendererHasVtxOffset = 1 << 3, // Backend Renderer supports ImDrawCmd::VtxOffset. This enables output of large meshes (64K+ vertices) while still using 16-bit indices.
+ ImGuiBackendFlags_RendererHasTextures = 1 << 4, // Backend Renderer supports ImTextureData requests to create/update/destroy textures. This enables incremental texture updates and texture reloads. See https://github.com/ocornut/imgui/blob/master/docs/BACKENDS.md for instructions on how to upgrade your custom backend.
- // [BETA] Viewports
- ImGuiBackendFlags_PlatformHasViewports = 1 << 10, // Backend Platform supports multiple viewports.
- ImGuiBackendFlags_HasMouseHoveredViewport=1 << 11, // Backend Platform supports calling io.AddMouseViewportEvent() with the viewport under the mouse. IF POSSIBLE, ignore viewports with the ImGuiViewportFlags_NoInputs flag (Win32 backend, GLFW 3.30+ backend can do this, SDL backend cannot). If this cannot be done, Dear ImGui needs to use a flawed heuristic to find the viewport under.
- ImGuiBackendFlags_RendererHasViewports = 1 << 12, // Backend Renderer supports multiple viewports.
+ // [BETA] Multi-Viewports
+ ImGuiBackendFlags_RendererHasViewports = 1 << 10, // Backend Renderer supports multiple viewports.
+ ImGuiBackendFlags_PlatformHasViewports = 1 << 11, // Backend Platform supports multiple viewports.
+ ImGuiBackendFlags_HasMouseHoveredViewport=1 << 12, // Backend Platform supports calling io.AddMouseViewportEvent() with the viewport under the mouse. IF POSSIBLE, ignore viewports with the ImGuiViewportFlags_NoInputs flag (Win32 backend, GLFW 3.30+ backend can do this, SDL backend cannot). If this cannot be done, Dear ImGui needs to use a flawed heuristic to find the viewport under.
+ ImGuiBackendFlags_HasParentViewport = 1 << 13, // Backend Platform supports honoring viewport->ParentViewport/ParentViewportId value, by applying the corresponding parent/child relation at the Platform level.
};
// Enumeration for PushStyleColor() / PopStyleColor()
@@ -1742,7 +1848,8 @@ enum ImGuiCol_
ImGuiCol_TableRowBg, // Table row background (even rows)
ImGuiCol_TableRowBgAlt, // Table row background (odd rows)
ImGuiCol_TextLink, // Hyperlink color
- ImGuiCol_TextSelectedBg,
+ ImGuiCol_TextSelectedBg, // Selected text inside an InputText
+ ImGuiCol_TreeLines, // Tree node hierarchy outlines when using ImGuiTreeNodeFlags_DrawLines
ImGuiCol_DragDropTarget, // Rectangle highlighting a drop target
ImGuiCol_NavCursor, // Color of keyboard/gamepad navigation cursor/rectangle, when visible
ImGuiCol_NavWindowingHighlight, // Highlight window when using CTRL+TAB
@@ -1789,15 +1896,20 @@ enum ImGuiStyleVar_
ImGuiStyleVar_CellPadding, // ImVec2 CellPadding
ImGuiStyleVar_ScrollbarSize, // float ScrollbarSize
ImGuiStyleVar_ScrollbarRounding, // float ScrollbarRounding
+ ImGuiStyleVar_ScrollbarPadding, // float ScrollbarPadding
ImGuiStyleVar_GrabMinSize, // float GrabMinSize
ImGuiStyleVar_GrabRounding, // float GrabRounding
ImGuiStyleVar_ImageBorderSize, // float ImageBorderSize
ImGuiStyleVar_TabRounding, // float TabRounding
ImGuiStyleVar_TabBorderSize, // float TabBorderSize
+ ImGuiStyleVar_TabMinWidthBase, // float TabMinWidthBase
+ ImGuiStyleVar_TabMinWidthShrink, // float TabMinWidthShrink
ImGuiStyleVar_TabBarBorderSize, // float TabBarBorderSize
ImGuiStyleVar_TabBarOverlineSize, // float TabBarOverlineSize
ImGuiStyleVar_TableAngledHeadersAngle, // float TableAngledHeadersAngle
ImGuiStyleVar_TableAngledHeadersTextAlign,// ImVec2 TableAngledHeadersTextAlign
+ ImGuiStyleVar_TreeLinesSize, // float TreeLinesSize
+ ImGuiStyleVar_TreeLinesRounding, // float TreeLinesRounding
ImGuiStyleVar_ButtonTextAlign, // ImVec2 ButtonTextAlign
ImGuiStyleVar_SelectableTextAlign, // ImVec2 SelectableTextAlign
ImGuiStyleVar_SeparatorTextBorderSize, // float SeparatorTextBorderSize
@@ -2162,7 +2274,7 @@ struct ImVector
// Constructors, destructor
inline ImVector() { Size = Capacity = 0; Data = NULL; }
inline ImVector(const ImVector& src) { Size = Capacity = 0; Data = NULL; operator=(src); }
- inline ImVector& operator=(const ImVector& src) { clear(); resize(src.Size); if (src.Data) memcpy(Data, src.Data, (size_t)Size * sizeof(T)); return *this; }
+ inline ImVector& operator=(const ImVector& src) { clear(); resize(src.Size); if (Data && src.Data) memcpy(Data, src.Data, (size_t)Size * sizeof(T)); return *this; }
inline ~ImVector() { if (Data) IM_FREE(Data); } // Important: does not destruct anything
inline void clear() { if (Data) { Size = Capacity = 0; IM_FREE(Data); Data = NULL; } } // Important: does not destruct anything
@@ -2222,6 +2334,12 @@ IM_MSVC_RUNTIME_CHECKS_RESTORE
struct ImGuiStyle
{
+ // Font scaling
+ // - recap: ImGui::GetFontSize() == FontSizeBase * (FontScaleMain * FontScaleDpi * other_scaling_factors)
+ float FontSizeBase; // Current base font size before external global factors are applied. Use PushFont(NULL, size) to modify. Use ImGui::GetFontSize() to obtain scaled value.
+ float FontScaleMain; // Main global scale factor. May be set by application once, or exposed to end-user.
+ float FontScaleDpi; // Additional global scale factor from viewport/monitor contents scale. When io.ConfigDpiScaleFonts is enabled, this is automatically overwritten when changing monitor DPI.
+
float Alpha; // Global alpha applies to everything in Dear ImGui.
float DisabledAlpha; // Additional alpha multiplier applied by BeginDisabled(). Multiply over current value of Alpha.
ImVec2 WindowPadding; // Padding within a window.
@@ -2246,18 +2364,24 @@ struct ImGuiStyle
float ColumnsMinSpacing; // Minimum horizontal spacing between two columns. Preferably > (FramePadding.x + 1).
float ScrollbarSize; // Width of the vertical scrollbar, Height of the horizontal scrollbar.
float ScrollbarRounding; // Radius of grab corners for scrollbar.
+ float ScrollbarPadding; // Padding of scrollbar grab within its frame (same for both axises).
float GrabMinSize; // Minimum width/height of a grab box for slider/scrollbar.
float GrabRounding; // Radius of grabs corners rounding. Set to 0.0f to have rectangular slider grabs.
float LogSliderDeadzone; // The size in pixels of the dead-zone around zero on logarithmic sliders that cross zero.
float ImageBorderSize; // Thickness of border around Image() calls.
float TabRounding; // Radius of upper corners of a tab. Set to 0.0f to have rectangular tabs.
float TabBorderSize; // Thickness of border around tabs.
+ float TabMinWidthBase; // Minimum tab width, to make tabs larger than their contents. TabBar buttons are not affected.
+ float TabMinWidthShrink; // Minimum tab width after shrinking, when using ImGuiTabBarFlags_FittingPolicyMixed policy.
float TabCloseButtonMinWidthSelected; // -1: always visible. 0.0f: visible when hovered. >0.0f: visible when hovered if minimum width.
float TabCloseButtonMinWidthUnselected; // -1: always visible. 0.0f: visible when hovered. >0.0f: visible when hovered if minimum width. FLT_MAX: never show close button when unselected.
float TabBarBorderSize; // Thickness of tab-bar separator, which takes on the tab active color to denote focus.
float TabBarOverlineSize; // Thickness of tab-bar overline, which highlights the selected tab-bar.
float TableAngledHeadersAngle; // Angle of angled headers (supported values range from -50.0f degrees to +50.0f degrees).
ImVec2 TableAngledHeadersTextAlign;// Alignment of angled headers within the cell
+ ImGuiTreeNodeFlags TreeLinesFlags; // Default way to draw lines connecting TreeNode hierarchy. ImGuiTreeNodeFlags_DrawLinesNone or ImGuiTreeNodeFlags_DrawLinesFull or ImGuiTreeNodeFlags_DrawLinesToNodes.
+ float TreeLinesSize; // Thickness of outlines when using ImGuiTreeNodeFlags_DrawLines.
+ float TreeLinesRounding; // Radius of lines connecting child nodes to the vertical line.
ImGuiDir ColorButtonPosition; // Side of the color button in the ColorEdit4 widget (left/right). Defaults to ImGuiDir_Right.
ImVec2 ButtonTextAlign; // Alignment of button text when button is larger than text. Defaults to (0.5f, 0.5f) (centered).
ImVec2 SelectableTextAlign; // Alignment of selectable text. Defaults to (0.0f, 0.0f) (top-left aligned). It's generally important to keep this left-aligned if you want to lay multiple items on a same line.
@@ -2266,6 +2390,7 @@ struct ImGuiStyle
ImVec2 SeparatorTextPadding; // Horizontal offset of text from each edge of the separator + spacing on other axis. Generally small values. .y is recommended to be == FramePadding.y.
ImVec2 DisplayWindowPadding; // Apply to regular windows: amount which we enforce to keep visible when moving near edges of your screen.
ImVec2 DisplaySafeAreaPadding; // Apply to every windows, menus, popups, tooltips: amount where we avoid displaying contents. Adjust if you cannot see the edges of your screen (e.g. on a TV where scaling has not been configured).
+ bool DockingNodeHasCloseButton; // Docking node has their own CloseButton() to close all docked windows.
float DockingSeparatorSize; // Thickness of resizing border between docked windows
float MouseCursorScale; // Scale software rendered mouse cursor (when io.MouseDrawCursor is enabled). We apply per-monitor DPI scaling over this scale. May be removed later.
bool AntiAliasedLines; // Enable anti-aliased lines/borders. Disable if you are really tight on CPU/GPU. Latched at the beginning of the frame (copied to ImDrawList).
@@ -2285,8 +2410,13 @@ struct ImGuiStyle
ImGuiHoveredFlags HoverFlagsForTooltipMouse;// Default flags when using IsItemHovered(ImGuiHoveredFlags_ForTooltip) or BeginItemTooltip()/SetItemTooltip() while using mouse.
ImGuiHoveredFlags HoverFlagsForTooltipNav; // Default flags when using IsItemHovered(ImGuiHoveredFlags_ForTooltip) or BeginItemTooltip()/SetItemTooltip() while using keyboard/gamepad.
- IMGUI_API ImGuiStyle();
- IMGUI_API void ScaleAllSizes(float scale_factor);
+ // [Internal]
+ float _MainScale; // FIXME-WIP: Reference scale, as applied by ScaleAllSizes().
+ float _NextFrameFontSizeBase; // FIXME: Temporary hack until we finish remaining work.
+
+ // Functions
+ IMGUI_API ImGuiStyle();
+ IMGUI_API void ScaleAllSizes(float scale_factor); // Scale all spacing/padding/thickness values. Do not scale fonts.
// Obsolete names
#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
@@ -2324,7 +2454,8 @@ struct ImGuiIO
ImGuiConfigFlags ConfigFlags; // = 0 // See ImGuiConfigFlags_ enum. Set by user/application. Keyboard/Gamepad navigation options, etc.
ImGuiBackendFlags BackendFlags; // = 0 // See ImGuiBackendFlags_ enum. Set by backend (imgui_impl_xxx files or custom backend) to communicate features supported by the backend.
- ImVec2 DisplaySize; // // Main display size, in pixels (generally == GetMainViewport()->Size). May change every frame.
+ ImVec2 DisplaySize; // // Main display size, in pixels (== GetMainViewport()->Size). May change every frame.
+ ImVec2 DisplayFramebufferScale; // = (1, 1) // Main display density. For retina display where window coordinates are different from framebuffer coordinates. This will affect font density + will end up in ImDrawData::FramebufferScale.
float DeltaTime; // = 1.0f/60.0f // Time elapsed since last frame, in seconds. May change every frame.
float IniSavingRate; // = 5.0f // Minimum time between saving positions/sizes to .ini file, in seconds.
const char* IniFilename; // = "imgui.ini" // Path to .ini file (important: default "imgui.ini" is relative to current working dir!). Set NULL to disable automatic .ini loading/saving or if you want to manually call LoadIniSettingsXXX() / SaveIniSettingsXXX() functions.
@@ -2333,10 +2464,8 @@ struct ImGuiIO
// Font system
ImFontAtlas*Fonts; // // Font atlas: load, rasterize and pack one or more fonts into a single texture.
- float FontGlobalScale; // = 1.0f // Global scale all fonts
- bool FontAllowUserScaling; // = false // [OBSOLETE] Allow user scaling text of individual window with CTRL+Wheel.
ImFont* FontDefault; // = NULL // Font to use on NewFrame(). Use NULL to uses Fonts->Fonts[0].
- ImVec2 DisplayFramebufferScale; // = (1, 1) // For retina display or other situations where window coordinates are different from framebuffer coordinates. This generally ends up in ImDrawData::FramebufferScale.
+ bool FontAllowUserScaling; // = false // [OBSOLETE] Allow user scaling text of individual window with CTRL+Wheel.
// Keyboard/Gamepad Navigation options
bool ConfigNavSwapGamepadButtons; // = false // Swap Activate<>Cancel (A<>B) buttons, matching typical "Nintendo/Japanese style" gamepad layout.
@@ -2357,7 +2486,13 @@ struct ImGuiIO
bool ConfigViewportsNoAutoMerge; // = false; // Set to make all floating imgui windows always create their own viewport. Otherwise, they are merged into the main host viewports when overlapping it. May also set ImGuiViewportFlags_NoAutoMerge on individual viewport.
bool ConfigViewportsNoTaskBarIcon; // = false // Disable default OS task bar icon flag for secondary viewports. When a viewport doesn't want a task bar icon, ImGuiViewportFlags_NoTaskBarIcon will be set on it.
bool ConfigViewportsNoDecoration; // = true // Disable default OS window decoration flag for secondary viewports. When a viewport doesn't want window decorations, ImGuiViewportFlags_NoDecoration will be set on it. Enabling decoration can create subsequent issues at OS levels (e.g. minimum window size).
- bool ConfigViewportsNoDefaultParent; // = false // Disable default OS parenting to main viewport for secondary viewports. By default, viewports are marked with ParentViewportId = , expecting the platform backend to setup a parent/child relationship between the OS windows (some backend may ignore this). Set to true if you want the default to be 0, then all viewports will be top-level OS windows.
+ bool ConfigViewportsNoDefaultParent; // = true // When false: set secondary viewports' ParentViewportId to main viewport ID by default. Expects the platform backend to setup a parent/child relationship between the OS windows based on this value. Some backend may ignore this. Set to true if you want viewports to automatically be parent of main viewport, otherwise all viewports will be top-level OS windows.
+ bool ConfigViewportsPlatformFocusSetsImGuiFocus;//= true // When a platform window is focused (e.g. using Alt+Tab, clicking Platform Title Bar), apply corresponding focus on imgui windows (may clear focus/active id from imgui windows location in other platform windows). In principle this is better enabled but we provide an opt-out, because some Linux window managers tend to eagerly focus windows (e.g. on mouse hover, or even a simple window pos/size change).
+
+ // DPI/Scaling options
+ // This may keep evolving during 1.92.x releases. Expect some turbulence.
+ bool ConfigDpiScaleFonts; // = false // [EXPERIMENTAL] Automatically overwrite style.FontScaleDpi when Monitor DPI changes. This will scale fonts but _NOT_ scale sizes/padding for now.
+ bool ConfigDpiScaleViewports; // = false // [EXPERIMENTAL] Scale Dear ImGui and Platform Windows when Monitor DPI changes.
// Miscellaneous options
// (you can visualize and interact with all options in 'Demo->Configuration')
@@ -2407,7 +2542,7 @@ struct ImGuiIO
// Option to enable various debug tools showing buttons that will call the IM_DEBUG_BREAK() macro.
// - The Item Picker tool will be available regardless of this being enabled, in order to maximize its discoverability.
// - Requires a debugger being attached, otherwise IM_DEBUG_BREAK() options will appear to crash your application.
- // e.g. io.ConfigDebugIsDebuggerPresent = ::IsDebuggerPresent() on Win32, or refer to ImOsIsDebuggerPresent() imgui_test_engine/imgui_te_utils.cpp for a Unix compatible version).
+ // e.g. io.ConfigDebugIsDebuggerPresent = ::IsDebuggerPresent() on Win32, or refer to ImOsIsDebuggerPresent() imgui_test_engine/imgui_te_utils.cpp for a Unix compatible version.
bool ConfigDebugIsDebuggerPresent; // = false // Enable various tools calling IM_DEBUG_BREAK().
// Tools to detect code submitting items with conflicting/duplicate IDs
@@ -2506,14 +2641,14 @@ struct ImGuiIO
float MouseWheelH; // Mouse wheel Horizontal. >0 scrolls Left, <0 scrolls Right. Most users don't have a mouse with a horizontal wheel, may not be filled by all backends.
ImGuiMouseSource MouseSource; // Mouse actual input peripheral (Mouse/TouchScreen/Pen).
ImGuiID MouseHoveredViewport; // (Optional) Modify using io.AddMouseViewportEvent(). With multi-viewports: viewport the OS mouse is hovering. If possible _IGNORING_ viewports with the ImGuiViewportFlags_NoInputs flag is much better (few backends can handle that). Set io.BackendFlags |= ImGuiBackendFlags_HasMouseHoveredViewport if you can provide this info. If you don't imgui will infer the value using the rectangles and last focused time of the viewports it knows about (ignoring other OS windows).
- bool KeyCtrl; // Keyboard modifier down: Control
+ bool KeyCtrl; // Keyboard modifier down: Ctrl (non-macOS), Cmd (macOS)
bool KeyShift; // Keyboard modifier down: Shift
bool KeyAlt; // Keyboard modifier down: Alt
- bool KeySuper; // Keyboard modifier down: Cmd/Super/Windows
+ bool KeySuper; // Keyboard modifier down: Windows/Super (non-macOS), Ctrl (macOS)
// Other state maintained from data above + IO function calls
- ImGuiKeyChord KeyMods; // Key mods flags (any of ImGuiMod_Ctrl/ImGuiMod_Shift/ImGuiMod_Alt/ImGuiMod_Super flags, same as io.KeyCtrl/KeyShift/KeyAlt/KeySuper but merged into flags. Read-only, updated by NewFrame()
- ImGuiKeyData KeysData[ImGuiKey_NamedKey_COUNT];// Key state for all known keys. Use IsKeyXXX() functions to access this.
+ ImGuiKeyChord KeyMods; // Key mods flags (any of ImGuiMod_Ctrl/ImGuiMod_Shift/ImGuiMod_Alt/ImGuiMod_Super flags, same as io.KeyCtrl/KeyShift/KeyAlt/KeySuper but merged into flags). Read-only, updated by NewFrame()
+ ImGuiKeyData KeysData[ImGuiKey_NamedKey_COUNT];// Key state for all known keys. MUST use 'key - ImGuiKey_NamedKey_BEGIN' as index. Use IsKeyXXX() functions to access this.
bool WantCaptureMouseUnlessPopupClose; // Alternative to WantCaptureMouse: (WantCaptureMouse == true && WantCaptureMouseUnlessPopupClose == false) when a click over void is expected to close a popup.
ImVec2 MousePosPrev; // Previous mouse position (note that MouseDelta is not necessary == MousePos-MousePosPrev, in case either position is invalid)
ImVec2 MouseClickedPos[5]; // Position at time of clicking
@@ -2548,9 +2683,11 @@ struct ImGuiIO
//float NavInputs[ImGuiNavInput_COUNT]; // [LEGACY] Since 1.88, NavInputs[] was removed. Backends from 1.60 to 1.86 won't build. Feed gamepad inputs via io.AddKeyEvent() and ImGuiKey_GamepadXXX enums.
//void* ImeWindowHandle; // [Obsoleted in 1.87] Set ImGuiViewport::PlatformHandleRaw instead. Set this to your HWND to get automatic IME cursor positioning.
+#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
+ float FontGlobalScale; // Moved io.FontGlobalScale to style.FontScaleMain in 1.92 (June 2025)
+
// Legacy: before 1.91.1, clipboard functions were stored in ImGuiIO instead of ImGuiPlatformIO.
// As this is will affect all users of custom engines/backends, we are providing proper legacy redirection (will obsolete).
-#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
const char* (*GetClipboardTextFn)(void* user_data);
void (*SetClipboardTextFn)(void* user_data, const char* text);
void* ClipboardUserData;
@@ -2588,10 +2725,10 @@ struct ImGuiInputTextCallbackData
ImGuiKey EventKey; // Key pressed (Up/Down/TAB) // Read-only // [Completion,History]
char* Buf; // Text buffer // Read-write // [Resize] Can replace pointer / [Completion,History,Always] Only write to pointed data, don't replace the actual pointer!
int BufTextLen; // Text length (in bytes) // Read-write // [Resize,Completion,History,Always] Exclude zero-terminator storage. In C land: == strlen(some_text), in C++ land: string.length()
- int BufSize; // Buffer size (in bytes) = capacity+1 // Read-only // [Resize,Completion,History,Always] Include zero-terminator storage. In C land == ARRAYSIZE(my_char_array), in C++ land: string.capacity()+1
+ int BufSize; // Buffer size (in bytes) = capacity+1 // Read-only // [Resize,Completion,History,Always] Include zero-terminator storage. In C land: == ARRAYSIZE(my_char_array), in C++ land: string.capacity()+1
bool BufDirty; // Set if you modify Buf/BufTextLen! // Write // [Completion,History,Always]
int CursorPos; // // Read-write // [Completion,History,Always]
- int SelectionStart; // // Read-write // [Completion,History,Always] == to SelectionEnd when no selection)
+ int SelectionStart; // // Read-write // [Completion,History,Always] == to SelectionEnd when no selection
int SelectionEnd; // // Read-write // [Completion,History,Always]
// Helper functions for text manipulation.
@@ -2782,6 +2919,13 @@ struct ImGuiStorage
#endif
};
+// Flags for ImGuiListClipper (currently not fully exposed in function calls: a future refactor will likely add this to ImGuiListClipper::Begin function equivalent)
+enum ImGuiListClipperFlags_
+{
+ ImGuiListClipperFlags_None = 0,
+ ImGuiListClipperFlags_NoSetTableRowCounters = 1 << 0, // [Internal] Disabled modifying table row counters. Avoid assumption that 1 clipper item == 1 table row.
+};
+
// Helper: Manually clip large list of items.
// If you have lots evenly spaced items and you have random access to the list, you can perform coarse
// clipping based on visibility to only submit items that are in view.
@@ -2809,9 +2953,10 @@ struct ImGuiListClipper
int DisplayEnd; // End of items to display (exclusive)
int ItemsCount; // [Internal] Number of items
float ItemsHeight; // [Internal] Height of item after a first step and item submission can calculate it
- float StartPosY; // [Internal] Cursor position at the time of Begin() or after table frozen rows are all processed
+ double StartPosY; // [Internal] Cursor position at the time of Begin() or after table frozen rows are all processed
double StartSeekOffsetY; // [Internal] Account for frozen rows in a table and initial loss of precision in very large windows.
void* TempData; // [Internal] Internal data
+ ImGuiListClipperFlags Flags; // [Internal] Flags, currently not yet well exposed.
// items_count: Use INT_MAX if you don't know how many items you have (in which case the cursor won't be advanced in the final step, and you can call SeekCursorForItem() manually if you need)
// items_height: Use -1.0f to be calculated automatically on first step. Otherwise pass in the distance between your items, typically GetTextLineHeightWithSpacing() or GetFrameHeightWithSpacing().
@@ -2833,7 +2978,7 @@ struct ImGuiListClipper
#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
inline void IncludeRangeByIndices(int item_begin, int item_end) { IncludeItemsByIndex(item_begin, item_end); } // [renamed in 1.89.9]
- inline void ForceDisplayRangeByIndices(int item_begin, int item_end) { IncludeItemsByIndex(item_begin, item_end); } // [renamed in 1.89.6]
+ //inline void ForceDisplayRangeByIndices(int item_begin, int item_end) { IncludeItemsByIndex(item_begin, item_end); } // [renamed in 1.89.6]
//inline ImGuiListClipper(int items_count, float items_height = -1.0f) { memset(this, 0, sizeof(*this)); ItemsCount = -1; Begin(items_count, items_height); } // [removed in 1.79]
#endif
};
@@ -2847,31 +2992,31 @@ struct ImGuiListClipper
#define IMGUI_DEFINE_MATH_OPERATORS_IMPLEMENTED
IM_MSVC_RUNTIME_CHECKS_OFF
// ImVec2 operators
-static inline ImVec2 operator*(const ImVec2& lhs, const float rhs) { return ImVec2(lhs.x * rhs, lhs.y * rhs); }
-static inline ImVec2 operator/(const ImVec2& lhs, const float rhs) { return ImVec2(lhs.x / rhs, lhs.y / rhs); }
-static inline ImVec2 operator+(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x + rhs.x, lhs.y + rhs.y); }
-static inline ImVec2 operator-(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x - rhs.x, lhs.y - rhs.y); }
-static inline ImVec2 operator*(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x * rhs.x, lhs.y * rhs.y); }
-static inline ImVec2 operator/(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x / rhs.x, lhs.y / rhs.y); }
-static inline ImVec2 operator-(const ImVec2& lhs) { return ImVec2(-lhs.x, -lhs.y); }
-static inline ImVec2& operator*=(ImVec2& lhs, const float rhs) { lhs.x *= rhs; lhs.y *= rhs; return lhs; }
-static inline ImVec2& operator/=(ImVec2& lhs, const float rhs) { lhs.x /= rhs; lhs.y /= rhs; return lhs; }
-static inline ImVec2& operator+=(ImVec2& lhs, const ImVec2& rhs) { lhs.x += rhs.x; lhs.y += rhs.y; return lhs; }
-static inline ImVec2& operator-=(ImVec2& lhs, const ImVec2& rhs) { lhs.x -= rhs.x; lhs.y -= rhs.y; return lhs; }
-static inline ImVec2& operator*=(ImVec2& lhs, const ImVec2& rhs) { lhs.x *= rhs.x; lhs.y *= rhs.y; return lhs; }
-static inline ImVec2& operator/=(ImVec2& lhs, const ImVec2& rhs) { lhs.x /= rhs.x; lhs.y /= rhs.y; return lhs; }
-static inline bool operator==(const ImVec2& lhs, const ImVec2& rhs) { return lhs.x == rhs.x && lhs.y == rhs.y; }
-static inline bool operator!=(const ImVec2& lhs, const ImVec2& rhs) { return lhs.x != rhs.x || lhs.y != rhs.y; }
+inline ImVec2 operator*(const ImVec2& lhs, const float rhs) { return ImVec2(lhs.x * rhs, lhs.y * rhs); }
+inline ImVec2 operator/(const ImVec2& lhs, const float rhs) { return ImVec2(lhs.x / rhs, lhs.y / rhs); }
+inline ImVec2 operator+(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x + rhs.x, lhs.y + rhs.y); }
+inline ImVec2 operator-(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x - rhs.x, lhs.y - rhs.y); }
+inline ImVec2 operator*(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x * rhs.x, lhs.y * rhs.y); }
+inline ImVec2 operator/(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x / rhs.x, lhs.y / rhs.y); }
+inline ImVec2 operator-(const ImVec2& lhs) { return ImVec2(-lhs.x, -lhs.y); }
+inline ImVec2& operator*=(ImVec2& lhs, const float rhs) { lhs.x *= rhs; lhs.y *= rhs; return lhs; }
+inline ImVec2& operator/=(ImVec2& lhs, const float rhs) { lhs.x /= rhs; lhs.y /= rhs; return lhs; }
+inline ImVec2& operator+=(ImVec2& lhs, const ImVec2& rhs) { lhs.x += rhs.x; lhs.y += rhs.y; return lhs; }
+inline ImVec2& operator-=(ImVec2& lhs, const ImVec2& rhs) { lhs.x -= rhs.x; lhs.y -= rhs.y; return lhs; }
+inline ImVec2& operator*=(ImVec2& lhs, const ImVec2& rhs) { lhs.x *= rhs.x; lhs.y *= rhs.y; return lhs; }
+inline ImVec2& operator/=(ImVec2& lhs, const ImVec2& rhs) { lhs.x /= rhs.x; lhs.y /= rhs.y; return lhs; }
+inline bool operator==(const ImVec2& lhs, const ImVec2& rhs) { return lhs.x == rhs.x && lhs.y == rhs.y; }
+inline bool operator!=(const ImVec2& lhs, const ImVec2& rhs) { return lhs.x != rhs.x || lhs.y != rhs.y; }
// ImVec4 operators
-static inline ImVec4 operator*(const ImVec4& lhs, const float rhs) { return ImVec4(lhs.x * rhs, lhs.y * rhs, lhs.z * rhs, lhs.w * rhs); }
-static inline ImVec4 operator/(const ImVec4& lhs, const float rhs) { return ImVec4(lhs.x / rhs, lhs.y / rhs, lhs.z / rhs, lhs.w / rhs); }
-static inline ImVec4 operator+(const ImVec4& lhs, const ImVec4& rhs) { return ImVec4(lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z, lhs.w + rhs.w); }
-static inline ImVec4 operator-(const ImVec4& lhs, const ImVec4& rhs) { return ImVec4(lhs.x - rhs.x, lhs.y - rhs.y, lhs.z - rhs.z, lhs.w - rhs.w); }
-static inline ImVec4 operator*(const ImVec4& lhs, const ImVec4& rhs) { return ImVec4(lhs.x * rhs.x, lhs.y * rhs.y, lhs.z * rhs.z, lhs.w * rhs.w); }
-static inline ImVec4 operator/(const ImVec4& lhs, const ImVec4& rhs) { return ImVec4(lhs.x / rhs.x, lhs.y / rhs.y, lhs.z / rhs.z, lhs.w / rhs.w); }
-static inline ImVec4 operator-(const ImVec4& lhs) { return ImVec4(-lhs.x, -lhs.y, -lhs.z, -lhs.w); }
-static inline bool operator==(const ImVec4& lhs, const ImVec4& rhs) { return lhs.x == rhs.x && lhs.y == rhs.y && lhs.z == rhs.z && lhs.w == rhs.w; }
-static inline bool operator!=(const ImVec4& lhs, const ImVec4& rhs) { return lhs.x != rhs.x || lhs.y != rhs.y || lhs.z != rhs.z || lhs.w != rhs.w; }
+inline ImVec4 operator*(const ImVec4& lhs, const float rhs) { return ImVec4(lhs.x * rhs, lhs.y * rhs, lhs.z * rhs, lhs.w * rhs); }
+inline ImVec4 operator/(const ImVec4& lhs, const float rhs) { return ImVec4(lhs.x / rhs, lhs.y / rhs, lhs.z / rhs, lhs.w / rhs); }
+inline ImVec4 operator+(const ImVec4& lhs, const ImVec4& rhs) { return ImVec4(lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z, lhs.w + rhs.w); }
+inline ImVec4 operator-(const ImVec4& lhs, const ImVec4& rhs) { return ImVec4(lhs.x - rhs.x, lhs.y - rhs.y, lhs.z - rhs.z, lhs.w - rhs.w); }
+inline ImVec4 operator*(const ImVec4& lhs, const ImVec4& rhs) { return ImVec4(lhs.x * rhs.x, lhs.y * rhs.y, lhs.z * rhs.z, lhs.w * rhs.w); }
+inline ImVec4 operator/(const ImVec4& lhs, const ImVec4& rhs) { return ImVec4(lhs.x / rhs.x, lhs.y / rhs.y, lhs.z / rhs.z, lhs.w / rhs.w); }
+inline ImVec4 operator-(const ImVec4& lhs) { return ImVec4(-lhs.x, -lhs.y, -lhs.z, -lhs.w); }
+inline bool operator==(const ImVec4& lhs, const ImVec4& rhs) { return lhs.x == rhs.x && lhs.y == rhs.y && lhs.z == rhs.z && lhs.w == rhs.w; }
+inline bool operator!=(const ImVec4& lhs, const ImVec4& rhs) { return lhs.x != rhs.x || lhs.y != rhs.y || lhs.z != rhs.z || lhs.w != rhs.w; }
IM_MSVC_RUNTIME_CHECKS_RESTORE
#endif
@@ -3105,11 +3250,11 @@ typedef void (*ImDrawCallback)(const ImDrawList* parent_list, const ImDrawCmd* c
// - VtxOffset: When 'io.BackendFlags & ImGuiBackendFlags_RendererHasVtxOffset' is enabled,
// this fields allow us to render meshes larger than 64K vertices while keeping 16-bit indices.
// Backends made for <1.71. will typically ignore the VtxOffset fields.
-// - The ClipRect/TextureId/VtxOffset fields must be contiguous as we memcmp() them together (this is asserted for).
+// - The ClipRect/TexRef/VtxOffset fields must be contiguous as we memcmp() them together (this is asserted for).
struct ImDrawCmd
{
ImVec4 ClipRect; // 4*4 // Clipping rectangle (x1, y1, x2, y2). Subtract ImDrawData->DisplayPos to get clipping rectangle in "viewport" coordinates
- ImTextureID TextureId; // 4-8 // User-provided texture ID. Set by user in ImfontAtlas::SetTexID() for fonts or passed to Image*() functions. Ignore if never using images or multiple fonts atlas.
+ ImTextureRef TexRef; // 16 // Reference to a font/texture atlas (where backend called ImTextureData::SetTexID()) or to a user-provided texture ID (via e.g. ImGui::Image() calls). Both will lead to a ImTextureID value.
unsigned int VtxOffset; // 4 // Start offset in vertex buffer. ImGuiBackendFlags_RendererHasVtxOffset: always 0, otherwise may be >0 to support meshes larger than 64K vertices with 16-bit indices.
unsigned int IdxOffset; // 4 // Start offset in index buffer.
unsigned int ElemCount; // 4 // Number of indices (multiple of 3) to be rendered as triangles. Vertices are stored in the callee ImDrawList's vtx_buffer[] array, indices in idx_buffer[].
@@ -3121,7 +3266,8 @@ struct ImDrawCmd
ImDrawCmd() { memset(this, 0, sizeof(*this)); } // Also ensure our padding fields are zeroed
// Since 1.83: returns ImTextureID associated with this draw call. Warning: DO NOT assume this is always same as 'TextureId' (we will change this function for an upcoming feature)
- inline ImTextureID GetTexID() const { return TextureId; }
+ // Since 1.92: removed ImDrawCmd::TextureId field, the getter function must be used!
+ inline ImTextureID GetTexID() const; // == (TexRef._TexData ? TexRef._TexData->TexID : TexRef._TexID)
};
// Vertex layout
@@ -3144,7 +3290,7 @@ IMGUI_OVERRIDE_DRAWVERT_STRUCT_LAYOUT;
struct ImDrawCmdHeader
{
ImVec4 ClipRect;
- ImTextureID TextureId;
+ ImTextureRef TexRef;
unsigned int VtxOffset;
};
@@ -3229,7 +3375,7 @@ struct ImDrawList
ImDrawCmdHeader _CmdHeader; // [Internal] template of active commands. Fields should match those of CmdBuffer.back().
ImDrawListSplitter _Splitter; // [Internal] for channels api (note: prefer using your own persistent instance of ImDrawListSplitter!)
ImVector _ClipRectStack; // [Internal]
- ImVector _TextureIdStack; // [Internal]
+ ImVector _TextureStack; // [Internal]
ImVector _CallbacksDataBuf; // [Internal]
float _FringeScale; // [Internal] anti-alias fringe is scaled by this value, this helps to keep things sharp while zooming at vertex buffer content
const char* _OwnerName; // Pointer to owner window's name for debugging
@@ -3242,8 +3388,8 @@ struct ImDrawList
IMGUI_API void PushClipRect(const ImVec2& clip_rect_min, const ImVec2& clip_rect_max, bool intersect_with_current_clip_rect = false); // Render-level scissoring. This is passed down to your render function but not used for CPU-side coarse clipping. Prefer using higher-level ImGui::PushClipRect() to affect logic (hit-testing and widget culling)
IMGUI_API void PushClipRectFullScreen();
IMGUI_API void PopClipRect();
- IMGUI_API void PushTextureID(ImTextureID texture_id);
- IMGUI_API void PopTextureID();
+ IMGUI_API void PushTexture(ImTextureRef tex_ref);
+ IMGUI_API void PopTexture();
inline ImVec2 GetClipRectMin() const { const ImVec4& cr = _ClipRectStack.back(); return ImVec2(cr.x, cr.y); }
inline ImVec2 GetClipRectMax() const { const ImVec4& cr = _ClipRectStack.back(); return ImVec2(cr.z, cr.w); }
@@ -3281,12 +3427,12 @@ struct ImDrawList
IMGUI_API void AddConcavePolyFilled(const ImVec2* points, int num_points, ImU32 col);
// Image primitives
- // - Read FAQ to understand what ImTextureID is.
+ // - Read FAQ to understand what ImTextureID/ImTextureRef are.
// - "p_min" and "p_max" represent the upper-left and lower-right corners of the rectangle.
// - "uv_min" and "uv_max" represent the normalized texture coordinates to use for those corners. Using (0,0)->(1,1) texture coordinates will generally display the entire texture.
- IMGUI_API void AddImage(ImTextureID user_texture_id, const ImVec2& p_min, const ImVec2& p_max, const ImVec2& uv_min = ImVec2(0, 0), const ImVec2& uv_max = ImVec2(1, 1), ImU32 col = IM_COL32_WHITE);
- IMGUI_API void AddImageQuad(ImTextureID user_texture_id, const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, const ImVec2& uv1 = ImVec2(0, 0), const ImVec2& uv2 = ImVec2(1, 0), const ImVec2& uv3 = ImVec2(1, 1), const ImVec2& uv4 = ImVec2(0, 1), ImU32 col = IM_COL32_WHITE);
- IMGUI_API void AddImageRounded(ImTextureID user_texture_id, const ImVec2& p_min, const ImVec2& p_max, const ImVec2& uv_min, const ImVec2& uv_max, ImU32 col, float rounding, ImDrawFlags flags = 0);
+ IMGUI_API void AddImage(ImTextureRef tex_ref, const ImVec2& p_min, const ImVec2& p_max, const ImVec2& uv_min = ImVec2(0, 0), const ImVec2& uv_max = ImVec2(1, 1), ImU32 col = IM_COL32_WHITE);
+ IMGUI_API void AddImageQuad(ImTextureRef tex_ref, const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, const ImVec2& uv1 = ImVec2(0, 0), const ImVec2& uv2 = ImVec2(1, 0), const ImVec2& uv3 = ImVec2(1, 1), const ImVec2& uv4 = ImVec2(0, 1), ImU32 col = IM_COL32_WHITE);
+ IMGUI_API void AddImageRounded(ImTextureRef tex_ref, const ImVec2& p_min, const ImVec2& p_max, const ImVec2& uv_min, const ImVec2& uv_max, ImU32 col, float rounding, ImDrawFlags flags = 0);
// Stateful path API, add points then finish with PathFillConvex() or PathStroke()
// - Important: filled shapes must always use clockwise winding order! The anti-aliasing fringe depends on it. Counter-clockwise shapes will have "inward" anti-aliasing.
@@ -3317,7 +3463,7 @@ struct ImDrawList
// Advanced: Miscellaneous
IMGUI_API void AddDrawCmd(); // This is useful if you need to forcefully create a new draw call (to allow for dependent rendering / blending). Otherwise primitives are merged into the same draw-call as much as possible
- IMGUI_API ImDrawList* CloneOutput() const; // Create a clone of the CmdBuffer/IdxBuffer/VtxBuffer.
+ IMGUI_API ImDrawList* CloneOutput() const; // Create a clone of the CmdBuffer/IdxBuffer/VtxBuffer. For multi-threaded rendering, consider using `imgui_threaded_rendering` from https://github.com/ocornut/imgui_club instead.
// Advanced: Channels
// - Use to split render into layers. By switching channels to can render out-of-order (e.g. submit FG primitives before BG primitives)
@@ -3342,6 +3488,10 @@ struct ImDrawList
inline void PrimVtx(const ImVec2& pos, const ImVec2& uv, ImU32 col) { PrimWriteIdx((ImDrawIdx)_VtxCurrentIdx); PrimWriteVtx(pos, uv, col); } // Write vertex with unique index
// Obsolete names
+#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
+ inline void PushTextureID(ImTextureRef tex_ref) { PushTexture(tex_ref); } // RENAMED in 1.92.x
+ inline void PopTextureID() { PopTexture(); } // RENAMED in 1.92.x
+#endif
//inline void AddEllipse(const ImVec2& center, float radius_x, float radius_y, ImU32 col, float rot = 0.0f, int num_segments = 0, float thickness = 1.0f) { AddEllipse(center, ImVec2(radius_x, radius_y), col, rot, num_segments, thickness); } // OBSOLETED in 1.90.5 (Mar 2024)
//inline void AddEllipseFilled(const ImVec2& center, float radius_x, float radius_y, ImU32 col, float rot = 0.0f, int num_segments = 0) { AddEllipseFilled(center, ImVec2(radius_x, radius_y), col, rot, num_segments); } // OBSOLETED in 1.90.5 (Mar 2024)
//inline void PathEllipticalArcTo(const ImVec2& center, float radius_x, float radius_y, float rot, float a_min, float a_max, int num_segments = 0) { PathEllipticalArcTo(center, ImVec2(radius_x, radius_y), rot, a_min, a_max, num_segments); } // OBSOLETED in 1.90.5 (Mar 2024)
@@ -3349,14 +3499,15 @@ struct ImDrawList
//inline void PathBezierCurveTo(const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, int num_segments = 0) { PathBezierCubicCurveTo(p2, p3, p4, num_segments); } // OBSOLETED in 1.80 (Jan 2021)
// [Internal helpers]
+ IMGUI_API void _SetDrawListSharedData(ImDrawListSharedData* data);
IMGUI_API void _ResetForNewFrame();
IMGUI_API void _ClearFreeMemory();
IMGUI_API void _PopUnusedDrawCmd();
IMGUI_API void _TryMergeDrawCmds();
IMGUI_API void _OnChangedClipRect();
- IMGUI_API void _OnChangedTextureID();
+ IMGUI_API void _OnChangedTexture();
IMGUI_API void _OnChangedVtxOffset();
- IMGUI_API void _SetTextureID(ImTextureID texture_id);
+ IMGUI_API void _SetTexture(ImTextureRef tex_ref);
IMGUI_API int _CalcCircleAutoSegmentCount(float radius) const;
IMGUI_API void _PathArcToFastEx(const ImVec2& center, float radius, int a_min_sample, int a_max_sample, int a_step);
IMGUI_API void _PathArcToN(const ImVec2& center, float radius, float a_min, float a_max, int num_segments);
@@ -3368,14 +3519,15 @@ struct ImDrawList
struct ImDrawData
{
bool Valid; // Only valid after Render() is called and before the next NewFrame() is called.
- int CmdListsCount; // Number of ImDrawList* to render
+ int CmdListsCount; // == CmdLists.Size. (OBSOLETE: exists for legacy reasons). Number of ImDrawList* to render.
int TotalIdxCount; // For convenience, sum of all ImDrawList's IdxBuffer.Size
int TotalVtxCount; // For convenience, sum of all ImDrawList's VtxBuffer.Size
ImVector CmdLists; // Array of ImDrawList* to render. The ImDrawLists are owned by ImGuiContext and only pointed to from here.
ImVec2 DisplayPos; // Top-left position of the viewport to render (== top-left of the orthogonal projection matrix to use) (== GetMainViewport()->Pos for the main viewport, == (0.0) in most single-viewport applications)
ImVec2 DisplaySize; // Size of the viewport to render (== GetMainViewport()->Size for the main viewport, == io.DisplaySize in most single-viewport applications)
- ImVec2 FramebufferScale; // Amount of pixels for each unit of DisplaySize. Based on io.DisplayFramebufferScale. Generally (1,1) on normal display, (2,2) on OSX with Retina display.
+ ImVec2 FramebufferScale; // Amount of pixels for each unit of DisplaySize. Copied from viewport->FramebufferScale (== io.DisplayFramebufferScale for main viewport). Generally (1,1) on normal display, (2,2) on OSX with Retina display.
ImGuiViewport* OwnerViewport; // Viewport carrying the ImDrawData instance, might be of use to the renderer (generally not).
+ ImVector* Textures; // List of textures to update. Most of the times the list is shared by all ImDrawData, has only 1 texture and it doesn't need any update. This almost always points to ImGui::GetPlatformIO().Textures[]. May be overriden or set to NULL if you want to manually update textures.
// Functions
ImDrawData() { Clear(); }
@@ -3385,6 +3537,89 @@ struct ImDrawData
IMGUI_API void ScaleClipRects(const ImVec2& fb_scale); // Helper to scale the ClipRect field of each ImDrawCmd. Use if your final output buffer is at a different scale than Dear ImGui expects, or if there is a difference between your window resolution and framebuffer resolution.
};
+//-----------------------------------------------------------------------------
+// [SECTION] Texture API (ImTextureFormat, ImTextureStatus, ImTextureRect, ImTextureData)
+//-----------------------------------------------------------------------------
+// In principle, the only data types that user/application code should care about are 'ImTextureRef' and 'ImTextureID'.
+// They are defined above in this header file. Read their description to the difference between ImTextureRef and ImTextureID.
+// FOR ALL OTHER ImTextureXXXX TYPES: ONLY CORE LIBRARY AND RENDERER BACKENDS NEED TO KNOW AND CARE ABOUT THEM.
+//-----------------------------------------------------------------------------
+
+#undef Status // X11 headers are leaking this.
+
+// We intentionally support a limited amount of texture formats to limit burden on CPU-side code and extension.
+// Most standard backends only support RGBA32 but we provide a single channel option for low-resource/embedded systems.
+enum ImTextureFormat
+{
+ ImTextureFormat_RGBA32, // 4 components per pixel, each is unsigned 8-bit. Total size = TexWidth * TexHeight * 4
+ ImTextureFormat_Alpha8, // 1 component per pixel, each is unsigned 8-bit. Total size = TexWidth * TexHeight
+};
+
+// Status of a texture to communicate with Renderer Backend.
+enum ImTextureStatus
+{
+ ImTextureStatus_OK,
+ ImTextureStatus_Destroyed, // Backend destroyed the texture.
+ ImTextureStatus_WantCreate, // Requesting backend to create the texture. Set status OK when done.
+ ImTextureStatus_WantUpdates, // Requesting backend to update specific blocks of pixels (write to texture portions which have never been used before). Set status OK when done.
+ ImTextureStatus_WantDestroy, // Requesting backend to destroy the texture. Set status to Destroyed when done.
+};
+
+// Coordinates of a rectangle within a texture.
+// When a texture is in ImTextureStatus_WantUpdates state, we provide a list of individual rectangles to copy to the graphics system.
+// You may use ImTextureData::Updates[] for the list, or ImTextureData::UpdateBox for a single bounding box.
+struct ImTextureRect
+{
+ unsigned short x, y; // Upper-left coordinates of rectangle to update
+ unsigned short w, h; // Size of rectangle to update (in pixels)
+};
+
+// Specs and pixel storage for a texture used by Dear ImGui.
+// This is only useful for (1) core library and (2) backends. End-user/applications do not need to care about this.
+// Renderer Backends will create a GPU-side version of this.
+// Why does we store two identifiers: TexID and BackendUserData?
+// - ImTextureID TexID = lower-level identifier stored in ImDrawCmd. ImDrawCmd can refer to textures not created by the backend, and for which there's no ImTextureData.
+// - void* BackendUserData = higher-level opaque storage for backend own book-keeping. Some backends may have enough with TexID and not need both.
+ // In columns below: who reads/writes each fields? 'r'=read, 'w'=write, 'core'=main library, 'backend'=renderer backend
+struct ImTextureData
+{
+ //------------------------------------------ core / backend ---------------------------------------
+ int UniqueID; // w - // [DEBUG] Sequential index to facilitate identifying a texture when debugging/printing. Unique per atlas.
+ ImTextureStatus Status; // rw rw // ImTextureStatus_OK/_WantCreate/_WantUpdates/_WantDestroy. Always use SetStatus() to modify!
+ void* BackendUserData; // - rw // Convenience storage for backend. Some backends may have enough with TexID.
+ ImTextureID TexID; // r w // Backend-specific texture identifier. Always use SetTexID() to modify! The identifier will stored in ImDrawCmd::GetTexID() and passed to backend's RenderDrawData function.
+ ImTextureFormat Format; // w r // ImTextureFormat_RGBA32 (default) or ImTextureFormat_Alpha8
+ int Width; // w r // Texture width
+ int Height; // w r // Texture height
+ int BytesPerPixel; // w r // 4 or 1
+ unsigned char* Pixels; // w r // Pointer to buffer holding 'Width*Height' pixels and 'Width*Height*BytesPerPixels' bytes.
+ ImTextureRect UsedRect; // w r // Bounding box encompassing all past and queued Updates[].
+ ImTextureRect UpdateRect; // w r // Bounding box encompassing all queued Updates[].
+ ImVector Updates; // w r // Array of individual updates.
+ int UnusedFrames; // w r // In order to facilitate handling Status==WantDestroy in some backend: this is a count successive frames where the texture was not used. Always >0 when Status==WantDestroy.
+ unsigned short RefCount; // w r // Number of contexts using this texture. Used during backend shutdown.
+ bool UseColors; // w r // Tell whether our texture data is known to use colors (rather than just white + alpha).
+ bool WantDestroyNextFrame; // rw - // [Internal] Queued to set ImTextureStatus_WantDestroy next frame. May still be used in the current frame.
+
+ // Functions
+ ImTextureData() { memset(this, 0, sizeof(*this)); Status = ImTextureStatus_Destroyed; TexID = ImTextureID_Invalid; }
+ ~ImTextureData() { DestroyPixels(); }
+ IMGUI_API void Create(ImTextureFormat format, int w, int h);
+ IMGUI_API void DestroyPixels();
+ void* GetPixels() { IM_ASSERT(Pixels != NULL); return Pixels; }
+ void* GetPixelsAt(int x, int y) { IM_ASSERT(Pixels != NULL); return Pixels + (x + y * Width) * BytesPerPixel; }
+ int GetSizeInBytes() const { return Width * Height * BytesPerPixel; }
+ int GetPitch() const { return Width * BytesPerPixel; }
+ ImTextureRef GetTexRef() { ImTextureRef tex_ref; tex_ref._TexData = this; tex_ref._TexID = ImTextureID_Invalid; return tex_ref; }
+ ImTextureID GetTexID() const { return TexID; }
+
+ // Called by Renderer backend
+ // - Call SetTexID() and SetStatus() after honoring texture requests. Never modify TexID and Status directly!
+ // - A backend may decide to destroy a texture that we did not request to destroy, which is fine (e.g. freeing resources), but we immediately set the texture back in _WantCreate mode.
+ void SetTexID(ImTextureID tex_id) { TexID = tex_id; }
+ void SetStatus(ImTextureStatus status) { Status = status; if (status == ImTextureStatus_Destroyed && !WantDestroyNextFrame) Status = ImTextureStatus_WantCreate; }
+};
+
//-----------------------------------------------------------------------------
// [SECTION] Font API (ImFontConfig, ImFontGlyph, ImFontAtlasFlags, ImFontAtlas, ImFontGlyphRangesBuilder, ImFont)
//-----------------------------------------------------------------------------
@@ -3392,43 +3627,56 @@ struct ImDrawData
// A font input/source (we may rename this to ImFontSource in the future)
struct ImFontConfig
{
+ // Data Source
+ char Name[40]; // // Name (strictly to ease debugging, hence limited size buffer)
void* FontData; // // TTF/OTF data
int FontDataSize; // // TTF/OTF data size
bool FontDataOwnedByAtlas; // true // TTF/OTF data ownership taken by the container ImFontAtlas (will delete memory itself).
+
+ // Options
bool MergeMode; // false // Merge into previous ImFont, so you can combine multiple inputs font into one ImFont (e.g. ASCII font + icons + Japanese glyphs). You may want to use GlyphOffset.y when merge font of different heights.
bool PixelSnapH; // false // Align every glyph AdvanceX to pixel boundaries. Useful e.g. if you are merging a non-pixel aligned font with the default font. If enabled, you can set OversampleH/V to 1.
- int FontNo; // 0 // Index of font within TTF/OTF file
- int OversampleH; // 0 (2) // Rasterize at higher quality for sub-pixel positioning. 0 == auto == 1 or 2 depending on size. Note the difference between 2 and 3 is minimal. You can reduce this to 1 for large glyphs save memory. Read https://github.com/nothings/stb/blob/master/tests/oversample/README.md for details.
- int OversampleV; // 0 (1) // Rasterize at higher quality for sub-pixel positioning. 0 == auto == 1. This is not really useful as we don't use sub-pixel positions on the Y axis.
- float SizePixels; // // Size in pixels for rasterizer (more or less maps to the resulting font height).
- //ImVec2 GlyphExtraSpacing; // 0, 0 // (REMOVED IN 1.91.9: use GlyphExtraAdvanceX)
- ImVec2 GlyphOffset; // 0, 0 // Offset all glyphs from this font input.
- const ImWchar* GlyphRanges; // NULL // THE ARRAY DATA NEEDS TO PERSIST AS LONG AS THE FONT IS ALIVE. Pointer to a user-provided list of Unicode range (2 value per range, values are inclusive, zero-terminated list).
- float GlyphMinAdvanceX; // 0 // Minimum AdvanceX for glyphs, set Min to align font icons, set both Min/Max to enforce mono-space font
- float GlyphMaxAdvanceX; // FLT_MAX // Maximum AdvanceX for glyphs
- float GlyphExtraAdvanceX; // 0 // Extra spacing (in pixels) between glyphs. Please contact us if you are using this.
- unsigned int FontBuilderFlags; // 0 // Settings for custom font builder. THIS IS BUILDER IMPLEMENTATION DEPENDENT. Leave as zero if unsure.
- float RasterizerMultiply; // 1.0f // Linearly brighten (>1.0f) or darken (<1.0f) font output. Brightening small fonts may be a good workaround to make them more readable. This is a silly thing we may remove in the future.
- float RasterizerDensity; // 1.0f // DPI scale for rasterization, not altering other font metrics: make it easy to swap between e.g. a 100% and a 400% fonts for a zooming display, or handle Retina screen. IMPORTANT: If you change this it is expected that you increase/decrease font scale roughly to the inverse of this, otherwise quality may look lowered.
+ bool PixelSnapV; // true // Align Scaled GlyphOffset.y to pixel boundaries.
+ ImS8 OversampleH; // 0 (2) // Rasterize at higher quality for sub-pixel positioning. 0 == auto == 1 or 2 depending on size. Note the difference between 2 and 3 is minimal. You can reduce this to 1 for large glyphs save memory. Read https://github.com/nothings/stb/blob/master/tests/oversample/README.md for details.
+ ImS8 OversampleV; // 0 (1) // Rasterize at higher quality for sub-pixel positioning. 0 == auto == 1. This is not really useful as we don't use sub-pixel positions on the Y axis.
ImWchar EllipsisChar; // 0 // Explicitly specify Unicode codepoint of ellipsis character. When fonts are being merged first specified ellipsis will be used.
+ float SizePixels; // // Size in pixels for rasterizer (more or less maps to the resulting font height).
+ const ImWchar* GlyphRanges; // NULL // *LEGACY* THE ARRAY DATA NEEDS TO PERSIST AS LONG AS THE FONT IS ALIVE. Pointer to a user-provided list of Unicode range (2 value per range, values are inclusive, zero-terminated list).
+ const ImWchar* GlyphExcludeRanges; // NULL // Pointer to a small user-provided list of Unicode ranges (2 value per range, values are inclusive, zero-terminated list). This is very close to GlyphRanges[] but designed to exclude ranges from a font source, when merging fonts with overlapping glyphs. Use "Input Glyphs Overlap Detection Tool" to find about your overlapping ranges.
+ //ImVec2 GlyphExtraSpacing; // 0, 0 // (REMOVED AT IT SEEMS LARGELY OBSOLETE. PLEASE REPORT IF YOU WERE USING THIS). Extra spacing (in pixels) between glyphs when rendered: essentially add to glyph->AdvanceX. Only X axis is supported for now.
+ ImVec2 GlyphOffset; // 0, 0 // Offset (in pixels) all glyphs from this font input. Absolute value for default size, other sizes will scale this value.
+ float GlyphMinAdvanceX; // 0 // Minimum AdvanceX for glyphs, set Min to align font icons, set both Min/Max to enforce mono-space font. Absolute value for default size, other sizes will scale this value.
+ float GlyphMaxAdvanceX; // FLT_MAX // Maximum AdvanceX for glyphs
+ float GlyphExtraAdvanceX; // 0 // Extra spacing (in pixels) between glyphs. Please contact us if you are using this. // FIXME-NEWATLAS: Intentionally unscaled
+ ImU32 FontNo; // 0 // Index of font within TTF/OTF file
+ unsigned int FontLoaderFlags; // 0 // Settings for custom font builder. THIS IS BUILDER IMPLEMENTATION DEPENDENT. Leave as zero if unsure.
+ //unsigned int FontBuilderFlags; // -- // [Renamed in 1.92] Ue FontLoaderFlags.
+ float RasterizerMultiply; // 1.0f // Linearly brighten (>1.0f) or darken (<1.0f) font output. Brightening small fonts may be a good workaround to make them more readable. This is a silly thing we may remove in the future.
+ float RasterizerDensity; // 1.0f // [LEGACY: this only makes sense when ImGuiBackendFlags_RendererHasTextures is not supported] DPI scale multiplier for rasterization. Not altering other font metrics: makes it easy to swap between e.g. a 100% and a 400% fonts for a zooming display, or handle Retina screen. IMPORTANT: If you change this it is expected that you increase/decrease font scale roughly to the inverse of this, otherwise quality may look lowered.
// [Internal]
- char Name[40]; // Name (strictly to ease debugging)
- ImFont* DstFont;
+ ImFontFlags Flags; // Font flags (don't use just yet, will be exposed in upcoming 1.92.X updates)
+ ImFont* DstFont; // Target font (as we merging fonts, multiple ImFontConfig may target the same font)
+ const ImFontLoader* FontLoader; // Custom font backend for this source (default source is the one stored in ImFontAtlas)
+ void* FontLoaderData; // Font loader opaque storage (per font config)
IMGUI_API ImFontConfig();
};
// Hold rendering data for one glyph.
-// (Note: some language parsers may fail to convert the 31+1 bitfield members, in this case maybe drop store a single u32 or we can rework this)
+// (Note: some language parsers may fail to convert the bitfield members, in this case maybe drop store a single u32 or we can rework this)
struct ImFontGlyph
{
unsigned int Colored : 1; // Flag to indicate glyph is colored and should generally ignore tinting (make it usable with no shift on little-endian as this is used in loops)
unsigned int Visible : 1; // Flag to indicate glyph has no visible pixels (e.g. space). Allow early out when rendering.
- unsigned int Codepoint : 30; // 0x0000..0x10FFFF
- float AdvanceX; // Horizontal distance to advance layout with
- float X0, Y0, X1, Y1; // Glyph corners
- float U0, V0, U1, V1; // Texture coordinates
+ unsigned int SourceIdx : 4; // Index of source in parent font
+ unsigned int Codepoint : 26; // 0x0000..0x10FFFF
+ float AdvanceX; // Horizontal distance to advance cursor/layout position.
+ float X0, Y0, X1, Y1; // Glyph corners. Offsets from current cursor/layout position.
+ float U0, V0, U1, V1; // Texture coordinates for the current value of ImFontAtlas->TexRef. Cached equivalent of calling GetCustomRect() with PackId.
+ int PackId; // [Internal] ImFontAtlasRectId value (FIXME: Cold data, could be moved elsewhere?)
+
+ ImFontGlyph() { memset(this, 0, sizeof(*this)); PackId = -1; }
};
// Helper to build glyph ranges from text/string data. Feed your application strings/characters to it then call BuildRanges().
@@ -3447,20 +3695,21 @@ struct ImFontGlyphRangesBuilder
IMGUI_API void BuildRanges(ImVector* out_ranges); // Output new ranges
};
-// See ImFontAtlas::AddCustomRectXXX functions.
-struct ImFontAtlasCustomRect
-{
- unsigned short X, Y; // Output // Packed position in Atlas
+// An opaque identifier to a rectangle in the atlas. -1 when invalid.
+// The rectangle may move and UV may be invalidated, use GetCustomRect() to retrieve it.
+typedef int ImFontAtlasRectId;
+#define ImFontAtlasRectId_Invalid -1
- // [Internal]
- unsigned short Width, Height; // Input // Desired rectangle dimension
- unsigned int GlyphID : 31; // Input // For custom font glyphs only (ID < 0x110000)
- unsigned int GlyphColored : 1; // Input // For custom font glyphs only: glyph is colored, removed tinting.
- float GlyphAdvanceX; // Input // For custom font glyphs only: glyph xadvance
- ImVec2 GlyphOffset; // Input // For custom font glyphs only: glyph display offset
- ImFont* Font; // Input // For custom font glyphs only: target font
- ImFontAtlasCustomRect() { X = Y = 0xFFFF; Width = Height = 0; GlyphID = 0; GlyphColored = 0; GlyphAdvanceX = 0.0f; GlyphOffset = ImVec2(0, 0); Font = NULL; }
- bool IsPacked() const { return X != 0xFFFF; }
+// Output of ImFontAtlas::GetCustomRect() when using custom rectangles.
+// Those values may not be cached/stored as they are only valid for the current value of atlas->TexRef
+// (this is in theory derived from ImTextureRect but we use separate structures for reasons)
+struct ImFontAtlasRect
+{
+ unsigned short x, y; // Position (in current texture)
+ unsigned short w, h; // Size
+ ImVec2 uv0, uv1; // UV coordinates (in current texture)
+
+ ImFontAtlasRect() { memset(this, 0, sizeof(*this)); }
};
// Flags for ImFontAtlas build
@@ -3476,12 +3725,14 @@ enum ImFontAtlasFlags_
// - One or more fonts.
// - Custom graphics data needed to render the shapes needed by Dear ImGui.
// - Mouse cursor shapes for software cursor rendering (unless setting 'Flags |= ImFontAtlasFlags_NoMouseCursors' in the font atlas).
-// It is the user-code responsibility to setup/build the atlas, then upload the pixel data into a texture accessible by your graphics api.
-// - Optionally, call any of the AddFont*** functions. If you don't call any, the default font embedded in the code will be loaded for you.
-// - Call GetTexDataAsAlpha8() or GetTexDataAsRGBA32() to build and retrieve pixels data.
-// - Upload the pixels data into a texture within your graphics system (see imgui_impl_xxxx.cpp examples)
+// - If you don't call any AddFont*** functions, the default font embedded in the code will be loaded for you.
+// It is the rendering backend responsibility to upload texture into your graphics API:
+// - ImGui_ImplXXXX_RenderDrawData() functions generally iterate platform_io->Textures[] to create/update/destroy each ImTextureData instance.
+// - Backend then set ImTextureData's TexID and BackendUserData.
+// - Texture id are passed back to you during rendering to identify the texture. Read FAQ entry about ImTextureID/ImTextureRef for more details.
+// Legacy path:
+// - Call Build() + GetTexDataAsAlpha8() or GetTexDataAsRGBA32() to build and retrieve pixels data.
// - Call SetTexID(my_tex_id); and pass the pointer/identifier to your texture in a format natural to your graphics API.
-// This value will be passed back to you during rendering to identify the texture. Read FAQ entry about ImTextureID for more details.
// Common pitfalls:
// - If you pass a 'glyph_ranges' array to AddFont*** functions, you need to make sure that your array persist up until the
// atlas is build (when calling GetTexData*** or Build()). We only copy the pointer, not the data.
@@ -3495,35 +3746,49 @@ struct ImFontAtlas
IMGUI_API ~ImFontAtlas();
IMGUI_API ImFont* AddFont(const ImFontConfig* font_cfg);
IMGUI_API ImFont* AddFontDefault(const ImFontConfig* font_cfg = NULL);
- IMGUI_API ImFont* AddFontFromFileTTF(const char* filename, float size_pixels, const ImFontConfig* font_cfg = NULL, const ImWchar* glyph_ranges = NULL);
- IMGUI_API ImFont* AddFontFromMemoryTTF(void* font_data, int font_data_size, float size_pixels, const ImFontConfig* font_cfg = NULL, const ImWchar* glyph_ranges = NULL); // Note: Transfer ownership of 'ttf_data' to ImFontAtlas! Will be deleted after destruction of the atlas. Set font_cfg->FontDataOwnedByAtlas=false to keep ownership of your data and it won't be freed.
- IMGUI_API ImFont* AddFontFromMemoryCompressedTTF(const void* compressed_font_data, int compressed_font_data_size, float size_pixels, const ImFontConfig* font_cfg = NULL, const ImWchar* glyph_ranges = NULL); // 'compressed_font_data' still owned by caller. Compress with binary_to_compressed_c.cpp.
- IMGUI_API ImFont* AddFontFromMemoryCompressedBase85TTF(const char* compressed_font_data_base85, float size_pixels, const ImFontConfig* font_cfg = NULL, const ImWchar* glyph_ranges = NULL); // 'compressed_font_data_base85' still owned by caller. Compress with binary_to_compressed_c.cpp with -base85 parameter.
- IMGUI_API void ClearInputData(); // Clear input data (all ImFontConfig structures including sizes, TTF data, glyph ranges, etc.) = all the data used to build the texture and fonts.
- IMGUI_API void ClearFonts(); // Clear input+output font data (same as ClearInputData() + glyphs storage, UV coordinates).
- IMGUI_API void ClearTexData(); // Clear output texture data (CPU side). Saves RAM once the texture has been copied to graphics memory.
- IMGUI_API void Clear(); // Clear all input and output.
+ IMGUI_API ImFont* AddFontFromFileTTF(const char* filename, float size_pixels = 0.0f, const ImFontConfig* font_cfg = NULL, const ImWchar* glyph_ranges = NULL);
+ IMGUI_API ImFont* AddFontFromMemoryTTF(void* font_data, int font_data_size, float size_pixels = 0.0f, const ImFontConfig* font_cfg = NULL, const ImWchar* glyph_ranges = NULL); // Note: Transfer ownership of 'ttf_data' to ImFontAtlas! Will be deleted after destruction of the atlas. Set font_cfg->FontDataOwnedByAtlas=false to keep ownership of your data and it won't be freed.
+ IMGUI_API ImFont* AddFontFromMemoryCompressedTTF(const void* compressed_font_data, int compressed_font_data_size, float size_pixels = 0.0f, const ImFontConfig* font_cfg = NULL, const ImWchar* glyph_ranges = NULL); // 'compressed_font_data' still owned by caller. Compress with binary_to_compressed_c.cpp.
+ IMGUI_API ImFont* AddFontFromMemoryCompressedBase85TTF(const char* compressed_font_data_base85, float size_pixels = 0.0f, const ImFontConfig* font_cfg = NULL, const ImWchar* glyph_ranges = NULL); // 'compressed_font_data_base85' still owned by caller. Compress with binary_to_compressed_c.cpp with -base85 parameter.
+ IMGUI_API void RemoveFont(ImFont* font);
- // Build atlas, retrieve pixel data.
- // User is in charge of copying the pixels into graphics memory (e.g. create a texture with your engine). Then store your texture handle with SetTexID().
- // The pitch is always = Width * BytesPerPixels (1 or 4)
- // Building in RGBA32 format is provided for convenience and compatibility, but note that unless you manually manipulate or copy color data into
- // the texture (e.g. when using the AddCustomRect*** api), then the RGB pixels emitted will always be white (~75% of memory/bandwidth waste.
- IMGUI_API bool Build(); // Build pixels data. This is called automatically for you by the GetTexData*** functions.
- IMGUI_API void GetTexDataAsAlpha8(unsigned char** out_pixels, int* out_width, int* out_height, int* out_bytes_per_pixel = NULL); // 1 byte per-pixel
- IMGUI_API void GetTexDataAsRGBA32(unsigned char** out_pixels, int* out_width, int* out_height, int* out_bytes_per_pixel = NULL); // 4 bytes-per-pixel
- bool IsBuilt() const { return Fonts.Size > 0 && TexReady; } // Bit ambiguous: used to detect when user didn't build texture but effectively we should check TexID != 0 except that would be backend dependent...
- void SetTexID(ImTextureID id) { TexID = id; }
+ IMGUI_API void Clear(); // Clear everything (input fonts, output glyphs/textures)
+ IMGUI_API void CompactCache(); // Compact cached glyphs and texture.
+ IMGUI_API void SetFontLoader(const ImFontLoader* font_loader); // Change font loader at runtime.
+
+ // As we are transitioning toward a new font system, we expect to obsolete those soon:
+ IMGUI_API void ClearInputData(); // [OBSOLETE] Clear input data (all ImFontConfig structures including sizes, TTF data, glyph ranges, etc.) = all the data used to build the texture and fonts.
+ IMGUI_API void ClearFonts(); // [OBSOLETE] Clear input+output font data (same as ClearInputData() + glyphs storage, UV coordinates).
+ IMGUI_API void ClearTexData(); // [OBSOLETE] Clear CPU-side copy of the texture data. Saves RAM once the texture has been copied to graphics memory.
+
+#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
+ // Legacy path for build atlas + retrieving pixel data.
+ // - User is in charge of copying the pixels into graphics memory (e.g. create a texture with your engine). Then store your texture handle with SetTexID().
+ // - The pitch is always = Width * BytesPerPixels (1 or 4)
+ // - Building in RGBA32 format is provided for convenience and compatibility, but note that unless you manually manipulate or copy color data into
+ // the texture (e.g. when using the AddCustomRect*** api), then the RGB pixels emitted will always be white (~75% of memory/bandwidth waste).
+ // - From 1.92 with backends supporting ImGuiBackendFlags_RendererHasTextures:
+ // - Calling Build(), GetTexDataAsAlpha8(), GetTexDataAsRGBA32() is not needed.
+ // - In backend: replace calls to ImFontAtlas::SetTexID() with calls to ImTextureData::SetTexID() after honoring texture creation.
+ IMGUI_API bool Build(); // Build pixels data. This is called automatically for you by the GetTexData*** functions.
+ IMGUI_API void GetTexDataAsAlpha8(unsigned char** out_pixels, int* out_width, int* out_height, int* out_bytes_per_pixel = NULL); // 1 byte per-pixel
+ IMGUI_API void GetTexDataAsRGBA32(unsigned char** out_pixels, int* out_width, int* out_height, int* out_bytes_per_pixel = NULL); // 4 bytes-per-pixel
+ void SetTexID(ImTextureID id) { IM_ASSERT(TexRef._TexID == ImTextureID_Invalid); TexRef._TexData->TexID = id; } // Called by legacy backends. May be called before texture creation.
+ void SetTexID(ImTextureRef id) { IM_ASSERT(TexRef._TexID == ImTextureID_Invalid && id._TexData == NULL); TexRef._TexData->TexID = id._TexID; } // Called by legacy backends.
+ bool IsBuilt() const { return Fonts.Size > 0 && TexIsBuilt; } // Bit ambiguous: used to detect when user didn't build texture but effectively we should check TexID != 0 except that would be backend dependent..
+#endif
//-------------------------------------------
// Glyph Ranges
//-------------------------------------------
+ // Since 1.92: specifying glyph ranges is only useful/necessary if your backend doesn't support ImGuiBackendFlags_RendererHasTextures!
+ IMGUI_API const ImWchar* GetGlyphRangesDefault(); // Basic Latin, Extended Latin
+#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
// Helpers to retrieve list of common Unicode ranges (2 value per range, values are inclusive, zero-terminated list)
// NB: Make sure that your string are UTF-8 and NOT in your local code page.
// Read https://github.com/ocornut/imgui/blob/master/docs/FONTS.md/#about-utf-8-encoding for details.
// NB: Consider using ImFontGlyphRangesBuilder to build glyph ranges from textual data.
- IMGUI_API const ImWchar* GetGlyphRangesDefault(); // Basic Latin, Extended Latin
IMGUI_API const ImWchar* GetGlyphRangesGreek(); // Default + Greek and Coptic
IMGUI_API const ImWchar* GetGlyphRangesKorean(); // Default + Korean characters
IMGUI_API const ImWchar* GetGlyphRangesJapanese(); // Default + Hiragana, Katakana, Half-Width, Selection of 2999 Ideographs
@@ -3532,24 +3797,32 @@ struct ImFontAtlas
IMGUI_API const ImWchar* GetGlyphRangesCyrillic(); // Default + about 400 Cyrillic characters
IMGUI_API const ImWchar* GetGlyphRangesThai(); // Default + Thai characters
IMGUI_API const ImWchar* GetGlyphRangesVietnamese(); // Default + Vietnamese characters
+#endif
//-------------------------------------------
// [ALPHA] Custom Rectangles/Glyphs API
//-------------------------------------------
- // You can request arbitrary rectangles to be packed into the atlas, for your own purposes.
- // - After calling Build(), you can query the rectangle position and render your pixels.
- // - If you render colored output, set 'atlas->TexPixelsUseColors = true' as this may help some backends decide of preferred texture format.
- // - You can also request your rectangles to be mapped as font glyph (given a font + Unicode point),
- // so you can render e.g. custom colorful icons and use them as regular glyphs.
+ // Register and retrieve custom rectangles
+ // - You can request arbitrary rectangles to be packed into the atlas, for your own purpose.
+ // - Since 1.92.X, packing is done immediately in the function call (previously packing was done during the Build call)
+ // - You can render your pixels into the texture right after calling the AddCustomRect() functions.
+ // - VERY IMPORTANT:
+ // - Texture may be created/resized at any time when calling ImGui or ImFontAtlas functions.
+ // - IT WILL INVALIDATE RECTANGLE DATA SUCH AS UV COORDINATES. Always use latest values from GetCustomRect().
+ // - UV coordinates are associated to the current texture identifier aka 'atlas->TexRef'. Both TexRef and UV coordinates are typically changed at the same time.
+ // - If you render colored output into your custom rectangles: set 'atlas->TexPixelsUseColors = true' as this may help some backends decide of preferred texture format.
// - Read docs/FONTS.md for more details about using colorful icons.
- // - Note: this API may be redesigned later in order to support multi-monitor varying DPI settings.
- IMGUI_API int AddCustomRectRegular(int width, int height);
- IMGUI_API int AddCustomRectFontGlyph(ImFont* font, ImWchar id, int width, int height, float advance_x, const ImVec2& offset = ImVec2(0, 0));
- ImFontAtlasCustomRect* GetCustomRectByIndex(int index) { IM_ASSERT(index >= 0); return &CustomRects[index]; }
-
- // [Internal]
- IMGUI_API void CalcCustomRectUV(const ImFontAtlasCustomRect* rect, ImVec2* out_uv_min, ImVec2* out_uv_max) const;
+ // - Note: this API may be reworked further in order to facilitate supporting e.g. multi-monitor, varying DPI settings.
+ // - (Pre-1.92 names) ------------> (1.92 names)
+ // - GetCustomRectByIndex() --> Use GetCustomRect()
+ // - CalcCustomRectUV() --> Use GetCustomRect() and read uv0, uv1 fields.
+ // - AddCustomRectRegular() --> Renamed to AddCustomRect()
+ // - AddCustomRectFontGlyph() --> Prefer using custom ImFontLoader inside ImFontConfig
+ // - ImFontAtlasCustomRect --> Renamed to ImFontAtlasRect
+ IMGUI_API ImFontAtlasRectId AddCustomRect(int width, int height, ImFontAtlasRect* out_r = NULL);// Register a rectangle. Return -1 (ImFontAtlasRectId_Invalid) on error.
+ IMGUI_API void RemoveCustomRect(ImFontAtlasRectId id); // Unregister a rectangle. Existing pixels will stay in texture until resized / garbage collected.
+ IMGUI_API bool GetCustomRect(ImFontAtlasRectId id, ImFontAtlasRect* out_r) const; // Get rectangle coordinates for current texture. Valid immediately, never store this (read above)!
//-------------------------------------------
// Members
@@ -3557,96 +3830,178 @@ struct ImFontAtlas
// Input
ImFontAtlasFlags Flags; // Build flags (see ImFontAtlasFlags_)
- ImTextureID TexID; // User data to refer to the texture once it has been uploaded to user's graphic systems. It is passed back to you during rendering via the ImDrawCmd structure.
- int TexDesiredWidth; // Texture width desired by user before Build(). Must be a power-of-two. If have many glyphs your graphics API have texture size restrictions you may want to increase texture width to decrease height.
+ ImTextureFormat TexDesiredFormat; // Desired texture format (default to ImTextureFormat_RGBA32 but may be changed to ImTextureFormat_Alpha8).
int TexGlyphPadding; // FIXME: Should be called "TexPackPadding". Padding between glyphs within texture in pixels. Defaults to 1. If your rendering method doesn't rely on bilinear filtering you may set this to 0 (will also need to set AntiAliasedLinesUseTex = false).
+ int TexMinWidth; // Minimum desired texture width. Must be a power of two. Default to 512.
+ int TexMinHeight; // Minimum desired texture height. Must be a power of two. Default to 128.
+ int TexMaxWidth; // Maximum desired texture width. Must be a power of two. Default to 8192.
+ int TexMaxHeight; // Maximum desired texture height. Must be a power of two. Default to 8192.
void* UserData; // Store your own atlas related user-data (if e.g. you have multiple font atlas).
+ // Output
+ // - Because textures are dynamically created/resized, the current texture identifier may changed at *ANY TIME* during the frame.
+ // - This should not affect you as you can always use the latest value. But note that any precomputed UV coordinates are only valid for the current TexRef.
+#ifdef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
+ ImTextureRef TexRef; // Latest texture identifier == TexData->GetTexRef().
+#else
+ union { ImTextureRef TexRef; ImTextureRef TexID; }; // Latest texture identifier == TexData->GetTexRef(). // RENAMED TexID to TexRef in 1.92.x
+#endif
+ ImTextureData* TexData; // Latest texture.
+
// [Internal]
- // NB: Access texture data via GetTexData*() calls! Which will setup a default font for you.
- bool Locked; // Marked as Locked by ImGui::NewFrame() so attempt to modify the atlas will assert.
- bool TexReady; // Set when texture was built matching current font input
- bool TexPixelsUseColors; // Tell whether our texture data is known to use colors (rather than just alpha channel), in order to help backend select a format.
- unsigned char* TexPixelsAlpha8; // 1 component per pixel, each component is unsigned 8-bit. Total size = TexWidth * TexHeight
- unsigned int* TexPixelsRGBA32; // 4 component per pixel, each component is unsigned 8-bit. Total size = TexWidth * TexHeight * 4
- int TexWidth; // Texture width calculated during Build().
- int TexHeight; // Texture height calculated during Build().
- ImVec2 TexUvScale; // = (1.0f/TexWidth, 1.0f/TexHeight)
- ImVec2 TexUvWhitePixel; // Texture coordinates to a white pixel
+ ImVector TexList; // Texture list (most often TexList.Size == 1). TexData is always == TexList.back(). DO NOT USE DIRECTLY, USE GetDrawData().Textures[]/GetPlatformIO().Textures[] instead!
+ bool Locked; // Marked as locked during ImGui::NewFrame()..EndFrame() scope if TexUpdates are not supported. Any attempt to modify the atlas will assert.
+ bool RendererHasTextures;// Copy of (BackendFlags & ImGuiBackendFlags_RendererHasTextures) from supporting context.
+ bool TexIsBuilt; // Set when texture was built matching current font input. Mostly useful for legacy IsBuilt() call.
+ bool TexPixelsUseColors; // Tell whether our texture data is known to use colors (rather than just alpha channel), in order to help backend select a format or conversion process.
+ ImVec2 TexUvScale; // = (1.0f/TexData->TexWidth, 1.0f/TexData->TexHeight). May change as new texture gets created.
+ ImVec2 TexUvWhitePixel; // Texture coordinates to a white pixel. May change as new texture gets created.
ImVector Fonts; // Hold all the fonts returned by AddFont*. Fonts[0] is the default font upon calling ImGui::NewFrame(), use ImGui::PushFont()/PopFont() to change the current font.
- ImVector CustomRects; // Rectangles for packing custom texture data into the atlas.
ImVector Sources; // Source/configuration data
ImVec4 TexUvLines[IM_DRAWLIST_TEX_LINES_WIDTH_MAX + 1]; // UVs for baked anti-aliased lines
-
- // [Internal] Font builder
- const ImFontBuilderIO* FontBuilderIO; // Opaque interface to a font builder (default to stb_truetype, can be changed to use FreeType by defining IMGUI_ENABLE_FREETYPE).
- unsigned int FontBuilderFlags; // Shared flags (for all fonts) for custom font builder. THIS IS BUILD IMPLEMENTATION DEPENDENT. Per-font override is also available in ImFontConfig.
-
- // [Internal] Packing data
- int PackIdMouseCursors; // Custom texture rectangle ID for white pixel and mouse cursors
- int PackIdLines; // Custom texture rectangle ID for baked anti-aliased lines
+ int TexNextUniqueID; // Next value to be stored in TexData->UniqueID
+ int FontNextUniqueID; // Next value to be stored in ImFont->FontID
+ ImVector DrawListSharedDatas; // List of users for this atlas. Typically one per Dear ImGui context.
+ ImFontAtlasBuilder* Builder; // Opaque interface to our data that doesn't need to be public and may be discarded when rebuilding.
+ const ImFontLoader* FontLoader; // Font loader opaque interface (default to use FreeType when IMGUI_ENABLE_FREETYPE is defined, otherwise default to use stb_truetype). Use SetFontLoader() to change this at runtime.
+ const char* FontLoaderName; // Font loader name (for display e.g. in About box) == FontLoader->Name
+ void* FontLoaderData; // Font backend opaque storage
+ unsigned int FontLoaderFlags; // Shared flags (for all fonts) for font loader. THIS IS BUILD IMPLEMENTATION DEPENDENT (e.g. Per-font override is also available in ImFontConfig).
+ int RefCount; // Number of contexts using this atlas
+ ImGuiContext* OwnerContext; // Context which own the atlas will be in charge of updating and destroying it.
// [Obsolete]
- //typedef ImFontAtlasCustomRect CustomRect; // OBSOLETED in 1.72+
- //typedef ImFontGlyphRangesBuilder GlyphRangesBuilder; // OBSOLETED in 1.67+
+#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
+ // Legacy: You can request your rectangles to be mapped as font glyph (given a font + Unicode point), so you can render e.g. custom colorful icons and use them as regular glyphs. --> Prefer using a custom ImFontLoader.
+ ImFontAtlasRect TempRect; // For old GetCustomRectByIndex() API
+ inline ImFontAtlasRectId AddCustomRectRegular(int w, int h) { return AddCustomRect(w, h); } // RENAMED in 1.92.X
+ inline const ImFontAtlasRect* GetCustomRectByIndex(ImFontAtlasRectId id) { return GetCustomRect(id, &TempRect) ? &TempRect : NULL; } // OBSOLETED in 1.92.X
+ inline void CalcCustomRectUV(const ImFontAtlasRect* r, ImVec2* out_uv_min, ImVec2* out_uv_max) const { *out_uv_min = r->uv0; *out_uv_max = r->uv1; } // OBSOLETED in 1.92.X
+ IMGUI_API ImFontAtlasRectId AddCustomRectFontGlyph(ImFont* font, ImWchar codepoint, int w, int h, float advance_x, const ImVec2& offset = ImVec2(0, 0)); // OBSOLETED in 1.92.X: Use custom ImFontLoader in ImFontConfig
+ IMGUI_API ImFontAtlasRectId AddCustomRectFontGlyphForSize(ImFont* font, float font_size, ImWchar codepoint, int w, int h, float advance_x, const ImVec2& offset = ImVec2(0, 0)); // ADDED AND OBSOLETED in 1.92.X
+#endif
+ //unsigned int FontBuilderFlags; // OBSOLETED in 1.92.X: Renamed to FontLoaderFlags.
+ //int TexDesiredWidth; // OBSOLETED in 1.92.X: Force texture width before calling Build(). Must be a power-of-two. If have many glyphs your graphics API have texture size restrictions you may want to increase texture width to decrease height.
+ //typedef ImFontAtlasRect ImFontAtlasCustomRect; // OBSOLETED in 1.92.X
+ //typedef ImFontAtlasCustomRect CustomRect; // OBSOLETED in 1.72+
+ //typedef ImFontGlyphRangesBuilder GlyphRangesBuilder; // OBSOLETED in 1.67+
};
-// Font runtime data and rendering
-// ImFontAtlas automatically loads a default embedded font for you when you call GetTexDataAsAlpha8() or GetTexDataAsRGBA32().
-struct ImFont
+// Font runtime data for a given size
+// Important: pointers to ImFontBaked are only valid for the current frame.
+struct ImFontBaked
{
// [Internal] Members: Hot ~20/24 bytes (for CalcTextSize)
ImVector IndexAdvanceX; // 12-16 // out // Sparse. Glyphs->AdvanceX in a directly indexable way (cache-friendly for CalcTextSize functions which only this info, and are often bottleneck in large UI).
- float FallbackAdvanceX; // 4 // out // = FallbackGlyph->AdvanceX
- float FontSize; // 4 // in // Height of characters/line, set during loading (don't change after loading)
+ float FallbackAdvanceX; // 4 // out // FindGlyph(FallbackChar)->AdvanceX
+ float Size; // 4 // in // Height of characters/line, set during loading (doesn't change after loading)
+ float RasterizerDensity; // 4 // in // Density this is baked at
- // [Internal] Members: Hot ~28/40 bytes (for RenderText loop)
+ // [Internal] Members: Hot ~28/36 bytes (for RenderText loop)
ImVector IndexLookup; // 12-16 // out // Sparse. Index glyphs by Unicode code-point.
ImVector Glyphs; // 12-16 // out // All glyphs.
- ImFontGlyph* FallbackGlyph; // 4-8 // out // = FindGlyph(FontFallbackChar)
+ int FallbackGlyphIndex; // 4 // out // Index of FontFallbackChar
- // [Internal] Members: Cold ~32/40 bytes
+ // [Internal] Members: Cold
+ float Ascent, Descent; // 4+4 // out // Ascent: distance from top to bottom of e.g. 'A' [0..FontSize] (unscaled)
+ unsigned int MetricsTotalSurface:26;// 3 // out // Total surface in pixels to get an idea of the font rasterization/texture cost (not exact, we approximate the cost of padding between glyphs)
+ unsigned int WantDestroy:1; // 0 // // Queued for destroy
+ unsigned int LoadNoFallback:1; // 0 // // Disable loading fallback in lower-level calls.
+ unsigned int LoadNoRenderOnLayout:1;// 0 // // Enable a two-steps mode where CalcTextSize() calls will load AdvanceX *without* rendering/packing glyphs. Only advantagous if you know that the glyph is unlikely to actually be rendered, otherwise it is slower because we'd do one query on the first CalcTextSize and one query on the first Draw.
+ int LastUsedFrame; // 4 // // Record of that time this was bounds
+ ImGuiID BakedId; // 4 // // Unique ID for this baked storage
+ ImFont* ContainerFont; // 4-8 // in // Parent font
+ void* FontLoaderDatas; // 4-8 // // Font loader opaque storage (per baked font * sources): single contiguous buffer allocated by imgui, passed to loader.
+
+ // Functions
+ IMGUI_API ImFontBaked();
+ IMGUI_API void ClearOutputData();
+ IMGUI_API ImFontGlyph* FindGlyph(ImWchar c); // Return U+FFFD glyph if requested glyph doesn't exists.
+ IMGUI_API ImFontGlyph* FindGlyphNoFallback(ImWchar c); // Return NULL if glyph doesn't exist
+ IMGUI_API float GetCharAdvance(ImWchar c);
+ IMGUI_API bool IsGlyphLoaded(ImWchar c);
+};
+
+// Font flags
+// (in future versions as we redesign font loading API, this will become more important and better documented. for now please consider this as internal/advanced use)
+enum ImFontFlags_
+{
+ ImFontFlags_None = 0,
+ ImFontFlags_NoLoadError = 1 << 1, // Disable throwing an error/assert when calling AddFontXXX() with missing file/data. Calling code is expected to check AddFontXXX() return value.
+ ImFontFlags_NoLoadGlyphs = 1 << 2, // [Internal] Disable loading new glyphs.
+ ImFontFlags_LockBakedSizes = 1 << 3, // [Internal] Disable loading new baked sizes, disable garbage collecting current ones. e.g. if you want to lock a font to a single size. Important: if you use this to preload given sizes, consider the possibility of multiple font density used on Retina display.
+};
+
+// Font runtime data and rendering
+// - ImFontAtlas automatically loads a default embedded font for you if you didn't load one manually.
+// - Since 1.92.X a font may be rendered as any size! Therefore a font doesn't have one specific size.
+// - Use 'font->GetFontBaked(size)' to retrieve the ImFontBaked* corresponding to a given size.
+// - If you used g.Font + g.FontSize (which is frequent from the ImGui layer), you can use g.FontBaked as a shortcut, as g.FontBaked == g.Font->GetFontBaked(g.FontSize).
+struct ImFont
+{
+ // [Internal] Members: Hot ~12-20 bytes
+ ImFontBaked* LastBaked; // 4-8 // Cache last bound baked. NEVER USE DIRECTLY. Use GetFontBaked().
+ ImFontAtlas* ContainerAtlas; // 4-8 // What we have been loaded into.
+ ImFontFlags Flags; // 4 // Font flags.
+ float CurrentRasterizerDensity; // Current rasterizer density. This is a varying state of the font.
+
+ // [Internal] Members: Cold ~24-52 bytes
// Conceptually Sources[] is the list of font sources merged to create this font.
- ImFontAtlas* ContainerAtlas; // 4-8 // out // What we has been loaded into
- ImFontConfig* Sources; // 4-8 // in // Pointer within ContainerAtlas->Sources[], to SourcesCount instances
- short SourcesCount; // 2 // in // Number of ImFontConfig involved in creating this font. Usually 1, or >1 when merging multiple font sources into one ImFont.
- short EllipsisCharCount; // 1 // out // 1 or 3
+ ImGuiID FontId; // Unique identifier for the font
+ float LegacySize; // 4 // in // Font size passed to AddFont(). Use for old code calling PushFont() expecting to use that size. (use ImGui::GetFontBaked() to get font baked at current bound size).
+ ImVector Sources; // 16 // in // List of sources. Pointers within ContainerAtlas->Sources[]
ImWchar EllipsisChar; // 2-4 // out // Character used for ellipsis rendering ('...').
ImWchar FallbackChar; // 2-4 // out // Character used if a glyph isn't found (U+FFFD, '?')
- float EllipsisWidth; // 4 // out // Total ellipsis Width
- float EllipsisCharStep; // 4 // out // Step between characters when EllipsisCount > 0
- float Scale; // 4 // in // Base font scale (1.0f), multiplied by the per-window font scale which you can adjust with SetWindowFontScale()
- float Ascent, Descent; // 4+4 // out // Ascent: distance from top to bottom of e.g. 'A' [0..FontSize] (unscaled)
- int MetricsTotalSurface;// 4 // out // Total surface in pixels to get an idea of the font rasterization/texture cost (not exact, we approximate the cost of padding between glyphs)
- bool DirtyLookupTables; // 1 // out //
ImU8 Used8kPagesMap[(IM_UNICODE_CODEPOINT_MAX+1)/8192/8]; // 1 bytes if ImWchar=ImWchar16, 16 bytes if ImWchar==ImWchar32. Store 1-bit for each block of 4K codepoints that has one active glyph. This is mainly used to facilitate iterations across all used codepoints.
+ bool EllipsisAutoBake; // 1 // // Mark when the "..." glyph needs to be generated.
+ ImGuiStorage RemapPairs; // 16 // // Remapping pairs when using AddRemapChar(), otherwise empty.
+#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
+ float Scale; // 4 // in // Legacy base font scale (~1.0f), multiplied by the per-window font scale which you can adjust with SetWindowFontScale()
+#endif
// Methods
IMGUI_API ImFont();
IMGUI_API ~ImFont();
- IMGUI_API ImFontGlyph* FindGlyph(ImWchar c);
- IMGUI_API ImFontGlyph* FindGlyphNoFallback(ImWchar c);
- float GetCharAdvance(ImWchar c) { return ((int)c < IndexAdvanceX.Size) ? IndexAdvanceX[(int)c] : FallbackAdvanceX; }
+ IMGUI_API bool IsGlyphInFont(ImWchar c);
bool IsLoaded() const { return ContainerAtlas != NULL; }
- const char* GetDebugName() const { return Sources ? Sources->Name : ""; }
+ const char* GetDebugName() const { return Sources.Size ? Sources[0]->Name : ""; } // Fill ImFontConfig::Name.
// [Internal] Don't use!
// 'max_width' stops rendering after a certain width (could be turned into a 2d size). FLT_MAX to disable.
// 'wrap_width' enable automatic word-wrapping across multiple lines to fit into given width. 0.0f to disable.
- IMGUI_API ImVec2 CalcTextSizeA(float size, float max_width, float wrap_width, const char* text_begin, const char* text_end = NULL, const char** remaining = NULL); // utf8
- IMGUI_API const char* CalcWordWrapPositionA(float scale, const char* text, const char* text_end, float wrap_width);
- IMGUI_API void RenderChar(ImDrawList* draw_list, float size, const ImVec2& pos, ImU32 col, ImWchar c);
- IMGUI_API void RenderText(ImDrawList* draw_list, float size, const ImVec2& pos, ImU32 col, const ImVec4& clip_rect, const char* text_begin, const char* text_end, float wrap_width = 0.0f, bool cpu_fine_clip = false);
+ IMGUI_API ImFontBaked* GetFontBaked(float font_size, float density = -1.0f); // Get or create baked data for given size
+ IMGUI_API ImVec2 CalcTextSizeA(float size, float max_width, float wrap_width, const char* text_begin, const char* text_end = NULL, const char** out_remaining = NULL);
+ IMGUI_API const char* CalcWordWrapPosition(float size, const char* text, const char* text_end, float wrap_width);
+ IMGUI_API void RenderChar(ImDrawList* draw_list, float size, const ImVec2& pos, ImU32 col, ImWchar c, const ImVec4* cpu_fine_clip = NULL);
+ IMGUI_API void RenderText(ImDrawList* draw_list, float size, const ImVec2& pos, ImU32 col, const ImVec4& clip_rect, const char* text_begin, const char* text_end, float wrap_width = 0.0f, ImDrawTextFlags flags = 0);
+#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
+ inline const char* CalcWordWrapPositionA(float scale, const char* text, const char* text_end, float wrap_width) { return CalcWordWrapPosition(LegacySize * scale, text, text_end, wrap_width); }
+#endif
// [Internal] Don't use!
- IMGUI_API void BuildLookupTable();
IMGUI_API void ClearOutputData();
- IMGUI_API void GrowIndex(int new_size);
- IMGUI_API void AddGlyph(const ImFontConfig* src_cfg, ImWchar c, float x0, float y0, float x1, float y1, float u0, float v0, float u1, float v1, float advance_x);
- IMGUI_API void AddRemapChar(ImWchar dst, ImWchar src, bool overwrite_dst = true); // Makes 'dst' character/glyph points to 'src' character/glyph. Currently needs to be called AFTER fonts have been built.
+ IMGUI_API void AddRemapChar(ImWchar from_codepoint, ImWchar to_codepoint); // Makes 'from_codepoint' character points to 'to_codepoint' glyph.
IMGUI_API bool IsGlyphRangeUnused(unsigned int c_begin, unsigned int c_last);
};
+// This is provided for consistency (but we don't actually use this)
+inline ImTextureID ImTextureRef::GetTexID() const
+{
+ IM_ASSERT(!(_TexData != NULL && _TexID != ImTextureID_Invalid));
+ return _TexData ? _TexData->TexID : _TexID;
+}
+
+// Using an indirection to avoid patching ImDrawCmd after a SetTexID() call (but this could be an alternative solution too)
+inline ImTextureID ImDrawCmd::GetTexID() const
+{
+ // If you are getting this assert: A renderer backend with support for ImGuiBackendFlags_RendererHasTextures (1.92)
+ // must iterate and handle ImTextureData requests stored in ImDrawData::Textures[].
+ ImTextureID tex_id = TexRef._TexData ? TexRef._TexData->TexID : TexRef._TexID; // == TexRef.GetTexID() above.
+ if (TexRef._TexData != NULL)
+ IM_ASSERT(tex_id != ImTextureID_Invalid && "ImDrawCmd is referring to ImTextureData that wasn't uploaded to graphics system. Backend must call ImTextureData::SetTexID() after handling ImTextureStatus_WantCreate request!");
+ return tex_id;
+}
+
//-----------------------------------------------------------------------------
// [SECTION] Viewports
//-----------------------------------------------------------------------------
@@ -3686,10 +4041,12 @@ struct ImGuiViewport
ImGuiViewportFlags Flags; // See ImGuiViewportFlags_
ImVec2 Pos; // Main Area: Position of the viewport (Dear ImGui coordinates are the same as OS desktop/native coordinates)
ImVec2 Size; // Main Area: Size of the viewport.
+ ImVec2 FramebufferScale; // Density of the viewport for Retina display (always 1,1 on Windows, may be 2,2 etc on macOS/iOS). This will affect font rasterizer density.
ImVec2 WorkPos; // Work Area: Position of the viewport minus task bars, menus bars, status bars (>= Pos)
ImVec2 WorkSize; // Work Area: Size of the viewport minus task bars, menu bars, status bars (<= Size)
float DpiScale; // 1.0f = 96 DPI = No extra scale.
ImGuiID ParentViewportId; // (Advanced) 0: no parent. Instruct the platform backend to setup a parent/child relationship between platform windows.
+ ImGuiViewport* ParentViewport; // (Advanced) Direct shortcut to ImGui::FindViewportByID(ParentViewportId). NULL: no parent.
ImDrawData* DrawData; // The ImDrawData corresponding to this viewport. Valid after Render() and until the next call to NewFrame().
// Platform/Backend Dependent Data
@@ -3798,6 +4155,10 @@ struct ImGuiPlatformIO
// Input - Interface with Renderer Backend
//------------------------------------------------------------------
+ // Optional: Maximum texture size supported by renderer (used to adjust how we size textures). 0 if not known.
+ int Renderer_TextureMaxWidth;
+ int Renderer_TextureMaxHeight;
+
// Written by some backends during ImGui_ImplXXXX_RenderDrawData() call to point backend_specific ImGui_ImplXXXX_RenderState* structure.
void* Renderer_RenderState;
@@ -3825,6 +4186,7 @@ struct ImGuiPlatformIO
ImVec2 (*Platform_GetWindowPos)(ImGuiViewport* vp); // N . . . . //
void (*Platform_SetWindowSize)(ImGuiViewport* vp, ImVec2 size); // . . U . . // Set platform window client area size (ignoring OS decorations such as OS title bar etc.)
ImVec2 (*Platform_GetWindowSize)(ImGuiViewport* vp); // N . . . . // Get platform window client area size
+ ImVec2 (*Platform_GetWindowFramebufferScale)(ImGuiViewport* vp); // N . . . . // Return viewport density. Always 1,1 on Windows, often 2,2 on Retina display on macOS/iOS. MUST BE INTEGER VALUES.
void (*Platform_SetWindowFocus)(ImGuiViewport* vp); // N . . . . // Move window to front and set input focus
bool (*Platform_GetWindowFocus)(ImGuiViewport* vp); // . . U . . //
bool (*Platform_GetWindowMinimized)(ImGuiViewport* vp); // N . . . . // Get platform window minimized state. When minimized, we generally won't attempt to get/set size and contents will be culled more easily
@@ -3851,12 +4213,23 @@ struct ImGuiPlatformIO
ImVector Monitors;
//------------------------------------------------------------------
- // Output - List of viewports to render into platform windows
+ // Output
//------------------------------------------------------------------
+ // Textures list (the list is updated by calling ImGui::EndFrame or ImGui::Render)
+ // The ImGui_ImplXXXX_RenderDrawData() function of each backend generally access this via ImDrawData::Textures which points to this. The array is available here mostly because backends will want to destroy textures on shutdown.
+ ImVector Textures; // List of textures used by Dear ImGui (most often 1) + contents of external texture list is automatically appended into this.
+
// Viewports list (the list is updated by calling ImGui::EndFrame or ImGui::Render)
// (in the future we will attempt to organize this feature to remove the need for a "main viewport")
ImVector Viewports; // Main viewports, followed by all secondary viewports.
+
+ //------------------------------------------------------------------
+ // Functions
+ //------------------------------------------------------------------
+
+ void ClearPlatformHandlers(); // Clear all Platform_XXX fields. Typically called on Platform Backend shutdown.
+ void ClearRendererHandlers(); // Clear all Renderer_XXX fields. Typically called on Renderer Backend shutdown.
};
// (Optional) This is required when enabling multi-viewport. Represent the bounds of each connected monitor/display and their DPI.
@@ -3870,12 +4243,14 @@ struct ImGuiPlatformMonitor
ImGuiPlatformMonitor() { MainPos = MainSize = WorkPos = WorkSize = ImVec2(0, 0); DpiScale = 1.0f; PlatformHandle = NULL; }
};
-// (Optional) Support for IME (Input Method Editor) via the platform_io.Platform_SetImeDataFn() function.
+// (Optional) Support for IME (Input Method Editor) via the platform_io.Platform_SetImeDataFn() function. Handler is called during EndFrame().
struct ImGuiPlatformImeData
{
- bool WantVisible; // A widget wants the IME to be visible
- ImVec2 InputPos; // Position of the input cursor
- float InputLineHeight; // Line height
+ bool WantVisible; // A widget wants the IME to be visible.
+ bool WantTextInput; // A widget wants text input, not necessarily IME to be visible. This is automatically set to the upcoming value of io.WantTextInput.
+ ImVec2 InputPos; // Position of input cursor (for IME).
+ float InputLineHeight; // Line height (for IME).
+ ImGuiID ViewportId; // ID of platform window/viewport.
ImGuiPlatformImeData() { memset(this, 0, sizeof(*this)); }
};
@@ -3889,31 +4264,34 @@ struct ImGuiPlatformImeData
#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
namespace ImGui
{
+ // OBSOLETED in 1.92.0 (from June 2025)
+ inline void PushFont(ImFont* font) { PushFont(font, font ? font->LegacySize : 0.0f); }
+ IMGUI_API void SetWindowFontScale(float scale); // Set font scale factor for current window. Prefer using PushFont(NULL, style.FontSizeBase * factor) or use style.FontScaleMain to scale all windows.
// OBSOLETED in 1.91.9 (from February 2025)
- IMGUI_API void Image(ImTextureID user_texture_id, const ImVec2& image_size, const ImVec2& uv0, const ImVec2& uv1, const ImVec4& tint_col, const ImVec4& border_col); // <-- border_col was removed in favor of ImGuiCol_ImageBorder.
+ IMGUI_API void Image(ImTextureRef tex_ref, const ImVec2& image_size, const ImVec2& uv0, const ImVec2& uv1, const ImVec4& tint_col, const ImVec4& border_col); // <-- 'border_col' was removed in favor of ImGuiCol_ImageBorder. If you use 'tint_col', use ImageWithBg() instead.
// OBSOLETED in 1.91.0 (from July 2024)
- static inline void PushButtonRepeat(bool repeat) { PushItemFlag(ImGuiItemFlags_ButtonRepeat, repeat); }
- static inline void PopButtonRepeat() { PopItemFlag(); }
- static inline void PushTabStop(bool tab_stop) { PushItemFlag(ImGuiItemFlags_NoTabStop, !tab_stop); }
- static inline void PopTabStop() { PopItemFlag(); }
+ inline void PushButtonRepeat(bool repeat) { PushItemFlag(ImGuiItemFlags_ButtonRepeat, repeat); }
+ inline void PopButtonRepeat() { PopItemFlag(); }
+ inline void PushTabStop(bool tab_stop) { PushItemFlag(ImGuiItemFlags_NoTabStop, !tab_stop); }
+ inline void PopTabStop() { PopItemFlag(); }
IMGUI_API ImVec2 GetContentRegionMax(); // Content boundaries max (e.g. window boundaries including scrolling, or current column boundaries). You should never need this. Always use GetCursorScreenPos() and GetContentRegionAvail()!
IMGUI_API ImVec2 GetWindowContentRegionMin(); // Content boundaries min for the window (roughly (0,0)-Scroll), in window-local coordinates. You should never need this. Always use GetCursorScreenPos() and GetContentRegionAvail()!
IMGUI_API ImVec2 GetWindowContentRegionMax(); // Content boundaries max for the window (roughly (0,0)+Size-Scroll), in window-local coordinates. You should never need this. Always use GetCursorScreenPos() and GetContentRegionAvail()!
// OBSOLETED in 1.90.0 (from September 2023)
- static inline bool BeginChildFrame(ImGuiID id, const ImVec2& size, ImGuiWindowFlags window_flags = 0) { return BeginChild(id, size, ImGuiChildFlags_FrameStyle, window_flags); }
- static inline void EndChildFrame() { EndChild(); }
- //static inline bool BeginChild(const char* str_id, const ImVec2& size_arg, bool borders, ImGuiWindowFlags window_flags){ return BeginChild(str_id, size_arg, borders ? ImGuiChildFlags_Borders : ImGuiChildFlags_None, window_flags); } // Unnecessary as true == ImGuiChildFlags_Borders
- //static inline bool BeginChild(ImGuiID id, const ImVec2& size_arg, bool borders, ImGuiWindowFlags window_flags) { return BeginChild(id, size_arg, borders ? ImGuiChildFlags_Borders : ImGuiChildFlags_None, window_flags); } // Unnecessary as true == ImGuiChildFlags_Borders
- static inline void ShowStackToolWindow(bool* p_open = NULL) { ShowIDStackToolWindow(p_open); }
+ inline bool BeginChildFrame(ImGuiID id, const ImVec2& size, ImGuiWindowFlags window_flags = 0) { return BeginChild(id, size, ImGuiChildFlags_FrameStyle, window_flags); }
+ inline void EndChildFrame() { EndChild(); }
+ //inline bool BeginChild(const char* str_id, const ImVec2& size_arg, bool borders, ImGuiWindowFlags window_flags){ return BeginChild(str_id, size_arg, borders ? ImGuiChildFlags_Borders : ImGuiChildFlags_None, window_flags); } // Unnecessary as true == ImGuiChildFlags_Borders
+ //inline bool BeginChild(ImGuiID id, const ImVec2& size_arg, bool borders, ImGuiWindowFlags window_flags) { return BeginChild(id, size_arg, borders ? ImGuiChildFlags_Borders : ImGuiChildFlags_None, window_flags); } // Unnecessary as true == ImGuiChildFlags_Borders
+ inline void ShowStackToolWindow(bool* p_open = NULL) { ShowIDStackToolWindow(p_open); }
IMGUI_API bool Combo(const char* label, int* current_item, bool (*old_callback)(void* user_data, int idx, const char** out_text), void* user_data, int items_count, int popup_max_height_in_items = -1);
IMGUI_API bool ListBox(const char* label, int* current_item, bool (*old_callback)(void* user_data, int idx, const char** out_text), void* user_data, int items_count, int height_in_items = -1);
// OBSOLETED in 1.89.7 (from June 2023)
IMGUI_API void SetItemAllowOverlap(); // Use SetNextItemAllowOverlap() before item.
- // OBSOLETED in 1.89.4 (from March 2023)
- static inline void PushAllowKeyboardFocus(bool tab_stop) { PushItemFlag(ImGuiItemFlags_NoTabStop, !tab_stop); }
- static inline void PopAllowKeyboardFocus() { PopItemFlag(); }
// Some of the older obsolete names along with their replacement (commented out so they are not reported in IDE)
+ //-- OBSOLETED in 1.89.4 (from March 2023)
+ //static inline void PushAllowKeyboardFocus(bool tab_stop) { PushItemFlag(ImGuiItemFlags_NoTabStop, !tab_stop); }
+ //static inline void PopAllowKeyboardFocus() { PopItemFlag(); }
//-- OBSOLETED in 1.89 (from August 2022)
//IMGUI_API bool ImageButton(ImTextureID user_texture_id, const ImVec2& size, const ImVec2& uv0 = ImVec2(0, 0), const ImVec2& uv1 = ImVec2(1, 1), int frame_padding = -1, const ImVec4& bg_col = ImVec4(0, 0, 0, 0), const ImVec4& tint_col = ImVec4(1, 1, 1, 1)); // --> Use new ImageButton() signature (explicit item id, regular FramePadding). Refer to code in 1.91 if you want to grab a copy of this version.
//-- OBSOLETED in 1.88 (from May 2022)
@@ -3976,6 +4354,25 @@ namespace ImGui
//static inline void SetScrollPosHere() { SetScrollHere(); } // OBSOLETED in 1.42
}
+//-- OBSOLETED in 1.92.x: ImFontAtlasCustomRect becomes ImTextureRect
+// - ImFontAtlasCustomRect::X,Y --> ImTextureRect::x,y
+// - ImFontAtlasCustomRect::Width,Height --> ImTextureRect::w,h
+// - ImFontAtlasCustomRect::GlyphColored --> if you need to write to this, instead you can write to 'font->Glyphs.back()->Colored' after calling AddCustomRectFontGlyph()
+// We could make ImTextureRect an union to use old names, but 1) this would be confusing 2) the fix is easy 3) ImFontAtlasCustomRect was always a rather esoteric api.
+typedef ImFontAtlasRect ImFontAtlasCustomRect;
+/*struct ImFontAtlasCustomRect
+{
+ unsigned short X, Y; // Output // Packed position in Atlas
+ unsigned short Width, Height; // Input // [Internal] Desired rectangle dimension
+ unsigned int GlyphID:31; // Input // [Internal] For custom font glyphs only (ID < 0x110000)
+ unsigned int GlyphColored:1; // Input // [Internal] For custom font glyphs only: glyph is colored, removed tinting.
+ float GlyphAdvanceX; // Input // [Internal] For custom font glyphs only: glyph xadvance
+ ImVec2 GlyphOffset; // Input // [Internal] For custom font glyphs only: glyph display offset
+ ImFont* Font; // Input // [Internal] For custom font glyphs only: target font
+ ImFontAtlasCustomRect() { X = Y = 0xFFFF; Width = Height = 0; GlyphID = 0; GlyphColored = 0; GlyphAdvanceX = 0.0f; GlyphOffset = ImVec2(0, 0); Font = NULL; }
+ bool IsPacked() const { return X != 0xFFFF; }
+};*/
+
//-- OBSOLETED in 1.82 (from Mars 2021): flags for AddRect(), AddRectFilled(), AddImageRounded(), PathRect()
//typedef ImDrawFlags ImDrawCornerFlags;
//enum ImDrawCornerFlags_
diff --git a/imgui_demo.cpp b/imgui_demo.cpp
index c28e94105..ff5281dfb 100644
--- a/imgui_demo.cpp
+++ b/imgui_demo.cpp
@@ -1,4 +1,4 @@
-// dear imgui, v1.92.0 WIP
+// dear imgui, v1.92.4 WIP
// (demo code)
// Help:
@@ -82,6 +82,7 @@ Index of this file:
// [SECTION] DemoWindowWidgetsDisableBlocks()
// [SECTION] DemoWindowWidgetsDragAndDrop()
// [SECTION] DemoWindowWidgetsDragsAndSliders()
+// [SECTION] DemoWindowWidgetsFonts()
// [SECTION] DemoWindowWidgetsImages()
// [SECTION] DemoWindowWidgetsListBoxes()
// [SECTION] DemoWindowWidgetsMultiComponents()
@@ -425,9 +426,19 @@ void ImGui::ShowDemoWindow(bool* p_open)
return;
}
- // Most "big" widgets share a common width settings by default. See 'Demo->Layout->Widgets Width' for details.
- ImGui::PushItemWidth(ImGui::GetFontSize() * -12); // e.g. Leave a fixed amount of width for labels (by passing a negative value), the rest goes to widgets.
- //ImGui::PushItemWidth(-ImGui::GetWindowWidth() * 0.35f); // e.g. Use 2/3 of the space for widgets and 1/3 for labels (right align)
+ // Most framed widgets share a common width settings. Remaining width is used for the label.
+ // The width of the frame may be changed with PushItemWidth() or SetNextItemWidth().
+ // - Positive value for absolute size, negative value for right-alignment.
+ // - The default value is about GetWindowWidth() * 0.65f.
+ // - See 'Demo->Layout->Widgets Width' for details.
+ // Here we change the frame width based on how much width we want to give to the label.
+ const float label_width_base = ImGui::GetFontSize() * 12; // Some amount of width for label, based on font size.
+ const float label_width_max = ImGui::GetContentRegionAvail().x * 0.40f; // ...but always leave some room for framed widgets.
+ const float label_width = IM_MIN(label_width_base, label_width_max);
+ ImGui::PushItemWidth(-label_width); // Right-align: framed items will leave 'label_width' available for the label.
+ //ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x * 0.40f); // e.g. Use 40% width for framed widgets, leaving 60% width for labels.
+ //ImGui::PushItemWidth(-ImGui::GetContentRegionAvail().x * 0.40f); // e.g. Use 40% width for labels, leaving 60% width for framed widgets.
+ //ImGui::PushItemWidth(ImGui::GetFontSize() * -12); // e.g. Use XXX width for labels, leaving the rest for framed widgets.
// Menu Bar
DemoWindowMenuBar(&demo_data);
@@ -540,14 +551,22 @@ void ImGui::ShowDemoWindow(bool* p_open)
ImGui::Checkbox("io.ConfigViewportsNoAutoMerge", &io.ConfigViewportsNoAutoMerge);
ImGui::SameLine(); HelpMarker("Set to make all floating imgui windows always create their own viewport. Otherwise, they are merged into the main host viewports when overlapping it.");
ImGui::Checkbox("io.ConfigViewportsNoTaskBarIcon", &io.ConfigViewportsNoTaskBarIcon);
- ImGui::SameLine(); HelpMarker("Toggling this at runtime is normally unsupported (most platform backends won't refresh the task bar icon state right away).");
+ ImGui::SameLine(); HelpMarker("(note: some platform backends may not reflect a change of this value for existing viewports, and may need the viewport to be recreated)");
ImGui::Checkbox("io.ConfigViewportsNoDecoration", &io.ConfigViewportsNoDecoration);
- ImGui::SameLine(); HelpMarker("Toggling this at runtime is normally unsupported (most platform backends won't refresh the decoration right away).");
+ ImGui::SameLine(); HelpMarker("(note: some platform backends may not reflect a change of this value for existing viewports, and may need the viewport to be recreated)");
ImGui::Checkbox("io.ConfigViewportsNoDefaultParent", &io.ConfigViewportsNoDefaultParent);
- ImGui::SameLine(); HelpMarker("Toggling this at runtime is normally unsupported (most platform backends won't refresh the parenting right away).");
+ ImGui::SameLine(); HelpMarker("(note: some platform backends may not reflect a change of this value for existing viewports, and may need the viewport to be recreated)");
+ ImGui::Checkbox("io.ConfigViewportsPlatformFocusSetsImGuiFocus", &io.ConfigViewportsPlatformFocusSetsImGuiFocus);
+ ImGui::SameLine(); HelpMarker("When a platform window is focused (e.g. using Alt+Tab, clicking Platform Title Bar), apply corresponding focus on imgui windows (may clear focus/active id from imgui windows location in other platform windows). In principle this is better enabled but we provide an opt-out, because some Linux window managers tend to eagerly focus windows (e.g. on mouse hover, or even a simple window pos/size change).");
ImGui::Unindent();
}
+ //ImGui::SeparatorText("DPI/Scaling");
+ //ImGui::Checkbox("io.ConfigDpiScaleFonts", &io.ConfigDpiScaleFonts);
+ //ImGui::SameLine(); HelpMarker("Experimental: Automatically update style.FontScaleDpi when Monitor DPI changes. This will scale fonts but NOT style sizes/padding for now.");
+ //ImGui::Checkbox("io.ConfigDpiScaleViewports", &io.ConfigDpiScaleViewports);
+ //ImGui::SameLine(); HelpMarker("Experimental: Scale Dear ImGui and Platform Windows when Monitor DPI changes.");
+
ImGui::SeparatorText("Windows");
ImGui::Checkbox("io.ConfigWindowsResizeFromEdges", &io.ConfigWindowsResizeFromEdges);
ImGui::SameLine(); HelpMarker("Enable resizing of windows from their edges and from the lower-left corner.\nThis requires ImGuiBackendFlags_HasMouseCursors for better mouse cursor feedback.");
@@ -621,7 +640,9 @@ void ImGui::ShowDemoWindow(bool* p_open)
ImGui::CheckboxFlags("io.BackendFlags: HasSetMousePos", &io.BackendFlags, ImGuiBackendFlags_HasSetMousePos);
ImGui::CheckboxFlags("io.BackendFlags: PlatformHasViewports", &io.BackendFlags, ImGuiBackendFlags_PlatformHasViewports);
ImGui::CheckboxFlags("io.BackendFlags: HasMouseHoveredViewport",&io.BackendFlags, ImGuiBackendFlags_HasMouseHoveredViewport);
+ ImGui::CheckboxFlags("io.BackendFlags: HasParentViewport", &io.BackendFlags, ImGuiBackendFlags_HasParentViewport);
ImGui::CheckboxFlags("io.BackendFlags: RendererHasVtxOffset", &io.BackendFlags, ImGuiBackendFlags_RendererHasVtxOffset);
+ ImGui::CheckboxFlags("io.BackendFlags: RendererHasTextures", &io.BackendFlags, ImGuiBackendFlags_RendererHasTextures);
ImGui::CheckboxFlags("io.BackendFlags: RendererHasViewports", &io.BackendFlags, ImGuiBackendFlags_RendererHasViewports);
ImGui::EndDisabled();
@@ -629,8 +650,8 @@ void ImGui::ShowDemoWindow(bool* p_open)
ImGui::Spacing();
}
- IMGUI_DEMO_MARKER("Configuration/Style");
- if (ImGui::TreeNode("Style"))
+ IMGUI_DEMO_MARKER("Configuration/Style, Fonts");
+ if (ImGui::TreeNode("Style, Fonts"))
{
ImGui::Checkbox("Style Editor", &demo_data.ShowStyleEditor);
ImGui::SameLine();
@@ -890,6 +911,9 @@ static void DemoWindowWidgetsBasic()
ImGui::RadioButton("radio b", &e, 1); ImGui::SameLine();
ImGui::RadioButton("radio c", &e, 2);
+ ImGui::AlignTextToFramePadding();
+ ImGui::TextLinkOpenURL("Hyperlink", "https://github.com/ocornut/imgui/wiki/Error-Handling");
+
// Color buttons, demonstrate using PushID() to add unique identifier in the ID stack, and changing style.
IMGUI_DEMO_MARKER("Widgets/Basic/Buttons (Colored)");
for (int i = 0; i < 7; i++)
@@ -1779,6 +1803,25 @@ static void DemoWindowWidgetsDragsAndSliders()
}
}
+//-----------------------------------------------------------------------------
+// [SECTION] DemoWindowWidgetsFonts()
+//-----------------------------------------------------------------------------
+
+// Forward declare ShowFontAtlas() which isn't worth putting in public API yet
+namespace ImGui { IMGUI_API void ShowFontAtlas(ImFontAtlas* atlas); }
+
+static void DemoWindowWidgetsFonts()
+{
+ IMGUI_DEMO_MARKER("Widgets/Fonts");
+ if (ImGui::TreeNode("Fonts"))
+ {
+ ImFontAtlas* atlas = ImGui::GetIO().Fonts;
+ ImGui::ShowFontAtlas(atlas);
+ // FIXME-NEWATLAS: Provide a demo to add/create a procedural font?
+ ImGui::TreePop();
+ }
+}
+
//-----------------------------------------------------------------------------
// [SECTION] DemoWindowWidgetsImages()
//-----------------------------------------------------------------------------
@@ -1795,13 +1838,12 @@ static void DemoWindowWidgetsImages()
"Hover the texture for a zoomed view!");
// Below we are displaying the font texture because it is the only texture we have access to inside the demo!
- // Remember that ImTextureID is just storage for whatever you want it to be. It is essentially a value that
- // will be passed to the rendering backend via the ImDrawCmd structure.
+ // Read description about ImTextureID/ImTextureRef and FAQ for details about texture identifiers.
// If you use one of the default imgui_impl_XXXX.cpp rendering backend, they all have comments at the top
- // of their respective source file to specify what they expect to be stored in ImTextureID, for example:
- // - The imgui_impl_dx11.cpp renderer expect a 'ID3D11ShaderResourceView*' pointer
+ // of their respective source file to specify what they are using as texture identifier, for example:
+ // - The imgui_impl_dx11.cpp renderer expect a 'ID3D11ShaderResourceView*' pointer.
// - The imgui_impl_opengl3.cpp renderer expect a GLuint OpenGL texture identifier, etc.
- // More:
+ // So with the DirectX11 backend, you call ImGui::Image() with a 'ID3D11ShaderResourceView*' cast to ImTextureID.
// - If you decided that ImTextureID = MyEngineTexture*, then you can pass your MyEngineTexture* pointers
// to ImGui::Image(), and gather width/height through your own functions, etc.
// - You can use ShowMetricsWindow() to inspect the draw data that are being passed to your renderer,
@@ -1809,14 +1851,19 @@ static void DemoWindowWidgetsImages()
// - Consider using the lower-level ImDrawList::AddImage() API, via ImGui::GetWindowDrawList()->AddImage().
// - Read https://github.com/ocornut/imgui/blob/master/docs/FAQ.md
// - Read https://github.com/ocornut/imgui/wiki/Image-Loading-and-Displaying-Examples
- ImTextureID my_tex_id = io.Fonts->TexID;
- float my_tex_w = (float)io.Fonts->TexWidth;
- float my_tex_h = (float)io.Fonts->TexHeight;
+
+ // Grab the current texture identifier used by the font atlas.
+ ImTextureRef my_tex_id = io.Fonts->TexRef;
+
+ // Regular user code should never have to care about TexData-> fields, but since we want to display the entire texture here, we pull Width/Height from it.
+ float my_tex_w = (float)io.Fonts->TexData->Width;
+ float my_tex_h = (float)io.Fonts->TexData->Height;
+
{
ImGui::Text("%.0fx%.0f", my_tex_w, my_tex_h);
ImVec2 pos = ImGui::GetCursorScreenPos();
- ImVec2 uv_min = ImVec2(0.0f, 0.0f); // Top-left
- ImVec2 uv_max = ImVec2(1.0f, 1.0f); // Lower-right
+ ImVec2 uv_min = ImVec2(0.0f, 0.0f); // Top-left
+ ImVec2 uv_max = ImVec2(1.0f, 1.0f); // Lower-right
ImGui::PushStyleVar(ImGuiStyleVar_ImageBorderSize, IM_MAX(1.0f, ImGui::GetStyle().ImageBorderSize));
ImGui::ImageWithBg(my_tex_id, ImVec2(my_tex_w, my_tex_h), uv_min, uv_max, ImVec4(0.0f, 0.0f, 0.0f, 1.0f));
if (ImGui::BeginItemTooltip())
@@ -2192,7 +2239,7 @@ static void DemoWindowWidgetsQueryingStatuses()
);
ImGui::BulletText(
"with Hovering Delay or Stationary test:\n"
- "IsItemHovered() = = %d\n"
+ "IsItemHovered() = %d\n"
"IsItemHovered(_Stationary) = %d\n"
"IsItemHovered(_DelayShort) = %d\n"
"IsItemHovered(_DelayNormal) = %d\n"
@@ -2566,7 +2613,7 @@ struct ExampleDualListBox
{
const int* a = (const int*)lhs;
const int* b = (const int*)rhs;
- return (*a - *b) > 0 ? +1 : -1;
+ return (*a - *b);
}
void SortItems(int n)
{
@@ -2574,7 +2621,7 @@ struct ExampleDualListBox
}
void Show()
{
- //ImGui::Checkbox("Sorted", &OptKeepSorted);
+ //if (ImGui::Checkbox("Sorted", &OptKeepSorted) && OptKeepSorted) { SortItems(0); SortItems(1); }
if (ImGui::BeginTable("split", 3, ImGuiTableFlags_None))
{
ImGui::TableSetupColumn("", ImGuiTableColumnFlags_WidthStretch); // Left side
@@ -3046,7 +3093,7 @@ static void DemoWindowWidgetsSelectionAndMultiSelect(ImGuiDemoWindowData* demo_d
static void DrawNode(ExampleTreeNode* node, ImGuiSelectionBasicStorage* selection)
{
ImGuiTreeNodeFlags tree_node_flags = ImGuiTreeNodeFlags_SpanAvailWidth | ImGuiTreeNodeFlags_OpenOnArrow | ImGuiTreeNodeFlags_OpenOnDoubleClick;
- tree_node_flags |= ImGuiTreeNodeFlags_NavLeftJumpsBackHere; // Enable pressing left to jump to parent
+ tree_node_flags |= ImGuiTreeNodeFlags_NavLeftJumpsToParent; // Enable pressing left to jump to parent
if (node->Childs.Size == 0)
tree_node_flags |= ImGuiTreeNodeFlags_Bullet | ImGuiTreeNodeFlags_Leaf;
if (selection->Contains((ImGuiID)node->UID))
@@ -3266,7 +3313,7 @@ static void DemoWindowWidgetsSelectionAndMultiSelect(ImGuiDemoWindowData* demo_d
ImGui::BeginTable("##Split", 2, ImGuiTableFlags_Resizable | ImGuiTableFlags_NoSavedSettings | ImGuiTableFlags_NoPadOuterX);
ImGui::TableSetupColumn("", ImGuiTableColumnFlags_WidthStretch, 0.70f);
ImGui::TableSetupColumn("", ImGuiTableColumnFlags_WidthStretch, 0.30f);
- //ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacingY, 0.0f);
+ //ImGui::PushStyleVarY(ImGuiStyleVar_ItemSpacing, 0.0f);
}
ImGuiListClipper clipper;
@@ -3416,6 +3463,18 @@ static void DemoWindowWidgetsSelectionAndMultiSelect(ImGuiDemoWindowData* demo_d
// [SECTION] DemoWindowWidgetsTabs()
//-----------------------------------------------------------------------------
+static void EditTabBarFittingPolicyFlags(ImGuiTabBarFlags* p_flags)
+{
+ if ((*p_flags & ImGuiTabBarFlags_FittingPolicyMask_) == 0)
+ *p_flags |= ImGuiTabBarFlags_FittingPolicyDefault_;
+ if (ImGui::CheckboxFlags("ImGuiTabBarFlags_FittingPolicyMixed", p_flags, ImGuiTabBarFlags_FittingPolicyMixed))
+ *p_flags &= ~(ImGuiTabBarFlags_FittingPolicyMask_ ^ ImGuiTabBarFlags_FittingPolicyMixed);
+ if (ImGui::CheckboxFlags("ImGuiTabBarFlags_FittingPolicyShrink", p_flags, ImGuiTabBarFlags_FittingPolicyShrink))
+ *p_flags &= ~(ImGuiTabBarFlags_FittingPolicyMask_ ^ ImGuiTabBarFlags_FittingPolicyShrink);
+ if (ImGui::CheckboxFlags("ImGuiTabBarFlags_FittingPolicyScroll", p_flags, ImGuiTabBarFlags_FittingPolicyScroll))
+ *p_flags &= ~(ImGuiTabBarFlags_FittingPolicyMask_ ^ ImGuiTabBarFlags_FittingPolicyScroll);
+}
+
static void DemoWindowWidgetsTabs()
{
IMGUI_DEMO_MARKER("Widgets/Tabs");
@@ -3458,12 +3517,7 @@ static void DemoWindowWidgetsTabs()
ImGui::CheckboxFlags("ImGuiTabBarFlags_TabListPopupButton", &tab_bar_flags, ImGuiTabBarFlags_TabListPopupButton);
ImGui::CheckboxFlags("ImGuiTabBarFlags_NoCloseWithMiddleMouseButton", &tab_bar_flags, ImGuiTabBarFlags_NoCloseWithMiddleMouseButton);
ImGui::CheckboxFlags("ImGuiTabBarFlags_DrawSelectedOverline", &tab_bar_flags, ImGuiTabBarFlags_DrawSelectedOverline);
- if ((tab_bar_flags & ImGuiTabBarFlags_FittingPolicyMask_) == 0)
- tab_bar_flags |= ImGuiTabBarFlags_FittingPolicyDefault_;
- if (ImGui::CheckboxFlags("ImGuiTabBarFlags_FittingPolicyResizeDown", &tab_bar_flags, ImGuiTabBarFlags_FittingPolicyResizeDown))
- tab_bar_flags &= ~(ImGuiTabBarFlags_FittingPolicyMask_ ^ ImGuiTabBarFlags_FittingPolicyResizeDown);
- if (ImGui::CheckboxFlags("ImGuiTabBarFlags_FittingPolicyScroll", &tab_bar_flags, ImGuiTabBarFlags_FittingPolicyScroll))
- tab_bar_flags &= ~(ImGuiTabBarFlags_FittingPolicyMask_ ^ ImGuiTabBarFlags_FittingPolicyScroll);
+ EditTabBarFittingPolicyFlags(&tab_bar_flags);
// Tab Bar
ImGui::AlignTextToFramePadding();
@@ -3512,12 +3566,8 @@ static void DemoWindowWidgetsTabs()
ImGui::Checkbox("Show Trailing TabItemButton()", &show_trailing_button);
// Expose some other flags which are useful to showcase how they interact with Leading/Trailing tabs
- static ImGuiTabBarFlags tab_bar_flags = ImGuiTabBarFlags_AutoSelectNewTabs | ImGuiTabBarFlags_Reorderable | ImGuiTabBarFlags_FittingPolicyResizeDown;
- ImGui::CheckboxFlags("ImGuiTabBarFlags_TabListPopupButton", &tab_bar_flags, ImGuiTabBarFlags_TabListPopupButton);
- if (ImGui::CheckboxFlags("ImGuiTabBarFlags_FittingPolicyResizeDown", &tab_bar_flags, ImGuiTabBarFlags_FittingPolicyResizeDown))
- tab_bar_flags &= ~(ImGuiTabBarFlags_FittingPolicyMask_ ^ ImGuiTabBarFlags_FittingPolicyResizeDown);
- if (ImGui::CheckboxFlags("ImGuiTabBarFlags_FittingPolicyScroll", &tab_bar_flags, ImGuiTabBarFlags_FittingPolicyScroll))
- tab_bar_flags &= ~(ImGuiTabBarFlags_FittingPolicyMask_ ^ ImGuiTabBarFlags_FittingPolicyScroll);
+ static ImGuiTabBarFlags tab_bar_flags = ImGuiTabBarFlags_AutoSelectNewTabs | ImGuiTabBarFlags_Reorderable | ImGuiTabBarFlags_FittingPolicyShrink;
+ EditTabBarFittingPolicyFlags(&tab_bar_flags);
if (ImGui::BeginTabBar("MyTabBar", tab_bar_flags))
{
@@ -3585,6 +3635,43 @@ static void DemoWindowWidgetsText()
ImGui::TreePop();
}
+ IMGUI_DEMO_MARKER("Widgets/Text/Font Size");
+ if (ImGui::TreeNode("Font Size"))
+ {
+ ImGuiStyle& style = ImGui::GetStyle();
+ const float global_scale = style.FontScaleMain * style.FontScaleDpi;
+ ImGui::Text("style.FontScaleMain = %0.2f", style.FontScaleMain);
+ ImGui::Text("style.FontScaleDpi = %0.2f", style.FontScaleDpi);
+ ImGui::Text("global_scale = ~%0.2f", global_scale); // This is not technically accurate as internal scales may apply, but conceptually let's pretend it is.
+ ImGui::Text("FontSize = %0.2f", ImGui::GetFontSize());
+
+ ImGui::SeparatorText("");
+ static float custom_size = 16.0f;
+ ImGui::SliderFloat("custom_size", &custom_size, 10.0f, 100.0f, "%.0f");
+ ImGui::Text("ImGui::PushFont(nullptr, custom_size);");
+ ImGui::PushFont(NULL, custom_size);
+ ImGui::Text("FontSize = %.2f (== %.2f * global_scale)", ImGui::GetFontSize(), custom_size);
+ ImGui::PopFont();
+
+ ImGui::SeparatorText("");
+ static float custom_scale = 1.0f;
+ ImGui::SliderFloat("custom_scale", &custom_scale, 0.5f, 4.0f, "%.2f");
+ ImGui::Text("ImGui::PushFont(nullptr, style.FontSizeBase * custom_scale);");
+ ImGui::PushFont(NULL, style.FontSizeBase * custom_scale);
+ ImGui::Text("FontSize = %.2f (== style.FontSizeBase * %.2f * global_scale)", ImGui::GetFontSize(), custom_scale);
+ ImGui::PopFont();
+
+ ImGui::SeparatorText("");
+ for (float scaling = 0.5f; scaling <= 4.0f; scaling += 0.5f)
+ {
+ ImGui::PushFont(NULL, style.FontSizeBase * scaling);
+ ImGui::Text("FontSize = %.2f (== style.FontSizeBase * %.2f * global_scale)", ImGui::GetFontSize(), scaling);
+ ImGui::PopFont();
+ }
+
+ ImGui::TreePop();
+ }
+
IMGUI_DEMO_MARKER("Widgets/Text/Word Wrapping");
if (ImGui::TreeNode("Word Wrapping"))
{
@@ -3705,6 +3792,8 @@ static void DemoWindowWidgetsTextInput()
static ImGuiInputTextFlags flags = ImGuiInputTextFlags_AllowTabInput;
HelpMarker("You can use the ImGuiInputTextFlags_CallbackResize facility if you need to wire InputTextMultiline() to a dynamic string type. See misc/cpp/imgui_stdlib.h for an example. (This is not demonstrated in imgui_demo.cpp because we don't want to include in here)");
ImGui::CheckboxFlags("ImGuiInputTextFlags_ReadOnly", &flags, ImGuiInputTextFlags_ReadOnly);
+ ImGui::CheckboxFlags("ImGuiInputTextFlags_WordWrap", &flags, ImGuiInputTextFlags_WordWrap);
+ ImGui::SameLine(); HelpMarker("Feature is currently in Beta. Please read comments in imgui.h");
ImGui::CheckboxFlags("ImGuiInputTextFlags_AllowTabInput", &flags, ImGuiInputTextFlags_AllowTabInput);
ImGui::SameLine(); HelpMarker("When _AllowTabInput is set, passing through the widget with Tabbing doesn't automatically activate it, in order to also cycling through subsequent widgets.");
ImGui::CheckboxFlags("ImGuiInputTextFlags_CtrlEnterForNewLine", &flags, ImGuiInputTextFlags_CtrlEnterForNewLine);
@@ -3734,13 +3823,13 @@ static void DemoWindowWidgetsTextInput()
}
};
- static char buf1[32] = ""; ImGui::InputText("default", buf1, 32);
- static char buf2[32] = ""; ImGui::InputText("decimal", buf2, 32, ImGuiInputTextFlags_CharsDecimal);
- static char buf3[32] = ""; ImGui::InputText("hexadecimal", buf3, 32, ImGuiInputTextFlags_CharsHexadecimal | ImGuiInputTextFlags_CharsUppercase);
- static char buf4[32] = ""; ImGui::InputText("uppercase", buf4, 32, ImGuiInputTextFlags_CharsUppercase);
- static char buf5[32] = ""; ImGui::InputText("no blank", buf5, 32, ImGuiInputTextFlags_CharsNoBlank);
- static char buf6[32] = ""; ImGui::InputText("casing swap", buf6, 32, ImGuiInputTextFlags_CallbackCharFilter, TextFilters::FilterCasingSwap); // Use CharFilter callback to replace characters.
- static char buf7[32] = ""; ImGui::InputText("\"imgui\"", buf7, 32, ImGuiInputTextFlags_CallbackCharFilter, TextFilters::FilterImGuiLetters); // Use CharFilter callback to disable some characters.
+ static char buf1[32] = ""; ImGui::InputText("default", buf1, IM_ARRAYSIZE(buf1));
+ static char buf2[32] = ""; ImGui::InputText("decimal", buf2, IM_ARRAYSIZE(buf2), ImGuiInputTextFlags_CharsDecimal);
+ static char buf3[32] = ""; ImGui::InputText("hexadecimal", buf3, IM_ARRAYSIZE(buf3), ImGuiInputTextFlags_CharsHexadecimal | ImGuiInputTextFlags_CharsUppercase);
+ static char buf4[32] = ""; ImGui::InputText("uppercase", buf4, IM_ARRAYSIZE(buf4), ImGuiInputTextFlags_CharsUppercase);
+ static char buf5[32] = ""; ImGui::InputText("no blank", buf5, IM_ARRAYSIZE(buf5), ImGuiInputTextFlags_CharsNoBlank);
+ static char buf6[32] = ""; ImGui::InputText("casing swap", buf6, IM_ARRAYSIZE(buf6), ImGuiInputTextFlags_CallbackCharFilter, TextFilters::FilterCasingSwap); // Use CharFilter callback to replace characters.
+ static char buf7[32] = ""; ImGui::InputText("\"imgui\"", buf7, IM_ARRAYSIZE(buf7), ImGuiInputTextFlags_CallbackCharFilter, TextFilters::FilterImGuiLetters); // Use CharFilter callback to disable some characters.
ImGui::TreePop();
}
@@ -3796,20 +3885,20 @@ static void DemoWindowWidgetsTextInput()
}
};
static char buf1[64];
- ImGui::InputText("Completion", buf1, 64, ImGuiInputTextFlags_CallbackCompletion, Funcs::MyCallback);
+ ImGui::InputText("Completion", buf1, IM_ARRAYSIZE(buf1), ImGuiInputTextFlags_CallbackCompletion, Funcs::MyCallback);
ImGui::SameLine(); HelpMarker(
"Here we append \"..\" each time Tab is pressed. "
"See 'Examples>Console' for a more meaningful demonstration of using this callback.");
static char buf2[64];
- ImGui::InputText("History", buf2, 64, ImGuiInputTextFlags_CallbackHistory, Funcs::MyCallback);
+ ImGui::InputText("History", buf2, IM_ARRAYSIZE(buf2), ImGuiInputTextFlags_CallbackHistory, Funcs::MyCallback);
ImGui::SameLine(); HelpMarker(
"Here we replace and select text each time Up/Down are pressed. "
"See 'Examples>Console' for a more meaningful demonstration of using this callback.");
static char buf3[64];
static int edit_count = 0;
- ImGui::InputText("Edit", buf3, 64, ImGuiInputTextFlags_CallbackEdit, Funcs::MyCallback, (void*)&edit_count);
+ ImGui::InputText("Edit", buf3, IM_ARRAYSIZE(buf3), ImGuiInputTextFlags_CallbackEdit, Funcs::MyCallback, (void*)&edit_count);
ImGui::SameLine(); HelpMarker(
"Here we toggle the casing of the first character on every edit + count edits.");
ImGui::SameLine(); ImGui::Text("(%d)", edit_count);
@@ -3852,10 +3941,13 @@ static void DemoWindowWidgetsTextInput()
// For this demo we are using ImVector as a string container.
// Note that because we need to store a terminating zero character, our size/capacity are 1 more
// than usually reported by a typical string class.
+ static ImGuiInputTextFlags flags = ImGuiInputTextFlags_None;
+ ImGui::CheckboxFlags("ImGuiInputTextFlags_WordWrap", &flags, ImGuiInputTextFlags_WordWrap);
+
static ImVector my_str;
if (my_str.empty())
my_str.push_back(0);
- Funcs::MyInputTextMultiline("##MyStr", &my_str, ImVec2(-FLT_MIN, ImGui::GetTextLineHeight() * 16));
+ Funcs::MyInputTextMultiline("##MyStr", &my_str, ImVec2(-FLT_MIN, ImGui::GetTextLineHeight() * 16), flags);
ImGui::Text("Data: %p\nSize: %d\nCapacity: %d", (void*)my_str.begin(), my_str.size(), my_str.capacity());
ImGui::TreePop();
}
@@ -3995,6 +4087,7 @@ static void DemoWindowWidgetsTreeNodes()
IMGUI_DEMO_MARKER("Widgets/Tree Nodes");
if (ImGui::TreeNode("Tree Nodes"))
{
+ // See see "Examples -> Property Editor" (ShowExampleAppPropertyEditor() function) for a fancier, data-driven tree.
IMGUI_DEMO_MARKER("Widgets/Tree Nodes/Basic trees");
if (ImGui::TreeNode("Basic trees"))
{
@@ -4021,6 +4114,35 @@ static void DemoWindowWidgetsTreeNodes()
ImGui::TreePop();
}
+ IMGUI_DEMO_MARKER("Widgets/Tree Nodes/Hierarchy lines");
+ if (ImGui::TreeNode("Hierarchy lines"))
+ {
+ static ImGuiTreeNodeFlags base_flags = ImGuiTreeNodeFlags_DrawLinesFull | ImGuiTreeNodeFlags_DefaultOpen;
+ HelpMarker("Default option for DrawLinesXXX is stored in style.TreeLinesFlags");
+ ImGui::CheckboxFlags("ImGuiTreeNodeFlags_DrawLinesNone", &base_flags, ImGuiTreeNodeFlags_DrawLinesNone);
+ ImGui::CheckboxFlags("ImGuiTreeNodeFlags_DrawLinesFull", &base_flags, ImGuiTreeNodeFlags_DrawLinesFull);
+ ImGui::CheckboxFlags("ImGuiTreeNodeFlags_DrawLinesToNodes", &base_flags, ImGuiTreeNodeFlags_DrawLinesToNodes);
+
+ if (ImGui::TreeNodeEx("Parent", base_flags))
+ {
+ if (ImGui::TreeNodeEx("Child 1", base_flags))
+ {
+ ImGui::Button("Button for Child 1");
+ ImGui::TreePop();
+ }
+ if (ImGui::TreeNodeEx("Child 2", base_flags))
+ {
+ ImGui::Button("Button for Child 2");
+ ImGui::TreePop();
+ }
+ ImGui::Text("Remaining contents");
+ ImGui::Text("Remaining contents");
+ ImGui::TreePop();
+ }
+
+ ImGui::TreePop();
+ }
+
IMGUI_DEMO_MARKER("Widgets/Tree Nodes/Advanced, with Selectable nodes");
if (ImGui::TreeNode("Advanced, with Selectable nodes"))
{
@@ -4038,7 +4160,13 @@ static void DemoWindowWidgetsTreeNodes()
ImGui::CheckboxFlags("ImGuiTreeNodeFlags_SpanAllColumns", &base_flags, ImGuiTreeNodeFlags_SpanAllColumns); ImGui::SameLine(); HelpMarker("For use in Tables only.");
ImGui::CheckboxFlags("ImGuiTreeNodeFlags_AllowOverlap", &base_flags, ImGuiTreeNodeFlags_AllowOverlap);
ImGui::CheckboxFlags("ImGuiTreeNodeFlags_Framed", &base_flags, ImGuiTreeNodeFlags_Framed); ImGui::SameLine(); HelpMarker("Draw frame with background (e.g. for CollapsingHeader)");
- ImGui::CheckboxFlags("ImGuiTreeNodeFlags_NavLeftJumpsBackHere", &base_flags, ImGuiTreeNodeFlags_NavLeftJumpsBackHere);
+ ImGui::CheckboxFlags("ImGuiTreeNodeFlags_NavLeftJumpsToParent", &base_flags, ImGuiTreeNodeFlags_NavLeftJumpsToParent);
+
+ HelpMarker("Default option for DrawLinesXXX is stored in style.TreeLinesFlags");
+ ImGui::CheckboxFlags("ImGuiTreeNodeFlags_DrawLinesNone", &base_flags, ImGuiTreeNodeFlags_DrawLinesNone);
+ ImGui::CheckboxFlags("ImGuiTreeNodeFlags_DrawLinesFull", &base_flags, ImGuiTreeNodeFlags_DrawLinesFull);
+ ImGui::CheckboxFlags("ImGuiTreeNodeFlags_DrawLinesToNodes", &base_flags, ImGuiTreeNodeFlags_DrawLinesToNodes);
+
ImGui::Checkbox("Align label with current X position", &align_label_with_current_x_position);
ImGui::Checkbox("Test tree node as drag source", &test_drag_and_drop);
ImGui::Text("Hello!");
@@ -4221,6 +4349,7 @@ static void DemoWindowWidgets(ImGuiDemoWindowData* demo_data)
DemoWindowWidgetsDragAndDrop();
DemoWindowWidgetsDragsAndSliders();
+ DemoWindowWidgetsFonts();
DemoWindowWidgetsImages();
DemoWindowWidgetsListBoxes();
DemoWindowWidgetsMultiComponents();
@@ -4445,16 +4574,27 @@ static void DemoWindowLayout()
}
ImGui::PopItemWidth();
+ ImGui::Text("SetNextItemWidth/PushItemWidth(-Min(GetContentRegionAvail().x * 0.40f, GetFontSize() * 12))");
+ ImGui::PushItemWidth(-IM_MIN(ImGui::GetFontSize() * 12, ImGui::GetContentRegionAvail().x * 0.40f));
+ ImGui::DragFloat("float##5a", &f);
+ if (show_indented_items)
+ {
+ ImGui::Indent();
+ ImGui::DragFloat("float (indented)##5b", &f);
+ ImGui::Unindent();
+ }
+ ImGui::PopItemWidth();
+
// Demonstrate using PushItemWidth to surround three items.
// Calling SetNextItemWidth() before each of them would have the same effect.
ImGui::Text("SetNextItemWidth/PushItemWidth(-FLT_MIN)");
ImGui::SameLine(); HelpMarker("Align to right edge");
ImGui::PushItemWidth(-FLT_MIN);
- ImGui::DragFloat("##float5a", &f);
+ ImGui::DragFloat("##float6a", &f);
if (show_indented_items)
{
ImGui::Indent();
- ImGui::DragFloat("float (indented)##5b", &f);
+ ImGui::DragFloat("float (indented)##6b", &f);
ImGui::Unindent();
}
ImGui::PopItemWidth();
@@ -4682,10 +4822,11 @@ static void DemoWindowLayout()
ImGui::SmallButton("SmallButton()");
// Tree
+ // (here the node appears after a button and has odd intent, so we use ImGuiTreeNodeFlags_DrawLinesNone to disable hierarchy outline)
const float spacing = ImGui::GetStyle().ItemInnerSpacing.x;
ImGui::Button("Button##1");
ImGui::SameLine(0.0f, spacing);
- if (ImGui::TreeNode("Node##1"))
+ if (ImGui::TreeNodeEx("Node##1", ImGuiTreeNodeFlags_DrawLinesNone))
{
// Placeholder tree data
for (int i = 0; i < 6; i++)
@@ -5130,7 +5271,7 @@ static void DemoWindowPopups()
// Typical use for regular windows:
// bool my_tool_is_active = false; if (ImGui::Button("Open")) my_tool_is_active = true; [...] if (my_tool_is_active) Begin("My Tool", &my_tool_is_active) { [...] } End();
// Typical use for popups:
- // if (ImGui::Button("Open")) ImGui::OpenPopup("MyPopup"); if (ImGui::BeginPopup("MyPopup") { [...] EndPopup(); }
+ // if (ImGui::Button("Open")) ImGui::OpenPopup("MyPopup"); if (ImGui::BeginPopup("MyPopup")) { [...] EndPopup(); }
// With popups we have to go through a library call (here OpenPopup) to manipulate the visibility state.
// This may be a bit confusing at first but it should quickly make sense. Follow on the examples below.
@@ -5716,7 +5857,7 @@ static void DemoWindowTables()
ImGui::SameLine(); ImGui::RadioButton("Text", &contents_type, CT_Text);
ImGui::SameLine(); ImGui::RadioButton("FillButton", &contents_type, CT_FillButton);
ImGui::Checkbox("Display headers", &display_headers);
- ImGui::CheckboxFlags("ImGuiTableFlags_NoBordersInBody", &flags, ImGuiTableFlags_NoBordersInBody); ImGui::SameLine(); HelpMarker("Disable vertical borders in columns Body (borders will always appear in Headers");
+ ImGui::CheckboxFlags("ImGuiTableFlags_NoBordersInBody", &flags, ImGuiTableFlags_NoBordersInBody); ImGui::SameLine(); HelpMarker("Disable vertical borders in columns Body (borders will always appear in Headers)");
PopStyleCompact();
if (ImGui::BeginTable("table1", 3, flags))
@@ -6667,7 +6808,7 @@ static void DemoWindowTables()
{
static ImGuiTableFlags table_flags = ImGuiTableFlags_BordersV | ImGuiTableFlags_BordersOuterH | ImGuiTableFlags_Resizable | ImGuiTableFlags_RowBg | ImGuiTableFlags_NoBordersInBody;
- static ImGuiTreeNodeFlags tree_node_flags_base = ImGuiTreeNodeFlags_SpanAllColumns;
+ static ImGuiTreeNodeFlags tree_node_flags_base = ImGuiTreeNodeFlags_SpanAllColumns | ImGuiTreeNodeFlags_DefaultOpen | ImGuiTreeNodeFlags_DrawLinesFull;
ImGui::CheckboxFlags("ImGuiTreeNodeFlags_SpanFullWidth", &tree_node_flags_base, ImGuiTreeNodeFlags_SpanFullWidth);
ImGui::CheckboxFlags("ImGuiTreeNodeFlags_SpanLabelWidth", &tree_node_flags_base, ImGuiTreeNodeFlags_SpanLabelWidth);
ImGui::CheckboxFlags("ImGuiTreeNodeFlags_SpanAllColumns", &tree_node_flags_base, ImGuiTreeNodeFlags_SpanAllColumns);
@@ -7209,7 +7350,7 @@ static void DemoWindowTables()
ImGui::CheckboxFlags("ImGuiTableFlags_BordersH", &flags, ImGuiTableFlags_BordersH);
ImGui::CheckboxFlags("ImGuiTableFlags_BordersOuterH", &flags, ImGuiTableFlags_BordersOuterH);
ImGui::CheckboxFlags("ImGuiTableFlags_BordersInnerH", &flags, ImGuiTableFlags_BordersInnerH);
- ImGui::CheckboxFlags("ImGuiTableFlags_NoBordersInBody", &flags, ImGuiTableFlags_NoBordersInBody); ImGui::SameLine(); HelpMarker("Disable vertical borders in columns Body (borders will always appear in Headers");
+ ImGui::CheckboxFlags("ImGuiTableFlags_NoBordersInBody", &flags, ImGuiTableFlags_NoBordersInBody); ImGui::SameLine(); HelpMarker("Disable vertical borders in columns Body (borders will always appear in Headers)");
ImGui::CheckboxFlags("ImGuiTableFlags_NoBordersInBodyUntilResize", &flags, ImGuiTableFlags_NoBordersInBodyUntilResize); ImGui::SameLine(); HelpMarker("Disable vertical borders in columns Body until hovered for resize (borders will always appear in Headers)");
ImGui::TreePop();
}
@@ -8015,6 +8156,8 @@ void ImGui::ShowAboutWindow(bool* p_open)
ImGui::SameLine();
ImGui::TextLinkOpenURL("Wiki", "https://github.com/ocornut/imgui/wiki");
ImGui::SameLine();
+ ImGui::TextLinkOpenURL("Extensions", "https://github.com/ocornut/imgui/wiki/Useful-Extensions");
+ ImGui::SameLine();
ImGui::TextLinkOpenURL("Releases", "https://github.com/ocornut/imgui/releases");
ImGui::SameLine();
ImGui::TextLinkOpenURL("Funding", "https://github.com/ocornut/imgui/wiki/Funding");
@@ -8038,13 +8181,16 @@ void ImGui::ShowAboutWindow(bool* p_open)
if (copy_to_clipboard)
{
ImGui::LogToClipboard();
- ImGui::LogText("```\n"); // Back quotes will make text appears without formatting when pasting on GitHub
+ ImGui::LogText("```cpp\n"); // Back quotes will make text appears without formatting when pasting on GitHub
}
ImGui::Text("Dear ImGui %s (%d)", IMGUI_VERSION, IMGUI_VERSION_NUM);
ImGui::Separator();
ImGui::Text("sizeof(size_t): %d, sizeof(ImDrawIdx): %d, sizeof(ImDrawVert): %d", (int)sizeof(size_t), (int)sizeof(ImDrawIdx), (int)sizeof(ImDrawVert));
ImGui::Text("define: __cplusplus=%d", (int)__cplusplus);
+#ifdef IMGUI_ENABLE_TEST_ENGINE
+ ImGui::Text("define: IMGUI_ENABLE_TEST_ENGINE");
+#endif
#ifdef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
ImGui::Text("define: IMGUI_DISABLE_OBSOLETE_FUNCTIONS");
#endif
@@ -8129,9 +8275,9 @@ void ImGui::ShowAboutWindow(bool* p_open)
if (io.ConfigFlags & ImGuiConfigFlags_NoKeyboard) ImGui::Text(" NoKeyboard");
if (io.ConfigFlags & ImGuiConfigFlags_DockingEnable) ImGui::Text(" DockingEnable");
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) ImGui::Text(" ViewportsEnable");
- if (io.ConfigFlags & ImGuiConfigFlags_DpiEnableScaleViewports) ImGui::Text(" DpiEnableScaleViewports");
- if (io.ConfigFlags & ImGuiConfigFlags_DpiEnableScaleFonts) ImGui::Text(" DpiEnableScaleFonts");
if (io.MouseDrawCursor) ImGui::Text("io.MouseDrawCursor");
+ if (io.ConfigDpiScaleFonts) ImGui::Text("io.ConfigDpiScaleFonts");
+ if (io.ConfigDpiScaleViewports) ImGui::Text("io.ConfigDpiScaleViewports");
if (io.ConfigViewportsNoAutoMerge) ImGui::Text("io.ConfigViewportsNoAutoMerge");
if (io.ConfigViewportsNoTaskBarIcon) ImGui::Text("io.ConfigViewportsNoTaskBarIcon");
if (io.ConfigViewportsNoDecoration) ImGui::Text("io.ConfigViewportsNoDecoration");
@@ -8153,10 +8299,13 @@ void ImGui::ShowAboutWindow(bool* p_open)
if (io.BackendFlags & ImGuiBackendFlags_HasSetMousePos) ImGui::Text(" HasSetMousePos");
if (io.BackendFlags & ImGuiBackendFlags_PlatformHasViewports) ImGui::Text(" PlatformHasViewports");
if (io.BackendFlags & ImGuiBackendFlags_HasMouseHoveredViewport)ImGui::Text(" HasMouseHoveredViewport");
+ if (io.BackendFlags & ImGuiBackendFlags_HasParentViewport) ImGui::Text(" HasParentViewport");
if (io.BackendFlags & ImGuiBackendFlags_RendererHasVtxOffset) ImGui::Text(" RendererHasVtxOffset");
+ if (io.BackendFlags & ImGuiBackendFlags_RendererHasTextures) ImGui::Text(" RendererHasTextures");
if (io.BackendFlags & ImGuiBackendFlags_RendererHasViewports) ImGui::Text(" RendererHasViewports");
ImGui::Separator();
- ImGui::Text("io.Fonts: %d fonts, Flags: 0x%08X, TexSize: %d,%d", io.Fonts->Fonts.Size, io.Fonts->Flags, io.Fonts->TexWidth, io.Fonts->TexHeight);
+ ImGui::Text("io.Fonts: %d fonts, Flags: 0x%08X, TexSize: %d,%d", io.Fonts->Fonts.Size, io.Fonts->Flags, io.Fonts->TexData->Width, io.Fonts->TexData->Height);
+ ImGui::Text("io.Fonts->FontLoaderName: %s", io.Fonts->FontLoaderName ? io.Fonts->FontLoaderName : "NULL");
ImGui::Text("io.DisplaySize: %.2f,%.2f", io.DisplaySize.x, io.DisplaySize.y);
ImGui::Text("io.DisplayFramebufferScale: %.2f,%.2f", io.DisplayFramebufferScale.x, io.DisplayFramebufferScale.y);
ImGui::Separator();
@@ -8181,66 +8330,56 @@ void ImGui::ShowAboutWindow(bool* p_open)
//-----------------------------------------------------------------------------
// [SECTION] Style Editor / ShowStyleEditor()
//-----------------------------------------------------------------------------
-// - ShowFontSelector()
// - ShowStyleSelector()
// - ShowStyleEditor()
//-----------------------------------------------------------------------------
-// Forward declare ShowFontAtlas() which isn't worth putting in public API yet
-namespace ImGui { IMGUI_API void ShowFontAtlas(ImFontAtlas* atlas); }
-
-// Demo helper function to select among loaded fonts.
-// Here we use the regular BeginCombo()/EndCombo() api which is the more flexible one.
-void ImGui::ShowFontSelector(const char* label)
+// Demo helper function to select among default colors. See ShowStyleEditor() for more advanced options.
+bool ImGui::ShowStyleSelector(const char* label)
{
- ImGuiIO& io = ImGui::GetIO();
- ImFont* font_current = ImGui::GetFont();
- if (ImGui::BeginCombo(label, font_current->GetDebugName()))
+ // FIXME: This is a bit tricky to get right as style are functions, they don't register a name nor the fact that one is active.
+ // So we keep track of last active one among our limited selection.
+ static int style_idx = -1;
+ const char* style_names[] = { "Dark", "Light", "Classic" };
+ bool ret = false;
+ if (ImGui::BeginCombo(label, (style_idx >= 0 && style_idx < IM_ARRAYSIZE(style_names)) ? style_names[style_idx] : ""))
{
- for (ImFont* font : io.Fonts->Fonts)
+ for (int n = 0; n < IM_ARRAYSIZE(style_names); n++)
{
- ImGui::PushID((void*)font);
- if (ImGui::Selectable(font->GetDebugName(), font == font_current))
- io.FontDefault = font;
- if (font == font_current)
+ if (ImGui::Selectable(style_names[n], style_idx == n, ImGuiSelectableFlags_SelectOnNav))
+ {
+ style_idx = n;
+ ret = true;
+ switch (style_idx)
+ {
+ case 0: ImGui::StyleColorsDark(); break;
+ case 1: ImGui::StyleColorsLight(); break;
+ case 2: ImGui::StyleColorsClassic(); break;
+ }
+ }
+ else if (style_idx == n)
ImGui::SetItemDefaultFocus();
- ImGui::PopID();
}
ImGui::EndCombo();
}
- ImGui::SameLine();
- HelpMarker(
- "- Load additional fonts with io.Fonts->AddFontFromFileTTF().\n"
- "- The font atlas is built when calling io.Fonts->GetTexDataAsXXXX() or io.Fonts->Build().\n"
- "- Read FAQ and docs/FONTS.md for more details.\n"
- "- If you need to add/remove fonts at runtime (e.g. for DPI change), do it before calling NewFrame().");
+ return ret;
}
-// Demo helper function to select among default colors. See ShowStyleEditor() for more advanced options.
-// Here we use the simplified Combo() api that packs items into a single literal string.
-// Useful for quick combo boxes where the choices are known locally.
-bool ImGui::ShowStyleSelector(const char* label)
+static const char* GetTreeLinesFlagsName(ImGuiTreeNodeFlags flags)
{
- static int style_idx = -1;
- if (ImGui::Combo(label, &style_idx, "Dark\0Light\0Classic\0"))
- {
- switch (style_idx)
- {
- case 0: ImGui::StyleColorsDark(); break;
- case 1: ImGui::StyleColorsLight(); break;
- case 2: ImGui::StyleColorsClassic(); break;
- }
- return true;
- }
- return false;
+ if (flags == ImGuiTreeNodeFlags_DrawLinesNone) return "DrawLinesNone";
+ if (flags == ImGuiTreeNodeFlags_DrawLinesFull) return "DrawLinesFull";
+ if (flags == ImGuiTreeNodeFlags_DrawLinesToNodes) return "DrawLinesToNodes";
+ return "";
}
+// We omit the ImGui:: prefix in this function, as we don't expect user to be copy and pasting this code.
void ImGui::ShowStyleEditor(ImGuiStyle* ref)
{
IMGUI_DEMO_MARKER("Tools/Style Editor");
// You can pass in a reference ImGuiStyle structure to compare to, revert to and save to
// (without a reference style pointer, we will use one compared locally as a reference)
- ImGuiStyle& style = ImGui::GetStyle();
+ ImGuiStyle& style = GetStyle();
static ImGuiStyle ref_saved_style;
// Default to using internal storage as reference
@@ -8251,197 +8390,239 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref)
if (ref == NULL)
ref = &ref_saved_style;
- ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.50f);
+ PushItemWidth(GetWindowWidth() * 0.50f);
- if (ImGui::ShowStyleSelector("Colors##Selector"))
- ref_saved_style = style;
- ImGui::ShowFontSelector("Fonts##Selector");
+ {
+ // General
+ SeparatorText("General");
+ if ((GetIO().BackendFlags & ImGuiBackendFlags_RendererHasTextures) == 0)
+ {
+ BulletText("Warning: Font scaling will NOT be smooth, because\nImGuiBackendFlags_RendererHasTextures is not set!");
+ BulletText("For instructions, see:");
+ SameLine();
+ TextLinkOpenURL("docs/BACKENDS.md", "https://github.com/ocornut/imgui/blob/master/docs/BACKENDS.md");
+ }
- // Simplified Settings (expose floating-pointer border sizes as boolean representing 0.0f or 1.0f)
- if (ImGui::SliderFloat("FrameRounding", &style.FrameRounding, 0.0f, 12.0f, "%.0f"))
- style.GrabRounding = style.FrameRounding; // Make GrabRounding always the same value as FrameRounding
- { bool border = (style.WindowBorderSize > 0.0f); if (ImGui::Checkbox("WindowBorder", &border)) { style.WindowBorderSize = border ? 1.0f : 0.0f; } }
- ImGui::SameLine();
- { bool border = (style.FrameBorderSize > 0.0f); if (ImGui::Checkbox("FrameBorder", &border)) { style.FrameBorderSize = border ? 1.0f : 0.0f; } }
- ImGui::SameLine();
- { bool border = (style.PopupBorderSize > 0.0f); if (ImGui::Checkbox("PopupBorder", &border)) { style.PopupBorderSize = border ? 1.0f : 0.0f; } }
+ if (ShowStyleSelector("Colors##Selector"))
+ ref_saved_style = style;
+ ShowFontSelector("Fonts##Selector");
+ if (DragFloat("FontSizeBase", &style.FontSizeBase, 0.20f, 5.0f, 100.0f, "%.0f"))
+ style._NextFrameFontSizeBase = style.FontSizeBase; // FIXME: Temporary hack until we finish remaining work.
+ SameLine(0.0f, 0.0f); Text(" (out %.2f)", GetFontSize());
+ DragFloat("FontScaleMain", &style.FontScaleMain, 0.02f, 0.5f, 4.0f);
+ //BeginDisabled(GetIO().ConfigDpiScaleFonts);
+ DragFloat("FontScaleDpi", &style.FontScaleDpi, 0.02f, 0.5f, 4.0f);
+ //SetItemTooltip("When io.ConfigDpiScaleFonts is set, this value is automatically overwritten.");
+ //EndDisabled();
+
+ // Simplified Settings (expose floating-pointer border sizes as boolean representing 0.0f or 1.0f)
+ if (SliderFloat("FrameRounding", &style.FrameRounding, 0.0f, 12.0f, "%.0f"))
+ style.GrabRounding = style.FrameRounding; // Make GrabRounding always the same value as FrameRounding
+ { bool border = (style.WindowBorderSize > 0.0f); if (Checkbox("WindowBorder", &border)) { style.WindowBorderSize = border ? 1.0f : 0.0f; } }
+ SameLine();
+ { bool border = (style.FrameBorderSize > 0.0f); if (Checkbox("FrameBorder", &border)) { style.FrameBorderSize = border ? 1.0f : 0.0f; } }
+ SameLine();
+ { bool border = (style.PopupBorderSize > 0.0f); if (Checkbox("PopupBorder", &border)) { style.PopupBorderSize = border ? 1.0f : 0.0f; } }
+ }
// Save/Revert button
- if (ImGui::Button("Save Ref"))
+ if (Button("Save Ref"))
*ref = ref_saved_style = style;
- ImGui::SameLine();
- if (ImGui::Button("Revert Ref"))
+ SameLine();
+ if (Button("Revert Ref"))
style = *ref;
- ImGui::SameLine();
+ SameLine();
HelpMarker(
"Save/Revert in local non-persistent storage. Default Colors definition are not affected. "
"Use \"Export\" below to save them somewhere.");
- ImGui::Separator();
-
- if (ImGui::BeginTabBar("##tabs", ImGuiTabBarFlags_None))
+ SeparatorText("Details");
+ if (BeginTabBar("##tabs", ImGuiTabBarFlags_None))
{
- if (ImGui::BeginTabItem("Sizes"))
+ if (BeginTabItem("Sizes"))
{
- ImGui::SeparatorText("Main");
- ImGui::SliderFloat2("WindowPadding", (float*)&style.WindowPadding, 0.0f, 20.0f, "%.0f");
- ImGui::SliderFloat2("FramePadding", (float*)&style.FramePadding, 0.0f, 20.0f, "%.0f");
- ImGui::SliderFloat2("ItemSpacing", (float*)&style.ItemSpacing, 0.0f, 20.0f, "%.0f");
- ImGui::SliderFloat2("ItemInnerSpacing", (float*)&style.ItemInnerSpacing, 0.0f, 20.0f, "%.0f");
- ImGui::SliderFloat2("TouchExtraPadding", (float*)&style.TouchExtraPadding, 0.0f, 10.0f, "%.0f");
- ImGui::SliderFloat("IndentSpacing", &style.IndentSpacing, 0.0f, 30.0f, "%.0f");
- ImGui::SliderFloat("ScrollbarSize", &style.ScrollbarSize, 1.0f, 20.0f, "%.0f");
- ImGui::SliderFloat("GrabMinSize", &style.GrabMinSize, 1.0f, 20.0f, "%.0f");
+ SeparatorText("Main");
+ SliderFloat2("WindowPadding", (float*)&style.WindowPadding, 0.0f, 20.0f, "%.0f");
+ SliderFloat2("FramePadding", (float*)&style.FramePadding, 0.0f, 20.0f, "%.0f");
+ SliderFloat2("ItemSpacing", (float*)&style.ItemSpacing, 0.0f, 20.0f, "%.0f");
+ SliderFloat2("ItemInnerSpacing", (float*)&style.ItemInnerSpacing, 0.0f, 20.0f, "%.0f");
+ SliderFloat2("TouchExtraPadding", (float*)&style.TouchExtraPadding, 0.0f, 10.0f, "%.0f");
+ SliderFloat("IndentSpacing", &style.IndentSpacing, 0.0f, 30.0f, "%.0f");
+ SliderFloat("GrabMinSize", &style.GrabMinSize, 1.0f, 20.0f, "%.0f");
- ImGui::SeparatorText("Borders");
- ImGui::SliderFloat("WindowBorderSize", &style.WindowBorderSize, 0.0f, 1.0f, "%.0f");
- ImGui::SliderFloat("ChildBorderSize", &style.ChildBorderSize, 0.0f, 1.0f, "%.0f");
- ImGui::SliderFloat("PopupBorderSize", &style.PopupBorderSize, 0.0f, 1.0f, "%.0f");
- ImGui::SliderFloat("FrameBorderSize", &style.FrameBorderSize, 0.0f, 1.0f, "%.0f");
+ SeparatorText("Borders");
+ SliderFloat("WindowBorderSize", &style.WindowBorderSize, 0.0f, 1.0f, "%.0f");
+ SliderFloat("ChildBorderSize", &style.ChildBorderSize, 0.0f, 1.0f, "%.0f");
+ SliderFloat("PopupBorderSize", &style.PopupBorderSize, 0.0f, 1.0f, "%.0f");
+ SliderFloat("FrameBorderSize", &style.FrameBorderSize, 0.0f, 1.0f, "%.0f");
- ImGui::SeparatorText("Rounding");
- ImGui::SliderFloat("WindowRounding", &style.WindowRounding, 0.0f, 12.0f, "%.0f");
- ImGui::SliderFloat("ChildRounding", &style.ChildRounding, 0.0f, 12.0f, "%.0f");
- ImGui::SliderFloat("FrameRounding", &style.FrameRounding, 0.0f, 12.0f, "%.0f");
- ImGui::SliderFloat("PopupRounding", &style.PopupRounding, 0.0f, 12.0f, "%.0f");
- ImGui::SliderFloat("ScrollbarRounding", &style.ScrollbarRounding, 0.0f, 12.0f, "%.0f");
- ImGui::SliderFloat("GrabRounding", &style.GrabRounding, 0.0f, 12.0f, "%.0f");
+ SeparatorText("Rounding");
+ SliderFloat("WindowRounding", &style.WindowRounding, 0.0f, 12.0f, "%.0f");
+ SliderFloat("ChildRounding", &style.ChildRounding, 0.0f, 12.0f, "%.0f");
+ SliderFloat("FrameRounding", &style.FrameRounding, 0.0f, 12.0f, "%.0f");
+ SliderFloat("PopupRounding", &style.PopupRounding, 0.0f, 12.0f, "%.0f");
+ SliderFloat("GrabRounding", &style.GrabRounding, 0.0f, 12.0f, "%.0f");
- ImGui::SeparatorText("Tabs");
- ImGui::SliderFloat("TabBorderSize", &style.TabBorderSize, 0.0f, 1.0f, "%.0f");
- ImGui::SliderFloat("TabBarBorderSize", &style.TabBarBorderSize, 0.0f, 2.0f, "%.0f");
- ImGui::SliderFloat("TabBarOverlineSize", &style.TabBarOverlineSize, 0.0f, 3.0f, "%.0f");
- ImGui::SameLine(); HelpMarker("Overline is only drawn over the selected tab when ImGuiTabBarFlags_DrawSelectedOverline is set.");
- ImGui::DragFloat("TabCloseButtonMinWidthSelected", &style.TabCloseButtonMinWidthSelected, 0.1f, -1.0f, 100.0f, (style.TabCloseButtonMinWidthSelected < 0.0f) ? "%.0f (Always)" : "%.0f");
- ImGui::DragFloat("TabCloseButtonMinWidthUnselected", &style.TabCloseButtonMinWidthUnselected, 0.1f, -1.0f, 100.0f, (style.TabCloseButtonMinWidthUnselected < 0.0f) ? "%.0f (Always)" : "%.0f");
- ImGui::SliderFloat("TabRounding", &style.TabRounding, 0.0f, 12.0f, "%.0f");
+ SeparatorText("Scrollbar");
+ SliderFloat("ScrollbarSize", &style.ScrollbarSize, 1.0f, 20.0f, "%.0f");
+ SliderFloat("ScrollbarRounding", &style.ScrollbarRounding, 0.0f, 12.0f, "%.0f");
+ SliderFloat("ScrollbarPadding", &style.ScrollbarPadding, 0.0f, 10.0f, "%.0f");
- ImGui::SeparatorText("Tables");
- ImGui::SliderFloat2("CellPadding", (float*)&style.CellPadding, 0.0f, 20.0f, "%.0f");
- ImGui::SliderAngle("TableAngledHeadersAngle", &style.TableAngledHeadersAngle, -50.0f, +50.0f);
- ImGui::SliderFloat2("TableAngledHeadersTextAlign", (float*)&style.TableAngledHeadersTextAlign, 0.0f, 1.0f, "%.2f");
+ SeparatorText("Tabs");
+ SliderFloat("TabBorderSize", &style.TabBorderSize, 0.0f, 1.0f, "%.0f");
+ SliderFloat("TabBarBorderSize", &style.TabBarBorderSize, 0.0f, 2.0f, "%.0f");
+ SliderFloat("TabBarOverlineSize", &style.TabBarOverlineSize, 0.0f, 3.0f, "%.0f");
+ SameLine(); HelpMarker("Overline is only drawn over the selected tab when ImGuiTabBarFlags_DrawSelectedOverline is set.");
+ DragFloat("TabMinWidthBase", &style.TabMinWidthBase, 0.5f, 1.0f, 500.0f, "%.0f");
+ DragFloat("TabMinWidthShrink", &style.TabMinWidthShrink, 0.5f, 1.0f, 500.0f, "%0.f");
+ DragFloat("TabCloseButtonMinWidthSelected", &style.TabCloseButtonMinWidthSelected, 0.5f, -1.0f, 100.0f, (style.TabCloseButtonMinWidthSelected < 0.0f) ? "%.0f (Always)" : "%.0f");
+ DragFloat("TabCloseButtonMinWidthUnselected", &style.TabCloseButtonMinWidthUnselected, 0.5f, -1.0f, 100.0f, (style.TabCloseButtonMinWidthUnselected < 0.0f) ? "%.0f (Always)" : "%.0f");
+ SliderFloat("TabRounding", &style.TabRounding, 0.0f, 12.0f, "%.0f");
- ImGui::SeparatorText("Windows");
- ImGui::SliderFloat2("WindowTitleAlign", (float*)&style.WindowTitleAlign, 0.0f, 1.0f, "%.2f");
- ImGui::SliderFloat("WindowBorderHoverPadding", &style.WindowBorderHoverPadding, 1.0f, 20.0f, "%.0f");
+ SeparatorText("Tables");
+ SliderFloat2("CellPadding", (float*)&style.CellPadding, 0.0f, 20.0f, "%.0f");
+ SliderAngle("TableAngledHeadersAngle", &style.TableAngledHeadersAngle, -50.0f, +50.0f);
+ SliderFloat2("TableAngledHeadersTextAlign", (float*)&style.TableAngledHeadersTextAlign, 0.0f, 1.0f, "%.2f");
+
+ SeparatorText("Trees");
+ bool combo_open = BeginCombo("TreeLinesFlags", GetTreeLinesFlagsName(style.TreeLinesFlags));
+ SameLine();
+ HelpMarker("[Experimental] Tree lines may not work in all situations (e.g. using a clipper) and may incurs slight traversal overhead.\n\nImGuiTreeNodeFlags_DrawLinesFull is faster than ImGuiTreeNodeFlags_DrawLinesToNode.");
+ if (combo_open)
+ {
+ const ImGuiTreeNodeFlags options[] = { ImGuiTreeNodeFlags_DrawLinesNone, ImGuiTreeNodeFlags_DrawLinesFull, ImGuiTreeNodeFlags_DrawLinesToNodes };
+ for (ImGuiTreeNodeFlags option : options)
+ if (Selectable(GetTreeLinesFlagsName(option), style.TreeLinesFlags == option))
+ style.TreeLinesFlags = option;
+ EndCombo();
+ }
+ SliderFloat("TreeLinesSize", &style.TreeLinesSize, 0.0f, 2.0f, "%.0f");
+ SliderFloat("TreeLinesRounding", &style.TreeLinesRounding, 0.0f, 12.0f, "%.0f");
+
+ SeparatorText("Windows");
+ SliderFloat2("WindowTitleAlign", (float*)&style.WindowTitleAlign, 0.0f, 1.0f, "%.2f");
+ SliderFloat("WindowBorderHoverPadding", &style.WindowBorderHoverPadding, 1.0f, 20.0f, "%.0f");
int window_menu_button_position = style.WindowMenuButtonPosition + 1;
- if (ImGui::Combo("WindowMenuButtonPosition", (int*)&window_menu_button_position, "None\0Left\0Right\0"))
+ if (Combo("WindowMenuButtonPosition", (int*)&window_menu_button_position, "None\0Left\0Right\0"))
style.WindowMenuButtonPosition = (ImGuiDir)(window_menu_button_position - 1);
- ImGui::SeparatorText("Widgets");
- ImGui::Combo("ColorButtonPosition", (int*)&style.ColorButtonPosition, "Left\0Right\0");
- ImGui::SliderFloat2("ButtonTextAlign", (float*)&style.ButtonTextAlign, 0.0f, 1.0f, "%.2f");
- ImGui::SameLine(); HelpMarker("Alignment applies when a button is larger than its text content.");
- ImGui::SliderFloat2("SelectableTextAlign", (float*)&style.SelectableTextAlign, 0.0f, 1.0f, "%.2f");
- ImGui::SameLine(); HelpMarker("Alignment applies when a selectable is larger than its text content.");
- ImGui::SliderFloat("SeparatorTextBorderSize", &style.SeparatorTextBorderSize, 0.0f, 10.0f, "%.0f");
- ImGui::SliderFloat2("SeparatorTextAlign", (float*)&style.SeparatorTextAlign, 0.0f, 1.0f, "%.2f");
- ImGui::SliderFloat2("SeparatorTextPadding", (float*)&style.SeparatorTextPadding, 0.0f, 40.0f, "%.0f");
- ImGui::SliderFloat("LogSliderDeadzone", &style.LogSliderDeadzone, 0.0f, 12.0f, "%.0f");
- ImGui::SliderFloat("ImageBorderSize", &style.ImageBorderSize, 0.0f, 1.0f, "%.0f");
+ SeparatorText("Widgets");
+ Combo("ColorButtonPosition", (int*)&style.ColorButtonPosition, "Left\0Right\0");
+ SliderFloat2("ButtonTextAlign", (float*)&style.ButtonTextAlign, 0.0f, 1.0f, "%.2f");
+ SameLine(); HelpMarker("Alignment applies when a button is larger than its text content.");
+ SliderFloat2("SelectableTextAlign", (float*)&style.SelectableTextAlign, 0.0f, 1.0f, "%.2f");
+ SameLine(); HelpMarker("Alignment applies when a selectable is larger than its text content.");
+ SliderFloat("SeparatorTextBorderSize", &style.SeparatorTextBorderSize, 0.0f, 10.0f, "%.0f");
+ SliderFloat2("SeparatorTextAlign", (float*)&style.SeparatorTextAlign, 0.0f, 1.0f, "%.2f");
+ SliderFloat2("SeparatorTextPadding", (float*)&style.SeparatorTextPadding, 0.0f, 40.0f, "%.0f");
+ SliderFloat("LogSliderDeadzone", &style.LogSliderDeadzone, 0.0f, 12.0f, "%.0f");
+ SliderFloat("ImageBorderSize", &style.ImageBorderSize, 0.0f, 1.0f, "%.0f");
- ImGui::SeparatorText("Docking");
- ImGui::SliderFloat("DockingSplitterSize", &style.DockingSeparatorSize, 0.0f, 12.0f, "%.0f");
+ SeparatorText("Docking");
+ //SetCursorPosX(GetCursorPosX() + CalcItemWidth() - GetFrameHeight());
+ Checkbox("DockingNodeHasCloseButton", &style.DockingNodeHasCloseButton);
+ SliderFloat("DockingSeparatorSize", &style.DockingSeparatorSize, 0.0f, 12.0f, "%.0f");
- ImGui::SeparatorText("Tooltips");
+ SeparatorText("Tooltips");
for (int n = 0; n < 2; n++)
- if (ImGui::TreeNodeEx(n == 0 ? "HoverFlagsForTooltipMouse" : "HoverFlagsForTooltipNav"))
+ if (TreeNodeEx(n == 0 ? "HoverFlagsForTooltipMouse" : "HoverFlagsForTooltipNav"))
{
ImGuiHoveredFlags* p = (n == 0) ? &style.HoverFlagsForTooltipMouse : &style.HoverFlagsForTooltipNav;
- ImGui::CheckboxFlags("ImGuiHoveredFlags_DelayNone", p, ImGuiHoveredFlags_DelayNone);
- ImGui::CheckboxFlags("ImGuiHoveredFlags_DelayShort", p, ImGuiHoveredFlags_DelayShort);
- ImGui::CheckboxFlags("ImGuiHoveredFlags_DelayNormal", p, ImGuiHoveredFlags_DelayNormal);
- ImGui::CheckboxFlags("ImGuiHoveredFlags_Stationary", p, ImGuiHoveredFlags_Stationary);
- ImGui::CheckboxFlags("ImGuiHoveredFlags_NoSharedDelay", p, ImGuiHoveredFlags_NoSharedDelay);
- ImGui::TreePop();
+ CheckboxFlags("ImGuiHoveredFlags_DelayNone", p, ImGuiHoveredFlags_DelayNone);
+ CheckboxFlags("ImGuiHoveredFlags_DelayShort", p, ImGuiHoveredFlags_DelayShort);
+ CheckboxFlags("ImGuiHoveredFlags_DelayNormal", p, ImGuiHoveredFlags_DelayNormal);
+ CheckboxFlags("ImGuiHoveredFlags_Stationary", p, ImGuiHoveredFlags_Stationary);
+ CheckboxFlags("ImGuiHoveredFlags_NoSharedDelay", p, ImGuiHoveredFlags_NoSharedDelay);
+ TreePop();
}
- ImGui::SeparatorText("Misc");
- ImGui::SliderFloat2("DisplayWindowPadding", (float*)&style.DisplayWindowPadding, 0.0f, 30.0f, "%.0f"); ImGui::SameLine(); HelpMarker("Apply to regular windows: amount which we enforce to keep visible when moving near edges of your screen.");
- ImGui::SliderFloat2("DisplaySafeAreaPadding", (float*)&style.DisplaySafeAreaPadding, 0.0f, 30.0f, "%.0f"); ImGui::SameLine(); HelpMarker("Apply to every windows, menus, popups, tooltips: amount where we avoid displaying contents. Adjust if you cannot see the edges of your screen (e.g. on a TV where scaling has not been configured).");
+ SeparatorText("Misc");
+ SliderFloat2("DisplayWindowPadding", (float*)&style.DisplayWindowPadding, 0.0f, 30.0f, "%.0f"); SameLine(); HelpMarker("Apply to regular windows: amount which we enforce to keep visible when moving near edges of your screen.");
+ SliderFloat2("DisplaySafeAreaPadding", (float*)&style.DisplaySafeAreaPadding, 0.0f, 30.0f, "%.0f"); SameLine(); HelpMarker("Apply to every windows, menus, popups, tooltips: amount where we avoid displaying contents. Adjust if you cannot see the edges of your screen (e.g. on a TV where scaling has not been configured).");
- ImGui::EndTabItem();
+ EndTabItem();
}
- if (ImGui::BeginTabItem("Colors"))
+ if (BeginTabItem("Colors"))
{
static int output_dest = 0;
static bool output_only_modified = true;
- if (ImGui::Button("Export"))
+ if (Button("Export"))
{
if (output_dest == 0)
- ImGui::LogToClipboard();
+ LogToClipboard();
else
- ImGui::LogToTTY();
- ImGui::LogText("ImVec4* colors = ImGui::GetStyle().Colors;" IM_NEWLINE);
+ LogToTTY();
+ LogText("ImVec4* colors = GetStyle().Colors;" IM_NEWLINE);
for (int i = 0; i < ImGuiCol_COUNT; i++)
{
const ImVec4& col = style.Colors[i];
- const char* name = ImGui::GetStyleColorName(i);
+ const char* name = GetStyleColorName(i);
if (!output_only_modified || memcmp(&col, &ref->Colors[i], sizeof(ImVec4)) != 0)
- ImGui::LogText("colors[ImGuiCol_%s]%*s= ImVec4(%.2ff, %.2ff, %.2ff, %.2ff);" IM_NEWLINE,
+ LogText("colors[ImGuiCol_%s]%*s= ImVec4(%.2ff, %.2ff, %.2ff, %.2ff);" IM_NEWLINE,
name, 23 - (int)strlen(name), "", col.x, col.y, col.z, col.w);
}
- ImGui::LogFinish();
+ LogFinish();
}
- ImGui::SameLine(); ImGui::SetNextItemWidth(120); ImGui::Combo("##output_type", &output_dest, "To Clipboard\0To TTY\0");
- ImGui::SameLine(); ImGui::Checkbox("Only Modified Colors", &output_only_modified);
+ SameLine(); SetNextItemWidth(120); Combo("##output_type", &output_dest, "To Clipboard\0To TTY\0");
+ SameLine(); Checkbox("Only Modified Colors", &output_only_modified);
static ImGuiTextFilter filter;
- filter.Draw("Filter colors", ImGui::GetFontSize() * 16);
+ filter.Draw("Filter colors", GetFontSize() * 16);
static ImGuiColorEditFlags alpha_flags = 0;
- if (ImGui::RadioButton("Opaque", alpha_flags == ImGuiColorEditFlags_AlphaOpaque)) { alpha_flags = ImGuiColorEditFlags_AlphaOpaque; } ImGui::SameLine();
- if (ImGui::RadioButton("Alpha", alpha_flags == ImGuiColorEditFlags_None)) { alpha_flags = ImGuiColorEditFlags_None; } ImGui::SameLine();
- if (ImGui::RadioButton("Both", alpha_flags == ImGuiColorEditFlags_AlphaPreviewHalf)) { alpha_flags = ImGuiColorEditFlags_AlphaPreviewHalf; } ImGui::SameLine();
+ if (RadioButton("Opaque", alpha_flags == ImGuiColorEditFlags_AlphaOpaque)) { alpha_flags = ImGuiColorEditFlags_AlphaOpaque; } SameLine();
+ if (RadioButton("Alpha", alpha_flags == ImGuiColorEditFlags_None)) { alpha_flags = ImGuiColorEditFlags_None; } SameLine();
+ if (RadioButton("Both", alpha_flags == ImGuiColorEditFlags_AlphaPreviewHalf)) { alpha_flags = ImGuiColorEditFlags_AlphaPreviewHalf; } SameLine();
HelpMarker(
"In the color list:\n"
"Left-click on color square to open color picker,\n"
"Right-click to open edit options menu.");
- ImGui::SetNextWindowSizeConstraints(ImVec2(0.0f, ImGui::GetTextLineHeightWithSpacing() * 10), ImVec2(FLT_MAX, FLT_MAX));
- ImGui::BeginChild("##colors", ImVec2(0, 0), ImGuiChildFlags_Borders | ImGuiChildFlags_NavFlattened, ImGuiWindowFlags_AlwaysVerticalScrollbar | ImGuiWindowFlags_AlwaysHorizontalScrollbar);
- ImGui::PushItemWidth(ImGui::GetFontSize() * -12);
+ SetNextWindowSizeConstraints(ImVec2(0.0f, GetTextLineHeightWithSpacing() * 10), ImVec2(FLT_MAX, FLT_MAX));
+ BeginChild("##colors", ImVec2(0, 0), ImGuiChildFlags_Borders | ImGuiChildFlags_NavFlattened, ImGuiWindowFlags_AlwaysVerticalScrollbar | ImGuiWindowFlags_AlwaysHorizontalScrollbar);
+ PushItemWidth(GetFontSize() * -12);
for (int i = 0; i < ImGuiCol_COUNT; i++)
{
- const char* name = ImGui::GetStyleColorName(i);
+ const char* name = GetStyleColorName(i);
if (!filter.PassFilter(name))
continue;
- ImGui::PushID(i);
+ PushID(i);
#ifndef IMGUI_DISABLE_DEBUG_TOOLS
- if (ImGui::Button("?"))
- ImGui::DebugFlashStyleColor((ImGuiCol)i);
- ImGui::SetItemTooltip("Flash given color to identify places where it is used.");
- ImGui::SameLine();
+ if (Button("?"))
+ DebugFlashStyleColor((ImGuiCol)i);
+ SetItemTooltip("Flash given color to identify places where it is used.");
+ SameLine();
#endif
- ImGui::ColorEdit4("##color", (float*)&style.Colors[i], ImGuiColorEditFlags_AlphaBar | alpha_flags);
+ ColorEdit4("##color", (float*)&style.Colors[i], ImGuiColorEditFlags_AlphaBar | alpha_flags);
if (memcmp(&style.Colors[i], &ref->Colors[i], sizeof(ImVec4)) != 0)
{
// Tips: in a real user application, you may want to merge and use an icon font into the main font,
// so instead of "Save"/"Revert" you'd use icons!
// Read the FAQ and docs/FONTS.md about using icon fonts. It's really easy and super convenient!
- ImGui::SameLine(0.0f, style.ItemInnerSpacing.x); if (ImGui::Button("Save")) { ref->Colors[i] = style.Colors[i]; }
- ImGui::SameLine(0.0f, style.ItemInnerSpacing.x); if (ImGui::Button("Revert")) { style.Colors[i] = ref->Colors[i]; }
+ SameLine(0.0f, style.ItemInnerSpacing.x); if (Button("Save")) { ref->Colors[i] = style.Colors[i]; }
+ SameLine(0.0f, style.ItemInnerSpacing.x); if (Button("Revert")) { style.Colors[i] = ref->Colors[i]; }
}
- ImGui::SameLine(0.0f, style.ItemInnerSpacing.x);
- ImGui::TextUnformatted(name);
- ImGui::PopID();
+ SameLine(0.0f, style.ItemInnerSpacing.x);
+ TextUnformatted(name);
+ PopID();
}
- ImGui::PopItemWidth();
- ImGui::EndChild();
+ PopItemWidth();
+ EndChild();
- ImGui::EndTabItem();
+ EndTabItem();
}
- if (ImGui::BeginTabItem("Fonts"))
+ if (BeginTabItem("Fonts"))
{
- ImGuiIO& io = ImGui::GetIO();
+ ImGuiIO& io = GetIO();
ImFontAtlas* atlas = io.Fonts;
- HelpMarker("Read FAQ and docs/FONTS.md for details on font loading.");
- ImGui::ShowFontAtlas(atlas);
+ ShowFontAtlas(atlas);
// Post-baking font scaling. Note that this is NOT the nice way of scaling fonts, read below.
// (we enforce hard clamping manually as by default DragFloat/SliderFloat allows CTRL+Click text to get out of bounds).
+ /*
+ SeparatorText("Legacy Scaling");
const float MIN_SCALE = 0.3f;
const float MAX_SCALE = 2.0f;
HelpMarker(
@@ -8449,120 +8630,121 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref)
"However, the _correct_ way of scaling your UI is currently to reload your font at the designed size, "
"rebuild the font atlas, and call style.ScaleAllSizes() on a reference ImGuiStyle structure.\n"
"Using those settings here will give you poor quality results.");
- static float window_scale = 1.0f;
- ImGui::PushItemWidth(ImGui::GetFontSize() * 8);
- if (ImGui::DragFloat("window scale", &window_scale, 0.005f, MIN_SCALE, MAX_SCALE, "%.2f", ImGuiSliderFlags_AlwaysClamp)) // Scale only this window
- ImGui::SetWindowFontScale(window_scale);
- ImGui::DragFloat("global scale", &io.FontGlobalScale, 0.005f, MIN_SCALE, MAX_SCALE, "%.2f", ImGuiSliderFlags_AlwaysClamp); // Scale everything
- ImGui::PopItemWidth();
+ PushItemWidth(GetFontSize() * 8);
+ DragFloat("global scale", &io.FontGlobalScale, 0.005f, MIN_SCALE, MAX_SCALE, "%.2f", ImGuiSliderFlags_AlwaysClamp); // Scale everything
+ //static float window_scale = 1.0f;
+ //if (DragFloat("window scale", &window_scale, 0.005f, MIN_SCALE, MAX_SCALE, "%.2f", ImGuiSliderFlags_AlwaysClamp)) // Scale only this window
+ // SetWindowFontScale(window_scale);
+ PopItemWidth();
+ */
- ImGui::EndTabItem();
+ EndTabItem();
}
- if (ImGui::BeginTabItem("Rendering"))
+ if (BeginTabItem("Rendering"))
{
- ImGui::Checkbox("Anti-aliased lines", &style.AntiAliasedLines);
- ImGui::SameLine();
+ Checkbox("Anti-aliased lines", &style.AntiAliasedLines);
+ SameLine();
HelpMarker("When disabling anti-aliasing lines, you'll probably want to disable borders in your style as well.");
- ImGui::Checkbox("Anti-aliased lines use texture", &style.AntiAliasedLinesUseTex);
- ImGui::SameLine();
+ Checkbox("Anti-aliased lines use texture", &style.AntiAliasedLinesUseTex);
+ SameLine();
HelpMarker("Faster lines using texture data. Require backend to render with bilinear filtering (not point/nearest filtering).");
- ImGui::Checkbox("Anti-aliased fill", &style.AntiAliasedFill);
- ImGui::PushItemWidth(ImGui::GetFontSize() * 8);
- ImGui::DragFloat("Curve Tessellation Tolerance", &style.CurveTessellationTol, 0.02f, 0.10f, 10.0f, "%.2f");
+ Checkbox("Anti-aliased fill", &style.AntiAliasedFill);
+ PushItemWidth(GetFontSize() * 8);
+ DragFloat("Curve Tessellation Tolerance", &style.CurveTessellationTol, 0.02f, 0.10f, 10.0f, "%.2f");
if (style.CurveTessellationTol < 0.10f) style.CurveTessellationTol = 0.10f;
// When editing the "Circle Segment Max Error" value, draw a preview of its effect on auto-tessellated circles.
- ImGui::DragFloat("Circle Tessellation Max Error", &style.CircleTessellationMaxError , 0.005f, 0.10f, 5.0f, "%.2f", ImGuiSliderFlags_AlwaysClamp);
- const bool show_samples = ImGui::IsItemActive();
+ DragFloat("Circle Tessellation Max Error", &style.CircleTessellationMaxError , 0.005f, 0.10f, 5.0f, "%.2f", ImGuiSliderFlags_AlwaysClamp);
+ const bool show_samples = IsItemActive();
if (show_samples)
- ImGui::SetNextWindowPos(ImGui::GetCursorScreenPos());
- if (show_samples && ImGui::BeginTooltip())
+ SetNextWindowPos(GetCursorScreenPos());
+ if (show_samples && BeginTooltip())
{
- ImGui::TextUnformatted("(R = radius, N = approx number of segments)");
- ImGui::Spacing();
- ImDrawList* draw_list = ImGui::GetWindowDrawList();
- const float min_widget_width = ImGui::CalcTextSize("R: MMM\nN: MMM").x;
+ TextUnformatted("(R = radius, N = approx number of segments)");
+ Spacing();
+ ImDrawList* draw_list = GetWindowDrawList();
+ const float min_widget_width = CalcTextSize("R: MMM\nN: MMM").x;
for (int n = 0; n < 8; n++)
{
const float RAD_MIN = 5.0f;
const float RAD_MAX = 70.0f;
const float rad = RAD_MIN + (RAD_MAX - RAD_MIN) * (float)n / (8.0f - 1.0f);
- ImGui::BeginGroup();
+ BeginGroup();
// N is not always exact here due to how PathArcTo() function work internally
- ImGui::Text("R: %.f\nN: %d", rad, draw_list->_CalcCircleAutoSegmentCount(rad));
+ Text("R: %.f\nN: %d", rad, draw_list->_CalcCircleAutoSegmentCount(rad));
const float canvas_width = IM_MAX(min_widget_width, rad * 2.0f);
const float offset_x = floorf(canvas_width * 0.5f);
const float offset_y = floorf(RAD_MAX);
- const ImVec2 p1 = ImGui::GetCursorScreenPos();
- draw_list->AddCircle(ImVec2(p1.x + offset_x, p1.y + offset_y), rad, ImGui::GetColorU32(ImGuiCol_Text));
- ImGui::Dummy(ImVec2(canvas_width, RAD_MAX * 2));
+ const ImVec2 p1 = GetCursorScreenPos();
+ draw_list->AddCircle(ImVec2(p1.x + offset_x, p1.y + offset_y), rad, GetColorU32(ImGuiCol_Text));
+ Dummy(ImVec2(canvas_width, RAD_MAX * 2));
/*
- const ImVec2 p2 = ImGui::GetCursorScreenPos();
- draw_list->AddCircleFilled(ImVec2(p2.x + offset_x, p2.y + offset_y), rad, ImGui::GetColorU32(ImGuiCol_Text));
- ImGui::Dummy(ImVec2(canvas_width, RAD_MAX * 2));
+ const ImVec2 p2 = GetCursorScreenPos();
+ draw_list->AddCircleFilled(ImVec2(p2.x + offset_x, p2.y + offset_y), rad, GetColorU32(ImGuiCol_Text));
+ Dummy(ImVec2(canvas_width, RAD_MAX * 2));
*/
- ImGui::EndGroup();
- ImGui::SameLine();
+ EndGroup();
+ SameLine();
}
- ImGui::EndTooltip();
+ EndTooltip();
}
- ImGui::SameLine();
+ SameLine();
HelpMarker("When drawing circle primitives with \"num_segments == 0\" tessellation will be calculated automatically.");
- ImGui::DragFloat("Global Alpha", &style.Alpha, 0.005f, 0.20f, 1.0f, "%.2f"); // Not exposing zero here so user doesn't "lose" the UI (zero alpha clips all widgets). But application code could have a toggle to switch between zero and non-zero.
- ImGui::DragFloat("Disabled Alpha", &style.DisabledAlpha, 0.005f, 0.0f, 1.0f, "%.2f"); ImGui::SameLine(); HelpMarker("Additional alpha multiplier for disabled items (multiply over current value of Alpha).");
- ImGui::PopItemWidth();
+ DragFloat("Global Alpha", &style.Alpha, 0.005f, 0.20f, 1.0f, "%.2f"); // Not exposing zero here so user doesn't "lose" the UI (zero alpha clips all widgets). But application code could have a toggle to switch between zero and non-zero.
+ DragFloat("Disabled Alpha", &style.DisabledAlpha, 0.005f, 0.0f, 1.0f, "%.2f"); SameLine(); HelpMarker("Additional alpha multiplier for disabled items (multiply over current value of Alpha).");
+ PopItemWidth();
- ImGui::EndTabItem();
+ EndTabItem();
}
- ImGui::EndTabBar();
+ EndTabBar();
}
-
- ImGui::PopItemWidth();
+ PopItemWidth();
}
//-----------------------------------------------------------------------------
// [SECTION] User Guide / ShowUserGuide()
//-----------------------------------------------------------------------------
+// We omit the ImGui:: prefix in this function, as we don't expect user to be copy and pasting this code.
void ImGui::ShowUserGuide()
{
- ImGuiIO& io = ImGui::GetIO();
- ImGui::BulletText("Double-click on title bar to collapse window.");
- ImGui::BulletText(
+ ImGuiIO& io = GetIO();
+ BulletText("Double-click on title bar to collapse window.");
+ BulletText(
"Click and drag on lower corner to resize window\n"
"(double-click to auto fit window to its contents).");
- ImGui::BulletText("CTRL+Click on a slider or drag box to input value as text.");
- ImGui::BulletText("TAB/SHIFT+TAB to cycle through keyboard editable fields.");
- ImGui::BulletText("CTRL+Tab to select a window.");
+ BulletText("CTRL+Click on a slider or drag box to input value as text.");
+ BulletText("TAB/SHIFT+TAB to cycle through keyboard editable fields.");
+ BulletText("CTRL+Tab to select a window.");
if (io.FontAllowUserScaling)
- ImGui::BulletText("CTRL+Mouse Wheel to zoom window contents.");
- ImGui::BulletText("While inputting text:\n");
- ImGui::Indent();
- ImGui::BulletText("CTRL+Left/Right to word jump.");
- ImGui::BulletText("CTRL+A or double-click to select all.");
- ImGui::BulletText("CTRL+X/C/V to use clipboard cut/copy/paste.");
- ImGui::BulletText("CTRL+Z to undo, CTRL+Y/CTRL+SHIFT+Z to redo.");
- ImGui::BulletText("ESCAPE to revert.");
- ImGui::Unindent();
- ImGui::BulletText("With keyboard navigation enabled:");
- ImGui::Indent();
- ImGui::BulletText("Arrow keys to navigate.");
- ImGui::BulletText("Space to activate a widget.");
- ImGui::BulletText("Return to input text into a widget.");
- ImGui::BulletText("Escape to deactivate a widget, close popup, exit child window.");
- ImGui::BulletText("Alt to jump to the menu layer of a window.");
- ImGui::Unindent();
+ BulletText("CTRL+Mouse Wheel to zoom window contents.");
+ BulletText("While inputting text:\n");
+ Indent();
+ BulletText("CTRL+Left/Right to word jump.");
+ BulletText("CTRL+A or double-click to select all.");
+ BulletText("CTRL+X/C/V to use clipboard cut/copy/paste.");
+ BulletText("CTRL+Z to undo, CTRL+Y/CTRL+SHIFT+Z to redo.");
+ BulletText("ESCAPE to revert.");
+ Unindent();
+ BulletText("With keyboard navigation enabled:");
+ Indent();
+ BulletText("Arrow keys to navigate.");
+ BulletText("Space to activate a widget.");
+ BulletText("Return to input text into a widget.");
+ BulletText("Escape to deactivate a widget, close popup, exit child window.");
+ BulletText("Alt to jump to the menu layer of a window.");
+ Unindent();
}
//-----------------------------------------------------------------------------
@@ -9234,10 +9416,9 @@ static void ShowExampleAppLayout(bool* p_open)
ImGui::BeginChild("left pane", ImVec2(150, 0), ImGuiChildFlags_Borders | ImGuiChildFlags_ResizeX);
for (int i = 0; i < 100; i++)
{
- // FIXME: Good candidate to use ImGuiSelectableFlags_SelectOnNav
char label[128];
sprintf(label, "MyObject %d", i);
- if (ImGui::Selectable(label, selected == i))
+ if (ImGui::Selectable(label, selected == i, ImGuiSelectableFlags_SelectOnNav))
selected = i;
}
ImGui::EndChild();
@@ -9384,8 +9565,10 @@ struct ExampleAppPropertyEditor
ImGui::TableNextColumn();
ImGui::PushID(node->UID);
ImGuiTreeNodeFlags tree_flags = ImGuiTreeNodeFlags_None;
- tree_flags |= ImGuiTreeNodeFlags_OpenOnArrow | ImGuiTreeNodeFlags_OpenOnDoubleClick; // Standard opening mode as we are likely to want to add selection afterwards
- tree_flags |= ImGuiTreeNodeFlags_NavLeftJumpsBackHere; // Left arrow support
+ tree_flags |= ImGuiTreeNodeFlags_OpenOnArrow | ImGuiTreeNodeFlags_OpenOnDoubleClick;// Standard opening mode as we are likely to want to add selection afterwards
+ tree_flags |= ImGuiTreeNodeFlags_NavLeftJumpsToParent; // Left arrow support
+ tree_flags |= ImGuiTreeNodeFlags_SpanFullWidth; // Span full width for easier mouse reach
+ tree_flags |= ImGuiTreeNodeFlags_DrawLinesToNodes; // Always draw hierarchy outlines
if (node == VisibleNode)
tree_flags |= ImGuiTreeNodeFlags_Selected;
if (node->Childs.Size == 0)
@@ -9587,7 +9770,7 @@ static void ShowExampleAppConstrainedResize(bool* p_open)
IMGUI_DEMO_MARKER("Examples/Constrained Resizing window");
if (ImGui::GetIO().KeyShift)
{
- // Display a dummy viewport (in your real app you would likely use ImageButton() to display a texture.
+ // Display a dummy viewport (in your real app you would likely use ImageButton() to display a texture)
ImVec2 avail_size = ImGui::GetContentRegionAvail();
ImVec2 pos = ImGui::GetCursorScreenPos();
ImGui::ColorButton("viewport", ImVec4(0.5f, 0.2f, 0.5f, 1.0f), ImGuiColorEditFlags_NoTooltip | ImGuiColorEditFlags_NoDragDrop, avail_size);
@@ -10976,9 +11159,8 @@ void ImGui::ShowAboutWindow(bool*) {}
void ImGui::ShowDemoWindow(bool*) {}
void ImGui::ShowUserGuide() {}
void ImGui::ShowStyleEditor(ImGuiStyle*) {}
-bool ImGui::ShowStyleSelector(const char* label) { return false; }
-void ImGui::ShowFontSelector(const char* label) {}
+bool ImGui::ShowStyleSelector(const char*) { return false; }
-#endif
+#endif // #ifndef IMGUI_DISABLE_DEMO_WINDOWS
#endif // #ifndef IMGUI_DISABLE
diff --git a/imgui_draw.cpp b/imgui_draw.cpp
index 736213d8b..afc1e0900 100644
--- a/imgui_draw.cpp
+++ b/imgui_draw.cpp
@@ -1,4 +1,4 @@
-// dear imgui, v1.92.0 WIP
+// dear imgui, v1.92.4 WIP
// (drawing and font code)
/*
@@ -13,7 +13,8 @@ Index of this file:
// [SECTION] ImDrawData
// [SECTION] Helpers ShadeVertsXXX functions
// [SECTION] ImFontConfig
-// [SECTION] ImFontAtlas
+// [SECTION] ImFontAtlas, ImFontAtlasBuilder
+// [SECTION] ImFontAtlas: backend for stb_truetype
// [SECTION] ImFontAtlas: glyph ranges helpers
// [SECTION] ImFontGlyphRangesBuilder
// [SECTION] ImFont
@@ -39,6 +40,7 @@ Index of this file:
#endif
#include // vsnprintf, sscanf, printf
+#include // intptr_t
// Visual Studio warnings
#ifdef _MSC_VER
@@ -46,7 +48,7 @@ Index of this file:
#pragma warning (disable: 4505) // unreferenced local function has been removed (stb stuff)
#pragma warning (disable: 4996) // 'This function or variable may be unsafe': strcpy, strdup, sprintf, vsnprintf, sscanf, fopen
#pragma warning (disable: 26451) // [Static Analyzer] Arithmetic overflow : Using operator 'xxx' on a 4 byte value and then casting the result to a 8 byte value. Cast the value to the wider type before calling operator 'xxx' to avoid overflow(io.2).
-#pragma warning (disable: 26812) // [Static Analyzer] The enum type 'xxx' is unscoped. Prefer 'enum class' over 'enum' (Enum.3). [MSVC Static Analyzer)
+#pragma warning (disable: 26812) // [Static Analyzer] The enum type 'xxx' is unscoped. Prefer 'enum class' over 'enum' (Enum.3).
#endif
// Clang/GCC warnings with -Weverything
@@ -238,6 +240,7 @@ void ImGui::StyleColorsDark(ImGuiStyle* dst)
colors[ImGuiCol_TableRowBgAlt] = ImVec4(1.00f, 1.00f, 1.00f, 0.06f);
colors[ImGuiCol_TextLink] = colors[ImGuiCol_HeaderActive];
colors[ImGuiCol_TextSelectedBg] = ImVec4(0.26f, 0.59f, 0.98f, 0.35f);
+ colors[ImGuiCol_TreeLines] = colors[ImGuiCol_Border];
colors[ImGuiCol_DragDropTarget] = ImVec4(1.00f, 1.00f, 0.00f, 0.90f);
colors[ImGuiCol_NavCursor] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f);
colors[ImGuiCol_NavWindowingHighlight] = ImVec4(1.00f, 1.00f, 1.00f, 0.70f);
@@ -304,6 +307,7 @@ void ImGui::StyleColorsClassic(ImGuiStyle* dst)
colors[ImGuiCol_TableRowBgAlt] = ImVec4(1.00f, 1.00f, 1.00f, 0.07f);
colors[ImGuiCol_TextLink] = colors[ImGuiCol_HeaderActive];
colors[ImGuiCol_TextSelectedBg] = ImVec4(0.00f, 0.00f, 1.00f, 0.35f);
+ colors[ImGuiCol_TreeLines] = colors[ImGuiCol_Border];
colors[ImGuiCol_DragDropTarget] = ImVec4(1.00f, 1.00f, 0.00f, 0.90f);
colors[ImGuiCol_NavCursor] = colors[ImGuiCol_HeaderHovered];
colors[ImGuiCol_NavWindowingHighlight] = ImVec4(1.00f, 1.00f, 1.00f, 0.70f);
@@ -371,6 +375,7 @@ void ImGui::StyleColorsLight(ImGuiStyle* dst)
colors[ImGuiCol_TableRowBgAlt] = ImVec4(0.30f, 0.30f, 0.30f, 0.09f);
colors[ImGuiCol_TextLink] = colors[ImGuiCol_HeaderActive];
colors[ImGuiCol_TextSelectedBg] = ImVec4(0.26f, 0.59f, 0.98f, 0.35f);
+ colors[ImGuiCol_TreeLines] = colors[ImGuiCol_Border];
colors[ImGuiCol_DragDropTarget] = ImVec4(0.26f, 0.59f, 0.98f, 0.95f);
colors[ImGuiCol_NavCursor] = colors[ImGuiCol_HeaderHovered];
colors[ImGuiCol_NavWindowingHighlight] = ImVec4(0.70f, 0.70f, 0.70f, 0.70f);
@@ -394,6 +399,11 @@ ImDrawListSharedData::ImDrawListSharedData()
ArcFastRadiusCutoff = IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_CALC_R(IM_DRAWLIST_ARCFAST_SAMPLE_MAX, CircleSegmentMaxError);
}
+ImDrawListSharedData::~ImDrawListSharedData()
+{
+ IM_ASSERT(DrawLists.Size == 0);
+}
+
void ImDrawListSharedData::SetCircleTessellationMaxError(float max_error)
{
if (CircleSegmentMaxError == max_error)
@@ -412,22 +422,32 @@ void ImDrawListSharedData::SetCircleTessellationMaxError(float max_error)
ImDrawList::ImDrawList(ImDrawListSharedData* shared_data)
{
memset(this, 0, sizeof(*this));
- _Data = shared_data;
+ _SetDrawListSharedData(shared_data);
}
ImDrawList::~ImDrawList()
{
_ClearFreeMemory();
+ _SetDrawListSharedData(NULL);
+}
+
+void ImDrawList::_SetDrawListSharedData(ImDrawListSharedData* data)
+{
+ if (_Data != NULL)
+ _Data->DrawLists.find_erase_unsorted(this);
+ _Data = data;
+ if (_Data != NULL)
+ _Data->DrawLists.push_back(this);
}
// Initialize before use in a new frame. We always have a command ready in the buffer.
-// In the majority of cases, you would want to call PushClipRect() and PushTextureID() after this.
+// In the majority of cases, you would want to call PushClipRect() and PushTexture() after this.
void ImDrawList::_ResetForNewFrame()
{
// Verify that the ImDrawCmd fields we want to memcmp() are contiguous in memory.
IM_STATIC_ASSERT(offsetof(ImDrawCmd, ClipRect) == 0);
- IM_STATIC_ASSERT(offsetof(ImDrawCmd, TextureId) == sizeof(ImVec4));
- IM_STATIC_ASSERT(offsetof(ImDrawCmd, VtxOffset) == sizeof(ImVec4) + sizeof(ImTextureID));
+ IM_STATIC_ASSERT(offsetof(ImDrawCmd, TexRef) == sizeof(ImVec4));
+ IM_STATIC_ASSERT(offsetof(ImDrawCmd, VtxOffset) == sizeof(ImVec4) + sizeof(ImTextureRef));
if (_Splitter._Count > 1)
_Splitter.Merge(this);
@@ -440,7 +460,7 @@ void ImDrawList::_ResetForNewFrame()
_VtxWritePtr = NULL;
_IdxWritePtr = NULL;
_ClipRectStack.resize(0);
- _TextureIdStack.resize(0);
+ _TextureStack.resize(0);
_CallbacksDataBuf.resize(0);
_Path.resize(0);
_Splitter.Clear();
@@ -458,15 +478,16 @@ void ImDrawList::_ClearFreeMemory()
_VtxWritePtr = NULL;
_IdxWritePtr = NULL;
_ClipRectStack.clear();
- _TextureIdStack.clear();
+ _TextureStack.clear();
_CallbacksDataBuf.clear();
_Path.clear();
_Splitter.ClearFreeMemory();
}
+// Note: For multi-threaded rendering, consider using `imgui_threaded_rendering` from https://github.com/ocornut/imgui_club
ImDrawList* ImDrawList::CloneOutput() const
{
- ImDrawList* dst = IM_NEW(ImDrawList(_Data));
+ ImDrawList* dst = IM_NEW(ImDrawList(NULL));
dst->CmdBuffer = CmdBuffer;
dst->IdxBuffer = IdxBuffer;
dst->VtxBuffer = VtxBuffer;
@@ -478,7 +499,7 @@ void ImDrawList::AddDrawCmd()
{
ImDrawCmd draw_cmd;
draw_cmd.ClipRect = _CmdHeader.ClipRect; // Same as calling ImDrawCmd_HeaderCopy()
- draw_cmd.TextureId = _CmdHeader.TextureId;
+ draw_cmd.TexRef = _CmdHeader.TexRef;
draw_cmd.VtxOffset = _CmdHeader.VtxOffset;
draw_cmd.IdxOffset = IdxBuffer.Size;
@@ -503,6 +524,7 @@ void ImDrawList::AddCallback(ImDrawCallback callback, void* userdata, size_t use
{
IM_ASSERT_PARANOID(CmdBuffer.Size > 0);
ImDrawCmd* curr_cmd = &CmdBuffer.Data[CmdBuffer.Size - 1];
+ IM_ASSERT(callback != NULL);
IM_ASSERT(curr_cmd->UserCallback == NULL);
if (curr_cmd->ElemCount != 0)
{
@@ -533,10 +555,10 @@ void ImDrawList::AddCallback(ImDrawCallback callback, void* userdata, size_t use
AddDrawCmd(); // Force a new command after us (see comment below)
}
-// Compare ClipRect, TextureId and VtxOffset with a single memcmp()
+// Compare ClipRect, TexRef and VtxOffset with a single memcmp()
#define ImDrawCmd_HeaderSize (offsetof(ImDrawCmd, VtxOffset) + sizeof(unsigned int))
-#define ImDrawCmd_HeaderCompare(CMD_LHS, CMD_RHS) (memcmp(CMD_LHS, CMD_RHS, ImDrawCmd_HeaderSize)) // Compare ClipRect, TextureId, VtxOffset
-#define ImDrawCmd_HeaderCopy(CMD_DST, CMD_SRC) (memcpy(CMD_DST, CMD_SRC, ImDrawCmd_HeaderSize)) // Copy ClipRect, TextureId, VtxOffset
+#define ImDrawCmd_HeaderCompare(CMD_LHS, CMD_RHS) (memcmp(CMD_LHS, CMD_RHS, ImDrawCmd_HeaderSize)) // Compare ClipRect, TexRef, VtxOffset
+#define ImDrawCmd_HeaderCopy(CMD_DST, CMD_SRC) (memcpy(CMD_DST, CMD_SRC, ImDrawCmd_HeaderSize)) // Copy ClipRect, TexRef, VtxOffset
#define ImDrawCmd_AreSequentialIdxOffset(CMD_0, CMD_1) (CMD_0->IdxOffset + CMD_0->ElemCount == CMD_1->IdxOffset)
// Try to merge two last draw commands
@@ -576,17 +598,20 @@ void ImDrawList::_OnChangedClipRect()
curr_cmd->ClipRect = _CmdHeader.ClipRect;
}
-void ImDrawList::_OnChangedTextureID()
+void ImDrawList::_OnChangedTexture()
{
// If current command is used with different settings we need to add a new command
IM_ASSERT_PARANOID(CmdBuffer.Size > 0);
ImDrawCmd* curr_cmd = &CmdBuffer.Data[CmdBuffer.Size - 1];
- if (curr_cmd->ElemCount != 0 && curr_cmd->TextureId != _CmdHeader.TextureId)
+ if (curr_cmd->ElemCount != 0 && curr_cmd->TexRef != _CmdHeader.TexRef)
{
AddDrawCmd();
return;
}
- IM_ASSERT(curr_cmd->UserCallback == NULL);
+
+ // Unlike other _OnChangedXXX functions this may be called by ImFontAtlasUpdateDrawListsTextures() in more locations so we need to handle this case.
+ if (curr_cmd->UserCallback != NULL)
+ return;
// Try to merge with previous command if it matches, else use current command
ImDrawCmd* prev_cmd = curr_cmd - 1;
@@ -595,7 +620,7 @@ void ImDrawList::_OnChangedTextureID()
CmdBuffer.pop_back();
return;
}
- curr_cmd->TextureId = _CmdHeader.TextureId;
+ curr_cmd->TexRef = _CmdHeader.TexRef;
}
void ImDrawList::_OnChangedVtxOffset()
@@ -656,27 +681,30 @@ void ImDrawList::PopClipRect()
_OnChangedClipRect();
}
-void ImDrawList::PushTextureID(ImTextureID texture_id)
+void ImDrawList::PushTexture(ImTextureRef tex_ref)
{
- _TextureIdStack.push_back(texture_id);
- _CmdHeader.TextureId = texture_id;
- _OnChangedTextureID();
+ _TextureStack.push_back(tex_ref);
+ _CmdHeader.TexRef = tex_ref;
+ if (tex_ref._TexData != NULL)
+ IM_ASSERT(tex_ref._TexData->WantDestroyNextFrame == false);
+ _OnChangedTexture();
}
-void ImDrawList::PopTextureID()
+void ImDrawList::PopTexture()
{
- _TextureIdStack.pop_back();
- _CmdHeader.TextureId = (_TextureIdStack.Size == 0) ? (ImTextureID)NULL : _TextureIdStack.Data[_TextureIdStack.Size - 1];
- _OnChangedTextureID();
+ _TextureStack.pop_back();
+ _CmdHeader.TexRef = (_TextureStack.Size == 0) ? ImTextureRef() : _TextureStack.Data[_TextureStack.Size - 1];
+ _OnChangedTexture();
}
-// This is used by ImGui::PushFont()/PopFont(). It works because we never use _TextureIdStack[] elsewhere than in PushTextureID()/PopTextureID().
-void ImDrawList::_SetTextureID(ImTextureID texture_id)
+// This is used by ImGui::PushFont()/PopFont(). It works because we never use _TextureIdStack[] elsewhere than in PushTexture()/PopTexture().
+void ImDrawList::_SetTexture(ImTextureRef tex_ref)
{
- if (_CmdHeader.TextureId == texture_id)
+ if (_CmdHeader.TexRef == tex_ref)
return;
- _CmdHeader.TextureId = texture_id;
- _OnChangedTextureID();
+ _CmdHeader.TexRef = tex_ref;
+ _TextureStack.back() = tex_ref;
+ _OnChangedTexture();
}
// Reserve space for a number of vertices and indices.
@@ -1689,8 +1717,6 @@ void ImDrawList::AddText(ImFont* font, float font_size, const ImVec2& pos, ImU32
if (font_size == 0.0f)
font_size = _Data->FontSize;
- IM_ASSERT(font->ContainerAtlas->TexID == _CmdHeader.TextureId); // Use high-level ImGui::PushFont() or low-level ImDrawList::PushTextureId() to change font.
-
ImVec4 clip_rect = _CmdHeader.ClipRect;
if (cpu_fine_clip_rect)
{
@@ -1699,7 +1725,7 @@ void ImDrawList::AddText(ImFont* font, float font_size, const ImVec2& pos, ImU32
clip_rect.z = ImMin(clip_rect.z, cpu_fine_clip_rect->z);
clip_rect.w = ImMin(clip_rect.w, cpu_fine_clip_rect->w);
}
- font->RenderText(this, font_size, pos, col, clip_rect, text_begin, text_end, wrap_width, cpu_fine_clip_rect != NULL);
+ font->RenderText(this, font_size, pos, col, clip_rect, text_begin, text_end, wrap_width, (cpu_fine_clip_rect != NULL) ? ImDrawTextFlags_CpuFineClip : ImDrawTextFlags_None);
}
void ImDrawList::AddText(const ImVec2& pos, ImU32 col, const char* text_begin, const char* text_end)
@@ -1707,39 +1733,39 @@ void ImDrawList::AddText(const ImVec2& pos, ImU32 col, const char* text_begin, c
AddText(_Data->Font, _Data->FontSize, pos, col, text_begin, text_end);
}
-void ImDrawList::AddImage(ImTextureID user_texture_id, const ImVec2& p_min, const ImVec2& p_max, const ImVec2& uv_min, const ImVec2& uv_max, ImU32 col)
+void ImDrawList::AddImage(ImTextureRef tex_ref, const ImVec2& p_min, const ImVec2& p_max, const ImVec2& uv_min, const ImVec2& uv_max, ImU32 col)
{
if ((col & IM_COL32_A_MASK) == 0)
return;
- const bool push_texture_id = user_texture_id != _CmdHeader.TextureId;
+ const bool push_texture_id = tex_ref != _CmdHeader.TexRef;
if (push_texture_id)
- PushTextureID(user_texture_id);
+ PushTexture(tex_ref);
PrimReserve(6, 4);
PrimRectUV(p_min, p_max, uv_min, uv_max, col);
if (push_texture_id)
- PopTextureID();
+ PopTexture();
}
-void ImDrawList::AddImageQuad(ImTextureID user_texture_id, const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, const ImVec2& uv1, const ImVec2& uv2, const ImVec2& uv3, const ImVec2& uv4, ImU32 col)
+void ImDrawList::AddImageQuad(ImTextureRef tex_ref, const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, const ImVec2& uv1, const ImVec2& uv2, const ImVec2& uv3, const ImVec2& uv4, ImU32 col)
{
if ((col & IM_COL32_A_MASK) == 0)
return;
- const bool push_texture_id = user_texture_id != _CmdHeader.TextureId;
+ const bool push_texture_id = tex_ref != _CmdHeader.TexRef;
if (push_texture_id)
- PushTextureID(user_texture_id);
+ PushTexture(tex_ref);
PrimReserve(6, 4);
PrimQuadUV(p1, p2, p3, p4, uv1, uv2, uv3, uv4, col);
if (push_texture_id)
- PopTextureID();
+ PopTexture();
}
-void ImDrawList::AddImageRounded(ImTextureID user_texture_id, const ImVec2& p_min, const ImVec2& p_max, const ImVec2& uv_min, const ImVec2& uv_max, ImU32 col, float rounding, ImDrawFlags flags)
+void ImDrawList::AddImageRounded(ImTextureRef tex_ref, const ImVec2& p_min, const ImVec2& p_max, const ImVec2& uv_min, const ImVec2& uv_max, ImU32 col, float rounding, ImDrawFlags flags)
{
if ((col & IM_COL32_A_MASK) == 0)
return;
@@ -1747,13 +1773,13 @@ void ImDrawList::AddImageRounded(ImTextureID user_texture_id, const ImVec2& p_mi
flags = FixRectCornerFlags(flags);
if (rounding < 0.5f || (flags & ImDrawFlags_RoundCornersMask_) == ImDrawFlags_RoundCornersNone)
{
- AddImage(user_texture_id, p_min, p_max, uv_min, uv_max, col);
+ AddImage(tex_ref, p_min, p_max, uv_min, uv_max, col);
return;
}
- const bool push_texture_id = user_texture_id != _CmdHeader.TextureId;
+ const bool push_texture_id = tex_ref != _CmdHeader.TexRef;
if (push_texture_id)
- PushTextureID(user_texture_id);
+ PushTexture(tex_ref);
int vert_start_idx = VtxBuffer.Size;
PathRect(p_min, p_max, rounding, flags);
@@ -1762,7 +1788,7 @@ void ImDrawList::AddImageRounded(ImTextureID user_texture_id, const ImVec2& p_mi
ImGui::ShadeVertsLinearUV(this, vert_start_idx, vert_end_idx, p_min, p_max, uv_min, uv_max, true);
if (push_texture_id)
- PopTextureID();
+ PopTexture();
}
//-----------------------------------------------------------------------------
@@ -2190,7 +2216,7 @@ void ImDrawListSplitter::Merge(ImDrawList* draw_list)
// If current command is used with different settings we need to add a new command
ImDrawCmd* curr_cmd = &draw_list->CmdBuffer.Data[draw_list->CmdBuffer.Size - 1];
if (curr_cmd->ElemCount == 0)
- ImDrawCmd_HeaderCopy(curr_cmd, &draw_list->_CmdHeader); // Copy ClipRect, TextureId, VtxOffset
+ ImDrawCmd_HeaderCopy(curr_cmd, &draw_list->_CmdHeader); // Copy ClipRect, TexRef, VtxOffset
else if (ImDrawCmd_HeaderCompare(curr_cmd, &draw_list->_CmdHeader) != 0)
draw_list->AddDrawCmd();
@@ -2216,7 +2242,7 @@ void ImDrawListSplitter::SetCurrentChannel(ImDrawList* draw_list, int idx)
if (curr_cmd == NULL)
draw_list->AddDrawCmd();
else if (curr_cmd->ElemCount == 0)
- ImDrawCmd_HeaderCopy(curr_cmd, &draw_list->_CmdHeader); // Copy ClipRect, TextureId, VtxOffset
+ ImDrawCmd_HeaderCopy(curr_cmd, &draw_list->_CmdHeader); // Copy ClipRect, TexRef, VtxOffset
else if (ImDrawCmd_HeaderCompare(curr_cmd, &draw_list->_CmdHeader) != 0)
draw_list->AddDrawCmd();
}
@@ -2232,6 +2258,7 @@ void ImDrawData::Clear()
CmdLists.resize(0); // The ImDrawList are NOT owned by ImDrawData but e.g. by ImGuiContext, so we don't clear them.
DisplayPos = DisplaySize = FramebufferScale = ImVec2(0.0f, 0.0f);
OwnerViewport = NULL;
+ Textures = NULL;
}
// Important: 'out_list' is generally going to be draw_data->CmdLists, but may be another temporary list
@@ -2293,17 +2320,16 @@ void ImDrawData::DeIndexAllBuffers()
{
ImVector new_vtx_buffer;
TotalVtxCount = TotalIdxCount = 0;
- for (int i = 0; i < CmdListsCount; i++)
+ for (ImDrawList* draw_list : CmdLists)
{
- ImDrawList* cmd_list = CmdLists[i];
- if (cmd_list->IdxBuffer.empty())
+ if (draw_list->IdxBuffer.empty())
continue;
- new_vtx_buffer.resize(cmd_list->IdxBuffer.Size);
- for (int j = 0; j < cmd_list->IdxBuffer.Size; j++)
- new_vtx_buffer[j] = cmd_list->VtxBuffer[cmd_list->IdxBuffer[j]];
- cmd_list->VtxBuffer.swap(new_vtx_buffer);
- cmd_list->IdxBuffer.resize(0);
- TotalVtxCount += cmd_list->VtxBuffer.Size;
+ new_vtx_buffer.resize(draw_list->IdxBuffer.Size);
+ for (int j = 0; j < draw_list->IdxBuffer.Size; j++)
+ new_vtx_buffer[j] = draw_list->VtxBuffer[draw_list->IdxBuffer[j]];
+ draw_list->VtxBuffer.swap(new_vtx_buffer);
+ draw_list->IdxBuffer.resize(0);
+ TotalVtxCount += draw_list->VtxBuffer.Size;
}
}
@@ -2382,6 +2408,7 @@ void ImGui::ShadeVertsTransformPos(ImDrawList* draw_list, int vert_start_idx, in
// [SECTION] ImFontConfig
//-----------------------------------------------------------------------------
+// FIXME-NEWATLAS: Oversample specification could be more dynamic. For now, favoring automatic selection.
ImFontConfig::ImFontConfig()
{
memset(this, 0, sizeof(*this));
@@ -2395,39 +2422,157 @@ ImFontConfig::ImFontConfig()
}
//-----------------------------------------------------------------------------
-// [SECTION] ImFontAtlas
+// [SECTION] ImTextureData
+//-----------------------------------------------------------------------------
+// - ImTextureData::Create()
+// - ImTextureData::DestroyPixels()
+//-----------------------------------------------------------------------------
+
+int ImTextureDataGetFormatBytesPerPixel(ImTextureFormat format)
+{
+ switch (format)
+ {
+ case ImTextureFormat_Alpha8: return 1;
+ case ImTextureFormat_RGBA32: return 4;
+ }
+ IM_ASSERT(0);
+ return 0;
+}
+
+const char* ImTextureDataGetStatusName(ImTextureStatus status)
+{
+ switch (status)
+ {
+ case ImTextureStatus_OK: return "OK";
+ case ImTextureStatus_Destroyed: return "Destroyed";
+ case ImTextureStatus_WantCreate: return "WantCreate";
+ case ImTextureStatus_WantUpdates: return "WantUpdates";
+ case ImTextureStatus_WantDestroy: return "WantDestroy";
+ }
+ return "N/A";
+}
+
+const char* ImTextureDataGetFormatName(ImTextureFormat format)
+{
+ switch (format)
+ {
+ case ImTextureFormat_Alpha8: return "Alpha8";
+ case ImTextureFormat_RGBA32: return "RGBA32";
+ }
+ return "N/A";
+}
+
+void ImTextureData::Create(ImTextureFormat format, int w, int h)
+{
+ IM_ASSERT(Status == ImTextureStatus_Destroyed);
+ DestroyPixels();
+ Format = format;
+ Status = ImTextureStatus_WantCreate;
+ Width = w;
+ Height = h;
+ BytesPerPixel = ImTextureDataGetFormatBytesPerPixel(format);
+ UseColors = false;
+ Pixels = (unsigned char*)IM_ALLOC(Width * Height * BytesPerPixel);
+ IM_ASSERT(Pixels != NULL);
+ memset(Pixels, 0, Width * Height * BytesPerPixel);
+ UsedRect.x = UsedRect.y = UsedRect.w = UsedRect.h = 0;
+ UpdateRect.x = UpdateRect.y = (unsigned short)~0;
+ UpdateRect.w = UpdateRect.h = 0;
+}
+
+void ImTextureData::DestroyPixels()
+{
+ if (Pixels)
+ IM_FREE(Pixels);
+ Pixels = NULL;
+ UseColors = false;
+}
+
+//-----------------------------------------------------------------------------
+// [SECTION] ImFontAtlas, ImFontAtlasBuilder
//-----------------------------------------------------------------------------
// - Default texture data encoded in ASCII
+// - ImFontAtlas()
+// - ImFontAtlas::Clear()
+// - ImFontAtlas::CompactCache()
// - ImFontAtlas::ClearInputData()
// - ImFontAtlas::ClearTexData()
// - ImFontAtlas::ClearFonts()
-// - ImFontAtlas::Clear()
-// - ImFontAtlas::GetTexDataAsAlpha8()
-// - ImFontAtlas::GetTexDataAsRGBA32()
+//-----------------------------------------------------------------------------
+// - ImFontAtlasUpdateNewFrame()
+// - ImFontAtlasTextureBlockConvert()
+// - ImFontAtlasTextureBlockPostProcess()
+// - ImFontAtlasTextureBlockPostProcessMultiply()
+// - ImFontAtlasTextureBlockFill()
+// - ImFontAtlasTextureBlockCopy()
+// - ImFontAtlasTextureBlockQueueUpload()
+//-----------------------------------------------------------------------------
+// - ImFontAtlas::GetTexDataAsAlpha8() [legacy]
+// - ImFontAtlas::GetTexDataAsRGBA32() [legacy]
+// - ImFontAtlas::Build() [legacy]
+//-----------------------------------------------------------------------------
// - ImFontAtlas::AddFont()
// - ImFontAtlas::AddFontDefault()
// - ImFontAtlas::AddFontFromFileTTF()
// - ImFontAtlas::AddFontFromMemoryTTF()
// - ImFontAtlas::AddFontFromMemoryCompressedTTF()
// - ImFontAtlas::AddFontFromMemoryCompressedBase85TTF()
-// - ImFontAtlas::AddCustomRectRegular()
-// - ImFontAtlas::AddCustomRectFontGlyph()
-// - ImFontAtlas::CalcCustomRectUV()
+// - ImFontAtlas::RemoveFont()
+// - ImFontAtlasBuildNotifySetFont()
+//-----------------------------------------------------------------------------
+// - ImFontAtlas::AddCustomRect()
+// - ImFontAtlas::RemoveCustomRect()
+// - ImFontAtlas::GetCustomRect()
+// - ImFontAtlas::AddCustomRectFontGlyph() [legacy]
+// - ImFontAtlas::AddCustomRectFontGlyphForSize() [legacy]
// - ImFontAtlasGetMouseCursorTexData()
-// - ImFontAtlas::Build()
-// - ImFontAtlasBuildMultiplyCalcLookupTable()
-// - ImFontAtlasBuildMultiplyRectAlpha8()
-// - ImFontAtlasBuildWithStbTruetype()
-// - ImFontAtlasGetBuilderForStbTruetype()
-// - ImFontAtlasUpdateSourcesPointers()
-// - ImFontAtlasBuildSetupFont()
-// - ImFontAtlasBuildPackCustomRects()
-// - ImFontAtlasBuildRender8bppRectFromString()
-// - ImFontAtlasBuildRender32bppRectFromString()
-// - ImFontAtlasBuildRenderDefaultTexData()
-// - ImFontAtlasBuildRenderLinesTexData()
+//-----------------------------------------------------------------------------
+// - ImFontAtlasBuildMain()
+// - ImFontAtlasBuildSetupFontLoader()
+// - ImFontAtlasBuildPreloadAllGlyphRanges()
+// - ImFontAtlasBuildUpdatePointers()
+// - ImFontAtlasBuildRenderBitmapFromString()
+// - ImFontAtlasBuildUpdateBasicTexData()
+// - ImFontAtlasBuildUpdateLinesTexData()
+// - ImFontAtlasBuildAddFont()
+// - ImFontAtlasBuildSetupFontBakedEllipsis()
+// - ImFontAtlasBuildSetupFontBakedBlanks()
+// - ImFontAtlasBuildSetupFontBakedFallback()
+// - ImFontAtlasBuildSetupFontSpecialGlyphs()
+// - ImFontAtlasBuildDiscardBakes()
+// - ImFontAtlasBuildDiscardFontBakedGlyph()
+// - ImFontAtlasBuildDiscardFontBaked()
+// - ImFontAtlasBuildDiscardFontBakes()
+//-----------------------------------------------------------------------------
+// - ImFontAtlasAddDrawListSharedData()
+// - ImFontAtlasRemoveDrawListSharedData()
+// - ImFontAtlasUpdateDrawListsTextures()
+// - ImFontAtlasUpdateDrawListsSharedData()
+//-----------------------------------------------------------------------------
+// - ImFontAtlasBuildSetTexture()
+// - ImFontAtlasBuildAddTexture()
+// - ImFontAtlasBuildMakeSpace()
+// - ImFontAtlasBuildRepackTexture()
+// - ImFontAtlasBuildGrowTexture()
+// - ImFontAtlasBuildRepackOrGrowTexture()
+// - ImFontAtlasBuildGetTextureSizeEstimate()
+// - ImFontAtlasBuildCompactTexture()
// - ImFontAtlasBuildInit()
-// - ImFontAtlasBuildFinish()
+// - ImFontAtlasBuildDestroy()
+//-----------------------------------------------------------------------------
+// - ImFontAtlasPackInit()
+// - ImFontAtlasPackAllocRectEntry()
+// - ImFontAtlasPackReuseRectEntry()
+// - ImFontAtlasPackDiscardRect()
+// - ImFontAtlasPackAddRect()
+// - ImFontAtlasPackGetRect()
+//-----------------------------------------------------------------------------
+// - ImFontBaked_BuildGrowIndex()
+// - ImFontBaked_BuildLoadGlyph()
+// - ImFontBaked_BuildLoadGlyphAdvanceX()
+// - ImFontAtlasDebugLogTextureRequests()
+//-----------------------------------------------------------------------------
+// - ImFontAtlasGetFontLoaderForStbTruetype()
//-----------------------------------------------------------------------------
// A work of art lies ahead! (. = white layer, X = black layer, others are blank)
@@ -2482,143 +2627,467 @@ static const ImVec2 FONT_ATLAS_DEFAULT_TEX_CURSOR_DATA[ImGuiMouseCursor_COUNT][3
{ ImVec2(109,0),ImVec2(13,15), ImVec2( 6, 7) }, // ImGuiMouseCursor_NotAllowed
};
+#define IM_FONTGLYPH_INDEX_UNUSED ((ImU16)-1) // 0xFFFF
+#define IM_FONTGLYPH_INDEX_NOT_FOUND ((ImU16)-2) // 0xFFFE
+
ImFontAtlas::ImFontAtlas()
{
memset(this, 0, sizeof(*this));
+ TexDesiredFormat = ImTextureFormat_RGBA32;
TexGlyphPadding = 1;
- PackIdMouseCursors = PackIdLines = -1;
+ TexMinWidth = 512;
+ TexMinHeight = 128;
+ TexMaxWidth = 8192;
+ TexMaxHeight = 8192;
+ TexRef._TexID = ImTextureID_Invalid;
+ RendererHasTextures = false; // Assumed false by default, as apps can call e.g Atlas::Build() after backend init and before ImGui can update.
+ TexNextUniqueID = 1;
+ FontNextUniqueID = 1;
+ Builder = NULL;
}
ImFontAtlas::~ImFontAtlas()
{
- IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!");
- Clear();
+ IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas!");
+ RendererHasTextures = false; // Full Clear() is supported, but ClearTexData() only isn't.
+ ClearFonts();
+ ClearTexData();
+ TexList.clear_delete();
+ TexData = NULL;
}
-void ImFontAtlas::ClearInputData()
+void ImFontAtlas::Clear()
{
- IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!");
- for (ImFontConfig& font_cfg : Sources)
- if (font_cfg.FontData && font_cfg.FontDataOwnedByAtlas)
- {
- IM_FREE(font_cfg.FontData);
- font_cfg.FontData = NULL;
- }
+ bool backup_renderer_has_textures = RendererHasTextures;
+ RendererHasTextures = false; // Full Clear() is supported, but ClearTexData() only isn't.
+ ClearFonts();
+ ClearTexData();
+ RendererHasTextures = backup_renderer_has_textures;
+}
+
+void ImFontAtlas::CompactCache()
+{
+ ImFontAtlasTextureCompact(this);
+}
+
+void ImFontAtlas::SetFontLoader(const ImFontLoader* font_loader)
+{
+ ImFontAtlasBuildSetupFontLoader(this, font_loader);
+}
+
+void ImFontAtlas::ClearInputData()
+{
+ IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas!");
- // When clearing this we lose access to the font name and other information used to build the font.
for (ImFont* font : Fonts)
- if (font->Sources >= Sources.Data && font->Sources < Sources.Data + Sources.Size)
- {
- font->Sources = NULL;
- font->SourcesCount = 0;
- }
+ ImFontAtlasFontDestroyOutput(this, font);
+ for (ImFontConfig& font_cfg : Sources)
+ ImFontAtlasFontDestroySourceData(this, &font_cfg);
+ for (ImFont* font : Fonts)
+ {
+ // When clearing this we lose access to the font name and other information used to build the font.
+ font->Sources.clear();
+ font->Flags |= ImFontFlags_NoLoadGlyphs;
+ }
Sources.clear();
- CustomRects.clear();
- PackIdMouseCursors = PackIdLines = -1;
- // Important: we leave TexReady untouched
}
-void ImFontAtlas::ClearTexData()
+// Clear CPU-side copy of the texture data.
+void ImFontAtlas::ClearTexData()
{
- IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!");
- if (TexPixelsAlpha8)
- IM_FREE(TexPixelsAlpha8);
- if (TexPixelsRGBA32)
- IM_FREE(TexPixelsRGBA32);
- TexPixelsAlpha8 = NULL;
- TexPixelsRGBA32 = NULL;
- TexPixelsUseColors = false;
- // Important: we leave TexReady untouched
+ IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas!");
+ IM_ASSERT(RendererHasTextures == false && "Not supported for dynamic atlases, but you may call Clear().");
+ for (ImTextureData* tex : TexList)
+ tex->DestroyPixels();
+ //Locked = true; // Hoped to be able to lock this down but some reload patterns may not be happy with it.
}
-void ImFontAtlas::ClearFonts()
+void ImFontAtlas::ClearFonts()
{
- IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!");
+ // FIXME-NEWATLAS: Illegal to remove currently bound font.
+ IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas!");
+ ImFontAtlasBuildDestroy(this);
ClearInputData();
Fonts.clear_delete();
- TexReady = false;
-}
-
-void ImFontAtlas::Clear()
-{
- ClearInputData();
- ClearTexData();
- ClearFonts();
-}
-
-void ImFontAtlas::GetTexDataAsAlpha8(unsigned char** out_pixels, int* out_width, int* out_height, int* out_bytes_per_pixel)
-{
- // Build atlas on demand
- if (TexPixelsAlpha8 == NULL)
- Build();
-
- *out_pixels = TexPixelsAlpha8;
- if (out_width) *out_width = TexWidth;
- if (out_height) *out_height = TexHeight;
- if (out_bytes_per_pixel) *out_bytes_per_pixel = 1;
-}
-
-void ImFontAtlas::GetTexDataAsRGBA32(unsigned char** out_pixels, int* out_width, int* out_height, int* out_bytes_per_pixel)
-{
- // Convert to RGBA32 format on demand
- // Although it is likely to be the most commonly used format, our font rendering is 1 channel / 8 bpp
- if (!TexPixelsRGBA32)
- {
- unsigned char* pixels = NULL;
- GetTexDataAsAlpha8(&pixels, NULL, NULL);
- if (pixels)
+ TexIsBuilt = false;
+ for (ImDrawListSharedData* shared_data : DrawListSharedDatas)
+ if (shared_data->FontAtlas == this)
{
- TexPixelsRGBA32 = (unsigned int*)IM_ALLOC((size_t)TexWidth * (size_t)TexHeight * 4);
- const unsigned char* src = pixels;
- unsigned int* dst = TexPixelsRGBA32;
- for (int n = TexWidth * TexHeight; n > 0; n--)
- *dst++ = IM_COL32(255, 255, 255, (unsigned int)(*src++));
+ shared_data->Font = NULL;
+ shared_data->FontScale = shared_data->FontSize = 0.0f;
+ }
+}
+
+static void ImFontAtlasBuildUpdateRendererHasTexturesFromContext(ImFontAtlas* atlas)
+{
+ // [LEGACY] Copy back the ImGuiBackendFlags_RendererHasTextures flag from ImGui context.
+ // - This is the 1% exceptional case where that dependency if useful, to bypass an issue where otherwise at the
+ // time of an early call to Build(), it would be impossible for us to tell if the backend supports texture update.
+ // - Without this hack, we would have quite a pitfall as many legacy codebases have an early call to Build().
+ // Whereas conversely, the portion of people using ImDrawList without ImGui is expected to be pathologically rare.
+ for (ImDrawListSharedData* shared_data : atlas->DrawListSharedDatas)
+ if (ImGuiContext* imgui_ctx = shared_data->Context)
+ {
+ atlas->RendererHasTextures = (imgui_ctx->IO.BackendFlags & ImGuiBackendFlags_RendererHasTextures) != 0;
+ break;
+ }
+}
+
+// Called by NewFrame() for atlases owned by a context.
+// If you manually manage font atlases, you'll need to call this yourself.
+// - 'frame_count' needs to be provided because we can gc/prioritize baked fonts based on their age.
+// - 'frame_count' may not match those of all imgui contexts using this atlas, as contexts may be updated as different frequencies. But generally you can use ImGui::GetFrameCount() on one of your context.
+void ImFontAtlasUpdateNewFrame(ImFontAtlas* atlas, int frame_count, bool renderer_has_textures)
+{
+ IM_ASSERT(atlas->Builder == NULL || atlas->Builder->FrameCount < frame_count); // Protection against being called twice.
+ atlas->RendererHasTextures = renderer_has_textures;
+
+ // Check that font atlas was built or backend support texture reload in which case we can build now
+ if (atlas->RendererHasTextures)
+ {
+ atlas->TexIsBuilt = true;
+ if (atlas->Builder == NULL) // This will only happen if fonts were not already loaded.
+ ImFontAtlasBuildMain(atlas);
+ }
+ // Legacy backend
+ if (!atlas->RendererHasTextures)
+ IM_ASSERT_USER_ERROR(atlas->TexIsBuilt, "Backend does not support ImGuiBackendFlags_RendererHasTextures, and font atlas is not built! Update backend OR make sure you called ImGui_ImplXXXX_NewFrame() function for renderer backend, which should call io.Fonts->GetTexDataAsRGBA32() / GetTexDataAsAlpha8().");
+ if (atlas->TexIsBuilt && atlas->Builder->PreloadedAllGlyphsRanges)
+ IM_ASSERT_USER_ERROR(atlas->RendererHasTextures == false, "Called ImFontAtlas::Build() before ImGuiBackendFlags_RendererHasTextures got set! With new backends: you don't need to call Build().");
+
+ // Clear BakedCurrent cache, this is important because it ensure the uncached path gets taken once.
+ // We also rely on ImFontBaked* pointers never crossing frames.
+ ImFontAtlasBuilder* builder = atlas->Builder;
+ builder->FrameCount = frame_count;
+ for (ImFont* font : atlas->Fonts)
+ font->LastBaked = NULL;
+
+ // Garbage collect BakedPool
+ if (builder->BakedDiscardedCount > 0)
+ {
+ int dst_n = 0, src_n = 0;
+ for (; src_n < builder->BakedPool.Size; src_n++)
+ {
+ ImFontBaked* p_src = &builder->BakedPool[src_n];
+ if (p_src->WantDestroy)
+ continue;
+ ImFontBaked* p_dst = &builder->BakedPool[dst_n++];
+ if (p_dst == p_src)
+ continue;
+ memcpy(p_dst, p_src, sizeof(ImFontBaked));
+ builder->BakedMap.SetVoidPtr(p_dst->BakedId, p_dst);
+ }
+ IM_ASSERT(dst_n + builder->BakedDiscardedCount == src_n);
+ builder->BakedPool.Size -= builder->BakedDiscardedCount;
+ builder->BakedDiscardedCount = 0;
+ }
+
+ // Update texture status
+ for (int tex_n = 0; tex_n < atlas->TexList.Size; tex_n++)
+ {
+ ImTextureData* tex = atlas->TexList[tex_n];
+ bool remove_from_list = false;
+ if (tex->Status == ImTextureStatus_OK)
+ {
+ tex->Updates.resize(0);
+ tex->UpdateRect.x = tex->UpdateRect.y = (unsigned short)~0;
+ tex->UpdateRect.w = tex->UpdateRect.h = 0;
+ }
+ if (tex->Status == ImTextureStatus_WantCreate && atlas->RendererHasTextures)
+ IM_ASSERT(tex->TexID == ImTextureID_Invalid && tex->BackendUserData == NULL && "Backend set texture's TexID/BackendUserData but did not update Status to OK.");
+
+ // Request destroy
+ // - Keep bool to true in order to differentiate a planned destroy vs a destroy decided by the backend.
+ // - We don't destroy pixels right away, as backend may have an in-flight copy from RAM.
+ if (tex->WantDestroyNextFrame && tex->Status != ImTextureStatus_Destroyed && tex->Status != ImTextureStatus_WantDestroy)
+ {
+ IM_ASSERT(tex->Status == ImTextureStatus_OK || tex->Status == ImTextureStatus_WantCreate || tex->Status == ImTextureStatus_WantUpdates);
+ tex->Status = ImTextureStatus_WantDestroy;
+ }
+
+ // If a texture has never reached the backend, they don't need to know about it.
+ // (note: backends between 1.92.0 and 1.92.4 could set an already destroyed texture to ImTextureStatus_WantDestroy
+ // when invalidating graphics objects twice, which would previously remove it from the list and crash.)
+ if (tex->Status == ImTextureStatus_WantDestroy && tex->TexID == ImTextureID_Invalid && tex->BackendUserData == NULL)
+ tex->Status = ImTextureStatus_Destroyed;
+
+ // Process texture being destroyed
+ if (tex->Status == ImTextureStatus_Destroyed)
+ {
+ IM_ASSERT(tex->TexID == ImTextureID_Invalid && tex->BackendUserData == NULL && "Backend set texture Status to Destroyed but did not clear TexID/BackendUserData!");
+ if (tex->WantDestroyNextFrame)
+ remove_from_list = true; // Destroy was scheduled by us
+ else
+ tex->Status = ImTextureStatus_WantCreate; // Destroy was done was backend: recreate it (e.g. freed resources mid-run)
+ }
+
+ // The backend may need defer destroying by a few frames, to handle texture used by previous in-flight rendering.
+ // We allow the texture staying in _WantDestroy state and increment a counter which the backend can use to take its decision.
+ if (tex->Status == ImTextureStatus_WantDestroy)
+ tex->UnusedFrames++;
+
+ // Destroy and remove
+ if (remove_from_list)
+ {
+ IM_ASSERT(atlas->TexData != tex);
+ tex->DestroyPixels();
+ IM_DELETE(tex);
+ atlas->TexList.erase(atlas->TexList.begin() + tex_n);
+ tex_n--;
}
}
-
- *out_pixels = (unsigned char*)TexPixelsRGBA32;
- if (out_width) *out_width = TexWidth;
- if (out_height) *out_height = TexHeight;
- if (out_bytes_per_pixel) *out_bytes_per_pixel = 4;
}
-ImFont* ImFontAtlas::AddFont(const ImFontConfig* font_cfg)
+void ImFontAtlasTextureBlockConvert(const unsigned char* src_pixels, ImTextureFormat src_fmt, int src_pitch, unsigned char* dst_pixels, ImTextureFormat dst_fmt, int dst_pitch, int w, int h)
{
- IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!");
- IM_ASSERT(font_cfg->FontData != NULL && font_cfg->FontDataSize > 0);
- IM_ASSERT(font_cfg->SizePixels > 0.0f && "Is ImFontConfig struct correctly initialized?");
- IM_ASSERT(font_cfg->RasterizerDensity > 0.0f && "Is ImFontConfig struct correctly initialized?");
+ IM_ASSERT(src_pixels != NULL && dst_pixels != NULL);
+ if (src_fmt == dst_fmt)
+ {
+ int line_sz = w * ImTextureDataGetFormatBytesPerPixel(src_fmt);
+ for (int ny = h; ny > 0; ny--, src_pixels += src_pitch, dst_pixels += dst_pitch)
+ memcpy(dst_pixels, src_pixels, line_sz);
+ }
+ else if (src_fmt == ImTextureFormat_Alpha8 && dst_fmt == ImTextureFormat_RGBA32)
+ {
+ for (int ny = h; ny > 0; ny--, src_pixels += src_pitch, dst_pixels += dst_pitch)
+ {
+ const ImU8* src_p = (const ImU8*)src_pixels;
+ ImU32* dst_p = (ImU32*)(void*)dst_pixels;
+ for (int nx = w; nx > 0; nx--)
+ *dst_p++ = IM_COL32(255, 255, 255, (unsigned int)(*src_p++));
+ }
+ }
+ else if (src_fmt == ImTextureFormat_RGBA32 && dst_fmt == ImTextureFormat_Alpha8)
+ {
+ for (int ny = h; ny > 0; ny--, src_pixels += src_pitch, dst_pixels += dst_pitch)
+ {
+ const ImU32* src_p = (const ImU32*)(void*)src_pixels;
+ ImU8* dst_p = (ImU8*)dst_pixels;
+ for (int nx = w; nx > 0; nx--)
+ *dst_p++ = ((*src_p++) >> IM_COL32_A_SHIFT) & 0xFF;
+ }
+ }
+ else
+ {
+ IM_ASSERT(0);
+ }
+}
+
+// Source buffer may be written to (used for in-place mods).
+// Post-process hooks may eventually be added here.
+void ImFontAtlasTextureBlockPostProcess(ImFontAtlasPostProcessData* data)
+{
+ // Multiply operator (legacy)
+ if (data->FontSrc->RasterizerMultiply != 1.0f)
+ ImFontAtlasTextureBlockPostProcessMultiply(data, data->FontSrc->RasterizerMultiply);
+}
+
+void ImFontAtlasTextureBlockPostProcessMultiply(ImFontAtlasPostProcessData* data, float multiply_factor)
+{
+ unsigned char* pixels = (unsigned char*)data->Pixels;
+ int pitch = data->Pitch;
+ if (data->Format == ImTextureFormat_Alpha8)
+ {
+ for (int ny = data->Height; ny > 0; ny--, pixels += pitch)
+ {
+ ImU8* p = (ImU8*)pixels;
+ for (int nx = data->Width; nx > 0; nx--, p++)
+ {
+ unsigned int v = ImMin((unsigned int)(*p * multiply_factor), (unsigned int)255);
+ *p = (unsigned char)v;
+ }
+ }
+ }
+ else if (data->Format == ImTextureFormat_RGBA32) //-V547
+ {
+ for (int ny = data->Height; ny > 0; ny--, pixels += pitch)
+ {
+ ImU32* p = (ImU32*)(void*)pixels;
+ for (int nx = data->Width; nx > 0; nx--, p++)
+ {
+ unsigned int a = ImMin((unsigned int)(((*p >> IM_COL32_A_SHIFT) & 0xFF) * multiply_factor), (unsigned int)255);
+ *p = IM_COL32((*p >> IM_COL32_R_SHIFT) & 0xFF, (*p >> IM_COL32_G_SHIFT) & 0xFF, (*p >> IM_COL32_B_SHIFT) & 0xFF, a);
+ }
+ }
+ }
+ else
+ {
+ IM_ASSERT(0);
+ }
+}
+
+// Fill with single color. We don't use this directly but it is convenient for anyone working on uploading custom rects.
+void ImFontAtlasTextureBlockFill(ImTextureData* dst_tex, int dst_x, int dst_y, int w, int h, ImU32 col)
+{
+ if (dst_tex->Format == ImTextureFormat_Alpha8)
+ {
+ ImU8 col_a = (col >> IM_COL32_A_SHIFT) & 0xFF;
+ for (int y = 0; y < h; y++)
+ memset((ImU8*)dst_tex->GetPixelsAt(dst_x, dst_y + y), col_a, w);
+ }
+ else
+ {
+ for (int y = 0; y < h; y++)
+ {
+ ImU32* p = (ImU32*)(void*)dst_tex->GetPixelsAt(dst_x, dst_y + y);
+ for (int x = w; x > 0; x--, p++)
+ *p = col;
+ }
+ }
+}
+
+// Copy block from one texture to another
+void ImFontAtlasTextureBlockCopy(ImTextureData* src_tex, int src_x, int src_y, ImTextureData* dst_tex, int dst_x, int dst_y, int w, int h)
+{
+ IM_ASSERT(src_tex->Pixels != NULL && dst_tex->Pixels != NULL);
+ IM_ASSERT(src_tex->Format == dst_tex->Format);
+ IM_ASSERT(src_x >= 0 && src_x + w <= src_tex->Width);
+ IM_ASSERT(src_y >= 0 && src_y + h <= src_tex->Height);
+ IM_ASSERT(dst_x >= 0 && dst_x + w <= dst_tex->Width);
+ IM_ASSERT(dst_y >= 0 && dst_y + h <= dst_tex->Height);
+ for (int y = 0; y < h; y++)
+ memcpy(dst_tex->GetPixelsAt(dst_x, dst_y + y), src_tex->GetPixelsAt(src_x, src_y + y), w * dst_tex->BytesPerPixel);
+}
+
+// Queue texture block update for renderer backend
+void ImFontAtlasTextureBlockQueueUpload(ImFontAtlas* atlas, ImTextureData* tex, int x, int y, int w, int h)
+{
+ IM_ASSERT(tex->Status != ImTextureStatus_WantDestroy && tex->Status != ImTextureStatus_Destroyed);
+ IM_ASSERT(x >= 0 && x <= 0xFFFF && y >= 0 && y <= 0xFFFF && w >= 0 && x + w <= 0x10000 && h >= 0 && y + h <= 0x10000);
+ IM_UNUSED(atlas);
+
+ ImTextureRect req = { (unsigned short)x, (unsigned short)y, (unsigned short)w, (unsigned short)h };
+ int new_x1 = ImMax(tex->UpdateRect.w == 0 ? 0 : tex->UpdateRect.x + tex->UpdateRect.w, req.x + req.w);
+ int new_y1 = ImMax(tex->UpdateRect.h == 0 ? 0 : tex->UpdateRect.y + tex->UpdateRect.h, req.y + req.h);
+ tex->UpdateRect.x = ImMin(tex->UpdateRect.x, req.x);
+ tex->UpdateRect.y = ImMin(tex->UpdateRect.y, req.y);
+ tex->UpdateRect.w = (unsigned short)(new_x1 - tex->UpdateRect.x);
+ tex->UpdateRect.h = (unsigned short)(new_y1 - tex->UpdateRect.y);
+ tex->UsedRect.x = ImMin(tex->UsedRect.x, req.x);
+ tex->UsedRect.y = ImMin(tex->UsedRect.y, req.y);
+ tex->UsedRect.w = (unsigned short)(ImMax(tex->UsedRect.x + tex->UsedRect.w, req.x + req.w) - tex->UsedRect.x);
+ tex->UsedRect.h = (unsigned short)(ImMax(tex->UsedRect.y + tex->UsedRect.h, req.y + req.h) - tex->UsedRect.y);
+ atlas->TexIsBuilt = false;
+
+ // No need to queue if status is == ImTextureStatus_WantCreate
+ if (tex->Status == ImTextureStatus_OK || tex->Status == ImTextureStatus_WantUpdates)
+ {
+ tex->Status = ImTextureStatus_WantUpdates;
+ tex->Updates.push_back(req);
+ }
+}
+
+#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
+static void GetTexDataAsFormat(ImFontAtlas* atlas, ImTextureFormat format, unsigned char** out_pixels, int* out_width, int* out_height, int* out_bytes_per_pixel)
+{
+ ImTextureData* tex = atlas->TexData;
+ if (!atlas->TexIsBuilt || tex == NULL || tex->Pixels == NULL || atlas->TexDesiredFormat != format)
+ {
+ atlas->TexDesiredFormat = format;
+ atlas->Build();
+ tex = atlas->TexData;
+ }
+ if (out_pixels) { *out_pixels = (unsigned char*)tex->Pixels; };
+ if (out_width) { *out_width = tex->Width; };
+ if (out_height) { *out_height = tex->Height; };
+ if (out_bytes_per_pixel) { *out_bytes_per_pixel = tex->BytesPerPixel; }
+}
+
+void ImFontAtlas::GetTexDataAsAlpha8(unsigned char** out_pixels, int* out_width, int* out_height, int* out_bytes_per_pixel)
+{
+ GetTexDataAsFormat(this, ImTextureFormat_Alpha8, out_pixels, out_width, out_height, out_bytes_per_pixel);
+}
+
+void ImFontAtlas::GetTexDataAsRGBA32(unsigned char** out_pixels, int* out_width, int* out_height, int* out_bytes_per_pixel)
+{
+ GetTexDataAsFormat(this, ImTextureFormat_RGBA32, out_pixels, out_width, out_height, out_bytes_per_pixel);
+}
+
+bool ImFontAtlas::Build()
+{
+ ImFontAtlasBuildMain(this);
+ return true;
+}
+#endif // #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
+
+ImFont* ImFontAtlas::AddFont(const ImFontConfig* font_cfg_in)
+{
+ // Sanity Checks
+ IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas!");
+ IM_ASSERT((font_cfg_in->FontData != NULL && font_cfg_in->FontDataSize > 0) || (font_cfg_in->FontLoader != NULL));
+ //IM_ASSERT(font_cfg_in->SizePixels > 0.0f && "Is ImFontConfig struct correctly initialized?");
+ IM_ASSERT(font_cfg_in->RasterizerDensity > 0.0f && "Is ImFontConfig struct correctly initialized?");
+ if (font_cfg_in->GlyphOffset.x != 0.0f || font_cfg_in->GlyphOffset.y != 0.0f || font_cfg_in->GlyphMinAdvanceX != 0.0f || font_cfg_in->GlyphMaxAdvanceX != FLT_MAX)
+ IM_ASSERT(font_cfg_in->SizePixels != 0.0f && "Specifying glyph offset/advances requires a reference size to base it on.");
+
+ // Lazily create builder on the first call to AddFont
+ if (Builder == NULL)
+ ImFontAtlasBuildInit(this);
// Create new font
- if (!font_cfg->MergeMode)
- Fonts.push_back(IM_NEW(ImFont));
- else
- IM_ASSERT(Fonts.Size > 0 && "Cannot use MergeMode for the first font"); // When using MergeMode make sure that a font has already been added before. You can use ImGui::GetIO().Fonts->AddFontDefault() to add the default imgui font.
-
- Sources.push_back(*font_cfg);
- ImFontConfig& new_font_cfg = Sources.back();
- if (new_font_cfg.DstFont == NULL)
- new_font_cfg.DstFont = Fonts.back();
- if (!new_font_cfg.FontDataOwnedByAtlas)
+ ImFont* font;
+ if (!font_cfg_in->MergeMode)
{
- new_font_cfg.FontData = IM_ALLOC(new_font_cfg.FontDataSize);
- new_font_cfg.FontDataOwnedByAtlas = true;
- memcpy(new_font_cfg.FontData, font_cfg->FontData, (size_t)new_font_cfg.FontDataSize);
+ font = IM_NEW(ImFont)();
+ font->FontId = FontNextUniqueID++;
+ font->Flags = font_cfg_in->Flags;
+ font->LegacySize = font_cfg_in->SizePixels;
+ font->CurrentRasterizerDensity = font_cfg_in->RasterizerDensity;
+ Fonts.push_back(font);
+ }
+ else
+ {
+ IM_ASSERT(Fonts.Size > 0 && "Cannot use MergeMode for the first font"); // When using MergeMode make sure that a font has already been added before. You can use ImGui::GetIO().Fonts->AddFontDefault() to add the default imgui font.
+ font = font_cfg_in->DstFont ? font_cfg_in->DstFont : Fonts.back();
}
- // Round font size
- // - We started rounding in 1.90 WIP (18991) as our layout system currently doesn't support non-rounded font size well yet.
- // - Note that using io.FontGlobalScale or SetWindowFontScale(), with are legacy-ish, partially supported features, can still lead to unrounded sizes.
- // - We may support it better later and remove this rounding.
- new_font_cfg.SizePixels = ImTrunc(new_font_cfg.SizePixels);
+ // Add to list
+ Sources.push_back(*font_cfg_in);
+ ImFontConfig* font_cfg = &Sources.back();
+ if (font_cfg->DstFont == NULL)
+ font_cfg->DstFont = font;
+ font->Sources.push_back(font_cfg);
+ ImFontAtlasBuildUpdatePointers(this); // Pointers to Sources are otherwise dangling after we called Sources.push_back().
- // Pointers to Sources data are otherwise dangling
- ImFontAtlasUpdateSourcesPointers(this);
+ if (font_cfg->FontDataOwnedByAtlas == false)
+ {
+ font_cfg->FontDataOwnedByAtlas = true;
+ font_cfg->FontData = ImMemdup(font_cfg->FontData, (size_t)font_cfg->FontDataSize);
+ }
- // Invalidate texture
- TexReady = false;
- ClearTexData();
- return new_font_cfg.DstFont;
+ // Sanity check
+ // We don't round cfg.SizePixels yet as relative size of merged fonts are used afterwards.
+ if (font_cfg->GlyphExcludeRanges != NULL)
+ {
+ int size = 0;
+ for (const ImWchar* p = font_cfg->GlyphExcludeRanges; p[0] != 0; p++, size++) {}
+ IM_ASSERT((size & 1) == 0 && "GlyphExcludeRanges[] size must be multiple of two!");
+ IM_ASSERT((size <= 64) && "GlyphExcludeRanges[] size must be small!");
+ font_cfg->GlyphExcludeRanges = (ImWchar*)ImMemdup(font_cfg->GlyphExcludeRanges, sizeof(font_cfg->GlyphExcludeRanges[0]) * (size + 1));
+ }
+ if (font_cfg->FontLoader != NULL)
+ {
+ IM_ASSERT(font_cfg->FontLoader->FontBakedLoadGlyph != NULL);
+ IM_ASSERT(font_cfg->FontLoader->LoaderInit == NULL && font_cfg->FontLoader->LoaderShutdown == NULL); // FIXME-NEWATLAS: Unsupported yet.
+ }
+ IM_ASSERT(font_cfg->FontLoaderData == NULL);
+
+ if (!ImFontAtlasFontSourceInit(this, font_cfg))
+ {
+ // Rollback (this is a fragile/rarely exercised code-path. TestSuite's "misc_atlas_add_invalid_font" aim to test this)
+ ImFontAtlasFontDestroySourceData(this, font_cfg);
+ Sources.pop_back();
+ font->Sources.pop_back();
+ if (!font_cfg->MergeMode)
+ {
+ IM_DELETE(font);
+ Fonts.pop_back();
+ }
+ return NULL;
+ }
+ ImFontAtlasFontSourceAddToFont(this, font, font_cfg);
+
+ return font;
}
// Default font TTF is compressed with stb_compress then base85 encoded (see misc/fonts/binary_to_compressed_c.cpp for encoder)
@@ -2640,6 +3109,7 @@ static const char* GetDefaultCompressedFontDataTTF(int* out_size);
#endif
// Load embedded ProggyClean.ttf at size 13, disable oversampling
+// If you want a similar font which may be better scaled, consider using ProggyVector from the same author!
ImFont* ImFontAtlas::AddFontDefault(const ImFontConfig* font_cfg_template)
{
#ifndef IMGUI_DISABLE_DEFAULT_FONT
@@ -2652,15 +3122,13 @@ ImFont* ImFontAtlas::AddFontDefault(const ImFontConfig* font_cfg_template)
if (font_cfg.SizePixels <= 0.0f)
font_cfg.SizePixels = 13.0f * 1.0f;
if (font_cfg.Name[0] == '\0')
- ImFormatString(font_cfg.Name, IM_ARRAYSIZE(font_cfg.Name), "ProggyClean.ttf, %dpx", (int)font_cfg.SizePixels);
+ ImFormatString(font_cfg.Name, IM_ARRAYSIZE(font_cfg.Name), "ProggyClean.ttf");
font_cfg.EllipsisChar = (ImWchar)0x0085;
- font_cfg.GlyphOffset.y = 1.0f * IM_TRUNC(font_cfg.SizePixels / 13.0f); // Add +1 offset per 13 units
+ font_cfg.GlyphOffset.y += 1.0f * IM_TRUNC(font_cfg.SizePixels / 13.0f); // Add +1 offset per 13 units
int ttf_compressed_size = 0;
const char* ttf_compressed = GetDefaultCompressedFontDataTTF(&ttf_compressed_size);
- const ImWchar* glyph_ranges = font_cfg.GlyphRanges != NULL ? font_cfg.GlyphRanges : GetGlyphRangesDefault();
- ImFont* font = AddFontFromMemoryCompressedTTF(ttf_compressed, ttf_compressed_size, font_cfg.SizePixels, &font_cfg, glyph_ranges);
- return font;
+ return AddFontFromMemoryCompressedTTF(ttf_compressed, ttf_compressed_size, font_cfg.SizePixels, &font_cfg);
#else
IM_ASSERT(0 && "AddFontDefault() disabled in this build.");
IM_UNUSED(font_cfg_template);
@@ -2670,12 +3138,16 @@ ImFont* ImFontAtlas::AddFontDefault(const ImFontConfig* font_cfg_template)
ImFont* ImFontAtlas::AddFontFromFileTTF(const char* filename, float size_pixels, const ImFontConfig* font_cfg_template, const ImWchar* glyph_ranges)
{
- IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!");
+ IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas!");
size_t data_size = 0;
void* data = ImFileLoadToMemory(filename, "rb", &data_size, 0);
if (!data)
{
- IM_ASSERT_USER_ERROR(0, "Could not load font file!");
+ if (font_cfg_template == NULL || (font_cfg_template->Flags & ImFontFlags_NoLoadError) == 0)
+ {
+ IMGUI_DEBUG_LOG("While loading '%s'\n", filename);
+ IM_ASSERT_USER_ERROR(0, "Could not load font file!");
+ }
return NULL;
}
ImFontConfig font_cfg = font_cfg_template ? *font_cfg_template : ImFontConfig();
@@ -2684,7 +3156,7 @@ ImFont* ImFontAtlas::AddFontFromFileTTF(const char* filename, float size_pixels,
// Store a short copy of filename into into the font name for convenience
const char* p;
for (p = filename + ImStrlen(filename); p > filename && p[-1] != '/' && p[-1] != '\\'; p--) {}
- ImFormatString(font_cfg.Name, IM_ARRAYSIZE(font_cfg.Name), "%s, %.0fpx", p, size_pixels);
+ ImFormatString(font_cfg.Name, IM_ARRAYSIZE(font_cfg.Name), "%s", p);
}
return AddFontFromMemoryTTF(data, (int)data_size, size_pixels, &font_cfg, glyph_ranges);
}
@@ -2692,7 +3164,7 @@ ImFont* ImFontAtlas::AddFontFromFileTTF(const char* filename, float size_pixels,
// NB: Transfer ownership of 'ttf_data' to ImFontAtlas, unless font_cfg_template->FontDataOwnedByAtlas == false. Owned TTF buffer will be deleted after Build().
ImFont* ImFontAtlas::AddFontFromMemoryTTF(void* font_data, int font_data_size, float size_pixels, const ImFontConfig* font_cfg_template, const ImWchar* glyph_ranges)
{
- IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!");
+ IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas!");
ImFontConfig font_cfg = font_cfg_template ? *font_cfg_template : ImFontConfig();
IM_ASSERT(font_cfg.FontData == NULL);
IM_ASSERT(font_data_size > 100 && "Incorrect value for font_data_size!"); // Heuristic to prevent accidentally passing a wrong value to font_data_size.
@@ -2726,43 +3198,156 @@ ImFont* ImFontAtlas::AddFontFromMemoryCompressedBase85TTF(const char* compressed
return font;
}
-int ImFontAtlas::AddCustomRectRegular(int width, int height)
+// On font removal we need to remove references (otherwise we could queue removal?)
+// We allow old_font == new_font which forces updating all values (e.g. sizes)
+static void ImFontAtlasBuildNotifySetFont(ImFontAtlas* atlas, ImFont* old_font, ImFont* new_font)
+{
+ for (ImDrawListSharedData* shared_data : atlas->DrawListSharedDatas)
+ {
+ if (shared_data->Font == old_font)
+ shared_data->Font = new_font;
+ if (ImGuiContext* ctx = shared_data->Context)
+ {
+ if (ctx->IO.FontDefault == old_font)
+ ctx->IO.FontDefault = new_font;
+ if (ctx->Font == old_font)
+ {
+ ImGuiContext* curr_ctx = ImGui::GetCurrentContext();
+ bool need_bind_ctx = ctx != curr_ctx;
+ if (need_bind_ctx)
+ ImGui::SetCurrentContext(ctx);
+ ImGui::SetCurrentFont(new_font, ctx->FontSizeBase, ctx->FontSize);
+ if (need_bind_ctx)
+ ImGui::SetCurrentContext(curr_ctx);
+ }
+ for (ImFontStackData& font_stack_data : ctx->FontStack)
+ if (font_stack_data.Font == old_font)
+ font_stack_data.Font = new_font;
+ }
+ }
+}
+
+void ImFontAtlas::RemoveFont(ImFont* font)
+{
+ IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas!");
+ font->ClearOutputData();
+
+ ImFontAtlasFontDestroyOutput(this, font);
+ for (ImFontConfig* src : font->Sources)
+ ImFontAtlasFontDestroySourceData(this, src);
+ for (int src_n = 0; src_n < Sources.Size; src_n++)
+ if (Sources[src_n].DstFont == font)
+ Sources.erase(&Sources[src_n--]);
+
+ bool removed = Fonts.find_erase(font);
+ IM_ASSERT(removed);
+ IM_UNUSED(removed);
+
+ ImFontAtlasBuildUpdatePointers(this);
+
+ font->ContainerAtlas = NULL;
+ IM_DELETE(font);
+
+ // Notify external systems
+ ImFont* new_current_font = Fonts.empty() ? NULL : Fonts[0];
+ ImFontAtlasBuildNotifySetFont(this, font, new_current_font);
+}
+
+// At it is common to do an AddCustomRect() followed by a GetCustomRect(), we provide an optional 'ImFontAtlasRect* out_r = NULL' argument to retrieve the info straight away.
+ImFontAtlasRectId ImFontAtlas::AddCustomRect(int width, int height, ImFontAtlasRect* out_r)
{
IM_ASSERT(width > 0 && width <= 0xFFFF);
IM_ASSERT(height > 0 && height <= 0xFFFF);
- ImFontAtlasCustomRect r;
- r.Width = (unsigned short)width;
- r.Height = (unsigned short)height;
- CustomRects.push_back(r);
- return CustomRects.Size - 1; // Return index
+
+ if (Builder == NULL)
+ ImFontAtlasBuildInit(this);
+
+ ImFontAtlasRectId r_id = ImFontAtlasPackAddRect(this, width, height);
+ if (r_id == ImFontAtlasRectId_Invalid)
+ return ImFontAtlasRectId_Invalid;
+ if (out_r != NULL)
+ GetCustomRect(r_id, out_r);
+
+ if (RendererHasTextures)
+ {
+ ImTextureRect* r = ImFontAtlasPackGetRect(this, r_id);
+ ImFontAtlasTextureBlockQueueUpload(this, TexData, r->x, r->y, r->w, r->h);
+ }
+ return r_id;
}
-int ImFontAtlas::AddCustomRectFontGlyph(ImFont* font, ImWchar id, int width, int height, float advance_x, const ImVec2& offset)
+void ImFontAtlas::RemoveCustomRect(ImFontAtlasRectId id)
+{
+ if (ImFontAtlasPackGetRectSafe(this, id) == NULL)
+ return;
+ ImFontAtlasPackDiscardRect(this, id);
+}
+
+#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
+// This API does not make sense anymore with scalable fonts.
+// - Prefer adding a font source (ImFontConfig) using a custom/procedural loader.
+// - You may use ImFontFlags_LockBakedSizes to limit an existing font to known baked sizes:
+// ImFont* myfont = io.Fonts->AddFontFromFileTTF(....);
+// myfont->GetFontBaked(16.0f);
+// myfont->Flags |= ImFontFlags_LockBakedSizes;
+ImFontAtlasRectId ImFontAtlas::AddCustomRectFontGlyph(ImFont* font, ImWchar codepoint, int width, int height, float advance_x, const ImVec2& offset)
+{
+ float font_size = font->LegacySize;
+ return AddCustomRectFontGlyphForSize(font, font_size, codepoint, width, height, advance_x, offset);
+}
+// FIXME: we automatically set glyph.Colored=true by default.
+// If you need to alter this, you can write 'font->Glyphs.back()->Colored' after calling AddCustomRectFontGlyph().
+ImFontAtlasRectId ImFontAtlas::AddCustomRectFontGlyphForSize(ImFont* font, float font_size, ImWchar codepoint, int width, int height, float advance_x, const ImVec2& offset)
{
#ifdef IMGUI_USE_WCHAR32
- IM_ASSERT(id <= IM_UNICODE_CODEPOINT_MAX);
+ IM_ASSERT(codepoint <= IM_UNICODE_CODEPOINT_MAX);
#endif
IM_ASSERT(font != NULL);
IM_ASSERT(width > 0 && width <= 0xFFFF);
IM_ASSERT(height > 0 && height <= 0xFFFF);
- ImFontAtlasCustomRect r;
- r.Width = (unsigned short)width;
- r.Height = (unsigned short)height;
- r.GlyphID = id;
- r.GlyphColored = 0; // Set to 1 manually to mark glyph as colored // FIXME: No official API for that (#8133)
- r.GlyphAdvanceX = advance_x;
- r.GlyphOffset = offset;
- r.Font = font;
- CustomRects.push_back(r);
- return CustomRects.Size - 1; // Return index
-}
-void ImFontAtlas::CalcCustomRectUV(const ImFontAtlasCustomRect* rect, ImVec2* out_uv_min, ImVec2* out_uv_max) const
+ ImFontBaked* baked = font->GetFontBaked(font_size);
+
+ ImFontAtlasRectId r_id = ImFontAtlasPackAddRect(this, width, height);
+ if (r_id == ImFontAtlasRectId_Invalid)
+ return ImFontAtlasRectId_Invalid;
+ ImTextureRect* r = ImFontAtlasPackGetRect(this, r_id);
+ if (RendererHasTextures)
+ ImFontAtlasTextureBlockQueueUpload(this, TexData, r->x, r->y, r->w, r->h);
+
+ if (baked->IsGlyphLoaded(codepoint))
+ ImFontAtlasBakedDiscardFontGlyph(this, font, baked, baked->FindGlyph(codepoint));
+
+ ImFontGlyph glyph;
+ glyph.Codepoint = codepoint;
+ glyph.AdvanceX = advance_x;
+ glyph.X0 = offset.x;
+ glyph.Y0 = offset.y;
+ glyph.X1 = offset.x + r->w;
+ glyph.Y1 = offset.y + r->h;
+ glyph.Visible = true;
+ glyph.Colored = true; // FIXME: Arbitrary
+ glyph.PackId = r_id;
+ ImFontAtlasBakedAddFontGlyph(this, baked, font->Sources[0], &glyph);
+ return r_id;
+}
+#endif // #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
+
+bool ImFontAtlas::GetCustomRect(ImFontAtlasRectId id, ImFontAtlasRect* out_r) const
{
- IM_ASSERT(TexWidth > 0 && TexHeight > 0); // Font atlas needs to be built before we can calculate UV coordinates
- IM_ASSERT(rect->IsPacked()); // Make sure the rectangle has been packed
- *out_uv_min = ImVec2((float)rect->X * TexUvScale.x, (float)rect->Y * TexUvScale.y);
- *out_uv_max = ImVec2((float)(rect->X + rect->Width) * TexUvScale.x, (float)(rect->Y + rect->Height) * TexUvScale.y);
+ ImTextureRect* r = ImFontAtlasPackGetRectSafe((ImFontAtlas*)this, id);
+ if (r == NULL)
+ return false;
+ IM_ASSERT(TexData->Width > 0 && TexData->Height > 0); // Font atlas needs to be built before we can calculate UV coordinates
+ if (out_r == NULL)
+ return true;
+ out_r->x = r->x;
+ out_r->y = r->y;
+ out_r->w = r->w;
+ out_r->h = r->h;
+ out_r->uv0 = ImVec2((float)(r->x), (float)(r->y)) * TexUvScale;
+ out_r->uv1 = ImVec2((float)(r->x + r->w), (float)(r->y + r->h)) * TexUvScale;
+ return true;
}
bool ImFontAtlasGetMouseCursorTexData(ImFontAtlas* atlas, ImGuiMouseCursor cursor_type, ImVec2* out_offset, ImVec2* out_size, ImVec2 out_uv_border[2], ImVec2 out_uv_fill[2])
@@ -2772,9 +3357,8 @@ bool ImFontAtlasGetMouseCursorTexData(ImFontAtlas* atlas, ImGuiMouseCursor curso
if (atlas->Flags & ImFontAtlasFlags_NoMouseCursors)
return false;
- IM_ASSERT(atlas->PackIdMouseCursors != -1);
- ImFontAtlasCustomRect* r = atlas->GetCustomRectByIndex(atlas->PackIdMouseCursors);
- ImVec2 pos = FONT_ATLAS_DEFAULT_TEX_CURSOR_DATA[cursor_type][0] + ImVec2((float)r->X, (float)r->Y);
+ ImTextureRect* r = ImFontAtlasPackGetRect(atlas, atlas->Builder->PackIdMouseCursors);
+ ImVec2 pos = FONT_ATLAS_DEFAULT_TEX_CURSOR_DATA[cursor_type][0] + ImVec2((float)r->x, (float)r->y);
ImVec2 size = FONT_ATLAS_DEFAULT_TEX_CURSOR_DATA[cursor_type][1];
*out_size = size;
*out_offset = FONT_ATLAS_DEFAULT_TEX_CURSOR_DATA[cursor_type][2];
@@ -2786,517 +3370,188 @@ bool ImFontAtlasGetMouseCursorTexData(ImFontAtlas* atlas, ImGuiMouseCursor curso
return true;
}
-bool ImFontAtlas::Build()
+// When atlas->RendererHasTextures = true, this is only called if no font were loaded.
+void ImFontAtlasBuildMain(ImFontAtlas* atlas)
{
- IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!");
+ IM_ASSERT(!atlas->Locked && "Cannot modify a locked ImFontAtlas!");
+ if (atlas->TexData && atlas->TexData->Format != atlas->TexDesiredFormat)
+ ImFontAtlasBuildClear(atlas);
+
+ if (atlas->Builder == NULL)
+ ImFontAtlasBuildInit(atlas);
// Default font is none are specified
- if (Sources.Size == 0)
- AddFontDefault();
+ if (atlas->Sources.Size == 0)
+ atlas->AddFontDefault();
- // Select builder
- // - Note that we do not reassign to atlas->FontBuilderIO, since it is likely to point to static data which
- // may mess with some hot-reloading schemes. If you need to assign to this (for dynamic selection) AND are
- // using a hot-reloading scheme that messes up static data, store your own instance of ImFontBuilderIO somewhere
- // and point to it instead of pointing directly to return value of the GetBuilderXXX functions.
- const ImFontBuilderIO* builder_io = FontBuilderIO;
- if (builder_io == NULL)
- {
-#ifdef IMGUI_ENABLE_FREETYPE
- builder_io = ImGuiFreeType::GetBuilderForFreeType();
-#elif defined(IMGUI_ENABLE_STB_TRUETYPE)
- builder_io = ImFontAtlasGetBuilderForStbTruetype();
-#else
- IM_ASSERT(0); // Invalid Build function
-#endif
- }
-
- // Build
- return builder_io->FontBuilder_Build(this);
+ // [LEGACY] For backends not supporting RendererHasTextures: preload all glyphs
+ ImFontAtlasBuildUpdateRendererHasTexturesFromContext(atlas);
+ if (atlas->RendererHasTextures == false) // ~ImGuiBackendFlags_RendererHasTextures
+ ImFontAtlasBuildLegacyPreloadAllGlyphRanges(atlas);
+ atlas->TexIsBuilt = true;
}
-void ImFontAtlasBuildMultiplyCalcLookupTable(unsigned char out_table[256], float in_brighten_factor)
-{
- for (unsigned int i = 0; i < 256; i++)
- {
- unsigned int value = (unsigned int)(i * in_brighten_factor);
- out_table[i] = value > 255 ? 255 : (value & 0xFF);
- }
-}
-
-void ImFontAtlasBuildMultiplyRectAlpha8(const unsigned char table[256], unsigned char* pixels, int x, int y, int w, int h, int stride)
-{
- IM_ASSERT_PARANOID(w <= stride);
- unsigned char* data = pixels + x + y * stride;
- for (int j = h; j > 0; j--, data += stride - w)
- for (int i = w; i > 0; i--, data++)
- *data = table[*data];
-}
-
-void ImFontAtlasBuildGetOversampleFactors(const ImFontConfig* src, int* out_oversample_h, int* out_oversample_v)
+void ImFontAtlasBuildGetOversampleFactors(ImFontConfig* src, ImFontBaked* baked, int* out_oversample_h, int* out_oversample_v)
{
// Automatically disable horizontal oversampling over size 36
- *out_oversample_h = (src->OversampleH != 0) ? src->OversampleH : (src->SizePixels * src->RasterizerDensity > 36.0f || src->PixelSnapH) ? 1 : 2;
+ const float raster_size = baked->Size * baked->RasterizerDensity * src->RasterizerDensity;
+ *out_oversample_h = (src->OversampleH != 0) ? src->OversampleH : (raster_size > 36.0f || src->PixelSnapH) ? 1 : 2;
*out_oversample_v = (src->OversampleV != 0) ? src->OversampleV : 1;
}
-#ifdef IMGUI_ENABLE_STB_TRUETYPE
-// Temporary data for one source font (multiple source fonts can be merged into one destination ImFont)
-// (C++03 doesn't allow instancing ImVector<> with function-local types so we declare the type here.)
-struct ImFontBuildSrcData
+// Setup main font loader for the atlas
+// Every font source (ImFontConfig) will use this unless ImFontConfig::FontLoader specify a custom loader.
+void ImFontAtlasBuildSetupFontLoader(ImFontAtlas* atlas, const ImFontLoader* font_loader)
{
- stbtt_fontinfo FontInfo;
- stbtt_pack_range PackRange; // Hold the list of codepoints to pack (essentially points to Codepoints.Data)
- stbrp_rect* Rects; // Rectangle to pack. We first fill in their size and the packer will give us their position.
- stbtt_packedchar* PackedChars; // Output glyphs
- const ImWchar* SrcRanges; // Ranges as requested by user (user is allowed to request too much, e.g. 0x0020..0xFFFF)
- int DstIndex; // Index into atlas->Fonts[] and dst_tmp_array[]
- int GlyphsHighest; // Highest requested codepoint
- int GlyphsCount; // Glyph count (excluding missing glyphs and glyphs already set by an earlier source font)
- ImBitVector GlyphsSet; // Glyph bit map (random access, 1-bit per codepoint. This will be a maximum of 8KB)
- ImVector GlyphsList; // Glyph codepoints list (flattened version of GlyphsSet)
-};
+ if (atlas->FontLoader == font_loader)
+ return;
+ IM_ASSERT(!atlas->Locked && "Cannot modify a locked ImFontAtlas!");
-// Temporary data for one destination ImFont* (multiple source fonts can be merged into one destination ImFont)
-struct ImFontBuildDstData
-{
- int SrcCount; // Number of source fonts targeting this destination font.
- int GlyphsHighest;
- int GlyphsCount;
- ImBitVector GlyphsSet; // This is used to resolve collision when multiple sources are merged into a same destination font.
-};
+ for (ImFont* font : atlas->Fonts)
+ ImFontAtlasFontDestroyOutput(atlas, font);
+ if (atlas->Builder && atlas->FontLoader && atlas->FontLoader->LoaderShutdown)
+ atlas->FontLoader->LoaderShutdown(atlas);
-static void UnpackBitVectorToFlatIndexList(const ImBitVector* in, ImVector* out)
-{
- IM_ASSERT(sizeof(in->Storage.Data[0]) == sizeof(int));
- const ImU32* it_begin = in->Storage.begin();
- const ImU32* it_end = in->Storage.end();
- for (const ImU32* it = it_begin; it < it_end; it++)
- if (ImU32 entries_32 = *it)
- for (ImU32 bit_n = 0; bit_n < 32; bit_n++)
- if (entries_32 & ((ImU32)1 << bit_n))
- out->push_back((int)(((it - it_begin) << 5) + bit_n));
+ atlas->FontLoader = font_loader;
+ atlas->FontLoaderName = font_loader ? font_loader->Name : "NULL";
+ IM_ASSERT(atlas->FontLoaderData == NULL);
+
+ if (atlas->Builder && atlas->FontLoader && atlas->FontLoader->LoaderInit)
+ atlas->FontLoader->LoaderInit(atlas);
+ for (ImFont* font : atlas->Fonts)
+ ImFontAtlasFontInitOutput(atlas, font);
+ for (ImFont* font : atlas->Fonts)
+ for (ImFontConfig* src : font->Sources)
+ ImFontAtlasFontSourceAddToFont(atlas, font, src);
}
-static bool ImFontAtlasBuildWithStbTruetype(ImFontAtlas* atlas)
+// Preload all glyph ranges for legacy backends.
+// This may lead to multiple texture creation which might be a little slower than before.
+void ImFontAtlasBuildLegacyPreloadAllGlyphRanges(ImFontAtlas* atlas)
{
- IM_ASSERT(atlas->Sources.Size > 0);
-
- ImFontAtlasBuildInit(atlas);
-
- // Clear atlas
- atlas->TexID = (ImTextureID)NULL;
- atlas->TexWidth = atlas->TexHeight = 0;
- atlas->TexUvScale = ImVec2(0.0f, 0.0f);
- atlas->TexUvWhitePixel = ImVec2(0.0f, 0.0f);
- atlas->ClearTexData();
-
- // Temporary storage for building
- ImVector src_tmp_array;
- ImVector dst_tmp_array;
- src_tmp_array.resize(atlas->Sources.Size);
- dst_tmp_array.resize(atlas->Fonts.Size);
- memset(src_tmp_array.Data, 0, (size_t)src_tmp_array.size_in_bytes());
- memset(dst_tmp_array.Data, 0, (size_t)dst_tmp_array.size_in_bytes());
-
- // 1. Initialize font loading structure, check font data validity
- for (int src_i = 0; src_i < atlas->Sources.Size; src_i++)
+ atlas->Builder->PreloadedAllGlyphsRanges = true;
+ for (ImFont* font : atlas->Fonts)
{
- ImFontBuildSrcData& src_tmp = src_tmp_array[src_i];
- ImFontConfig& src = atlas->Sources[src_i];
- IM_ASSERT(src.DstFont && (!src.DstFont->IsLoaded() || src.DstFont->ContainerAtlas == atlas));
-
- // Find index from src.DstFont (we allow the user to set cfg.DstFont. Also it makes casual debugging nicer than when storing indices)
- src_tmp.DstIndex = -1;
- for (int output_i = 0; output_i < atlas->Fonts.Size && src_tmp.DstIndex == -1; output_i++)
- if (src.DstFont == atlas->Fonts[output_i])
- src_tmp.DstIndex = output_i;
- if (src_tmp.DstIndex == -1)
+ ImFontBaked* baked = font->GetFontBaked(font->LegacySize);
+ if (font->FallbackChar != 0)
+ baked->FindGlyph(font->FallbackChar);
+ if (font->EllipsisChar != 0)
+ baked->FindGlyph(font->EllipsisChar);
+ for (ImFontConfig* src : font->Sources)
{
- IM_ASSERT(src_tmp.DstIndex != -1); // src.DstFont not pointing within atlas->Fonts[] array?
- return false;
- }
- // Initialize helper structure for font loading and verify that the TTF/OTF data is correct
- const int font_offset = stbtt_GetFontOffsetForIndex((unsigned char*)src.FontData, src.FontNo);
- IM_ASSERT(font_offset >= 0 && "FontData is incorrect, or FontNo cannot be found.");
- if (!stbtt_InitFont(&src_tmp.FontInfo, (unsigned char*)src.FontData, font_offset))
- {
- IM_ASSERT(0 && "stbtt_InitFont(): failed to parse FontData. It is correct and complete? Check FontDataSize.");
- return false;
- }
-
- // Measure highest codepoints
- ImFontBuildDstData& dst_tmp = dst_tmp_array[src_tmp.DstIndex];
- src_tmp.SrcRanges = src.GlyphRanges ? src.GlyphRanges : atlas->GetGlyphRangesDefault();
- for (const ImWchar* src_range = src_tmp.SrcRanges; src_range[0] && src_range[1]; src_range += 2)
- {
- // Check for valid range. This may also help detect *some* dangling pointers, because a common
- // user error is to setup ImFontConfig::GlyphRanges with a pointer to data that isn't persistent,
- // or to forget to zero-terminate the glyph range array.
- IM_ASSERT(src_range[0] <= src_range[1] && "Invalid range: is your glyph range array persistent? it is zero-terminated?");
- src_tmp.GlyphsHighest = ImMax(src_tmp.GlyphsHighest, (int)src_range[1]);
- }
- dst_tmp.SrcCount++;
- dst_tmp.GlyphsHighest = ImMax(dst_tmp.GlyphsHighest, src_tmp.GlyphsHighest);
- }
-
- // 2. For every requested codepoint, check for their presence in the font data, and handle redundancy or overlaps between source fonts to avoid unused glyphs.
- int total_glyphs_count = 0;
- for (int src_i = 0; src_i < src_tmp_array.Size; src_i++)
- {
- ImFontBuildSrcData& src_tmp = src_tmp_array[src_i];
- ImFontBuildDstData& dst_tmp = dst_tmp_array[src_tmp.DstIndex];
- src_tmp.GlyphsSet.Create(src_tmp.GlyphsHighest + 1);
- if (dst_tmp.GlyphsSet.Storage.empty())
- dst_tmp.GlyphsSet.Create(dst_tmp.GlyphsHighest + 1);
-
- for (const ImWchar* src_range = src_tmp.SrcRanges; src_range[0] && src_range[1]; src_range += 2)
- for (unsigned int codepoint = src_range[0]; codepoint <= src_range[1]; codepoint++)
- {
- if (dst_tmp.GlyphsSet.TestBit(codepoint)) // Don't overwrite existing glyphs. We could make this an option for MergeMode (e.g. MergeOverwrite==true)
- continue;
- if (!stbtt_FindGlyphIndex(&src_tmp.FontInfo, codepoint)) // It is actually in the font?
- continue;
-
- // Add to avail set/counters
- src_tmp.GlyphsCount++;
- dst_tmp.GlyphsCount++;
- src_tmp.GlyphsSet.SetBit(codepoint);
- dst_tmp.GlyphsSet.SetBit(codepoint);
- total_glyphs_count++;
- }
- }
-
- // 3. Unpack our bit map into a flat list (we now have all the Unicode points that we know are requested _and_ available _and_ not overlapping another)
- for (int src_i = 0; src_i < src_tmp_array.Size; src_i++)
- {
- ImFontBuildSrcData& src_tmp = src_tmp_array[src_i];
- src_tmp.GlyphsList.reserve(src_tmp.GlyphsCount);
- UnpackBitVectorToFlatIndexList(&src_tmp.GlyphsSet, &src_tmp.GlyphsList);
- src_tmp.GlyphsSet.Clear();
- IM_ASSERT(src_tmp.GlyphsList.Size == src_tmp.GlyphsCount);
- }
- for (int dst_i = 0; dst_i < dst_tmp_array.Size; dst_i++)
- dst_tmp_array[dst_i].GlyphsSet.Clear();
- dst_tmp_array.clear();
-
- // Allocate packing character data and flag packed characters buffer as non-packed (x0=y0=x1=y1=0)
- // (We technically don't need to zero-clear buf_rects, but let's do it for the sake of sanity)
- ImVector buf_rects;
- ImVector buf_packedchars;
- buf_rects.resize(total_glyphs_count);
- buf_packedchars.resize(total_glyphs_count);
- memset(buf_rects.Data, 0, (size_t)buf_rects.size_in_bytes());
- memset(buf_packedchars.Data, 0, (size_t)buf_packedchars.size_in_bytes());
-
- // 4. Gather glyphs sizes so we can pack them in our virtual canvas.
- int total_surface = 0;
- int buf_rects_out_n = 0;
- int buf_packedchars_out_n = 0;
- const int pack_padding = atlas->TexGlyphPadding;
- for (int src_i = 0; src_i < src_tmp_array.Size; src_i++)
- {
- ImFontBuildSrcData& src_tmp = src_tmp_array[src_i];
- if (src_tmp.GlyphsCount == 0)
- continue;
-
- src_tmp.Rects = &buf_rects[buf_rects_out_n];
- src_tmp.PackedChars = &buf_packedchars[buf_packedchars_out_n];
- buf_rects_out_n += src_tmp.GlyphsCount;
- buf_packedchars_out_n += src_tmp.GlyphsCount;
-
- // Automatic selection of oversampling parameters
- ImFontConfig& src = atlas->Sources[src_i];
- int oversample_h, oversample_v;
- ImFontAtlasBuildGetOversampleFactors(&src, &oversample_h, &oversample_v);
-
- // Convert our ranges in the format stb_truetype wants
- src_tmp.PackRange.font_size = src.SizePixels * src.RasterizerDensity;
- src_tmp.PackRange.first_unicode_codepoint_in_range = 0;
- src_tmp.PackRange.array_of_unicode_codepoints = src_tmp.GlyphsList.Data;
- src_tmp.PackRange.num_chars = src_tmp.GlyphsList.Size;
- src_tmp.PackRange.chardata_for_range = src_tmp.PackedChars;
- src_tmp.PackRange.h_oversample = (unsigned char)oversample_h;
- src_tmp.PackRange.v_oversample = (unsigned char)oversample_v;
-
- // Gather the sizes of all rectangles we will need to pack (this loop is based on stbtt_PackFontRangesGatherRects)
- const float scale = (src.SizePixels > 0.0f) ? stbtt_ScaleForPixelHeight(&src_tmp.FontInfo, src.SizePixels * src.RasterizerDensity) : stbtt_ScaleForMappingEmToPixels(&src_tmp.FontInfo, -src.SizePixels * src.RasterizerDensity);
- for (int glyph_i = 0; glyph_i < src_tmp.GlyphsList.Size; glyph_i++)
- {
- int x0, y0, x1, y1;
- const int glyph_index_in_font = stbtt_FindGlyphIndex(&src_tmp.FontInfo, src_tmp.GlyphsList[glyph_i]);
- IM_ASSERT(glyph_index_in_font != 0);
- stbtt_GetGlyphBitmapBoxSubpixel(&src_tmp.FontInfo, glyph_index_in_font, scale * oversample_h, scale * oversample_v, 0, 0, &x0, &y0, &x1, &y1);
- src_tmp.Rects[glyph_i].w = (stbrp_coord)(x1 - x0 + pack_padding + oversample_h - 1);
- src_tmp.Rects[glyph_i].h = (stbrp_coord)(y1 - y0 + pack_padding + oversample_v - 1);
- total_surface += src_tmp.Rects[glyph_i].w * src_tmp.Rects[glyph_i].h;
+ const ImWchar* ranges = src->GlyphRanges ? src->GlyphRanges : atlas->GetGlyphRangesDefault();
+ for (; ranges[0]; ranges += 2)
+ for (unsigned int c = ranges[0]; c <= ranges[1] && c <= IM_UNICODE_CODEPOINT_MAX; c++) //-V560
+ baked->FindGlyph((ImWchar)c);
}
}
- for (int i = 0; i < atlas->CustomRects.Size; i++)
- total_surface += (atlas->CustomRects[i].Width + pack_padding) * (atlas->CustomRects[i].Height + pack_padding);
-
- // We need a width for the skyline algorithm, any width!
- // The exact width doesn't really matter much, but some API/GPU have texture size limitations and increasing width can decrease height.
- // User can override TexDesiredWidth and TexGlyphPadding if they wish, otherwise we use a simple heuristic to select the width based on expected surface.
- const int surface_sqrt = (int)ImSqrt((float)total_surface) + 1;
- atlas->TexHeight = 0;
- if (atlas->TexDesiredWidth > 0)
- atlas->TexWidth = atlas->TexDesiredWidth;
- else
- atlas->TexWidth = (surface_sqrt >= 4096 * 0.7f) ? 4096 : (surface_sqrt >= 2048 * 0.7f) ? 2048 : (surface_sqrt >= 1024 * 0.7f) ? 1024 : 512;
-
- // 5. Start packing
- // Pack our extra data rectangles first, so it will be on the upper-left corner of our texture (UV will have small values).
- const int TEX_HEIGHT_MAX = 1024 * 32;
- stbtt_pack_context spc = {};
- stbtt_PackBegin(&spc, NULL, atlas->TexWidth, TEX_HEIGHT_MAX, 0, 0, NULL);
- spc.padding = atlas->TexGlyphPadding; // Because we mixup stbtt_PackXXX and stbrp_PackXXX there's a bit of a hack here, not passing the value to stbtt_PackBegin() allows us to still pack a TexWidth-1 wide item. (#8107)
- ImFontAtlasBuildPackCustomRects(atlas, spc.pack_info);
-
- // 6. Pack each source font. No rendering yet, we are working with rectangles in an infinitely tall texture at this point.
- for (int src_i = 0; src_i < src_tmp_array.Size; src_i++)
- {
- ImFontBuildSrcData& src_tmp = src_tmp_array[src_i];
- if (src_tmp.GlyphsCount == 0)
- continue;
-
- stbrp_pack_rects((stbrp_context*)spc.pack_info, src_tmp.Rects, src_tmp.GlyphsCount);
-
- // Extend texture height and mark missing glyphs as non-packed so we won't render them.
- // FIXME: We are not handling packing failure here (would happen if we got off TEX_HEIGHT_MAX or if a single if larger than TexWidth?)
- for (int glyph_i = 0; glyph_i < src_tmp.GlyphsCount; glyph_i++)
- if (src_tmp.Rects[glyph_i].was_packed)
- atlas->TexHeight = ImMax(atlas->TexHeight, src_tmp.Rects[glyph_i].y + src_tmp.Rects[glyph_i].h);
- }
-
- // 7. Allocate texture
- atlas->TexHeight = (atlas->Flags & ImFontAtlasFlags_NoPowerOfTwoHeight) ? (atlas->TexHeight + 1) : ImUpperPowerOfTwo(atlas->TexHeight);
- atlas->TexUvScale = ImVec2(1.0f / atlas->TexWidth, 1.0f / atlas->TexHeight);
- atlas->TexPixelsAlpha8 = (unsigned char*)IM_ALLOC(atlas->TexWidth * atlas->TexHeight);
- memset(atlas->TexPixelsAlpha8, 0, atlas->TexWidth * atlas->TexHeight);
- spc.pixels = atlas->TexPixelsAlpha8;
- spc.height = atlas->TexHeight;
-
- // 8. Render/rasterize font characters into the texture
- for (int src_i = 0; src_i < src_tmp_array.Size; src_i++)
- {
- ImFontConfig& src = atlas->Sources[src_i];
- ImFontBuildSrcData& src_tmp = src_tmp_array[src_i];
- if (src_tmp.GlyphsCount == 0)
- continue;
-
- stbtt_PackFontRangesRenderIntoRects(&spc, &src_tmp.FontInfo, &src_tmp.PackRange, 1, src_tmp.Rects);
-
- // Apply multiply operator
- if (src.RasterizerMultiply != 1.0f)
- {
- unsigned char multiply_table[256];
- ImFontAtlasBuildMultiplyCalcLookupTable(multiply_table, src.RasterizerMultiply);
- stbrp_rect* r = &src_tmp.Rects[0];
- for (int glyph_i = 0; glyph_i < src_tmp.GlyphsCount; glyph_i++, r++)
- if (r->was_packed)
- ImFontAtlasBuildMultiplyRectAlpha8(multiply_table, atlas->TexPixelsAlpha8, r->x, r->y, r->w, r->h, atlas->TexWidth * 1);
- }
- src_tmp.Rects = NULL;
- }
-
- // End packing
- stbtt_PackEnd(&spc);
- buf_rects.clear();
-
- // 9. Setup ImFont and glyphs for runtime
- for (int src_i = 0; src_i < src_tmp_array.Size; src_i++)
- {
- // When merging fonts with MergeMode=true:
- // - We can have multiple input fonts writing into a same destination font.
- // - dst_font->Sources is != from src which is our source configuration.
- ImFontBuildSrcData& src_tmp = src_tmp_array[src_i];
- ImFontConfig& src = atlas->Sources[src_i];
- ImFont* dst_font = src.DstFont;
-
- const float font_scale = stbtt_ScaleForPixelHeight(&src_tmp.FontInfo, src.SizePixels);
- int unscaled_ascent, unscaled_descent, unscaled_line_gap;
- stbtt_GetFontVMetrics(&src_tmp.FontInfo, &unscaled_ascent, &unscaled_descent, &unscaled_line_gap);
-
- const float ascent = ImCeil(unscaled_ascent * font_scale);
- const float descent = ImFloor(unscaled_descent * font_scale);
- ImFontAtlasBuildSetupFont(atlas, dst_font, &src, ascent, descent);
- const float font_off_x = src.GlyphOffset.x;
- const float font_off_y = src.GlyphOffset.y + IM_ROUND(dst_font->Ascent);
-
- const float inv_rasterization_scale = 1.0f / src.RasterizerDensity;
-
- for (int glyph_i = 0; glyph_i < src_tmp.GlyphsCount; glyph_i++)
- {
- // Register glyph
- const int codepoint = src_tmp.GlyphsList[glyph_i];
- const stbtt_packedchar& pc = src_tmp.PackedChars[glyph_i];
- stbtt_aligned_quad q;
- float unused_x = 0.0f, unused_y = 0.0f;
- stbtt_GetPackedQuad(src_tmp.PackedChars, atlas->TexWidth, atlas->TexHeight, glyph_i, &unused_x, &unused_y, &q, 0);
- float x0 = q.x0 * inv_rasterization_scale + font_off_x;
- float y0 = q.y0 * inv_rasterization_scale + font_off_y;
- float x1 = q.x1 * inv_rasterization_scale + font_off_x;
- float y1 = q.y1 * inv_rasterization_scale + font_off_y;
- dst_font->AddGlyph(&src, (ImWchar)codepoint, x0, y0, x1, y1, q.s0, q.t0, q.s1, q.t1, pc.xadvance * inv_rasterization_scale);
- }
- }
-
- // Cleanup
- src_tmp_array.clear_destruct();
-
- ImFontAtlasBuildFinish(atlas);
- return true;
}
-const ImFontBuilderIO* ImFontAtlasGetBuilderForStbTruetype()
-{
- static ImFontBuilderIO io;
- io.FontBuilder_Build = ImFontAtlasBuildWithStbTruetype;
- return &io;
-}
-
-#endif // IMGUI_ENABLE_STB_TRUETYPE
-
-void ImFontAtlasUpdateSourcesPointers(ImFontAtlas* atlas)
+// FIXME: May make ImFont::Sources a ImSpan<> and move ownership to ImFontAtlas
+void ImFontAtlasBuildUpdatePointers(ImFontAtlas* atlas)
{
+ for (ImFont* font : atlas->Fonts)
+ font->Sources.resize(0);
for (ImFontConfig& src : atlas->Sources)
+ src.DstFont->Sources.push_back(&src);
+}
+
+// Render a white-colored bitmap encoded in a string
+void ImFontAtlasBuildRenderBitmapFromString(ImFontAtlas* atlas, int x, int y, int w, int h, const char* in_str, char in_marker_char)
+{
+ ImTextureData* tex = atlas->TexData;
+ IM_ASSERT(x >= 0 && x + w <= tex->Width);
+ IM_ASSERT(y >= 0 && y + h <= tex->Height);
+
+ switch (tex->Format)
{
- ImFont* font = src.DstFont;
- if (!src.MergeMode)
- {
- font->Sources = &src;
- font->SourcesCount = 0;
- }
- font->SourcesCount++;
+ case ImTextureFormat_Alpha8:
+ {
+ ImU8* out_p = (ImU8*)tex->GetPixelsAt(x, y);
+ for (int off_y = 0; off_y < h; off_y++, out_p += tex->Width, in_str += w)
+ for (int off_x = 0; off_x < w; off_x++)
+ out_p[off_x] = (in_str[off_x] == in_marker_char) ? 0xFF : 0x00;
+ break;
+ }
+ case ImTextureFormat_RGBA32:
+ {
+ ImU32* out_p = (ImU32*)tex->GetPixelsAt(x, y);
+ for (int off_y = 0; off_y < h; off_y++, out_p += tex->Width, in_str += w)
+ for (int off_x = 0; off_x < w; off_x++)
+ out_p[off_x] = (in_str[off_x] == in_marker_char) ? IM_COL32_WHITE : IM_COL32_BLACK_TRANS;
+ break;
+ }
}
}
-void ImFontAtlasBuildSetupFont(ImFontAtlas* atlas, ImFont* font, ImFontConfig* font_config, float ascent, float descent)
+static void ImFontAtlasBuildUpdateBasicTexData(ImFontAtlas* atlas)
{
- if (!font_config->MergeMode)
+ // Pack and store identifier so we can refresh UV coordinates on texture resize.
+ // FIXME-NEWATLAS: User/custom rects where user code wants to store UV coordinates will need to do the same thing.
+ ImFontAtlasBuilder* builder = atlas->Builder;
+ ImVec2i pack_size = (atlas->Flags & ImFontAtlasFlags_NoMouseCursors) ? ImVec2i(2, 2) : ImVec2i(FONT_ATLAS_DEFAULT_TEX_DATA_W * 2 + 1, FONT_ATLAS_DEFAULT_TEX_DATA_H);
+
+ ImFontAtlasRect r;
+ bool add_and_draw = (atlas->GetCustomRect(builder->PackIdMouseCursors, &r) == false);
+ if (add_and_draw)
{
- font->ClearOutputData();
- font->FontSize = font_config->SizePixels;
- IM_ASSERT(font->Sources == font_config);
- font->ContainerAtlas = atlas;
- font->Ascent = ascent;
- font->Descent = descent;
- }
-}
+ builder->PackIdMouseCursors = atlas->AddCustomRect(pack_size.x, pack_size.y, &r);
+ IM_ASSERT(builder->PackIdMouseCursors != ImFontAtlasRectId_Invalid);
-void ImFontAtlasBuildPackCustomRects(ImFontAtlas* atlas, void* stbrp_context_opaque)
-{
- stbrp_context* pack_context = (stbrp_context*)stbrp_context_opaque;
- IM_ASSERT(pack_context != NULL);
-
- ImVector& user_rects = atlas->CustomRects;
- IM_ASSERT(user_rects.Size >= 1); // We expect at least the default custom rects to be registered, else something went wrong.
-#ifdef __GNUC__
- if (user_rects.Size < 1) { __builtin_unreachable(); } // Workaround for GCC bug if IM_ASSERT() is defined to conditionally throw (see #5343)
-#endif
-
- const int pack_padding = atlas->TexGlyphPadding;
- ImVector pack_rects;
- pack_rects.resize(user_rects.Size);
- memset(pack_rects.Data, 0, (size_t)pack_rects.size_in_bytes());
- for (int i = 0; i < user_rects.Size; i++)
- {
- pack_rects[i].w = user_rects[i].Width + pack_padding;
- pack_rects[i].h = user_rects[i].Height + pack_padding;
- }
- stbrp_pack_rects(pack_context, &pack_rects[0], pack_rects.Size);
- for (int i = 0; i < pack_rects.Size; i++)
- if (pack_rects[i].was_packed)
+ // Draw to texture
+ if (atlas->Flags & ImFontAtlasFlags_NoMouseCursors)
{
- user_rects[i].X = (unsigned short)pack_rects[i].x;
- user_rects[i].Y = (unsigned short)pack_rects[i].y;
- IM_ASSERT(pack_rects[i].w == user_rects[i].Width + pack_padding && pack_rects[i].h == user_rects[i].Height + pack_padding);
- atlas->TexHeight = ImMax(atlas->TexHeight, pack_rects[i].y + pack_rects[i].h);
- }
-}
-
-void ImFontAtlasBuildRender8bppRectFromString(ImFontAtlas* atlas, int x, int y, int w, int h, const char* in_str, char in_marker_char, unsigned char in_marker_pixel_value)
-{
- IM_ASSERT(x >= 0 && x + w <= atlas->TexWidth);
- IM_ASSERT(y >= 0 && y + h <= atlas->TexHeight);
- unsigned char* out_pixel = atlas->TexPixelsAlpha8 + x + (y * atlas->TexWidth);
- for (int off_y = 0; off_y < h; off_y++, out_pixel += atlas->TexWidth, in_str += w)
- for (int off_x = 0; off_x < w; off_x++)
- out_pixel[off_x] = (in_str[off_x] == in_marker_char) ? in_marker_pixel_value : 0x00;
-}
-
-void ImFontAtlasBuildRender32bppRectFromString(ImFontAtlas* atlas, int x, int y, int w, int h, const char* in_str, char in_marker_char, unsigned int in_marker_pixel_value)
-{
- IM_ASSERT(x >= 0 && x + w <= atlas->TexWidth);
- IM_ASSERT(y >= 0 && y + h <= atlas->TexHeight);
- unsigned int* out_pixel = atlas->TexPixelsRGBA32 + x + (y * atlas->TexWidth);
- for (int off_y = 0; off_y < h; off_y++, out_pixel += atlas->TexWidth, in_str += w)
- for (int off_x = 0; off_x < w; off_x++)
- out_pixel[off_x] = (in_str[off_x] == in_marker_char) ? in_marker_pixel_value : IM_COL32_BLACK_TRANS;
-}
-
-static void ImFontAtlasBuildRenderDefaultTexData(ImFontAtlas* atlas)
-{
- ImFontAtlasCustomRect* r = atlas->GetCustomRectByIndex(atlas->PackIdMouseCursors);
- IM_ASSERT(r->IsPacked());
-
- const int w = atlas->TexWidth;
- if (atlas->Flags & ImFontAtlasFlags_NoMouseCursors)
- {
- // White pixels only
- IM_ASSERT(r->Width == 2 && r->Height == 2);
- const int offset = (int)r->X + (int)r->Y * w;
- if (atlas->TexPixelsAlpha8 != NULL)
- {
- atlas->TexPixelsAlpha8[offset] = atlas->TexPixelsAlpha8[offset + 1] = atlas->TexPixelsAlpha8[offset + w] = atlas->TexPixelsAlpha8[offset + w + 1] = 0xFF;
+ // 2x2 white pixels
+ ImFontAtlasBuildRenderBitmapFromString(atlas, r.x, r.y, 2, 2, "XX" "XX", 'X');
}
else
{
- atlas->TexPixelsRGBA32[offset] = atlas->TexPixelsRGBA32[offset + 1] = atlas->TexPixelsRGBA32[offset + w] = atlas->TexPixelsRGBA32[offset + w + 1] = IM_COL32_WHITE;
+ // 2x2 white pixels + mouse cursors
+ const int x_for_white = r.x;
+ const int x_for_black = r.x + FONT_ATLAS_DEFAULT_TEX_DATA_W + 1;
+ ImFontAtlasBuildRenderBitmapFromString(atlas, x_for_white, r.y, FONT_ATLAS_DEFAULT_TEX_DATA_W, FONT_ATLAS_DEFAULT_TEX_DATA_H, FONT_ATLAS_DEFAULT_TEX_DATA_PIXELS, '.');
+ ImFontAtlasBuildRenderBitmapFromString(atlas, x_for_black, r.y, FONT_ATLAS_DEFAULT_TEX_DATA_W, FONT_ATLAS_DEFAULT_TEX_DATA_H, FONT_ATLAS_DEFAULT_TEX_DATA_PIXELS, 'X');
}
}
- else
- {
- // White pixels and mouse cursor
- IM_ASSERT(r->Width == FONT_ATLAS_DEFAULT_TEX_DATA_W * 2 + 1 && r->Height == FONT_ATLAS_DEFAULT_TEX_DATA_H);
- const int x_for_white = r->X;
- const int x_for_black = r->X + FONT_ATLAS_DEFAULT_TEX_DATA_W + 1;
- if (atlas->TexPixelsAlpha8 != NULL)
- {
- ImFontAtlasBuildRender8bppRectFromString(atlas, x_for_white, r->Y, FONT_ATLAS_DEFAULT_TEX_DATA_W, FONT_ATLAS_DEFAULT_TEX_DATA_H, FONT_ATLAS_DEFAULT_TEX_DATA_PIXELS, '.', 0xFF);
- ImFontAtlasBuildRender8bppRectFromString(atlas, x_for_black, r->Y, FONT_ATLAS_DEFAULT_TEX_DATA_W, FONT_ATLAS_DEFAULT_TEX_DATA_H, FONT_ATLAS_DEFAULT_TEX_DATA_PIXELS, 'X', 0xFF);
- }
- else
- {
- ImFontAtlasBuildRender32bppRectFromString(atlas, x_for_white, r->Y, FONT_ATLAS_DEFAULT_TEX_DATA_W, FONT_ATLAS_DEFAULT_TEX_DATA_H, FONT_ATLAS_DEFAULT_TEX_DATA_PIXELS, '.', IM_COL32_WHITE);
- ImFontAtlasBuildRender32bppRectFromString(atlas, x_for_black, r->Y, FONT_ATLAS_DEFAULT_TEX_DATA_W, FONT_ATLAS_DEFAULT_TEX_DATA_H, FONT_ATLAS_DEFAULT_TEX_DATA_PIXELS, 'X', IM_COL32_WHITE);
- }
- }
- atlas->TexUvWhitePixel = ImVec2((r->X + 0.5f) * atlas->TexUvScale.x, (r->Y + 0.5f) * atlas->TexUvScale.y);
+
+ // Refresh UV coordinates
+ atlas->TexUvWhitePixel = ImVec2((r.x + 0.5f) * atlas->TexUvScale.x, (r.y + 0.5f) * atlas->TexUvScale.y);
}
-static void ImFontAtlasBuildRenderLinesTexData(ImFontAtlas* atlas)
+static void ImFontAtlasBuildUpdateLinesTexData(ImFontAtlas* atlas)
{
if (atlas->Flags & ImFontAtlasFlags_NoBakedLines)
return;
+ // Pack and store identifier so we can refresh UV coordinates on texture resize.
+ ImTextureData* tex = atlas->TexData;
+ ImFontAtlasBuilder* builder = atlas->Builder;
+
+ ImFontAtlasRect r;
+ bool add_and_draw = atlas->GetCustomRect(builder->PackIdLinesTexData, &r) == false;
+ if (add_and_draw)
+ {
+ ImVec2i pack_size = ImVec2i(IM_DRAWLIST_TEX_LINES_WIDTH_MAX + 2, IM_DRAWLIST_TEX_LINES_WIDTH_MAX + 1);
+ builder->PackIdLinesTexData = atlas->AddCustomRect(pack_size.x, pack_size.y, &r);
+ IM_ASSERT(builder->PackIdLinesTexData != ImFontAtlasRectId_Invalid);
+ }
+
+ // Register texture region for thick lines
+ // The +2 here is to give space for the end caps, whilst height +1 is to accommodate the fact we have a zero-width row
// This generates a triangular shape in the texture, with the various line widths stacked on top of each other to allow interpolation between them
- ImFontAtlasCustomRect* r = atlas->GetCustomRectByIndex(atlas->PackIdLines);
- IM_ASSERT(r->IsPacked());
for (int n = 0; n < IM_DRAWLIST_TEX_LINES_WIDTH_MAX + 1; n++) // +1 because of the zero-width row
{
// Each line consists of at least two empty pixels at the ends, with a line of solid pixels in the middle
- int y = n;
- int line_width = n;
- int pad_left = (r->Width - line_width) / 2;
- int pad_right = r->Width - (pad_left + line_width);
+ const int y = n;
+ const int line_width = n;
+ const int pad_left = (r.w - line_width) / 2;
+ const int pad_right = r.w - (pad_left + line_width);
+ IM_ASSERT(pad_left + line_width + pad_right == r.w && y < r.h); // Make sure we're inside the texture bounds before we start writing pixels
// Write each slice
- IM_ASSERT(pad_left + line_width + pad_right == r->Width && y < r->Height); // Make sure we're inside the texture bounds before we start writing pixels
- if (atlas->TexPixelsAlpha8 != NULL)
+ if (add_and_draw && tex->Format == ImTextureFormat_Alpha8)
{
- unsigned char* write_ptr = &atlas->TexPixelsAlpha8[r->X + ((r->Y + y) * atlas->TexWidth)];
+ ImU8* write_ptr = (ImU8*)tex->GetPixelsAt(r.x, r.y + y);
for (int i = 0; i < pad_left; i++)
*(write_ptr + i) = 0x00;
@@ -3306,9 +3561,9 @@ static void ImFontAtlasBuildRenderLinesTexData(ImFontAtlas* atlas)
for (int i = 0; i < pad_right; i++)
*(write_ptr + pad_left + line_width + i) = 0x00;
}
- else
+ else if (add_and_draw && tex->Format == ImTextureFormat_RGBA32)
{
- unsigned int* write_ptr = &atlas->TexPixelsRGBA32[r->X + ((r->Y + y) * atlas->TexWidth)];
+ ImU32* write_ptr = (ImU32*)(void*)tex->GetPixelsAt(r.x, r.y + y);
for (int i = 0; i < pad_left; i++)
*(write_ptr + i) = IM_COL32(255, 255, 255, 0);
@@ -3319,71 +3574,1179 @@ static void ImFontAtlasBuildRenderLinesTexData(ImFontAtlas* atlas)
*(write_ptr + pad_left + line_width + i) = IM_COL32(255, 255, 255, 0);
}
- // Calculate UVs for this line
- ImVec2 uv0 = ImVec2((float)(r->X + pad_left - 1), (float)(r->Y + y)) * atlas->TexUvScale;
- ImVec2 uv1 = ImVec2((float)(r->X + pad_left + line_width + 1), (float)(r->Y + y + 1)) * atlas->TexUvScale;
+ // Refresh UV coordinates
+ ImVec2 uv0 = ImVec2((float)(r.x + pad_left - 1), (float)(r.y + y)) * atlas->TexUvScale;
+ ImVec2 uv1 = ImVec2((float)(r.x + pad_left + line_width + 1), (float)(r.y + y + 1)) * atlas->TexUvScale;
float half_v = (uv0.y + uv1.y) * 0.5f; // Calculate a constant V in the middle of the row to avoid sampling artifacts
atlas->TexUvLines[n] = ImVec4(uv0.x, half_v, uv1.x, half_v);
}
}
-// Note: this is called / shared by both the stb_truetype and the FreeType builder
+//-----------------------------------------------------------------------------------------------------------------------------
+
+// Was tempted to lazily init FontSrc but wouldn't save much + makes it more complicated to detect invalid data at AddFont()
+bool ImFontAtlasFontInitOutput(ImFontAtlas* atlas, ImFont* font)
+{
+ bool ret = true;
+ for (ImFontConfig* src : font->Sources)
+ if (!ImFontAtlasFontSourceInit(atlas, src))
+ ret = false;
+ IM_ASSERT(ret); // Unclear how to react to this meaningfully. Assume that result will be same as initial AddFont() call.
+ return ret;
+}
+
+// Keep source/input FontData
+void ImFontAtlasFontDestroyOutput(ImFontAtlas* atlas, ImFont* font)
+{
+ font->ClearOutputData();
+ for (ImFontConfig* src : font->Sources)
+ {
+ const ImFontLoader* loader = src->FontLoader ? src->FontLoader : atlas->FontLoader;
+ if (loader && loader->FontSrcDestroy != NULL)
+ loader->FontSrcDestroy(atlas, src);
+ }
+}
+
+//-----------------------------------------------------------------------------------------------------------------------------
+
+bool ImFontAtlasFontSourceInit(ImFontAtlas* atlas, ImFontConfig* src)
+{
+ const ImFontLoader* loader = src->FontLoader ? src->FontLoader : atlas->FontLoader;
+ if (loader->FontSrcInit != NULL && !loader->FontSrcInit(atlas, src))
+ return false;
+ return true;
+}
+
+void ImFontAtlasFontSourceAddToFont(ImFontAtlas* atlas, ImFont* font, ImFontConfig* src)
+{
+ if (src->MergeMode == false)
+ {
+ font->ClearOutputData();
+ //font->FontSize = src->SizePixels;
+ font->ContainerAtlas = atlas;
+ IM_ASSERT(font->Sources[0] == src);
+ }
+ atlas->TexIsBuilt = false; // For legacy backends
+ ImFontAtlasBuildSetupFontSpecialGlyphs(atlas, font, src);
+}
+
+void ImFontAtlasFontDestroySourceData(ImFontAtlas* atlas, ImFontConfig* src)
+{
+ IM_UNUSED(atlas);
+ if (src->FontDataOwnedByAtlas)
+ IM_FREE(src->FontData);
+ src->FontData = NULL;
+ if (src->GlyphExcludeRanges)
+ IM_FREE((void*)src->GlyphExcludeRanges);
+ src->GlyphExcludeRanges = NULL;
+}
+
+// Create a compact, baked "..." if it doesn't exist, by using the ".".
+// This may seem overly complicated right now but the point is to exercise and improve a technique which should be increasingly used.
+// FIXME-NEWATLAS: This borrows too much from FontLoader's FontLoadGlyph() handlers and suggest that we should add further helpers.
+static ImFontGlyph* ImFontAtlasBuildSetupFontBakedEllipsis(ImFontAtlas* atlas, ImFontBaked* baked)
+{
+ ImFont* font = baked->ContainerFont;
+ IM_ASSERT(font->EllipsisChar != 0);
+
+ const ImFontGlyph* dot_glyph = baked->FindGlyphNoFallback((ImWchar)'.');
+ if (dot_glyph == NULL)
+ dot_glyph = baked->FindGlyphNoFallback((ImWchar)0xFF0E);
+ if (dot_glyph == NULL)
+ return NULL;
+ ImFontAtlasRectId dot_r_id = dot_glyph->PackId; // Deep copy to avoid invalidation of glyphs and rect pointers
+ ImTextureRect* dot_r = ImFontAtlasPackGetRect(atlas, dot_r_id);
+ const int dot_spacing = 1;
+ const float dot_step = (dot_glyph->X1 - dot_glyph->X0) + dot_spacing;
+
+ ImFontAtlasRectId pack_id = ImFontAtlasPackAddRect(atlas, (dot_r->w * 3 + dot_spacing * 2), dot_r->h);
+ ImTextureRect* r = ImFontAtlasPackGetRect(atlas, pack_id);
+
+ ImFontGlyph glyph_in = {};
+ ImFontGlyph* glyph = &glyph_in;
+ glyph->Codepoint = font->EllipsisChar;
+ glyph->AdvanceX = ImMax(dot_glyph->AdvanceX, dot_glyph->X0 + dot_step * 3.0f - dot_spacing); // FIXME: Slightly odd for normally mono-space fonts but since this is used for trailing contents.
+ glyph->X0 = dot_glyph->X0;
+ glyph->Y0 = dot_glyph->Y0;
+ glyph->X1 = dot_glyph->X0 + dot_step * 3 - dot_spacing;
+ glyph->Y1 = dot_glyph->Y1;
+ glyph->Visible = true;
+ glyph->PackId = pack_id;
+ glyph = ImFontAtlasBakedAddFontGlyph(atlas, baked, NULL, glyph);
+ dot_glyph = NULL; // Invalidated
+
+ // Copy to texture, post-process and queue update for backend
+ // FIXME-NEWATLAS-V2: Dot glyph is already post-processed as this point, so this would damage it.
+ dot_r = ImFontAtlasPackGetRect(atlas, dot_r_id);
+ ImTextureData* tex = atlas->TexData;
+ for (int n = 0; n < 3; n++)
+ ImFontAtlasTextureBlockCopy(tex, dot_r->x, dot_r->y, tex, r->x + (dot_r->w + dot_spacing) * n, r->y, dot_r->w, dot_r->h);
+ ImFontAtlasTextureBlockQueueUpload(atlas, tex, r->x, r->y, r->w, r->h);
+
+ return glyph;
+}
+
+// Load fallback in order to obtain its index
+// (this is called from in hot-path so we avoid extraneous parameters to minimize impact on code size)
+static void ImFontAtlasBuildSetupFontBakedFallback(ImFontBaked* baked)
+{
+ IM_ASSERT(baked->FallbackGlyphIndex == -1);
+ IM_ASSERT(baked->FallbackAdvanceX == 0.0f);
+ ImFont* font = baked->ContainerFont;
+ ImFontGlyph* fallback_glyph = NULL;
+ if (font->FallbackChar != 0)
+ fallback_glyph = baked->FindGlyphNoFallback(font->FallbackChar);
+ if (fallback_glyph == NULL)
+ {
+ ImFontGlyph* space_glyph = baked->FindGlyphNoFallback((ImWchar)' ');
+ ImFontGlyph glyph;
+ glyph.Codepoint = 0;
+ glyph.AdvanceX = space_glyph ? space_glyph->AdvanceX : IM_ROUND(baked->Size * 0.40f);
+ fallback_glyph = ImFontAtlasBakedAddFontGlyph(font->ContainerAtlas, baked, NULL, &glyph);
+ }
+ baked->FallbackGlyphIndex = baked->Glyphs.index_from_ptr(fallback_glyph); // Storing index avoid need to update pointer on growth and simplify inner loop code
+ baked->FallbackAdvanceX = fallback_glyph->AdvanceX;
+}
+
+static void ImFontAtlasBuildSetupFontBakedBlanks(ImFontAtlas* atlas, ImFontBaked* baked)
+{
+ // Mark space as always hidden (not strictly correct/necessary. but some e.g. icons fonts don't have a space. it tends to look neater in previews)
+ ImFontGlyph* space_glyph = baked->FindGlyphNoFallback((ImWchar)' ');
+ if (space_glyph != NULL)
+ space_glyph->Visible = false;
+
+ // Setup Tab character.
+ // FIXME: Needs proper TAB handling but it needs to be contextualized (or we could arbitrary say that each string starts at "column 0" ?)
+ if (baked->FindGlyphNoFallback('\t') == NULL && space_glyph != NULL)
+ {
+ ImFontGlyph tab_glyph;
+ tab_glyph.Codepoint = '\t';
+ tab_glyph.AdvanceX = space_glyph->AdvanceX * IM_TABSIZE;
+ ImFontAtlasBakedAddFontGlyph(atlas, baked, NULL, &tab_glyph);
+ }
+}
+
+// Load/identify special glyphs
+// (note that this is called again for fonts with MergeMode)
+void ImFontAtlasBuildSetupFontSpecialGlyphs(ImFontAtlas* atlas, ImFont* font, ImFontConfig* src)
+{
+ IM_UNUSED(atlas);
+ IM_ASSERT(font->Sources.contains(src));
+
+ // Find Fallback character. Actual glyph loaded in GetFontBaked().
+ const ImWchar fallback_chars[] = { font->FallbackChar, (ImWchar)IM_UNICODE_CODEPOINT_INVALID, (ImWchar)'?', (ImWchar)' ' };
+ if (font->FallbackChar == 0)
+ for (ImWchar candidate_char : fallback_chars)
+ if (candidate_char != 0 && font->IsGlyphInFont(candidate_char))
+ {
+ font->FallbackChar = (ImWchar)candidate_char;
+ break;
+ }
+
+ // Setup Ellipsis character. It is required for rendering elided text. We prefer using U+2026 (horizontal ellipsis).
+ // However some old fonts may contain ellipsis at U+0085. Here we auto-detect most suitable ellipsis character.
+ // FIXME: Note that 0x2026 is rarely included in our font ranges. Because of this we are more likely to use three individual dots.
+ const ImWchar ellipsis_chars[] = { src->EllipsisChar, (ImWchar)0x2026, (ImWchar)0x0085 };
+ if (font->EllipsisChar == 0)
+ for (ImWchar candidate_char : ellipsis_chars)
+ if (candidate_char != 0 && font->IsGlyphInFont(candidate_char))
+ {
+ font->EllipsisChar = candidate_char;
+ break;
+ }
+ if (font->EllipsisChar == 0)
+ {
+ font->EllipsisChar = 0x0085;
+ font->EllipsisAutoBake = true;
+ }
+}
+
+void ImFontAtlasBakedDiscardFontGlyph(ImFontAtlas* atlas, ImFont* font, ImFontBaked* baked, ImFontGlyph* glyph)
+{
+ if (glyph->PackId != ImFontAtlasRectId_Invalid)
+ {
+ ImFontAtlasPackDiscardRect(atlas, glyph->PackId);
+ glyph->PackId = ImFontAtlasRectId_Invalid;
+ }
+ ImWchar c = (ImWchar)glyph->Codepoint;
+ IM_ASSERT(font->FallbackChar != c && font->EllipsisChar != c); // Unsupported for simplicity
+ IM_ASSERT(glyph >= baked->Glyphs.Data && glyph < baked->Glyphs.Data + baked->Glyphs.Size);
+ IM_UNUSED(font);
+ baked->IndexLookup[c] = IM_FONTGLYPH_INDEX_UNUSED;
+ baked->IndexAdvanceX[c] = baked->FallbackAdvanceX;
+}
+
+ImFontBaked* ImFontAtlasBakedAdd(ImFontAtlas* atlas, ImFont* font, float font_size, float font_rasterizer_density, ImGuiID baked_id)
+{
+ IMGUI_DEBUG_LOG_FONT("[font] Created baked %.2fpx\n", font_size);
+ ImFontBaked* baked = atlas->Builder->BakedPool.push_back(ImFontBaked());
+ baked->Size = font_size;
+ baked->RasterizerDensity = font_rasterizer_density;
+ baked->BakedId = baked_id;
+ baked->ContainerFont = font;
+ baked->LastUsedFrame = atlas->Builder->FrameCount;
+
+ // Initialize backend data
+ size_t loader_data_size = 0;
+ for (ImFontConfig* src : font->Sources) // Cannot easily be cached as we allow changing backend
+ {
+ const ImFontLoader* loader = src->FontLoader ? src->FontLoader : atlas->FontLoader;
+ loader_data_size += loader->FontBakedSrcLoaderDataSize;
+ }
+ baked->FontLoaderDatas = (loader_data_size > 0) ? IM_ALLOC(loader_data_size) : NULL;
+ char* loader_data_p = (char*)baked->FontLoaderDatas;
+ for (ImFontConfig* src : font->Sources)
+ {
+ const ImFontLoader* loader = src->FontLoader ? src->FontLoader : atlas->FontLoader;
+ if (loader->FontBakedInit)
+ loader->FontBakedInit(atlas, src, baked, loader_data_p);
+ loader_data_p += loader->FontBakedSrcLoaderDataSize;
+ }
+
+ ImFontAtlasBuildSetupFontBakedBlanks(atlas, baked);
+ return baked;
+}
+
+// FIXME-OPT: This is not a fast query. Adding a BakedCount field in Font might allow to take a shortcut for the most common case.
+ImFontBaked* ImFontAtlasBakedGetClosestMatch(ImFontAtlas* atlas, ImFont* font, float font_size, float font_rasterizer_density)
+{
+ ImFontAtlasBuilder* builder = atlas->Builder;
+ for (int step_n = 0; step_n < 2; step_n++)
+ {
+ ImFontBaked* closest_larger_match = NULL;
+ ImFontBaked* closest_smaller_match = NULL;
+ for (int baked_n = 0; baked_n < builder->BakedPool.Size; baked_n++)
+ {
+ ImFontBaked* baked = &builder->BakedPool[baked_n];
+ if (baked->ContainerFont != font || baked->WantDestroy)
+ continue;
+ if (step_n == 0 && baked->RasterizerDensity != font_rasterizer_density) // First try with same density
+ continue;
+ if (baked->Size > font_size && (closest_larger_match == NULL || baked->Size < closest_larger_match->Size))
+ closest_larger_match = baked;
+ if (baked->Size < font_size && (closest_smaller_match == NULL || baked->Size > closest_smaller_match->Size))
+ closest_smaller_match = baked;
+ }
+ if (closest_larger_match)
+ if (closest_smaller_match == NULL || (closest_larger_match->Size >= font_size * 2.0f && closest_smaller_match->Size > font_size * 0.5f))
+ return closest_larger_match;
+ if (closest_smaller_match)
+ return closest_smaller_match;
+ }
+ return NULL;
+}
+
+void ImFontAtlasBakedDiscard(ImFontAtlas* atlas, ImFont* font, ImFontBaked* baked)
+{
+ ImFontAtlasBuilder* builder = atlas->Builder;
+ IMGUI_DEBUG_LOG_FONT("[font] Discard baked %.2f for \"%s\"\n", baked->Size, font->GetDebugName());
+
+ for (ImFontGlyph& glyph : baked->Glyphs)
+ if (glyph.PackId != ImFontAtlasRectId_Invalid)
+ ImFontAtlasPackDiscardRect(atlas, glyph.PackId);
+
+ char* loader_data_p = (char*)baked->FontLoaderDatas;
+ for (ImFontConfig* src : font->Sources)
+ {
+ const ImFontLoader* loader = src->FontLoader ? src->FontLoader : atlas->FontLoader;
+ if (loader->FontBakedDestroy)
+ loader->FontBakedDestroy(atlas, src, baked, loader_data_p);
+ loader_data_p += loader->FontBakedSrcLoaderDataSize;
+ }
+ if (baked->FontLoaderDatas)
+ {
+ IM_FREE(baked->FontLoaderDatas);
+ baked->FontLoaderDatas = NULL;
+ }
+ builder->BakedMap.SetVoidPtr(baked->BakedId, NULL);
+ builder->BakedDiscardedCount++;
+ baked->ClearOutputData();
+ baked->WantDestroy = true;
+ font->LastBaked = NULL;
+}
+
+// use unused_frames==0 to discard everything.
+void ImFontAtlasFontDiscardBakes(ImFontAtlas* atlas, ImFont* font, int unused_frames)
+{
+ if (ImFontAtlasBuilder* builder = atlas->Builder) // This can be called from font destructor
+ for (int baked_n = 0; baked_n < builder->BakedPool.Size; baked_n++)
+ {
+ ImFontBaked* baked = &builder->BakedPool[baked_n];
+ if (baked->LastUsedFrame + unused_frames > atlas->Builder->FrameCount)
+ continue;
+ if (baked->ContainerFont != font || baked->WantDestroy)
+ continue;
+ ImFontAtlasBakedDiscard(atlas, font, baked);
+ }
+}
+
+// use unused_frames==0 to discard everything.
+void ImFontAtlasBuildDiscardBakes(ImFontAtlas* atlas, int unused_frames)
+{
+ ImFontAtlasBuilder* builder = atlas->Builder;
+ for (int baked_n = 0; baked_n < builder->BakedPool.Size; baked_n++)
+ {
+ ImFontBaked* baked = &builder->BakedPool[baked_n];
+ if (baked->LastUsedFrame + unused_frames > atlas->Builder->FrameCount)
+ continue;
+ if (baked->WantDestroy || (baked->ContainerFont->Flags & ImFontFlags_LockBakedSizes))
+ continue;
+ ImFontAtlasBakedDiscard(atlas, baked->ContainerFont, baked);
+ }
+}
+
+// Those functions are designed to facilitate changing the underlying structures for ImFontAtlas to store an array of ImDrawListSharedData*
+void ImFontAtlasAddDrawListSharedData(ImFontAtlas* atlas, ImDrawListSharedData* data)
+{
+ IM_ASSERT(!atlas->DrawListSharedDatas.contains(data));
+ atlas->DrawListSharedDatas.push_back(data);
+}
+
+void ImFontAtlasRemoveDrawListSharedData(ImFontAtlas* atlas, ImDrawListSharedData* data)
+{
+ IM_ASSERT(atlas->DrawListSharedDatas.contains(data));
+ atlas->DrawListSharedDatas.find_erase(data);
+}
+
+// Update texture identifier in all active draw lists
+void ImFontAtlasUpdateDrawListsTextures(ImFontAtlas* atlas, ImTextureRef old_tex, ImTextureRef new_tex)
+{
+ for (ImDrawListSharedData* shared_data : atlas->DrawListSharedDatas)
+ for (ImDrawList* draw_list : shared_data->DrawLists)
+ {
+ // Replace in command-buffer
+ // (there is not need to replace in ImDrawListSplitter: current channel is in ImDrawList's CmdBuffer[],
+ // other channels will be on SetCurrentChannel() which already needs to compare CmdHeader anyhow)
+ if (draw_list->CmdBuffer.Size > 0 && draw_list->_CmdHeader.TexRef == old_tex)
+ draw_list->_SetTexture(new_tex);
+
+ // Replace in stack
+ for (ImTextureRef& stacked_tex : draw_list->_TextureStack)
+ if (stacked_tex == old_tex)
+ stacked_tex = new_tex;
+ }
+}
+
+// Update texture coordinates in all draw list shared context
+// FIXME-NEWATLAS FIXME-OPT: Doesn't seem necessary to update for all, only one bound to current context?
+void ImFontAtlasUpdateDrawListsSharedData(ImFontAtlas* atlas)
+{
+ for (ImDrawListSharedData* shared_data : atlas->DrawListSharedDatas)
+ if (shared_data->FontAtlas == atlas)
+ {
+ shared_data->TexUvWhitePixel = atlas->TexUvWhitePixel;
+ shared_data->TexUvLines = atlas->TexUvLines;
+ }
+}
+
+// Set current texture. This is mostly called from AddTexture() + to handle a failed resize.
+static void ImFontAtlasBuildSetTexture(ImFontAtlas* atlas, ImTextureData* tex)
+{
+ ImTextureRef old_tex_ref = atlas->TexRef;
+ atlas->TexData = tex;
+ atlas->TexUvScale = ImVec2(1.0f / tex->Width, 1.0f / tex->Height);
+ atlas->TexRef._TexData = tex;
+ //atlas->TexRef._TexID = tex->TexID; // <-- We intentionally don't do that. It would be misleading and betray promise that both fields aren't set.
+ ImFontAtlasUpdateDrawListsTextures(atlas, old_tex_ref, atlas->TexRef);
+}
+
+// Create a new texture, discard previous one
+ImTextureData* ImFontAtlasTextureAdd(ImFontAtlas* atlas, int w, int h)
+{
+ ImTextureData* old_tex = atlas->TexData;
+ ImTextureData* new_tex;
+
+ // FIXME: Cannot reuse texture because old UV may have been used already (unless we remap UV).
+ /*if (old_tex != NULL && old_tex->Status == ImTextureStatus_WantCreate)
+ {
+ // Reuse texture not yet used by backend.
+ IM_ASSERT(old_tex->TexID == ImTextureID_Invalid && old_tex->BackendUserData == NULL);
+ old_tex->DestroyPixels();
+ old_tex->Updates.clear();
+ new_tex = old_tex;
+ old_tex = NULL;
+ }
+ else*/
+ {
+ // Add new
+ new_tex = IM_NEW(ImTextureData)();
+ new_tex->UniqueID = atlas->TexNextUniqueID++;
+ atlas->TexList.push_back(new_tex);
+ }
+ if (old_tex != NULL)
+ {
+ // Queue old as to destroy next frame
+ old_tex->WantDestroyNextFrame = true;
+ IM_ASSERT(old_tex->Status == ImTextureStatus_OK || old_tex->Status == ImTextureStatus_WantCreate || old_tex->Status == ImTextureStatus_WantUpdates);
+ }
+
+ new_tex->Create(atlas->TexDesiredFormat, w, h);
+ atlas->TexIsBuilt = false;
+
+ ImFontAtlasBuildSetTexture(atlas, new_tex);
+
+ return new_tex;
+}
+
+#if 0
+#define STB_IMAGE_WRITE_IMPLEMENTATION
+#include "../stb/stb_image_write.h"
+static void ImFontAtlasDebugWriteTexToDisk(ImTextureData* tex, const char* description)
+{
+ ImGuiContext& g = *GImGui;
+ char buf[128];
+ ImFormatString(buf, IM_ARRAYSIZE(buf), "[%05d] Texture #%03d - %s.png", g.FrameCount, tex->UniqueID, description);
+ stbi_write_png(buf, tex->Width, tex->Height, tex->BytesPerPixel, tex->Pixels, tex->GetPitch()); // tex->BytesPerPixel is technically not component, but ok for the formats we support.
+}
+#endif
+
+void ImFontAtlasTextureRepack(ImFontAtlas* atlas, int w, int h)
+{
+ ImFontAtlasBuilder* builder = atlas->Builder;
+ builder->LockDisableResize = true;
+
+ ImTextureData* old_tex = atlas->TexData;
+ ImTextureData* new_tex = ImFontAtlasTextureAdd(atlas, w, h);
+ new_tex->UseColors = old_tex->UseColors;
+ IMGUI_DEBUG_LOG_FONT("[font] Texture #%03d: resize+repack %dx%d => Texture #%03d: %dx%d\n", old_tex->UniqueID, old_tex->Width, old_tex->Height, new_tex->UniqueID, new_tex->Width, new_tex->Height);
+ //for (int baked_n = 0; baked_n < builder->BakedPool.Size; baked_n++)
+ // IMGUI_DEBUG_LOG_FONT("[font] - Baked %.2fpx, %d glyphs, want_destroy=%d\n", builder->BakedPool[baked_n].FontSize, builder->BakedPool[baked_n].Glyphs.Size, builder->BakedPool[baked_n].WantDestroy);
+ //IMGUI_DEBUG_LOG_FONT("[font] - Old packed rects: %d, area %d px\n", builder->RectsPackedCount, builder->RectsPackedSurface);
+ //ImFontAtlasDebugWriteTexToDisk(old_tex, "Before Pack");
+
+ // Repack, lose discarded rectangle, copy pixels
+ // FIXME-NEWATLAS: This is unstable because packing order is based on RectsIndex
+ // FIXME-NEWATLAS-V2: Repacking in batch would be beneficial to packing heuristic, and fix stability.
+ // FIXME-NEWATLAS-TESTS: Test calling RepackTexture with size too small to fits existing rects.
+ ImFontAtlasPackInit(atlas);
+ ImVector old_rects;
+ ImVector old_index = builder->RectsIndex;
+ old_rects.swap(builder->Rects);
+
+ for (ImFontAtlasRectEntry& index_entry : builder->RectsIndex)
+ {
+ if (index_entry.IsUsed == false)
+ continue;
+ ImTextureRect& old_r = old_rects[index_entry.TargetIndex];
+ if (old_r.w == 0 && old_r.h == 0)
+ continue;
+ ImFontAtlasRectId new_r_id = ImFontAtlasPackAddRect(atlas, old_r.w, old_r.h, &index_entry);
+ if (new_r_id == ImFontAtlasRectId_Invalid)
+ {
+ // Undo, grow texture and try repacking again.
+ // FIXME-NEWATLAS-TESTS: This is a very rarely exercised path! It needs to be automatically tested properly.
+ IMGUI_DEBUG_LOG_FONT("[font] Texture #%03d: resize failed. Will grow.\n", new_tex->UniqueID);
+ new_tex->WantDestroyNextFrame = true;
+ builder->Rects.swap(old_rects);
+ builder->RectsIndex = old_index;
+ ImFontAtlasBuildSetTexture(atlas, old_tex);
+ ImFontAtlasTextureGrow(atlas, w, h); // Recurse
+ return;
+ }
+ IM_ASSERT(ImFontAtlasRectId_GetIndex(new_r_id) == builder->RectsIndex.index_from_ptr(&index_entry));
+ ImTextureRect* new_r = ImFontAtlasPackGetRect(atlas, new_r_id);
+ ImFontAtlasTextureBlockCopy(old_tex, old_r.x, old_r.y, new_tex, new_r->x, new_r->y, new_r->w, new_r->h);
+ }
+ IM_ASSERT(old_rects.Size == builder->Rects.Size + builder->RectsDiscardedCount);
+ builder->RectsDiscardedCount = 0;
+ builder->RectsDiscardedSurface = 0;
+
+ // Patch glyphs UV
+ for (int baked_n = 0; baked_n < builder->BakedPool.Size; baked_n++)
+ for (ImFontGlyph& glyph : builder->BakedPool[baked_n].Glyphs)
+ if (glyph.PackId != ImFontAtlasRectId_Invalid)
+ {
+ ImTextureRect* r = ImFontAtlasPackGetRect(atlas, glyph.PackId);
+ glyph.U0 = (r->x) * atlas->TexUvScale.x;
+ glyph.V0 = (r->y) * atlas->TexUvScale.y;
+ glyph.U1 = (r->x + r->w) * atlas->TexUvScale.x;
+ glyph.V1 = (r->y + r->h) * atlas->TexUvScale.y;
+ }
+
+ // Update other cached UV
+ ImFontAtlasBuildUpdateLinesTexData(atlas);
+ ImFontAtlasBuildUpdateBasicTexData(atlas);
+
+ builder->LockDisableResize = false;
+ ImFontAtlasUpdateDrawListsSharedData(atlas);
+ //ImFontAtlasDebugWriteTexToDisk(new_tex, "After Pack");
+}
+
+void ImFontAtlasTextureGrow(ImFontAtlas* atlas, int old_tex_w, int old_tex_h)
+{
+ //ImFontAtlasDebugWriteTexToDisk(atlas->TexData, "Before Grow");
+ ImFontAtlasBuilder* builder = atlas->Builder;
+ if (old_tex_w == -1)
+ old_tex_w = atlas->TexData->Width;
+ if (old_tex_h == -1)
+ old_tex_h = atlas->TexData->Height;
+
+ // FIXME-NEWATLAS-V2: What to do when reaching limits exposed by backend?
+ // FIXME-NEWATLAS-V2: Does ImFontAtlasFlags_NoPowerOfTwoHeight makes sense now? Allow 'lock' and 'compact' operations?
+ IM_ASSERT(ImIsPowerOfTwo(old_tex_w) && ImIsPowerOfTwo(old_tex_h));
+ IM_ASSERT(ImIsPowerOfTwo(atlas->TexMinWidth) && ImIsPowerOfTwo(atlas->TexMaxWidth) && ImIsPowerOfTwo(atlas->TexMinHeight) && ImIsPowerOfTwo(atlas->TexMaxHeight));
+
+ // Grow texture so it follows roughly a square.
+ // - Grow height before width, as width imply more packing nodes.
+ // - Caller should be taking account of RectsDiscardedSurface and may not need to grow.
+ int new_tex_w = (old_tex_h <= old_tex_w) ? old_tex_w : old_tex_w * 2;
+ int new_tex_h = (old_tex_h <= old_tex_w) ? old_tex_h * 2 : old_tex_h;
+
+ // Handle minimum size first (for pathologically large packed rects)
+ const int pack_padding = atlas->TexGlyphPadding;
+ new_tex_w = ImMax(new_tex_w, ImUpperPowerOfTwo(builder->MaxRectSize.x + pack_padding));
+ new_tex_h = ImMax(new_tex_h, ImUpperPowerOfTwo(builder->MaxRectSize.y + pack_padding));
+ new_tex_w = ImClamp(new_tex_w, atlas->TexMinWidth, atlas->TexMaxWidth);
+ new_tex_h = ImClamp(new_tex_h, atlas->TexMinHeight, atlas->TexMaxHeight);
+ if (new_tex_w == old_tex_w && new_tex_h == old_tex_h)
+ return;
+
+ ImFontAtlasTextureRepack(atlas, new_tex_w, new_tex_h);
+}
+
+void ImFontAtlasTextureMakeSpace(ImFontAtlas* atlas)
+{
+ // Can some baked contents be ditched?
+ //IMGUI_DEBUG_LOG_FONT("[font] ImFontAtlasBuildMakeSpace()\n");
+ ImFontAtlasBuilder* builder = atlas->Builder;
+ ImFontAtlasBuildDiscardBakes(atlas, 2);
+
+ // Currently using a heuristic for repack without growing.
+ if (builder->RectsDiscardedSurface < builder->RectsPackedSurface * 0.20f)
+ ImFontAtlasTextureGrow(atlas);
+ else
+ ImFontAtlasTextureRepack(atlas, atlas->TexData->Width, atlas->TexData->Height);
+}
+
+ImVec2i ImFontAtlasTextureGetSizeEstimate(ImFontAtlas* atlas)
+{
+ int min_w = ImUpperPowerOfTwo(atlas->TexMinWidth);
+ int min_h = ImUpperPowerOfTwo(atlas->TexMinHeight);
+ if (atlas->Builder == NULL || atlas->TexData == NULL || atlas->TexData->Status == ImTextureStatus_WantDestroy)
+ return ImVec2i(min_w, min_h);
+
+ ImFontAtlasBuilder* builder = atlas->Builder;
+ min_w = ImMax(ImUpperPowerOfTwo(builder->MaxRectSize.x), min_w);
+ min_h = ImMax(ImUpperPowerOfTwo(builder->MaxRectSize.y), min_h);
+ const int surface_approx = builder->RectsPackedSurface - builder->RectsDiscardedSurface; // Expected surface after repack
+ const int surface_sqrt = (int)sqrtf((float)surface_approx);
+
+ int new_tex_w;
+ int new_tex_h;
+ if (min_w >= min_h)
+ {
+ new_tex_w = ImMax(min_w, ImUpperPowerOfTwo(surface_sqrt));
+ new_tex_h = ImMax(min_h, (int)((surface_approx + new_tex_w - 1) / new_tex_w));
+ if ((atlas->Flags & ImFontAtlasFlags_NoPowerOfTwoHeight) == 0)
+ new_tex_h = ImUpperPowerOfTwo(new_tex_h);
+ }
+ else
+ {
+ new_tex_h = ImMax(min_h, ImUpperPowerOfTwo(surface_sqrt));
+ if ((atlas->Flags & ImFontAtlasFlags_NoPowerOfTwoHeight) == 0)
+ new_tex_h = ImUpperPowerOfTwo(new_tex_h);
+ new_tex_w = ImMax(min_w, (int)((surface_approx + new_tex_h - 1) / new_tex_h));
+ }
+
+ IM_ASSERT(ImIsPowerOfTwo(new_tex_w) && ImIsPowerOfTwo(new_tex_h));
+ return ImVec2i(new_tex_w, new_tex_h);
+}
+
+// Clear all output. Invalidates all AddCustomRect() return values!
+void ImFontAtlasBuildClear(ImFontAtlas* atlas)
+{
+ ImVec2i new_tex_size = ImFontAtlasTextureGetSizeEstimate(atlas);
+ ImFontAtlasBuildDestroy(atlas);
+ ImFontAtlasTextureAdd(atlas, new_tex_size.x, new_tex_size.y);
+ ImFontAtlasBuildInit(atlas);
+ for (ImFontConfig& src : atlas->Sources)
+ ImFontAtlasFontSourceInit(atlas, &src);
+ for (ImFont* font : atlas->Fonts)
+ for (ImFontConfig* src : font->Sources)
+ ImFontAtlasFontSourceAddToFont(atlas, font, src);
+}
+
+// You should not need to call this manually!
+// If you think you do, let us know and we can advise about policies auto-compact.
+void ImFontAtlasTextureCompact(ImFontAtlas* atlas)
+{
+ ImFontAtlasBuilder* builder = atlas->Builder;
+ ImFontAtlasBuildDiscardBakes(atlas, 1);
+
+ ImTextureData* old_tex = atlas->TexData;
+ ImVec2i old_tex_size = ImVec2i(old_tex->Width, old_tex->Height);
+ ImVec2i new_tex_size = ImFontAtlasTextureGetSizeEstimate(atlas);
+ if (builder->RectsDiscardedCount == 0 && new_tex_size.x == old_tex_size.x && new_tex_size.y == old_tex_size.y)
+ return;
+
+ ImFontAtlasTextureRepack(atlas, new_tex_size.x, new_tex_size.y);
+}
+
+// Start packing over current empty texture
void ImFontAtlasBuildInit(ImFontAtlas* atlas)
{
- // Register texture region for mouse cursors or standard white pixels
- if (atlas->PackIdMouseCursors < 0)
+ // Select Backend
+ // - Note that we do not reassign to atlas->FontLoader, since it is likely to point to static data which
+ // may mess with some hot-reloading schemes. If you need to assign to this (for dynamic selection) AND are
+ // using a hot-reloading scheme that messes up static data, store your own instance of FontLoader somewhere
+ // and point to it instead of pointing directly to return value of the GetFontLoaderXXX functions.
+ if (atlas->FontLoader == NULL)
{
- if (!(atlas->Flags & ImFontAtlasFlags_NoMouseCursors))
- atlas->PackIdMouseCursors = atlas->AddCustomRectRegular(FONT_ATLAS_DEFAULT_TEX_DATA_W * 2 + 1, FONT_ATLAS_DEFAULT_TEX_DATA_H);
- else
- atlas->PackIdMouseCursors = atlas->AddCustomRectRegular(2, 2);
+#ifdef IMGUI_ENABLE_FREETYPE
+ atlas->SetFontLoader(ImGuiFreeType::GetFontLoader());
+#elif defined(IMGUI_ENABLE_STB_TRUETYPE)
+ atlas->SetFontLoader(ImFontAtlasGetFontLoaderForStbTruetype());
+#else
+ IM_ASSERT(0); // Invalid Build function
+#endif
}
- // Register texture region for thick lines
- // The +2 here is to give space for the end caps, whilst height +1 is to accommodate the fact we have a zero-width row
- if (atlas->PackIdLines < 0)
- {
- if (!(atlas->Flags & ImFontAtlasFlags_NoBakedLines))
- atlas->PackIdLines = atlas->AddCustomRectRegular(IM_DRAWLIST_TEX_LINES_WIDTH_MAX + 2, IM_DRAWLIST_TEX_LINES_WIDTH_MAX + 1);
- }
+ // Create initial texture size
+ if (atlas->TexData == NULL || atlas->TexData->Pixels == NULL)
+ ImFontAtlasTextureAdd(atlas, ImUpperPowerOfTwo(atlas->TexMinWidth), ImUpperPowerOfTwo(atlas->TexMinHeight));
+
+ atlas->Builder = IM_NEW(ImFontAtlasBuilder)();
+ if (atlas->FontLoader->LoaderInit)
+ atlas->FontLoader->LoaderInit(atlas);
+
+ ImFontAtlasBuildUpdateRendererHasTexturesFromContext(atlas);
+
+ ImFontAtlasPackInit(atlas);
+
+ // Add required texture data
+ ImFontAtlasBuildUpdateLinesTexData(atlas);
+ ImFontAtlasBuildUpdateBasicTexData(atlas);
+
+ // Register fonts
+ ImFontAtlasBuildUpdatePointers(atlas);
+
+ // Update UV coordinates etc. stored in bound ImDrawListSharedData instance
+ ImFontAtlasUpdateDrawListsSharedData(atlas);
+
+ //atlas->TexIsBuilt = true;
}
-// This is called/shared by both the stb_truetype and the FreeType builder.
-void ImFontAtlasBuildFinish(ImFontAtlas* atlas)
+// Destroy builder and all cached glyphs. Do not destroy actual fonts.
+void ImFontAtlasBuildDestroy(ImFontAtlas* atlas)
{
- // Render into our custom data blocks
- IM_ASSERT(atlas->TexPixelsAlpha8 != NULL || atlas->TexPixelsRGBA32 != NULL);
- ImFontAtlasBuildRenderDefaultTexData(atlas);
- ImFontAtlasBuildRenderLinesTexData(atlas);
-
- // Register custom rectangle glyphs
- for (int i = 0; i < atlas->CustomRects.Size; i++)
+ for (ImFont* font : atlas->Fonts)
+ ImFontAtlasFontDestroyOutput(atlas, font);
+ if (atlas->Builder && atlas->FontLoader && atlas->FontLoader->LoaderShutdown)
{
- const ImFontAtlasCustomRect* r = &atlas->CustomRects[i];
- if (r->Font == NULL || r->GlyphID == 0)
- continue;
+ atlas->FontLoader->LoaderShutdown(atlas);
+ IM_ASSERT(atlas->FontLoaderData == NULL);
+ }
+ IM_DELETE(atlas->Builder);
+ atlas->Builder = NULL;
+}
- // Will ignore ImFontConfig settings: GlyphMinAdvanceX, GlyphMinAdvanceY, PixelSnapH
- IM_ASSERT(r->Font->ContainerAtlas == atlas);
- ImVec2 uv0, uv1;
- atlas->CalcCustomRectUV(r, &uv0, &uv1);
- r->Font->AddGlyph(NULL, (ImWchar)r->GlyphID, r->GlyphOffset.x, r->GlyphOffset.y, r->GlyphOffset.x + r->Width, r->GlyphOffset.y + r->Height, uv0.x, uv0.y, uv1.x, uv1.y, r->GlyphAdvanceX);
- if (r->GlyphColored)
- r->Font->Glyphs.back().Colored = 1;
+void ImFontAtlasPackInit(ImFontAtlas * atlas)
+{
+ ImTextureData* tex = atlas->TexData;
+ ImFontAtlasBuilder* builder = atlas->Builder;
+
+ // In theory we could decide to reduce the number of nodes, e.g. halve them, and waste a little texture space, but it doesn't seem worth it.
+ const int pack_node_count = tex->Width / 2;
+ builder->PackNodes.resize(pack_node_count);
+ IM_STATIC_ASSERT(sizeof(stbrp_context) <= sizeof(stbrp_context_opaque));
+ stbrp_init_target((stbrp_context*)(void*)&builder->PackContext, tex->Width, tex->Height, builder->PackNodes.Data, builder->PackNodes.Size);
+ builder->RectsPackedSurface = builder->RectsPackedCount = 0;
+ builder->MaxRectSize = ImVec2i(0, 0);
+ builder->MaxRectBounds = ImVec2i(0, 0);
+}
+
+// This is essentially a free-list pattern, it may be nice to wrap it into a dedicated type.
+static ImFontAtlasRectId ImFontAtlasPackAllocRectEntry(ImFontAtlas* atlas, int rect_idx)
+{
+ ImFontAtlasBuilder* builder = (ImFontAtlasBuilder*)atlas->Builder;
+ int index_idx;
+ ImFontAtlasRectEntry* index_entry;
+ if (builder->RectsIndexFreeListStart < 0)
+ {
+ builder->RectsIndex.resize(builder->RectsIndex.Size + 1);
+ index_idx = builder->RectsIndex.Size - 1;
+ index_entry = &builder->RectsIndex[index_idx];
+ memset(index_entry, 0, sizeof(*index_entry));
+ }
+ else
+ {
+ index_idx = builder->RectsIndexFreeListStart;
+ index_entry = &builder->RectsIndex[index_idx];
+ IM_ASSERT(index_entry->IsUsed == false && index_entry->Generation > 0); // Generation is incremented during DiscardRect
+ builder->RectsIndexFreeListStart = index_entry->TargetIndex;
+ }
+ index_entry->TargetIndex = rect_idx;
+ index_entry->IsUsed = 1;
+ return ImFontAtlasRectId_Make(index_idx, index_entry->Generation);
+}
+
+// Overwrite existing entry
+static ImFontAtlasRectId ImFontAtlasPackReuseRectEntry(ImFontAtlas* atlas, ImFontAtlasRectEntry* index_entry)
+{
+ IM_ASSERT(index_entry->IsUsed);
+ index_entry->TargetIndex = atlas->Builder->Rects.Size - 1;
+ int index_idx = atlas->Builder->RectsIndex.index_from_ptr(index_entry);
+ return ImFontAtlasRectId_Make(index_idx, index_entry->Generation);
+}
+
+// This is expected to be called in batches and followed by a repack
+void ImFontAtlasPackDiscardRect(ImFontAtlas* atlas, ImFontAtlasRectId id)
+{
+ IM_ASSERT(id != ImFontAtlasRectId_Invalid);
+
+ ImTextureRect* rect = ImFontAtlasPackGetRect(atlas, id);
+ if (rect == NULL)
+ return;
+
+ ImFontAtlasBuilder* builder = atlas->Builder;
+ int index_idx = ImFontAtlasRectId_GetIndex(id);
+ ImFontAtlasRectEntry* index_entry = &builder->RectsIndex[index_idx];
+ IM_ASSERT(index_entry->IsUsed && index_entry->TargetIndex >= 0);
+ index_entry->IsUsed = false;
+ index_entry->TargetIndex = builder->RectsIndexFreeListStart;
+ index_entry->Generation++;
+ if (index_entry->Generation == 0)
+ index_entry->Generation++; // Keep non-zero on overflow
+
+ const int pack_padding = atlas->TexGlyphPadding;
+ builder->RectsIndexFreeListStart = index_idx;
+ builder->RectsDiscardedCount++;
+ builder->RectsDiscardedSurface += (rect->w + pack_padding) * (rect->h + pack_padding);
+ rect->w = rect->h = 0; // Clear rectangle so it won't be packed again
+}
+
+// Important: Calling this may recreate a new texture and therefore change atlas->TexData
+// FIXME-NEWFONTS: Expose other glyph padding settings for custom alteration (e.g. drop shadows). See #7962
+ImFontAtlasRectId ImFontAtlasPackAddRect(ImFontAtlas* atlas, int w, int h, ImFontAtlasRectEntry* overwrite_entry)
+{
+ IM_ASSERT(w > 0 && w <= 0xFFFF);
+ IM_ASSERT(h > 0 && h <= 0xFFFF);
+
+ ImFontAtlasBuilder* builder = (ImFontAtlasBuilder*)atlas->Builder;
+ const int pack_padding = atlas->TexGlyphPadding;
+ builder->MaxRectSize.x = ImMax(builder->MaxRectSize.x, w);
+ builder->MaxRectSize.y = ImMax(builder->MaxRectSize.y, h);
+
+ // Pack
+ ImTextureRect r = { 0, 0, (unsigned short)w, (unsigned short)h };
+ for (int attempts_remaining = 3; attempts_remaining >= 0; attempts_remaining--)
+ {
+ // Try packing
+ stbrp_rect pack_r = {};
+ pack_r.w = w + pack_padding;
+ pack_r.h = h + pack_padding;
+ stbrp_pack_rects((stbrp_context*)(void*)&builder->PackContext, &pack_r, 1);
+ r.x = (unsigned short)pack_r.x;
+ r.y = (unsigned short)pack_r.y;
+ if (pack_r.was_packed)
+ break;
+
+ // If we ran out of attempts, return fallback
+ if (attempts_remaining == 0 || builder->LockDisableResize)
+ {
+ IMGUI_DEBUG_LOG_FONT("[font] Failed packing %dx%d rectangle. Returning fallback.\n", w, h);
+ return ImFontAtlasRectId_Invalid;
+ }
+
+ // Resize or repack atlas! (this should be a rare event)
+ ImFontAtlasTextureMakeSpace(atlas);
}
- // Build all fonts lookup tables
- for (ImFont* font : atlas->Fonts)
- if (font->DirtyLookupTables)
- font->BuildLookupTable();
+ builder->MaxRectBounds.x = ImMax(builder->MaxRectBounds.x, r.x + r.w + pack_padding);
+ builder->MaxRectBounds.y = ImMax(builder->MaxRectBounds.y, r.y + r.h + pack_padding);
+ builder->RectsPackedCount++;
+ builder->RectsPackedSurface += (w + pack_padding) * (h + pack_padding);
- atlas->TexReady = true;
+ builder->Rects.push_back(r);
+ if (overwrite_entry != NULL)
+ return ImFontAtlasPackReuseRectEntry(atlas, overwrite_entry); // Write into an existing entry instead of adding one (used during repack)
+ else
+ return ImFontAtlasPackAllocRectEntry(atlas, builder->Rects.Size - 1);
}
+// Generally for non-user facing functions: assert on invalid ID.
+ImTextureRect* ImFontAtlasPackGetRect(ImFontAtlas* atlas, ImFontAtlasRectId id)
+{
+ IM_ASSERT(id != ImFontAtlasRectId_Invalid);
+ int index_idx = ImFontAtlasRectId_GetIndex(id);
+ ImFontAtlasBuilder* builder = (ImFontAtlasBuilder*)atlas->Builder;
+ ImFontAtlasRectEntry* index_entry = &builder->RectsIndex[index_idx];
+ IM_ASSERT(index_entry->Generation == ImFontAtlasRectId_GetGeneration(id));
+ IM_ASSERT(index_entry->IsUsed);
+ return &builder->Rects[index_entry->TargetIndex];
+}
+
+// For user-facing functions: return NULL on invalid ID.
+// Important: return pointer is valid until next call to AddRect(), e.g. FindGlyph(), CalcTextSize() can all potentially invalidate previous pointers.
+ImTextureRect* ImFontAtlasPackGetRectSafe(ImFontAtlas* atlas, ImFontAtlasRectId id)
+{
+ if (id == ImFontAtlasRectId_Invalid)
+ return NULL;
+ int index_idx = ImFontAtlasRectId_GetIndex(id);
+ if (atlas->Builder == NULL)
+ ImFontAtlasBuildInit(atlas);
+ ImFontAtlasBuilder* builder = (ImFontAtlasBuilder*)atlas->Builder;
+ IM_MSVC_WARNING_SUPPRESS(28182); // Static Analysis false positive "warning C28182: Dereferencing NULL pointer 'builder'"
+ if (index_idx >= builder->RectsIndex.Size)
+ return NULL;
+ ImFontAtlasRectEntry* index_entry = &builder->RectsIndex[index_idx];
+ if (index_entry->Generation != ImFontAtlasRectId_GetGeneration(id) || !index_entry->IsUsed)
+ return NULL;
+ return &builder->Rects[index_entry->TargetIndex];
+}
+
+// Important! This assume by ImFontConfig::GlyphExcludeRanges[] is a SMALL ARRAY (e.g. <10 entries)
+// Use "Input Glyphs Overlap Detection Tool" to display a list of glyphs provided by multiple sources in order to set this array up.
+static bool ImFontAtlasBuildAcceptCodepointForSource(ImFontConfig* src, ImWchar codepoint)
+{
+ if (const ImWchar* exclude_list = src->GlyphExcludeRanges)
+ for (; exclude_list[0] != 0; exclude_list += 2)
+ if (codepoint >= exclude_list[0] && codepoint <= exclude_list[1])
+ return false;
+ return true;
+}
+
+static void ImFontBaked_BuildGrowIndex(ImFontBaked* baked, int new_size)
+{
+ IM_ASSERT(baked->IndexAdvanceX.Size == baked->IndexLookup.Size);
+ if (new_size <= baked->IndexLookup.Size)
+ return;
+ baked->IndexAdvanceX.resize(new_size, -1.0f);
+ baked->IndexLookup.resize(new_size, IM_FONTGLYPH_INDEX_UNUSED);
+}
+
+static void ImFontAtlas_FontHookRemapCodepoint(ImFontAtlas* atlas, ImFont* font, ImWchar* c)
+{
+ IM_UNUSED(atlas);
+ if (font->RemapPairs.Data.Size != 0)
+ *c = (ImWchar)font->RemapPairs.GetInt((ImGuiID)*c, (int)*c);
+}
+
+static ImFontGlyph* ImFontBaked_BuildLoadGlyph(ImFontBaked* baked, ImWchar codepoint, float* only_load_advance_x)
+{
+ ImFont* font = baked->ContainerFont;
+ ImFontAtlas* atlas = font->ContainerAtlas;
+ if (atlas->Locked || (font->Flags & ImFontFlags_NoLoadGlyphs))
+ {
+ // Lazily load fallback glyph
+ if (baked->FallbackGlyphIndex == -1 && baked->LoadNoFallback == 0)
+ ImFontAtlasBuildSetupFontBakedFallback(baked);
+ return NULL;
+ }
+
+ // User remapping hooks
+ ImWchar src_codepoint = codepoint;
+ ImFontAtlas_FontHookRemapCodepoint(atlas, font, &codepoint);
+
+ //char utf8_buf[5];
+ //IMGUI_DEBUG_LOG("[font] BuildLoadGlyph U+%04X (%s)\n", (unsigned int)codepoint, ImTextCharToUtf8(utf8_buf, (unsigned int)codepoint));
+
+ // Special hook
+ // FIXME-NEWATLAS: it would be nicer if this used a more standardized way of hooking
+ if (codepoint == font->EllipsisChar && font->EllipsisAutoBake)
+ if (ImFontGlyph* glyph = ImFontAtlasBuildSetupFontBakedEllipsis(atlas, baked))
+ return glyph;
+
+ // Call backend
+ char* loader_user_data_p = (char*)baked->FontLoaderDatas;
+ int src_n = 0;
+ for (ImFontConfig* src : font->Sources)
+ {
+ const ImFontLoader* loader = src->FontLoader ? src->FontLoader : atlas->FontLoader;
+ if (!src->GlyphExcludeRanges || ImFontAtlasBuildAcceptCodepointForSource(src, codepoint))
+ {
+ if (only_load_advance_x == NULL)
+ {
+ ImFontGlyph glyph_buf;
+ if (loader->FontBakedLoadGlyph(atlas, src, baked, loader_user_data_p, codepoint, &glyph_buf, NULL))
+ {
+ // FIXME: Add hooks for e.g. #7962
+ glyph_buf.Codepoint = src_codepoint;
+ glyph_buf.SourceIdx = src_n;
+ return ImFontAtlasBakedAddFontGlyph(atlas, baked, src, &glyph_buf);
+ }
+ }
+ else
+ {
+ // Special mode but only loading glyphs metrics. Will rasterize and pack later.
+ if (loader->FontBakedLoadGlyph(atlas, src, baked, loader_user_data_p, codepoint, NULL, only_load_advance_x))
+ {
+ ImFontAtlasBakedAddFontGlyphAdvancedX(atlas, baked, src, codepoint, *only_load_advance_x);
+ return NULL;
+ }
+ }
+ }
+ loader_user_data_p += loader->FontBakedSrcLoaderDataSize;
+ src_n++;
+ }
+
+ // Lazily load fallback glyph
+ if (baked->LoadNoFallback)
+ return NULL;
+ if (baked->FallbackGlyphIndex == -1)
+ ImFontAtlasBuildSetupFontBakedFallback(baked);
+
+ // Mark index as not found, so we don't attempt the search twice
+ ImFontBaked_BuildGrowIndex(baked, codepoint + 1);
+ baked->IndexAdvanceX[codepoint] = baked->FallbackAdvanceX;
+ baked->IndexLookup[codepoint] = IM_FONTGLYPH_INDEX_NOT_FOUND;
+ return NULL;
+}
+
+static float ImFontBaked_BuildLoadGlyphAdvanceX(ImFontBaked* baked, ImWchar codepoint)
+{
+ if (baked->Size >= IMGUI_FONT_SIZE_THRESHOLD_FOR_LOADADVANCEXONLYMODE || baked->LoadNoRenderOnLayout)
+ {
+ // First load AdvanceX value used by CalcTextSize() API then load the rest when loaded by drawing API.
+ float only_advance_x = 0.0f;
+ ImFontGlyph* glyph = ImFontBaked_BuildLoadGlyph(baked, (ImWchar)codepoint, &only_advance_x);
+ return glyph ? glyph->AdvanceX : only_advance_x;
+ }
+ else
+ {
+ ImFontGlyph* glyph = ImFontBaked_BuildLoadGlyph(baked, (ImWchar)codepoint, NULL);
+ return glyph ? glyph->AdvanceX : baked->FallbackAdvanceX;
+ }
+}
+
+// The point of this indirection is to not be inlined in debug mode in order to not bloat inner loop.b
+IM_MSVC_RUNTIME_CHECKS_OFF
+static float BuildLoadGlyphGetAdvanceOrFallback(ImFontBaked* baked, unsigned int codepoint)
+{
+ return ImFontBaked_BuildLoadGlyphAdvanceX(baked, (ImWchar)codepoint);
+}
+IM_MSVC_RUNTIME_CHECKS_RESTORE
+
+#ifndef IMGUI_DISABLE_DEBUG_TOOLS
+void ImFontAtlasDebugLogTextureRequests(ImFontAtlas* atlas)
+{
+ // [DEBUG] Log texture update requests
+ ImGuiContext& g = *GImGui;
+ IM_UNUSED(g);
+ for (ImTextureData* tex : atlas->TexList)
+ {
+ if ((g.IO.BackendFlags & ImGuiBackendFlags_RendererHasTextures) == 0)
+ IM_ASSERT(tex->Updates.Size == 0);
+ if (tex->Status == ImTextureStatus_WantCreate)
+ IMGUI_DEBUG_LOG_FONT("[font] Texture #%03d: create %dx%d\n", tex->UniqueID, tex->Width, tex->Height);
+ else if (tex->Status == ImTextureStatus_WantDestroy)
+ IMGUI_DEBUG_LOG_FONT("[font] Texture #%03d: destroy %dx%d, texid=0x%" IM_PRIX64 ", backend_data=%p\n", tex->UniqueID, tex->Width, tex->Height, IM_TEXTUREID_TO_U64(tex->TexID), tex->BackendUserData);
+ else if (tex->Status == ImTextureStatus_WantUpdates)
+ {
+ IMGUI_DEBUG_LOG_FONT("[font] Texture #%03d: update %d regions, texid=0x%" IM_PRIX64 ", backend_data=0x%" IM_PRIX64 "\n", tex->UniqueID, tex->Updates.Size, IM_TEXTUREID_TO_U64(tex->TexID), (ImU64)(intptr_t)tex->BackendUserData);
+ for (const ImTextureRect& r : tex->Updates)
+ {
+ IM_UNUSED(r);
+ IM_ASSERT(r.x >= 0 && r.y >= 0);
+ IM_ASSERT(r.x + r.w <= tex->Width && r.y + r.h <= tex->Height); // In theory should subtract PackPadding but it's currently part of atlas and mid-frame change would wreck assert.
+ //IMGUI_DEBUG_LOG_FONT("[font] Texture #%03d: update (% 4d..%-4d)->(% 4d..%-4d), texid=0x%" IM_PRIX64 ", backend_data=0x%" IM_PRIX64 "\n", tex->UniqueID, r.x, r.y, r.x + r.w, r.y + r.h, IM_TEXTUREID_TO_U64(tex->TexID), (ImU64)(intptr_t)tex->BackendUserData);
+ }
+ }
+ }
+}
+#endif
+
+//-------------------------------------------------------------------------
+// [SECTION] ImFontAtlas: backend for stb_truetype
+//-------------------------------------------------------------------------
+// (imstb_truetype.h in included near the top of this file, when IMGUI_ENABLE_STB_TRUETYPE is set)
+//-------------------------------------------------------------------------
+
+#ifdef IMGUI_ENABLE_STB_TRUETYPE
+
+// One for each ConfigData
+struct ImGui_ImplStbTrueType_FontSrcData
+{
+ stbtt_fontinfo FontInfo;
+ float ScaleFactor;
+};
+
+static bool ImGui_ImplStbTrueType_FontSrcInit(ImFontAtlas* atlas, ImFontConfig* src)
+{
+ IM_UNUSED(atlas);
+
+ ImGui_ImplStbTrueType_FontSrcData* bd_font_data = IM_NEW(ImGui_ImplStbTrueType_FontSrcData);
+ IM_ASSERT(src->FontLoaderData == NULL);
+
+ // Initialize helper structure for font loading and verify that the TTF/OTF data is correct
+ const int font_offset = stbtt_GetFontOffsetForIndex((unsigned char*)src->FontData, src->FontNo);
+ if (font_offset < 0)
+ {
+ IM_DELETE(bd_font_data);
+ IM_ASSERT_USER_ERROR(0, "stbtt_GetFontOffsetForIndex(): FontData is incorrect, or FontNo cannot be found.");
+ return false;
+ }
+ if (!stbtt_InitFont(&bd_font_data->FontInfo, (unsigned char*)src->FontData, font_offset))
+ {
+ IM_DELETE(bd_font_data);
+ IM_ASSERT_USER_ERROR(0, "stbtt_InitFont(): failed to parse FontData. It is correct and complete? Check FontDataSize.");
+ return false;
+ }
+ src->FontLoaderData = bd_font_data;
+
+ const float ref_size = src->DstFont->Sources[0]->SizePixels;
+ if (src->MergeMode && src->SizePixels == 0.0f)
+ src->SizePixels = ref_size;
+
+ if (src->SizePixels >= 0.0f)
+ bd_font_data->ScaleFactor = stbtt_ScaleForPixelHeight(&bd_font_data->FontInfo, 1.0f);
+ else
+ bd_font_data->ScaleFactor = stbtt_ScaleForMappingEmToPixels(&bd_font_data->FontInfo, 1.0f);
+ if (src->MergeMode && src->SizePixels != 0.0f && ref_size != 0.0f)
+ bd_font_data->ScaleFactor *= src->SizePixels / ref_size; // FIXME-NEWATLAS: Should tidy up that a bit
+
+ return true;
+}
+
+static void ImGui_ImplStbTrueType_FontSrcDestroy(ImFontAtlas* atlas, ImFontConfig* src)
+{
+ IM_UNUSED(atlas);
+ ImGui_ImplStbTrueType_FontSrcData* bd_font_data = (ImGui_ImplStbTrueType_FontSrcData*)src->FontLoaderData;
+ IM_DELETE(bd_font_data);
+ src->FontLoaderData = NULL;
+}
+
+static bool ImGui_ImplStbTrueType_FontSrcContainsGlyph(ImFontAtlas* atlas, ImFontConfig* src, ImWchar codepoint)
+{
+ IM_UNUSED(atlas);
+
+ ImGui_ImplStbTrueType_FontSrcData* bd_font_data = (ImGui_ImplStbTrueType_FontSrcData*)src->FontLoaderData;
+ IM_ASSERT(bd_font_data != NULL);
+
+ int glyph_index = stbtt_FindGlyphIndex(&bd_font_data->FontInfo, (int)codepoint);
+ return glyph_index != 0;
+}
+
+static bool ImGui_ImplStbTrueType_FontBakedInit(ImFontAtlas* atlas, ImFontConfig* src, ImFontBaked* baked, void*)
+{
+ IM_UNUSED(atlas);
+
+ ImGui_ImplStbTrueType_FontSrcData* bd_font_data = (ImGui_ImplStbTrueType_FontSrcData*)src->FontLoaderData;
+ if (src->MergeMode == false)
+ {
+ // FIXME-NEWFONTS: reevaluate how to use sizing metrics
+ // FIXME-NEWFONTS: make use of line gap value
+ float scale_for_layout = bd_font_data->ScaleFactor * baked->Size;
+ int unscaled_ascent, unscaled_descent, unscaled_line_gap;
+ stbtt_GetFontVMetrics(&bd_font_data->FontInfo, &unscaled_ascent, &unscaled_descent, &unscaled_line_gap);
+ baked->Ascent = ImCeil(unscaled_ascent * scale_for_layout);
+ baked->Descent = ImFloor(unscaled_descent * scale_for_layout);
+ }
+ return true;
+}
+
+static bool ImGui_ImplStbTrueType_FontBakedLoadGlyph(ImFontAtlas* atlas, ImFontConfig* src, ImFontBaked* baked, void*, ImWchar codepoint, ImFontGlyph* out_glyph, float* out_advance_x)
+{
+ // Search for first font which has the glyph
+ ImGui_ImplStbTrueType_FontSrcData* bd_font_data = (ImGui_ImplStbTrueType_FontSrcData*)src->FontLoaderData;
+ IM_ASSERT(bd_font_data);
+ int glyph_index = stbtt_FindGlyphIndex(&bd_font_data->FontInfo, (int)codepoint);
+ if (glyph_index == 0)
+ return false;
+
+ // Fonts unit to pixels
+ int oversample_h, oversample_v;
+ ImFontAtlasBuildGetOversampleFactors(src, baked, &oversample_h, &oversample_v);
+ const float scale_for_layout = bd_font_data->ScaleFactor * baked->Size;
+ const float rasterizer_density = src->RasterizerDensity * baked->RasterizerDensity;
+ const float scale_for_raster_x = bd_font_data->ScaleFactor * baked->Size * rasterizer_density * oversample_h;
+ const float scale_for_raster_y = bd_font_data->ScaleFactor * baked->Size * rasterizer_density * oversample_v;
+
+ // Obtain size and advance
+ int x0, y0, x1, y1;
+ int advance, lsb;
+ stbtt_GetGlyphBitmapBoxSubpixel(&bd_font_data->FontInfo, glyph_index, scale_for_raster_x, scale_for_raster_y, 0, 0, &x0, &y0, &x1, &y1);
+ stbtt_GetGlyphHMetrics(&bd_font_data->FontInfo, glyph_index, &advance, &lsb);
+
+ // Load metrics only mode
+ if (out_advance_x != NULL)
+ {
+ IM_ASSERT(out_glyph == NULL);
+ *out_advance_x = advance * scale_for_layout;
+ return true;
+ }
+
+ // Prepare glyph
+ out_glyph->Codepoint = codepoint;
+ out_glyph->AdvanceX = advance * scale_for_layout;
+
+ // Pack and retrieve position inside texture atlas
+ // (generally based on stbtt_PackFontRangesRenderIntoRects)
+ const bool is_visible = (x0 != x1 && y0 != y1);
+ if (is_visible)
+ {
+ const int w = (x1 - x0 + oversample_h - 1);
+ const int h = (y1 - y0 + oversample_v - 1);
+ ImFontAtlasRectId pack_id = ImFontAtlasPackAddRect(atlas, w, h);
+ if (pack_id == ImFontAtlasRectId_Invalid)
+ {
+ // Pathological out of memory case (TexMaxWidth/TexMaxHeight set too small?)
+ IM_ASSERT(pack_id != ImFontAtlasRectId_Invalid && "Out of texture memory.");
+ return false;
+ }
+ ImTextureRect* r = ImFontAtlasPackGetRect(atlas, pack_id);
+
+ // Render
+ stbtt_GetGlyphBitmapBox(&bd_font_data->FontInfo, glyph_index, scale_for_raster_x, scale_for_raster_y, &x0, &y0, &x1, &y1);
+ ImFontAtlasBuilder* builder = atlas->Builder;
+ builder->TempBuffer.resize(w * h * 1);
+ unsigned char* bitmap_pixels = builder->TempBuffer.Data;
+ memset(bitmap_pixels, 0, w * h * 1);
+
+ // Render with oversampling
+ // (those functions conveniently assert if pixels are not cleared, which is another safety layer)
+ float sub_x, sub_y;
+ stbtt_MakeGlyphBitmapSubpixelPrefilter(&bd_font_data->FontInfo, bitmap_pixels, w, h, w,
+ scale_for_raster_x, scale_for_raster_y, 0, 0, oversample_h, oversample_v, &sub_x, &sub_y, glyph_index);
+
+ const float ref_size = baked->ContainerFont->Sources[0]->SizePixels;
+ const float offsets_scale = (ref_size != 0.0f) ? (baked->Size / ref_size) : 1.0f;
+ float font_off_x = (src->GlyphOffset.x * offsets_scale);
+ float font_off_y = (src->GlyphOffset.y * offsets_scale);
+ if (src->PixelSnapH) // Snap scaled offset. This is to mitigate backward compatibility issues for GlyphOffset, but a better design would be welcome.
+ font_off_x = IM_ROUND(font_off_x);
+ if (src->PixelSnapV)
+ font_off_y = IM_ROUND(font_off_y);
+ font_off_x += sub_x;
+ font_off_y += sub_y + IM_ROUND(baked->Ascent);
+ float recip_h = 1.0f / (oversample_h * rasterizer_density);
+ float recip_v = 1.0f / (oversample_v * rasterizer_density);
+
+ // Register glyph
+ // r->x r->y are coordinates inside texture (in pixels)
+ // glyph.X0, glyph.Y0 are drawing coordinates from base text position, and accounting for oversampling.
+ out_glyph->X0 = x0 * recip_h + font_off_x;
+ out_glyph->Y0 = y0 * recip_v + font_off_y;
+ out_glyph->X1 = (x0 + (int)r->w) * recip_h + font_off_x;
+ out_glyph->Y1 = (y0 + (int)r->h) * recip_v + font_off_y;
+ out_glyph->Visible = true;
+ out_glyph->PackId = pack_id;
+ ImFontAtlasBakedSetFontGlyphBitmap(atlas, baked, src, out_glyph, r, bitmap_pixels, ImTextureFormat_Alpha8, w);
+ }
+
+ return true;
+}
+
+const ImFontLoader* ImFontAtlasGetFontLoaderForStbTruetype()
+{
+ static ImFontLoader loader;
+ loader.Name = "stb_truetype";
+ loader.FontSrcInit = ImGui_ImplStbTrueType_FontSrcInit;
+ loader.FontSrcDestroy = ImGui_ImplStbTrueType_FontSrcDestroy;
+ loader.FontSrcContainsGlyph = ImGui_ImplStbTrueType_FontSrcContainsGlyph;
+ loader.FontBakedInit = ImGui_ImplStbTrueType_FontBakedInit;
+ loader.FontBakedDestroy = NULL;
+ loader.FontBakedLoadGlyph = ImGui_ImplStbTrueType_FontBakedLoadGlyph;
+ return &loader;
+}
+
+#endif // IMGUI_ENABLE_STB_TRUETYPE
+
//-------------------------------------------------------------------------
// [SECTION] ImFontAtlas: glyph ranges helpers
//-------------------------------------------------------------------------
// - GetGlyphRangesDefault()
+// Obsolete functions since 1.92:
// - GetGlyphRangesGreek()
// - GetGlyphRangesKorean()
// - GetGlyphRangesChineseFull()
@@ -3405,6 +4768,7 @@ const ImWchar* ImFontAtlas::GetGlyphRangesDefault()
return &ranges[0];
}
+#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
const ImWchar* ImFontAtlas::GetGlyphRangesGreek()
{
static const ImWchar ranges[] =
@@ -3654,6 +5018,7 @@ const ImWchar* ImFontAtlas::GetGlyphRangesVietnamese()
};
return &ranges[0];
}
+#endif // #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
//-----------------------------------------------------------------------------
// [SECTION] ImFontGlyphRangesBuilder
@@ -3697,10 +5062,29 @@ void ImFontGlyphRangesBuilder::BuildRanges(ImVector* out_ranges)
// [SECTION] ImFont
//-----------------------------------------------------------------------------
+ImFontBaked::ImFontBaked()
+{
+ memset(this, 0, sizeof(*this));
+ FallbackGlyphIndex = -1;
+}
+
+void ImFontBaked::ClearOutputData()
+{
+ FallbackAdvanceX = 0.0f;
+ Glyphs.clear();
+ IndexAdvanceX.clear();
+ IndexLookup.clear();
+ FallbackGlyphIndex = -1;
+ Ascent = Descent = 0.0f;
+ MetricsTotalSurface = 0;
+}
+
ImFont::ImFont()
{
memset(this, 0, sizeof(*this));
+#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
Scale = 1.0f;
+#endif
}
ImFont::~ImFont()
@@ -3708,113 +5092,13 @@ ImFont::~ImFont()
ClearOutputData();
}
-void ImFont::ClearOutputData()
+void ImFont::ClearOutputData()
{
- FontSize = 0.0f;
- FallbackAdvanceX = 0.0f;
- Glyphs.clear();
- IndexAdvanceX.clear();
- IndexLookup.clear();
- FallbackGlyph = NULL;
- ContainerAtlas = NULL;
- DirtyLookupTables = true;
- Ascent = Descent = 0.0f;
- MetricsTotalSurface = 0;
+ if (ImFontAtlas* atlas = ContainerAtlas)
+ ImFontAtlasFontDiscardBakes(atlas, this, 0);
+ FallbackChar = EllipsisChar = 0;
memset(Used8kPagesMap, 0, sizeof(Used8kPagesMap));
-}
-
-static ImWchar FindFirstExistingGlyph(ImFont* font, const ImWchar* candidate_chars, int candidate_chars_count)
-{
- for (int n = 0; n < candidate_chars_count; n++)
- if (font->FindGlyphNoFallback(candidate_chars[n]) != NULL)
- return candidate_chars[n];
- return 0;
-}
-
-void ImFont::BuildLookupTable()
-{
- int max_codepoint = 0;
- for (int i = 0; i != Glyphs.Size; i++)
- max_codepoint = ImMax(max_codepoint, (int)Glyphs[i].Codepoint);
-
- // Build lookup table
- IM_ASSERT(Glyphs.Size > 0 && "Font has not loaded glyph!");
- IM_ASSERT(Glyphs.Size < 0xFFFF); // -1 is reserved
- IndexAdvanceX.clear();
- IndexLookup.clear();
- DirtyLookupTables = false;
- memset(Used8kPagesMap, 0, sizeof(Used8kPagesMap));
- GrowIndex(max_codepoint + 1);
- for (int i = 0; i < Glyphs.Size; i++)
- {
- int codepoint = (int)Glyphs[i].Codepoint;
- IndexAdvanceX[codepoint] = Glyphs[i].AdvanceX;
- IndexLookup[codepoint] = (ImU16)i;
-
- // Mark 4K page as used
- const int page_n = codepoint / 8192;
- Used8kPagesMap[page_n >> 3] |= 1 << (page_n & 7);
- }
-
- // Create a glyph to handle TAB
- // FIXME: Needs proper TAB handling but it needs to be contextualized (or we could arbitrary say that each string starts at "column 0" ?)
- if (FindGlyph((ImWchar)' '))
- {
- if (Glyphs.back().Codepoint != '\t') // So we can call this function multiple times (FIXME: Flaky)
- Glyphs.resize(Glyphs.Size + 1);
- ImFontGlyph& tab_glyph = Glyphs.back();
- tab_glyph = *FindGlyph((ImWchar)' ');
- tab_glyph.Codepoint = '\t';
- tab_glyph.AdvanceX *= IM_TABSIZE;
- IndexAdvanceX[(int)tab_glyph.Codepoint] = (float)tab_glyph.AdvanceX;
- IndexLookup[(int)tab_glyph.Codepoint] = (ImU16)(Glyphs.Size - 1);
- }
-
- // Mark special glyphs as not visible (note that AddGlyph already mark as non-visible glyphs with zero-size polygons)
- if (ImFontGlyph* glyph = (ImFontGlyph*)(void*)FindGlyph((ImWchar)' '))
- glyph->Visible = false;
- if (ImFontGlyph* glyph = (ImFontGlyph*)(void*)FindGlyph((ImWchar)'\t'))
- glyph->Visible = false;
-
- // Setup Fallback character
- const ImWchar fallback_chars[] = { (ImWchar)IM_UNICODE_CODEPOINT_INVALID, (ImWchar)'?', (ImWchar)' ' };
- FallbackGlyph = FindGlyphNoFallback(FallbackChar);
- if (FallbackGlyph == NULL)
- {
- FallbackChar = FindFirstExistingGlyph(this, fallback_chars, IM_ARRAYSIZE(fallback_chars));
- FallbackGlyph = FindGlyphNoFallback(FallbackChar);
- if (FallbackGlyph == NULL)
- {
- FallbackGlyph = &Glyphs.back();
- FallbackChar = (ImWchar)FallbackGlyph->Codepoint;
- }
- }
- FallbackAdvanceX = FallbackGlyph->AdvanceX;
- for (int i = 0; i < max_codepoint + 1; i++)
- if (IndexAdvanceX[i] < 0.0f)
- IndexAdvanceX[i] = FallbackAdvanceX;
-
- // Setup Ellipsis character. It is required for rendering elided text. We prefer using U+2026 (horizontal ellipsis).
- // However some old fonts may contain ellipsis at U+0085. Here we auto-detect most suitable ellipsis character.
- // FIXME: Note that 0x2026 is rarely included in our font ranges. Because of this we are more likely to use three individual dots.
- const ImWchar ellipsis_chars[] = { Sources->EllipsisChar, (ImWchar)0x2026, (ImWchar)0x0085 };
- const ImWchar dots_chars[] = { (ImWchar)'.', (ImWchar)0xFF0E };
- if (EllipsisChar == 0)
- EllipsisChar = FindFirstExistingGlyph(this, ellipsis_chars, IM_ARRAYSIZE(ellipsis_chars));
- const ImWchar dot_char = FindFirstExistingGlyph(this, dots_chars, IM_ARRAYSIZE(dots_chars));
- if (EllipsisChar != 0)
- {
- EllipsisCharCount = 1;
- EllipsisWidth = EllipsisCharStep = FindGlyph(EllipsisChar)->X1;
- }
- else if (dot_char != 0)
- {
- const ImFontGlyph* dot_glyph = FindGlyph(dot_char);
- EllipsisChar = dot_char;
- EllipsisCharCount = 3;
- EllipsisCharStep = (float)(int)(dot_glyph->X1 - dot_glyph->X0) + 1.0f;
- EllipsisWidth = ImMax(dot_glyph->AdvanceX, dot_glyph->X0 + EllipsisCharStep * 3.0f - 1.0f); // FIXME: Slightly odd for normally mono-space fonts but since this is used for trailing contents.
- }
+ LastBaked = NULL;
}
// API is designed this way to avoid exposing the 8K page size
@@ -3830,116 +5114,258 @@ bool ImFont::IsGlyphRangeUnused(unsigned int c_begin, unsigned int c_last)
return true;
}
-void ImFont::GrowIndex(int new_size)
-{
- IM_ASSERT(IndexAdvanceX.Size == IndexLookup.Size);
- if (new_size <= IndexLookup.Size)
- return;
- IndexAdvanceX.resize(new_size, -1.0f);
- IndexLookup.resize(new_size, (ImU16)-1);
-}
-
// x0/y0/x1/y1 are offset from the character upper-left layout position, in pixels. Therefore x0/y0 are often fairly close to zero.
// Not to be mistaken with texture coordinates, which are held by u0/v0/u1/v1 in normalized format (0.0..1.0 on each texture axis).
-// 'src' is not necessarily == 'this->Sources' because multiple source fonts+configs can be used to build one target font.
-void ImFont::AddGlyph(const ImFontConfig* src, ImWchar codepoint, float x0, float y0, float x1, float y1, float u0, float v0, float u1, float v1, float advance_x)
+// - 'src' is not necessarily == 'this->Sources' because multiple source fonts+configs can be used to build one target font.
+ImFontGlyph* ImFontAtlasBakedAddFontGlyph(ImFontAtlas* atlas, ImFontBaked* baked, ImFontConfig* src, const ImFontGlyph* in_glyph)
{
+ int glyph_idx = baked->Glyphs.Size;
+ baked->Glyphs.push_back(*in_glyph);
+ ImFontGlyph* glyph = &baked->Glyphs[glyph_idx];
+ IM_ASSERT(baked->Glyphs.Size < 0xFFFE); // IndexLookup[] hold 16-bit values and -1/-2 are reserved.
+
+ // Set UV from packed rectangle
+ if (glyph->PackId != ImFontAtlasRectId_Invalid)
+ {
+ ImTextureRect* r = ImFontAtlasPackGetRect(atlas, glyph->PackId);
+ IM_ASSERT(glyph->U0 == 0.0f && glyph->V0 == 0.0f && glyph->U1 == 0.0f && glyph->V1 == 0.0f);
+ glyph->U0 = (r->x) * atlas->TexUvScale.x;
+ glyph->V0 = (r->y) * atlas->TexUvScale.y;
+ glyph->U1 = (r->x + r->w) * atlas->TexUvScale.x;
+ glyph->V1 = (r->y + r->h) * atlas->TexUvScale.y;
+ baked->MetricsTotalSurface += r->w * r->h;
+ }
+
if (src != NULL)
{
// Clamp & recenter if needed
- const float advance_x_original = advance_x;
- advance_x = ImClamp(advance_x, src->GlyphMinAdvanceX, src->GlyphMaxAdvanceX);
- if (advance_x != advance_x_original)
+ const float ref_size = baked->ContainerFont->Sources[0]->SizePixels;
+ const float offsets_scale = (ref_size != 0.0f) ? (baked->Size / ref_size) : 1.0f;
+ float advance_x = ImClamp(glyph->AdvanceX, src->GlyphMinAdvanceX * offsets_scale, src->GlyphMaxAdvanceX * offsets_scale);
+ if (advance_x != glyph->AdvanceX)
{
- float char_off_x = src->PixelSnapH ? ImTrunc((advance_x - advance_x_original) * 0.5f) : (advance_x - advance_x_original) * 0.5f;
- x0 += char_off_x;
- x1 += char_off_x;
+ float char_off_x = src->PixelSnapH ? ImTrunc((advance_x - glyph->AdvanceX) * 0.5f) : (advance_x - glyph->AdvanceX) * 0.5f;
+ glyph->X0 += char_off_x;
+ glyph->X1 += char_off_x;
}
// Snap to pixel
if (src->PixelSnapH)
advance_x = IM_ROUND(advance_x);
- // Bake extra spacing
+ // Bake spacing
+ glyph->AdvanceX = advance_x + src->GlyphExtraAdvanceX;
+ }
+ if (glyph->Colored)
+ atlas->TexPixelsUseColors = atlas->TexData->UseColors = true;
+
+ // Update lookup tables
+ const int codepoint = glyph->Codepoint;
+ ImFontBaked_BuildGrowIndex(baked, codepoint + 1);
+ baked->IndexAdvanceX[codepoint] = glyph->AdvanceX;
+ baked->IndexLookup[codepoint] = (ImU16)glyph_idx;
+ const int page_n = codepoint / 8192;
+ baked->ContainerFont->Used8kPagesMap[page_n >> 3] |= 1 << (page_n & 7);
+
+ return glyph;
+}
+
+// FIXME: Code is duplicated with code above.
+void ImFontAtlasBakedAddFontGlyphAdvancedX(ImFontAtlas* atlas, ImFontBaked* baked, ImFontConfig* src, ImWchar codepoint, float advance_x)
+{
+ IM_UNUSED(atlas);
+ if (src != NULL)
+ {
+ // Clamp & recenter if needed
+ const float ref_size = baked->ContainerFont->Sources[0]->SizePixels;
+ const float offsets_scale = (ref_size != 0.0f) ? (baked->Size / ref_size) : 1.0f;
+ advance_x = ImClamp(advance_x, src->GlyphMinAdvanceX * offsets_scale, src->GlyphMaxAdvanceX * offsets_scale);
+
+ // Snap to pixel
+ if (src->PixelSnapH)
+ advance_x = IM_ROUND(advance_x);
+
+ // Bake spacing
advance_x += src->GlyphExtraAdvanceX;
}
- int glyph_idx = Glyphs.Size;
- Glyphs.resize(Glyphs.Size + 1);
- ImFontGlyph& glyph = Glyphs[glyph_idx];
- glyph.Codepoint = (unsigned int)codepoint;
- glyph.Visible = (x0 != x1) && (y0 != y1);
- glyph.Colored = false;
- glyph.X0 = x0;
- glyph.Y0 = y0;
- glyph.X1 = x1;
- glyph.Y1 = y1;
- glyph.U0 = u0;
- glyph.V0 = v0;
- glyph.U1 = u1;
- glyph.V1 = v1;
- glyph.AdvanceX = advance_x;
- IM_ASSERT(Glyphs.Size < 0xFFFF); // IndexLookup[] hold 16-bit values and -1 is reserved.
-
- // Compute rough surface usage metrics (+1 to account for average padding, +0.99 to round)
- // We use (U1-U0)*TexWidth instead of X1-X0 to account for oversampling.
- float pad = ContainerAtlas->TexGlyphPadding + 0.99f;
- DirtyLookupTables = true;
- MetricsTotalSurface += (int)((glyph.U1 - glyph.U0) * ContainerAtlas->TexWidth + pad) * (int)((glyph.V1 - glyph.V0) * ContainerAtlas->TexHeight + pad);
+ ImFontBaked_BuildGrowIndex(baked, codepoint + 1);
+ baked->IndexAdvanceX[codepoint] = advance_x;
}
-void ImFont::AddRemapChar(ImWchar dst, ImWchar src, bool overwrite_dst)
+// Copy to texture, post-process and queue update for backend
+void ImFontAtlasBakedSetFontGlyphBitmap(ImFontAtlas* atlas, ImFontBaked* baked, ImFontConfig* src, ImFontGlyph* glyph, ImTextureRect* r, const unsigned char* src_pixels, ImTextureFormat src_fmt, int src_pitch)
{
- IM_ASSERT(IndexLookup.Size > 0); // Currently this can only be called AFTER the font has been built, aka after calling ImFontAtlas::GetTexDataAs*() function.
- unsigned int index_size = (unsigned int)IndexLookup.Size;
-
- if (dst < index_size && IndexLookup.Data[dst] == (ImU16)-1 && !overwrite_dst) // 'dst' already exists
- return;
- if (src >= index_size && dst >= index_size) // both 'dst' and 'src' don't exist -> no-op
- return;
-
- GrowIndex(dst + 1);
- IndexLookup[dst] = (src < index_size) ? IndexLookup.Data[src] : (ImU16)-1;
- IndexAdvanceX[dst] = (src < index_size) ? IndexAdvanceX.Data[src] : 1.0f;
+ ImTextureData* tex = atlas->TexData;
+ IM_ASSERT(r->x + r->w <= tex->Width && r->y + r->h <= tex->Height);
+ ImFontAtlasTextureBlockConvert(src_pixels, src_fmt, src_pitch, (unsigned char*)tex->GetPixelsAt(r->x, r->y), tex->Format, tex->GetPitch(), r->w, r->h);
+ ImFontAtlasPostProcessData pp_data = { atlas, baked->ContainerFont, src, baked, glyph, tex->GetPixelsAt(r->x, r->y), tex->Format, tex->GetPitch(), r->w, r->h };
+ ImFontAtlasTextureBlockPostProcess(&pp_data);
+ ImFontAtlasTextureBlockQueueUpload(atlas, tex, r->x, r->y, r->w, r->h);
}
-// Find glyph, return fallback if missing
-ImFontGlyph* ImFont::FindGlyph(ImWchar c)
+void ImFont::AddRemapChar(ImWchar from_codepoint, ImWchar to_codepoint)
{
- if (c >= (size_t)IndexLookup.Size)
- return FallbackGlyph;
- const ImU16 i = IndexLookup.Data[c];
- if (i == (ImU16)-1)
- return FallbackGlyph;
- return &Glyphs.Data[i];
+ RemapPairs.SetInt((ImGuiID)from_codepoint, (int)to_codepoint);
}
-ImFontGlyph* ImFont::FindGlyphNoFallback(ImWchar c)
+// Find glyph, load if necessary, return fallback if missing
+ImFontGlyph* ImFontBaked::FindGlyph(ImWchar c)
{
- if (c >= (size_t)IndexLookup.Size)
+ if (c < (size_t)IndexLookup.Size) IM_LIKELY
+ {
+ const int i = (int)IndexLookup.Data[c];
+ if (i == IM_FONTGLYPH_INDEX_NOT_FOUND)
+ return &Glyphs.Data[FallbackGlyphIndex];
+ if (i != IM_FONTGLYPH_INDEX_UNUSED)
+ return &Glyphs.Data[i];
+ }
+ ImFontGlyph* glyph = ImFontBaked_BuildLoadGlyph(this, c, NULL);
+ return glyph ? glyph : &Glyphs.Data[FallbackGlyphIndex];
+}
+
+// Attempt to load but when missing, return NULL instead of FallbackGlyph
+ImFontGlyph* ImFontBaked::FindGlyphNoFallback(ImWchar c)
+{
+ if (c < (size_t)IndexLookup.Size) IM_LIKELY
+ {
+ const int i = (int)IndexLookup.Data[c];
+ if (i == IM_FONTGLYPH_INDEX_NOT_FOUND)
+ return NULL;
+ if (i != IM_FONTGLYPH_INDEX_UNUSED)
+ return &Glyphs.Data[i];
+ }
+ LoadNoFallback = true; // This is actually a rare call, not done in hot-loop, so we prioritize not adding extra cruft to ImFontBaked_BuildLoadGlyph() call sites.
+ ImFontGlyph* glyph = ImFontBaked_BuildLoadGlyph(this, c, NULL);
+ LoadNoFallback = false;
+ return glyph;
+}
+
+bool ImFontBaked::IsGlyphLoaded(ImWchar c)
+{
+ if (c < (size_t)IndexLookup.Size) IM_LIKELY
+ {
+ const int i = (int)IndexLookup.Data[c];
+ if (i == IM_FONTGLYPH_INDEX_NOT_FOUND)
+ return false;
+ if (i != IM_FONTGLYPH_INDEX_UNUSED)
+ return true;
+ }
+ return false;
+}
+
+// This is not fast query
+bool ImFont::IsGlyphInFont(ImWchar c)
+{
+ ImFontAtlas* atlas = ContainerAtlas;
+ ImFontAtlas_FontHookRemapCodepoint(atlas, this, &c);
+ for (ImFontConfig* src : Sources)
+ {
+ const ImFontLoader* loader = src->FontLoader ? src->FontLoader : atlas->FontLoader;
+ if (loader->FontSrcContainsGlyph != NULL && loader->FontSrcContainsGlyph(atlas, src, c))
+ return true;
+ }
+ return false;
+}
+
+// This is manually inlined in CalcTextSizeA() and CalcWordWrapPosition(), with a non-inline call to BuildLoadGlyphGetAdvanceOrFallback().
+IM_MSVC_RUNTIME_CHECKS_OFF
+float ImFontBaked::GetCharAdvance(ImWchar c)
+{
+ if ((int)c < IndexAdvanceX.Size)
+ {
+ // Missing glyphs fitting inside index will have stored FallbackAdvanceX already.
+ const float x = IndexAdvanceX.Data[c];
+ if (x >= 0.0f)
+ return x;
+ }
+ return ImFontBaked_BuildLoadGlyphAdvanceX(this, c);
+}
+IM_MSVC_RUNTIME_CHECKS_RESTORE
+
+ImGuiID ImFontAtlasBakedGetId(ImGuiID font_id, float baked_size, float rasterizer_density)
+{
+ struct { ImGuiID FontId; float BakedSize; float RasterizerDensity; } hashed_data;
+ hashed_data.FontId = font_id;
+ hashed_data.BakedSize = baked_size;
+ hashed_data.RasterizerDensity = rasterizer_density;
+ return ImHashData(&hashed_data, sizeof(hashed_data));
+}
+
+// ImFontBaked pointers are valid for the entire frame but shall never be kept between frames.
+ImFontBaked* ImFont::GetFontBaked(float size, float density)
+{
+ ImFontBaked* baked = LastBaked;
+
+ // Round font size
+ // - ImGui::PushFont() will already round, but other paths calling GetFontBaked() directly also needs it (e.g. ImFontAtlasBuildPreloadAllGlyphRanges)
+ size = ImGui::GetRoundedFontSize(size);
+
+ if (density < 0.0f)
+ density = CurrentRasterizerDensity;
+ if (baked && baked->Size == size && baked->RasterizerDensity == density)
+ return baked;
+
+ ImFontAtlas* atlas = ContainerAtlas;
+ ImFontAtlasBuilder* builder = atlas->Builder;
+ baked = ImFontAtlasBakedGetOrAdd(atlas, this, size, density);
+ if (baked == NULL)
return NULL;
- const ImU16 i = IndexLookup.Data[c];
- if (i == (ImU16)-1)
- return NULL;
- return &Glyphs.Data[i];
+ baked->LastUsedFrame = builder->FrameCount;
+ LastBaked = baked;
+ return baked;
+}
+
+ImFontBaked* ImFontAtlasBakedGetOrAdd(ImFontAtlas* atlas, ImFont* font, float font_size, float font_rasterizer_density)
+{
+ // FIXME-NEWATLAS: Design for picking a nearest size based on some criteria?
+ // FIXME-NEWATLAS: Altering font density won't work right away.
+ IM_ASSERT(font_size > 0.0f && font_rasterizer_density > 0.0f);
+ ImGuiID baked_id = ImFontAtlasBakedGetId(font->FontId, font_size, font_rasterizer_density);
+ ImFontAtlasBuilder* builder = atlas->Builder;
+ ImFontBaked** p_baked_in_map = (ImFontBaked**)builder->BakedMap.GetVoidPtrRef(baked_id);
+ ImFontBaked* baked = *p_baked_in_map;
+ if (baked != NULL)
+ {
+ IM_ASSERT(baked->Size == font_size && baked->ContainerFont == font && baked->BakedId == baked_id);
+ return baked;
+ }
+
+ // If atlas is locked, find closest match
+ // FIXME-OPT: This is not an optimal query.
+ if ((font->Flags & ImFontFlags_LockBakedSizes) || atlas->Locked)
+ {
+ baked = ImFontAtlasBakedGetClosestMatch(atlas, font, font_size, font_rasterizer_density);
+ if (baked != NULL)
+ return baked;
+ if (atlas->Locked)
+ {
+ IM_ASSERT(!atlas->Locked && "Cannot use dynamic font size with a locked ImFontAtlas!"); // Locked because rendering backend does not support ImGuiBackendFlags_RendererHasTextures!
+ return NULL;
+ }
+ }
+
+ // Create new
+ baked = ImFontAtlasBakedAdd(atlas, font, font_size, font_rasterizer_density, baked_id);
+ *p_baked_in_map = baked; // To avoid 'builder->BakedMap.SetVoidPtr(baked_id, baked);' while we can.
+ return baked;
}
// Trim trailing space and find beginning of next line
-static inline const char* CalcWordWrapNextLineStartA(const char* text, const char* text_end)
+const char* ImTextCalcWordWrapNextLineStart(const char* text, const char* text_end, ImDrawTextFlags flags)
{
- while (text < text_end && ImCharIsBlankA(*text))
- text++;
+ if ((flags & ImDrawTextFlags_WrapKeepBlanks) == 0)
+ while (text < text_end && ImCharIsBlankA(*text))
+ text++;
if (*text == '\n')
text++;
return text;
}
-#define ImFontGetCharAdvanceX(_FONT, _CH) ((int)(_CH) < (_FONT)->IndexAdvanceX.Size ? (_FONT)->IndexAdvanceX.Data[_CH] : (_FONT)->FallbackAdvanceX)
-
// Simple word-wrapping for English, not full-featured. Please submit failing cases!
// This will return the next location to wrap from. If no wrapping if necessary, this will fast-forward to e.g. text_end.
// FIXME: Much possible improvements (don't cut things like "word !", "word!!!" but cut within "word,,,,", more sensible support for punctuations, support for Unicode punctuations, etc.)
-const char* ImFont::CalcWordWrapPositionA(float scale, const char* text, const char* text_end, float wrap_width)
+const char* ImFontCalcWordWrapPositionEx(ImFont* font, float size, const char* text, const char* text_end, float wrap_width, ImDrawTextFlags flags)
{
// For references, possible wrap point marked with ^
// "aaa bbb, ccc,ddd. eee fff. ggg!"
@@ -3952,6 +5378,10 @@ const char* ImFont::CalcWordWrapPositionA(float scale, const char* text, const c
// Cut words that cannot possibly fit within one line.
// e.g.: "The tropical fish" with ~5 characters worth of width --> "The tr" "opical" "fish"
+
+ ImFontBaked* baked = font->GetFontBaked(size);
+ const float scale = size / baked->Size;
+
float line_width = 0.0f;
float word_width = 0.0f;
float blank_width = 0.0f;
@@ -3975,12 +5405,7 @@ const char* ImFont::CalcWordWrapPositionA(float scale, const char* text, const c
if (c < 32)
{
if (c == '\n')
- {
- line_width = word_width = blank_width = 0.0f;
- inside_word = true;
- s = next_s;
- continue;
- }
+ return s; // Direct return, skip "Wrap_width is too small to fit anything" path.
if (c == '\r')
{
s = next_s;
@@ -3988,7 +5413,11 @@ const char* ImFont::CalcWordWrapPositionA(float scale, const char* text, const c
}
}
- const float char_width = ImFontGetCharAdvanceX(this, c);
+ // Optimized inline version of 'float char_width = GetCharAdvance((ImWchar)c);'
+ float char_width = (c < (unsigned int)baked->IndexAdvanceX.Size) ? baked->IndexAdvanceX.Data[c] : -1.0f;
+ if (char_width < 0.0f)
+ char_width = BuildLoadGlyphGetAdvanceOrFallback(baked, c);
+
if (ImCharIsBlankW(c))
{
if (inside_word)
@@ -4011,11 +5440,13 @@ const char* ImFont::CalcWordWrapPositionA(float scale, const char* text, const c
{
prev_word_end = word_end;
line_width += word_width + blank_width;
+ if ((flags & ImDrawTextFlags_WrapKeepBlanks) && line_width <= wrap_width)
+ prev_word_end = s;
word_width = blank_width = 0.0f;
}
// Allow wrapping after punctuation.
- inside_word = (c != '.' && c != ',' && c != ';' && c != '!' && c != '?' && c != '\"');
+ inside_word = (c != '.' && c != ',' && c != ';' && c != '!' && c != '?' && c != '\"' && c != 0x3001 && c != 0x3002);
}
// We ignore blank width at the end of the line (they can be skipped)
@@ -4033,17 +5464,25 @@ const char* ImFont::CalcWordWrapPositionA(float scale, const char* text, const c
// Wrap_width is too small to fit anything. Force displaying 1 character to minimize the height discontinuity.
// +1 may not be a character start point in UTF-8 but it's ok because caller loops use (text >= word_wrap_eol).
if (s == text && text < text_end)
- return s + 1;
+ return s + ImTextCountUtf8BytesFromChar(s, text_end);
return s;
}
-ImVec2 ImFont::CalcTextSizeA(float size, float max_width, float wrap_width, const char* text_begin, const char* text_end, const char** remaining)
+const char* ImFont::CalcWordWrapPosition(float size, const char* text, const char* text_end, float wrap_width)
+{
+ return ImFontCalcWordWrapPositionEx(this, size, text, text_end, wrap_width, ImDrawTextFlags_None);
+}
+
+ImVec2 ImFontCalcTextSizeEx(ImFont* font, float size, float max_width, float wrap_width, const char* text_begin, const char* text_end_display, const char* text_end, const char** out_remaining, ImVec2* out_offset, ImDrawTextFlags flags)
{
if (!text_end)
text_end = text_begin + ImStrlen(text_begin); // FIXME-OPT: Need to avoid this.
+ if (!text_end_display)
+ text_end_display = text_end;
+ ImFontBaked* baked = font->GetFontBaked(size);
const float line_height = size;
- const float scale = size / FontSize;
+ const float scale = line_height / baked->Size;
ImVec2 text_size = ImVec2(0, 0);
float line_width = 0.0f;
@@ -4052,13 +5491,14 @@ ImVec2 ImFont::CalcTextSizeA(float size, float max_width, float wrap_width, cons
const char* word_wrap_eol = NULL;
const char* s = text_begin;
- while (s < text_end)
+ while (s < text_end_display)
{
+ // Word-wrapping
if (word_wrap_enabled)
{
// Calculate how far we can render. Requires two passes on the string data but keeps the code simple and not intrusive for what's essentially an uncommon feature.
if (!word_wrap_eol)
- word_wrap_eol = CalcWordWrapPositionA(scale, s, text_end, wrap_width - line_width);
+ word_wrap_eol = ImFontCalcWordWrapPositionEx(font, size, s, text_end, wrap_width - line_width, flags);
if (s >= word_wrap_eol)
{
@@ -4066,8 +5506,10 @@ ImVec2 ImFont::CalcTextSizeA(float size, float max_width, float wrap_width, cons
text_size.x = line_width;
text_size.y += line_height;
line_width = 0.0f;
+ s = ImTextCalcWordWrapNextLineStart(s, text_end, flags); // Wrapping skips upcoming blanks
+ if (flags & ImDrawTextFlags_StopOnNewLine)
+ break;
word_wrap_eol = NULL;
- s = CalcWordWrapNextLineStartA(s, text_end); // Wrapping skips upcoming blanks
continue;
}
}
@@ -4080,20 +5522,24 @@ ImVec2 ImFont::CalcTextSizeA(float size, float max_width, float wrap_width, cons
else
s += ImTextCharFromUtf8(&c, s, text_end);
- if (c < 32)
+ if (c == '\n')
{
- if (c == '\n')
- {
- text_size.x = ImMax(text_size.x, line_width);
- text_size.y += line_height;
- line_width = 0.0f;
- continue;
- }
- if (c == '\r')
- continue;
+ text_size.x = ImMax(text_size.x, line_width);
+ text_size.y += line_height;
+ line_width = 0.0f;
+ if (flags & ImDrawTextFlags_StopOnNewLine)
+ break;
+ continue;
}
+ if (c == '\r')
+ continue;
+
+ // Optimized inline version of 'float char_width = GetCharAdvance((ImWchar)c);'
+ float char_width = (c < (unsigned int)baked->IndexAdvanceX.Size) ? baked->IndexAdvanceX.Data[c] : -1.0f;
+ if (char_width < 0.0f)
+ char_width = BuildLoadGlyphGetAdvanceOrFallback(baked, c);
+ char_width *= scale;
- const float char_width = ImFontGetCharAdvanceX(this, c) * scale;
if (line_width + char_width >= max_width)
{
s = prev_s;
@@ -4106,34 +5552,68 @@ ImVec2 ImFont::CalcTextSizeA(float size, float max_width, float wrap_width, cons
if (text_size.x < line_width)
text_size.x = line_width;
- if (line_width > 0 || text_size.y == 0.0f)
+ if (out_offset != NULL)
+ *out_offset = ImVec2(line_width, text_size.y + line_height); // offset allow for the possibility of sitting after a trailing \n
+
+ if (line_width > 0 || text_size.y == 0.0f) // whereas size.y will ignore the trailing \n
text_size.y += line_height;
- if (remaining)
- *remaining = s;
+ if (out_remaining != NULL)
+ *out_remaining = s;
return text_size;
}
-// Note: as with every ImDrawList drawing function, this expects that the font atlas texture is bound.
-void ImFont::RenderChar(ImDrawList* draw_list, float size, const ImVec2& pos, ImU32 col, ImWchar c)
+ImVec2 ImFont::CalcTextSizeA(float size, float max_width, float wrap_width, const char* text_begin, const char* text_end, const char** out_remaining)
{
- const ImFontGlyph* glyph = FindGlyph(c);
+ return ImFontCalcTextSizeEx(this, size, max_width, wrap_width, text_begin, text_end, text_end, out_remaining, NULL, ImDrawTextFlags_None);
+}
+
+// Note: as with every ImDrawList drawing function, this expects that the font atlas texture is bound.
+void ImFont::RenderChar(ImDrawList* draw_list, float size, const ImVec2& pos, ImU32 col, ImWchar c, const ImVec4* cpu_fine_clip)
+{
+ ImFontBaked* baked = GetFontBaked(size);
+ const ImFontGlyph* glyph = baked->FindGlyph(c);
if (!glyph || !glyph->Visible)
return;
if (glyph->Colored)
col |= ~IM_COL32_A_MASK;
- float scale = (size >= 0.0f) ? (size / FontSize) : 1.0f;
+ float scale = (size >= 0.0f) ? (size / baked->Size) : 1.0f;
float x = IM_TRUNC(pos.x);
float y = IM_TRUNC(pos.y);
+
+ float x1 = x + glyph->X0 * scale;
+ float x2 = x + glyph->X1 * scale;
+ if (cpu_fine_clip && (x1 > cpu_fine_clip->z || x2 < cpu_fine_clip->x))
+ return;
+ float y1 = y + glyph->Y0 * scale;
+ float y2 = y + glyph->Y1 * scale;
+ float u1 = glyph->U0;
+ float v1 = glyph->V0;
+ float u2 = glyph->U1;
+ float v2 = glyph->V1;
+
+ // Always CPU fine clip. Code extracted from RenderText().
+ // CPU side clipping used to fit text in their frame when the frame is too small. Only does clipping for axis aligned quads.
+ if (cpu_fine_clip != NULL)
+ {
+ if (x1 < cpu_fine_clip->x) { u1 = u1 + (1.0f - (x2 - cpu_fine_clip->x) / (x2 - x1)) * (u2 - u1); x1 = cpu_fine_clip->x; }
+ if (y1 < cpu_fine_clip->y) { v1 = v1 + (1.0f - (y2 - cpu_fine_clip->y) / (y2 - y1)) * (v2 - v1); y1 = cpu_fine_clip->y; }
+ if (x2 > cpu_fine_clip->z) { u2 = u1 + ((cpu_fine_clip->z - x1) / (x2 - x1)) * (u2 - u1); x2 = cpu_fine_clip->z; }
+ if (y2 > cpu_fine_clip->w) { v2 = v1 + ((cpu_fine_clip->w - y1) / (y2 - y1)) * (v2 - v1); y2 = cpu_fine_clip->w; }
+ if (y1 >= y2)
+ return;
+ }
draw_list->PrimReserve(6, 4);
- draw_list->PrimRectUV(ImVec2(x + glyph->X0 * scale, y + glyph->Y0 * scale), ImVec2(x + glyph->X1 * scale, y + glyph->Y1 * scale), ImVec2(glyph->U0, glyph->V0), ImVec2(glyph->U1, glyph->V1), col);
+ draw_list->PrimRectUV(ImVec2(x1, y1), ImVec2(x2, y2), ImVec2(u1, v1), ImVec2(u2, v2), col);
}
// Note: as with every ImDrawList drawing function, this expects that the font atlas texture is bound.
-void ImFont::RenderText(ImDrawList* draw_list, float size, const ImVec2& pos, ImU32 col, const ImVec4& clip_rect, const char* text_begin, const char* text_end, float wrap_width, bool cpu_fine_clip)
+// DO NOT CALL DIRECTLY THIS WILL CHANGE WILDLY IN 2025-2025. Use ImDrawList::AddText().
+void ImFont::RenderText(ImDrawList* draw_list, float size, const ImVec2& pos, ImU32 col, const ImVec4& clip_rect, const char* text_begin, const char* text_end, float wrap_width, ImDrawTextFlags flags)
{
// Align to be pixel perfect
+begin:
float x = IM_TRUNC(pos.x);
float y = IM_TRUNC(pos.y);
if (y > clip_rect.w)
@@ -4142,8 +5622,10 @@ void ImFont::RenderText(ImDrawList* draw_list, float size, const ImVec2& pos, Im
if (!text_end)
text_end = text_begin + ImStrlen(text_begin); // ImGui:: functions generally already provides a valid text_end, so this is merely to handle direct calls.
- const float scale = size / FontSize;
- const float line_height = FontSize * scale;
+ const float line_height = size;
+ ImFontBaked* baked = GetFontBaked(size);
+
+ const float scale = size / baked->Size;
const float origin_x = x;
const bool word_wrap_enabled = (wrap_width > 0.0f);
@@ -4155,11 +5637,11 @@ void ImFont::RenderText(ImDrawList* draw_list, float size, const ImVec2& pos, Im
const char* line_end = (const char*)ImMemchr(s, '\n', text_end - s);
if (word_wrap_enabled)
{
- // FIXME-OPT: This is not optimal as do first do a search for \n before calling CalcWordWrapPositionA().
- // If the specs for CalcWordWrapPositionA() were reworked to optionally return on \n we could combine both.
+ // FIXME-OPT: This is not optimal as do first do a search for \n before calling CalcWordWrapPosition().
+ // If the specs for CalcWordWrapPosition() were reworked to optionally return on \n we could combine both.
// However it is still better than nothing performing the fast-forward!
- s = CalcWordWrapPositionA(scale, s, line_end ? line_end : text_end, wrap_width);
- s = CalcWordWrapNextLineStartA(s, text_end);
+ s = ImFontCalcWordWrapPositionEx(this, size, s, line_end ? line_end : text_end, wrap_width, flags);
+ s = ImTextCalcWordWrapNextLineStart(s, text_end, flags);
}
else
{
@@ -4193,6 +5675,8 @@ void ImFont::RenderText(ImDrawList* draw_list, float size, const ImVec2& pos, Im
ImDrawVert* vtx_write = draw_list->_VtxWritePtr;
ImDrawIdx* idx_write = draw_list->_IdxWritePtr;
unsigned int vtx_index = draw_list->_VtxCurrentIdx;
+ const int cmd_count = draw_list->CmdBuffer.Size;
+ const bool cpu_fine_clip = (flags & ImDrawTextFlags_CpuFineClip) != 0;
const ImU32 col_untinted = col | ~IM_COL32_A_MASK;
const char* word_wrap_eol = NULL;
@@ -4203,7 +5687,7 @@ void ImFont::RenderText(ImDrawList* draw_list, float size, const ImVec2& pos, Im
{
// Calculate how far we can render. Requires two passes on the string data but keeps the code simple and not intrusive for what's essentially an uncommon feature.
if (!word_wrap_eol)
- word_wrap_eol = CalcWordWrapPositionA(scale, s, text_end, wrap_width - (x - origin_x));
+ word_wrap_eol = ImFontCalcWordWrapPositionEx(this, size, s, text_end, wrap_width - (x - origin_x), flags);
if (s >= word_wrap_eol)
{
@@ -4212,7 +5696,7 @@ void ImFont::RenderText(ImDrawList* draw_list, float size, const ImVec2& pos, Im
if (y > clip_rect.w)
break; // break out of main loop
word_wrap_eol = NULL;
- s = CalcWordWrapNextLineStartA(s, text_end); // Wrapping skips upcoming blanks
+ s = ImTextCalcWordWrapNextLineStart(s, text_end, flags); // Wrapping skips upcoming blanks
continue;
}
}
@@ -4238,9 +5722,9 @@ void ImFont::RenderText(ImDrawList* draw_list, float size, const ImVec2& pos, Im
continue;
}
- const ImFontGlyph* glyph = FindGlyph((ImWchar)c);
- if (glyph == NULL)
- continue;
+ const ImFontGlyph* glyph = baked->FindGlyph((ImWchar)c);
+ //if (glyph == NULL)
+ // continue;
float char_width = glyph->AdvanceX * scale;
if (glyph->Visible)
@@ -4308,6 +5792,20 @@ void ImFont::RenderText(ImDrawList* draw_list, float size, const ImVec2& pos, Im
x += char_width;
}
+ // Edge case: calling RenderText() with unloaded glyphs triggering texture change. It doesn't happen via ImGui:: calls because CalcTextSize() is always used.
+ if (cmd_count != draw_list->CmdBuffer.Size) //-V547
+ {
+ IM_ASSERT(draw_list->CmdBuffer[draw_list->CmdBuffer.Size - 1].ElemCount == 0);
+ draw_list->CmdBuffer.pop_back();
+ draw_list->PrimUnreserve(idx_count_max, vtx_count_max);
+ draw_list->AddDrawCmd();
+ //IMGUI_DEBUG_LOG("RenderText: cancel and retry to missing glyphs.\n"); // [DEBUG]
+ //draw_list->AddRectFilled(pos, pos + ImVec2(10, 10), IM_COL32(255, 0, 0, 255)); // [DEBUG]
+ goto begin;
+ //RenderText(draw_list, size, pos, col, clip_rect, text_begin, text_end, wrap_width, cpu_fine_clip); // FIXME-OPT: Would a 'goto begin' be better for code-gen?
+ //return;
+ }
+
// Give back unused vertices (clipped ones, blanks) ~ this is essentially a PrimUnreserve() action.
draw_list->VtxBuffer.Size = (int)(vtx_write - draw_list->VtxBuffer.Data); // Same as calling shrink()
draw_list->IdxBuffer.Size = (int)(idx_write - draw_list->IdxBuffer.Data);
@@ -4367,8 +5865,9 @@ void ImGui::RenderArrow(ImDrawList* draw_list, ImVec2 pos, ImU32 col, ImGuiDir d
void ImGui::RenderBullet(ImDrawList* draw_list, ImVec2 pos, ImU32 col)
{
- // FIXME-OPT: This should be baked in font.
- draw_list->AddCircleFilled(pos, draw_list->_Data->FontSize * 0.20f, col, 8);
+ // FIXME-OPT: This should be baked in font now that it's easier.
+ float font_size = draw_list->_Data->FontSize;
+ draw_list->AddCircleFilled(pos, font_size * 0.20f, col, (font_size < 22) ? 8 : (font_size < 40) ? 12 : 0); // Hardcode optimal/nice tessellation threshold
}
void ImGui::RenderCheckMark(ImDrawList* draw_list, ImVec2 pos, ImU32 col, float sz)
@@ -4670,6 +6169,7 @@ static unsigned int stb_decompress(unsigned char *output, const unsigned char *i
// Copyright (c) 2004, 2005 Tristan Grimmer
// MIT license (see License.txt in http://www.proggyfonts.net/index.php?menu=download)
// Download and more information at http://www.proggyfonts.net or http://upperboundsinteractive.com/fonts.php
+// If you want a similar font which may be better scaled, consider using ProggyVector from the same author!
//-----------------------------------------------------------------------------
#ifndef IMGUI_DISABLE_DEFAULT_FONT
diff --git a/imgui_internal.h b/imgui_internal.h
index e24618bd8..b7eb659d5 100644
--- a/imgui_internal.h
+++ b/imgui_internal.h
@@ -1,4 +1,4 @@
-// dear imgui, v1.92.0 WIP
+// dear imgui, v1.92.4 WIP
// (internal structures/api)
// You may use this file to debug, understand or extend Dear ImGui features but we don't provide any guarantee of forward compatibility.
@@ -37,6 +37,7 @@ Index of this file:
// [SECTION] Tab bar, Tab item support
// [SECTION] Table support
// [SECTION] ImGui internal API
+// [SECTION] ImFontLoader
// [SECTION] ImFontAtlas internal API
// [SECTION] Test Engine specific hooks (imgui_test_engine)
@@ -76,8 +77,8 @@ Index of this file:
#ifdef _MSC_VER
#pragma warning (push)
#pragma warning (disable: 4251) // class 'xxx' needs to have dll-interface to be used by clients of struct 'xxx' // when IMGUI_API is set to__declspec(dllexport)
-#pragma warning (disable: 26812) // The enum type 'xxx' is unscoped. Prefer 'enum class' over 'enum' (Enum.3). [MSVC Static Analyzer)
#pragma warning (disable: 26495) // [Static Analyzer] Variable 'XXX' is uninitialized. Always initialize a member variable (type.6).
+#pragma warning (disable: 26812) // [Static Analyzer] The enum type 'xxx' is unscoped. Prefer 'enum class' over 'enum' (Enum.3).
#if defined(_MSC_VER) && _MSC_VER >= 1922 // MSVC 2019 16.2 or later
#pragma warning (disable: 5054) // operator '|': deprecated between enumerations of different types
#endif
@@ -132,7 +133,7 @@ Index of this file:
//-----------------------------------------------------------------------------
// Utilities
-// (other types which are not forwarded declared are: ImBitArray<>, ImSpan<>, ImSpanAllocator<>, ImPool<>, ImChunkStream<>)
+// (other types which are not forwarded declared are: ImBitArray<>, ImSpan<>, ImSpanAllocator<>, ImStableVector<>, ImPool<>, ImChunkStream<>)
struct ImBitVector; // Store 1-bit per value
struct ImRect; // An axis-aligned rectangle (2 points)
struct ImGuiTextIndex; // Maintain a line index for a text buffer.
@@ -140,6 +141,9 @@ struct ImGuiTextIndex; // Maintain a line index for a text buffer.
// ImDrawList/ImFontAtlas
struct ImDrawDataBuilder; // Helper to build a ImDrawData instance
struct ImDrawListSharedData; // Data shared between all ImDrawList instances
+struct ImFontAtlasBuilder; // Internal storage for incrementally packing and building a ImFontAtlas
+struct ImFontAtlasPostProcessData; // Data available to potential texture post-processing functions
+struct ImFontAtlasRectEntry; // Packed rectangle lookup entry
// ImGui
struct ImGuiBoxSelectState; // Box-selection state (currently used by multi-selection, could potentially be used by others)
@@ -195,6 +199,7 @@ typedef int ImGuiDataAuthority; // -> enum ImGuiDataAuthority_ // E
typedef int ImGuiLayoutType; // -> enum ImGuiLayoutType_ // Enum: Horizontal or vertical
// Flags
+typedef int ImDrawTextFlags; // -> enum ImDrawTextFlags_ // Flags: for ImTextCalcWordWrapPositionEx()
typedef int ImGuiActivateFlags; // -> enum ImGuiActivateFlags_ // Flags: for navigation/focus function (will be for ActivateItem() later)
typedef int ImGuiDebugLogFlags; // -> enum ImGuiDebugLogFlags_ // Flags: for ShowDebugLogWindow(), g.DebugLogFlags
typedef int ImGuiFocusRequestFlags; // -> enum ImGuiFocusRequestFlags_ // Flags: for FocusWindow()
@@ -212,6 +217,10 @@ typedef int ImGuiTooltipFlags; // -> enum ImGuiTooltipFlags_ // F
typedef int ImGuiTypingSelectFlags; // -> enum ImGuiTypingSelectFlags_ // Flags: for GetTypingSelectRequest()
typedef int ImGuiWindowRefreshFlags; // -> enum ImGuiWindowRefreshFlags_ // Flags: for SetNextWindowRefreshPolicy()
+// Table column indexing
+typedef ImS16 ImGuiTableColumnIdx;
+typedef ImU16 ImGuiTableDrawChannelIdx;
+
//-----------------------------------------------------------------------------
// [SECTION] Context pointer
// See implementation of this variable in imgui.cpp for comments and details.
@@ -247,7 +256,7 @@ extern IMGUI_API ImGuiContext* GImGui; // Current implicit context pointer
#define IMGUI_DEBUG_LOG_SELECTION(...) do { if (g.DebugLogFlags & ImGuiDebugLogFlags_EventSelection) IMGUI_DEBUG_LOG(__VA_ARGS__); } while (0)
#define IMGUI_DEBUG_LOG_CLIPPER(...) do { if (g.DebugLogFlags & ImGuiDebugLogFlags_EventClipper) IMGUI_DEBUG_LOG(__VA_ARGS__); } while (0)
#define IMGUI_DEBUG_LOG_IO(...) do { if (g.DebugLogFlags & ImGuiDebugLogFlags_EventIO) IMGUI_DEBUG_LOG(__VA_ARGS__); } while (0)
-#define IMGUI_DEBUG_LOG_FONT(...) do { if (g.DebugLogFlags & ImGuiDebugLogFlags_EventFont) IMGUI_DEBUG_LOG(__VA_ARGS__); } while (0)
+#define IMGUI_DEBUG_LOG_FONT(...) do { ImGuiContext* g2 = GImGui; if (g2 && g2->DebugLogFlags & ImGuiDebugLogFlags_EventFont) IMGUI_DEBUG_LOG(__VA_ARGS__); } while (0) // Called from ImFontAtlas function which may operate without a context.
#define IMGUI_DEBUG_LOG_INPUTROUTING(...) do{if (g.DebugLogFlags & ImGuiDebugLogFlags_EventInputRouting)IMGUI_DEBUG_LOG(__VA_ARGS__); } while (0)
#define IMGUI_DEBUG_LOG_DOCKING(...) do { if (g.DebugLogFlags & ImGuiDebugLogFlags_EventDocking) IMGUI_DEBUG_LOG(__VA_ARGS__); } while (0)
#define IMGUI_DEBUG_LOG_VIEWPORT(...) do { if (g.DebugLogFlags & ImGuiDebugLogFlags_EventViewport) IMGUI_DEBUG_LOG(__VA_ARGS__); } while (0)
@@ -338,6 +347,7 @@ extern IMGUI_API ImGuiContext* GImGui; // Current implicit context pointer
#define IM_PRIu64 "llu"
#define IM_PRIX64 "llX"
#endif
+#define IM_TEXTUREID_TO_U64(_TEXID) ((ImU64)(intptr_t)(_TEXID))
//-----------------------------------------------------------------------------
// [SECTION] Generic helpers
@@ -359,6 +369,7 @@ extern IMGUI_API ImGuiContext* GImGui; // Current implicit context pointer
// - Helper: ImBitArray
// - Helper: ImBitVector
// - Helper: ImSpan<>, ImSpanAllocator<>
+// - Helper: ImStableVector<>
// - Helper: ImPool<>
// - Helper: ImChunkStream<>
// - Helper: ImGuiTextIndex
@@ -368,20 +379,21 @@ extern IMGUI_API ImGuiContext* GImGui; // Current implicit context pointer
// Helpers: Hashing
IMGUI_API ImGuiID ImHashData(const void* data, size_t data_size, ImGuiID seed = 0);
IMGUI_API ImGuiID ImHashStr(const char* data, size_t data_size = 0, ImGuiID seed = 0);
+IMGUI_API const char* ImHashSkipUncontributingPrefix(const char* label);
// Helpers: Sorting
#ifndef ImQsort
-static inline void ImQsort(void* base, size_t count, size_t size_of_element, int(IMGUI_CDECL *compare_func)(void const*, void const*)) { if (count > 1) qsort(base, count, size_of_element, compare_func); }
+inline void ImQsort(void* base, size_t count, size_t size_of_element, int(IMGUI_CDECL *compare_func)(void const*, void const*)) { if (count > 1) qsort(base, count, size_of_element, compare_func); }
#endif
// Helpers: Color Blending
IMGUI_API ImU32 ImAlphaBlendColors(ImU32 col_a, ImU32 col_b);
// Helpers: Bit manipulation
-static inline bool ImIsPowerOfTwo(int v) { return v != 0 && (v & (v - 1)) == 0; }
-static inline bool ImIsPowerOfTwo(ImU64 v) { return v != 0 && (v & (v - 1)) == 0; }
-static inline int ImUpperPowerOfTwo(int v) { v--; v |= v >> 1; v |= v >> 2; v |= v >> 4; v |= v >> 8; v |= v >> 16; v++; return v; }
-static inline unsigned int ImCountSetBits(unsigned int v) { unsigned int count = 0; while (v > 0) { v = v & (v - 1); count++; } return count; }
+inline bool ImIsPowerOfTwo(int v) { return v != 0 && (v & (v - 1)) == 0; }
+inline bool ImIsPowerOfTwo(ImU64 v) { return v != 0 && (v & (v - 1)) == 0; }
+inline int ImUpperPowerOfTwo(int v) { v--; v |= v >> 1; v |= v >> 2; v |= v >> 4; v |= v >> 8; v |= v >> 16; v++; return v; }
+inline unsigned int ImCountSetBits(unsigned int v) { unsigned int count = 0; while (v > 0) { v = v & (v - 1); count++; } return count; }
// Helpers: String
#define ImStrlen strlen
@@ -390,6 +402,7 @@ IMGUI_API int ImStricmp(const char* str1, const char* str2);
IMGUI_API int ImStrnicmp(const char* str1, const char* str2, size_t count); // Case insensitive compare to a certain count.
IMGUI_API void ImStrncpy(char* dst, const char* src, size_t count); // Copy to a certain count and always zero terminate (strncpy doesn't).
IMGUI_API char* ImStrdup(const char* str); // Duplicate a string.
+IMGUI_API void* ImMemdup(const void* src, size_t size); // Duplicate a chunk of memory.
IMGUI_API char* ImStrdupcpy(char* dst, size_t* p_dst_size, const char* str); // Copy in provided buffer, recreate buffer if needed.
IMGUI_API const char* ImStrchrRange(const char* str_begin, const char* str_end, char c); // Find first occurrence of 'c' in string range.
IMGUI_API const char* ImStreolRange(const char* str, const char* str_end); // End end-of-line
@@ -399,10 +412,10 @@ IMGUI_API const char* ImStrSkipBlank(const char* str);
IMGUI_API int ImStrlenW(const ImWchar* str); // Computer string length (ImWchar string)
IMGUI_API const char* ImStrbol(const char* buf_mid_line, const char* buf_begin); // Find beginning-of-line
IM_MSVC_RUNTIME_CHECKS_OFF
-static inline char ImToUpper(char c) { return (c >= 'a' && c <= 'z') ? c &= ~32 : c; }
-static inline bool ImCharIsBlankA(char c) { return c == ' ' || c == '\t'; }
-static inline bool ImCharIsBlankW(unsigned int c) { return c == ' ' || c == '\t' || c == 0x3000; }
-static inline bool ImCharIsXdigitA(char c) { return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f'); }
+inline char ImToUpper(char c) { return (c >= 'a' && c <= 'z') ? c &= ~32 : c; }
+inline bool ImCharIsBlankA(char c) { return c == ' ' || c == '\t'; }
+inline bool ImCharIsBlankW(unsigned int c) { return c == ' ' || c == '\t' || c == 0x3000; }
+inline bool ImCharIsXdigitA(char c) { return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f'); }
IM_MSVC_RUNTIME_CHECKS_RESTORE
// Helpers: Formatting
@@ -418,7 +431,7 @@ IMGUI_API const char* ImParseFormatSanitizeForScanning(const char* fmt_in, cha
IMGUI_API int ImParseFormatPrecision(const char* format, int default_value);
// Helpers: UTF-8 <> wchar conversions
-IMGUI_API const char* ImTextCharToUtf8(char out_buf[5], unsigned int c); // return out_buf
+IMGUI_API int ImTextCharToUtf8(char out_buf[5], unsigned int c); // return output UTF-8 bytes count
IMGUI_API int ImTextStrToUtf8(char* out_buf, int out_buf_size, const ImWchar* in_text, const ImWchar* in_text_end); // return output UTF-8 bytes count
IMGUI_API int ImTextCharFromUtf8(unsigned int* out_char, const char* in_text, const char* in_text_end); // read one character. return input UTF-8 bytes count
IMGUI_API int ImTextStrFromUtf8(ImWchar* out_buf, int out_buf_size, const char* in_text, const char* in_text_end, const char** in_remaining = NULL); // return input UTF-8 bytes count
@@ -428,15 +441,27 @@ IMGUI_API int ImTextCountUtf8BytesFromStr(const ImWchar* in_text, cons
IMGUI_API const char* ImTextFindPreviousUtf8Codepoint(const char* in_text_start, const char* in_text_curr); // return previous UTF-8 code-point.
IMGUI_API int ImTextCountLines(const char* in_text, const char* in_text_end); // return number of lines taken by text. trailing carriage return doesn't count as an extra line.
+// Helpers: High-level text functions (DO NOT USE!!! THIS IS A MINIMAL SUBSET OF LARGER UPCOMING CHANGES)
+enum ImDrawTextFlags_
+{
+ ImDrawTextFlags_None = 0,
+ ImDrawTextFlags_CpuFineClip = 1 << 0, // Must be == 1/true for legacy with 'bool cpu_fine_clip' arg to RenderText()
+ ImDrawTextFlags_WrapKeepBlanks = 1 << 1,
+ ImDrawTextFlags_StopOnNewLine = 1 << 2,
+};
+IMGUI_API ImVec2 ImFontCalcTextSizeEx(ImFont* font, float size, float max_width, float wrap_width, const char* text_begin, const char* text_end_display, const char* text_end, const char** out_remaining, ImVec2* out_offset, ImDrawTextFlags flags);
+IMGUI_API const char* ImFontCalcWordWrapPositionEx(ImFont* font, float size, const char* text, const char* text_end, float wrap_width, ImDrawTextFlags flags = 0);
+IMGUI_API const char* ImTextCalcWordWrapNextLineStart(const char* text, const char* text_end, ImDrawTextFlags flags = 0); // trim trailing space and find beginning of next line
+
// Helpers: File System
#ifdef IMGUI_DISABLE_FILE_FUNCTIONS
#define IMGUI_DISABLE_DEFAULT_FILE_FUNCTIONS
typedef void* ImFileHandle;
-static inline ImFileHandle ImFileOpen(const char*, const char*) { return NULL; }
-static inline bool ImFileClose(ImFileHandle) { return false; }
-static inline ImU64 ImFileGetSize(ImFileHandle) { return (ImU64)-1; }
-static inline ImU64 ImFileRead(void*, ImU64, ImU64, ImFileHandle) { return 0; }
-static inline ImU64 ImFileWrite(const void*, ImU64, ImU64, ImFileHandle) { return 0; }
+inline ImFileHandle ImFileOpen(const char*, const char*) { return NULL; }
+inline bool ImFileClose(ImFileHandle) { return false; }
+inline ImU64 ImFileGetSize(ImFileHandle) { return (ImU64)-1; }
+inline ImU64 ImFileRead(void*, ImU64, ImU64, ImFileHandle) { return 0; }
+inline ImU64 ImFileWrite(const void*, ImU64, ImU64, ImFileHandle) { return 0; }
#endif
#ifndef IMGUI_DISABLE_DEFAULT_FILE_FUNCTIONS
typedef FILE* ImFileHandle;
@@ -463,54 +488,56 @@ IM_MSVC_RUNTIME_CHECKS_OFF
#define ImAtan2(Y, X) atan2f((Y), (X))
#define ImAtof(STR) atof(STR)
#define ImCeil(X) ceilf(X)
-static inline float ImPow(float x, float y) { return powf(x, y); } // DragBehaviorT/SliderBehaviorT uses ImPow with either float/double and need the precision
-static inline double ImPow(double x, double y) { return pow(x, y); }
-static inline float ImLog(float x) { return logf(x); } // DragBehaviorT/SliderBehaviorT uses ImLog with either float/double and need the precision
-static inline double ImLog(double x) { return log(x); }
-static inline int ImAbs(int x) { return x < 0 ? -x : x; }
-static inline float ImAbs(float x) { return fabsf(x); }
-static inline double ImAbs(double x) { return fabs(x); }
-static inline float ImSign(float x) { return (x < 0.0f) ? -1.0f : (x > 0.0f) ? 1.0f : 0.0f; } // Sign operator - returns -1, 0 or 1 based on sign of argument
-static inline double ImSign(double x) { return (x < 0.0) ? -1.0 : (x > 0.0) ? 1.0 : 0.0; }
+inline float ImPow(float x, float y) { return powf(x, y); } // DragBehaviorT/SliderBehaviorT uses ImPow with either float/double and need the precision
+inline double ImPow(double x, double y) { return pow(x, y); }
+inline float ImLog(float x) { return logf(x); } // DragBehaviorT/SliderBehaviorT uses ImLog with either float/double and need the precision
+inline double ImLog(double x) { return log(x); }
+inline int ImAbs(int x) { return x < 0 ? -x : x; }
+inline float ImAbs(float x) { return fabsf(x); }
+inline double ImAbs(double x) { return fabs(x); }
+inline float ImSign(float x) { return (x < 0.0f) ? -1.0f : (x > 0.0f) ? 1.0f : 0.0f; } // Sign operator - returns -1, 0 or 1 based on sign of argument
+inline double ImSign(double x) { return (x < 0.0) ? -1.0 : (x > 0.0) ? 1.0 : 0.0; }
#ifdef IMGUI_ENABLE_SSE
-static inline float ImRsqrt(float x) { return _mm_cvtss_f32(_mm_rsqrt_ss(_mm_set_ss(x))); }
+inline float ImRsqrt(float x) { return _mm_cvtss_f32(_mm_rsqrt_ss(_mm_set_ss(x))); }
#else
-static inline float ImRsqrt(float x) { return 1.0f / sqrtf(x); }
+inline float ImRsqrt(float x) { return 1.0f / sqrtf(x); }
#endif
-static inline double ImRsqrt(double x) { return 1.0 / sqrt(x); }
+inline double ImRsqrt(double x) { return 1.0 / sqrt(x); }
#endif
// - ImMin/ImMax/ImClamp/ImLerp/ImSwap are used by widgets which support variety of types: signed/unsigned int/long long float/double
// (Exceptionally using templates here but we could also redefine them for those types)
-template static inline T ImMin(T lhs, T rhs) { return lhs < rhs ? lhs : rhs; }
-template static inline T ImMax(T lhs, T rhs) { return lhs >= rhs ? lhs : rhs; }
-template static inline T ImClamp(T v, T mn, T mx) { return (v < mn) ? mn : (v > mx) ? mx : v; }
-template static inline T ImLerp(T a, T b, float t) { return (T)(a + (b - a) * t); }
-template static inline void ImSwap(T& a, T& b) { T tmp = a; a = b; b = tmp; }
-template static inline T ImAddClampOverflow(T a, T b, T mn, T mx) { if (b < 0 && (a < mn - b)) return mn; if (b > 0 && (a > mx - b)) return mx; return a + b; }
-template static inline T ImSubClampOverflow(T a, T b, T mn, T mx) { if (b > 0 && (a < mn + b)) return mn; if (b < 0 && (a > mx + b)) return mx; return a - b; }
+template T ImMin(T lhs, T rhs) { return lhs < rhs ? lhs : rhs; }
+template T ImMax(T lhs, T rhs) { return lhs >= rhs ? lhs : rhs; }
+template T ImClamp(T v, T mn, T mx) { return (v < mn) ? mn : (v > mx) ? mx : v; }
+template T ImLerp(T a, T b, float t) { return (T)(a + (b - a) * t); }
+template void ImSwap(T& a, T& b) { T tmp = a; a = b; b = tmp; }
+template T ImAddClampOverflow(T a, T b, T mn, T mx) { if (b < 0 && (a < mn - b)) return mn; if (b > 0 && (a > mx - b)) return mx; return a + b; }
+template T ImSubClampOverflow(T a, T b, T mn, T mx) { if (b > 0 && (a < mn + b)) return mn; if (b < 0 && (a > mx + b)) return mx; return a - b; }
// - Misc maths helpers
-static inline ImVec2 ImMin(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x < rhs.x ? lhs.x : rhs.x, lhs.y < rhs.y ? lhs.y : rhs.y); }
-static inline ImVec2 ImMax(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x >= rhs.x ? lhs.x : rhs.x, lhs.y >= rhs.y ? lhs.y : rhs.y); }
-static inline ImVec2 ImClamp(const ImVec2& v, const ImVec2&mn, const ImVec2&mx) { return ImVec2((v.x < mn.x) ? mn.x : (v.x > mx.x) ? mx.x : v.x, (v.y < mn.y) ? mn.y : (v.y > mx.y) ? mx.y : v.y); }
-static inline ImVec2 ImLerp(const ImVec2& a, const ImVec2& b, float t) { return ImVec2(a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t); }
-static inline ImVec2 ImLerp(const ImVec2& a, const ImVec2& b, const ImVec2& t) { return ImVec2(a.x + (b.x - a.x) * t.x, a.y + (b.y - a.y) * t.y); }
-static inline ImVec4 ImLerp(const ImVec4& a, const ImVec4& b, float t) { return ImVec4(a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t, a.z + (b.z - a.z) * t, a.w + (b.w - a.w) * t); }
-static inline float ImSaturate(float f) { return (f < 0.0f) ? 0.0f : (f > 1.0f) ? 1.0f : f; }
-static inline float ImLengthSqr(const ImVec2& lhs) { return (lhs.x * lhs.x) + (lhs.y * lhs.y); }
-static inline float ImLengthSqr(const ImVec4& lhs) { return (lhs.x * lhs.x) + (lhs.y * lhs.y) + (lhs.z * lhs.z) + (lhs.w * lhs.w); }
-static inline float ImInvLength(const ImVec2& lhs, float fail_value) { float d = (lhs.x * lhs.x) + (lhs.y * lhs.y); if (d > 0.0f) return ImRsqrt(d); return fail_value; }
-static inline float ImTrunc(float f) { return (float)(int)(f); }
-static inline ImVec2 ImTrunc(const ImVec2& v) { return ImVec2((float)(int)(v.x), (float)(int)(v.y)); }
-static inline float ImFloor(float f) { return (float)((f >= 0 || (float)(int)f == f) ? (int)f : (int)f - 1); } // Decent replacement for floorf()
-static inline ImVec2 ImFloor(const ImVec2& v) { return ImVec2(ImFloor(v.x), ImFloor(v.y)); }
-static inline int ImModPositive(int a, int b) { return (a + b) % b; }
-static inline float ImDot(const ImVec2& a, const ImVec2& b) { return a.x * b.x + a.y * b.y; }
-static inline ImVec2 ImRotate(const ImVec2& v, float cos_a, float sin_a) { return ImVec2(v.x * cos_a - v.y * sin_a, v.x * sin_a + v.y * cos_a); }
-static inline float ImLinearSweep(float current, float target, float speed) { if (current < target) return ImMin(current + speed, target); if (current > target) return ImMax(current - speed, target); return current; }
-static inline float ImLinearRemapClamp(float s0, float s1, float d0, float d1, float x) { return ImSaturate((x - s0) / (s1 - s0)) * (d1 - d0) + d0; }
-static inline ImVec2 ImMul(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x * rhs.x, lhs.y * rhs.y); }
-static inline bool ImIsFloatAboveGuaranteedIntegerPrecision(float f) { return f <= -16777216 || f >= 16777216; }
-static inline float ImExponentialMovingAverage(float avg, float sample, int n) { avg -= avg / n; avg += sample / n; return avg; }
+inline ImVec2 ImMin(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x < rhs.x ? lhs.x : rhs.x, lhs.y < rhs.y ? lhs.y : rhs.y); }
+inline ImVec2 ImMax(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x >= rhs.x ? lhs.x : rhs.x, lhs.y >= rhs.y ? lhs.y : rhs.y); }
+inline ImVec2 ImClamp(const ImVec2& v, const ImVec2&mn, const ImVec2&mx){ return ImVec2((v.x < mn.x) ? mn.x : (v.x > mx.x) ? mx.x : v.x, (v.y < mn.y) ? mn.y : (v.y > mx.y) ? mx.y : v.y); }
+inline ImVec2 ImLerp(const ImVec2& a, const ImVec2& b, float t) { return ImVec2(a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t); }
+inline ImVec2 ImLerp(const ImVec2& a, const ImVec2& b, const ImVec2& t) { return ImVec2(a.x + (b.x - a.x) * t.x, a.y + (b.y - a.y) * t.y); }
+inline ImVec4 ImLerp(const ImVec4& a, const ImVec4& b, float t) { return ImVec4(a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t, a.z + (b.z - a.z) * t, a.w + (b.w - a.w) * t); }
+inline float ImSaturate(float f) { return (f < 0.0f) ? 0.0f : (f > 1.0f) ? 1.0f : f; }
+inline float ImLengthSqr(const ImVec2& lhs) { return (lhs.x * lhs.x) + (lhs.y * lhs.y); }
+inline float ImLengthSqr(const ImVec4& lhs) { return (lhs.x * lhs.x) + (lhs.y * lhs.y) + (lhs.z * lhs.z) + (lhs.w * lhs.w); }
+inline float ImInvLength(const ImVec2& lhs, float fail_value) { float d = (lhs.x * lhs.x) + (lhs.y * lhs.y); if (d > 0.0f) return ImRsqrt(d); return fail_value; }
+inline float ImTrunc(float f) { return (float)(int)(f); }
+inline ImVec2 ImTrunc(const ImVec2& v) { return ImVec2((float)(int)(v.x), (float)(int)(v.y)); }
+inline float ImFloor(float f) { return (float)((f >= 0 || (float)(int)f == f) ? (int)f : (int)f - 1); } // Decent replacement for floorf()
+inline ImVec2 ImFloor(const ImVec2& v) { return ImVec2(ImFloor(v.x), ImFloor(v.y)); }
+inline float ImTrunc64(float f) { return (float)(ImS64)(f); }
+inline float ImRound64(float f) { return (float)(ImS64)(f + 0.5f); }
+inline int ImModPositive(int a, int b) { return (a + b) % b; }
+inline float ImDot(const ImVec2& a, const ImVec2& b) { return a.x * b.x + a.y * b.y; }
+inline ImVec2 ImRotate(const ImVec2& v, float cos_a, float sin_a) { return ImVec2(v.x * cos_a - v.y * sin_a, v.x * sin_a + v.y * cos_a); }
+inline float ImLinearSweep(float current, float target, float speed) { if (current < target) return ImMin(current + speed, target); if (current > target) return ImMax(current - speed, target); return current; }
+inline float ImLinearRemapClamp(float s0, float s1, float d0, float d1, float x) { return ImSaturate((x - s0) / (s1 - s0)) * (d1 - d0) + d0; }
+inline ImVec2 ImMul(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x * rhs.x, lhs.y * rhs.y); }
+inline bool ImIsFloatAboveGuaranteedIntegerPrecision(float f) { return f <= -16777216 || f >= 16777216; }
+inline float ImExponentialMovingAverage(float avg, float sample, int n){ avg -= avg / n; avg += sample / n; return avg; }
IM_MSVC_RUNTIME_CHECKS_RESTORE
// Helpers: Geometry
@@ -535,6 +562,14 @@ struct ImVec1
constexpr ImVec1(float _x) : x(_x) { }
};
+// Helper: ImVec2i (2D vector, integer)
+struct ImVec2i
+{
+ int x, y;
+ constexpr ImVec2i() : x(0), y(0) {}
+ constexpr ImVec2i(int _x, int _y) : x(_x), y(_y) {}
+};
+
// Helper: ImVec2ih (2D vector, half-size integer, for long-term packed storage)
struct ImVec2ih
{
@@ -581,6 +616,7 @@ struct IMGUI_API ImRect
void Floor() { Min.x = IM_TRUNC(Min.x); Min.y = IM_TRUNC(Min.y); Max.x = IM_TRUNC(Max.x); Max.y = IM_TRUNC(Max.y); }
bool IsInverted() const { return Min.x > Max.x || Min.y > Max.y; }
ImVec4 ToVec4() const { return ImVec4(Min.x, Min.y, Max.x, Max.y); }
+ const ImVec4& AsVec4() const { return *(const ImVec4*)&Min.x; }
};
// Helper: ImBitArray
@@ -686,6 +722,39 @@ struct ImSpanAllocator
inline void GetSpan(int n, ImSpan* span) { span->set((T*)GetSpanPtrBegin(n), (T*)GetSpanPtrEnd(n)); }
};
+// Helper: ImStableVector<>
+// Allocating chunks of BLOCK_SIZE items. Objects pointers are never invalidated when growing, only by clear().
+// Important: does not destruct anything!
+// Implemented only the minimum set of functions we need for it.
+template
+struct ImStableVector
+{
+ int Size = 0;
+ int Capacity = 0;
+ ImVector Blocks;
+
+ // Functions
+ inline ~ImStableVector() { for (T* block : Blocks) IM_FREE(block); }
+
+ inline void clear() { Size = Capacity = 0; Blocks.clear_delete(); }
+ inline void resize(int new_size) { if (new_size > Capacity) reserve(new_size); Size = new_size; }
+ inline void reserve(int new_cap)
+ {
+ new_cap = IM_MEMALIGN(new_cap, BLOCK_SIZE);
+ int old_count = Capacity / BLOCK_SIZE;
+ int new_count = new_cap / BLOCK_SIZE;
+ if (new_count <= old_count)
+ return;
+ Blocks.resize(new_count);
+ for (int n = old_count; n < new_count; n++)
+ Blocks[n] = (T*)IM_ALLOC(sizeof(T) * BLOCK_SIZE);
+ Capacity = new_cap;
+ }
+ inline T& operator[](int i) { IM_ASSERT(i >= 0 && i < Size); return Blocks[i / BLOCK_SIZE][i % BLOCK_SIZE]; }
+ inline const T& operator[](int i) const { IM_ASSERT(i >= 0 && i < Size); return Blocks[i / BLOCK_SIZE][i % BLOCK_SIZE]; }
+ inline T* push_back(const T& v) { int i = Size; IM_ASSERT(i >= 0); if (Size == Capacity) reserve(Capacity + BLOCK_SIZE); void* ptr = &Blocks[i / BLOCK_SIZE][i % BLOCK_SIZE]; memcpy(ptr, &v, sizeof(v)); Size++; return (T*)ptr; }
+};
+
// Helper: ImPool<>
// Basic keyed storage for contiguous instances, slow/amortized insertion, O(1) indexable, O(Log N) queries by ID over a dense/hot buffer,
// Honor constructor/destructor. Add/remove invalidate all pointers. Indexes have the same lifetime as the associated object.
@@ -746,13 +815,13 @@ struct ImChunkStream
// Maintain a line index for a text buffer. This is a strong candidate to be moved into the public API.
struct ImGuiTextIndex
{
- ImVector LineOffsets;
+ ImVector Offsets;
int EndOffset = 0; // Because we don't own text buffer we need to maintain EndOffset (may bake in LineOffsets?)
- void clear() { LineOffsets.clear(); EndOffset = 0; }
- int size() { return LineOffsets.Size; }
- const char* get_line_begin(const char* base, int n) { return base + LineOffsets[n]; }
- const char* get_line_end(const char* base, int n) { return base + (n + 1 < LineOffsets.Size ? (LineOffsets[n + 1] - 1) : EndOffset); }
+ void clear() { Offsets.clear(); EndOffset = 0; }
+ int size() { return Offsets.Size; }
+ const char* get_line_begin(const char* base, int n) { return base + (Offsets.Size != 0 ? Offsets[n] : 0); }
+ const char* get_line_end(const char* base, int n) { return base + (n + 1 < Offsets.Size ? (Offsets[n + 1] - 1) : EndOffset); }
void append(const char* base, int old_size, int new_size);
};
@@ -794,17 +863,20 @@ IMGUI_API ImGuiStoragePair* ImLowerBound(ImGuiStoragePair* in_begin, ImGuiStorag
// You may want to create your own instance of you try to ImDrawList completely without ImGui. In that case, watch out for future changes to this structure.
struct IMGUI_API ImDrawListSharedData
{
- ImVec2 TexUvWhitePixel; // UV of white pixel in the atlas
- const ImVec4* TexUvLines; // UV of anti-aliased lines in the atlas
- ImFont* Font; // Current/default font (optional, for simplified AddText overload)
- float FontSize; // Current/default font size (optional, for simplified AddText overload)
- float FontScale; // Current/default font scale (== FontSize / Font->FontSize)
+ ImVec2 TexUvWhitePixel; // UV of white pixel in the atlas (== FontAtlas->TexUvWhitePixel)
+ const ImVec4* TexUvLines; // UV of anti-aliased lines in the atlas (== FontAtlas->TexUvLines)
+ ImFontAtlas* FontAtlas; // Current font atlas
+ ImFont* Font; // Current font (used for simplified AddText overload)
+ float FontSize; // Current font size (used for for simplified AddText overload)
+ float FontScale; // Current font scale (== FontSize / Font->FontSize)
float CurveTessellationTol; // Tessellation tolerance when using PathBezierCurveTo()
float CircleSegmentMaxError; // Number of circle segments to use per pixel of radius for AddCircle() etc
float InitialFringeScale; // Initial scale to apply to AA fringe
ImDrawListFlags InitialFlags; // Initial flags at the beginning of the frame (it is possible to alter flags on a per-drawlist basis afterwards)
ImVec4 ClipRectFullscreen; // Value for PushClipRectFullscreen()
ImVector TempBuffer; // Temporary write buffer
+ ImVector DrawLists; // All draw lists associated to this ImDrawListSharedData
+ ImGuiContext* Context; // [OPTIONAL] Link to Dear ImGui context. 99% of ImDrawList/ImFontAtlas can function without an ImGui context, but this facilitate handling one legacy edge case.
// Lookup tables
ImVec2 ArcFastVtx[IM_DRAWLIST_ARCFAST_TABLE_SIZE]; // Sample points on the quarter of the circle.
@@ -812,6 +884,7 @@ struct IMGUI_API ImDrawListSharedData
ImU8 CircleSegmentCounts[64]; // Precomputed segment count for given radius before we calculate it dynamically (to avoid calculation overhead)
ImDrawListSharedData();
+ ~ImDrawListSharedData();
void SetCircleTessellationMaxError(float max_error);
};
@@ -823,6 +896,13 @@ struct ImDrawDataBuilder
ImDrawDataBuilder() { memset(this, 0, sizeof(*this)); }
};
+struct ImFontStackData
+{
+ ImFont* Font;
+ float FontSizeBeforeScaling; // ~~ style.FontSizeBase
+ float FontSizeAfterScaling; // ~~ g.FontSize
+};
+
//-----------------------------------------------------------------------------
// [SECTION] Style support
//-----------------------------------------------------------------------------
@@ -894,6 +974,7 @@ enum ImGuiItemFlagsPrivate_
ImGuiItemFlags_AllowOverlap = 1 << 14, // false // Allow being overlapped by another widget. Not-hovered to Hovered transition deferred by a frame.
ImGuiItemFlags_NoNavDisableMouseHover = 1 << 15, // false // Nav keyboard/gamepad mode doesn't disable hover highlight (behave as if NavHighlightItemUnderNav==false).
ImGuiItemFlags_NoMarkEdited = 1 << 16, // false // Skip calling MarkItemEdited()
+ ImGuiItemFlags_NoFocus = 1 << 17, // false // [EXPERIMENTAL: Not very well specced] Clicking doesn't take focus. Automatically sets ImGuiButtonFlags_NoFocus + ImGuiButtonFlags_NoNavFocus in ButtonBehavior().
// Controlled by widget code
ImGuiItemFlags_Inputable = 1 << 20, // false // [WIP] Auto-activate input mode when tab focused. Currently only used and supported by a few items before it becomes a generic feature.
@@ -922,6 +1003,7 @@ enum ImGuiItemStatusFlags_
ImGuiItemStatusFlags_Visible = 1 << 8, // [WIP] Set when item is overlapping the current clipping rectangle (Used internally. Please don't use yet: API/system will change as we refactor Itemadd()).
ImGuiItemStatusFlags_HasClipRect = 1 << 9, // g.LastItemData.ClipRect is valid.
ImGuiItemStatusFlags_HasShortcut = 1 << 10, // g.LastItemData.Shortcut valid. Set by SetNextItemShortcut() -> ItemAdd().
+ //ImGuiItemStatusFlags_FocusedByTabbing = 1 << 8, // Removed IN 1.90.1 (Dec 2023). The trigger is part of g.NavActivateId. See commit 54c1bdeceb.
// Additional status + semantic for ImGuiTestEngine
#ifdef IMGUI_ENABLE_TEST_ENGINE
@@ -971,8 +1053,10 @@ enum ImGuiButtonFlagsPrivate_
ImGuiButtonFlags_NoHoveredOnFocus = 1 << 19, // don't report as hovered when nav focus is on this item
ImGuiButtonFlags_NoSetKeyOwner = 1 << 20, // don't set key/input owner on the initial click (note: mouse buttons are keys! often, the key in question will be ImGuiKey_MouseLeft!)
ImGuiButtonFlags_NoTestKeyOwner = 1 << 21, // don't test key/input owner when polling the key (note: mouse buttons are keys! often, the key in question will be ImGuiKey_MouseLeft!)
+ ImGuiButtonFlags_NoFocus = 1 << 22, // [EXPERIMENTAL: Not very well specced]. Don't focus parent window when clicking.
ImGuiButtonFlags_PressedOnMask_ = ImGuiButtonFlags_PressedOnClick | ImGuiButtonFlags_PressedOnClickRelease | ImGuiButtonFlags_PressedOnClickReleaseAnywhere | ImGuiButtonFlags_PressedOnRelease | ImGuiButtonFlags_PressedOnDoubleClick | ImGuiButtonFlags_PressedOnDragDropHold,
ImGuiButtonFlags_PressedOnDefault_ = ImGuiButtonFlags_PressedOnClickRelease,
+ //ImGuiButtonFlags_NoKeyModifiers = ImGuiButtonFlags_NoKeyModsAllowed, // Renamed in 1.91.4
};
// Extend ImGuiComboFlags_
@@ -993,7 +1077,6 @@ enum ImGuiSelectableFlagsPrivate_
{
// NB: need to be in sync with last value of ImGuiSelectableFlags_
ImGuiSelectableFlags_NoHoldingActiveID = 1 << 20,
- ImGuiSelectableFlags_SelectOnNav = 1 << 21, // (WIP) Auto-select when moved into. This is not exposed in public API as to handle multi-select and modifiers we will need user to explicitly control focus scope. May be replaced with a BeginSelection() API.
ImGuiSelectableFlags_SelectOnClick = 1 << 22, // Override button behavior to react on Click (default is Click+Release)
ImGuiSelectableFlags_SelectOnRelease = 1 << 23, // Override button behavior to react on Release (default is Click+Release)
ImGuiSelectableFlags_SpanAvailWidth = 1 << 24, // Span all avail width even if we declared less for layout purpose. FIXME: We may be able to remove this (added in 6251d379, 2bcafc86 for menus)
@@ -1005,9 +1088,11 @@ enum ImGuiSelectableFlagsPrivate_
// Extend ImGuiTreeNodeFlags_
enum ImGuiTreeNodeFlagsPrivate_
{
+ ImGuiTreeNodeFlags_NoNavFocus = 1 << 27,// Don't claim nav focus when interacting with this item (#8551)
ImGuiTreeNodeFlags_ClipLabelForTrailingButton = 1 << 28,// FIXME-WIP: Hard-coded for CollapsingHeader()
ImGuiTreeNodeFlags_UpsideDownArrow = 1 << 29,// FIXME-WIP: Turn Down arrow into an Up arrow, for reversed trees (#6517)
ImGuiTreeNodeFlags_OpenOnMask_ = ImGuiTreeNodeFlags_OpenOnDoubleClick | ImGuiTreeNodeFlags_OpenOnArrow,
+ ImGuiTreeNodeFlags_DrawLinesMask_ = ImGuiTreeNodeFlags_DrawLinesNone | ImGuiTreeNodeFlags_DrawLinesFull | ImGuiTreeNodeFlags_DrawLinesToNodes,
};
enum ImGuiSeparatorFlags_
@@ -1159,11 +1244,15 @@ struct IMGUI_API ImGuiInputTextState
ImVector CallbackTextBackup; // temporary storage for callback to support automatic reconcile of undo-stack
int BufCapacity; // end-user buffer capacity (include zero terminator)
ImVec2 Scroll; // horizontal offset (managed manually) + vertical scrolling (pulled from child window's own Scroll.y)
+ int LineCount; // last line count (solely for debugging)
+ float WrapWidth; // word-wrapping width
float CursorAnim; // timer for cursor blink, reset on every user action so the cursor reappears immediately
bool CursorFollow; // set when we want scrolling to follow the current cursor position (not always!)
+ bool CursorCenterY; // set when we want scrolling to be centered over the cursor position (while resizing a word-wrapping field)
bool SelectedAllMouseLock; // after a double-click to select all, we ignore further mouse drags to update selection
bool Edited; // edited this frame
bool WantReloadUserBuf; // force a reload of user buf so it may be modified externally. may be automatic in future version.
+ ImS8 LastMoveDirectionLR; // ImGuiDir_Left or ImGuiDir_Right. track last movement direction so when cursor cross over a word-wrapping boundaries we can display it on either line depending on last move.s
int ReloadSelectionStart;
int ReloadSelectionEnd;
@@ -1173,6 +1262,7 @@ struct IMGUI_API ImGuiInputTextState
void ClearFreeMemory() { TextA.clear(); TextToRevertTo.clear(); }
void OnKeyPressed(int key); // Cannot be inline because we call in code in stb_textedit.h implementation
void OnCharPressed(unsigned int c);
+ float GetPreferredOffsetX() const;
// Cursor & Selection
void CursorAnimReset();
@@ -1302,15 +1392,18 @@ struct ImGuiLastItemData
};
// Store data emitted by TreeNode() for usage by TreePop()
-// - To implement ImGuiTreeNodeFlags_NavLeftJumpsBackHere: store the minimum amount of data
+// - To implement ImGuiTreeNodeFlags_NavLeftJumpsToParent: store the minimum amount of data
// which we can't infer in TreePop(), to perform the equivalent of NavApplyItemToResult().
// Only stored when the node is a potential candidate for landing on a Left arrow jump.
struct ImGuiTreeNodeStackData
{
ImGuiID ID;
ImGuiTreeNodeFlags TreeFlags;
- ImGuiItemFlags ItemFlags; // Used for nav landing
- ImRect NavRect; // Used for nav landing
+ ImGuiItemFlags ItemFlags; // Used for nav landing
+ ImRect NavRect; // Used for nav landing
+ float DrawLinesX1;
+ float DrawLinesToNodesY2;
+ ImGuiTableColumnIdx DrawLinesTableColumn;
};
// sizeof() = 20
@@ -1434,7 +1527,7 @@ enum ImGuiInputEventType
ImGuiInputEventType_COUNT
};
-enum ImGuiInputSource
+enum ImGuiInputSource : int
{
ImGuiInputSource_None = 0,
ImGuiInputSource_Mouse, // Note: may be Mouse or TouchScreen or Pen. See io.MouseSource to distinguish them.
@@ -1599,8 +1692,9 @@ enum ImGuiActivateFlags_
ImGuiActivateFlags_PreferInput = 1 << 0, // Favor activation that requires keyboard text input (e.g. for Slider/Drag). Default for Enter key.
ImGuiActivateFlags_PreferTweak = 1 << 1, // Favor activation for tweaking with arrows or gamepad (e.g. for Slider/Drag). Default for Space key and if keyboard is not used.
ImGuiActivateFlags_TryToPreserveState = 1 << 2, // Request widget to preserve state if it can (e.g. InputText will try to preserve cursor/selection)
- ImGuiActivateFlags_FromTabbing = 1 << 3, // Activation requested by a tabbing request
+ ImGuiActivateFlags_FromTabbing = 1 << 3, // Activation requested by a tabbing request (ImGuiNavMoveFlags_IsTabbing)
ImGuiActivateFlags_FromShortcut = 1 << 4, // Activation requested by an item shortcut via SetNextItemShortcut() function.
+ ImGuiActivateFlags_FromFocusApi = 1 << 5, // Activation requested by an api request (ImGuiNavMoveFlags_FocusApi)
};
// Early work-in-progress API for ScrollToItem()
@@ -1629,6 +1723,7 @@ enum ImGuiNavRenderCursorFlags_
ImGuiNavHighlightFlags_Compact = ImGuiNavRenderCursorFlags_Compact, // Renamed in 1.91.4
ImGuiNavHighlightFlags_AlwaysDraw = ImGuiNavRenderCursorFlags_AlwaysDraw, // Renamed in 1.91.4
ImGuiNavHighlightFlags_NoRounding = ImGuiNavRenderCursorFlags_NoRounding, // Renamed in 1.91.4
+ //ImGuiNavHighlightFlags_TypeThin = ImGuiNavRenderCursorFlags_Compact, // Renamed in 1.90.2
#endif
};
@@ -2199,11 +2294,13 @@ struct ImGuiMetricsConfig
bool ShowDrawCmdMesh = true;
bool ShowDrawCmdBoundingBoxes = true;
bool ShowTextEncodingViewer = false;
+ bool ShowTextureUsedRect = false;
bool ShowDockingNodes = false;
int ShowWindowsRectsType = -1;
int ShowTablesRectsType = -1;
int HighlightMonitorIdx = -1;
ImGuiID HighlightViewportID = 0;
+ bool ShowFontPreview = true;
};
struct ImGuiStackLevelInfo
@@ -2211,10 +2308,10 @@ struct ImGuiStackLevelInfo
ImGuiID ID;
ImS8 QueryFrameCount; // >= 1: Query in progress
bool QuerySuccess; // Obtained result from DebugHookIdInfo()
- ImGuiDataType DataType : 8;
- char Desc[57]; // Arbitrarily sized buffer to hold a result (FIXME: could replace Results[] with a chunk stream?) FIXME: Now that we added CTRL+C this should be fixed.
+ ImS8 DataType; // ImGuiDataType
+ int DescOffset; // -1 or offset into parent's ResultPathsBuf
- ImGuiStackLevelInfo() { memset(this, 0, sizeof(*this)); }
+ ImGuiStackLevelInfo() { memset(this, 0, sizeof(*this)); DescOffset = -1; }
};
// State for ID Stack tool queries
@@ -2222,13 +2319,16 @@ struct ImGuiIDStackTool
{
int LastActiveFrame;
int StackLevel; // -1: query stack and resize Results, >= 0: individual stack level
- ImGuiID QueryId; // ID to query details for
+ ImGuiID QueryMainId; // ID to query details for
ImVector Results;
- bool CopyToClipboardOnCtrlC;
+ bool QueryHookActive; // Used to disambiguate the case where DebugHookIdInfoId == 0 which is valid.
+ bool OptHexEncodeNonAsciiChars;
+ bool OptCopyToClipboardOnCtrlC;
float CopyToClipboardLastTime;
- ImGuiTextBuffer ResultPathBuf;
+ ImGuiTextBuffer ResultPathsBuf;
+ ImGuiTextBuffer ResultTempBuf;
- ImGuiIDStackTool() { memset(this, 0, sizeof(*this)); CopyToClipboardLastTime = -FLT_MAX; }
+ ImGuiIDStackTool() { memset(this, 0, sizeof(*this)); LastActiveFrame = -1; OptHexEncodeNonAsciiChars = true; CopyToClipboardLastTime = -FLT_MAX; }
};
//-----------------------------------------------------------------------------
@@ -2256,16 +2356,18 @@ struct ImGuiContextHook
struct ImGuiContext
{
bool Initialized;
- bool FontAtlasOwnedByContext; // IO.Fonts-> is owned by the ImGuiContext and will be destructed along with it.
ImGuiIO IO;
ImGuiPlatformIO PlatformIO;
ImGuiStyle Style;
ImGuiConfigFlags ConfigFlagsCurrFrame; // = g.IO.ConfigFlags at the time of NewFrame()
ImGuiConfigFlags ConfigFlagsLastFrame;
- ImFont* Font; // (Shortcut) == FontStack.empty() ? IO.Font : FontStack.back()
- float FontSize; // (Shortcut) == FontBaseSize * g.CurrentWindow->FontWindowScale == window->FontSize(). Text height for current window.
- float FontBaseSize; // (Shortcut) == IO.FontGlobalScale * Font->Scale * Font->FontSize. Base text height.
- float FontScale; // == FontSize / Font->FontSize
+ ImVector FontAtlases; // List of font atlases used by the context (generally only contains g.IO.Fonts aka the main font atlas)
+ ImFont* Font; // Currently bound font. (== FontStack.back().Font)
+ ImFontBaked* FontBaked; // Currently bound font at currently bound size. (== Font->GetFontBaked(FontSize))
+ float FontSize; // Currently bound font size == line height (== FontSizeBase + externals scales applied in the UpdateCurrentFontSize() function).
+ float FontSizeBase; // Font size before scaling == style.FontSizeBase == value passed to PushFont() when specified.
+ float FontBakedScale; // == FontBaked->Size / FontSize. Scale factor over baked size. Rarely used nowadays, very often == 1.0f.
+ float FontRasterizerDensity; // Current font density. Used by all calls to GetFontBaked().
float CurrentDpiScale; // Current window/viewport DpiScale == CurrentViewport->DpiScale
ImDrawListSharedData DrawListSharedData;
double Time;
@@ -2310,8 +2412,8 @@ struct ImGuiContext
ImVec2 WheelingAxisAvg;
// Item/widgets state and tracking information
- ImGuiID DebugDrawIdConflicts; // Set when we detect multiple items with the same identifier
- ImGuiID DebugHookIdInfo; // Will call core hooks: DebugHookIdInfo() from GetID functions, used by ID Stack Tool [next HoveredId/ActiveId to not pull in an extra cache-line]
+ ImGuiID DebugDrawIdConflictsId; // Set when we detect multiple items with the same identifier
+ ImGuiID DebugHookIdInfoId; // Will call core hooks: DebugHookIdInfo() from GetID functions, used by ID Stack Tool [next HoveredId/ActiveId to not pull in an extra cache-line]
ImGuiID HoveredId; // Hovered widget, filled during the frame
ImGuiID HoveredIdPreviousFrame;
int HoveredIdPreviousFrameItemCount; // Count numbers of items using the same ID as last frame's hovered id
@@ -2330,6 +2432,7 @@ struct ImGuiContext
bool ActiveIdHasBeenEditedBefore; // Was the value associated to the widget Edited over the course of the Active state.
bool ActiveIdHasBeenEditedThisFrame;
bool ActiveIdFromShortcut;
+ ImGuiID ActiveIdDisabledId; // When clicking a disabled item we set ActiveId=window->MoveId to avoid interference with widget code. Actual item ID is stored here.
int ActiveIdMouseButton : 8;
ImVec2 ActiveIdClickOffset; // Clicked offset from upper-left corner, if applicable (currently only set by ButtonBehavior)
ImGuiWindow* ActiveIdWindow;
@@ -2368,7 +2471,7 @@ struct ImGuiContext
ImGuiCol DebugFlashStyleColorIdx; // (Keep close to ColorStack to share cache line)
ImVector ColorStack; // Stack for PushStyleColor()/PopStyleColor() - inherited by Begin()
ImVector StyleVarStack; // Stack for PushStyleVar()/PopStyleVar() - inherited by Begin()
- ImVector FontStack; // Stack for PushFont()/PopFont() - inherited by Begin()
+ ImVector FontStack; // Stack for PushFont()/PopFont() - inherited by Begin()
ImVector FocusScopeStack; // Stack for PushFocusScope()/PopFocusScope() - inherited by BeginChild(), pushed into by Begin()
ImVector ItemFlagsStack; // Stack for PushItemFlag()/PopItemFlag() - inherited by Begin()
ImVector GroupStack; // Stack for BeginGroup()/EndGroup() - not inherited by Begin()
@@ -2399,18 +2502,19 @@ struct ImGuiContext
ImGuiWindow* NavWindow; // Focused window for navigation. Could be called 'FocusedWindow'
ImGuiID NavFocusScopeId; // Focused focus scope (e.g. selection code often wants to "clear other items" when landing on an item of the same scope)
ImGuiNavLayer NavLayer; // Focused layer (main scrolling layer, or menu/title bar layer)
- ImGuiID NavActivateId; // ~~ (g.ActiveId == 0) && (IsKeyPressed(ImGuiKey_Space) || IsKeyDown(ImGuiKey_Enter) || IsKeyPressed(ImGuiKey_NavGamepadActivate)) ? NavId : 0, also set when calling ActivateItem()
+ ImGuiID NavActivateId; // ~~ (g.ActiveId == 0) && (IsKeyPressed(ImGuiKey_Space) || IsKeyDown(ImGuiKey_Enter) || IsKeyPressed(ImGuiKey_NavGamepadActivate)) ? NavId : 0, also set when calling ActivateItemByID()
ImGuiID NavActivateDownId; // ~~ IsKeyDown(ImGuiKey_Space) || IsKeyDown(ImGuiKey_Enter) || IsKeyDown(ImGuiKey_NavGamepadActivate) ? NavId : 0
ImGuiID NavActivatePressedId; // ~~ IsKeyPressed(ImGuiKey_Space) || IsKeyPressed(ImGuiKey_Enter) || IsKeyPressed(ImGuiKey_NavGamepadActivate) ? NavId : 0 (no repeat)
ImGuiActivateFlags NavActivateFlags;
ImVector NavFocusRoute; // Reversed copy focus scope stack for NavId (should contains NavFocusScopeId). This essentially follow the window->ParentWindowForFocusRoute chain.
ImGuiID NavHighlightActivatedId;
float NavHighlightActivatedTimer;
- ImGuiID NavNextActivateId; // Set by ActivateItem(), queued until next frame.
+ ImGuiID NavNextActivateId; // Set by ActivateItemByID(), queued until next frame.
ImGuiActivateFlags NavNextActivateFlags;
ImGuiInputSource NavInputSource; // Keyboard or Gamepad mode? THIS CAN ONLY BE ImGuiInputSource_Keyboard or ImGuiInputSource_Mouse
ImGuiSelectionUserData NavLastValidSelectionUserData; // Last valid data passed to SetNextItemSelectionUser(), or -1. For current window. Not reset when focusing an item that doesn't have selection data.
ImS8 NavCursorHideFrames;
+ //ImGuiID NavActivateInputId; // Removed in 1.89.4 (July 2023). This is now part of g.NavActivateId and sets g.NavActivateFlags |= ImGuiActivateFlags_PreferInput. See commit c9a53aa74, issue #5606.
// Navigation: Init & Move Requests
bool NavAnyRequest; // ~~ NavMoveRequest || NavInitRequest this is to perform early out in ItemAdd()
@@ -2454,8 +2558,8 @@ struct ImGuiContext
float NavWindowingTimer;
float NavWindowingHighlightAlpha;
ImGuiInputSource NavWindowingInputSource;
- bool NavWindowingToggleLayer;
- ImGuiKey NavWindowingToggleKey;
+ bool NavWindowingToggleLayer; // Set while Alt or GamepadMenu is held, may be cleared by other operations, and processed when releasing the key.
+ ImGuiKey NavWindowingToggleKey; // Keyboard/gamepad key used when toggling to menu layer.
ImVec2 NavWindowingAccumDeltaPos;
ImVec2 NavWindowingAccumDeltaSize;
@@ -2473,6 +2577,7 @@ struct ImGuiContext
ImRect DragDropTargetRect; // Store rectangle of current target candidate (we favor small targets when overlapping)
ImRect DragDropTargetClipRect; // Store ClipRect at the time of item's drawing
ImGuiID DragDropTargetId;
+ ImGuiID DragDropTargetFullViewport;
ImGuiDragDropFlags DragDropAcceptFlags;
float DragDropAcceptIdCurrRectSurface; // Target item surface (we resolve overlapping targets by prioritizing the smaller surface)
ImGuiID DragDropAcceptIdCurr; // Target item id (set at the time of accepting the payload)
@@ -2523,8 +2628,10 @@ struct ImGuiContext
// Widget state
ImGuiInputTextState InputTextState;
+ ImGuiTextIndex InputTextLineIndex; // Temporary storage
ImGuiInputTextDeactivatedState InputTextDeactivatedState;
- ImFont InputTextPasswordFont;
+ ImFontBaked InputTextPasswordFontBackupBaked;
+ ImFontFlags InputTextPasswordFontBackupFlags;
ImGuiID TempInputId; // Temporary text input when CTRL+clicking on a slider, etc.
ImGuiDataTypeStorage DataTypeZeroValue; // 0 for all data types
int BeginMenuDepth;
@@ -2556,12 +2663,12 @@ struct ImGuiContext
ImGuiTypingSelectState TypingSelectState; // State for GetTypingSelectRequest()
// Platform support
- ImGuiPlatformImeData PlatformImeData; // Data updated by current frame
+ ImGuiPlatformImeData PlatformImeData; // Data updated by current frame. Will be applied at end of the frame. For some backends, this is required to have WantVisible=true in order to receive text message.
ImGuiPlatformImeData PlatformImeDataPrev; // Previous frame data. When changed we call the platform_io.Platform_SetImeDataFn() handler.
- ImGuiID PlatformImeViewport;
// Extensions
// FIXME: We could provide an API to register one slot in an array held in ImGuiContext?
+ ImVector UserTextures; // List of textures created/managed by user or third-party extension. Automatically appended into platform_io.Textures[].
ImGuiDockContext DockContext;
void (*DockNodeWindowMenuHandler)(ImGuiContext* ctx, ImGuiDockNode* node, ImGuiTabBar* tab_bar);
@@ -2623,6 +2730,10 @@ struct ImGuiContext
ImGuiIDStackTool DebugIDStackTool;
ImGuiDebugAllocInfo DebugAllocInfo;
ImGuiDockNode* DebugHoveredDockNode; // Hovered dock node.
+#if defined(IMGUI_DEBUG_HIGHLIGHT_ALL_ID_CONFLICTS) && !defined(IMGUI_DISABLE_DEBUG_TOOLS)
+ ImGuiStorage DebugDrawIdConflictsAliveCount;
+ ImGuiStorage DebugDrawIdConflictsHighlightSet;
+#endif
// Misc
float FramerateSecPerFrame[60]; // Calculate estimate of framerate for user over the last 60 frames..
@@ -2631,7 +2742,7 @@ struct ImGuiContext
float FramerateSecPerFrameAccum;
int WantCaptureMouseNextFrame; // Explicit capture override via SetNextFrameWantCaptureMouse()/SetNextFrameWantCaptureKeyboard(). Default to -1.
int WantCaptureKeyboardNextFrame; // "
- int WantTextInputNextFrame;
+ int WantTextInputNextFrame; // Copied in EndFrame() from g.PlatformImeData.WantTextInput. Needs to be set for some backends (SDL3) to emit character inputs.
ImVector TempBuffer; // Temporary text buffer
char TempKeychordName[64];
@@ -2677,7 +2788,8 @@ struct IMGUI_API ImGuiWindowTempData
ImVec2 MenuBarOffset; // MenuBarOffset.x is sort of equivalent of a per-layer CursorPos.x, saved/restored as we switch to the menu bar. The only situation when MenuBarOffset.y is > 0 if when (SafeAreaPadding.y > FramePadding.y), often used on TVs.
ImGuiMenuColumns MenuColumns; // Simplified columns storage for menu items measurement
int TreeDepth; // Current tree depth.
- ImU32 TreeHasStackDataDepthMask; // Store whether given depth has ImGuiTreeNodeStackData data. Could be turned into a ImU64 if necessary.
+ ImU32 TreeHasStackDataDepthMask; // Store whether given depth has ImGuiTreeNodeStackData data. Could be turned into a ImU64 if necessary.
+ ImU32 TreeRecordsClippedNodesY2Mask; // Store whether we should keep recording Y2. Cleared when passing clip max. Equivalent TreeHasStackDataDepthMask value should always be set.
ImVector ChildWindows;
ImGuiStorage* StateStorage; // Current persistent per-window storage (store e.g. tree node open/close state)
ImGuiOldColumns* CurrentColumns; // Current columns set
@@ -2797,7 +2909,6 @@ struct IMGUI_API ImGuiWindow
ImVector ColumnsStorage;
float FontWindowScale; // User scale multiplier per-window, via SetWindowFontScale()
float FontWindowScaleParents;
- float FontDpiScale;
float FontRefSize; // This is a copy of window->CalcFontSize() at the time of Begin(), trying to phase out CalcFontSize() especially as it may be called on non-current window.
int SettingsOffset; // Offset into SettingsWindows[] (offsets are always valid as we only grow the array from the back)
@@ -2845,9 +2956,11 @@ public:
// We don't use g.FontSize because the window may be != g.CurrentWindow.
ImRect Rect() const { return ImRect(Pos.x, Pos.y, Pos.x + Size.x, Pos.y + Size.y); }
- float CalcFontSize() const { ImGuiContext& g = *Ctx; return g.FontBaseSize * FontWindowScale * FontDpiScale * FontWindowScaleParents; }
ImRect TitleBarRect() const { return ImRect(Pos, ImVec2(Pos.x + SizeFull.x, Pos.y + TitleBarHeight)); }
ImRect MenuBarRect() const { float y1 = Pos.y + TitleBarHeight; return ImRect(Pos.x, y1, Pos.x + SizeFull.x, y1 + MenuBarHeight); }
+
+ // [Obsolete] ImGuiWindow::CalcFontSize() was removed in 1.92.x because error-prone/misleading. You can use window->FontRefSize for a copy of g.FontSize at the time of the last Begin() call for this window.
+ //float CalcFontSize() const { ImGuiContext& g = *Ctx; return g.FontSizeBase * FontWindowScale * FontDpiScale * FontWindowScaleParents;
};
//-----------------------------------------------------------------------------
@@ -2882,7 +2995,7 @@ struct ImGuiTabItem
int LastFrameSelected; // This allows us to infer an ordered list of the last activated tabs with little maintenance
float Offset; // Position relative to beginning of tab
float Width; // Width currently displayed
- float ContentWidth; // Width of label, stored during BeginTabItem() call
+ float ContentWidth; // Width of label + padding, stored during BeginTabItem() call (misnamed as "Content" would normally imply width of label only)
float RequestedWidth; // Width optionally requested by caller, -1.0f is unused
ImS32 NameOffset; // When Window==NULL, offset to name within parent ImGuiTabBar::TabsNames
ImS16 BeginOrder; // BeginTabItem() order, used to re-order tabs after toggling ImGuiTabBarFlags_Reorderable
@@ -2905,6 +3018,7 @@ struct IMGUI_API ImGuiTabBar
int CurrFrameVisible;
int PrevFrameVisible;
ImRect BarRect;
+ float BarRectPrevWidth; // Backup of previous width. When width change we enforce keep horizontal scroll on focused tab.
float CurrTabsContentsHeight;
float PrevTabsContentsHeight; // Record the height of contents submitted below the tab bar
float WidthAllTabs; // Actual width of all tabs (locked during layout)
@@ -2923,6 +3037,7 @@ struct IMGUI_API ImGuiTabBar
bool WantLayout;
bool VisibleTabWasSubmitted;
bool TabsAddedNew; // Set to true when a new tab item or button has been added to the tab bar during last frame
+ bool ScrollButtonEnabled;
ImS16 TabsActiveCount; // Number of tabs submitted this frame.
ImS16 LastTabItemIdx; // Index of last BeginTabItem() tab for use by EndTabItem()
float ItemSpacingY;
@@ -2938,11 +3053,7 @@ struct IMGUI_API ImGuiTabBar
//-----------------------------------------------------------------------------
#define IM_COL32_DISABLE IM_COL32(0,0,0,1) // Special sentinel code which cannot be used as a regular color.
-#define IMGUI_TABLE_MAX_COLUMNS 512 // May be further lifted
-
-// Our current column maximum is 64 but we may raise that in the future.
-typedef ImS16 ImGuiTableColumnIdx;
-typedef ImU16 ImGuiTableDrawChannelIdx;
+#define IMGUI_TABLE_MAX_COLUMNS 512 // Arbitrary "safety" maximum, may be lifted in the future if needed. Must fit in ImGuiTableColumnIdx/ImGuiTableDrawChannelIdx.
// [Internal] sizeof() ~ 112
// We use the terminology "Enabled" to refer to a column that is not Hidden by user/api.
@@ -3141,7 +3252,7 @@ struct IMGUI_API ImGuiTable
bool IsSortSpecsDirty;
bool IsUsingHeaders; // Set when the first row had the ImGuiTableRowFlags_Headers flag.
bool IsContextPopupOpen; // Set when default context menu is open (also see: ContextPopupColumn, InstanceInteracted).
- bool DisableDefaultContextMenu; // Disable default context menu contents. You may submit your own using TableBeginContextMenuPopup()/EndPopup()
+ bool DisableDefaultContextMenu; // Disable default context menu. You may submit your own using TableBeginContextMenuPopup()/EndPopup()
bool IsSettingsRequestLoad;
bool IsSettingsDirty; // Set when table settings have changed and needs to be reported into ImGuiTableSetttings data.
bool IsDefaultDisplayOrder; // Set when display order is unchanged from default (DisplayOrder contains 0...Count-1)
@@ -3275,9 +3386,18 @@ namespace ImGui
IMGUI_API void SetNextWindowRefreshPolicy(ImGuiWindowRefreshFlags flags);
// Fonts, drawing
- IMGUI_API void SetCurrentFont(ImFont* font);
- inline ImFont* GetDefaultFont() { ImGuiContext& g = *GImGui; return g.IO.FontDefault ? g.IO.FontDefault : g.IO.Fonts->Fonts[0]; }
+ IMGUI_API void RegisterUserTexture(ImTextureData* tex); // Register external texture. EXPERIMENTAL: DO NOT USE YET.
+ IMGUI_API void UnregisterUserTexture(ImTextureData* tex);
+ IMGUI_API void RegisterFontAtlas(ImFontAtlas* atlas);
+ IMGUI_API void UnregisterFontAtlas(ImFontAtlas* atlas);
+ IMGUI_API void SetCurrentFont(ImFont* font, float font_size_before_scaling, float font_size_after_scaling);
+ IMGUI_API void UpdateCurrentFontSize(float restore_font_size_after_scaling);
+ IMGUI_API void SetFontRasterizerDensity(float rasterizer_density);
+ inline float GetFontRasterizerDensity() { return GImGui->FontRasterizerDensity; }
+ inline float GetRoundedFontSize(float size) { return IM_ROUND(size); }
+ IMGUI_API ImFont* GetDefaultFont();
IMGUI_API void PushPasswordFont();
+ IMGUI_API void PopPasswordFont();
inline ImDrawList* GetForegroundDrawList(ImGuiWindow* window) { return GetForegroundDrawList(window->Viewport); }
IMGUI_API void AddDrawListToDrawDataEx(ImDrawData* draw_data, ImVector* out_list, ImDrawList* draw_list);
@@ -3291,6 +3411,7 @@ namespace ImGui
IMGUI_API void FindHoveredWindowEx(const ImVec2& pos, bool find_first_and_in_any_viewport, ImGuiWindow** out_hovered_window, ImGuiWindow** out_hovered_window_under_moving_window);
IMGUI_API void StartMouseMovingWindow(ImGuiWindow* window);
IMGUI_API void StartMouseMovingWindowOrNode(ImGuiWindow* window, ImGuiDockNode* node, bool undock);
+ IMGUI_API void StopMouseMovingWindow();
IMGUI_API void UpdateMouseMovingWindowNewFrame();
IMGUI_API void UpdateMouseMovingWindowEndFrame();
@@ -3367,7 +3488,8 @@ namespace ImGui
IMGUI_API ImVec2 CalcItemSize(ImVec2 size, float default_w, float default_h);
IMGUI_API float CalcWrapWidthForPos(const ImVec2& pos, float wrap_pos_x);
IMGUI_API void PushMultiItemsWidths(int components, float width_full);
- IMGUI_API void ShrinkWidths(ImGuiShrinkWidthItem* items, int count, float width_excess);
+ IMGUI_API void ShrinkWidths(ImGuiShrinkWidthItem* items, int count, float width_excess, float width_min);
+ IMGUI_API void CalcClipRectVisibleItemsY(const ImRect& clip_rect, const ImVec2& pos, float items_height, int* out_visible_start, int* out_visible_end);
// Parameter stacks (shared)
IMGUI_API const ImGuiStyleVarInfo* GetStyleVarInfo(ImGuiStyleVar idx);
@@ -3419,7 +3541,7 @@ namespace ImGui
IMGUI_API void NavMoveRequestSubmit(ImGuiDir move_dir, ImGuiDir clip_dir, ImGuiNavMoveFlags move_flags, ImGuiScrollFlags scroll_flags);
IMGUI_API void NavMoveRequestForward(ImGuiDir move_dir, ImGuiDir clip_dir, ImGuiNavMoveFlags move_flags, ImGuiScrollFlags scroll_flags);
IMGUI_API void NavMoveRequestResolveWithLastItem(ImGuiNavItemData* result);
- IMGUI_API void NavMoveRequestResolveWithPastTreeNode(ImGuiNavItemData* result, ImGuiTreeNodeStackData* tree_node_data);
+ IMGUI_API void NavMoveRequestResolveWithPastTreeNode(ImGuiNavItemData* result, const ImGuiTreeNodeStackData* tree_node_data);
IMGUI_API void NavMoveRequestCancel();
IMGUI_API void NavMoveRequestApplyResult();
IMGUI_API void NavMoveRequestTryWrapping(ImGuiWindow* window, ImGuiNavMoveFlags move_flags);
@@ -3435,7 +3557,7 @@ namespace ImGui
// This should be part of a larger set of API: FocusItem(offset = -1), FocusItemByID(id), ActivateItem(offset = -1), ActivateItemByID(id) etc. which are
// much harder to design and implement than expected. I have a couple of private branches on this matter but it's not simple. For now implementing the easy ones.
IMGUI_API void FocusItem(); // Focus last item (no selection/activation).
- IMGUI_API void ActivateItemByID(ImGuiID id); // Activate an item by ID (button, checkbox, tree node etc.). Activation is queued and processed on the next frame when the item is encountered again.
+ IMGUI_API void ActivateItemByID(ImGuiID id); // Activate an item by ID (button, checkbox, tree node etc.). Activation is queued and processed on the next frame when the item is encountered again. Was called 'ActivateItem()' before 1.89.7.
// Inputs
// FIXME: Eventually we should aim to move e.g. IsActiveIdUsingKey() into IsKeyXXX functions.
@@ -3592,9 +3714,11 @@ namespace ImGui
// Drag and Drop
IMGUI_API bool IsDragDropActive();
IMGUI_API bool BeginDragDropTargetCustom(const ImRect& bb, ImGuiID id);
+ IMGUI_API bool BeginDragDropTargetViewport(ImGuiViewport* viewport, const ImRect* p_bb = NULL);
IMGUI_API void ClearDragDrop();
IMGUI_API bool IsDragDropPayloadBeingAccepted();
- IMGUI_API void RenderDragDropTargetRect(const ImRect& bb, const ImRect& item_clip_rect);
+ IMGUI_API void RenderDragDropTargetRectForItem(const ImRect& bb);
+ IMGUI_API void RenderDragDropTargetRectEx(ImDrawList* draw_list, const ImRect& bb);
// Typing-Select API
// (provide Windows Explorer style "select items by typing partial name" + "cycle through items by typing same letter" feature)
@@ -3637,6 +3761,8 @@ namespace ImGui
IMGUI_API float TableGetHeaderAngledMaxLabelWidth();
IMGUI_API void TablePushBackgroundChannel();
IMGUI_API void TablePopBackgroundChannel();
+ IMGUI_API void TablePushColumnChannel(int column_n);
+ IMGUI_API void TablePopColumnChannel();
IMGUI_API void TableAngledHeadersRowEx(ImGuiID row_id, float angle, float max_label_width, const ImGuiTableHeaderData* data, int data_count);
// Tables: Internals
@@ -3686,6 +3812,8 @@ namespace ImGui
// Tab Bars
inline ImGuiTabBar* GetCurrentTabBar() { ImGuiContext& g = *GImGui; return g.CurrentTabBar; }
+ IMGUI_API ImGuiTabBar* TabBarFindByID(ImGuiID id);
+ IMGUI_API void TabBarRemove(ImGuiTabBar* tab_bar);
IMGUI_API bool BeginTabBarEx(ImGuiTabBar* tab_bar, const ImRect& bb, ImGuiTabBarFlags flags);
IMGUI_API ImGuiTabItem* TabBarFindTabByID(ImGuiTabBar* tab_bar, ImGuiID tab_id);
IMGUI_API ImGuiTabItem* TabBarFindTabByOrder(ImGuiTabBar* tab_bar, int order);
@@ -3715,7 +3843,7 @@ namespace ImGui
IMGUI_API void RenderTextWrapped(ImVec2 pos, const char* text, const char* text_end, float wrap_width);
IMGUI_API void RenderTextClipped(const ImVec2& pos_min, const ImVec2& pos_max, const char* text, const char* text_end, const ImVec2* text_size_if_known, const ImVec2& align = ImVec2(0, 0), const ImRect* clip_rect = NULL);
IMGUI_API void RenderTextClippedEx(ImDrawList* draw_list, const ImVec2& pos_min, const ImVec2& pos_max, const char* text, const char* text_end, const ImVec2* text_size_if_known, const ImVec2& align = ImVec2(0, 0), const ImRect* clip_rect = NULL);
- IMGUI_API void RenderTextEllipsis(ImDrawList* draw_list, const ImVec2& pos_min, const ImVec2& pos_max, float clip_max_x, float ellipsis_max_x, const char* text, const char* text_end, const ImVec2* text_size_if_known);
+ IMGUI_API void RenderTextEllipsis(ImDrawList* draw_list, const ImVec2& pos_min, const ImVec2& pos_max, float ellipsis_max_x, const char* text, const char* text_end, const ImVec2* text_size_if_known);
IMGUI_API void RenderFrame(ImVec2 p_min, ImVec2 p_max, ImU32 fill_col, bool borders = true, float rounding = 0.0f);
IMGUI_API void RenderFrameBorder(ImVec2 p_min, ImVec2 p_max, float rounding = 0.0f);
IMGUI_API void RenderColorRectWithAlphaCheckerboard(ImDrawList* draw_list, ImVec2 p_min, ImVec2 p_max, ImU32 fill_col, float grid_step, ImVec2 grid_off, float rounding = 0.0f, ImDrawFlags flags = 0);
@@ -3736,11 +3864,15 @@ namespace ImGui
IMGUI_API void RenderRectFilledWithHole(ImDrawList* draw_list, const ImRect& outer, const ImRect& inner, ImU32 col, float rounding);
IMGUI_API ImDrawFlags CalcRoundingFlagsForRectInRect(const ImRect& r_in, const ImRect& r_outer, float threshold);
- // Widgets
+ // Widgets: Text
IMGUI_API void TextEx(const char* text, const char* text_end = NULL, ImGuiTextFlags flags = 0);
+ IMGUI_API void TextAligned(float align_x, float size_x, const char* fmt, ...); // FIXME-WIP: Works but API is likely to be reworked. This is designed for 1 item on the line. (#7024)
+ IMGUI_API void TextAlignedV(float align_x, float size_x, const char* fmt, va_list args);
+
+ // Widgets
IMGUI_API bool ButtonEx(const char* label, const ImVec2& size_arg = ImVec2(0, 0), ImGuiButtonFlags flags = 0);
IMGUI_API bool ArrowButtonEx(const char* str_id, ImGuiDir dir, ImVec2 size_arg, ImGuiButtonFlags flags = 0);
- IMGUI_API bool ImageButtonEx(ImGuiID id, ImTextureID user_texture_id, const ImVec2& image_size, const ImVec2& uv0, const ImVec2& uv1, const ImVec4& bg_col, const ImVec4& tint_col, ImGuiButtonFlags flags = 0);
+ IMGUI_API bool ImageButtonEx(ImGuiID id, ImTextureRef tex_ref, const ImVec2& image_size, const ImVec2& uv0, const ImVec2& uv1, const ImVec4& bg_col, const ImVec4& tint_col, ImGuiButtonFlags flags = 0);
IMGUI_API void SeparatorEx(ImGuiSeparatorFlags flags, float thickness = 1.0f);
IMGUI_API void SeparatorTextEx(ImGuiID id, const char* label, const char* label_end, float extra_width);
IMGUI_API bool CheckboxFlags(const char* label, ImS64* flags, ImS64 flags_value);
@@ -3764,6 +3896,8 @@ namespace ImGui
// Widgets: Tree Nodes
IMGUI_API bool TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* label, const char* label_end = NULL);
+ IMGUI_API void TreeNodeDrawLineToChildNode(const ImVec2& target_pos);
+ IMGUI_API void TreeNodeDrawLineToTreePop(const ImGuiTreeNodeStackData* data);
IMGUI_API void TreePushOverrideID(ImGuiID id);
IMGUI_API bool TreeNodeGetOpen(ImGuiID storage_id);
IMGUI_API void TreeNodeSetOpen(ImGuiID storage_id, bool open);
@@ -3845,7 +3979,9 @@ namespace ImGui
IMGUI_API void DebugNodeDrawList(ImGuiWindow* window, ImGuiViewportP* viewport, const ImDrawList* draw_list, const char* label);
IMGUI_API void DebugNodeDrawCmdShowMeshAndBoundingBox(ImDrawList* out_draw_list, const ImDrawList* draw_list, const ImDrawCmd* draw_cmd, bool show_mesh, bool show_aabb);
IMGUI_API void DebugNodeFont(ImFont* font);
+ IMGUI_API void DebugNodeFontGlyphesForSrcMask(ImFont* font, ImFontBaked* baked, int src_mask);
IMGUI_API void DebugNodeFontGlyph(ImFont* font, const ImFontGlyph* glyph);
+ IMGUI_API void DebugNodeTexture(ImTextureData* tex, int int_id, const ImFontAtlasRect* highlight_rect = NULL); // ID used to facilitate persisting the "current" texture.
IMGUI_API void DebugNodeStorage(ImGuiStorage* storage, const char* label);
IMGUI_API void DebugNodeTabBar(ImGuiTabBar* tab_bar, const char* label);
IMGUI_API void DebugNodeTable(ImGuiTable* table);
@@ -3879,31 +4015,192 @@ namespace ImGui
} // namespace ImGui
+//-----------------------------------------------------------------------------
+// [SECTION] ImFontLoader
+//-----------------------------------------------------------------------------
+
+// Hooks and storage for a given font backend.
+// This structure is likely to evolve as we add support for incremental atlas updates.
+// Conceptually this could be public, but API is still going to be evolve.
+struct ImFontLoader
+{
+ const char* Name;
+ bool (*LoaderInit)(ImFontAtlas* atlas);
+ void (*LoaderShutdown)(ImFontAtlas* atlas);
+ bool (*FontSrcInit)(ImFontAtlas* atlas, ImFontConfig* src);
+ void (*FontSrcDestroy)(ImFontAtlas* atlas, ImFontConfig* src);
+ bool (*FontSrcContainsGlyph)(ImFontAtlas* atlas, ImFontConfig* src, ImWchar codepoint);
+ bool (*FontBakedInit)(ImFontAtlas* atlas, ImFontConfig* src, ImFontBaked* baked, void* loader_data_for_baked_src);
+ void (*FontBakedDestroy)(ImFontAtlas* atlas, ImFontConfig* src, ImFontBaked* baked, void* loader_data_for_baked_src);
+ bool (*FontBakedLoadGlyph)(ImFontAtlas* atlas, ImFontConfig* src, ImFontBaked* baked, void* loader_data_for_baked_src, ImWchar codepoint, ImFontGlyph* out_glyph, float* out_advance_x);
+
+ // Size of backend data, Per Baked * Per Source. Buffers are managed by core to avoid excessive allocations.
+ // FIXME: At this point the two other types of buffers may be managed by core to be consistent?
+ size_t FontBakedSrcLoaderDataSize;
+
+ ImFontLoader() { memset(this, 0, sizeof(*this)); }
+};
+
+#ifdef IMGUI_ENABLE_STB_TRUETYPE
+IMGUI_API const ImFontLoader* ImFontAtlasGetFontLoaderForStbTruetype();
+#endif
+#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
+typedef ImFontLoader ImFontBuilderIO; // [renamed/changed in 1.92] The types are not actually compatible but we provide this as a compile-time error report helper.
+#endif
+
//-----------------------------------------------------------------------------
// [SECTION] ImFontAtlas internal API
//-----------------------------------------------------------------------------
-// This structure is likely to evolve as we add support for incremental atlas updates.
-// Conceptually this could be in ImGuiPlatformIO, but we are far from ready to make this public.
-struct ImFontBuilderIO
+#define IMGUI_FONT_SIZE_MAX (512.0f)
+#define IMGUI_FONT_SIZE_THRESHOLD_FOR_LOADADVANCEXONLYMODE (128.0f)
+
+// Helpers: ImTextureRef ==/!= operators provided as convenience
+// (note that _TexID and _TexData are never set simultaneously)
+inline bool operator==(const ImTextureRef& lhs, const ImTextureRef& rhs) { return lhs._TexID == rhs._TexID && lhs._TexData == rhs._TexData; }
+inline bool operator!=(const ImTextureRef& lhs, const ImTextureRef& rhs) { return lhs._TexID != rhs._TexID || lhs._TexData != rhs._TexData; }
+
+// Refer to ImFontAtlasPackGetRect() to better understand how this works.
+#define ImFontAtlasRectId_IndexMask_ (0x0007FFFF) // 20-bits signed: index to access builder->RectsIndex[].
+#define ImFontAtlasRectId_GenerationMask_ (0x3FF00000) // 10-bits: entry generation, so each ID is unique and get can safely detected old identifiers.
+#define ImFontAtlasRectId_GenerationShift_ (20)
+inline int ImFontAtlasRectId_GetIndex(ImFontAtlasRectId id) { return (id & ImFontAtlasRectId_IndexMask_); }
+inline unsigned int ImFontAtlasRectId_GetGeneration(ImFontAtlasRectId id) { return (unsigned int)(id & ImFontAtlasRectId_GenerationMask_) >> ImFontAtlasRectId_GenerationShift_; }
+inline ImFontAtlasRectId ImFontAtlasRectId_Make(int index_idx, int gen_idx) { IM_ASSERT(index_idx >= 0 && index_idx <= ImFontAtlasRectId_IndexMask_ && gen_idx <= (ImFontAtlasRectId_GenerationMask_ >> ImFontAtlasRectId_GenerationShift_)); return (ImFontAtlasRectId)(index_idx | (gen_idx << ImFontAtlasRectId_GenerationShift_)); }
+
+// Packed rectangle lookup entry (we need an indirection to allow removing/reordering rectangles)
+// User are returned ImFontAtlasRectId values which are meant to be persistent.
+// We handle this with an indirection. While Rects[] may be in theory shuffled, compacted etc., RectsIndex[] cannot it is keyed by ImFontAtlasRectId.
+// RectsIndex[] is used both as an index into Rects[] and an index into itself. This is basically a free-list. See ImFontAtlasBuildAllocRectIndexEntry() code.
+// Having this also makes it easier to e.g. sort rectangles during repack.
+struct ImFontAtlasRectEntry
{
- bool (*FontBuilder_Build)(ImFontAtlas* atlas);
+ int TargetIndex : 20; // When Used: ImFontAtlasRectId -> into Rects[]. When unused: index to next unused RectsIndex[] slot to consume free-list.
+ unsigned int Generation : 10; // Increased each time the entry is reused for a new rectangle.
+ unsigned int IsUsed : 1;
};
-// Helper for font builder
-#ifdef IMGUI_ENABLE_STB_TRUETYPE
-IMGUI_API const ImFontBuilderIO* ImFontAtlasGetBuilderForStbTruetype();
+// Data available to potential texture post-processing functions
+struct ImFontAtlasPostProcessData
+{
+ ImFontAtlas* FontAtlas;
+ ImFont* Font;
+ ImFontConfig* FontSrc;
+ ImFontBaked* FontBaked;
+ ImFontGlyph* Glyph;
+
+ // Pixel data
+ void* Pixels;
+ ImTextureFormat Format;
+ int Pitch;
+ int Width;
+ int Height;
+};
+
+// We avoid dragging imstb_rectpack.h into public header (partly because binding generators are having issues with it)
+#ifdef IMGUI_STB_NAMESPACE
+namespace IMGUI_STB_NAMESPACE { struct stbrp_node; }
+typedef IMGUI_STB_NAMESPACE::stbrp_node stbrp_node_im;
+#else
+struct stbrp_node;
+typedef stbrp_node stbrp_node_im;
+#endif
+struct stbrp_context_opaque { char data[80]; };
+
+// Internal storage for incrementally packing and building a ImFontAtlas
+struct ImFontAtlasBuilder
+{
+ stbrp_context_opaque PackContext; // Actually 'stbrp_context' but we don't want to define this in the header file.
+ ImVector PackNodes;
+ ImVector Rects;
+ ImVector RectsIndex; // ImFontAtlasRectId -> index into Rects[]
+ ImVector TempBuffer; // Misc scratch buffer
+ int RectsIndexFreeListStart;// First unused entry
+ int RectsPackedCount; // Number of packed rectangles.
+ int RectsPackedSurface; // Number of packed pixels. Used when compacting to heuristically find the ideal texture size.
+ int RectsDiscardedCount;
+ int RectsDiscardedSurface;
+ int FrameCount; // Current frame count
+ ImVec2i MaxRectSize; // Largest rectangle to pack (de-facto used as a "minimum texture size")
+ ImVec2i MaxRectBounds; // Bottom-right most used pixels
+ bool LockDisableResize; // Disable resizing texture
+ bool PreloadedAllGlyphsRanges; // Set when missing ImGuiBackendFlags_RendererHasTextures features forces atlas to preload everything.
+
+ // Cache of all ImFontBaked
+ ImStableVector BakedPool;
+ ImGuiStorage BakedMap; // BakedId --> ImFontBaked*
+ int BakedDiscardedCount;
+
+ // Custom rectangle identifiers
+ ImFontAtlasRectId PackIdMouseCursors; // White pixel + mouse cursors. Also happen to be fallback in case of packing failure.
+ ImFontAtlasRectId PackIdLinesTexData;
+
+ ImFontAtlasBuilder() { memset(this, 0, sizeof(*this)); FrameCount = -1; RectsIndexFreeListStart = -1; PackIdMouseCursors = PackIdLinesTexData = -1; }
+};
+
+IMGUI_API void ImFontAtlasBuildInit(ImFontAtlas* atlas);
+IMGUI_API void ImFontAtlasBuildDestroy(ImFontAtlas* atlas);
+IMGUI_API void ImFontAtlasBuildMain(ImFontAtlas* atlas);
+IMGUI_API void ImFontAtlasBuildSetupFontLoader(ImFontAtlas* atlas, const ImFontLoader* font_loader);
+IMGUI_API void ImFontAtlasBuildUpdatePointers(ImFontAtlas* atlas);
+IMGUI_API void ImFontAtlasBuildRenderBitmapFromString(ImFontAtlas* atlas, int x, int y, int w, int h, const char* in_str, char in_marker_char);
+IMGUI_API void ImFontAtlasBuildClear(ImFontAtlas* atlas); // Clear output and custom rects
+
+IMGUI_API ImTextureData* ImFontAtlasTextureAdd(ImFontAtlas* atlas, int w, int h);
+IMGUI_API void ImFontAtlasTextureMakeSpace(ImFontAtlas* atlas);
+IMGUI_API void ImFontAtlasTextureRepack(ImFontAtlas* atlas, int w, int h);
+IMGUI_API void ImFontAtlasTextureGrow(ImFontAtlas* atlas, int old_w = -1, int old_h = -1);
+IMGUI_API void ImFontAtlasTextureCompact(ImFontAtlas* atlas);
+IMGUI_API ImVec2i ImFontAtlasTextureGetSizeEstimate(ImFontAtlas* atlas);
+
+IMGUI_API void ImFontAtlasBuildSetupFontSpecialGlyphs(ImFontAtlas* atlas, ImFont* font, ImFontConfig* src);
+IMGUI_API void ImFontAtlasBuildLegacyPreloadAllGlyphRanges(ImFontAtlas* atlas); // Legacy
+IMGUI_API void ImFontAtlasBuildGetOversampleFactors(ImFontConfig* src, ImFontBaked* baked, int* out_oversample_h, int* out_oversample_v);
+IMGUI_API void ImFontAtlasBuildDiscardBakes(ImFontAtlas* atlas, int unused_frames);
+
+IMGUI_API bool ImFontAtlasFontSourceInit(ImFontAtlas* atlas, ImFontConfig* src);
+IMGUI_API void ImFontAtlasFontSourceAddToFont(ImFontAtlas* atlas, ImFont* font, ImFontConfig* src);
+IMGUI_API void ImFontAtlasFontDestroySourceData(ImFontAtlas* atlas, ImFontConfig* src);
+IMGUI_API bool ImFontAtlasFontInitOutput(ImFontAtlas* atlas, ImFont* font); // Using FontDestroyOutput/FontInitOutput sequence useful notably if font loader params have changed
+IMGUI_API void ImFontAtlasFontDestroyOutput(ImFontAtlas* atlas, ImFont* font);
+IMGUI_API void ImFontAtlasFontDiscardBakes(ImFontAtlas* atlas, ImFont* font, int unused_frames);
+
+IMGUI_API ImGuiID ImFontAtlasBakedGetId(ImGuiID font_id, float baked_size, float rasterizer_density);
+IMGUI_API ImFontBaked* ImFontAtlasBakedGetOrAdd(ImFontAtlas* atlas, ImFont* font, float font_size, float font_rasterizer_density);
+IMGUI_API ImFontBaked* ImFontAtlasBakedGetClosestMatch(ImFontAtlas* atlas, ImFont* font, float font_size, float font_rasterizer_density);
+IMGUI_API ImFontBaked* ImFontAtlasBakedAdd(ImFontAtlas* atlas, ImFont* font, float font_size, float font_rasterizer_density, ImGuiID baked_id);
+IMGUI_API void ImFontAtlasBakedDiscard(ImFontAtlas* atlas, ImFont* font, ImFontBaked* baked);
+IMGUI_API ImFontGlyph* ImFontAtlasBakedAddFontGlyph(ImFontAtlas* atlas, ImFontBaked* baked, ImFontConfig* src, const ImFontGlyph* in_glyph);
+IMGUI_API void ImFontAtlasBakedAddFontGlyphAdvancedX(ImFontAtlas* atlas, ImFontBaked* baked, ImFontConfig* src, ImWchar codepoint, float advance_x);
+IMGUI_API void ImFontAtlasBakedDiscardFontGlyph(ImFontAtlas* atlas, ImFont* font, ImFontBaked* baked, ImFontGlyph* glyph);
+IMGUI_API void ImFontAtlasBakedSetFontGlyphBitmap(ImFontAtlas* atlas, ImFontBaked* baked, ImFontConfig* src, ImFontGlyph* glyph, ImTextureRect* r, const unsigned char* src_pixels, ImTextureFormat src_fmt, int src_pitch);
+
+IMGUI_API void ImFontAtlasPackInit(ImFontAtlas* atlas);
+IMGUI_API ImFontAtlasRectId ImFontAtlasPackAddRect(ImFontAtlas* atlas, int w, int h, ImFontAtlasRectEntry* overwrite_entry = NULL);
+IMGUI_API ImTextureRect* ImFontAtlasPackGetRect(ImFontAtlas* atlas, ImFontAtlasRectId id);
+IMGUI_API ImTextureRect* ImFontAtlasPackGetRectSafe(ImFontAtlas* atlas, ImFontAtlasRectId id);
+IMGUI_API void ImFontAtlasPackDiscardRect(ImFontAtlas* atlas, ImFontAtlasRectId id);
+
+IMGUI_API void ImFontAtlasUpdateNewFrame(ImFontAtlas* atlas, int frame_count, bool renderer_has_textures);
+IMGUI_API void ImFontAtlasAddDrawListSharedData(ImFontAtlas* atlas, ImDrawListSharedData* data);
+IMGUI_API void ImFontAtlasRemoveDrawListSharedData(ImFontAtlas* atlas, ImDrawListSharedData* data);
+IMGUI_API void ImFontAtlasUpdateDrawListsTextures(ImFontAtlas* atlas, ImTextureRef old_tex, ImTextureRef new_tex);
+IMGUI_API void ImFontAtlasUpdateDrawListsSharedData(ImFontAtlas* atlas);
+
+IMGUI_API void ImFontAtlasTextureBlockConvert(const unsigned char* src_pixels, ImTextureFormat src_fmt, int src_pitch, unsigned char* dst_pixels, ImTextureFormat dst_fmt, int dst_pitch, int w, int h);
+IMGUI_API void ImFontAtlasTextureBlockPostProcess(ImFontAtlasPostProcessData* data);
+IMGUI_API void ImFontAtlasTextureBlockPostProcessMultiply(ImFontAtlasPostProcessData* data, float multiply_factor);
+IMGUI_API void ImFontAtlasTextureBlockFill(ImTextureData* dst_tex, int dst_x, int dst_y, int w, int h, ImU32 col);
+IMGUI_API void ImFontAtlasTextureBlockCopy(ImTextureData* src_tex, int src_x, int src_y, ImTextureData* dst_tex, int dst_x, int dst_y, int w, int h);
+IMGUI_API void ImFontAtlasTextureBlockQueueUpload(ImFontAtlas* atlas, ImTextureData* tex, int x, int y, int w, int h);
+
+IMGUI_API int ImTextureDataGetFormatBytesPerPixel(ImTextureFormat format);
+IMGUI_API const char* ImTextureDataGetStatusName(ImTextureStatus status);
+IMGUI_API const char* ImTextureDataGetFormatName(ImTextureFormat format);
+
+#ifndef IMGUI_DISABLE_DEBUG_TOOLS
+IMGUI_API void ImFontAtlasDebugLogTextureRequests(ImFontAtlas* atlas);
#endif
-IMGUI_API void ImFontAtlasUpdateSourcesPointers(ImFontAtlas* atlas);
-IMGUI_API void ImFontAtlasBuildInit(ImFontAtlas* atlas);
-IMGUI_API void ImFontAtlasBuildSetupFont(ImFontAtlas* atlas, ImFont* font, ImFontConfig* src, float ascent, float descent);
-IMGUI_API void ImFontAtlasBuildPackCustomRects(ImFontAtlas* atlas, void* stbrp_context_opaque);
-IMGUI_API void ImFontAtlasBuildFinish(ImFontAtlas* atlas);
-IMGUI_API void ImFontAtlasBuildRender8bppRectFromString(ImFontAtlas* atlas, int x, int y, int w, int h, const char* in_str, char in_marker_char, unsigned char in_marker_pixel_value);
-IMGUI_API void ImFontAtlasBuildRender32bppRectFromString(ImFontAtlas* atlas, int x, int y, int w, int h, const char* in_str, char in_marker_char, unsigned int in_marker_pixel_value);
-IMGUI_API void ImFontAtlasBuildMultiplyCalcLookupTable(unsigned char out_table[256], float in_multiply_factor);
-IMGUI_API void ImFontAtlasBuildMultiplyRectAlpha8(const unsigned char table[256], unsigned char* pixels, int x, int y, int w, int h, int stride);
-IMGUI_API void ImFontAtlasBuildGetOversampleFactors(const ImFontConfig* src, int* out_oversample_h, int* out_oversample_v);
IMGUI_API bool ImFontAtlasGetMouseCursorTexData(ImFontAtlas* atlas, ImGuiMouseCursor cursor_type, ImVec2* out_offset, ImVec2* out_size, ImVec2 out_uv_border[2], ImVec2 out_uv_fill[2]);
diff --git a/imgui_tables.cpp b/imgui_tables.cpp
index 0954784bb..af0e8c6cf 100644
--- a/imgui_tables.cpp
+++ b/imgui_tables.cpp
@@ -1,4 +1,4 @@
-// dear imgui, v1.92.0 WIP
+// dear imgui, v1.92.4 WIP
// (tables and columns code)
/*
@@ -437,7 +437,7 @@ bool ImGui::BeginTableEx(const char* name, ImGuiID id, int columns_count, ImG
if (table->InnerWindow->SkipItems && outer_window_is_measuring_size)
table->InnerWindow->SkipItems = false;
- // When using multiple instances, ensure they have the same amount of horizontal decorations (aka vertical scrollbar) so stretched columns can be aligned)
+ // When using multiple instances, ensure they have the same amount of horizontal decorations (aka vertical scrollbar) so stretched columns can be aligned
if (instance_no == 0)
{
table->HasScrollbarYPrev = table->HasScrollbarYCurr;
@@ -451,6 +451,7 @@ bool ImGui::BeginTableEx(const char* name, ImGuiID id, int columns_count, ImG
// But at this point we do NOT have a correct value for .Max.y (unless a height has been explicitly passed in). It will only be updated in EndTable().
table->WorkRect = table->OuterRect = table->InnerRect = outer_rect;
table->HasScrollbarYPrev = table->HasScrollbarYCurr = false;
+ table->InnerWindow->DC.TreeDepth++; // This is designed to always linking ImGuiTreeNodeFlags_DrawLines linking accross a table
}
// Push a standardized ID for both child-using and not-child-using tables
@@ -541,7 +542,7 @@ bool ImGui::BeginTableEx(const char* name, ImGuiID id, int columns_count, ImG
// Make table current
g.CurrentTable = table;
- outer_window->DC.NavIsScrollPushableX = false; // Shortcut for NavUpdateCurrentWindowIsScrollPushableX();
+ inner_window->DC.NavIsScrollPushableX = false; // Shortcut for NavUpdateCurrentWindowIsScrollPushableX();
outer_window->DC.CurrentTableIdx = table_idx;
if (inner_window != outer_window) // So EndChild() within the inner window can restore the table properly.
inner_window->DC.CurrentTableIdx = table_idx;
@@ -1250,7 +1251,7 @@ void ImGui::TableUpdateLayout(ImGuiTable* table)
// [Part 11] Default context menu
// - To append to this menu: you can call TableBeginContextMenuPopup()/.../EndPopup().
- // - To modify or replace this: set table->IsContextPopupNoDefaultContents = true, then call TableBeginContextMenuPopup()/.../EndPopup().
+ // - To modify or replace this: set table->DisableDefaultContextMenu = true, then call TableBeginContextMenuPopup()/.../EndPopup().
// - You may call TableDrawDefaultContextMenu() with selected flags to display specific sections of the default menu,
// e.g. TableDrawDefaultContextMenu(table, table->Flags & ~ImGuiTableFlags_Hideable) will display everything EXCEPT columns visibility options.
if (table->DisableDefaultContextMenu == false && TableBeginContextMenuPopup(table))
@@ -1510,6 +1511,7 @@ void ImGui::EndTable()
}
else
{
+ table->InnerWindow->DC.TreeDepth--;
ItemSize(table->OuterRect.GetSize());
ItemAdd(table->OuterRect, 0);
}
@@ -1823,6 +1825,11 @@ void ImGui::TableSetBgColor(ImGuiTableBgTarget target, ImU32 color, int column_n
ImGuiContext& g = *GImGui;
ImGuiTable* table = g.CurrentTable;
IM_ASSERT(target != ImGuiTableBgTarget_None);
+ if (table == NULL)
+ {
+ IM_ASSERT_USER_ERROR(table != NULL, "Call should only be done while in BeginTable() scope!");
+ return;
+ }
if (color == IM_COL32_DISABLE)
color = 0;
@@ -1951,7 +1958,10 @@ void ImGui::TableEndRow(ImGuiTable* table)
IM_ASSERT(table->IsInsideRow);
if (table->CurrentColumn != -1)
+ {
TableEndCell(table);
+ table->CurrentColumn = -1;
+ }
// Logging
if (g.LogEnabled)
@@ -2044,10 +2054,11 @@ void ImGui::TableEndRow(ImGuiTable* table)
}
// End frozen rows (when we are past the last frozen row line, teleport cursor and alter clipping rectangle)
- // We need to do that in TableEndRow() instead of TableBeginRow() so the list clipper can mark end of row and
- // get the new cursor position.
+ // - We need to do that in TableEndRow() instead of TableBeginRow() so the list clipper can mark
+ // end of row and get the new cursor position.
if (unfreeze_rows_request)
{
+ IM_ASSERT(table->FreezeRowsRequest > 0);
for (int column_n = 0; column_n < table->ColumnsCount; column_n++)
table->Columns[column_n].NavLayerCurrent = table->NavLayer;
const float y0 = ImMax(table->RowPosY2 + 1, table->InnerClipRect.Min.y);
@@ -2191,6 +2202,7 @@ void ImGui::TableBeginCell(ImGuiTable* table, int column_n)
g.LastItemData.StatusFlags = 0;
}
+ // Also see TablePushColumnChannel()
if (table->Flags & ImGuiTableFlags_NoClip)
{
// FIXME: if we end up drawing all borders/bg in EndTable, could remove this and just assert that channel hasn't changed.
@@ -2464,10 +2476,38 @@ void ImGui::TablePopBackgroundChannel()
ImGuiContext& g = *GImGui;
ImGuiWindow* window = g.CurrentWindow;
ImGuiTable* table = g.CurrentTable;
- ImGuiTableColumn* column = &table->Columns[table->CurrentColumn];
// Optimization: avoid PopClipRect() + SetCurrentChannel()
SetWindowClipRectBeforeSetChannel(window, table->HostBackupInnerClipRect);
+ table->DrawSplitter->SetCurrentChannel(window->DrawList, table->Columns[table->CurrentColumn].DrawChannelCurrent);
+}
+
+// Also see TableBeginCell()
+void ImGui::TablePushColumnChannel(int column_n)
+{
+ ImGuiContext& g = *GImGui;
+ ImGuiTable* table = g.CurrentTable;
+
+ // Optimization: avoid SetCurrentChannel() + PushClipRect()
+ if (table->Flags & ImGuiTableFlags_NoClip)
+ return;
+ ImGuiWindow* window = g.CurrentWindow;
+ const ImGuiTableColumn* column = &table->Columns[column_n];
+ SetWindowClipRectBeforeSetChannel(window, column->ClipRect);
+ table->DrawSplitter->SetCurrentChannel(window->DrawList, column->DrawChannelCurrent);
+}
+
+void ImGui::TablePopColumnChannel()
+{
+ ImGuiContext& g = *GImGui;
+ ImGuiTable* table = g.CurrentTable;
+
+ // Optimization: avoid PopClipRect() + SetCurrentChannel()
+ if ((table->Flags & ImGuiTableFlags_NoClip) || (table->CurrentColumn == -1)) // Calling TreePop() after TableNextRow() is supported.
+ return;
+ ImGuiWindow* window = g.CurrentWindow;
+ const ImGuiTableColumn* column = &table->Columns[table->CurrentColumn];
+ SetWindowClipRectBeforeSetChannel(window, column->ClipRect);
table->DrawSplitter->SetCurrentChannel(window->DrawList, column->DrawChannelCurrent);
}
@@ -2778,8 +2818,13 @@ void ImGui::TableDrawBorders(ImGuiTable* table)
continue;
// Draw in outer window so right-most column won't be clipped
- // Always draw full height border when being resized/hovered, or on the delimitation of frozen column scrolling.
- float draw_y2 = (is_hovered || is_resized || is_frozen_separator || (table->Flags & (ImGuiTableFlags_NoBordersInBody | ImGuiTableFlags_NoBordersInBodyUntilResize)) == 0) ? draw_y2_body : draw_y2_head;
+ float draw_y2 = draw_y2_head;
+ if (is_frozen_separator)
+ draw_y2 = draw_y2_body;
+ else if ((table->Flags & ImGuiTableFlags_NoBordersInBodyUntilResize) != 0 && (is_hovered || is_resized))
+ draw_y2 = draw_y2_body;
+ else if ((table->Flags & (ImGuiTableFlags_NoBordersInBodyUntilResize | ImGuiTableFlags_NoBordersInBody)) == 0)
+ draw_y2 = draw_y2_body;
if (draw_y2 > draw_y1)
inner_drawlist->AddLine(ImVec2(column->MaxX, draw_y1), ImVec2(column->MaxX, draw_y2), TableGetColumnBorderCol(table, order_n, column_n), border_size);
}
@@ -2842,9 +2887,7 @@ ImGuiTableSortSpecs* ImGui::TableGetSortSpecs()
{
ImGuiContext& g = *GImGui;
ImGuiTable* table = g.CurrentTable;
- IM_ASSERT(table != NULL);
-
- if (!(table->Flags & ImGuiTableFlags_Sortable))
+ if (table == NULL || !(table->Flags & ImGuiTableFlags_Sortable))
return NULL;
// Require layout (in case TableHeadersRow() hasn't been called) as it may alter IsSortSpecsDirty in some paths.
@@ -3244,7 +3287,7 @@ void ImGui::TableHeader(const char* label)
// Render clipped label. Clipping here ensure that in the majority of situations, all our header cells will
// be merged into a single draw call.
//window->DrawList->AddCircleFilled(ImVec2(ellipsis_max, label_pos.y), 40, IM_COL32_WHITE);
- RenderTextEllipsis(window->DrawList, label_pos, ImVec2(ellipsis_max, label_pos.y + label_height + g.Style.FramePadding.y), ellipsis_max, ellipsis_max, label, label_end, &label_size);
+ RenderTextEllipsis(window->DrawList, label_pos, ImVec2(ellipsis_max, bb.Max.y), ellipsis_max, label, label_end, &label_size);
const bool text_clipped = label_size.x > (ellipsis_max - label_pos.x);
if (text_clipped && hovered && g.ActiveId == 0)
@@ -3341,7 +3384,7 @@ void ImGui::TableAngledHeadersRowEx(ImGuiID row_id, float angle, float max_label
ButtonBehavior(row_r, row_id, NULL, NULL);
KeepAliveID(row_id);
- const float ascent_scaled = g.Font->Ascent * g.FontScale; // FIXME: Standardize those scaling factors better
+ const float ascent_scaled = g.FontBaked->Ascent * g.FontBakedScale; // FIXME: Standardize those scaling factors better
const float line_off_for_ascent_x = (ImMax((g.FontSize - ascent_scaled) * 0.5f, 0.0f) / -sin_a) * (flip_label ? -1.0f : 1.0f);
const ImVec2 padding = g.Style.CellPadding; // We will always use swapped component
const ImVec2 align = g.Style.TableAngledHeadersTextAlign;
@@ -3396,7 +3439,7 @@ void ImGui::TableAngledHeadersRowEx(ImGuiID row_id, float angle, float max_label
ImRect clip_r(window->ClipRect.Min, window->ClipRect.Min + ImVec2(clip_width, clip_height));
int vtx_idx_begin = draw_list->_VtxCurrentIdx;
PushStyleColor(ImGuiCol_Text, request->TextColor);
- RenderTextEllipsis(draw_list, clip_r.Min, clip_r.Max, clip_r.Max.x, clip_r.Max.x, label_name, label_name_eol, &label_size);
+ RenderTextEllipsis(draw_list, clip_r.Min, clip_r.Max, clip_r.Max.x, label_name, label_name_eol, &label_size);
PopStyleColor();
int vtx_idx_end = draw_list->_VtxCurrentIdx;
@@ -3905,7 +3948,7 @@ void ImGui::TableSettingsAddSettingsHandler()
// - TableGcCompactSettings() [Internal]
//-------------------------------------------------------------------------
-// Remove Table (currently only used by TestEngine)
+// Remove Table data (currently only used by TestEngine)
void ImGui::TableRemove(ImGuiTable* table)
{
//IMGUI_DEBUG_PRINT("TableRemove() id=0x%08X\n", table->ID);
diff --git a/imgui_widgets.cpp b/imgui_widgets.cpp
index c0645ad78..c5cafbeb4 100644
--- a/imgui_widgets.cpp
+++ b/imgui_widgets.cpp
@@ -1,4 +1,4 @@
-// dear imgui, v1.92.0 WIP
+// dear imgui, v1.92.4 WIP
// (widgets code)
/*
@@ -135,8 +135,7 @@ static const ImU64 IM_U64_MAX = (2ULL * 9223372036854775807LL + 1);
// For InputTextEx()
static bool InputTextFilterCharacter(ImGuiContext* ctx, unsigned int* p_char, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data, bool input_source_is_clipboard = false);
-static int InputTextCalcTextLenAndLineCount(const char* text_begin, const char** out_text_end);
-static ImVec2 InputTextCalcTextSize(ImGuiContext* ctx, const char* text_begin, const char* text_end, const char** remaining = NULL, ImVec2* out_offset = NULL, bool stop_on_new_line = false);
+static ImVec2 InputTextCalcTextSize(ImGuiContext* ctx, const char* text_begin, const char* text_end_display, const char* text_end, const char** out_remaining = NULL, ImVec2* out_offset = NULL, ImDrawTextFlags flags = 0);
//-------------------------------------------------------------------------
// [SECTION] Widgets: Text, etc.
@@ -339,6 +338,46 @@ void ImGui::TextWrappedV(const char* fmt, va_list args)
PopTextWrapPos();
}
+void ImGui::TextAligned(float align_x, float size_x, const char* fmt, ...)
+{
+ va_list args;
+ va_start(args, fmt);
+ TextAlignedV(align_x, size_x, fmt, args);
+ va_end(args);
+}
+
+// align_x: 0.0f = left, 0.5f = center, 1.0f = right.
+// size_x : 0.0f = shortcut for GetContentRegionAvail().x
+// FIXME-WIP: Works but API is likely to be reworked. This is designed for 1 item on the line. (#7024)
+void ImGui::TextAlignedV(float align_x, float size_x, const char* fmt, va_list args)
+{
+ ImGuiWindow* window = GetCurrentWindow();
+ if (window->SkipItems)
+ return;
+
+ const char* text, *text_end;
+ ImFormatStringToTempBufferV(&text, &text_end, fmt, args);
+ const ImVec2 text_size = CalcTextSize(text, text_end);
+ size_x = CalcItemSize(ImVec2(size_x, 0.0f), 0.0f, text_size.y).x;
+
+ ImVec2 pos(window->DC.CursorPos.x, window->DC.CursorPos.y + window->DC.CurrLineTextBaseOffset);
+ ImVec2 pos_max(pos.x + size_x, window->ClipRect.Max.y);
+ ImVec2 size(ImMin(size_x, text_size.x), text_size.y);
+ window->DC.CursorMaxPos.x = ImMax(window->DC.CursorMaxPos.x, pos.x + text_size.x);
+ window->DC.IdealMaxPos.x = ImMax(window->DC.IdealMaxPos.x, pos.x + text_size.x);
+ if (align_x > 0.0f && text_size.x < size_x)
+ pos.x += ImTrunc((size_x - text_size.x) * align_x);
+ RenderTextEllipsis(window->DrawList, pos, pos_max, pos_max.x, text, text_end, &text_size);
+
+ const ImVec2 backup_max_pos = window->DC.CursorMaxPos;
+ ItemSize(size);
+ ItemAdd(ImRect(pos, pos + size), 0);
+ window->DC.CursorMaxPos.x = backup_max_pos.x; // Cancel out extending content size because right-aligned text would otherwise mess it up.
+
+ if (size_x < text_size.x && IsItemHovered(ImGuiHoveredFlags_NoNavOverride | ImGuiHoveredFlags_AllowWhenDisabled | ImGuiHoveredFlags_ForTooltip))
+ SetTooltip("%.*s", (int)(text_end - text), text);
+}
+
void ImGui::LabelText(const char* label, const char* fmt, ...)
{
va_list args;
@@ -508,6 +547,8 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool
ImGuiItemFlags item_flags = (g.LastItemData.ID == id ? g.LastItemData.ItemFlags : g.CurrentItemFlags);
if (flags & ImGuiButtonFlags_AllowOverlap)
item_flags |= ImGuiItemFlags_AllowOverlap;
+ if (item_flags & ImGuiItemFlags_NoFocus)
+ flags |= ImGuiButtonFlags_NoFocus | ImGuiButtonFlags_NoNavFocus;
// Default only reacts to left mouse button
if ((flags & ImGuiButtonFlags_MouseButtonMask_) == 0)
@@ -531,7 +572,8 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool
bool pressed = false;
bool hovered = ItemHoverable(bb, id, item_flags);
- // Special mode for Drag and Drop where holding button pressed for a long time while dragging another item triggers the button
+ // Special mode for Drag and Drop used by openables (tree nodes, tabs etc.)
+ // where holding the button pressed for a long time while drag a payload item triggers the button.
if (g.DragDropActive && (flags & ImGuiButtonFlags_PressedOnDragDropHold) && !(g.DragDropSourceFlags & ImGuiDragDropFlags_SourceNoHoldToOpenOthers))
if (IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByActiveItem))
{
@@ -583,7 +625,7 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool
SetFocusID(id, window);
FocusWindow(window);
}
- else
+ else if (!(flags & ImGuiButtonFlags_NoFocus))
{
FocusWindow(window, ImGuiFocusRequestFlags_RestoreFocusedChild); // Still need to focus and bring to front, but try to avoid losing NavId when navigating a child
}
@@ -601,7 +643,7 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool
SetFocusID(id, window);
FocusWindow(window);
}
- else
+ else if (!(flags & ImGuiButtonFlags_NoFocus))
{
FocusWindow(window, ImGuiFocusRequestFlags_RestoreFocusedChild); // Still need to focus and bring to front, but try to avoid losing NavId when navigating a child
}
@@ -989,7 +1031,8 @@ bool ImGui::ScrollbarEx(const ImRect& bb_frame, ImGuiID id, ImGuiAxis axis, ImS6
const bool allow_interaction = (alpha >= 1.0f);
ImRect bb = bb_frame;
- bb.Expand(ImVec2(-ImClamp(IM_TRUNC((bb_frame_width - 2.0f) * 0.5f), 0.0f, 3.0f), -ImClamp(IM_TRUNC((bb_frame_height - 2.0f) * 0.5f), 0.0f, 3.0f)));
+ float padding = IM_TRUNC(ImMin(style.ScrollbarPadding, ImMin(bb_frame_width, bb_frame_height) * 0.5f));
+ bb.Expand(-padding);
// V denote the main, longer axis of the scrollbar (= height for a vertical scrollbar)
const float scrollbar_size_v = (axis == ImGuiAxis_X) ? bb.GetWidth() : bb.GetHeight();
@@ -1069,9 +1112,9 @@ bool ImGui::ScrollbarEx(const ImRect& bb_frame, ImGuiID id, ImGuiAxis axis, ImS6
return held;
}
-// - Read about ImTextureID here: https://github.com/ocornut/imgui/wiki/Image-Loading-and-Displaying-Examples
+// - Read about ImTextureID/ImTextureRef here: https://github.com/ocornut/imgui/wiki/Image-Loading-and-Displaying-Examples
// - 'uv0' and 'uv1' are texture coordinates. Read about them from the same link above.
-void ImGui::ImageWithBg(ImTextureID user_texture_id, const ImVec2& image_size, const ImVec2& uv0, const ImVec2& uv1, const ImVec4& bg_col, const ImVec4& tint_col)
+void ImGui::ImageWithBg(ImTextureRef tex_ref, const ImVec2& image_size, const ImVec2& uv0, const ImVec2& uv1, const ImVec4& bg_col, const ImVec4& tint_col)
{
ImGuiContext& g = *GImGui;
ImGuiWindow* window = GetCurrentWindow();
@@ -1089,28 +1132,28 @@ void ImGui::ImageWithBg(ImTextureID user_texture_id, const ImVec2& image_size, c
window->DrawList->AddRect(bb.Min, bb.Max, GetColorU32(ImGuiCol_Border), 0.0f, ImDrawFlags_None, g.Style.ImageBorderSize);
if (bg_col.w > 0.0f)
window->DrawList->AddRectFilled(bb.Min + padding, bb.Max - padding, GetColorU32(bg_col));
- window->DrawList->AddImage(user_texture_id, bb.Min + padding, bb.Max - padding, uv0, uv1, GetColorU32(tint_col));
+ window->DrawList->AddImage(tex_ref, bb.Min + padding, bb.Max - padding, uv0, uv1, GetColorU32(tint_col));
}
-void ImGui::Image(ImTextureID user_texture_id, const ImVec2& image_size, const ImVec2& uv0, const ImVec2& uv1)
+void ImGui::Image(ImTextureRef tex_ref, const ImVec2& image_size, const ImVec2& uv0, const ImVec2& uv1)
{
- ImageWithBg(user_texture_id, image_size, uv0, uv1);
+ ImageWithBg(tex_ref, image_size, uv0, uv1);
}
// 1.91.9 (February 2025) removed 'tint_col' and 'border_col' parameters, made border size not depend on color value. (#8131, #8238)
#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
-void ImGui::Image(ImTextureID user_texture_id, const ImVec2& image_size, const ImVec2& uv0, const ImVec2& uv1, const ImVec4& tint_col, const ImVec4& border_col)
+void ImGui::Image(ImTextureRef tex_ref, const ImVec2& image_size, const ImVec2& uv0, const ImVec2& uv1, const ImVec4& tint_col, const ImVec4& border_col)
{
ImGuiContext& g = *GImGui;
PushStyleVar(ImGuiStyleVar_ImageBorderSize, (border_col.w > 0.0f) ? ImMax(1.0f, g.Style.ImageBorderSize) : 0.0f); // Preserve legacy behavior where border is always visible when border_col's Alpha is >0.0f
PushStyleColor(ImGuiCol_Border, border_col);
- ImageWithBg(user_texture_id, image_size, uv0, uv1, ImVec4(0, 0, 0, 0), tint_col);
+ ImageWithBg(tex_ref, image_size, uv0, uv1, ImVec4(0, 0, 0, 0), tint_col);
PopStyleColor();
PopStyleVar();
}
#endif
-bool ImGui::ImageButtonEx(ImGuiID id, ImTextureID user_texture_id, const ImVec2& image_size, const ImVec2& uv0, const ImVec2& uv1, const ImVec4& bg_col, const ImVec4& tint_col, ImGuiButtonFlags flags)
+bool ImGui::ImageButtonEx(ImGuiID id, ImTextureRef tex_ref, const ImVec2& image_size, const ImVec2& uv0, const ImVec2& uv1, const ImVec4& bg_col, const ImVec4& tint_col, ImGuiButtonFlags flags)
{
ImGuiContext& g = *GImGui;
ImGuiWindow* window = GetCurrentWindow();
@@ -1132,21 +1175,21 @@ bool ImGui::ImageButtonEx(ImGuiID id, ImTextureID user_texture_id, const ImVec2&
RenderFrame(bb.Min, bb.Max, col, true, ImClamp((float)ImMin(padding.x, padding.y), 0.0f, g.Style.FrameRounding));
if (bg_col.w > 0.0f)
window->DrawList->AddRectFilled(bb.Min + padding, bb.Max - padding, GetColorU32(bg_col));
- window->DrawList->AddImage(user_texture_id, bb.Min + padding, bb.Max - padding, uv0, uv1, GetColorU32(tint_col));
+ window->DrawList->AddImage(tex_ref, bb.Min + padding, bb.Max - padding, uv0, uv1, GetColorU32(tint_col));
return pressed;
}
// - ImageButton() adds style.FramePadding*2.0f to provided size. This is in order to facilitate fitting an image in a button.
// - ImageButton() draws a background based on regular Button() color + optionally an inner background if specified. (#8165) // FIXME: Maybe that's not the best design?
-bool ImGui::ImageButton(const char* str_id, ImTextureID user_texture_id, const ImVec2& image_size, const ImVec2& uv0, const ImVec2& uv1, const ImVec4& bg_col, const ImVec4& tint_col)
+bool ImGui::ImageButton(const char* str_id, ImTextureRef tex_ref, const ImVec2& image_size, const ImVec2& uv0, const ImVec2& uv1, const ImVec4& bg_col, const ImVec4& tint_col)
{
ImGuiContext& g = *GImGui;
ImGuiWindow* window = g.CurrentWindow;
if (window->SkipItems)
return false;
- return ImageButtonEx(window->GetID(str_id), user_texture_id, image_size, uv0, uv1, bg_col, tint_col);
+ return ImageButtonEx(window->GetID(str_id), tex_ref, image_size, uv0, uv1, bg_col, tint_col);
}
#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
@@ -1484,7 +1527,7 @@ bool ImGui::TextLink(const char* label)
ColorConvertHSVtoRGB(h, s, v, line_colf.x, line_colf.y, line_colf.z);
}
- float line_y = bb.Max.y + ImFloor(g.Font->Descent * g.FontScale * 0.20f);
+ float line_y = bb.Max.y + ImFloor(g.FontBaked->Descent * g.FontBakedScale * 0.20f);
window->DrawList->AddLine(ImVec2(bb.Min.x, line_y), ImVec2(bb.Max.x, line_y), GetColorU32(line_colf)); // FIXME-TEXT: Underline mode // FIXME-DPI
PushStyleColor(ImGuiCol_Text, GetColorU32(text_colf));
@@ -1495,14 +1538,14 @@ bool ImGui::TextLink(const char* label)
return pressed;
}
-void ImGui::TextLinkOpenURL(const char* label, const char* url)
+bool ImGui::TextLinkOpenURL(const char* label, const char* url)
{
ImGuiContext& g = *GImGui;
if (url == NULL)
url = label;
- if (TextLink(label))
- if (g.PlatformIO.Platform_OpenInShellFn != NULL)
- g.PlatformIO.Platform_OpenInShellFn(&g, url);
+ bool pressed = TextLink(label);
+ if (pressed && g.PlatformIO.Platform_OpenInShellFn != NULL)
+ g.PlatformIO.Platform_OpenInShellFn(&g, url);
SetItemTooltip(LocalizeGetMsg(ImGuiLocKey_OpenLink_s), url); // It is more reassuring for user to _always_ display URL when we same as label
if (BeginPopupContextItem())
{
@@ -1510,6 +1553,7 @@ void ImGui::TextLinkOpenURL(const char* label, const char* url)
SetClipboardText(url);
EndPopup();
}
+ return pressed;
}
//-------------------------------------------------------------------------
@@ -1696,7 +1740,7 @@ void ImGui::SeparatorTextEx(ImGuiID id, const char* label, const char* label_end
window->DrawList->AddLine(ImVec2(sep2_x1, seps_y), ImVec2(sep2_x2, seps_y), separator_col, separator_thickness);
if (g.LogEnabled)
LogSetNextTextDecoration("---", NULL);
- RenderTextEllipsis(window->DrawList, label_pos, ImVec2(bb.Max.x, bb.Max.y + style.ItemSpacing.y), bb.Max.x, bb.Max.x, label, label_end, &label_size);
+ RenderTextEllipsis(window->DrawList, label_pos, ImVec2(bb.Max.x, bb.Max.y + style.ItemSpacing.y), bb.Max.x, label, label_end, &label_size);
}
else
{
@@ -1792,27 +1836,31 @@ static int IMGUI_CDECL ShrinkWidthItemComparer(const void* lhs, const void* rhs)
// Shrink excess width from a set of item, by removing width from the larger items first.
// Set items Width to -1.0f to disable shrinking this item.
-void ImGui::ShrinkWidths(ImGuiShrinkWidthItem* items, int count, float width_excess)
+void ImGui::ShrinkWidths(ImGuiShrinkWidthItem* items, int count, float width_excess, float width_min)
{
if (count == 1)
{
if (items[0].Width >= 0.0f)
- items[0].Width = ImMax(items[0].Width - width_excess, 1.0f);
+ items[0].Width = ImMax(items[0].Width - width_excess, width_min);
return;
}
- ImQsort(items, (size_t)count, sizeof(ImGuiShrinkWidthItem), ShrinkWidthItemComparer);
+ ImQsort(items, (size_t)count, sizeof(ImGuiShrinkWidthItem), ShrinkWidthItemComparer); // Sort largest first, smallest last.
int count_same_width = 1;
- while (width_excess > 0.0f && count_same_width < count)
+ while (width_excess > 0.001f && count_same_width < count)
{
while (count_same_width < count && items[0].Width <= items[count_same_width].Width)
count_same_width++;
float max_width_to_remove_per_item = (count_same_width < count && items[count_same_width].Width >= 0.0f) ? (items[0].Width - items[count_same_width].Width) : (items[0].Width - 1.0f);
+ max_width_to_remove_per_item = ImMin(items[0].Width - width_min, max_width_to_remove_per_item);
if (max_width_to_remove_per_item <= 0.0f)
break;
- float width_to_remove_per_item = ImMin(width_excess / count_same_width, max_width_to_remove_per_item);
+ float base_width_to_remove_per_item = ImMin(width_excess / count_same_width, max_width_to_remove_per_item);
for (int item_n = 0; item_n < count_same_width; item_n++)
- items[item_n].Width -= width_to_remove_per_item;
- width_excess -= width_to_remove_per_item * count_same_width;
+ {
+ float width_to_remove_for_this_item = ImMin(base_width_to_remove_per_item, items[item_n].Width - width_min);
+ items[item_n].Width -= width_to_remove_for_this_item;
+ width_excess -= width_to_remove_for_this_item;
+ }
}
// Round width and redistribute remainder
@@ -1989,7 +2037,8 @@ bool ImGui::BeginComboPopup(ImGuiID popup_id, const ImRect& bb, ImGuiComboFlags
if (!ret)
{
EndPopup();
- IM_ASSERT(0); // This should never happen as we tested for IsPopupOpen() above
+ if (!g.IO.ConfigDebugBeginReturnValueOnce && !g.IO.ConfigDebugBeginReturnValueLoop) // Begin may only return false with those debug tools activated.
+ IM_ASSERT(0); // This should never happen as we tested for IsPopupOpen() above
return false;
}
g.BeginComboDepth++;
@@ -3868,9 +3917,6 @@ bool ImGui::InputDouble(const char* label, double* v, double step, double step_f
// - InputText()
// - InputTextWithHint()
// - InputTextMultiline()
-// - InputTextGetCharInfo() [Internal]
-// - InputTextReindexLines() [Internal]
-// - InputTextReindexLinesRange() [Internal]
// - InputTextEx() [Internal]
// - DebugNodeInputTextState() [Internal]
//-------------------------------------------------------------------------
@@ -3897,75 +3943,11 @@ bool ImGui::InputTextWithHint(const char* label, const char* hint, char* buf, si
return InputTextEx(label, hint, buf, (int)buf_size, ImVec2(0, 0), flags, callback, user_data);
}
-// This is only used in the path where the multiline widget is inactive.
-static int InputTextCalcTextLenAndLineCount(const char* text_begin, const char** out_text_end)
-{
- int line_count = 0;
- const char* s = text_begin;
- while (true)
- {
- const char* s_eol = strchr(s, '\n');
- line_count++;
- if (s_eol == NULL)
- {
- s = s + ImStrlen(s);
- break;
- }
- s = s_eol + 1;
- }
- *out_text_end = s;
- return line_count;
-}
-
-// FIXME: Ideally we'd share code with ImFont::CalcTextSizeA()
-static ImVec2 InputTextCalcTextSize(ImGuiContext* ctx, const char* text_begin, const char* text_end, const char** remaining, ImVec2* out_offset, bool stop_on_new_line)
+static ImVec2 InputTextCalcTextSize(ImGuiContext* ctx, const char* text_begin, const char* text_end_display, const char* text_end, const char** out_remaining, ImVec2* out_offset, ImDrawTextFlags flags)
{
ImGuiContext& g = *ctx;
- ImFont* font = g.Font;
- const float line_height = g.FontSize;
- const float scale = line_height / font->FontSize;
-
- ImVec2 text_size = ImVec2(0, 0);
- float line_width = 0.0f;
-
- const char* s = text_begin;
- while (s < text_end)
- {
- unsigned int c = (unsigned int)*s;
- if (c < 0x80)
- s += 1;
- else
- s += ImTextCharFromUtf8(&c, s, text_end);
-
- if (c == '\n')
- {
- text_size.x = ImMax(text_size.x, line_width);
- text_size.y += line_height;
- line_width = 0.0f;
- if (stop_on_new_line)
- break;
- continue;
- }
- if (c == '\r')
- continue;
-
- const float char_width = ((int)c < font->IndexAdvanceX.Size ? font->IndexAdvanceX.Data[c] : font->FallbackAdvanceX) * scale;
- line_width += char_width;
- }
-
- if (text_size.x < line_width)
- text_size.x = line_width;
-
- if (out_offset)
- *out_offset = ImVec2(line_width, text_size.y + line_height); // offset allow for the possibility of sitting after a trailing \n
-
- if (line_width > 0 || text_size.y == 0.0f) // whereas size.y will ignore the trailing \n
- text_size.y += line_height;
-
- if (remaining)
- *remaining = s;
-
- return text_size;
+ ImGuiInputTextState* obj = &g.InputTextState;
+ return ImFontCalcTextSizeEx(g.Font, g.FontSize, FLT_MAX, obj->WrapWidth, text_begin, text_end_display, text_end, out_remaining, out_offset, flags);
}
// Wrapper for stb_textedit.h to edit text (our wrapper is for: statically sized buffer, single-line, wchar characters. InputText converts between UTF-8 and wchar)
@@ -3976,14 +3958,14 @@ static ImVec2 InputTextCalcTextSize(ImGuiContext* ctx, const char* text_begin, c
namespace ImStb
{
static int STB_TEXTEDIT_STRINGLEN(const ImGuiInputTextState* obj) { return obj->TextLen; }
-static char STB_TEXTEDIT_GETCHAR(const ImGuiInputTextState* obj, int idx) { IM_ASSERT(idx <= obj->TextLen); return obj->TextSrc[idx]; }
-static float STB_TEXTEDIT_GETWIDTH(ImGuiInputTextState* obj, int line_start_idx, int char_idx) { unsigned int c; ImTextCharFromUtf8(&c, obj->TextSrc + line_start_idx + char_idx, obj->TextSrc + obj->TextLen); if ((ImWchar)c == '\n') return IMSTB_TEXTEDIT_GETWIDTH_NEWLINE; ImGuiContext& g = *obj->Ctx; return g.Font->GetCharAdvance((ImWchar)c) * g.FontScale; }
+static char STB_TEXTEDIT_GETCHAR(const ImGuiInputTextState* obj, int idx) { IM_ASSERT(idx >= 0 && idx <= obj->TextLen); return obj->TextSrc[idx]; }
+static float STB_TEXTEDIT_GETWIDTH(ImGuiInputTextState* obj, int line_start_idx, int char_idx) { unsigned int c; ImTextCharFromUtf8(&c, obj->TextSrc + line_start_idx + char_idx, obj->TextSrc + obj->TextLen); if ((ImWchar)c == '\n') return IMSTB_TEXTEDIT_GETWIDTH_NEWLINE; ImGuiContext& g = *obj->Ctx; return g.FontBaked->GetCharAdvance((ImWchar)c) * g.FontBakedScale; }
static char STB_TEXTEDIT_NEWLINE = '\n';
static void STB_TEXTEDIT_LAYOUTROW(StbTexteditRow* r, ImGuiInputTextState* obj, int line_start_idx)
{
const char* text = obj->TextSrc;
const char* text_remaining = NULL;
- const ImVec2 size = InputTextCalcTextSize(obj->Ctx, text + line_start_idx, text + obj->TextLen, &text_remaining, NULL, true);
+ const ImVec2 size = InputTextCalcTextSize(obj->Ctx, text + line_start_idx, text + obj->TextLen, text + obj->TextLen, &text_remaining, NULL, ImDrawTextFlags_StopOnNewLine | ImDrawTextFlags_WrapKeepBlanks);
r->x0 = 0.0f;
r->x1 = size.x;
r->baseline_y_delta = size.y;
@@ -4085,6 +4067,75 @@ static int STB_TEXTEDIT_MOVEWORDRIGHT_IMPL(ImGuiInputTextState* obj, int idx)
#define STB_TEXTEDIT_MOVEWORDLEFT STB_TEXTEDIT_MOVEWORDLEFT_IMPL // They need to be #define for stb_textedit.h
#define STB_TEXTEDIT_MOVEWORDRIGHT STB_TEXTEDIT_MOVEWORDRIGHT_IMPL
+// Reimplementation of stb_textedit_move_line_start()/stb_textedit_move_line_end() which supports word-wrapping.
+static int STB_TEXTEDIT_MOVELINESTART_IMPL(ImGuiInputTextState* obj, ImStb::STB_TexteditState* state, int cursor)
+{
+ if (state->single_line)
+ return 0;
+
+ if (obj->WrapWidth > 0.0f)
+ {
+ ImGuiContext& g = *obj->Ctx;
+ const char* p_cursor = obj->TextSrc + cursor;
+ const char* p_bol = ImStrbol(p_cursor, obj->TextSrc);
+ const char* p = p_bol;
+ const char* text_end = obj->TextSrc + obj->TextLen; // End of line would be enough
+ while (p >= p_bol)
+ {
+ const char* p_eol = ImFontCalcWordWrapPositionEx(g.Font, g.FontSize, p, text_end, obj->WrapWidth, ImDrawTextFlags_WrapKeepBlanks);
+ if (p == p_cursor) // If we are already on a visible beginning-of-line, return real beginning-of-line (would be same as regular handler below)
+ return (int)(p_bol - obj->TextSrc);
+ if (p_eol == p_cursor && obj->TextA[cursor] != '\n' && obj->LastMoveDirectionLR == ImGuiDir_Left)
+ return (int)(p_bol - obj->TextSrc);
+ if (p_eol >= p_cursor)
+ return (int)(p - obj->TextSrc);
+ p = (*p_eol == '\n') ? p_eol + 1 : p_eol;
+ }
+ }
+
+ // Regular handler, same as stb_textedit_move_line_start()
+ while (cursor > 0)
+ {
+ int prev_cursor = IMSTB_TEXTEDIT_GETPREVCHARINDEX(obj, cursor);
+ if (STB_TEXTEDIT_GETCHAR(obj, prev_cursor) == STB_TEXTEDIT_NEWLINE)
+ break;
+ cursor = prev_cursor;
+ }
+ return cursor;
+}
+
+static int STB_TEXTEDIT_MOVELINEEND_IMPL(ImGuiInputTextState* obj, ImStb::STB_TexteditState* state, int cursor)
+{
+ int n = STB_TEXTEDIT_STRINGLEN(obj);
+ if (state->single_line)
+ return n;
+
+ if (obj->WrapWidth > 0.0f)
+ {
+ ImGuiContext& g = *obj->Ctx;
+ const char* p_cursor = obj->TextSrc + cursor;
+ const char* p = ImStrbol(p_cursor, obj->TextSrc);
+ const char* text_end = obj->TextSrc + obj->TextLen; // End of line would be enough
+ while (p < text_end)
+ {
+ const char* p_eol = ImFontCalcWordWrapPositionEx(g.Font, g.FontSize, p, text_end, obj->WrapWidth, ImDrawTextFlags_WrapKeepBlanks);
+ cursor = (int)(p_eol - obj->TextSrc);
+ if (p_eol == p_cursor && obj->LastMoveDirectionLR != ImGuiDir_Left) // If we are already on a visible end-of-line, switch to regular handle
+ break;
+ if (p_eol > p_cursor)
+ return cursor;
+ p = (*p_eol == '\n') ? p_eol + 1 : p_eol;
+ }
+ }
+ // Regular handler, same as stb_textedit_move_line_end()
+ while (cursor < n && STB_TEXTEDIT_GETCHAR(obj, cursor) != STB_TEXTEDIT_NEWLINE)
+ cursor = IMSTB_TEXTEDIT_GETNEXTCHARINDEX(obj, cursor);
+ return cursor;
+}
+
+#define STB_TEXTEDIT_MOVELINESTART STB_TEXTEDIT_MOVELINESTART_IMPL
+#define STB_TEXTEDIT_MOVELINEEND STB_TEXTEDIT_MOVELINEEND_IMPL
+
static void STB_TEXTEDIT_DELETECHARS(ImGuiInputTextState* obj, int pos, int n)
{
// Offset remaining text (+ copy zero terminator)
@@ -4188,6 +4239,11 @@ void ImGuiInputTextState::OnKeyPressed(int key)
stb_textedit_key(this, Stb, key);
CursorFollow = true;
CursorAnimReset();
+ const int key_u = (key & ~STB_TEXTEDIT_K_SHIFT);
+ if (key_u == STB_TEXTEDIT_K_LEFT || key_u == STB_TEXTEDIT_K_LINESTART || key_u == STB_TEXTEDIT_K_TEXTSTART || key_u == STB_TEXTEDIT_K_BACKSPACE || key_u == STB_TEXTEDIT_K_WORDLEFT)
+ LastMoveDirectionLR = ImGuiDir_Left;
+ else if (key_u == STB_TEXTEDIT_K_RIGHT || key_u == STB_TEXTEDIT_K_LINEEND || key_u == STB_TEXTEDIT_K_TEXTEND || key_u == STB_TEXTEDIT_K_DELETE || key_u == STB_TEXTEDIT_K_WORDRIGHT)
+ LastMoveDirectionLR = ImGuiDir_Right;
}
void ImGuiInputTextState::OnCharPressed(unsigned int c)
@@ -4209,6 +4265,7 @@ void ImGuiInputTextState::ClearSelection() { Stb->select_start
int ImGuiInputTextState::GetCursorPos() const { return Stb->cursor; }
int ImGuiInputTextState::GetSelectionStart() const { return Stb->select_start; }
int ImGuiInputTextState::GetSelectionEnd() const { return Stb->select_end; }
+float ImGuiInputTextState::GetPreferredOffsetX() const { return Stb->has_preferred_x ? Stb->preferred_x : -1; }
void ImGuiInputTextState::SelectAll() { Stb->select_start = 0; Stb->cursor = Stb->select_end = TextLen; Stb->has_preferred_x = 0; }
void ImGuiInputTextState::ReloadUserBufAndSelectAll() { WantReloadUserBuf = true; ReloadSelectionStart = 0; ReloadSelectionEnd = INT_MAX; }
void ImGuiInputTextState::ReloadUserBufAndKeepSelection() { WantReloadUserBuf = true; ReloadSelectionStart = Stb->select_start; ReloadSelectionEnd = Stb->select_end; }
@@ -4245,23 +4302,24 @@ void ImGuiInputTextCallbackData::InsertChars(int pos, const char* new_text, cons
if (new_text == new_text_end)
return;
+ ImGuiContext& g = *Ctx;
+ ImGuiInputTextState* obj = &g.InputTextState;
+ IM_ASSERT(obj->ID != 0 && g.ActiveId == obj->ID);
+
// Grow internal buffer if needed
const bool is_resizable = (Flags & ImGuiInputTextFlags_CallbackResize) != 0;
const int new_text_len = new_text_end ? (int)(new_text_end - new_text) : (int)ImStrlen(new_text);
- if (new_text_len + BufTextLen >= BufSize)
+ if (new_text_len + BufTextLen + 1 > obj->TextA.Size && (Flags & ImGuiInputTextFlags_ReadOnly) == 0)
{
if (!is_resizable)
return;
- ImGuiContext& g = *Ctx;
- ImGuiInputTextState* edit_state = &g.InputTextState;
- IM_ASSERT(edit_state->ID != 0 && g.ActiveId == edit_state->ID);
- IM_ASSERT(Buf == edit_state->TextA.Data);
+ IM_ASSERT(Buf == obj->TextA.Data);
int new_buf_size = BufTextLen + ImClamp(new_text_len * 4, 32, ImMax(256, new_text_len)) + 1;
- edit_state->TextA.resize(new_buf_size + 1);
- edit_state->TextSrc = edit_state->TextA.Data;
- Buf = edit_state->TextA.Data;
- BufSize = edit_state->BufCapacity = new_buf_size;
+ obj->TextA.resize(new_buf_size + 1);
+ obj->TextSrc = obj->TextA.Data;
+ Buf = obj->TextA.Data;
+ BufSize = obj->BufCapacity = new_buf_size;
}
if (BufTextLen != pos)
@@ -4279,18 +4337,29 @@ void ImGuiInputTextCallbackData::InsertChars(int pos, const char* new_text, cons
void ImGui::PushPasswordFont()
{
ImGuiContext& g = *GImGui;
- ImFont* in_font = g.Font;
- ImFont* out_font = &g.InputTextPasswordFont;
- ImFontGlyph* glyph = in_font->FindGlyph('*');
- out_font->FontSize = in_font->FontSize;
- out_font->Scale = in_font->Scale;
- out_font->Ascent = in_font->Ascent;
- out_font->Descent = in_font->Descent;
- out_font->ContainerAtlas = in_font->ContainerAtlas;
- out_font->FallbackGlyph = glyph;
- out_font->FallbackAdvanceX = glyph->AdvanceX;
- IM_ASSERT(out_font->Glyphs.Size == 0 && out_font->IndexAdvanceX.Size == 0 && out_font->IndexLookup.Size == 0);
- PushFont(out_font);
+ ImFontBaked* backup = &g.InputTextPasswordFontBackupBaked;
+ IM_ASSERT(backup->IndexAdvanceX.Size == 0 && backup->IndexLookup.Size == 0);
+ ImFontGlyph* glyph = g.FontBaked->FindGlyph('*');
+ g.InputTextPasswordFontBackupFlags = g.Font->Flags;
+ backup->FallbackGlyphIndex = g.FontBaked->FallbackGlyphIndex;
+ backup->FallbackAdvanceX = g.FontBaked->FallbackAdvanceX;
+ backup->IndexLookup.swap(g.FontBaked->IndexLookup);
+ backup->IndexAdvanceX.swap(g.FontBaked->IndexAdvanceX);
+ g.Font->Flags |= ImFontFlags_NoLoadGlyphs;
+ g.FontBaked->FallbackGlyphIndex = g.FontBaked->Glyphs.index_from_ptr(glyph);
+ g.FontBaked->FallbackAdvanceX = glyph->AdvanceX;
+}
+
+void ImGui::PopPasswordFont()
+{
+ ImGuiContext& g = *GImGui;
+ ImFontBaked* backup = &g.InputTextPasswordFontBackupBaked;
+ g.Font->Flags = g.InputTextPasswordFontBackupFlags;
+ g.FontBaked->FallbackGlyphIndex = backup->FallbackGlyphIndex;
+ g.FontBaked->FallbackAdvanceX = backup->FallbackAdvanceX;
+ g.FontBaked->IndexLookup.swap(backup->IndexLookup);
+ g.FontBaked->IndexAdvanceX.swap(backup->IndexAdvanceX);
+ IM_ASSERT(backup->IndexAdvanceX.Size == 0 && backup->IndexLookup.Size == 0);
}
// Return false to discard a character.
@@ -4346,7 +4415,7 @@ static bool InputTextFilterCharacter(ImGuiContext* ctx, unsigned int* p_char, Im
if (c == '.' || c == ',')
c = c_decimal_point;
- // Full-width -> half-width conversion for numeric fields (https://en.wikipedia.org/wiki/Halfwidth_and_Fullwidth_Forms_(Unicode_block)
+ // Full-width -> half-width conversion for numeric fields: https://en.wikipedia.org/wiki/Halfwidth_and_Fullwidth_Forms_(Unicode_block)
// While this is mostly convenient, this has the side-effect for uninformed users accidentally inputting full-width characters that they may
// scratch their head as to why it works in numerical fields vs in generic text fields it would require support in the font.
if (flags & (ImGuiInputTextFlags_CharsDecimal | ImGuiInputTextFlags_CharsScientific | ImGuiInputTextFlags_CharsHexadecimal))
@@ -4451,6 +4520,97 @@ void ImGui::InputTextDeactivateHook(ImGuiID id)
}
}
+static int* ImLowerBound(int* in_begin, int* in_end, int v)
+{
+ int* in_p = in_begin;
+ for (size_t count = (size_t)(in_end - in_p); count > 0; )
+ {
+ size_t count2 = count >> 1;
+ int* mid = in_p + count2;
+ if (*mid < v)
+ {
+ in_p = ++mid;
+ count -= count2 + 1;
+ }
+ else
+ {
+ count = count2;
+ }
+ }
+ return in_p;
+}
+
+// FIXME-WORDWRAP: Bundle some of this into ImGuiTextIndex and/or extract as a different tool?
+// 'max_output_buffer_size' happens to be a meaningful optimization to avoid writing the full line_index when not necessarily needed (e.g. very large buffer, scrolled up, inactive)
+static int InputTextLineIndexBuild(ImGuiInputTextFlags flags, ImGuiTextIndex* line_index, const char* buf, const char* buf_end, float wrap_width, int max_output_buffer_size, const char** out_buf_end)
+{
+ ImGuiContext& g = *GImGui;
+ int size = 0;
+ const char* s;
+ if (flags & ImGuiInputTextFlags_WordWrap)
+ {
+ for (s = buf; s < buf_end; s = (*s == '\n') ? s + 1 : s)
+ {
+ if (size++ <= max_output_buffer_size)
+ line_index->Offsets.push_back((int)(s - buf));
+ s = ImFontCalcWordWrapPositionEx(g.Font, g.FontSize, s, buf_end, wrap_width, ImDrawTextFlags_WrapKeepBlanks);
+ }
+ }
+ else if (buf_end != NULL)
+ {
+ for (s = buf; s < buf_end; s = s ? s + 1 : buf_end)
+ {
+ if (size++ <= max_output_buffer_size)
+ line_index->Offsets.push_back((int)(s - buf));
+ s = (const char*)ImMemchr(s, '\n', buf_end - s);
+ }
+ }
+ else
+ {
+ const char* s_eol;
+ for (s = buf; ; s = s_eol + 1)
+ {
+ if (size++ <= max_output_buffer_size)
+ line_index->Offsets.push_back((int)(s - buf));
+ if ((s_eol = strchr(s, '\n')) != NULL)
+ continue;
+ s += strlen(s);
+ break;
+ }
+ }
+ if (out_buf_end != NULL)
+ *out_buf_end = buf_end = s;
+ if (size == 0)
+ {
+ line_index->Offsets.push_back(0);
+ size++;
+ }
+ if (buf_end > buf && buf_end[-1] == '\n' && size <= max_output_buffer_size)
+ {
+ line_index->Offsets.push_back((int)(buf_end - buf));
+ size++;
+ }
+ return size;
+}
+
+static ImVec2 InputTextLineIndexGetPosOffset(ImGuiContext& g, ImGuiInputTextState* state, ImGuiTextIndex* line_index, const char* buf, const char* buf_end, int cursor_n)
+{
+ const char* cursor_ptr = buf + cursor_n;
+ int* it_begin = line_index->Offsets.begin();
+ int* it_end = line_index->Offsets.end();
+ const int* it = ImLowerBound(it_begin, it_end, cursor_n);
+ if (it > it_begin)
+ if (it == it_end || *it != cursor_n || (state != NULL && state->WrapWidth > 0.0f && state->LastMoveDirectionLR == ImGuiDir_Right && cursor_ptr[-1] != '\n' && cursor_ptr[-1] != 0))
+ it--;
+
+ const int line_no = (it == it_begin) ? 0 : line_index->Offsets.index_from_ptr(it);
+ const char* line_start = line_index->get_line_begin(buf, line_no);
+ ImVec2 offset;
+ offset.x = InputTextCalcTextSize(&g, line_start, cursor_ptr, buf_end, NULL, NULL, ImDrawTextFlags_WrapKeepBlanks).x;
+ offset.y = (line_no + 1) * g.FontSize;
+ return offset;
+}
+
// Edit a string of text
// - buf_size account for the zero-terminator, so a buf_size of 6 can hold "Hello" but not "Hello!".
// This is so we can easily call InputText() on static arrays using ARRAYSIZE() and to match
@@ -4468,7 +4628,9 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_
IM_ASSERT(buf != NULL && buf_size >= 0);
IM_ASSERT(!((flags & ImGuiInputTextFlags_CallbackHistory) && (flags & ImGuiInputTextFlags_Multiline))); // Can't use both together (they both use up/down keys)
IM_ASSERT(!((flags & ImGuiInputTextFlags_CallbackCompletion) && (flags & ImGuiInputTextFlags_AllowTabInput))); // Can't use both together (they both use tab key)
- IM_ASSERT(!((flags & ImGuiInputTextFlags_ElideLeft) && (flags & ImGuiInputTextFlags_Multiline))); // Multiline will not work with left-trimming
+ IM_ASSERT(!((flags & ImGuiInputTextFlags_ElideLeft) && (flags & ImGuiInputTextFlags_Multiline))); // Multiline does not not work with left-trimming
+ IM_ASSERT((flags & ImGuiInputTextFlags_WordWrap) == 0 || (flags & ImGuiInputTextFlags_Password) == 0); // WordWrap does not work with Password mode.
+ IM_ASSERT((flags & ImGuiInputTextFlags_WordWrap) == 0 || (flags & ImGuiInputTextFlags_Multiline) != 0); // WordWrap does not work in single-line mode.
ImGuiContext& g = *GImGui;
ImGuiIO& io = g.IO;
@@ -4502,8 +4664,8 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_
item_data_backup = g.LastItemData;
window->DC.CursorPos = backup_pos;
- // Prevent NavActivation from Tabbing when our widget accepts Tab inputs: this allows cycling through widgets without stopping.
- if (g.NavActivateId == id && (g.NavActivateFlags & ImGuiActivateFlags_FromTabbing) && (flags & ImGuiInputTextFlags_AllowTabInput))
+ // Prevent NavActivation from explicit Tabbing when our widget accepts Tab inputs: this allows cycling through widgets without stopping.
+ if (g.NavActivateId == id && (g.NavActivateFlags & ImGuiActivateFlags_FromTabbing) && !(g.NavActivateFlags & ImGuiActivateFlags_FromFocusApi) && (flags & ImGuiInputTextFlags_AllowTabInput))
g.NavActivateId = 0;
// Prevent NavActivate reactivating in BeginChild() when we are already active.
@@ -4559,6 +4721,13 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_
if (is_resizable)
IM_ASSERT(callback != NULL); // Must provide a callback if you set the ImGuiInputTextFlags_CallbackResize flag!
+ // Word-wrapping: enforcing a fixed width not altered by vertical scrollbar makes things easier, notably to track cursor reliably and avoid one-frame glitches.
+ // Instead of using ImGuiWindowFlags_AlwaysVerticalScrollbar we account for that space if the scrollbar is not visible.
+ const bool is_wordwrap = (flags & ImGuiInputTextFlags_WordWrap) != 0;
+ float wrap_width = 0.0f;
+ if (is_wordwrap)
+ wrap_width = ImMax(1.0f, GetContentRegionAvail().x + (draw_window->ScrollbarY ? 0.0f : -g.Style.ScrollbarSize));
+
const bool input_requested_by_nav = (g.ActiveId != id) && ((g.NavActivateId == id) && ((g.NavActivateFlags & ImGuiActivateFlags_PreferInput) || (g.NavInputSource == ImGuiInputSource_Keyboard)));
const bool user_clicked = hovered && io.MouseClicked[0];
@@ -4598,7 +4767,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_
// Take a copy of the initial buffer value.
// From the moment we focused we are normally ignoring the content of 'buf' (unless we are in read-only mode)
const int buf_len = (int)ImStrlen(buf);
- IM_ASSERT(buf_len + 1 <= buf_size && "Is your input buffer properly zero-terminated?");
+ IM_ASSERT(((buf_len + 1 <= buf_size) || (buf_len == 0 && buf_size == 0)) && "Is your input buffer properly zero-terminated?");
state->TextToRevertTo.resize(buf_len + 1); // UTF-8. we use +1 to make sure that .Data is always pointing to at least an empty string.
memcpy(state->TextToRevertTo.Data, buf, buf_len + 1);
@@ -4711,6 +4880,15 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_
if (is_password && !is_displaying_hint)
PushPasswordFont();
+ // Word-wrapping: attempt to keep cursor in view while resizing frame/parent
+ // FIXME-WORDWRAP: It would be better to preserve same relative offset.
+ if (is_wordwrap && state != NULL && state->ID == id && state->WrapWidth != wrap_width)
+ {
+ state->CursorCenterY = true;
+ state->WrapWidth = wrap_width;
+ render_cursor = true;
+ }
+
// Process mouse inputs and character inputs
if (g.ActiveId == id)
{
@@ -4718,6 +4896,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_
state->Edited = false;
state->BufCapacity = buf_size;
state->Flags = flags;
+ state->WrapWidth = wrap_width;
// Although we are active we don't prevent mouse from hovering other elements unless we are interacting right now with the widget.
// Down the line we should have a cleaner library-wide concept of Selected vs Active.
@@ -4755,9 +4934,11 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_
{
// Triple-click: Select line
const bool is_eol = ImStb::STB_TEXTEDIT_GETCHAR(state, state->Stb->cursor) == '\n';
+ state->WrapWidth = 0.0f; // Temporarily disable wrapping so we use real line start.
state->OnKeyPressed(STB_TEXTEDIT_K_LINESTART);
state->OnKeyPressed(STB_TEXTEDIT_K_LINEEND | STB_TEXTEDIT_K_SHIFT);
state->OnKeyPressed(STB_TEXTEDIT_K_RIGHT | STB_TEXTEDIT_K_SHIFT);
+ state->WrapWidth = wrap_width;
if (!is_eol && is_multiline)
{
ImSwap(state->Stb->select_start, state->Stb->select_end);
@@ -4890,7 +5071,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_
{
// Determine if we turn Enter into a \n character
bool ctrl_enter_for_new_line = (flags & ImGuiInputTextFlags_CtrlEnterForNewLine) != 0;
- if (!is_multiline || is_gamepad_validate || (ctrl_enter_for_new_line && !io.KeyCtrl) || (!ctrl_enter_for_new_line && io.KeyCtrl))
+ if (!is_multiline || is_gamepad_validate || (ctrl_enter_for_new_line != io.KeyCtrl))
{
validated = true;
if (io.ConfigInputTextEnterKeepActive && !is_multiline)
@@ -4909,7 +5090,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_
{
if (flags & ImGuiInputTextFlags_EscapeClearsAll)
{
- if (buf[0] != 0)
+ if (state->TextA.Data[0] != 0)
{
revert_edit = true;
}
@@ -5001,14 +5182,14 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_
if (flags & ImGuiInputTextFlags_EscapeClearsAll)
{
// Clear input
- IM_ASSERT(buf[0] != 0);
+ IM_ASSERT(state->TextA.Data[0] != 0);
apply_new_text = "";
apply_new_text_length = 0;
value_changed = true;
- IMSTB_TEXTEDIT_CHARTYPE empty_string;
+ char empty_string = 0;
stb_textedit_replace(state, state->Stb, &empty_string, 0);
}
- else if (strcmp(buf, state->TextToRevertTo.Data) != 0)
+ else if (strcmp(state->TextA.Data, state->TextToRevertTo.Data) != 0)
{
apply_new_text = state->TextToRevertTo.Data;
apply_new_text_length = state->TextToRevertTo.Size - 1;
@@ -5167,8 +5348,6 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_
// Otherwise request text input ahead for next frame.
if (g.ActiveId == id && clear_active_id)
ClearActiveID();
- else if (g.ActiveId == id)
- g.WantTextInputNextFrame = 1;
// Render frame
if (!is_multiline)
@@ -5177,9 +5356,11 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_
RenderFrame(frame_bb.Min, frame_bb.Max, GetColorU32(ImGuiCol_FrameBg), true, style.FrameRounding);
}
- const ImVec4 clip_rect(frame_bb.Min.x, frame_bb.Min.y, frame_bb.Min.x + inner_size.x, frame_bb.Min.y + inner_size.y); // Not using frame_bb.Max because we have adjusted size
ImVec2 draw_pos = is_multiline ? draw_window->DC.CursorPos : frame_bb.Min + style.FramePadding;
ImVec2 text_size(0.0f, 0.0f);
+ ImRect clip_rect(frame_bb.Min.x, frame_bb.Min.y, frame_bb.Min.x + inner_size.x, frame_bb.Min.y + inner_size.y); // Not using frame_bb.Max because we have adjusted size
+ if (is_multiline)
+ clip_rect.ClipWith(draw_window->ClipRect);
// Set upper limit of single-line InputTextEx() at 2 million characters strings. The current pathological worst case is a long line
// without any carriage return, which would makes ImFont::RenderText() reserve too many vertices and probably crash. Avoid it altogether.
@@ -5194,7 +5375,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_
if (new_is_displaying_hint != is_displaying_hint)
{
if (is_password && !is_displaying_hint)
- PopFont();
+ PopPasswordFont();
is_displaying_hint = new_is_displaying_hint;
if (is_password && !is_displaying_hint)
PushPasswordFont();
@@ -5204,15 +5385,42 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_
buf_display = hint;
buf_display_end = hint + ImStrlen(hint);
}
+ else
+ {
+ if (render_cursor || render_selection || g.ActiveId == id)
+ buf_display_end = buf_display + state->TextLen; //-V595
+ else if (is_multiline && !is_wordwrap)
+ buf_display_end = NULL; // Inactive multi-line: end of buffer will be output by InputTextLineIndexBuild() special strchr() path.
+ else
+ buf_display_end = buf_display + ImStrlen(buf_display);
+ }
+
+ // Calculate visibility
+ int line_visible_n0 = 0, line_visible_n1 = 1;
+ if (is_multiline)
+ CalcClipRectVisibleItemsY(clip_rect, draw_pos, g.FontSize, &line_visible_n0, &line_visible_n1);
+
+ // Build line index for easy data access (makes code below simpler and faster)
+ ImGuiTextIndex* line_index = &g.InputTextLineIndex;
+ line_index->Offsets.resize(0);
+ int line_count = 1;
+ if (is_multiline)
+ line_count = InputTextLineIndexBuild(flags, line_index, buf_display, buf_display_end, wrap_width, (render_cursor && state && state->CursorFollow) ? INT_MAX : line_visible_n1 + 1, buf_display_end ? NULL : &buf_display_end);
+ line_index->EndOffset = (int)(buf_display_end - buf_display);
+ line_visible_n1 = ImMin(line_visible_n1, line_count);
+
+ // Store text height (we don't need width)
+ text_size = ImVec2(inner_size.x, line_count * g.FontSize);
+ //GetForegroundDrawList()->AddRect(draw_pos + ImVec2(0, line_visible_n0 * g.FontSize), draw_pos + ImVec2(frame_size.x, line_visible_n1 * g.FontSize), IM_COL32(255, 0, 0, 255));
+
+ // Calculate blinking cursor position
+ const ImVec2 cursor_offset = render_cursor && state ? InputTextLineIndexGetPosOffset(g, state, line_index, buf_display, buf_display_end, state->Stb->cursor) : ImVec2(0.0f, 0.0f);
+ ImVec2 draw_scroll;
// Render text. We currently only render selection when the widget is active or while scrolling.
- // FIXME: We could remove the '&& render_cursor' to keep rendering selection when inactive.
+ const ImU32 text_col = GetColorU32(is_displaying_hint ? ImGuiCol_TextDisabled : ImGuiCol_Text);
if (render_cursor || render_selection)
{
- IM_ASSERT(state != NULL);
- if (!is_displaying_hint)
- buf_display_end = buf_display + state->TextLen;
-
// Render text (with cursor and selection)
// This is going to be messy. We need to:
// - Display the text (this alone can be more easily clipped)
@@ -5220,48 +5428,11 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_
// - Measure text height (for scrollbar)
// We are attempting to do most of that in **one main pass** to minimize the computation cost (non-negligible for large amount of text) + 2nd pass for selection rendering (we could merge them by an extra refactoring effort)
// FIXME: This should occur on buf_display but we'd need to maintain cursor/select_start/select_end for UTF-8.
- const char* text_begin = buf_display;
- const char* text_end = text_begin + state->TextLen;
- ImVec2 cursor_offset, select_start_offset;
-
- {
- // Find lines numbers straddling cursor and selection min position
- int cursor_line_no = render_cursor ? -1 : -1000;
- int selmin_line_no = render_selection ? -1 : -1000;
- const char* cursor_ptr = render_cursor ? text_begin + state->Stb->cursor : NULL;
- const char* selmin_ptr = render_selection ? text_begin + ImMin(state->Stb->select_start, state->Stb->select_end) : NULL;
-
- // Count lines and find line number for cursor and selection ends
- int line_count = 1;
- if (is_multiline)
- {
- for (const char* s = text_begin; (s = (const char*)ImMemchr(s, '\n', (size_t)(text_end - s))) != NULL; s++)
- {
- if (cursor_line_no == -1 && s >= cursor_ptr) { cursor_line_no = line_count; }
- if (selmin_line_no == -1 && s >= selmin_ptr) { selmin_line_no = line_count; }
- line_count++;
- }
- }
- if (cursor_line_no == -1)
- cursor_line_no = line_count;
- if (selmin_line_no == -1)
- selmin_line_no = line_count;
-
- // Calculate 2d position by finding the beginning of the line and measuring distance
- cursor_offset.x = InputTextCalcTextSize(&g, ImStrbol(cursor_ptr, text_begin), cursor_ptr).x;
- cursor_offset.y = cursor_line_no * g.FontSize;
- if (selmin_line_no >= 0)
- {
- select_start_offset.x = InputTextCalcTextSize(&g, ImStrbol(selmin_ptr, text_begin), selmin_ptr).x;
- select_start_offset.y = selmin_line_no * g.FontSize;
- }
-
- // Store text height (note that we haven't calculated text width at all, see GitHub issues #383, #1224)
- if (is_multiline)
- text_size = ImVec2(inner_size.x, line_count * g.FontSize);
- }
+ IM_ASSERT(state != NULL);
+ state->LineCount = line_count;
// Scroll
+ float new_scroll_y = scroll_y;
if (render_cursor && state->CursorFollow)
{
// Horizontal scroll in chunks of quarter width
@@ -5276,7 +5447,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_
}
else
{
- state->Scroll.y = 0.0f;
+ state->Scroll.x = 0.0f;
}
// Vertical scroll
@@ -5284,104 +5455,110 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_
{
// Test if cursor is vertically visible
if (cursor_offset.y - g.FontSize < scroll_y)
- scroll_y = ImMax(0.0f, cursor_offset.y - g.FontSize);
+ new_scroll_y = ImMax(0.0f, cursor_offset.y - g.FontSize);
else if (cursor_offset.y - (inner_size.y - style.FramePadding.y * 2.0f) >= scroll_y)
- scroll_y = cursor_offset.y - inner_size.y + style.FramePadding.y * 2.0f;
- const float scroll_max_y = ImMax((text_size.y + style.FramePadding.y * 2.0f) - inner_size.y, 0.0f);
- scroll_y = ImClamp(scroll_y, 0.0f, scroll_max_y);
- draw_pos.y += (draw_window->Scroll.y - scroll_y); // Manipulate cursor pos immediately avoid a frame of lag
- draw_window->Scroll.y = scroll_y;
+ new_scroll_y = cursor_offset.y - inner_size.y + style.FramePadding.y * 2.0f;
}
-
state->CursorFollow = false;
}
+ if (state->CursorCenterY)
+ {
+ if (is_multiline)
+ new_scroll_y = cursor_offset.y - g.FontSize - (inner_size.y * 0.5f - style.FramePadding.y);
+ state->CursorCenterY = false;
+ render_cursor = false;
+ }
+ if (new_scroll_y != scroll_y)
+ {
+ const float scroll_max_y = ImMax((text_size.y + style.FramePadding.y * 2.0f) - inner_size.y, 0.0f);
+ scroll_y = ImClamp(new_scroll_y, 0.0f, scroll_max_y);
+ draw_pos.y += (draw_window->Scroll.y - scroll_y); // Manipulate cursor pos immediately avoid a frame of lag
+ draw_window->Scroll.y = scroll_y;
+ CalcClipRectVisibleItemsY(clip_rect, draw_pos, g.FontSize, &line_visible_n0, &line_visible_n1);
+ line_visible_n1 = ImMin(line_visible_n1, line_count);
+ }
// Draw selection
- const ImVec2 draw_scroll = ImVec2(state->Scroll.x, 0.0f);
+ draw_scroll.x = state->Scroll.x;
if (render_selection)
{
- const char* text_selected_begin = text_begin + ImMin(state->Stb->select_start, state->Stb->select_end);
- const char* text_selected_end = text_begin + ImMax(state->Stb->select_start, state->Stb->select_end);
+ const ImU32 bg_color = GetColorU32(ImGuiCol_TextSelectedBg, render_cursor ? 1.0f : 0.6f); // FIXME: current code flow mandate that render_cursor is always true here, we are leaving the transparent one for tests.
+ const float bg_offy_up = is_multiline ? 0.0f : -1.0f; // FIXME: those offsets should be part of the style? they don't play so well with multi-line selection.
+ const float bg_offy_dn = is_multiline ? 0.0f : 2.0f;
+ const float bg_eol_width = IM_TRUNC(g.FontBaked->GetCharAdvance((ImWchar)' ') * 0.50f); // So we can see selected empty lines
- ImU32 bg_color = GetColorU32(ImGuiCol_TextSelectedBg, render_cursor ? 1.0f : 0.6f); // FIXME: current code flow mandate that render_cursor is always true here, we are leaving the transparent one for tests.
- float bg_offy_up = is_multiline ? 0.0f : -1.0f; // FIXME: those offsets should be part of the style? they don't play so well with multi-line selection.
- float bg_offy_dn = is_multiline ? 0.0f : 2.0f;
- ImVec2 rect_pos = draw_pos + select_start_offset - draw_scroll;
- for (const char* p = text_selected_begin; p < text_selected_end; )
+ const char* text_selected_begin = buf_display + ImMin(state->Stb->select_start, state->Stb->select_end);
+ const char* text_selected_end = buf_display + ImMax(state->Stb->select_start, state->Stb->select_end);
+ for (int line_n = line_visible_n0; line_n < line_visible_n1; line_n++)
{
- if (rect_pos.y > clip_rect.w + g.FontSize)
- break;
- if (rect_pos.y < clip_rect.y)
- {
- p = (const char*)ImMemchr((void*)p, '\n', text_selected_end - p);
- p = p ? p + 1 : text_selected_end;
- }
- else
- {
- ImVec2 rect_size = InputTextCalcTextSize(&g, p, text_selected_end, &p, NULL, true);
- if (rect_size.x <= 0.0f) rect_size.x = IM_TRUNC(g.Font->GetCharAdvance((ImWchar)' ') * 0.50f); // So we can see selected empty lines
- ImRect rect(rect_pos + ImVec2(0.0f, bg_offy_up - g.FontSize), rect_pos + ImVec2(rect_size.x, bg_offy_dn));
- rect.ClipWith(clip_rect);
- if (rect.Overlaps(clip_rect))
- draw_window->DrawList->AddRectFilled(rect.Min, rect.Max, bg_color);
- rect_pos.x = draw_pos.x - draw_scroll.x;
- }
- rect_pos.y += g.FontSize;
- }
- }
+ const char* p = line_index->get_line_begin(buf_display, line_n);
+ const char* p_eol = line_index->get_line_end(buf_display, line_n);
+ const bool p_eol_is_wrap = (p_eol < buf_display_end && *p_eol != '\n');
+ if (p_eol_is_wrap)
+ p_eol++;
+ const char* line_selected_begin = (text_selected_begin > p) ? text_selected_begin : p;
+ const char* line_selected_end = (text_selected_end < p_eol) ? text_selected_end : p_eol;
- // We test for 'buf_display_max_length' as a way to avoid some pathological cases (e.g. single-line 1 MB string) which would make ImDrawList crash.
- // FIXME-OPT: Multiline could submit a smaller amount of contents to AddText() since we already iterated through it.
- if (is_multiline || (buf_display_end - buf_display) < buf_display_max_length)
- {
- ImU32 col = GetColorU32(is_displaying_hint ? ImGuiCol_TextDisabled : ImGuiCol_Text);
- draw_window->DrawList->AddText(g.Font, g.FontSize, draw_pos - draw_scroll, col, buf_display, buf_display_end, 0.0f, is_multiline ? NULL : &clip_rect);
- }
+ float rect_width = 0.0f;
+ if (line_selected_begin < line_selected_end)
+ rect_width += CalcTextSize(line_selected_begin, line_selected_end).x;
+ if (text_selected_begin <= p_eol && text_selected_end > p_eol && !p_eol_is_wrap)
+ rect_width += bg_eol_width; // So we can see selected empty lines
+ if (rect_width == 0.0f)
+ continue;
- // Draw blinking cursor
- if (render_cursor)
- {
- state->CursorAnim += io.DeltaTime;
- bool cursor_is_visible = (!g.IO.ConfigInputTextCursorBlink) || (state->CursorAnim <= 0.0f) || ImFmod(state->CursorAnim, 1.20f) <= 0.80f;
- ImVec2 cursor_screen_pos = ImTrunc(draw_pos + cursor_offset - draw_scroll);
- ImRect cursor_screen_rect(cursor_screen_pos.x, cursor_screen_pos.y - g.FontSize + 0.5f, cursor_screen_pos.x + 1.0f, cursor_screen_pos.y - 1.5f);
- if (cursor_is_visible && cursor_screen_rect.Overlaps(clip_rect))
- draw_window->DrawList->AddLine(cursor_screen_rect.Min, cursor_screen_rect.GetBL(), GetColorU32(ImGuiCol_InputTextCursor), 1.0f); // FIXME-DPI: Cursor thickness (#7031)
-
- // Notify OS of text input position for advanced IME (-1 x offset so that Windows IME can cover our cursor. Bit of an extra nicety.)
- if (!is_readonly)
- {
- g.PlatformImeData.WantVisible = true;
- g.PlatformImeData.InputPos = ImVec2(cursor_screen_pos.x - 1.0f, cursor_screen_pos.y - g.FontSize);
- g.PlatformImeData.InputLineHeight = g.FontSize;
- g.PlatformImeViewport = window->Viewport->ID;
+ ImRect rect;
+ rect.Min.x = draw_pos.x - draw_scroll.x + CalcTextSize(p, line_selected_begin).x;
+ rect.Min.y = draw_pos.y - draw_scroll.y + line_n * g.FontSize;
+ rect.Max.x = rect.Min.x + rect_width;
+ rect.Max.y = rect.Min.y + bg_offy_dn + g.FontSize;
+ rect.Min.y -= bg_offy_up;
+ rect.ClipWith(clip_rect);
+ draw_window->DrawList->AddRectFilled(rect.Min, rect.Max, bg_color);
}
}
}
- else
+
+ // Find render position for right alignment (single-line only)
+ if (g.ActiveId != id && flags & ImGuiInputTextFlags_ElideLeft)
+ draw_pos.x = ImMin(draw_pos.x, frame_bb.Max.x - CalcTextSize(buf_display, NULL).x - style.FramePadding.x);
+ //draw_scroll.x = state->Scroll.x; // Preserve scroll when inactive?
+
+ // Render text
+ if ((is_multiline || (buf_display_end - buf_display) < buf_display_max_length) && (text_col & IM_COL32_A_MASK) && (line_visible_n0 < line_visible_n1))
+ g.Font->RenderText(draw_window->DrawList, g.FontSize,
+ draw_pos - draw_scroll + ImVec2(0.0f, line_visible_n0 * g.FontSize),
+ text_col, clip_rect.AsVec4(),
+ line_index->get_line_begin(buf_display, line_visible_n0),
+ line_index->get_line_end(buf_display, line_visible_n1 - 1),
+ wrap_width, ImDrawTextFlags_WrapKeepBlanks | ImDrawTextFlags_CpuFineClip);
+
+ // Render blinking cursor
+ if (render_cursor)
{
- // Render text only (no selection, no cursor)
- if (is_multiline)
- text_size = ImVec2(inner_size.x, InputTextCalcTextLenAndLineCount(buf_display, &buf_display_end) * g.FontSize); // We don't need width
- else if (!is_displaying_hint && g.ActiveId == id)
- buf_display_end = buf_display + state->TextLen;
- else if (!is_displaying_hint)
- buf_display_end = buf_display + ImStrlen(buf_display);
+ state->CursorAnim += io.DeltaTime;
+ bool cursor_is_visible = (!g.IO.ConfigInputTextCursorBlink) || (state->CursorAnim <= 0.0f) || ImFmod(state->CursorAnim, 1.20f) <= 0.80f;
+ ImVec2 cursor_screen_pos = ImTrunc(draw_pos + cursor_offset - draw_scroll);
+ ImRect cursor_screen_rect(cursor_screen_pos.x, cursor_screen_pos.y - g.FontSize + 0.5f, cursor_screen_pos.x + 1.0f, cursor_screen_pos.y - 1.5f);
+ if (cursor_is_visible && cursor_screen_rect.Overlaps(clip_rect))
+ draw_window->DrawList->AddLine(cursor_screen_rect.Min, cursor_screen_rect.GetBL(), GetColorU32(ImGuiCol_InputTextCursor), 1.0f); // FIXME-DPI: Cursor thickness (#7031)
- if (is_multiline || (buf_display_end - buf_display) < buf_display_max_length)
+ // Notify OS of text input position for advanced IME (-1 x offset so that Windows IME can cover our cursor. Bit of an extra nicety.)
+ // This is required for some backends (SDL3) to start emitting character/text inputs.
+ // As per #6341, make sure we don't set that on the deactivating frame.
+ if (!is_readonly && g.ActiveId == id)
{
- // Find render position for right alignment
- if (flags & ImGuiInputTextFlags_ElideLeft)
- draw_pos.x = ImMin(draw_pos.x, frame_bb.Max.x - CalcTextSize(buf_display, NULL).x - style.FramePadding.x);
-
- const ImVec2 draw_scroll = /*state ? ImVec2(state->Scroll.x, 0.0f) :*/ ImVec2(0.0f, 0.0f); // Preserve scroll when inactive?
- ImU32 col = GetColorU32(is_displaying_hint ? ImGuiCol_TextDisabled : ImGuiCol_Text);
- draw_window->DrawList->AddText(g.Font, g.FontSize, draw_pos - draw_scroll, col, buf_display, buf_display_end, 0.0f, is_multiline ? NULL : &clip_rect);
+ ImGuiPlatformImeData* ime_data = &g.PlatformImeData; // (this is a public struct, passed to io.Platform_SetImeDataFn() handler)
+ ime_data->WantVisible = true;
+ ime_data->WantTextInput = true;
+ ime_data->InputPos = ImVec2(cursor_screen_pos.x - 1.0f, cursor_screen_pos.y - g.FontSize);
+ ime_data->InputLineHeight = g.FontSize;
+ ime_data->ViewportId = window->Viewport->ID;
}
}
if (is_password && !is_displaying_hint)
- PopFont();
+ PopPasswordFont();
if (is_multiline)
{
@@ -5432,8 +5609,10 @@ void ImGui::DebugNodeInputTextState(ImGuiInputTextState* state)
ImStb::StbUndoState* undo_state = &stb_state->undostate;
Text("ID: 0x%08X, ActiveID: 0x%08X", state->ID, g.ActiveId);
DebugLocateItemOnHover(state->ID);
- Text("CurLenA: %d, Cursor: %d, Selection: %d..%d", state->TextLen, stb_state->cursor, stb_state->select_start, stb_state->select_end);
- Text("BufCapacityA: %d", state->BufCapacity);
+ Text("TextLen: %d, Cursor: %d%s, Selection: %d..%d", state->TextLen, stb_state->cursor,
+ (state->Flags & ImGuiInputTextFlags_WordWrap) ? (state->LastMoveDirectionLR == ImGuiDir_Left ? " (L)" : " (R)") : "",
+ stb_state->select_start, stb_state->select_end);
+ Text("BufCapacity: %d, LineCount: %d", state->BufCapacity, state->LineCount);
Text("(Internal Buffer: TextA Size: %d, Capacity: %d)", state->TextA.Size, state->TextA.Capacity);
Text("has_preferred_x: %d (%.2f)", stb_state->has_preferred_x, stb_state->preferred_x);
Text("undo_point: %d, redo_point: %d, undo_char_point: %d, redo_char_point: %d", undo_state->undo_point, undo_state->redo_point, undo_state->undo_char_point, undo_state->redo_char_point);
@@ -6397,6 +6576,7 @@ void ImGui::ColorPickerOptionsPopup(const float* ref_col, ImGuiColorEditFlags fl
// - TreeNodeV()
// - TreeNodeEx()
// - TreeNodeExV()
+// - TreeNodeStoreStackData() [Internal]
// - TreeNodeBehavior() [Internal]
// - TreePush()
// - TreePop()
@@ -6555,18 +6735,26 @@ bool ImGui::TreeNodeUpdateNextOpen(ImGuiID storage_id, ImGuiTreeNodeFlags flags)
// Store ImGuiTreeNodeStackData for just submitted node.
// Currently only supports 32 level deep and we are fine with (1 << Depth) overflowing into a zero, easy to increase.
-static void TreeNodeStoreStackData(ImGuiTreeNodeFlags flags)
+static void TreeNodeStoreStackData(ImGuiTreeNodeFlags flags, float x1)
{
ImGuiContext& g = *GImGui;
ImGuiWindow* window = g.CurrentWindow;
g.TreeNodeStack.resize(g.TreeNodeStack.Size + 1);
- ImGuiTreeNodeStackData* tree_node_data = &g.TreeNodeStack.back();
+ ImGuiTreeNodeStackData* tree_node_data = &g.TreeNodeStack.Data[g.TreeNodeStack.Size - 1];
tree_node_data->ID = g.LastItemData.ID;
tree_node_data->TreeFlags = flags;
tree_node_data->ItemFlags = g.LastItemData.ItemFlags;
tree_node_data->NavRect = g.LastItemData.NavRect;
+
+ // Initially I tried to latch value for GetColorU32(ImGuiCol_TreeLines) but it's not a good trade-off for very large trees.
+ const bool draw_lines = (flags & (ImGuiTreeNodeFlags_DrawLinesFull | ImGuiTreeNodeFlags_DrawLinesToNodes)) != 0;
+ tree_node_data->DrawLinesX1 = draw_lines ? (x1 + g.FontSize * 0.5f + g.Style.FramePadding.x) : +FLT_MAX;
+ tree_node_data->DrawLinesTableColumn = (draw_lines && g.CurrentTable) ? (ImGuiTableColumnIdx)g.CurrentTable->CurrentColumn : -1;
+ tree_node_data->DrawLinesToNodesY2 = -FLT_MAX;
window->DC.TreeHasStackDataDepthMask |= (1 << window->DC.TreeDepth);
+ if (flags & ImGuiTreeNodeFlags_DrawLinesToNodes)
+ window->DC.TreeRecordsClippedNodesY2Mask |= (1 << window->DC.TreeDepth);
}
// When using public API, currently 'id == storage_id' is always true, but we separate the values to facilitate advanced user code doing storage queries outside of UI loop.
@@ -6636,14 +6824,18 @@ bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* l
g.LastItemData.StatusFlags |= ImGuiItemStatusFlags_HasDisplayRect;
g.LastItemData.DisplayRect = frame_bb;
- // If a NavLeft request is happening and ImGuiTreeNodeFlags_NavLeftJumpsBackHere enabled:
+ // If a NavLeft request is happening and ImGuiTreeNodeFlags_NavLeftJumpsToParent enabled:
// Store data for the current depth to allow returning to this node from any child item.
// For this purpose we essentially compare if g.NavIdIsAlive went from 0 to 1 between TreeNode() and TreePop().
- // It will become tempting to enable ImGuiTreeNodeFlags_NavLeftJumpsBackHere by default or move it to ImGuiStyle.
+ // It will become tempting to enable ImGuiTreeNodeFlags_NavLeftJumpsToParent by default or move it to ImGuiStyle.
bool store_tree_node_stack_data = false;
+ if ((flags & ImGuiTreeNodeFlags_DrawLinesMask_) == 0)
+ flags |= g.Style.TreeLinesFlags;
+ const bool draw_tree_lines = (flags & (ImGuiTreeNodeFlags_DrawLinesFull | ImGuiTreeNodeFlags_DrawLinesToNodes)) && (frame_bb.Min.y < window->ClipRect.Max.y) && (g.Style.TreeLinesSize > 0.0f);
if (!(flags & ImGuiTreeNodeFlags_NoTreePushOnOpen))
{
- if ((flags & ImGuiTreeNodeFlags_NavLeftJumpsBackHere) && is_open && !g.NavIdIsAlive)
+ store_tree_node_stack_data = draw_tree_lines;
+ if ((flags & ImGuiTreeNodeFlags_NavLeftJumpsToParent) && !g.NavIdIsAlive)
if (g.NavMoveDir == ImGuiDir_Left && g.NavWindow == window && NavMoveRequestButNoResultYet())
store_tree_node_stack_data = true;
}
@@ -6651,8 +6843,15 @@ bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* l
const bool is_leaf = (flags & ImGuiTreeNodeFlags_Leaf) != 0;
if (!is_visible)
{
- if (store_tree_node_stack_data && is_open)
- TreeNodeStoreStackData(flags); // Call before TreePushOverrideID()
+ if ((flags & ImGuiTreeNodeFlags_DrawLinesToNodes) && (window->DC.TreeRecordsClippedNodesY2Mask & (1 << (window->DC.TreeDepth - 1))))
+ {
+ ImGuiTreeNodeStackData* parent_data = &g.TreeNodeStack.Data[g.TreeNodeStack.Size - 1];
+ parent_data->DrawLinesToNodesY2 = ImMax(parent_data->DrawLinesToNodesY2, window->DC.CursorPos.y); // Don't need to aim to mid Y position as we are clipped anyway.
+ if (frame_bb.Min.y >= window->ClipRect.Max.y)
+ window->DC.TreeRecordsClippedNodesY2Mask &= ~(1 << (window->DC.TreeDepth - 1)); // Done
+ }
+ if (is_open && store_tree_node_stack_data)
+ TreeNodeStoreStackData(flags, text_pos.x - text_offset_x); // Call before TreePushOverrideID()
if (is_open && !(flags & ImGuiTreeNodeFlags_NoTreePushOnOpen))
TreePushOverrideID(id);
IMGUI_TEST_ENGINE_ITEM_INFO(g.LastItemData.ID, label, g.LastItemData.StatusFlags | (is_leaf ? 0 : ImGuiItemStatusFlags_Openable) | (is_open ? ImGuiItemStatusFlags_Opened : 0));
@@ -6698,6 +6897,8 @@ bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* l
button_flags |= ImGuiButtonFlags_PressedOnClickRelease | ImGuiButtonFlags_PressedOnDoubleClick;
else
button_flags |= ImGuiButtonFlags_PressedOnClickRelease;
+ if (flags & ImGuiTreeNodeFlags_NoNavFocus)
+ button_flags |= ImGuiButtonFlags_NoNavFocus;
bool selected = (flags & ImGuiTreeNodeFlags_Selected) != 0;
const bool was_selected = selected;
@@ -6784,6 +6985,8 @@ bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* l
const ImU32 bg_col = GetColorU32((held && hovered) ? ImGuiCol_HeaderActive : hovered ? ImGuiCol_HeaderHovered : ImGuiCol_Header);
RenderFrame(frame_bb.Min, frame_bb.Max, bg_col, true, style.FrameRounding);
RenderNavCursor(frame_bb, id, nav_render_cursor_flags);
+ if (span_all_columns && !span_all_columns_label)
+ TablePopBackgroundChannel();
if (flags & ImGuiTreeNodeFlags_Bullet)
RenderBullet(window->DrawList, ImVec2(text_pos.x - text_offset_x * 0.60f, text_pos.y + g.FontSize * 0.5f), text_col);
else if (!is_leaf)
@@ -6804,6 +7007,8 @@ bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* l
RenderFrame(frame_bb.Min, frame_bb.Max, bg_col, false);
}
RenderNavCursor(frame_bb, id, nav_render_cursor_flags);
+ if (span_all_columns && !span_all_columns_label)
+ TablePopBackgroundChannel();
if (flags & ImGuiTreeNodeFlags_Bullet)
RenderBullet(window->DrawList, ImVec2(text_pos.x - text_offset_x * 0.5f, text_pos.y + g.FontSize * 0.5f), text_col);
else if (!is_leaf)
@@ -6812,8 +7017,8 @@ bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* l
LogSetNextTextDecoration(">", NULL);
}
- if (span_all_columns && !span_all_columns_label)
- TablePopBackgroundChannel();
+ if (draw_tree_lines)
+ TreeNodeDrawLineToChildNode(ImVec2(text_pos.x - text_offset_x + padding.x, text_pos.y + g.FontSize * 0.5f));
// Label
if (display_frame)
@@ -6825,8 +7030,8 @@ bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* l
TablePopBackgroundChannel();
}
- if (store_tree_node_stack_data && is_open)
- TreeNodeStoreStackData(flags); // Call before TreePushOverrideID()
+ if (is_open && store_tree_node_stack_data)
+ TreeNodeStoreStackData(flags, text_pos.x - text_offset_x); // Call before TreePushOverrideID()
if (is_open && !(flags & ImGuiTreeNodeFlags_NoTreePushOnOpen))
TreePushOverrideID(id); // Could use TreePush(label) but this avoid computing twice
@@ -6834,6 +7039,64 @@ bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* l
return is_open;
}
+// Draw horizontal line from our parent node
+// This is only called for visible child nodes so we are not too fussy anymore about performances
+void ImGui::TreeNodeDrawLineToChildNode(const ImVec2& target_pos)
+{
+ ImGuiContext& g = *GImGui;
+ ImGuiWindow* window = g.CurrentWindow;
+ if (window->DC.TreeDepth == 0 || (window->DC.TreeHasStackDataDepthMask & (1 << (window->DC.TreeDepth - 1))) == 0)
+ return;
+
+ ImGuiTreeNodeStackData* parent_data = &g.TreeNodeStack.Data[g.TreeNodeStack.Size - 1];
+ float x1 = ImTrunc(parent_data->DrawLinesX1);
+ float x2 = ImTrunc(target_pos.x - g.Style.ItemInnerSpacing.x);
+ float y = ImTrunc(target_pos.y);
+ float rounding = (g.Style.TreeLinesRounding > 0.0f) ? ImMin(x2 - x1, g.Style.TreeLinesRounding) : 0.0f;
+ parent_data->DrawLinesToNodesY2 = ImMax(parent_data->DrawLinesToNodesY2, y - rounding);
+ if (x1 >= x2)
+ return;
+ if (rounding > 0.0f)
+ {
+ x1 += 0.5f + rounding;
+ window->DrawList->PathArcToFast(ImVec2(x1, y - rounding), rounding, 6, 3);
+ if (x1 < x2)
+ window->DrawList->PathLineTo(ImVec2(x2, y));
+ window->DrawList->PathStroke(GetColorU32(ImGuiCol_TreeLines), ImDrawFlags_None, g.Style.TreeLinesSize);
+ }
+ else
+ {
+ window->DrawList->AddLine(ImVec2(x1, y), ImVec2(x2, y), GetColorU32(ImGuiCol_TreeLines), g.Style.TreeLinesSize);
+ }
+}
+
+// Draw vertical line of the hierarchy
+void ImGui::TreeNodeDrawLineToTreePop(const ImGuiTreeNodeStackData* data)
+{
+ ImGuiContext& g = *GImGui;
+ ImGuiWindow* window = g.CurrentWindow;
+ float y1 = ImMax(data->NavRect.Max.y, window->ClipRect.Min.y);
+ float y2 = data->DrawLinesToNodesY2;
+ if (data->TreeFlags & ImGuiTreeNodeFlags_DrawLinesFull)
+ {
+ float y2_full = window->DC.CursorPos.y;
+ if (g.CurrentTable)
+ y2_full = ImMax(g.CurrentTable->RowPosY2, y2_full);
+ y2_full = ImTrunc(y2_full - g.Style.ItemSpacing.y - g.FontSize * 0.5f);
+ if (y2 + (g.Style.ItemSpacing.y + g.Style.TreeLinesRounding) < y2_full) // FIXME: threshold to use ToNodes Y2 instead of Full Y2 when close by ItemSpacing.y
+ y2 = y2_full;
+ }
+ y2 = ImMin(y2, window->ClipRect.Max.y);
+ if (y2 <= y1)
+ return;
+ float x = ImTrunc(data->DrawLinesX1);
+ if (data->DrawLinesTableColumn != -1)
+ TablePushColumnChannel(data->DrawLinesTableColumn);
+ window->DrawList->AddLine(ImVec2(x, y1), ImVec2(x, y2), GetColorU32(ImGuiCol_TreeLines), g.Style.TreeLinesSize);
+ if (data->DrawLinesTableColumn != -1)
+ TablePopColumnChannel();
+}
+
void ImGui::TreePush(const char* str_id)
{
ImGuiWindow* window = GetCurrentWindow();
@@ -6868,18 +7131,23 @@ void ImGui::TreePop()
window->DC.TreeDepth--;
ImU32 tree_depth_mask = (1 << window->DC.TreeDepth);
- if (window->DC.TreeHasStackDataDepthMask & tree_depth_mask) // Only set during request
+ if (window->DC.TreeHasStackDataDepthMask & tree_depth_mask)
{
- ImGuiTreeNodeStackData* data = &g.TreeNodeStack.back();
+ const ImGuiTreeNodeStackData* data = &g.TreeNodeStack.Data[g.TreeNodeStack.Size - 1];
IM_ASSERT(data->ID == window->IDStack.back());
- if (data->TreeFlags & ImGuiTreeNodeFlags_NavLeftJumpsBackHere)
- {
- // Handle Left arrow to move to parent tree node (when ImGuiTreeNodeFlags_NavLeftJumpsBackHere is enabled)
+
+ // Handle Left arrow to move to parent tree node (when ImGuiTreeNodeFlags_NavLeftJumpsToParent is enabled)
+ if (data->TreeFlags & ImGuiTreeNodeFlags_NavLeftJumpsToParent)
if (g.NavIdIsAlive && g.NavMoveDir == ImGuiDir_Left && g.NavWindow == window && NavMoveRequestButNoResultYet())
NavMoveRequestResolveWithPastTreeNode(&g.NavMoveResultLocal, data);
- }
+
+ // Draw hierarchy lines
+ if (data->DrawLinesX1 != +FLT_MAX && window->DC.CursorPos.y >= window->ClipRect.Min.y)
+ TreeNodeDrawLineToTreePop(data);
+
g.TreeNodeStack.pop_back();
window->DC.TreeHasStackDataDepthMask &= ~tree_depth_mask;
+ window->DC.TreeRecordsClippedNodesY2Mask &= ~tree_depth_mask;
}
IM_ASSERT(window->IDStack.Size > 1); // There should always be 1 element in the IDStack (pushed during window creation). If this triggers you called TreePop/PopID too much.
@@ -7072,6 +7340,7 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl
bool hovered, held;
bool pressed = ButtonBehavior(bb, id, &hovered, &held, button_flags);
+ bool auto_selected = false;
// Multi-selection support (footer)
if (is_multi_select)
@@ -7088,8 +7357,8 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl
// - (2) usage will fail with clipped items
// The multi-select API aim to fix those issues, e.g. may be replaced with a BeginSelection() API.
if ((flags & ImGuiSelectableFlags_SelectOnNav) && g.NavJustMovedToId != 0 && g.NavJustMovedToFocusScopeId == g.CurrentFocusScopeId)
- if (g.NavJustMovedToId == id)
- selected = pressed = true;
+ if (g.NavJustMovedToId == id && (g.NavJustMovedToKeyMods & ImGuiMod_Ctrl) == 0)
+ selected = pressed = auto_selected = true;
}
// Update NavId when clicking or when Hovering (this doesn't happen on most widgets), so navigation can be resumed with keyboard/gamepad
@@ -7140,7 +7409,7 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl
RenderTextClipped(pos, ImVec2(ImMin(pos.x + size.x, window->WorkRect.Max.x), pos.y + size.y), label, NULL, &label_size, style.SelectableTextAlign, &bb);
// Automatically close popups
- if (pressed && (window->Flags & ImGuiWindowFlags_Popup) && !(flags & ImGuiSelectableFlags_NoAutoClosePopups) && (g.LastItemData.ItemFlags & ImGuiItemFlags_AutoClosePopups))
+ if (pressed && !auto_selected && (window->Flags & ImGuiWindowFlags_Popup) && !(flags & ImGuiSelectableFlags_NoAutoClosePopups) && (g.LastItemData.ItemFlags & ImGuiItemFlags_AutoClosePopups))
CloseCurrentPopup();
if (disabled_item && !disabled_global)
@@ -8218,7 +8487,7 @@ void ImGuiSelectionBasicStorage::ApplyRequests(ImGuiMultiSelectIO* ms_io)
// - Optimized select can append unsorted, then sort in a second pass. Optimized unselect can clear in-place then compact in a second pass.
// - A more optimal version wouldn't even use ImGuiStorage but directly a ImVector to reduce bandwidth, but this is a reasonable trade off to reuse code.
// - There are many ways this could be better optimized. The worse case scenario being: using BoxSelect2d in a grid, box-select scrolling down while wiggling
- // left and right: it affects coarse clipping + can emit multiple SetRange with 1 item each.)
+ // left and right: it affects coarse clipping + can emit multiple SetRange with 1 item each.
// FIXME-OPT: For each block of consecutive SetRange request:
// - add all requests to a sorted list, store ID, selected, offset in ImGuiStorage.
// - rewrite sorted storage a single time.
@@ -8739,6 +9008,7 @@ void ImGui::EndMenuBar()
PopClipRect();
PopID();
+ IM_MSVC_WARNING_SUPPRESS(6011); // Static Analysis false positive "warning C6011: Dereferencing NULL pointer 'window'"
window->DC.MenuBarOffset.x = window->DC.CursorPos.x - window->Pos.x; // Save horizontal position so next append can reuse it. This is kinda equivalent to a per-layer CursorPos.
// FIXME: Extremely confusing, cleanup by (a) working on WorkRect stack system (b) not using a Group confusingly here.
@@ -8937,7 +9207,7 @@ bool ImGui::BeginMenuEx(const char* label, const char* icon, bool enabled)
{
// Menu inside a regular/vertical menu
// (In a typical menu window where all items are BeginMenu() or MenuItem() calls, extra_w will always be 0.0f.
- // Only when they are other items sticking out we're going to add spacing, yet only register minimum width into the layout system.
+ // Only when they are other items sticking out we're going to add spacing, yet only register minimum width into the layout system.)
popup_pos = ImVec2(pos.x, pos.y - style.WindowPadding.y);
float icon_w = (icon && icon[0]) ? CalcTextSize(icon, NULL).x : 0.0f;
float checkmark_w = IM_TRUNC(g.FontSize * 1.20f);
@@ -9144,7 +9414,7 @@ bool ImGui::MenuItemEx(const char* label, const char* icon, const char* shortcut
{
// Menu item inside a vertical menu
// (In a typical menu window where all items are BeginMenu() or MenuItem() calls, extra_w will always be 0.0f.
- // Only when they are other items sticking out we're going to add spacing, yet only register minimum width into the layout system.
+ // Only when they are other items sticking out we're going to add spacing, yet only register minimum width into the layout system.)
float icon_w = (icon && icon[0]) ? CalcTextSize(icon, NULL).x : 0.0f;
float shortcut_w = (shortcut && shortcut[0]) ? CalcTextSize(shortcut, NULL).x : 0.0f;
float checkmark_w = IM_TRUNC(g.FontSize * 1.20f);
@@ -9224,6 +9494,7 @@ struct ImGuiTabBarSection
{
int TabCount; // Number of tabs in this section.
float Width; // Sum of width of tabs in this section (after shrinking down)
+ float WidthAfterShrinkMinWidth;
float Spacing; // Horizontal spacing at the end of the section.
ImGuiTabBarSection() { memset(this, 0, sizeof(*this)); }
@@ -9284,6 +9555,19 @@ static ImGuiPtrOrIndex GetTabBarRefFromTabBar(ImGuiTabBar* tab_bar)
return ImGuiPtrOrIndex(tab_bar);
}
+ImGuiTabBar* ImGui::TabBarFindByID(ImGuiID id)
+{
+ ImGuiContext& g = *GImGui;
+ return g.TabBars.GetByKey(id);
+}
+
+// Remove TabBar data (currently only used by TestEngine)
+void ImGui::TabBarRemove(ImGuiTabBar* tab_bar)
+{
+ ImGuiContext& g = *GImGui;
+ g.TabBars.Remove(tab_bar->ID, tab_bar);
+}
+
bool ImGui::BeginTabBar(const char* str_id, ImGuiTabBarFlags flags)
{
ImGuiContext& g = *GImGui;
@@ -9295,8 +9579,8 @@ bool ImGui::BeginTabBar(const char* str_id, ImGuiTabBarFlags flags)
ImGuiTabBar* tab_bar = g.TabBars.GetOrAddByKey(id);
ImRect tab_bar_bb = ImRect(window->DC.CursorPos.x, window->DC.CursorPos.y, window->WorkRect.Max.x, window->DC.CursorPos.y + g.FontSize + g.Style.FramePadding.y * 2);
tab_bar->ID = id;
- tab_bar->SeparatorMinX = tab_bar->BarRect.Min.x - IM_TRUNC(window->WindowPadding.x * 0.5f);
- tab_bar->SeparatorMaxX = tab_bar->BarRect.Max.x + IM_TRUNC(window->WindowPadding.x * 0.5f);
+ tab_bar->SeparatorMinX = tab_bar_bb.Min.x - IM_TRUNC(window->WindowPadding.x * 0.5f);
+ tab_bar->SeparatorMaxX = tab_bar_bb.Max.x + IM_TRUNC(window->WindowPadding.x * 0.5f);
//if (g.NavWindow && IsWindowChildOf(g.NavWindow, window, false, false))
flags |= ImGuiTabBarFlags_IsFocused;
return BeginTabBarEx(tab_bar, tab_bar_bb, flags);
@@ -9417,6 +9701,10 @@ static void ImGui::TabBarLayout(ImGuiTabBar* tab_bar)
ImGuiContext& g = *GImGui;
tab_bar->WantLayout = false;
+ // Track selected tab when resizing our parent down
+ const bool scroll_to_selected_tab = (tab_bar->BarRectPrevWidth > tab_bar->BarRect.GetWidth());
+ tab_bar->BarRectPrevWidth = tab_bar->BarRect.GetWidth();
+
// Garbage collect by compacting list
// Detect if we need to sort out tab list (e.g. in rare case where a tab changed section)
int tab_dst_n = 0;
@@ -9493,6 +9781,9 @@ static void ImGui::TabBarLayout(ImGuiTabBar* tab_bar)
int shrink_buffer_indexes[3] = { 0, sections[0].TabCount + sections[2].TabCount, sections[0].TabCount };
g.ShrinkWidthBuffer.resize(tab_bar->Tabs.Size);
+ // Minimum shrink width
+ const float shrink_min_width = (tab_bar->Flags & ImGuiTabBarFlags_FittingPolicyMixed) ? g.Style.TabMinWidthShrink : 1.0f;
+
// Compute ideal tabs widths + store them into shrink buffer
ImGuiTabItem* most_recently_selected_tab = NULL;
int curr_section_n = -1;
@@ -9515,10 +9806,13 @@ static void ImGui::TabBarLayout(ImGuiTabBar* tab_bar)
const char* tab_name = TabBarGetTabName(tab_bar, tab);
const bool has_close_button_or_unsaved_marker = (tab->Flags & ImGuiTabItemFlags_NoCloseButton) == 0 || (tab->Flags & ImGuiTabItemFlags_UnsavedDocument);
tab->ContentWidth = (tab->RequestedWidth >= 0.0f) ? tab->RequestedWidth : TabItemCalcSize(tab_name, has_close_button_or_unsaved_marker).x;
+ if ((tab->Flags & ImGuiTabItemFlags_Button) == 0)
+ tab->ContentWidth = ImMax(tab->ContentWidth, g.Style.TabMinWidthBase);
int section_n = TabItemGetSectionIdx(tab);
ImGuiTabBarSection* section = §ions[section_n];
section->Width += tab->ContentWidth + (section_n == curr_section_n ? g.Style.ItemInnerSpacing.x : 0.0f);
+ section->WidthAfterShrinkMinWidth += ImMin(tab->ContentWidth, shrink_min_width) + (section_n == curr_section_n ? g.Style.ItemInnerSpacing.x : 0.0f);
curr_section_n = section_n;
// Store data so we can build an array sorted by width if we need to shrink tabs down
@@ -9530,19 +9824,28 @@ static void ImGui::TabBarLayout(ImGuiTabBar* tab_bar)
}
// Compute total ideal width (used for e.g. auto-resizing a window)
+ float width_all_tabs_after_min_width_shrink = 0.0f;
tab_bar->WidthAllTabsIdeal = 0.0f;
for (int section_n = 0; section_n < 3; section_n++)
+ {
tab_bar->WidthAllTabsIdeal += sections[section_n].Width + sections[section_n].Spacing;
+ width_all_tabs_after_min_width_shrink += sections[section_n].WidthAfterShrinkMinWidth + sections[section_n].Spacing;
+ }
// Horizontal scrolling buttons
- // (note that TabBarScrollButtons() will alter BarRect.Max.x)
- if ((tab_bar->WidthAllTabsIdeal > tab_bar->BarRect.GetWidth() && tab_bar->Tabs.Size > 1) && !(tab_bar->Flags & ImGuiTabBarFlags_NoTabListScrollingButtons) && (tab_bar->Flags & ImGuiTabBarFlags_FittingPolicyScroll))
+ // Important: note that TabBarScrollButtons() will alter BarRect.Max.x.
+ const bool can_scroll = (tab_bar->Flags & ImGuiTabBarFlags_FittingPolicyScroll) || (tab_bar->Flags & ImGuiTabBarFlags_FittingPolicyMixed);
+ const float width_all_tabs_to_use_for_scroll = (tab_bar->Flags & ImGuiTabBarFlags_FittingPolicyScroll) ? tab_bar->WidthAllTabs : width_all_tabs_after_min_width_shrink;
+ tab_bar->ScrollButtonEnabled = ((width_all_tabs_to_use_for_scroll > tab_bar->BarRect.GetWidth() && tab_bar->Tabs.Size > 1) && !(tab_bar->Flags & ImGuiTabBarFlags_NoTabListScrollingButtons) && can_scroll);
+ if (tab_bar->ScrollButtonEnabled)
if (ImGuiTabItem* scroll_and_select_tab = TabBarScrollingButtons(tab_bar))
{
scroll_to_tab_id = scroll_and_select_tab->ID;
if ((scroll_and_select_tab->Flags & ImGuiTabItemFlags_Button) == 0)
tab_bar->SelectedTabId = scroll_to_tab_id;
}
+ if (scroll_to_tab_id == 0 && scroll_to_selected_tab)
+ scroll_to_tab_id = tab_bar->SelectedTabId;
// Shrink widths if full tabs don't fit in their allocated space
float section_0_w = sections[0].Width + sections[0].Spacing;
@@ -9556,11 +9859,12 @@ static void ImGui::TabBarLayout(ImGuiTabBar* tab_bar)
width_excess = (section_0_w + section_2_w) - tab_bar->BarRect.GetWidth(); // Excess used to shrink leading/trailing section
// With ImGuiTabBarFlags_FittingPolicyScroll policy, we will only shrink leading/trailing if the central section is not visible anymore
- if (width_excess >= 1.0f && ((tab_bar->Flags & ImGuiTabBarFlags_FittingPolicyResizeDown) || !central_section_is_visible))
+ const bool can_shrink = (tab_bar->Flags & ImGuiTabBarFlags_FittingPolicyShrink) || (tab_bar->Flags & ImGuiTabBarFlags_FittingPolicyMixed);
+ if (width_excess >= 1.0f && (can_shrink || !central_section_is_visible))
{
int shrink_data_count = (central_section_is_visible ? sections[1].TabCount : sections[0].TabCount + sections[2].TabCount);
int shrink_data_offset = (central_section_is_visible ? sections[0].TabCount + sections[2].TabCount : 0);
- ShrinkWidths(g.ShrinkWidthBuffer.Data + shrink_data_offset, shrink_data_count, width_excess);
+ ShrinkWidths(g.ShrinkWidthBuffer.Data + shrink_data_offset, shrink_data_count, width_excess, shrink_min_width);
// Apply shrunk values into tabs and sections
for (int tab_n = shrink_data_offset; tab_n < shrink_data_offset + shrink_data_count; tab_n++)
@@ -9603,7 +9907,8 @@ static void ImGui::TabBarLayout(ImGuiTabBar* tab_bar)
tab_bar->TabsNames.Buf.resize(0);
// If we have lost the selected tab, select the next most recently active one
- if (found_selected_tab_id == false)
+ const bool tab_bar_appearing = (tab_bar->PrevFrameVisible + 1 < g.FrameCount);
+ if (found_selected_tab_id == false && !tab_bar_appearing)
tab_bar->SelectedTabId = 0;
if (tab_bar->SelectedTabId == 0 && tab_bar->NextSelectedTabId == 0 && most_recently_selected_tab != NULL)
scroll_to_tab_id = tab_bar->SelectedTabId = most_recently_selected_tab->ID;
@@ -9619,7 +9924,7 @@ static void ImGui::TabBarLayout(ImGuiTabBar* tab_bar)
// Apply request requests
if (scroll_to_tab_id != 0)
TabBarScrollToTab(tab_bar, scroll_to_tab_id, sections);
- else if ((tab_bar->Flags & ImGuiTabBarFlags_FittingPolicyScroll) && IsMouseHoveringRect(tab_bar->BarRect.Min, tab_bar->BarRect.Max, true) && IsWindowContentHoverable(g.CurrentWindow))
+ else if (tab_bar->ScrollButtonEnabled && IsMouseHoveringRect(tab_bar->BarRect.Min, tab_bar->BarRect.Max, true) && IsWindowContentHoverable(g.CurrentWindow))
{
const float wheel = g.IO.MouseWheelRequestAxisSwap ? g.IO.MouseWheel : g.IO.MouseWheelH;
const ImGuiKey wheel_key = g.IO.MouseWheelRequestAxisSwap ? ImGuiKey_MouseWheelY : ImGuiKey_MouseWheelX;
@@ -9930,7 +10235,7 @@ static ImGuiTabItem* ImGui::TabBarScrollingButtons(ImGuiTabBar* tab_bar)
PushStyleColor(ImGuiCol_Text, arrow_col);
PushStyleColor(ImGuiCol_Button, ImVec4(0, 0, 0, 0));
- PushItemFlag(ImGuiItemFlags_ButtonRepeat, true);
+ PushItemFlag(ImGuiItemFlags_ButtonRepeat | ImGuiItemFlags_NoNav, true);
const float backup_repeat_delay = g.IO.KeyRepeatDelay;
const float backup_repeat_rate = g.IO.KeyRepeatRate;
g.IO.KeyRepeatDelay = 0.250f;
@@ -10502,13 +10807,12 @@ void ImGui::TabItemLabelAndCloseButton(ImDrawList* draw_list, const ImRect& bb,
#endif
// Render text label (with clipping + alpha gradient) + unsaved marker
- ImRect text_pixel_clip_bb(bb.Min.x + frame_padding.x, bb.Min.y + frame_padding.y, bb.Max.x - frame_padding.x, bb.Max.y);
- ImRect text_ellipsis_clip_bb = text_pixel_clip_bb;
+ ImRect text_ellipsis_clip_bb(bb.Min.x + frame_padding.x, bb.Min.y + frame_padding.y, bb.Max.x - frame_padding.x, bb.Max.y);
// Return clipped state ignoring the close button
if (out_text_clipped)
{
- *out_text_clipped = (text_ellipsis_clip_bb.Min.x + label_size.x) > text_pixel_clip_bb.Max.x;
+ *out_text_clipped = (text_ellipsis_clip_bb.Min.x + label_size.x) > text_ellipsis_clip_bb.Max.x;
//draw_list->AddCircle(text_ellipsis_clip_bb.Min, 3.0f, *out_text_clipped ? IM_COL32(255, 0, 0, 255) : IM_COL32(0, 255, 0, 255));
}
@@ -10554,15 +10858,22 @@ void ImGui::TabItemLabelAndCloseButton(ImDrawList* draw_list, const ImRect& bb,
// This is all rather complicated
// (the main idea is that because the close button only appears on hover, we don't want it to alter the ellipsis position)
// FIXME: if FramePadding is noticeably large, ellipsis_max_x will be wrong here (e.g. #3497), maybe for consistency that parameter of RenderTextEllipsis() shouldn't exist..
- float ellipsis_max_x = close_button_visible ? text_pixel_clip_bb.Max.x : bb.Max.x - 1.0f;
+ float ellipsis_max_x = text_ellipsis_clip_bb.Max.x;
if (close_button_visible || unsaved_marker_visible)
{
- text_pixel_clip_bb.Max.x -= close_button_visible ? (button_sz) : (button_sz * 0.80f);
- text_ellipsis_clip_bb.Max.x -= unsaved_marker_visible ? (button_sz * 0.80f) : 0.0f;
- ellipsis_max_x = text_pixel_clip_bb.Max.x;
+ const bool visible_without_hover = unsaved_marker_visible || (is_contents_visible ? g.Style.TabCloseButtonMinWidthSelected : g.Style.TabCloseButtonMinWidthUnselected) < 0.0f;
+ if (visible_without_hover)
+ {
+ text_ellipsis_clip_bb.Max.x -= button_sz * 0.90f;
+ ellipsis_max_x -= button_sz * 0.90f;
+ }
+ else
+ {
+ text_ellipsis_clip_bb.Max.x -= button_sz * 1.00f;
+ }
}
LogSetNextTextDecoration("/", "\\");
- RenderTextEllipsis(draw_list, text_ellipsis_clip_bb.Min, text_ellipsis_clip_bb.Max, text_pixel_clip_bb.Max.x, ellipsis_max_x, label, NULL, &label_size);
+ RenderTextEllipsis(draw_list, text_ellipsis_clip_bb.Min, text_ellipsis_clip_bb.Max, ellipsis_max_x, label, NULL, &label_size);
#if 0
if (!is_contents_visible)
diff --git a/imstb_textedit.h b/imstb_textedit.h
index b7a761c85..c41078c8a 100644
--- a/imstb_textedit.h
+++ b/imstb_textedit.h
@@ -141,6 +141,7 @@
// with previous char)
// STB_TEXTEDIT_KEYTOTEXT(k) maps a keyboard input to an insertable character
// (return type is int, -1 means not valid to insert)
+// (not supported if you want to use UTF-8, see below)
// STB_TEXTEDIT_GETCHAR(obj,i) returns the i'th character of obj, 0-based
// STB_TEXTEDIT_NEWLINE the character returned by _GETCHAR() we recognize
// as manually wordwrapping for end-of-line positioning
@@ -178,6 +179,13 @@
// STB_TEXTEDIT_K_TEXTSTART2 secondary keyboard input to move cursor to start of text
// STB_TEXTEDIT_K_TEXTEND2 secondary keyboard input to move cursor to end of text
//
+// To support UTF-8:
+//
+// STB_TEXTEDIT_GETPREVCHARINDEX returns index of previous character
+// STB_TEXTEDIT_GETNEXTCHARINDEX returns index of next character
+// Do NOT define STB_TEXTEDIT_KEYTOTEXT.
+// Instead, call stb_textedit_text() directly for text contents.
+//
// Keyboard input must be encoded as a single integer value; e.g. a character code
// and some bitflags that represent shift states. to simplify the interface, SHIFT must
// be a bitflag, so we can test the shifted state of cursor movements to allow selection,
@@ -250,8 +258,10 @@
// if the STB_TEXTEDIT_KEYTOTEXT function is defined, selected keys are
// transformed into text and stb_textedit_text() is automatically called.
//
-// text: [DEAR IMGUI] added 2024-09
-// call this to text inputs sent to the textfield.
+// text: (added 2025)
+// call this to directly send text input the textfield, which is required
+// for UTF-8 support, because stb_textedit_key() + STB_TEXTEDIT_KEYTOTEXT()
+// cannot infer text length.
//
//
// When rendering, you can read the cursor position and selection state from
@@ -400,6 +410,16 @@ typedef struct
#define IMSTB_TEXTEDIT_memmove memmove
#endif
+// [DEAR IMGUI]
+// Functions must be implemented for UTF8 support
+// Code in this file that uses those functions is modified for [DEAR IMGUI] and deviates from the original stb_textedit.
+// There is not necessarily a '[DEAR IMGUI]' at the usage sites.
+#ifndef IMSTB_TEXTEDIT_GETPREVCHARINDEX
+#define IMSTB_TEXTEDIT_GETPREVCHARINDEX(OBJ, IDX) ((IDX) - 1)
+#endif
+#ifndef IMSTB_TEXTEDIT_GETNEXTCHARINDEX
+#define IMSTB_TEXTEDIT_GETNEXTCHARINDEX(OBJ, IDX) ((IDX) + 1)
+#endif
/////////////////////////////////////////////////////////////////////////////
//
@@ -407,7 +427,7 @@ typedef struct
//
// traverse the layout to locate the nearest character to a display position
-static int stb_text_locate_coord(IMSTB_TEXTEDIT_STRING *str, float x, float y)
+static int stb_text_locate_coord(IMSTB_TEXTEDIT_STRING *str, float x, float y, int* out_side_on_line)
{
StbTexteditRow r;
int n = STB_TEXTEDIT_STRINGLEN(str);
@@ -417,6 +437,7 @@ static int stb_text_locate_coord(IMSTB_TEXTEDIT_STRING *str, float x, float y)
r.x0 = r.x1 = 0;
r.ymin = r.ymax = 0;
r.num_chars = 0;
+ *out_side_on_line = 0;
// search rows to find one that straddles 'y'
while (i < n) {
@@ -436,7 +457,10 @@ static int stb_text_locate_coord(IMSTB_TEXTEDIT_STRING *str, float x, float y)
// below all text, return 'after' last character
if (i >= n)
+ {
+ *out_side_on_line = 1;
return n;
+ }
// check if it's before the beginning of the line
if (x < r.x0)
@@ -449,6 +473,7 @@ static int stb_text_locate_coord(IMSTB_TEXTEDIT_STRING *str, float x, float y)
for (k=0; k < r.num_chars; k = IMSTB_TEXTEDIT_GETNEXTCHARINDEX(str, i + k) - i) {
float w = STB_TEXTEDIT_GETWIDTH(str, i, k);
if (x < prev_x+w) {
+ *out_side_on_line = (k == 0) ? 0 : 1;
if (x < prev_x+w/2)
return k+i;
else
@@ -460,6 +485,7 @@ static int stb_text_locate_coord(IMSTB_TEXTEDIT_STRING *str, float x, float y)
}
// if the last character is a newline, return that. otherwise return 'after' the last character
+ *out_side_on_line = 1;
if (STB_TEXTEDIT_GETCHAR(str, i+r.num_chars-1) == STB_TEXTEDIT_NEWLINE)
return i+r.num_chars-1;
else
@@ -471,6 +497,7 @@ static void stb_textedit_click(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *st
{
// In single-line mode, just always make y = 0. This lets the drag keep working if the mouse
// goes off the top or bottom of the text
+ int side_on_line;
if( state->single_line )
{
StbTexteditRow r;
@@ -478,16 +505,18 @@ static void stb_textedit_click(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *st
y = r.ymin;
}
- state->cursor = stb_text_locate_coord(str, x, y);
+ state->cursor = stb_text_locate_coord(str, x, y, &side_on_line);
state->select_start = state->cursor;
state->select_end = state->cursor;
state->has_preferred_x = 0;
+ str->LastMoveDirectionLR = (ImS8)(side_on_line ? ImGuiDir_Right : ImGuiDir_Left);
}
// API drag: on mouse drag, move the cursor and selection endpoint to the clicked location
static void stb_textedit_drag(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state, float x, float y)
{
int p = 0;
+ int side_on_line;
// In single-line mode, just always make y = 0. This lets the drag keep working if the mouse
// goes off the top or bottom of the text
@@ -501,8 +530,9 @@ static void stb_textedit_drag(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *sta
if (state->select_start == state->select_end)
state->select_start = state->cursor;
- p = stb_text_locate_coord(str, x, y);
+ p = stb_text_locate_coord(str, x, y, &side_on_line);
state->cursor = state->select_end = p;
+ str->LastMoveDirectionLR = (ImS8)(side_on_line ? ImGuiDir_Right : ImGuiDir_Left);
}
/////////////////////////////////////////////////////////////////////////////
@@ -552,6 +582,8 @@ static void stb_textedit_find_charpos(StbFindState *find, IMSTB_TEXTEDIT_STRING
STB_TEXTEDIT_LAYOUTROW(&r, str, i);
if (n < i + r.num_chars)
break;
+ if (str->LastMoveDirectionLR == ImGuiDir_Right && str->Stb->cursor > 0 && str->Stb->cursor == i + r.num_chars && STB_TEXTEDIT_GETCHAR(str, i + r.num_chars - 1) != STB_TEXTEDIT_NEWLINE) // [DEAR IMGUI] Wrapping point handling
+ break;
if (i + r.num_chars == z && z > 0 && STB_TEXTEDIT_GETCHAR(str, z - 1) != STB_TEXTEDIT_NEWLINE) // [DEAR IMGUI] special handling for last line
break; // [DEAR IMGUI]
prev_start = i;
@@ -648,15 +680,33 @@ static void stb_textedit_move_to_last(IMSTB_TEXTEDIT_STRING *str, STB_TexteditSt
}
}
-// [DEAR IMGUI]
-// Functions must be implemented for UTF8 support
-// Code in this file that uses those functions is modified for [DEAR IMGUI] and deviates from the original stb_textedit.
-// There is not necessarily a '[DEAR IMGUI]' at the usage sites.
-#ifndef IMSTB_TEXTEDIT_GETPREVCHARINDEX
-#define IMSTB_TEXTEDIT_GETPREVCHARINDEX(obj, idx) (idx - 1)
+// [DEAR IMGUI] Extracted this function so we can more easily add support for word-wrapping.
+#ifndef STB_TEXTEDIT_MOVELINESTART
+static int stb_textedit_move_line_start(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state, int cursor)
+{
+ if (state->single_line)
+ return 0;
+ while (cursor > 0) {
+ int prev = IMSTB_TEXTEDIT_GETPREVCHARINDEX(str, cursor);
+ if (STB_TEXTEDIT_GETCHAR(str, prev) == STB_TEXTEDIT_NEWLINE)
+ break;
+ cursor = prev;
+ }
+ return cursor;
+}
+#define STB_TEXTEDIT_MOVELINESTART stb_textedit_move_line_start
#endif
-#ifndef IMSTB_TEXTEDIT_GETNEXTCHARINDEX
-#define IMSTB_TEXTEDIT_GETNEXTCHARINDEX(obj, idx) (idx + 1)
+#ifndef STB_TEXTEDIT_MOVELINEEND
+static int stb_textedit_move_line_end(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state, int cursor)
+{
+ int n = STB_TEXTEDIT_STRINGLEN(str);
+ if (state->single_line)
+ return n;
+ while (cursor < n && STB_TEXTEDIT_GETCHAR(str, cursor) != STB_TEXTEDIT_NEWLINE)
+ cursor = IMSTB_TEXTEDIT_GETNEXTCHARINDEX(str, cursor);
+ return cursor;
+}
+#define STB_TEXTEDIT_MOVELINEEND stb_textedit_move_line_end
#endif
#ifdef STB_TEXTEDIT_IS_SPACE
@@ -668,9 +718,9 @@ static int is_word_boundary( IMSTB_TEXTEDIT_STRING *str, int idx )
#ifndef STB_TEXTEDIT_MOVEWORDLEFT
static int stb_textedit_move_to_word_previous( IMSTB_TEXTEDIT_STRING *str, int c )
{
- --c; // always move at least one character
- while( c >= 0 && !is_word_boundary( str, c ) )
- --c;
+ c = IMSTB_TEXTEDIT_GETPREVCHARINDEX( str, c ); // always move at least one character
+ while (c >= 0 && !is_word_boundary(str, c))
+ c = IMSTB_TEXTEDIT_GETPREVCHARINDEX(str, c);
if( c < 0 )
c = 0;
@@ -684,9 +734,9 @@ static int stb_textedit_move_to_word_previous( IMSTB_TEXTEDIT_STRING *str, int c
static int stb_textedit_move_to_word_next( IMSTB_TEXTEDIT_STRING *str, int c )
{
const int len = STB_TEXTEDIT_STRINGLEN(str);
- ++c; // always move at least one character
+ c = IMSTB_TEXTEDIT_GETNEXTCHARINDEX(str, c); // always move at least one character
while( c < len && !is_word_boundary( str, c ) )
- ++c;
+ c = IMSTB_TEXTEDIT_GETNEXTCHARINDEX(str, c);
if( c > len )
c = len;
@@ -739,6 +789,7 @@ static int stb_textedit_paste_internal(IMSTB_TEXTEDIT_STRING *str, STB_TexteditS
#define STB_TEXTEDIT_KEYTYPE int
#endif
+// API key: process text input
// [DEAR IMGUI] Added stb_textedit_text(), extracted out and called by stb_textedit_key() for backward compatibility.
static void stb_textedit_text(IMSTB_TEXTEDIT_STRING* str, STB_TexteditState* state, const IMSTB_TEXTEDIT_CHARTYPE* text, int text_len)
{
@@ -753,8 +804,7 @@ static void stb_textedit_text(IMSTB_TEXTEDIT_STRING* str, STB_TexteditState* sta
state->cursor += text_len;
state->has_preferred_x = 0;
}
- }
- else {
+ } else {
stb_textedit_delete_selection(str, state); // implicitly clamps
if (STB_TEXTEDIT_INSERTCHARS(str, state->cursor, text, text_len)) {
stb_text_makeundo_insert(state, state->cursor, text_len);
@@ -771,6 +821,7 @@ retry:
switch (key) {
default: {
#ifdef STB_TEXTEDIT_KEYTOTEXT
+ // This is not suitable for UTF-8 support.
int c = STB_TEXTEDIT_KEYTOTEXT(key);
if (c > 0) {
IMSTB_TEXTEDIT_CHARTYPE ch = (IMSTB_TEXTEDIT_CHARTYPE)c;
@@ -911,15 +962,16 @@ retry:
// [DEAR IMGUI]
// going down while being on the last line shouldn't bring us to that line end
- if (STB_TEXTEDIT_GETCHAR(str, find.first_char + find.length - 1) != STB_TEXTEDIT_NEWLINE)
- break;
+ //if (STB_TEXTEDIT_GETCHAR(str, find.first_char + find.length - 1) != STB_TEXTEDIT_NEWLINE)
+ // break;
// now find character position down a row
state->cursor = start;
STB_TEXTEDIT_LAYOUTROW(&row, str, state->cursor);
x = row.x0;
- for (i=0; i < row.num_chars; ++i) {
+ for (i=0; i < row.num_chars; ) {
float dx = STB_TEXTEDIT_GETWIDTH(str, start, i);
+ int next = IMSTB_TEXTEDIT_GETNEXTCHARINDEX(str, state->cursor);
#ifdef IMSTB_TEXTEDIT_GETWIDTH_NEWLINE
if (dx == IMSTB_TEXTEDIT_GETWIDTH_NEWLINE)
break;
@@ -927,10 +979,13 @@ retry:
x += dx;
if (x > goal_x)
break;
- state->cursor = IMSTB_TEXTEDIT_GETNEXTCHARINDEX(str, state->cursor);
+ i += next - state->cursor;
+ state->cursor = next;
}
stb_textedit_clamp(str, state);
+ if (state->cursor == find.first_char + find.length)
+ str->LastMoveDirectionLR = ImGuiDir_Left;
state->has_preferred_x = 1;
state->preferred_x = goal_x;
@@ -980,8 +1035,9 @@ retry:
state->cursor = find.prev_first;
STB_TEXTEDIT_LAYOUTROW(&row, str, state->cursor);
x = row.x0;
- for (i=0; i < row.num_chars; ++i) {
+ for (i=0; i < row.num_chars; ) {
float dx = STB_TEXTEDIT_GETWIDTH(str, find.prev_first, i);
+ int next = IMSTB_TEXTEDIT_GETNEXTCHARINDEX(str, state->cursor);
#ifdef IMSTB_TEXTEDIT_GETWIDTH_NEWLINE
if (dx == IMSTB_TEXTEDIT_GETWIDTH_NEWLINE)
break;
@@ -989,10 +1045,15 @@ retry:
x += dx;
if (x > goal_x)
break;
- state->cursor = IMSTB_TEXTEDIT_GETNEXTCHARINDEX(str, state->cursor);
+ i += next - state->cursor;
+ state->cursor = next;
}
stb_textedit_clamp(str, state);
+ if (state->cursor == find.first_char)
+ str->LastMoveDirectionLR = ImGuiDir_Right;
+ else if (state->cursor == find.prev_first)
+ str->LastMoveDirectionLR = ImGuiDir_Left;
state->has_preferred_x = 1;
state->preferred_x = goal_x;
@@ -1002,10 +1063,15 @@ retry:
// go to previous line
// (we need to scan previous line the hard way. maybe we could expose this as a new API function?)
prev_scan = find.prev_first > 0 ? find.prev_first - 1 : 0;
- while (prev_scan > 0 && STB_TEXTEDIT_GETCHAR(str, prev_scan - 1) != STB_TEXTEDIT_NEWLINE)
- --prev_scan;
+ while (prev_scan > 0)
+ {
+ int prev = IMSTB_TEXTEDIT_GETPREVCHARINDEX(str, prev_scan);
+ if (STB_TEXTEDIT_GETCHAR(str, prev) == STB_TEXTEDIT_NEWLINE)
+ break;
+ prev_scan = prev;
+ }
find.first_char = find.prev_first;
- find.prev_first = prev_scan;
+ find.prev_first = STB_TEXTEDIT_MOVELINESTART(str, state, prev_scan);
}
break;
}
@@ -1079,10 +1145,7 @@ retry:
case STB_TEXTEDIT_K_LINESTART:
stb_textedit_clamp(str, state);
stb_textedit_move_to_first(state);
- if (state->single_line)
- state->cursor = 0;
- else while (state->cursor > 0 && STB_TEXTEDIT_GETCHAR(str, state->cursor-1) != STB_TEXTEDIT_NEWLINE)
- --state->cursor;
+ state->cursor = STB_TEXTEDIT_MOVELINESTART(str, state, state->cursor);
state->has_preferred_x = 0;
break;
@@ -1090,13 +1153,9 @@ retry:
case STB_TEXTEDIT_K_LINEEND2:
#endif
case STB_TEXTEDIT_K_LINEEND: {
- int n = STB_TEXTEDIT_STRINGLEN(str);
stb_textedit_clamp(str, state);
- stb_textedit_move_to_first(state);
- if (state->single_line)
- state->cursor = n;
- else while (state->cursor < n && STB_TEXTEDIT_GETCHAR(str, state->cursor) != STB_TEXTEDIT_NEWLINE)
- ++state->cursor;
+ stb_textedit_move_to_last(str, state);
+ state->cursor = STB_TEXTEDIT_MOVELINEEND(str, state, state->cursor);
state->has_preferred_x = 0;
break;
}
@@ -1107,10 +1166,7 @@ retry:
case STB_TEXTEDIT_K_LINESTART | STB_TEXTEDIT_K_SHIFT:
stb_textedit_clamp(str, state);
stb_textedit_prep_selection_at_cursor(state);
- if (state->single_line)
- state->cursor = 0;
- else while (state->cursor > 0 && STB_TEXTEDIT_GETCHAR(str, state->cursor-1) != STB_TEXTEDIT_NEWLINE)
- --state->cursor;
+ state->cursor = STB_TEXTEDIT_MOVELINESTART(str, state, state->cursor);
state->select_end = state->cursor;
state->has_preferred_x = 0;
break;
@@ -1119,13 +1175,9 @@ retry:
case STB_TEXTEDIT_K_LINEEND2 | STB_TEXTEDIT_K_SHIFT:
#endif
case STB_TEXTEDIT_K_LINEEND | STB_TEXTEDIT_K_SHIFT: {
- int n = STB_TEXTEDIT_STRINGLEN(str);
stb_textedit_clamp(str, state);
stb_textedit_prep_selection_at_cursor(state);
- if (state->single_line)
- state->cursor = n;
- else while (state->cursor < n && STB_TEXTEDIT_GETCHAR(str, state->cursor) != STB_TEXTEDIT_NEWLINE)
- ++state->cursor;
+ state->cursor = STB_TEXTEDIT_MOVELINEEND(str, state, state->cursor);
state->select_end = state->cursor;
state->has_preferred_x = 0;
break;
diff --git a/imstb_truetype.h b/imstb_truetype.h
index 976f09cb9..cf33289f6 100644
--- a/imstb_truetype.h
+++ b/imstb_truetype.h
@@ -4516,8 +4516,8 @@ static int stbtt__compute_crossings_x(float x, float y, int nverts, stbtt_vertex
q2[0] = (float)x2;
q2[1] = (float)y2;
if (equal(q0,q1) || equal(q1,q2)) {
- x0 = (int)verts[i-1].x;
- y0 = (int)verts[i-1].y;
+ x0 = (int)verts[i-1].x; //-V1048
+ y0 = (int)verts[i-1].y; //-V1048
x1 = (int)verts[i ].x;
y1 = (int)verts[i ].y;
if (y > STBTT_min(y0,y1) && y < STBTT_max(y0,y1) && x > STBTT_min(x0,x1)) {
diff --git a/misc/README.txt b/misc/README.txt
index b4ce89f03..58c00ee8c 100644
--- a/misc/README.txt
+++ b/misc/README.txt
@@ -4,8 +4,8 @@ misc/cpp/
This is also an example of how you may wrap your own similar types.
misc/debuggers/
- Helper files for popular debuggers.
- With the .natvis file, types like ImVector<> will be displayed nicely in Visual Studio debugger.
+ Helper files for popular debuggers (Visual Studio, GDB, LLDB).
+ e.g. With the .natvis file, types like ImVector<> will be displayed nicely in Visual Studio debugger.
misc/fonts/
Fonts loading/merging instructions (e.g. How to handle glyph ranges, how to merge icons fonts).
diff --git a/misc/debuggers/README.txt b/misc/debuggers/README.txt
index 3f4ba83e3..ad6f491fe 100644
--- a/misc/debuggers/README.txt
+++ b/misc/debuggers/README.txt
@@ -14,3 +14,8 @@ imgui.natvis
With this, types like ImVector<> will be displayed nicely in the debugger.
(read comments inside file for details)
+imgui_lldb.py
+ LLDB-based debuggers (*): synthetic children provider and summaries for Dear ImGui types.
+ With this, types like ImVector<> will be displayed nicely in the debugger.
+ (read comments inside file for details)
+ (*) Xcode, Android Studio, may be used from VS Code, C++Builder, CLion, Eclipse etc.
diff --git a/misc/debuggers/imgui_lldb.py b/misc/debuggers/imgui_lldb.py
new file mode 100644
index 000000000..7d3c6bfcb
--- /dev/null
+++ b/misc/debuggers/imgui_lldb.py
@@ -0,0 +1,189 @@
+# This file implements synthetic children providers and summaries for various Dear ImGui types for LLDB.
+# LLDB is used by Xcode, Android Studio, and may be used from VS Code, C++Builder, CLion, Eclipse etc.
+
+#
+# Useful links/documentation related to the feature:
+# - https://lldb.llvm.org/use/variable.html#summary-strings
+# - https://lldb.llvm.org/use/variable.html#synthetic-children
+# - https://lldb.llvm.org/python_reference/lldb-module.html
+#
+# To use it in a debug session:
+# > (lldb) command script import
+#
+# Alternatively you may include the above command in your ~/.lldbinit file to have the formatters
+# available in all future sessions
+
+import lldb
+
+class ArraySynthBase(object):
+ """
+ Helper baseclass aimed to reduce the boilerplate needed for "array-like" containers
+ """
+
+ def __init__(self, valobj, internal_dict):
+ self.valobj = valobj
+
+ def bind_to(self, pointer, size):
+ array_p = pointer.GetType().GetPointeeType().GetArrayType(size).GetPointerType()
+ self.array = pointer.Cast(array_p).Dereference()
+
+ def update(self):
+ self.array = self.valobj
+
+ def num_children(self, max_children):
+ return self.array.GetNumChildren(max_children)
+
+ def get_child_index(self, name):
+ return self.array.GetIndexOfChildWithName(name)
+
+ def get_child_at_index(self, index):
+ return self.array.GetChildAtIndex(index)
+
+ def has_children(self):
+ return self.array.MightHaveChildren()
+
+ def get_value(self):
+ return self.array
+
+class ImVectorSynth(ArraySynthBase):
+ def update(self):
+ self.size = self.valobj.GetChildMemberWithName("Size").GetValueAsUnsigned()
+ self.capacity = self.valobj.GetChildMemberWithName("Capacity").GetValueAsUnsigned()
+
+ data = self.valobj.GetChildMemberWithName("Data")
+
+ self.bind_to(data, self.size)
+
+ def get_summary(self):
+ return f"Size={self.size} Capacity={self.capacity}"
+
+class ImSpanSynth(ArraySynthBase):
+ def update(self):
+ data = self.valobj.GetChildMemberWithName("Data")
+ end = self.valobj.GetChildMemberWithName("DataEnd")
+
+ element_size = data.GetType().GetPointeeType().GetByteSize()
+ array_size = end.GetValueAsUnsigned() - data.GetValueAsUnsigned()
+
+ self.size = int(array_size / element_size)
+
+ self.bind_to(data, self.size)
+
+ def get_summary(self):
+ return f"Size={self.size}"
+
+class ImRectSummary(object):
+ def __init__(self, valobj, internal_dict):
+ self.valobj = valobj
+
+ def update(self):
+ pass
+
+ def get_summary(self):
+ min = self.valobj.GetChildMemberWithName("Min")
+ max = self.valobj.GetChildMemberWithName("Max")
+
+ minX = float(min.GetChildMemberWithName("x").GetValue())
+ minY = float(min.GetChildMemberWithName("y").GetValue())
+
+ maxX = float(max.GetChildMemberWithName("x").GetValue())
+ maxY = float(max.GetChildMemberWithName("y").GetValue())
+
+ return f"Min=({minX}, {minY}) Max=({maxX}, {maxY}) Size=({maxX - minX}, {maxY - minY})"
+
+def get_active_enum_flags(valobj):
+ flag_set = set()
+
+ enum_name = valobj.GetType().GetName() + "_"
+ enum_type = valobj.GetTarget().FindFirstType(enum_name)
+
+ if not enum_type.IsValid():
+ return flag_set
+
+ enum_members = enum_type.GetEnumMembers()
+ value = valobj.GetValueAsUnsigned()
+
+ for i in range(0, enum_members.GetSize()):
+ member = enum_members.GetTypeEnumMemberAtIndex(i)
+
+ if value & member.GetValueAsUnsigned():
+ flag_set.add(member.GetName().removeprefix(enum_name))
+
+ return flag_set
+
+class ImGuiWindowSummary(object):
+ def __init__(self, valobj, internal_dict):
+ self.valobj = valobj
+
+ def update(self):
+ pass
+
+ def get_summary(self):
+ name = self.valobj.GetChildMemberWithName("Name").GetSummary()
+
+ active = self.valobj.GetChildMemberWithName("Active").GetValueAsUnsigned() != 0
+ was_active = self.valobj.GetChildMemberWithName("WasActive").GetValueAsUnsigned() != 0
+ hidden = self.valobj.GetChildMemberWithName("Hidden") != 0
+
+ flags = get_active_enum_flags(self.valobj.GetChildMemberWithName("Flags"))
+
+ active = 1 if active or was_active else 0
+ child = 1 if "ChildWindow" in flags else 0
+ popup = 1 if "Popup" in flags else 0
+ hidden = 1 if hidden else 0
+
+ return f"Name {name} Active {active} Child {child} Popup {popup} Hidden {hidden}"
+
+
+def __lldb_init_module(debugger, internal_dict):
+ """
+ This function will be automatically called by LLDB when the module is loaded, here
+ we register the various synthetics/summaries we have build before
+ """
+
+ category_name = "imgui"
+ category = debugger.GetCategory(category_name)
+
+ # Make sure we don't accidentally keep accumulating languages or override the user's
+ # category enablement in Xcode, where lldb-rpc-server loads this file once for eac
+ # debugging session
+ if not category.IsValid():
+ category = debugger.CreateCategory(category_name)
+ category.AddLanguage(lldb.eLanguageTypeC_plus_plus)
+ category.SetEnabled(True)
+
+ def add_summary(typename, impl):
+ summary = None
+
+ if isinstance(impl, str):
+ summary = lldb.SBTypeSummary.CreateWithSummaryString(impl)
+ summary.SetOptions(lldb.eTypeOptionCascade)
+ else:
+ # Unfortunately programmatic summary string generation is an entirely different codepath
+ # in LLDB. Register a convenient trampoline function which makes it look like it's part
+ # of the SyntheticChildrenProvider contract
+ summary = lldb.SBTypeSummary.CreateWithScriptCode(f'''
+ synth = {impl.__module__}.{impl.__qualname__}(valobj.GetNonSyntheticValue(), internal_dict)
+ synth.update()
+
+ return synth.get_summary()
+ ''')
+ summary.SetOptions(lldb.eTypeOptionCascade | lldb.eTypeOptionFrontEndWantsDereference)
+
+ category.AddTypeSummary(lldb.SBTypeNameSpecifier(typename, True), summary)
+
+ def add_synthetic(typename, impl):
+ add_summary(typename, impl)
+
+ synthetic = lldb.SBTypeSynthetic.CreateWithClassName(f"{impl.__module__}.{impl.__qualname__}")
+ synthetic.SetOptions(lldb.eTypeOptionCascade | lldb.eTypeOptionFrontEndWantsDereference)
+
+ category.AddTypeSynthetic(lldb.SBTypeNameSpecifier(typename, True), synthetic)
+
+ add_synthetic("^ImVector<.+>$", ImVectorSynth)
+ add_synthetic("^ImSpan<.+>$", ImSpanSynth)
+
+ add_summary("^ImVec2$", "x=${var.x} y=${var.y}")
+ add_summary("^ImVec4$", "x=${var.x} y=${var.y} z=${var.z} w=${var.w}")
+ add_summary("^ImRect$", ImRectSummary)
+ add_summary("^ImGuiWindow$", ImGuiWindowSummary)
diff --git a/misc/freetype/imgui_freetype.cpp b/misc/freetype/imgui_freetype.cpp
index 73b9862bc..cbbde767c 100644
--- a/misc/freetype/imgui_freetype.cpp
+++ b/misc/freetype/imgui_freetype.cpp
@@ -2,10 +2,12 @@
// (code)
// Get the latest version at https://github.com/ocornut/imgui/tree/master/misc/freetype
-// Original code by @vuhdo (Aleksei Skriabin). Improvements by @mikesart. Maintained since 2019 by @ocornut.
+// Original code by @vuhdo (Aleksei Skriabin) in 2017, with improvements by @mikesart.
+// Maintained since 2019 by @ocornut.
// CHANGELOG
// (minor and older changes stripped away, please see git history for details)
+// 2025/06/11: refactored for the new ImFontLoader architecture, and ImGuiBackendFlags_RendererHasTextures support.
// 2024/10/17: added plutosvg support for SVG Fonts (seems faster/better than lunasvg). Enable by using '#define IMGUI_ENABLE_FREETYPE_PLUTOSVG'. (#7927)
// 2023/11/13: added support for ImFontConfig::RasterizationDensity field for scaling render density without scaling metrics.
// 2023/08/01: added support for SVG fonts, enable by using '#define IMGUI_ENABLE_FREETYPE_LUNASVG'. (#6591)
@@ -44,6 +46,7 @@
#include FT_FREETYPE_H //
#include FT_MODULE_H //
#include FT_GLYPH_H //
+#include FT_SIZES_H //
#include FT_SYNTHESIS_H //
// Handle LunaSVG and PlutoSVG
@@ -107,664 +110,206 @@ static FT_Error ImGuiLunasvgPortPresetSlot(FT_GlyphSlot slot, FT_Bool cache, FT_
#define FT_CEIL(X) (((X + 63) & -64) / 64) // From SDL_ttf: Handy routines for converting from fixed point
#define FT_SCALEFACTOR 64.0f
-namespace
+// Glyph metrics:
+// --------------
+//
+// xmin xmax
+// | |
+// |<-------- width -------->|
+// | |
+// | +-------------------------+----------------- ymax
+// | | ggggggggg ggggg | ^ ^
+// | | g:::::::::ggg::::g | | |
+// | | g:::::::::::::::::g | | |
+// | | g::::::ggggg::::::gg | | |
+// | | g:::::g g:::::g | | |
+// offsetX -|-------->| g:::::g g:::::g | offsetY |
+// | | g:::::g g:::::g | | |
+// | | g::::::g g:::::g | | |
+// | | g:::::::ggggg:::::g | | |
+// | | g::::::::::::::::g | | height
+// | | gg::::::::::::::g | | |
+// baseline ---*---------|---- gggggggg::::::g-----*-------- |
+// / | | g:::::g | |
+// origin | | gggggg g:::::g | |
+// | | g:::::gg gg:::::g | |
+// | | g::::::ggg:::::::g | |
+// | | gg:::::::::::::g | |
+// | | ggg::::::ggg | |
+// | | gggggg | v
+// | +-------------------------+----------------- ymin
+// | |
+// |------------- advanceX ----------->|
+
+// Stored in ImFontAtlas::FontLoaderData. ALLOCATED BY US.
+struct ImGui_ImplFreeType_Data
{
- // Glyph metrics:
- // --------------
- //
- // xmin xmax
- // | |
- // |<-------- width -------->|
- // | |
- // | +-------------------------+----------------- ymax
- // | | ggggggggg ggggg | ^ ^
- // | | g:::::::::ggg::::g | | |
- // | | g:::::::::::::::::g | | |
- // | | g::::::ggggg::::::gg | | |
- // | | g:::::g g:::::g | | |
- // offsetX -|-------->| g:::::g g:::::g | offsetY |
- // | | g:::::g g:::::g | | |
- // | | g::::::g g:::::g | | |
- // | | g:::::::ggggg:::::g | | |
- // | | g::::::::::::::::g | | height
- // | | gg::::::::::::::g | | |
- // baseline ---*---------|---- gggggggg::::::g-----*-------- |
- // / | | g:::::g | |
- // origin | | gggggg g:::::g | |
- // | | g:::::gg gg:::::g | |
- // | | g::::::ggg:::::::g | |
- // | | gg:::::::::::::g | |
- // | | ggg::::::ggg | |
- // | | gggggg | v
- // | +-------------------------+----------------- ymin
- // | |
- // |------------- advanceX ----------->|
-
- // A structure that describe a glyph.
- struct GlyphInfo
- {
- int Width; // Glyph's width in pixels.
- int Height; // Glyph's height in pixels.
- FT_Int OffsetX; // The distance from the origin ("pen position") to the left of the glyph.
- FT_Int OffsetY; // The distance from the origin to the top of the glyph. This is usually a value < 0.
- float AdvanceX; // The distance from the origin to the origin of the next glyph. This is usually a value > 0.
- bool IsColored; // The glyph is colored
- };
-
- // Font parameters and metrics.
- struct FontInfo
- {
- uint32_t PixelHeight; // Size this font was generated with.
- float Ascender; // The pixel extents above the baseline in pixels (typically positive).
- float Descender; // The extents below the baseline in pixels (typically negative).
- float LineSpacing; // The baseline-to-baseline distance. Note that it usually is larger than the sum of the ascender and descender taken as absolute values. There is also no guarantee that no glyphs extend above or below subsequent baselines when using this distance. Think of it as a value the designer of the font finds appropriate.
- float LineGap; // The spacing in pixels between one row's descent and the next row's ascent.
- float MaxAdvanceWidth; // This field gives the maximum horizontal cursor advance for all glyphs in the font.
- };
-
- // FreeType glyph rasterizer.
- // NB: No ctor/dtor, explicitly call Init()/Shutdown()
- struct FreeTypeFont
- {
- bool InitFont(FT_Library ft_library, const ImFontConfig& src, unsigned int extra_user_flags); // Initialize from an external data buffer. Doesn't copy data, and you must ensure it stays valid up to this object lifetime.
- void CloseFont();
- void SetPixelHeight(int pixel_height); // Change font pixel size. All following calls to RasterizeGlyph() will use this size
- const FT_Glyph_Metrics* LoadGlyph(uint32_t in_codepoint);
- const FT_Bitmap* RenderGlyphAndGetInfo(GlyphInfo* out_glyph_info);
- void BlitGlyph(const FT_Bitmap* ft_bitmap, uint32_t* dst, uint32_t dst_pitch, unsigned char* multiply_table = nullptr);
- FreeTypeFont() { memset((void*)this, 0, sizeof(*this)); }
- ~FreeTypeFont() { CloseFont(); }
-
- // [Internals]
- FontInfo Info; // Font descriptor of the current font.
- FT_Face Face;
- unsigned int UserFlags; // = ImFontConfig::RasterizerFlags
- FT_Int32 LoadFlags;
- FT_Render_Mode RenderMode;
- float RasterizationDensity;
- float InvRasterizationDensity;
- };
-
- bool FreeTypeFont::InitFont(FT_Library ft_library, const ImFontConfig& src, unsigned int extra_font_builder_flags)
- {
- FT_Error error = FT_New_Memory_Face(ft_library, (uint8_t*)src.FontData, (uint32_t)src.FontDataSize, (uint32_t)src.FontNo, &Face);
- if (error != 0)
- return false;
- error = FT_Select_Charmap(Face, FT_ENCODING_UNICODE);
- if (error != 0)
- return false;
-
- // Convert to FreeType flags (NB: Bold and Oblique are processed separately)
- UserFlags = src.FontBuilderFlags | extra_font_builder_flags;
-
- LoadFlags = 0;
- if ((UserFlags & ImGuiFreeTypeBuilderFlags_Bitmap) == 0)
- LoadFlags |= FT_LOAD_NO_BITMAP;
-
- if (UserFlags & ImGuiFreeTypeBuilderFlags_NoHinting)
- LoadFlags |= FT_LOAD_NO_HINTING;
- if (UserFlags & ImGuiFreeTypeBuilderFlags_NoAutoHint)
- LoadFlags |= FT_LOAD_NO_AUTOHINT;
- if (UserFlags & ImGuiFreeTypeBuilderFlags_ForceAutoHint)
- LoadFlags |= FT_LOAD_FORCE_AUTOHINT;
- if (UserFlags & ImGuiFreeTypeBuilderFlags_LightHinting)
- LoadFlags |= FT_LOAD_TARGET_LIGHT;
- else if (UserFlags & ImGuiFreeTypeBuilderFlags_MonoHinting)
- LoadFlags |= FT_LOAD_TARGET_MONO;
- else
- LoadFlags |= FT_LOAD_TARGET_NORMAL;
-
- if (UserFlags & ImGuiFreeTypeBuilderFlags_Monochrome)
- RenderMode = FT_RENDER_MODE_MONO;
- else
- RenderMode = FT_RENDER_MODE_NORMAL;
-
- if (UserFlags & ImGuiFreeTypeBuilderFlags_LoadColor)
- LoadFlags |= FT_LOAD_COLOR;
-
- RasterizationDensity = src.RasterizerDensity;
- InvRasterizationDensity = 1.0f / RasterizationDensity;
-
- memset(&Info, 0, sizeof(Info));
- SetPixelHeight((uint32_t)src.SizePixels);
-
- return true;
- }
-
- void FreeTypeFont::CloseFont()
- {
- if (Face)
- {
- FT_Done_Face(Face);
- Face = nullptr;
- }
- }
-
- void FreeTypeFont::SetPixelHeight(int pixel_height)
- {
- // Vuhdo: I'm not sure how to deal with font sizes properly. As far as I understand, currently ImGui assumes that the 'pixel_height'
- // is a maximum height of an any given glyph, i.e. it's the sum of font's ascender and descender. Seems strange to me.
- // NB: FT_Set_Pixel_Sizes() doesn't seem to get us the same result.
- FT_Size_RequestRec req;
- req.type = (UserFlags & ImGuiFreeTypeBuilderFlags_Bitmap) ? FT_SIZE_REQUEST_TYPE_NOMINAL : FT_SIZE_REQUEST_TYPE_REAL_DIM;
- req.width = 0;
- req.height = (uint32_t)(pixel_height * 64 * RasterizationDensity);
- req.horiResolution = 0;
- req.vertResolution = 0;
- FT_Request_Size(Face, &req);
-
- // Update font info
- FT_Size_Metrics metrics = Face->size->metrics;
- Info.PixelHeight = (uint32_t)(pixel_height * InvRasterizationDensity);
- Info.Ascender = (float)FT_CEIL(metrics.ascender) * InvRasterizationDensity;
- Info.Descender = (float)FT_CEIL(metrics.descender) * InvRasterizationDensity;
- Info.LineSpacing = (float)FT_CEIL(metrics.height) * InvRasterizationDensity;
- Info.LineGap = (float)FT_CEIL(metrics.height - metrics.ascender + metrics.descender) * InvRasterizationDensity;
- Info.MaxAdvanceWidth = (float)FT_CEIL(metrics.max_advance) * InvRasterizationDensity;
- }
-
- const FT_Glyph_Metrics* FreeTypeFont::LoadGlyph(uint32_t codepoint)
- {
- uint32_t glyph_index = FT_Get_Char_Index(Face, codepoint);
- if (glyph_index == 0)
- return nullptr;
-
- // If this crash for you: FreeType 2.11.0 has a crash bug on some bitmap/colored fonts.
- // - https://gitlab.freedesktop.org/freetype/freetype/-/issues/1076
- // - https://github.com/ocornut/imgui/issues/4567
- // - https://github.com/ocornut/imgui/issues/4566
- // You can use FreeType 2.10, or the patched version of 2.11.0 in VcPkg, or probably any upcoming FreeType version.
- FT_Error error = FT_Load_Glyph(Face, glyph_index, LoadFlags);
- if (error)
- return nullptr;
-
- // Need an outline for this to work
- FT_GlyphSlot slot = Face->glyph;
-#if defined(IMGUI_ENABLE_FREETYPE_LUNASVG) || defined(IMGUI_ENABLE_FREETYPE_PLUTOSVG)
- IM_ASSERT(slot->format == FT_GLYPH_FORMAT_OUTLINE || slot->format == FT_GLYPH_FORMAT_BITMAP || slot->format == FT_GLYPH_FORMAT_SVG);
-#else
-#if ((FREETYPE_MAJOR >= 2) && (FREETYPE_MINOR >= 12))
- IM_ASSERT(slot->format != FT_GLYPH_FORMAT_SVG && "The font contains SVG glyphs, you'll need to enable IMGUI_ENABLE_FREETYPE_PLUTOSVG or IMGUI_ENABLE_FREETYPE_LUNASVG in imconfig.h and install required libraries in order to use this font");
-#endif
- IM_ASSERT(slot->format == FT_GLYPH_FORMAT_OUTLINE || slot->format == FT_GLYPH_FORMAT_BITMAP);
-#endif // IMGUI_ENABLE_FREETYPE_LUNASVG
-
- // Apply convenience transform (this is not picking from real "Bold"/"Italic" fonts! Merely applying FreeType helper transform. Oblique == Slanting)
- if (UserFlags & ImGuiFreeTypeBuilderFlags_Bold)
- FT_GlyphSlot_Embolden(slot);
- if (UserFlags & ImGuiFreeTypeBuilderFlags_Oblique)
- {
- FT_GlyphSlot_Oblique(slot);
- //FT_BBox bbox;
- //FT_Outline_Get_BBox(&slot->outline, &bbox);
- //slot->metrics.width = bbox.xMax - bbox.xMin;
- //slot->metrics.height = bbox.yMax - bbox.yMin;
- }
-
- return &slot->metrics;
- }
-
- const FT_Bitmap* FreeTypeFont::RenderGlyphAndGetInfo(GlyphInfo* out_glyph_info)
- {
- FT_GlyphSlot slot = Face->glyph;
- FT_Error error = FT_Render_Glyph(slot, RenderMode);
- if (error != 0)
- return nullptr;
-
- FT_Bitmap* ft_bitmap = &Face->glyph->bitmap;
- out_glyph_info->Width = (int)ft_bitmap->width;
- out_glyph_info->Height = (int)ft_bitmap->rows;
- out_glyph_info->OffsetX = Face->glyph->bitmap_left;
- out_glyph_info->OffsetY = -Face->glyph->bitmap_top;
- out_glyph_info->AdvanceX = (float)slot->advance.x / FT_SCALEFACTOR;
- out_glyph_info->IsColored = (ft_bitmap->pixel_mode == FT_PIXEL_MODE_BGRA);
-
- return ft_bitmap;
- }
-
- void FreeTypeFont::BlitGlyph(const FT_Bitmap* ft_bitmap, uint32_t* dst, uint32_t dst_pitch, unsigned char* multiply_table)
- {
- IM_ASSERT(ft_bitmap != nullptr);
- const uint32_t w = ft_bitmap->width;
- const uint32_t h = ft_bitmap->rows;
- const uint8_t* src = ft_bitmap->buffer;
- const uint32_t src_pitch = ft_bitmap->pitch;
-
- switch (ft_bitmap->pixel_mode)
- {
- case FT_PIXEL_MODE_GRAY: // Grayscale image, 1 byte per pixel.
- {
- if (multiply_table == nullptr)
- {
- for (uint32_t y = 0; y < h; y++, src += src_pitch, dst += dst_pitch)
- for (uint32_t x = 0; x < w; x++)
- dst[x] = IM_COL32(255, 255, 255, src[x]);
- }
- else
- {
- for (uint32_t y = 0; y < h; y++, src += src_pitch, dst += dst_pitch)
- for (uint32_t x = 0; x < w; x++)
- dst[x] = IM_COL32(255, 255, 255, multiply_table[src[x]]);
- }
- break;
- }
- case FT_PIXEL_MODE_MONO: // Monochrome image, 1 bit per pixel. The bits in each byte are ordered from MSB to LSB.
- {
- uint8_t color0 = multiply_table ? multiply_table[0] : 0;
- uint8_t color1 = multiply_table ? multiply_table[255] : 255;
- for (uint32_t y = 0; y < h; y++, src += src_pitch, dst += dst_pitch)
- {
- uint8_t bits = 0;
- const uint8_t* bits_ptr = src;
- for (uint32_t x = 0; x < w; x++, bits <<= 1)
- {
- if ((x & 7) == 0)
- bits = *bits_ptr++;
- dst[x] = IM_COL32(255, 255, 255, (bits & 0x80) ? color1 : color0);
- }
- }
- break;
- }
- case FT_PIXEL_MODE_BGRA:
- {
- // FIXME: Converting pre-multiplied alpha to straight. Doesn't smell good.
- #define DE_MULTIPLY(color, alpha) ImMin((ImU32)(255.0f * (float)color / (float)(alpha + FLT_MIN) + 0.5f), 255u)
- if (multiply_table == nullptr)
- {
- for (uint32_t y = 0; y < h; y++, src += src_pitch, dst += dst_pitch)
- for (uint32_t x = 0; x < w; x++)
- {
- uint8_t r = src[x * 4 + 2], g = src[x * 4 + 1], b = src[x * 4], a = src[x * 4 + 3];
- dst[x] = IM_COL32(DE_MULTIPLY(r, a), DE_MULTIPLY(g, a), DE_MULTIPLY(b, a), a);
- }
- }
- else
- {
- for (uint32_t y = 0; y < h; y++, src += src_pitch, dst += dst_pitch)
- {
- for (uint32_t x = 0; x < w; x++)
- {
- uint8_t r = src[x * 4 + 2], g = src[x * 4 + 1], b = src[x * 4], a = src[x * 4 + 3];
- dst[x] = IM_COL32(multiply_table[DE_MULTIPLY(r, a)], multiply_table[DE_MULTIPLY(g, a)], multiply_table[DE_MULTIPLY(b, a)], multiply_table[a]);
- }
- }
- }
- #undef DE_MULTIPLY
- break;
- }
- default:
- IM_ASSERT(0 && "FreeTypeFont::BlitGlyph(): Unknown bitmap pixel mode!");
- }
- }
-} // namespace
-
-#ifndef STB_RECT_PACK_IMPLEMENTATION // in case the user already have an implementation in the _same_ compilation unit (e.g. unity builds)
-#ifndef IMGUI_DISABLE_STB_RECT_PACK_IMPLEMENTATION
-#define STBRP_ASSERT(x) do { IM_ASSERT(x); } while (0)
-#define STBRP_STATIC
-#define STB_RECT_PACK_IMPLEMENTATION
-#endif
-#ifdef IMGUI_STB_RECT_PACK_FILENAME
-#include IMGUI_STB_RECT_PACK_FILENAME
-#else
-#include "imstb_rectpack.h"
-#endif
-#endif
-
-struct ImFontBuildSrcGlyphFT
-{
- GlyphInfo Info;
- uint32_t Codepoint;
- unsigned int* BitmapData; // Point within one of the dst_tmp_bitmap_buffers[] array
-
- ImFontBuildSrcGlyphFT() { memset((void*)this, 0, sizeof(*this)); }
+ FT_Library Library;
+ FT_MemoryRec_ MemoryManager;
+ ImGui_ImplFreeType_Data() { memset((void*)this, 0, sizeof(*this)); }
};
-struct ImFontBuildSrcDataFT
+// Stored in ImFontConfig::FontLoaderData. ALLOCATED BY US.
+struct ImGui_ImplFreeType_FontSrcData
{
- FreeTypeFont Font;
- stbrp_rect* Rects; // Rectangle to pack. We first fill in their size and the packer will give us their position.
- const ImWchar* SrcRanges; // Ranges as requested by user (user is allowed to request too much, e.g. 0x0020..0xFFFF)
- int DstIndex; // Index into atlas->Fonts[] and dst_tmp_array[]
- int GlyphsHighest; // Highest requested codepoint
- int GlyphsCount; // Glyph count (excluding missing glyphs and glyphs already set by an earlier source font)
- ImBitVector GlyphsSet; // Glyph bit map (random access, 1-bit per codepoint. This will be a maximum of 8KB)
- ImVector GlyphsList;
+ // Initialize from an external data buffer. Doesn't copy data, and you must ensure it stays valid up to this object lifetime.
+ bool InitFont(FT_Library ft_library, ImFontConfig* src, ImGuiFreeTypeLoaderFlags extra_user_flags);
+ void CloseFont();
+ ImGui_ImplFreeType_FontSrcData() { memset((void*)this, 0, sizeof(*this)); }
+ ~ImGui_ImplFreeType_FontSrcData() { CloseFont(); }
+
+ // Members
+ FT_Face FtFace;
+ ImGuiFreeTypeLoaderFlags UserFlags; // = ImFontConfig::FontLoaderFlags
+ FT_Int32 LoadFlags;
+ ImFontBaked* BakedLastActivated;
};
-// Temporary data for one destination ImFont* (multiple source fonts can be merged into one destination ImFont)
-struct ImFontBuildDstDataFT
+// Stored in ImFontBaked::FontLoaderDatas: pointer to SourcesCount instances of this. ALLOCATED BY CORE.
+struct ImGui_ImplFreeType_FontSrcBakedData
{
- int SrcCount; // Number of source fonts targeting this destination font.
- int GlyphsHighest;
- int GlyphsCount;
- ImBitVector GlyphsSet; // This is used to resolve collision when multiple sources are merged into a same destination font.
+ FT_Size FtSize; // This represent a FT_Face with a given size.
+ ImGui_ImplFreeType_FontSrcBakedData() { memset((void*)this, 0, sizeof(*this)); }
};
-bool ImFontAtlasBuildWithFreeTypeEx(FT_Library ft_library, ImFontAtlas* atlas, unsigned int extra_flags)
+bool ImGui_ImplFreeType_FontSrcData::InitFont(FT_Library ft_library, ImFontConfig* src, ImGuiFreeTypeLoaderFlags extra_font_loader_flags)
{
- IM_ASSERT(atlas->Sources.Size > 0);
+ FT_Error error = FT_New_Memory_Face(ft_library, (uint8_t*)src->FontData, (FT_Long)src->FontDataSize, (FT_Long)src->FontNo, &FtFace);
+ if (error != 0)
+ return false;
+ error = FT_Select_Charmap(FtFace, FT_ENCODING_UNICODE);
+ if (error != 0)
+ return false;
- ImFontAtlasBuildInit(atlas);
+ // Convert to FreeType flags (NB: Bold and Oblique are processed separately)
+ UserFlags = (ImGuiFreeTypeLoaderFlags)(src->FontLoaderFlags | extra_font_loader_flags);
- // Clear atlas
- atlas->TexID = 0;
- atlas->TexWidth = atlas->TexHeight = 0;
- atlas->TexUvScale = ImVec2(0.0f, 0.0f);
- atlas->TexUvWhitePixel = ImVec2(0.0f, 0.0f);
- atlas->ClearTexData();
+ LoadFlags = 0;
+ if ((UserFlags & ImGuiFreeTypeLoaderFlags_Bitmap) == 0)
+ LoadFlags |= FT_LOAD_NO_BITMAP;
- // Temporary storage for building
- bool src_load_color = false;
- ImVector src_tmp_array;
- ImVector dst_tmp_array;
- src_tmp_array.resize(atlas->Sources.Size);
- dst_tmp_array.resize(atlas->Fonts.Size);
- memset((void*)src_tmp_array.Data, 0, (size_t)src_tmp_array.size_in_bytes());
- memset((void*)dst_tmp_array.Data, 0, (size_t)dst_tmp_array.size_in_bytes());
-
- // 1. Initialize font loading structure, check font data validity
- for (int src_i = 0; src_i < atlas->Sources.Size; src_i++)
- {
- ImFontBuildSrcDataFT& src_tmp = src_tmp_array[src_i];
- ImFontConfig& src = atlas->Sources[src_i];
- FreeTypeFont& font_face = src_tmp.Font;
- IM_ASSERT(src.DstFont && (!src.DstFont->IsLoaded() || src.DstFont->ContainerAtlas == atlas));
-
- // Find index from src.DstFont (we allow the user to set cfg.DstFont. Also it makes casual debugging nicer than when storing indices)
- src_tmp.DstIndex = -1;
- for (int output_i = 0; output_i < atlas->Fonts.Size && src_tmp.DstIndex == -1; output_i++)
- if (src.DstFont == atlas->Fonts[output_i])
- src_tmp.DstIndex = output_i;
- IM_ASSERT(src_tmp.DstIndex != -1); // src.DstFont not pointing within atlas->Fonts[] array?
- if (src_tmp.DstIndex == -1)
- return false;
-
- // Load font
- if (!font_face.InitFont(ft_library, src, extra_flags))
- return false;
-
- // Measure highest codepoints
- src_load_color |= (src.FontBuilderFlags & ImGuiFreeTypeBuilderFlags_LoadColor) != 0;
- ImFontBuildDstDataFT& dst_tmp = dst_tmp_array[src_tmp.DstIndex];
- src_tmp.SrcRanges = src.GlyphRanges ? src.GlyphRanges : atlas->GetGlyphRangesDefault();
- for (const ImWchar* src_range = src_tmp.SrcRanges; src_range[0] && src_range[1]; src_range += 2)
- {
- // Check for valid range. This may also help detect *some* dangling pointers, because a common
- // user error is to setup ImFontConfig::GlyphRanges with a pointer to data that isn't persistent,
- // or to forget to zero-terminate the glyph range array.
- IM_ASSERT(src_range[0] <= src_range[1] && "Invalid range: is your glyph range array persistent? it is zero-terminated?");
- src_tmp.GlyphsHighest = ImMax(src_tmp.GlyphsHighest, (int)src_range[1]);
- }
- dst_tmp.SrcCount++;
- dst_tmp.GlyphsHighest = ImMax(dst_tmp.GlyphsHighest, src_tmp.GlyphsHighest);
- }
-
- // 2. For every requested codepoint, check for their presence in the font data, and handle redundancy or overlaps between source fonts to avoid unused glyphs.
- int total_glyphs_count = 0;
- for (int src_i = 0; src_i < src_tmp_array.Size; src_i++)
- {
- ImFontBuildSrcDataFT& src_tmp = src_tmp_array[src_i];
- ImFontBuildDstDataFT& dst_tmp = dst_tmp_array[src_tmp.DstIndex];
- src_tmp.GlyphsSet.Create(src_tmp.GlyphsHighest + 1);
- if (dst_tmp.GlyphsSet.Storage.empty())
- dst_tmp.GlyphsSet.Create(dst_tmp.GlyphsHighest + 1);
-
- for (const ImWchar* src_range = src_tmp.SrcRanges; src_range[0] && src_range[1]; src_range += 2)
- for (int codepoint = src_range[0]; codepoint <= (int)src_range[1]; codepoint++)
- {
- if (dst_tmp.GlyphsSet.TestBit(codepoint)) // Don't overwrite existing glyphs. We could make this an option (e.g. MergeOverwrite)
- continue;
- uint32_t glyph_index = FT_Get_Char_Index(src_tmp.Font.Face, codepoint); // It is actually in the font? (FIXME-OPT: We are not storing the glyph_index..)
- if (glyph_index == 0)
- continue;
-
- // Add to avail set/counters
- src_tmp.GlyphsCount++;
- dst_tmp.GlyphsCount++;
- src_tmp.GlyphsSet.SetBit(codepoint);
- dst_tmp.GlyphsSet.SetBit(codepoint);
- total_glyphs_count++;
- }
- }
-
- // 3. Unpack our bit map into a flat list (we now have all the Unicode points that we know are requested _and_ available _and_ not overlapping another)
- for (int src_i = 0; src_i < src_tmp_array.Size; src_i++)
- {
- ImFontBuildSrcDataFT& src_tmp = src_tmp_array[src_i];
- src_tmp.GlyphsList.reserve(src_tmp.GlyphsCount);
-
- IM_ASSERT(sizeof(src_tmp.GlyphsSet.Storage.Data[0]) == sizeof(ImU32));
- const ImU32* it_begin = src_tmp.GlyphsSet.Storage.begin();
- const ImU32* it_end = src_tmp.GlyphsSet.Storage.end();
- for (const ImU32* it = it_begin; it < it_end; it++)
- if (ImU32 entries_32 = *it)
- for (ImU32 bit_n = 0; bit_n < 32; bit_n++)
- if (entries_32 & ((ImU32)1 << bit_n))
- {
- ImFontBuildSrcGlyphFT src_glyph;
- src_glyph.Codepoint = (ImWchar)(((it - it_begin) << 5) + bit_n);
- //src_glyph.GlyphIndex = 0; // FIXME-OPT: We had this info in the previous step and lost it..
- src_tmp.GlyphsList.push_back(src_glyph);
- }
- src_tmp.GlyphsSet.Clear();
- IM_ASSERT(src_tmp.GlyphsList.Size == src_tmp.GlyphsCount);
- }
- for (int dst_i = 0; dst_i < dst_tmp_array.Size; dst_i++)
- dst_tmp_array[dst_i].GlyphsSet.Clear();
- dst_tmp_array.clear();
-
- // Allocate packing character data and flag packed characters buffer as non-packed (x0=y0=x1=y1=0)
- // (We technically don't need to zero-clear buf_rects, but let's do it for the sake of sanity)
- ImVector buf_rects;
- buf_rects.resize(total_glyphs_count);
- memset(buf_rects.Data, 0, (size_t)buf_rects.size_in_bytes());
-
- // Allocate temporary rasterization data buffers.
- // We could not find a way to retrieve accurate glyph size without rendering them.
- // (e.g. slot->metrics->width not always matching bitmap->width, especially considering the Oblique transform)
- // We allocate in chunks of 256 KB to not waste too much extra memory ahead. Hopefully users of FreeType won't mind the temporary allocations.
- const int BITMAP_BUFFERS_CHUNK_SIZE = 256 * 1024;
- int buf_bitmap_current_used_bytes = 0;
- ImVector buf_bitmap_buffers;
- buf_bitmap_buffers.push_back((unsigned char*)IM_ALLOC(BITMAP_BUFFERS_CHUNK_SIZE));
-
- // 4. Gather glyphs sizes so we can pack them in our virtual canvas.
- // 8. Render/rasterize font characters into the texture
- int total_surface = 0;
- int buf_rects_out_n = 0;
- const int pack_padding = atlas->TexGlyphPadding;
- for (int src_i = 0; src_i < src_tmp_array.Size; src_i++)
- {
- ImFontBuildSrcDataFT& src_tmp = src_tmp_array[src_i];
- ImFontConfig& src = atlas->Sources[src_i];
- if (src_tmp.GlyphsCount == 0)
- continue;
-
- src_tmp.Rects = &buf_rects[buf_rects_out_n];
- buf_rects_out_n += src_tmp.GlyphsCount;
-
- // Compute multiply table if requested
- const bool multiply_enabled = (src.RasterizerMultiply != 1.0f);
- unsigned char multiply_table[256];
- if (multiply_enabled)
- ImFontAtlasBuildMultiplyCalcLookupTable(multiply_table, src.RasterizerMultiply);
-
- // Gather the sizes of all rectangles we will need to pack
- for (int glyph_i = 0; glyph_i < src_tmp.GlyphsList.Size; glyph_i++)
- {
- ImFontBuildSrcGlyphFT& src_glyph = src_tmp.GlyphsList[glyph_i];
-
- const FT_Glyph_Metrics* metrics = src_tmp.Font.LoadGlyph(src_glyph.Codepoint);
- if (metrics == nullptr)
- continue;
-
- // Render glyph into a bitmap (currently held by FreeType)
- const FT_Bitmap* ft_bitmap = src_tmp.Font.RenderGlyphAndGetInfo(&src_glyph.Info);
- if (ft_bitmap == nullptr)
- continue;
-
- // Allocate new temporary chunk if needed
- const int bitmap_size_in_bytes = src_glyph.Info.Width * src_glyph.Info.Height * 4;
- if (buf_bitmap_current_used_bytes + bitmap_size_in_bytes > BITMAP_BUFFERS_CHUNK_SIZE)
- {
- buf_bitmap_current_used_bytes = 0;
- buf_bitmap_buffers.push_back((unsigned char*)IM_ALLOC(BITMAP_BUFFERS_CHUNK_SIZE));
- }
- IM_ASSERT(buf_bitmap_current_used_bytes + bitmap_size_in_bytes <= BITMAP_BUFFERS_CHUNK_SIZE); // We could probably allocate custom-sized buffer instead.
-
- // Blit rasterized pixels to our temporary buffer and keep a pointer to it.
- src_glyph.BitmapData = (unsigned int*)(buf_bitmap_buffers.back() + buf_bitmap_current_used_bytes);
- buf_bitmap_current_used_bytes += bitmap_size_in_bytes;
- src_tmp.Font.BlitGlyph(ft_bitmap, src_glyph.BitmapData, src_glyph.Info.Width, multiply_enabled ? multiply_table : nullptr);
-
- src_tmp.Rects[glyph_i].w = (stbrp_coord)(src_glyph.Info.Width + pack_padding);
- src_tmp.Rects[glyph_i].h = (stbrp_coord)(src_glyph.Info.Height + pack_padding);
- total_surface += src_tmp.Rects[glyph_i].w * src_tmp.Rects[glyph_i].h;
- }
- }
- for (int i = 0; i < atlas->CustomRects.Size; i++)
- total_surface += (atlas->CustomRects[i].Width + pack_padding) * (atlas->CustomRects[i].Height + pack_padding);
-
- // We need a width for the skyline algorithm, any width!
- // The exact width doesn't really matter much, but some API/GPU have texture size limitations and increasing width can decrease height.
- // User can override TexDesiredWidth and TexGlyphPadding if they wish, otherwise we use a simple heuristic to select the width based on expected surface.
- const int surface_sqrt = (int)ImSqrt((float)total_surface) + 1;
- atlas->TexHeight = 0;
- if (atlas->TexDesiredWidth > 0)
- atlas->TexWidth = atlas->TexDesiredWidth;
+ if (UserFlags & ImGuiFreeTypeLoaderFlags_NoHinting)
+ LoadFlags |= FT_LOAD_NO_HINTING;
else
- atlas->TexWidth = (surface_sqrt >= 4096 * 0.7f) ? 4096 : (surface_sqrt >= 2048 * 0.7f) ? 2048 : (surface_sqrt >= 1024 * 0.7f) ? 1024 : 512;
+ src->PixelSnapH = true; // FIXME: A bit weird to do this this way.
- // 5. Start packing
- // Pack our extra data rectangles first, so it will be on the upper-left corner of our texture (UV will have small values).
- const int TEX_HEIGHT_MAX = 1024 * 32;
- const int num_nodes_for_packing_algorithm = atlas->TexWidth - atlas->TexGlyphPadding;
- ImVector pack_nodes;
- pack_nodes.resize(num_nodes_for_packing_algorithm);
- stbrp_context pack_context;
- stbrp_init_target(&pack_context, atlas->TexWidth - atlas->TexGlyphPadding, TEX_HEIGHT_MAX - atlas->TexGlyphPadding, pack_nodes.Data, pack_nodes.Size);
- ImFontAtlasBuildPackCustomRects(atlas, &pack_context);
-
- // 6. Pack each source font. No rendering yet, we are working with rectangles in an infinitely tall texture at this point.
- for (int src_i = 0; src_i < src_tmp_array.Size; src_i++)
- {
- ImFontBuildSrcDataFT& src_tmp = src_tmp_array[src_i];
- if (src_tmp.GlyphsCount == 0)
- continue;
-
- stbrp_pack_rects(&pack_context, src_tmp.Rects, src_tmp.GlyphsCount);
-
- // Extend texture height and mark missing glyphs as non-packed so we won't render them.
- // FIXME: We are not handling packing failure here (would happen if we got off TEX_HEIGHT_MAX or if a single if larger than TexWidth?)
- for (int glyph_i = 0; glyph_i < src_tmp.GlyphsCount; glyph_i++)
- if (src_tmp.Rects[glyph_i].was_packed)
- atlas->TexHeight = ImMax(atlas->TexHeight, src_tmp.Rects[glyph_i].y + src_tmp.Rects[glyph_i].h);
- }
-
- // 7. Allocate texture
- atlas->TexHeight = (atlas->Flags & ImFontAtlasFlags_NoPowerOfTwoHeight) ? (atlas->TexHeight + 1) : ImUpperPowerOfTwo(atlas->TexHeight);
- atlas->TexUvScale = ImVec2(1.0f / atlas->TexWidth, 1.0f / atlas->TexHeight);
- if (src_load_color)
- {
- size_t tex_size = (size_t)atlas->TexWidth * atlas->TexHeight * 4;
- atlas->TexPixelsRGBA32 = (unsigned int*)IM_ALLOC(tex_size);
- memset(atlas->TexPixelsRGBA32, 0, tex_size);
- }
+ if (UserFlags & ImGuiFreeTypeLoaderFlags_NoAutoHint)
+ LoadFlags |= FT_LOAD_NO_AUTOHINT;
+ if (UserFlags & ImGuiFreeTypeLoaderFlags_ForceAutoHint)
+ LoadFlags |= FT_LOAD_FORCE_AUTOHINT;
+ if (UserFlags & ImGuiFreeTypeLoaderFlags_LightHinting)
+ LoadFlags |= FT_LOAD_TARGET_LIGHT;
+ else if (UserFlags & ImGuiFreeTypeLoaderFlags_MonoHinting)
+ LoadFlags |= FT_LOAD_TARGET_MONO;
else
- {
- size_t tex_size = (size_t)atlas->TexWidth * atlas->TexHeight * 1;
- atlas->TexPixelsAlpha8 = (unsigned char*)IM_ALLOC(tex_size);
- memset(atlas->TexPixelsAlpha8, 0, tex_size);
- }
+ LoadFlags |= FT_LOAD_TARGET_NORMAL;
- // 8. Copy rasterized font characters back into the main texture
- // 9. Setup ImFont and glyphs for runtime
- bool tex_use_colors = false;
- for (int src_i = 0; src_i < src_tmp_array.Size; src_i++)
- {
- ImFontBuildSrcDataFT& src_tmp = src_tmp_array[src_i];
-
- // When merging fonts with MergeMode=true:
- // - We can have multiple input fonts writing into a same destination font.
- // - dst_font->Sources is != from src which is our source configuration.
- ImFontConfig& src = atlas->Sources[src_i];
- ImFont* dst_font = src.DstFont;
-
- const float ascent = src_tmp.Font.Info.Ascender;
- const float descent = src_tmp.Font.Info.Descender;
- ImFontAtlasBuildSetupFont(atlas, dst_font, &src, ascent, descent);
-
- if (src_tmp.GlyphsCount == 0)
- continue;
- const float font_off_x = src.GlyphOffset.x;
- const float font_off_y = src.GlyphOffset.y + IM_ROUND(dst_font->Ascent);
-
- const int padding = atlas->TexGlyphPadding;
- for (int glyph_i = 0; glyph_i < src_tmp.GlyphsCount; glyph_i++)
- {
- ImFontBuildSrcGlyphFT& src_glyph = src_tmp.GlyphsList[glyph_i];
- stbrp_rect& pack_rect = src_tmp.Rects[glyph_i];
- IM_ASSERT(pack_rect.was_packed);
- if (pack_rect.w == 0 && pack_rect.h == 0)
- continue;
-
- GlyphInfo& info = src_glyph.Info;
- IM_ASSERT(info.Width + padding <= pack_rect.w);
- IM_ASSERT(info.Height + padding <= pack_rect.h);
- const int tx = pack_rect.x + padding;
- const int ty = pack_rect.y + padding;
-
- // Register glyph
- float x0 = info.OffsetX * src_tmp.Font.InvRasterizationDensity + font_off_x;
- float y0 = info.OffsetY * src_tmp.Font.InvRasterizationDensity + font_off_y;
- float x1 = x0 + info.Width * src_tmp.Font.InvRasterizationDensity;
- float y1 = y0 + info.Height * src_tmp.Font.InvRasterizationDensity;
- float u0 = (tx) / (float)atlas->TexWidth;
- float v0 = (ty) / (float)atlas->TexHeight;
- float u1 = (tx + info.Width) / (float)atlas->TexWidth;
- float v1 = (ty + info.Height) / (float)atlas->TexHeight;
- dst_font->AddGlyph(&src, (ImWchar)src_glyph.Codepoint, x0, y0, x1, y1, u0, v0, u1, v1, info.AdvanceX * src_tmp.Font.InvRasterizationDensity);
-
- ImFontGlyph* dst_glyph = &dst_font->Glyphs.back();
- IM_ASSERT(dst_glyph->Codepoint == src_glyph.Codepoint);
- if (src_glyph.Info.IsColored)
- dst_glyph->Colored = tex_use_colors = true;
-
- // Blit from temporary buffer to final texture
- size_t blit_src_stride = (size_t)src_glyph.Info.Width;
- size_t blit_dst_stride = (size_t)atlas->TexWidth;
- unsigned int* blit_src = src_glyph.BitmapData;
- if (atlas->TexPixelsAlpha8 != nullptr)
- {
- unsigned char* blit_dst = atlas->TexPixelsAlpha8 + (ty * blit_dst_stride) + tx;
- for (int y = 0; y < info.Height; y++, blit_dst += blit_dst_stride, blit_src += blit_src_stride)
- for (int x = 0; x < info.Width; x++)
- blit_dst[x] = (unsigned char)((blit_src[x] >> IM_COL32_A_SHIFT) & 0xFF);
- }
- else
- {
- unsigned int* blit_dst = atlas->TexPixelsRGBA32 + (ty * blit_dst_stride) + tx;
- for (int y = 0; y < info.Height; y++, blit_dst += blit_dst_stride, blit_src += blit_src_stride)
- for (int x = 0; x < info.Width; x++)
- blit_dst[x] = blit_src[x];
- }
- }
-
- src_tmp.Rects = nullptr;
- }
- atlas->TexPixelsUseColors = tex_use_colors;
-
- // Cleanup
- for (int buf_i = 0; buf_i < buf_bitmap_buffers.Size; buf_i++)
- IM_FREE(buf_bitmap_buffers[buf_i]);
- src_tmp_array.clear_destruct();
-
- ImFontAtlasBuildFinish(atlas);
+ if (UserFlags & ImGuiFreeTypeLoaderFlags_LoadColor)
+ LoadFlags |= FT_LOAD_COLOR;
return true;
}
+void ImGui_ImplFreeType_FontSrcData::CloseFont()
+{
+ if (FtFace)
+ {
+ FT_Done_Face(FtFace);
+ FtFace = nullptr;
+ }
+}
+
+static const FT_Glyph_Metrics* ImGui_ImplFreeType_LoadGlyph(ImGui_ImplFreeType_FontSrcData* src_data, uint32_t codepoint)
+{
+ uint32_t glyph_index = FT_Get_Char_Index(src_data->FtFace, codepoint);
+ if (glyph_index == 0)
+ return nullptr;
+
+ // If this crash for you: FreeType 2.11.0 has a crash bug on some bitmap/colored fonts.
+ // - https://gitlab.freedesktop.org/freetype/freetype/-/issues/1076
+ // - https://github.com/ocornut/imgui/issues/4567
+ // - https://github.com/ocornut/imgui/issues/4566
+ // You can use FreeType 2.10, or the patched version of 2.11.0 in VcPkg, or probably any upcoming FreeType version.
+ FT_Error error = FT_Load_Glyph(src_data->FtFace, glyph_index, src_data->LoadFlags);
+ if (error)
+ return nullptr;
+
+ // Need an outline for this to work
+ FT_GlyphSlot slot = src_data->FtFace->glyph;
+#if defined(IMGUI_ENABLE_FREETYPE_LUNASVG) || defined(IMGUI_ENABLE_FREETYPE_PLUTOSVG)
+ IM_ASSERT(slot->format == FT_GLYPH_FORMAT_OUTLINE || slot->format == FT_GLYPH_FORMAT_BITMAP || slot->format == FT_GLYPH_FORMAT_SVG);
+#else
+#if ((FREETYPE_MAJOR >= 2) && (FREETYPE_MINOR >= 12))
+ IM_ASSERT(slot->format != FT_GLYPH_FORMAT_SVG && "The font contains SVG glyphs, you'll need to enable IMGUI_ENABLE_FREETYPE_PLUTOSVG or IMGUI_ENABLE_FREETYPE_LUNASVG in imconfig.h and install required libraries in order to use this font");
+#endif
+ IM_ASSERT(slot->format == FT_GLYPH_FORMAT_OUTLINE || slot->format == FT_GLYPH_FORMAT_BITMAP);
+#endif // IMGUI_ENABLE_FREETYPE_LUNASVG
+
+ // Apply convenience transform (this is not picking from real "Bold"/"Italic" fonts! Merely applying FreeType helper transform. Oblique == Slanting)
+ if (src_data->UserFlags & ImGuiFreeTypeLoaderFlags_Bold)
+ FT_GlyphSlot_Embolden(slot);
+ if (src_data->UserFlags & ImGuiFreeTypeLoaderFlags_Oblique)
+ {
+ FT_GlyphSlot_Oblique(slot);
+ //FT_BBox bbox;
+ //FT_Outline_Get_BBox(&slot->outline, &bbox);
+ //slot->metrics.width = bbox.xMax - bbox.xMin;
+ //slot->metrics.height = bbox.yMax - bbox.yMin;
+ }
+
+ return &slot->metrics;
+}
+
+static void ImGui_ImplFreeType_BlitGlyph(const FT_Bitmap* ft_bitmap, uint32_t* dst, uint32_t dst_pitch)
+{
+ IM_ASSERT(ft_bitmap != nullptr);
+ const uint32_t w = ft_bitmap->width;
+ const uint32_t h = ft_bitmap->rows;
+ const uint8_t* src = ft_bitmap->buffer;
+ const uint32_t src_pitch = ft_bitmap->pitch;
+
+ switch (ft_bitmap->pixel_mode)
+ {
+ case FT_PIXEL_MODE_GRAY: // Grayscale image, 1 byte per pixel.
+ {
+ for (uint32_t y = 0; y < h; y++, src += src_pitch, dst += dst_pitch)
+ for (uint32_t x = 0; x < w; x++)
+ dst[x] = IM_COL32(255, 255, 255, src[x]);
+ break;
+ }
+ case FT_PIXEL_MODE_MONO: // Monochrome image, 1 bit per pixel. The bits in each byte are ordered from MSB to LSB.
+ {
+ for (uint32_t y = 0; y < h; y++, src += src_pitch, dst += dst_pitch)
+ {
+ uint8_t bits = 0;
+ const uint8_t* bits_ptr = src;
+ for (uint32_t x = 0; x < w; x++, bits <<= 1)
+ {
+ if ((x & 7) == 0)
+ bits = *bits_ptr++;
+ dst[x] = IM_COL32(255, 255, 255, (bits & 0x80) ? 255 : 0);
+ }
+ }
+ break;
+ }
+ case FT_PIXEL_MODE_BGRA:
+ {
+ // FIXME: Converting pre-multiplied alpha to straight. Doesn't smell good.
+ #define DE_MULTIPLY(color, alpha) ImMin((ImU32)(255.0f * (float)color / (float)(alpha + FLT_MIN) + 0.5f), 255u)
+ for (uint32_t y = 0; y < h; y++, src += src_pitch, dst += dst_pitch)
+ for (uint32_t x = 0; x < w; x++)
+ {
+ uint8_t r = src[x * 4 + 2], g = src[x * 4 + 1], b = src[x * 4], a = src[x * 4 + 3];
+ dst[x] = IM_COL32(DE_MULTIPLY(r, a), DE_MULTIPLY(g, a), DE_MULTIPLY(b, a), a);
+ }
+ #undef DE_MULTIPLY
+ break;
+ }
+ default:
+ IM_ASSERT(0 && "FreeTypeFont::BlitGlyph(): Unknown bitmap pixel mode!");
+ }
+}
+
// FreeType memory allocation callbacks
static void* FreeType_Alloc(FT_Memory /*memory*/, long size)
{
@@ -799,47 +344,250 @@ static void* FreeType_Realloc(FT_Memory /*memory*/, long cur_size, long new_size
return block;
}
-static bool ImFontAtlasBuildWithFreeType(ImFontAtlas* atlas)
+static bool ImGui_ImplFreeType_LoaderInit(ImFontAtlas* atlas)
{
+ IM_ASSERT(atlas->FontLoaderData == nullptr);
+ ImGui_ImplFreeType_Data* bd = IM_NEW(ImGui_ImplFreeType_Data)();
+
// FreeType memory management: https://www.freetype.org/freetype2/docs/design/design-4.html
- FT_MemoryRec_ memory_rec = {};
- memory_rec.user = nullptr;
- memory_rec.alloc = &FreeType_Alloc;
- memory_rec.free = &FreeType_Free;
- memory_rec.realloc = &FreeType_Realloc;
+ bd->MemoryManager.user = nullptr;
+ bd->MemoryManager.alloc = &FreeType_Alloc;
+ bd->MemoryManager.free = &FreeType_Free;
+ bd->MemoryManager.realloc = &FreeType_Realloc;
// https://www.freetype.org/freetype2/docs/reference/ft2-module_management.html#FT_New_Library
- FT_Library ft_library;
- FT_Error error = FT_New_Library(&memory_rec, &ft_library);
+ FT_Error error = FT_New_Library(&bd->MemoryManager, &bd->Library);
if (error != 0)
+ {
+ IM_DELETE(bd);
return false;
+ }
// If you don't call FT_Add_Default_Modules() the rest of code may work, but FreeType won't use our custom allocator.
- FT_Add_Default_Modules(ft_library);
+ FT_Add_Default_Modules(bd->Library);
#ifdef IMGUI_ENABLE_FREETYPE_LUNASVG
// Install svg hooks for FreeType
// https://freetype.org/freetype2/docs/reference/ft2-properties.html#svg-hooks
// https://freetype.org/freetype2/docs/reference/ft2-svg_fonts.html#svg_fonts
SVG_RendererHooks hooks = { ImGuiLunasvgPortInit, ImGuiLunasvgPortFree, ImGuiLunasvgPortRender, ImGuiLunasvgPortPresetSlot };
- FT_Property_Set(ft_library, "ot-svg", "svg-hooks", &hooks);
+ FT_Property_Set(bd->Library, "ot-svg", "svg-hooks", &hooks);
#endif // IMGUI_ENABLE_FREETYPE_LUNASVG
#ifdef IMGUI_ENABLE_FREETYPE_PLUTOSVG
// With plutosvg, use provided hooks
- FT_Property_Set(ft_library, "ot-svg", "svg-hooks", plutosvg_ft_svg_hooks());
+ FT_Property_Set(bd->Library, "ot-svg", "svg-hooks", plutosvg_ft_svg_hooks());
#endif // IMGUI_ENABLE_FREETYPE_PLUTOSVG
- bool ret = ImFontAtlasBuildWithFreeTypeEx(ft_library, atlas, atlas->FontBuilderFlags);
- FT_Done_Library(ft_library);
+ // Store our data
+ atlas->FontLoaderData = (void*)bd;
- return ret;
+ return true;
}
-const ImFontBuilderIO* ImGuiFreeType::GetBuilderForFreeType()
+static void ImGui_ImplFreeType_LoaderShutdown(ImFontAtlas* atlas)
{
- static ImFontBuilderIO io;
- io.FontBuilder_Build = ImFontAtlasBuildWithFreeType;
- return &io;
+ ImGui_ImplFreeType_Data* bd = (ImGui_ImplFreeType_Data*)atlas->FontLoaderData;
+ IM_ASSERT(bd != nullptr);
+ FT_Done_Library(bd->Library);
+ IM_DELETE(bd);
+ atlas->FontLoaderData = nullptr;
+}
+
+static bool ImGui_ImplFreeType_FontSrcInit(ImFontAtlas* atlas, ImFontConfig* src)
+{
+ ImGui_ImplFreeType_Data* bd = (ImGui_ImplFreeType_Data*)atlas->FontLoaderData;
+ ImGui_ImplFreeType_FontSrcData* bd_font_data = IM_NEW(ImGui_ImplFreeType_FontSrcData);
+ IM_ASSERT(src->FontLoaderData == nullptr);
+ src->FontLoaderData = bd_font_data;
+
+ if (!bd_font_data->InitFont(bd->Library, src, (ImGuiFreeTypeLoaderFlags)atlas->FontLoaderFlags))
+ {
+ IM_DELETE(bd_font_data);
+ src->FontLoaderData = nullptr;
+ return false;
+ }
+
+ return true;
+}
+
+static void ImGui_ImplFreeType_FontSrcDestroy(ImFontAtlas* atlas, ImFontConfig* src)
+{
+ IM_UNUSED(atlas);
+ ImGui_ImplFreeType_FontSrcData* bd_font_data = (ImGui_ImplFreeType_FontSrcData*)src->FontLoaderData;
+ IM_DELETE(bd_font_data);
+ src->FontLoaderData = nullptr;
+}
+
+static bool ImGui_ImplFreeType_FontBakedInit(ImFontAtlas* atlas, ImFontConfig* src, ImFontBaked* baked, void* loader_data_for_baked_src)
+{
+ IM_UNUSED(atlas);
+ float size = baked->Size;
+ if (src->MergeMode && src->SizePixels != 0.0f)
+ size *= (src->SizePixels / baked->ContainerFont->Sources[0]->SizePixels);
+
+ ImGui_ImplFreeType_FontSrcData* bd_font_data = (ImGui_ImplFreeType_FontSrcData*)src->FontLoaderData;
+ bd_font_data->BakedLastActivated = baked;
+
+ // We use one FT_Size per (source + baked) combination.
+ ImGui_ImplFreeType_FontSrcBakedData* bd_baked_data = (ImGui_ImplFreeType_FontSrcBakedData*)loader_data_for_baked_src;
+ IM_ASSERT(bd_baked_data != nullptr);
+ IM_PLACEMENT_NEW(bd_baked_data) ImGui_ImplFreeType_FontSrcBakedData();
+
+ FT_New_Size(bd_font_data->FtFace, &bd_baked_data->FtSize);
+ FT_Activate_Size(bd_baked_data->FtSize);
+
+ // Vuhdo 2017: "I'm not sure how to deal with font sizes properly. As far as I understand, currently ImGui assumes that the 'pixel_height'
+ // is a maximum height of an any given glyph, i.e. it's the sum of font's ascender and descender. Seems strange to me.
+ // FT_Set_Pixel_Sizes() doesn't seem to get us the same result."
+ // (FT_Set_Pixel_Sizes() essentially calls FT_Request_Size() with FT_SIZE_REQUEST_TYPE_NOMINAL)
+ const float rasterizer_density = src->RasterizerDensity * baked->RasterizerDensity;
+ FT_Size_RequestRec req;
+ req.type = (bd_font_data->UserFlags & ImGuiFreeTypeLoaderFlags_Bitmap) ? FT_SIZE_REQUEST_TYPE_NOMINAL : FT_SIZE_REQUEST_TYPE_REAL_DIM;
+ req.width = 0;
+ req.height = (uint32_t)(size * 64 * rasterizer_density);
+ req.horiResolution = 0;
+ req.vertResolution = 0;
+ FT_Request_Size(bd_font_data->FtFace, &req);
+
+ // Output
+ if (src->MergeMode == false)
+ {
+ // Read metrics
+ FT_Size_Metrics metrics = bd_baked_data->FtSize->metrics;
+ const float scale = 1.0f / rasterizer_density;
+ baked->Ascent = (float)FT_CEIL(metrics.ascender) * scale; // The pixel extents above the baseline in pixels (typically positive).
+ baked->Descent = (float)FT_CEIL(metrics.descender) * scale; // The extents below the baseline in pixels (typically negative).
+ //LineSpacing = (float)FT_CEIL(metrics.height) * scale; // The baseline-to-baseline distance. Note that it usually is larger than the sum of the ascender and descender taken as absolute values. There is also no guarantee that no glyphs extend above or below subsequent baselines when using this distance. Think of it as a value the designer of the font finds appropriate.
+ //LineGap = (float)FT_CEIL(metrics.height - metrics.ascender + metrics.descender) * scale; // The spacing in pixels between one row's descent and the next row's ascent.
+ //MaxAdvanceWidth = (float)FT_CEIL(metrics.max_advance) * scale; // This field gives the maximum horizontal cursor advance for all glyphs in the font.
+ }
+ return true;
+}
+
+static void ImGui_ImplFreeType_FontBakedDestroy(ImFontAtlas* atlas, ImFontConfig* src, ImFontBaked* baked, void* loader_data_for_baked_src)
+{
+ IM_UNUSED(atlas);
+ IM_UNUSED(baked);
+ IM_UNUSED(src);
+ ImGui_ImplFreeType_FontSrcBakedData* bd_baked_data = (ImGui_ImplFreeType_FontSrcBakedData*)loader_data_for_baked_src;
+ IM_ASSERT(bd_baked_data != nullptr);
+ FT_Done_Size(bd_baked_data->FtSize);
+ bd_baked_data->~ImGui_ImplFreeType_FontSrcBakedData(); // ~IM_PLACEMENT_DELETE()
+}
+
+static bool ImGui_ImplFreeType_FontBakedLoadGlyph(ImFontAtlas* atlas, ImFontConfig* src, ImFontBaked* baked, void* loader_data_for_baked_src, ImWchar codepoint, ImFontGlyph* out_glyph, float* out_advance_x)
+{
+ ImGui_ImplFreeType_FontSrcData* bd_font_data = (ImGui_ImplFreeType_FontSrcData*)src->FontLoaderData;
+ uint32_t glyph_index = FT_Get_Char_Index(bd_font_data->FtFace, codepoint);
+ if (glyph_index == 0)
+ return false;
+
+ if (bd_font_data->BakedLastActivated != baked) // <-- could use id
+ {
+ // Activate current size
+ ImGui_ImplFreeType_FontSrcBakedData* bd_baked_data = (ImGui_ImplFreeType_FontSrcBakedData*)loader_data_for_baked_src;
+ FT_Activate_Size(bd_baked_data->FtSize);
+ bd_font_data->BakedLastActivated = baked;
+ }
+
+ const FT_Glyph_Metrics* metrics = ImGui_ImplFreeType_LoadGlyph(bd_font_data, codepoint);
+ if (metrics == nullptr)
+ return false;
+
+ FT_Face face = bd_font_data->FtFace;
+ FT_GlyphSlot slot = face->glyph;
+ const float rasterizer_density = src->RasterizerDensity * baked->RasterizerDensity;
+
+ // Load metrics only mode
+ const float advance_x = (slot->advance.x / FT_SCALEFACTOR) / rasterizer_density;
+ if (out_advance_x != NULL)
+ {
+ IM_ASSERT(out_glyph == NULL);
+ *out_advance_x = advance_x;
+ return true;
+ }
+
+ // Render glyph into a bitmap (currently held by FreeType)
+ FT_Render_Mode render_mode = (bd_font_data->UserFlags & ImGuiFreeTypeLoaderFlags_Monochrome) ? FT_RENDER_MODE_MONO : FT_RENDER_MODE_NORMAL;
+ FT_Error error = FT_Render_Glyph(slot, render_mode);
+ const FT_Bitmap* ft_bitmap = &slot->bitmap;
+ if (error != 0 || ft_bitmap == nullptr)
+ return false;
+
+ const int w = (int)ft_bitmap->width;
+ const int h = (int)ft_bitmap->rows;
+ const bool is_visible = (w != 0 && h != 0);
+
+ // Prepare glyph
+ out_glyph->Codepoint = codepoint;
+ out_glyph->AdvanceX = advance_x;
+
+ // Pack and retrieve position inside texture atlas
+ if (is_visible)
+ {
+ ImFontAtlasRectId pack_id = ImFontAtlasPackAddRect(atlas, w, h);
+ if (pack_id == ImFontAtlasRectId_Invalid)
+ {
+ // Pathological out of memory case (TexMaxWidth/TexMaxHeight set too small?)
+ IM_ASSERT(pack_id != ImFontAtlasRectId_Invalid && "Out of texture memory.");
+ return false;
+ }
+ ImTextureRect* r = ImFontAtlasPackGetRect(atlas, pack_id);
+
+ // Render pixels to our temporary buffer
+ atlas->Builder->TempBuffer.resize(w * h * 4);
+ uint32_t* temp_buffer = (uint32_t*)atlas->Builder->TempBuffer.Data;
+ ImGui_ImplFreeType_BlitGlyph(ft_bitmap, temp_buffer, w);
+
+ const float ref_size = baked->ContainerFont->Sources[0]->SizePixels;
+ const float offsets_scale = (ref_size != 0.0f) ? (baked->Size / ref_size) : 1.0f;
+ float font_off_x = (src->GlyphOffset.x * offsets_scale);
+ float font_off_y = (src->GlyphOffset.y * offsets_scale) + baked->Ascent;
+ if (src->PixelSnapH) // Snap scaled offset. This is to mitigate backward compatibility issues for GlyphOffset, but a better design would be welcome.
+ font_off_x = IM_ROUND(font_off_x);
+ if (src->PixelSnapV)
+ font_off_y = IM_ROUND(font_off_y);
+ float recip_h = 1.0f / rasterizer_density;
+ float recip_v = 1.0f / rasterizer_density;
+
+ // Register glyph
+ float glyph_off_x = (float)face->glyph->bitmap_left;
+ float glyph_off_y = (float)-face->glyph->bitmap_top;
+ out_glyph->X0 = glyph_off_x * recip_h + font_off_x;
+ out_glyph->Y0 = glyph_off_y * recip_v + font_off_y;
+ out_glyph->X1 = (glyph_off_x + w) * recip_h + font_off_x;
+ out_glyph->Y1 = (glyph_off_y + h) * recip_v + font_off_y;
+ out_glyph->Visible = true;
+ out_glyph->Colored = (ft_bitmap->pixel_mode == FT_PIXEL_MODE_BGRA);
+ out_glyph->PackId = pack_id;
+ ImFontAtlasBakedSetFontGlyphBitmap(atlas, baked, src, out_glyph, r, (const unsigned char*)temp_buffer, ImTextureFormat_RGBA32, w * 4);
+ }
+
+ return true;
+}
+
+static bool ImGui_ImplFreetype_FontSrcContainsGlyph(ImFontAtlas* atlas, ImFontConfig* src, ImWchar codepoint)
+{
+ IM_UNUSED(atlas);
+ ImGui_ImplFreeType_FontSrcData* bd_font_data = (ImGui_ImplFreeType_FontSrcData*)src->FontLoaderData;
+ int glyph_index = FT_Get_Char_Index(bd_font_data->FtFace, codepoint);
+ return glyph_index != 0;
+}
+
+const ImFontLoader* ImGuiFreeType::GetFontLoader()
+{
+ static ImFontLoader loader;
+ loader.Name = "FreeType";
+ loader.LoaderInit = ImGui_ImplFreeType_LoaderInit;
+ loader.LoaderShutdown = ImGui_ImplFreeType_LoaderShutdown;
+ loader.FontSrcInit = ImGui_ImplFreeType_FontSrcInit;
+ loader.FontSrcDestroy = ImGui_ImplFreeType_FontSrcDestroy;
+ loader.FontSrcContainsGlyph = ImGui_ImplFreetype_FontSrcContainsGlyph;
+ loader.FontBakedInit = ImGui_ImplFreeType_FontBakedInit;
+ loader.FontBakedDestroy = ImGui_ImplFreeType_FontBakedDestroy;
+ loader.FontBakedLoadGlyph = ImGui_ImplFreeType_FontBakedLoadGlyph;
+ loader.FontBakedSrcLoaderDataSize = sizeof(ImGui_ImplFreeType_FontSrcBakedData);
+ return &loader;
}
void ImGuiFreeType::SetAllocatorFunctions(void* (*alloc_func)(size_t sz, void* user_data), void (*free_func)(void* ptr, void* user_data), void* user_data)
@@ -849,6 +597,22 @@ void ImGuiFreeType::SetAllocatorFunctions(void* (*alloc_func)(size_t sz, void* u
GImGuiFreeTypeAllocatorUserData = user_data;
}
+bool ImGuiFreeType::DebugEditFontLoaderFlags(unsigned int* p_font_loader_flags)
+{
+ bool edited = false;
+ edited |= ImGui::CheckboxFlags("NoHinting", p_font_loader_flags, ImGuiFreeTypeLoaderFlags_NoHinting);
+ edited |= ImGui::CheckboxFlags("NoAutoHint", p_font_loader_flags, ImGuiFreeTypeLoaderFlags_NoAutoHint);
+ edited |= ImGui::CheckboxFlags("ForceAutoHint",p_font_loader_flags, ImGuiFreeTypeLoaderFlags_ForceAutoHint);
+ edited |= ImGui::CheckboxFlags("LightHinting", p_font_loader_flags, ImGuiFreeTypeLoaderFlags_LightHinting);
+ edited |= ImGui::CheckboxFlags("MonoHinting", p_font_loader_flags, ImGuiFreeTypeLoaderFlags_MonoHinting);
+ edited |= ImGui::CheckboxFlags("Bold", p_font_loader_flags, ImGuiFreeTypeLoaderFlags_Bold);
+ edited |= ImGui::CheckboxFlags("Oblique", p_font_loader_flags, ImGuiFreeTypeLoaderFlags_Oblique);
+ edited |= ImGui::CheckboxFlags("Monochrome", p_font_loader_flags, ImGuiFreeTypeLoaderFlags_Monochrome);
+ edited |= ImGui::CheckboxFlags("LoadColor", p_font_loader_flags, ImGuiFreeTypeLoaderFlags_LoadColor);
+ edited |= ImGui::CheckboxFlags("Bitmap", p_font_loader_flags, ImGuiFreeTypeLoaderFlags_Bitmap);
+ return edited;
+}
+
#ifdef IMGUI_ENABLE_FREETYPE_LUNASVG
// For more details, see https://gitlab.freedesktop.org/freetype/freetype-demos/-/blob/master/src/rsvg-port.c
// The original code from the demo is licensed under CeCILL-C Free Software License Agreement (https://gitlab.freedesktop.org/freetype/freetype/-/blob/master/LICENSE.TXT)
@@ -880,8 +644,12 @@ static FT_Error ImGuiLunasvgPortRender(FT_GlyphSlot slot, FT_Pointer* _state)
// rows is height, pitch (or stride) equals to width * sizeof(int32)
lunasvg::Bitmap bitmap((uint8_t*)slot->bitmap.buffer, slot->bitmap.width, slot->bitmap.rows, slot->bitmap.pitch);
+#if LUNASVG_VERSION_MAJOR >= 3
+ state->svg->render(bitmap, state->matrix); // state->matrix is already scaled and translated
+#else
state->svg->setMatrix(state->svg->matrix().identity()); // Reset the svg matrix to the default value
state->svg->render(bitmap, state->matrix); // state->matrix is already scaled and translated
+#endif
state->err = FT_Err_Ok;
return state->err;
}
@@ -904,7 +672,11 @@ static FT_Error ImGuiLunasvgPortPresetSlot(FT_GlyphSlot slot, FT_Bool cache, FT_
return state->err;
}
+#if LUNASVG_VERSION_MAJOR >= 3
+ lunasvg::Box box = state->svg->boundingBox();
+#else
lunasvg::Box box = state->svg->box();
+#endif
double scale = std::min(metrics.x_ppem / box.w, metrics.y_ppem / box.h);
double xx = (double)document->transform.xx / (1 << 16);
double xy = -(double)document->transform.xy / (1 << 16);
@@ -913,6 +685,15 @@ static FT_Error ImGuiLunasvgPortPresetSlot(FT_GlyphSlot slot, FT_Bool cache, FT_
double x0 = (double)document->delta.x / 64 * box.w / metrics.x_ppem;
double y0 = -(double)document->delta.y / 64 * box.h / metrics.y_ppem;
+#if LUNASVG_VERSION_MAJOR >= 3
+ // Scale, transform and pre-translate the matrix for the rendering step
+ state->matrix = lunasvg::Matrix::translated(-box.x, -box.y);
+ state->matrix.multiply(lunasvg::Matrix(xx, xy, yx, yy, x0, y0));
+ state->matrix.scale(scale, scale);
+
+ // Apply updated transformation to the bounding box
+ box.transform(state->matrix);
+#else
// Scale and transform, we don't translate the svg yet
state->matrix.identity();
state->matrix.scale(scale, scale);
@@ -924,6 +705,7 @@ static FT_Error ImGuiLunasvgPortPresetSlot(FT_GlyphSlot slot, FT_Bool cache, FT_
// Get the box again after the transformation
box = state->svg->box();
+#endif
// Calculate the bitmap size
slot->bitmap_left = FT_Int(box.x);
diff --git a/misc/freetype/imgui_freetype.h b/misc/freetype/imgui_freetype.h
index 9d367074e..85313699d 100644
--- a/misc/freetype/imgui_freetype.h
+++ b/misc/freetype/imgui_freetype.h
@@ -6,7 +6,9 @@
#ifndef IMGUI_DISABLE
// Usage:
-// - Add '#define IMGUI_ENABLE_FREETYPE' in your imconfig to enable support for imgui_freetype in imgui.
+// - Add '#define IMGUI_ENABLE_FREETYPE' in your imconfig to automatically enable support
+// for imgui_freetype in imgui. It is equivalent to selecting the default loader with:
+// io.Fonts->SetFontLoader(ImGuiFreeType::GetFontLoader())
// Optional support for OpenType SVG fonts:
// - Add '#define IMGUI_ENABLE_FREETYPE_PLUTOSVG' to use plutosvg (not provided). See #7927.
@@ -14,44 +16,67 @@
// Forward declarations
struct ImFontAtlas;
-struct ImFontBuilderIO;
+struct ImFontLoader;
// Hinting greatly impacts visuals (and glyph sizes).
// - By default, hinting is enabled and the font's native hinter is preferred over the auto-hinter.
// - When disabled, FreeType generates blurrier glyphs, more or less matches the stb_truetype.h
// - The Default hinting mode usually looks good, but may distort glyphs in an unusual way.
// - The Light hinting mode generates fuzzier glyphs but better matches Microsoft's rasterizer.
-// You can set those flags globally in ImFontAtlas::FontBuilderFlags
-// You can set those flags on a per font basis in ImFontConfig::FontBuilderFlags
-enum ImGuiFreeTypeBuilderFlags
+// You can set those flags globally in ImFontAtlas::FontLoaderFlags
+// You can set those flags on a per font basis in ImFontConfig::FontLoaderFlags
+typedef unsigned int ImGuiFreeTypeLoaderFlags;
+enum ImGuiFreeTypeLoaderFlags_
{
- ImGuiFreeTypeBuilderFlags_NoHinting = 1 << 0, // Disable hinting. This generally generates 'blurrier' bitmap glyphs when the glyph are rendered in any of the anti-aliased modes.
- ImGuiFreeTypeBuilderFlags_NoAutoHint = 1 << 1, // Disable auto-hinter.
- ImGuiFreeTypeBuilderFlags_ForceAutoHint = 1 << 2, // Indicates that the auto-hinter is preferred over the font's native hinter.
- ImGuiFreeTypeBuilderFlags_LightHinting = 1 << 3, // A lighter hinting algorithm for gray-level modes. Many generated glyphs are fuzzier but better resemble their original shape. This is achieved by snapping glyphs to the pixel grid only vertically (Y-axis), as is done by Microsoft's ClearType and Adobe's proprietary font renderer. This preserves inter-glyph spacing in horizontal text.
- ImGuiFreeTypeBuilderFlags_MonoHinting = 1 << 4, // Strong hinting algorithm that should only be used for monochrome output.
- ImGuiFreeTypeBuilderFlags_Bold = 1 << 5, // Styling: Should we artificially embolden the font?
- ImGuiFreeTypeBuilderFlags_Oblique = 1 << 6, // Styling: Should we slant the font, emulating italic style?
- ImGuiFreeTypeBuilderFlags_Monochrome = 1 << 7, // Disable anti-aliasing. Combine this with MonoHinting for best results!
- ImGuiFreeTypeBuilderFlags_LoadColor = 1 << 8, // Enable FreeType color-layered glyphs
- ImGuiFreeTypeBuilderFlags_Bitmap = 1 << 9 // Enable FreeType bitmap glyphs
+ ImGuiFreeTypeLoaderFlags_NoHinting = 1 << 0, // Disable hinting. This generally generates 'blurrier' bitmap glyphs when the glyph are rendered in any of the anti-aliased modes.
+ ImGuiFreeTypeLoaderFlags_NoAutoHint = 1 << 1, // Disable auto-hinter.
+ ImGuiFreeTypeLoaderFlags_ForceAutoHint = 1 << 2, // Indicates that the auto-hinter is preferred over the font's native hinter.
+ ImGuiFreeTypeLoaderFlags_LightHinting = 1 << 3, // A lighter hinting algorithm for gray-level modes. Many generated glyphs are fuzzier but better resemble their original shape. This is achieved by snapping glyphs to the pixel grid only vertically (Y-axis), as is done by Microsoft's ClearType and Adobe's proprietary font renderer. This preserves inter-glyph spacing in horizontal text.
+ ImGuiFreeTypeLoaderFlags_MonoHinting = 1 << 4, // Strong hinting algorithm that should only be used for monochrome output.
+ ImGuiFreeTypeLoaderFlags_Bold = 1 << 5, // Styling: Should we artificially embolden the font?
+ ImGuiFreeTypeLoaderFlags_Oblique = 1 << 6, // Styling: Should we slant the font, emulating italic style?
+ ImGuiFreeTypeLoaderFlags_Monochrome = 1 << 7, // Disable anti-aliasing. Combine this with MonoHinting for best results!
+ ImGuiFreeTypeLoaderFlags_LoadColor = 1 << 8, // Enable FreeType color-layered glyphs
+ ImGuiFreeTypeLoaderFlags_Bitmap = 1 << 9, // Enable FreeType bitmap glyphs
+
+#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
+ ImGuiFreeTypeBuilderFlags_NoHinting = ImGuiFreeTypeLoaderFlags_NoHinting,
+ ImGuiFreeTypeBuilderFlags_NoAutoHint = ImGuiFreeTypeLoaderFlags_NoAutoHint,
+ ImGuiFreeTypeBuilderFlags_ForceAutoHint = ImGuiFreeTypeLoaderFlags_ForceAutoHint,
+ ImGuiFreeTypeBuilderFlags_LightHinting = ImGuiFreeTypeLoaderFlags_LightHinting,
+ ImGuiFreeTypeBuilderFlags_MonoHinting = ImGuiFreeTypeLoaderFlags_MonoHinting,
+ ImGuiFreeTypeBuilderFlags_Bold = ImGuiFreeTypeLoaderFlags_Bold,
+ ImGuiFreeTypeBuilderFlags_Oblique = ImGuiFreeTypeLoaderFlags_Oblique,
+ ImGuiFreeTypeBuilderFlags_Monochrome = ImGuiFreeTypeLoaderFlags_Monochrome,
+ ImGuiFreeTypeBuilderFlags_LoadColor = ImGuiFreeTypeLoaderFlags_LoadColor,
+ ImGuiFreeTypeBuilderFlags_Bitmap = ImGuiFreeTypeLoaderFlags_Bitmap,
+#endif
};
+// Obsolete names (will be removed)
+#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
+typedef ImGuiFreeTypeLoaderFlags_ ImGuiFreeTypeBuilderFlags_;
+#endif
+
namespace ImGuiFreeType
{
// This is automatically assigned when using '#define IMGUI_ENABLE_FREETYPE'.
// If you need to dynamically select between multiple builders:
- // - you can manually assign this builder with 'atlas->FontBuilderIO = ImGuiFreeType::GetBuilderForFreeType()'
- // - prefer deep-copying this into your own ImFontBuilderIO instance if you use hot-reloading that messes up static data.
- IMGUI_API const ImFontBuilderIO* GetBuilderForFreeType();
+ // - you can manually assign this builder with 'atlas->SetFontLoader(ImGuiFreeType::GetFontLoader())'
+ // - prefer deep-copying this into your own ImFontLoader instance if you use hot-reloading that messes up static data.
+ IMGUI_API const ImFontLoader* GetFontLoader();
// Override allocators. By default ImGuiFreeType will use IM_ALLOC()/IM_FREE()
// However, as FreeType does lots of allocations we provide a way for the user to redirect it to a separate memory heap if desired.
IMGUI_API void SetAllocatorFunctions(void* (*alloc_func)(size_t sz, void* user_data), void (*free_func)(void* ptr, void* user_data), void* user_data = nullptr);
- // Obsolete names (will be removed soon)
+ // Display UI to edit ImFontAtlas::FontLoaderFlags (shared) or ImFontConfig::FontLoaderFlags (single source)
+ IMGUI_API bool DebugEditFontLoaderFlags(ImGuiFreeTypeLoaderFlags* p_font_loader_flags);
+
+ // Obsolete names (will be removed)
#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
- //static inline bool BuildFontAtlas(ImFontAtlas* atlas, unsigned int flags = 0) { atlas->FontBuilderIO = GetBuilderForFreeType(); atlas->FontBuilderFlags = flags; return atlas->Build(); } // Prefer using '#define IMGUI_ENABLE_FREETYPE'
+ //IMGUI_API const ImFontBuilderIO* GetBuilderForFreeType(); // Renamed/changed in 1.92. Change 'io.Fonts->FontBuilderIO = ImGuiFreeType::GetBuilderForFreeType()' to 'io.Fonts->SetFontLoader(ImGuiFreeType::GetFontLoader())' if you need runtime selection.
+ //static inline bool BuildFontAtlas(ImFontAtlas* atlas, unsigned int flags = 0) { atlas->FontBuilderIO = GetBuilderForFreeType(); atlas->FontLoaderFlags = flags; return atlas->Build(); } // Prefer using '#define IMGUI_ENABLE_FREETYPE'
#endif
}