what
[QuestHelper.git] / AstrolabeQH / AstrolabeMapMonitor.lua
blob3c56a5a17c8380f42c9722536671bee701d478f4
1 --[[
2 Name: AstrolabeMapMonitor
3 Revision: $Rev: 44 $
4 $Date: 2007-03-30 11:56:21 -0700 (Fri, 30 Mar 2007) $
5 Author(s): Esamynn (esamynn@wowinterface.com)
6 Inspired By: Gatherer by Norganna
7 MapLibrary by Kristofer Karlsson (krka@kth.se)
8 Website: http://esamynn.wowinterface.com/
9 Documentation: http://www.esamynn.org/wiki/Astrolabe/World_Map_Monitor
10 SVN: http://esamynn.org/svn/astrolabe/
11 Description:
12 This is a small stub library to support the main Astrolabe
13 library. It's purpose is to monitor the visibility of
14 various World Map frames, so that Astrolabe can modify its
15 behaviour accordingly.
17 Copyright (C) 2007 James Carrothers
19 License:
20 This library is free software; you can redistribute it and/or
21 modify it under the terms of the GNU Lesser General Public
22 License as published by the Free Software Foundation; either
23 version 2.1 of the License, or (at your option) any later version.
25 This library is distributed in the hope that it will be useful,
26 but WITHOUT ANY WARRANTY; without even the implied warranty of
27 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28 Lesser General Public License for more details.
30 You should have received a copy of the GNU Lesser General Public
31 License along with this library; if not, write to the Free Software
32 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
34 Note:
35 This library's source code is specifically designed to work with
36 World of Warcraft's interpreted AddOn system. You have an implicit
37 licence to use this library with these facilities since that is its
38 designated purpose as per:
39 http://www.fsf.org/licensing/licenses/gpl-faq.html#InterpreterIncompat
42 QuestHelper_File["AstrolabeQH/AstrolabeMapMonitor.lua"] = "Development Version"
43 QuestHelper_Loadtime["AstrolabeQH/AstrolabeMapMonitor.lua"] = GetTime()
45 -- WARNING!!!
46 -- DO NOT MAKE CHANGES TO THIS LIBRARY WITHOUT FIRST CHANGING THE LIBRARY_VERSION_MAJOR
47 -- STRING (to something unique) OR ELSE YOU MAY BREAK OTHER ADDONS THAT USE THIS LIBRARY!!!
48 local LIBRARY_VERSION_MAJOR = "AstrolabeMapMonitor"
49 local LIBRARY_VERSION_MINOR = tonumber(string.match("$Revision: 44 $", "(%d+)") or 1)
51 if not DongleStub then error(LIBRARY_VERSION_MAJOR .. " requires DongleStub.") end
52 if not DongleStub:IsNewerVersion(LIBRARY_VERSION_MAJOR, LIBRARY_VERSION_MINOR) then return end
54 local AstrolabeMapMonitor = {};
56 function AstrolabeMapMonitor:GetVersion()
57 return LIBRARY_VERSION_MAJOR, LIBRARY_VERSION_MINOR;
58 end
61 --------------------------------------------------------------------------------------------------------------
62 -- Global World Map Frame Registration Table
63 --------------------------------------------------------------------------------------------------------------
65 if ( type(WorldMapDisplayFrames) ~= "table" ) then
66 WorldMapDisplayFrames = { WorldMapFrame };
67 else
68 local worldMapFound = false;
69 for k, v in pairs(WorldMapDisplayFrames) do
70 if ( v == WorldMapFrame ) then
71 worldMapFound = true;
72 break;
73 end
74 end
75 if not ( worldMapFound ) then
76 table.insert(WorldMapDisplayFrames, WorldMapFrame);
77 end
78 end
81 --------------------------------------------------------------------------------------------------------------
82 -- Working Tables and Config Constants
83 --------------------------------------------------------------------------------------------------------------
85 AstrolabeMapMonitor.TrackedWorldMaps = {};
86 AstrolabeMapMonitor.AstrolabeLibrarys = {};
88 AstrolabeMapMonitor.NumVisibleWorldMaps = 0;
91 --------------------------------------------------------------------------------------------------------------
92 -- Monitor Frame Script Handlers
93 --------------------------------------------------------------------------------------------------------------
95 local function onShow( frame )
96 AstrolabeMapMonitor.NumVisibleWorldMaps = AstrolabeMapMonitor.NumVisibleWorldMaps + 1;
97 AstrolabeMapMonitor:Update()
98 end
100 local function onHide( frame )
101 AstrolabeMapMonitor.NumVisibleWorldMaps = AstrolabeMapMonitor.NumVisibleWorldMaps - 1;
102 AstrolabeMapMonitor:Update()
105 local function setScripts( monitorFrame )
106 QH_Hook( monitorFrame, "OnShow", onShow);
107 QH_Hook( monitorFrame, "OnHide", onHide);
111 --------------------------------------------------------------------------------------------------------------
112 -- Internal Utility Functions
113 --------------------------------------------------------------------------------------------------------------
115 local function assert(level,condition,message)
116 if not condition then
117 error(message,level)
121 local function argcheck(value, num, ...)
122 assert(1, type(num) == "number",
123 "Bad argument #2 to 'argcheck' (number expected, got " .. type(level) .. ")")
125 for i=1,select("#", ...) do
126 if type(value) == select(i, ...) then return end
129 local types = strjoin(", ", ...)
130 local name = string.match(debugstack(2,2,0), ": in function [`<](.-)['>]")
131 error(string.format("Bad argument #%d to 'AstrolabeMapMonitor.%s' (%s expected, got %s)", num, name, types, type(value)), 3)
135 --------------------------------------------------------------------------------------------------------------
136 -- Public API
137 --------------------------------------------------------------------------------------------------------------
139 function AstrolabeMapMonitor:MonitorWorldMap( worldMapFrame )
140 -- check argument types
141 argcheck(worldMapFrame, 2, "table");
142 assert((worldMapFrame.SetParent), "Usage Message");
144 local TrackedWorldMaps = self.TrackedWorldMaps;
145 if ( TrackedWorldMaps[worldMapFrame] ) then
146 return 1;
148 local monitorFrame = CreateFrame("Frame", nil, worldMapFrame);
149 TrackedWorldMaps[worldMapFrame] = monitorFrame;
150 setScripts(monitorFrame);
151 self:ForceUpdate();
152 return 0;
155 function AstrolabeMapMonitor:LookForMapsToRegister()
156 for k, frame in pairs(WorldMapDisplayFrames) do
157 if ( type(frame) == "table" and frame.SetParent ) then
158 self:MonitorWorldMap(frame);
163 function AstrolabeMapMonitor:Update()
164 local visibleMap = false;
165 if ( (self.NumVisibleWorldMaps) > 0 ) then
166 visibleMap = true;
168 for lib, versionString in pairs(self.AstrolabeLibrarys) do
169 lib.WorldMapVisible = visibleMap;
170 if ( (not visibleMap) and lib.AllWorldMapsHidden ) then
171 lib:AllWorldMapsHidden();
174 return visibleMap;
177 function AstrolabeMapMonitor:ForceUpdate()
178 self.NumVisibleWorldMaps = 0;
179 for worldMap, monitorFrame in pairs(self.TrackedWorldMaps) do
180 if ( worldMap:IsVisible() ) then
181 self.NumVisibleWorldMaps = self.NumVisibleWorldMaps + 1;
184 return self:Update();
187 function AstrolabeMapMonitor:RegisterAstrolabeLibrary( lib, majorVersionString )
188 -- check argument types
189 argcheck(lib, 2, "table");
190 argcheck(majorVersionString, 3, "string");
192 self.AstrolabeLibrarys[lib] = majorVersionString;
193 self:Update();
197 --------------------------------------------------------------------------------------------------------------
198 -- Handler Scripts
199 --------------------------------------------------------------------------------------------------------------
201 function AstrolabeMapMonitor:OnEvent( frame, event )
202 if ( event == "ADDON_LOADED" ) then
203 self:LookForMapsToRegister();
204 self:ForceUpdate();
209 --------------------------------------------------------------------------------------------------------------
210 -- Library Registration
211 --------------------------------------------------------------------------------------------------------------
213 local function activate( newInstance, oldInstance )
214 if ( oldInstance ) then -- this is an upgrade activate
215 for k, v in pairs(oldInstance) do
216 if ( type(v) ~= "function" ) then
217 newInstance[k] = v;
220 AstrolabeMapMonitor = oldInstance;
221 else
222 AstrolabeMapMonitor.eventFrame = CreateFrame("Frame");
224 for worldMap, monitorFrame in pairs(AstrolabeMapMonitor.TrackedWorldMaps) do
225 setScripts(monitorFrame);
227 local frame = AstrolabeMapMonitor.eventFrame;
228 frame:Hide();
229 frame:UnregisterAllEvents();
230 frame:RegisterEvent("ADDON_LOADED");
231 frame:SetScript("OnEvent",
232 function( frame, event, ... )
233 AstrolabeMapMonitor:OnEvent(frame, event, ...);
238 DongleStub:Register(AstrolabeMapMonitor, activate)