about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2022-05-18 17:22:04 -0700
committerKartik K. Agaram <vc@akkartik.com>2022-05-18 17:42:08 -0700
commitf91e5201241451fe5f9712a97c288f1534e74b11 (patch)
treee5d6618d03e6a49bfbd120a536b4aba6dc7361cc
parente27165cb9f0f0635f0290cb345442292faeba7fd (diff)
downloadlines.love-f91e5201241451fe5f9712a97c288f1534e74b11.tar.gz
scroll past first page
Still some limitations. The text cursor has to be visible on screen, so
if you have a long series of drawings without intervening lines of text
you won't be able to scroll through them all.
-rw-r--r--drawing.lua1
-rw-r--r--main.lua69
-rw-r--r--text.lua13
3 files changed, 63 insertions, 20 deletions
diff --git a/drawing.lua b/drawing.lua
index be4952e..7047893 100644
--- a/drawing.lua
+++ b/drawing.lua
@@ -177,6 +177,7 @@ end
 
 
 function Drawing.in_drawing(drawing, x,y)
+  if drawing.y == nil then return false end  -- outside current page
   return y >= drawing.y and y < drawing.y + Drawing.pixels(drawing.h) and x >= 16 and x < 16+Drawing_width
 end
 
diff --git a/main.lua b/main.lua
index d95c259..8ca3709 100644
--- a/main.lua
+++ b/main.lua
@@ -36,6 +36,8 @@ require 'icons'
 -- solving for them. But for now, this is a program to create static drawings
 -- once, and read them passively thereafter.
 Lines = {{mode='text', data=''}}
+Screen_top_line = 1
+Screen_bottom_line = 1
 Cursor_line = 1
 -- this is a line
 -- ^cursor_pos = 1
@@ -99,28 +101,35 @@ function love.draw()
   love.graphics.setColor(1, 1, 1)
   love.graphics.rectangle('fill', 0, 0, Screen_width-1, Screen_height-1)
   love.graphics.setColor(0, 0, 0)
+  for line_index,line in ipairs(Lines) do
+    line.y = nil
+  end
   local y = 0
   for line_index,line in ipairs(Lines) do
-    y = y+15*Zoom
-    line.y = y
-    if line.mode == 'text' and line.data == '' then
-      button('draw', {x=4,y=y+4, w=12,h=12, color={1,1,0},
-        icon = icon.insert_drawing,
-        onpress1 = function()
-                     table.insert(Lines, line_index, {mode='drawing', y=y, h=256/2, points={}, shapes={}, pending={}})
-                     if Cursor_line >= line_index then
-                       Cursor_line = Cursor_line+1
-                     end
-                   end})
-        if line_index == Cursor_line then
-          love.graphics.setColor(0,0,0)
-          love.graphics.print('_', 25, y+6)  -- drop the cursor down a bit to account for the increased font size
-        end
-    elseif line.mode == 'drawing' then
-      y = y+Drawing.pixels(line.h)
-      Drawing.draw(line, y)
-    else
-      Text.draw(line, line_index, Cursor_line, y, Cursor_pos)
+    if line_index >= Screen_top_line then
+      y = y+15*Zoom
+      if y > Screen_height then break end
+      Screen_bottom_line = line_index
+      line.y = y
+      if line.mode == 'text' and line.data == '' then
+        button('draw', {x=4,y=y+4, w=12,h=12, color={1,1,0},
+          icon = icon.insert_drawing,
+          onpress1 = function()
+                       table.insert(Lines, line_index, {mode='drawing', y=y, h=256/2, points={}, shapes={}, pending={}})
+                       if Cursor_line >= line_index then
+                         Cursor_line = Cursor_line+1
+                       end
+                     end})
+          if line_index == Cursor_line then
+            love.graphics.setColor(0,0,0)
+            love.graphics.print('_', 25, y+6)  -- drop the cursor down a bit to account for the increased font size
+          end
+      elseif line.mode == 'drawing' then
+        y = y+Drawing.pixels(line.h)
+        Drawing.draw(line, y)
+      else
+        Text.draw(line, line_index, Cursor_line, y, Cursor_pos)
+      end
     end
   end
 end
@@ -157,6 +166,26 @@ function keychord_pressed(chord)
     if drawing then
       drawing.pending = {}
     end
+  elseif chord == 'pagedown' then
+    Screen_top_line = Screen_bottom_line
+    Cursor_line = Screen_top_line
+    Cursor_pos = 1
+  elseif chord == 'pageup' then
+    -- duplicate some logic from love.draw
+    local y = Screen_height
+    while y >= 0 do
+      if Screen_top_line == 1 then break end
+      if Lines[Screen_top_line].mode == 'text' then
+        y = y - 15*Zoom
+      else
+        y = y - Drawing.pixels(Lines[Screen_top_line].h)
+      end
+      Screen_top_line = Screen_top_line - 1
+    end
+    if Cursor_line ~= Screen_top_line then
+      Cursor_pos = 1
+    end
+    Cursor_line = Screen_top_line
   else
     Text.keychord_pressed(chord)
   end
diff --git a/text.lua b/text.lua
index 6888fda..f7ffb63 100644
--- a/text.lua
+++ b/text.lua
@@ -50,6 +50,9 @@ function Text.keychord_pressed(chord)
           break
         end
       end
+      if Cursor_line < Screen_top_line then
+        Screen_top_line = Cursor_line
+      end
     end
   elseif chord == 'right' then
     assert(Lines[Cursor_line].mode == 'text')
@@ -65,6 +68,9 @@ function Text.keychord_pressed(chord)
           break
         end
       end
+      if Cursor_line > Screen_bottom_line then
+        Screen_top_line = Cursor_line
+      end
     end
   elseif chord == 'home' then
     Cursor_pos = 1
@@ -128,6 +134,9 @@ function Text.keychord_pressed(chord)
         break
       end
     end
+    if Cursor_line < Screen_top_line then
+      Screen_top_line = Cursor_line
+    end
   elseif chord == 'down' then
     assert(Lines[Cursor_line].mode == 'text')
     local new_cursor_line = Cursor_line
@@ -140,10 +149,14 @@ function Text.keychord_pressed(chord)
         break
       end
     end
+    if Cursor_line > Screen_bottom_line then
+      Screen_top_line = Cursor_line
+    end
   end
 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+15*Zoom
 end