Skip to content

Commit ec1548c

Browse files
committed
Fixed scaling to remain constant regardless of screen.
1 parent 2f51b58 commit ec1548c

2 files changed

Lines changed: 46 additions & 27 deletions

File tree

src/graphics/ui_init.cpp

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,24 @@
1111

1212

1313
void init_imgui(GLFWwindow* window) {
14-
IMGUI_CHECKVERSION();
15-
ImGui::CreateContext();
16-
ImGuiIO& io = ImGui::GetIO();
17-
float xscale = 1.0f;
18-
float yscale = 1.0f;
19-
14+
IMGUI_CHECKVERSION();
15+
ImGui::CreateContext();
16+
ImGuiIO& io = ImGui::GetIO();
17+
io.FontGlobalScale = 2.0f;
18+
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
19+
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;
20+
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
21+
ImGui_ImplGlfw_InitForOpenGL(window, true);
2022
#ifdef __EMSCRIPTEN__
21-
xscale = emscripten_get_device_pixel_ratio();
22-
#else
23-
glfwGetWindowContentScale(window, &xscale, &yscale);
24-
#endif
25-
io.FontGlobalScale = xscale;
2623

27-
//ImGui::GetStyle().ScaleAllSizes(xscale);
28-
29-
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
30-
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;
31-
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
32-
ImGui_ImplGlfw_InitForOpenGL(window, true);
33-
34-
#ifdef __EMSCRIPTEN__
35-
emscripten_browser_clipboard::paste([](std::string&& paste_data, void* callback_data) {
36-
clip_content = std::move(paste_data);
37-
just_pasted = true;
38-
}, nullptr);
39-
40-
ImGui_ImplOpenGL3_Init("#version 300 es");
24+
emscripten_browser_clipboard::paste([](std::string&& paste_data, void* callback_data) {
25+
clip_content = std::move(paste_data);
26+
just_pasted = true;
27+
}, nullptr);
28+
29+
ImGui_ImplOpenGL3_Init("#version 300 es");
4130
#else
42-
ImGui_ImplOpenGL3_Init();
31+
ImGui_ImplOpenGL3_Init();
4332
#endif
4433
}
4534

src/interactions/interactions.cpp

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,46 @@ void window_size_callback(GLFWwindow* window, const int width, const int height)
103103
state->height = static_cast<float>(height);
104104
}
105105

106+
void apply_resolution_scaling(int current_height) {
107+
if (current_height <= 0) return;
108+
109+
ImGuiIO& io = ImGui::GetIO();
110+
float base_height = 1080.0f;
111+
float raw_scale = static_cast<float>(current_height) / base_height;
112+
113+
if (raw_scale < 0.75f) raw_scale = 0.75f;
114+
if (raw_scale > 2.5f) raw_scale = 2.5f;
115+
116+
float scale = std::round(raw_scale * 4.0f) / 4.0f;
117+
118+
io.FontGlobalScale = scale;
119+
120+
ImGui::GetStyle() = ImGuiStyle();
121+
ImGui::GetStyle().ScaleAllSizes(scale);
122+
123+
ImGuiStyle& style = ImGui::GetStyle();
124+
if (style.WindowBorderSize < 1.0f) style.WindowBorderSize = 1.0f;
125+
if (style.FrameBorderSize < 1.0f) style.FrameBorderSize = 1.0f;
126+
if (style.PopupBorderSize < 1.0f) style.PopupBorderSize = 1.0f;
127+
if (style.ChildBorderSize < 1.0f) style.ChildBorderSize = 1.0f;
128+
if (style.TabBorderSize < 1.0f) style.TabBorderSize = 1.0f;
129+
130+
#if IMGUI_VERSION_NUM >= 18900
131+
if (style.SeparatorTextBorderSize < 1.0f) style.SeparatorTextBorderSize = 1.0f;
132+
#endif
133+
}
134+
106135
void framebuffer_size_callback(GLFWwindow* window, const int width, const int height) {
107136
glViewport(0, 0, width, height);
137+
//apply_resolution_scaling(height);
108138
}
109139

110140
#ifdef __EMSCRIPTEN__
111141
EM_BOOL browser_resize_callback(int event_type, const EmscriptenUiEvent* ui_event, void* data) {
112142
GLFWwindow* window = static_cast<GLFWwindow*>(data);
113143
const double width = ui_event->windowInnerWidth;
114144
const double height = ui_event->windowInnerHeight;
115-
const double ratio = emscripten_get_device_pixel_ratio();
145+
const double ratio = 2;
116146

117147
emscripten_set_canvas_element_size("#canvas", width * ratio, height * ratio);
118148

0 commit comments

Comments
 (0)