about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-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