ensure tapping on editor brings up soft keyboard
[view.love.git] / edit.lua
blobb0962746cd98ff7025d6bb41f8b341a495843c40
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}
13 Margin_top = 15
14 Margin_left = 25
15 Margin_right = 25
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
23 edit = {}
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
27 local result = {
28 -- a line is either text or a drawing
29 -- a text is a table with:
30 -- mode = 'text',
31 -- string data,
32 -- a drawing is a table with:
33 -- mode = 'drawing'
34 -- a (y) coord in pixels (updated while painting screen),
35 -- a (h)eight,
36 -- an array of points, and
37 -- an array of shapes
38 -- a shape is a table containing:
39 -- a mode
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
58 line_cache = {},
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
73 selection1 = {},
74 -- some extra state to compute selection between mouse press and release
75 old_cursor1 = nil,
76 old_selection1 = nil,
77 mousepress_shift = nil,
79 -- cursor coordinates in pixels
80 cursor_x = 0,
81 cursor_y = 0,
83 current_drawing_mode = 'line', -- one of the available shape modes
84 previous_drawing_mode = nil, -- extra state for some ephemeral modes like moving/deleting/naming points
86 font = font,
87 font_height = font_height,
88 line_height = line_height,
90 top = top,
91 left = math.floor(left),
92 right = math.floor(right),
93 width = right-left,
95 filename = love.filesystem.getSourceBaseDirectory()..'/lines.txt', -- '/' should work even on Windows
96 next_save = nil,
98 -- undo
99 history = {},
100 next_history = 1,
102 -- search
103 search_term = nil,
104 search_backup = nil, -- stuff to restore when cancelling search
106 return result
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)
145 while true do
146 if State.cursor1.line >= #State.lines then
147 break
149 if State.lines[State.cursor1.line].mode == 'text' then
150 break
152 State.cursor1.line = State.cursor1.line+1
153 State.cursor1.pos = 1
157 -- return y drawn until
158 function edit.draw(State)
159 State.button_handlers = {}
160 love.graphics.setFont(State.font)
161 App.color(Text_color)
162 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))
163 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 State.cursor_x = nil
165 State.cursor_y = nil
166 local y = State.top
167 local screen_bottom1 = {line=nil, pos=nil}
168 --? print('== draw')
169 for line_index = State.screen_top1.line,#State.lines do
170 local line = State.lines[line_index]
171 --? print('draw:', y, line_index, line)
172 if y + State.line_height > App.screen.height then break end
173 screen_bottom1.line = line_index
174 if line.mode == 'text' then
175 --? print('text.draw', y, line_index)
176 local startpos = 1
177 if line_index == State.screen_top1.line then
178 startpos = State.screen_top1.pos
180 if line.data == '' then
181 -- button to insert new drawing
182 button(State, 'draw', {x=State.left-Margin_left+4, y=y+4, w=12,h=12, bg={r=1,g=1,b=0},
183 icon = icon.insert_drawing,
184 onpress1 = function()
185 Drawing.before = snapshot(State, line_index-1, line_index)
186 table.insert(State.lines, line_index, {mode='drawing', y=y, h=256/2, points={}, shapes={}, pending={}})
187 table.insert(State.line_cache, line_index, {})
188 if State.cursor1.line >= line_index then
189 State.cursor1.line = State.cursor1.line+1
191 schedule_save(State)
192 record_undo_event(State, {before=Drawing.before, after=snapshot(State, line_index-1, line_index+1)})
193 end,
196 y, screen_bottom1.pos = Text.draw(State, line_index, y, startpos)
197 --? print('=> y', y)
198 elseif line.mode == 'drawing' then
199 y = y+Drawing_padding_top
200 Drawing.draw(State, line_index, y)
201 y = y + Drawing.pixels(line.h, State.width) + Drawing_padding_bottom
202 else
203 assert(false, ('unknown line mode %s'):format(line.mode))
206 State.screen_bottom1 = screen_bottom1
207 if State.search_term then
208 Text.draw_search_bar(State)
210 return y
213 function edit.update(State, dt)
214 Drawing.update(State, dt)
215 if State.next_save and State.next_save < Current_time then
216 save_to_disk(State)
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
230 save_to_disk(State)
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
243 return
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()
250 State.selection1 = {
251 line=State.screen_top1.line,
252 pos=State.screen_top1.pos,
254 return
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
261 -- scenarios:
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
265 -- sets cursor
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()
273 State.selection1 = {
274 line=line_index,
275 pos=Text.to_pos_on_line(State, line_index, x, y),
277 return
279 elseif line.mode == 'drawing' then
280 local line_cache = State.line_cache[line_index]
281 if Drawing.in_drawing(line, line_cache, x, y, State.left,State.right) then
282 State.lines.current_drawing_index = line_index
283 State.lines.current_drawing = line
284 Drawing.before = snapshot(State, line_index)
285 Drawing.mouse_press(State, line_index, x,y, mouse_button)
286 return
291 -- still here? mouse press is below all screen lines
292 State.old_cursor1 = State.cursor1
293 State.old_selection1 = State.selection1
294 State.mousepress_shift = App.shift_down()
295 State.selection1 = {
296 line=State.screen_bottom1.line,
297 pos=Text.pos_at_end_of_screen_line(State, State.screen_bottom1),
301 function edit.mouse_release(State, x,y, mouse_button)
302 if State.search_term then return end
303 --? print_and_log(('edit.mouse_release: cursor at %d,%d'):format(State.cursor1.line, State.cursor1.pos))
304 State.mouse_down = nil
305 if State.lines.current_drawing then
306 Drawing.mouse_release(State, x,y, mouse_button)
307 schedule_save(State)
308 if Drawing.before then
309 record_undo_event(State, {before=Drawing.before, after=snapshot(State, State.lines.current_drawing_index)})
310 Drawing.before = nil
312 else
313 --? print_and_log('edit.mouse_release: no current drawing')
314 if y < State.top then
315 State.cursor1 = {line=State.screen_top1.line, pos=State.screen_top1.pos}
316 edit.clean_up_mouse_press(State)
317 return
320 for line_index,line in ipairs(State.lines) do
321 if line.mode == 'text' then
322 if Text.in_line(State, line_index, x,y) then
323 --? print_and_log(('edit.mouse_release: in line %d'):format(line_index))
324 State.cursor1 = {
325 line=line_index,
326 pos=Text.to_pos_on_line(State, line_index, x, y),
328 --? print_and_log(('edit.mouse_release: cursor now %d,%d'):format(State.cursor1.line, State.cursor1.pos))
329 edit.clean_up_mouse_press(State)
330 return
335 -- still here? mouse release is below all screen lines
336 State.cursor1.line, State.cursor1.pos = State.screen_bottom1.line, Text.pos_at_end_of_screen_line(State, State.screen_bottom1)
337 edit.clean_up_mouse_press(State)
338 --? 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))
342 function edit.clean_up_mouse_press(State)
343 if State.mousepress_shift then
344 if State.old_selection1.line == nil then
345 State.selection1 = State.old_cursor1
346 else
347 State.selection1 = State.old_selection1
350 State.old_cursor1, State.old_selection1, State.mousepress_shift = nil
351 if eq(State.cursor1, State.selection1) then
352 State.selection1 = {}
356 function edit.mouse_wheel_move(State, dx,dy)
357 if dy > 0 then
358 State.cursor1 = {line=State.screen_top1.line, pos=State.screen_top1.pos}
359 edit.put_cursor_on_next_text_line(State)
360 for i=1,math.floor(dy) do
361 Text.up(State)
363 elseif dy < 0 then
364 State.cursor1 = {line=State.screen_bottom1.line, pos=State.screen_bottom1.pos}
365 edit.put_cursor_on_next_text_line(State)
366 for i=1,math.floor(-dy) do
367 Text.down(State)
372 function edit.text_input(State, t)
373 --? print('text input', t)
374 if State.search_term then
375 State.search_term = State.search_term..t
376 Text.search_next(State)
377 elseif State.lines.current_drawing and State.current_drawing_mode == 'name' then
378 local before = snapshot(State, State.lines.current_drawing_index)
379 local drawing = State.lines.current_drawing
380 local p = drawing.points[drawing.pending.target_point]
381 p.name = p.name..t
382 record_undo_event(State, {before=before, after=snapshot(State, State.lines.current_drawing_index)})
383 else
384 local drawing_index, drawing = Drawing.current_drawing(State)
385 if drawing_index == nil then
386 Text.text_input(State, t)
389 schedule_save(State)
392 function edit.keychord_press(State, chord, key)
393 if State.selection1.line and
394 not State.lines.current_drawing and
395 -- printable character created using shift key => delete selection
396 -- (we're not creating any ctrl-shift- or alt-shift- combinations using regular/printable keys)
397 (not App.shift_down() or utf8.len(key) == 1) and
398 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
399 Text.delete_selection(State, State.left, State.right)
401 if State.search_term then
402 for _,line_cache in ipairs(State.line_cache) do line_cache.starty = nil end -- just in case we scroll
403 if chord == 'escape' then
404 State.search_term = nil
405 State.cursor1 = State.search_backup.cursor
406 State.screen_top1 = State.search_backup.screen_top
407 State.search_backup = nil
408 Text.redraw_all(State) -- if we're scrolling, reclaim all fragments to avoid memory leaks
409 elseif chord == 'return' then
410 State.search_term = nil
411 State.search_backup = nil
412 elseif chord == 'backspace' then
413 local len = utf8.len(State.search_term)
414 local byte_offset = Text.offset(State.search_term, len)
415 State.search_term = string.sub(State.search_term, 1, byte_offset-1)
416 elseif chord == 'down' then
417 State.cursor1.pos = State.cursor1.pos+1
418 Text.search_next(State)
419 elseif chord == 'up' then
420 Text.search_previous(State)
422 return
423 elseif chord == 'C-f' then
424 State.search_term = ''
425 State.search_backup = {
426 cursor={line=State.cursor1.line, pos=State.cursor1.pos},
427 screen_top={line=State.screen_top1.line, pos=State.screen_top1.pos},
429 -- zoom
430 elseif chord == 'C-=' then
431 edit.update_font_settings(State, State.font_height+2)
432 Text.redraw_all(State)
433 elseif chord == 'C--' then
434 if State.font_height > 2 then
435 edit.update_font_settings(State, State.font_height-2)
436 Text.redraw_all(State)
438 elseif chord == 'C-0' then
439 edit.update_font_settings(State, 20)
440 Text.redraw_all(State)
441 -- undo
442 elseif chord == 'C-z' then
443 local event = undo_event(State)
444 if event then
445 local src = event.before
446 State.screen_top1 = deepcopy(src.screen_top)
447 State.cursor1 = deepcopy(src.cursor)
448 State.selection1 = deepcopy(src.selection)
449 patch(State.lines, event.after, event.before)
450 patch_placeholders(State.line_cache, event.after, event.before)
451 -- invalidate various cached bits of lines
452 State.lines.current_drawing = nil
453 -- if we're scrolling, reclaim all fragments to avoid memory leaks
454 Text.redraw_all(State)
455 schedule_save(State)
457 elseif chord == 'C-y' then
458 local event = redo_event(State)
459 if event then
460 local src = event.after
461 State.screen_top1 = deepcopy(src.screen_top)
462 State.cursor1 = deepcopy(src.cursor)
463 State.selection1 = deepcopy(src.selection)
464 patch(State.lines, event.before, event.after)
465 -- invalidate various cached bits of lines
466 State.lines.current_drawing = nil
467 -- if we're scrolling, reclaim all fragments to avoid memory leaks
468 Text.redraw_all(State)
469 schedule_save(State)
471 -- clipboard
472 elseif chord == 'C-a' then
473 State.selection1 = {line=1, pos=1}
474 State.cursor1 = {line=#State.lines, pos=utf8.len(State.lines[#State.lines].data)+1}
475 elseif chord == 'C-c' then
476 local s = Text.selection(State)
477 if s then
478 App.set_clipboard(s)
480 elseif chord == 'C-x' then
481 local s = Text.cut_selection(State, State.left, State.right)
482 if s then
483 App.set_clipboard(s)
485 schedule_save(State)
486 elseif chord == 'C-v' then
487 -- We don't have a good sense of when to scroll, so we'll be conservative
488 -- and sometimes scroll when we didn't quite need to.
489 local before_line = State.cursor1.line
490 local before = snapshot(State, before_line)
491 local clipboard_data = App.get_clipboard()
492 for _,code in utf8.codes(clipboard_data) do
493 local c = utf8.char(code)
494 if c == '\n' then
495 Text.insert_return(State)
496 else
497 Text.insert_at_cursor(State, c)
500 if Text.cursor_out_of_screen(State) then
501 Text.snap_cursor_to_bottom_of_screen(State, State.left, State.right)
503 schedule_save(State)
504 record_undo_event(State, {before=before, after=snapshot(State, before_line, State.cursor1.line)})
505 -- dispatch to drawing or text
506 elseif App.mouse_down(1) or chord:sub(1,2) == 'C-' then
507 -- DON'T reset line_cache.starty here
508 local drawing_index, drawing = Drawing.current_drawing(State)
509 if drawing_index then
510 local before = snapshot(State, drawing_index)
511 Drawing.keychord_press(State, chord)
512 record_undo_event(State, {before=before, after=snapshot(State, drawing_index)})
513 schedule_save(State)
515 elseif chord == 'escape' and not App.mouse_down(1) then
516 for _,line in ipairs(State.lines) do
517 if line.mode == 'drawing' then
518 line.show_help = false
521 elseif State.lines.current_drawing and State.current_drawing_mode == 'name' then
522 if chord == 'return' then
523 State.current_drawing_mode = State.previous_drawing_mode
524 State.previous_drawing_mode = nil
525 else
526 local before = snapshot(State, State.lines.current_drawing_index)
527 local drawing = State.lines.current_drawing
528 local p = drawing.points[drawing.pending.target_point]
529 if chord == 'escape' then
530 p.name = nil
531 record_undo_event(State, {before=before, after=snapshot(State, State.lines.current_drawing_index)})
532 elseif chord == 'backspace' then
533 local len = utf8.len(p.name)
534 if len > 0 then
535 local byte_offset = Text.offset(p.name, len-1)
536 if len == 1 then byte_offset = 0 end
537 p.name = string.sub(p.name, 1, byte_offset)
538 record_undo_event(State, {before=before, after=snapshot(State, State.lines.current_drawing_index)})
542 schedule_save(State)
543 else
544 for _,line_cache in ipairs(State.line_cache) do line_cache.starty = nil end -- just in case we scroll
545 Text.keychord_press(State, chord)
549 function edit.key_release(State, key, scancode)
552 function edit.update_font_settings(State, font_height)
553 State.font_height = font_height
554 State.font = love.graphics.newFont(State.font_height)
555 State.line_height = math.floor(font_height*1.3)
558 --== some methods for tests
560 -- Insulate tests from some key globals so I don't have to change the vast
561 -- majority of tests when they're modified for the real app.
562 Test_margin_left = 25
563 Test_margin_right = 0
565 function edit.initialize_test_state()
566 -- if you change these values, tests will start failing
567 return edit.initialize_state(
568 15, -- top margin
569 Test_margin_left,
570 App.screen.width - Test_margin_right,
571 love.graphics.getFont(),
573 15) -- line height
576 -- all text_input events are also keypresses
577 -- TODO: handle chords of multiple keys
578 function edit.run_after_text_input(State, t)
579 edit.keychord_press(State, t)
580 edit.text_input(State, t)
581 edit.key_release(State, t)
582 App.screen.contents = {}
583 edit.update(State, 0)
584 edit.draw(State)
587 -- not all keys are text_input
588 function edit.run_after_keychord(State, chord, key)
589 edit.keychord_press(State, chord, key)
590 edit.key_release(State, key)
591 App.screen.contents = {}
592 edit.update(State, 0)
593 edit.draw(State)
596 function edit.run_after_mouse_click(State, x,y, mouse_button)
597 App.fake_mouse_press(x,y, mouse_button)
598 edit.mouse_press(State, x,y, mouse_button)
599 App.fake_mouse_release(x,y, mouse_button)
600 edit.mouse_release(State, x,y, mouse_button)
601 App.screen.contents = {}
602 edit.update(State, 0)
603 edit.draw(State)
606 function edit.run_after_mouse_press(State, x,y, mouse_button)
607 App.fake_mouse_press(x,y, mouse_button)
608 edit.mouse_press(State, x,y, mouse_button)
609 App.screen.contents = {}
610 edit.update(State, 0)
611 edit.draw(State)
614 function edit.run_after_mouse_release(State, x,y, mouse_button)
615 App.fake_mouse_release(x,y, mouse_button)
616 edit.mouse_release(State, x,y, mouse_button)
617 App.screen.contents = {}
618 edit.update(State, 0)
619 edit.draw(State)