Skip to content

Commit 0148859

Browse files
committed
Feature: Store scene name and allow textboxes to process input events
Need to refactor to cleanup shared functionality between ui elements before adding more
1 parent 59965b0 commit 0148859

File tree

8 files changed

+33
-19
lines changed

8 files changed

+33
-19
lines changed

src/Main.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@ function JulGame.change_scene(sceneFileName::String)
373373
#load new scene
374374
camera = this.scene.camera
375375
this.scene = SceneModule.Scene()
376+
this.scene.name = split(sceneFileName, ".")[1]
376377
this.scene.entities = persistentEntities
377378
this.scene.uiElements = persistentUIElements
378379
this.scene.camera = camera

src/editor/JulGameEditor/Components/ScreenButtonFields.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
function show_screenbutton_fields(selectedScreenButton, screenButtonField)
22
fieldName = getFieldName1(screenButtonField)
3-
unusedFields = ["alpha","clickEvents", "currentTexture", "buttonDownSprite", "buttonDownSpritePath", "buttonDownTexture", "buttonUpSprite", "buttonUpSpritePath", "buttonUpTexture", "fontPath", "isInitialized", "mouseOverSprite", "textTexture"]
3+
unusedFields = ["alpha","clickEvents", "currentTexture", "buttonDownSprite", "buttonDownSpritePath", "buttonDownTexture", "buttonUpSprite", "buttonUpSpritePath", "buttonUpTexture", "fontPath", "isInitialized", "textTexture"]
44
push!(unusedFields, "text")
55
if fieldName in unusedFields
66
return

src/engine/Input/Input.jl

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -116,32 +116,30 @@ module InputModule
116116
end
117117

118118
insideAnyButton = false
119-
for screenButton in MAIN.scene.uiElements
120-
if split("$(typeof(screenButton))", ".")[end] != "ScreenButton"
121-
continue
122-
end
119+
for uiElement in MAIN.scene.uiElements
123120
# Check position of button to see which we are interacting with
124121
eventWasInsideThisButton = true
125-
if x[1] < screenButton.position.x + MAIN.scene.camera.startingCoordinates.x
122+
if x[1] < uiElement.position.x + MAIN.scene.camera.startingCoordinates.x
126123
eventWasInsideThisButton = false
127-
elseif x[1] > MAIN.scene.camera.startingCoordinates.x + screenButton.position.x + screenButton.size.x * MAIN.zoom
124+
elseif x[1] > MAIN.scene.camera.startingCoordinates.x + uiElement.position.x + uiElement.size.x * MAIN.zoom
128125
eventWasInsideThisButton = false
129-
elseif y[1] < screenButton.position.y + MAIN.scene.camera.startingCoordinates.y
126+
elseif y[1] < uiElement.position.y + MAIN.scene.camera.startingCoordinates.y
130127
eventWasInsideThisButton = false
131-
elseif y[1] > MAIN.scene.camera.startingCoordinates.y + screenButton.position.y + screenButton.size.y * MAIN.zoom
128+
elseif y[1] > MAIN.scene.camera.startingCoordinates.y + uiElement.position.y + uiElement.size.y * MAIN.zoom
132129
eventWasInsideThisButton = false
133130
end
134131

135-
screenButton.mouseOverSprite = eventWasInsideThisButton
136132
if !eventWasInsideThisButton
137-
screenButton.isHovered = false
133+
uiElement.isHovered = false
138134
continue
139135
end
140136
insideAnyButton = true
141137

142-
SDL2.SDL_SetCursor(this.cursorBank["hand"])
138+
if split("$(typeof(uiElement))", ".")[end] == "ScreenButton"
139+
SDL2.SDL_SetCursor(this.cursorBank["crosshair"])
140+
end
143141

144-
JulGame.UI.handle_event(screenButton, evt, x[1], y[1])
142+
JulGame.UI.handle_event(uiElement, evt, x[1], y[1])
145143
end
146144

147145
if !insideAnyButton

src/engine/Scene.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
entities::Vector{Any}
99
rigidbodies::Vector{Any}
1010
uiElements::Vector{Any}
11+
name::String
1112

1213
function Scene()
1314
this = new()

src/engine/SceneManagement/SceneBuilder.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ module SceneBuilderModule
115115
MAIN.globals = globals
116116
MAIN.level = this
117117
MAIN.targetFrameRate = targetFrameRate
118+
MAIN.scene.name = split(this.scene, ".")[1]
118119

119120
if size == Math.Vector2()
120121
displayMode = SDL2.SDL_DisplayMode[SDL2.SDL_DisplayMode(0x12345678, 800, 600, 60, C_NULL)]

src/engine/UI/DefaultFont.jl

Whitespace-only changes.

src/engine/UI/ScreenButton.jl

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ module ScreenButtonModule
1919
isActive::Bool
2020
isHovered::Bool
2121
isInitialized::Bool
22-
mouseOverSprite
2322
name::String
2423
persistentBetweenScenes::Bool
2524
position::Math.Vector2
@@ -40,7 +39,6 @@ module ScreenButtonModule
4039
this.currentTexture = C_NULL
4140
this.size = size
4241
this.fontPath = fontPath
43-
this.mouseOverSprite = false
4442
this.name = name
4543
this.position = position
4644
this.text = text
@@ -64,9 +62,6 @@ module ScreenButtonModule
6462
return
6563
end
6664

67-
if !this.mouseOverSprite && this.currentTexture == this.buttonDownTexture
68-
#TODO: this.currentTexture = this.buttonUpTexture
69-
end
7065
if this.currentTexture == C_NULL ||
7166
#this.textTexture == C_NULL ||
7267
this.currentTexture === nothing #|| this.textTexture === nothing

src/engine/UI/TextBox.jl

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ module TextBoxModule
55
export TextBox
66
mutable struct TextBox
77
alpha
8+
clickEvents
89
font
910
fontPath::String
1011
fontSize::Int32
1112
id::Int32
1213
isActive::Bool
1314
isCenteredX::Bool
1415
isCenteredY::Bool
16+
isHovered::Bool
1517
isWorldEntity::Bool
1618
name::String
1719
persistentBetweenScenes::Bool
@@ -27,6 +29,7 @@ module TextBoxModule
2729

2830
this.isConstructed = false
2931
this.alpha = 255
32+
this.clickEvents = []
3033
this.fontPath = fontPath
3134
this.fontSize = Int32(fontSize)
3235
this.id = Int32(0)
@@ -35,12 +38,12 @@ module TextBoxModule
3538
this.name = name
3639
this.position = position
3740
setfield!(this, :text, text)
41+
this.isHovered = false
3842
this.isWorldEntity = isWorldEntity
3943
this.textTexture = C_NULL
4044
this.persistentBetweenScenes = false
4145
this.isActive = true
4246
this.renderText = C_NULL
43-
4447

4548
if fontPath == ""
4649
fontPath = joinpath("FiraCode-Regular.ttf")
@@ -100,6 +103,21 @@ module TextBoxModule
100103
end
101104
end
102105

106+
function UI.add_click_event(this::TextBox, event)
107+
push!(this.clickEvents, event)
108+
end
109+
110+
function UI.handle_event(this::TextBox, evt, x, y)
111+
if evt.type == evt.type == SDL2.SDL_MOUSEBUTTONDOWN
112+
elseif evt.type == SDL2.SDL_MOUSEBUTTONUP
113+
for eventToCall in this.clickEvents
114+
Base.invokelatest(eventToCall,(evt = evt, x = x, y = y))
115+
end
116+
elseif evt.type == SDL2.SDL_MOUSEMOTION
117+
this.isHovered = true
118+
end
119+
end
120+
103121
function load_font_sdl(basePath::String, fontPath::String, fontSize::Int32)
104122
if haskey(JulGame.FONT_CACHE, get_comma_separated_path(fontPath))
105123
raw_data = JulGame.FONT_CACHE[get_comma_separated_path(fontPath)]

0 commit comments

Comments
 (0)