bugfix in search UI
[view.love.git] / source_edit.lua
blob90c117174ae3f39d17652845e6f480399ef72e5a
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 Hyperlink_decoration_color = {r=0.4, g=0.4, b=1}
5 Stroke_color = {r=0, g=0, b=0}
6 Current_stroke_color = {r=0.7, g=0.7, b=0.7} -- in process of being drawn
7 Current_name_background_color = {r=1, g=0, b=0, a=0.1} -- name currently being edited
8 Focus_stroke_color = {r=1, g=0, b=0} -- what mouse is hovering over
9 Highlight_color = {r=0.7, g=0.7, b=0.9} -- selected text
10 Line_number_color = {r=0.6, g=0.6, b=0.6}
11 Icon_color = {r=0.7, g=0.7, b=0.7} -- color of current mode icon in drawings
12 Help_color = {r=0, g=0.5, b=0}
13 Help_background_color = {r=0, g=0.5, b=0, a=0.1}
15 Margin_top = 15
16 Margin_left = 25
17 Margin_right = 25
19 Drawing_padding_top = 10
20 Drawing_padding_bottom = 10
21 Drawing_padding_height = Drawing_padding_top + Drawing_padding_bottom
23 Same_point_distance = 4 -- pixel distance at which two points are considered the same
25 edit = {}
27 -- run in both tests and a real run
28 function edit.initialize_state(top, left, right, font, font_height, line_height) -- currently always draws to bottom of screen
29 local result = {
30 -- a line is either text or a drawing
31 -- a text is a table with:
32 -- mode = 'text',
33 -- string data,
34 -- a drawing is a table with:
35 -- mode = 'drawing'
36 -- a (h)eight,
37 -- an array of points, and
38 -- an array of shapes
39 -- a shape is a table containing:
40 -- a mode
41 -- an array points for mode 'freehand' (raw x,y coords; freehand drawings don't pollute the points array of a drawing)
42 -- an array vertices for mode 'polygon', 'rectangle', 'square'
43 -- p1, p2 for mode 'line'
44 -- center, radius for mode 'circle'
45 -- center, radius, start_angle, end_angle for mode 'arc'
46 -- Unless otherwise specified, coord fields are normalized; a drawing is always 256 units wide
47 -- The field names are carefully chosen so that switching modes in midstream
48 -- remembers previously entered points where that makes sense.
49 lines = {{mode='text', data=''}}, -- array of lines
51 -- Lines can be too long to fit on screen, in which case they _wrap_ into
52 -- multiple _screen lines_.
54 -- rendering wrapped text lines needs some additional short-lived data per line:
55 -- startpos, the index of data the line starts rendering from, can only be >1 for topmost line on screen
56 -- screen_line_starting_pos: optional array of codepoint indices if it wraps over more than one screen line
57 line_cache = {},
59 -- Given wrapping, any potential location for the text cursor can be described in two ways:
60 -- * schema 1: As a combination of line index and position within a line (in utf8 codepoint units)
61 -- * schema 2: As a combination of line index, screen line index within the line, and a position within the screen line.
63 -- Most of the time we'll only persist positions in schema 1, translating to
64 -- schema 2 when that's convenient.
66 -- Make sure these coordinates are never aliased, so that changing one causes
67 -- action at a distance.
69 -- On lines that are drawings, pos will be nil.
70 screen_top1 = {line=1, pos=1}, -- position of start of screen line at top of screen
71 cursor1 = {line=1, pos=1}, -- position of cursor; must be on a text line
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 = font,
87 font_height = font_height,
88 line_height = line_height,
90 top = top,
91 left = math.floor(left), -- left margin for text; line numbers go to the left of this
92 right = math.floor(right),
93 width = right-left,
95 filename = 'run.lua',
96 next_save = nil,
98 -- undo
99 history = {},
100 next_history = 1,
102 -- search
103 search_term = nil,
104 search_backup = nil, -- stuff to restore when cancelling search
106 return result
107 end -- edit.initialize_state
109 function edit.check_locs(State)
110 -- if State is inconsistent (i.e. file changed by some other program),
111 -- throw away all cursor state entirely
112 if edit.invalid1(State, State.screen_top1)
113 or edit.invalid_cursor1(State)
114 or not edit.cursor_on_text(State)
115 or not Text.le1(State.screen_top1, State.cursor1) then
116 State.screen_top1 = {line=1, pos=1}
117 State.cursor1 = {line=1, pos=1}
118 edit.put_cursor_on_next_text_line(State)
122 function edit.invalid1(State, loc1)
123 if loc1.line > #State.lines then return true end
124 local l = State.lines[loc1.line]
125 if l.mode ~= 'text' then return false end -- pos is irrelevant to validity for a drawing line
126 return loc1.pos > #State.lines[loc1.line].data
129 -- cursor loc in particular differs from other locs in one way:
130 -- pos might occur just after end of line
131 function edit.invalid_cursor1(State)
132 local cursor1 = State.cursor1
133 if cursor1.line > #State.lines then return true end
134 local l = State.lines[cursor1.line]
135 if l.mode ~= 'text' then return false end -- pos is irrelevant to validity for a drawing line
136 return cursor1.pos > #State.lines[cursor1.line].data + 1
139 function edit.cursor_on_text(State)
140 return State.cursor1.line <= #State.lines
141 and State.lines[State.cursor1.line].mode == 'text'
144 function edit.put_cursor_on_next_text_line(State)
145 local line = State.cursor1.line
146 while line < #State.lines do
147 line = line+1
148 if State.lines[line].mode == 'text' then
149 State.cursor1.line = line
150 State.cursor1.pos = 1
151 break
156 function edit.put_cursor_on_next_text_line_wrapping_around_if_necessary(State)
157 local line = State.cursor1.line
158 local max = #State.lines
159 for _ = 1, max-1 do
160 line = (line+1) % max
161 if State.lines[line].mode == 'text' then
162 State.cursor1.line = line
163 State.cursor1.pos = 1
164 break
169 function edit.put_cursor_on_next_text_loc_wrapping_around_if_necessary(State)
170 local cursor_line = State.lines[State.cursor1.line].data
171 if State.cursor1.pos <= utf8.len(cursor_line) then
172 State.cursor1.pos = State.cursor1.pos + 1
173 else
174 edit.put_cursor_on_next_text_line_wrapping_around_if_necessary(State)
178 function edit.draw(State, hide_cursor, show_line_numbers)
179 State.button_handlers = {}
180 love.graphics.setFont(State.font)
181 App.color(Text_color)
182 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))
183 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))
184 State.cursor_x = nil
185 State.cursor_y = nil
186 local y = State.top
187 --? print('== draw')
188 for line_index = State.screen_top1.line,#State.lines do
189 local line = State.lines[line_index]
190 --? print('draw:', y, line_index, line)
191 if y + State.line_height > App.screen.height then break end
192 if line.mode == 'text' then
193 --? print('text.draw', y, line_index)
194 local startpos = 1
195 if line_index == State.screen_top1.line then
196 startpos = State.screen_top1.pos
198 if line.data == '' then
199 -- button to insert new drawing
200 local buttonx = State.left-Margin_left+4
201 if show_line_numbers then
202 buttonx = 4 -- HACK: position draw buttons at a fixed x on screen
204 button(State, 'draw', {x=buttonx, y=y+4, w=12,h=12, bg={r=1,g=1,b=0},
205 icon = icon.insert_drawing,
206 onpress1 = function()
207 Drawing.before = snapshot(State, line_index-1, line_index)
208 table.insert(State.lines, line_index, {mode='drawing', y=y, h=256/2, points={}, shapes={}, pending={}})
209 table.insert(State.line_cache, line_index, {})
210 if State.cursor1.line >= line_index then
211 State.cursor1.line = State.cursor1.line+1
213 record_undo_event(State, {before=Drawing.before, after=snapshot(State, line_index-1, line_index+1)})
214 Drawing.before = nil
215 schedule_save(State)
216 end,
219 y = Text.draw(State, line_index, y, startpos, hide_cursor, show_line_numbers)
220 --? print('=> y', y)
221 elseif line.mode == 'drawing' then
222 y = y+Drawing_padding_top
223 Drawing.draw(State, line_index, y)
224 y = y + Drawing.pixels(line.h, State.width) + Drawing_padding_bottom
225 else
226 assert(false, ('unknown line mode %s'):format(line.mode))
229 if State.search_term then
230 Text.draw_search_bar(State)
234 function edit.update(State, dt)
235 Drawing.update(State, dt)
236 if State.next_save and State.next_save < Current_time then
237 save_to_disk(State)
238 State.next_save = nil
242 function schedule_save(State)
243 if State.next_save == nil then
244 State.next_save = Current_time + 3 -- short enough that you're likely to still remember what you did
248 function edit.quit(State)
249 -- make sure to save before quitting
250 if State.next_save then
251 save_to_disk(State)
252 -- give some time for the OS to flush everything to disk
253 love.timer.sleep(0.1)
257 function edit.mouse_press(State, x,y, mouse_button)
258 if State.search_term then return end
259 State.mouse_down = mouse_button
260 --? print_and_log(('edit.mouse_press: cursor at %d,%d'):format(State.cursor1.line, State.cursor1.pos))
261 if mouse_press_consumed_by_any_button(State, x,y, mouse_button) then
262 -- press on a button and it returned 'true' to short-circuit
263 return
266 if y < State.top then
267 State.old_cursor1 = State.cursor1
268 State.old_selection1 = State.selection1
269 State.mousepress_shift = App.shift_down()
270 State.selection1 = {
271 line=State.screen_top1.line,
272 pos=State.screen_top1.pos,
274 return
277 for line_index,line in ipairs(State.lines) do
278 if line.mode == 'text' then
279 if Text.in_line(State, line_index, x,y) then
280 -- delicate dance between cursor, selection and old cursor/selection
281 -- scenarios:
282 -- regular press+release: sets cursor, clears selection
283 -- shift press+release:
284 -- sets selection to old cursor if not set otherwise leaves it untouched
285 -- sets cursor
286 -- press and hold to start a selection: sets selection on press, cursor on release
287 -- press and hold, then press shift: ignore shift
288 -- i.e. mouse_release should never look at shift state
289 --? print_and_log(('edit.mouse_press: in line %d'):format(line_index))
290 State.old_cursor1 = State.cursor1
291 State.old_selection1 = State.selection1
292 State.mousepress_shift = App.shift_down()
293 State.selection1 = {
294 line=line_index,
295 pos=Text.to_pos_on_line(State, line_index, x, y),
297 return
299 elseif line.mode == 'drawing' then
300 if Drawing.in_drawing(State, line_index, x, y, State.left,State.right) then
301 State.lines.current_drawing_index = line_index
302 State.lines.current_drawing = line
303 Drawing.before = snapshot(State, line_index)
304 Drawing.mouse_press(State, line_index, x,y, mouse_button)
305 return
310 -- still here? mouse press is below all screen lines
311 State.old_cursor1 = State.cursor1
312 State.old_selection1 = State.selection1
313 State.mousepress_shift = App.shift_down()
314 State.selection1 = Text.final_text_loc_on_screen(State)
317 function edit.mouse_release(State, x,y, mouse_button)
318 if State.search_term then return end
319 --? print_and_log(('edit.mouse_release: cursor at %d,%d'):format(State.cursor1.line, State.cursor1.pos))
320 State.mouse_down = nil
321 if State.lines.current_drawing then
322 Drawing.mouse_release(State, x,y, mouse_button)
323 if Drawing.before then
324 record_undo_event(State, {before=Drawing.before, after=snapshot(State, State.lines.current_drawing_index)})
325 Drawing.before = nil
327 schedule_save(State)
328 else
329 --? print_and_log('edit.mouse_release: no current drawing')
330 if y < State.top then
331 State.cursor1 = deepcopy(State.screen_top1)
332 edit.clean_up_mouse_press(State)
333 return
336 for line_index,line in ipairs(State.lines) do
337 if line.mode == 'text' then
338 if Text.in_line(State, line_index, x,y) then
339 --? print_and_log(('edit.mouse_release: in line %d'):format(line_index))
340 State.cursor1 = {
341 line=line_index,
342 pos=Text.to_pos_on_line(State, line_index, x, y),
344 --? print_and_log(('edit.mouse_release: cursor now %d,%d'):format(State.cursor1.line, State.cursor1.pos))
345 edit.clean_up_mouse_press(State)
346 return
351 -- still here? mouse release is below all screen lines
352 State.cursor1 = Text.final_text_loc_on_screen(State)
353 edit.clean_up_mouse_press(State)
354 --? 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))
358 function edit.clean_up_mouse_press(State)
359 if State.mousepress_shift then
360 if State.old_selection1.line == nil then
361 State.selection1 = State.old_cursor1
362 else
363 State.selection1 = State.old_selection1
366 State.old_cursor1, State.old_selection1, State.mousepress_shift = nil
367 if eq(State.cursor1, State.selection1) then
368 State.selection1 = {}
372 function edit.mouse_wheel_move(State, dx,dy)
373 if dy > 0 then
374 State.cursor1 = deepcopy(State.screen_top1)
375 edit.put_cursor_on_next_text_line(State)
376 for i=1,math.floor(dy) do
377 Text.up(State)
379 elseif dy < 0 then
380 State.cursor1 = Text.screen_bottom1(State)
381 edit.put_cursor_on_next_text_line(State)
382 for i=1,math.floor(-dy) do
383 Text.down(State)
388 function edit.text_input(State, t)
389 --? print('text input', t)
390 if State.search_term then
391 State.search_term = State.search_term..t
392 Text.search_next(State)
393 elseif State.lines.current_drawing and State.current_drawing_mode == 'name' then
394 local before = snapshot(State, State.lines.current_drawing_index)
395 local drawing = State.lines.current_drawing
396 local p = drawing.points[drawing.pending.target_point]
397 p.name = p.name..t
398 record_undo_event(State, {before=before, after=snapshot(State, State.lines.current_drawing_index)})
399 else
400 local drawing_index, drawing = Drawing.current_drawing(State)
401 if drawing_index == nil then
402 Text.text_input(State, t)
405 schedule_save(State)
408 function edit.keychord_press(State, chord, key)
409 if State.selection1.line and
410 not State.lines.current_drawing and
411 -- printable character created using shift key => delete selection
412 -- (we're not creating any ctrl-shift- or alt-shift- combinations using regular/printable keys)
413 (not App.shift_down() or utf8.len(key) == 1) and
414 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
415 Text.delete_selection_and_record_undo_event(State)
417 if State.search_term then
418 if chord == 'escape' then
419 State.search_term = nil
420 State.cursor1 = State.search_backup.cursor
421 State.screen_top1 = State.search_backup.screen_top
422 State.search_backup = nil
423 Text.redraw_all(State) -- if we're scrolling, reclaim all line caches to avoid memory leaks
424 elseif chord == 'return' then
425 State.search_term = nil
426 State.search_backup = nil
427 elseif chord == 'backspace' then
428 local len = utf8.len(State.search_term)
429 local byte_offset = Text.offset(State.search_term, len)
430 State.search_term = string.sub(State.search_term, 1, byte_offset-1)
431 State.cursor = deepcopy(State.search_backup.cursor)
432 State.screen_top = deepcopy(State.search_backup.screen_top)
433 Text.search_next(State)
434 elseif chord == 'down' then
435 edit.put_cursor_on_next_text_loc_wrapping_around_if_necessary(State)
436 Text.search_next(State)
437 elseif chord == 'up' then
438 Text.search_previous(State)
440 return
441 elseif chord == 'C-f' then
442 State.search_term = ''
443 State.search_backup = {
444 cursor={line=State.cursor1.line, pos=State.cursor1.pos},
445 screen_top={line=State.screen_top1.line, pos=State.screen_top1.pos},
447 -- zoom
448 elseif chord == 'C-=' then
449 edit.update_font_settings(State, State.font_height+2)
450 Text.redraw_all(State)
451 elseif chord == 'C--' then
452 if State.font_height > 2 then
453 edit.update_font_settings(State, State.font_height-2)
454 Text.redraw_all(State)
456 elseif chord == 'C-0' then
457 edit.update_font_settings(State, 20)
458 Text.redraw_all(State)
459 -- undo
460 elseif chord == 'C-z' then
461 local event = undo_event(State)
462 if event then
463 local src = event.before
464 State.screen_top1 = deepcopy(src.screen_top)
465 State.cursor1 = deepcopy(src.cursor)
466 State.selection1 = deepcopy(src.selection)
467 patch(State.lines, event.after, event.before)
468 -- invalidate various cached bits of lines
469 State.lines.current_drawing = nil
470 Text.redraw_all(State) -- if we're scrolling, reclaim all line caches to avoid memory leaks
471 schedule_save(State)
473 elseif chord == 'C-y' then
474 local event = redo_event(State)
475 if event then
476 local src = event.after
477 State.screen_top1 = deepcopy(src.screen_top)
478 State.cursor1 = deepcopy(src.cursor)
479 State.selection1 = deepcopy(src.selection)
480 patch(State.lines, event.before, event.after)
481 -- invalidate various cached bits of lines
482 State.lines.current_drawing = nil
483 Text.redraw_all(State) -- if we're scrolling, reclaim all line caches to avoid memory leaks
484 schedule_save(State)
486 -- clipboard
487 elseif chord == 'C-a' then
488 State.selection1 = {line=1, pos=1}
489 State.cursor1 = {line=#State.lines, pos=utf8.len(State.lines[#State.lines].data)+1}
490 elseif chord == 'C-c' then
491 local s = Text.selection(State)
492 if s then
493 App.set_clipboard(s)
495 elseif chord == 'C-x' then
496 local s = Text.cut_selection_and_record_undo_event(State)
497 if s then
498 App.set_clipboard(s)
500 schedule_save(State)
501 elseif chord == 'C-v' then
502 -- We don't have a good sense of when to scroll, so we'll be conservative
503 -- and sometimes scroll when we didn't quite need to.
504 local before_line = State.cursor1.line
505 local before = snapshot(State, before_line)
506 local clipboard_data = App.get_clipboard()
507 for _,code in utf8.codes(clipboard_data) do
508 local c = utf8.char(code)
509 if c == '\n' then
510 Text.insert_return(State)
511 else
512 Text.insert_at_cursor(State, c)
515 if Text.cursor_out_of_screen(State) then
516 Text.snap_cursor_to_bottom_of_screen(State, State.left, State.right)
518 record_undo_event(State, {before=before, after=snapshot(State, before_line, State.cursor1.line)})
519 schedule_save(State)
520 -- dispatch to drawing or text
521 elseif App.mouse_down(1) or chord:sub(1,2) == 'C-' then
522 local drawing_index, drawing = Drawing.current_drawing(State)
523 if drawing_index then
524 local before = snapshot(State, drawing_index)
525 Drawing.keychord_press(State, chord)
526 record_undo_event(State, {before=before, after=snapshot(State, drawing_index)})
527 schedule_save(State)
529 elseif chord == 'escape' and not App.mouse_down(1) then
530 for _,line in ipairs(State.lines) do
531 if line.mode == 'drawing' then
532 line.show_help = false
535 elseif State.lines.current_drawing and State.current_drawing_mode == 'name' then
536 if chord == 'return' then
537 State.current_drawing_mode = State.previous_drawing_mode
538 State.previous_drawing_mode = nil
539 else
540 local before = snapshot(State, State.lines.current_drawing_index)
541 local drawing = State.lines.current_drawing
542 local p = drawing.points[drawing.pending.target_point]
543 if chord == 'escape' then
544 p.name = nil
545 record_undo_event(State, {before=before, after=snapshot(State, State.lines.current_drawing_index)})
546 elseif chord == 'backspace' then
547 local len = utf8.len(p.name)
548 if len > 0 then
549 local byte_offset = Text.offset(p.name, len-1)
550 if len == 1 then byte_offset = 0 end
551 p.name = string.sub(p.name, 1, byte_offset)
552 record_undo_event(State, {before=before, after=snapshot(State, State.lines.current_drawing_index)})
556 schedule_save(State)
557 else
558 Text.keychord_press(State, chord)
562 function edit.key_release(State, key, scancode)
565 function edit.update_font_settings(State, font_height)
566 State.font_height = font_height
567 State.font = love.graphics.newFont(State.font_height)
568 State.line_height = math.floor(font_height*1.3)
571 --== some methods for tests
573 -- Insulate tests from some key globals so I don't have to change the vast
574 -- majority of tests when they're modified for the real app.
575 Test_margin_left = 25
576 Test_margin_right = 0
578 function edit.initialize_test_state()
579 -- if you change these values, tests will start failing
580 return edit.initialize_state(
581 15, -- top margin
582 Test_margin_left,
583 App.screen.width - Test_margin_right,
584 love.graphics.getFont(),
586 15) -- line height
589 -- all text_input events are also keypresses
590 -- TODO: handle chords of multiple keys
591 function edit.run_after_text_input(State, t)
592 edit.keychord_press(State, t)
593 edit.text_input(State, t)
594 edit.key_release(State, t)
595 App.screen.contents = {}
596 edit.update(State, 0)
597 edit.draw(State)
600 -- not all keys are text_input
601 function edit.run_after_keychord(State, chord, key)
602 edit.keychord_press(State, chord, key)
603 edit.key_release(State, key)
604 App.screen.contents = {}
605 edit.update(State, 0)
606 edit.draw(State)
609 function edit.run_after_mouse_click(State, x,y, mouse_button)
610 App.fake_mouse_press(x,y, mouse_button)
611 edit.mouse_press(State, x,y, mouse_button)
612 edit.draw(State)
613 App.fake_mouse_release(x,y, mouse_button)
614 edit.mouse_release(State, x,y, mouse_button)
615 App.screen.contents = {}
616 edit.update(State, 0)
617 edit.draw(State)
620 function edit.run_after_mouse_press(State, x,y, mouse_button)
621 App.fake_mouse_press(x,y, mouse_button)
622 edit.mouse_press(State, x,y, mouse_button)
623 App.screen.contents = {}
624 edit.update(State, 0)
625 edit.draw(State)
628 function edit.run_after_mouse_release(State, x,y, mouse_button)
629 App.fake_mouse_release(x,y, mouse_button)
630 edit.mouse_release(State, x,y, mouse_button)
631 App.screen.contents = {}
632 edit.update(State, 0)
633 edit.draw(State)