@@ -3,60 +3,79 @@ module TextBoxModule
3
3
using .. UI. JulGame. Math
4
4
import .. UI
5
5
export TextBox
6
- mutable struct TextBox
7
- anchorOffset:: Vector2
8
- # anchor::JulGame.Enum
9
- clickEvents
10
- font
6
+ mutable struct TextBox <: UI.UIElement
7
+ font:: Union{Ptr{SDL2.TTF_Font}, Ptr{Nothing}}
11
8
fontPath:: String
12
9
fontSize:: Int
13
- id:: String
14
- isActive:: Bool
15
- isCenteredX:: Bool
16
- isCenteredY:: Bool
17
- isHovered:: Bool
18
- isWorldEntity:: Bool
19
- layer:: Int
20
- name:: String
21
- persistentBetweenScenes:: Bool
22
- position:: Vector2
23
- renderText
24
- size:: Vector2
25
- text:: String
26
- textTexture
27
10
isConstructed:: Bool
28
- color :: NTuple{4, Int}
11
+ isWorldEntity :: Bool
29
12
maxLineWidth:: Int
13
+ renderText:: Union{Ptr{SDL2.SDL_Surface}, Ptr{Nothing}}
14
+ text:: String
15
+ textTexture:: Union{Ptr{SDL2.SDL_Texture}, Ptr{Nothing}}
30
16
wrapWords:: Bool
31
17
32
- function TextBox (name:: String , fontPath:: String , fontSize:: Int , position:: Math.Vector2 , text:: String , isCenteredX:: Bool = false , isCenteredY:: Bool = false ; anchorOffset:: Math.Vector2 = Math. Vector2 (0 ,0 ), id:: String = JulGame. generate_uuid (), isWorldEntity:: Bool = false , layer:: Int = 0 , color:: NTuple{4, Int} = (255 , 255 , 255 , 255 ), maxLineWidth:: Int = 0 , wrapWords:: Bool = true ) # TODO : replace bool with enum { left, center, right, etc }
33
- this = new ()
18
+ function TextBox (text:: String ;
19
+ id:: String = JulGame. generate_uuid (),
20
+ name:: String = " TextBox" ,
21
+ anchor:: Symbol = :center ,
22
+ anchorOffset:: Math.Vector2 = Math. Vector2 (0 ,0 ),
23
+ isWorldEntity:: Bool = false ,
24
+ layer:: Int = 0 ,
25
+ position:: Math.Vector2 = Math. Vector2 (0 ,0 ),
26
+ clickEvents:: Vector{Function} = Function[],
27
+ hoverEnterEvents:: Vector{Function} = Function[],
28
+ hoverExitEvents:: Vector{Function} = Function[],
29
+ isActive:: Bool = true ,
30
+ persistentBetweenScenes:: Bool = false ,
31
+ color:: NTuple{4, Int} = (255 , 255 , 255 , 255 ),
32
+ fontPath:: String = " FiraCode-Regular.ttf" ,
33
+ fontSize:: Int = 16 ,
34
+ maxLineWidth:: Int = 0 ,
35
+ wrapWords:: Bool = true )
34
36
37
+ this = new ()
38
+
35
39
this. isConstructed = false
36
- this. clickEvents = []
40
+ this. anchor = JulGame. Enum {Any} (
41
+ :center ,
42
+ :top ,
43
+ :bottom ,
44
+ :left ,
45
+ :right ,
46
+ :topLeft ,
47
+ :topRight ,
48
+ :bottomLeft ,
49
+ :bottomRight ,
50
+ )
51
+
52
+ this. anchor. current_state = anchor
53
+ this. anchorOffset = anchorOffset
54
+
55
+ this. clickEvents = clickEvents
56
+ this. hoverEnterEvents = hoverEnterEvents
57
+ this. hoverExitEvents = hoverExitEvents
58
+
37
59
this. fontPath = fontPath
38
60
this. fontSize = fontSize # Store the base font size
39
61
this. id = id
40
- this. anchorOffset = anchorOffset
41
- this. isCenteredX = isCenteredX
42
- this. isCenteredY = isCenteredY
43
62
this. layer = layer
44
63
this. name = name
45
64
this. position = position
46
65
setfield! (this, :text , text)
47
- this. isHovered = false
48
66
this. isWorldEntity = isWorldEntity
49
- this. textTexture = C_NULL
50
- this. persistentBetweenScenes = false
51
- this. isActive = true
52
- this. renderText = C_NULL
67
+ this. persistentBetweenScenes = persistentBetweenScenes
68
+ this. isActive = isActive
53
69
this. color = color
54
70
this. maxLineWidth = maxLineWidth
55
71
this. wrapWords = wrapWords
56
72
73
+ this. textTexture = C_NULL
74
+ this. renderText = C_NULL
75
+
57
76
if strip (fontPath) == " "
58
77
@debug " fontPath is empty, using default font"
59
- fontPath = joinpath ( " FiraCode-Regular.ttf " )
78
+ fontPath = " Default "
60
79
end
61
80
62
81
# Load the font with the true font size (scaled for current window size)
@@ -126,7 +145,8 @@ module TextBoxModule
126
145
@debug string (" loading font from $(basePath) \\ $(fontPath) " )
127
146
128
147
# Calculate the true font size based on window resolution
129
- trueFontSize = get_true_font_size (this. fontSize)
148
+ # trueFontSize = get_true_font_size(this.fontSize)
149
+ trueFontSize = this. fontSize
130
150
131
151
this. font = load_font_sdl (basePath, fontPath, trueFontSize)
132
152
if this. font == C_NULL
@@ -187,8 +207,14 @@ module TextBoxModule
187
207
end
188
208
189
209
function load_font_sdl (basePath:: String , fontPath:: String , fontSize:: Int )
190
- if haskey (JulGame. FONT_CACHE, get_comma_separated_path (fontPath))
191
- raw_data = JulGame. FONT_CACHE[get_comma_separated_path (fontPath)]
210
+ if haskey (JulGame. FONT_CACHE, get_comma_separated_path (fontPath)) || fontPath == " Default"
211
+ if fontPath == " Default"
212
+ raw_data = JulGame. BUILT_IN_ASSETS[" Font" ]
213
+ @debug " loading default font"
214
+ else
215
+ raw_data = JulGame. FONT_CACHE[get_comma_separated_path (fontPath)]
216
+ @debug " loading font from cache"
217
+ end
192
218
rw = SDL2. SDL_RWFromConstMem (pointer (raw_data), length (raw_data))
193
219
if rw != C_NULL
194
220
@debug (" loading font from cache" )
@@ -334,11 +360,26 @@ module TextBoxModule
334
360
return
335
361
end
336
362
337
- if this. isCenteredX
338
- this. position = Math. Vector2 (max (MAIN. scene. camera. size. x/ 2 - this. size. x/ 2 , 0 ) + this. anchorOffset. x, this. position. y)
339
- end
340
- if this. isCenteredY
341
- this. position = Math. Vector2 (this. position. x, max (MAIN. scene. camera. size. y/ 2 - this. size. y/ 2 , 0 ) + this. anchorOffset. y)
363
+ if this. anchor. current_state == :center
364
+ 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)
365
+ elseif this. anchor. current_state == :top
366
+ this. position = Math. Vector2 (max (MAIN. scene. camera. size. x/ 2 - this. size. x/ 2 , 0 ) + this. anchorOffset. x, this. anchorOffset. y)
367
+ elseif this. anchor. current_state == :bottom
368
+ this. position = Math. Vector2 (max (MAIN. scene. camera. size. x/ 2 - this. size. x/ 2 , 0 ) + this. anchorOffset. x, MAIN. scene. camera. size. y - this. size. y + this. anchorOffset. y)
369
+ elseif this. anchor. current_state == :left
370
+ this. position = Math. Vector2 (this. anchorOffset. x, max (MAIN. scene. camera. size. y/ 2 - this. size. y/ 2 , 0 ) + this. anchorOffset. y)
371
+ elseif this. anchor. current_state == :right
372
+ this. position = Math. Vector2 (MAIN. scene. camera. size. x - this. size. x + this. anchorOffset. x, max (MAIN. scene. camera. size. y/ 2 - this. size. y/ 2 , 0 ) + this. anchorOffset. y)
373
+ elseif this. anchor. current_state == :topLeft
374
+ this. position = Math. Vector2 (this. anchorOffset. x, this. anchorOffset. y)
375
+ elseif this. anchor. current_state == :topRight
376
+ this. position = Math. Vector2 (MAIN. scene. camera. size. x - this. size. x + this. anchorOffset. x, this. anchorOffset. y)
377
+ elseif this. anchor. current_state == :bottomLeft
378
+ this. position = Math. Vector2 (this. anchorOffset. x, MAIN. scene. camera. size. y - this. size. y + this. anchorOffset. y)
379
+ elseif this. anchor. current_state == :bottomRight
380
+ this. position = Math. Vector2 (MAIN. scene. camera. size. x - this. size. x + this. anchorOffset. x, MAIN. scene. camera. size. y - this. size. y + this. anchorOffset. y)
381
+ else
382
+ @error " Invalid anchor state: $(this. anchor. current_state) "
342
383
end
343
384
end
344
385
@@ -398,7 +439,7 @@ module TextBoxModule
398
439
SDL2. SDL_DestroyTexture (this. textTexture)
399
440
this. textTexture = C_NULL
400
441
end
401
-
442
+ #=
402
443
function Base.setproperty!(this::TextBox, s::Symbol, x)
403
444
try
404
445
setfield!(this, s, x)
@@ -415,7 +456,7 @@ module TextBoxModule
415
456
error(e)
416
457
Base.show_backtrace(stderr, catch_backtrace())
417
458
end
418
- end
459
+ end =#
419
460
420
461
# Add methods to set and get the maximum line width
421
462
function set_max_line_width (this:: TextBox , maxWidth:: Int )
0 commit comments