comment
[view.love.git] / edit.lua
blob7154a7d4b29dac5cef898c67d2cf9a254d05c6d6
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, 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 -- fragments: snippets of the line guaranteed to not straddle screen lines
55 -- screen_line_starting_pos: optional array of grapheme indices if it wraps over more than one screen line
56 line_cache = {},
58 -- Given wrapping, any potential location for the text cursor can be described in two ways:
59 -- * schema 1: As a combination of line index and position within a line (in utf8 codepoint units)
60 -- * schema 2: As a combination of line index, screen line index within the line, and a position within the screen line.
62 -- Most of the time we'll only persist positions in schema 1, translating to
63 -- schema 2 when that's convenient.
65 -- Make sure these coordinates are never aliased, so that changing one causes
66 -- action at a distance.
68 -- On lines that are drawings, pos will be nil.
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; must be on a text line
72 selection1 = {},
73 -- some extra state to compute selection between mouse press and release
74 old_cursor1 = nil,
75 old_selection1 = nil,
76 mousepress_shift = nil,
78 -- cursor coordinates in pixels
79 cursor_x = 0,
80 cursor_y = 0,
82 current_drawing_mode = 'line', -- one of the available shape modes
83 previous_drawing_mode = nil, -- extra state for some ephemeral modes like moving/deleting/naming points
85 font = font,
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 -- edit.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 State.cursor1 = {line=1, pos=1}
117 edit.put_cursor_on_next_text_line(State)
121 function edit.invalid1(State, loc1)
122 if loc1.line > #State.lines then return true end
123 local l = State.lines[loc1.line]
124 if l.mode ~= 'text' then return false end -- pos is irrelevant to validity for a drawing line
125 return loc1.pos > #State.lines[loc1.line].data
128 -- cursor loc in particular differs from other locs in one way:
129 -- pos might occur just after end of line
130 function edit.invalid_cursor1(State)
131 local cursor1 = State.cursor1
132 if cursor1.line > #State.lines then return true end
133 local l = State.lines[cursor1.line]
134 if l.mode ~= 'text' then return false end -- pos is irrelevant to validity for a drawing line
135 return cursor1.pos > #State.lines[cursor1.line].data + 1
138 function edit.cursor_on_text(State)
139 return State.cursor1.line <= #State.lines
140 and State.lines[State.cursor1.line].mode == 'text'
143 function edit.put_cursor_on_next_text_line(State)
144 while true do
145 if State.cursor1.line >= #State.lines then
146 break
148 if State.lines[State.cursor1.line].mode == 'text' then
149 break
151 State.cursor1.line = State.cursor1.line+1
152 State.cursor1.pos = 1
156 -- return y drawn until
157 function edit.draw(State)
158 State.button_handlers = {}
159 love.graphics.setFont(State.font)
160 App.color(Text_color)
161 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))
162 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))
163 State.cursor_x = nil
164 State.cursor_y = nil
165 local y = State.top
166 --? print('== draw')
167 for line_index = State.screen_top1.line,#State.lines do
168 local line = State.lines[line_index]
169 --? print('draw:', y, line_index, line)
170 if y + State.line_height > App.screen.height then break end
171 if line.mode == 'text' then
172 --? print('text.draw', y, line_index)
173 local startpos = 1
174 if line_index == State.screen_top1.line then
175 startpos = State.screen_top1.pos
177 if line.data == '' then
178 -- button to insert new drawing
179 button(State, 'draw', {x=State.left-Margin_left+4, y=y+4, w=12,h=12, bg={r=1,g=1,b=0},
180 icon = icon.insert_drawing,
181 onpress1 = function()
182 Drawing.before = snapshot(State, line_index-1, line_index)
183 table.insert(State.lines, line_index, {mode='drawing', y=y, h=256/2, points={}, shapes={}, pending={}})
184 table.insert(State.line_cache, line_index, {})
185 if State.cursor1.line >= line_index then
186 State.cursor1.line = State.cursor1.line+1
188 schedule_save(State)
189 record_undo_event(State, {before=Drawing.before, after=snapshot(State, line_index-1, line_index+1)})
190 end,
193 y = Text.draw(State, line_index, y, startpos)
194 --? print('=> y', y)
195 elseif line.mode == 'drawing' then
196 y = y+Drawing_padding_top
197 Drawing.draw(State, line_index, y)
198 y = y + Drawing.pixels(line.h, State.width) + Drawing_padding_bottom
199 else
200 assert(false, ('unknown line mode %s'):format(line.mode))
203 if State.search_term then
204 Text.draw_search_bar(State)
206 return y
209 function edit.update(State, dt)
210 Drawing.update(State, dt)
211 if State.next_save and State.next_save < Current_time then
212 save_to_disk(State)
213 State.next_save = nil
217 function schedule_save(State)
218 if State.next_save == nil then
219 State.next_save = Current_time + 3 -- short enough that you're likely to still remember what you did
223 function edit.quit(State)
224 -- make sure to save before quitting
225 if State.next_save then
226 save_to_disk(State)
227 -- give some time for the OS to flush everything to disk
228 love.timer.sleep(0.1)
232 function edit.mouse_press(State, x,y, mouse_button)
233 love.keyboard.setTextInput(true) -- bring up keyboard on touch screen
234 if State.search_term then return end
235 State.mouse_down = mouse_button
236 --? print_and_log(('edit.mouse_press: cursor at %d,%d'):format(State.cursor1.line, State.cursor1.pos))
237 if mouse_press_consumed_by_any_button(State, x,y, mouse_button) then
238 -- press on a button and it returned 'true' to short-circuit
239 return
242 if y < State.top then
243 State.old_cursor1 = State.cursor1
244 State.old_selection1 = State.selection1
245 State.mousepress_shift = App.shift_down()
246 State.selection1 = {
247 line=State.screen_top1.line,
248 pos=State.screen_top1.pos,
250 return
253 for line_index,line in ipairs(State.lines) do
254 if line.mode == 'text' then
255 if Text.in_line(State, line_index, x,y) then
256 -- delicate dance between cursor, selection and old cursor/selection
257 -- scenarios:
258 -- regular press+release: sets cursor, clears selection
259 -- shift press+release:
260 -- sets selection to old cursor if not set otherwise leaves it untouched
261 -- sets cursor
262 -- press and hold to start a selection: sets selection on press, cursor on release
263 -- press and hold, then press shift: ignore shift
264 -- i.e. mouse_release should never look at shift state
265 --? print_and_log(('edit.mouse_press: in line %d'):format(line_index))
266 State.old_cursor1 = State.cursor1
267 State.old_selection1 = State.selection1
268 State.mousepress_shift = App.shift_down()
269 State.selection1 = {
270 line=line_index,
271 pos=Text.to_pos_on_line(State, line_index, x, y),
273 return
275 elseif line.mode == 'drawing' then
276 if Drawing.in_drawing(State, line_index, x, y, State.left,State.right) then
277 State.lines.current_drawing_index = line_index
278 State.lines.current_drawing = line
279 Drawing.before = snapshot(State, line_index)
280 Drawing.mouse_press(State, line_index, x,y, mouse_button)
281 return
286 -- still here? mouse press is below all screen lines
287 State.old_cursor1 = State.cursor1
288 State.old_selection1 = State.selection1
289 State.mousepress_shift = App.shift_down()
290 State.selection1 = Text.final_text_loc_on_screen(State)
293 function edit.mouse_release(State, x,y, mouse_button)
294 if State.search_term then return end
295 --? print_and_log(('edit.mouse_release: cursor at %d,%d'):format(State.cursor1.line, State.cursor1.pos))
296 State.mouse_down = nil
297 if State.lines.current_drawing then
298 Drawing.mouse_release(State, x,y, mouse_button)
299 schedule_save(State)
300 if Drawing.before then
301 record_undo_event(State, {before=Drawing.before, after=snapshot(State, State.lines.current_drawing_index)})
302 Drawing.before = nil
304 else
305 --? print_and_log('edit.mouse_release: no current drawing')
306 if y < State.top then
307 State.cursor1 = {line=State.screen_top1.line, pos=State.screen_top1.pos}
308 edit.clean_up_mouse_press(State)
309 return
312 for line_index,line in ipairs(State.lines) do
313 if line.mode == 'text' then
314 if Text.in_line(State, line_index, x,y) then
315 --? print_and_log(('edit.mouse_release: in line %d'):format(line_index))
316 State.cursor1 = {
317 line=line_index,
318 pos=Text.to_pos_on_line(State, line_index, x, y),
320 --? print_and_log(('edit.mouse_release: cursor now %d,%d'):format(State.cursor1.line, State.cursor1.pos))
321 edit.clean_up_mouse_press(State)
322 return
327 -- still here? mouse release is below all screen lines
328 State.cursor1 = Text.final_text_loc_on_screen(State)
329 edit.clean_up_mouse_press(State)
330 --? 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))
334 function edit.clean_up_mouse_press(State)
335 if State.mousepress_shift then
336 if State.old_selection1.line == nil then
337 State.selection1 = State.old_cursor1
338 else
339 State.selection1 = State.old_selection1
342 State.old_cursor1, State.old_selection1, State.mousepress_shift = nil
343 if eq(State.cursor1, State.selection1) then
344 State.selection1 = {}
348 function edit.mouse_wheel_move(State, dx,dy)
349 if dy > 0 then
350 State.cursor1 = {line=State.screen_top1.line, pos=State.screen_top1.pos}
351 edit.put_cursor_on_next_text_line(State)
352 for i=1,math.floor(dy) do
353 Text.up(State)
355 elseif dy < 0 then
356 State.cursor1 = Text.screen_bottom1(State)
357 edit.put_cursor_on_next_text_line(State)
358 for i=1,math.floor(-dy) do
359 Text.down(State)
364 function edit.text_input(State, t)
365 --? print('text input', t)
366 if State.search_term then
367 State.search_term = State.search_term..t
368 Text.search_next(State)
369 elseif State.lines.current_drawing and State.current_drawing_mode == 'name' then
370 local before = snapshot(State, State.lines.current_drawing_index)
371 local drawing = State.lines.current_drawing
372 local p = drawing.points[drawing.pending.target_point]
373 p.name = p.name..t
374 record_undo_event(State, {before=before, after=snapshot(State, State.lines.current_drawing_index)})
375 else
376 local drawing_index, drawing = Drawing.current_drawing(State)
377 if drawing_index == nil then
378 Text.text_input(State, t)
381 schedule_save(State)
384 function edit.keychord_press(State, chord, key)
385 if State.selection1.line and
386 not State.lines.current_drawing and
387 -- printable character created using shift key => delete selection
388 -- (we're not creating any ctrl-shift- or alt-shift- combinations using regular/printable keys)
389 (not App.shift_down() or utf8.len(key) == 1) and
390 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
391 Text.delete_selection(State, State.left, State.right)
393 if State.search_term then
394 if chord == 'escape' then
395 State.search_term = nil
396 State.cursor1 = State.search_backup.cursor
397 State.screen_top1 = State.search_backup.screen_top
398 State.search_backup = nil
399 Text.redraw_all(State) -- if we're scrolling, reclaim all fragments to avoid memory leaks
400 elseif chord == 'return' then
401 State.search_term = nil
402 State.search_backup = nil
403 elseif chord == 'backspace' then
404 local len = utf8.len(State.search_term)
405 local byte_offset = Text.offset(State.search_term, len)
406 State.search_term = string.sub(State.search_term, 1, byte_offset-1)
407 elseif chord == 'down' then
408 State.cursor1.pos = State.cursor1.pos+1
409 Text.search_next(State)
410 elseif chord == 'up' then
411 Text.search_previous(State)
413 return
414 elseif chord == 'C-f' then
415 State.search_term = ''
416 State.search_backup = {
417 cursor={line=State.cursor1.line, pos=State.cursor1.pos},
418 screen_top={line=State.screen_top1.line, pos=State.screen_top1.pos},
420 -- zoom
421 elseif chord == 'C-=' then
422 edit.update_font_settings(State, State.font_height+2)
423 Text.redraw_all(State)
424 elseif chord == 'C--' then
425 if State.font_height > 2 then
426 edit.update_font_settings(State, State.font_height-2)
427 Text.redraw_all(State)
429 elseif chord == 'C-0' then
430 edit.update_font_settings(State, 20)
431 Text.redraw_all(State)
432 -- undo
433 elseif chord == 'C-z' then
434 local event = undo_event(State)
435 if event then
436 local src = event.before
437 State.screen_top1 = deepcopy(src.screen_top)
438 State.cursor1 = deepcopy(src.cursor)
439 State.selection1 = deepcopy(src.selection)
440 patch(State.lines, event.after, event.before)
441 patch_placeholders(State.line_cache, event.after, event.before)
442 -- invalidate various cached bits of lines
443 State.lines.current_drawing = nil
444 -- if we're scrolling, reclaim all fragments to avoid memory leaks
445 Text.redraw_all(State)
446 schedule_save(State)
448 elseif chord == 'C-y' then
449 local event = redo_event(State)
450 if event then
451 local src = event.after
452 State.screen_top1 = deepcopy(src.screen_top)
453 State.cursor1 = deepcopy(src.cursor)
454 State.selection1 = deepcopy(src.selection)
455 patch(State.lines, event.before, event.after)
456 -- invalidate various cached bits of lines
457 State.lines.current_drawing = nil
458 -- if we're scrolling, reclaim all fragments to avoid memory leaks
459 Text.redraw_all(State)
460 schedule_save(State)
462 -- clipboard
463 elseif chord == 'C-a' then
464 State.selection1 = {line=1, pos=1}
465 State.cursor1 = {line=#State.lines, pos=utf8.len(State.lines[#State.lines].data)+1}
466 elseif chord == 'C-c' then
467 local s = Text.selection(State)
468 if s then
469 App.set_clipboard(s)
471 elseif chord == 'C-x' then
472 local s = Text.cut_selection(State, State.left, State.right)
473 if s then
474 App.set_clipboard(s)
476 schedule_save(State)
477 elseif chord == 'C-v' then
478 -- We don't have a good sense of when to scroll, so we'll be conservative
479 -- and sometimes scroll when we didn't quite need to.
480 local before_line = State.cursor1.line
481 local before = snapshot(State, before_line)
482 local clipboard_data = App.get_clipboard()
483 for _,code in utf8.codes(clipboard_data) do
484 local c = utf8.char(code)
485 if c == '\n' then
486 Text.insert_return(State)
487 else
488 Text.insert_at_cursor(State, c)
491 if Text.cursor_out_of_screen(State) then
492 Text.snap_cursor_to_bottom_of_screen(State, State.left, State.right)
494 schedule_save(State)
495 record_undo_event(State, {before=before, after=snapshot(State, before_line, State.cursor1.line)})
496 -- dispatch to drawing or text
497 elseif App.mouse_down(1) or chord:sub(1,2) == 'C-' then
498 local drawing_index, drawing = Drawing.current_drawing(State)
499 if drawing_index then
500 local before = snapshot(State, drawing_index)
501 Drawing.keychord_press(State, chord)
502 record_undo_event(State, {before=before, after=snapshot(State, drawing_index)})
503 schedule_save(State)
505 elseif chord == 'escape' and not App.mouse_down(1) then
506 for _,line in ipairs(State.lines) do
507 if line.mode == 'drawing' then
508 line.show_help = false
511 elseif State.lines.current_drawing and State.current_drawing_mode == 'name' then
512 if chord == 'return' then
513 State.current_drawing_mode = State.previous_drawing_mode
514 State.previous_drawing_mode = nil
515 else
516 local before = snapshot(State, State.lines.current_drawing_index)
517 local drawing = State.lines.current_drawing
518 local p = drawing.points[drawing.pending.target_point]
519 if chord == 'escape' then
520 p.name = nil
521 record_undo_event(State, {before=before, after=snapshot(State, State.lines.current_drawing_index)})
522 elseif chord == 'backspace' then
523 local len = utf8.len(p.name)
524 if len > 0 then
525 local byte_offset = Text.offset(p.name, len-1)
526 if len == 1 then byte_offset = 0 end
527 p.name = string.sub(p.name, 1, byte_offset)
528 record_undo_event(State, {before=before, after=snapshot(State, State.lines.current_drawing_index)})
532 schedule_save(State)
533 else
534 Text.keychord_press(State, chord)
538 function edit.key_release(State, key, scancode)
541 function edit.update_font_settings(State, font_height)
542 State.font_height = font_height
543 State.font = love.graphics.newFont(State.font_height)
544 State.line_height = math.floor(font_height*1.3)
547 --== some methods for tests
549 -- Insulate tests from some key globals so I don't have to change the vast
550 -- majority of tests when they're modified for the real app.
551 Test_margin_left = 25
552 Test_margin_right = 0
554 function edit.initialize_test_state()
555 -- if you change these values, tests will start failing
556 return edit.initialize_state(
557 15, -- top margin
558 Test_margin_left,
559 App.screen.width - Test_margin_right,
560 love.graphics.getFont(),
562 15) -- line height
565 -- all text_input events are also keypresses
566 -- TODO: handle chords of multiple keys
567 function edit.run_after_text_input(State, t)
568 edit.keychord_press(State, t)
569 edit.text_input(State, t)
570 edit.key_release(State, t)
571 App.screen.contents = {}
572 edit.update(State, 0)
573 edit.draw(State)
576 -- not all keys are text_input
577 function edit.run_after_keychord(State, chord, key)
578 edit.keychord_press(State, chord, key)
579 edit.key_release(State, key)
580 App.screen.contents = {}
581 edit.update(State, 0)
582 edit.draw(State)
585 function edit.run_after_mouse_click(State, x,y, mouse_button)
586 App.fake_mouse_press(x,y, mouse_button)
587 edit.mouse_press(State, x,y, mouse_button)
588 edit.draw(State)
589 App.fake_mouse_release(x,y, mouse_button)
590 edit.mouse_release(State, x,y, mouse_button)
591 App.screen.contents = {}
592 edit.update(State, 0)
593 edit.draw(State)
596 function edit.run_after_mouse_press(State, x,y, mouse_button)
597 App.fake_mouse_press(x,y, mouse_button)
598 edit.mouse_press(State, x,y, mouse_button)
599 App.screen.contents = {}
600 edit.update(State, 0)
601 edit.draw(State)
604 function edit.run_after_mouse_release(State, x,y, mouse_button)
605 App.fake_mouse_release(x,y, mouse_button)
606 edit.mouse_release(State, x,y, mouse_button)
607 App.screen.contents = {}
608 edit.update(State, 0)
609 edit.draw(State)