edit.lua 23 KB

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