about summary refs log tree commit diff stats
path: root/text.lua
diff options
context:
space:
mode:
Diffstat (limited to 'text.lua')
-rw-r--r--text.lua55
1 files changed, 50 insertions, 5 deletions
diff --git a/text.lua b/text.lua
index 4f992d5..216ba32 100644
--- a/text.lua
+++ b/text.lua
@@ -39,8 +39,6 @@ function Text.compute_fragments(line, line_width)
 end
 
 function Text.draw(line, line_width, line_index, cursor_line, cursor_pos)
---?   love.graphics.setColor(0.75,0.75,0.75)
---?   love.graphics.line(line_width, 0, line_width, Screen_height)
   love.graphics.setColor(0,0,0)
   -- wrap long lines
   local x = 25
@@ -57,6 +55,11 @@ function Text.draw(line, line_width, line_index, cursor_line, cursor_pos)
       assert(x > 25)  -- no overfull lines
       y = y + math.floor(15*Zoom)
       x = 25
+      if line.screen_line_starting_pos == nil then
+        line.screen_line_starting_pos = {1, pos}
+      else
+        table.insert(line.screen_line_starting_pos, pos)
+      end
     end
     love.graphics.draw(frag_text, x,y, 0, Zoom)
     -- render cursor if necessary
@@ -267,13 +270,55 @@ end
 
 function Text.in_line(line, x,y)
   if line.y == nil then return false end  -- outside current page
-  return x >= 16 and y >= line.y and y < line.y + math.floor(15*Zoom)
+  if x < 16 then return false end
+  if y < line.y then return false end
+  if line.screen_line_starting_pos == nil then return y < line.y + math.floor(15*Zoom) end
+  return y < line.y + #line.screen_line_starting_pos * math.floor(15*Zoom)
 end
 
-function Text.move_cursor(line_index, line, mx)
+function Text.move_cursor(line_index, line, mx, my)
   Cursor_line = line_index
-  Cursor_pos = Text.nearest_cursor_pos(line.data, mx)
+  if line.screen_line_starting_pos == nil then
+--?     print('single screen line')
+    Cursor_pos = Text.nearest_cursor_pos(line.data, mx)
+    return
+  end
+  assert(line.fragments)
+  assert(my >= line.y)
+--?   print('move_cursor', mx, my)
+  if my < line.y + math.floor(15*Zoom) then
+--?     print('first screen line')
+    Cursor_pos = Text.nearest_cursor_pos(line.data, mx)
+    return
+  end
+  -- duplicate some logic from Text.draw
+  local y = line.y
+  for _,screen_line_starting_pos in ipairs(line.screen_line_starting_pos) do
+--?     print('screen line:', screen_line_starting_pos)
+    local nexty = y + math.floor(15*Zoom)
+    if my < nexty then
+      local s = string.sub(line.data, screen_line_starting_pos)
+      Cursor_pos = screen_line_starting_pos + Text.nearest_cursor_pos(s, mx) - 1
+      return
+    end
+    y = nexty
+  end
+  assert(false)
 end
+-- manual test:
+--  line: abc
+--        def
+--        gh
+--  fragments: abc, def, gh
+--  click inside e
+--  line_starting_pos = 1 + 3 = 4
+--  nearest_cursor_pos('defgh', mx) = 2
+--  Cursor_pos = 4 + 2 - 1 = 5
+-- manual test:
+--  click inside h
+--  line_starting_pos = 1 + 3 + 3 = 7
+--  nearest_cursor_pos('gh', mx) = 2
+--  Cursor_pos = 7 + 2 - 1 = 8
 
 function Text.nearest_cursor_pos(line, x, hint)
   if x == 0 then
='n221' href='#n221'>221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333