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}
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
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
30 -- a line is either text or a drawing
31 -- a text is a table with:
34 -- a drawing is a table with:
36 -- a (y) coord in pixels (updated while painting screen),
38 -- an array of points, and
40 -- a shape is a table containing:
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
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
76 -- some extra state to compute selection between mouse press and release
79 mousepress_shift
= nil,
81 -- cursor coordinates in pixels
85 current_drawing_mode
= 'line',
86 previous_drawing_mode
= nil, -- extra state for some ephemeral modes like moving/deleting/naming points
89 font_height
= font_height
,
90 line_height
= line_height
,
93 left
= math
.floor(left
), -- left margin for text; line numbers go to the left of this
94 right
= math
.floor(right
),
106 search_backup
= nil, -- stuff to restore when cancelling search
109 end -- edit.initialize_state
111 function edit
.check_locs(State
)
112 -- if State is inconsistent (i.e. file changed by some other program),
113 -- throw away all cursor state entirely
114 if edit
.invalid1(State
, State
.screen_top1
)
115 or edit
.invalid_cursor1(State
)
116 or not edit
.cursor_on_text(State
)
117 or not Text
.le1(State
.screen_top1
, State
.cursor1
) then
118 State
.screen_top1
= {line
=1, pos
=1}
119 State
.cursor1
= {line
=1, pos
=1}
120 edit
.put_cursor_on_next_text_line(State
)
124 function edit
.invalid1(State
, loc1
)
125 if loc1
.line
> #State
.lines
then return true end
126 local l
= State
.lines
[loc1
.line
]
127 if l
.mode
~= 'text' then return false end -- pos is irrelevant to validity for a drawing line
128 return loc1
.pos
> #State
.lines
[loc1
.line
].data
131 -- cursor loc in particular differs from other locs in one way:
132 -- pos might occur just after end of line
133 function edit
.invalid_cursor1(State
)
134 local cursor1
= State
.cursor1
135 if cursor1
.line
> #State
.lines
then return true end
136 local l
= State
.lines
[cursor1
.line
]
137 if l
.mode
~= 'text' then return false end -- pos is irrelevant to validity for a drawing line
138 return cursor1
.pos
> #State
.lines
[cursor1
.line
].data
+ 1
141 function edit
.cursor_on_text(State
)
142 return State
.cursor1
.line
<= #State
.lines
143 and State
.lines
[State
.cursor1
.line
].mode
== 'text'
146 function edit
.put_cursor_on_next_text_line(State
)
148 if State
.cursor1
.line
>= #State
.lines
then
151 if State
.lines
[State
.cursor1
.line
].mode
== 'text' then
154 State
.cursor1
.line
= State
.cursor1
.line
+1
155 State
.cursor1
.pos
= 1
159 function edit
.draw(State
, hide_cursor
, show_line_numbers
)
160 State
.button_handlers
= {}
161 love
.graphics
.setFont(State
.font
)
162 App
.color(Text_color
)
163 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
))
164 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
))
168 local screen_bottom1
= {line
=nil, pos
=nil}
170 for line_index
= State
.screen_top1
.line
,#State
.lines
do
171 local line
= State
.lines
[line_index
]
172 --? print('draw:', y, line_index, line)
173 if y
+ State
.line_height
> App
.screen
.height
then break end
174 screen_bottom1
.line
= line_index
175 if line
.mode
== 'text' then
176 --? print('text.draw', y, line_index)
178 if line_index
== State
.screen_top1
.line
then
179 startpos
= State
.screen_top1
.pos
181 if line
.data
== '' then
182 -- button to insert new drawing
183 local buttonx
= State
.left
-Margin_left
+4
184 if show_line_numbers
then
185 buttonx
= 4 -- HACK: position draw buttons at a fixed x on screen
187 button(State
, 'draw', {x
=buttonx
, y
=y
+4, w
=12,h
=12, bg
={r
=1,g
=1,b
=0},
188 icon
= icon
.insert_drawing
,
189 onpress1
= function()
190 Drawing
.before
= snapshot(State
, line_index
-1, line_index
)
191 table.insert(State
.lines
, line_index
, {mode
='drawing', y
=y
, h
=256/2, points
={}, shapes
={}, pending
={}})
192 table.insert(State
.line_cache
, line_index
, {})
193 if State
.cursor1
.line
>= line_index
then
194 State
.cursor1
.line
= State
.cursor1
.line
+1
197 record_undo_event(State
, {before
=Drawing
.before
, after
=snapshot(State
, line_index
-1, line_index
+1)})
201 y
, screen_bottom1
.pos
= Text
.draw(State
, line_index
, y
, startpos
, hide_cursor
, show_line_numbers
)
203 elseif line
.mode
== 'drawing' then
204 y
= y
+Drawing_padding_top
205 Drawing
.draw(State
, line_index
, y
)
206 y
= y
+ Drawing
.pixels(line
.h
, State
.width
) + Drawing_padding_bottom
208 assert(false, ('unknown line mode %s'):format(line
.mode
))
211 State
.screen_bottom1
= screen_bottom1
212 if State
.search_term
then
213 Text
.draw_search_bar(State
)
217 function edit
.update(State
, dt
)
218 Drawing
.update(State
, dt
)
219 if State
.next_save
and State
.next_save
< Current_time
then
221 State
.next_save
= nil
225 function schedule_save(State
)
226 if State
.next_save
== nil then
227 State
.next_save
= Current_time
+ 3 -- short enough that you're likely to still remember what you did
231 function edit
.quit(State
)
232 -- make sure to save before quitting
233 if State
.next_save
then
235 -- give some time for the OS to flush everything to disk
236 love
.timer
.sleep(0.1)
240 function edit
.mouse_press(State
, x
,y
, mouse_button
)
241 if State
.search_term
then return end
242 State
.mouse_down
= mouse_button
243 --? print_and_log(('edit.mouse_press: cursor at %d,%d'):format(State.cursor1.line, State.cursor1.pos))
244 if mouse_press_consumed_by_any_button(State
, x
,y
, mouse_button
) then
245 -- press on a button and it returned 'true' to short-circuit
249 if y
< State
.top
then
250 State
.old_cursor1
= State
.cursor1
251 State
.old_selection1
= State
.selection1
252 State
.mousepress_shift
= App
.shift_down()
254 line
=State
.screen_top1
.line
,
255 pos
=State
.screen_top1
.pos
,
260 for line_index
,line
in ipairs(State
.lines
) do
261 if line
.mode
== 'text' then
262 if Text
.in_line(State
, line_index
, x
,y
) then
263 -- delicate dance between cursor, selection and old cursor/selection
265 -- regular press+release: sets cursor, clears selection
266 -- shift press+release:
267 -- sets selection to old cursor if not set otherwise leaves it untouched
269 -- press and hold to start a selection: sets selection on press, cursor on release
270 -- press and hold, then press shift: ignore shift
271 -- i.e. mouse_release should never look at shift state
272 --? print_and_log(('edit.mouse_press: in line %d'):format(line_index))
273 State
.old_cursor1
= State
.cursor1
274 State
.old_selection1
= State
.selection1
275 State
.mousepress_shift
= App
.shift_down()
278 pos
=Text
.to_pos_on_line(State
, line_index
, x
, y
),
282 elseif line
.mode
== 'drawing' then
283 local line_cache
= State
.line_cache
[line_index
]
284 if Drawing
.in_drawing(line
, line_cache
, x
, y
, State
.left
,State
.right
) then
285 State
.lines
.current_drawing_index
= line_index
286 State
.lines
.current_drawing
= line
287 Drawing
.before
= snapshot(State
, line_index
)
288 Drawing
.mouse_press(State
, line_index
, x
,y
, mouse_button
)
294 -- still here? mouse press is below all screen lines
295 State
.old_cursor1
= State
.cursor1
296 State
.old_selection1
= State
.selection1
297 State
.mousepress_shift
= App
.shift_down()
299 line
=State
.screen_bottom1
.line
,
300 pos
=Text
.pos_at_end_of_screen_line(State
, State
.screen_bottom1
),
304 function edit
.mouse_release(State
, x
,y
, mouse_button
)
305 if State
.search_term
then return end
306 --? print_and_log(('edit.mouse_release: cursor at %d,%d'):format(State.cursor1.line, State.cursor1.pos))
307 State
.mouse_down
= nil
308 if State
.lines
.current_drawing
then
309 Drawing
.mouse_release(State
, x
,y
, mouse_button
)
311 if Drawing
.before
then
312 record_undo_event(State
, {before
=Drawing
.before
, after
=snapshot(State
, State
.lines
.current_drawing_index
)})
316 --? print_and_log('edit.mouse_release: no current drawing')
317 if y
< State
.top
then
318 State
.cursor1
= {line
=State
.screen_top1
.line
, pos
=State
.screen_top1
.pos
}
319 edit
.clean_up_mouse_press(State
)
323 for line_index
,line
in ipairs(State
.lines
) do
324 if line
.mode
== 'text' then
325 if Text
.in_line(State
, line_index
, x
,y
) then
326 --? print_and_log(('edit.mouse_release: in line %d'):format(line_index))
329 pos
=Text
.to_pos_on_line(State
, line_index
, x
, y
),
331 --? print_and_log(('edit.mouse_release: cursor now %d,%d'):format(State.cursor1.line, State.cursor1.pos))
332 edit
.clean_up_mouse_press(State
)
338 -- still here? mouse release is below all screen lines
339 State
.cursor1
.line
, State
.cursor1
.pos
= State
.screen_bottom1
.line
, Text
.pos_at_end_of_screen_line(State
, State
.screen_bottom1
)
340 edit
.clean_up_mouse_press(State
)
341 --? 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))
345 function edit
.clean_up_mouse_press(State
)
346 if State
.mousepress_shift
then
347 if State
.old_selection1
.line
== nil then
348 State
.selection1
= State
.old_cursor1
350 State
.selection1
= State
.old_selection1
353 State
.old_cursor1
, State
.old_selection1
, State
.mousepress_shift
= nil
354 if eq(State
.cursor1
, State
.selection1
) then
355 State
.selection1
= {}
359 function edit
.mouse_wheel_move(State
, dx
,dy
)
361 State
.cursor1
= {line
=State
.screen_top1
.line
, pos
=State
.screen_top1
.pos
}
362 edit
.put_cursor_on_next_text_line(State
)
363 for i
=1,math
.floor(dy
) do
367 State
.cursor1
= {line
=State
.screen_bottom1
.line
, pos
=State
.screen_bottom1
.pos
}
368 edit
.put_cursor_on_next_text_line(State
)
369 for i
=1,math
.floor(-dy
) do
375 function edit
.text_input(State
, t
)
376 --? print('text input', t)
377 if State
.search_term
then
378 State
.search_term
= State
.search_term
..t
379 Text
.search_next(State
)
380 elseif State
.lines
.current_drawing
and State
.current_drawing_mode
== 'name' then
381 local before
= snapshot(State
, State
.lines
.current_drawing_index
)
382 local drawing
= State
.lines
.current_drawing
383 local p
= drawing
.points
[drawing
.pending
.target_point
]
385 record_undo_event(State
, {before
=before
, after
=snapshot(State
, State
.lines
.current_drawing_index
)})
387 local drawing_index
, drawing
= Drawing
.current_drawing(State
)
388 if drawing_index
== nil then
389 Text
.text_input(State
, t
)
395 function edit
.keychord_press(State
, chord
, key
)
396 if State
.selection1
.line
and
397 not State
.lines
.current_drawing
and
398 -- printable character created using shift key => delete selection
399 -- (we're not creating any ctrl-shift- or alt-shift- combinations using regular/printable keys)
400 (not App
.shift_down() or utf8
.len(key
) == 1) and
401 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
402 Text
.delete_selection(State
, State
.left
, State
.right
)
404 if State
.search_term
then
405 for _
,line_cache
in ipairs(State
.line_cache
) do line_cache
.starty
= nil end -- just in case we scroll
406 if chord
== 'escape' then
407 State
.search_term
= nil
408 State
.cursor1
= State
.search_backup
.cursor
409 State
.screen_top1
= State
.search_backup
.screen_top
410 State
.search_backup
= nil
411 Text
.redraw_all(State
) -- if we're scrolling, reclaim all fragments to avoid memory leaks
412 elseif chord
== 'return' then
413 State
.search_term
= nil
414 State
.search_backup
= nil
415 elseif chord
== 'backspace' then
416 local len
= utf8
.len(State
.search_term
)
417 local byte_offset
= Text
.offset(State
.search_term
, len
)
418 State
.search_term
= string.sub(State
.search_term
, 1, byte_offset
-1)
419 elseif chord
== 'down' then
420 State
.cursor1
.pos
= State
.cursor1
.pos
+1
421 Text
.search_next(State
)
422 elseif chord
== 'up' then
423 Text
.search_previous(State
)
426 elseif chord
== 'C-f' then
427 State
.search_term
= ''
428 State
.search_backup
= {
429 cursor
={line
=State
.cursor1
.line
, pos
=State
.cursor1
.pos
},
430 screen_top
={line
=State
.screen_top1
.line
, pos
=State
.screen_top1
.pos
},
433 elseif chord
== 'C-=' then
434 edit
.update_font_settings(State
, State
.font_height
+2)
435 Text
.redraw_all(State
)
436 elseif chord
== 'C--' then
437 if State
.font_height
> 2 then
438 edit
.update_font_settings(State
, State
.font_height
-2)
439 Text
.redraw_all(State
)
441 elseif chord
== 'C-0' then
442 edit
.update_font_settings(State
, 20)
443 Text
.redraw_all(State
)
445 elseif chord
== 'C-z' then
446 local event
= undo_event(State
)
448 local src
= event
.before
449 State
.screen_top1
= deepcopy(src
.screen_top
)
450 State
.cursor1
= deepcopy(src
.cursor
)
451 State
.selection1
= deepcopy(src
.selection
)
452 patch(State
.lines
, event
.after
, event
.before
)
453 patch_placeholders(State
.line_cache
, event
.after
, event
.before
)
454 -- invalidate various cached bits of lines
455 State
.lines
.current_drawing
= nil
456 -- if we're scrolling, reclaim all fragments to avoid memory leaks
457 Text
.redraw_all(State
)
460 elseif chord
== 'C-y' then
461 local event
= redo_event(State
)
463 local src
= event
.after
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
.before
, event
.after
)
468 -- invalidate various cached bits of lines
469 State
.lines
.current_drawing
= nil
470 -- if we're scrolling, reclaim all fragments to avoid memory leaks
471 Text
.redraw_all(State
)
475 elseif chord
== 'C-a' then
476 State
.selection1
= {line
=1, pos
=1}
477 State
.cursor1
= {line
=#State
.lines
, pos
=utf8
.len(State
.lines
[#State
.lines
].data
)+1}
478 elseif chord
== 'C-c' then
479 local s
= Text
.selection(State
)
483 elseif chord
== 'C-x' then
484 local s
= Text
.cut_selection(State
, State
.left
, State
.right
)
489 elseif chord
== 'C-v' then
490 -- We don't have a good sense of when to scroll, so we'll be conservative
491 -- and sometimes scroll when we didn't quite need to.
492 local before_line
= State
.cursor1
.line
493 local before
= snapshot(State
, before_line
)
494 local clipboard_data
= App
.get_clipboard()
495 for _
,code
in utf8
.codes(clipboard_data
) do
496 local c
= utf8
.char(code
)
498 Text
.insert_return(State
)
500 Text
.insert_at_cursor(State
, c
)
503 if Text
.cursor_out_of_screen(State
) then
504 Text
.snap_cursor_to_bottom_of_screen(State
, State
.left
, State
.right
)
507 record_undo_event(State
, {before
=before
, after
=snapshot(State
, before_line
, State
.cursor1
.line
)})
508 -- dispatch to drawing or text
509 elseif App
.mouse_down(1) or chord
:sub(1,2) == 'C-' then
510 -- DON'T reset line_cache.starty here
511 local drawing_index
, drawing
= Drawing
.current_drawing(State
)
512 if drawing_index
then
513 local before
= snapshot(State
, drawing_index
)
514 Drawing
.keychord_press(State
, chord
)
515 record_undo_event(State
, {before
=before
, after
=snapshot(State
, drawing_index
)})
518 elseif chord
== 'escape' and not App
.mouse_down(1) then
519 for _
,line
in ipairs(State
.lines
) do
520 if line
.mode
== 'drawing' then
521 line
.show_help
= false
524 elseif State
.lines
.current_drawing
and State
.current_drawing_mode
== 'name' then
525 if chord
== 'return' then
526 State
.current_drawing_mode
= State
.previous_drawing_mode
527 State
.previous_drawing_mode
= nil
529 local before
= snapshot(State
, State
.lines
.current_drawing_index
)
530 local drawing
= State
.lines
.current_drawing
531 local p
= drawing
.points
[drawing
.pending
.target_point
]
532 if chord
== 'escape' then
534 record_undo_event(State
, {before
=before
, after
=snapshot(State
, State
.lines
.current_drawing_index
)})
535 elseif chord
== 'backspace' then
536 local len
= utf8
.len(p
.name
)
538 local byte_offset
= Text
.offset(p
.name
, len
-1)
539 if len
== 1 then byte_offset
= 0 end
540 p
.name
= string.sub(p
.name
, 1, byte_offset
)
541 record_undo_event(State
, {before
=before
, after
=snapshot(State
, State
.lines
.current_drawing_index
)})
547 for _
,line_cache
in ipairs(State
.line_cache
) do line_cache
.starty
= nil end -- just in case we scroll
548 Text
.keychord_press(State
, chord
)
552 function edit
.key_release(State
, key
, scancode
)
555 function edit
.update_font_settings(State
, font_height
)
556 State
.font_height
= font_height
557 State
.font
= love
.graphics
.newFont(State
.font_height
)
558 State
.line_height
= math
.floor(font_height
*1.3)
561 --== some methods for tests
563 -- Insulate tests from some key globals so I don't have to change the vast
564 -- majority of tests when they're modified for the real app.
565 Test_margin_left
= 25
566 Test_margin_right
= 0
568 function edit
.initialize_test_state()
569 -- if you change these values, tests will start failing
570 return edit
.initialize_state(
573 App
.screen
.width
- Test_margin_right
,
574 love
.graphics
.getFont(),
579 -- all text_input events are also keypresses
580 -- TODO: handle chords of multiple keys
581 function edit
.run_after_text_input(State
, t
)
582 edit
.keychord_press(State
, t
)
583 edit
.text_input(State
, t
)
584 edit
.key_release(State
, t
)
585 App
.screen
.contents
= {}
586 edit
.update(State
, 0)
590 -- not all keys are text_input
591 function edit
.run_after_keychord(State
, chord
)
592 edit
.keychord_press(State
, chord
)
593 edit
.key_release(State
, chord
)
594 App
.screen
.contents
= {}
595 edit
.update(State
, 0)
599 function edit
.run_after_mouse_click(State
, x
,y
, mouse_button
)
600 App
.fake_mouse_press(x
,y
, mouse_button
)
601 edit
.mouse_press(State
, x
,y
, mouse_button
)
602 App
.fake_mouse_release(x
,y
, mouse_button
)
603 edit
.mouse_release(State
, x
,y
, mouse_button
)
604 App
.screen
.contents
= {}
605 edit
.update(State
, 0)
609 function edit
.run_after_mouse_press(State
, x
,y
, mouse_button
)
610 App
.fake_mouse_press(x
,y
, mouse_button
)
611 edit
.mouse_press(State
, x
,y
, mouse_button
)
612 App
.screen
.contents
= {}
613 edit
.update(State
, 0)
617 function edit
.run_after_mouse_release(State
, x
,y
, mouse_button
)
618 App
.fake_mouse_release(x
,y
, mouse_button
)
619 edit
.mouse_release(State
, x
,y
, mouse_button
)
620 App
.screen
.contents
= {}
621 edit
.update(State
, 0)