40 lines
1,022 B
Lua
40 lines
1,022 B
Lua
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)
|
|
h = self.height - y
|
|
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
|