Android Question [B4X] [XUI] B4XCanvas - how to remove a line

Alexander Stolte

Expert
Licensed User
Longtime User
Hello,

i draw a line with this code:
B4X:
If Action = xpnl_base.TOUCH_ACTION_DOWN Then
        g_y = y
        g_x = x
        xpath_main.Initialize(X,Y) 
    Else if Action = xpnl_base.TOUCH_ACTION_MOVE Then
        xcv_main.DrawLine(X, Y, g_x, g_y, xui.Color_Red,5)
        xpath_main.LineTo(x,y)
        g_x = X
        g_y = Y       
        xcv_main.Invalidate
        End If
And save the line in a B4XPath.
how can i remove this line with the help of the B4XPath?
 

JordiCP

Expert
Licensed User
Longtime User
A simple solution would be to use a list to add/remove the coords, and only create the path based on it, when/if needed. This will allow you for an undo-depth greater than one.

To remove the line once it is drawn, you could either just clear the canvas and redraw everything using a path created with the new list (once the last element is removed) , or redraw the last line with the background color (if it is solid)
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
A cleaner approach, if you only want a 1-undo-depth, would be to draw the latest line always on a different canvas (on top of the current one).
So,
  • When a new line is drawn, the previous one is removed from the temporal canvas and drawn on the final one (you keep the latest couple of coords)
  • When a line is removed, just clear the temporal canvas.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Unless you are creating fast moving graphics (in which case you should probably be looking to use a game library approach), redrawing the canvas is quick enough to redraw all of the objects. Save the paths in a list, then manage the list to delete the ones you no longer want and clear and redraw them all.

It'll save problems where lines may cross or overlay at corners etc..
 
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
Thanks all!
i did it.

B4X:
Public Sub Undo
    If (current_index) > -1 Then
        xcv_main.ClearRect(xcv_main.TargetRect)   
        For i = 0 To current_index -1           
            xcv_main.DrawPath(lst_paths.Get(i),xui.Color_Red,False,5)           
        Next   
    xcv_main.Invalidate   
    current_index = current_index -1
    End If
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…