comparison Libs/AceGUI-3.0/widgets/AceGUIWidget-Label.lua @ 57:01b63b8ed811 v21

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