Skip to content

Commit

Permalink
cleanup and starting to redo script display and editing
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyjor committed Sep 21, 2024
1 parent f243705 commit 0816ea0
Show file tree
Hide file tree
Showing 9 changed files with 560 additions and 485 deletions.
7 changes: 6 additions & 1 deletion src/Main.jl
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,12 @@ function initialize_scripts_and_components()
for script in scripts
try
# TODO: only call latest if in editor and in game mode
Base.invokelatest(JulGame.initialize, script)
if JulGame.IS_EDITOR
println(typeof(script))
Base.invokelatest(JulGame.initialize, script)

Check warning on line 248 in src/Main.jl

View check run for this annotation

Codecov / codecov/patch

src/Main.jl#L247-L248

Added lines #L247 - L248 were not covered by tests
else
JulGame.initialize(script)
end
catch e
#if typeof(e) != ErrorException || !contains(e.msg, "initialize")
@error string(e)
Expand Down
104 changes: 79 additions & 25 deletions src/editor/JulGameEditor/Components/ComponentInputs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -540,45 +540,99 @@ function show_script_editor(entity, newScriptText)

script = display_files(joinpath(JulGame.BasePath, "scripts"), "scripts", "Add Script")
if script != ""

Check warning on line 542 in src/editor/JulGameEditor/Components/ComponentInputs.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/ComponentInputs.jl#L541-L542

Added lines #L541 - L542 were not covered by tests
push!(entity.scripts, scriptObj(script, []))
#push!(entity.scripts, scriptObj(script, []))
end

for i = eachindex(entity.scripts)
if CImGui.TreeNode("Script $(i)")
if CImGui.TreeNode("$(i): $(split("$(typeof(entity.scripts[i]))", ".")[end])")

Check warning on line 547 in src/editor/JulGameEditor/Components/ComponentInputs.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/ComponentInputs.jl#L547

Added line #L547 was not covered by tests
CImGui.Button("Delete $(i)") && (deleteat!(entity.scripts, i); return;)
CImGui.Text(entity.scripts[i].name)

entity.scripts[i] = scriptObj(entity.scripts[i].name, entity.scripts[i].parameters)
if CImGui.TreeNode("Script $(i) parameters")
params = entity.scripts[i].parameters
CImGui.Button("Add New Script Parameter") && (push!(params, ""); entity.scripts[i] = scriptObj(entity.scripts[i].name, params); break;)

for j = eachindex(entity.scripts[i].parameters)
buf = "$(entity.scripts[i].parameters[j])"*"\0"^(64)
CImGui.Button("Delete $(j)") && (deleteat!(params, j); entity.scripts[i] = scriptObj(currentTextInTextBox, params); break;)
CImGui.InputText("Parameter $(j)", buf, length(buf))
currentTextInTextBox = ""
for characterIndex = eachindex(buf)
if Int32(buf[characterIndex]) == 0
if characterIndex != 1
currentTextInTextBox = String(SubString(buf, 1, characterIndex-1))
end
break
end
end
params[j] = currentTextInTextBox
entity.scripts[i] = scriptObj(entity.scripts[i].name, params)
for field in fieldnames(typeof(entity.scripts[i]))
if field == :parent
continue

Check warning on line 551 in src/editor/JulGameEditor/Components/ComponentInputs.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/ComponentInputs.jl#L549-L551

Added lines #L549 - L551 were not covered by tests
end

if isdefined(entity.scripts[i], Symbol(field))
display_script_field_input(entity.scripts[i], field)
CImGui.Text("$(field): $(getfield(entity.scripts[i], field))")

Check warning on line 556 in src/editor/JulGameEditor/Components/ComponentInputs.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/ComponentInputs.jl#L554-L556

Added lines #L554 - L556 were not covered by tests
else
init_undefined_field(entity.scripts[i], field)

Check warning on line 558 in src/editor/JulGameEditor/Components/ComponentInputs.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/ComponentInputs.jl#L558

Added line #L558 was not covered by tests
end
CImGui.TreePop()
end
# CImGui.Text(entity.scripts[i].name)

# entity.scripts[i] = scriptObj(entity.scripts[i].name, entity.scripts[i].parameters)
# if CImGui.TreeNode("Script $(i) parameters")
# params = entity.scripts[i].parameters
# CImGui.Button("Add New Script Parameter") && (push!(params, ""); entity.scripts[i] = scriptObj(entity.scripts[i].name, params); break;)

# for j = eachindex(entity.scripts[i].parameters)
# buf = "$(entity.scripts[i].parameters[j])"*"\0"^(64)
# CImGui.Button("Delete $(j)") && (deleteat!(params, j); entity.scripts[i] = scriptObj(currentTextInTextBox, params); break;)
# CImGui.InputText("Parameter $(j)", buf, length(buf))
# currentTextInTextBox = ""
# for characterIndex = eachindex(buf)
# if Int32(buf[characterIndex]) == 0
# if characterIndex != 1
# currentTextInTextBox = String(SubString(buf, 1, characterIndex-1))
# end
# break
# end
# end
# params[j] = currentTextInTextBox
# entity.scripts[i] = scriptObj(entity.scripts[i].name, params)

# end
# CImGui.TreePop()
# end
CImGui.TreePop()
end
end
CImGui.TreePop()
end
end

function display_script_field_input(script, field)
ftype = fieldtype(typeof(script), field)
if ftype == String
buf = "$(getfield(script, field))"*"\0"^(64)
CImGui.InputText("$(field)", buf, length(buf))
currentTextInTextBox = ""
for characterIndex = eachindex(buf)
if Int32(buf[characterIndex]) == 0
if characterIndex != 1
currentTextInTextBox = String(SubString(buf, 1, characterIndex-1))

Check warning on line 603 in src/editor/JulGameEditor/Components/ComponentInputs.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/ComponentInputs.jl#L594-L603

Added lines #L594 - L603 were not covered by tests
end
break

Check warning on line 605 in src/editor/JulGameEditor/Components/ComponentInputs.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/ComponentInputs.jl#L605

Added line #L605 was not covered by tests
end
end
setfield!(script, field, currentTextInTextBox)
elseif ftype == Float64 || ftype == Float32
x = ftype(getfield(script, field))
x = Cfloat(x)
@c CImGui.InputFloat("$(field)", &x, 1)
setfield!(script, field, ftype(x))
elseif ftype <: Int
x = ftype(getfield(script, field))
@c CImGui.InputInt("$(field)", &x, 1)
setfield!(script, field, x)
elseif ftype == Bool
x = getfield(script, field)
@c CImGui.Checkbox("$(field)", &x)
setfield!(script, field, x)

Check warning on line 621 in src/editor/JulGameEditor/Components/ComponentInputs.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/ComponentInputs.jl#L607-L621

Added lines #L607 - L621 were not covered by tests
end
end

function init_undefined_field(script, field)
ftype = fieldtype(typeof(script), field)
if ftype == String
setfield!(script, field, "")
elseif ftype <: Number
setfield!(script, field, 0)
elseif ftype == Bool
setfield!(script, field, false)

Check warning on line 632 in src/editor/JulGameEditor/Components/ComponentInputs.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/ComponentInputs.jl#L625-L632

Added lines #L625 - L632 were not covered by tests
end
end

function scriptObj(name::String, parameters::Array)
() -> (name; parameters)
end
2 changes: 1 addition & 1 deletion src/editor/JulGameEditor/Utils/EditorUtils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ function log_exceptions(error_type, latest_exceptions, e, top_backtrace, is_test
Base.show_backtrace(stderr, catch_backtrace())
push!(latest_exceptions[], [e, String("$(Dates.now())"), top_backtrace])
if length(latest_exceptions[]) > 10
deleteat!(latest_exceptions, 1)
deleteat!(latest_exceptions[], 1)

Check warning on line 356 in src/editor/JulGameEditor/Utils/EditorUtils.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Utils/EditorUtils.jl#L354-L356

Added lines #L354 - L356 were not covered by tests
end
if is_test_mode
@warn "Error in renderloop!" exception=e
Expand Down
2 changes: 1 addition & 1 deletion src/engine/Entity.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ module EntityModule
for script in this.scripts
try
# TODO: only call latest if in editor and in game mode
Base.invokelatest(JulGame.update, script, deltaTime)
Base.invokelatest(JulGame.update, script, deltaTime)
catch e
@error string(e)
Base.show_backtrace(stdout, catch_backtrace())
Expand Down
126 changes: 59 additions & 67 deletions src/engine/SceneManagement/SceneBuilder.jl
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ module SceneBuilderModule
end
end

if JulGame.IS_EDITOR
add_scripts_to_entities(joinpath(BasePath))

Check warning on line 173 in src/engine/SceneManagement/SceneBuilder.jl

View check run for this annotation

Codecov / codecov/patch

src/engine/SceneManagement/SceneBuilder.jl#L173

Added line #L173 was not covered by tests
end

MAIN.assets = joinpath(BasePath, "assets")
JulGame.MainLoop.prepare_window_scripts_and_start_loop(size, isResizable, autoScaleZoom)
end
Expand Down Expand Up @@ -243,6 +247,10 @@ module SceneBuilderModule
end
end
end

if JulGame.IS_EDITOR
add_scripts_to_entities(joinpath(BasePath))

Check warning on line 252 in src/engine/SceneManagement/SceneBuilder.jl

View check run for this annotation

Codecov / codecov/patch

src/engine/SceneManagement/SceneBuilder.jl#L251-L252

Added lines #L251 - L252 were not covered by tests
end
end

"""
Expand Down Expand Up @@ -295,7 +303,7 @@ module SceneBuilderModule
end
push!(params, param)
end
newScript = C_NULL
newScript = nothing
try

Check warning on line 307 in src/engine/SceneManagement/SceneBuilder.jl

View check run for this annotation

Codecov / codecov/patch

src/engine/SceneManagement/SceneBuilder.jl#L304-L307

Added lines #L304 - L307 were not covered by tests
# TODO: only call latest if in editor and in game mode
newScript = Base.invokelatest(eval, Symbol(script.name))
Expand All @@ -305,7 +313,7 @@ module SceneBuilderModule
Base.show_backtrace(stdout, catch_backtrace())
rethrow(e)

Check warning on line 314 in src/engine/SceneManagement/SceneBuilder.jl

View check run for this annotation

Codecov / codecov/patch

src/engine/SceneManagement/SceneBuilder.jl#L312-L314

Added lines #L312 - L314 were not covered by tests
end
if newScript != C_NULL
if newScript != C_NULL && newScript !== nothing
entity.scripts[scriptCounter] = newScript
newScript.parent = entity

Check warning on line 318 in src/engine/SceneManagement/SceneBuilder.jl

View check run for this annotation

Codecov / codecov/patch

src/engine/SceneManagement/SceneBuilder.jl#L316-L318

Added lines #L316 - L318 were not covered by tests
end
Expand All @@ -315,79 +323,63 @@ module SceneBuilderModule
end

# Define default configuration values
const DEFAULT_CONFIG = Dict(
"WindowName" => "Default Game",
"Width" => "800",
"Height" => "600",
"CameraWidth" => "800",
"CameraHeight" => "600",
"IsResizable" => "0",
"Zoom" => "1.0",
"AutoScaleZoom" => "0",
"FrameRate" => "30"
)

# Function to read and parse the config file
function parse_config()
filename = joinpath(JulGame.BasePath, "config.julgame")
config = copy(DEFAULT_CONFIG)

if isfile(filename)
# Open the file for reading
open(filename, "r") do file
for line in eachline(file)
# Split the line at the '=' character
parts = split(line, "=", limit=2)
if length(parts) == 2
key, value = parts[1], parts[2]
# Strip any extra whitespace and add to dictionary
config[strip(key)] = strip(value)
const DEFAULT_CONFIG = Dict(
"WindowName" => "Default Game",
"Width" => "800",
"Height" => "600",
"CameraWidth" => "800",
"CameraHeight" => "600",
"IsResizable" => "0",
"Zoom" => "1.0",
"AutoScaleZoom" => "0",
"FrameRate" => "30"
)

# Function to read and parse the config file
function parse_config()
filename = joinpath(JulGame.BasePath, "config.julgame")
config = copy(DEFAULT_CONFIG)

if isfile(filename)
# Open the file for reading
open(filename, "r") do file
for line in eachline(file)
# Split the line at the '=' character
parts = split(line, "=", limit=2)
if length(parts) == 2
key, value = parts[1], parts[2]
# Strip any extra whitespace and add to dictionary
config[strip(key)] = strip(value)
end
end
end
else
write_config(filename, config)

Check warning on line 357 in src/engine/SceneManagement/SceneBuilder.jl

View check run for this annotation

Codecov / codecov/patch

src/engine/SceneManagement/SceneBuilder.jl#L357

Added line #L357 was not covered by tests
end
else
write_config(filename, config)


return config
end


return config
end

function fill_in_config(config)
for (key, value) in DEFAULT_CONFIG
if !haskey(config, key)
config[key] = value
function fill_in_config(config)
for (key, value) in DEFAULT_CONFIG
if !haskey(config, key)
config[key] = value

Check warning on line 367 in src/engine/SceneManagement/SceneBuilder.jl

View check run for this annotation

Codecov / codecov/patch

src/engine/SceneManagement/SceneBuilder.jl#L367

Added line #L367 was not covered by tests
end
end
end

return config
end
return config
end

# Function to write values to the config file
function write_config(filename::String, config::Dict{String, String})
# Open the file for writing
open(filename, "w") do file
for (key, value) in config
# Write each key-value pair to the file
println(file, "$key=$value")
# Function to write values to the config file
function write_config(filename::String, config::Dict{String, String})

Check warning on line 375 in src/engine/SceneManagement/SceneBuilder.jl

View check run for this annotation

Codecov / codecov/patch

src/engine/SceneManagement/SceneBuilder.jl#L375

Added line #L375 was not covered by tests
# Open the file for writing
open(filename, "w") do file
for (key, value) in config

Check warning on line 378 in src/engine/SceneManagement/SceneBuilder.jl

View check run for this annotation

Codecov / codecov/patch

src/engine/SceneManagement/SceneBuilder.jl#L377-L378

Added lines #L377 - L378 were not covered by tests
# Write each key-value pair to the file
println(file, "$key=$value")
end

Check warning on line 381 in src/engine/SceneManagement/SceneBuilder.jl

View check run for this annotation

Codecov / codecov/patch

src/engine/SceneManagement/SceneBuilder.jl#L380-L381

Added lines #L380 - L381 were not covered by tests
end
end
end

# # Example usage
# config_file = "config.txt"

# # Read the configuration
# config = parse_config(config_file)
# println("Parsed Configuration:")
# println(config)

# # Modify some values (example)
# config["Width"] = "800"
# config["Height"] = "600"

# # Write the updated configuration back to the file
# write_config(config_file, config)
# println("Updated Configuration Written to File.")
end # module

end
Loading

0 comments on commit 0816ea0

Please sign in to comment.