local easing = require "lib.utils.easing" local AnimationNode = require "lib.animation_node" local Element = require "lib.simple_ui.element" local Rect = require "lib.simple_ui.rect" local SkillRow = require "lib.simple_ui.level.skill_row" --- @class BarElement : UIElement --- @field getter fun() : number --- @field value number --- @field maxValue number local barElement = setmetatable({}, Element) barElement.__index = barElement 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() love.graphics.push() love.graphics.applyTransform(self.transform) love.graphics.setColor(1, 1, 1) love.graphics.rectangle("fill", self.bounds.x, self.bounds.y, self.bounds.width * self.value / self.maxValue, self.bounds.height) love.graphics.setColor(1, 1, 1) love.graphics.pop() end --- @class BottomBars : UIElement --- @field children BarElement[] local bottomBars = setmetatable({}, Element) bottomBars.__index = bottomBars; --- @param cid Id function bottomBars.new(cid) local t = setmetatable({}, bottomBars) t.children = { barElement:new { getter = function() local char = Tree.level.characters[cid] return char:try(Tree.behaviors.stats, function(stats) return stats.hp or 0 end) end, maxValue = 20 }, barElement:new { getter = function() local char = Tree.level.characters[cid] return char:try(Tree.behaviors.stats, function(stats) return stats.mana or 0 end) end, maxValue = 10 } } return t end function bottomBars:update(dt) local padding = 8 local screenW, screenH = love.graphics.getDimensions() self.bounds = Rect { height = 40 + 2 * padding, width = screenW * 0.25, y = screenH - 64 - 1 - 2 * padding - 40 } self.transform = love.math.newTransform():translate(-self.bounds.width / 2 + screenW / 2, self.bounds.y) for i = 1, #self.children do self.children[i].bounds = Rect { width = screenW * 0.25, height = 20 } self.children[i].transform = self.transform:clone():translate(0, self.children[i].bounds.height * (i - 1) + (i - 1) * padding) end for _, el in ipairs(self.children) do el:update(dt) end end function bottomBars:draw() for _, el in ipairs(self.children) do el:draw() end end local layout = {} function layout:update(dt) local cid = Tree.level.selector:selected() if cid then self.skillRow = SkillRow(cid) self.skillRow:show() -- self.bottomBars = bottomBars.new(cid) elseif Tree.level.selector:deselected() then self.skillRow:hide() -- self.bottomBars = nil end if self.skillRow then self.skillRow:update(dt) end -- if self.bottomBars then self.bottomBars:update(dt) end end function layout:draw() if self.skillRow then self.skillRow:draw() end -- if self.bottomBars then self.bottomBars:draw() end end return layout