Skip to content

Commit c842130

Browse files
committed
Added scaling setting
1 parent 17534e5 commit c842130

File tree

1 file changed

+144
-0
lines changed

1 file changed

+144
-0
lines changed

src/editor/JulGameEditor/Components/MainMenuBar.jl

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@ using CImGui.CSyntax
33
using CImGui.CSyntax.CStatic
44
using Dates
55

6+
# Global editor scale factor (persisted across frames)
7+
const EDITOR_SCALE = Ref(1.0f0)
8+
const EDITOR_SCALE_MIN = 0.5f0
9+
const EDITOR_SCALE_MAX = 2.5f0
10+
const EDITOR_SCALE_STEP = 0.1f0
11+
12+
# Keep a copy of the base style so scaling is stable (non-cumulative).
13+
const _BASE_STYLE_SET = Ref(false)
14+
const _BASE_STYLE = Ref{CImGui.ImGuiStyle}()
15+
616
"""
717
ShowAppMainMenuBar(events)
818
Create a fullscreen menu bar and populate it.
@@ -33,6 +43,13 @@ function show_main_menu_bar(events, main, recent_paths::Vector)
3343
end
3444
end
3545

46+
@cstatic buf="View"*"\0"^128 begin
47+
if CImGui.BeginMenu(buf)
48+
show_view_menu()
49+
CImGui.EndMenu()
50+
end
51+
end
52+
3653
CImGui.EndMainMenuBar()
3754
end
3855
end
@@ -125,4 +142,131 @@ function show_tools_menu(events)
125142
if CImGui.MenuItem("Open Script", "Ctrl+O")
126143
events["Open-script"]()
127144
end
145+
end
146+
147+
function show_view_menu()
148+
# Get the ImGui style pointer
149+
style = CImGui.GetStyle()
150+
io = CImGui.GetIO()
151+
152+
if CImGui.BeginMenu("Editor Scale")
153+
# Show current scale
154+
CImGui.Text("Current: $(round(Int, EDITOR_SCALE[] * 100))%")
155+
CImGui.Separator()
156+
157+
# Preset scale options
158+
scale_presets = [
159+
(0.75f0, "75%"),
160+
(1.0f0, "100% (Default)"),
161+
(1.25f0, "125%"),
162+
(1.5f0, "150%"),
163+
(1.75f0, "175%"),
164+
(2.0f0, "200%"),
165+
]
166+
167+
for (scale_value, label) in scale_presets
168+
is_selected = abs(EDITOR_SCALE[] - scale_value) < 0.01f0
169+
if CImGui.MenuItem(label, "", is_selected)
170+
apply_editor_scale(scale_value, style, io)
171+
end
172+
end
173+
174+
CImGui.Separator()
175+
176+
# Custom scale slider
177+
CImGui.Text("Custom Scale:")
178+
CImGui.SetNextItemWidth(150)
179+
if @c CImGui.SliderFloat("##scale_slider", &EDITOR_SCALE[], EDITOR_SCALE_MIN, EDITOR_SCALE_MAX, "%.2f")
180+
apply_editor_scale(EDITOR_SCALE[], style, io)
181+
end
182+
183+
CImGui.Separator()
184+
185+
# Increase/Decrease buttons
186+
if CImGui.MenuItem("Increase Scale", "Ctrl++")
187+
new_scale = min(EDITOR_SCALE[] + EDITOR_SCALE_STEP, EDITOR_SCALE_MAX)
188+
apply_editor_scale(new_scale, style, io)
189+
end
190+
191+
if CImGui.MenuItem("Decrease Scale", "Ctrl+-")
192+
new_scale = max(EDITOR_SCALE[] - EDITOR_SCALE_STEP, EDITOR_SCALE_MIN)
193+
apply_editor_scale(new_scale, style, io)
194+
end
195+
196+
CImGui.Separator()
197+
198+
if CImGui.MenuItem("Reset to Default")
199+
apply_editor_scale(1.0f0, style, io)
200+
end
201+
202+
CImGui.EndMenu()
203+
end
204+
205+
CImGui.Separator()
206+
207+
if CImGui.BeginMenu("DPI Info")
208+
# Read-only diagnostics (useful to confirm whether SDL is reporting HiDPI)
209+
fb = unsafe_load(io.DisplayFramebufferScale)
210+
CImGui.Text("DisplayFramebufferScale: $(round(fb.x; digits=2)) x $(round(fb.y; digits=2))")
211+
CImGui.Text("Window DPI scale: $(round(Float64(CImGui.igGetWindowDpiScale()); digits=2))")
212+
CImGui.Separator()
213+
214+
if CImGui.MenuItem("Set editor scale = window DPI scale")
215+
apply_editor_scale(Float32(CImGui.igGetWindowDpiScale()), style, io)
216+
end
217+
218+
CImGui.EndMenu()
219+
end
220+
221+
CImGui.Separator()
222+
223+
# Style options
224+
if CImGui.BeginMenu("Theme")
225+
if CImGui.MenuItem("Dark")
226+
CImGui.StyleColorsDark()
227+
end
228+
if CImGui.MenuItem("Light")
229+
CImGui.StyleColorsLight()
230+
end
231+
if CImGui.MenuItem("Classic")
232+
CImGui.StyleColorsClassic()
233+
end
234+
CImGui.EndMenu()
235+
end
236+
end
237+
238+
"""
239+
apply_editor_scale(new_scale, style, io)
240+
241+
Apply a new scale factor to the editor UI.
242+
243+
# Arguments
244+
- `new_scale`: The new scale factor (1.0 = 100%)
245+
- `style`: Pointer to ImGuiStyle
246+
- `io`: Pointer to ImGuiIO
247+
"""
248+
function apply_editor_scale(new_scale::Float32, style, io)
249+
# Clamp to configured limits
250+
clamped = min(max(new_scale, EDITOR_SCALE_MIN), EDITOR_SCALE_MAX)
251+
EDITOR_SCALE[] = clamped
252+
253+
# Capture the base style once so scaling doesn't accumulate across changes.
254+
if !_BASE_STYLE_SET[]
255+
_BASE_STYLE[] = unsafe_load(style)
256+
_BASE_STYLE_SET[] = true
257+
end
258+
259+
# Reset to base style, then apply scaling deterministically.
260+
unsafe_store!(style, _BASE_STYLE[])
261+
262+
# Scale spacing/paddings/etc (does not inherently scale fonts).
263+
CImGui.ImGuiStyle_ScaleAllSizes(style, clamped)
264+
265+
# Scale fonts using FontGlobalScale - this is THE key setting for font scaling!
266+
font_global_scale_ptr = io.FontGlobalScale
267+
unsafe_store!(font_global_scale_ptr, clamped)
268+
269+
# Scale mouse cursor
270+
mouse_cursor_scale_ptr = style.MouseCursorScale
271+
unsafe_store!(mouse_cursor_scale_ptr, clamped)
128272
end

0 commit comments

Comments
 (0)