delete edge by clicking on either terminus

This wasn't my original plan; I wanted to detach/reattach edges, but
that seems more complicated now when I consider the possibility of
detaching the source of an edge.

I think I'm not going to bother with a mechanic for deleting nodes. Just
disconnect edges and repurpose them. That way the number of widges per
node doesn't get worse, and I also don't have to worry about undoing
deletion.
This commit is contained in:
Kartik K. Agaram 2023-04-20 22:51:03 -07:00
parent 8202765f98
commit 3a2d6b5839
6 changed files with 55 additions and 2 deletions

View File

@ -24,9 +24,15 @@ on.mouse_press = function(x,y, mouse_button)
end
node = on_border(sx,sy)
if node then
Edge = {source=node, s={sx=sx, sy=sy}, e={sx=sx, sy=sy}}
local edge = on_edge(sx,sy)
if edge == nil then
Edge = {source=node, s={sx=sx, sy=sy}, e={sx=sx, sy=sy}}
else
detach_edge(edge)
A()
end
return
end
-- default
Pan = {sx=sx,sy=sy}
end
end

View File

@ -24,6 +24,8 @@ on.update = function(dt)
set_mouse_cursor('crosshair')
elseif on_resize(sx, sy) then
set_mouse_cursor('sizewe')
elseif on_edge(sx,sy) then
set_mouse_cursor('no')
else
set_mouse_cursor('arrow')
end

17
0069-on_edge Normal file
View File

@ -0,0 +1,17 @@
on_edge = function(sx,sy)
-- check if mouse is on the border of a node and also on an edge to another node.
-- reading from Surface; I'm going to regret this.
for _,shape in ipairs(Surface) do
if shape.type == 'line' then
if shape.keys then
-- it's an edge; assuming keys are in a specific, brittle order
if distance_sq(sx,sy, shape.data[1], shape.data[2]) < 5*5 then
return shape.keys
end
if distance_sq(sx,sy, shape.data[3], shape.data[4]) < 5*5 then
return shape.keys
end
end
end
end
end

5
0070-detach_edge Normal file
View File

@ -0,0 +1,5 @@
detach_edge = function(edge)
local src, dest = edge[1], edge[2]
table.remove_value(Nodes[src].outgoing_edges, dest)
table.remove_value(Nodes[dest].incoming_edges, src)
end

6
0071-table.remove_value Normal file
View File

@ -0,0 +1,6 @@
table.remove_value = function(h, val)
local i = table.find(h, val)
if i then
table.remove(h, i)
end
end

17
0072-test_remove_value Normal file
View File

@ -0,0 +1,17 @@
test_remove_value = function()
local h = {1,2,3}
table.remove_value(h, 1)
check_eq(h, {2,3}, "a")
local h = {1,2,3}
table.remove_value(h, 2)
check_eq(h, {1,3}, "b")
local h = {1,2,3}
table.remove_value(h, 3)
check_eq(h, {1,2}, "b")
local h = {1,2,3}
table.remove_value(h, 4)
check_eq(h, {1,2,3}, "b")
end