Skip to content

Commit af33ee5

Browse files
committed
Add editor export type
1 parent 967884f commit af33ee5

File tree

7 files changed

+78
-24
lines changed

7 files changed

+78
-24
lines changed

src/JulGame.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ module JulGame
1616
const SDL2E = SDL2Extension
1717
export DELTA_TIME, IS_EDITOR, SDL2, SDL2E, MAIN
1818

19+
include("utils/Structs.jl")
20+
export EditorExport
21+
22+
include("utils/Types.jl")
23+
export Script
24+
1925
include("utils/Utils.jl")
2026
export CallSDLFunction
2127

src/editor/JulGameEditor/Components/ComponentInputs.jl

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -573,10 +573,9 @@ function show_script_editor(entity, newScriptText)
573573

574574
CImGui.Button("Delete $(i)") && (deleteat!(entity.scripts, i); return;)
575575
for field in fieldnames(typeof(entity.scripts[i]))
576-
if field == :parent
576+
if field == :parent || !(fieldtype(typeof(entity.scripts[i]), field) <: EditorExport)
577577
continue
578578
end
579-
580579
if isdefined(entity.scripts[i], Symbol(field))
581580
display_script_field_input(entity.scripts[i], field)
582581
else
@@ -592,9 +591,9 @@ function show_script_editor(entity, newScriptText)
592591
end
593592

594593
function display_script_field_input(script, field)
595-
ftype = fieldtype(typeof(script), field)
594+
ftype = typeof(getproperty(script, field))
596595
if ftype == String
597-
buf = "$(getfield(script, field))"*"\0"^(64)
596+
buf = "$(getproperty(script, field))"*"\0"^(64)
598597
CImGui.InputText("$(field)", buf, length(buf))
599598
currentTextInTextBox = ""
600599
for characterIndex = eachindex(buf)
@@ -605,33 +604,33 @@ function display_script_field_input(script, field)
605604
break
606605
end
607606
end
608-
setfield!(script, field, currentTextInTextBox)
607+
setproperty!(script, field, currentTextInTextBox)
609608
elseif ftype == Float64 || ftype == Float32
610-
x = ftype(getfield(script, field))
609+
x = ftype(getproperty(script, field))
611610
x = Cfloat(x)
612611
@c CImGui.InputFloat("$(field)", &x, 1)
613-
setfield!(script, field, ftype(x))
612+
setproperty!(script, field, ftype(x))
614613
elseif ftype <: Int64 || ftype <: Int32 || ftype <: Int16 || ftype <: Int8
615-
x = ftype(getfield(script, field))
614+
x = ftype(getproperty(script, field))
616615
x = convert(Int32, x)
617616
@c CImGui.InputInt("$(field)", &x, 1)
618617
x = convert(ftype, x)
619-
setfield!(script, field, x)
618+
setproperty!(script, field, x)
620619
elseif ftype == Bool
621-
x = getfield(script, field)
620+
x = getproperty(script, field)
622621
@c CImGui.Checkbox("$(field)", &x)
623-
setfield!(script, field, x)
622+
setproperty!(script, field, x)
624623
end
625624
end
626625

627626
function init_undefined_field(script, field)
628-
ftype = fieldtype(typeof(script), field)
627+
ftype = typeof(getproperty(script, field))
629628
if ftype == String
630-
setfield!(script, field, "")
629+
setproperty!(script, field, "")
631630
elseif ftype <: Number
632-
setfield!(script, field, 0)
631+
setproperty!(script, field, 0)
633632
elseif ftype == Bool
634-
setfield!(script, field, false)
633+
setproperty!(script, field, false)
635634
end
636635
end
637636

src/editor/JulGameEditor/Utils/FileContents.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ end
7777
function newScriptContent(scriptName)
7878
return "module $(scriptName)Module
7979
using ..JulGame
80-
mutable struct $scriptName
81-
parent # do not remove this line, this is a reference to the entity that this script is attached to
82-
# This is where you define your script's fields
83-
# Example: speed::Float64
80+
mutable struct $(scriptName) <: Script
81+
parent # do not remove this line, this is a reference to the entity that this script is attached to
82+
# This is where you define your script's fields
83+
# Example: speed::Float64
8484
85-
function $scriptName()
85+
function $(scriptName)()
8686
this = new() # do not remove this line
8787
8888
# this is where you initialize your script's fields

src/engine/SceneManagement/SceneBuilder.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ module SceneBuilderModule
243243
continue
244244
end
245245
@debug "Overwriting $(key) to $(value) using scene file"
246-
Base.invokelatest(setfield!, newScript, key, value)
246+
Base.invokelatest(setfield!, newScript, key, EditorExport(value))
247247
catch e
248248
@warn string(e)
249249
end

src/engine/SceneManagement/SceneWriter.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module SceneWriterModule
2+
using ...JulGame
23
using JSON3
34

45
export serialize_entities
@@ -241,11 +242,12 @@ module SceneWriterModule
241242
val = nothing
242243
if isdefined(script, Symbol(field))
243244
val = getfield(script, field)
244-
if typeof(val) <: AbstractArray
245-
val = nothing
245+
if !isa(val, EditorExport)
246+
continue
246247
end
248+
val = val.value
247249
else
248-
val = set_undefined_field(script, field)
250+
continue
249251
end
250252
fields["$(field)"] = val
251253
end

src/utils/Structs.jl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
mutable struct EditorExport{T}
2+
value::T
3+
4+
# Inner constructor
5+
function EditorExport(value::T) where T
6+
return new{T}(value) # Use `new` to construct the object
7+
end
8+
end
9+
10+
# Overload `getproperty` to access `value` transparently
11+
function Base.getproperty(editor::EditorExport{T}, sym::Symbol) where T
12+
if sym === :value
13+
return getfield(editor, :value) # Preserve direct access to `value`
14+
else
15+
return editor.value # Redirect other accesses to `value`
16+
end
17+
end
18+
19+
# Overload `setproperty!` to modify `value` transparently
20+
function Base.setproperty!(editor::EditorExport{T}, sym::Symbol, new_value) where T
21+
if sym === :value
22+
setfield!(editor, :value, new_value) # Directly update `value`
23+
else
24+
editor.value = new_value # Redirect updates to `value`
25+
end
26+
end

src/utils/Types.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
abstract type Script end
2+
3+
function Base.getproperty(script::Script, property::Symbol)
4+
if isa(getfield(script, property), EditorExport)
5+
return getfield(script, property).value
6+
end
7+
8+
return getfield(script, property)
9+
end
10+
11+
function Base.setproperty!(script::Script, property::Symbol, value)
12+
field = findfirst(f->f==property, fieldnames(typeof(script)))
13+
14+
if fieldtype(typeof(script), field) <: EditorExport
15+
setfield!(script, property, EditorExport(value))
16+
else
17+
setfield!(script, property, value)
18+
end
19+
end
20+
21+
# TODO: Add a way to add custom fields to scripts

0 commit comments

Comments
 (0)