beginnings of support for multiple shapes
[?]
May 14, 2022, 8:08 PM
JCSLDGAH2F6AIY4Z6XM6K4LOMW7EFY3E4NF5YXLMHLTYTX3A4Z3QCDependencies
- [2]
KCIM5UTVrevert: back to freehand - [3]
HRWN5V6JDevine's suggestion to try to live with just freehand - [4]
EFMLTMZGbugfix: restrict strokes to the drawing they started in - [5]
TRCAEE2Aclip drawings inside the border - [6]
Z2CJVAPVlighter border for figures - [7]
XX7G2FFJintermingle freehand line drawings with text - [8]
MNWHXPBLmore lightweight; select just the stroke at the mouse - [9]
D2GCFTTTclean up repl functionality - [10]
3XD6M3CFrefactor - [11]
O2UFJ6G3switch from freehand to just straight lines - [12]
G77XIN7Mselecting a stroke - [13]
T76KKDWZturn strokes into horizontal and vertical lines - [14]
JVRL5TWLstore device-independent coordinates inside drawings - [15]
OTIBCAUJlove2d scaffold - [16]
6LJZN727handle chords - [17]
4NDYV4WDfix 2 bugs in line selection - [18]
WAZVXUV2simplest possible way to straighten strokes - [*]
2C7CTIQYmake space for multiple kinds of width
Change contents
- edit in main.lua at line 6
-- lines is an array of lines-- a line is either:-- a string containing text-- or a drawing-- a drawing is a table with:-- a (y) coord in pixels,-- a (h)eight,-- an array of points, and-- an array of shapes-- a shape is a table containing:-- a mode-- an array points for mode 'freehand' (raw x,y coords; freehand drawings don't pollute the points array of a drawing)-- an array vertices for mode 'polygon', 'rectangle', 'square'-- p1, p2 for mode 'line'-- p1, p2, arrow-mode for mode 'arrow-line'-- cx,cy, r for mode 'circle'-- pc, r for mode 'circle'-- pc, r, s, e for mode 'arc'-- Unless otherwise specified, coord fields are normalized; a drawing is always 256 units wide-- The field names are carefully chosen so that switching modes in midstream-- remembers previously entered points where that makes sense.---- Open question: how to maintain Sketchpad-style constraints? Answer for now:-- we don't. Constraints operate only for the duration of a drawing operation.-- We'll continue to persist them just to keep the option open to continue-- solving for them. But for now, this is a program to create static drawings-- once, and read them passively thereafter. - edit in main.lua at line 34[4.14][20.3]
- edit in main.lua at line 36[20.45][4.3]
current_mode = 'freehand' - replacement in main.lua at line 90
if on_freehand(mx,my, shape) thenif on_shape(mx,my, shape) then - edit in main.lua at line 94
endprev = nilfor _,point in ipairs(shape) doif prev thenlove.graphics.line(pixels(prev.x)+12,pixels(prev.y)+line.y, pixels(point.x)+12,pixels(point.y)+line.y)endprev = point - edit in main.lua at line 95
draw_shape(12,line.y, shape) - replacement in main.lua at line 97[4.1053]→[2.293:522](∅→∅),[4.313]→[4.1230:1240](∅→∅),[2.522]→[4.1230:1240](∅→∅),[4.1230]→[4.1230:1240](∅→∅)
prev = nilfor _,point in ipairs(line.pending) doif prev thenlove.graphics.line(pixels(prev.x)+12,pixels(prev.y)+line.y, pixels(point.x)+12,pixels(point.y)+line.y)endprev = pointenddraw_pending_shape(12,line.y, line.pending) - replacement in main.lua at line 118
table.insert(drawing.pending, {x=coord(love.mouse.getX()-12), y=coord(love.mouse.getY()-drawing.y)})if drawing.pending.mode == 'freehand' thentable.insert(drawing.pending.points, {x=coord(love.mouse.getX()-12), y=coord(love.mouse.getY()-drawing.y)})end - edit in main.lua at line 132
function love.mousereleased(x,y, button)if lines.current thenif lines.current.pending thenif lines.current.pending.mode == 'freehand' then-- the last point added during update is good enoughelseif lines.current.pending.mode == 'line' thenlines.current.pending.x2 = coord(x-12)lines.current.pending.y2 = coord(y-lines.current.y)endtable.insert(lines.current.shapes, lines.current.pending)lines.current.pending = {}lines.current = nilendendend - edit in main.lua at line 153
if current_mode == 'freehand' thendrawing.pending = {mode='freehand', points={x=coord(x-12), y=coord(y-drawing.y)}}elseif current_mode == 'line' thendrawing.pending = {mode='line', x1=coord(x-12), y1=coord(y-drawing.y)}end - edit in main.lua at line 164
function draw_shape(left,top, shape)if shape.mode == 'freehand' thenlocal prev = nilfor _,point in ipairs(shape.points) doif prev thenlove.graphics.line(pixels(prev.x)+left,pixels(prev.y)+top, pixels(point.x)+left,pixels(point.y)+top)endprev = pointendelseif shape.mode == 'line' thenlove.graphics.line(pixels(shape.x1)+left,pixels(shape.y1)+top, pixels(shape.x2)+left,pixels(shape.y2)+top)endendfunction draw_pending_shape(left,top, shape)if shape.mode == 'freehand' thendraw_shape(left,top, shape)elseif shape.mode == 'line' thenlove.graphics.line(pixels(line.pending.x1)+left,pixels(line.pending.y1)+top, love.mouse.getX(),love.mouse.getY())endendfunction on_shape(x,y, shape)if shape.mode == 'freehand' thenreturn on_freehand(x,y, shape)elseif shape.mode == 'line' thenreturn on_line(x,y, shape)elseassert(false)endend - replacement in main.lua at line 198
for _,p in ipairs(shape) dofor _,p in ipairs(shape.points) do - edit in main.lua at line 231
function love.mousereleased(x,y, button)if lines.current thenif lines.current.pending thentable.insert(lines.current.shapes, lines.current.pending)lines.current.pending = {}lines.current = nilendendend - replacement in main.lua at line 256
convert_line(drawing,i,shape)convert_line(shape) - replacement in main.lua at line 261
convert_horvert(drawing,i,shape)convert_horvert(shape) - replacement in main.lua at line 278
if on_freehand(mx,my, shape) thenif on_shape(mx,my, shape) then - replacement in main.lua at line 287
function convert_line(drawing, i, shape)function convert_line(shape) - replacement in main.lua at line 292
drawing.shapes[i] = {shape[1], shape[#shape]}assert(shape.mode == 'freehand')shape.mode = 'line'shape.x1 = shape.points[1].xshape.y1 = shape.points[1].yshape.x2 = shape.points[#shape.points].xshape.y2 = shape.points[#shape.points].y - replacement in main.lua at line 300
-- turn a stroke into either a horizontal or vertical linefunction convert_horvert(drawing, i, shape)local x1,y1 = shape[1].x, shape[1].ylocal x2,y2 = shape[#shape].x, shape[#shape].yif math.abs(x1-x2) > math.abs(y1-y2) thendrawing.shapes[i] = {{x=x1, y=y1}, {x=x2, y=y1}}-- turn a line either horizontal or verticalfunction convert_horvert(shape)if shape.mode == 'freehand' thenconvert_line(shape)endassert(shape.mode == 'line')if math.abs(shape.x1-shape.x2) > math.abs(shape.y1-shape.y2) thenshape.y2 = shape.y1 - replacement in main.lua at line 309
drawing.shapes[i] = {{x=x1, y=y1}, {x=x1, y=y2}}shape.x2 = shape.x1 - edit in main.lua at line 314
assert(shape.mode == 'freehand') - replacement in main.lua at line 316
for i=2,#shape-1 dolocal a = shape[i-1]local b = shape[i]local c = shape[i+1]for i=2,#shape.points-1 dolocal a = shape.points[i-1]local b = shape.points[i]local c = shape.points[i+1]