vanilla-wow-addons – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | -------------------------------------------------------- |
2 | -- Nurfed Dynamic Frame Creation |
||
3 | -------------------------------------------------------- |
||
4 | |||
5 | if (not Nurfed_Frames) then |
||
6 | |||
7 | Nurfed_Frames = {}; |
||
8 | |||
9 | Nurfed_Frames.templates = {}; |
||
10 | Nurfed_Frames.init = { |
||
11 | size = function(object, value) object:SetWidth(value[1]) object:SetHeight(value[2]) end, |
||
12 | events = function(object, value) for k, v in value do object:RegisterEvent(v) end end, |
||
13 | children = function(object, value) for k, v in value do if not object:GetName() then return end Nurfed_Frames:CreateObject(object:GetName()..k, v, object, k) end end, |
||
14 | events = function(object, value) for k, v in value do object:RegisterEvent(v) end end, |
||
15 | vars = function(object, value) for k, v in value do object[k] = v end end, |
||
16 | Hide = function(object, value) object:Hide() end, |
||
17 | |||
18 | -- Run after creation |
||
19 | Anchor = function(object, value) table.insert(Nurfed_Frames.complete.Anchors, { object, value }) end, |
||
20 | BackdropColor = function(object, value) table.insert(Nurfed_Frames.complete.BackdropColor, { object, value }) end, |
||
21 | BackdropBorderColor = function(object, value) table.insert(Nurfed_Frames.complete.BackdropBorderColor, { object, value }) end, |
||
22 | }; |
||
23 | Nurfed_Frames.complete = { |
||
24 | Anchors = {}, |
||
25 | BackdropColor = {}, |
||
26 | BackdropBorderColor = {}, |
||
27 | }; |
||
28 | |||
29 | function Nurfed_Frames:New() |
||
30 | local object = {}; |
||
31 | setmetatable(object, self); |
||
32 | self.__index = self; |
||
33 | return object; |
||
34 | end |
||
35 | |||
36 | function Nurfed_Frames:SetProperty(object, value, prop) |
||
37 | local method; |
||
38 | if (object["Set"..prop]) then |
||
39 | method = object["Set"..prop]; |
||
40 | if (type(value) == "table" and prop ~= "Backdrop") then |
||
41 | method(object, unpack(value)); |
||
42 | else |
||
43 | method(object, value); |
||
44 | end |
||
45 | elseif (object["Enable"..prop]) then |
||
46 | method = object["Enable"..prop]; |
||
47 | method(object, value); |
||
48 | end |
||
49 | end |
||
50 | |||
51 | function Nurfed_Frames:CreateTemplate(name, spec) |
||
52 | self.templates[name] = spec; |
||
53 | end |
||
54 | |||
55 | function Nurfed_Frames:ObjectInit(name, layout, parent) |
||
56 | if (not name or not layout) then |
||
57 | return; |
||
58 | end |
||
59 | if (not parent) then |
||
60 | parent = UIParent; |
||
61 | end |
||
62 | local object = self:CreateObject(name, layout, parent); |
||
63 | self:CompleteObject(); |
||
64 | return object; |
||
65 | end |
||
66 | |||
67 | function Nurfed_Frames:CreateObject(name, layout, parent, apply) |
||
68 | if (type(parent) == "string") then |
||
69 | parent = getglobal(parent); |
||
70 | end |
||
71 | if (type(layout) == "string") then |
||
72 | layout = self.templates[layout]; |
||
73 | end |
||
74 | |||
75 | local spec = layout; |
||
76 | if (layout.template) then |
||
77 | spec = self.templates[layout.template]; |
||
78 | end |
||
79 | local object; |
||
80 | local objtype = rawget(spec, "type"); |
||
81 | if (objtype == "Texture") then |
||
82 | object = parent:CreateTexture(name, spec.layer); |
||
83 | elseif (objtype == "FontString") then |
||
84 | object = parent:CreateFontString(name, spec.layer); |
||
85 | else |
||
86 | object = CreateFrame(objtype, name, parent); |
||
87 | end |
||
88 | |||
89 | for k, v in pairs(spec) do |
||
90 | if ((type(v) == "function") and string.find(k,"^On")) then |
||
91 | object:SetScript(k, v); |
||
92 | elseif (self.init[k]) then |
||
93 | local value = v; |
||
94 | if (type(v) == "table" and v.template) then |
||
95 | value = self.templates[v.template]; |
||
96 | end |
||
97 | self.init[k](object, value); |
||
98 | else |
||
99 | local value = v; |
||
100 | if (type(v) == "table" and v.template) then |
||
101 | value = self.templates[v.template]; |
||
102 | end |
||
103 | self:SetProperty(object, value, k); |
||
104 | end |
||
105 | end |
||
106 | |||
107 | if (layout.properties) then |
||
108 | for k, v in pairs(layout.properties) do |
||
109 | if ((type(v) == "function") and string.find(k,"^On")) then |
||
110 | object:SetScript(k, v); |
||
111 | elseif (self.init[k]) then |
||
112 | local value = v; |
||
113 | if (type(v) == "table" and v.template) then |
||
114 | value = self.templates[v.template]; |
||
115 | end |
||
116 | self.init[k](object, value); |
||
117 | else |
||
118 | local value = v; |
||
119 | if (type(v) == "table" and v.template) then |
||
120 | value = self.templates[v.template]; |
||
121 | end |
||
122 | self:SetProperty(object, value, k); |
||
123 | end |
||
124 | end |
||
125 | end |
||
126 | |||
127 | if (apply and string.find(apply, "Texture", 1, true)) then |
||
128 | local method = parent["Set"..apply]; |
||
129 | if (method) then |
||
130 | method(parent, object); |
||
131 | end |
||
132 | end |
||
133 | |||
134 | return object; |
||
135 | end |
||
136 | |||
137 | function Nurfed_Frames:CompleteObject() |
||
138 | for _, v in pairs(self.complete.Anchors) do |
||
139 | v[1]:ClearAllPoints(); |
||
140 | if (type(v[2]) ~= "table") then |
||
141 | v[1]:SetAllPoints(v[1]:GetParent()); |
||
142 | else |
||
143 | local parent = string.gsub(v[2][2], "$parent", v[1]:GetParent():GetName()); |
||
144 | v[1]:SetPoint(v[2][1], parent, v[2][3], v[2][4], v[2][5]); |
||
145 | end |
||
146 | end |
||
147 | self.complete.Anchors = {}; |
||
148 | |||
149 | for _, v in pairs(self.complete.BackdropColor) do |
||
150 | v[1]:SetBackdropColor(unpack(v[2])); |
||
151 | end |
||
152 | self.complete.BackdropColor = {}; |
||
153 | |||
154 | for _, v in pairs(self.complete.BackdropBorderColor) do |
||
155 | v[1]:SetBackdropBorderColor(unpack(v[2])); |
||
156 | end |
||
157 | self.complete.BackdropColor = {}; |
||
158 | end |
||
159 | end |