polygone
This commit is contained in:
parent
b4678feada
commit
b04aaa7019
1 changed files with 56 additions and 36 deletions
92
pixler.bas
92
pixler.bas
|
|
@ -151,11 +151,14 @@ sub canvas
|
|||
dim boxX1 as integer: boxX1 = 70
|
||||
dim boxWidth as integer: boxWidth = _width - 1 - boxX1
|
||||
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
|
||||
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
|
||||
for i = 0 to ubound(layers)
|
||||
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
|
||||
next
|
||||
|
||||
' Translate Mouse to Canvas Space
|
||||
' Translate Mouse to Canvas Space
|
||||
dim canX as integer: canX = (_mousex - state.offsetX) / 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
|
||||
if mousedown or mouseclicked then drawCol = state.fcolor else drawCol = state.bcolor
|
||||
drawCol = state.fcolor
|
||||
|
||||
' Interaction Boundary Check
|
||||
if _mousex > boxX1 then
|
||||
' On Initial Click: Set the anchor point for shapes
|
||||
if (mousedown or rmousedown) and state.isDrawing = 0 then
|
||||
state.startX = canX
|
||||
state.startY = canY
|
||||
state.isDrawing = -1
|
||||
' Handle First Point / Adding Points
|
||||
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.startY = canY
|
||||
state.isDrawing = -1
|
||||
end if
|
||||
end if
|
||||
|
||||
if state.isDrawing then
|
||||
' preview layer handles the "rubber-banding" of shapes
|
||||
_dest layers(2).ihandle
|
||||
cls , 0
|
||||
|
||||
select case state.tool
|
||||
case 1 ' Pencil (Direct to Canvas)
|
||||
case 1 ' Pencil
|
||||
_dest layers(1).ihandle
|
||||
thickline state.startX, state.startY, canX, canY, drawCol
|
||||
state.startX = canX: state.startY = canY
|
||||
|
||||
case 2 ' Straight Line
|
||||
case 2 ' Line
|
||||
thickline state.startX, state.startY, canX, canY, drawCol
|
||||
|
||||
case 3 ' Thick Circle (Outline)
|
||||
case 3 ' Circle
|
||||
dim r as single: r = sqr((canX - state.startX)^2 + (canY - state.startY)^2)
|
||||
r=abs(r)+1
|
||||
thickcircle state.startX, state.startY, r, drawCol
|
||||
|
||||
thickcircle state.startX, state.startY, r + 1, drawCol
|
||||
case 4 ' Filled Circle
|
||||
dim r_fill as single: r_fill = sqr((canX - state.startX)^2 + (canY - state.startY)^2)
|
||||
filledcircle state.startX, state.startY, r_fill, drawCol
|
||||
|
||||
case 5 ' Rectangle Outline (using thickline)
|
||||
case 5 ' Rect
|
||||
thickline state.startX, state.startY, canX, state.startY, drawCol
|
||||
thickline canX, state.startY, canX, canY, drawCol
|
||||
thickline canX, canY, state.startX, canY, drawCol
|
||||
thickline state.startX, canY, state.startX, state.startY, drawCol
|
||||
|
||||
case 6 ' Filled Rectangle
|
||||
case 6 ' Filled Rect
|
||||
line (state.startX, state.startY)-(canX, canY), drawCol, bf
|
||||
|
||||
case 7
|
||||
|
||||
case 8 ' Polygon Outline (using thickline)
|
||||
' Simple preview of a line; complex polygons usually require
|
||||
' a point-collection state machine.
|
||||
thickline state.startX, state.startY, canX, canY, drawCol
|
||||
|
||||
|
||||
case 7, 8 ' Polygon Outline and Filled
|
||||
if pointCount > 0 then
|
||||
' Draw existing segments
|
||||
for p = 1 to pointCount - 1
|
||||
thickline polypoints((p - 1) * 2), polypoints((p - 1) * 2 + 1), polypoints(p * 2), polypoints(p * 2 + 1), 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
|
||||
|
||||
' Release Logic: Commit preview to permanent layer
|
||||
if _mousebutton(1) = 0 and _mousebutton(2) = 0 then
|
||||
' Release/Commit Logic
|
||||
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
|
||||
_putimage , layers(2).ihandle
|
||||
_dest layers(2).ihandle
|
||||
cls , 0
|
||||
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
|
||||
end if
|
||||
|
||||
_dest layers(2).ihandle: cls , 0
|
||||
state.isDrawing = 0
|
||||
pointCount = 0 ' Reset polygon points
|
||||
end if
|
||||
_dest 0
|
||||
end if
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue