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,
79 -- cursor coordinates in pixels
83 current_drawing_mode
= 'line',
84 previous_drawing_mode
= nil, -- extra state for some ephemeral modes like moving/deleting/naming points
86 font_height
= font_height
,
87 line_height
= line_height
,
90 left
= math
.floor(left
),
91 right
= math
.floor(right
),
94 filename
= love
.filesystem
.getSourceBaseDirectory()..'/lines.txt', -- '/' should work even on Windows
103 search_backup
= nil, -- stuff to restore when cancelling search
106 end -- App.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
)
145 if State
.cursor1
.line
>= #State
.lines
then
148 if State
.lines
[State
.cursor1
.line
].mode
== 'text' then
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 App
.color(Text_color
)
160 if #State
.lines
~= #State
.line_cache
then
161 print(('line_cache is out of date; %d when it should be %d'):format(#State
.line_cache
, #State
.lines
))
164 if not Text
.le1(State
.screen_top1
, State
.cursor1
) then
165 print(State
.screen_top1
.line
, State
.screen_top1
.pos
, State
.cursor1
.line
, State
.cursor1
.pos
)
171 local screen_bottom1
= {line
=nil, pos
=nil}
173 for line_index
= State
.screen_top1
.line
,#State
.lines
do
174 local line
= State
.lines
[line_index
]
175 --? print('draw:', y, line_index, line)
176 if y
+ State
.line_height
> App
.screen
.height
then break end
177 screen_bottom1
.line
= line_index
178 if line
.mode
== 'text' then
179 --? print('text.draw', y, line_index)
181 if line_index
== State
.screen_top1
.line
then
182 startpos
= State
.screen_top1
.pos
184 if line
.data
== '' then
185 -- button to insert new drawing
186 button(State
, 'draw', {x
=State
.left
-Margin_left
+4, y
=y
+4, w
=12,h
=12, color
={1,1,0},
187 icon
= icon
.insert_drawing
,
188 onpress1
= function()
189 Drawing
.before
= snapshot(State
, line_index
-1, line_index
)
190 table.insert(State
.lines
, line_index
, {mode
='drawing', y
=y
, h
=256/2, points
={}, shapes
={}, pending
={}})
191 table.insert(State
.line_cache
, line_index
, {})
192 if State
.cursor1
.line
>= line_index
then
193 State
.cursor1
.line
= State
.cursor1
.line
+1
196 record_undo_event(State
, {before
=Drawing
.before
, after
=snapshot(State
, line_index
-1, line_index
+1)})
200 y
, screen_bottom1
.pos
= Text
.draw(State
, line_index
, y
, startpos
)
202 elseif line
.mode
== 'drawing' then
203 y
= y
+Drawing_padding_top
204 Drawing
.draw(State
, line_index
, y
)
205 y
= y
+ Drawing
.pixels(line
.h
, State
.width
) + Drawing_padding_bottom
211 State
.screen_bottom1
= screen_bottom1
212 if State
.search_term
then
213 Text
.draw_search_bar(State
)
218 function edit
.update(State
, dt
)
219 Drawing
.update(State
, dt
)
220 if State
.next_save
and State
.next_save
< Current_time
then
222 State
.next_save
= nil
226 function schedule_save(State
)
227 if State
.next_save
== nil then
228 State
.next_save
= Current_time
+ 3 -- short enough that you're likely to still remember what you did
232 function edit
.quit(State
)
233 -- make sure to save before quitting
234 if State
.next_save
then
236 -- give some time for the OS to flush everything to disk
237 love
.timer
.sleep(0.1)
241 function edit
.mouse_press(State
, x
,y
, mouse_button
)
242 if State
.search_term
then return end
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_handler(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? click 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 if State
.lines
.current_drawing
then
308 Drawing
.mouse_release(State
, x
,y
, mouse_button
)
310 if Drawing
.before
then
311 record_undo_event(State
, {before
=Drawing
.before
, after
=snapshot(State
, State
.lines
.current_drawing_index
)})
315 --? print_and_log('edit.mouse_release: no current drawing')
316 for line_index
,line
in ipairs(State
.lines
) do
317 if line
.mode
== 'text' then
318 if Text
.in_line(State
, line_index
, x
,y
) then
319 --? print_and_log(('edit.mouse_release: in line %d'):format(line_index))
322 pos
=Text
.to_pos_on_line(State
, line_index
, x
, y
),
324 --? print_and_log(('edit.mouse_release: cursor now %d,%d'):format(State.cursor1.line, State.cursor1.pos))
325 if State
.mousepress_shift
then
326 if State
.old_selection1
.line
== nil then
327 State
.selection1
= State
.old_cursor1
329 State
.selection1
= State
.old_selection1
332 State
.old_cursor1
, State
.old_selection1
, State
.mousepress_shift
= nil
333 if eq(State
.cursor1
, State
.selection1
) then
334 State
.selection1
= {}
340 --? 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))
344 function edit
.mouse_wheel_move(State
, dx
,dy
)
346 State
.cursor1
= {line
=State
.screen_top1
.line
, pos
=State
.screen_top1
.pos
}
347 edit
.put_cursor_on_next_text_line(State
)
348 for i
=1,math
.floor(dy
) do
352 State
.cursor1
= {line
=State
.screen_bottom1
.line
, pos
=State
.screen_bottom1
.pos
}
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 for _
,line_cache
in ipairs(State
.line_cache
) do line_cache
.starty
= nil end -- just in case we scroll
375 Text
.text_input(State
, t
)
381 function edit
.keychord_press(State
, chord
, key
)
382 if State
.selection1
.line
and
383 not State
.lines
.current_drawing
and
384 -- printable character created using shift key => delete selection
385 -- (we're not creating any ctrl-shift- or alt-shift- combinations using regular/printable keys)
386 (not App
.shift_down() or utf8
.len(key
) == 1) and
387 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
388 Text
.delete_selection(State
, State
.left
, State
.right
)
390 if State
.search_term
then
391 for _
,line_cache
in ipairs(State
.line_cache
) do line_cache
.starty
= nil end -- just in case we scroll
392 if chord
== 'escape' then
393 State
.search_term
= nil
394 State
.cursor1
= State
.search_backup
.cursor
395 State
.screen_top1
= State
.search_backup
.screen_top
396 State
.search_backup
= nil
397 Text
.redraw_all(State
) -- if we're scrolling, reclaim all fragments to avoid memory leaks
398 elseif chord
== 'return' then
399 State
.search_term
= nil
400 State
.search_backup
= nil
401 elseif chord
== 'backspace' then
402 local len
= utf8
.len(State
.search_term
)
403 local byte_offset
= Text
.offset(State
.search_term
, len
)
404 State
.search_term
= string.sub(State
.search_term
, 1, byte_offset
-1)
405 elseif chord
== 'down' then
406 State
.cursor1
.pos
= State
.cursor1
.pos
+1
407 Text
.search_next(State
)
408 elseif chord
== 'up' then
409 Text
.search_previous(State
)
412 elseif chord
== 'C-f' then
413 State
.search_term
= ''
414 State
.search_backup
= {
415 cursor
={line
=State
.cursor1
.line
, pos
=State
.cursor1
.pos
},
416 screen_top
={line
=State
.screen_top1
.line
, pos
=State
.screen_top1
.pos
},
419 elseif chord
== 'C-=' then
420 edit
.update_font_settings(State
, State
.font_height
+2)
421 Text
.redraw_all(State
)
422 elseif chord
== 'C--' then
423 if State
.font_height
> 2 then
424 edit
.update_font_settings(State
, State
.font_height
-2)
425 Text
.redraw_all(State
)
427 elseif chord
== 'C-0' then
428 edit
.update_font_settings(State
, 20)
429 Text
.redraw_all(State
)
431 elseif chord
== 'C-z' then
432 for _
,line_cache
in ipairs(State
.line_cache
) do line_cache
.starty
= nil end -- just in case we scroll
433 local event
= undo_event(State
)
435 local src
= event
.before
436 State
.screen_top1
= deepcopy(src
.screen_top
)
437 State
.cursor1
= deepcopy(src
.cursor
)
438 State
.selection1
= deepcopy(src
.selection
)
439 patch(State
.lines
, event
.after
, event
.before
)
440 patch_placeholders(State
.line_cache
, event
.after
, event
.before
)
441 -- invalidate various cached bits of lines
442 State
.lines
.current_drawing
= nil
443 -- if we're scrolling, reclaim all fragments to avoid memory leaks
444 Text
.redraw_all(State
)
447 elseif chord
== 'C-y' then
448 for _
,line_cache
in ipairs(State
.line_cache
) do line_cache
.starty
= nil end -- just in case we scroll
449 local event
= redo_event(State
)
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
)
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
)
471 elseif chord
== 'C-x' then
472 for _
,line_cache
in ipairs(State
.line_cache
) do line_cache
.starty
= nil end -- just in case we scroll
473 local s
= Text
.cut_selection(State
, State
.left
, State
.right
)
478 elseif chord
== 'C-v' then
479 for _
,line_cache
in ipairs(State
.line_cache
) do line_cache
.starty
= nil end -- just in case we scroll
480 -- We don't have a good sense of when to scroll, so we'll be conservative
481 -- and sometimes scroll when we didn't quite need to.
482 local before_line
= State
.cursor1
.line
483 local before
= snapshot(State
, before_line
)
484 local clipboard_data
= App
.get_clipboard()
485 for _
,code
in utf8
.codes(clipboard_data
) do
486 local c
= utf8
.char(code
)
488 Text
.insert_return(State
)
490 Text
.insert_at_cursor(State
, c
)
493 if Text
.cursor_out_of_screen(State
) then
494 Text
.snap_cursor_to_bottom_of_screen(State
, State
.left
, State
.right
)
497 record_undo_event(State
, {before
=before
, after
=snapshot(State
, before_line
, State
.cursor1
.line
)})
498 -- dispatch to drawing or text
499 elseif App
.mouse_down(1) or chord
:sub(1,2) == 'C-' then
500 -- DON'T reset line_cache.starty here
501 local drawing_index
, drawing
= Drawing
.current_drawing(State
)
502 if drawing_index
then
503 local before
= snapshot(State
, drawing_index
)
504 Drawing
.keychord_press(State
, chord
)
505 record_undo_event(State
, {before
=before
, after
=snapshot(State
, drawing_index
)})
508 elseif chord
== 'escape' and not App
.mouse_down(1) then
509 for _
,line
in ipairs(State
.lines
) do
510 if line
.mode
== 'drawing' then
511 line
.show_help
= false
514 elseif State
.lines
.current_drawing
and State
.current_drawing_mode
== 'name' then
515 if chord
== 'return' then
516 State
.current_drawing_mode
= State
.previous_drawing_mode
517 State
.previous_drawing_mode
= nil
519 local before
= snapshot(State
, State
.lines
.current_drawing_index
)
520 local drawing
= State
.lines
.current_drawing
521 local p
= drawing
.points
[drawing
.pending
.target_point
]
522 if chord
== 'escape' then
524 record_undo_event(State
, {before
=before
, after
=snapshot(State
, State
.lines
.current_drawing_index
)})
525 elseif chord
== 'backspace' then
526 local len
= utf8
.len(p
.name
)
528 local byte_offset
= Text
.offset(p
.name
, len
-1)
529 if len
== 1 then byte_offset
= 0 end
530 p
.name
= string.sub(p
.name
, 1, byte_offset
)
531 record_undo_event(State
, {before
=before
, after
=snapshot(State
, State
.lines
.current_drawing_index
)})
537 for _
,line_cache
in ipairs(State
.line_cache
) do line_cache
.starty
= nil end -- just in case we scroll
538 Text
.keychord_press(State
, chord
)
542 function edit
.key_release(State
, key
, scancode
)
545 function edit
.update_font_settings(State
, font_height
)
546 State
.font_height
= font_height
547 love
.graphics
.setFont(love
.graphics
.newFont(State
.font_height
))
548 State
.line_height
= math
.floor(font_height
*1.3)
551 --== some methods for tests
553 -- Insulate tests from some key globals so I don't have to change the vast
554 -- majority of tests when they're modified for the real app.
555 Test_margin_left
= 25
556 Test_margin_right
= 0
558 function edit
.initialize_test_state()
559 -- if you change these values, tests will start failing
560 return edit
.initialize_state(
563 App
.screen
.width
- Test_margin_right
,
564 14, -- font height assuming default LÖVE font
568 -- all text_input events are also keypresses
569 -- TODO: handle chords of multiple keys
570 function edit
.run_after_text_input(State
, t
)
571 edit
.keychord_press(State
, t
)
572 edit
.text_input(State
, t
)
573 edit
.key_release(State
, t
)
574 App
.screen
.contents
= {}
575 edit
.update(State
, 0)
579 -- not all keys are text_input
580 function edit
.run_after_keychord(State
, chord
)
581 edit
.keychord_press(State
, chord
)
582 edit
.key_release(State
, chord
)
583 App
.screen
.contents
= {}
584 edit
.update(State
, 0)
588 function edit
.run_after_mouse_click(State
, x
,y
, mouse_button
)
589 App
.fake_mouse_press(x
,y
, mouse_button
)
590 edit
.mouse_press(State
, x
,y
, mouse_button
)
591 App
.fake_mouse_release(x
,y
, mouse_button
)
592 edit
.mouse_release(State
, x
,y
, mouse_button
)
593 App
.screen
.contents
= {}
594 edit
.update(State
, 0)
598 function edit
.run_after_mouse_press(State
, x
,y
, mouse_button
)
599 App
.fake_mouse_press(x
,y
, mouse_button
)
600 edit
.mouse_press(State
, x
,y
, mouse_button
)
601 App
.screen
.contents
= {}
602 edit
.update(State
, 0)
606 function edit
.run_after_mouse_release(State
, x
,y
, mouse_button
)
607 App
.fake_mouse_release(x
,y
, mouse_button
)
608 edit
.mouse_release(State
, x
,y
, mouse_button
)
609 App
.screen
.contents
= {}
610 edit
.update(State
, 0)