new mode for polygons
[?]
Jun 15, 2022, 6:49 PM
DDXXXXBRUH4UHHPOLAK2IG45C4QAPGSR4MTLV3MVFV3EMND43DIACDependencies
- [2]
IHHNPJ5Qmove - [3]
SRVDX4I5local var - [4]
6DE7RBZ6move mouse_released events to Drawing - [5]
WTDKUACNrectangle and square shapes - [6]
W7JMT5V2rip out most support for polygons - [7]
NYQ7HD4Dmove - [8]
DRFE3B3Zmouse buttons are integers, not strings - [9]
XRLJDW3Wcasting about for more helpers to extract.. - [10]
XNFTJHC4split keyboard handling between Text and Drawing - [11]
LAW2O3NWextract variable Margin_left - [*]
OTIBCAUJlove2d scaffold - [*]
VHQCNMARseveral more modules - [*]
UN7L3DNNavoid some string concatenations - [*]
QYIFOHW3first test! - [*]
K2X6G75Zstart writing some tests for drawings - [*]
CRYGI3LRmore drawing tests - [*]
OWK3U6VDtests for drawing polygons - [*]
BLWAYPKVextract a module - [*]
HYEAFRZ2split mouse_pressed events between Text and Drawing
Change contents
- edit in main.lua at line 64
Current_drawing_submode = nil - edit in geom.lua at line 30
elseif shape.mode == 'polygon' then-- TODOreturn false - edit in geom.lua at line 170
endfunction geom.rotate(cx,cy, x,y, theta)local x2,y2 = x-cx,y-cylocal x3 = x2*math.cos(theta) - y2*math.sin(theta)local y3 = x2*math.sin(theta) + y2*math.cos(theta)return cx+x3,cy+y3 - edit in file.lua at line 66
elseif shape.mode == 'polygon' thenlocal name = shape.center.nameshape.center = Drawing.insert_point(drawing.points, shape.center.x,shape.center.y)drawing.points[shape.center].name = namelocal name = shape.p1.nameshape.p1 = Drawing.insert_point(drawing.points, shape.p1.x,shape.p1.y)drawing.points[shape.p1].name = name - edit in file.lua at line 103
elseif shape.mode == 'polygon' thenoutfile:write(json.encode({mode=shape.mode, num_vertices=shape.num_vertices, center=drawing.points[shape.center], p1=drawing.points[shape.p1]}), '\n') - edit in file.lua at line 168
elseif shape.mode == 'polygon' thenlocal name = shape.center.nameshape.center = Drawing.insert_point(drawing.points, shape.center.x,shape.center.y)drawing.points[shape.center].name = namelocal name = shape.p1.nameshape.p1 = Drawing.insert_point(drawing.points, shape.p1.x,shape.p1.y)drawing.points[shape.p1].name = name - edit in drawing_tests.lua at line 188
endfunction test_draw_polygon()io.write('\ntest_draw_polygon')-- display a drawing followed by a line of text (you shouldn't ever have a drawing right at the end)App.screen.init{width=Margin_left+300, height=300}Lines = load_array{'```lines', '```', ''}Line_width = 256 -- drawing coordinates 1:1 with pixelsApp.draw()check_eq(Current_drawing_mode, 'line', 'F - test_draw_polygon/baseline/drawing_mode')check_eq(#Lines, 2, 'F - test_draw_polygon/baseline/#lines')check_eq(Lines[1].mode, 'drawing', 'F - test_draw_polygon/baseline/mode')check_eq(Lines[1].y, Margin_top+Drawing_padding_top, 'F - test_draw_polygon/baseline/y')check_eq(Lines[1].h, 128, 'F - test_draw_polygon/baseline/y')check_eq(#Lines[1].shapes, 0, 'F - test_draw_polygon/baseline/#shapes')-- start a strokeApp.run_after_mouse_press(Margin_left+35, Margin_top+Drawing_padding_top+36, 1)-- switch to polygon modeApp.run_after_keychord('5')App.run_after_mouse_release(Margin_left+55, Margin_top+Drawing_padding_top+26, 1)local drawing = Lines[1]check_eq(#drawing.shapes, 1, 'F - test_draw_polygon/#shapes')check_eq(#drawing.points, 2, 'F - test_draw_polygon/vertices')local shape = drawing.shapes[1]check_eq(shape.mode, 'polygon', 'F - test_draw_polygon/shape_mode')check_eq(shape.num_vertices, 5, 'F - test_draw_polygon/vertices') - edit in drawing.lua at line 99
elseif shape.mode == 'polygon' thenlocal center = drawing.points[shape.center]local cx,cy = Drawing.pixels(center.x)+left, Drawing.pixels(center.y)+toplocal p1 = drawing.points[shape.p1]local x,y = Drawing.pixels(p1.x)+left, Drawing.pixels(p1.y)+topDrawing.draw_polygon(shape.num_vertices, cx,cy, x,y) - edit in drawing.lua at line 144
elseif shape.mode == 'polygon' then-- TODO - edit in drawing.lua at line 158
elseif shape.mode == 'polygon' thenlocal center = drawing.points[shape.center]local mx,my = Drawing.coord(App.mouse_x()-left), Drawing.coord(App.mouse_y()-top)if mx < 0 or mx >= 256 or my < 0 or my >= drawing.h thenreturnendlocal cx,cy = Drawing.pixels(center.x)+left, Drawing.pixels(center.y)+topDrawing.draw_polygon(Current_drawing_submode, cx,cy, App.mouse_x(),App.mouse_y()) - edit in drawing.lua at line 193[21.646][3.4359]
function Drawing.draw_polygon(n, cx,cy, x1,y1)local r = geom.dist(cx,cy, x1,y1)local theta = 2*math.pi/nlocal prevx,prevy = x1,y1for i=1,n dox,y = geom.rotate(cx,cy, prevx,prevy, theta)love.graphics.line(prevx,prevy, x,y)prevx,prevy = x,yendend - edit in drawing.lua at line 218[21.1464][21.1464]
elseif Current_drawing_mode == 'polygon' thenlocal j = Drawing.insert_point(drawing.points, Drawing.coord(x-Margin_left), Drawing.coord(y-drawing.y))drawing.pending = {mode=Current_drawing_mode, num_vertices=Current_drawing_submode, center=j} - edit in drawing.lua at line 294
elseif drawing.pending.mode == 'polygon' then-- TODO - edit in drawing.lua at line 309
elseif drawing.pending.mode == 'polygon' thenlocal mx,my = Drawing.coord(x-Margin_left), Drawing.coord(y-drawing.y)drawing.pending.p1 = Drawing.insert_point(drawing.points, mx,my)table.insert(drawing.shapes, drawing.pending) - replacement in drawing.lua at line 347
elseif drawing.pending.mode == 'polygon' or drawing.pending.mode == 'rectangle' or drawing.pending.mode == 'square' thenelseif drawing.pending.mode == 'rectangle' then - replacement in drawing.lua at line 349
elseif drawing.pending.mode == 'circle' or drawing.pending.mode == 'arc' thenelseif drawing.pending.mode == 'polygon' or drawing.pending.mode == 'circle' or drawing.pending.mode == 'arc' then - replacement in drawing.lua at line 362
elseif drawing.pending.mode == 'polygon' or drawing.pending.mode == 'rectangle' or drawing.pending.mode == 'square' thenelseif drawing.pending.mode == 'rectangle' then - replacement in drawing.lua at line 364
elseif drawing.pending.mode == 'circle' or drawing.pending.mode == 'arc' thenelseif drawing.pending.mode == 'polygon' or drawing.pending.mode == 'circle' or drawing.pending.mode == 'arc' then - edit in drawing.lua at line 393
elseif chord == 'C-3' and not App.mouse_down(1) thenCurrent_drawing_mode = 'polygon'Current_drawing_submode = 3elseif chord == 'C-4' and not App.mouse_down(1) thenCurrent_drawing_mode = 'polygon'Current_drawing_submode = 4elseif chord == 'C-5' and not App.mouse_down(1) thenCurrent_drawing_mode = 'polygon'Current_drawing_submode = 5elseif chord == 'C-6' and not App.mouse_down(1) thenCurrent_drawing_mode = 'polygon'Current_drawing_submode = 6elseif chord == 'C-7' and not App.mouse_down(1) thenCurrent_drawing_mode = 'polygon'Current_drawing_submode = 7elseif chord == 'C-8' and not App.mouse_down(1) thenCurrent_drawing_mode = 'polygon'Current_drawing_submode = 8elseif chord == 'C-9' and not App.mouse_down(1) thenCurrent_drawing_mode = 'polygon'Current_drawing_submode = 9elseif chord == '3' and App.mouse_down(1) thenCurrent_drawing_mode = 'polygon'Current_drawing_submode = 3Drawing.switch_current_drawing_pending_to_polygon()elseif chord == '4' and App.mouse_down(1) thenCurrent_drawing_mode = 'polygon'Current_drawing_submode = 4Drawing.switch_current_drawing_pending_to_polygon()elseif chord == '5' and App.mouse_down(1) thenCurrent_drawing_mode = 'polygon'Current_drawing_submode = 5Drawing.switch_current_drawing_pending_to_polygon()elseif chord == '6' and App.mouse_down(1) thenCurrent_drawing_mode = 'polygon'Current_drawing_submode = 6Drawing.switch_current_drawing_pending_to_polygon()elseif chord == '7' and App.mouse_down(1) thenCurrent_drawing_mode = 'polygon'Current_drawing_submode = 7Drawing.switch_current_drawing_pending_to_polygon()elseif chord == '8' and App.mouse_down(1) thenCurrent_drawing_mode = 'polygon'Current_drawing_submode = 8Drawing.switch_current_drawing_pending_to_polygon()elseif chord == '9' and App.mouse_down(1) thenCurrent_drawing_mode = 'polygon'Current_drawing_submode = 9Drawing.switch_current_drawing_pending_to_polygon() - edit in drawing.lua at line 510
function Drawing.switch_current_drawing_pending_to_polygon()local _,drawing = Drawing.current_drawing()if drawing.pending.mode == 'freehand' thendrawing.pending.center = Drawing.insert_point(drawing.points, drawing.pending.points[1].x, drawing.pending.points[1].y)elseif drawing.pending.mode == 'line' or drawing.pending.mode == 'manhattan' thendrawing.pending.center = drawing.pending.p1elseif drawing.pending.mode == 'rectangle' thendrawing.pending.center = drawing.pending.vertices[1]elseif drawing.pending.mode == 'polygon' or drawing.pending.mode == 'circle' or drawing.pending.mode == 'arc' then-- reuse centerenddrawing.pending.mode = 'polygon'drawing.pending.num_vertices = Current_drawing_submodeend - edit in drawing.lua at line 620
elseif shape.mode == 'polygon' thenreturn shape.center == p or shape.p1 == p