Giter Club home page Giter Club logo

Comments (10)

Gnimuc avatar Gnimuc commented on July 4, 2024

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

from cimgui.jl.

Gnimuc avatar Gnimuc commented on July 4, 2024

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.

from cimgui.jl.

claforte avatar claforte commented on July 4, 2024

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.

from cimgui.jl.

Gnimuc avatar Gnimuc commented on July 4, 2024

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.

from cimgui.jl.

claforte avatar claforte commented on July 4, 2024

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: #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!

from cimgui.jl.

Gnimuc avatar Gnimuc commented on July 4, 2024

@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)

from cimgui.jl.

Gnimuc avatar Gnimuc commented on July 4, 2024

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

from cimgui.jl.

claforte avatar claforte commented on July 4, 2024

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!

from cimgui.jl.

Gnimuc avatar Gnimuc commented on July 4, 2024

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.

from cimgui.jl.

claforte avatar claforte commented on July 4, 2024

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

from cimgui.jl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.