Skip to content

Commit

Permalink
Merge pull request #110 from Gnimuc/update-examples
Browse files Browse the repository at this point in the history
Update examples for v1.89
  • Loading branch information
Gnimuc authored Jan 14, 2024
2 parents 42dc1ae + 7641c37 commit 1132364
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 36 deletions.
14 changes: 8 additions & 6 deletions examples/app_custom_rendering.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,22 @@ function ShowExampleAppCustomRendering(p_open::Ref{Bool})
th::Cfloat = (n == 0) ? 1.0 : thickness
CImGui.AddCircle(draw_list, ImVec2(x+sz*0.5, y+sz*0.5), sz*0.5, col32, 6, th); x += sz + spacing; # hexagon
CImGui.AddCircle(draw_list, ImVec2(x+sz*0.5, y+sz*0.5), sz*0.5, col32, 20, th); x += sz + spacing; # circle
CImGui.AddRect(draw_list, ImVec2(x, y), ImVec2(x+sz, y+sz), col32, 0.0, CImGui.ImDrawCornerFlags_All, th); x += sz + spacing;
CImGui.AddRect(draw_list, ImVec2(x, y), ImVec2(x+sz, y+sz), col32, 10.0, CImGui.ImDrawCornerFlags_All, th); x += sz + spacing;
CImGui.AddRect(draw_list, ImVec2(x, y), ImVec2(x+sz, y+sz), col32, 10.0, CImGui.ImDrawCornerFlags_TopLeft|CImGui.ImDrawCornerFlags_BotRight, th); x += sz + spacing;
CImGui.AddRect(draw_list, ImVec2(x, y), ImVec2(x+sz, y+sz), col32, 0.0, CImGui.ImDrawFlags_RoundCornersAll, th); x += sz + spacing;
CImGui.AddRect(draw_list, ImVec2(x, y), ImVec2(x+sz, y+sz), col32, 10.0, CImGui.ImDrawFlags_RoundCornersAll, th); x += sz + spacing;
CImGui.AddRect(draw_list, ImVec2(x, y), ImVec2(x+sz, y+sz), col32, 10.0, CImGui.ImDrawFlags_RoundCornersTopLeft | CImGui.ImDrawFlags_RoundCornersBottomRight, th); x += sz + spacing;
CImGui.AddTriangle(draw_list, ImVec2(x+sz*0.5, y), ImVec2(x+sz,y+sz-0.5), ImVec2(x,y+sz-0.5), col32, th); x += sz + spacing;
CImGui.AddLine(draw_list, ImVec2(x, y), ImVec2(x+sz, y), col32, th); x += sz + spacing; # horizontal line (note: drawing a filled rectangle will be faster!)
CImGui.AddLine(draw_list, ImVec2(x, y), ImVec2(x, y+sz), col32, th); x += spacing; # vertical line (note: drawing a filled rectangle will be faster!)
CImGui.AddLine(draw_list, ImVec2(x, y), ImVec2(x+sz, y+sz), col32, th); x += sz +spacing; # diagonal line
CImGui.AddBezierCurve(draw_list, ImVec2(x, y), ImVec2(x+sz*1.3,y+sz*0.3), (x+sz-sz*1.3,y+sz-sz*0.3), ImVec2(x+sz, y+sz), col32, th);
CImGui.AddBezierCubic(draw_list, ImVec2(x, y), ImVec2(x+sz*1.3,y+sz*0.3), (x+sz-sz*1.3,y+sz-sz*0.3), ImVec2(x+sz, y+sz), col32, th);
x = p.x + 4
y += sz + spacing
end
CImGui.AddCircleFilled(draw_list, ImVec2(x+sz*0.5, y+sz*0.5), sz*0.5, col32, 6); x += sz+spacing; # hexagon
CImGui.AddCircleFilled(draw_list, ImVec2(x+sz*0.5, y+sz*0.5), sz*0.5, col32, 32); x += sz+spacing; # circle
CImGui.AddRectFilled(draw_list, ImVec2(x, y), ImVec2(x+sz, y+sz), col32); x += sz+spacing;
CImGui.AddRectFilled(draw_list, ImVec2(x, y), ImVec2(x+sz, y+sz), col32, 10.0); x += sz+spacing;
CImGui.AddRectFilled(draw_list, ImVec2(x, y), ImVec2(x+sz, y+sz), col32, 10.0, CImGui.ImDrawCornerFlags_TopLeft|CImGui.ImDrawCornerFlags_BotRight); x += sz+spacing;
CImGui.AddRectFilled(draw_list, ImVec2(x, y), ImVec2(x+sz, y+sz), col32, 10.0, CImGui.ImDrawFlags_RoundCornersTopLeft | CImGui.ImDrawFlags_RoundCornersBottomRight); x += sz+spacing;
CImGui.AddTriangleFilled(draw_list, ImVec2(x+sz*0.5, y), ImVec2(x+sz,y+sz-0.5), ImVec2(x,y+sz-0.5), col32); x += sz+spacing;
CImGui.AddRectFilled(draw_list, ImVec2(x, y), ImVec2(x+sz, y+thickness), col32); x += sz+spacing; # horizontal line (faster than AddLine, but only handle integer thickness)
CImGui.AddRectFilled(draw_list, ImVec2(x, y), ImVec2(x+thickness, y+sz), col32); x += spacing+spacing; # vertical line (faster than AddLine, but only handle integer thickness)
Expand Down Expand Up @@ -81,7 +81,9 @@ function ShowExampleAppCustomRendering(p_open::Ref{Bool})

adding_preview = false
CImGui.InvisibleButton("canvas", canvas_size)
mouse_pos_in_canvas = ImVec2(CImGui.GetIO().MousePos.x - canvas_pos.x, CImGui.GetIO().MousePos.y - canvas_pos.y)
mouse_x = unsafe_load(CImGui.GetIO().MousePos.x)
mouse_y = unsafe_load(CImGui.GetIO().MousePos.y)
mouse_pos_in_canvas = ImVec2(mouse_x - canvas_pos.x, mouse_y - canvas_pos.y)
if adding_line
adding_preview = true
push!(points, mouse_pos_in_canvas)
Expand Down
4 changes: 2 additions & 2 deletions examples/demo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ using CImGui.ImGuiOpenGLBackend.ModernGL
# using CImGui.ImGuiGLFWBackend.GLFW
using CImGui.CSyntax

# include(joinpath(@__DIR__, "demo_window.jl"))
include(joinpath(@__DIR__, "demo_window.jl"))

glfwDefaultWindowHints()
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3)
Expand Down Expand Up @@ -86,7 +86,7 @@ try
ImGuiGLFWBackend.new_frame(window_ctx)
CImGui.NewFrame()

demo_open && @c CImGui.ShowDemoWindow(&demo_open)
demo_open && @c ShowJuliaDemoWindow(&demo_open)

# show image example
if CImGui.Begin("Image Demo")
Expand Down
2 changes: 1 addition & 1 deletion examples/demo_layout.jl
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ function ShowDemoWindowLayout()
CImGui.Button("LEVERAGE\nBUZZWORD", size)
CImGui.SameLine()

if CImGui.ListBoxHeader("List", size)
if CImGui.BeginListBox("List", size)
CImGui.Selectable("Selected", true)
CImGui.Selectable("Not Selected", false)
CImGui.EndListBox()
Expand Down
32 changes: 19 additions & 13 deletions examples/demo_misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,45 +64,51 @@ function ShowDemoWindowMisc()
end
CImGui.Text(@sprintf("Mouse wheel: %.1f", unsafe_load(io.MouseWheel)))

key_range = CImGui.ImGuiKey_NamedKey_BEGIN:CImGui.ImGuiKey_NamedKey_END - 1

CImGui.Text("Keys down:")
for i = 0:511
dur = CImGui.c_get(io.KeysDownDuration, i)
for i = key_range
key_data = unsafe_load(CImGui.GetKeyData(CImGui.ImGuiKey(i)))
dur = key_data.DownDuration
dur 0 || continue
CImGui.SameLine()
txt = @sprintf "%d (%.02f secs)" i dur
CImGui.Text(txt)
end
CImGui.Text("Keys pressed:")
for i = 0:511
CImGui.IsKeyPressed(i) || continue
for i = key_range
CImGui.IsKeyPressed(CImGui.ImGuiKey(i)) || continue
CImGui.SameLine()
CImGui.Text("$i")
end
CImGui.Text("Keys release:")
for i = 0:511
CImGui.IsKeyReleased(i) || continue
for i = key_range
CImGui.IsKeyReleased(CImGui.ImGuiKey(i)) || continue
CImGui.SameLine()
CImGui.Text("$i")
end
CImGui.Text(@sprintf("Keys mods: %s%s%s%s", unsafe_load(io.KeyCtrl) ? "CTRL " : "", unsafe_load(io.KeyShift) ? "SHIFT " : "", unsafe_load(io.KeyAlt) ? "ALT " : "", unsafe_load(io.KeySuper) ? "SUPER " : ""))

navinput_range = CImGui.ImGuiKey_GamepadStart:CImGui.ImGuiKey_GamepadRStickDown

CImGui.Text("NavInputs down:")
for i = 0:20
nav = CImGui.c_get(io.NavInputs, i)
nav > 0 || continue
for i = navinput_range
CImGui.IsKeyDown(CImGui.ImGuiKey(i)) || continue
CImGui.SameLine()
CImGui.Text(@sprintf("[%d] %.2f", i, nav))
end
CImGui.Text("NavInputs pressed:")
for i = 0:20
dur = CImGui.c_get(io.NavInputsDownDuration, i)
for i = navinput_range
key_data = unsafe_load(CImGui.GetKeyData(CImGui.ImGuiKey(i)))
dur = key_data.DownDuration
dur == 0.0 || continue
CImGui.SameLine()
CImGui.Text("[$i]")
end
CImGui.Text("NavInputs duration:")
for i = 0:20
dur = CImGui.c_get(io.NavInputsDownDuration, i)
for i = navinput_range
key_data = unsafe_load(CImGui.GetKeyData(CImGui.ImGuiKey(i)))
dur = key_data.DownDuration
dur 0.0 || continue
CImGui.SameLine()
CImGui.Text(@sprintf("[%d] %.2f", i, dur))
Expand Down
7 changes: 5 additions & 2 deletions examples/demo_popups.jl
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,11 @@ function ShowDemoWindowPopups()
@cstatic value = Cfloat(0.5) begin
CImGui.Text(@sprintf("Value = %.3f (<-- right-click here)", value))
if CImGui.BeginPopupContextItem("item context menu")
CImGui.Selectable("Set to zero") && (value = 0.0;)
CImGui.Selectable("Set to PI") && (value = 3.1415;)
# Note that we explicitly use Float32 literals, otherwise
# `value` would change type to Float64, which would cause an
# error when calling DragFloat(), which expects a Float32.
CImGui.Selectable("Set to zero") && (value = 0f0;)
CImGui.Selectable("Set to PI") && (value = 3.1415f0;)
CImGui.PushItemWidth(-1)
@c CImGui.DragFloat("##Value", &value, 0.1, 0.0, 0.0)
CImGui.PopItemWidth()
Expand Down
30 changes: 21 additions & 9 deletions examples/demo_widgets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function ShowDemoWindowWidgets()

# arrow buttons with Repeater
@cstatic counter=Cint(0) begin
spacing = CImGui.GetStyle().ItemInnerSpacing.x
spacing = unsafe_load(CImGui.GetStyle().ItemInnerSpacing.x)
CImGui.PushButtonRepeat(true)
CImGui.ArrowButton("##left", CImGui.ImGuiDir_Left) && (counter-=1;)
CImGui.SameLine(0.0, spacing)
Expand Down Expand Up @@ -182,7 +182,7 @@ function ShowDemoWindowWidgets()
end
if node_clicked != -1
# update selection state. Process outside of tree loop to avoid visual inconsistencies during the clicking-frame.
if CImGui.GetIO().KeyCtrl
if unsafe_load(CImGui.GetIO().KeyCtrl)
selection_mask ⊻= 1 << node_clicked # CTRL+click to toggle
else #if (!(selection_mask & (1 << node_clicked))) # Depending on selection behavior you want, this commented bit preserve selection when clicking on item that is part of the selection
selection_mask = 1 << node_clicked # Click to single-select
Expand Down Expand Up @@ -290,9 +290,10 @@ function ShowDemoWindowWidgets()
# If you decided that ImTextureID = MyEngineTexture*, then you can pass your MyEngineTexture* pointers to CImGui.Image(), and gather width/height through your own functions, etc.
# Using ShowMetricsWindow() as a "debugger" to inspect the draw data that are being passed to your render will help you debug issues if you are confused about this.
# Consider using the lower-level ImDrawList::AddImage() API, via CImGui.GetWindowDrawList()->AddImage().
my_tex_id = unsafe_load(io.Fonts.TexID)
my_tex_w = unsafe_load(io.Fonts.TexWidth)
my_tex_h = unsafe_load(io.Fonts.TexHeight)
font_atlas = unsafe_load(io.Fonts)
my_tex_id = unsafe_load(font_atlas.TexID)
my_tex_w = unsafe_load(font_atlas.TexWidth)
my_tex_h = unsafe_load(font_atlas.TexHeight)

CImGui.Text(@sprintf("%.0fx%.0f", my_tex_w, my_tex_h))
pos = CImGui.GetCursorScreenPos()
Expand Down Expand Up @@ -325,9 +326,18 @@ function ShowDemoWindowWidgets()
for i = 0:8-1
CImGui.PushID(i)
frame_padding = -1 + i # -1 = uses default padding
if CImGui.ImageButton(my_tex_id, (32,32), (0,0), ImVec2(32.0/my_tex_w,32/my_tex_h), frame_padding, (0,0,0,255))
if frame_padding > 0
CImGui.PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(i, i))
end

if CImGui.ImageButton("", my_tex_id, (32,32), (0,0), ImVec2(32.0/my_tex_w,32/my_tex_h), ImVec4(0,0,0,255))
pressed_count += 1
end

if frame_padding > 0
CImGui.PopStyleVar()
end

CImGui.PopID()
CImGui.SameLine()
end
Expand Down Expand Up @@ -421,7 +431,7 @@ function ShowDemoWindowWidgets()
buf = @sprintf "Object %d" n
if CImGui.Selectable(buf, selection[n+1])
# clear selection when CTRL is not held
!CImGui.GetIO().KeyCtrl && fill!(selection, false)
!unsafe_load(CImGui.GetIO().KeyCtrl) && fill!(selection, false)
selection[n+1] ⊻= 1
end
end
Expand Down Expand Up @@ -979,8 +989,10 @@ function ShowDemoWindowWidgets()
if CImGui.BeginDragDropTarget()
payload = CImGui.AcceptDragDropPayload("DND_DEMO_CELL")
if payload != C_NULL
@assert CImGui.Get(payload, :DataSize) == sizeof(Cint)
payload_n = unsafe_load(Ptr{Cint}(CImGui.Get(payload, :Data)))
payload = unsafe_load(payload)

@assert payload.DataSize == sizeof(Cint)
payload_n = unsafe_load(Ptr{Cint}(payload.Data))
if mode == Mode_Copy
names[n+1] = names[payload_n+1]
end
Expand Down
6 changes: 3 additions & 3 deletions examples/demo_window.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ no_nav = false
no_background = false
no_bring_to_front = false
"""
ShowDemoWindow(p_open::Ref{Bool})
Demonstrate most Dear ImGui features.
ShowJuliaDemoWindow(p_open::Ref{Bool})
Demonstrate most Dear ImGui features implemented using the Julia bindings.
You may execute this function to experiment with the UI and understand what it does.
You may then search for keywords in the code when you are interested by a specific feature.
"""
global function ShowDemoWindow(p_open::Ref{Bool})
global function ShowJuliaDemoWindow(p_open::Ref{Bool})
# examples Apps (accessible from the "Examples" menu)
# show_app_documents && @c ShowExampleAppDocuments(&show_app_documents) # process the Document app next, as it may also use a DockSpace()
show_app_main_menu_bar && @c ShowExampleAppMainMenuBar(&show_app_main_menu_bar)
Expand Down
6 changes: 6 additions & 0 deletions src/wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2457,6 +2457,12 @@ Map ImGuiKey_* values into user's key index. == io.KeyMap[key]
"""
GetKeyIndex(imgui_key) = igGetKeyIndex(imgui_key)

"""
GetKeyData(imgui_key) -> Ptr{ImGuiKeyData}
Get the key data for a specific key.
"""
GetKeyData(imgui_key) = igGetKeyData(imgui_key)

"""
IsKeyDown(user_key_index) -> Bool
Is key being held. == io.KeysDown[user_key_index]. note that imgui doesn't know the semantic
Expand Down

0 comments on commit 1132364

Please sign in to comment.