consistent names in a few more places
[lines.love.git] / edit.lua
blob4d36780ea8b020620e29814266f3463c95f1e64d
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_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 rendered love.graphics.Text, 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,
78 -- when selecting text, avoid recomputing some state on every single frame
79 recent_mouse = {},
81 -- cursor coordinates in pixels
82 cursor_x = 0,
83 cursor_y = 0,
85 current_drawing_mode = 'line',
86 previous_drawing_mode = nil, -- extra state for some ephemeral modes like moving/deleting/naming points
88 font_height = font_height,
89 line_height = line_height,
90 em = App.newText(love.graphics.getFont(), 'm'), -- widest possible character width
92 top = top,
93 left = math.floor(left),
94 right = math.floor(right),
95 width = right-left,
97 filename = love.filesystem.getUserDirectory()..'/lines.txt', -- '/' should work even on Windows
98 next_save = nil,
100 -- undo
101 history = {},
102 next_history = 1,
104 -- search
105 search_term = nil,
106 search_text = nil,
107 search_backup = nil, -- stuff to restore when cancelling search
109 return result
110 end -- App.initialize_state
112 function edit.fixup_cursor(State)
113 for i,line in ipairs(State.lines) do
114 if line.mode == 'text' then
115 State.cursor1.line = i
116 break
121 function edit.draw(State)
122 State.button_handlers = {}
123 App.color(Text_color)
124 assert(#State.lines == #State.line_cache)
125 if not Text.le1(State.screen_top1, State.cursor1) then
126 print(State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos)
127 assert(false)
129 State.cursor_x = nil
130 State.cursor_y = nil
131 local y = State.top
132 --? print('== draw')
133 for line_index = State.screen_top1.line,#State.lines do
134 local line = State.lines[line_index]
135 --? print('draw:', y, line_index, line)
136 if y + State.line_height > App.screen.height then break end
137 State.screen_bottom1 = {line=line_index, pos=nil}
138 if line.mode == 'text' then
139 --? print('text.draw', y, line_index)
140 local startpos = 1
141 if line_index == State.screen_top1.line then
142 startpos = State.screen_top1.pos
144 if line.data == '' then
145 -- button to insert new drawing
146 button(State, 'draw', {x=4,y=y+4, w=12,h=12, color={1,1,0},
147 icon = icon.insert_drawing,
148 onpress1 = function()
149 Drawing.before = snapshot(State, line_index-1, line_index)
150 table.insert(State.lines, line_index, {mode='drawing', y=y, h=256/2, points={}, shapes={}, pending={}})
151 table.insert(State.line_cache, line_index, {})
152 if State.cursor1.line >= line_index then
153 State.cursor1.line = State.cursor1.line+1
155 schedule_save(State)
156 record_undo_event(State, {before=Drawing.before, after=snapshot(State, line_index-1, line_index+1)})
157 end,
160 y, State.screen_bottom1.pos = Text.draw(State, line_index, y, startpos)
161 y = y + State.line_height
162 --? print('=> y', y)
163 elseif line.mode == 'drawing' then
164 y = y+Drawing_padding_top
165 Drawing.draw(State, line_index, y)
166 y = y + Drawing.pixels(line.h, State.width) + Drawing_padding_bottom
167 else
168 print(line.mode)
169 assert(false)
172 if State.search_term then
173 Text.draw_search_bar(State)
177 function edit.update(State, dt)
178 Drawing.update(State, dt)
179 if State.next_save and State.next_save < Current_time then
180 save_to_disk(State)
181 State.next_save = nil
185 function schedule_save(State)
186 if State.next_save == nil then
187 State.next_save = Current_time + 3 -- short enough that you're likely to still remember what you did
191 function edit.quit(State)
192 -- make sure to save before quitting
193 if State.next_save then
194 save_to_disk(State)
198 function edit.mouse_press(State, x,y, mouse_button)
199 if State.search_term then return end
200 --? print('press', State.selection1.line, State.selection1.pos)
201 if mouse_press_consumed_by_any_button_handler(State, x,y, mouse_button) then
202 -- press on a button and it returned 'true' to short-circuit
203 return
206 for line_index,line in ipairs(State.lines) do
207 if line.mode == 'text' then
208 if Text.in_line(State, line_index, x,y) then
209 -- delicate dance between cursor, selection and old cursor/selection
210 -- scenarios:
211 -- regular press+release: sets cursor, clears selection
212 -- shift press+release:
213 -- sets selection to old cursor if not set otherwise leaves it untouched
214 -- sets cursor
215 -- press and hold to start a selection: sets selection on press, cursor on release
216 -- press and hold, then press shift: ignore shift
217 -- i.e. mouse_release should never look at shift state
218 State.old_cursor1 = State.cursor1
219 State.old_selection1 = State.selection1
220 State.mousepress_shift = App.shift_down()
221 State.selection1 = {
222 line=line_index,
223 pos=Text.to_pos_on_line(State, line_index, x, y),
225 --? print('selection', State.selection1.line, State.selection1.pos)
226 break
228 elseif line.mode == 'drawing' then
229 local line_cache = State.line_cache[line_index]
230 if Drawing.in_drawing(line, line_cache, x, y, State.left,State.right) then
231 State.lines.current_drawing_index = line_index
232 State.lines.current_drawing = line
233 Drawing.before = snapshot(State, line_index)
234 Drawing.mouse_press(State, line_index, x,y, mouse_button)
235 break
241 function edit.mouse_release(State, x,y, mouse_button)
242 if State.search_term then return end
243 --? print('release')
244 if State.lines.current_drawing then
245 Drawing.mouse_release(State, x,y, mouse_button)
246 schedule_save(State)
247 if Drawing.before then
248 record_undo_event(State, {before=Drawing.before, after=snapshot(State, State.lines.current_drawing_index)})
249 Drawing.before = nil
251 else
252 for line_index,line in ipairs(State.lines) do
253 if line.mode == 'text' then
254 if Text.in_line(State, line_index, x,y) then
255 --? print('reset selection')
256 State.cursor1 = {
257 line=line_index,
258 pos=Text.to_pos_on_line(State, line_index, x, y),
260 --? print('cursor', State.cursor1.line, State.cursor1.pos)
261 if State.mousepress_shift then
262 if State.old_selection1.line == nil then
263 State.selection1 = State.old_cursor1
264 else
265 State.selection1 = State.old_selection1
268 State.old_cursor1, State.old_selection1, State.mousepress_shift = nil
269 if eq(State.cursor1, State.selection1) then
270 State.selection1 = {}
272 break
276 --? print('selection:', State.selection1.line, State.selection1.pos)
280 function edit.text_input(State, t)
281 if State.search_term then
282 State.search_term = State.search_term..t
283 State.search_text = nil
284 Text.search_next(State)
285 elseif State.current_drawing_mode == 'name' then
286 local before = snapshot(State, State.lines.current_drawing_index)
287 local drawing = State.lines.current_drawing
288 local p = drawing.points[drawing.pending.target_point]
289 p.name = p.name..t
290 record_undo_event(State, {before=before, after=snapshot(State, State.lines.current_drawing_index)})
291 else
292 local drawing_index, drawing = Drawing.current_drawing(State)
293 if drawing_index == nil then
294 for _,line_cache in ipairs(State.line_cache) do line_cache.starty = nil end -- just in case we scroll
295 Text.text_input(State, t)
298 schedule_save(State)
301 function edit.keychord_press(State, chord, key)
302 if State.selection1.line and
303 not State.lines.current_drawing and
304 -- printable character created using shift key => delete selection
305 -- (we're not creating any ctrl-shift- or alt-shift- combinations using regular/printable keys)
306 (not App.shift_down() or utf8.len(key) == 1) and
307 chord ~= 'C-a' and chord ~= 'C-c' and chord ~= 'C-x' and chord ~= 'backspace' and backspace ~= 'delete' and not App.is_cursor_movement(chord) then
308 Text.delete_selection(State, State.left, State.right)
310 if State.search_term then
311 if chord == 'escape' then
312 State.search_term = nil
313 State.search_text = nil
314 State.cursor1 = State.search_backup.cursor
315 State.screen_top1 = State.search_backup.screen_top
316 State.search_backup = nil
317 Text.redraw_all(State) -- if we're scrolling, reclaim all fragments to avoid memory leaks
318 elseif chord == 'return' then
319 State.search_term = nil
320 State.search_text = nil
321 State.search_backup = nil
322 elseif chord == 'backspace' then
323 local len = utf8.len(State.search_term)
324 local byte_offset = Text.offset(State.search_term, len)
325 State.search_term = string.sub(State.search_term, 1, byte_offset-1)
326 State.search_text = nil
327 elseif chord == 'down' then
328 State.cursor1.pos = State.cursor1.pos+1
329 Text.search_next(State)
330 elseif chord == 'up' then
331 Text.search_previous(State)
333 return
334 elseif chord == 'C-f' then
335 State.search_term = ''
336 State.search_backup = {
337 cursor={line=State.cursor1.line, pos=State.cursor1.pos},
338 screen_top={line=State.screen_top1.line, pos=State.screen_top1.pos},
340 assert(State.search_text == nil)
341 -- zoom
342 elseif chord == 'C-=' then
343 edit.update_font_settings(State, State.font_height+2)
344 Text.redraw_all(State)
345 elseif chord == 'C--' then
346 edit.update_font_settings(State, State.font_height-2)
347 Text.redraw_all(State)
348 elseif chord == 'C-0' then
349 edit.update_font_settings(State, 20)
350 Text.redraw_all(State)
351 -- undo
352 elseif chord == 'C-z' then
353 for _,line_cache in ipairs(State.line_cache) do line_cache.starty = nil end -- just in case we scroll
354 local event = undo_event(State)
355 if event then
356 local src = event.before
357 State.screen_top1 = deepcopy(src.screen_top)
358 State.cursor1 = deepcopy(src.cursor)
359 State.selection1 = deepcopy(src.selection)
360 patch(State.lines, event.after, event.before)
361 patch_placeholders(State.line_cache, event.after, event.before)
362 -- invalidate various cached bits of lines
363 State.lines.current_drawing = nil
364 -- if we're scrolling, reclaim all fragments to avoid memory leaks
365 Text.redraw_all(State)
366 schedule_save(State)
368 elseif chord == 'C-y' then
369 for _,line_cache in ipairs(State.line_cache) do line_cache.starty = nil end -- just in case we scroll
370 local event = redo_event(State)
371 if event then
372 local src = event.after
373 State.screen_top1 = deepcopy(src.screen_top)
374 State.cursor1 = deepcopy(src.cursor)
375 State.selection1 = deepcopy(src.selection)
376 patch(State.lines, event.before, event.after)
377 -- invalidate various cached bits of lines
378 State.lines.current_drawing = nil
379 -- if we're scrolling, reclaim all fragments to avoid memory leaks
380 Text.redraw_all(State)
381 schedule_save(State)
383 -- clipboard
384 elseif chord == 'C-a' then
385 State.selection1 = {line=1, pos=1}
386 State.cursor1 = {line=#State.lines, pos=utf8.len(State.lines[#State.lines].data)+1}
387 elseif chord == 'C-c' then
388 local s = Text.selection(State)
389 if s then
390 App.setClipboardText(s)
392 elseif chord == 'C-x' then
393 for _,line_cache in ipairs(State.line_cache) do line_cache.starty = nil end -- just in case we scroll
394 local s = Text.cut_selection(State, State.left, State.right)
395 if s then
396 App.setClipboardText(s)
398 schedule_save(State)
399 elseif chord == 'C-v' then
400 for _,line_cache in ipairs(State.line_cache) do line_cache.starty = nil end -- just in case we scroll
401 -- We don't have a good sense of when to scroll, so we'll be conservative
402 -- and sometimes scroll when we didn't quite need to.
403 local before_line = State.cursor1.line
404 local before = snapshot(State, before_line)
405 local clipboard_data = App.getClipboardText()
406 for _,code in utf8.codes(clipboard_data) do
407 local c = utf8.char(code)
408 if c == '\n' then
409 Text.insert_return(State)
410 else
411 Text.insert_at_cursor(State, c)
414 if Text.cursor_out_of_screen(State) then
415 Text.snap_cursor_to_bottom_of_screen(State, State.left, State.right)
417 schedule_save(State)
418 record_undo_event(State, {before=before, after=snapshot(State, before_line, State.cursor1.line)})
419 -- dispatch to drawing or text
420 elseif App.mouse_down(1) or chord:sub(1,2) == 'C-' then
421 -- DON'T reset line_cache.starty here
422 local drawing_index, drawing = Drawing.current_drawing(State)
423 if drawing_index then
424 local before = snapshot(State, drawing_index)
425 Drawing.keychord_press(State, chord)
426 record_undo_event(State, {before=before, after=snapshot(State, drawing_index)})
427 schedule_save(State)
429 elseif chord == 'escape' and not App.mouse_down(1) then
430 for _,line in ipairs(State.lines) do
431 if line.mode == 'drawing' then
432 line.show_help = false
435 elseif State.current_drawing_mode == 'name' then
436 if chord == 'return' then
437 State.current_drawing_mode = State.previous_drawing_mode
438 State.previous_drawing_mode = nil
439 else
440 local before = snapshot(State, State.lines.current_drawing_index)
441 local drawing = State.lines.current_drawing
442 local p = drawing.points[drawing.pending.target_point]
443 if chord == 'escape' then
444 p.name = nil
445 record_undo_event(State, {before=before, after=snapshot(State, State.lines.current_drawing_index)})
446 elseif chord == 'backspace' then
447 local len = utf8.len(p.name)
448 local byte_offset = Text.offset(p.name, len-1)
449 if len == 1 then byte_offset = 0 end
450 p.name = string.sub(p.name, 1, byte_offset)
451 record_undo_event(State, {before=before, after=snapshot(State, State.lines.current_drawing_index)})
454 schedule_save(State)
455 else
456 for _,line_cache in ipairs(State.line_cache) do line_cache.starty = nil end -- just in case we scroll
457 Text.keychord_press(State, chord)
461 function edit.key_release(State, key, scancode)
464 function edit.update_font_settings(State, font_height)
465 State.font_height = font_height
466 love.graphics.setFont(love.graphics.newFont(State.font_height))
467 State.line_height = math.floor(font_height*1.3)
468 State.em = App.newText(love.graphics.getFont(), 'm')
469 Text_cache = {}
472 --== some methods for tests
474 -- Insulate tests from some key globals so I don't have to change the vast
475 -- majority of tests when they're modified for the real app.
476 Test_margin_left = 25
477 Test_margin_right = 0
479 function edit.initialize_test_state()
480 -- if you change these values, tests will start failing
481 return edit.initialize_state(
482 15, -- top margin
483 Test_margin_left,
484 App.screen.width - Test_margin_right,
485 14, -- font height assuming default LÖVE font
486 15) -- line height
489 -- all text_input events are also keypresses
490 -- TODO: handle chords of multiple keys
491 function edit.run_after_text_input(State, t)
492 edit.keychord_press(State, t)
493 edit.text_input(State, t)
494 edit.key_release(State, t)
495 App.screen.contents = {}
496 edit.update(State, 0)
497 edit.draw(State)
500 -- not all keys are text_input
501 function edit.run_after_keychord(State, chord)
502 edit.keychord_press(State, chord)
503 edit.key_release(State, chord)
504 App.screen.contents = {}
505 edit.update(State, 0)
506 edit.draw(State)
509 function edit.run_after_mouse_click(State, x,y, mouse_button)
510 App.fake_mouse_press(x,y, mouse_button)
511 edit.mouse_press(State, x,y, mouse_button)
512 App.fake_mouse_release(x,y, mouse_button)
513 edit.mouse_release(State, x,y, mouse_button)
514 App.screen.contents = {}
515 edit.update(State, 0)
516 edit.draw(State)
519 function edit.run_after_mouse_press(State, x,y, mouse_button)
520 App.fake_mouse_press(x,y, mouse_button)
521 edit.mouse_press(State, x,y, mouse_button)
522 App.screen.contents = {}
523 edit.update(State, 0)
524 edit.draw(State)
527 function edit.run_after_mouse_release(State, x,y, mouse_button)
528 App.fake_mouse_release(x,y, mouse_button)
529 edit.mouse_release(State, x,y, mouse_button)
530 App.screen.contents = {}
531 edit.update(State, 0)
532 edit.draw(State)