annotate Libs/DF/label.lua @ 22:dbd417f413a8

- framework update.
author Tercio
date Tue, 08 Sep 2015 13:16:49 -0300
parents dc1c77254f80
children 5da06cb420d4
rev   line source
Tercio@11 1
Tercio@11 2 local DF = _G ["DetailsFramework"]
Tercio@20 3 if (not DF or not DetailsFrameworkCanLoad) then
Tercio@20 4 return
Tercio@20 5 end
Tercio@20 6
Tercio@11 7 local _
Tercio@11 8 local _rawset = rawset --> lua local
Tercio@11 9 local _rawget = rawget --> lua local
Tercio@11 10 local _setmetatable = setmetatable --> lua local
Tercio@11 11 local _unpack = unpack --> lua local
Tercio@11 12 local _type = type --> lua local
Tercio@11 13 local _math_floor = math.floor --> lua local
Tercio@11 14 local loadstring = loadstring --> lua local
Tercio@11 15
Tercio@11 16 local cleanfunction = function() end
Tercio@11 17 local APILabelFunctions = false
Tercio@11 18 local LabelMetaFunctions = {}
Tercio@11 19
Tercio@11 20 ------------------------------------------------------------------------------------------------------------
Tercio@11 21 --> metatables
Tercio@11 22
Tercio@11 23 LabelMetaFunctions.__call = function (_table, value)
Tercio@11 24 return self.label:SetText (value)
Tercio@11 25 end
Tercio@11 26
Tercio@11 27 ------------------------------------------------------------------------------------------------------------
Tercio@11 28 --> members
Tercio@11 29
Tercio@11 30 --> shown
Tercio@11 31 local gmember_shown = function (_object)
Tercio@11 32 return _object:IsShown()
Tercio@11 33 end
Tercio@11 34 --> frame width
Tercio@11 35 local gmember_width = function (_object)
Tercio@11 36 return _object.label:GetStringWidth()
Tercio@11 37 end
Tercio@11 38 --> frame height
Tercio@11 39 local gmember_height = function (_object)
Tercio@11 40 return _object.label:GetStringHeight()
Tercio@11 41 end
Tercio@11 42 --> text
Tercio@11 43 local gmember_text = function (_object)
Tercio@11 44 return _object.label:GetText()
Tercio@11 45 end
Tercio@11 46 --> text color
Tercio@11 47 local gmember_textcolor = function (_object)
Tercio@11 48 return _object.label:GetTextColor()
Tercio@11 49 end
Tercio@11 50 --> text font
Tercio@11 51 local gmember_textfont = function (_object)
Tercio@11 52 local fontface = _object.label:GetFont()
Tercio@11 53 return fontface
Tercio@11 54 end
Tercio@11 55 --> text size
Tercio@11 56 local gmember_textsize = function (_object)
Tercio@11 57 local _, fontsize = _object.label:GetFont()
Tercio@11 58 return fontsize
Tercio@11 59 end
Tercio@11 60
Tercio@11 61
Tercio@11 62 local get_members_function_index = {
Tercio@11 63 ["shown"] = gmember_shown,
Tercio@11 64 ["width"] = gmember_width,
Tercio@11 65 ["height"] = gmember_height,
Tercio@11 66 ["text"] = gmember_text,
Tercio@11 67 ["fontcolor"] = gmember_textcolor,
Tercio@11 68 ["fontface"] = gmember_textfont,
Tercio@11 69 ["fontsize"] = gmember_textsize,
Tercio@11 70 ["textcolor"] = gmember_textcolor, --alias
Tercio@11 71 ["textfont"] = gmember_textfont, --alias
Tercio@11 72 ["textsize"] = gmember_textsize --alias
Tercio@11 73 }
Tercio@11 74
Tercio@11 75 LabelMetaFunctions.__index = function (_table, _member_requested)
Tercio@11 76
Tercio@11 77 local func = get_members_function_index [_member_requested]
Tercio@11 78 if (func) then
Tercio@11 79 return func (_table, _member_requested)
Tercio@11 80 end
Tercio@11 81
Tercio@11 82 local fromMe = _rawget (_table, _member_requested)
Tercio@11 83 if (fromMe) then
Tercio@11 84 return fromMe
Tercio@11 85 end
Tercio@11 86
Tercio@11 87 return LabelMetaFunctions [_member_requested]
Tercio@11 88 end
Tercio@11 89
Tercio@11 90 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Tercio@11 91
Tercio@11 92 --> show
Tercio@11 93 local smember_show = function (_object, _value)
Tercio@11 94 if (_value) then
Tercio@11 95 return _object:Show()
Tercio@11 96 else
Tercio@11 97 return _object:Hide()
Tercio@11 98 end
Tercio@11 99 end
Tercio@11 100 --> hide
Tercio@11 101 local smember_hide = function (_object, _value)
Tercio@11 102 if (not _value) then
Tercio@11 103 return _object:Show()
Tercio@11 104 else
Tercio@11 105 return _object:Hide()
Tercio@11 106 end
Tercio@11 107 end
Tercio@11 108 --> text
Tercio@11 109 local smember_text = function (_object, _value)
Tercio@11 110 return _object.label:SetText (_value)
Tercio@11 111 end
Tercio@11 112 --> text color
Tercio@11 113 local smember_textcolor = function (_object, _value)
Tercio@11 114 local _value1, _value2, _value3, _value4 = DF:ParseColors (_value)
Tercio@11 115 return _object.label:SetTextColor (_value1, _value2, _value3, _value4)
Tercio@11 116 end
Tercio@11 117 --> text font
Tercio@11 118 local smember_textfont = function (_object, _value)
Tercio@11 119 return DF:SetFontFace (_object.label, _value)
Tercio@11 120 end
Tercio@11 121 --> text size
Tercio@11 122 local smember_textsize = function (_object, _value)
Tercio@11 123 return DF:SetFontSize (_object.label, _value)
Tercio@11 124 end
Tercio@11 125 --> text align
Tercio@11 126 local smember_textalign = function (_object, _value)
Tercio@11 127 if (_value == "<") then
Tercio@11 128 _value = "left"
Tercio@11 129 elseif (_value == ">") then
Tercio@11 130 _value = "right"
Tercio@11 131 elseif (_value == "|") then
Tercio@11 132 _value = "center"
Tercio@11 133 end
Tercio@11 134 return _object.label:SetJustifyH (_value)
Tercio@11 135 end
Tercio@11 136 --> text valign
Tercio@11 137 local smember_textvalign = function (_object, _value)
Tercio@11 138 if (_value == "^") then
Tercio@11 139 _value = "top"
Tercio@11 140 elseif (_value == "_") then
Tercio@11 141 _value = "bottom"
Tercio@11 142 elseif (_value == "|") then
Tercio@11 143 _value = "middle"
Tercio@11 144 end
Tercio@11 145 return _object.label:SetJustifyV (_value)
Tercio@11 146 end
Tercio@11 147 --> field size width
Tercio@11 148 local smember_width = function (_object, _value)
Tercio@11 149 return _object.label:SetWidth (_value)
Tercio@11 150 end
Tercio@11 151 --> field size height
Tercio@11 152 local smember_height = function (_object, _value)
Tercio@11 153 return _object.label:SetHeight (_value)
Tercio@11 154 end
Tercio@11 155 --> outline (shadow)
Tercio@11 156 local smember_outline = function (_object, _value)
Tercio@11 157 DF:SetFontOutline (_object.label, _value)
Tercio@11 158 end
Tercio@11 159
Tercio@11 160 local set_members_function_index = {
Tercio@11 161 ["show"] = smember_show,
Tercio@11 162 ["hide"] = smember_hide,
Tercio@11 163 ["align"] = smember_textalign,
Tercio@11 164 ["valign"] = smember_textvalign,
Tercio@11 165 ["text"] = smember_text,
Tercio@11 166 ["width"] = smember_width,
Tercio@11 167 ["height"] = smember_height,
Tercio@11 168 ["fontcolor"] = smember_textcolor,
Tercio@11 169 ["color"] = smember_textcolor,--alias
Tercio@11 170 ["fontface"] = smember_textfont,
Tercio@11 171 ["fontsize"] = smember_textsize,
Tercio@11 172 ["textcolor"] = smember_textcolor,--alias
Tercio@11 173 ["textfont"] = smember_textfont,--alias
Tercio@11 174 ["textsize"] = smember_textsize,--alias
Tercio@11 175 ["shadow"] = smember_outline,
Tercio@11 176 ["outline"] = smember_outline,--alias
Tercio@11 177 }
Tercio@11 178
Tercio@11 179 LabelMetaFunctions.__newindex = function (_table, _key, _value)
Tercio@11 180 local func = set_members_function_index [_key]
Tercio@11 181 if (func) then
Tercio@11 182 return func (_table, _value)
Tercio@11 183 else
Tercio@11 184 return _rawset (_table, _key, _value)
Tercio@11 185 end
Tercio@11 186 end
Tercio@11 187
Tercio@11 188 ------------------------------------------------------------------------------------------------------------
Tercio@11 189 --> methods
Tercio@11 190
Tercio@11 191 --> show & hide
Tercio@11 192 function LabelMetaFunctions:IsShown()
Tercio@11 193 return self.label:IsShown()
Tercio@11 194 end
Tercio@11 195 function LabelMetaFunctions:Show()
Tercio@11 196 return self.label:Show()
Tercio@11 197 end
Tercio@11 198 function LabelMetaFunctions:Hide()
Tercio@11 199 return self.label:Hide()
Tercio@11 200 end
Tercio@11 201
Tercio@11 202 -- textcolor
Tercio@11 203 function LabelMetaFunctions:SetTextColor (color, arg2, arg3, arg4)
Tercio@11 204 if (arg2) then
Tercio@11 205 return self.label:SetTextColor (color, arg2, arg3, arg4 or 1)
Tercio@11 206 end
Tercio@11 207 local _value1, _value2, _value3, _value4 = DF:ParseColors (color)
Tercio@11 208 return self.label:SetTextColor (_value1, _value2, _value3, _value4)
Tercio@11 209 end
Tercio@11 210
Tercio@11 211 -- setpoint
Tercio@11 212 function LabelMetaFunctions:SetPoint (v1, v2, v3, v4, v5)
Tercio@11 213 v1, v2, v3, v4, v5 = DF:CheckPoints (v1, v2, v3, v4, v5, self)
Tercio@11 214 if (not v1) then
Tercio@11 215 print ("Invalid parameter for SetPoint")
Tercio@11 216 return
Tercio@11 217 end
Tercio@11 218 return self.widget:SetPoint (v1, v2, v3, v4, v5)
Tercio@11 219 end
Tercio@11 220
Tercio@11 221 ------------------------------------------------------------------------------------------------------------
Tercio@22 222
Tercio@22 223 function LabelMetaFunctions:SetTemplate (template)
Tercio@22 224 if (template.size) then
Tercio@22 225 DF:SetFontSize (self.label, template.size)
Tercio@22 226 end
Tercio@22 227 if (template.color) then
Tercio@22 228 local r, g, b, a = DF:ParseColors (template.color)
Tercio@22 229 self:SetTextColor (r, g, b, a)
Tercio@22 230 end
Tercio@22 231 if (template.font) then
Tercio@22 232 local SharedMedia = LibStub:GetLibrary ("LibSharedMedia-3.0")
Tercio@22 233 local font = SharedMedia:Fetch ("font", template.font)
Tercio@22 234 DF:SetFontFace (self.label, font)
Tercio@22 235 end
Tercio@22 236 end
Tercio@22 237
Tercio@22 238 ------------------------------------------------------------------------------------------------------------
Tercio@11 239 --> object constructor
Tercio@11 240 function DF:CreateLabel (parent, text, size, color, font, member, name, layer)
Tercio@11 241 return DF:NewLabel (parent, nil, name, member, text, font, size, color, layer)
Tercio@11 242 end
Tercio@11 243
Tercio@11 244 function DF:NewLabel (parent, container, name, member, text, font, size, color, layer)
Tercio@11 245
Tercio@11 246 if (not parent) then
Tercio@11 247 return nil
Tercio@11 248 end
Tercio@11 249 if (not container) then
Tercio@11 250 container = parent
Tercio@11 251 end
Tercio@11 252
Tercio@11 253 if (not name) then
Tercio@11 254 name = "DetailsFrameworkLabelNumber" .. DF.LabelNameCounter
Tercio@11 255 DF.LabelNameCounter = DF.LabelNameCounter + 1
Tercio@11 256 end
Tercio@11 257
Tercio@11 258 if (name:find ("$parent")) then
Tercio@11 259 local pname = parent:GetName()
Tercio@11 260 assert (pname, "label used $parent but parent has no name.")
Tercio@11 261 name = name:gsub ("$parent", parent:GetName())
Tercio@11 262 end
Tercio@11 263
Tercio@11 264 local LabelObject = {type = "label", dframework = true}
Tercio@11 265
Tercio@11 266 if (member) then
Tercio@11 267 parent [member] = LabelObject
Tercio@11 268 --container [member] = LabelObject.label
Tercio@11 269 end
Tercio@11 270
Tercio@11 271 if (parent.dframework) then
Tercio@11 272 parent = parent.widget
Tercio@11 273 end
Tercio@11 274 if (container.dframework) then
Tercio@11 275 container = container.widget
Tercio@11 276 end
Tercio@11 277
Tercio@11 278 font = font or "GameFontHighlightSmall"
Tercio@11 279
Tercio@11 280 LabelObject.label = parent:CreateFontString (name, layer or "OVERLAY", font)
Tercio@11 281 LabelObject.widget = LabelObject.label
Tercio@11 282
Tercio@11 283 LabelObject.label.MyObject = LabelObject
Tercio@11 284
Tercio@11 285 if (not APILabelFunctions) then
Tercio@11 286 APILabelFunctions = true
Tercio@11 287 local idx = getmetatable (LabelObject.label).__index
Tercio@11 288 for funcName, funcAddress in pairs (idx) do
Tercio@11 289 if (not LabelMetaFunctions [funcName]) then
Tercio@11 290 LabelMetaFunctions [funcName] = function (object, ...)
Tercio@20 291 local x = loadstring ( "return _G['"..object.label:GetName().."']:"..funcName.."(...)")
Tercio@11 292 return x (...)
Tercio@11 293 end
Tercio@11 294 end
Tercio@11 295 end
Tercio@11 296 end
Tercio@11 297
Tercio@11 298 LabelObject.label:SetText (text)
Tercio@11 299
Tercio@11 300 if (color) then
Tercio@11 301 local r, g, b, a = DF:ParseColors (color)
Tercio@11 302 LabelObject.label:SetTextColor (r, g, b, a)
Tercio@20 303 end
Tercio@20 304
Tercio@20 305 if (size and type (size) == "number") then
Tercio@20 306 DF:SetFontSize (LabelObject.label, size)
Tercio@11 307 end
Tercio@11 308
Tercio@20 309
Tercio@20 310
Tercio@11 311 LabelObject.label:SetJustifyH ("LEFT")
Tercio@11 312
Tercio@11 313 setmetatable (LabelObject, LabelMetaFunctions)
Tercio@22 314
Tercio@22 315 if (size and type (size) == "table") then
Tercio@22 316 LabelObject:SetTemplate (size)
Tercio@22 317 end
Tercio@11 318
Tercio@11 319 return LabelObject
Tercio@11 320 end