-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPoncho-2.0.lua
187 lines (142 loc) · 4.4 KB
/
Poncho-2.0.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
--[[
Copyright 2011-2025 João Cardoso
Poncho is distributed under the terms of the GNU General Public License (or the Lesser GPL).
This file is part of Poncho.
Poncho is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Poncho is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Poncho. If not, see <http://www.gnu.org/licenses/>.
--]]
local Lib = LibStub:NewLibrary('Poncho-2.0', 4)
if not Lib then return end
local setmetatable, getmetatable, tinsert, tremove, type = setmetatable, getmetatable, tinsert, tremove, type
local Base = {__type = 'Abstract'}
local ClassMeta = {
__index = function(class, key)
return class.__super[key] or class.__base[key]
end,
__call = function(class, ...)
return class:New(...)
end
}
local SuperCall = {
__index = function(tmp, key)
local var = ClassMeta.__index(tmp.class, key) or tmp.frame.__base[key]
if type(var) == 'function' then
return function(tmp, ...) return var(tmp.frame, ...) end
end
return var
end
}
--[[ Static ]]--
function Base:NewClass(kind, name, template)
assert(not kind or type(kind) == 'string', 'Bad argument #1 to `:NewClass` (string or nil expected)')
assert(not name or type(name) == 'string', 'Bad argument #2 to `:NewClass` (string or nil expected)')
assert(not template or type(template) == 'string', 'Bad argument #3 to `:NewClass` (string or nil expected)')
if kind and not Lib.Types[kind] then
Lib.Types[kind] = getmetatable(CreateFrame(kind)).__index
end
local class = setmetatable({}, ClassMeta)
class.__index = class
class.__name = name
class.__type = kind
class.__super = self
class.__template = template
class.__base = Lib.Types[class.__type]
if class.__type ~= 'Abstract' then
class.__frames = {}
class.__count = 0
end
return class
end
function Base:New(parent)
assert(self.__type ~= 'Abstract', 'Cannot initialize frame for absract class')
local frame = tremove(self.__frames)
if not frame then
self.__count = self.__count + 1
frame = self:Construct()
end
self.__frames[frame] = true
frame:ClearAllPoints()
frame:SetParent(parent)
return frame
end
function Base:Construct()
return self:Bind(CreateFrame(self.__type, self.__name and (self.__name .. self.__count) or nil, UIParent, self.__template))
end
function Base:Bind(frame)
return setmetatable(frame, self)
end
--[[ Flexible ]]--
function Base:GetClassName()
return self.__name
end
function Base:GetSuper()
return self.__super
end
function Base:GetTemplate()
return self.__template
end
function Base:GetFrameType()
return self.__type
end
function Base:IsAbstract()
return self.__type == 'Abstract'
end
function Base:NumActive()
return self.__count - #self.__frames
end
function Base:NumInactive()
return #self.__frames
end
function Base:NumFrames()
return self.__count
end
--[[ Instance ]]--
function Base:GetClass()
return getmetatable(self)
end
function Base:Release()
if self:IsActive() then
tinsert(self.__frames, 1, self)
self.__frames[self] = nil
self:Reset()
end
end
function Base:Reset()
self:SetParent(UIParent)
self:ClearAllPoints()
self:SetPoint('BOTTOM', UIParent, 'TOP', 0, GetScreenHeight()) -- twice outside of screen
end
function Base:IsActive()
return self.__frames[self]
end
function Base:Super(class)
assert(class, 'No argument #1 to `:Super`')
return setmetatable({class = class, frame = self}, SuperCall)
end
--[[ Public ]]--
function Lib:NewClass(kind, name, template)
return Base:NewClass(kind, name, template)
end
function Lib:Embed(object)
object.NewClass = Lib.NewClass
end
--[[ Properties ]]--
setmetatable(Lib, Lib)
Lib.__call = Lib.NewClass
Lib.Base, Lib.ClassMeta, Lib.SuperCall = Base, ClassMeta, SuperCall
Lib.Types = Lib.Types or {
Abstract = {},
Frame = getmetatable(GameMenuFrame).__index,
Button = getmetatable(ChatFrameChannelButton).__index,
CheckButton = getmetatable(AddonListForceLoad).__index,
EditBox = getmetatable(ChatFrame1EditBox).__index,
GameTooltip = getmetatable(GameTooltip).__index,
}