Mercurial > wow > icu
comparison Libs/AceGUI-3.0/widgets/AceGUIWidget-Label.lua @ 0:98c6f55e6619
First commit
| author | Xiiph |
|---|---|
| date | Sat, 05 Feb 2011 16:45:02 +0100 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:98c6f55e6619 |
|---|---|
| 1 --[[----------------------------------------------------------------------------- | |
| 2 Label Widget | |
| 3 Displays text and optionally an icon. | |
| 4 -------------------------------------------------------------------------------]] | |
| 5 local Type, Version = "Label", 21 | |
| 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 label:SetPoint("TOPLEFT", image, "TOPRIGHT", 4, 0) | |
| 47 label:SetWidth(width - imagewidth - 4) | |
| 48 height = max(image:GetHeight(), label:GetHeight()) | |
| 49 end | |
| 50 else | |
| 51 -- no image shown | |
| 52 label:SetPoint("TOPLEFT") | |
| 53 label:SetWidth(width) | |
| 54 height = label:GetHeight() | |
| 55 end | |
| 56 | |
| 57 self.resizing = true | |
| 58 frame:SetHeight(height) | |
| 59 frame.height = height | |
| 60 self.resizing = nil | |
| 61 end | |
| 62 | |
| 63 --[[----------------------------------------------------------------------------- | |
| 64 Methods | |
| 65 -------------------------------------------------------------------------------]] | |
| 66 local methods = { | |
| 67 ["OnAcquire"] = function(self) | |
| 68 -- set the flag to stop constant size updates | |
| 69 self.resizing = true | |
| 70 -- height is set dynamically by the text and image size | |
| 71 self:SetWidth(200) | |
| 72 self:SetText() | |
| 73 self:SetImage(nil) | |
| 74 self:SetImageSize(16, 16) | |
| 75 self:SetColor() | |
| 76 self:SetFontObject() | |
| 77 | |
| 78 -- reset the flag | |
| 79 self.resizing = nil | |
| 80 -- run the update explicitly | |
| 81 UpdateImageAnchor(self) | |
| 82 end, | |
| 83 | |
| 84 -- ["OnRelease"] = nil, | |
| 85 | |
| 86 ["OnWidthSet"] = function(self, width) | |
| 87 UpdateImageAnchor(self) | |
| 88 end, | |
| 89 | |
| 90 ["SetText"] = function(self, text) | |
| 91 self.label:SetText(text) | |
| 92 UpdateImageAnchor(self) | |
| 93 end, | |
| 94 | |
| 95 ["SetColor"] = function(self, r, g, b) | |
| 96 if not (r and g and b) then | |
| 97 r, g, b = 1, 1, 1 | |
| 98 end | |
| 99 self.label:SetVertexColor(r, g, b) | |
| 100 end, | |
| 101 | |
| 102 ["SetImage"] = function(self, path, ...) | |
| 103 local image = self.image | |
| 104 image:SetTexture(path) | |
| 105 | |
| 106 if image:GetTexture() then | |
| 107 self.imageshown = true | |
| 108 local n = select("#", ...) | |
| 109 if n == 4 or n == 8 then | |
| 110 image:SetTexCoord(...) | |
| 111 else | |
| 112 image:SetTexCoord(0, 1, 0, 1) | |
| 113 end | |
| 114 else | |
| 115 self.imageshown = nil | |
| 116 end | |
| 117 UpdateImageAnchor(self) | |
| 118 end, | |
| 119 | |
| 120 ["SetFont"] = function(self, font, height, flags) | |
| 121 self.label:SetFont(font, height, flags) | |
| 122 end, | |
| 123 | |
| 124 ["SetFontObject"] = function(self, font) | |
| 125 self:SetFont((font or GameFontHighlightSmall):GetFont()) | |
| 126 end, | |
| 127 | |
| 128 ["SetImageSize"] = function(self, width, height) | |
| 129 self.image:SetWidth(width) | |
| 130 self.image:SetHeight(height) | |
| 131 UpdateImageAnchor(self) | |
| 132 end, | |
| 133 } | |
| 134 | |
| 135 --[[----------------------------------------------------------------------------- | |
| 136 Constructor | |
| 137 -------------------------------------------------------------------------------]] | |
| 138 local function Constructor() | |
| 139 local frame = CreateFrame("Frame", nil, UIParent) | |
| 140 frame:Hide() | |
| 141 | |
| 142 local label = frame:CreateFontString(nil, "BACKGROUND", "GameFontHighlightSmall") | |
| 143 label:SetJustifyH("LEFT") | |
| 144 label:SetJustifyV("TOP") | |
| 145 | |
| 146 local image = frame:CreateTexture(nil, "BACKGROUND") | |
| 147 | |
| 148 -- create widget | |
| 149 local widget = { | |
| 150 label = label, | |
| 151 image = image, | |
| 152 frame = frame, | |
| 153 type = Type | |
| 154 } | |
| 155 for method, func in pairs(methods) do | |
| 156 widget[method] = func | |
| 157 end | |
| 158 | |
| 159 return AceGUI:RegisterAsWidget(widget) | |
| 160 end | |
| 161 | |
| 162 AceGUI:RegisterWidgetType(Type, Constructor, Version) |
