yellowfive@57
|
1 --[[-----------------------------------------------------------------------------
|
yellowfive@57
|
2 Label Widget
|
yellowfive@57
|
3 Displays text and optionally an icon.
|
yellowfive@57
|
4 -------------------------------------------------------------------------------]]
|
yellowfive@106
|
5 local Type, Version = "Label", 24
|
yellowfive@57
|
6 local AceGUI = LibStub and LibStub("AceGUI-3.0", true)
|
yellowfive@57
|
7 if not AceGUI or (AceGUI:GetWidgetVersion(Type) or 0) >= Version then return end
|
yellowfive@57
|
8
|
yellowfive@57
|
9 -- Lua APIs
|
yellowfive@57
|
10 local max, select, pairs = math.max, select, pairs
|
yellowfive@57
|
11
|
yellowfive@57
|
12 -- WoW APIs
|
yellowfive@57
|
13 local CreateFrame, UIParent = CreateFrame, UIParent
|
yellowfive@57
|
14
|
yellowfive@57
|
15 -- Global vars/functions that we don't upvalue since they might get hooked, or upgraded
|
yellowfive@57
|
16 -- List them here for Mikk's FindGlobals script
|
yellowfive@57
|
17 -- GLOBALS: GameFontHighlightSmall
|
yellowfive@57
|
18
|
yellowfive@57
|
19 --[[-----------------------------------------------------------------------------
|
yellowfive@57
|
20 Support functions
|
yellowfive@57
|
21 -------------------------------------------------------------------------------]]
|
yellowfive@57
|
22
|
yellowfive@57
|
23 local function UpdateImageAnchor(self)
|
yellowfive@57
|
24 if self.resizing then return end
|
yellowfive@57
|
25 local frame = self.frame
|
yellowfive@57
|
26 local width = frame.width or frame:GetWidth() or 0
|
yellowfive@57
|
27 local image = self.image
|
yellowfive@57
|
28 local label = self.label
|
yellowfive@57
|
29 local height
|
yellowfive@57
|
30
|
yellowfive@57
|
31 label:ClearAllPoints()
|
yellowfive@57
|
32 image:ClearAllPoints()
|
yellowfive@57
|
33
|
yellowfive@57
|
34 if self.imageshown then
|
yellowfive@57
|
35 local imagewidth = image:GetWidth()
|
yellowfive@57
|
36 if (width - imagewidth) < 200 or (label:GetText() or "") == "" then
|
yellowfive@57
|
37 -- image goes on top centered when less than 200 width for the text, or if there is no text
|
yellowfive@57
|
38 image:SetPoint("TOP")
|
yellowfive@57
|
39 label:SetPoint("TOP", image, "BOTTOM")
|
yellowfive@57
|
40 label:SetPoint("LEFT")
|
yellowfive@57
|
41 label:SetWidth(width)
|
yellowfive@57
|
42 height = image:GetHeight() + label:GetHeight()
|
yellowfive@57
|
43 else
|
yellowfive@57
|
44 -- image on the left
|
yellowfive@57
|
45 image:SetPoint("TOPLEFT")
|
yellowfive@57
|
46 if image:GetHeight() > label:GetHeight() then
|
yellowfive@57
|
47 label:SetPoint("LEFT", image, "RIGHT", 4, 0)
|
yellowfive@57
|
48 else
|
yellowfive@57
|
49 label:SetPoint("TOPLEFT", image, "TOPRIGHT", 4, 0)
|
yellowfive@57
|
50 end
|
yellowfive@57
|
51 label:SetWidth(width - imagewidth - 4)
|
yellowfive@57
|
52 height = max(image:GetHeight(), label:GetHeight())
|
yellowfive@57
|
53 end
|
yellowfive@57
|
54 else
|
yellowfive@57
|
55 -- no image shown
|
yellowfive@57
|
56 label:SetPoint("TOPLEFT")
|
yellowfive@57
|
57 label:SetWidth(width)
|
yellowfive@57
|
58 height = label:GetHeight()
|
yellowfive@57
|
59 end
|
yellowfive@57
|
60
|
yellowfive@57
|
61 self.resizing = true
|
yellowfive@57
|
62 frame:SetHeight(height)
|
yellowfive@57
|
63 frame.height = height
|
yellowfive@57
|
64 self.resizing = nil
|
yellowfive@57
|
65 end
|
yellowfive@57
|
66
|
yellowfive@57
|
67 --[[-----------------------------------------------------------------------------
|
yellowfive@57
|
68 Methods
|
yellowfive@57
|
69 -------------------------------------------------------------------------------]]
|
yellowfive@57
|
70 local methods = {
|
yellowfive@57
|
71 ["OnAcquire"] = function(self)
|
yellowfive@57
|
72 -- set the flag to stop constant size updates
|
yellowfive@57
|
73 self.resizing = true
|
yellowfive@57
|
74 -- height is set dynamically by the text and image size
|
yellowfive@57
|
75 self:SetWidth(200)
|
yellowfive@57
|
76 self:SetText()
|
yellowfive@57
|
77 self:SetImage(nil)
|
yellowfive@57
|
78 self:SetImageSize(16, 16)
|
yellowfive@57
|
79 self:SetColor()
|
yellowfive@57
|
80 self:SetFontObject()
|
yellowfive@106
|
81 self:SetJustifyH("LEFT")
|
yellowfive@106
|
82 self:SetJustifyV("TOP")
|
yellowfive@57
|
83
|
yellowfive@57
|
84 -- reset the flag
|
yellowfive@57
|
85 self.resizing = nil
|
yellowfive@57
|
86 -- run the update explicitly
|
yellowfive@57
|
87 UpdateImageAnchor(self)
|
yellowfive@57
|
88 end,
|
yellowfive@57
|
89
|
yellowfive@57
|
90 -- ["OnRelease"] = nil,
|
yellowfive@57
|
91
|
yellowfive@57
|
92 ["OnWidthSet"] = function(self, width)
|
yellowfive@57
|
93 UpdateImageAnchor(self)
|
yellowfive@57
|
94 end,
|
yellowfive@57
|
95
|
yellowfive@57
|
96 ["SetText"] = function(self, text)
|
yellowfive@57
|
97 self.label:SetText(text)
|
yellowfive@57
|
98 UpdateImageAnchor(self)
|
yellowfive@57
|
99 end,
|
yellowfive@57
|
100
|
yellowfive@57
|
101 ["SetColor"] = function(self, r, g, b)
|
yellowfive@57
|
102 if not (r and g and b) then
|
yellowfive@57
|
103 r, g, b = 1, 1, 1
|
yellowfive@57
|
104 end
|
yellowfive@57
|
105 self.label:SetVertexColor(r, g, b)
|
yellowfive@57
|
106 end,
|
yellowfive@57
|
107
|
yellowfive@57
|
108 ["SetImage"] = function(self, path, ...)
|
yellowfive@57
|
109 local image = self.image
|
yellowfive@57
|
110 image:SetTexture(path)
|
yellowfive@57
|
111
|
yellowfive@57
|
112 if image:GetTexture() then
|
yellowfive@57
|
113 self.imageshown = true
|
yellowfive@57
|
114 local n = select("#", ...)
|
yellowfive@57
|
115 if n == 4 or n == 8 then
|
yellowfive@57
|
116 image:SetTexCoord(...)
|
yellowfive@57
|
117 else
|
yellowfive@57
|
118 image:SetTexCoord(0, 1, 0, 1)
|
yellowfive@57
|
119 end
|
yellowfive@57
|
120 else
|
yellowfive@57
|
121 self.imageshown = nil
|
yellowfive@57
|
122 end
|
yellowfive@57
|
123 UpdateImageAnchor(self)
|
yellowfive@57
|
124 end,
|
yellowfive@57
|
125
|
yellowfive@57
|
126 ["SetFont"] = function(self, font, height, flags)
|
yellowfive@57
|
127 self.label:SetFont(font, height, flags)
|
yellowfive@57
|
128 end,
|
yellowfive@57
|
129
|
yellowfive@57
|
130 ["SetFontObject"] = function(self, font)
|
yellowfive@57
|
131 self:SetFont((font or GameFontHighlightSmall):GetFont())
|
yellowfive@57
|
132 end,
|
yellowfive@57
|
133
|
yellowfive@57
|
134 ["SetImageSize"] = function(self, width, height)
|
yellowfive@57
|
135 self.image:SetWidth(width)
|
yellowfive@57
|
136 self.image:SetHeight(height)
|
yellowfive@57
|
137 UpdateImageAnchor(self)
|
yellowfive@57
|
138 end,
|
yellowfive@106
|
139
|
yellowfive@106
|
140 ["SetJustifyH"] = function(self, justifyH)
|
yellowfive@106
|
141 self.label:SetJustifyH(justifyH)
|
yellowfive@106
|
142 end,
|
yellowfive@106
|
143
|
yellowfive@106
|
144 ["SetJustifyV"] = function(self, justifyV)
|
yellowfive@106
|
145 self.label:SetJustifyV(justifyV)
|
yellowfive@106
|
146 end,
|
yellowfive@57
|
147 }
|
yellowfive@57
|
148
|
yellowfive@57
|
149 --[[-----------------------------------------------------------------------------
|
yellowfive@57
|
150 Constructor
|
yellowfive@57
|
151 -------------------------------------------------------------------------------]]
|
yellowfive@57
|
152 local function Constructor()
|
yellowfive@57
|
153 local frame = CreateFrame("Frame", nil, UIParent)
|
yellowfive@57
|
154 frame:Hide()
|
yellowfive@57
|
155
|
yellowfive@57
|
156 local label = frame:CreateFontString(nil, "BACKGROUND", "GameFontHighlightSmall")
|
yellowfive@57
|
157 local image = frame:CreateTexture(nil, "BACKGROUND")
|
yellowfive@57
|
158
|
yellowfive@57
|
159 -- create widget
|
yellowfive@57
|
160 local widget = {
|
yellowfive@57
|
161 label = label,
|
yellowfive@57
|
162 image = image,
|
yellowfive@57
|
163 frame = frame,
|
yellowfive@57
|
164 type = Type
|
yellowfive@57
|
165 }
|
yellowfive@57
|
166 for method, func in pairs(methods) do
|
yellowfive@57
|
167 widget[method] = func
|
yellowfive@57
|
168 end
|
yellowfive@57
|
169
|
yellowfive@57
|
170 return AceGUI:RegisterAsWidget(widget)
|
yellowfive@57
|
171 end
|
yellowfive@57
|
172
|
yellowfive@57
|
173 AceGUI:RegisterWidgetType(Type, Constructor, Version)
|