whups
[QuestHelper.git] / AstrolabeQH / AstrolabeMapMonitor.lua
blobee8215120f64b4459335e61e5e0cb9746928ab8b
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 -- WARNING!!!
43 -- DO NOT MAKE CHANGES TO THIS LIBRARY WITHOUT FIRST CHANGING THE LIBRARY_VERSION_MAJOR
44 -- STRING (to something unique) OR ELSE YOU MAY BREAK OTHER ADDONS THAT USE THIS LIBRARY!!!
45 local LIBRARY_VERSION_MAJOR = "AstrolabeMapMonitor"
46 local LIBRARY_VERSION_MINOR = tonumber(string.match("$Revision: 44 $", "(%d+)") or 1)
48 if not DongleStub then error(LIBRARY_VERSION_MAJOR .. " requires DongleStub.") end
49 if not DongleStub:IsNewerVersion(LIBRARY_VERSION_MAJOR, LIBRARY_VERSION_MINOR) then return end
51 local AstrolabeMapMonitor = {};
53 function AstrolabeMapMonitor:GetVersion()
54 return LIBRARY_VERSION_MAJOR, LIBRARY_VERSION_MINOR;
55 end
58 --------------------------------------------------------------------------------------------------------------
59 -- Global World Map Frame Registration Table
60 --------------------------------------------------------------------------------------------------------------
62 if ( type(WorldMapDisplayFrames) ~= "table" ) then
63 WorldMapDisplayFrames = { WorldMapFrame };
64 else
65 local worldMapFound = false;
66 for k, v in pairs(WorldMapDisplayFrames) do
67 if ( v == WorldMapFrame ) then
68 worldMapFound = true;
69 break;
70 end
71 end
72 if not ( worldMapFound ) then
73 table.insert(WorldMapDisplayFrames, WorldMapFrame);
74 end
75 end
78 --------------------------------------------------------------------------------------------------------------
79 -- Working Tables and Config Constants
80 --------------------------------------------------------------------------------------------------------------
82 AstrolabeMapMonitor.TrackedWorldMaps = {};
83 AstrolabeMapMonitor.AstrolabeLibrarys = {};
85 AstrolabeMapMonitor.NumVisibleWorldMaps = 0;
88 --------------------------------------------------------------------------------------------------------------
89 -- Monitor Frame Script Handlers
90 --------------------------------------------------------------------------------------------------------------
92 local function onShow( frame )
93 AstrolabeMapMonitor.NumVisibleWorldMaps = AstrolabeMapMonitor.NumVisibleWorldMaps + 1;
94 AstrolabeMapMonitor:Update()
95 end
97 local function onHide( frame )
98 AstrolabeMapMonitor.NumVisibleWorldMaps = AstrolabeMapMonitor.NumVisibleWorldMaps - 1;
99 AstrolabeMapMonitor:Update()
102 local function setScripts( monitorFrame )
103 QH_Hook( monitorFrame, "OnShow", onShow);
104 QH_Hook( monitorFrame, "OnHide", onHide);
108 --------------------------------------------------------------------------------------------------------------
109 -- Internal Utility Functions
110 --------------------------------------------------------------------------------------------------------------
112 local function assert(level,condition,message)
113 if not condition then
114 error(message,level)
118 local function argcheck(value, num, ...)
119 assert(1, type(num) == "number",
120 "Bad argument #2 to 'argcheck' (number expected, got " .. type(level) .. ")")
122 for i=1,select("#", ...) do
123 if type(value) == select(i, ...) then return end
126 local types = strjoin(", ", ...)
127 local name = string.match(debugstack(2,2,0), ": in function [`<](.-)['>]")
128 error(string.format("Bad argument #%d to 'AstrolabeMapMonitor.%s' (%s expected, got %s)", num, name, types, type(value)), 3)
132 --------------------------------------------------------------------------------------------------------------
133 -- Public API
134 --------------------------------------------------------------------------------------------------------------
136 function AstrolabeMapMonitor:MonitorWorldMap( worldMapFrame )
137 -- check argument types
138 argcheck(worldMapFrame, 2, "table");
139 assert((worldMapFrame.SetParent), "Usage Message");
141 local TrackedWorldMaps = self.TrackedWorldMaps;
142 if ( TrackedWorldMaps[worldMapFrame] ) then
143 return 1;
145 local monitorFrame = CreateFrame("Frame", nil, worldMapFrame);
146 TrackedWorldMaps[worldMapFrame] = monitorFrame;
147 setScripts(monitorFrame);
148 self:ForceUpdate();
149 return 0;
152 function AstrolabeMapMonitor:LookForMapsToRegister()
153 for k, frame in pairs(WorldMapDisplayFrames) do
154 if ( type(frame) == "table" and frame.SetParent ) then
155 self:MonitorWorldMap(frame);
160 function AstrolabeMapMonitor:Update()
161 local visibleMap = false;
162 if ( (self.NumVisibleWorldMaps) > 0 ) then
163 visibleMap = true;
165 for lib, versionString in pairs(self.AstrolabeLibrarys) do
166 lib.WorldMapVisible = visibleMap;
167 if ( (not visibleMap) and lib.AllWorldMapsHidden ) then
168 lib:AllWorldMapsHidden();
171 return visibleMap;
174 function AstrolabeMapMonitor:ForceUpdate()
175 self.NumVisibleWorldMaps = 0;
176 for worldMap, monitorFrame in pairs(self.TrackedWorldMaps) do
177 if ( worldMap:IsVisible() ) then
178 self.NumVisibleWorldMaps = self.NumVisibleWorldMaps + 1;
181 return self:Update();
184 function AstrolabeMapMonitor:RegisterAstrolabeLibrary( lib, majorVersionString )
185 -- check argument types
186 argcheck(lib, 2, "table");
187 argcheck(majorVersionString, 3, "string");
189 self.AstrolabeLibrarys[lib] = majorVersionString;
190 self:Update();
194 --------------------------------------------------------------------------------------------------------------
195 -- Handler Scripts
196 --------------------------------------------------------------------------------------------------------------
198 function AstrolabeMapMonitor:OnEvent( frame, event )
199 if ( event == "ADDON_LOADED" ) then
200 self:LookForMapsToRegister();
201 self:ForceUpdate();
206 --------------------------------------------------------------------------------------------------------------
207 -- Library Registration
208 --------------------------------------------------------------------------------------------------------------
210 local function activate( newInstance, oldInstance )
211 if ( oldInstance ) then -- this is an upgrade activate
212 for k, v in pairs(oldInstance) do
213 if ( type(v) ~= "function" ) then
214 newInstance[k] = v;
217 AstrolabeMapMonitor = oldInstance;
218 else
219 AstrolabeMapMonitor.eventFrame = CreateFrame("Frame");
221 for worldMap, monitorFrame in pairs(AstrolabeMapMonitor.TrackedWorldMaps) do
222 setScripts(monitorFrame);
224 local frame = AstrolabeMapMonitor.eventFrame;
225 frame:Hide();
226 frame:UnregisterAllEvents();
227 frame:RegisterEvent("ADDON_LOADED");
228 frame:SetScript("OnEvent",
229 function( frame, event, ... )
230 AstrolabeMapMonitor:OnEvent(frame, event, ...);
235 DongleStub:Register(AstrolabeMapMonitor, activate)