about summary refs log tree commit diff stats
path: root/main.lua
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 /main.lua
parente27165cb9f0f0635f0290cb345442292faeba7fd (diff)
downloadtext.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.
Diffstat (limited to 'main.lua')
-rw-r--r--main.lua69
1 files changed, 49 insertions, 20 deletions
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