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EB5KVM4O2L5AC Z5HLXU4PJWWJJDBCK52NBD6PIRIA3TAN2BKZB5HBYFGIDBX4F5HAC MXA3RZYKUI4UF2ISY7JEF6VKX6NOPZMZH5SLLCZHRJKFIXXXDPSAC 36Z442IVPXHZ7D2QI26YLN3TDDEMDRQ2GKBYQAD6NUHQZVCCY4VAC App.screen.init{width=800, height=600}check_eq(Text.starty(Editor_state, 1), Editor_state.top+Drawing_padding_top, 'baseline/y')check_eq(Editor_state.lines[1].h, 128, 'baseline/y')check_eq(#Editor_state.lines[1].shapes, 0, 'baseline/#shapes')check_eq(Text.starty(Editor_state, 1), Editor_state.top+Drawing_padding_top, 'baseline/y')check_eq(Text.starty(Editor_state, 1), Editor_state.top+Drawing_padding_top, 'baseline/y')check_eq(Text.starty(Editor_state, 1), Editor_state.top+Drawing_padding_top, 'baseline/y')check_eq(Text.starty(Editor_state, 1), Editor_state.top+Drawing_padding_top, 'baseline/y')check_eq(Text.starty(Editor_state, 1), Editor_state.top+Drawing_padding_top, 'baseline/y')check_eq(Text.starty(Editor_state, 1), Editor_state.top+Drawing_padding_top, 'baseline/y')check_eq(Text.starty(Editor_state, 1), Editor_state.top+Drawing_padding_top, 'baseline/y')check_eq(Text.starty(Editor_state, 1), Editor_state.top+Drawing_padding_top, 'baseline/y')check_eq(Text.starty(Editor_state, 1), Editor_state.top+Drawing_padding_top, 'baseline/y')check_eq(#drawing.shapes, 1, 'baseline/#shapes')check_eq(#drawing.points, 2, 'baseline/#points')check_eq(drawing.shapes[1].mode, 'line', 'baseline/shape:1')check_eq(Editor_state.lines[1].h, 128, 'baseline/y')check_eq(#Editor_state.lines[1].shapes, 0, 'baseline/#shapes')check_eq(Editor_state.lines[1].h, 128, 'baseline/y')check_eq(#Editor_state.lines[1].shapes, 0, 'baseline/#shapes')check_eq(Editor_state.lines[1].h, 128, 'baseline/y')check_eq(#Editor_state.lines[1].shapes, 0, 'baseline/#shapes')check_eq(Editor_state.lines[1].h, 128, 'baseline/y')check_eq(#Editor_state.lines[1].shapes, 0, 'baseline/#shapes')check_eq(Editor_state.lines[1].h, 128, 'baseline/y')check_eq(#Editor_state.lines[1].shapes, 0, 'baseline/#shapes')check_eq(Editor_state.lines[1].h, 128, 'baseline/y')check_eq(#Editor_state.lines[1].shapes, 0, 'baseline/#shapes')check_eq(Editor_state.lines[1].h, 128, 'baseline/y')check_eq(#Editor_state.lines[1].shapes, 0, 'baseline/#shapes')check_eq(Editor_state.lines[1].h, 128, 'baseline/y')check_eq(#Editor_state.lines[1].shapes, 0, 'baseline/#shapes')check_eq(Editor_state.lines[1].h, 128, 'baseline/y')check_eq(#Editor_state.lines[1].shapes, 0, 'baseline/#shapes')check_nil(App.filesystem['foo'], 'early')Editor_state = edit.initialize_test_state()Editor_state.filename = 'foo'Editor_state.lines = load_array{}Text.redraw_all(Editor_state)edit.draw(Editor_state)-- click on button to create drawingedit.run_after_mouse_click(Editor_state, 8,Editor_state.top+8, 1)-- file not immediately savededit.update(Editor_state, 0.01)
local starty = Text.starty(State, line_index)if pmx < State.right and pmy > starty and pmy < starty+Drawing.pixels(line.h, State.width) thenlove.graphics.rectangle('line', State.left,starty, State.width,Drawing.pixels(line.h, State.width))icon[State.current_drawing_mode](State.right-22, starty+4)icon[State.previous_drawing_mode](State.right-22, starty+4)local my = Drawing.coord(pmy-starty, State.width)Drawing.draw_shape(line, shape, starty, State.left,State.right)local function py(y) return Drawing.pixels(y, State.width)+starty endDrawing.draw_pending_shape(line, starty, State.left,State.right)function Drawing.in_current_drawing(State, x,y, left,right)return Drawing.in_drawing(State, State.lines.current_drawing_index, x,y, left,right)endfunction Drawing.in_drawing(State, line_index, x,y, left,right)assert(State.lines[line_index].mode == 'drawing')local starty = Text.starty(State, line_index)if starty == nil then return false end -- outside current pagelocal drawing = State.lines[line_index]local width = right-leftreturn y >= starty and y < starty + Drawing.pixels(drawing.h, width) and x >= left and x < rightendlocal starty = Text.starty(State, drawing_index)local cy = Drawing.coord(y-starty, State.width)local starty = Text.starty(State, State.lines.current_drawing_index)if starty == nil thenlocal my = Drawing.coord(pmy-starty, State.width)if Drawing.in_current_drawing(State, pmx,pmy, State.left,State.right) thenif Drawing.in_current_drawing(State, pmx, pmy, State.left,State.right) thenlocal starty = Text.starty(State, State.lines.current_drawing_index)local mx,my = Drawing.coord(x-State.left, State.width), Drawing.coord(y-starty, State.width)local mx,my = Drawing.coord(x-State.left, State.width), Drawing.coord(y-starty, State.width)App.mouse_move(State.left+Drawing.pixels(p2.x, State.width), starty+Drawing.pixels(p2.y, State.width))local mx,my = Drawing.coord(x-State.left, State.width), Drawing.coord(y-starty, State.width)local mx,my = Drawing.coord(x-State.left, State.width), Drawing.coord(y-starty, State.width)local mx,my = Drawing.coord(x-State.left, State.width), Drawing.coord(y-starty, State.width)local mx,my = Drawing.coord(x-State.left, State.width), Drawing.coord(y-starty, State.width)local mx,my = Drawing.coord(x-State.left, State.width), Drawing.coord(y-starty, State.width)local drawing_index,drawing = Drawing.current_drawing(State)local starty = Text.starty(State, drawing_index)local mx,my = Drawing.coord(App.mouse_x()-State.left, State.width), Drawing.coord(App.mouse_y()-starty, State.width)local drawing_index,drawing = Drawing.current_drawing(State)local starty = Text.starty(State, drawing_index)local mx,my = Drawing.coord(App.mouse_x()-State.left, State.width), Drawing.coord(App.mouse_y()-starty, State.width)local drawing_index,drawing = Drawing.current_drawing(State)local starty = Text.starty(State, drawing_index)local mx,my = Drawing.coord(App.mouse_x()-State.left, State.width), Drawing.coord(App.mouse_y()-starty, State.width)local drawing_index,drawing,_,i,p = Drawing.select_point_at_mouse(State)local drawing_index,drawing,_,point_index,p = Drawing.select_point_at_mouse(State)if Drawing.in_drawing(State, drawing_index, x,y, State.left,State.right) thenreturn drawing_index,drawinglocal starty = Text.starty(State, drawing_index)if Drawing.in_drawing(State, drawing_index, x,y, State.left,State.right) thenlocal mx,my = Drawing.coord(x-State.left, State.width), Drawing.coord(y-starty, State.width)return drawing,starty,i,shapelocal starty = Text.starty(State, drawing_index)if Drawing.in_drawing(State, drawing_index, x,y, State.left,State.right) thenlocal mx,my = Drawing.coord(x-State.left, State.width), Drawing.coord(y-starty, State.width)return drawing_index,drawing,starty,i,pointif Drawing.in_drawing(State, drawing_index, x,y, State.left,State.right) thenreturn 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 doneelseendendendendendendfunction Drawing.select_drawing_at_mouse(State)for drawing_index,drawing in ipairs(State.lines) doif drawing.mode == 'drawing' thenlocal x, y = App.mouse_x(), App.mouse_y()for i,point in ipairs(drawing.points) doendendendendendendfunction Drawing.select_point_at_mouse(State)for drawing_index,drawing in ipairs(State.lines) doif drawing.mode == 'drawing' thenlocal x, y = App.mouse_x(), App.mouse_y()for i,shape in ipairs(drawing.shapes) doendendendreturn nilendfunction Drawing.select_shape_at_mouse(State)for drawing_index,drawing in ipairs(State.lines) doif drawing.mode == 'drawing' thenlocal x, y = App.mouse_x(), App.mouse_y()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)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) thenlocal 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) thendrawing.pending.mode = 'arc'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' thenlocal 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') thenfunction Drawing.keychord_press(State, chord)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 itelseif 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' thenif 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' thenif 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' thenif 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' thentable.insert(drawing.shapes, drawing.pending)endelseif drawing.pending.mode == 'polygon' thenif 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 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 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' thenfunction Drawing.mouse_release(State, x,y, mouse_button)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 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' thenif App.mouse_down(1) then-- some event cleared starty just this frame-- draw in this frame will soon set starty-- just skip this framereturnend-- all the action is in mouse_releaseif 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' thenlocal cx = Drawing.coord(x-State.left, State.width)function Drawing.mouse_press(State, drawing_index, x,y, mouse_button)endlocal width = right-left-- after mouse_releaseendfunction Drawing.draw_shape(drawing, shape, top, left,right)local width = right-leftlocal function px(x) return Drawing.pixels(x, width)+left endlocal function py(y) return Drawing.pixels(y, width)+top endif shape.mode == 'freehand' thenlocal prev = nilfor _,point in ipairs(shape.points) doif prev thenlove.graphics.line(px(prev.x),py(prev.y), px(point.x),py(point.y))endprev = pointendelseif shape.mode == 'line' or shape.mode == 'manhattan' thenlocal p1 = drawing.points[shape.p1]local p2 = drawing.points[shape.p2]love.graphics.line(px(p1.x),py(p1.y), px(p2.x),py(p2.y))elseif shape.mode == 'polygon' or shape.mode == 'rectangle' or shape.mode == 'square' thenlocal 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 = currend-- close the looplocal curr = drawing.points[shape.vertices[1]]love.graphics.line(px(prev.x),py(prev.y), px(curr.x),py(curr.y))elseif shape.mode == 'circle' then-- TODO: cliplocal center = drawing.points[shape.center]love.graphics.circle('line', px(center.x),py(center.y), Drawing.pixels(shape.radius, width))elseif shape.mode == 'arc' thenlocal center = drawing.points[shape.center]love.graphics.arc('line', 'open', px(center.x),py(center.y), Drawing.pixels(shape.radius, width), shape.start_angle, shape.end_angle, 360)elseif shape.mode == 'deleted' then-- ignoreelsefor i,p in ipairs(line.points) doif p.deleted == nil thenif Drawing.near(p, mx,my, State.width) thenApp.color(Focus_stroke_color)love.graphics.circle('line', px(p.x),py(p.y), Same_point_distance)elseApp.color(Stroke_color)love.graphics.circle('fill', px(p.x),py(p.y), 2)endif p.name then-- TODO: cliplocal x,y = px(p.x)+5, py(p.y)+5love.graphics.print(p.name, x,y)if State.current_drawing_mode == 'name' and i == line.pending.target_point then-- create a faint red box for the nameApp.color(Current_name_background_color)endlocal function px(x) return Drawing.pixels(x, State.width)+State.left endfor _,shape in ipairs(line.shapes) doendif App.mouse_down(1) and love.keyboard.isDown('h') thendraw_help_with_mouse_pressed(State, line_index)returnendendif line.show_help thendraw_help_without_mouse_pressed(State, line_index)returnendlocal mx = Drawing.coord(pmx-State.left, State.width)elseif icon[State.current_drawing_mode] thenApp.color(Icon_color)
local starty = Text.starty(State, drawing_index)local y = starty+10love.graphics.rectangle('fill', State.left,starty, State.width, math.max(Drawing.pixels(drawing.h, State.width),y-starty))local starty = Text.starty(State, drawing_index)local y = starty+10love.graphics.rectangle('fill', State.left,starty, State.width, math.max(Drawing.pixels(drawing.h, State.width),y-starty))endendfunction current_shape(State, shape)if State.current_drawing_mode == 'freehand' thenreturn 'freehand stroke'elseif State.current_drawing_mode == 'line' thenreturn 'straight line'elseif State.current_drawing_mode == 'manhattan' thenreturn 'horizontal/vertical line'elseif State.current_drawing_mode == 'circle' and shape and shape.start_angle thenreturn 'arc'elsereturn State.current_drawing_modeendlove.graphics.print("You're currently drawing a "..current_shape(State, drawing.pending), State.left+30,y)y = y + State.line_heightlove.graphics.print('Things you can do now:', State.left+30,y)y = y + State.line_heightif State.current_drawing_mode == 'freehand' thenlove.graphics.print('* Release the mouse button to finish drawing the stroke', State.left+30,y)y = y + State.line_heightelseif State.current_drawing_mode == 'line' or State.current_drawing_mode == 'manhattan' thenlove.graphics.print('* Release the mouse button to finish drawing the line', State.left+30,y)y = y + State.line_heightelseif State.current_drawing_mode == 'circle' thenif drawing.pending.mode == 'circle' thenlove.graphics.print('* Release the mouse button to finish drawing the circle', State.left+30,y)y = y + State.line_heightlove.graphics.print("* Press 'a' to draw just an arc of a circle", State.left+30,y)elselove.graphics.print('* Release the mouse button to finish drawing the arc', State.left+30,y)endy = y + State.line_heightelseif State.current_drawing_mode == 'polygon' thenlove.graphics.print('* Release the mouse button to finish drawing the polygon', State.left+30,y)y = y + State.line_heightlove.graphics.print("* Press 'p' to add a vertex to the polygon", State.left+30,y)y = y + State.line_heightelseif State.current_drawing_mode == 'rectangle' thenif #drawing.pending.vertices < 2 thenlove.graphics.print("* Press 'p' to add a vertex to the rectangle", State.left+30,y)y = y + State.line_heightelselove.graphics.print('* Release the mouse button to finish drawing the rectangle', State.left+30,y)y = y + State.line_heightlove.graphics.print("* Press 'p' to replace the second vertex of the rectangle", State.left+30,y)y = y + State.line_heightendelseif State.current_drawing_mode == 'square' thenif #drawing.pending.vertices < 2 thenlove.graphics.print("* Press 'p' to add a vertex to the square", State.left+30,y)y = y + State.line_heightelselove.graphics.print('* Release the mouse button to finish drawing the square', State.left+30,y)y = y + State.line_heightlove.graphics.print("* Press 'p' to replace the second vertex of the square", State.left+30,y)y = y + State.line_heightendendlove.graphics.print("* Press 'esc' then release the mouse button to cancel the current shape", State.left+30,y)y = y + State.line_heighty = y + State.line_heightif State.current_drawing_mode ~= 'line' thenlove.graphics.print("* Press 'l' to switch to drawing lines", State.left+30,y)y = y + State.line_heightendif State.current_drawing_mode ~= 'manhattan' thenlove.graphics.print("* Press 'm' to switch to drawing horizontal/vertical lines", State.left+30,y)y = y + State.line_heightendif State.current_drawing_mode ~= 'circle' thenlove.graphics.print("* Press 'o' to switch to drawing circles/arcs", State.left+30,y)y = y + State.line_heightendif State.current_drawing_mode ~= 'polygon' thenlove.graphics.print("* Press 'g' to switch to drawing polygons", State.left+30,y)y = y + State.line_heightendif State.current_drawing_mode ~= 'rectangle' thenlove.graphics.print("* Press 'r' to switch to drawing rectangles", State.left+30,y)y = y + State.line_heightendif State.current_drawing_mode ~= 'square' thenlove.graphics.print("* Press 's' to switch to drawing squares", State.left+30,y)y = y + State.line_heightendApp.color(Help_background_color)App.color(Help_color)endfunction draw_help_with_mouse_pressed(State, drawing_index)local drawing = State.lines[drawing_index]love.graphics.print("Things you can do:", State.left+30,y)y = y + State.line_heightlove.graphics.print("* Press the mouse button to start drawing a "..current_shape(State), State.left+30,y)y = y + State.line_heightlove.graphics.print("* Hover on a point and press 'ctrl+u' to pick it up and start moving it,", State.left+30,y)y = y + State.line_heightApp.color(Help_color)
if y < State.top thenlocal screen_bottom1 = Text.screen_bottom1(State)return screen_bottom1.line, Text.pos_at_end_of_screen_line(State, screen_bottom1)endfunction Text.cut_selection(State)if State.selection1.line == nil then return endlocal result = Text.selection(State)Text.delete_selection(State)return resultendfunction Text.delete_selection(State)if State.selection1.line == nil then return endlocal minl,maxl = minmax(State.selection1.line, State.cursor1.line)local before = snapshot(State, minl, maxl)Text.delete_selection_without_undo(State)record_undo_event(State, {before=before, after=snapshot(State, State.cursor1.line)})endfunction Text.delete_selection_without_undo(State)if State.selection1.line == nil then return end-- min,max = sorted(State.selection1,State.cursor1)local minl,minp = State.selection1.line,State.selection1.poslocal maxl,maxp = State.cursor1.line,State.cursor1.posif minl > maxl thenminl,maxl = maxl,minlminp,maxp = maxp,minpelseif minl == maxl thenif minp > maxp thenminp,maxp = maxp,minpendend-- update State.cursor1 and State.selection1State.cursor1.line = minlState.cursor1.pos = minpif Text.lt1(State.cursor1, State.screen_top1) thenState.screen_top1.line = State.cursor1.lineState.screen_top1.pos = Text.pos_at_start_of_screen_line(State, State.cursor1)endState.selection1 = {}-- delete everything between min (inclusive) and max (exclusive)Text.clear_screen_line_cache(State, minl)local min_offset = Text.offset(State.lines[minl].data, minp)local max_offset = Text.offset(State.lines[maxl].data, maxp)if minl == maxl then--? print('minl == maxl')State.lines[minl].data = State.lines[minl].data:sub(1, min_offset-1)..State.lines[minl].data:sub(max_offset)returnendreturn State.screen_top1.line, State.screen_top1.pos
App.screen.init{width=800, height=600}edit.run_after_keychord(Editor_state, 'M-right', 'right')edit.run_after_keychord(Editor_state, 'M-right', 'right')edit.draw(Editor_state) -- populate line_cache.startpos for each lineedit.run_after_keychord(Editor_state, 'S-right', 'right')edit.run_after_keychord(Editor_state, 'right', 'right')edit.draw(Editor_state) -- populate line_cache.startpos for each lineedit.draw(Editor_state) -- populate line_cache.startpos for each lineedit.draw(Editor_state) -- populate line_cache.startpos for each lineedit.draw(Editor_state) -- populate line_cache.startpos for each lineedit.run_after_keychord(Editor_state, 'right', 'right')check_eq(Editor_state.screen_top1.line, 2, 'screen_top')check_eq(Editor_state.cursor1.line, 3, 'cursor:line')check_eq(Editor_state.cursor1.pos, 6, 'cursor:pos')-- click on first locationedit.run_after_mouse_press(Editor_state, Editor_state.left+8,Editor_state.top+5, 1)edit.run_after_mouse_release(Editor_state, Editor_state.left+8,Editor_state.top+5, 1)-- hold down shift and click on a second locationApp.fake_key_press('lshift')edit.run_after_mouse_press(Editor_state, Editor_state.left+20,Editor_state.top+5, 1)edit.run_after_mouse_release(Editor_state, Editor_state.left+20,Editor_state.top+Editor_state.line_height+5, 1)-- hold down shift and click at a third locationApp.fake_key_press('lshift')edit.run_after_mouse_press(Editor_state, Editor_state.left+20,Editor_state.top+5, 1)edit.run_after_mouse_release(Editor_state, Editor_state.left+8,Editor_state.top+Editor_state.line_height+5, 1)App.fake_key_release('lshift')-- selection is between first and third location. forget the second location, not the first.-- click on first locationedit.run_after_mouse_press(Editor_state, Editor_state.left+8,Editor_state.top+5, 1)edit.run_after_mouse_release(Editor_state, Editor_state.left+8,Editor_state.top+5, 1)-- hold down shift and click somewhere elseApp.fake_key_press('lshift')edit.run_after_mouse_press(Editor_state, Editor_state.left+20,Editor_state.top+5, 1)edit.run_after_mouse_release(Editor_state, Editor_state.left+20,Editor_state.top+Editor_state.line_height+5, 1)App.fake_key_release('lshift')-- press mouse above first line of textedit.run_after_mouse_press(Editor_state, Editor_state.left+8,5, 1)check(Editor_state.selection1.line ~= nil, 'selection:line-not-nil')check_eq(Editor_state.selection1.line, 1, 'selection:line')check_eq(Editor_state.selection1.pos, 1, 'selection:pos')-- press and hold on first locationedit.run_after_mouse_press(Editor_state, Editor_state.left+8,Editor_state.top+5, 1)-- drag and release somewhere elseedit.run_after_mouse_release(Editor_state, Editor_state.left+20,Editor_state.top+Editor_state.line_height+5, 1)-- no change to data, selection is resetApp.fake_key_release('lshift')edit.run_after_mouse_release(Editor_state, Editor_state.left+8,Editor_state.top+5, 1)check_eq(Editor_state.cursor1.line, 2, 'line')check_eq(Editor_state.cursor1.pos, 4, 'pos')check_eq(Editor_state.cursor1.pos, 9, 'check')Editor_state = edit.initialize_test_state()Editor_state.lines = load_array{}Text.redraw_all(Editor_state)edit.draw(Editor_state)edit.run_after_mouse_click(Editor_state, 8,Editor_state.top+8, 1)-- cursor skips drawing to always remain on text
-- return y for the next linereturn y--? print(State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos)--? print(State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos)--? print('=>', State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos)State.screen_top1 = Text.previous_screen_top1(State)State.cursor1 = {line=State.screen_top1.line, pos=State.screen_top1.pos}Text.move_cursor_down_to_next_text_line_while_scrolling_again_if_necessary(State)Text.redraw_all(State) -- if we're scrolling, reclaim all fragments to avoid memory leaksend-- return the top y coordinate of a given line_index,-- or nil if no part of it is on screenfunction Text.starty(State, line_index)-- does not modify State (except to populate line_cache)if line_index < State.screen_top1.line then return endlocal loc2 = Text.to2(State, State.screen_top1)local y = State.topwhile true doif State.lines[loc2.line].mode == 'drawing' theny = y + Drawing_padding_topendif loc2.line == line_index then return y endif State.lines[loc2.line].mode == 'text' theny = y + State.line_heightelseif State.lines[loc2.line].mode == 'drawing' theny = y + Drawing.pixels(State.lines[loc2.line].h, State.width) + Drawing_padding_bottomendif y + State.line_height > App.screen.height then break endlocal next_loc2 = Text.next_screen_line(State, loc2)if Text.eq2(next_loc2, loc2) then break end -- end of fileloc2 = next_loc2endendfunction Text.previous_screen_top1(State)-- duplicate some logic from love.draw-- does not modify State (except to populate line_cache)local loc2 = Text.to2(State, State.screen_top1)if loc2.line == 1 and loc2.screen_line == 1 and loc2.screen_pos == 1 then break endif State.lines[loc2.line].mode == 'text' thenelseif State.lines[loc2.line].mode == 'drawing' theny = y - Drawing_padding_height - Drawing.pixels(State.lines[loc2.line].h, State.width)loc2 = Text.previous_screen_line(State, loc2)return Text.to1(State, loc2)State.screen_top1 = Text.screen_bottom1(State)end-- return the location of the start of the bottom-most line on screenfunction Text.screen_bottom1(State)-- duplicate some logic from love.draw-- does not modify State (except to populate line_cache)local loc2 = Text.to2(State, State.screen_top1)local y = State.topwhile true doif State.lines[loc2.line].mode == 'text' theny = y + State.line_heightelseif State.lines[loc2.line].mode == 'drawing' theny = y + Drawing_padding_height + Drawing.pixels(State.lines[loc2.line].h, State.width)endif y + State.line_height > App.screen.height then break endlocal next_loc2 = Text.next_screen_line(State, loc2)if Text.eq2(next_loc2, loc2) then break endloc2 = next_loc2endreturn Text.to1(State, loc2)--? print('down', State.cursor1.line, State.cursor1.pos, State.screen_top1.line, State.screen_top1.pos)local screen_bottom1 = Text.screen_bottom1(State)--? print('down 2', State.cursor1.line, State.cursor1.pos, State.screen_top1.line, State.screen_top1.pos, screen_bottom1.line, screen_bottom1.pos)if State.cursor1.line > screen_bottom1.line thenlocal screen_bottom1 = Text.screen_bottom1(State)local scroll_down = Text.le1(screen_bottom1, State.cursor1)--? print('=>', State.cursor1.line, State.cursor1.pos, State.screen_top1.line, State.screen_top1.pos)assert(State.lines[loc1.line].mode == 'text')endfunction Text.final_text_loc_on_screen(State)local screen_bottom1 = Text.screen_bottom1(State)if State.lines[screen_bottom1.line].mode == 'text' thenreturn {line=screen_bottom1.line,pos=Text.pos_at_end_of_screen_line(State, screen_bottom1),}endlocal loc2 = Text.to2(State, screen_bottom1)while true doif State.lines[loc2.line].mode == 'text' then break endassert(loc2.line > 1 or loc2.screen_line > 1 and loc2.screen_pos > 1) -- elsewhere we're making sure there's always at least one text line on screenloc2 = Text.previous_screen_line(State, loc2)endlocal result = Text.to1(State, loc2)result.pos = Text.pos_at_end_of_screen_line(State, result)return result--? print('snap', State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos)--? print('snap =>', State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos)local starty = Text.starty(State, line_index)if starty == nil then return false end -- outside current pageif y < starty then return false endreturn y < starty + State.line_height*(#line_cache.screen_line_starting_pos - Text.screen_line_index(line_cache.screen_line_starting_pos, line_cache.startpos) + 1)local starty = Text.starty(State, line_index)assert(my >= starty, 'failed to map y pixel to line')local y = startyendfunction Text.eq2(a, b)return a.line == b.line and a.screen_line == b.screen_line and a.screen_pos == b.screen_posfunction Text.next_screen_line(State, loc2)if State.lines[loc2.line].mode == 'drawing' thenreturn {line=loc2.line+1, screen_line=1, screen_pos=1}endText.populate_screen_line_starting_pos(State, loc2.line)if loc2.screen_line >= #State.line_cache[loc2.line].screen_line_starting_pos thenif loc2.line < #State.lines thenreturn {line=loc2.line+1, screen_line=1, screen_pos=1}elsereturn loc2endelsereturn {line=loc2.line, screen_line=loc2.screen_line+1, screen_pos=1}endendlocal screen_bottom1 = Text.screen_bottom1(State)elseif State.cursor1.line >= screen_bottom1.line thenState.cursor1 = Text.final_text_loc_on_screen(State)endendend-- slightly expensive since it redraws the screenfunction Text.cursor_out_of_screen(State)if Text.cursor_out_of_screen(State) thenif Text.lt1(State.cursor1, State.screen_top1) thenState.cursor1 = {line=State.screen_top1.line, pos=State.screen_top1.pos}-- resize helperfunction Text.tweak_screen_top_and_cursor(State)if State.screen_top1.pos == 1 then return endText.populate_screen_line_starting_pos(State, State.screen_top1.line)local line = State.lines[State.screen_top1.line]local line_cache = State.line_cache[State.screen_top1.line]for i=2,#line_cache.screen_line_starting_pos dolocal pos = line_cache.screen_line_starting_pos[i]if pos == State.screen_top1.pos thenbreakendif pos > State.screen_top1.pos then-- make sure screen top is at start of a screen linelocal prev = line_cache.screen_line_starting_pos[i-1]if State.screen_top1.pos - prev < pos - State.screen_top1.pos thenState.screen_top1.pos = prevelseState.screen_top1.pos = posendbreakendend-- make sure cursor is on screenendlocal start_screen_line_index = Text.screen_line_index(line_cache.screen_line_starting_pos, line_cache.startpos)for screen_line_index = start_screen_line_index,#line_cache.screen_line_starting_pos dolocal screen_line_starting_pos = line_cache.screen_line_starting_pos[screen_line_index]local screen_line_starting_byte_offset = Text.offset(line.data, screen_line_starting_pos)--? print('iter', y, screen_line_index, screen_line_starting_pos, string.sub(line.data, screen_line_starting_byte_offset))-- duplicate some logic from Text.drawend-- convert mx,my in pixels to schema-1 coordinatesText.populate_screen_line_starting_pos(State, line_index)Text.redraw_all(State) -- if we're scrolling, reclaim all fragments to avoid memory leaks--? print('cursor pos '..tostring(State.cursor1.pos)..' is on the #'..tostring(top2.screen_line)..' screen line down')local y = App.screen.height - State.line_height-- duplicate some logic from love.drawwhile true doendText.populate_screen_line_starting_pos(State, loc1.line)local line_cache = State.line_cache[loc1.line]local most_recent_final_pos = utf8.len(State.lines[loc1.line].data)+1for i=#line_cache.screen_line_starting_pos,1,-1 dolocal spos = line_cache.screen_line_starting_pos[i]if spos <= loc1.pos thenreturn most_recent_final_posendmost_recent_final_pos = spos-1endendfunction Text.start_of_line(State)--? print('cursor is NOT at final screen line of its line')local screen_line_starting_pos, screen_line_index = Text.pos_at_start_of_screen_line(State, State.cursor1)Text.populate_screen_line_starting_pos(State, State.cursor1.line)local new_screen_line_starting_pos = State.line_cache[State.cursor1.line].screen_line_starting_pos[screen_line_index+1]--? print('switching pos of screen line at cursor from '..tostring(screen_line_starting_pos)..' to '..tostring(new_screen_line_starting_pos))local new_screen_line_starting_byte_offset = Text.offset(State.lines[State.cursor1.line].data, new_screen_line_starting_pos)local s = string.sub(State.lines[State.cursor1.line].data, new_screen_line_starting_byte_offset)--? print('screen top before:', State.screen_top1.line, State.screen_top1.pos)--? print('scroll up preserving cursor')Text.snap_cursor_to_bottom_of_screen(State)--? print('screen top after:', State.screen_top1.line, State.screen_top1.pos)endassert(State.cursor1.pos, 'cursor has no pos')endfunction Text.up(State)State.cursor1 = {line=State.screen_top1.line, pos=State.screen_top1.pos}endfunction Text.pagedown(State)endendy = y - State.line_heightlocal y = App.screen.height - State.line_heightwhile y >= State.top do-- duplicate some logic from love.drawendschedule_save(State)record_undo_event(State, {before=before, after=snapshot(State, State.cursor1.line)})elseif chord == 'backspace' thenText.insert_at_cursor(State, '\t')if State.cursor_y > App.screen.height - State.line_height thenText.populate_screen_line_starting_pos(State, State.cursor1.line)Text.snap_cursor_to_bottom_of_screen(State, State.left, State.right)Text.insert_at_cursor(State, t)if State.cursor_y > App.screen.height - State.line_height thenText.populate_screen_line_starting_pos(State, State.cursor1.line)Text.snap_cursor_to_bottom_of_screen(State, State.left, State.right)endrecord_undo_event(State, {before=before, after=snapshot(State, State.cursor1.line)})endfunction Text.insert_at_cursor(State, t)endfunction Text.screen_line(line, line_cache, i)local pos = line_cache.screen_line_starting_pos[i]local offset = Text.offset(line.data, pos)if i >= #line_cache.screen_line_starting_pos thenreturn line.data:sub(offset)function Text.draw(State, line_index, y, startpos, hide_cursor, show_line_numbers)
---- On lines that are drawings, pos will be nil.cursor1 = {line=1, pos=1}, -- position of cursor; must be on a text liney = Text.draw(State, line_index, y, startpos, hide_cursor, show_line_numbers)if Drawing.in_drawing(State, line_index, x, y, State.left,State.right) thenState.selection1 = Text.final_text_loc_on_screen(State)State.cursor1 = Text.final_text_loc_on_screen(State)State.cursor1 = Text.screen_bottom1(State)chord ~= 'C-a' and chord ~= 'C-c' and chord ~= 'C-x' and chord ~= 'backspace' and chord ~= 'delete' and chord ~= 'C-z' and chord ~= 'C-y' and not App.is_cursor_movement(key) thenedit.draw(State)App.fake_mouse_release(x,y, mouse_button)Text.delete_selection(State, State.left, State.right)endedit.put_cursor_on_next_text_line(State)edit.clean_up_mouse_press(State)endState.lines.current_drawing_index = line_indexState.lines.current_drawing = lineDrawing.before = snapshot(State, line_index)--? print('=> y', y)elseif line.mode == 'drawing' theny = y+Drawing_padding_topDrawing.draw(State, line_index, y)y = y + Drawing.pixels(line.h, State.width) + Drawing_padding_bottomelseselection1 = {},-- some extra state to compute selection between mouse press and releaseold_cursor1 = nil,old_selection1 = nil,mousepress_shift = nil,screen_top1 = {line=1, pos=1}, -- position of start of screen line at top of screen