-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmodules.lua
188 lines (165 loc) · 5.16 KB
/
modules.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
188
local addon, ns = ...;
local L = ns.L;
local pairs,type=pairs,type;
ns.modules = {};
local queue,queue_ticker = {};
local function queue_ticker_func()
local mod
if #queue>0 then
mod = ns.modules[tremove(queue,1)];
mod.onevent(mod.eventFrame,"PLAYER_LOGIN");
elseif queue_ticker then
queue_ticker:Cancel();
queue_ticker = nil;
end
end
function ns.modulePLQueueInit()
queue_ticker = C_Timer.NewTicker(0.001,queue_ticker_func);
ns.modulePLQueueInit = nil;
end
function ns.updateMinimapButton(modName)
local mod = ns.modules[modName];
local cfg = ns.profile[modName];
-- check config
if type(cfg.minimap)~="table" then
cfg.minimap = {hide=true};
end
if ns.LDBI:IsRegistered(mod.ldbName) then
-- perform refresh on minimap button if already exists
ns.LDBI:Refresh(mod.ldbName);
elseif not cfg.minimap.hide then
-- register minimap button if not exists
ns.LDBI:Register(mod.ldbName,mod.obj,cfg.minimap);
end
end
-- [force] - used by child modules to enable core module (like gps and followers)
local function moduleInit(name, force)
if not ns.modules[name] then return end
if ns.profile[name] and (ns.profile[name].enabled==true or force) then
local mod = ns.modules[name];
mod.isEnabled = true;
-- module init
if mod.init then
mod.init();
mod.init = nil;
end
if not mod.isHiddenModule then
-- new clickOptions system
local onclick;
if (type(mod.clickOptions)=="table") then
local active = ns.ClickOpts.update(name);
if active then
function onclick(self,button)
ns.ClickOpts.func(self,button,name);
end
end
elseif (type(mod.onclick)=="function") then
onclick = mod.onclick;
end
-- LDB init
if (not mod.onenter) and mod.ontooltip then
mod.ontooltipshow = mod.ontooltip;
end
if not mod.iconName then
mod.iconName = name;
end
local icon = ns.I(mod.iconName .. (mod.icon_suffix or ""));
local iColor = ns.profile.GeneralOptions.iconcolor;
local ldbOblectTable = {
-- button data
type = "data source",
label = L[name],
text = L[name],
icon = icon.iconfile, -- default or custom icon
staticIcon = icon.staticIcon or icon.iconfile, -- default icon only
iconCoords = icon.coords or {0, 1, 0, 1},
-- button event functions
OnEnter = mod.onenter or nil,
OnLeave = mod.onleave or nil,
OnClick = onclick,
OnTooltipShow = mod.ontooltipshow or nil,
-- let user know who registered the broker
-- displayable by broker dispay addons...
-- DataBrokerGroups using it in option panel.
parent = addon
};
mod.ldbName = (ns.profile.GeneralOptions.usePrefix and "BE.." or "")..name;
mod.obj = ns.LDB:NewDataObject(mod.ldbName,ldbOblectTable);
if not mod.obj then -- another addon has used the same name for registration
mod.ldbName = "BE.."..mod.ldbName;
mod.obj = ns.LDB:NewDataObject(mod.ldbName,ldbOblectTable);
end
ns.updateIcons(name);
-- is possible that
if mod.obj then
-- register minimap button
ns.updateMinimapButton(name);
end
end
-- event handling
if (mod.onevent and mod.events) or (mod.onupdate) then
if not mod.noEventFrame then
mod.eventFrame=CreateFrame("Frame");
else
-- table as dummy to execute following BE_UPDATE_CFG, VARIABLES_LOADED and PLAYER_LOGIN only
mod.eventFrame={};
end
mod.eventFrame.modName = name;
if type(mod.onevent)=="function" and type(mod.events)=="table" then
mod.eventFrame:SetScript("OnEvent",mod.onevent);
mod.onevent(mod.eventFrame,"BE_UPDATE_CFG");
if mod.clickOptions then
mod.onevent(mod.eventFrame,"BE_UPDATE_CFG","ClickOpt");
end
for _, e in pairs(mod.events) do
if e=="VARIABLES_LOADED" then
mod.onevent(mod.eventFrame,e,addon);
elseif (e=="PLAYER_LOGIN" and ns.eventPlayerEnteredWorld) then
mod.onevent(mod.eventFrame,e);
end
if e=="PLAYER_LOGIN" then
tinsert(queue,name);
else
mod.eventFrame:RegisterEvent(e);
end
end
end
end
-- timeout function
if type(mod.ontimeout)=="function" and type(mod.timeout)=="number" and mod.timeout>0 then
C_Timer.After(mod.timeout,mod.ontimeout);
end
-- onupdate function
-- frame script OnUpdate ticking too fast... C_Timer is better
if type(mod.onupdate)=="function" and type(mod.onupdate_interval)=="number" and mod.onupdate_interval>0 then
mod.onupdate_ticker = C_Timer.NewTicker(mod.onupdate_interval,mod.onupdate);
end
-- chat command registration
if (mod.chatcommands) then
for k,v in pairs(mod.chatcommands) do
if (type(k)=="string") then
-- Example: ns.AddChatCommand("<string>",{desc="<string>",func=function()end});
ns.AddChatCommand(k,v);
end
end
end
end
end
-- in core.lua on event ADDON_LOADED or option panel
function ns.moduleInit(name,force)
if (name) then
-- enable module by name
ns.Options_RegisterModule(name);
ns.Options_RegisterDefaults();
moduleInit(name,force);
else
-- enable all registered modules
for name, data in pairs(ns.modules) do
ns.Options_RegisterModule(name);
end
ns.Options_RegisterDefaults();
for name, data in pairs(ns.modules) do
moduleInit(name);
end
end
end