72 lines
2.5 KiB
Lua
72 lines
2.5 KiB
Lua
local Element = require "lib.simple_ui.element"
|
|
|
|
|
|
--- @class BarElement : UIElement
|
|
--- @field getter fun() : number
|
|
--- @field value number
|
|
--- @field maxValue number
|
|
--- @field color Color
|
|
--- @field useDividers boolean
|
|
--- @field drawText boolean
|
|
local barElement = setmetatable({}, Element)
|
|
barElement.__index = barElement
|
|
barElement.useDividers = false
|
|
barElement.drawText = false
|
|
|
|
function barElement:update(dt)
|
|
local val = self.getter()
|
|
self.value = val < 0 and 0 or val > self.maxValue and self.maxValue or val
|
|
end
|
|
|
|
function barElement:draw()
|
|
local valueWidth = self.bounds.width * self.value / self.maxValue
|
|
local emptyWidth = self.bounds.width - valueWidth
|
|
|
|
--- шум
|
|
love.graphics.setShader(Tree.assets.files.shaders.soft_uniform_noise)
|
|
love.graphics.rectangle("fill", self.bounds.x, self.bounds.y, self.bounds.width, self.bounds.height)
|
|
love.graphics.setShader()
|
|
|
|
--- закраска пустой части
|
|
love.graphics.setColor(0.05, 0.05, 0.05)
|
|
love.graphics.setBlendMode("multiply", "premultiplied")
|
|
love.graphics.rectangle("fill", self.bounds.x + valueWidth, self.bounds.y, emptyWidth,
|
|
self.bounds.height)
|
|
love.graphics.setBlendMode("alpha")
|
|
|
|
--- закраска значимой части её цветом
|
|
love.graphics.setColor(self.color.r, self.color.g, self.color.b)
|
|
love.graphics.setBlendMode("multiply", "premultiplied")
|
|
love.graphics.rectangle("fill", self.bounds.x, self.bounds.y, valueWidth,
|
|
self.bounds.height)
|
|
love.graphics.setBlendMode("alpha")
|
|
|
|
--- мерки
|
|
love.graphics.setColor(38 / 255, 50 / 255, 56 / 255)
|
|
if self.useDividers then
|
|
local count = self.maxValue - 1
|
|
local measureWidth = self.bounds.width / self.maxValue
|
|
|
|
for i = 1, count, 1 do
|
|
love.graphics.line(self.bounds.x + i * measureWidth, self.bounds.y, self.bounds.x + i * measureWidth,
|
|
self.bounds.y + self.bounds.height)
|
|
end
|
|
end
|
|
|
|
|
|
love.graphics.setColor(1, 1, 1)
|
|
--- текст поверх
|
|
if self.drawText then
|
|
local font = Tree.fonts:getDefaultTheme():getVariant("medium")
|
|
local t = love.graphics.newText(font, tostring(self.value) .. "/" .. tostring(self.maxValue))
|
|
love.graphics.draw(t, math.floor(self.bounds.x + self.bounds.width / 2 - t:getWidth() / 2),
|
|
math.floor(self.bounds.y + self.bounds.height / 2 - t:getHeight() / 2))
|
|
end
|
|
|
|
self:drawBorder("inner")
|
|
|
|
self:drawGradientOverlay()
|
|
end
|
|
|
|
return function(values) return barElement:new(values) end
|