uiprogressbar.lua

main 100 lines · 2.6 KB Raw
Alex Alex Commit Client Upload Part 2 19/10/2026 00:06
1-- @docclass
2UIProgressBar = extends(UIWidget, "UIProgressBar")
3
4function UIProgressBar.create()
5 local progressbar = UIProgressBar.internalCreate()
6 progressbar:setFocusable(false)
7 progressbar:setOn(true)
8 progressbar.min = 0
9 progressbar.max = 100
10 progressbar.value = 0
11 progressbar.bgBorderLeft = 0
12 progressbar.bgBorderRight = 0
13 progressbar.bgBorderTop = 0
14 progressbar.bgBorderBottom = 0
15 return progressbar
16end
17
18function UIProgressBar:setMinimum(minimum)
19 self.minimum = minimum
20 if self.value < minimum then
21 self:setValue(minimum)
22 end
23end
24
25function UIProgressBar:setMaximum(maximum)
26 self.maximum = maximum
27 if self.value > maximum then
28 self:setValue(maximum)
29 end
30end
31
32function UIProgressBar:setValue(value, minimum, maximum)
33 if minimum then
34 self:setMinimum(minimum)
35 end
36
37 if maximum then
38 self:setMaximum(maximum)
39 end
40
41 self.value = math.max(math.min(value, self.maximum), self.minimum)
42 self:updateBackground()
43end
44
45function UIProgressBar:setPercent(percent)
46 self:setValue(percent, 0, 100)
47end
48
49function UIProgressBar:getPercent()
50 return self.value
51end
52
53function UIProgressBar:getPercentPixels()
54 return (self.maximum - self.minimum) / self:getWidth()
55end
56
57function UIProgressBar:getProgress()
58 if self.minimum == self.maximum then return 1 end
59 return (self.value - self.minimum) / (self.maximum - self.minimum)
60end
61
62function UIProgressBar:updateBackground()
63 if self:isOn() then
64 local width = math.round(math.max((self:getProgress() * (self:getWidth() - self.bgBorderLeft - self.bgBorderRight)), 1))
65 local height = self:getHeight() - self.bgBorderTop - self.bgBorderBottom
66 local rect = { x = self.bgBorderLeft, y = self.bgBorderTop, width = width, height = height }
67 self:setBackgroundRect(rect)
68 end
69end
70
71function UIProgressBar:onSetup()
72 self:updateBackground()
73end
74
75function UIProgressBar:onStyleApply(name, node)
76 for name,value in pairs(node) do
77 if name == 'background-border-left' then
78 self.bgBorderLeft = tonumber(value)
79 elseif name == 'background-border-right' then
80 self.bgBorderRight = tonumber(value)
81 elseif name == 'background-border-top' then
82 self.bgBorderTop = tonumber(value)
83 elseif name == 'background-border-bottom' then
84 self.bgBorderBottom = tonumber(value)
85 elseif name == 'background-border' then
86 self.bgBorderLeft = tonumber(value)
87 self.bgBorderRight = tonumber(value)
88 self.bgBorderTop = tonumber(value)
89 self.bgBorderBottom = tonumber(value)
90 end
91 end
92end
93
94function UIProgressBar:onGeometryChange(oldRect, newRect)
95 if not self:isOn() then
96 self:setHeight(0)
97 end
98 self:updateBackground()
99end
100
Top