diff options
Diffstat (limited to 'source_edit.lua')
-rw-r--r-- | source_edit.lua | 117 |
1 files changed, 36 insertions, 81 deletions
diff --git a/source_edit.lua b/source_edit.lua index f340ab3..964f6ff 100644 --- a/source_edit.lua +++ b/source_edit.lua @@ -10,8 +10,6 @@ Highlight_color = {r=0.7, g=0.7, b=0.9} -- selected text Icon_color = {r=0.7, g=0.7, b=0.7} -- color of current mode icon in drawings Help_color = {r=0, g=0.5, b=0} Help_background_color = {r=0, g=0.5, b=0, a=0.1} -Fold_color = {r=0, g=0.6, b=0} -Fold_background_color = {r=0, g=0.7, b=0} Margin_top = 15 Margin_left = 25 @@ -28,12 +26,10 @@ edit = {} -- run in both tests and a real run function edit.initialize_state(top, left, right, font_height, line_height) -- currently always draws to bottom of screen local result = { - -- a line is either bifold text or a drawing - -- a line of bifold text consists of an A side and an optional B side + -- a line is either text or a drawing + -- a text is a table with: -- mode = 'text', -- string data, - -- string dataB, - -- expanded: whether to show B side -- a drawing is a table with: -- mode = 'drawing' -- a (y) coord in pixels (updated while painting screen), @@ -50,7 +46,7 @@ function edit.initialize_state(top, left, right, font_height, line_height) -- c -- 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. - lines = {{mode='text', data='', dataB=nil, expanded=nil}}, -- array of lines + lines = {{mode='text', data=''}}, -- array of lines -- Lines can be too long to fit on screen, in which case they _wrap_ into -- multiple _screen lines_. @@ -65,16 +61,15 @@ function edit.initialize_state(top, left, right, font_height, line_height) -- c -- Given wrapping, any potential location for the text cursor can be described in two ways: -- * schema 1: As a combination of line index and position within a line (in utf8 codepoint units) -- * schema 2: As a combination of line index, screen line index within the line, and a position within the screen line. - -- Positions (and screen line indexes) can be in either the A or the B side. -- -- Most of the time we'll only persist positions in schema 1, translating to -- schema 2 when that's convenient. -- -- Make sure these coordinates are never aliased, so that changing one causes -- action at a distance. - screen_top1 = {line=1, pos=1, posB=nil}, -- position of start of screen line at top of screen - cursor1 = {line=1, pos=1, posB=nil}, -- position of cursor - screen_bottom1 = {line=1, pos=1, posB=nil}, -- position of start of screen line at bottom of screen + screen_top1 = {line=1, pos=1}, -- position of start of screen line at top of screen + cursor1 = {line=1, pos=1}, -- position of cursor + screen_bottom1 = {line=1, pos=1}, -- position of start of screen line at bottom of screen selection1 = {}, -- some extra state to compute selection between mouse press and release @@ -96,8 +91,8 @@ function edit.initialize_state(top, left, right, font_height, line_height) -- c em = App.newText(love.graphics.getFont(), 'm'), -- widest possible character width top = top, - left = left, - right = right, + left = math.floor(left), + right = math.floor(right), width = right-left, filename = love.filesystem.getUserDirectory()..'/lines.txt', -- '/' should work even on Windows @@ -154,7 +149,7 @@ function edit.draw(State, hide_cursor) assert(false) end if not Text.le1(State.screen_top1, State.cursor1) then - print(State.screen_top1.line, State.screen_top1.pos, State.screen_top1.posB, State.cursor1.line, State.cursor1.pos, State.cursor1.posB) + print(State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos) assert(false) end State.cursor_x = nil @@ -163,18 +158,14 @@ function edit.draw(State, hide_cursor) --? print('== draw') for line_index = State.screen_top1.line,#State.lines do local line = State.lines[line_index] ---? print('draw:', y, line_index, line, line.mode) +--? print('draw:', y, line_index, line) if y + State.line_height > App.screen.height then break end - State.screen_bottom1 = {line=line_index, pos=nil, posB=nil} + State.screen_bottom1 = {line=line_index, pos=nil} if line.mode == 'text' then ---? print('text.draw', y, line_index) - local startpos, startposB = 1, nil +--? print('text.draw', y, line_index) + local startpos = 1 if line_index == State.screen_top1.line then - if State.screen_top1.pos then - startpos = State.screen_top1.pos - else - startpos, startposB = nil, State.screen_top1.posB - end + startpos = State.screen_top1.pos end if line.data == '' then -- button to insert new drawing @@ -192,7 +183,7 @@ function edit.draw(State, hide_cursor) end, }) end - y, State.screen_bottom1.pos, State.screen_bottom1.posB = Text.draw(State, line_index, y, startpos, startposB, hide_cursor) + y, State.screen_bottom1.pos = Text.draw(State, line_index, y, startpos, hide_cursor) y = y + State.line_height --? print('=> y', y) elseif line.mode == 'drawing' then @@ -227,12 +218,14 @@ function edit.quit(State) -- make sure to save before quitting if State.next_save then save_to_disk(State) + -- give some time for the OS to flush everything to disk + love.timer.sleep(0.1) end end function edit.mouse_press(State, x,y, mouse_button) if State.search_term then return end ---? print('press') +--? print('press', State.selection1.line, State.selection1.pos) 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 @@ -253,10 +246,11 @@ function edit.mouse_press(State, x,y, mouse_button) State.old_cursor1 = State.cursor1 State.old_selection1 = State.selection1 State.mousepress_shift = App.shift_down() - local pos,posB = Text.to_pos_on_line(State, line_index, x, y) - --? print(x,y, 'setting cursor:', line_index, pos, posB) - State.selection1 = {line=line_index, pos=pos, posB=posB} ---? print('selection', State.selection1.line, State.selection1.pos, State.selection1.posB) + State.selection1 = { + line=line_index, + pos=Text.to_pos_on_line(State, line_index, x, y), + } +--? print('selection', State.selection1.line, State.selection1.pos) break end elseif line.mode == 'drawing' then @@ -287,9 +281,11 @@ function edit.mouse_release(State, x,y, mouse_button) if line.mode == 'text' then if Text.in_line(State, line_index, x,y) then --? print('reset selection') - local pos,posB = Text.to_pos_on_line(State, line_index, x, y) - State.cursor1 = {line=line_index, pos=pos, posB=posB} ---? print('cursor', State.cursor1.line, State.cursor1.pos, State.cursor1.posB) + State.cursor1 = { + line=line_index, + pos=Text.to_pos_on_line(State, line_index, x, y), + } +--? print('cursor', State.cursor1.line, State.cursor1.pos) if State.mousepress_shift then if State.old_selection1.line == nil then State.selection1 = State.old_cursor1 @@ -358,11 +354,7 @@ function edit.keychord_press(State, chord, key) State.search_term = string.sub(State.search_term, 1, byte_offset-1) State.search_text = nil elseif chord == 'down' then - if State.cursor1.pos then - State.cursor1.pos = State.cursor1.pos+1 - else - State.cursor1.posB = State.cursor1.posB+1 - end + State.cursor1.pos = State.cursor1.pos+1 Text.search_next(State) elseif chord == 'up' then Text.search_previous(State) @@ -371,42 +363,19 @@ function edit.keychord_press(State, chord, key) elseif chord == 'C-f' then State.search_term = '' State.search_backup = { - cursor={line=State.cursor1.line, pos=State.cursor1.pos, posB=State.cursor1.posB}, - screen_top={line=State.screen_top1.line, pos=State.screen_top1.pos, posB=State.screen_top1.posB}, + cursor={line=State.cursor1.line, pos=State.cursor1.pos}, + screen_top={line=State.screen_top1.line, pos=State.screen_top1.pos}, } assert(State.search_text == nil) - -- bifold text - elseif chord == 'M-b' then - State.expanded = not State.expanded - Text.redraw_all(State) - if not State.expanded then - for _,line in ipairs(State.lines) do - line.expanded = nil - end - edit.eradicate_locations_after_the_fold(State) - end - elseif chord == 'M-d' then - if State.cursor1.posB == nil then - local before = snapshot(State, State.cursor1.line) - if State.lines[State.cursor1.line].dataB == nil then - State.lines[State.cursor1.line].dataB = '' - end - State.lines[State.cursor1.line].expanded = true - State.cursor1.pos = nil - State.cursor1.posB = 1 - if Text.cursor_out_of_screen(State) then - Text.snap_cursor_to_bottom_of_screen(State, State.left, State.right) - end - schedule_save(State) - record_undo_event(State, {before=before, after=snapshot(State, State.cursor1.line)}) - end -- zoom elseif chord == 'C-=' then edit.update_font_settings(State, State.font_height+2) Text.redraw_all(State) elseif chord == 'C--' then - edit.update_font_settings(State, State.font_height-2) - Text.redraw_all(State) + if State.font_height > 2 then + edit.update_font_settings(State, State.font_height-2) + Text.redraw_all(State) + end elseif chord == 'C-0' then edit.update_font_settings(State, 20) Text.redraw_all(State) @@ -445,7 +414,7 @@ function edit.keychord_press(State, chord, key) -- clipboard elseif chord == 'C-a' then State.selection1 = {line=1, pos=1} - State.cursor1 = {line=#State.lines, pos=utf8.len(State.lines[#State.lines].data)+1, posB=nil} + State.cursor1 = {line=#State.lines, pos=utf8.len(State.lines[#State.lines].data)+1} elseif chord == 'C-c' then local s = Text.selection(State) if s then @@ -520,20 +489,6 @@ function edit.keychord_press(State, chord, key) end end -function edit.eradicate_locations_after_the_fold(State) - -- eradicate side B from any locations we track - if State.cursor1.posB then - State.cursor1.posB = nil - State.cursor1.pos = utf8.len(State.lines[State.cursor1.line].data) - State.cursor1.pos = Text.pos_at_start_of_screen_line(State, State.cursor1) - end - if State.screen_top1.posB then - State.screen_top1.posB = nil - State.screen_top1.pos = utf8.len(State.lines[State.screen_top1.line].data) - State.screen_top1.pos = Text.pos_at_start_of_screen_line(State, State.screen_top1) - end -end - function edit.key_release(State, key, scancode) end |