d39b6e67f40b23c10020c025870b992ff58a9722
[wowui.git] / OmaRF / Core.lua
1 local unpack = unpack;
2 local wipe = wipe;
3 local next = next;
4 local pairs = pairs;
5 local ipairs = ipairs;
6
7 OmaRF = CreateFrame("Frame");
8
9 OmaRF.normalBarColor = CreateColor(0.3, 0.3, 0.3);
10 OmaRF.dispelBarColor = CreateColor(1, 0.5, 0);
11 OmaRF.normalBackColor = {0.7, 0.7, 0.7};
12 OmaRF.dispelBackColor = {0.5, 0.2, 0};
13 OmaRF.frames = {};
14 OmaRF.majorFrames = {};
15 OmaRF.positions = {
16     "TOPLEFT", "TOPRIGHT", "CENTER", "BOTTOMLEFT", "BOTTOMRIGHT"
17 };
18 OmaRF.running = false;
19 OmaRF.ooc_queue = {};
20
21 local defaults = {
22     profile = {
23         enabled = true,
24         majorAuras = {
25             auras = {"Aqua Bomb"},
26             max = 3;
27             iconSize = 24,
28             textSize = 10,
29         },
30         indicators = {
31             ['**'] = {
32                 auras = {},
33                 textSize = 10,
34                 textColor = {1, 1, 1, 1},
35                 mine = false,
36                 stack = true,
37                 showText = true,
38                 showIcon = true,
39                 useDefaultIcon = true,
40                 iconSize = 10,
41                 iconColor = {1, 1, 1, 1},
42             },
43         },
44     }
45 };
46
47 function OmaRF:OnInitialize()
48     self.db = LibStub("AceDB-3.0"):New("OmaRFDB", defaults);
49     self.db.RegisterCallback(self, "OnProfileChanged", "RefreshConfig");
50     self.db.RegisterCallback(self, "OnProfileCopied", "RefreshConfig");
51     self.db.RegisterCallback(self, "OnProfileReset", "RefreshConfig");
52 end
53
54 function OmaRF:OnEnable()
55     self:RefreshConfig();
56 end
57
58 function OmaRF:OnDisable()
59     self.running = false;
60     for name, frame in pairs(self.frames) do
61         for _, ind in pairs(frame) do
62             ind.text:Hide();
63             ind.icon:Hide();
64         end
65         for _, ind in ipairs(self.majorFrames[name]) do
66             ind.icon:Hide();
67             ind.expireText:Hide();
68             ind.stackText:Hide();
69         end
70     end
71 end
72
73 local function onEvent(self, event, ...)
74     if event == "PLAYER_REGEN_ENABLED" then
75         for _, t in pairs(self.ooc_queue) do
76             t.func(unpack(t.args));
77         end
78         if next(self.ooc_queue) ~= nil then
79             wipe(self.ooc_queue);
80         end
81     elseif event == "PLAYER_LOGIN" then
82         self:OnEnable();
83     elseif event == "ADDON_LOADED" then
84         self:OnInitialize();
85     end
86 end
87
88 SLASH_OMARF1 = "/omarf";
89 function SlashCmdList.OMARF(msg, editBox)
90     local loaded, finished = IsAddOnLoaded("OmaRFConfig");
91     if not loaded then
92         local loaded, reason = LoadAddOn("OmaRFConfig");
93         if not loaded then
94             if reason == "DISABLED" then
95                 print("OmaRFConfig is disabled");
96             elseif reason == "MISSING" then
97                 print("OmaRFConfig is missing");
98             elseif reason == "CORRUPT" then
99                 print("OmaRFConfig is corrupt");
100             elseif reason == "INCOMPATIBLE" then
101                 print("OmaRFConfig is incompatible");
102             elseif reason == "INTERFACE_VERSION" then
103                 print("OmaRFConfig has wrong interface version");
104             end
105             return;
106         end
107     elseif not finished then
108         -- slash command sent again when loading process is in progress
109         return;
110     end
111
112     InterfaceOptionsFrame_OpenToCategory(OmaRF.optionsFrames.Profile);
113     InterfaceOptionsFrame_OpenToCategory(OmaRF.optionsFrames.Indicators);
114 end
115
116 OmaRF:RegisterEvent("ADDON_LOADED");
117 OmaRF:RegisterEvent("PLAYER_LOGIN");
118 OmaRF:RegisterEvent("PLAYER_REGEN_ENABLED");
119 OmaRF:SetScript("OnEvent", onEvent);