-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrimeGui_Object.lua
More file actions
114 lines (86 loc) · 2.51 KB
/
Copy pathPrimeGui_Object.lua
File metadata and controls
114 lines (86 loc) · 2.51 KB
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
--[[
====================================================
LibPrime
Author: Ivan Leben
====================================================
--]]
--Object
--=============================================================
function PrimeGui.Object_New( o )
o.Init = PrimeGui.Object_Init;
o.Free = PrimeGui.Object_Free;
o.RegisterScript = PrimeGui.Object_RegisterScript;
o.UnregisterScript = PrimeGui.Object_UnregisterScript;
o.UnregisterAllScripts = PrimeGui.Object_UnregisterAllScripts;
o.InvokeScript = PrimeGui.Object_InvokeScript;
return o;
end
function PrimeGui.Object_Init( o )
end
function PrimeGui.Object_Free( o )
end
function PrimeGui.Object_RegisterScript( object, script, func )
--Create callback table if missing
if (object.callbacks == nil) then
object.callbacks = {};
end
if (object.callbacks[ script ] == nil) then
object.callbacks[ script ] = {};
end
--Check if function already registered
local callbacks = object.callbacks[ script ];
for i,f in ipairs( callbacks ) do
if (f == func) then
return
end
end
--Add to callback table
table.insert( callbacks, func );
--Check for missing script handler
if (object:GetScript( script ) == nil) then
--Closure passes script name to invoke function along with other arguments
local scriptClosure = function( object, ... )
PrimeGui.Object_InvokeScript( object, script, ... );
end
--Set closure as script handler
object:SetScript( script, scriptClosure );
end
end
function PrimeGui.Object_UnregisterScript( object, script, func )
--Must have valid callback table
if (object.callbacks == nil) then
return;
end
if (object.callbacks[ script ] == nil) then
return
end
--Search for registered function
local callbacks = object.callbacks[ script ];
for i,f in ipairs( callbacks ) do
if (f == func) then
--Remove from callback table
table.remove( callbacks, i );
end
end
end
function PrimeGui.Object_UnregisterAllScripts( object )
--Must have valid callback table
if (object.callbacks == nil) then
return;
end
--Remove all callbacks from every callback tables
for script,callbacks in pairs(object.callbacks) do
PrimeUtil.ClearTable( callbacks );
end
end
function PrimeGui.Object_InvokeScript( object, script, ... )
--Must have valid callback table
if (object.callbacks == nil) then
return;
end
--Invoke all the callback functions
local callbacks = object.callbacks[ script ];
for i,func in ipairs( callbacks ) do
func( object, ... );
end
end