refactor canvas sub. a start.

This commit is contained in:
visionmercer 2026-05-18 14:05:35 +02:00
commit 947c7eac4c

View file

@ -187,6 +187,10 @@ end sub
sub redraw
redim numarr(0) as long
dim i as integer
dim j as integer
dim x as long
dim y as long
_dest layers(1).ihandle
_source layers(1).ihandle
cls ,_rgb32(255)
@ -196,6 +200,21 @@ sub redraw
_dest layers(1).ihandle
select case lcase$(_trim$(left$(commands(i),instr(commands(i),"(")-1)))
case "canvas"
for j=0 to 3
if layers(j).ihandle <> 0 then _freeimage layers(j).ihandle
layers(j).ihandle = _newimage(numarr(0),numarr(1),32)
next j
_dest layers(0).ihandle
for y = 0 to _height - 16 step 16
for x = 0 to _width - 16 step 16
if ((x + y) / 16 AND 1) = 0 then
line (x, y)-(x + 16, y + 16), _rgb32(127), bf
else
line (x, y)-(x + 16, y + 16), _rgb32(192), bf
end if
next
next
case "fcolor"
state.fcolor=numarr(0)
case "bcolor"
@ -389,7 +408,7 @@ sub canvas
_putimage (drawX1, drawY1)-(drawX2, drawY2), img, 0, (srcX1, srcY1)-(srcX2, srcY2)
end if
next
_dest layers(2).ihandle: cls , 0: _dest 0
' 2.5 if the mouse is in ui thats all we need
if showtoolbox then
if _mousex >= 0 and _mousex <= 70 then
@ -421,152 +440,288 @@ sub canvas
static drawCol
if _mousebutton(1) then drawCol = state.fcolor
if _mousebutton(2) then drawCol = state.bcolor
if state.tool = 9 and (mouseclicked or rmouseclicked) then
_dest layers(1).ihandle
_source layers(1).ihandle
if mouseclicked then
floodfill canX,canY,state.fcolor
addcommand "floodfill ("+tst(canX)+","+tst(canY)+","+hex$(state.fcolor)+")"
else
floodfill canX,canY,state.bcolor
addcommand "floodfill ("+tst(canX)+","+tst(canY)+","+hex$(state.bcolor)+")"
end if
_dest 0
_source 0
exit sub
end if
if state.tool = 10 and (mouseclicked or rmouseclicked) then
_dest layers(1).ihandle
_source layers(1).ihandle
if mouseclicked then
state.fcolor=point(canX,canY)
addcommand "fcolor ("+hex$(point(canX,canY))+")"
else
state.bcolor=point(canX,canY)
addcommand "bcolor ("+hex$(point(canX,cany))+")"
end if
_dest 0
_source 0
exit sub
end if
if state.tool = 7 or state.tool = 8 then
if mouseclicked then
polypoints(pointCount * 2) = canX
polypoints(pointCount * 2 + 1) = canY
pointCount = pointCount + 1
state.isDrawing = -1
end if
else
if (mousedown or rmousedown) and state.isDrawing = 0 then
state.startX = canX
state.startY = canY
state.isDrawing = -1
end if
end if
if state.isDrawing then
_dest layers(2).ihandle
cls , 0
select case state.tool
case 1
_dest layers(1).ihandle
if canX=state.startX and canY=state.startY then
thickpixel canX,canY,drawCol
addcommand "pixel ("+tst(canX)+","+tst(canY)+","+hex$(drawCol)+")"
else
thickline state.startX, state.startY, canX, canY, drawCol
addcommand "line ("+tst(state.startX)+","+tst(state.startY)+","+tst(canX)+","+tst(canY)+","+hex$(drawCol)+")"
end if
state.startX = canX: state.startY = canY
do.pencil canX,canY,drawCol
case 2
thickline state.startX, state.startY, canX, canY, drawCol
do.line state.startX,state.startY,canX,canY,drawCol
case 3
r = sqr((canX - state.startX)^2 + (canY - state.startY)^2)
thickcircle state.startX, state.startY, r + 1, drawCol
do.circle state.startX,state.startY,sqr((canX - state.startX)^2 + (canY - state.startY)^2),drawCol
case 4
r = sqr((canX - state.startX)^2 + (canY - state.startY)^2)
filledcircle state.startX, state.startY, r, drawCol
do.fcircle state.startX,state.startY,sqr((canX - state.startX)^2 + (canY - state.startY)^2),drawCol
case 5
thickbox state.startX,state.starty,canX,canY,drawCol
do.box canX,canY
case 6
filledbox state.startX,state.startY,canX, canY, drawCol
case 7, 8
if pointCount > 0 then
for p = 1 to pointCount - 1
thickline polypoints((p - 1) * 2), polypoints((p - 1) * 2 + 1), polypoints(p * 2), polypoints(p * 2 + 1), state.fcolor
next p
thickline polypoints((pointCount - 1) * 2), polypoints((pointCount - 1) * 2 + 1), canX, canY, state.fcolor
end if
do.fbox canX,canY
case 7
do.polygon canX,canY
case 8
do.fpolygon canX,canY
case 9
do.floodfill canX,canY,drawCol
case 10
do.eyedropper canX,canY
end select
' 5. Commit Logic
dim commit as integer: commit = 0
if state.tool = 7 or state.tool = 8 then
if rmouseclicked then commit = -1
else
if (mousedown=0) and (rmousedown=0) then commit = -1
end if
if commit then
_dest layers(1).ihandle ' Final destination is always the drawing layer
if (state.tool = 8 or state.tool=7) and pointCount > 2 then
redim finalP(pointCount * 2 - 1) as long
for p = 0 to (pointCount * 2) - 1: finalP(p) = polypoints(p): next
dim tmpstr as string
if state.tool =8 then
filledPolygon finalP(), state.fcolor
tmpstr="fpolygon ("
for i=0 to ubound(finalP)-1
tmpstr=tmpstr+tst(finalP(i))+","
next i
tmpstr=tmpstr+tst(finalP(i))+","+hex$(state.fcolor)+")"
addcommand tmpstr
else
Polygon finalP(), state.fcolor
tmpstr="polygon ("
for i=0 to ubound(finalP)-1
tmpstr=tmpstr+tst(finalP(i))+","
next i
tmpstr=tmpstr+tst(finalP(i))+","+hex$(state.fcolor)+")"
addcommand tmpstr
end if
else
' Merge the preview into the drawing layer
'_putimage , layers(2).ihandle, layers(1).ihandle
select case state.tool
case 2 ' Line
if canX=state.startX and canY=state.startY then
thickpixel canX,canY,drawCol
addcommand "pixel ("+tst(canX)+","+tst(canY)+","+hex$(drawCol)+")"
else
thickline state.startX, state.startY, canX, canY, drawCol
addcommand "line ("+tst(state.startX)+","+tst(state.startY)+","+tst(canX)+","+tst(canY)+","+hex$(drawCol)+")"
end if
case 3 ' Circle
r = int(sqr((canX - state.startX)^2 + (canY - state.startY)^2))
thickcircle state.startX, state.startY, r + 1, drawCol
addcommand "circle ("+tst(state.startX)+","+tst(state.startY)+","+tst(int(r))+","+hex$(drawCol)+")"
case 4
r = int(sqr((canX - state.startX)^2 + (canY - state.startY)^2))
filledcircle state.startX, state.startY, r + 1, drawCol
addcommand "fcircle ("+tst(state.startX)+","+tst(state.startY)+","+tst(int(r))+","+hex$(drawCol)+")"
case 5 ' Box
thickbox state.startX,state.starty,canX,canY,drawCol
'line (state.startX, state.startY)-(canX, canY), drawCol, b
addcommand "box ("+tst(state.startX)+","+tst(state.startY)+","+tst(canX)+","+tst(canY)+","+hex$(drawCol)+")"
case 6 ' Filled Box
filledbox state.startX,state.startY,canX, canY, drawCol
'line (state.startX, state.startY)-(canX, canY), drawCol, bf
addcommand "fbox ("+tst(state.startX)+","+tst(state.startY)+","+tst(canX)+","+tst(canY)+","+hex$(drawCol)+")"
end select
end if
_dest layers(2).ihandle: cls , 0
state.isDrawing = 0
pointCount = 0
end if
end if
'if state.tool = 9 and (mouseclicked or rmouseclicked) then
'' _dest layers(1).ihandle
'' _source layers(1).ihandle
'' if mouseclicked then
'' floodfill canX,canY,state.fcolor
'' addcommand "floodfill ("+tst(canX)+","+tst(canY)+","+hex$(state.fcolor)+")"
'' else
'' floodfill canX,canY,state.bcolor
'' addcommand "floodfill ("+tst(canX)+","+tst(canY)+","+hex$(state.bcolor)+")"
'' end if
'' _dest 0
'' _source 0
'' exit sub
'end if
''
'if state.tool = 10 and (mouseclicked or rmouseclicked) then
'' _dest layers(1).ihandle
'' _source layers(1).ihandle
'' if mouseclicked then
'' state.fcolor=point(canX,canY)
'' addcommand "fcolor ("+hex$(point(canX,canY))+")"
'' else
'' state.bcolor=point(canX,canY)
'' addcommand "bcolor ("+hex$(point(canX,cany))+")"
'' end if
'' _dest 0
'' _source 0
'' exit sub
'end if
'if state.tool = 7 or state.tool = 8 then
'' if mouseclicked then
'' polypoints(pointCount * 2) = canX
'' polypoints(pointCount * 2 + 1) = canY
'' pointCount = pointCount + 1
'' state.isDrawing = -1
'' end if
'else
'' if (mousedown or rmousedown) and state.isDrawing = 0 then
'' state.startX = canX
'' state.startY = canY
'' state.isDrawing = -1
'' end if
'end if
'if state.isDrawing then
'' _dest layers(2).ihandle
'' cls , 0
'' select case state.tool
'' case 1
'' _dest layers(1).ihandle
'' if canX=state.startX and canY=state.startY then
'' thickpixel canX,canY,drawCol
'' addcommand "pixel ("+tst(canX)+","+tst(canY)+","+hex$(drawCol)+")"
'' else
'' thickline state.startX, state.startY, canX, canY, drawCol
'' addcommand "line ("+tst(state.startX)+","+tst(state.startY)+","+tst(canX)+","+tst(canY)+","+hex$(drawCol)+")"
'' end if
'' state.startX = canX: state.startY = canY
'' case 2
'' thickline state.startX, state.startY, canX, canY, drawCol
'' case 3
'' r = sqr((canX - state.startX)^2 + (canY - state.startY)^2)
'' thickcircle state.startX, state.startY, r + 1, drawCol
'' case 4
'' r = sqr((canX - state.startX)^2 + (canY - state.startY)^2)
'' filledcircle state.startX, state.startY, r, drawCol
'' case 5
'' thickbox state.startX,state.starty,canX,canY,drawCol
''
'' case 6
'' filledbox state.startX,state.startY,canX, canY, drawCol
'' case 7, 8
'' if pointCount > 0 then
'' for p = 1 to pointCount - 1
'' thickline polypoints((p - 1) * 2), polypoints((p - 1) * 2 + 1), polypoints(p * 2), polypoints(p * 2 + 1), state.fcolor
'' next p
'' thickline polypoints((pointCount - 1) * 2), polypoints((pointCount - 1) * 2 + 1), canX, canY, state.fcolor
'' end if
'' end select
'' ' 5. Commit Logic
'' dim commit as integer: commit = 0
'' if state.tool = 7 or state.tool = 8 then
'' if rmouseclicked then commit = -1
'' else
'' if (mousedown=0) and (rmousedown=0) then commit = -1
'' end if
'' if commit then
'' _dest layers(1).ihandle ' Final destination is always the drawing layer
'' if (state.tool = 8 or state.tool=7) and pointCount > 2 then
'' redim finalP(pointCount * 2 - 1) as long
'' for p = 0 to (pointCount * 2) - 1: finalP(p) = polypoints(p): next
'' dim tmpstr as string
'' if state.tool =8 then
'' filledPolygon finalP(), state.fcolor
'' tmpstr="fpolygon ("
'' for i=0 to ubound(finalP)-1
'' tmpstr=tmpstr+tst(finalP(i))+","
'' next i
'' tmpstr=tmpstr+tst(finalP(i))+","+hex$(state.fcolor)+")"
'' addcommand tmpstr
'' else
'' Polygon finalP(), state.fcolor
'' tmpstr="polygon ("
'' for i=0 to ubound(finalP)-1
'' tmpstr=tmpstr+tst(finalP(i))+","
'' next i
'' tmpstr=tmpstr+tst(finalP(i))+","+hex$(state.fcolor)+")"
'' addcommand tmpstr
'' end if
'' else
'' ' Merge the preview into the drawing layer
'' '_putimage , layers(2).ihandle, layers(1).ihandle
'' select case state.tool
'' case 2 ' Line
'' if canX=state.startX and canY=state.startY then
'' thickpixel canX,canY,drawCol
'' addcommand "pixel ("+tst(canX)+","+tst(canY)+","+hex$(drawCol)+")"
'' else
'' thickline state.startX, state.startY, canX, canY, drawCol
'' addcommand "line ("+tst(state.startX)+","+tst(state.startY)+","+tst(canX)+","+tst(canY)+","+hex$(drawCol)+")"
'' end if
'' case 3 ' Circle
'' r = int(sqr((canX - state.startX)^2 + (canY - state.startY)^2))
'' thickcircle state.startX, state.startY, r + 1, drawCol
'' addcommand "circle ("+tst(state.startX)+","+tst(state.startY)+","+tst(int(r))+","+hex$(drawCol)+")"
'' case 4
'' r = int(sqr((canX - state.startX)^2 + (canY - state.startY)^2))
'' filledcircle state.startX, state.startY, r + 1, drawCol
'' addcommand "fcircle ("+tst(state.startX)+","+tst(state.startY)+","+tst(int(r))+","+hex$(drawCol)+")"
'' case 5 ' Box
'' thickbox state.startX,state.starty,canX,canY,drawCol
'' 'line (state.startX, state.startY)-(canX, canY), drawCol, b
'' addcommand "box ("+tst(state.startX)+","+tst(state.startY)+","+tst(canX)+","+tst(canY)+","+hex$(drawCol)+")"
'' case 6 ' Filled Box
'' filledbox state.startX,state.startY,canX, canY, drawCol
'' 'line (state.startX, state.startY)-(canX, canY), drawCol, bf
'' addcommand "fbox ("+tst(state.startX)+","+tst(state.startY)+","+tst(canX)+","+tst(canY)+","+hex$(drawCol)+")"
'' end select
'' end if
'' _dest layers(2).ihandle: cls , 0
'' state.isDrawing = 0
'' pointCount = 0
'' end if
'end if
''
_dest 0
end sub
sub do.pencil(x as long,y as long, col as long)
dim osource as long
dim odest as long
osource = _source
odest = _dest
_dest layers(1).ihandle
if _mousebutton(1) or _mousebutton(2) then
thickpixel x,y,col
addcommand "pixel ("+tst(x)+","+tst(y)+","+hex$(col)+")"
end if
state.isdrawing=0
_dest odest
end sub
sub do.line(sx as long,sy as long,ex as long,ey as long,col as long)
dim osource as long
dim odest as long
osource = _source
odest = _dest
end sub
sub do.circle (x as long,y as long,r as long,col as long)
dim osource as long
dim odest as long
if state.isdrawing then
osource = _source
odest = _dest
if mouseclicked or rmouseclicked then
_dest layers(1).ihandle
addcommand "circle ("+tst(x)+","+tst(y)+","+tst(int(r))+","+hex$(col)+")"
state.isdrawing=0
else
_dest layers(2).ihandle
end if
thickcircle x, y, r, col
_source osource
_dest odest
end if
end sub
sub do.fcircle (x as long,y as long,r as long,col as long)
dim osource as long
dim odest as long
osource = _source
odest = _dest
end sub
sub do.box(x,y)
dim osource as long
dim odest as long
osource = _source
odest = _dest
end sub
sub do.fbox(x,y)
dim osource as long
dim odest as long
osource = _source
odest = _dest
end sub
sub do.polygon(x,y)
dim osource as long
dim odest as long
osource = _source
odest = _dest
end sub
sub do.fpolygon(x,y)
dim osource as long
dim odest as long
osource = _source
odest = _dest
end sub
sub do.floodfill(x as long,y as long, col as long)
dim osource as long
dim odest as long
osource = _source
odest = _dest
if mouseclicked or rmouseclicked then
_source layers(1).ihandle
_dest layers(1).ihandle
floodfill x,y,col
addcommand "floodfill ("+tst(x)+","+tst(y)+","+hex$(col)+")"
_source osource
_dest odest
end if
end sub
sub do.eyedropper(x,y)
dim osource as long
dim odest as long
osource = _source
odest = _dest
_source layers(1).ihandle
end sub
function icon (index as long)
static init as integer
static icons() as long
@ -747,7 +902,18 @@ end select
done=-1
end if
if link(10,78,"exit") then system
if link(10,78,"refenece img") then
filename=textinput(10,78,100,23,"")
if filename="" then exit sub
if not _fileexists(filename) then exit sub
if layers(3).ihandle <> 0 then _freeimage layers(3).ihandle
layers(3).ihandle=_loadimage(filename)
_setalpha 20, layers(3).ihandle
done=-1
end if
if link(10,100,"exit") then system
if k$=chr$(27) then done=-1
_limit 30
_display