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, a
=0.4} -- 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
, 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:
35 -- an array of points, and
37 -- a shape is a table containing:
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 -- screen_line_starting_pos: optional array of codepoint indices if it wraps over more than one screen line
57 -- Given wrapping, any potential location for the text cursor can be described in two ways:
58 -- * schema 1: As a combination of line index and position within a line (in utf8 codepoint units)
59 -- * schema 2: As a combination of line index, screen line index within the line, and a position within the screen line.
61 -- Most of the time we'll only persist positions in schema 1, translating to
62 -- schema 2 when that's convenient.
64 -- Make sure these coordinates are never aliased, so that changing one causes
65 -- action at a distance.
67 -- On lines that are drawings, pos will be nil.
68 screen_top1
= {line
=1, pos
=1}, -- position of start of screen line at top of screen
69 cursor1
= {line
=1, pos
=1}, -- position of cursor; must be on a text line
72 -- some extra state to compute selection between mouse press and release
75 mousepress_shift
= nil,
77 -- cursor coordinates in pixels
81 current_drawing_mode
= 'line', -- one of the available shape modes
82 previous_drawing_mode
= nil, -- extra state for some ephemeral modes like moving/deleting/naming points
85 font_height
= font_height
,
86 line_height
= line_height
,
89 left
= math
.floor(left
),
90 right
= math
.floor(right
),
93 filename
= love
.filesystem
.getSourceBaseDirectory()..'/lines.txt', -- '/' should work even on Windows
102 search_backup
= nil, -- stuff to restore when cancelling search
105 end -- edit.initialize_state
107 function edit
.check_locs(State
)
108 -- if State is inconsistent (i.e. file changed by some other program),
109 -- throw away all cursor state entirely
110 if edit
.invalid1(State
, State
.screen_top1
)
111 or edit
.invalid_cursor1(State
)
112 or not edit
.cursor_on_text(State
)
113 or not Text
.le1(State
.screen_top1
, State
.cursor1
) then
114 State
.screen_top1
= {line
=1, pos
=1}
115 State
.cursor1
= {line
=1, pos
=1}
116 edit
.put_cursor_on_next_text_line(State
)
120 function edit
.invalid1(State
, loc1
)
121 if loc1
.line
> #State
.lines
then return true end
122 local l
= State
.lines
[loc1
.line
]
123 if l
.mode
~= 'text' then return false end -- pos is irrelevant to validity for a drawing line
124 return loc1
.pos
> #State
.lines
[loc1
.line
].data
127 -- cursor loc in particular differs from other locs in one way:
128 -- pos might occur just after end of line
129 function edit
.invalid_cursor1(State
)
130 local cursor1
= State
.cursor1
131 if cursor1
.line
> #State
.lines
then return true end
132 local l
= State
.lines
[cursor1
.line
]
133 if l
.mode
~= 'text' then return false end -- pos is irrelevant to validity for a drawing line
134 return cursor1
.pos
> #State
.lines
[cursor1
.line
].data
+ 1
137 function edit
.cursor_on_text(State
)
138 return State
.cursor1
.line
<= #State
.lines
139 and State
.lines
[State
.cursor1
.line
].mode
== 'text'
142 function edit
.put_cursor_on_next_text_line(State
)
143 local line
= State
.cursor1
.line
144 while line
< #State
.lines
do
146 if State
.lines
[line
].mode
== 'text' then
147 State
.cursor1
.line
= line
148 State
.cursor1
.pos
= 1
154 function edit
.draw(State
)
155 State
.button_handlers
= {}
156 love
.graphics
.setFont(State
.font
)
157 App
.color(Text_color
)
158 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
))
159 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
))
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 if line
.mode
== 'text' then
169 --? print('text.draw', y, line_index)
171 if line_index
== State
.screen_top1
.line
then
172 startpos
= State
.screen_top1
.pos
174 if line
.data
== '' then
175 -- button to insert new drawing
176 button(State
, 'draw', {x
=State
.left
-Margin_left
+4, y
=y
+4, w
=12,h
=12, bg
={r
=1,g
=1,b
=0},
177 icon
= icon
.insert_drawing
,
178 onpress1
= function()
179 Drawing
.before
= snapshot(State
, line_index
-1, line_index
)
180 table.insert(State
.lines
, line_index
, {mode
='drawing', y
=y
, h
=256/2, points
={}, shapes
={}, pending
={}})
181 table.insert(State
.line_cache
, line_index
, {})
182 if State
.cursor1
.line
>= line_index
then
183 State
.cursor1
.line
= State
.cursor1
.line
+1
185 record_undo_event(State
, {before
=Drawing
.before
, after
=snapshot(State
, line_index
-1, line_index
+1)})
191 y
= Text
.draw(State
, line_index
, y
, startpos
)
193 elseif line
.mode
== 'drawing' then
194 y
= y
+Drawing_padding_top
195 Drawing
.draw(State
, line_index
, y
)
196 y
= y
+ Drawing
.pixels(line
.h
, State
.width
) + Drawing_padding_bottom
198 assert(false, ('unknown line mode %s'):format(line
.mode
))
201 if State
.search_term
then
202 Text
.draw_search_bar(State
)
206 function edit
.update(State
, dt
)
207 Drawing
.update(State
, dt
)
208 if State
.next_save
and State
.next_save
< Current_time
then
210 State
.next_save
= nil
214 function schedule_save(State
)
215 if State
.next_save
== nil then
216 State
.next_save
= Current_time
+ 3 -- short enough that you're likely to still remember what you did
220 function edit
.quit(State
)
221 -- make sure to save before quitting
222 if State
.next_save
then
224 -- give some time for the OS to flush everything to disk
225 love
.timer
.sleep(0.1)
229 function edit
.mouse_press(State
, x
,y
, mouse_button
)
230 if State
.search_term
then return end
231 State
.mouse_down
= mouse_button
232 --? print_and_log(('edit.mouse_press: cursor at %d,%d'):format(State.cursor1.line, State.cursor1.pos))
233 if mouse_press_consumed_by_any_button(State
, x
,y
, mouse_button
) then
234 -- press on a button and it returned 'true' to short-circuit
238 if y
< State
.top
then
239 State
.old_cursor1
= State
.cursor1
240 State
.old_selection1
= State
.selection1
241 State
.mousepress_shift
= App
.shift_down()
243 line
=State
.screen_top1
.line
,
244 pos
=State
.screen_top1
.pos
,
249 for line_index
,line
in ipairs(State
.lines
) do
250 if line
.mode
== 'text' then
251 if Text
.in_line(State
, line_index
, x
,y
) then
252 -- delicate dance between cursor, selection and old cursor/selection
254 -- regular press+release: sets cursor, clears selection
255 -- shift press+release:
256 -- sets selection to old cursor if not set otherwise leaves it untouched
258 -- press and hold to start a selection: sets selection on press, cursor on release
259 -- press and hold, then press shift: ignore shift
260 -- i.e. mouse_release should never look at shift state
261 --? print_and_log(('edit.mouse_press: in line %d'):format(line_index))
262 State
.old_cursor1
= State
.cursor1
263 State
.old_selection1
= State
.selection1
264 State
.mousepress_shift
= App
.shift_down()
267 pos
=Text
.to_pos_on_line(State
, line_index
, x
, y
),
271 elseif line
.mode
== 'drawing' then
272 if Drawing
.in_drawing(State
, line_index
, x
, y
, State
.left
,State
.right
) then
273 State
.lines
.current_drawing_index
= line_index
274 State
.lines
.current_drawing
= line
275 Drawing
.before
= snapshot(State
, line_index
)
276 Drawing
.mouse_press(State
, line_index
, x
,y
, mouse_button
)
282 -- still here? mouse press is below all screen lines
283 State
.old_cursor1
= State
.cursor1
284 State
.old_selection1
= State
.selection1
285 State
.mousepress_shift
= App
.shift_down()
286 State
.selection1
= Text
.final_text_loc_on_screen(State
)
289 function edit
.mouse_release(State
, x
,y
, mouse_button
)
290 if State
.search_term
then return end
291 --? print_and_log(('edit.mouse_release: cursor at %d,%d'):format(State.cursor1.line, State.cursor1.pos))
292 State
.mouse_down
= nil
293 if State
.lines
.current_drawing
then
294 Drawing
.mouse_release(State
, x
,y
, mouse_button
)
295 if Drawing
.before
then
296 record_undo_event(State
, {before
=Drawing
.before
, after
=snapshot(State
, State
.lines
.current_drawing_index
)})
301 --? print_and_log('edit.mouse_release: no current drawing')
302 if y
< State
.top
then
303 State
.cursor1
= deepcopy(State
.screen_top1
)
304 edit
.clean_up_mouse_press(State
)
308 for line_index
,line
in ipairs(State
.lines
) do
309 if line
.mode
== 'text' then
310 if Text
.in_line(State
, line_index
, x
,y
) then
311 --? print_and_log(('edit.mouse_release: in line %d'):format(line_index))
314 pos
=Text
.to_pos_on_line(State
, line_index
, x
, y
),
316 --? print_and_log(('edit.mouse_release: cursor now %d,%d'):format(State.cursor1.line, State.cursor1.pos))
317 edit
.clean_up_mouse_press(State
)
323 -- still here? mouse release is below all screen lines
324 State
.cursor1
= Text
.final_text_loc_on_screen(State
)
325 edit
.clean_up_mouse_press(State
)
326 --? 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))
330 function edit
.clean_up_mouse_press(State
)
331 if State
.mousepress_shift
then
332 if State
.old_selection1
.line
== nil then
333 State
.selection1
= State
.old_cursor1
335 State
.selection1
= State
.old_selection1
338 State
.old_cursor1
, State
.old_selection1
, State
.mousepress_shift
= nil
339 if eq(State
.cursor1
, State
.selection1
) then
340 State
.selection1
= {}
344 function edit
.mouse_wheel_move(State
, dx
,dy
)
346 State
.cursor1
= deepcopy(State
.screen_top1
)
347 edit
.put_cursor_on_next_text_line(State
)
348 for i
=1,math
.floor(dy
) do
352 State
.cursor1
= Text
.screen_bottom1(State
)
353 edit
.put_cursor_on_next_text_line(State
)
354 for i
=1,math
.floor(-dy
) do
360 function edit
.text_input(State
, t
)
361 --? print('text input', t)
362 if State
.search_term
then
363 State
.search_term
= State
.search_term
..t
364 Text
.search_next(State
)
365 elseif State
.lines
.current_drawing
and State
.current_drawing_mode
== 'name' then
366 local before
= snapshot(State
, State
.lines
.current_drawing_index
)
367 local drawing
= State
.lines
.current_drawing
368 local p
= drawing
.points
[drawing
.pending
.target_point
]
370 record_undo_event(State
, {before
=before
, after
=snapshot(State
, State
.lines
.current_drawing_index
)})
372 local drawing_index
, drawing
= Drawing
.current_drawing(State
)
373 if drawing_index
== nil then
374 Text
.text_input(State
, t
)
380 function edit
.keychord_press(State
, chord
, key
)
381 if State
.selection1
.line
and
382 not State
.lines
.current_drawing
and
383 -- printable character created using shift key => delete selection
384 -- (we're not creating any ctrl-shift- or alt-shift- combinations using regular/printable keys)
385 (not App
.shift_down() or utf8
.len(key
) == 1) and
386 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
387 Text
.delete_selection_and_record_undo_event(State
)
389 if State
.search_term
then
390 if chord
== 'escape' then
391 State
.search_term
= nil
392 State
.cursor1
= State
.search_backup
.cursor
393 State
.screen_top1
= State
.search_backup
.screen_top
394 State
.search_backup
= nil
395 Text
.redraw_all(State
) -- if we're scrolling, reclaim all line caches to avoid memory leaks
396 elseif chord
== 'return' then
397 State
.search_term
= nil
398 State
.search_backup
= nil
399 elseif chord
== 'backspace' then
400 local len
= utf8
.len(State
.search_term
)
401 local byte_offset
= Text
.offset(State
.search_term
, len
)
402 State
.search_term
= string.sub(State
.search_term
, 1, byte_offset
-1)
403 State
.cursor
= deepcopy(State
.search_backup
.cursor
)
404 State
.screen_top
= deepcopy(State
.search_backup
.screen_top
)
405 Text
.search_next(State
)
406 elseif chord
== 'down' then
407 if #State
.search_term
> 0 then
409 Text
.search_next(State
)
411 elseif chord
== 'up' then
412 Text
.search_previous(State
)
415 elseif chord
== 'C-f' then
416 State
.search_term
= ''
417 State
.search_backup
= {
418 cursor
={line
=State
.cursor1
.line
, pos
=State
.cursor1
.pos
},
419 screen_top
={line
=State
.screen_top1
.line
, pos
=State
.screen_top1
.pos
},
422 elseif chord
== 'C-=' then
423 edit
.update_font_settings(State
, State
.font_height
+2)
424 Text
.redraw_all(State
)
425 elseif chord
== 'C--' then
426 if State
.font_height
> 2 then
427 edit
.update_font_settings(State
, State
.font_height
-2)
428 Text
.redraw_all(State
)
430 elseif chord
== 'C-0' then
431 edit
.update_font_settings(State
, 20)
432 Text
.redraw_all(State
)
434 elseif chord
== 'C-z' then
435 local event
= undo_event(State
)
437 local src
= event
.before
438 State
.screen_top1
= deepcopy(src
.screen_top
)
439 State
.cursor1
= deepcopy(src
.cursor
)
440 State
.selection1
= deepcopy(src
.selection
)
441 patch(State
.lines
, event
.after
, event
.before
)
442 -- invalidate various cached bits of lines
443 State
.lines
.current_drawing
= nil
444 Text
.redraw_all(State
) -- if we're scrolling, reclaim all line caches to avoid memory leaks
447 elseif chord
== 'C-y' then
448 local event
= redo_event(State
)
450 local src
= event
.after
451 State
.screen_top1
= deepcopy(src
.screen_top
)
452 State
.cursor1
= deepcopy(src
.cursor
)
453 State
.selection1
= deepcopy(src
.selection
)
454 patch(State
.lines
, event
.before
, event
.after
)
455 -- invalidate various cached bits of lines
456 State
.lines
.current_drawing
= nil
457 Text
.redraw_all(State
) -- if we're scrolling, reclaim all line caches to avoid memory leaks
461 elseif chord
== 'C-a' then
462 State
.selection1
= {line
=1, pos
=1}
463 State
.cursor1
= {line
=#State
.lines
, pos
=utf8
.len(State
.lines
[#State
.lines
].data
)+1}
464 elseif chord
== 'C-c' then
465 local s
= Text
.selection(State
)
469 elseif chord
== 'C-x' then
470 local s
= Text
.cut_selection_and_record_undo_event(State
)
475 elseif chord
== 'C-v' then
476 -- We don't have a good sense of when to scroll, so we'll be conservative
477 -- and sometimes scroll when we didn't quite need to.
478 local before_line
= State
.cursor1
.line
479 local before
= snapshot(State
, before_line
)
480 local clipboard_data
= App
.get_clipboard()
481 for _
,code
in utf8
.codes(clipboard_data
) do
482 local c
= utf8
.char(code
)
484 Text
.insert_return(State
)
486 Text
.insert_at_cursor(State
, c
)
489 if Text
.cursor_out_of_screen(State
) then
490 Text
.snap_cursor_to_bottom_of_screen(State
, State
.left
, State
.right
)
492 record_undo_event(State
, {before
=before
, after
=snapshot(State
, before_line
, State
.cursor1
.line
)})
494 -- dispatch to drawing or text
495 elseif App
.mouse_down(1) or chord
:sub(1,2) == 'C-' then
496 local drawing_index
, drawing
= Drawing
.current_drawing(State
)
497 if drawing_index
then
498 local before
= snapshot(State
, drawing_index
)
499 Drawing
.keychord_press(State
, chord
)
500 record_undo_event(State
, {before
=before
, after
=snapshot(State
, drawing_index
)})
503 elseif chord
== 'escape' and not App
.mouse_down(1) then
504 for _
,line
in ipairs(State
.lines
) do
505 if line
.mode
== 'drawing' then
506 line
.show_help
= false
509 elseif State
.lines
.current_drawing
and State
.current_drawing_mode
== 'name' then
510 if chord
== 'return' then
511 State
.current_drawing_mode
= State
.previous_drawing_mode
512 State
.previous_drawing_mode
= nil
514 local before
= snapshot(State
, State
.lines
.current_drawing_index
)
515 local drawing
= State
.lines
.current_drawing
516 local p
= drawing
.points
[drawing
.pending
.target_point
]
517 if chord
== 'escape' then
519 record_undo_event(State
, {before
=before
, after
=snapshot(State
, State
.lines
.current_drawing_index
)})
520 elseif chord
== 'backspace' then
521 local len
= utf8
.len(p
.name
)
523 local byte_offset
= Text
.offset(p
.name
, len
-1)
524 if len
== 1 then byte_offset
= 0 end
525 p
.name
= string.sub(p
.name
, 1, byte_offset
)
526 record_undo_event(State
, {before
=before
, after
=snapshot(State
, State
.lines
.current_drawing_index
)})
532 Text
.keychord_press(State
, chord
)
536 function edit
.key_release(State
, key
, scancode
)
539 function edit
.update_font_settings(State
, font_height
, font
)
540 State
.font_height
= font_height
541 State
.font
= font
or 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(
557 App
.screen
.width
- Test_margin_right
,
558 love
.graphics
.getFont(),
563 -- all text_input events are also keypresses
564 -- TODO: handle chords of multiple keys
565 function edit
.run_after_text_input(State
, t
)
566 edit
.keychord_press(State
, t
)
567 edit
.text_input(State
, t
)
568 edit
.key_release(State
, t
)
569 App
.screen
.contents
= {}
570 edit
.update(State
, 0)
574 -- not all keys are text_input
575 function edit
.run_after_keychord(State
, chord
, key
)
576 edit
.keychord_press(State
, chord
, key
)
577 edit
.key_release(State
, key
)
578 App
.screen
.contents
= {}
579 edit
.update(State
, 0)
583 function edit
.run_after_mouse_click(State
, x
,y
, mouse_button
)
584 App
.fake_mouse_press(x
,y
, mouse_button
)
585 edit
.mouse_press(State
, x
,y
, mouse_button
)
587 App
.fake_mouse_release(x
,y
, mouse_button
)
588 edit
.mouse_release(State
, x
,y
, mouse_button
)
589 App
.screen
.contents
= {}
590 edit
.update(State
, 0)
594 function edit
.run_after_mouse_press(State
, x
,y
, mouse_button
)
595 App
.fake_mouse_press(x
,y
, mouse_button
)
596 edit
.mouse_press(State
, x
,y
, mouse_button
)
597 App
.screen
.contents
= {}
598 edit
.update(State
, 0)
602 function edit
.run_after_mouse_release(State
, x
,y
, mouse_button
)
603 App
.fake_mouse_release(x
,y
, mouse_button
)
604 edit
.mouse_release(State
, x
,y
, mouse_button
)
605 App
.screen
.contents
= {}
606 edit
.update(State
, 0)