diff --git a/gfx/progress.lua b/gfx/progress.lua new file mode 100644 index 0000000..8040eac --- /dev/null +++ b/gfx/progress.lua @@ -0,0 +1,77 @@ +local Element = require("gfx.element") +local Orientation = require("gfx.prop.orientation") + +local BlankElement = Element:new() + +function BlankElement:draw() + local dirty = Element.draw(self) + if dirty then + self:_getWindow().clear() + end +end + +local function round(value, biasDown) + local decimal = value % 1 + return (((not biasDown) and decimal < 0.5) or ((not not biasDown) and decimal <= 0.5)) and math.floor(value) or math.ceil(value) +end + +local Progress = Orientation.with(Element:new{ + progress = 0.0, + active = nil, + inactive = nil +}) + +function Progress:new(o) + local obj = o or {} + obj.active = BlankElement:new() + obj.inactive = BlankElement:new() + return Element.new(self, obj) +end + +function Progress:_updateProgress() + local width, height = self:getWidth(), self:getHeight() + if self:isVertical() then + self.active:resize(width, round(self:getProgress() * height)) + self.inactive:resize(width, round((1 - self:getProgress()) * height, true)) + self.inactive:setY(self.active:getY() + self.active:getHeight()) + else + self.active:resize(round(self:getProgress() * width), height) + self.active:resize(round((1 - self:getProgress()) * width, true), height) + self.inactive:setX(self.active:setX() + self.active:getWidth()) + end +end + +function Progress:setProgress(progress) + self:setDirty() + self.progress = math.min(1, math.max(0, progress)) + self:_updateProgress() +end + +function Progress:getProgress() + return self.progress +end + +function Progress:draw() + local dirty = Element.draw(self) + if dirty then + self.inactive:draw() + self.active:draw() + end + return dirty +end + +function Progress:_isDirty() + return Element._isDirty(self) or self.active:_isDirty() or self.inactive:_isDirty() +end + +function Progress:_reload() + Element._reload(self) + + -- Reload child windows + local win = self:_getWindow() + self.active:setParent(win) + self.inactive:setParent(win) + self:_updateProgress() +end + +return Progress \ No newline at end of file