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}
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
25 -- run in both tests and a real run
26 function edit
.initialize_state(top
, left
, right
, font_height
, line_height
) -- currently always draws to bottom of screen
28 -- a line is either text or a drawing
29 -- a text is a table with:
32 -- a drawing is a table with:
34 -- a (y) coord in pixels (updated while painting screen),
36 -- an array of points, and
38 -- a shape is a table containing:
40 -- an array points for mode 'freehand' (raw x,y coords; freehand drawings don't pollute the points array of a drawing)
41 -- an array vertices for mode 'polygon', 'rectangle', 'square'
42 -- p1, p2 for mode 'line'
43 -- center, radius for mode 'circle'
44 -- center, radius, start_angle, end_angle for mode 'arc'
45 -- Unless otherwise specified, coord fields are normalized; a drawing is always 256 units wide
46 -- The field names are carefully chosen so that switching modes in midstream
47 -- remembers previously entered points where that makes sense.
48 lines
= {{mode
='text', data
=''}}, -- array of lines
50 -- Lines can be too long to fit on screen, in which case they _wrap_ into
51 -- multiple _screen lines_.
53 -- rendering wrapped text lines needs some additional short-lived data per line:
54 -- startpos, the index of data the line starts rendering from, can only be >1 for topmost line on screen
55 -- starty, the y coord in pixels the line starts rendering from
56 -- fragments: snippets of the line guaranteed to not straddle screen lines
57 -- screen_line_starting_pos: optional array of grapheme indices if it wraps over more than one screen line
60 -- Given wrapping, any potential location for the text cursor can be described in two ways:
61 -- * schema 1: As a combination of line index and position within a line (in utf8 codepoint units)
62 -- * schema 2: As a combination of line index, screen line index within the line, and a position within the screen line.
64 -- Most of the time we'll only persist positions in schema 1, translating to
65 -- schema 2 when that's convenient.
67 -- Make sure these coordinates are never aliased, so that changing one causes
68 -- action at a distance.
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
71 screen_bottom1
= {line
=1, pos
=1}, -- position of start of screen line at bottom of screen
74 -- some extra state to compute selection between mouse press and release
77 mousepress_shift
= nil,
78 -- when selecting text, avoid recomputing some state on every single frame
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
88 font_height
= font_height
,
89 line_height
= line_height
,
92 left
= math
.floor(left
),
93 right
= math
.floor(right
),
96 filename
= love
.filesystem
.getSourceBaseDirectory()..'/lines.txt', -- '/' should work even on Windows
105 search_backup
= nil, -- stuff to restore when cancelling search
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 edit
.put_cursor_on_first_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 function edit
.cursor_on_text(State
)
130 return State
.cursor1
.line
<= #State
.lines
131 and State
.lines
[State
.cursor1
.line
].mode
== 'text'
134 function edit
.put_cursor_on_first_text_line(State
)
135 for i
,line
in ipairs(State
.lines
) do
136 if line
.mode
== 'text' then
137 State
.cursor1
= {line
=i
, pos
=1}
143 function edit
.draw(State
)
144 State
.button_handlers
= {}
145 App
.color(Text_color
)
146 if #State
.lines
~= #State
.line_cache
then
147 print(('line_cache is out of date; %d when it should be %d'):format(#State
.line_cache
, #State
.lines
))
150 if not Text
.le1(State
.screen_top1
, State
.cursor1
) then
151 print(State
.screen_top1
.line
, State
.screen_top1
.pos
, State
.cursor1
.line
, State
.cursor1
.pos
)
158 for line_index
= State
.screen_top1
.line
,#State
.lines
do
159 local line
= State
.lines
[line_index
]
160 --? print('draw:', y, line_index, line)
161 if y
+ State
.line_height
> App
.screen
.height
then break end
162 State
.screen_bottom1
= {line
=line_index
, pos
=nil}
163 if line
.mode
== 'text' then
164 --? print('text.draw', y, line_index)
166 if line_index
== State
.screen_top1
.line
then
167 startpos
= State
.screen_top1
.pos
169 if line
.data
== '' then
170 -- button to insert new drawing
171 button(State
, 'draw', {x
=State
.left
-Margin_left
+4, y
=y
+4, w
=12,h
=12, color
={1,1,0},
172 icon
= icon
.insert_drawing
,
173 onpress1
= function()
174 Drawing
.before
= snapshot(State
, line_index
-1, line_index
)
175 table.insert(State
.lines
, line_index
, {mode
='drawing', y
=y
, h
=256/2, points
={}, shapes
={}, pending
={}})
176 table.insert(State
.line_cache
, line_index
, {})
177 if State
.cursor1
.line
>= line_index
then
178 State
.cursor1
.line
= State
.cursor1
.line
+1
181 record_undo_event(State
, {before
=Drawing
.before
, after
=snapshot(State
, line_index
-1, line_index
+1)})
185 y
, State
.screen_bottom1
.pos
= Text
.draw(State
, line_index
, y
, startpos
)
187 elseif line
.mode
== 'drawing' then
188 y
= y
+Drawing_padding_top
189 Drawing
.draw(State
, line_index
, y
)
190 y
= y
+ Drawing
.pixels(line
.h
, State
.width
) + Drawing_padding_bottom
196 if State
.search_term
then
197 Text
.draw_search_bar(State
)
201 function edit
.update(State
, dt
)
202 Drawing
.update(State
, dt
)
203 if State
.next_save
and State
.next_save
< Current_time
then
205 State
.next_save
= nil
209 function schedule_save(State
)
210 if State
.next_save
== nil then
211 State
.next_save
= Current_time
+ 3 -- short enough that you're likely to still remember what you did
215 function edit
.quit(State
)
216 -- make sure to save before quitting
217 if State
.next_save
then
219 -- give some time for the OS to flush everything to disk
220 love
.timer
.sleep(0.1)
224 function edit
.mouse_press(State
, x
,y
, mouse_button
)
225 if State
.search_term
then return end
226 --? print('press', State.cursor1.line)
227 if mouse_press_consumed_by_any_button_handler(State
, x
,y
, mouse_button
) then
228 -- press on a button and it returned 'true' to short-circuit
232 for line_index
,line
in ipairs(State
.lines
) do
233 if line
.mode
== 'text' then
234 if Text
.in_line(State
, line_index
, x
,y
) then
235 -- delicate dance between cursor, selection and old cursor/selection
237 -- regular press+release: sets cursor, clears selection
238 -- shift press+release:
239 -- sets selection to old cursor if not set otherwise leaves it untouched
241 -- press and hold to start a selection: sets selection on press, cursor on release
242 -- press and hold, then press shift: ignore shift
243 -- i.e. mouse_release should never look at shift state
244 State
.old_cursor1
= State
.cursor1
245 State
.old_selection1
= State
.selection1
246 State
.mousepress_shift
= App
.shift_down()
249 pos
=Text
.to_pos_on_line(State
, line_index
, x
, y
),
251 --? print('selection', State.selection1.line, State.selection1.pos)
254 elseif line
.mode
== 'drawing' then
255 local line_cache
= State
.line_cache
[line_index
]
256 if Drawing
.in_drawing(line
, line_cache
, x
, y
, State
.left
,State
.right
) then
257 State
.lines
.current_drawing_index
= line_index
258 State
.lines
.current_drawing
= line
259 Drawing
.before
= snapshot(State
, line_index
)
260 Drawing
.mouse_press(State
, line_index
, x
,y
, mouse_button
)
267 function edit
.mouse_release(State
, x
,y
, mouse_button
)
268 if State
.search_term
then return end
269 --? print('release', State.cursor1.line)
270 if State
.lines
.current_drawing
then
271 Drawing
.mouse_release(State
, x
,y
, mouse_button
)
273 if Drawing
.before
then
274 record_undo_event(State
, {before
=Drawing
.before
, after
=snapshot(State
, State
.lines
.current_drawing_index
)})
278 for line_index
,line
in ipairs(State
.lines
) do
279 if line
.mode
== 'text' then
280 if Text
.in_line(State
, line_index
, x
,y
) then
281 --? print('reset selection')
284 pos
=Text
.to_pos_on_line(State
, line_index
, x
, y
),
286 --? print('cursor', State.cursor1.line, State.cursor1.pos)
287 if State
.mousepress_shift
then
288 if State
.old_selection1
.line
== nil then
289 State
.selection1
= State
.old_cursor1
291 State
.selection1
= State
.old_selection1
294 State
.old_cursor1
, State
.old_selection1
, State
.mousepress_shift
= nil
295 if eq(State
.cursor1
, State
.selection1
) then
296 State
.selection1
= {}
302 --? print('selection:', State.selection1.line, State.selection1.pos)
306 function edit
.mouse_wheel_move(State
, dx
,dy
)
308 State
.cursor1
= {line
=State
.screen_top1
.line
, pos
=State
.screen_top1
.pos
}
309 for i
=1,math
.floor(dy
) do
313 State
.cursor1
= {line
=State
.screen_bottom1
.line
, pos
=State
.screen_bottom1
.pos
}
314 for i
=1,math
.floor(-dy
) do
320 function edit
.text_input(State
, t
)
321 --? print('text input', t)
322 if State
.search_term
then
323 State
.search_term
= State
.search_term
..t
324 Text
.search_next(State
)
325 elseif State
.lines
.current_drawing
and State
.current_drawing_mode
== 'name' then
326 local before
= snapshot(State
, State
.lines
.current_drawing_index
)
327 local drawing
= State
.lines
.current_drawing
328 local p
= drawing
.points
[drawing
.pending
.target_point
]
330 record_undo_event(State
, {before
=before
, after
=snapshot(State
, State
.lines
.current_drawing_index
)})
332 local drawing_index
, drawing
= Drawing
.current_drawing(State
)
333 if drawing_index
== nil then
334 for _
,line_cache
in ipairs(State
.line_cache
) do line_cache
.starty
= nil end -- just in case we scroll
335 Text
.text_input(State
, t
)
341 function edit
.keychord_press(State
, chord
, key
)
342 if State
.selection1
.line
and
343 not State
.lines
.current_drawing
and
344 -- printable character created using shift key => delete selection
345 -- (we're not creating any ctrl-shift- or alt-shift- combinations using regular/printable keys)
346 (not App
.shift_down() or utf8
.len(key
) == 1) and
347 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
348 Text
.delete_selection(State
, State
.left
, State
.right
)
350 if State
.search_term
then
351 for _
,line_cache
in ipairs(State
.line_cache
) do line_cache
.starty
= nil end -- just in case we scroll
352 if chord
== 'escape' then
353 State
.search_term
= nil
354 State
.cursor1
= State
.search_backup
.cursor
355 State
.screen_top1
= State
.search_backup
.screen_top
356 State
.search_backup
= nil
357 Text
.redraw_all(State
) -- if we're scrolling, reclaim all fragments to avoid memory leaks
358 elseif chord
== 'return' then
359 State
.search_term
= nil
360 State
.search_backup
= nil
361 elseif chord
== 'backspace' then
362 local len
= utf8
.len(State
.search_term
)
363 local byte_offset
= Text
.offset(State
.search_term
, len
)
364 State
.search_term
= string.sub(State
.search_term
, 1, byte_offset
-1)
365 elseif chord
== 'down' then
366 State
.cursor1
.pos
= State
.cursor1
.pos
+1
367 Text
.search_next(State
)
368 elseif chord
== 'up' then
369 Text
.search_previous(State
)
372 elseif chord
== 'C-f' then
373 State
.search_term
= ''
374 State
.search_backup
= {
375 cursor
={line
=State
.cursor1
.line
, pos
=State
.cursor1
.pos
},
376 screen_top
={line
=State
.screen_top1
.line
, pos
=State
.screen_top1
.pos
},
379 elseif chord
== 'C-=' then
380 edit
.update_font_settings(State
, State
.font_height
+2)
381 Text
.redraw_all(State
)
382 elseif chord
== 'C--' then
383 if State
.font_height
> 2 then
384 edit
.update_font_settings(State
, State
.font_height
-2)
385 Text
.redraw_all(State
)
387 elseif chord
== 'C-0' then
388 edit
.update_font_settings(State
, 20)
389 Text
.redraw_all(State
)
391 elseif chord
== 'C-z' then
392 for _
,line_cache
in ipairs(State
.line_cache
) do line_cache
.starty
= nil end -- just in case we scroll
393 local event
= undo_event(State
)
395 local src
= event
.before
396 State
.screen_top1
= deepcopy(src
.screen_top
)
397 State
.cursor1
= deepcopy(src
.cursor
)
398 State
.selection1
= deepcopy(src
.selection
)
399 patch(State
.lines
, event
.after
, event
.before
)
400 patch_placeholders(State
.line_cache
, event
.after
, event
.before
)
401 -- invalidate various cached bits of lines
402 State
.lines
.current_drawing
= nil
403 -- if we're scrolling, reclaim all fragments to avoid memory leaks
404 Text
.redraw_all(State
)
407 elseif chord
== 'C-y' then
408 for _
,line_cache
in ipairs(State
.line_cache
) do line_cache
.starty
= nil end -- just in case we scroll
409 local event
= redo_event(State
)
411 local src
= event
.after
412 State
.screen_top1
= deepcopy(src
.screen_top
)
413 State
.cursor1
= deepcopy(src
.cursor
)
414 State
.selection1
= deepcopy(src
.selection
)
415 patch(State
.lines
, event
.before
, event
.after
)
416 -- invalidate various cached bits of lines
417 State
.lines
.current_drawing
= nil
418 -- if we're scrolling, reclaim all fragments to avoid memory leaks
419 Text
.redraw_all(State
)
423 elseif chord
== 'C-a' then
424 State
.selection1
= {line
=1, pos
=1}
425 State
.cursor1
= {line
=#State
.lines
, pos
=utf8
.len(State
.lines
[#State
.lines
].data
)+1}
426 elseif chord
== 'C-c' then
427 local s
= Text
.selection(State
)
429 App
.setClipboardText(s
)
431 elseif chord
== 'C-x' then
432 for _
,line_cache
in ipairs(State
.line_cache
) do line_cache
.starty
= nil end -- just in case we scroll
433 local s
= Text
.cut_selection(State
, State
.left
, State
.right
)
435 App
.setClipboardText(s
)
438 elseif chord
== 'C-v' then
439 for _
,line_cache
in ipairs(State
.line_cache
) do line_cache
.starty
= nil end -- just in case we scroll
440 -- We don't have a good sense of when to scroll, so we'll be conservative
441 -- and sometimes scroll when we didn't quite need to.
442 local before_line
= State
.cursor1
.line
443 local before
= snapshot(State
, before_line
)
444 local clipboard_data
= App
.getClipboardText()
445 for _
,code
in utf8
.codes(clipboard_data
) do
446 local c
= utf8
.char(code
)
448 Text
.insert_return(State
)
450 Text
.insert_at_cursor(State
, c
)
453 if Text
.cursor_out_of_screen(State
) then
454 Text
.snap_cursor_to_bottom_of_screen(State
, State
.left
, State
.right
)
457 record_undo_event(State
, {before
=before
, after
=snapshot(State
, before_line
, State
.cursor1
.line
)})
458 -- dispatch to drawing or text
459 elseif App
.mouse_down(1) or chord
:sub(1,2) == 'C-' then
460 -- DON'T reset line_cache.starty here
461 local drawing_index
, drawing
= Drawing
.current_drawing(State
)
462 if drawing_index
then
463 local before
= snapshot(State
, drawing_index
)
464 Drawing
.keychord_press(State
, chord
)
465 record_undo_event(State
, {before
=before
, after
=snapshot(State
, drawing_index
)})
468 elseif chord
== 'escape' and not App
.mouse_down(1) then
469 for _
,line
in ipairs(State
.lines
) do
470 if line
.mode
== 'drawing' then
471 line
.show_help
= false
474 elseif State
.lines
.current_drawing
and State
.current_drawing_mode
== 'name' then
475 if chord
== 'return' then
476 State
.current_drawing_mode
= State
.previous_drawing_mode
477 State
.previous_drawing_mode
= nil
479 local before
= snapshot(State
, State
.lines
.current_drawing_index
)
480 local drawing
= State
.lines
.current_drawing
481 local p
= drawing
.points
[drawing
.pending
.target_point
]
482 if chord
== 'escape' then
484 record_undo_event(State
, {before
=before
, after
=snapshot(State
, State
.lines
.current_drawing_index
)})
485 elseif chord
== 'backspace' then
486 local len
= utf8
.len(p
.name
)
488 local byte_offset
= Text
.offset(p
.name
, len
-1)
489 if len
== 1 then byte_offset
= 0 end
490 p
.name
= string.sub(p
.name
, 1, byte_offset
)
491 record_undo_event(State
, {before
=before
, after
=snapshot(State
, State
.lines
.current_drawing_index
)})
497 for _
,line_cache
in ipairs(State
.line_cache
) do line_cache
.starty
= nil end -- just in case we scroll
498 Text
.keychord_press(State
, chord
)
502 function edit
.key_release(State
, key
, scancode
)
505 function edit
.update_font_settings(State
, font_height
)
506 State
.font_height
= font_height
507 love
.graphics
.setFont(love
.graphics
.newFont(State
.font_height
))
508 State
.line_height
= math
.floor(font_height
*1.3)
511 --== some methods for tests
513 -- Insulate tests from some key globals so I don't have to change the vast
514 -- majority of tests when they're modified for the real app.
515 Test_margin_left
= 25
516 Test_margin_right
= 0
518 function edit
.initialize_test_state()
519 -- if you change these values, tests will start failing
520 return edit
.initialize_state(
523 App
.screen
.width
- Test_margin_right
,
524 14, -- font height assuming default LÖVE font
528 -- all text_input events are also keypresses
529 -- TODO: handle chords of multiple keys
530 function edit
.run_after_text_input(State
, t
)
531 edit
.keychord_press(State
, t
)
532 edit
.text_input(State
, t
)
533 edit
.key_release(State
, t
)
534 App
.screen
.contents
= {}
535 edit
.update(State
, 0)
539 -- not all keys are text_input
540 function edit
.run_after_keychord(State
, chord
)
541 edit
.keychord_press(State
, chord
)
542 edit
.key_release(State
, chord
)
543 App
.screen
.contents
= {}
544 edit
.update(State
, 0)
548 function edit
.run_after_mouse_click(State
, x
,y
, mouse_button
)
549 App
.fake_mouse_press(x
,y
, mouse_button
)
550 edit
.mouse_press(State
, x
,y
, mouse_button
)
551 App
.fake_mouse_release(x
,y
, mouse_button
)
552 edit
.mouse_release(State
, x
,y
, mouse_button
)
553 App
.screen
.contents
= {}
554 edit
.update(State
, 0)
558 function edit
.run_after_mouse_press(State
, x
,y
, mouse_button
)
559 App
.fake_mouse_press(x
,y
, mouse_button
)
560 edit
.mouse_press(State
, x
,y
, mouse_button
)
561 App
.screen
.contents
= {}
562 edit
.update(State
, 0)
566 function edit
.run_after_mouse_release(State
, x
,y
, mouse_button
)
567 App
.fake_mouse_release(x
,y
, mouse_button
)
568 edit
.mouse_release(State
, x
,y
, mouse_button
)
569 App
.screen
.contents
= {}
570 edit
.update(State
, 0)