edit.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. button_handlers = {},
  52. }
  53. return result
  54. end -- edit.initialize_state
  55. function edit.check_locs(State)
  56. -- if State is inconsistent (i.e. file changed by some other program),
  57. -- throw away all cursor state entirely
  58. if edit.invalid1(State, State.screen_top1)
  59. or edit.invalid_cursor1(State)
  60. or not Text.le1(State.screen_top1, State.cursor1) then
  61. State.screen_top1 = {line=1, pos=1}
  62. State.cursor1 = {line=1, pos=1}
  63. end
  64. end
  65. function edit.invalid1(State, loc1)
  66. if loc1.line > #State.lines then return true end
  67. local l = State.lines[loc1.line]
  68. if l.mode ~= 'text' then return false end -- pos is irrelevant to validity for a drawing line
  69. return loc1.pos > #State.lines[loc1.line].data
  70. end
  71. -- cursor loc in particular differs from other locs in one way:
  72. -- pos might occur just after end of line
  73. function edit.invalid_cursor1(State)
  74. local cursor1 = State.cursor1
  75. if cursor1.line > #State.lines then return true end
  76. local l = State.lines[cursor1.line]
  77. if l.mode ~= 'text' then return false end -- pos is irrelevant to validity for a drawing line
  78. return cursor1.pos > #State.lines[cursor1.line].data + 1
  79. end
  80. function edit.draw(State)
  81. love.graphics.setFont(State.font)
  82. App.color(Text_color)
  83. 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))
  84. 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))
  85. State.cursor_x = nil
  86. State.cursor_y = nil
  87. local y = State.top
  88. --? print('== draw')
  89. for line_index = State.screen_top1.line,#State.lines do
  90. local line = State.lines[line_index]
  91. --? print('draw:', y, line_index, line)
  92. if y + State.line_height > App.screen.height then break end
  93. --? print('text.draw', y, line_index)
  94. local startpos = 1
  95. if line_index == State.screen_top1.line then
  96. startpos = State.screen_top1.pos
  97. end
  98. y = Text.draw(State, line_index, y, startpos)
  99. --? print('=> y', y)
  100. end
  101. if State.search_term then
  102. Text.draw_search_bar(State)
  103. end
  104. end
  105. function edit.update(State, dt)
  106. end
  107. function edit.quit(State)
  108. end
  109. function edit.mouse_press(State, x,y, mouse_button, is_touch, presses)
  110. if State.search_term then return end
  111. State.mouse_down = mouse_button
  112. --? print_and_log(('edit.mouse_press: cursor at %d,%d'):format(State.cursor1.line, State.cursor1.pos))
  113. if y < State.top then
  114. State.old_cursor1 = State.cursor1
  115. State.old_selection1 = State.selection1
  116. State.mousepress_shift = App.shift_down()
  117. State.selection1 = {
  118. line=State.screen_top1.line,
  119. pos=State.screen_top1.pos,
  120. }
  121. return
  122. end
  123. for line_index,line in ipairs(State.lines) do
  124. if Text.in_line(State, line_index, x,y) then
  125. -- delicate dance between cursor, selection and old cursor/selection
  126. -- scenarios:
  127. -- regular press+release: sets cursor, clears selection
  128. -- shift press+release:
  129. -- sets selection to old cursor if not set otherwise leaves it untouched
  130. -- sets cursor
  131. -- press and hold to start a selection: sets selection on press, cursor on release
  132. -- press and hold, then press shift: ignore shift
  133. -- i.e. mouse_release should never look at shift state
  134. --? print_and_log(('edit.mouse_press: in line %d'):format(line_index))
  135. State.old_cursor1 = State.cursor1
  136. State.old_selection1 = State.selection1
  137. State.mousepress_shift = App.shift_down()
  138. State.selection1 = {
  139. line=line_index,
  140. pos=Text.to_pos_on_line(State, line_index, x, y),
  141. }
  142. return
  143. end
  144. end
  145. -- still here? mouse press is below all screen lines
  146. State.old_cursor1 = State.cursor1
  147. State.old_selection1 = State.selection1
  148. State.mousepress_shift = App.shift_down()
  149. State.selection1 = Text.final_loc_on_screen(State)
  150. end
  151. function edit.mouse_release(State, x,y, mouse_button, is_touch, presses)
  152. if State.search_term then return end
  153. --? print_and_log(('edit.mouse_release(%d,%d): cursor at %d,%d'):format(x,y, State.cursor1.line, State.cursor1.pos))
  154. State.mouse_down = nil
  155. if y < State.top then
  156. State.cursor1 = deepcopy(State.screen_top1)
  157. edit.clean_up_mouse_press(State)
  158. return
  159. end
  160. for line_index,line in ipairs(State.lines) do
  161. if Text.in_line(State, line_index, x,y) then
  162. --? print_and_log(('edit.mouse_release: in line %d'):format(line_index))
  163. State.cursor1 = {
  164. line=line_index,
  165. pos=Text.to_pos_on_line(State, line_index, x, y),
  166. }
  167. --? print_and_log(('edit.mouse_release: cursor now %d,%d'):format(State.cursor1.line, State.cursor1.pos))
  168. edit.clean_up_mouse_press(State)
  169. return
  170. end
  171. end
  172. -- still here? mouse release is below all screen lines
  173. State.cursor1 = Text.final_loc_on_screen(State)
  174. edit.clean_up_mouse_press(State)
  175. --? 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))
  176. end
  177. function edit.clean_up_mouse_press(State)
  178. if State.mousepress_shift then
  179. if State.old_selection1.line == nil then
  180. State.selection1 = State.old_cursor1
  181. else
  182. State.selection1 = State.old_selection1
  183. end
  184. end
  185. State.old_cursor1, State.old_selection1, State.mousepress_shift = nil
  186. if eq(State.cursor1, State.selection1) then
  187. State.selection1 = {}
  188. end
  189. end
  190. function edit.mouse_wheel_move(State, dx,dy)
  191. if dy > 0 then
  192. State.cursor1 = deepcopy(State.screen_top1)
  193. for i=1,math.floor(dy) do
  194. Text.up(State)
  195. end
  196. elseif dy < 0 then
  197. State.cursor1 = Text.screen_bottom1(State)
  198. for i=1,math.floor(-dy) do
  199. Text.down(State)
  200. end
  201. end
  202. end
  203. function edit.text_input(State, t)
  204. --? print('text input', t)
  205. if State.search_term then
  206. State.search_term = State.search_term..t
  207. Text.search_next(State)
  208. end
  209. end
  210. function edit.keychord_press(State, chord, key, scancode, is_repeat)
  211. if State.selection1.line and
  212. -- printable character created using shift key => delete selection
  213. -- (we're not creating any ctrl-shift- or alt-shift- combinations using regular/printable keys)
  214. (not App.shift_down() or utf8.len(key) == 1) and
  215. 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
  216. Text.delete_selection_and_record_undo_event(State)
  217. end
  218. if State.search_term then
  219. if chord == 'escape' then
  220. State.search_term = nil
  221. State.cursor1 = State.search_backup.cursor
  222. State.screen_top1 = State.search_backup.screen_top
  223. State.search_backup = nil
  224. Text.redraw_all(State) -- if we're scrolling, reclaim all line caches to avoid memory leaks
  225. elseif chord == 'return' then
  226. State.search_term = nil
  227. State.search_backup = nil
  228. elseif chord == 'backspace' then
  229. local len = utf8.len(State.search_term)
  230. local byte_offset = Text.offset(State.search_term, len)
  231. State.search_term = string.sub(State.search_term, 1, byte_offset-1)
  232. State.cursor = deepcopy(State.search_backup.cursor)
  233. State.screen_top = deepcopy(State.search_backup.screen_top)
  234. Text.search_next(State)
  235. elseif chord == 'down' then
  236. if #State.search_term > 0 then
  237. Text.right(State)
  238. Text.search_next(State)
  239. end
  240. elseif chord == 'up' then
  241. Text.search_previous(State)
  242. end
  243. return
  244. elseif chord == 'C-f' then
  245. State.search_term = ''
  246. State.search_backup = {
  247. cursor={line=State.cursor1.line, pos=State.cursor1.pos},
  248. screen_top={line=State.screen_top1.line, pos=State.screen_top1.pos},
  249. }
  250. -- zoom
  251. elseif chord == 'C-=' then
  252. edit.update_font_settings(State, State.font_height+2)
  253. Text.redraw_all(State)
  254. elseif chord == 'C--' then
  255. if State.font_height > 2 then
  256. edit.update_font_settings(State, State.font_height-2)
  257. Text.redraw_all(State)
  258. end
  259. elseif chord == 'C-0' then
  260. edit.update_font_settings(State, 20)
  261. Text.redraw_all(State)
  262. -- clipboard
  263. elseif chord == 'C-a' then
  264. State.selection1 = {line=1, pos=1}
  265. State.cursor1 = {line=#State.lines, pos=utf8.len(State.lines[#State.lines].data)+1}
  266. elseif chord == 'C-c' then
  267. local s = Text.selection(State)
  268. if s then
  269. App.set_clipboard(s)
  270. end
  271. else
  272. Text.keychord_press(State, chord, key, scancode, is_repeat)
  273. end
  274. end
  275. function edit.key_release(State, key, scancode)
  276. end
  277. function edit.update_font_settings(State, font_height, font)
  278. State.font_height = font_height
  279. State.font = font or love.graphics.newFont(State.font_height)
  280. State.line_height = math.floor(font_height*1.3)
  281. end
  282. --== some methods for tests
  283. -- Insulate tests from some key globals so I don't have to change the vast
  284. -- majority of tests when they're modified for the real app.
  285. Test_margin_left = 25
  286. Test_margin_right = 0
  287. function edit.initialize_test_state()
  288. -- if you change these values, tests will start failing
  289. return edit.initialize_state(
  290. 15, -- top margin
  291. Test_margin_left,
  292. App.screen.width - Test_margin_right,
  293. love.graphics.getFont(),
  294. 14,
  295. 15) -- line height
  296. end
  297. -- all text_input events are also keypresses
  298. -- TODO: handle chords of multiple keys
  299. function edit.run_after_text_input(State, t)
  300. edit.keychord_press(State, t)
  301. edit.text_input(State, t)
  302. edit.key_release(State, t)
  303. App.screen.contents = {}
  304. edit.update(State, 0)
  305. edit.draw(State)
  306. end
  307. -- not all keys are text_input
  308. function edit.run_after_keychord(State, chord, key)
  309. edit.keychord_press(State, chord, key)
  310. edit.key_release(State, key)
  311. App.screen.contents = {}
  312. edit.update(State, 0)
  313. edit.draw(State)
  314. end
  315. function edit.run_after_mouse_click(State, x,y, mouse_button)
  316. App.fake_mouse_press(x,y, mouse_button)
  317. edit.mouse_press(State, x,y, mouse_button)
  318. edit.draw(State)
  319. App.fake_mouse_release(x,y, mouse_button)
  320. edit.mouse_release(State, x,y, mouse_button)
  321. App.screen.contents = {}
  322. edit.update(State, 0)
  323. edit.draw(State)
  324. end
  325. function edit.run_after_mouse_press(State, x,y, mouse_button)
  326. App.fake_mouse_press(x,y, mouse_button)
  327. edit.mouse_press(State, x,y, mouse_button)
  328. App.screen.contents = {}
  329. edit.update(State, 0)
  330. edit.draw(State)
  331. end
  332. function edit.run_after_mouse_release(State, x,y, mouse_button)
  333. App.fake_mouse_release(x,y, mouse_button)
  334. edit.mouse_release(State, x,y, mouse_button)
  335. App.screen.contents = {}
  336. edit.update(State, 0)
  337. edit.draw(State)
  338. end