Skip to content

Instantly share code, notes, and snippets.

@sairus7
Created August 24, 2022 17:59
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sairus7/c87a7dc82af4859dd0c2f8dc58d361c0 to your computer and use it in GitHub Desktop.
Save sairus7/c87a7dc82af4859dd0c2f8dc58d361c0 to your computer and use it in GitHub Desktop.
Julia MWE using CImGui.jl and ImPlot.jl
using Pkg
Pkg.add(name="CImGui", rev="master")
Pkg.add(name="ImPlot", rev="main")
using CImGui
using ImPlot
include(joinpath(pathof(ImPlot), "..", "..", "demo", "Renderer.jl"))
using .Renderer
using CImGui.CSyntax
const USERDATA = Dict{String, Any}(
"xy1" => (zeros(Float32, 1001), zeros(Float32, 1001)),
"xy2" => (zeros(Float32, 11), zeros(Float32, 11)),
)
const STORAGE = Dict{UInt32, Any}()
get_uistate(key::String, default = nothing) = get(STORAGE, CImGui.GetID(key), default)
set_uistate(key::String, value) = STORAGE[CImGui.GetID(key)] = value
function test_buttons()
for i = 1:3
n = get_uistate("counter_$i", 0)
if CImGui.Button("Clicked $n times###$i")
n += 1
set_uistate("counter_$i", n)
#@show n
end
end
if CImGui.Button("Reset")
for i = 1:3
set_uistate("counter_$i", 0)
end
end
end
function ui()
CImGui.Begin("Window")
xs1, ys1 = USERDATA["xy1"]
xs2, ys2 = USERDATA["xy2"]
check = get_uistate("time_checkbox", false)
if @c CImGui.Checkbox("Click me to animate plot", &check)
set_uistate("time_checkbox", check)
end
if check
DEMO_TIME = CImGui.GetTime()
for i in eachindex(xs1)
xs1[i] = (i - 1) * 0.001
ys1[i] = 0.5 + 0.5 * sin(50 * (xs1[i] + DEMO_TIME / 10))
end
for i in eachindex(xs2)
xs2[i] = (i - 1) * 0.1
ys2[i] = xs2[i] * xs2[i]
end
end
CImGui.BulletText("Anti-aliasing can be enabled from the plot's context menu (see Help).")
if ImPlot.BeginPlot("Line Plot", "x", "f(x)")
ImPlot.PlotLine("sin(x)", xs1, ys1, length(xs1))
ImPlot.SetNextMarkerStyle(ImPlotMarker_Circle)
ImPlot.PlotLine("x^2", xs2, ys2, length(xs2))
ImPlot.EndPlot()
end
test_buttons()
CImGui.End()
end
# # main functinon
function show_gui()
Renderer.render(
ui, # function object
width = 1360,
height = 780,
title = "",
hotloading = true
)
return nothing
end
show_gui()
@cesr42
Copy link

cesr42 commented Aug 25, 2022

I love this demo. It works great on windows for me, but not on Linux. Should it work there or is that a known concern?

@sairus7
Copy link
Author

sairus7 commented Aug 25, 2022

No, Its supposed to work on both OS. You can add an issue to CImGui or ImPlot repo.

@sairus7
Copy link
Author

sairus7 commented Aug 25, 2022

@cesr42 by the way, I have it working on Ubuntu, maybe you have to run it into a separate environment? ]activate .
image

@cesr42
Copy link

cesr42 commented Aug 25, 2022

Thanks for the follow-up!
I'm seeing

ERROR: LoadError: AssertionError: window != C_NULL

on Scientific Linux (Red Hat) 7.9 with Julia 1.8.
I'll assume it's something wrong on my end until proven otherwise.

@sairus7
Copy link
Author

sairus7 commented Apr 25, 2023

@cesr42

ERROR: LoadError: AssertionError: window != C_NULL

I've seen the same error on one Windows 10 machine, while on the others it works fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment