Skip to content

Commit

Permalink
Fix clear on resize bug (#36)
Browse files Browse the repository at this point in the history
Fixes #5
  • Loading branch information
InDieTasten authored Oct 15, 2024
1 parent 56ffc31 commit 3fb310a
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ require("lib/term")
require("lib/canvas")
require("lib/term-ui")

local defaultCanvasWidth = 20
local defaultCanvasHeight = 10

local cursorX
local cursorY

local canvas
local canvasX
local canvasY

local function update(inputs)
for _, input in ipairs(inputs) do
Expand All @@ -17,10 +22,18 @@ local function update(inputs)
cursorY = input.y
elseif input.type == "mouse_press" then
if canvas then
Canvas.setPixel(canvas, input.x, input.y, "O")
if input.x > canvasX and input.x <= canvasX + canvas.width and
input.y > canvasY and input.y <= canvasY + canvas.height then
Canvas.setPixel(canvas, input.x - canvasX, input.y - canvasY, "O")
end
end
elseif input.type == "resize" then
canvas = Canvas.new(Term.width, Term.height)
if not canvas then
canvas = Canvas.new(defaultCanvasWidth, defaultCanvasHeight)
end

canvasX = math.floor(Term.width / 2 - canvas.width / 2)
canvasY = math.floor(Term.height / 2 - canvas.height / 2)
end
end

Expand All @@ -31,7 +44,7 @@ local function render()
Term.clear()

if canvas then
TermUI.drawCanvas(Term, canvas, 1, 1)
TermUI.drawCanvas(Term, canvas, canvasX + 1, canvasY + 1)
end

Term.setCursorPos(1, 1)
Expand Down

0 comments on commit 3fb310a

Please sign in to comment.