view MinimapRange.lua @ 0:6fdb0c5751ab

Importing old repo data under /trunk
author root@5e7f7089-49ba-45b1-8616-21aa0651eee7
date Mon, 29 Sep 2008 20:58:24 +0000
parents
children 596068b6b1bf
line wrap: on
line source
--
--		MinimapRange 1.00 - Steve Kaye
--
--		Shows a circle on the minimap to indicate spell range
--

MinimapRange = AceLibrary("AceAddon-2.0"):new("AceHook-2.1", "AceEvent-2.0", "AceConsole-2.0", "AceDB-2.0", "AceDebug-2.0")

MinimapRange.outdoorRanges = 
{
	[0] = {
		[5] = 13,
		[8] = 15,     
		[45] = 35,
		[100] = 70
	},
	[1] = {
		[5] = 13.5,
		[8] = 15.5,
		[45] = 40,
		[100] = 80
	},
	[2] = {
		[5] = 14,
		[8] = 16,
		[45] = 45,
		[100] = 94
	},
	[3] = {
		[5] = 15,
		[8] = 18,
		[45] = 57,
		[100] = 115
	},
	[4] = {
		[5] = 16,
		[8] = 22,
		[45] = 73
	},
	[5] = {
		[5] = 20,
		[8] = 30,
		[45] = 105
	}
}

MinimapRange.indoorRanges = 
{
	[0] = {
		[5] = 13,
		[8] = 16,
		[30] = 37,
		[36] = 43,
		[45] = 52,
		[100] = 70 -- suspect
	},
	[1] = {
		[5] = 15,
		[8] = 19,
		[30] = 43,
		[36] = 53,
		[45] = 63,
		[100] = 80 -- suspect
	},
	[2] = {
		[5] = 15,
		[8] = 23,
		[30] = 54,
		[36] = 66,
		[45] = 84,
		[100] = 94 -- suspect
	},
	[3] = {
		[5] = 20,
		[8] = 34,
		[30] = 75,
		[36] = 94,
		[45] = 118 -- suspect
	},
	[4] = {
		[5] = 26,
		[8] = 90,
		[24] = 98,
		[30] = 105,
		[36] = 118
	},
	[5] = {
		[5] = 35,
		[8] = 100,
		[24] = 130 -- off the minimap
	}
}


-- Set up our chat command arguments

function MinimapRange:OnInitialize()

	self:Debug("MinimapRange:OnInitialize()")
	
	local defaults = {
		colorRed = 1,
		colorGreen = 0,
		colorBlue = 0,
		colorAlpha = 0.25,
		range = 30,
		indoors = true
	}
	
	local args = {
		type = 'group',
		args = {
			color = {
				type = 'color',
				name = 'color',
				desc = "Set the color of the range circle.",
				get = function()
					return self.db.profile.colorRed, self.db.profile.colorGreen, self.db.profile.colorBlue, self.db.profile.colorAlpha
				end,
				set = function(r, g, b, a)
					self:SetColour(r, g, b, a)
				end,
				hasAlpha = true
			},
			range = {
				type = 'range',
				name = 'range',
				desc = "The range of the circle (in yards)",
				min = 5,
				max = 100,
				step = 1,
				get = function()
					return self.db.profile.range
				end,
				set = function(value)
					self:SetRange(value)
				end
			},
			indoors = {
				type = 'toggle',
				name = 'indoors',
				desc = 'Whether you are indoors or outdoors.  Once you tell it which you are in it will try to keep track but you can fix it with this.',
				get = function()
					return self.db.profile.indoors
				end,
				set = function(value)
					self:SetIndoors(value)
				end
			},
		},
	}
	
	self:RegisterDB("MinimapRangeDB")
	self:RegisterDefaults('profile', defaults )
	
	self:RegisterChatCommand({"/mmr" }, args)
end

-- Enable the addon
function MinimapRange:OnEnable()

	self:CreateFrame()
	
	self:SecureHook(Minimap, "SetZoom")
	self:RegisterEvent("MINIMAP_UPDATE_ZOOM","UpdateZoom")
end

function MinimapRange:OnDisable()
	if self.rangeFrame ~= nil then
		self.rangeFrame:Hide()
	end
end

function MinimapRange:CreateFrame()

	if self.rangeFrame == nil then
		-- Create our frame
		self.rangeFrame = CreateFrame('Frame', 'MinimapRangeFrame', Minimap)
	
		-- Set the properties
		self.rangeFrame:SetFrameStrata("LOW")
	
		-- Add the texture
		local t = self.rangeFrame:CreateTexture(nil, "BACKGROUND")
		t:SetTexture("Interface\\AddOns\\MinimapRange\\MM")
		t:SetVertexColor(self.db.profile.colorRed, self.db.profile.colorGreen, self.db.profile.colorBlue, self.db.profile.colorAlpha)
		t:SetAllPoints(self.rangeFrame)
		self.rangeFrame.texture = t
	
		self.rangeFrame:SetPoint("CENTER",0,0)
		self:UpdateCircle(self.db.profile.range)
	end
	
	self.rangeFrame:Show() 
end

function MinimapRange:SetColour(r, g, b, a)
	-- Save the setting
	self.db.profile.colorRed, self.db.profile.colorGreen, self.db.profile.colorBlue, self.db.profile.colorAlpha = r, g, b, a

	-- Set the colour of the circle
	if self.rangeFrame ~= nil then
		self.rangeFrame.texture:SetVertexColor(r, g, b, a)
	end
end

function MinimapRange:SetRange(value)
	-- Save the setting
	self.db.profile.range = value
	
	-- Show the new range
	self:UpdateCircle(value)
end

function MinimapRange:SetIndoors(value)
	-- Save the setting
	self.db.profile.indoors = value
	
	if value then
		self:Debug("Indoors: TRUE")
	else
		self:Debug("Indoors: FALSE")
	end
	
	-- Show the new range
	self:UpdateCircle(self.db.profile.range)
end

function MinimapRange:SetCircle(range, size)
	if self.rangeFrame ~= nil then
	
		local rangeTable = self.outdoorRanges
		if self.db.profile.indoors then
			rangeTable = self.indoorRanges
		end
		
		rangeTable[Minimap:GetZoom()][range] = size

		self:UpdateCircle(range)
	end
end

function MinimapRange:UpdateCircle(range)
	self:Debug(string.format('Z: %s  S: %s', Minimap:GetZoom(), range))
	
	if self.rangeFrame ~= nil then
		local lowerKey, lowerValue = 0, 0
		local upperKey, upperValue = 100, 140
		
		-- Get the corrct range table to work with
		local rangeTable = self.outdoorRanges
		if self.db.profile.indoors then
			rangeTable = self.indoorRanges
		end
		
		-- Find two numbers to interpolate between
		for k, v in pairs(rangeTable[Minimap:GetZoom()]) do
			self:Debug(string.format("K: %s  LK: %s  R: %s", k, lowerKey, range))
			if k > lowerKey and k <= range then
				lowerKey = k
				lowerValue = v
			end
			
			if k < upperKey and k >= range then
				upperKey = k
				upperValue = v
			end
		end
		
		local size

		-- Do the interpolation if we haven't found an exact match
		if lowerKey == upperKey then
			size = lowerValue
		else
			size = lowerValue + ((upperValue - lowerValue) / (upperKey - lowerKey)) * (range - lowerKey)
		end
		
		self:Debug("Size: "..size)
		-- Change the circle size
		if size ~= nil then
			self.rangeFrame:SetWidth(size)
			self.rangeFrame:SetHeight(size)
		end
	end
end

function MinimapRange:SetZoom(minimap, level)
	-- Resize the circle
	self:UpdateCircle(self.db.profile.range)
end

function MinimapRange:UpdateZoom()
	
	-- Try to detect whether we are indoors or outdoors
	local zoom = Minimap:GetZoom()
	local indoorsZoom = tonumber(GetCVar("minimapInsideZoom")) 
	local outdoorsZoom = tonumber(GetCVar("minimapZoom"))
	
	-- Have we detected whether we are inside or outside?
	if indoorsZoom ~= outdoorsZoom then
		self:Debug("Using detected location")
		
		-- Yes, use the detected value
		self:SetIndoors(indoorsZoom == zoom)
		
	else
		self:Debug("Using best guess location")
		
		-- If this is not the first time that we have had this message
		-- then we must assume that we have moved from indoors to outdoors
		-- or vice versa
		if self.firstUpdateZoomComplete == true then
			self.db.profile.indoors = not self.db.profile.indoors 
		end
		
		self:SetIndoors(self.db.profile.indoors)
	end
	
	-- Resize the circle
	self:UpdateCircle(self.db.profile.range)
	
	-- Note that the first update after a reload has been done
	self.firstUpdateZoomComplete = true
end