edit.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. Highlight_color = {r=0.7, g=0.7, b=0.9, a=0.4} -- selected text
  5. Margin_top = 15
  6. Margin_left = 25
  7. Margin_right = 25
  8. edit = {}
  9. -- run in both tests and a real run
  10. function edit.initialize_state(top, left, right, font, font_height, line_height) -- currently always draws to bottom of screen
  11. local result = {
  12. lines = {{data=''}}, -- array of strings
  13. -- Lines can be too long to fit on screen, in which case they _wrap_ into
  14. -- multiple _screen lines_.
  15. -- rendering wrapped text lines needs some additional short-lived data per line:
  16. -- startpos, the index of data the line starts rendering from, can only be >1 for topmost line on screen
  17. -- screen_line_starting_pos: optional array of codepoint indices if it wraps over more than one screen line
  18. line_cache = {},
  19. -- Given wrapping, any potential location for the text cursor can be described in two ways:
  20. -- * schema 1: As a combination of line index and position within a line (in utf8 codepoint units)
  21. -- * schema 2: As a combination of line index, screen line index within the line, and a position within the screen line.
  22. --
  23. -- Most of the time we'll only persist positions in schema 1, translating to
  24. -- schema 2 when that's convenient.
  25. --
  26. -- Make sure these coordinates are never aliased, so that changing one causes
  27. -- action at a distance.
  28. --
  29. -- On lines that are drawings, pos will be nil.
  30. screen_top1 = {line=1, pos=1}, -- position of start of screen line at top of screen
  31. cursor1 = {line=1, pos=1}, -- position of cursor; must be on a text line
  32. selection1 = {},
  33. -- some extra state to compute selection between mouse press and release
  34. old_cursor1 = nil,
  35. old_selection1 = nil,
  36. mousepress_shift = nil,
  37. -- cursor coordinates in pixels
  38. cursor_x = 0,
  39. cursor_y = 0,
  40. font = font,
  41. font_height = font_height,
  42. line_height = line_height,
  43. top = top,
  44. left = math.floor(left),
  45. right = math.floor(right),
  46. width = right-left,
  47. filename = love.filesystem.getSourceBaseDirectory()..'/lines.txt', -- '/' should work even on Windows
  48. -- search
  49. search_term = nil,
  50. search_backup = nil, -- stuff to restore when cancelling search
  51. }
  52. return result
  53. end -- edit.initialize_state
  54. function edit.check_locs(State)
  55. -- if State is inconsistent (i.e. file changed by some other program),
  56. -- throw away all cursor state entirely
  57. if edit.invalid1(State, State.screen_top1)
  58. or edit.invalid_cursor1(State)
  59. or not Text.le1(State.screen_top1, State.cursor1) then
  60. State.screen_top1 = {line=1, pos=1}
  61. State.cursor1 = {line=1, pos=1}
  62. end
  63. end
  64. function edit.invalid1(State, loc1)
  65. if loc1.line > #State.lines then return true end
  66. local l = State.lines[loc1.line]
  67. if l.mode ~= 'text' then return false end -- pos is irrelevant to validity for a drawing line
  68. return loc1.pos > #State.lines[loc1.line].data
  69. end
  70. -- cursor loc in particular differs from other locs in one way:
  71. -- pos might occur just after end of line
  72. function edit.invalid_cursor1(State)
  73. local cursor1 = State.cursor1
  74. if cursor1.line > #State.lines then return true end
  75. local l = State.lines[cursor1.line]
  76. if l.mode ~= 'text' then return false end -- pos is irrelevant to validity for a drawing line
  77. return cursor1.pos > #State.lines[cursor1.line].data + 1
  78. end
  79. function edit.draw(State)
  80. love.graphics.setFont(State.font)
  81. App.color(Text_color)
  82. 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))
  83. 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))
  84. State.cursor_x = nil
  85. State.cursor_y = nil
  86. local y = State.top
  87. --? print('== draw')
  88. for line_index = State.screen_top1.line,#State.lines do
  89. local line = State.lines[line_index]
  90. --? print('draw:', y, line_index, line)
  91. if y + State.line_height > App.screen.height then break end
  92. --? print('text.draw', y, line_index)
  93. local startpos = 1
  94. if line_index == State.screen_top1.line then
  95. startpos = State.screen_top1.pos
  96. end
  97. y = Text.draw(State, line_index, y, startpos)
  98. --? print('=> y', y)
  99. end
  100. if State.search_term then
  101. Text.draw_search_bar(State)
  102. end
  103. end
  104. function edit.update(State, dt)
  105. end
  106. function edit.quit(State)
  107. end
  108. function edit.mouse_press(State, x,y, mouse_button)
  109. if State.search_term then return end
  110. State.mouse_down = mouse_button
  111. --? print_and_log(('edit.mouse_press: cursor at %d,%d'):format(State.cursor1.line, State.cursor1.pos))
  112. if y < State.top then
  113. State.old_cursor1 = State.cursor1
  114. State.old_selection1 = State.selection1
  115. State.mousepress_shift = App.shift_down()
  116. State.selection1 = {
  117. line=State.screen_top1.line,
  118. pos=State.screen_top1.pos,
  119. }
  120. return
  121. end
  122. for line_index,line in ipairs(State.lines) do
  123. if Text.in_line(State, line_index, x,y) then
  124. -- delicate dance between cursor, selection and old cursor/selection
  125. -- scenarios:
  126. -- regular press+release: sets cursor, clears selection
  127. -- shift press+release:
  128. -- sets selection to old cursor if not set otherwise leaves it untouched
  129. -- sets cursor
  130. -- press and hold to start a selection: sets selection on press, cursor on release
  131. -- press and hold, then press shift: ignore shift
  132. -- i.e. mouse_release should never look at shift state
  133. --? print_and_log(('edit.mouse_press: in line %d'):format(line_index))
  134. State.old_cursor1 = State.cursor1
  135. State.old_selection1 = State.selection1
  136. State.mousepress_shift = App.shift_down()
  137. State.selection1 = {
  138. line=line_index,
  139. pos=Text.to_pos_on_line(State, line_index, x, y),
  140. }
  141. return
  142. end
  143. end
  144. -- still here? mouse press is below all screen lines
  145. State.old_cursor1 = State.cursor1
  146. State.old_selection1 = State.selection1
  147. State.mousepress_shift = App.shift_down()
  148. State.selection1 = Text.final_loc_on_screen(State)
  149. end
  150. function edit.mouse_release(State, x,y, mouse_button)
  151. if State.search_term then return end
  152. --? print_and_log(('edit.mouse_release(%d,%d): cursor at %d,%d'):format(x,y, State.cursor1.line, State.cursor1.pos))
  153. State.mouse_down = nil
  154. if y < State.top then
  155. State.cursor1 = deepcopy(State.screen_top1)
  156. edit.clean_up_mouse_press(State)
  157. return
  158. end
  159. for line_index,line in ipairs(State.lines) do
  160. if Text.in_line(State, line_index, x,y) then
  161. --? print_and_log(('edit.mouse_release: in line %d'):format(line_index))
  162. State.cursor1 = {
  163. line=line_index,
  164. pos=Text.to_pos_on_line(State, line_index, x, y),
  165. }
  166. --? print_and_log(('edit.mouse_release: cursor now %d,%d'):format(State.cursor1.line, State.cursor1.pos))
  167. edit.clean_up_mouse_press(State)
  168. return
  169. end
  170. end
  171. -- still here? mouse release is below all screen lines
  172. State.cursor1 = Text.final_loc_on_screen(State)
  173. edit.clean_up_mouse_press(State)
  174. --? 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))
  175. end
  176. function edit.clean_up_mouse_press(State)
  177. if State.mousepress_shift then
  178. if State.old_selection1.line == nil then
  179. State.selection1 = State.old_cursor1
  180. else
  181. State.selection1 = State.old_selection1
  182. end
  183. end
  184. State.old_cursor1, State.old_selection1, State.mousepress_shift = nil
  185. if eq(State.cursor1, State.selection1) then
  186. State.selection1 = {}
  187. end
  188. end
  189. function edit.mouse_wheel_move(State, dx,dy)
  190. if dy > 0 then
  191. State.cursor1 = deepcopy(State.screen_top1)
  192. for i=1,math.floor(dy) do
  193. Text.up(State)
  194. end
  195. elseif dy < 0 then
  196. State.cursor1 = Text.screen_bottom1(State)
  197. for i=1,math.floor(-dy) do
  198. Text.down(State)
  199. end
  200. end
  201. end
  202. function edit.text_input(State, t)
  203. --? print('text input', t)
  204. if State.search_term then
  205. State.search_term = State.search_term..t
  206. Text.search_next(State)
  207. end
  208. end
  209. function edit.keychord_press(State, chord, key)
  210. if State.selection1.line and
  211. -- printable character created using shift key => delete selection
  212. -- (we're not creating any ctrl-shift- or alt-shift- combinations using regular/printable keys)
  213. (not App.shift_down() or utf8.len(key) == 1) and
  214. 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
  215. Text.delete_selection_and_record_undo_event(State)
  216. end
  217. if State.search_term then
  218. if chord == 'escape' then
  219. State.search_term = nil
  220. State.cursor1 = State.search_backup.cursor
  221. State.screen_top1 = State.search_backup.screen_top
  222. State.search_backup = nil
  223. Text.redraw_all(State) -- if we're scrolling, reclaim all line caches to avoid memory leaks
  224. elseif chord == 'return' then
  225. State.search_term = nil
  226. State.search_backup = nil
  227. elseif chord == 'backspace' then
  228. local len = utf8.len(State.search_term)
  229. local byte_offset = Text.offset(State.search_term, len)
  230. State.search_term = string.sub(State.search_term, 1, byte_offset-1)
  231. State.cursor = deepcopy(State.search_backup.cursor)
  232. State.screen_top = deepcopy(State.search_backup.screen_top)
  233. Text.search_next(State)
  234. elseif chord == 'down' then
  235. if #State.search_term > 0 then
  236. Text.right(State)
  237. Text.search_next(State)
  238. end
  239. elseif chord == 'up' then
  240. Text.search_previous(State)
  241. end
  242. return
  243. elseif chord == 'C-f' then
  244. State.search_term = ''
  245. State.search_backup = {
  246. cursor={line=State.cursor1.line, pos=State.cursor1.pos},
  247. screen_top={line=State.screen_top1.line, pos=State.screen_top1.pos},
  248. }
  249. -- zoom
  250. elseif chord == 'C-=' then
  251. edit.update_font_settings(State, State.font_height+2)
  252. Text.redraw_all(State)
  253. elseif chord == 'C--' then
  254. if State.font_height > 2 then
  255. edit.update_font_settings(State, State.font_height-2)
  256. Text.redraw_all(State)
  257. end
  258. elseif chord == 'C-0' then
  259. edit.update_font_settings(State, 20)
  260. Text.redraw_all(State)
  261. -- clipboard
  262. elseif chord == 'C-a' then
  263. State.selection1 = {line=1, pos=1}
  264. State.cursor1 = {line=#State.lines, pos=utf8.len(State.lines[#State.lines].data)+1}
  265. elseif chord == 'C-c' then
  266. local s = Text.selection(State)
  267. if s then
  268. App.set_clipboard(s)
  269. end
  270. else
  271. Text.keychord_press(State, chord)
  272. end
  273. end
  274. function edit.key_release(State, key, scancode)
  275. end
  276. function edit.update_font_settings(State, font_height, font)
  277. State.font_height = font_height
  278. State.font = font or love.graphics.newFont(State.font_height)
  279. State.line_height = math.floor(font_height*1.3)
  280. end
  281. --== some methods for tests
  282. -- Insulate tests from some key globals so I don't have to change the vast
  283. -- majority of tests when they're modified for the real app.
  284. Test_margin_left = 25
  285. Test_margin_right = 0
  286. function edit.initialize_test_state()
  287. -- if you change these values, tests will start failing
  288. return edit.initialize_state(
  289. 15, -- top margin
  290. Test_margin_left,
  291. App.screen.width - Test_margin_right,
  292. love.graphics.getFont(),
  293. 14,
  294. 15) -- line height
  295. end
  296. -- all text_input events are also keypresses
  297. -- TODO: handle chords of multiple keys
  298. function edit.run_after_text_input(State, t)
  299. edit.keychord_press(State, t)
  300. edit.text_input(State, t)
  301. edit.key_release(State, t)
  302. App.screen.contents = {}
  303. edit.update(State, 0)
  304. edit.draw(State)
  305. end
  306. -- not all keys are text_input
  307. function edit.run_after_keychord(State, chord, key)
  308. edit.keychord_press(State, chord, key)
  309. edit.key_release(State, key)
  310. App.screen.contents = {}
  311. edit.update(State, 0)
  312. edit.draw(State)
  313. end
  314. function edit.run_after_mouse_click(State, x,y, mouse_button)
  315. App.fake_mouse_press(x,y, mouse_button)
  316. edit.mouse_press(State, x,y, mouse_button)
  317. edit.draw(State)
  318. App.fake_mouse_release(x,y, mouse_button)
  319. edit.mouse_release(State, x,y, mouse_button)
  320. App.screen.contents = {}
  321. edit.update(State, 0)
  322. edit.draw(State)
  323. end
  324. function edit.run_after_mouse_press(State, x,y, mouse_button)
  325. App.fake_mouse_press(x,y, mouse_button)
  326. edit.mouse_press(State, x,y, mouse_button)
  327. App.screen.contents = {}
  328. edit.update(State, 0)
  329. edit.draw(State)
  330. end
  331. function edit.run_after_mouse_release(State, x,y, mouse_button)
  332. App.fake_mouse_release(x,y, mouse_button)
  333. edit.mouse_release(State, x,y, mouse_button)
  334. App.screen.contents = {}
  335. edit.update(State, 0)
  336. edit.draw(State)
  337. end