annotate Libs/DF/label.lua @ 20:dc1c77254f80

- added close button to users panel. - framework update.
author Tercio
date Tue, 11 Aug 2015 12:46:46 -0300
parents 2f09fe4be15c
children dbd417f413a8
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@11 222 --> object constructor
Tercio@11 223 function DF:CreateLabel (parent, text, size, color, font, member, name, layer)
Tercio@11 224 return DF:NewLabel (parent, nil, name, member, text, font, size, color, layer)
Tercio@11 225 end
Tercio@11 226
Tercio@11 227 function DF:NewLabel (parent, container, name, member, text, font, size, color, layer)
Tercio@11 228
Tercio@11 229 if (not parent) then
Tercio@11 230 return nil
Tercio@11 231 end
Tercio@11 232 if (not container) then
Tercio@11 233 container = parent
Tercio@11 234 end
Tercio@11 235
Tercio@11 236 if (not name) then
Tercio@11 237 name = "DetailsFrameworkLabelNumber" .. DF.LabelNameCounter
Tercio@11 238 DF.LabelNameCounter = DF.LabelNameCounter + 1
Tercio@11 239 end
Tercio@11 240
Tercio@11 241 if (name:find ("$parent")) then
Tercio@11 242 local pname = parent:GetName()
Tercio@11 243 assert (pname, "label used $parent but parent has no name.")
Tercio@11 244 name = name:gsub ("$parent", parent:GetName())
Tercio@11 245 end
Tercio@11 246
Tercio@11 247 local LabelObject = {type = "label", dframework = true}
Tercio@11 248
Tercio@11 249 if (member) then
Tercio@11 250 parent [member] = LabelObject
Tercio@11 251 --container [member] = LabelObject.label
Tercio@11 252 end
Tercio@11 253
Tercio@11 254 if (parent.dframework) then
Tercio@11 255 parent = parent.widget
Tercio@11 256 end
Tercio@11 257 if (container.dframework) then
Tercio@11 258 container = container.widget
Tercio@11 259 end
Tercio@11 260
Tercio@11 261 font = font or "GameFontHighlightSmall"
Tercio@11 262
Tercio@11 263 LabelObject.label = parent:CreateFontString (name, layer or "OVERLAY", font)
Tercio@11 264 LabelObject.widget = LabelObject.label
Tercio@11 265
Tercio@11 266 LabelObject.label.MyObject = LabelObject
Tercio@11 267
Tercio@11 268 if (not APILabelFunctions) then
Tercio@11 269 APILabelFunctions = true
Tercio@11 270 local idx = getmetatable (LabelObject.label).__index
Tercio@11 271 for funcName, funcAddress in pairs (idx) do
Tercio@11 272 if (not LabelMetaFunctions [funcName]) then
Tercio@11 273 LabelMetaFunctions [funcName] = function (object, ...)
Tercio@20 274 local x = loadstring ( "return _G['"..object.label:GetName().."']:"..funcName.."(...)")
Tercio@11 275 return x (...)
Tercio@11 276 end
Tercio@11 277 end
Tercio@11 278 end
Tercio@11 279 end
Tercio@11 280
Tercio@11 281 LabelObject.label:SetText (text)
Tercio@11 282
Tercio@11 283 if (color) then
Tercio@11 284 local r, g, b, a = DF:ParseColors (color)
Tercio@11 285 LabelObject.label:SetTextColor (r, g, b, a)
Tercio@20 286 end
Tercio@20 287
Tercio@20 288 if (size and type (size) == "number") then
Tercio@20 289 DF:SetFontSize (LabelObject.label, size)
Tercio@20 290
Tercio@20 291 elseif (size and type (size) == "table") then
Tercio@20 292 local template = size
Tercio@20 293
Tercio@20 294 if (template.size) then
Tercio@20 295 DF:SetFontSize (LabelObject.label, template.size)
Tercio@20 296 end
Tercio@20 297 if (template.color) then
Tercio@20 298 local r, g, b, a = DF:ParseColors (template.color)
Tercio@20 299 LabelObject.label:SetTextColor (r, g, b, a)
Tercio@20 300 end
Tercio@20 301 if (template.font) then
Tercio@20 302 local SharedMedia = LibStub:GetLibrary ("LibSharedMedia-3.0")
Tercio@20 303 local font = SharedMedia:Fetch ("font", template.font)
Tercio@20 304 DF:SetFontFace (LabelObject.label, font)
Tercio@20 305 end
Tercio@11 306 end
Tercio@11 307
Tercio@20 308
Tercio@20 309
Tercio@11 310 LabelObject.label:SetJustifyH ("LEFT")
Tercio@11 311
Tercio@11 312 setmetatable (LabelObject, LabelMetaFunctions)
Tercio@11 313
Tercio@11 314 return LabelObject
Tercio@11 315 end