bugfix: highlight search patterns on the right line
[lines.love.git] / edit.lua
blobef1cda4f630cff475653f487c03b8c62361c8df7
1 -- some constants people might like to tweak
2 Text_color = {r=0, g=0, b=0}
3 Cursor_color = {r=1, g=0, b=0}
4 Stroke_color = {r=0, g=0, b=0}
5 Current_stroke_color = {r=0.7, g=0.7, b=0.7} -- in process of being drawn
6 Current_name_background_color = {r=1, g=0, b=0, a=0.1} -- name currently being edited
7 Focus_stroke_color = {r=1, g=0, b=0} -- what mouse is hovering over
8 Highlight_color = {r=0.7, g=0.7, b=0.9} -- selected text
9 Icon_color = {r=0.7, g=0.7, b=0.7} -- color of current mode icon in drawings
10 Help_color = {r=0, g=0.5, b=0}
11 Help_background_color = {r=0, g=0.5, b=0, a=0.1}
13 Margin_top = 15
14 Margin_left = 25
15 Margin_right = 25
17 Drawing_padding_top = 10
18 Drawing_padding_bottom = 10
19 Drawing_padding_height = Drawing_padding_top + Drawing_padding_bottom
21 Same_point_distance = 4 -- pixel distance at which two points are considered the same
23 edit = {}
25 -- run in both tests and a real run
26 function edit.initialize_state(top, left, right, font_height, line_height) -- currently always draws to bottom of screen
27 local result = {
28 -- a line is either text or a drawing
29 -- a text is a table with:
30 -- mode = 'text',
31 -- string data,
32 -- a drawing is a table with:
33 -- mode = 'drawing'
34 -- a (y) coord in pixels (updated while painting screen),
35 -- a (h)eight,
36 -- an array of points, and
37 -- an array of shapes
38 -- a shape is a table containing:
39 -- a mode
40 -- an array points for mode 'freehand' (raw x,y coords; freehand drawings don't pollute the points array of a drawing)
41 -- an array vertices for mode 'polygon', 'rectangle', 'square'
42 -- p1, p2 for mode 'line'
43 -- center, radius for mode 'circle'
44 -- center, radius, start_angle, end_angle for mode 'arc'
45 -- Unless otherwise specified, coord fields are normalized; a drawing is always 256 units wide
46 -- The field names are carefully chosen so that switching modes in midstream
47 -- remembers previously entered points where that makes sense.
48 lines = {{mode='text', data=''}}, -- array of lines
50 -- Lines can be too long to fit on screen, in which case they _wrap_ into
51 -- multiple _screen lines_.
53 -- rendering wrapped text lines needs some additional short-lived data per line:
54 -- startpos, the index of data the line starts rendering from, can only be >1 for topmost line on screen
55 -- starty, the y coord in pixels the line starts rendering from
56 -- fragments: snippets of the line guaranteed to not straddle screen lines
57 -- screen_line_starting_pos: optional array of grapheme indices if it wraps over more than one screen line
58 line_cache = {},
60 -- Given wrapping, any potential location for the text cursor can be described in two ways:
61 -- * schema 1: As a combination of line index and position within a line (in utf8 codepoint units)
62 -- * schema 2: As a combination of line index, screen line index within the line, and a position within the screen line.
64 -- Most of the time we'll only persist positions in schema 1, translating to
65 -- schema 2 when that's convenient.
67 -- Make sure these coordinates are never aliased, so that changing one causes
68 -- action at a distance.
69 screen_top1 = {line=1, pos=1}, -- position of start of screen line at top of screen
70 cursor1 = {line=1, pos=1}, -- position of cursor
71 screen_bottom1 = {line=1, pos=1}, -- position of start of screen line at bottom of screen
73 selection1 = {},
74 -- some extra state to compute selection between mouse press and release
75 old_cursor1 = nil,
76 old_selection1 = nil,
77 mousepress_shift = nil,
79 -- cursor coordinates in pixels
80 cursor_x = 0,
81 cursor_y = 0,
83 current_drawing_mode = 'line',
84 previous_drawing_mode = nil, -- extra state for some ephemeral modes like moving/deleting/naming points
86 font_height = font_height,
87 line_height = line_height,
89 top = top,
90 left = math.floor(left),
91 right = math.floor(right),
92 width = right-left,
94 filename = love.filesystem.getSourceBaseDirectory()..'/lines.txt', -- '/' should work even on Windows
95 next_save = nil,
97 -- undo
98 history = {},
99 next_history = 1,
101 -- search
102 search_term = nil,
103 search_backup = nil, -- stuff to restore when cancelling search
105 return result
106 end -- App.initialize_state
108 function edit.check_locs(State)
109 -- if State is inconsistent (i.e. file changed by some other program),
110 -- throw away all cursor state entirely
111 if edit.invalid1(State, State.screen_top1)
112 or edit.invalid_cursor1(State)
113 or not edit.cursor_on_text(State)
114 or not Text.le1(State.screen_top1, State.cursor1) then
115 State.screen_top1 = {line=1, pos=1}
116 edit.put_cursor_on_first_text_line(State)
120 function edit.invalid1(State, loc1)
121 if loc1.line > #State.lines then return true end
122 local l = State.lines[loc1.line]
123 if l.mode ~= 'text' then return false end -- pos is irrelevant to validity for a drawing line
124 return loc1.pos > #State.lines[loc1.line].data
127 -- cursor loc in particular differs from other locs in one way:
128 -- pos might occur just after end of line
129 function edit.invalid_cursor1(State)
130 local cursor1 = State.cursor1
131 if cursor1.line > #State.lines then return true end
132 local l = State.lines[cursor1.line]
133 if l.mode ~= 'text' then return false end -- pos is irrelevant to validity for a drawing line
134 return cursor1.pos > #State.lines[cursor1.line].data + 1
137 function edit.cursor_on_text(State)
138 return State.cursor1.line <= #State.lines
139 and State.lines[State.cursor1.line].mode == 'text'
142 function edit.put_cursor_on_first_text_line(State)
143 for i,line in ipairs(State.lines) do
144 if line.mode == 'text' then
145 State.cursor1 = {line=i, pos=1}
146 break
151 -- return y drawn until
152 function edit.draw(State)
153 State.button_handlers = {}
154 App.color(Text_color)
155 if #State.lines ~= #State.line_cache then
156 print(('line_cache is out of date; %d when it should be %d'):format(#State.line_cache, #State.lines))
157 assert(false)
159 if not Text.le1(State.screen_top1, State.cursor1) then
160 print(State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos)
161 assert(false)
163 State.cursor_x = nil
164 State.cursor_y = nil
165 local y = State.top
166 local screen_bottom1 = {line=nil, pos=nil}
167 --? print('== draw')
168 for line_index = State.screen_top1.line,#State.lines do
169 local line = State.lines[line_index]
170 --? print('draw:', y, line_index, line)
171 if y + State.line_height > App.screen.height then break end
172 screen_bottom1.line = line_index
173 if line.mode == 'text' then
174 --? print('text.draw', y, line_index)
175 local startpos = 1
176 if line_index == State.screen_top1.line then
177 startpos = State.screen_top1.pos
179 if line.data == '' then
180 -- button to insert new drawing
181 button(State, 'draw', {x=State.left-Margin_left+4, y=y+4, w=12,h=12, color={1,1,0},
182 icon = icon.insert_drawing,
183 onpress1 = function()
184 Drawing.before = snapshot(State, line_index-1, line_index)
185 table.insert(State.lines, line_index, {mode='drawing', y=y, h=256/2, points={}, shapes={}, pending={}})
186 table.insert(State.line_cache, line_index, {})
187 if State.cursor1.line >= line_index then
188 State.cursor1.line = State.cursor1.line+1
190 schedule_save(State)
191 record_undo_event(State, {before=Drawing.before, after=snapshot(State, line_index-1, line_index+1)})
192 end,
195 y, screen_bottom1.pos = Text.draw(State, line_index, y, startpos)
196 --? print('=> y', y)
197 elseif line.mode == 'drawing' then
198 y = y+Drawing_padding_top
199 Drawing.draw(State, line_index, y)
200 y = y + Drawing.pixels(line.h, State.width) + Drawing_padding_bottom
201 else
202 print(line.mode)
203 assert(false)
206 State.screen_bottom1 = screen_bottom1
207 if State.search_term then
208 Text.draw_search_bar(State)
210 return y
213 function edit.update(State, dt)
214 Drawing.update(State, dt)
215 if State.next_save and State.next_save < Current_time then
216 save_to_disk(State)
217 State.next_save = nil
221 function schedule_save(State)
222 if State.next_save == nil then
223 State.next_save = Current_time + 3 -- short enough that you're likely to still remember what you did
227 function edit.quit(State)
228 -- make sure to save before quitting
229 if State.next_save then
230 save_to_disk(State)
231 -- give some time for the OS to flush everything to disk
232 love.timer.sleep(0.1)
236 function edit.mouse_press(State, x,y, mouse_button)
237 if State.search_term then return end
238 --? print_and_log(('edit.mouse_press: cursor at %d,%d'):format(State.cursor1.line, State.cursor1.pos))
239 if mouse_press_consumed_by_any_button_handler(State, x,y, mouse_button) then
240 -- press on a button and it returned 'true' to short-circuit
241 return
244 if y < State.top then
245 State.old_cursor1 = State.cursor1
246 State.old_selection1 = State.selection1
247 State.mousepress_shift = App.shift_down()
248 State.selection1 = {
249 line=State.screen_top1.line,
250 pos=State.screen_top1.pos,
252 return
255 for line_index,line in ipairs(State.lines) do
256 if line.mode == 'text' then
257 if Text.in_line(State, line_index, x,y) then
258 -- delicate dance between cursor, selection and old cursor/selection
259 -- scenarios:
260 -- regular press+release: sets cursor, clears selection
261 -- shift press+release:
262 -- sets selection to old cursor if not set otherwise leaves it untouched
263 -- sets cursor
264 -- press and hold to start a selection: sets selection on press, cursor on release
265 -- press and hold, then press shift: ignore shift
266 -- i.e. mouse_release should never look at shift state
267 --? print_and_log(('edit.mouse_press: in line %d'):format(line_index))
268 State.old_cursor1 = State.cursor1
269 State.old_selection1 = State.selection1
270 State.mousepress_shift = App.shift_down()
271 State.selection1 = {
272 line=line_index,
273 pos=Text.to_pos_on_line(State, line_index, x, y),
275 return
277 elseif line.mode == 'drawing' then
278 local line_cache = State.line_cache[line_index]
279 if Drawing.in_drawing(line, line_cache, x, y, State.left,State.right) then
280 State.lines.current_drawing_index = line_index
281 State.lines.current_drawing = line
282 Drawing.before = snapshot(State, line_index)
283 Drawing.mouse_press(State, line_index, x,y, mouse_button)
284 return
289 -- still here? click is below all screen lines
290 State.old_cursor1 = State.cursor1
291 State.old_selection1 = State.selection1
292 State.mousepress_shift = App.shift_down()
293 State.selection1 = {
294 line=State.screen_bottom1.line,
295 pos=Text.pos_at_end_of_screen_line(State, State.screen_bottom1),
299 function edit.mouse_release(State, x,y, mouse_button)
300 if State.search_term then return end
301 --? print_and_log(('edit.mouse_release: cursor at %d,%d'):format(State.cursor1.line, State.cursor1.pos))
302 if State.lines.current_drawing then
303 Drawing.mouse_release(State, x,y, mouse_button)
304 schedule_save(State)
305 if Drawing.before then
306 record_undo_event(State, {before=Drawing.before, after=snapshot(State, State.lines.current_drawing_index)})
307 Drawing.before = nil
309 else
310 --? print_and_log('edit.mouse_release: no current drawing')
311 for line_index,line in ipairs(State.lines) do
312 if line.mode == 'text' then
313 if Text.in_line(State, line_index, x,y) then
314 --? print_and_log(('edit.mouse_release: in line %d'):format(line_index))
315 State.cursor1 = {
316 line=line_index,
317 pos=Text.to_pos_on_line(State, line_index, x, y),
319 --? print_and_log(('edit.mouse_release: cursor now %d,%d'):format(State.cursor1.line, State.cursor1.pos))
320 if State.mousepress_shift then
321 if State.old_selection1.line == nil then
322 State.selection1 = State.old_cursor1
323 else
324 State.selection1 = State.old_selection1
327 State.old_cursor1, State.old_selection1, State.mousepress_shift = nil
328 if eq(State.cursor1, State.selection1) then
329 State.selection1 = {}
331 break
335 --? print_and_log(('edit.mouse_release: finally selection %s,%s cursor %d,%d'):format(tostring(State.selection1.line), tostring(State.selection1.pos), State.cursor1.line, State.cursor1.pos))
339 function edit.mouse_wheel_move(State, dx,dy)
340 if dy > 0 then
341 State.cursor1 = {line=State.screen_top1.line, pos=State.screen_top1.pos}
342 for i=1,math.floor(dy) do
343 Text.up(State)
345 elseif dy < 0 then
346 State.cursor1 = {line=State.screen_bottom1.line, pos=State.screen_bottom1.pos}
347 for i=1,math.floor(-dy) do
348 Text.down(State)
353 function edit.text_input(State, t)
354 --? print('text input', t)
355 if State.search_term then
356 State.search_term = State.search_term..t
357 Text.search_next(State)
358 elseif State.lines.current_drawing and State.current_drawing_mode == 'name' then
359 local before = snapshot(State, State.lines.current_drawing_index)
360 local drawing = State.lines.current_drawing
361 local p = drawing.points[drawing.pending.target_point]
362 p.name = p.name..t
363 record_undo_event(State, {before=before, after=snapshot(State, State.lines.current_drawing_index)})
364 else
365 local drawing_index, drawing = Drawing.current_drawing(State)
366 if drawing_index == nil then
367 for _,line_cache in ipairs(State.line_cache) do line_cache.starty = nil end -- just in case we scroll
368 Text.text_input(State, t)
371 schedule_save(State)
374 function edit.keychord_press(State, chord, key)
375 if State.selection1.line and
376 not State.lines.current_drawing and
377 -- printable character created using shift key => delete selection
378 -- (we're not creating any ctrl-shift- or alt-shift- combinations using regular/printable keys)
379 (not App.shift_down() or utf8.len(key) == 1) and
380 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(chord) then
381 Text.delete_selection(State, State.left, State.right)
383 if State.search_term then
384 for _,line_cache in ipairs(State.line_cache) do line_cache.starty = nil end -- just in case we scroll
385 if chord == 'escape' then
386 State.search_term = nil
387 State.cursor1 = State.search_backup.cursor
388 State.screen_top1 = State.search_backup.screen_top
389 State.search_backup = nil
390 Text.redraw_all(State) -- if we're scrolling, reclaim all fragments to avoid memory leaks
391 elseif chord == 'return' then
392 State.search_term = nil
393 State.search_backup = nil
394 elseif chord == 'backspace' then
395 local len = utf8.len(State.search_term)
396 local byte_offset = Text.offset(State.search_term, len)
397 State.search_term = string.sub(State.search_term, 1, byte_offset-1)
398 elseif chord == 'down' then
399 State.cursor1.pos = State.cursor1.pos+1
400 Text.search_next(State)
401 elseif chord == 'up' then
402 Text.search_previous(State)
404 return
405 elseif chord == 'C-f' then
406 State.search_term = ''
407 State.search_backup = {
408 cursor={line=State.cursor1.line, pos=State.cursor1.pos},
409 screen_top={line=State.screen_top1.line, pos=State.screen_top1.pos},
411 -- zoom
412 elseif chord == 'C-=' then
413 edit.update_font_settings(State, State.font_height+2)
414 Text.redraw_all(State)
415 elseif chord == 'C--' then
416 if State.font_height > 2 then
417 edit.update_font_settings(State, State.font_height-2)
418 Text.redraw_all(State)
420 elseif chord == 'C-0' then
421 edit.update_font_settings(State, 20)
422 Text.redraw_all(State)
423 -- undo
424 elseif chord == 'C-z' then
425 for _,line_cache in ipairs(State.line_cache) do line_cache.starty = nil end -- just in case we scroll
426 local event = undo_event(State)
427 if event then
428 local src = event.before
429 State.screen_top1 = deepcopy(src.screen_top)
430 State.cursor1 = deepcopy(src.cursor)
431 State.selection1 = deepcopy(src.selection)
432 patch(State.lines, event.after, event.before)
433 patch_placeholders(State.line_cache, event.after, event.before)
434 -- invalidate various cached bits of lines
435 State.lines.current_drawing = nil
436 -- if we're scrolling, reclaim all fragments to avoid memory leaks
437 Text.redraw_all(State)
438 schedule_save(State)
440 elseif chord == 'C-y' then
441 for _,line_cache in ipairs(State.line_cache) do line_cache.starty = nil end -- just in case we scroll
442 local event = redo_event(State)
443 if event then
444 local src = event.after
445 State.screen_top1 = deepcopy(src.screen_top)
446 State.cursor1 = deepcopy(src.cursor)
447 State.selection1 = deepcopy(src.selection)
448 patch(State.lines, event.before, event.after)
449 -- invalidate various cached bits of lines
450 State.lines.current_drawing = nil
451 -- if we're scrolling, reclaim all fragments to avoid memory leaks
452 Text.redraw_all(State)
453 schedule_save(State)
455 -- clipboard
456 elseif chord == 'C-a' then
457 State.selection1 = {line=1, pos=1}
458 State.cursor1 = {line=#State.lines, pos=utf8.len(State.lines[#State.lines].data)+1}
459 elseif chord == 'C-c' then
460 local s = Text.selection(State)
461 if s then
462 App.setClipboardText(s)
464 elseif chord == 'C-x' then
465 for _,line_cache in ipairs(State.line_cache) do line_cache.starty = nil end -- just in case we scroll
466 local s = Text.cut_selection(State, State.left, State.right)
467 if s then
468 App.setClipboardText(s)
470 schedule_save(State)
471 elseif chord == 'C-v' then
472 for _,line_cache in ipairs(State.line_cache) do line_cache.starty = nil end -- just in case we scroll
473 -- We don't have a good sense of when to scroll, so we'll be conservative
474 -- and sometimes scroll when we didn't quite need to.
475 local before_line = State.cursor1.line
476 local before = snapshot(State, before_line)
477 local clipboard_data = App.getClipboardText()
478 for _,code in utf8.codes(clipboard_data) do
479 local c = utf8.char(code)
480 if c == '\n' then
481 Text.insert_return(State)
482 else
483 Text.insert_at_cursor(State, c)
486 if Text.cursor_out_of_screen(State) then
487 Text.snap_cursor_to_bottom_of_screen(State, State.left, State.right)
489 schedule_save(State)
490 record_undo_event(State, {before=before, after=snapshot(State, before_line, State.cursor1.line)})
491 -- dispatch to drawing or text
492 elseif App.mouse_down(1) or chord:sub(1,2) == 'C-' then
493 -- DON'T reset line_cache.starty here
494 local drawing_index, drawing = Drawing.current_drawing(State)
495 if drawing_index then
496 local before = snapshot(State, drawing_index)
497 Drawing.keychord_press(State, chord)
498 record_undo_event(State, {before=before, after=snapshot(State, drawing_index)})
499 schedule_save(State)
501 elseif chord == 'escape' and not App.mouse_down(1) then
502 for _,line in ipairs(State.lines) do
503 if line.mode == 'drawing' then
504 line.show_help = false
507 elseif State.lines.current_drawing and State.current_drawing_mode == 'name' then
508 if chord == 'return' then
509 State.current_drawing_mode = State.previous_drawing_mode
510 State.previous_drawing_mode = nil
511 else
512 local before = snapshot(State, State.lines.current_drawing_index)
513 local drawing = State.lines.current_drawing
514 local p = drawing.points[drawing.pending.target_point]
515 if chord == 'escape' then
516 p.name = nil
517 record_undo_event(State, {before=before, after=snapshot(State, State.lines.current_drawing_index)})
518 elseif chord == 'backspace' then
519 local len = utf8.len(p.name)
520 if len > 0 then
521 local byte_offset = Text.offset(p.name, len-1)
522 if len == 1 then byte_offset = 0 end
523 p.name = string.sub(p.name, 1, byte_offset)
524 record_undo_event(State, {before=before, after=snapshot(State, State.lines.current_drawing_index)})
528 schedule_save(State)
529 else
530 for _,line_cache in ipairs(State.line_cache) do line_cache.starty = nil end -- just in case we scroll
531 Text.keychord_press(State, chord)
535 function edit.key_release(State, key, scancode)
538 function edit.update_font_settings(State, font_height)
539 State.font_height = font_height
540 love.graphics.setFont(love.graphics.newFont(State.font_height))
541 State.line_height = math.floor(font_height*1.3)
544 --== some methods for tests
546 -- Insulate tests from some key globals so I don't have to change the vast
547 -- majority of tests when they're modified for the real app.
548 Test_margin_left = 25
549 Test_margin_right = 0
551 function edit.initialize_test_state()
552 -- if you change these values, tests will start failing
553 return edit.initialize_state(
554 15, -- top margin
555 Test_margin_left,
556 App.screen.width - Test_margin_right,
557 14, -- font height assuming default LÖVE font
558 15) -- line height
561 -- all text_input events are also keypresses
562 -- TODO: handle chords of multiple keys
563 function edit.run_after_text_input(State, t)
564 edit.keychord_press(State, t)
565 edit.text_input(State, t)
566 edit.key_release(State, t)
567 App.screen.contents = {}
568 edit.update(State, 0)
569 edit.draw(State)
572 -- not all keys are text_input
573 function edit.run_after_keychord(State, chord)
574 edit.keychord_press(State, chord)
575 edit.key_release(State, chord)
576 App.screen.contents = {}
577 edit.update(State, 0)
578 edit.draw(State)
581 function edit.run_after_mouse_click(State, x,y, mouse_button)
582 App.fake_mouse_press(x,y, mouse_button)
583 edit.mouse_press(State, x,y, mouse_button)
584 App.fake_mouse_release(x,y, mouse_button)
585 edit.mouse_release(State, x,y, mouse_button)
586 App.screen.contents = {}
587 edit.update(State, 0)
588 edit.draw(State)
591 function edit.run_after_mouse_press(State, x,y, mouse_button)
592 App.fake_mouse_press(x,y, mouse_button)
593 edit.mouse_press(State, x,y, mouse_button)
594 App.screen.contents = {}
595 edit.update(State, 0)
596 edit.draw(State)
599 function edit.run_after_mouse_release(State, x,y, mouse_button)
600 App.fake_mouse_release(x,y, mouse_button)
601 edit.mouse_release(State, x,y, mouse_button)
602 App.screen.contents = {}
603 edit.update(State, 0)
604 edit.draw(State)