This commit is contained in:
visionmercer 2026-04-30 13:28:15 +02:00
commit b04aaa7019

View file

@ -151,11 +151,14 @@ sub canvas
dim boxX1 as integer: boxX1 = 70 dim boxX1 as integer: boxX1 = 70
dim boxWidth as integer: boxWidth = _width - 1 - boxX1 dim boxWidth as integer: boxWidth = _width - 1 - boxX1
dim boxHeight as integer: boxHeight = _height - 20 dim boxHeight as integer: boxHeight = _height - 20
' Added static array to persist points between frames
static polypoints(200) as single
static pointCount as integer
' Workspace background ' Workspace background
line (boxX1, 0)-(_width - 1, boxHeight), _rgb32(32), bf line (boxX1, 0)-(_width - 1, boxHeight), _rgb32(32), bf
' Render Layers (0 = background,1 = drawlayer, 2=reference 3 = Preview) ' Render Layers
dim i as integer dim i as integer
for i = 0 to ubound(layers) for i = 0 to ubound(layers)
dim w as integer: w = _width(layers(i).ihandle) * state.zoom dim w as integer: w = _width(layers(i).ihandle) * state.zoom
@ -163,71 +166,88 @@ sub canvas
_putimage (state.offsetX, state.offsetY)-(state.offsetX + w, state.offsetY + h), layers(i).ihandle _putimage (state.offsetX, state.offsetY)-(state.offsetX + w, state.offsetY + h), layers(i).ihandle
next next
' Translate Mouse to Canvas Space ' Translate Mouse to Canvas Space
dim canX as integer: canX = (_mousex - state.offsetX) / state.zoom dim canX as integer: canX = (_mousex - state.offsetX) / state.zoom
dim canY as integer: canY = (_mousey - state.offsetY) / state.zoom dim canY as integer: canY = (_mousey - state.offsetY) / state.zoom
' Color Selection: Left Click = Foreground, Right Click = Background
dim drawCol as _unsigned long dim drawCol as _unsigned long
if mousedown or mouseclicked then drawCol = state.fcolor else drawCol = state.bcolor drawCol = state.fcolor
' Interaction Boundary Check
if _mousex > boxX1 then if _mousex > boxX1 then
' On Initial Click: Set the anchor point for shapes ' Handle First Point / Adding Points
if (mousedown or rmousedown) and state.isDrawing = 0 then if mouseclicked then
if state.tool = 7 or state.tool = 8 then
polypoints(pointCount * 2) = canX
polypoints(pointCount * 2 + 1) = canY
pointCount = pointCount + 1
state.isDrawing = -1
elseif state.isDrawing = 0 then
state.startX = canX state.startX = canX
state.startY = canY state.startY = canY
state.isDrawing = -1 state.isDrawing = -1
end if end if
end if
if state.isDrawing then if state.isDrawing then
' preview layer handles the "rubber-banding" of shapes
_dest layers(2).ihandle _dest layers(2).ihandle
cls , 0 cls , 0
select case state.tool select case state.tool
case 1 ' Pencil (Direct to Canvas) case 1 ' Pencil
_dest layers(1).ihandle _dest layers(1).ihandle
thickline state.startX, state.startY, canX, canY, drawCol thickline state.startX, state.startY, canX, canY, drawCol
state.startX = canX: state.startY = canY state.startX = canX: state.startY = canY
case 2 ' Line
case 2 ' Straight Line
thickline state.startX, state.startY, canX, canY, drawCol thickline state.startX, state.startY, canX, canY, drawCol
case 3 ' Circle
case 3 ' Thick Circle (Outline)
dim r as single: r = sqr((canX - state.startX)^2 + (canY - state.startY)^2) dim r as single: r = sqr((canX - state.startX)^2 + (canY - state.startY)^2)
r=abs(r)+1 thickcircle state.startX, state.startY, r + 1, drawCol
thickcircle state.startX, state.startY, r, drawCol
case 4 ' Filled Circle case 4 ' Filled Circle
dim r_fill as single: r_fill = sqr((canX - state.startX)^2 + (canY - state.startY)^2) dim r_fill as single: r_fill = sqr((canX - state.startX)^2 + (canY - state.startY)^2)
filledcircle state.startX, state.startY, r_fill, drawCol filledcircle state.startX, state.startY, r_fill, drawCol
case 5 ' Rect
case 5 ' Rectangle Outline (using thickline)
thickline state.startX, state.startY, canX, state.startY, drawCol thickline state.startX, state.startY, canX, state.startY, drawCol
thickline canX, state.startY, canX, canY, drawCol thickline canX, state.startY, canX, canY, drawCol
thickline canX, canY, state.startX, canY, drawCol thickline canX, canY, state.startX, canY, drawCol
thickline state.startX, canY, state.startX, state.startY, drawCol thickline state.startX, canY, state.startX, state.startY, drawCol
case 6 ' Filled Rect
case 6 ' Filled Rectangle
line (state.startX, state.startY)-(canX, canY), drawCol, bf line (state.startX, state.startY)-(canX, canY), drawCol, bf
case 7 case 7, 8 ' Polygon Outline and Filled
if pointCount > 0 then
case 8 ' Polygon Outline (using thickline) ' Draw existing segments
' Simple preview of a line; complex polygons usually require for p = 1 to pointCount - 1
' a point-collection state machine. thickline polypoints((p - 1) * 2), polypoints((p - 1) * 2 + 1), polypoints(p * 2), polypoints(p * 2 + 1), drawCol
thickline state.startX, state.startY, canX, canY, drawCol next p
' Draw "rubber-band" to cursor
thickline polypoints((pointCount - 1) * 2), polypoints((pointCount - 1) * 2 + 1), canX, canY, drawCol
end if
end select end select
' Release Logic: Commit preview to permanent layer ' Release/Commit Logic
if _mousebutton(1) = 0 and _mousebutton(2) = 0 then dim finishDrawing as integer: finishDrawing = 0
' Polygons finish on Right Click or Enter
if state.tool = 7 or state.tool = 8 then
if rmouseclicked or _keydown(13) then finishDrawing = -1
else
if _mousebutton(1) = 0 then finishDrawing = -1
end if
if finishDrawing then
_dest layers(1).ihandle _dest layers(1).ihandle
if state.tool = 8 and pointCount > 2 then
' Finalize Filled Polygon array for the helper
redim finalPoints(pointCount * 2 - 1) as single
for p = 0 to (pointCount * 2) - 1: finalPoints(p) = polypoints(p): next
filledPolygon finalPoints(), drawCol
else
_putimage , layers(2).ihandle _putimage , layers(2).ihandle
_dest layers(2).ihandle end if
cls , 0
_dest layers(2).ihandle: cls , 0
state.isDrawing = 0 state.isDrawing = 0
pointCount = 0 ' Reset polygon points
end if end if
_dest 0 _dest 0
end if end if