VuhDoDirections.lua 4.09 KB
Newer Older
humfras's avatar
humfras committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
local VUHDO_1_DIV_2_PI_MUL_108 = 108 / math.pi / 2;
local UnitIsUnit = UnitIsUnit;
local floor = floor;
local sOldButton;
local sOldDistance;
local sIsDeadOnly;
local sIsAlways;
local sIsDistanceText;
local sScale;
local VuhDoDirectionFrame;
local VuhDoDirectionFrameArrow;
local VuhDoDirectionFrameText;
local VUHDO_setMapToCurrentZone;
local VUHDO_getDistanceBetween;
local VUHDO_getUnitDirection;

local VUHDO_RAID = { };
function VUHDO_directionsInitLocalOverrides()
	VUHDO_RAID = _G["VUHDO_RAID"];

	sIsDeadOnly = VUHDO_CONFIG["DIRECTION"]["isDeadOnly"];
	sIsAlways = VUHDO_CONFIG["DIRECTION"]["isAlways"];
	sIsDistanceText = VUHDO_CONFIG["DIRECTION"]["isDistanceText"];
	sScale = VUHDO_CONFIG["DIRECTION"]["scale"] * 0.01;
	sOldButton = nil;
	sOldDistance = nil;

	VuhDoDirectionFrame = _G["VuhDoDirectionFrame"];
	VuhDoDirectionFrameArrow = _G["VuhDoDirectionFrameArrow"];
	VuhDoDirectionFrameText = _G["VuhDoDirectionFrameText"];
	VUHDO_setMapToCurrentZone = _G["VUHDO_setMapToCurrentZone"];
	VUHDO_getDistanceBetween = _G["VUHDO_getDistanceBetween"];
	VUHDO_getUnitDirection = _G["VUHDO_getUnitDirection"];
end



--
function VUHDO_getCellForDirection(aDirection)
	return floor(aDirection * VUHDO_1_DIV_2_PI_MUL_108 + 0.5) % 108
end
local VUHDO_getCellForDirection = VUHDO_getCellForDirection;



--
local tStartX, tStartY;
function VUHDO_getTexCoordsForCell(aCell)
	tStartX, tStartY = (aCell % 9) * 0.109375, floor(aCell / 9) * 0.08203125;
	return tStartX, tStartX + 0.109375, tStartY, tStartY + 0.08203125;
end
local VUHDO_getTexCoordsForCell = VUHDO_getTexCoordsForCell;



--
local tQuota;
local tR, tG;
local tInvModi;
function VUHDO_getRedGreenForDistance(aDistance)
	tQuota = 4 - 0.05 * aDistance;

	if tQuota > 2 then tQuota = 2;
	elseif tQuota < 0 then tQuota = 0; end

	if tQuota > 1 then
		tR, tG = 0, 1;
		tQuota = tQuota - 1;
	else
		tR, tG = 1, 0;
	end

	tInvModi = 1 - tQuota;
	return tInvModi + tR * tQuota, tG * tInvModi + tQuota;
end
local VUHDO_getRedGreenForDistance = VUHDO_getRedGreenForDistance;



--
local tInfo;
function VUHDO_shouldDisplayArrow(aUnit)
	tInfo = VUHDO_RAID[aUnit];
	return
	  not UnitIsUnit("player", aUnit)
		and tInfo
		and (not tInfo["range"] or sIsAlways)
		and (not sIsDeadOnly or tInfo["dead"])
		and tInfo["connected"]
		and not tInfo["isPet"];
end
local VUHDO_shouldDisplayArrow = VUHDO_shouldDisplayArrow;



--
local tUnit;
local tCell;
local sLastCell = nil;
local tButton = nil;
local tHeight;
local tDistance;
local tHeight;
local tDestR, tDestG;
function VUHDO_updateDirectionFrame(aButton)
	if aButton then tButton = aButton;
	elseif not tButton then return; end

	tUnit = tButton:GetAttribute("unit");

	if not VUHDO_shouldDisplayArrow(tUnit) then
		VuhDoDirectionFrame["shown"] = false;
		VuhDoDirectionFrame:Hide();
		return;
	end

	tDirection = VUHDO_getUnitDirection(tUnit);
	if not tDirection then
		VuhDoDirectionFrame["shown"] = false;
		VuhDoDirectionFrame:Hide();
		return;
	end

	tCell = VUHDO_getCellForDirection(tDirection);
	if tCell ~= sLastCell then
		sLastCell = tCell;
		VuhDoDirectionFrameArrow:SetTexCoord(VUHDO_getTexCoordsForCell(tCell));
	end

	if sIsDistanceText then
		tDistance = VUHDO_getDistanceBetween("player", tUnit);
		if (tDistance or 0) > 0 then
			tDistance = floor(tDistance + 0.5);

			if tDistance ~= sOldDistance then
				sOldDistance = tDistance;

				VuhDoDirectionFrameText:SetText(tDistance);

				tDestR, tDestG = VUHDO_getRedGreenForDistance(tDistance);
				VuhDoDirectionFrameText:SetTextColor(tDestR, tDestG, 0.2, 0.8);
				VuhDoDirectionFrameArrow:SetVertexColor(tDestR, tDestG, 0);
			end
		else
			VuhDoDirectionFrameText:SetText("");
		end
	end

	if sOldButton ~= tButton then
		sOldButton = tButton;
		tHeight = tButton:GetHeight() * sScale * tButton:GetEffectiveScale();
		VuhDoDirectionFrame:SetPoint("CENTER", tButton:GetName(), "CENTER", 0, 0);
		VuhDoDirectionFrame:SetWidth(tHeight);
		VuhDoDirectionFrame:SetHeight(tHeight);
	end
	VuhDoDirectionFrame:Show();
	VuhDoDirectionFrame["shown"] = true;
end