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:
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 -- screen_line_starting_pos: optional array of codepoint indices if it wraps over more than one screen line
59 -- Given wrapping, any potential location for the text cursor can be described in two ways:
60 -- * schema 1: As a combination of line index and position within a line (in utf8 codepoint units)
61 -- * schema 2: As a combination of line index, screen line index within the line, and a position within the screen line.
63 -- Most of the time we'll only persist positions in schema 1, translating to
64 -- schema 2 when that's convenient.
66 -- Make sure these coordinates are never aliased, so that changing one causes
67 -- action at a distance.
69 -- On lines that are drawings, pos will be nil.
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; must be on a text line
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
87 font_height
= font_height
,
88 line_height
= line_height
,
91 left
= math
.floor(left
), -- left margin for text; line numbers go to the left of this
92 right
= math
.floor(right
),
104 search_backup
= nil, -- stuff to restore when cancelling search
107 end -- edit.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
.invalid_cursor1(State
)
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 State
.cursor1
= {line
=1, pos
=1}
118 edit
.put_cursor_on_next_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 -- cursor loc in particular differs from other locs in one way:
130 -- pos might occur just after end of line
131 function edit
.invalid_cursor1(State
)
132 local cursor1
= State
.cursor1
133 if cursor1
.line
> #State
.lines
then return true end
134 local l
= State
.lines
[cursor1
.line
]
135 if l
.mode
~= 'text' then return false end -- pos is irrelevant to validity for a drawing line
136 return cursor1
.pos
> #State
.lines
[cursor1
.line
].data
+ 1
139 function edit
.cursor_on_text(State
)
140 return State
.cursor1
.line
<= #State
.lines
141 and State
.lines
[State
.cursor1
.line
].mode
== 'text'
144 function edit
.put_cursor_on_next_text_line(State
)
146 if State
.cursor1
.line
>= #State
.lines
then
149 if State
.lines
[State
.cursor1
.line
].mode
== 'text' then
152 State
.cursor1
.line
= State
.cursor1
.line
+1
153 State
.cursor1
.pos
= 1
157 function edit
.draw(State
, hide_cursor
, show_line_numbers
)
158 State
.button_handlers
= {}
159 love
.graphics
.setFont(State
.font
)
160 App
.color(Text_color
)
161 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
))
162 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
))
167 for line_index
= State
.screen_top1
.line
,#State
.lines
do
168 local line
= State
.lines
[line_index
]
169 --? print('draw:', y, line_index, line)
170 if y
+ State
.line_height
> App
.screen
.height
then break end
171 if line
.mode
== 'text' then
172 --? print('text.draw', y, line_index)
174 if line_index
== State
.screen_top1
.line
then
175 startpos
= State
.screen_top1
.pos
177 if line
.data
== '' then
178 -- button to insert new drawing
179 local buttonx
= State
.left
-Margin_left
+4
180 if show_line_numbers
then
181 buttonx
= 4 -- HACK: position draw buttons at a fixed x on screen
183 button(State
, 'draw', {x
=buttonx
, y
=y
+4, w
=12,h
=12, bg
={r
=1,g
=1,b
=0},
184 icon
= icon
.insert_drawing
,
185 onpress1
= function()
186 Drawing
.before
= snapshot(State
, line_index
-1, line_index
)
187 table.insert(State
.lines
, line_index
, {mode
='drawing', y
=y
, h
=256/2, points
={}, shapes
={}, pending
={}})
188 table.insert(State
.line_cache
, line_index
, {})
189 if State
.cursor1
.line
>= line_index
then
190 State
.cursor1
.line
= State
.cursor1
.line
+1
192 record_undo_event(State
, {before
=Drawing
.before
, after
=snapshot(State
, line_index
-1, line_index
+1)})
198 y
= Text
.draw(State
, line_index
, y
, startpos
, hide_cursor
, show_line_numbers
)
200 elseif line
.mode
== 'drawing' then
201 y
= y
+Drawing_padding_top
202 Drawing
.draw(State
, line_index
, y
)
203 y
= y
+ Drawing
.pixels(line
.h
, State
.width
) + Drawing_padding_bottom
205 assert(false, ('unknown line mode %s'):format(line
.mode
))
208 if State
.search_term
then
209 Text
.draw_search_bar(State
)
213 function edit
.update(State
, dt
)
214 Drawing
.update(State
, dt
)
215 if State
.next_save
and State
.next_save
< Current_time
then
217 State
.next_save
= nil
221 function schedule_save(State
)
222 if State
.next_save
== nil then
223 State
.next_save
= Current_time
+ 3 -- short enough that you're likely to still remember what you did
227 function edit
.quit(State
)
228 -- make sure to save before quitting
229 if State
.next_save
then
231 -- give some time for the OS to flush everything to disk
232 love
.timer
.sleep(0.1)
236 function edit
.mouse_press(State
, x
,y
, mouse_button
)
237 love
.keyboard
.setTextInput(true) -- bring up keyboard on touch screen
238 if State
.search_term
then return end
239 State
.mouse_down
= mouse_button
240 --? print_and_log(('edit.mouse_press: cursor at %d,%d'):format(State.cursor1.line, State.cursor1.pos))
241 if mouse_press_consumed_by_any_button(State
, x
,y
, mouse_button
) then
242 -- press on a button and it returned 'true' to short-circuit
246 if y
< State
.top
then
247 State
.old_cursor1
= State
.cursor1
248 State
.old_selection1
= State
.selection1
249 State
.mousepress_shift
= App
.shift_down()
251 line
=State
.screen_top1
.line
,
252 pos
=State
.screen_top1
.pos
,
257 for line_index
,line
in ipairs(State
.lines
) do
258 if line
.mode
== 'text' then
259 if Text
.in_line(State
, line_index
, x
,y
) then
260 -- delicate dance between cursor, selection and old cursor/selection
262 -- regular press+release: sets cursor, clears selection
263 -- shift press+release:
264 -- sets selection to old cursor if not set otherwise leaves it untouched
266 -- press and hold to start a selection: sets selection on press, cursor on release
267 -- press and hold, then press shift: ignore shift
268 -- i.e. mouse_release should never look at shift state
269 --? print_and_log(('edit.mouse_press: in line %d'):format(line_index))
270 State
.old_cursor1
= State
.cursor1
271 State
.old_selection1
= State
.selection1
272 State
.mousepress_shift
= App
.shift_down()
275 pos
=Text
.to_pos_on_line(State
, line_index
, x
, y
),
279 elseif line
.mode
== 'drawing' then
280 if Drawing
.in_drawing(State
, line_index
, x
, y
, State
.left
,State
.right
) then
281 State
.lines
.current_drawing_index
= line_index
282 State
.lines
.current_drawing
= line
283 Drawing
.before
= snapshot(State
, line_index
)
284 Drawing
.mouse_press(State
, line_index
, x
,y
, mouse_button
)
290 -- still here? mouse press is below all screen lines
291 State
.old_cursor1
= State
.cursor1
292 State
.old_selection1
= State
.selection1
293 State
.mousepress_shift
= App
.shift_down()
294 State
.selection1
= Text
.final_text_loc_on_screen(State
)
297 function edit
.mouse_release(State
, x
,y
, mouse_button
)
298 if State
.search_term
then return end
299 --? print_and_log(('edit.mouse_release: cursor at %d,%d'):format(State.cursor1.line, State.cursor1.pos))
300 State
.mouse_down
= nil
301 if State
.lines
.current_drawing
then
302 Drawing
.mouse_release(State
, x
,y
, mouse_button
)
303 if Drawing
.before
then
304 record_undo_event(State
, {before
=Drawing
.before
, after
=snapshot(State
, State
.lines
.current_drawing_index
)})
309 --? print_and_log('edit.mouse_release: no current drawing')
310 if y
< State
.top
then
311 State
.cursor1
= deepcopy(State
.screen_top1
)
312 edit
.clean_up_mouse_press(State
)
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 edit
.clean_up_mouse_press(State
)
331 -- still here? mouse release is below all screen lines
332 State
.cursor1
= Text
.final_text_loc_on_screen(State
)
333 edit
.clean_up_mouse_press(State
)
334 --? 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))
338 function edit
.clean_up_mouse_press(State
)
339 if State
.mousepress_shift
then
340 if State
.old_selection1
.line
== nil then
341 State
.selection1
= State
.old_cursor1
343 State
.selection1
= State
.old_selection1
346 State
.old_cursor1
, State
.old_selection1
, State
.mousepress_shift
= nil
347 if eq(State
.cursor1
, State
.selection1
) then
348 State
.selection1
= {}
352 function edit
.mouse_wheel_move(State
, dx
,dy
)
354 State
.cursor1
= deepcopy(State
.screen_top1
)
355 edit
.put_cursor_on_next_text_line(State
)
356 for i
=1,math
.floor(dy
) do
360 State
.cursor1
= Text
.screen_bottom1(State
)
361 edit
.put_cursor_on_next_text_line(State
)
362 for i
=1,math
.floor(-dy
) do
368 function edit
.text_input(State
, t
)
369 --? print('text input', t)
370 if State
.search_term
then
371 State
.search_term
= State
.search_term
..t
372 Text
.search_next(State
)
373 elseif State
.lines
.current_drawing
and State
.current_drawing_mode
== 'name' then
374 local before
= snapshot(State
, State
.lines
.current_drawing_index
)
375 local drawing
= State
.lines
.current_drawing
376 local p
= drawing
.points
[drawing
.pending
.target_point
]
378 record_undo_event(State
, {before
=before
, after
=snapshot(State
, State
.lines
.current_drawing_index
)})
380 local drawing_index
, drawing
= Drawing
.current_drawing(State
)
381 if drawing_index
== nil then
382 Text
.text_input(State
, t
)
388 function edit
.keychord_press(State
, chord
, key
)
389 if State
.selection1
.line
and
390 not State
.lines
.current_drawing
and
391 -- printable character created using shift key => delete selection
392 -- (we're not creating any ctrl-shift- or alt-shift- combinations using regular/printable keys)
393 (not App
.shift_down() or utf8
.len(key
) == 1) and
394 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
395 Text
.delete_selection_and_record_undo_event(State
)
397 if State
.search_term
then
398 if chord
== 'escape' then
399 State
.search_term
= nil
400 State
.cursor1
= State
.search_backup
.cursor
401 State
.screen_top1
= State
.search_backup
.screen_top
402 State
.search_backup
= nil
403 Text
.redraw_all(State
) -- if we're scrolling, reclaim all line caches to avoid memory leaks
404 elseif chord
== 'return' then
405 State
.search_term
= nil
406 State
.search_backup
= nil
407 elseif chord
== 'backspace' then
408 local len
= utf8
.len(State
.search_term
)
409 local byte_offset
= Text
.offset(State
.search_term
, len
)
410 State
.search_term
= string.sub(State
.search_term
, 1, byte_offset
-1)
411 elseif chord
== 'down' then
412 State
.cursor1
.pos
= State
.cursor1
.pos
+1
413 Text
.search_next(State
)
414 elseif chord
== 'up' then
415 Text
.search_previous(State
)
418 elseif chord
== 'C-f' then
419 State
.search_term
= ''
420 State
.search_backup
= {
421 cursor
={line
=State
.cursor1
.line
, pos
=State
.cursor1
.pos
},
422 screen_top
={line
=State
.screen_top1
.line
, pos
=State
.screen_top1
.pos
},
425 elseif chord
== 'C-=' then
426 edit
.update_font_settings(State
, State
.font_height
+2)
427 Text
.redraw_all(State
)
428 elseif chord
== 'C--' then
429 if State
.font_height
> 2 then
430 edit
.update_font_settings(State
, State
.font_height
-2)
431 Text
.redraw_all(State
)
433 elseif chord
== 'C-0' then
434 edit
.update_font_settings(State
, 20)
435 Text
.redraw_all(State
)
437 elseif chord
== 'C-z' then
438 local event
= undo_event(State
)
440 local src
= event
.before
441 State
.screen_top1
= deepcopy(src
.screen_top
)
442 State
.cursor1
= deepcopy(src
.cursor
)
443 State
.selection1
= deepcopy(src
.selection
)
444 patch(State
.lines
, event
.after
, event
.before
)
445 -- invalidate various cached bits of lines
446 State
.lines
.current_drawing
= nil
447 Text
.redraw_all(State
) -- if we're scrolling, reclaim all line caches to avoid memory leaks
450 elseif chord
== 'C-y' then
451 local event
= redo_event(State
)
453 local src
= event
.after
454 State
.screen_top1
= deepcopy(src
.screen_top
)
455 State
.cursor1
= deepcopy(src
.cursor
)
456 State
.selection1
= deepcopy(src
.selection
)
457 patch(State
.lines
, event
.before
, event
.after
)
458 -- invalidate various cached bits of lines
459 State
.lines
.current_drawing
= nil
460 Text
.redraw_all(State
) -- if we're scrolling, reclaim all line caches to avoid memory leaks
464 elseif chord
== 'C-a' then
465 State
.selection1
= {line
=1, pos
=1}
466 State
.cursor1
= {line
=#State
.lines
, pos
=utf8
.len(State
.lines
[#State
.lines
].data
)+1}
467 elseif chord
== 'C-c' then
468 local s
= Text
.selection(State
)
472 elseif chord
== 'C-x' then
473 local s
= Text
.cut_selection_and_record_undo_event(State
)
478 elseif chord
== 'C-v' then
479 -- We don't have a good sense of when to scroll, so we'll be conservative
480 -- and sometimes scroll when we didn't quite need to.
481 local before_line
= State
.cursor1
.line
482 local before
= snapshot(State
, before_line
)
483 local clipboard_data
= App
.get_clipboard()
484 for _
,code
in utf8
.codes(clipboard_data
) do
485 local c
= utf8
.char(code
)
487 Text
.insert_return(State
)
489 Text
.insert_at_cursor(State
, c
)
492 if Text
.cursor_out_of_screen(State
) then
493 Text
.snap_cursor_to_bottom_of_screen(State
, State
.left
, State
.right
)
495 record_undo_event(State
, {before
=before
, after
=snapshot(State
, before_line
, State
.cursor1
.line
)})
497 -- dispatch to drawing or text
498 elseif App
.mouse_down(1) or chord
:sub(1,2) == 'C-' then
499 local drawing_index
, drawing
= Drawing
.current_drawing(State
)
500 if drawing_index
then
501 local before
= snapshot(State
, drawing_index
)
502 Drawing
.keychord_press(State
, chord
)
503 record_undo_event(State
, {before
=before
, after
=snapshot(State
, drawing_index
)})
506 elseif chord
== 'escape' and not App
.mouse_down(1) then
507 for _
,line
in ipairs(State
.lines
) do
508 if line
.mode
== 'drawing' then
509 line
.show_help
= false
512 elseif State
.lines
.current_drawing
and State
.current_drawing_mode
== 'name' then
513 if chord
== 'return' then
514 State
.current_drawing_mode
= State
.previous_drawing_mode
515 State
.previous_drawing_mode
= nil
517 local before
= snapshot(State
, State
.lines
.current_drawing_index
)
518 local drawing
= State
.lines
.current_drawing
519 local p
= drawing
.points
[drawing
.pending
.target_point
]
520 if chord
== 'escape' then
522 record_undo_event(State
, {before
=before
, after
=snapshot(State
, State
.lines
.current_drawing_index
)})
523 elseif chord
== 'backspace' then
524 local len
= utf8
.len(p
.name
)
526 local byte_offset
= Text
.offset(p
.name
, len
-1)
527 if len
== 1 then byte_offset
= 0 end
528 p
.name
= string.sub(p
.name
, 1, byte_offset
)
529 record_undo_event(State
, {before
=before
, after
=snapshot(State
, State
.lines
.current_drawing_index
)})
535 Text
.keychord_press(State
, chord
)
539 function edit
.key_release(State
, key
, scancode
)
542 function edit
.update_font_settings(State
, font_height
)
543 State
.font_height
= font_height
544 State
.font
= love
.graphics
.newFont(State
.font_height
)
545 State
.line_height
= math
.floor(font_height
*1.3)
548 --== some methods for tests
550 -- Insulate tests from some key globals so I don't have to change the vast
551 -- majority of tests when they're modified for the real app.
552 Test_margin_left
= 25
553 Test_margin_right
= 0
555 function edit
.initialize_test_state()
556 -- if you change these values, tests will start failing
557 return edit
.initialize_state(
560 App
.screen
.width
- Test_margin_right
,
561 love
.graphics
.getFont(),
566 -- all text_input events are also keypresses
567 -- TODO: handle chords of multiple keys
568 function edit
.run_after_text_input(State
, t
)
569 edit
.keychord_press(State
, t
)
570 edit
.text_input(State
, t
)
571 edit
.key_release(State
, t
)
572 App
.screen
.contents
= {}
573 edit
.update(State
, 0)
577 -- not all keys are text_input
578 function edit
.run_after_keychord(State
, chord
, key
)
579 edit
.keychord_press(State
, chord
, key
)
580 edit
.key_release(State
, key
)
581 App
.screen
.contents
= {}
582 edit
.update(State
, 0)
586 function edit
.run_after_mouse_click(State
, x
,y
, mouse_button
)
587 App
.fake_mouse_press(x
,y
, mouse_button
)
588 edit
.mouse_press(State
, x
,y
, mouse_button
)
590 App
.fake_mouse_release(x
,y
, mouse_button
)
591 edit
.mouse_release(State
, x
,y
, mouse_button
)
592 App
.screen
.contents
= {}
593 edit
.update(State
, 0)
597 function edit
.run_after_mouse_press(State
, x
,y
, mouse_button
)
598 App
.fake_mouse_press(x
,y
, mouse_button
)
599 edit
.mouse_press(State
, x
,y
, mouse_button
)
600 App
.screen
.contents
= {}
601 edit
.update(State
, 0)
605 function edit
.run_after_mouse_release(State
, x
,y
, mouse_button
)
606 App
.fake_mouse_release(x
,y
, mouse_button
)
607 edit
.mouse_release(State
, x
,y
, mouse_button
)
608 App
.screen
.contents
= {}
609 edit
.update(State
, 0)