Gnimuc / CImGui.jl

Julia wrapper for cimgui
https://github.com/cimgui/cimgui
MIT License
259 stars 25 forks source link

hotloading... does it work? can it be documented? #36

Closed claforte closed 3 years ago

claforte commented 3 years ago

Hi Gnimuc, thanks so much for this great package!

I noticed in examples/Renderer.jl that render() takes a hotloading parameter. That sounds super useful, if it means we can just save a function and instantly the UI updated accordingly. Does this fully work? If so can you explain how? I passing a global function as an argument but it didn't do anything. BTW I have the revise Pkg installed. Here's the code I tested:

module ImageCompare
    using CImGui

    #@run include(joinpath(pathof(CImGui), "..", "..", "demo", "demo.jl"))
    #@run include(joinpath(pathof(CImGui), "..", "..", "examples", "demo.jl"))
    include(joinpath(pathof(CImGui), "..", "..", "examples", "Renderer.jl"))
    using .Renderer

    function ui()
        CImGui.Begin("Hello ImGui AGAIN!")
        CImGui.Button("My Button") && @show "triggered"
        CImGui.End()
        CImGui.ShowMetricsWindow()
    end

    function test()
        # Default: without hotloading

        # Renderer.render(title = "IMGUI Window") do
        #     CImGui.Begin("Hello ImGui")
        #     CImGui.Button("My Button") && @show "triggered"
        #     CImGui.End()
        #     CImGui.ShowMetricsWindow()
        # end

        Renderer.render(ui, title = "IMGUI Window", hotloading=true)
    end
    export test
end

Here are the steps I tried:

  1. from the REPL, run using ImageCompare then test()
  2. modify the code, e.g. replace Hello ImGui AGAIN by something else. Save the file.
  3. I also tried re-doing using ImageCompare in case that mattered. I don't notice any changes to the ImGui window.
Gnimuc commented 3 years ago

Is ImageCompare in the module load path? If so, simply running revise() will trigger the update. If not, you need to use includet instead of include to load the script.

➜  ~ cat ImageCompare.jl 
module ImageCompare
    using CImGui

    #@run include(joinpath(pathof(CImGui), "..", "..", "demo", "demo.jl"))
    #@run include(joinpath(pathof(CImGui), "..", "..", "examples", "demo.jl"))
    include(joinpath(pathof(CImGui), "..", "..", "examples", "Renderer.jl"))
    using .Renderer

    function ui()
        CImGui.Begin("Hello ImGui!")
        CImGui.Button("My Button") && @show "triggered"
        CImGui.End()
        CImGui.ShowMetricsWindow()
    end

    function test()
        # Default: without hotloading

        # Renderer.render(title = "IMGUI Window") do
        #     CImGui.Begin("Hello ImGui")
        #     CImGui.Button("My Button") && @show "triggered"
        #     CImGui.End()
        #     CImGui.ShowMetricsWindow()
        # end

        Renderer.render(ui, title = "IMGUI Window", hotloading=true)
    end
    export test
end
➜  ~ julia
               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.5.3 (2020-11-09)
 _/ |\__'_|_|_|\__'_|  |  Official https://julialang.org/ release
|__/                   |

julia> includet("ImageCompare.jl")
WARNING: replacing module Renderer.

julia> using .ImageCompare

julia> test()
Task (runnable) @0x00000001139918d0

shell> vim ImageCompare.jl

julia> revise()

shell> cat ImageCompare.jl
module ImageCompare
    using CImGui

    #@run include(joinpath(pathof(CImGui), "..", "..", "demo", "demo.jl"))
    #@run include(joinpath(pathof(CImGui), "..", "..", "examples", "demo.jl"))
    include(joinpath(pathof(CImGui), "..", "..", "examples", "Renderer.jl"))
    using .Renderer

    function ui()
        CImGui.Begin("Hello ImGui again!")
        CImGui.Button("My Button") && @show "triggered"
        CImGui.End()
        CImGui.ShowMetricsWindow()
    end

    function test()
        # Default: without hotloading

        # Renderer.render(title = "IMGUI Window") do
        #     CImGui.Begin("Hello ImGui")
        #     CImGui.Button("My Button") && @show "triggered"
        #     CImGui.End()
        #     CImGui.ShowMetricsWindow()
        # end

        Renderer.render(ui, title = "IMGUI Window", hotloading=true)
    end
    export test
end
Gnimuc commented 3 years ago

FWIW, here is an example of how to hook a Julia function to a CImGui widget by using Redux.jl.

ReduxImGui.jl is an experimental project for doing state-management with CImGui, you can take it as an example of how to use Redux.jl, but it's still WIP and may contain design flaws, so I wouldn't recommend to use it in practice.

claforte commented 3 years ago

Thanks Gnimuc! I'm still not clear on how hotloading is supposed to work, but I'll keep watching your project and try it out when I have some more time. :-)

BTW I did a ] add Revise so IIUC I don't need to includet, simply using ImageCompare should do the trick.

Gnimuc commented 3 years ago

I'm still not clear on how hotloading is supposed to work,

I'm pretty sure all the magic happened on the Revise.jl side. It is probably because those UI functions are running in an async mode that the explicit use of Revise.revise() is needed.

BTW I did a ] add Revise so IIUC I don't need to includet, simply using ImageCompare should do the trick.

Yes, there is no need to use includet for packages. The result I posted above is for doing a quick test in the REPL, that's why I need includet and using .ImageCompare.

claforte commented 3 years ago

Hi Gnumic, I finally took time this week-end to revisit Julia and try again to get this working. I got many of the examples working. Thanks a lot for all your work! I also greatly learned from your post: https://github.com/Gnimuc/CImGui.jl/discussions/39 and was planning to close this issue since that post resolves it, but...

The code in your post was working fine on Julia 1.5.2 (IRCC) but later today I upgraded to Julia 1.6.0 and since then, the final code doesn't seem to work - it stops right away, silently. It looks like infinite_loop only gets called once, and ui no longer gets called. Can you check if this happens for you as well? Maybe there was a breaking change in Julia? Just in case I re-cut-and-pasted your final code, to no avail.

Thank you!

Gnimuc commented 3 years ago

@claforte, sorry for the late response. I missed the GitHub notification. The following code works fine on my machine in a fresh REPL.

using CImGui
include(joinpath(pathof(CImGui), "..", "..", "examples", "Renderer.jl"))
using .Renderer

mutable struct MyStates
    arr::Vector{Cfloat}
    is_stop::Bool
end

# nothing special, just a simple initialization 
state_holder = MyStates(Cfloat[sin(x) for x in 0:0.01:2pi], false) 

# this method is left intact
update!(arr) = foreach(1:50:600) do i
    arr[i] = sin(time()+i)
end
# we only need to add a helper method for multiple dispatch
update!(state::MyStates) = update!(state.arr)

# this is the UI function, whenever the structure of `MyStates` is changed, 
# the corresponding changes should be applied
function ui(state::MyStates)
    CImGui.Begin("Hello ImGui")
        CImGui.PlotLines("sine wave", state.arr, length(state.arr))
    CImGui.End()
end

# automatically set the flag when the function gets invoked
function infinite_loop(state::MyStates)
    state.is_stop = false
    @async while true
        state.is_stop && break
        update!(state)
        yield()
    end
end

Renderer.render(()->ui(state_holder), width = 360, height = 480, title = "A simple UI")
infinite_loop(state_holder)
Gnimuc commented 3 years ago

BTW, I was using the master branch, not the release.

claforte commented 3 years ago

Thanks Gnimuc! This one works for me on Julia 1.6.0. I'll compare with what I had and I should be able to fix my problem. Thanks a lot!

Gnimuc commented 3 years ago

I guess you probably started two imgui contexts(two UI windows) at the same time(this is actually very likely if you follow the tutorial from the start). ImGui works best with one single context, so multi-context triggered undefined behavior which in turn caused the crash.

claforte commented 3 years ago

OK! Yeah, I just restarted the Julia REPL before running your example, that's a likely explanation.