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FVFE2YKCZCODFKIJKAQQC
2L5MEZV344TOZLVY3432RHJFIRVXFD6O3GWLL5O4CV66BGAFTURQC
-- create a drawing with a line
App.screen.init{width=Test_margin_left+256, height=300} -- drawing coordinates 1:1 with pixels
Editor_state = edit.initialize_test_state()
Editor_state.filename = 'foo'
Editor_state.lines = load_array{'```lines', '```', ''}
Text.redraw_all(Editor_state)
edit.check_locs(Editor_state)
Editor_state.current_drawing_mode = 'line'
edit.draw(Editor_state)
-- draw a line
edit.run_after_mouse_press(Editor_state, Editor_state.left+5, Editor_state.top+Drawing_padding_top+6, 1)
edit.run_after_mouse_release(Editor_state, Editor_state.left+35, Editor_state.top+Drawing_padding_top+36, 1)
local drawing = Editor_state.lines[1]
check_eq(#drawing.shapes, 1, 'baseline/#shapes')
check_eq(#drawing.points, 2, 'baseline/#points')
check_eq(drawing.shapes[1].mode, 'line', 'baseline/shape:1')
local p1 = drawing.points[drawing.shapes[1].p1]
local p2 = drawing.points[drawing.shapes[1].p2]
check_eq(p1.x, 5, 'baseline/p1:x')
check_eq(p1.y, 6, 'baseline/p1:y')
check_eq(p2.x, 35, 'baseline/p2:x')
check_eq(p2.y, 36, 'baseline/p2:y')
check_nil(p2.name, 'baseline/p2:name')
-- enter 'name' mode without moving the mouse
edit.run_after_keychord(Editor_state, 'C-n')
check_eq(Editor_state.current_drawing_mode, 'name', 'mode:1')
end
function test_move_point()
-- create a drawing with a line
App.screen.init{width=Test_margin_left+256, height=300} -- drawing coordinates 1:1 with pixels
Editor_state = edit.initialize_test_state()
Editor_state.filename = 'foo'
Editor_state.lines = load_array{'```lines', '```', ''}
Text.redraw_all(Editor_state)
Editor_state.current_drawing_mode = 'line'
edit.draw(Editor_state)
edit.run_after_mouse_press(Editor_state, Editor_state.left+5, Editor_state.top+Drawing_padding_top+6, 1)
edit.run_after_mouse_release(Editor_state, Editor_state.left+35, Editor_state.top+Drawing_padding_top+36, 1)
local drawing = Editor_state.lines[1]
check_eq(#drawing.shapes, 1, 'baseline/#shapes')
check_eq(#drawing.points, 2, 'baseline/#points')
check_eq(drawing.shapes[1].mode, 'line', 'baseline/shape:1')
function test_move_point()
-- click outside the drawing
edit.run_after_mouse_click(Editor_state, App.screen.width-5, App.screen.height-5, 1)
-- press a key
edit.run_after_text_input(Editor_state, 'a')
-- key goes to text
check_eq(Editor_state.lines[2].data, 'a')
check_eq(#drawing.shapes, 1, 'baseline/#shapes')
check_eq(#drawing.points, 2, 'baseline/#points')
check_eq(drawing.shapes[1].mode, 'line', 'baseline/shape:1')
local p1 = drawing.points[drawing.shapes[1].p1]
local p2 = drawing.points[drawing.shapes[1].p2]
check_eq(p1.x, 5, 'baseline/p1:x')
check_eq(p1.y, 6, 'baseline/p1:y')
check_eq(p2.x, 35, 'baseline/p2:x')
check_eq(p2.y, 36, 'baseline/p2:y')
check_nil(p2.name, 'baseline/p2:name')
-- enter 'name' mode without moving the mouse
edit.run_after_keychord(Editor_state, 'C-n')
check_eq(Editor_state.current_drawing_mode, 'name', 'mode:1')
-- hit backspace
edit.run_after_keychord(Editor_state, 'backspace')
-- no crash
end
function test_name_point_then_exit_drawing()
-- draw a line
edit.run_after_mouse_press(Editor_state, Editor_state.left+5, Editor_state.top+Drawing_padding_top+6, 1)
edit.run_after_mouse_release(Editor_state, Editor_state.left+35, Editor_state.top+Drawing_padding_top+36, 1)
local drawing = Editor_state.lines[1]
function test_name_point_then_hit_backspace()
-- create a drawing with a line
App.screen.init{width=Test_margin_left+256, height=300} -- drawing coordinates 1:1 with pixels
Editor_state = edit.initialize_test_state()
Editor_state.filename = 'foo'
Editor_state.lines = load_array{'```lines', '```', ''}
Text.redraw_all(Editor_state)
Editor_state.current_drawing_mode = 'line'
edit.draw(Editor_state)
assert(State.lines[State.cursor1.line].mode == 'text')
local byte_offset = Text.offset(State.lines[State.cursor1.line].data, State.cursor1.pos)
State.lines[State.cursor1.line].data = string.sub(State.lines[State.cursor1.line].data, 1, byte_offset-1)..t..string.sub(State.lines[State.cursor1.line].data, byte_offset)
Text.clear_screen_line_cache(State, State.cursor1.line)
State.cursor1.pos = State.cursor1.pos+1
if len > 0 then
local byte_offset = Text.offset(p.name, len-1)
if len == 1 then byte_offset = 0 end
p.name = string.sub(p.name, 1, byte_offset)
record_undo_event(State, {before=before, after=snapshot(State, State.lines.current_drawing_index)})
end
end
end
schedule_save(State)
elseif State.lines.current_drawing and State.current_drawing_mode == 'name' then
if chord == 'return' then
State.current_drawing_mode = State.previous_drawing_mode
State.previous_drawing_mode = nil
else
local before = snapshot(State, State.lines.current_drawing_index)
local drawing = State.lines.current_drawing
local p = drawing.points[drawing.pending.target_point]
if chord == 'escape' then
p.name = nil
record_undo_event(State, {before=before, after=snapshot(State, State.lines.current_drawing_index)})
elseif chord == 'backspace' then
local len = utf8.len(p.name)
elseif State.lines.current_drawing and State.current_drawing_mode == 'name' then
local before = snapshot(State, State.lines.current_drawing_index)
local drawing = State.lines.current_drawing
local p = drawing.points[drawing.pending.target_point]
p.name = p.name..t
record_undo_event(State, {before=before, after=snapshot(State, State.lines.current_drawing_index)})
--? print('text input', t)
if State.search_term then
State.search_term = State.search_term..t
State.search_text = nil
Text.search_next(State)
--? print('release', State.cursor1.line)
if State.lines.current_drawing then
--? print('press', State.cursor1.line)
if mouse_press_consumed_by_any_button_handler(State, x,y, mouse_button) then
-- press on a button and it returned 'true' to short-circuit
return
end
for line_index,line in ipairs(State.lines) do
button(State, 'draw', {x=State.left-Margin_left+4, y=y+4, w=12,h=12, color={1,1,0},
icon = icon.insert_drawing,
onpress1 = function()
Drawing.before = snapshot(State, line_index-1, line_index)
table.insert(State.lines, line_index, {mode='drawing', y=y, h=256/2, points={}, shapes={}, pending={}})
table.insert(State.line_cache, line_index, {})
if State.cursor1.line >= line_index then
State.cursor1.line = State.cursor1.line+1
end
schedule_save(State)
record_undo_event(State, {before=Drawing.before, after=snapshot(State, line_index-1, line_index+1)})
end,
})
if loc1.line > #State.lines then return true end
local l = State.lines[loc1.line]
if l.mode ~= 'text' then return false end -- pos is irrelevant to validity for a drawing line
return loc1.pos > #State.lines[loc1.line].data
end
function edit.cursor_on_text(State)
return State.cursor1.line <= #State.lines
and State.lines[State.cursor1.line].mode == 'text'
end
function edit.put_cursor_on_first_text_line(State)