zz_MiscHelper/core.lua

130 lines
3.1 KiB
Lua

local name, addon = ...
local parentName = addon['parentName'] or "BrokerPack"
local childName = "zz_MiscHelper"
local defaults = {
['test'] = false,
['sellgrey'] = true,
['noduel'] = true,
['autorepair'] = false,
['hideclock'] = true,
}
local options = {
}
addon['mhmods'] = addon['mhmods'] or {}
local mods = {}
local events = {}
local registered = {}
local db = {}
local ldbicon = LibStub:GetLibrary("LibDBIcon-1.0")
local function OnClick(self, button)
if(IsShiftKeyDown() and button == "LeftButton") then
addon['db']['global']['ldbicons'][childName]['hide'] = not addon['db']['global']['ldbicons'][childName]['hide']
if(ldbicon) then
if(addon['db']['global']['ldbicons'][childName]['hide']) then
ldbicon:Hide(childName)
else
ldbicon:Show(childName)
end
end
else
if(InterfaceOptionsFrame:IsVisible() and not InCombatLockdown()) then
InterfaceOptionsFrame:Hide()
else
InterfaceOptionsFrame_OpenToCategory(childName)
InterfaceOptionsFrame_OpenToCategory(childName) -- Twice because of a bug in InterfaceOptionsFrame_OpenToCategory
end
end
end
local function eventHandler(self, event, ...)
for k,v in pairs(mods) do
if(type(events[v][event]) == 'function' and db[v]) then
events[v][event](event, ...)
end
end
end
local inited = {}
local function iterateModuls()
for _, modul in ipairs(addon['mhmods']) do
if((addon:IsClassic() and not modul['skipClassic']) or not addon:IsClassic()) then
mods[#mods + 1] = modul['name']
for _, event in ipairs(modul['events']) do
events[modul['name']] = { [event] = modul['onevent']}
if(not registered[event]) then
registered[event] = true
addon:RegisterFunc(event, "OnEvent", eventHandler)
end
end
addon:AddConfigEntry(childName, "toggle",modul['name'],modul['message'],nil,1,nil,nil,nil,nil,options['args'][childName])
if(type(modul['init']) == 'function' and db[modul['name']]) then
modul['init']()
inited[modul['name']] = true
end
end
end
end
addon['MH_GetPref'] = function(key)
return db[key]
end
addon['MH_GetDB'] = function(subTable, def)
if(not subTable) then return db end
def = def or {}
local key = subTable.."Settings"
db[key] = db[key] or CopyTable(def)
return db[key]
end
addon['MH_SetPref'] = function(key, value)
db[key] = value
end
addon['MH_GetOptions'] = function()
return options
end
local function getPref(pref)
return db[pref[#pref]]
end
local function setPref(pref,value)
db[pref[#pref]] = value
if(value and not inited[pref[#pref]]) then
for _, modul in ipairs(addon['mhmods']) do
if(modul['name'] == pref[#pref]) then
if(type(modul['init']) == 'function') then
modul['init']()
break
end
end
end
end
if(not value) then
for _, modul in ipairs(addon['mhmods']) do
if(modul['name'] == pref[#pref]) then
if(type(modul['disable']) == 'function') then
modul['disable']()
break
end
end
end
end
end
local function init()
options = addon:InitConfig(childName, true, {
['name'] = childName,
['typ'] = 'launcher',
['OnClick'] = OnClick,
}, getPref, setPref)
db = addon['db']['profile'][childName]
iterateModuls()
end
addon:startup(name, childName, init, false, defaults)