bugfix to the helper I added yesterday
[view.love.git] / source_edit.lua
blob5c7775886488004474f35feb199df0e784742f91
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_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 (y) coord in pixels (updated while painting screen),
37 -- a (h)eight,
38 -- an array of points, and
39 -- an array of shapes
40 -- a shape is a table containing:
41 -- a mode
42 -- an array points for mode 'freehand' (raw x,y coords; freehand drawings don't pollute the points array of a drawing)
43 -- an array vertices for mode 'polygon', 'rectangle', 'square'
44 -- p1, p2 for mode 'line'
45 -- center, radius for mode 'circle'
46 -- center, radius, start_angle, end_angle for mode 'arc'
47 -- Unless otherwise specified, coord fields are normalized; a drawing is always 256 units wide
48 -- The field names are carefully chosen so that switching modes in midstream
49 -- remembers previously entered points where that makes sense.
50 lines = {{mode='text', data=''}}, -- array of lines
52 -- Lines can be too long to fit on screen, in which case they _wrap_ into
53 -- multiple _screen lines_.
55 -- rendering wrapped text lines needs some additional short-lived data per line:
56 -- startpos, the index of data the line starts rendering from, can only be >1 for topmost line on screen
57 -- starty, the y coord in pixels the line starts rendering from
58 -- fragments: snippets of the line guaranteed to not straddle screen lines
59 -- screen_line_starting_pos: optional array of grapheme indices if it wraps over more than one screen line
60 line_cache = {},
62 -- Given wrapping, any potential location for the text cursor can be described in two ways:
63 -- * schema 1: As a combination of line index and position within a line (in utf8 codepoint units)
64 -- * schema 2: As a combination of line index, screen line index within the line, and a position within the screen line.
66 -- Most of the time we'll only persist positions in schema 1, translating to
67 -- schema 2 when that's convenient.
69 -- Make sure these coordinates are never aliased, so that changing one causes
70 -- action at a distance.
71 screen_top1 = {line=1, pos=1}, -- position of start of screen line at top of screen
72 cursor1 = {line=1, pos=1}, -- position of cursor
73 screen_bottom1 = {line=1, pos=1}, -- position of start of screen line at bottom of screen
75 selection1 = {},
76 -- some extra state to compute selection between mouse press and release
77 old_cursor1 = nil,
78 old_selection1 = nil,
79 mousepress_shift = nil,
81 -- cursor coordinates in pixels
82 cursor_x = 0,
83 cursor_y = 0,
85 current_drawing_mode = 'line',
86 previous_drawing_mode = nil, -- extra state for some ephemeral modes like moving/deleting/naming points
88 font_height = font_height,
89 line_height = line_height,
91 top = top,
92 left = math.floor(left), -- left margin for text; line numbers go to the left of this
93 right = math.floor(right),
94 width = right-left,
96 filename = love.filesystem.getSourceBaseDirectory()..'/lines.txt', -- '/' should work even on Windows
97 next_save = nil,
99 -- undo
100 history = {},
101 next_history = 1,
103 -- search
104 search_term = nil,
105 search_backup = nil, -- stuff to restore when cancelling search
107 return result
108 end -- App.initialize_state
110 function edit.check_locs(State)
111 -- if State is inconsistent (i.e. file changed by some other program),
112 -- throw away all cursor state entirely
113 if edit.invalid1(State, State.screen_top1)
114 or edit.invalid1(State, State.cursor1)
115 or not edit.cursor_on_text(State)
116 or not Text.le1(State.screen_top1, State.cursor1) then
117 State.screen_top1 = {line=1, pos=1}
118 State.cursor1 = {line=1, pos=1}
119 edit.put_cursor_on_next_text_line(State)
123 function edit.invalid1(State, loc1)
124 if loc1.line > #State.lines then return true end
125 local l = State.lines[loc1.line]
126 if l.mode ~= 'text' then return false end -- pos is irrelevant to validity for a drawing line
127 return loc1.pos > #State.lines[loc1.line].data
130 function edit.cursor_on_text(State)
131 return State.cursor1.line <= #State.lines
132 and State.lines[State.cursor1.line].mode == 'text'
135 function edit.put_cursor_on_next_text_line(State)
136 while true do
137 if State.cursor1.line >= #State.lines then
138 break
140 if State.lines[State.cursor1.line].mode == 'text' then
141 break
143 State.cursor1.line = State.cursor1.line+1
144 State.cursor1.pos = 1
148 function edit.draw(State, hide_cursor, show_line_numbers)
149 State.button_handlers = {}
150 App.color(Text_color)
151 if #State.lines ~= #State.line_cache then
152 print(('line_cache is out of date; %d when it should be %d'):format(#State.line_cache, #State.lines))
153 assert(false)
155 if not Text.le1(State.screen_top1, State.cursor1) then
156 print(State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos)
157 assert(false)
159 State.cursor_x = nil
160 State.cursor_y = nil
161 local y = State.top
162 local screen_bottom1 = {line=nil, pos=nil}
163 --? print('== draw')
164 for line_index = State.screen_top1.line,#State.lines do
165 local line = State.lines[line_index]
166 --? print('draw:', y, line_index, line)
167 if y + State.line_height > App.screen.height then break end
168 screen_bottom1.line = line_index
169 if line.mode == 'text' then
170 --? print('text.draw', y, line_index)
171 local startpos = 1
172 if line_index == State.screen_top1.line then
173 startpos = State.screen_top1.pos
175 if line.data == '' then
176 -- button to insert new drawing
177 local buttonx = State.left-Margin_left+4
178 if show_line_numbers then
179 buttonx = 4 -- HACK: position draw buttons at a fixed x on screen
181 button(State, 'draw', {x=buttonx, 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, hide_cursor, show_line_numbers)
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)
212 function edit.update(State, dt)
213 Drawing.update(State, dt)
214 if State.next_save and State.next_save < Current_time then
215 save_to_disk(State)
216 State.next_save = nil
220 function schedule_save(State)
221 if State.next_save == nil then
222 State.next_save = Current_time + 3 -- short enough that you're likely to still remember what you did
226 function edit.quit(State)
227 -- make sure to save before quitting
228 if State.next_save then
229 save_to_disk(State)
230 -- give some time for the OS to flush everything to disk
231 love.timer.sleep(0.1)
235 function edit.mouse_press(State, x,y, mouse_button)
236 if State.search_term then return end
237 --? print_and_log(('edit.mouse_press: cursor at %d,%d'):format(State.cursor1.line, State.cursor1.pos))
238 if mouse_press_consumed_by_any_button_handler(State, x,y, mouse_button) then
239 -- press on a button and it returned 'true' to short-circuit
240 return
243 if y < State.top then
244 State.old_cursor1 = State.cursor1
245 State.old_selection1 = State.selection1
246 State.mousepress_shift = App.shift_down()
247 State.selection1 = {
248 line=State.screen_top1.line,
249 pos=State.screen_top1.pos,
251 return
254 for line_index,line in ipairs(State.lines) do
255 if line.mode == 'text' then
256 if Text.in_line(State, line_index, x,y) then
257 -- delicate dance between cursor, selection and old cursor/selection
258 -- scenarios:
259 -- regular press+release: sets cursor, clears selection
260 -- shift press+release:
261 -- sets selection to old cursor if not set otherwise leaves it untouched
262 -- sets cursor
263 -- press and hold to start a selection: sets selection on press, cursor on release
264 -- press and hold, then press shift: ignore shift
265 -- i.e. mouse_release should never look at shift state
266 --? print_and_log(('edit.mouse_press: in line %d'):format(line_index))
267 State.old_cursor1 = State.cursor1
268 State.old_selection1 = State.selection1
269 State.mousepress_shift = App.shift_down()
270 State.selection1 = {
271 line=line_index,
272 pos=Text.to_pos_on_line(State, line_index, x, y),
274 return
276 elseif line.mode == 'drawing' then
277 local line_cache = State.line_cache[line_index]
278 if Drawing.in_drawing(line, line_cache, x, y, State.left,State.right) then
279 State.lines.current_drawing_index = line_index
280 State.lines.current_drawing = line
281 Drawing.before = snapshot(State, line_index)
282 Drawing.mouse_press(State, line_index, x,y, mouse_button)
283 return
288 -- still here? click is below all screen lines
289 State.old_cursor1 = State.cursor1
290 State.old_selection1 = State.selection1
291 State.mousepress_shift = App.shift_down()
292 State.selection1 = {
293 line=State.screen_bottom1.line,
294 pos=Text.pos_at_end_of_screen_line(State, State.screen_bottom1),
298 function edit.mouse_release(State, x,y, mouse_button)
299 if State.search_term then return end
300 --? print_and_log(('edit.mouse_release: cursor at %d,%d'):format(State.cursor1.line, State.cursor1.pos))
301 if State.lines.current_drawing then
302 Drawing.mouse_release(State, x,y, mouse_button)
303 schedule_save(State)
304 if Drawing.before then
305 record_undo_event(State, {before=Drawing.before, after=snapshot(State, State.lines.current_drawing_index)})
306 Drawing.before = nil
308 else
309 --? print_and_log('edit.mouse_release: no current drawing')
310 for line_index,line in ipairs(State.lines) do
311 if line.mode == 'text' then
312 if Text.in_line(State, line_index, x,y) then
313 --? print_and_log(('edit.mouse_release: in line %d'):format(line_index))
314 State.cursor1 = {
315 line=line_index,
316 pos=Text.to_pos_on_line(State, line_index, x, y),
318 --? print_and_log(('edit.mouse_release: cursor now %d,%d'):format(State.cursor1.line, State.cursor1.pos))
319 if State.mousepress_shift then
320 if State.old_selection1.line == nil then
321 State.selection1 = State.old_cursor1
322 else
323 State.selection1 = State.old_selection1
326 State.old_cursor1, State.old_selection1, State.mousepress_shift = nil
327 if eq(State.cursor1, State.selection1) then
328 State.selection1 = {}
330 break
334 --? 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))
338 function edit.mouse_wheel_move(State, dx,dy)
339 if dy > 0 then
340 State.cursor1 = {line=State.screen_top1.line, pos=State.screen_top1.pos}
341 edit.put_cursor_on_next_text_line(State)
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 edit.put_cursor_on_next_text_line(State)
348 for i=1,math.floor(-dy) do
349 Text.down(State)
354 function edit.text_input(State, t)
355 --? print('text input', t)
356 if State.search_term then
357 State.search_term = State.search_term..t
358 Text.search_next(State)
359 elseif State.lines.current_drawing and State.current_drawing_mode == 'name' then
360 local before = snapshot(State, State.lines.current_drawing_index)
361 local drawing = State.lines.current_drawing
362 local p = drawing.points[drawing.pending.target_point]
363 p.name = p.name..t
364 record_undo_event(State, {before=before, after=snapshot(State, State.lines.current_drawing_index)})
365 else
366 local drawing_index, drawing = Drawing.current_drawing(State)
367 if drawing_index == nil then
368 for _,line_cache in ipairs(State.line_cache) do line_cache.starty = nil end -- just in case we scroll
369 Text.text_input(State, t)
372 schedule_save(State)
375 function edit.keychord_press(State, chord, key)
376 if State.selection1.line and
377 not State.lines.current_drawing and
378 -- printable character created using shift key => delete selection
379 -- (we're not creating any ctrl-shift- or alt-shift- combinations using regular/printable keys)
380 (not App.shift_down() or utf8.len(key) == 1) and
381 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
382 Text.delete_selection(State, State.left, State.right)
384 if State.search_term then
385 for _,line_cache in ipairs(State.line_cache) do line_cache.starty = nil end -- just in case we scroll
386 if chord == 'escape' then
387 State.search_term = nil
388 State.cursor1 = State.search_backup.cursor
389 State.screen_top1 = State.search_backup.screen_top
390 State.search_backup = nil
391 Text.redraw_all(State) -- if we're scrolling, reclaim all fragments to avoid memory leaks
392 elseif chord == 'return' then
393 State.search_term = nil
394 State.search_backup = nil
395 elseif chord == 'backspace' then
396 local len = utf8.len(State.search_term)
397 local byte_offset = Text.offset(State.search_term, len)
398 State.search_term = string.sub(State.search_term, 1, byte_offset-1)
399 elseif chord == 'down' then
400 State.cursor1.pos = State.cursor1.pos+1
401 Text.search_next(State)
402 elseif chord == 'up' then
403 Text.search_previous(State)
405 return
406 elseif chord == 'C-f' then
407 State.search_term = ''
408 State.search_backup = {
409 cursor={line=State.cursor1.line, pos=State.cursor1.pos},
410 screen_top={line=State.screen_top1.line, pos=State.screen_top1.pos},
412 -- zoom
413 elseif chord == 'C-=' then
414 edit.update_font_settings(State, State.font_height+2)
415 Text.redraw_all(State)
416 elseif chord == 'C--' then
417 if State.font_height > 2 then
418 edit.update_font_settings(State, State.font_height-2)
419 Text.redraw_all(State)
421 elseif chord == 'C-0' then
422 edit.update_font_settings(State, 20)
423 Text.redraw_all(State)
424 -- undo
425 elseif chord == 'C-z' then
426 for _,line_cache in ipairs(State.line_cache) do line_cache.starty = nil end -- just in case we scroll
427 local event = undo_event(State)
428 if event then
429 local src = event.before
430 State.screen_top1 = deepcopy(src.screen_top)
431 State.cursor1 = deepcopy(src.cursor)
432 State.selection1 = deepcopy(src.selection)
433 patch(State.lines, event.after, event.before)
434 patch_placeholders(State.line_cache, event.after, event.before)
435 -- invalidate various cached bits of lines
436 State.lines.current_drawing = nil
437 -- if we're scrolling, reclaim all fragments to avoid memory leaks
438 Text.redraw_all(State)
439 schedule_save(State)
441 elseif chord == 'C-y' then
442 for _,line_cache in ipairs(State.line_cache) do line_cache.starty = nil end -- just in case we scroll
443 local event = redo_event(State)
444 if event then
445 local src = event.after
446 State.screen_top1 = deepcopy(src.screen_top)
447 State.cursor1 = deepcopy(src.cursor)
448 State.selection1 = deepcopy(src.selection)
449 patch(State.lines, event.before, event.after)
450 -- invalidate various cached bits of lines
451 State.lines.current_drawing = nil
452 -- if we're scrolling, reclaim all fragments to avoid memory leaks
453 Text.redraw_all(State)
454 schedule_save(State)
456 -- clipboard
457 elseif chord == 'C-a' then
458 State.selection1 = {line=1, pos=1}
459 State.cursor1 = {line=#State.lines, pos=utf8.len(State.lines[#State.lines].data)+1}
460 elseif chord == 'C-c' then
461 local s = Text.selection(State)
462 if s then
463 App.set_clipboard(s)
465 elseif chord == 'C-x' then
466 for _,line_cache in ipairs(State.line_cache) do line_cache.starty = nil end -- just in case we scroll
467 local s = Text.cut_selection(State, State.left, State.right)
468 if s then
469 App.set_clipboard(s)
471 schedule_save(State)
472 elseif chord == 'C-v' then
473 for _,line_cache in ipairs(State.line_cache) do line_cache.starty = nil end -- just in case we scroll
474 -- We don't have a good sense of when to scroll, so we'll be conservative
475 -- and sometimes scroll when we didn't quite need to.
476 local before_line = State.cursor1.line
477 local before = snapshot(State, before_line)
478 local clipboard_data = App.get_clipboard()
479 for _,code in utf8.codes(clipboard_data) do
480 local c = utf8.char(code)
481 if c == '\n' then
482 Text.insert_return(State)
483 else
484 Text.insert_at_cursor(State, c)
487 if Text.cursor_out_of_screen(State) then
488 Text.snap_cursor_to_bottom_of_screen(State, State.left, State.right)
490 schedule_save(State)
491 record_undo_event(State, {before=before, after=snapshot(State, before_line, State.cursor1.line)})
492 -- dispatch to drawing or text
493 elseif App.mouse_down(1) or chord:sub(1,2) == 'C-' then
494 -- DON'T reset line_cache.starty here
495 local drawing_index, drawing = Drawing.current_drawing(State)
496 if drawing_index then
497 local before = snapshot(State, drawing_index)
498 Drawing.keychord_press(State, chord)
499 record_undo_event(State, {before=before, after=snapshot(State, drawing_index)})
500 schedule_save(State)
502 elseif chord == 'escape' and not App.mouse_down(1) then
503 for _,line in ipairs(State.lines) do
504 if line.mode == 'drawing' then
505 line.show_help = false
508 elseif State.lines.current_drawing and State.current_drawing_mode == 'name' then
509 if chord == 'return' then
510 State.current_drawing_mode = State.previous_drawing_mode
511 State.previous_drawing_mode = nil
512 else
513 local before = snapshot(State, State.lines.current_drawing_index)
514 local drawing = State.lines.current_drawing
515 local p = drawing.points[drawing.pending.target_point]
516 if chord == 'escape' then
517 p.name = nil
518 record_undo_event(State, {before=before, after=snapshot(State, State.lines.current_drawing_index)})
519 elseif chord == 'backspace' then
520 local len = utf8.len(p.name)
521 if len > 0 then
522 local byte_offset = Text.offset(p.name, len-1)
523 if len == 1 then byte_offset = 0 end
524 p.name = string.sub(p.name, 1, byte_offset)
525 record_undo_event(State, {before=before, after=snapshot(State, State.lines.current_drawing_index)})
529 schedule_save(State)
530 else
531 for _,line_cache in ipairs(State.line_cache) do line_cache.starty = nil end -- just in case we scroll
532 Text.keychord_press(State, chord)
536 function edit.key_release(State, key, scancode)
539 function edit.update_font_settings(State, font_height)
540 State.font_height = font_height
541 love.graphics.setFont(love.graphics.newFont(State.font_height))
542 State.line_height = math.floor(font_height*1.3)
545 --== some methods for tests
547 -- Insulate tests from some key globals so I don't have to change the vast
548 -- majority of tests when they're modified for the real app.
549 Test_margin_left = 25
550 Test_margin_right = 0
552 function edit.initialize_test_state()
553 -- if you change these values, tests will start failing
554 return edit.initialize_state(
555 15, -- top margin
556 Test_margin_left,
557 App.screen.width - Test_margin_right,
558 14, -- font height assuming default LÖVE font
559 15) -- line height
562 -- all text_input events are also keypresses
563 -- TODO: handle chords of multiple keys
564 function edit.run_after_text_input(State, t)
565 edit.keychord_press(State, t)
566 edit.text_input(State, t)
567 edit.key_release(State, t)
568 App.screen.contents = {}
569 edit.update(State, 0)
570 edit.draw(State)
573 -- not all keys are text_input
574 function edit.run_after_keychord(State, chord)
575 edit.keychord_press(State, chord)
576 edit.key_release(State, chord)
577 App.screen.contents = {}
578 edit.update(State, 0)
579 edit.draw(State)
582 function edit.run_after_mouse_click(State, x,y, mouse_button)
583 App.fake_mouse_press(x,y, mouse_button)
584 edit.mouse_press(State, x,y, mouse_button)
585 App.fake_mouse_release(x,y, mouse_button)
586 edit.mouse_release(State, x,y, mouse_button)
587 App.screen.contents = {}
588 edit.update(State, 0)
589 edit.draw(State)
592 function edit.run_after_mouse_press(State, x,y, mouse_button)
593 App.fake_mouse_press(x,y, mouse_button)
594 edit.mouse_press(State, x,y, mouse_button)
595 App.screen.contents = {}
596 edit.update(State, 0)
597 edit.draw(State)
600 function edit.run_after_mouse_release(State, x,y, mouse_button)
601 App.fake_mouse_release(x,y, mouse_button)
602 edit.mouse_release(State, x,y, mouse_button)
603 App.screen.contents = {}
604 edit.update(State, 0)
605 edit.draw(State)