1
0
mirror of https://github.com/ocornut/imgui.git synced 2024-12-18 02:26:06 +01:00

Examples: Extracted gamepad code into ImGui_ImplGlfw_UpdateGamepads(). Renamed matching Win32 function for consistency.

Added more link to nothing's oversample document. Spacing bits.
This commit is contained in:
omar 2019-02-14 18:55:08 +01:00
parent 3c07ec6a61
commit 93d1179805
5 changed files with 53 additions and 44 deletions

View File

@ -265,31 +265,13 @@ static void ImGui_ImplGlfw_UpdateMouseCursor()
} }
} }
void ImGui_ImplGlfw_NewFrame() static void ImGui_ImplGlfw_UpdateGamepads()
{ {
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();
IM_ASSERT(io.Fonts->IsBuilt() && "Font atlas not built! It is generally built by the renderer back-end. Missing call to renderer _NewFrame() function? e.g. ImGui_ImplOpenGL3_NewFrame().");
// Setup display size (every frame to accommodate for window resizing)
int w, h;
int display_w, display_h;
glfwGetWindowSize(g_Window, &w, &h);
glfwGetFramebufferSize(g_Window, &display_w, &display_h);
io.DisplaySize = ImVec2((float)w, (float)h);
io.DisplayFramebufferScale = ImVec2(w > 0 ? ((float)display_w / w) : 0, h > 0 ? ((float)display_h / h) : 0);
// Setup time step
double current_time = glfwGetTime();
io.DeltaTime = g_Time > 0.0 ? (float)(current_time - g_Time) : (float)(1.0f/60.0f);
g_Time = current_time;
ImGui_ImplGlfw_UpdateMousePosAndButtons();
ImGui_ImplGlfw_UpdateMouseCursor();
// Gamepad navigation mapping
memset(io.NavInputs, 0, sizeof(io.NavInputs)); memset(io.NavInputs, 0, sizeof(io.NavInputs));
if (io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) if ((io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) == 0)
{ return;
// Update gamepad inputs // Update gamepad inputs
#define MAP_BUTTON(NAV_NO, BUTTON_NO) { if (buttons_count > BUTTON_NO && buttons[BUTTON_NO] == GLFW_PRESS) io.NavInputs[NAV_NO] = 1.0f; } #define MAP_BUTTON(NAV_NO, BUTTON_NO) { if (buttons_count > BUTTON_NO && buttons[BUTTON_NO] == GLFW_PRESS) io.NavInputs[NAV_NO] = 1.0f; }
#define MAP_ANALOG(NAV_NO, AXIS_NO, V0, V1) { float v = (axes_count > AXIS_NO) ? axes[AXIS_NO] : V0; v = (v - V0) / (V1 - V0); if (v > 1.0f) v = 1.0f; if (io.NavInputs[NAV_NO] < v) io.NavInputs[NAV_NO] = v; } #define MAP_ANALOG(NAV_NO, AXIS_NO, V0, V1) { float v = (axes_count > AXIS_NO) ? axes[AXIS_NO] : V0; v = (v - V0) / (V1 - V0); if (v > 1.0f) v = 1.0f; if (io.NavInputs[NAV_NO] < v) io.NavInputs[NAV_NO] = v; }
@ -319,4 +301,28 @@ void ImGui_ImplGlfw_NewFrame()
else else
io.BackendFlags &= ~ImGuiBackendFlags_HasGamepad; io.BackendFlags &= ~ImGuiBackendFlags_HasGamepad;
} }
void ImGui_ImplGlfw_NewFrame()
{
ImGuiIO& io = ImGui::GetIO();
IM_ASSERT(io.Fonts->IsBuilt() && "Font atlas not built! It is generally built by the renderer back-end. Missing call to renderer _NewFrame() function? e.g. ImGui_ImplOpenGL3_NewFrame().");
// Setup display size (every frame to accommodate for window resizing)
int w, h;
int display_w, display_h;
glfwGetWindowSize(g_Window, &w, &h);
glfwGetFramebufferSize(g_Window, &display_w, &display_h);
io.DisplaySize = ImVec2((float)w, (float)h);
io.DisplayFramebufferScale = ImVec2(w > 0 ? ((float)display_w / w) : 0, h > 0 ? ((float)display_h / h) : 0);
// Setup time step
double current_time = glfwGetTime();
io.DeltaTime = g_Time > 0.0 ? (float)(current_time - g_Time) : (float)(1.0f/60.0f);
g_Time = current_time;
ImGui_ImplGlfw_UpdateMousePosAndButtons();
ImGui_ImplGlfw_UpdateMouseCursor();
// Gamepad navigation mapping
ImGui_ImplGlfw_UpdateGamepads();
} }

View File

@ -150,7 +150,7 @@ static void ImGui_ImplWin32_UpdateMousePos()
#endif #endif
// Gamepad navigation mapping // Gamepad navigation mapping
void ImGui_ImplWin32_UpdateGameControllers() static void ImGui_ImplWin32_UpdateGamepads()
{ {
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();
memset(io.NavInputs, 0, sizeof(io.NavInputs)); memset(io.NavInputs, 0, sizeof(io.NavInputs));
@ -231,7 +231,7 @@ void ImGui_ImplWin32_NewFrame()
} }
// Update game controllers (if available) // Update game controllers (if available)
ImGui_ImplWin32_UpdateGameControllers(); ImGui_ImplWin32_UpdateGamepads();
} }
// Allow compilation with old Windows SDK. MinGW doesn't have default _WIN32_WINNT/WINVER versions. // Allow compilation with old Windows SDK. MinGW doesn't have default _WIN32_WINNT/WINVER versions.

View File

@ -1361,7 +1361,7 @@ ImFontConfig::ImFontConfig()
FontDataOwnedByAtlas = true; FontDataOwnedByAtlas = true;
FontNo = 0; FontNo = 0;
SizePixels = 0.0f; SizePixels = 0.0f;
OversampleH = 3; OversampleH = 3; // FIXME: 2 may be a better default?
OversampleV = 1; OversampleV = 1;
PixelSnapH = false; PixelSnapH = false;
GlyphExtraSpacing = ImVec2(0.0f, 0.0f); GlyphExtraSpacing = ImVec2(0.0f, 0.0f);

View File

@ -25,7 +25,7 @@ If you have other loading/merging/adding fonts, you can post on the Dear ImGui "
- Building Custom Glyph Ranges - Building Custom Glyph Ranges
- Embedding Fonts in Source Code - Embedding Fonts in Source Code
- Credits/Licences for fonts included in this folder - Credits/Licences for fonts included in this folder
- Links, Other fonts - Fonts Links
--------------------------------------- ---------------------------------------
@ -106,11 +106,14 @@ Load .TTF/.OTF file with:
For advanced options create a ImFontConfig structure and pass it to the AddFont function (it will be copied internally): For advanced options create a ImFontConfig structure and pass it to the AddFont function (it will be copied internally):
ImFontConfig config; ImFontConfig config;
config.OversampleH = 3; config.OversampleH = 2;
config.OversampleV = 1; config.OversampleV = 1;
config.GlyphExtraSpacing.x = 1.0f; config.GlyphExtraSpacing.x = 1.0f;
ImFont* font = io.Fonts->AddFontFromFileTTF("font.ttf", size_pixels, &config); ImFont* font = io.Fonts->AddFontFromFileTTF("font.ttf", size_pixels, &config);
Read about oversampling here:
https://github.com/nothings/stb/blob/master/tests/oversample
If you have very large number of glyphs or multiple fonts, the texture may become too big for your graphics API. If you have very 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 glyphs appears as white rectangles. The typical result of failing to upload a texture is if every glyphs appears as white rectangles.
In particular, using a large range such as GetGlyphRangesChineseSimplifiedCommon() is not recommended unless you In particular, using a large range such as GetGlyphRangesChineseSimplifiedCommon() is not recommended unless you