Skip to content

Commit

Permalink
Merge pull request #134 from Gnimuc/makie
Browse files Browse the repository at this point in the history
Improved Makie support
  • Loading branch information
JamesWrigley authored Aug 2, 2024
2 parents 38eadb5 + b1cbbd3 commit 5542784
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "CImGui"
uuid = "5d785b6c-b76f-510e-a07c-3070796c7e87"
authors = ["Yupei Qi <qiyupei@gmail.com>"]
version = "2.1.0"
version = "2.2.0"

[deps]
CEnum = "fa961155-64e5-5f13-b03f-caf6b980ea82"
Expand Down
6 changes: 6 additions & 0 deletions docs/src/_changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ CurrentModule = CImGui
This documents notable changes in CImGui.jl. The format is based on [Keep a
Changelog](https://keepachangelog.com).

## [v2.2.0] - 2024-08-02

### Added
- Support for more GLMakie controls, and a tooltip for each figure by default
([#134]).

## [v2.1.0] - 2024-07-29

### Added
Expand Down
46 changes: 45 additions & 1 deletion ext/MakieIntegration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,29 @@ function apply_config!(screen::GLMakie.Screen, config::GLMakie.ScreenConfig)
screen.config = config
end

function ig.MakieFigure(title_id::String, f::GLMakie.Figure; auto_resize_x=true, auto_resize_y=false)
function draw_figure_tooltip(cursor_pos, image_size)
help_str = "(?)"
text_size = ig.CalcTextSize(help_str)
text_pos = (cursor_pos.x + image_size[1] - text_size.x, cursor_pos.y)
ig.AddText(ig.GetWindowDrawList(), text_pos, ig.IM_COL32_BLACK, help_str)
hovering = ig.IsMouseHoveringRect(text_pos, (text_pos[1] + text_size.x, text_pos[2] + text_size.y))

if hovering && ig.BeginTooltip()
ig.PushTextWrapPos(ig.GetFontSize() * 35.0)
ig.TextUnformatted("""
Controls:
- Scroll to zoom
- Click and drag to rectangle select a region to zoom to
- Right click and drag to pan
- Shift + {x/y} and scroll to zoom along the X/Y axes
- Ctrl + left click to reset the limits
""")
ig.PopTextWrapPos()
ig.EndTooltip()
end
end

function ig.MakieFigure(title_id::String, f::GLMakie.Figure; auto_resize_x=true, auto_resize_y=false, tooltip=true)
ig.PushID(title_id)
id = ig.GetID(title_id)

Expand Down Expand Up @@ -152,6 +174,12 @@ function ig.MakieFigure(title_id::String, f::GLMakie.Figure; auto_resize_x=true,
cursor_pos,
(cursor_pos.x + image_size[1], cursor_pos.y + image_size[2]),
(0, 1), (1, 0))

# Draw tooltip
if tooltip
draw_figure_tooltip(cursor_pos, image_size)
end

ig.InvisibleButton("figure_image", size(color_buffer))

# Update the scene events
Expand All @@ -164,6 +192,7 @@ function ig.MakieFigure(title_id::String, f::GLMakie.Figure; auto_resize_x=true,

io = ig.GetIO()
if ig.IsItemHovered()
# Update the mouse position
pos = ig.GetMousePos()
cursor_pos = ig.GetCursorScreenPos()
item_spacing = unsafe_load(ig.GetStyle().ItemSpacing.y)
Expand All @@ -172,6 +201,7 @@ function ig.MakieFigure(title_id::String, f::GLMakie.Figure; auto_resize_x=true,
scene.events.mouseposition[] = new_pos
end

# Update the mouse buttons
for (igkey, makiekey) in ((ig.ImGuiKey_MouseLeft, Makie.Mouse.left),
(ig.ImGuiKey_MouseRight, Makie.Mouse.right))
if ig.IsKeyPressed(igkey)
Expand All @@ -181,12 +211,26 @@ function ig.MakieFigure(title_id::String, f::GLMakie.Figure; auto_resize_x=true,
end
end

# Update the scroll rate
wheel_y = unsafe_load(io.MouseWheel)
wheel_x = unsafe_load(io.MouseWheelH)
if (wheel_x, wheel_y) != scene.events.scroll[]
scene.events.scroll[] = (wheel_x, wheel_y)
end

# Update pressed and released keys. For now we only support X/Y/left
# Ctrl because those are the ones that Makie uses by default and to do
# it properly for all keys we need to make a mapping from ImGuiKey's to
# GLFW keys.
for (igkey, glfwkey) in ((ig.lib.ImGuiKey_X, Int(GLFW.KEY_X)),
(ig.lib.ImGuiKey_Y, Int(GLFW.KEY_Y)),
(ig.lib.ImGuiKey_LeftCtrl, Int(GLFW.KEY_LEFT_CONTROL)))
if ig.IsKeyPressed(igkey)
scene.events.keyboardbutton[] = Makie.KeyEvent(Makie.Keyboard.Button(glfwkey), Makie.Keyboard.press)
elseif ig.IsKeyReleased(igkey)
scene.events.keyboardbutton[] = Makie.KeyEvent(Makie.Keyboard.Button(glfwkey), Makie.Keyboard.release)
end
end
end

ig.PopID()
Expand Down
12 changes: 8 additions & 4 deletions src/CImGui.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,15 @@ const IMGUI_VERSION = unsafe_string(GetVersion())
# This is implemented by the MakieIntegration extension but we document it here
# so that we don't have to install GLMakie to build the docs.
"""
MakieFigure(id::String, f::GLMakie.Figure; auto_resize_x=true, auto_resize_y=false)
MakieFigure(id::String, f::GLMakie.Figure; auto_resize_x=true, auto_resize_y=false, tooltip=true)
Display a Makie figure in ImGui. See `examples/makie_demo.jl` for an example of
how to use it. This supports all the interaction features in GLMakie:
- Scrolling to zoom
- Rectangle select to zoom
- RMB to pan
- Scroll to zoom
- Click and drag to rectangle select a region to zoom to
- Right click and drag to pan
- Shift + {x/y} and scroll to zoom along the X/Y axes
- Ctrl + left click to reset the limits
Note that scrolling to zoom will also cause the ImGui window to scroll, which
can be annoying. This may be fixed in the future by using some other key
Expand All @@ -99,6 +101,8 @@ Known issues:
keep the old mouse state. e.g. if you're RMB panning and the mouse goes
outside the figure, when it enters the figure again panning will resume even
the RMB was released.
- Drawing can be a bit janky, occasionally the image will not be drawn for a
frame or two and you'll see an empty black square instead.
!!! warning
This is very experimental, you will almost definitely encounter bugs (and if
Expand Down

2 comments on commit 5542784

@JamesWrigley
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/112324

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v2.2.0 -m "<description of version>" 554278415dfa0c00faee3e536046b171259ad383
git push origin v2.2.0

Please sign in to comment.