Skip to content

Instantly share code, notes, and snippets.

@SimonDanisch
Created July 31, 2018 13:15
Show Gist options
  • Save SimonDanisch/9e51c486d145e8914aa4084938425d4d to your computer and use it in GitHub Desktop.
Save SimonDanisch/9e51c486d145e8914aa4084938425d4d to your computer and use it in GitHub Desktop.
using Makie
points = node(:poly, Point2f0[(0, 0), (0.5, 0.5), (1.0, 0.0)])
scene = poly(points, strokewidth = 2, strokecolor = :black, color = :skyblue2, show_axis = false, scale_plot = false)
scatter!(points, color = :white, strokewidth = 10, markersize = 0.05, strokecolor = :black, raw = true)
pplot = scene[end]
push!(points[], Point2f0(0.6, -0.3))
points[] = points[]
function add_move!(scene, points, pplot)
idx = Ref(0); dragstart = Ref(false); startpos = Base.RefValue(Point2f0(0))
foreach(events(scene).mousedrag) do drag
if ispressed(scene, Mouse.left)
if drag == Mouse.down
plot, _idx = Makie.mouse_selection(scene)
if plot == pplot
idx[] = _idx; dragstart[] = true
startpos[] = to_world(scene, Point2f0(scene.events.mouseposition[]))
end
elseif drag == Mouse.pressed && dragstart[] && checkbounds(Bool, points[], idx[])
pos = to_world(scene, Point2f0(scene.events.mouseposition[]))
points[][idx[]] = pos
points[] = points[]
end
else
dragstart[] = false
end
return
end
end
function add_remove_add!(scene, points, pplot)
foreach(events(scene).mousebuttons) do but
if ispressed(but, Mouse.left) && ispressed(scene, Keyboard.left_control)
pos = to_world(scene, Point2f0(events(scene).mouseposition[]))
push!(points[], pos)
points[] = points[]
elseif ispressed(but, Mouse.right)
plot, idx = Makie.mouse_selection(scene)
if plot == pplot && checkbounds(Bool, points[], idx)
deleteat!(points[], idx)
points[] = points[]
end
end
return
end
end
add_move!(scene, points, pplot)
add_remove_add!(scene, points, pplot)
center!(scene)
scene
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment