bugfix in source editor: don't clear selection on M-arrow
[view.love.git] / source_edit.lua
blob53b50eb888465e10e84e6d04afa42090c8c303a1
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}
15 Margin_top = 15
16 Margin_left = 25
17 Margin_right = 25
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
25 edit = {}
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
29 local result = {
30 -- a line is either text or a drawing
31 -- a text is a table with:
32 -- mode = 'text',
33 -- string data,
34 -- a drawing is a table with:
35 -- mode = 'drawing'
36 -- a (y) coord in pixels (updated while painting screen),
37 -- a (h)eight,
38 -- an array of points, and
39 -- an array of shapes
40 -- a shape is a table containing:
41 -- a mode
42 -- an array points for mode 'freehand' (raw x,y coords; freehand drawings don't pollute the points array of a drawing)
43 -- an array vertices for mode 'polygon', 'rectangle', 'square'
44 -- p1, p2 for mode 'line'
45 -- center, radius for mode 'circle'
46 -- center, radius, start_angle, end_angle for mode 'arc'
47 -- Unless otherwise specified, coord fields are normalized; a drawing is always 256 units wide
48 -- The field names are carefully chosen so that switching modes in midstream
49 -- remembers previously entered points where that makes sense.
50 lines = {{mode='text', data=''}}, -- array of lines
52 -- Lines can be too long to fit on screen, in which case they _wrap_ into
53 -- multiple _screen lines_.
55 -- rendering wrapped text lines needs some additional short-lived data per line:
56 -- startpos, the index of data the line starts rendering from, can only be >1 for topmost line on screen
57 -- starty, the y coord in pixels the line starts rendering from
58 -- fragments: snippets of the line guaranteed to not straddle screen lines
59 -- screen_line_starting_pos: optional array of grapheme indices if it wraps over more than one screen line
60 line_cache = {},
62 -- Given wrapping, any potential location for the text cursor can be described in two ways:
63 -- * schema 1: As a combination of line index and position within a line (in utf8 codepoint units)
64 -- * schema 2: As a combination of line index, screen line index within the line, and a position within the screen line.
66 -- Most of the time we'll only persist positions in schema 1, translating to
67 -- schema 2 when that's convenient.
69 -- Make sure these coordinates are never aliased, so that changing one causes
70 -- action at a distance.
72 -- On lines that are drawings, pos will be nil.
73 screen_top1 = {line=1, pos=1}, -- position of start of screen line at top of screen
74 cursor1 = {line=1, pos=1}, -- position of cursor; must be on a text line
75 screen_bottom1 = {line=1, pos=1}, -- position of start of screen line at bottom of screen
77 selection1 = {},
78 -- some extra state to compute selection between mouse press and release
79 old_cursor1 = nil,
80 old_selection1 = nil,
81 mousepress_shift = nil,
83 -- cursor coordinates in pixels
84 cursor_x = 0,
85 cursor_y = 0,
87 current_drawing_mode = 'line',
88 previous_drawing_mode = nil, -- extra state for some ephemeral modes like moving/deleting/naming points
90 font = font,
91 font_height = font_height,
92 line_height = line_height,
94 top = top,
95 left = math.floor(left), -- left margin for text; line numbers go to the left of this
96 right = math.floor(right),
97 width = right-left,
99 filename = 'run.lua',
100 next_save = nil,
102 -- undo
103 history = {},
104 next_history = 1,
106 -- search
107 search_term = nil,
108 search_backup = nil, -- stuff to restore when cancelling search
110 return result
111 end -- edit.initialize_state
113 function edit.check_locs(State)
114 -- if State is inconsistent (i.e. file changed by some other program),
115 -- throw away all cursor state entirely
116 if edit.invalid1(State, State.screen_top1)
117 or edit.invalid_cursor1(State)
118 or not edit.cursor_on_text(State)
119 or not Text.le1(State.screen_top1, State.cursor1) then
120 State.screen_top1 = {line=1, pos=1}
121 State.cursor1 = {line=1, pos=1}
122 edit.put_cursor_on_next_text_line(State)
126 function edit.invalid1(State, loc1)
127 if loc1.line > #State.lines then return true end
128 local l = State.lines[loc1.line]
129 if l.mode ~= 'text' then return false end -- pos is irrelevant to validity for a drawing line
130 return loc1.pos > #State.lines[loc1.line].data
133 -- cursor loc in particular differs from other locs in one way:
134 -- pos might occur just after end of line
135 function edit.invalid_cursor1(State)
136 local cursor1 = State.cursor1
137 if cursor1.line > #State.lines then return true end
138 local l = State.lines[cursor1.line]
139 if l.mode ~= 'text' then return false end -- pos is irrelevant to validity for a drawing line
140 return cursor1.pos > #State.lines[cursor1.line].data + 1
143 function edit.cursor_on_text(State)
144 return State.cursor1.line <= #State.lines
145 and State.lines[State.cursor1.line].mode == 'text'
148 function edit.put_cursor_on_next_text_line(State)
149 while true do
150 if State.cursor1.line >= #State.lines then
151 break
153 if State.lines[State.cursor1.line].mode == 'text' then
154 break
156 State.cursor1.line = State.cursor1.line+1
157 State.cursor1.pos = 1
161 function edit.draw(State, hide_cursor, show_line_numbers)
162 State.button_handlers = {}
163 love.graphics.setFont(State.font)
164 App.color(Text_color)
165 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))
166 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 State.cursor_x = nil
168 State.cursor_y = nil
169 local y = State.top
170 local screen_bottom1 = {line=nil, pos=nil}
171 --? print('== draw')
172 for line_index = State.screen_top1.line,#State.lines do
173 local line = State.lines[line_index]
174 --? print('draw:', y, line_index, line)
175 if y + State.line_height > App.screen.height then break end
176 screen_bottom1.line = line_index
177 if line.mode == 'text' then
178 --? print('text.draw', y, line_index)
179 local startpos = 1
180 if line_index == State.screen_top1.line then
181 startpos = State.screen_top1.pos
183 if line.data == '' then
184 -- button to insert new drawing
185 local buttonx = State.left-Margin_left+4
186 if show_line_numbers then
187 buttonx = 4 -- HACK: position draw buttons at a fixed x on screen
189 button(State, 'draw', {x=buttonx, y=y+4, w=12,h=12, bg={r=1,g=1,b=0},
190 icon = icon.insert_drawing,
191 onpress1 = function()
192 Drawing.before = snapshot(State, line_index-1, line_index)
193 table.insert(State.lines, line_index, {mode='drawing', y=y, h=256/2, points={}, shapes={}, pending={}})
194 table.insert(State.line_cache, line_index, {})
195 for _,line_cache in ipairs(State.line_cache) do line_cache.starty = nil end
196 if State.cursor1.line >= line_index then
197 State.cursor1.line = State.cursor1.line+1
199 schedule_save(State)
200 record_undo_event(State, {before=Drawing.before, after=snapshot(State, line_index-1, line_index+1)})
201 end,
204 y, screen_bottom1.pos = Text.draw(State, line_index, y, startpos, hide_cursor, show_line_numbers)
205 --? print('=> y', y)
206 elseif line.mode == 'drawing' then
207 y = y+Drawing_padding_top
208 Drawing.draw(State, line_index, y)
209 y = y + Drawing.pixels(line.h, State.width) + Drawing_padding_bottom
210 else
211 assert(false, ('unknown line mode %s'):format(line.mode))
214 State.screen_bottom1 = screen_bottom1
215 if State.search_term then
216 Text.draw_search_bar(State)
220 function edit.update(State, dt)
221 Drawing.update(State, dt)
222 if State.next_save and State.next_save < Current_time then
223 save_to_disk(State)
224 State.next_save = nil
228 function schedule_save(State)
229 if State.next_save == nil then
230 State.next_save = Current_time + 3 -- short enough that you're likely to still remember what you did
234 function edit.quit(State)
235 -- make sure to save before quitting
236 if State.next_save then
237 save_to_disk(State)
238 -- give some time for the OS to flush everything to disk
239 love.timer.sleep(0.1)
243 function edit.mouse_press(State, x,y, mouse_button)
244 love.keyboard.setTextInput(true) -- bring up keyboard on touch screen
245 if State.search_term then return end
246 State.mouse_down = mouse_button
247 --? print_and_log(('edit.mouse_press: cursor at %d,%d'):format(State.cursor1.line, State.cursor1.pos))
248 if mouse_press_consumed_by_any_button(State, x,y, mouse_button) then
249 -- press on a button and it returned 'true' to short-circuit
250 return
253 if y < State.top then
254 State.old_cursor1 = State.cursor1
255 State.old_selection1 = State.selection1
256 State.mousepress_shift = App.shift_down()
257 State.selection1 = {
258 line=State.screen_top1.line,
259 pos=State.screen_top1.pos,
261 return
264 for line_index,line in ipairs(State.lines) do
265 if line.mode == 'text' then
266 if Text.in_line(State, line_index, x,y) then
267 -- delicate dance between cursor, selection and old cursor/selection
268 -- scenarios:
269 -- regular press+release: sets cursor, clears selection
270 -- shift press+release:
271 -- sets selection to old cursor if not set otherwise leaves it untouched
272 -- sets cursor
273 -- press and hold to start a selection: sets selection on press, cursor on release
274 -- press and hold, then press shift: ignore shift
275 -- i.e. mouse_release should never look at shift state
276 --? print_and_log(('edit.mouse_press: in line %d'):format(line_index))
277 State.old_cursor1 = State.cursor1
278 State.old_selection1 = State.selection1
279 State.mousepress_shift = App.shift_down()
280 State.selection1 = {
281 line=line_index,
282 pos=Text.to_pos_on_line(State, line_index, x, y),
284 return
286 elseif line.mode == 'drawing' then
287 local line_cache = State.line_cache[line_index]
288 if Drawing.in_drawing(line, line_cache, x, y, State.left,State.right) then
289 State.lines.current_drawing_index = line_index
290 State.lines.current_drawing = line
291 Drawing.before = snapshot(State, line_index)
292 Drawing.mouse_press(State, line_index, x,y, mouse_button)
293 return
298 -- still here? mouse press is below all screen lines
299 State.old_cursor1 = State.cursor1
300 State.old_selection1 = State.selection1
301 State.mousepress_shift = App.shift_down()
302 State.selection1 = Text.final_text_loc_on_screen(State)
305 function edit.mouse_release(State, x,y, mouse_button)
306 if State.search_term then return end
307 --? print_and_log(('edit.mouse_release: cursor at %d,%d'):format(State.cursor1.line, State.cursor1.pos))
308 State.mouse_down = nil
309 if State.lines.current_drawing then
310 Drawing.mouse_release(State, x,y, mouse_button)
311 schedule_save(State)
312 if Drawing.before then
313 record_undo_event(State, {before=Drawing.before, after=snapshot(State, State.lines.current_drawing_index)})
314 Drawing.before = nil
316 else
317 --? print_and_log('edit.mouse_release: no current drawing')
318 if y < State.top then
319 State.cursor1 = {line=State.screen_top1.line, pos=State.screen_top1.pos}
320 edit.clean_up_mouse_press(State)
321 return
324 for line_index,line in ipairs(State.lines) do
325 if line.mode == 'text' then
326 if Text.in_line(State, line_index, x,y) then
327 --? print_and_log(('edit.mouse_release: in line %d'):format(line_index))
328 State.cursor1 = {
329 line=line_index,
330 pos=Text.to_pos_on_line(State, line_index, x, y),
332 --? print_and_log(('edit.mouse_release: cursor now %d,%d'):format(State.cursor1.line, State.cursor1.pos))
333 edit.clean_up_mouse_press(State)
334 return
339 -- still here? mouse release is below all screen lines
340 State.cursor1 = Text.final_text_loc_on_screen(State)
341 edit.clean_up_mouse_press(State)
342 --? 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))
346 function edit.clean_up_mouse_press(State)
347 if State.mousepress_shift then
348 if State.old_selection1.line == nil then
349 State.selection1 = State.old_cursor1
350 else
351 State.selection1 = State.old_selection1
354 State.old_cursor1, State.old_selection1, State.mousepress_shift = nil
355 if eq(State.cursor1, State.selection1) then
356 State.selection1 = {}
360 function edit.mouse_wheel_move(State, dx,dy)
361 if dy > 0 then
362 State.cursor1 = {line=State.screen_top1.line, pos=State.screen_top1.pos}
363 edit.put_cursor_on_next_text_line(State)
364 for i=1,math.floor(dy) do
365 Text.up(State)
367 elseif dy < 0 then
368 State.cursor1 = {line=State.screen_bottom1.line, pos=State.screen_bottom1.pos}
369 edit.put_cursor_on_next_text_line(State)
370 for i=1,math.floor(-dy) do
371 Text.down(State)
376 function edit.text_input(State, t)
377 --? print('text input', t)
378 if State.search_term then
379 State.search_term = State.search_term..t
380 Text.search_next(State)
381 elseif State.lines.current_drawing and State.current_drawing_mode == 'name' then
382 local before = snapshot(State, State.lines.current_drawing_index)
383 local drawing = State.lines.current_drawing
384 local p = drawing.points[drawing.pending.target_point]
385 p.name = p.name..t
386 record_undo_event(State, {before=before, after=snapshot(State, State.lines.current_drawing_index)})
387 else
388 local drawing_index, drawing = Drawing.current_drawing(State)
389 if drawing_index == nil then
390 Text.text_input(State, t)
393 schedule_save(State)
396 function edit.keychord_press(State, chord, key)
397 if State.selection1.line and
398 not State.lines.current_drawing and
399 -- printable character created using shift key => delete selection
400 -- (we're not creating any ctrl-shift- or alt-shift- combinations using regular/printable keys)
401 (not App.shift_down() or utf8.len(key) == 1) and
402 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
403 Text.delete_selection(State, State.left, State.right)
405 if State.search_term then
406 for _,line_cache in ipairs(State.line_cache) do line_cache.starty = nil end -- just in case we scroll
407 if chord == 'escape' then
408 State.search_term = nil
409 State.cursor1 = State.search_backup.cursor
410 State.screen_top1 = State.search_backup.screen_top
411 State.search_backup = nil
412 Text.redraw_all(State) -- if we're scrolling, reclaim all fragments to avoid memory leaks
413 elseif chord == 'return' then
414 State.search_term = nil
415 State.search_backup = nil
416 elseif chord == 'backspace' then
417 local len = utf8.len(State.search_term)
418 local byte_offset = Text.offset(State.search_term, len)
419 State.search_term = string.sub(State.search_term, 1, byte_offset-1)
420 elseif chord == 'down' then
421 State.cursor1.pos = State.cursor1.pos+1
422 Text.search_next(State)
423 elseif chord == 'up' then
424 Text.search_previous(State)
426 return
427 elseif chord == 'C-f' then
428 State.search_term = ''
429 State.search_backup = {
430 cursor={line=State.cursor1.line, pos=State.cursor1.pos},
431 screen_top={line=State.screen_top1.line, pos=State.screen_top1.pos},
433 -- zoom
434 elseif chord == 'C-=' then
435 edit.update_font_settings(State, State.font_height+2)
436 Text.redraw_all(State)
437 elseif chord == 'C--' then
438 if State.font_height > 2 then
439 edit.update_font_settings(State, State.font_height-2)
440 Text.redraw_all(State)
442 elseif chord == 'C-0' then
443 edit.update_font_settings(State, 20)
444 Text.redraw_all(State)
445 -- undo
446 elseif chord == 'C-z' then
447 local event = undo_event(State)
448 if event then
449 local src = event.before
450 State.screen_top1 = deepcopy(src.screen_top)
451 State.cursor1 = deepcopy(src.cursor)
452 State.selection1 = deepcopy(src.selection)
453 patch(State.lines, event.after, event.before)
454 patch_placeholders(State.line_cache, event.after, event.before)
455 -- invalidate various cached bits of lines
456 State.lines.current_drawing = nil
457 -- if we're scrolling, reclaim all fragments to avoid memory leaks
458 Text.redraw_all(State)
459 schedule_save(State)
461 elseif chord == 'C-y' then
462 local event = redo_event(State)
463 if event then
464 local src = event.after
465 State.screen_top1 = deepcopy(src.screen_top)
466 State.cursor1 = deepcopy(src.cursor)
467 State.selection1 = deepcopy(src.selection)
468 patch(State.lines, event.before, event.after)
469 -- invalidate various cached bits of lines
470 State.lines.current_drawing = nil
471 -- if we're scrolling, reclaim all fragments to avoid memory leaks
472 Text.redraw_all(State)
473 schedule_save(State)
475 -- clipboard
476 elseif chord == 'C-a' then
477 State.selection1 = {line=1, pos=1}
478 State.cursor1 = {line=#State.lines, pos=utf8.len(State.lines[#State.lines].data)+1}
479 elseif chord == 'C-c' then
480 local s = Text.selection(State)
481 if s then
482 App.set_clipboard(s)
484 elseif chord == 'C-x' then
485 local s = Text.cut_selection(State, State.left, State.right)
486 if s then
487 App.set_clipboard(s)
489 schedule_save(State)
490 elseif chord == 'C-v' then
491 -- We don't have a good sense of when to scroll, so we'll be conservative
492 -- and sometimes scroll when we didn't quite need to.
493 local before_line = State.cursor1.line
494 local before = snapshot(State, before_line)
495 local clipboard_data = App.get_clipboard()
496 for _,code in utf8.codes(clipboard_data) do
497 local c = utf8.char(code)
498 if c == '\n' then
499 Text.insert_return(State)
500 else
501 Text.insert_at_cursor(State, c)
504 if Text.cursor_out_of_screen(State) then
505 Text.snap_cursor_to_bottom_of_screen(State, State.left, State.right)
507 schedule_save(State)
508 record_undo_event(State, {before=before, after=snapshot(State, before_line, State.cursor1.line)})
509 -- dispatch to drawing or text
510 elseif App.mouse_down(1) or chord:sub(1,2) == 'C-' then
511 -- DON'T reset line_cache.starty here
512 local drawing_index, drawing = Drawing.current_drawing(State)
513 if drawing_index then
514 local before = snapshot(State, drawing_index)
515 Drawing.keychord_press(State, chord)
516 record_undo_event(State, {before=before, after=snapshot(State, drawing_index)})
517 schedule_save(State)
519 elseif chord == 'escape' and not App.mouse_down(1) then
520 for _,line in ipairs(State.lines) do
521 if line.mode == 'drawing' then
522 line.show_help = false
525 elseif State.lines.current_drawing and State.current_drawing_mode == 'name' then
526 if chord == 'return' then
527 State.current_drawing_mode = State.previous_drawing_mode
528 State.previous_drawing_mode = nil
529 else
530 local before = snapshot(State, State.lines.current_drawing_index)
531 local drawing = State.lines.current_drawing
532 local p = drawing.points[drawing.pending.target_point]
533 if chord == 'escape' then
534 p.name = nil
535 record_undo_event(State, {before=before, after=snapshot(State, State.lines.current_drawing_index)})
536 elseif chord == 'backspace' then
537 local len = utf8.len(p.name)
538 if len > 0 then
539 local byte_offset = Text.offset(p.name, len-1)
540 if len == 1 then byte_offset = 0 end
541 p.name = string.sub(p.name, 1, byte_offset)
542 record_undo_event(State, {before=before, after=snapshot(State, State.lines.current_drawing_index)})
546 schedule_save(State)
547 else
548 for _,line_cache in ipairs(State.line_cache) do line_cache.starty = nil end -- just in case we scroll
549 Text.keychord_press(State, chord)
553 function edit.key_release(State, key, scancode)
556 function edit.update_font_settings(State, font_height)
557 State.font_height = font_height
558 State.font = love.graphics.newFont(State.font_height)
559 State.line_height = math.floor(font_height*1.3)
562 --== some methods for tests
564 -- Insulate tests from some key globals so I don't have to change the vast
565 -- majority of tests when they're modified for the real app.
566 Test_margin_left = 25
567 Test_margin_right = 0
569 function edit.initialize_test_state()
570 -- if you change these values, tests will start failing
571 return edit.initialize_state(
572 15, -- top margin
573 Test_margin_left,
574 App.screen.width - Test_margin_right,
575 love.graphics.getFont(),
577 15) -- line height
580 -- all text_input events are also keypresses
581 -- TODO: handle chords of multiple keys
582 function edit.run_after_text_input(State, t)
583 edit.keychord_press(State, t)
584 edit.text_input(State, t)
585 edit.key_release(State, t)
586 App.screen.contents = {}
587 edit.update(State, 0)
588 edit.draw(State)
591 -- not all keys are text_input
592 function edit.run_after_keychord(State, chord, key)
593 edit.keychord_press(State, chord, key)
594 edit.key_release(State, key)
595 App.screen.contents = {}
596 edit.update(State, 0)
597 edit.draw(State)
600 function edit.run_after_mouse_click(State, x,y, mouse_button)
601 App.fake_mouse_press(x,y, mouse_button)
602 edit.mouse_press(State, x,y, mouse_button)
603 edit.draw(State)
604 App.fake_mouse_release(x,y, mouse_button)
605 edit.mouse_release(State, x,y, mouse_button)
606 App.screen.contents = {}
607 edit.update(State, 0)
608 edit.draw(State)
611 function edit.run_after_mouse_press(State, x,y, mouse_button)
612 App.fake_mouse_press(x,y, mouse_button)
613 edit.mouse_press(State, x,y, mouse_button)
614 App.screen.contents = {}
615 edit.update(State, 0)
616 edit.draw(State)
619 function edit.run_after_mouse_release(State, x,y, mouse_button)
620 App.fake_mouse_release(x,y, mouse_button)
621 edit.mouse_release(State, x,y, mouse_button)
622 App.screen.contents = {}
623 edit.update(State, 0)
624 edit.draw(State)