-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathQuartzStatusBar.lua
143 lines (119 loc) · 4.13 KB
/
QuartzStatusBar.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
--[[
Copyright (C) 2006-2007 Nymbia
Copyright (C) 2010-2017 Hendrik "Nevcairiel" Leppkes < h.leppkes@gmail.com >
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
]]
local Quartz3 = LibStub("AceAddon-3.0"):GetAddon("Quartz3")
local QuartzStatusBar = CreateFrame("Frame")
local MetaTable = {__index = QuartzStatusBar}
QuartzStatusBar.__min = 0
QuartzStatusBar.__max = 100
QuartzStatusBar.__value = 0
QuartzStatusBar.__orientation = "HORIZONTAL"
QuartzStatusBar.__rotatesTexture = 1
local DrawBar, UpdateBarValue
function Quartz3:CreateStatusBar(name, parent)
local bar = setmetatable(CreateFrame("Frame", name, parent, "BackdropTemplate"), MetaTable)
bar.__texture = bar:CreateTexture(nil, "ARTWORK")
bar.__texture:SetTexture([[Interface\TargetingFrame\UI-StatusBar]])
DrawBar(bar)
return bar
end
function DrawBar(self)
self.__texture:ClearAllPoints()
self.__texture:SetPoint("TOPLEFT", self, "TOPLEFT")
if self.__orientation == "HORIZONTAL" then
self.__texture:SetPoint("BOTTOMLEFT", self, "BOTTOMLEFT")
elseif self.__orientation == "VERTICAL" then
self.__texture:SetPoint("TOPRIGHT", self, "TOPRIGHT")
end
local r, g, b, a = 1, 1, 1, 1
if self.__color then
if #self.__color == 3 then
r, g, b = unpack(self.__color)
elseif #self.__color == 4 then
r, g, b, a = unpack(self.__color)
end
end
self.__texture:SetVertexColor(r, g, b, a)
UpdateBarValue(self)
end
function UpdateBarValue(self)
local perc = 0
if self.__max ~= self.__min then
perc = (self.__value - self.__min) / (self.__max - self.__min)
end
perc = min(max(perc, 0), 1)
local width = self:GetWidth()
self.__texture:SetPoint("RIGHT", self, "LEFT", perc * width, 0)
self.__texture:SetTexCoord(0, perc, 0, 1)
end
function QuartzStatusBar:GetMinMaxValues()
return self.__min, self.__max
end
function QuartzStatusBar:SetMinMaxValues(min, max)
if not tonumber(min) or not tonumber(max) then
return error(format("QuartzStatusBar:SetMinMaxValues(min, max): Invalid min or max specified! Values were: %q, %q", min, max), 2)
end
self.__min = tonumber(min)
self.__max = tonumber(max)
UpdateBarValue(self)
end
function QuartzStatusBar:GetValue()
return self.__value
end
function QuartzStatusBar:SetValue(value)
if not tonumber(value) then
return error(format("QuartzStatusBar:SetValue(value): Invalid value specified! Value was: %q", value), 2)
end
self.__value = tonumber(value)
UpdateBarValue(self)
end
function QuartzStatusBar:GetOrientation()
return self.__orientation
end
function QuartzStatusBar:SetOrientation(orientation)
if orientation ~= "HORIZONTAL" and orientation ~= "VERTICAL" then
return error("QuartzStatusBar:SetOrientation(orientation): Only HORIZONTAL and VERTICAL orientations supported!", 2)
end
self.__orientation = orientation
DrawBar(self)
end
function QuartzStatusBar:GetRotatesTexture()
return self.__rotatesTexture
end
function QuartzStatusBar:SetRotatesTexture(rotate)
self.__rotatesTexture = rotate and 1 or nil
DrawBar(self)
end
function QuartzStatusBar:GetStatusBarColor()
return unpack(self.__color)
end
function QuartzStatusBar:SetStatusBarColor(r, g, b, a)
if not (r and g and b) then
error("Usage: SetStatusBarColor(r, g, b[, a])", 2)
end
self.__color = {r, g, b, a}
DrawBar(self)
end
function QuartzStatusBar:GetStatusBarTexture()
return self.__texture
end
function QuartzStatusBar:SetStatusBarTexture(texture, layer)
if type(texture) == "string" then
self.__texture:SetTexture(texture)
elseif type(texture) == "table" then
self.__texture = texture
end
self.__texture:SetDrawLayer(layer or "ARTWORK")
end