Implement charge meter.

This commit is contained in:
Anna Rose Wiggins 2023-09-30 01:10:27 -04:00
parent b222ce3d67
commit e045eec163
4 changed files with 78 additions and 3 deletions

42
src/meter.lua Normal file
View file

@ -0,0 +1,42 @@
import "CoreLibs/graphics"
import "CoreLibs/object"
import "CoreLibs/math"
local gfx <const> = playdate.graphics
class("Meter").extends(gfx.sprite)
function Meter:init(value, w, h, vertical)
Meter.super.init(self)
self.vertical = vertical or true
self:setSize(w, h)
self:setZIndex(999)
self:setValue(value)
end
function Meter:setValue(value)
local img = gfx.image.new(self:getSize())
gfx.pushContext(img)
gfx.drawRect(0, 0, self:getSize())
local x, y, w, h = 0
if self.vertical then
x = 0
w = self.width
y = self.height - playdate.math.lerp(0, self.height, value / 100)
print(y)
h = self.height - y
print(h)
else
y = 0
h = self.height
x = 0
w = self.width - playdate.math.lerp(0, self.width, value / 100)
end
gfx.setColor(gfx.kColorWhite)
gfx.fillRect(1, 1, self.width-2, self.height-2)
gfx.setColor(gfx.kColorBlack)
gfx.fillRect(x, y, w, h)
gfx.popContext()
self:setImage(img)
end