1-- @docclass
2UIRadialProgress = extends(UIWidget, "UIRadialProgress")
3
4function UIRadialProgress.create()
5 local progressbar = UIRadialProgress.internalCreate()
6 progressbar.progress = 100
7 progressbar.radial = false
8 return progressbar
9end
10
11function UIRadialProgress:onStyleApply(name, node)
12 for name,value in pairs(node) do
13 if name == 'radial' then
14 self:setRadial(value)
15 end
16 end
17end
18
19function UIRadialProgress:setProgress(value)
20 self.progress = tonumber(value) or 100
21 self:updateProgressbar()
22end
23
24function UIRadialProgress:getProgress()
25 return self.progress
26end
27
28function UIRadialProgress:setRadial(value)
29 self.radial = not not value
30end
31
32function UIRadialProgress:isRadial()
33 return self.radial
34end
35
36function UIRadialProgress:updateProgressbar()
37 local progress1 = self:getChildById('progress1')
38 local progress2 = self:getChildById('progress2')
39
40 if not (progress1 and progress2) then
41 return
42 end
43
44 local radius = progress1:getImageTextureWidth()
45 local height = radius * 2
46 local progressRad = self.progress / 100 * math.pi * 2
47
48 -- calculate progressbar heights
49 local height1 = radius * 2
50 local height2 = 0
51
52 if self.radial then
53 if progressRad < math.pi then
54 height1 = radius - math.cos(progressRad) * radius
55 else
56 progressRad = progressRad - math.pi
57 height2 = radius - math.cos(progressRad) * radius
58 end
59 else
60 if self.progress < 50 then
61 height1 = self.progress / 50 * height
62 else
63 height2 = (self.progress-50) / 50 * height
64 end
65 end
66
67 -- set image rects
68 if height1 == 0 then
69 progress1:hide()
70 else
71 progress1:show()
72 progress1:setImageRect({x=0, y=0, width=radius, height=height1})
73 progress1:setImageClip({x=0, y=0, width=radius, height=height1})
74 end
75
76 if height2 == 0 then
77 progress2:hide()
78 else
79 progress2:show()
80 progress2:setImageRect({x=0, y=height-height2, width=radius, height=height2})
81 progress2:setImageClip({x=0, y=height-height2, width=radius, height=height2})
82 end
83end