Skip to content

Commit 4a97c47

Browse files
committed
Fix textbox rendering and camera setup
1 parent b90d0c1 commit 4a97c47

File tree

3 files changed

+45
-45
lines changed

3 files changed

+45
-45
lines changed

src/engine/SceneManagement/SceneBuilder.jl

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,8 @@ module SceneBuilderModule
150150
MAIN.scene.uiElements = scene[2]
151151
MAIN.scene.camera = scene[3]
152152

153-
if size.x < MAIN.scene.camera.size.x && size.x > 0
154-
MAIN.scene.camera.size = Vector2(size.x, MAIN.scene.camera.size.y)
155-
end
156-
if size.y < MAIN.scene.camera.size.y && size.y > 0
157-
MAIN.scene.camera.size = Vector2(MAIN.scene.camera.size.x, size.y)
158-
end
159153
if !JulGame.IS_EDITOR && !JulGame.IS_WEB
154+
@info "Setting logical size to $(MAIN.scene.camera.size.x)x$(MAIN.scene.camera.size.y)"
160155
SDL2.SDL_RenderSetLogicalSize(JulGame.Renderer, MAIN.scene.camera.size.x, MAIN.scene.camera.size.y)
161156
end
162157

src/engine/UI/ImmediateUI.jl

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ module ImmediateUIModule
1818
const IMMEDIATE_UI_TIMESTAMPS = Dict{String, UInt64}()
1919

2020
# Lifetime in milliseconds before an unused immediate component is removed (default: 5 seconds)
21-
const DEFAULT_LIFETIME = 200
21+
const DEFAULT_LIFETIME = 5000
2222

2323
"""
2424
immediate_text(id::String, text::String;
2525
name::String = "TextBox",
26-
anchor::Symbol = :center,
26+
anchor::Symbol = :none,
2727
anchorOffset::Math.Vector2 = Math.Vector2(0,0),
2828
isWorldEntity::Bool=false,
2929
layer::Int=0,
@@ -66,7 +66,7 @@ module ImmediateUIModule
6666
# Returns
6767
The TextBox object
6868
"""
69-
function immediate_text(text::String, id::String;
69+
function immediate_text(id::String, text::String = "TextBox";
7070
name::String = "TextBox",
7171
anchor::Symbol = :none,
7272
anchorOffset::Math.Vector2 = Math.Vector2(0,0),
@@ -162,10 +162,6 @@ module ImmediateUIModule
162162
# Reload font and regenerate texture
163163
UI.load_font(textBox, joinpath(BasePath, "assets", "fonts"), fontPath)
164164
UI.rerender_text(textBox)
165-
166-
if anchor != :none
167-
UI.center_text(textBox)
168-
end
169165
end
170166

171167
# Ensure the component is in the scene's uiElements
@@ -175,6 +171,7 @@ module ImmediateUIModule
175171

176172
return textBox
177173
else
174+
@debug "creating new text component with id $(composite_id): $(length(IMMEDIATE_UI_CACHE))"
178175
# Create new text component
179176
textBox = TextBox(text;
180177
id=id,
@@ -967,26 +964,16 @@ module ImmediateUIModule
967964
component_layers = Dict{String, Int}()
968965

969966
# First pass: collect layers for each component and check expiration
970-
for (id, component) in IMMEDIATE_UI_CACHE
971-
# Skip if the component is not properly initialized
972-
if !isdefined(component.element, :isActive) || component.element === nothing
973-
push!(expired_ids, id)
974-
continue
975-
end
976-
967+
for (composite_id, component) in IMMEDIATE_UI_CACHE
977968
# Check if this component hasn't been used for a while
978-
if !haskey(IMMEDIATE_UI_TIMESTAMPS, id) || current_time - IMMEDIATE_UI_TIMESTAMPS[id] > component.lifetime
979-
push!(expired_ids, id)
980-
continue
981-
end
982-
983-
# Skip inactive components
984-
if isdefined(component.element, :isActive) && !component.element.isActive
969+
if !haskey(IMMEDIATE_UI_TIMESTAMPS, composite_id) || current_time - IMMEDIATE_UI_TIMESTAMPS[composite_id] > component.lifetime
970+
@info "component $(composite_id) expired from lifetime $(component.lifetime)"
971+
push!(expired_ids, composite_id)
985972
continue
986973
end
987974

988975
# Store the layer for sorting
989-
component_layers[id] = isdefined(component.element, :layer) ? component.element.layer : 0
976+
component_layers[composite_id] = component.element.layer
990977
end
991978

992979
# Sort component IDs by layer

src/engine/UI/TextBox.jl

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ module TextBoxModule
5757
this.hoverEnterEvents = hoverEnterEvents
5858
this.hoverExitEvents = hoverExitEvents
5959

60+
this.font = C_NULL
6061
this.fontPath = fontPath
6162
this.fontSize = fontSize # Store the base font size
6263
this.id = id
@@ -144,17 +145,26 @@ module TextBoxModule
144145

145146
function UI.load_font(this::TextBox, basePath::String, fontPath::String)
146147
@debug string("loading font from $(basePath)\\$(fontPath)")
147-
148148
# Calculate the true font size based on window resolution
149149
#trueFontSize = get_true_font_size(this.fontSize)
150150
trueFontSize = this.fontSize
151+
152+
# If the font is already loaded, clean it up
153+
if this.font != C_NULL
154+
println("closing font")
155+
println(this.font)
156+
SDL2.TTF_CloseFont(this.font)
157+
this.font = C_NULL
158+
end
159+
free_text_resources(this)
160+
151161

152162
this.font = load_font_sdl(basePath, fontPath, trueFontSize)
153163
if this.font == C_NULL
154164
error("Failed to load font, $(unsafe_string(SDL2.SDL_GetError()))")
155165
return
156166
end
157-
if fontPath != joinpath("FiraCode-Regular.ttf")
167+
if fontPath != "Default"
158168
this.fontPath = fontPath
159169
end
160170

@@ -220,7 +230,7 @@ module TextBoxModule
220230
if rw != C_NULL
221231
@debug("loading font from cache")
222232
@debug("comma separated path: ", get_comma_separated_path(fontPath))
223-
return SDL2.TTF_OpenFontRW(rw, 1, Math.TypeConversions.safe_int32_convert(fontSize))
233+
return CallSDLFunction(SDL2.TTF_OpenFontRW, rw, 1, Math.TypeConversions.safe_int32_convert(fontSize))
224234
end
225235
end
226236
@debug "Loading font from disk, there are $(length(JulGame.FONT_CACHE)) fonts in cache"
@@ -254,12 +264,7 @@ module TextBoxModule
254264
# Examples
255265
"""
256266
function UI.rerender_text(this::TextBox)
257-
if this.renderText != C_NULL
258-
SDL2.SDL_FreeSurface(this.renderText)
259-
end
260-
if this.textTexture != C_NULL
261-
SDL2.SDL_DestroyTexture(this.textTexture)
262-
end
267+
free_text_resources(this)
263268

264269
# Check if we need to wrap text
265270
if this.maxLineWidth > 0
@@ -288,12 +293,23 @@ module TextBoxModule
288293
this.size = Math.Vector2(surface[1].w, surface[1].h)
289294

290295
this.textTexture = SDL2.SDL_CreateTextureFromSurface(JulGame.Renderer::Ptr{SDL2.SDL_Renderer}, this.renderText)
291-
296+
292297
if !this.isWorldEntity
293298
UI.center_text(this)
294299
end
295300
end
296301

302+
function free_text_resources(this::TextBox)
303+
if this.renderText != C_NULL
304+
SDL2.SDL_FreeSurface(this.renderText)
305+
this.renderText = C_NULL
306+
end
307+
if this.textTexture != C_NULL
308+
SDL2.SDL_DestroyTexture(this.textTexture)
309+
this.textTexture = C_NULL
310+
end
311+
end
312+
297313
# Helper function to manually wrap text at character boundaries
298314
function wrap_text(text::String, font, maxWidth::Int, wrapWords::Bool)
299315
if maxWidth <= 0 || isempty(text)
@@ -361,7 +377,7 @@ module TextBoxModule
361377
return
362378
end
363379

364-
@info "centering text $(this.name) with anchor $(this.anchor.current_state)"
380+
#@info "centering text $(this.name) with anchor $(this.anchor.current_state)"
365381
if this.anchor.current_state == :center
366382
this.position = Math.Vector2(max(MAIN.scene.camera.size.x/2 - this.size.x/2, 0) + this.anchorOffset.x, max(MAIN.scene.camera.size.y/2 - this.size.y/2, 0) + this.anchorOffset.y)
367383
elseif this.anchor.current_state == :top
@@ -396,7 +412,9 @@ module TextBoxModule
396412

397413
# Close the current font
398414
if this.font != C_NULL
415+
println("closing font from update_font_size")
399416
SDL2.TTF_CloseFont(this.font)
417+
this.font = C_NULL
400418
end
401419

402420
# Load the font with the scaled size
@@ -436,12 +454,11 @@ module TextBoxModule
436454
end
437455

438456
function UI.destroy(this::TextBox)
439-
if this.textTexture == C_NULL
440-
return
457+
if this.font != C_NULL
458+
SDL2.TTF_CloseFont(this.font)
459+
this.font = C_NULL
441460
end
442-
443-
SDL2.SDL_DestroyTexture(this.textTexture)
444-
this.textTexture = C_NULL
461+
free_text_resources(this)
445462
end
446463
#=
447464
function Base.setproperty!(this::TextBox, s::Symbol, x)
@@ -494,8 +511,9 @@ module TextBoxModule
494511
function UI.handle_window_resize(this::TextBox)
495512
if this.font != C_NULL
496513
# Close the current font
514+
println("closing font from handle_window_resize")
497515
SDL2.TTF_CloseFont(this.font)
498-
516+
this.font = C_NULL
499517
# Reload the font with the new scaled size
500518
basePath = joinpath(BasePath, "assets", "fonts")
501519
UI.load_font(this, basePath, joinpath(this.fontPath))

0 commit comments

Comments
 (0)