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 Icon_color
= {r
=0.7, g
=0.7, b
=0.7} -- color of current mode icon in drawings
11 Help_color
= {r
=0, g
=0.5, b
=0}
12 Help_background_color
= {r
=0, g
=0.5, b
=0, a
=0.1}
18 Drawing_padding_top
= 10
19 Drawing_padding_bottom
= 10
20 Drawing_padding_height
= Drawing_padding_top
+ Drawing_padding_bottom
22 Same_point_distance
= 4 -- pixel distance at which two points are considered the same
26 -- run in both tests and a real run
27 function edit
.initialize_state(top
, left
, right
, font_height
, line_height
) -- currently always draws to bottom of screen
29 -- a line is either text or a drawing
30 -- a text is a table with:
33 -- a drawing is a table with:
35 -- a (y) coord in pixels (updated while painting screen),
37 -- an array of points, and
39 -- a shape is a table containing:
41 -- an array points for mode 'freehand' (raw x,y coords; freehand drawings don't pollute the points array of a drawing)
42 -- an array vertices for mode 'polygon', 'rectangle', 'square'
43 -- p1, p2 for mode 'line'
44 -- center, radius for mode 'circle'
45 -- center, radius, start_angle, end_angle for mode 'arc'
46 -- Unless otherwise specified, coord fields are normalized; a drawing is always 256 units wide
47 -- The field names are carefully chosen so that switching modes in midstream
48 -- remembers previously entered points where that makes sense.
49 lines
= {{mode
='text', data
=''}}, -- array of lines
51 -- Lines can be too long to fit on screen, in which case they _wrap_ into
52 -- multiple _screen lines_.
54 -- rendering wrapped text lines needs some additional short-lived data per line:
55 -- startpos, the index of data the line starts rendering from, can only be >1 for topmost line on screen
56 -- starty, the y coord in pixels the line starts rendering from
57 -- fragments: snippets of the line guaranteed to not straddle screen lines
58 -- screen_line_starting_pos: optional array of grapheme indices if it wraps over more than one screen line
61 -- Given wrapping, any potential location for the text cursor can be described in two ways:
62 -- * schema 1: As a combination of line index and position within a line (in utf8 codepoint units)
63 -- * schema 2: As a combination of line index, screen line index within the line, and a position within the screen line.
65 -- Most of the time we'll only persist positions in schema 1, translating to
66 -- schema 2 when that's convenient.
68 -- Make sure these coordinates are never aliased, so that changing one causes
69 -- action at a distance.
70 screen_top1
= {line
=1, pos
=1}, -- position of start of screen line at top of screen
71 cursor1
= {line
=1, pos
=1}, -- position of cursor
72 screen_bottom1
= {line
=1, pos
=1}, -- position of start of screen line at bottom of screen
75 -- some extra state to compute selection between mouse press and release
78 mousepress_shift
= nil,
80 -- cursor coordinates in pixels
84 current_drawing_mode
= 'line',
85 previous_drawing_mode
= nil, -- extra state for some ephemeral modes like moving/deleting/naming points
87 font_height
= font_height
,
88 line_height
= line_height
,
91 left
= math
.floor(left
),
92 right
= math
.floor(right
),
95 filename
= love
.filesystem
.getSourceBaseDirectory()..'/lines.txt', -- '/' should work even on Windows
104 search_backup
= nil, -- stuff to restore when cancelling search
107 end -- App.initialize_state
109 function edit
.check_locs(State
)
110 -- if State is inconsistent (i.e. file changed by some other program),
111 -- throw away all cursor state entirely
112 if edit
.invalid1(State
, State
.screen_top1
)
113 or edit
.invalid1(State
, State
.cursor1
)
114 or not edit
.cursor_on_text(State
)
115 or not Text
.le1(State
.screen_top1
, State
.cursor1
) then
116 State
.screen_top1
= {line
=1, pos
=1}
117 edit
.put_cursor_on_first_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 function edit
.cursor_on_text(State
)
129 return State
.cursor1
.line
<= #State
.lines
130 and State
.lines
[State
.cursor1
.line
].mode
== 'text'
133 function edit
.put_cursor_on_first_text_line(State
)
134 for i
,line
in ipairs(State
.lines
) do
135 if line
.mode
== 'text' then
136 State
.cursor1
= {line
=i
, pos
=1}
142 function edit
.draw(State
, hide_cursor
)
143 State
.button_handlers
= {}
144 App
.color(Text_color
)
145 if #State
.lines
~= #State
.line_cache
then
146 print(('line_cache is out of date; %d when it should be %d'):format(#State
.line_cache
, #State
.lines
))
149 if not Text
.le1(State
.screen_top1
, State
.cursor1
) then
150 print(State
.screen_top1
.line
, State
.screen_top1
.pos
, State
.cursor1
.line
, State
.cursor1
.pos
)
156 local screen_bottom1
= {line
=nil, pos
=nil}
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 screen_bottom1
.line
= line_index
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
, screen_bottom1
.pos
= Text
.draw(State
, line_index
, y
, startpos
, hide_cursor
)
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 State
.screen_bottom1
= screen_bottom1
197 if State
.search_term
then
198 Text
.draw_search_bar(State
)
202 function edit
.update(State
, dt
)
203 Drawing
.update(State
, dt
)
204 if State
.next_save
and State
.next_save
< Current_time
then
206 State
.next_save
= nil
210 function schedule_save(State
)
211 if State
.next_save
== nil then
212 State
.next_save
= Current_time
+ 3 -- short enough that you're likely to still remember what you did
216 function edit
.quit(State
)
217 -- make sure to save before quitting
218 if State
.next_save
then
220 -- give some time for the OS to flush everything to disk
221 love
.timer
.sleep(0.1)
225 function edit
.mouse_press(State
, x
,y
, mouse_button
)
226 if State
.search_term
then return end
227 --? print_and_log(('edit.mouse_press: cursor at %d,%d'):format(State.cursor1.line, State.cursor1.pos))
228 if mouse_press_consumed_by_any_button_handler(State
, x
,y
, mouse_button
) then
229 -- press on a button and it returned 'true' to short-circuit
233 if y
< State
.top
then
234 State
.old_cursor1
= State
.cursor1
235 State
.old_selection1
= State
.selection1
236 State
.mousepress_shift
= App
.shift_down()
238 line
=State
.screen_top1
.line
,
239 pos
=State
.screen_top1
.pos
,
244 for line_index
,line
in ipairs(State
.lines
) do
245 if line
.mode
== 'text' then
246 if Text
.in_line(State
, line_index
, x
,y
) then
247 -- delicate dance between cursor, selection and old cursor/selection
249 -- regular press+release: sets cursor, clears selection
250 -- shift press+release:
251 -- sets selection to old cursor if not set otherwise leaves it untouched
253 -- press and hold to start a selection: sets selection on press, cursor on release
254 -- press and hold, then press shift: ignore shift
255 -- i.e. mouse_release should never look at shift state
256 --? print_and_log(('edit.mouse_press: in line %d'):format(line_index))
257 State
.old_cursor1
= State
.cursor1
258 State
.old_selection1
= State
.selection1
259 State
.mousepress_shift
= App
.shift_down()
262 pos
=Text
.to_pos_on_line(State
, line_index
, x
, y
),
266 elseif line
.mode
== 'drawing' then
267 local line_cache
= State
.line_cache
[line_index
]
268 if Drawing
.in_drawing(line
, line_cache
, x
, y
, State
.left
,State
.right
) then
269 State
.lines
.current_drawing_index
= line_index
270 State
.lines
.current_drawing
= line
271 Drawing
.before
= snapshot(State
, line_index
)
272 Drawing
.mouse_press(State
, line_index
, x
,y
, mouse_button
)
278 -- still here? click is below all screen lines
279 State
.old_cursor1
= State
.cursor1
280 State
.old_selection1
= State
.selection1
281 State
.mousepress_shift
= App
.shift_down()
283 line
=State
.screen_bottom1
.line
,
284 pos
=Text
.pos_at_end_of_screen_line(State
, State
.screen_bottom1
),
288 function edit
.mouse_release(State
, x
,y
, mouse_button
)
289 if State
.search_term
then return end
290 --? print_and_log(('edit.mouse_release: cursor at %d,%d'):format(State.cursor1.line, State.cursor1.pos))
291 if State
.lines
.current_drawing
then
292 Drawing
.mouse_release(State
, x
,y
, mouse_button
)
294 if Drawing
.before
then
295 record_undo_event(State
, {before
=Drawing
.before
, after
=snapshot(State
, State
.lines
.current_drawing_index
)})
299 --? print_and_log('edit.mouse_release: no current drawing')
300 for line_index
,line
in ipairs(State
.lines
) do
301 if line
.mode
== 'text' then
302 if Text
.in_line(State
, line_index
, x
,y
) then
303 --? print_and_log(('edit.mouse_release: in line %d'):format(line_index))
306 pos
=Text
.to_pos_on_line(State
, line_index
, x
, y
),
308 --? print_and_log(('edit.mouse_release: cursor now %d,%d'):format(State.cursor1.line, State.cursor1.pos))
309 if State
.mousepress_shift
then
310 if State
.old_selection1
.line
== nil then
311 State
.selection1
= State
.old_cursor1
313 State
.selection1
= State
.old_selection1
316 State
.old_cursor1
, State
.old_selection1
, State
.mousepress_shift
= nil
317 if eq(State
.cursor1
, State
.selection1
) then
318 State
.selection1
= {}
324 --? 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))
328 function edit
.mouse_wheel_move(State
, dx
,dy
)
330 State
.cursor1
= {line
=State
.screen_top1
.line
, pos
=State
.screen_top1
.pos
}
331 for i
=1,math
.floor(dy
) do
335 State
.cursor1
= {line
=State
.screen_bottom1
.line
, pos
=State
.screen_bottom1
.pos
}
336 for i
=1,math
.floor(-dy
) do
342 function edit
.text_input(State
, t
)
343 --? print('text input', t)
344 if State
.search_term
then
345 State
.search_term
= State
.search_term
..t
346 Text
.search_next(State
)
347 elseif State
.lines
.current_drawing
and State
.current_drawing_mode
== 'name' then
348 local before
= snapshot(State
, State
.lines
.current_drawing_index
)
349 local drawing
= State
.lines
.current_drawing
350 local p
= drawing
.points
[drawing
.pending
.target_point
]
352 record_undo_event(State
, {before
=before
, after
=snapshot(State
, State
.lines
.current_drawing_index
)})
354 local drawing_index
, drawing
= Drawing
.current_drawing(State
)
355 if drawing_index
== nil then
356 for _
,line_cache
in ipairs(State
.line_cache
) do line_cache
.starty
= nil end -- just in case we scroll
357 Text
.text_input(State
, t
)
363 function edit
.keychord_press(State
, chord
, key
)
364 if State
.selection1
.line
and
365 not State
.lines
.current_drawing
and
366 -- printable character created using shift key => delete selection
367 -- (we're not creating any ctrl-shift- or alt-shift- combinations using regular/printable keys)
368 (not App
.shift_down() or utf8
.len(key
) == 1) and
369 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
370 Text
.delete_selection(State
, State
.left
, State
.right
)
372 if State
.search_term
then
373 for _
,line_cache
in ipairs(State
.line_cache
) do line_cache
.starty
= nil end -- just in case we scroll
374 if chord
== 'escape' then
375 State
.search_term
= nil
376 State
.cursor1
= State
.search_backup
.cursor
377 State
.screen_top1
= State
.search_backup
.screen_top
378 State
.search_backup
= nil
379 Text
.redraw_all(State
) -- if we're scrolling, reclaim all fragments to avoid memory leaks
380 elseif chord
== 'return' then
381 State
.search_term
= nil
382 State
.search_backup
= nil
383 elseif chord
== 'backspace' then
384 local len
= utf8
.len(State
.search_term
)
385 local byte_offset
= Text
.offset(State
.search_term
, len
)
386 State
.search_term
= string.sub(State
.search_term
, 1, byte_offset
-1)
387 elseif chord
== 'down' then
388 State
.cursor1
.pos
= State
.cursor1
.pos
+1
389 Text
.search_next(State
)
390 elseif chord
== 'up' then
391 Text
.search_previous(State
)
394 elseif chord
== 'C-f' then
395 State
.search_term
= ''
396 State
.search_backup
= {
397 cursor
={line
=State
.cursor1
.line
, pos
=State
.cursor1
.pos
},
398 screen_top
={line
=State
.screen_top1
.line
, pos
=State
.screen_top1
.pos
},
401 elseif chord
== 'C-=' then
402 edit
.update_font_settings(State
, State
.font_height
+2)
403 Text
.redraw_all(State
)
404 elseif chord
== 'C--' then
405 if State
.font_height
> 2 then
406 edit
.update_font_settings(State
, State
.font_height
-2)
407 Text
.redraw_all(State
)
409 elseif chord
== 'C-0' then
410 edit
.update_font_settings(State
, 20)
411 Text
.redraw_all(State
)
413 elseif chord
== 'C-z' then
414 for _
,line_cache
in ipairs(State
.line_cache
) do line_cache
.starty
= nil end -- just in case we scroll
415 local event
= undo_event(State
)
417 local src
= event
.before
418 State
.screen_top1
= deepcopy(src
.screen_top
)
419 State
.cursor1
= deepcopy(src
.cursor
)
420 State
.selection1
= deepcopy(src
.selection
)
421 patch(State
.lines
, event
.after
, event
.before
)
422 patch_placeholders(State
.line_cache
, event
.after
, event
.before
)
423 -- invalidate various cached bits of lines
424 State
.lines
.current_drawing
= nil
425 -- if we're scrolling, reclaim all fragments to avoid memory leaks
426 Text
.redraw_all(State
)
429 elseif chord
== 'C-y' then
430 for _
,line_cache
in ipairs(State
.line_cache
) do line_cache
.starty
= nil end -- just in case we scroll
431 local event
= redo_event(State
)
433 local src
= event
.after
434 State
.screen_top1
= deepcopy(src
.screen_top
)
435 State
.cursor1
= deepcopy(src
.cursor
)
436 State
.selection1
= deepcopy(src
.selection
)
437 patch(State
.lines
, event
.before
, event
.after
)
438 -- invalidate various cached bits of lines
439 State
.lines
.current_drawing
= nil
440 -- if we're scrolling, reclaim all fragments to avoid memory leaks
441 Text
.redraw_all(State
)
445 elseif chord
== 'C-a' then
446 State
.selection1
= {line
=1, pos
=1}
447 State
.cursor1
= {line
=#State
.lines
, pos
=utf8
.len(State
.lines
[#State
.lines
].data
)+1}
448 elseif chord
== 'C-c' then
449 local s
= Text
.selection(State
)
451 App
.setClipboardText(s
)
453 elseif chord
== 'C-x' then
454 for _
,line_cache
in ipairs(State
.line_cache
) do line_cache
.starty
= nil end -- just in case we scroll
455 local s
= Text
.cut_selection(State
, State
.left
, State
.right
)
457 App
.setClipboardText(s
)
460 elseif chord
== 'C-v' then
461 for _
,line_cache
in ipairs(State
.line_cache
) do line_cache
.starty
= nil end -- just in case we scroll
462 -- We don't have a good sense of when to scroll, so we'll be conservative
463 -- and sometimes scroll when we didn't quite need to.
464 local before_line
= State
.cursor1
.line
465 local before
= snapshot(State
, before_line
)
466 local clipboard_data
= App
.getClipboardText()
467 for _
,code
in utf8
.codes(clipboard_data
) do
468 local c
= utf8
.char(code
)
470 Text
.insert_return(State
)
472 Text
.insert_at_cursor(State
, c
)
475 if Text
.cursor_out_of_screen(State
) then
476 Text
.snap_cursor_to_bottom_of_screen(State
, State
.left
, State
.right
)
479 record_undo_event(State
, {before
=before
, after
=snapshot(State
, before_line
, State
.cursor1
.line
)})
480 -- dispatch to drawing or text
481 elseif App
.mouse_down(1) or chord
:sub(1,2) == 'C-' then
482 -- DON'T reset line_cache.starty here
483 local drawing_index
, drawing
= Drawing
.current_drawing(State
)
484 if drawing_index
then
485 local before
= snapshot(State
, drawing_index
)
486 Drawing
.keychord_press(State
, chord
)
487 record_undo_event(State
, {before
=before
, after
=snapshot(State
, drawing_index
)})
490 elseif chord
== 'escape' and not App
.mouse_down(1) then
491 for _
,line
in ipairs(State
.lines
) do
492 if line
.mode
== 'drawing' then
493 line
.show_help
= false
496 elseif State
.lines
.current_drawing
and State
.current_drawing_mode
== 'name' then
497 if chord
== 'return' then
498 State
.current_drawing_mode
= State
.previous_drawing_mode
499 State
.previous_drawing_mode
= nil
501 local before
= snapshot(State
, State
.lines
.current_drawing_index
)
502 local drawing
= State
.lines
.current_drawing
503 local p
= drawing
.points
[drawing
.pending
.target_point
]
504 if chord
== 'escape' then
506 record_undo_event(State
, {before
=before
, after
=snapshot(State
, State
.lines
.current_drawing_index
)})
507 elseif chord
== 'backspace' then
508 local len
= utf8
.len(p
.name
)
510 local byte_offset
= Text
.offset(p
.name
, len
-1)
511 if len
== 1 then byte_offset
= 0 end
512 p
.name
= string.sub(p
.name
, 1, byte_offset
)
513 record_undo_event(State
, {before
=before
, after
=snapshot(State
, State
.lines
.current_drawing_index
)})
519 for _
,line_cache
in ipairs(State
.line_cache
) do line_cache
.starty
= nil end -- just in case we scroll
520 Text
.keychord_press(State
, chord
)
524 function edit
.key_release(State
, key
, scancode
)
527 function edit
.update_font_settings(State
, font_height
)
528 State
.font_height
= font_height
529 love
.graphics
.setFont(love
.graphics
.newFont(State
.font_height
))
530 State
.line_height
= math
.floor(font_height
*1.3)
533 --== some methods for tests
535 -- Insulate tests from some key globals so I don't have to change the vast
536 -- majority of tests when they're modified for the real app.
537 Test_margin_left
= 25
538 Test_margin_right
= 0
540 function edit
.initialize_test_state()
541 -- if you change these values, tests will start failing
542 return edit
.initialize_state(
545 App
.screen
.width
- Test_margin_right
,
546 14, -- font height assuming default LÖVE font
550 -- all text_input events are also keypresses
551 -- TODO: handle chords of multiple keys
552 function edit
.run_after_text_input(State
, t
)
553 edit
.keychord_press(State
, t
)
554 edit
.text_input(State
, t
)
555 edit
.key_release(State
, t
)
556 App
.screen
.contents
= {}
557 edit
.update(State
, 0)
561 -- not all keys are text_input
562 function edit
.run_after_keychord(State
, chord
)
563 edit
.keychord_press(State
, chord
)
564 edit
.key_release(State
, chord
)
565 App
.screen
.contents
= {}
566 edit
.update(State
, 0)
570 function edit
.run_after_mouse_click(State
, x
,y
, mouse_button
)
571 App
.fake_mouse_press(x
,y
, mouse_button
)
572 edit
.mouse_press(State
, x
,y
, mouse_button
)
573 App
.fake_mouse_release(x
,y
, mouse_button
)
574 edit
.mouse_release(State
, x
,y
, mouse_button
)
575 App
.screen
.contents
= {}
576 edit
.update(State
, 0)
580 function edit
.run_after_mouse_press(State
, x
,y
, mouse_button
)
581 App
.fake_mouse_press(x
,y
, mouse_button
)
582 edit
.mouse_press(State
, x
,y
, mouse_button
)
583 App
.screen
.contents
= {}
584 edit
.update(State
, 0)
588 function edit
.run_after_mouse_release(State
, x
,y
, mouse_button
)
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)