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N5C3GF6AQC VPRGENLAJTF52YSY6NNZAPWQOD2TLP7DJU7DAAWGL4WFHOYAZF5QC 6VJTQKW7YJ7E3WRNSTFYULUDHHTYAE4JELX4J55LO75X4FDM3JZAC R2ASHK5CEE3PTRLS37GP4PXJ7HIGJ6UD72KKBI57UDJI7VRROQGQC 2L5MEZV344TOZLVY3432RHJFIRVXFD6O3GWLL5O4CV66BGAFTURQC MTJEVRJR5GLWUSK7HMIM4UXM6GS6O6YCRWJT3DUSU2RYMHCQNOEQC MD3W5IRAC6UQALQE4LJC52VQNDO3I3HXF3XE2XHDABXBYJBUVAXQC LNUHQOGHIOFGJXNGA3DZLYEASLYYDGLN2I3EDZY5ANASQAHCG3YQC BLWAYPKV3MLDZ4ALXLUJ25AIR6PCIL4RFYNRYLB26GFVC2KQBYBAC 34BZ5ZKNAB4XQGXOPVBZHBDYD5D3X4V6T72XZSR5LJXF4UIVSWQAC KKQKPGCIHAG2JESQAWEMCBTAKBDC5AVIQ6LCZ2ORQM2AUCFQYLSQC 2TCIWW6ZC73FHPZLO5QOBUFZCIL74U2MBYTPNXFYYFJ7IH4YAGUQC CRBLAWBOTECOU5MFURWCRBR43OO7NWIHP5LC35IMGTQKN73AUS6AC KYNGDE2CKNOKUC2XMAS5MEU6YT2C3IW5SIZLOJE64G3ERT7BSWFAC HYEAFRZ2UEKDYTAE2GDQLHEJBPQASP2NDLMXB7F6MTVK2BKOXKEAC LYN3L74WRXZI4KNNIMNLPRFQ36RAGPWNE2O5AMB42H3CSTI6QM6QC VHQCNMARPMNBSIUFLJG7HVK4QGDNPCGNVFLHS3I4IGNVSV5MRLYQC function edit.mouse_press(State, x,y, mouse_button, is_touch, presses)Drawing.mouse_press(State, line_index, x,y, mouse_button, is_touch, presses)function edit.mouse_release(State, x,y, mouse_button, is_touch, presses)Drawing.mouse_release(State, x,y, mouse_button, is_touch, presses)function edit.keychord_press(State, chord, key, scancode, is_repeat)Drawing.keychord_press(State, chord, key, scancode, is_repeat)Text.keychord_press(State, chord, key, scancode, is_repeat)endendrecord_undo_event(State, {before=before, after=snapshot(State, drawing_index)})schedule_save(State)endelseif chord == 'escape' and not App.mouse_down(1) thenfor _,line in ipairs(State.lines) doif line.mode == 'drawing' thenline.show_help = falseendendif State.selection1.line andnot State.lines.current_drawing and-- printable character created using shift key => delete selection-- (we're not creating any ctrl-shift- or alt-shift- combinations using regular/printable keys)(not App.shift_down() or utf8.len(key) == 1) andif Drawing.before thenrecord_undo_event(State, {before=Drawing.before, after=snapshot(State, State.lines.current_drawing_index)})Drawing.before = nilendif State.search_term then return endreturnif State.search_term then return end
function source.initialize(arg, unfiltered_arg)function source.resize(w,h)function source.mouse_press(x,y, mouse_button, is_touch, presses)edit.mouse_press(Editor_state, x,y, mouse_button, is_touch, presses)edit.mouse_press(Editor_state, x,y, mouse_button, is_touch, presses)log_browser.mouse_press(Log_browser_state, x,y, mouse_button, is_touch, presses)function source.mouse_release(x,y, mouse_button, is_touch, presses)return edit.mouse_release(Editor_state, x,y, mouse_button, is_touch, presses)return log_browser.mouse_release(Log_browser_state, x,y, mouse_button, is_touch, presses)function source.keychord_press(chord, key, scancode, is_repeat)return edit.keychord_press(Editor_state, chord, key, scancode, is_repeat)return log_browser.keychord_press(Log_browser_state, chord, key, scancode, is_repeat)return log_browser.key_release(Log_browser_state, key, scancode)endendendelseCursor_time = 0 -- ensure cursor is visible immediately after it moves--? print('source keychord')if Show_file_navigator thenendendelseCursor_time = 0 -- ensure cursor is visible immediately after it movesif Focus == 'edit' thenendendelseif Show_log_browser_side and Log_browser_state.left <= x and x < Log_browser_state.right then--? print('click on log_browser side')if Focus ~= 'log_browser' thenFocus = 'log_browser'returnendCursor_time = 0 -- ensure cursor is visible immediately after it moves--? print(("Window resized to width: %d and height: %d."):format(w, h))App.screen.width, App.screen.height = w, hText.redraw_all(Editor_state)Editor_state.selection1 = {} -- no support for shift drag while we're resizingif Show_log_browser_side thenEditor_state.right = App.screen.width/2 - Margin_rightelseEditor_state.right = App.screen.width-Margin_rightendLog_browser_state.left = App.screen.width/2 + Margin_rightLog_browser_state.right = App.screen.width-Margin_rightEditor_state.width = Editor_state.right-Editor_state.leftText.tweak_screen_top_and_cursor(Editor_state, Editor_state.left, Editor_state.right)--? print('end resize')endlog_new('source')
function run.initialize(arg, unfiltered_arg)function run.resize(w,h)function run.mouse_press(x,y, mouse_button, is_touch, presses)return edit.mouse_press(Editor_state, x,y, mouse_button, is_touch, presses)function run.mouse_release(x,y, mouse_button, is_touch, presses)return edit.mouse_release(Editor_state, x,y, mouse_button, is_touch, presses)function run.keychord_press(chord, key, scancode, is_repeat)return edit.keychord_press(Editor_state, chord, key, scancode, is_repeat)endCursor_time = 0 -- ensure cursor is visible immediately after it movesendCursor_time = 0 -- ensure cursor is visible immediately after it movesendCursor_time = 0 -- ensure cursor is visible immediately after it moves--? print(("Window resized to width: %d and height: %d."):format(w, h))App.screen.width, App.screen.height = w, hText.redraw_all(Editor_state)Editor_state.selection1 = {} -- no support for shift drag while we're resizingEditor_state.right = App.screen.width-Margin_rightEditor_state.width = Editor_state.right-Editor_state.leftText.tweak_screen_top_and_cursor(Editor_state, Editor_state.left, Editor_state.right)endlog_new('run')
function log_browser.mouse_press(State, x,y, mouse_button, is_touch, presses)function log_browser.mouse_release(State, x,y, mouse_button, is_touch, presses)function log_browser.keychord_press(State, chord, key, scancode, is_repeat)-- moveif chord == 'up' thenendfunction log_browser.mouse_wheel_move(State, dx,dy)if dy > 0 thenfor i=1,math.floor(dy) dolog_browser.up(State)endlocal line_index = log_browser.line_index(State, x,y)if line_index == nil then-- below lower marginreturnend-- leave some space to click without focusinglocal line = State.lines[line_index]local xleft = log_browser.left_margin(State, line)local xright = log_browser.right_margin(State, line)if x < xleft or x > xright thenreturnend-- if it's a section begin/end and the section is collapsed, expand it-- TODO: how to collapse?if line.section_begin or line.section_end then-- HACK: get section reference from next/previous linelocal new_sectionif line.section_begin thenif line_index < #State.lines thenlocal next_section_stack = State.lines[line_index+1].section_stackif next_section_stack thennew_section = next_section_stack[#next_section_stack]endendelseif line.section_end thenif line_index > 1 thenlocal previous_section_stack = State.lines[line_index-1].section_stackif previous_section_stack thennew_section = previous_section_stack[#previous_section_stack]endendendif new_section and new_section.expanded == nil thennew_section.expanded = truereturnendend-- open appropriate file in source sideif line.filename ~= Editor_state.filename thensource.switch_to_file(line.filename)end-- set cursor
function App.mousepressed(x,y, mouse_button, is_touch, presses)if run.mouse_press then run.mouse_press(x,y, mouse_button, is_touch, presses) endif source.mouse_press then source.mouse_press(x,y, mouse_button, is_touch, presses) end
if source.mouse_press then source.mouse_press(x,y, mouse_button) endfunction App.mousereleased(x,y, mouse_button, is_touch, presses)if run.mouse_release then run.mouse_release(x,y, mouse_button, is_touch, presses) endif source.mouse_release then source.mouse_release(x,y, mouse_button, is_touch, presses) end
if source.mouse_press then source.mouse_press(x,y, mouse_button, is_touch, presses) end
Text.keychord_press(State, chord, key, scancode, is_repeat)Drawing.keychord_press(State, chord, key, scancode, is_repeat)function edit.keychord_press(State, chord, key, scancode, is_repeat)Drawing.mouse_release(State, x,y, mouse_button, is_touch, presses)function edit.mouse_release(State, x,y, mouse_button, is_touch, presses)Drawing.mouse_press(State, line_index, x,y, mouse_button, is_touch, presses)function edit.mouse_press(State, x,y, mouse_button, is_touch, presses)
pmx,pmy = px(mx), py(my)local shape = drawing.pendingif shape.mode == nil then-- nothing pendingelseif shape.mode == 'freehand' thenlocal shape_copy = deepcopy(shape)Drawing.smoothen(shape_copy)Drawing.draw_shape(drawing, shape_copy, top, left,right)elseif shape.mode == 'line' thenif mx < 0 or mx >= 256 or my < 0 or my >= drawing.h thenreturnendlocal p1 = drawing.points[shape.p1]love.graphics.line(px(p1.x),py(p1.y), pmx,pmy)elseif shape.mode == 'manhattan' thenif mx < 0 or mx >= 256 or my < 0 or my >= drawing.h thenreturnendlocal p1 = drawing.points[shape.p1]if math.abs(mx-p1.x) > math.abs(my-p1.y) thenlove.graphics.line(px(p1.x),py(p1.y), pmx, py(p1.y))elselove.graphics.line(px(p1.x),py(p1.y), px(p1.x),pmy)endelseif shape.mode == 'polygon' then-- don't close the loop on a pending polygonlocal prev = nilfor _,point in ipairs(shape.vertices) dolocal curr = drawing.points[point]if prev thenlove.graphics.line(px(prev.x),py(prev.y), px(curr.x),py(curr.y))endprev = currendlove.graphics.line(px(prev.x),py(prev.y), pmx,pmy)elseif shape.mode == 'rectangle' thenlocal first = drawing.points[shape.vertices[1]]if #shape.vertices == 1 thenlove.graphics.line(px(first.x),py(first.y), pmx,pmy)returnendlocal second = drawing.points[shape.vertices[2]]local thirdx,thirdy, fourthx,fourthy = Drawing.complete_rectangle(first.x,first.y, second.x,second.y, mx,my)love.graphics.line(px(first.x),py(first.y), px(second.x),py(second.y))love.graphics.line(px(second.x),py(second.y), px(thirdx),py(thirdy))love.graphics.line(px(thirdx),py(thirdy), px(fourthx),py(fourthy))love.graphics.line(px(fourthx),py(fourthy), px(first.x),py(first.y))elseif shape.mode == 'square' thenlocal first = drawing.points[shape.vertices[1]]if #shape.vertices == 1 thenlove.graphics.line(px(first.x),py(first.y), pmx,pmy)returnendlocal second = drawing.points[shape.vertices[2]]local thirdx,thirdy, fourthx,fourthy = Drawing.complete_square(first.x,first.y, second.x,second.y, mx,my)love.graphics.line(px(first.x),py(first.y), px(second.x),py(second.y))love.graphics.line(px(second.x),py(second.y), px(thirdx),py(thirdy))love.graphics.line(px(thirdx),py(thirdy), px(fourthx),py(fourthy))love.graphics.line(px(fourthx),py(fourthy), px(first.x),py(first.y))elseif shape.mode == 'circle' thenlocal center = drawing.points[shape.center]if mx < 0 or mx >= 256 or my < 0 or my >= drawing.h thenreturnend
elseif shape.mode == 'arc' thenlocal center = drawing.points[shape.center]if mx < 0 or mx >= 256 or my < 0 or my >= drawing.h thenreturnendshape.end_angle = geom.angle_with_hint(center.x,center.y, mx,my, shape.end_angle)local cx,cy = px(center.x), py(center.y)love.graphics.arc('line', 'open', cx,cy, Drawing.pixels(shape.radius, width), shape.start_angle, shape.end_angle, 360)elseif shape.mode == 'move' then-- nothing pending; changes are immediately committedelseif shape.mode == 'name' then-- nothing pending; changes are immediately committedelse
function Drawing.mouse_press(State, drawing_index, x,y, mouse_button)
function Drawing.mouse_press(State, drawing_index, x,y, mouse_button, is_touch, presses)
if State.current_drawing_mode == 'freehand' thendrawing.pending = {mode=State.current_drawing_mode, points={{x=cx, y=cy}}}elseif State.current_drawing_mode == 'line' or State.current_drawing_mode == 'manhattan' thenlocal j = Drawing.find_or_insert_point(drawing.points, cx, cy, State.width)drawing.pending = {mode=State.current_drawing_mode, p1=j}elseif State.current_drawing_mode == 'polygon' or State.current_drawing_mode == 'rectangle' or State.current_drawing_mode == 'square' thenlocal j = Drawing.find_or_insert_point(drawing.points, cx, cy, State.width)drawing.pending = {mode=State.current_drawing_mode, vertices={j}}elseif State.current_drawing_mode == 'circle' thenlocal j = Drawing.find_or_insert_point(drawing.points, cx, cy, State.width)drawing.pending = {mode=State.current_drawing_mode, center=j}elseif State.current_drawing_mode == 'move' then
endend-- a couple of operations on drawings need to constantly check the state of the mousefunction Drawing.update(State)if State.lines.current_drawing == nil then return endlocal drawing = State.lines.current_drawing
if drawing.pending.mode == 'freehand' thentable.insert(drawing.pending.points, {x=mx, y=my})elseif drawing.pending.mode == 'move' thendrawing.pending.target_point.x = mxdrawing.pending.target_point.y = myDrawing.relax_constraints(drawing, drawing.pending.target_point_index)endendelseif State.current_drawing_mode == 'move' then
drawing.pending.target_point.x = mxdrawing.pending.target_point.y = myDrawing.relax_constraints(drawing, drawing.pending.target_point_index)endelse-- do nothingendendfunction Drawing.relax_constraints(drawing, p)for _,shape in ipairs(drawing.shapes) doif shape.mode == 'manhattan' thenif shape.p1 == p thenshape.mode = 'line'elseif shape.p2 == p thenshape.mode = 'line'endelseif shape.mode == 'rectangle' or shape.mode == 'square' thenfor _,v in ipairs(shape.vertices) doif v == p thenshape.mode = 'polygon'endendendendendif State.current_drawing_mode == 'move' thenState.current_drawing_mode = State.previous_drawing_modeState.previous_drawing_mode = nilif State.lines.current_drawing thenState.lines.current_drawing.pending = {}State.lines.current_drawing = nilendelseif State.lines.current_drawing thenlocal drawing = State.lines.current_drawing
function Drawing.mouse_release(State, x,y, mouse_button)
function Drawing.mouse_release(State, x,y, mouse_button, is_touch, presses)
if drawing.pending thenif drawing.pending.mode == nil then-- nothing pendingelseif drawing.pending.mode == 'freehand' then-- the last point added during update is good enoughDrawing.smoothen(drawing.pending)table.insert(drawing.shapes, drawing.pending)elseif drawing.pending.mode == 'line' then
if mx >= 0 and mx < 256 and my >= 0 and my < drawing.h thendrawing.pending.p2 = Drawing.find_or_insert_point(drawing.points, mx,my, State.width)table.insert(drawing.shapes, drawing.pending)endelseif drawing.pending.mode == 'manhattan' thenlocal p1 = drawing.points[drawing.pending.p1]
if mx >= 0 and mx < 256 and my >= 0 and my < drawing.h thenif math.abs(mx-p1.x) > math.abs(my-p1.y) thendrawing.pending.p2 = Drawing.find_or_insert_point(drawing.points, mx, p1.y, State.width)elsedrawing.pending.p2 = Drawing.find_or_insert_point(drawing.points, p1.x, my, State.width)endlocal p2 = drawing.points[drawing.pending.p2]
if mx >= 0 and mx < 256 and my >= 0 and my < drawing.h thentable.insert(drawing.pending.vertices, Drawing.find_or_insert_point(drawing.points, mx,my, State.width))table.insert(drawing.shapes, drawing.pending)endelseif drawing.pending.mode == 'rectangle' then
if mx >= 0 and mx < 256 and my >= 0 and my < drawing.h thenlocal first = drawing.points[drawing.pending.vertices[1]]local second = drawing.points[drawing.pending.vertices[2]]local thirdx,thirdy, fourthx,fourthy = Drawing.complete_rectangle(first.x,first.y, second.x,second.y, mx,my)table.insert(drawing.pending.vertices, Drawing.find_or_insert_point(drawing.points, thirdx,thirdy, State.width))table.insert(drawing.pending.vertices, Drawing.find_or_insert_point(drawing.points, fourthx,fourthy, State.width))table.insert(drawing.shapes, drawing.pending)endelse-- too few points; draw nothingendelseif drawing.pending.mode == 'square' then
if mx >= 0 and mx < 256 and my >= 0 and my < drawing.h thenlocal first = drawing.points[drawing.pending.vertices[1]]local second = drawing.points[drawing.pending.vertices[2]]local thirdx,thirdy, fourthx,fourthy = Drawing.complete_square(first.x,first.y, second.x,second.y, mx,my)table.insert(drawing.pending.vertices, Drawing.find_or_insert_point(drawing.points, thirdx,thirdy, State.width))table.insert(drawing.pending.vertices, Drawing.find_or_insert_point(drawing.points, fourthx,fourthy, State.width))table.insert(drawing.shapes, drawing.pending)endendelseif drawing.pending.mode == 'circle' then
if mx >= 0 and mx < 256 and my >= 0 and my < drawing.h thenlocal center = drawing.points[drawing.pending.center]drawing.pending.radius = round(geom.dist(center.x,center.y, mx,my))table.insert(drawing.shapes, drawing.pending)endelseif drawing.pending.mode == 'arc' then
if mx >= 0 and mx < 256 and my >= 0 and my < drawing.h thenlocal center = drawing.points[drawing.pending.center]drawing.pending.end_angle = geom.angle_with_hint(center.x,center.y, mx,my, drawing.pending.end_angle)table.insert(drawing.shapes, drawing.pending)endelseif drawing.pending.mode == 'name' then-- drop itelse
endState.lines.current_drawing.pending = {}State.lines.current_drawing = nilendendendif chord == 'C-p' and not App.mouse_down(1) thenState.current_drawing_mode = 'freehand'elseif App.mouse_down(1) and chord == 'l' thenState.current_drawing_mode = 'line'local _,drawing = Drawing.current_drawing(State)if drawing.pending.mode == 'freehand' thendrawing.pending.p1 = Drawing.find_or_insert_point(drawing.points, drawing.pending.points[1].x, drawing.pending.points[1].y, State.width)elseif drawing.pending.mode == 'polygon' or drawing.pending.mode == 'rectangle' or drawing.pending.mode == 'square' thendrawing.pending.p1 = drawing.pending.vertices[1]elseif drawing.pending.mode == 'circle' or drawing.pending.mode == 'arc' thendrawing.pending.p1 = drawing.pending.centerenddrawing.pending.mode = 'line'elseif chord == 'C-l' and not App.mouse_down(1) thenState.current_drawing_mode = 'line'elseif App.mouse_down(1) and chord == 'm' thenState.current_drawing_mode = 'manhattan'local drawing = Drawing.select_drawing_at_mouse(State)if drawing.pending.mode == 'freehand' thendrawing.pending.p1 = Drawing.find_or_insert_point(drawing.points, drawing.pending.points[1].x, drawing.pending.points[1].y, State.width)elseif drawing.pending.mode == 'line' then-- do nothingelseif drawing.pending.mode == 'polygon' or drawing.pending.mode == 'rectangle' or drawing.pending.mode == 'square' thendrawing.pending.p1 = drawing.pending.vertices[1]elseif drawing.pending.mode == 'circle' or drawing.pending.mode == 'arc' thendrawing.pending.p1 = drawing.pending.centerenddrawing.pending.mode = 'manhattan'elseif chord == 'C-m' and not App.mouse_down(1) thenState.current_drawing_mode = 'manhattan'elseif chord == 'C-g' and not App.mouse_down(1) thenState.current_drawing_mode = 'polygon'elseif App.mouse_down(1) and chord == 'g' thenState.current_drawing_mode = 'polygon'local _,drawing = Drawing.current_drawing(State)if drawing.pending.mode == 'freehand' thendrawing.pending.vertices = {Drawing.find_or_insert_point(drawing.points, drawing.pending.points[1].x, drawing.pending.points[1].y, State.width)}elseif drawing.pending.mode == 'line' or drawing.pending.mode == 'manhattan' thenif drawing.pending.vertices == nil thendrawing.pending.vertices = {drawing.pending.p1}endelseif drawing.pending.mode == 'rectangle' or drawing.pending.mode == 'square' then-- reuse existing verticeselseif drawing.pending.mode == 'circle' or drawing.pending.mode == 'arc' thendrawing.pending.vertices = {drawing.pending.center}enddrawing.pending.mode = 'polygon'elseif chord == 'C-r' and not App.mouse_down(1) thenState.current_drawing_mode = 'rectangle'elseif App.mouse_down(1) and chord == 'r' thenState.current_drawing_mode = 'rectangle'local _,drawing = Drawing.current_drawing(State)if drawing.pending.mode == 'freehand' thendrawing.pending.vertices = {Drawing.find_or_insert_point(drawing.points, drawing.pending.points[1].x, drawing.pending.points[1].y, State.width)}elseif drawing.pending.mode == 'line' or drawing.pending.mode == 'manhattan' thenif drawing.pending.vertices == nil thendrawing.pending.vertices = {drawing.pending.p1}endelseif drawing.pending.mode == 'polygon' or drawing.pending.mode == 'square' then-- reuse existing (1-2) verticeselseif drawing.pending.mode == 'circle' or drawing.pending.mode == 'arc' thendrawing.pending.vertices = {drawing.pending.center}enddrawing.pending.mode = 'rectangle'elseif chord == 'C-s' and not App.mouse_down(1) thenState.current_drawing_mode = 'square'elseif App.mouse_down(1) and chord == 's' thenState.current_drawing_mode = 'square'local _,drawing = Drawing.current_drawing(State)if drawing.pending.mode == 'freehand' thendrawing.pending.vertices = {Drawing.find_or_insert_point(drawing.points, drawing.pending.points[1].x, drawing.pending.points[1].y, State.width)}elseif drawing.pending.mode == 'line' or drawing.pending.mode == 'manhattan' thenif drawing.pending.vertices == nil thendrawing.pending.vertices = {drawing.pending.p1}endelseif drawing.pending.mode == 'polygon' thenwhile #drawing.pending.vertices > 2 dotable.remove(drawing.pending.vertices)endelseif drawing.pending.mode == 'rectangle' then-- reuse existing (1-2) verticeselseif drawing.pending.mode == 'circle' or drawing.pending.mode == 'arc' thendrawing.pending.vertices = {drawing.pending.center}enddrawing.pending.mode = 'square'elseif App.mouse_down(1) and chord == 'p' and State.current_drawing_mode == 'polygon' then
function Drawing.keychord_press(State, chord)
function Drawing.keychord_press(State, chord, key, scancode, is_repeat)
local j = Drawing.find_or_insert_point(drawing.points, mx,my, State.width)table.insert(drawing.pending.vertices, j)elseif App.mouse_down(1) and chord == 'p' and (State.current_drawing_mode == 'rectangle' or State.current_drawing_mode == 'square') then
local j = Drawing.find_or_insert_point(drawing.points, mx,my, State.width)while #drawing.pending.vertices >= 2 dotable.remove(drawing.pending.vertices)endtable.insert(drawing.pending.vertices, j)elseif chord == 'C-o' and not App.mouse_down(1) thenState.current_drawing_mode = 'circle'elseif App.mouse_down(1) and chord == 'a' and State.current_drawing_mode == 'circle' then
local center = drawing.points[drawing.pending.center]drawing.pending.radius = round(geom.dist(center.x,center.y, mx,my))drawing.pending.start_angle = geom.angle(center.x,center.y, mx,my)elseif App.mouse_down(1) and chord == 'o' thenState.current_drawing_mode = 'circle'local _,drawing = Drawing.current_drawing(State)if drawing.pending.mode == 'freehand' thendrawing.pending.center = Drawing.find_or_insert_point(drawing.points, drawing.pending.points[1].x, drawing.pending.points[1].y, State.width)elseif drawing.pending.mode == 'line' or drawing.pending.mode == 'manhattan' thendrawing.pending.center = drawing.pending.p1elseif drawing.pending.mode == 'polygon' or drawing.pending.mode == 'rectangle' or drawing.pending.mode == 'square' thendrawing.pending.center = drawing.pending.vertices[1]enddrawing.pending.mode = 'circle'elseif chord == 'C-u' and not App.mouse_down(1) then
if drawing thenif State.previous_drawing_mode == nil thenState.previous_drawing_mode = State.current_drawing_modeendState.current_drawing_mode = 'move'drawing.pending = {mode=State.current_drawing_mode, target_point=p, target_point_index=i}State.lines.current_drawing_index = drawing_indexState.lines.current_drawing = drawingendelseif chord == 'C-n' and not App.mouse_down(1) then
if drawing thenif State.previous_drawing_mode == nil then-- don't clobberState.previous_drawing_mode = State.current_drawing_modeendState.current_drawing_mode = 'name'p.name = ''drawing.pending = {mode=State.current_drawing_mode, target_point=point_index}State.lines.current_drawing_index = drawing_indexState.lines.current_drawing = drawingendelseif chord == 'C-d' and not App.mouse_down(1) thenlocal _,drawing,_,i,p = Drawing.select_point_at_mouse(State)if drawing thenfor _,shape in ipairs(drawing.shapes) doif Drawing.contains_point(shape, i) thenif shape.mode == 'polygon' thenlocal idx = table.find(shape.vertices, i)
table.remove(shape.vertices, idx)if #shape.vertices < 3 thenshape.mode = 'deleted'endelseshape.mode = 'deleted'endendenddrawing.points[i].deleted = trueendlocal drawing,_,_,shape = Drawing.select_shape_at_mouse(State)if drawing thenshape.mode = 'deleted'endelseif chord == 'C-h' and not App.mouse_down(1) thenlocal drawing = Drawing.select_drawing_at_mouse(State)if drawing thendrawing.show_help = trueendelseif chord == 'escape' and App.mouse_down(1) thenlocal _,drawing = Drawing.current_drawing(State)drawing.pending = {}endendfunction Drawing.complete_rectangle(firstx,firsty, secondx,secondy, x,y)if firstx == secondx thenreturn x,secondy, x,firstyendif firsty == secondy thenreturn secondx,y, firstx,yendlocal first_slope = (secondy-firsty)/(secondx-firstx)-- slope of second edge:-- -1/first_slope-- equation of line containing the second edge:-- y-secondy = -1/first_slope*(x-secondx)-- => 1/first_slope*x + y + (- secondy - secondx/first_slope) = 0-- now we want to find the point on this line that's closest to the mouse pointer.-- https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line#Line_defined_by_an_equationlocal a = 1/first_slopelocal c = -secondy - secondx/first_slopelocal thirdx = round(((x-a*y) - a*c) / (a*a + 1))local thirdy = round((a*(-x + a*y) - c) / (a*a + 1))-- slope of third edge = first_slope-- equation of line containing third edge:-- y - thirdy = first_slope*(x-thirdx)-- => -first_slope*x + y + (-thirdy + thirdx*first_slope) = 0-- now we want to find the point on this line that's closest to the first pointlocal a = -first_slopelocal c = -thirdy + thirdx*first_slopelocal fourthx = round(((firstx-a*firsty) - a*c) / (a*a + 1))local fourthy = round((a*(-firstx + a*firsty) - c) / (a*a + 1))return thirdx,thirdy, fourthx,fourthyendfunction Drawing.complete_square(firstx,firsty, secondx,secondy, x,y)-- use x,y only to decide which side of the first edge to complete the square onlocal deltax = secondx-firstxlocal deltay = secondy-firstylocal thirdx = secondx+deltaylocal thirdy = secondy-deltaxif not geom.same_side(firstx,firsty, secondx,secondy, thirdx,thirdy, x,y) thendeltax = -deltaxdeltay = -deltaythirdx = secondx+deltaythirdy = secondy-deltaxendlocal fourthx = firstx+deltaylocal fourthy = firsty-deltaxreturn thirdx,thirdy, fourthx,fourthyendfunction Drawing.current_drawing(State)local x, y = App.mouse_x(), App.mouse_y()for drawing_index,drawing in ipairs(State.lines) doif drawing.mode == 'drawing' then
return drawingendendendendfunction Drawing.contains_point(shape, p)if shape.mode == 'freehand' then-- not supportedelseif shape.mode == 'line' or shape.mode == 'manhattan' thenreturn shape.p1 == p or shape.p2 == pelseif shape.mode == 'polygon' or shape.mode == 'rectangle' or shape.mode == 'square' thenreturn table.find(shape.vertices, p)elseif shape.mode == 'circle' thenreturn shape.center == pelseif shape.mode == 'arc' thenreturn shape.center == p-- ugh, how to support angleselseif shape.mode == 'deleted' then-- already doneelse
for _=1,7 dofor i=2,#shape.points-1 dolocal a = shape.points[i-1]local b = shape.points[i]local c = shape.points[i+1]b.x = round((a.x + b.x + c.x)/3)b.y = round((a.y + b.y + c.y)/3)endendendfunction round(num)return math.floor(num+.5)endfunction Drawing.find_or_insert_point(points, x,y, width)-- check if UI would snap the two points togetherfor i,point in ipairs(points) doif Drawing.near(point, x,y, width) thenreturn iendendtable.insert(points, {x=x, y=y})return #pointsendfunction Drawing.near(point, x,y, width)local px,py = Drawing.pixels(x, width),Drawing.pixels(y, width)local cx,cy = Drawing.pixels(point.x, width), Drawing.pixels(point.y, width)return (cx-px)*(cx-px) + (cy-py)*(cy-py) < Same_point_distance*Same_point_distanceendfunction Drawing.pixels(n, width) -- parts to pixelsreturn math.floor(n*width/256)endfunction Drawing.coord(n, width) -- pixels to partsreturn math.floor(n*256/width)endfunction table.find(h, x)for k,v in pairs(h) doif v == x thenreturn kendendend
function Drawing.mouse_press(State, drawing_index, x,y, mouse_button, is_touch, presses)function Drawing.mouse_release(State, x,y, mouse_button, is_touch, presses)function Drawing.keychord_press(State, chord, key, scancode, is_repeat)