bugfix #3, attempt #2 in search UI
[view.love.git] / edit.lua
blob44e58ab15783c232493917e372bfaef640c49729
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, a=0.4} -- 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, 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 (h)eight,
35 -- an array of points, and
36 -- an array of shapes
37 -- a shape is a table containing:
38 -- a mode
39 -- an array points for mode 'freehand' (raw x,y coords; freehand drawings don't pollute the points array of a drawing)
40 -- an array vertices for mode 'polygon', 'rectangle', 'square'
41 -- p1, p2 for mode 'line'
42 -- center, radius for mode 'circle'
43 -- center, radius, start_angle, end_angle for mode 'arc'
44 -- Unless otherwise specified, coord fields are normalized; a drawing is always 256 units wide
45 -- The field names are carefully chosen so that switching modes in midstream
46 -- remembers previously entered points where that makes sense.
47 lines = {{mode='text', data=''}}, -- array of lines
49 -- Lines can be too long to fit on screen, in which case they _wrap_ into
50 -- multiple _screen lines_.
52 -- rendering wrapped text lines needs some additional short-lived data per line:
53 -- startpos, the index of data the line starts rendering from, can only be >1 for topmost line on screen
54 -- screen_line_starting_pos: optional array of codepoint indices if it wraps over more than one screen line
55 line_cache = {},
57 -- Given wrapping, any potential location for the text cursor can be described in two ways:
58 -- * schema 1: As a combination of line index and position within a line (in utf8 codepoint units)
59 -- * schema 2: As a combination of line index, screen line index within the line, and a position within the screen line.
61 -- Most of the time we'll only persist positions in schema 1, translating to
62 -- schema 2 when that's convenient.
64 -- Make sure these coordinates are never aliased, so that changing one causes
65 -- action at a distance.
67 -- On lines that are drawings, pos will be nil.
68 screen_top1 = {line=1, pos=1}, -- position of start of screen line at top of screen
69 cursor1 = {line=1, pos=1}, -- position of cursor; must be on a text line
71 selection1 = {},
72 -- some extra state to compute selection between mouse press and release
73 old_cursor1 = nil,
74 old_selection1 = nil,
75 mousepress_shift = nil,
77 -- cursor coordinates in pixels
78 cursor_x = 0,
79 cursor_y = 0,
81 current_drawing_mode = 'line', -- one of the available shape modes
82 previous_drawing_mode = nil, -- extra state for some ephemeral modes like moving/deleting/naming points
84 font = font,
85 font_height = font_height,
86 line_height = line_height,
88 top = top,
89 left = math.floor(left),
90 right = math.floor(right),
91 width = right-left,
93 filename = love.filesystem.getSourceBaseDirectory()..'/lines.txt', -- '/' should work even on Windows
94 next_save = nil,
96 -- undo
97 history = {},
98 next_history = 1,
100 -- search
101 search_term = nil,
102 search_backup = nil, -- stuff to restore when cancelling search
104 return result
105 end -- edit.initialize_state
107 function edit.check_locs(State)
108 -- if State is inconsistent (i.e. file changed by some other program),
109 -- throw away all cursor state entirely
110 if edit.invalid1(State, State.screen_top1)
111 or edit.invalid_cursor1(State)
112 or not edit.cursor_on_text(State)
113 or not Text.le1(State.screen_top1, State.cursor1) then
114 State.screen_top1 = {line=1, pos=1}
115 State.cursor1 = {line=1, pos=1}
116 edit.put_cursor_on_next_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_next_text_line(State)
143 local line = State.cursor1.line
144 while line < #State.lines do
145 line = line+1
146 if State.lines[line].mode == 'text' then
147 State.cursor1.line = line
148 State.cursor1.pos = 1
149 break
154 function edit.put_cursor_on_next_text_line_wrapping_around_if_necessary(State)
155 local line = State.cursor1.line
156 local max = #State.lines
157 for _ = 1, max-1 do
158 line = (line+1) % max
159 if State.lines[line].mode == 'text' then
160 State.cursor1.line = line
161 State.cursor1.pos = 1
162 break
167 function edit.put_cursor_on_next_text_loc_wrapping_around_if_necessary(State)
168 local cursor_line = State.lines[State.cursor1.line].data
169 if State.cursor1.pos <= utf8.len(cursor_line) then
170 State.cursor1.pos = State.cursor1.pos + 1
171 else
172 edit.put_cursor_on_next_text_line_wrapping_around_if_necessary(State)
176 function edit.draw(State)
177 State.button_handlers = {}
178 love.graphics.setFont(State.font)
179 App.color(Text_color)
180 assert(#State.lines == #State.line_cache, ('line_cache is out of date; %d elements when it should be %d'):format(#State.line_cache, #State.lines))
181 assert(Text.le1(State.screen_top1, State.cursor1), ('screen_top (line=%d,pos=%d) is below cursor (line=%d,pos=%d)'):format(State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos))
182 State.cursor_x = nil
183 State.cursor_y = nil
184 local y = State.top
185 --? print('== draw')
186 for line_index = State.screen_top1.line,#State.lines do
187 local line = State.lines[line_index]
188 --? print('draw:', y, line_index, line)
189 if y + State.line_height > App.screen.height then break end
190 if line.mode == 'text' then
191 --? print('text.draw', y, line_index)
192 local startpos = 1
193 if line_index == State.screen_top1.line then
194 startpos = State.screen_top1.pos
196 if line.data == '' then
197 -- button to insert new drawing
198 button(State, 'draw', {x=State.left-Margin_left+4, y=y+4, w=12,h=12, bg={r=1,g=1,b=0},
199 icon = icon.insert_drawing,
200 onpress1 = function()
201 Drawing.before = snapshot(State, line_index-1, line_index)
202 table.insert(State.lines, line_index, {mode='drawing', y=y, h=256/2, points={}, shapes={}, pending={}})
203 table.insert(State.line_cache, line_index, {})
204 if State.cursor1.line >= line_index then
205 State.cursor1.line = State.cursor1.line+1
207 record_undo_event(State, {before=Drawing.before, after=snapshot(State, line_index-1, line_index+1)})
208 Drawing.before = nil
209 schedule_save(State)
210 end,
213 y = Text.draw(State, line_index, y, startpos)
214 --? print('=> y', y)
215 elseif line.mode == 'drawing' then
216 y = y+Drawing_padding_top
217 Drawing.draw(State, line_index, y)
218 y = y + Drawing.pixels(line.h, State.width) + Drawing_padding_bottom
219 else
220 assert(false, ('unknown line mode %s'):format(line.mode))
223 if State.search_term then
224 Text.draw_search_bar(State)
228 function edit.update(State, dt)
229 Drawing.update(State, dt)
230 if State.next_save and State.next_save < Current_time then
231 save_to_disk(State)
232 State.next_save = nil
236 function schedule_save(State)
237 if State.next_save == nil then
238 State.next_save = Current_time + 3 -- short enough that you're likely to still remember what you did
242 function edit.quit(State)
243 -- make sure to save before quitting
244 if State.next_save then
245 save_to_disk(State)
246 -- give some time for the OS to flush everything to disk
247 love.timer.sleep(0.1)
251 function edit.mouse_press(State, x,y, mouse_button)
252 if State.search_term then return end
253 State.mouse_down = mouse_button
254 --? print_and_log(('edit.mouse_press: cursor at %d,%d'):format(State.cursor1.line, State.cursor1.pos))
255 if mouse_press_consumed_by_any_button(State, x,y, mouse_button) then
256 -- press on a button and it returned 'true' to short-circuit
257 return
260 if y < State.top then
261 State.old_cursor1 = State.cursor1
262 State.old_selection1 = State.selection1
263 State.mousepress_shift = App.shift_down()
264 State.selection1 = {
265 line=State.screen_top1.line,
266 pos=State.screen_top1.pos,
268 return
271 for line_index,line in ipairs(State.lines) do
272 if line.mode == 'text' then
273 if Text.in_line(State, line_index, x,y) then
274 -- delicate dance between cursor, selection and old cursor/selection
275 -- scenarios:
276 -- regular press+release: sets cursor, clears selection
277 -- shift press+release:
278 -- sets selection to old cursor if not set otherwise leaves it untouched
279 -- sets cursor
280 -- press and hold to start a selection: sets selection on press, cursor on release
281 -- press and hold, then press shift: ignore shift
282 -- i.e. mouse_release should never look at shift state
283 --? print_and_log(('edit.mouse_press: in line %d'):format(line_index))
284 State.old_cursor1 = State.cursor1
285 State.old_selection1 = State.selection1
286 State.mousepress_shift = App.shift_down()
287 State.selection1 = {
288 line=line_index,
289 pos=Text.to_pos_on_line(State, line_index, x, y),
291 return
293 elseif line.mode == 'drawing' then
294 if Drawing.in_drawing(State, line_index, x, y, State.left,State.right) then
295 State.lines.current_drawing_index = line_index
296 State.lines.current_drawing = line
297 Drawing.before = snapshot(State, line_index)
298 Drawing.mouse_press(State, line_index, x,y, mouse_button)
299 return
304 -- still here? mouse press is below all screen lines
305 State.old_cursor1 = State.cursor1
306 State.old_selection1 = State.selection1
307 State.mousepress_shift = App.shift_down()
308 State.selection1 = Text.final_text_loc_on_screen(State)
311 function edit.mouse_release(State, x,y, mouse_button)
312 if State.search_term then return end
313 --? print_and_log(('edit.mouse_release: cursor at %d,%d'):format(State.cursor1.line, State.cursor1.pos))
314 State.mouse_down = nil
315 if State.lines.current_drawing then
316 Drawing.mouse_release(State, x,y, mouse_button)
317 if Drawing.before then
318 record_undo_event(State, {before=Drawing.before, after=snapshot(State, State.lines.current_drawing_index)})
319 Drawing.before = nil
321 schedule_save(State)
322 else
323 --? print_and_log('edit.mouse_release: no current drawing')
324 if y < State.top then
325 State.cursor1 = deepcopy(State.screen_top1)
326 edit.clean_up_mouse_press(State)
327 return
330 for line_index,line in ipairs(State.lines) do
331 if line.mode == 'text' then
332 if Text.in_line(State, line_index, x,y) then
333 --? print_and_log(('edit.mouse_release: in line %d'):format(line_index))
334 State.cursor1 = {
335 line=line_index,
336 pos=Text.to_pos_on_line(State, line_index, x, y),
338 --? print_and_log(('edit.mouse_release: cursor now %d,%d'):format(State.cursor1.line, State.cursor1.pos))
339 edit.clean_up_mouse_press(State)
340 return
345 -- still here? mouse release is below all screen lines
346 State.cursor1 = Text.final_text_loc_on_screen(State)
347 edit.clean_up_mouse_press(State)
348 --? 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))
352 function edit.clean_up_mouse_press(State)
353 if State.mousepress_shift then
354 if State.old_selection1.line == nil then
355 State.selection1 = State.old_cursor1
356 else
357 State.selection1 = State.old_selection1
360 State.old_cursor1, State.old_selection1, State.mousepress_shift = nil
361 if eq(State.cursor1, State.selection1) then
362 State.selection1 = {}
366 function edit.mouse_wheel_move(State, dx,dy)
367 if dy > 0 then
368 State.cursor1 = deepcopy(State.screen_top1)
369 edit.put_cursor_on_next_text_line(State)
370 for i=1,math.floor(dy) do
371 Text.up(State)
373 elseif dy < 0 then
374 State.cursor1 = Text.screen_bottom1(State)
375 edit.put_cursor_on_next_text_line(State)
376 for i=1,math.floor(-dy) do
377 Text.down(State)
382 function edit.text_input(State, t)
383 --? print('text input', t)
384 if State.search_term then
385 State.search_term = State.search_term..t
386 Text.search_next(State)
387 elseif State.lines.current_drawing and State.current_drawing_mode == 'name' then
388 local before = snapshot(State, State.lines.current_drawing_index)
389 local drawing = State.lines.current_drawing
390 local p = drawing.points[drawing.pending.target_point]
391 p.name = p.name..t
392 record_undo_event(State, {before=before, after=snapshot(State, State.lines.current_drawing_index)})
393 else
394 local drawing_index, drawing = Drawing.current_drawing(State)
395 if drawing_index == nil then
396 Text.text_input(State, t)
399 schedule_save(State)
402 function edit.keychord_press(State, chord, key)
403 if State.selection1.line and
404 not State.lines.current_drawing and
405 -- printable character created using shift key => delete selection
406 -- (we're not creating any ctrl-shift- or alt-shift- combinations using regular/printable keys)
407 (not App.shift_down() or utf8.len(key) == 1) and
408 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) then
409 Text.delete_selection_and_record_undo_event(State)
411 if State.search_term then
412 if chord == 'escape' then
413 State.search_term = nil
414 State.cursor1 = State.search_backup.cursor
415 State.screen_top1 = State.search_backup.screen_top
416 State.search_backup = nil
417 Text.redraw_all(State) -- if we're scrolling, reclaim all line caches to avoid memory leaks
418 elseif chord == 'return' then
419 State.search_term = nil
420 State.search_backup = nil
421 elseif chord == 'backspace' then
422 local len = utf8.len(State.search_term)
423 local byte_offset = Text.offset(State.search_term, len)
424 State.search_term = string.sub(State.search_term, 1, byte_offset-1)
425 State.cursor = deepcopy(State.search_backup.cursor)
426 State.screen_top = deepcopy(State.search_backup.screen_top)
427 Text.search_next(State)
428 elseif chord == 'down' then
429 if #State.search_term > 0 then
430 edit.put_cursor_on_next_text_loc_wrapping_around_if_necessary(State)
431 Text.search_next(State)
433 elseif chord == 'up' then
434 Text.search_previous(State)
436 return
437 elseif chord == 'C-f' then
438 State.search_term = ''
439 State.search_backup = {
440 cursor={line=State.cursor1.line, pos=State.cursor1.pos},
441 screen_top={line=State.screen_top1.line, pos=State.screen_top1.pos},
443 -- zoom
444 elseif chord == 'C-=' then
445 edit.update_font_settings(State, State.font_height+2)
446 Text.redraw_all(State)
447 elseif chord == 'C--' then
448 if State.font_height > 2 then
449 edit.update_font_settings(State, State.font_height-2)
450 Text.redraw_all(State)
452 elseif chord == 'C-0' then
453 edit.update_font_settings(State, 20)
454 Text.redraw_all(State)
455 -- undo
456 elseif chord == 'C-z' then
457 local event = undo_event(State)
458 if event then
459 local src = event.before
460 State.screen_top1 = deepcopy(src.screen_top)
461 State.cursor1 = deepcopy(src.cursor)
462 State.selection1 = deepcopy(src.selection)
463 patch(State.lines, event.after, event.before)
464 -- invalidate various cached bits of lines
465 State.lines.current_drawing = nil
466 Text.redraw_all(State) -- if we're scrolling, reclaim all line caches to avoid memory leaks
467 schedule_save(State)
469 elseif chord == 'C-y' then
470 local event = redo_event(State)
471 if event then
472 local src = event.after
473 State.screen_top1 = deepcopy(src.screen_top)
474 State.cursor1 = deepcopy(src.cursor)
475 State.selection1 = deepcopy(src.selection)
476 patch(State.lines, event.before, event.after)
477 -- invalidate various cached bits of lines
478 State.lines.current_drawing = nil
479 Text.redraw_all(State) -- if we're scrolling, reclaim all line caches to avoid memory leaks
480 schedule_save(State)
482 -- clipboard
483 elseif chord == 'C-a' then
484 State.selection1 = {line=1, pos=1}
485 State.cursor1 = {line=#State.lines, pos=utf8.len(State.lines[#State.lines].data)+1}
486 elseif chord == 'C-c' then
487 local s = Text.selection(State)
488 if s then
489 App.set_clipboard(s)
491 elseif chord == 'C-x' then
492 local s = Text.cut_selection_and_record_undo_event(State)
493 if s then
494 App.set_clipboard(s)
496 schedule_save(State)
497 elseif chord == 'C-v' then
498 -- We don't have a good sense of when to scroll, so we'll be conservative
499 -- and sometimes scroll when we didn't quite need to.
500 local before_line = State.cursor1.line
501 local before = snapshot(State, before_line)
502 local clipboard_data = App.get_clipboard()
503 for _,code in utf8.codes(clipboard_data) do
504 local c = utf8.char(code)
505 if c == '\n' then
506 Text.insert_return(State)
507 else
508 Text.insert_at_cursor(State, c)
511 if Text.cursor_out_of_screen(State) then
512 Text.snap_cursor_to_bottom_of_screen(State, State.left, State.right)
514 record_undo_event(State, {before=before, after=snapshot(State, before_line, State.cursor1.line)})
515 schedule_save(State)
516 -- dispatch to drawing or text
517 elseif App.mouse_down(1) or chord:sub(1,2) == 'C-' then
518 local drawing_index, drawing = Drawing.current_drawing(State)
519 if drawing_index then
520 local before = snapshot(State, drawing_index)
521 Drawing.keychord_press(State, chord)
522 record_undo_event(State, {before=before, after=snapshot(State, drawing_index)})
523 schedule_save(State)
525 elseif chord == 'escape' and not App.mouse_down(1) then
526 for _,line in ipairs(State.lines) do
527 if line.mode == 'drawing' then
528 line.show_help = false
531 elseif State.lines.current_drawing and State.current_drawing_mode == 'name' then
532 if chord == 'return' then
533 State.current_drawing_mode = State.previous_drawing_mode
534 State.previous_drawing_mode = nil
535 else
536 local before = snapshot(State, State.lines.current_drawing_index)
537 local drawing = State.lines.current_drawing
538 local p = drawing.points[drawing.pending.target_point]
539 if chord == 'escape' then
540 p.name = nil
541 record_undo_event(State, {before=before, after=snapshot(State, State.lines.current_drawing_index)})
542 elseif chord == 'backspace' then
543 local len = utf8.len(p.name)
544 if len > 0 then
545 local byte_offset = Text.offset(p.name, len-1)
546 if len == 1 then byte_offset = 0 end
547 p.name = string.sub(p.name, 1, byte_offset)
548 record_undo_event(State, {before=before, after=snapshot(State, State.lines.current_drawing_index)})
552 schedule_save(State)
553 else
554 Text.keychord_press(State, chord)
558 function edit.key_release(State, key, scancode)
561 function edit.update_font_settings(State, font_height, font)
562 State.font_height = font_height
563 State.font = font or love.graphics.newFont(State.font_height)
564 State.line_height = math.floor(font_height*1.3)
567 --== some methods for tests
569 -- Insulate tests from some key globals so I don't have to change the vast
570 -- majority of tests when they're modified for the real app.
571 Test_margin_left = 25
572 Test_margin_right = 0
574 function edit.initialize_test_state()
575 -- if you change these values, tests will start failing
576 return edit.initialize_state(
577 15, -- top margin
578 Test_margin_left,
579 App.screen.width - Test_margin_right,
580 love.graphics.getFont(),
582 15) -- line height
585 -- all text_input events are also keypresses
586 -- TODO: handle chords of multiple keys
587 function edit.run_after_text_input(State, t)
588 edit.keychord_press(State, t)
589 edit.text_input(State, t)
590 edit.key_release(State, t)
591 App.screen.contents = {}
592 edit.update(State, 0)
593 edit.draw(State)
596 -- not all keys are text_input
597 function edit.run_after_keychord(State, chord, key)
598 edit.keychord_press(State, chord, key)
599 edit.key_release(State, key)
600 App.screen.contents = {}
601 edit.update(State, 0)
602 edit.draw(State)
605 function edit.run_after_mouse_click(State, x,y, mouse_button)
606 App.fake_mouse_press(x,y, mouse_button)
607 edit.mouse_press(State, x,y, mouse_button)
608 edit.draw(State)
609 App.fake_mouse_release(x,y, mouse_button)
610 edit.mouse_release(State, x,y, mouse_button)
611 App.screen.contents = {}
612 edit.update(State, 0)
613 edit.draw(State)
616 function edit.run_after_mouse_press(State, x,y, mouse_button)
617 App.fake_mouse_press(x,y, mouse_button)
618 edit.mouse_press(State, x,y, mouse_button)
619 App.screen.contents = {}
620 edit.update(State, 0)
621 edit.draw(State)
624 function edit.run_after_mouse_release(State, x,y, mouse_button)
625 App.fake_mouse_release(x,y, mouse_button)
626 edit.mouse_release(State, x,y, mouse_button)
627 App.screen.contents = {}
628 edit.update(State, 0)
629 edit.draw(State)