VuhDoKeySetup.lua 14.8 KB
Newer Older
humfras's avatar
humfras committed
1 2 3 4 5 6 7 8 9 10 11 12 13
local _;

local VUHDO_RAID_NAMES;
local VUHDO_RAID;
local VUHDO_BUFF_REMOVAL_SPELLS;
local VUHDO_SPELL_ASSIGNMENTS;
local VUHDO_CONFIG;

local VUHDO_buildMacroText;
local VUHDO_buildTargetButtonMacroText;
local VUHDO_buildTargetMacroText;
local VUHDO_buildFocusMacroText;
local VUHDO_buildAssistMacroText;
14
local VUHDO_buildExtraActionButtonMacroText;
15
local VUHDO_buildMouseLookMacroText;
humfras's avatar
humfras committed
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
local VUHDO_replaceMacroTemplates;
local VUHDO_isActionValid;
local VUHDO_isSpellKnown;

local GetMacroIndexByName = GetMacroIndexByName;
local GetMacroInfo = GetMacroInfo;
local GetSpellBookItemTexture = GetSpellBookItemTexture;
local UnitIsDeadOrGhost = UnitIsDeadOrGhost;
local gsub = gsub;
local GetCursorInfo = GetCursorInfo;
local GetShapeshiftForm = GetShapeshiftForm;
local InCombatLockdown = InCombatLockdown;
local pairs = pairs;
local strlower = strlower;
local format = format;

local sIsCliqueCompat;

function VUHDO_keySetupInitLocalOverrides()
	VUHDO_RAID_NAMES = _G["VUHDO_RAID_NAMES"];
	VUHDO_RAID = _G["VUHDO_RAID"];
	VUHDO_BUFF_REMOVAL_SPELLS = _G["VUHDO_BUFF_REMOVAL_SPELLS"];
	VUHDO_SPELL_ASSIGNMENTS = _G["VUHDO_SPELL_ASSIGNMENTS"];
	VUHDO_CONFIG = _G["VUHDO_CONFIG"];

	VUHDO_buildMacroText = _G["VUHDO_buildMacroText"];
	VUHDO_buildTargetButtonMacroText = _G["VUHDO_buildTargetButtonMacroText"];
	VUHDO_buildTargetMacroText = _G["VUHDO_buildTargetMacroText"];
	VUHDO_buildFocusMacroText = _G["VUHDO_buildFocusMacroText"];
	VUHDO_buildAssistMacroText = _G["VUHDO_buildAssistMacroText"];
46
	VUHDO_buildExtraActionButtonMacroText = _G["VUHDO_buildExtraActionButtonMacroText"];
47
	VUHDO_buildMouseLookMacroText = _G["VUHDO_buildMouseLookMacroText"];
humfras's avatar
humfras committed
48 49 50 51 52 53 54 55 56 57
	VUHDO_replaceMacroTemplates = _G["VUHDO_replaceMacroTemplates"];
	VUHDO_isActionValid = _G["VUHDO_isActionValid"];
	VUHDO_isSpellKnown = _G["VUHDO_isSpellKnown"];
	sIsCliqueCompat = VUHDO_CONFIG["IS_CLIQUE_COMPAT_MODE"];
end



local VUHDO_REZ_SPELLS_NAMES = {
	[VUHDO_SPELL_ID.REDEMPTION] = true,
58
	[VUHDO_SPELL_ID.ABSOLUTION] = true,
humfras's avatar
humfras committed
59
	[VUHDO_SPELL_ID.ANCESTRAL_SPIRIT] = true,
60
	[VUHDO_SPELL_ID.ANCESTRAL_VISION] = true,
humfras's avatar
humfras committed
61 62
	[VUHDO_SPELL_ID.REVIVE] = true,
	[VUHDO_SPELL_ID.REBIRTH] = true,
63
	[VUHDO_SPELL_ID.REVITALIZE] = true,
humfras's avatar
humfras committed
64
	[VUHDO_SPELL_ID.RESURRECTION] = true,
65
	[VUHDO_SPELL_ID.MASS_RESURRECTION] = true,
humfras's avatar
humfras committed
66
	[VUHDO_SPELL_ID.RESUSCITATE] = true,
67
	[VUHDO_SPELL_ID.REAWAKEN] = true,
humfras's avatar
humfras committed
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
};



local tUnit, tInfo, tIdent;
local tButtonName;
local tMacroId, tMacroText;
local tActionLow;
local tInvalidGroup = { ["group"] = -1 };

local function _VUHDO_setupHealButtonAttributes(aModiKey, aButtonId, anAction, aButton, anIsTgButton, anIndex)

	tUnit = aButton["raidid"];
	tActionLow = strlower(anAction);

	if "assist" == tActionLow then
		aButton:SetAttribute(aModiKey .. "type" .. aButtonId, "macro");
		aButton:SetAttribute(aModiKey .. "macrotext" .. aButtonId, VUHDO_buildAssistMacroText(tUnit));

	elseif "focus" == tActionLow then
		aButton:SetAttribute(aModiKey .. "type" .. aButtonId, "macro");
		aButton:SetAttribute(aModiKey .. "macrotext" .. aButtonId, VUHDO_buildFocusMacroText(tUnit));

	elseif "target" == tActionLow then
		aButton:SetAttribute(aModiKey .. "type" .. aButtonId, "macro");
		aButton:SetAttribute(aModiKey .. "macrotext" .. aButtonId, VUHDO_buildTargetMacroText(tUnit));

95 96 97 98
	elseif "extraactionbutton" == tActionLow then
		aButton:SetAttribute(aModiKey .. "type" .. aButtonId, "macro");
		aButton:SetAttribute(aModiKey .. "macrotext" .. aButtonId, VUHDO_buildExtraActionButtonMacroText(tUnit));

99 100 101 102
	elseif "mouselook" == tActionLow then
		aButton:SetAttribute(aModiKey .. "type" .. aButtonId, "macro");
		aButton:SetAttribute(aModiKey .. "macrotext" .. aButtonId, VUHDO_buildMouseLookMacroText());

humfras's avatar
humfras committed
103 104 105 106 107 108 109
	elseif "menu" == tActionLow or "tell" == tActionLow then
		aButton:SetAttribute(aModiKey .. "type" .. aButtonId, nil);

	elseif "dropdown" == tActionLow then
		aButton:SetAttribute(aModiKey .."type" .. aButtonId, "VUHDO_contextMenu");

		VUHDO_contextMenu = function()
110 111 112 113 114 115 116 117 118 119
			tUnit = aButton["raidid"];
			local tName, tNumber, tMenu;

			if UnitIsUnit(tUnit, "player") then 
				tMenu = "SELF"; 
			elseif UnitIsUnit(tUnit, "vehicle") then 
				tMenu = "VEHICLE";
			elseif UnitIsUnit(tUnit, "pet") then 
				tMenu = "PET"; 
			elseif UnitIsPlayer(tUnit) then
humfras's avatar
humfras committed
120
				tInfo = VUHDO_RAID[tUnit];
121 122 123 124 125 126 127 128 129 130
				
				tName = tInfo["name"];
				tNumber = tInfo["number"];

				if UnitInRaid(tUnit) then
					tMenu = "RAID_PLAYER";
				elseif UnitInParty(tUnit) then
					tMenu = "PARTY";
				else
					tMenu = "PLAYER";
humfras's avatar
humfras committed
131
				end
132 133 134
			else
				tMenu = "TARGET";
				tName = RAID_TARGET_ICON;
humfras's avatar
humfras committed
135 136
			end

137 138 139 140 141 142 143 144 145 146
			UIDropDownMenu_Initialize(VuhDoUnitButtonDropDown, 
				function(self) 
					if tMenu then
						UnitPopup_ShowMenu(self, tMenu, tUnit, tName, tNumber);
					end
				end, 
				"MENU"
			);
			
			ToggleDropDownMenu(1, nil, VuhDoUnitButtonDropDown, "cursor", 0, 0);
humfras's avatar
humfras committed
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
		end

		aButton["VUHDO_contextMenu"] = VUHDO_contextMenu;
	else
		anAction = VUHDO_REPLACE_SPELL_NAME[anAction] or anAction;

		if VUHDO_NATIVE_ASSIGN_SPELLS[anAction] then
			aButton:SetAttribute(aModiKey .. "type" .. aButtonId, "spell");
			aButton:SetAttribute(aModiKey .. "spell" .. aButtonId, anAction);

		elseif VUHDO_isSpellKnown(anAction) or VUHDO_IN_COMBAT_RELOG then -- Spells may not be initialized yet
			-- Dead players do not trigger "help/noharm" conditionals
			if VUHDO_REZ_SPELLS_NAMES[anAction] then

				aButton:SetAttribute(aModiKey .. "type" .. aButtonId, "macro");
				aButton:SetAttribute(aModiKey .. "macrotext" .. aButtonId,
163
				VUHDO_buildRezMacroText(anAction, tUnit));
humfras's avatar
humfras committed
164 165 166 167 168 169
				return;
			-- Cleansing charmed players is an offensive thing to do
			elseif VUHDO_BUFF_REMOVAL_SPELLS[anAction] then

				aButton:SetAttribute(aModiKey .. "type" .. aButtonId, "macro");
				aButton:SetAttribute(aModiKey .. "macrotext" .. aButtonId,
170
				VUHDO_buildPurgeMacroText(anAction, tUnit));
humfras's avatar
humfras committed
171 172 173 174 175
				return;
			else
				-- build a spell macro
				aButton:SetAttribute(aModiKey .. "type" .. aButtonId, "macro");
				aButton:SetAttribute(aModiKey .. "macrotext" .. aButtonId,
176
				VUHDO_buildMacroText(anAction, false, tUnit));
humfras's avatar
humfras committed
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
			end
		else
			tMacroId = GetMacroIndexByName(anAction);
			if tMacroId ~= 0 then -- Macro?

				_, _, tMacroText = GetMacroInfo(tMacroId);
				tMacroText = VUHDO_replaceMacroTemplates(tMacroText, tUnit);
				aButton:SetAttribute(aModiKey .. "type" .. aButtonId, "macro");
				aButton:SetAttribute(aModiKey .. "macrotext" .. aButtonId, tMacroText);
			elseif IsUsableItem(anAction) then -- Item?

				aButton:SetAttribute(aModiKey .. "type" .. aButtonId, "item");
				aButton:SetAttribute(aModiKey .. "item" .. aButtonId, anAction);
			else -- we don't know, assume it's a spell

				aButton:SetAttribute(aModiKey .. "type" .. aButtonId, "spell");
				aButton:SetAttribute(aModiKey .. "spell" .. aButtonId, anAction);
			end
		end
	end
end



--
local tUnit;
local tHostSpell;
local tSpellInfo;
205
local tActionLow;
humfras's avatar
humfras committed
206 207 208
local function VUHDO_setupHealButtonAttributes(aModiKey, aButtonId, anAction, aButton, anIsTgButton, anIndex)

	tUnit = aButton["raidid"];
209
	tActionLow = strlower(anAction);
humfras's avatar
humfras committed
210

211
	if anIsTgButton or tUnit == "focus" or (tUnit == "target" and "dropdown" ~= tActionLow) or VUHDO_isBossUnit(tUnit) then
humfras's avatar
humfras committed
212 213 214 215 216 217 218
		if not anIndex then
			tSpellInfo = VUHDO_HOSTILE_SPELL_ASSIGNMENTS[VUHDO_KEYS_MODIFIER[aModiKey] .. aButtonId];
			tHostSpell = tSpellInfo ~= nil and tSpellInfo[3] or "";
		else
			tHostSpell = VUHDO_SPELLS_KEYBOARD["HOSTILE_WHEEL"][anIndex][3];
		end
		aButton:SetAttribute(aModiKey .. "type" .. aButtonId, "macro");
219
		if (tHostSpell or "") ~= "" or (tActionLow or "") ~= "" then
humfras's avatar
humfras committed
220
			aButton:SetAttribute(aModiKey .. "macrotext" .. aButtonId,
221
				VUHDO_buildTargetButtonMacroText(tUnit, tActionLow, tHostSpell));
humfras's avatar
humfras committed
222 223 224 225 226 227
		else
			aButton:SetAttribute(aModiKey .. "macrotext" .. aButtonId, nil);
		end
		return;
	end

228
	if (tActionLow or "") == "" then
humfras's avatar
humfras committed
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
		aButton:SetAttribute(aModiKey .. "type" .. aButtonId, nil);
		return;
	else
		_VUHDO_setupHealButtonAttributes(aModiKey, aButtonId, anAction, aButton, anIsTgButton, anIndex);
	end
end



--
local tString, tAssignIdx, tFriendSpell, tHostSpell;
local function VUHDO_getWheelDefString()
	tString = "";
	for tIndex, tValue in pairs(VUHDO_WHEEL_BINDINGS) do
		tAssignIdx = VUHDO_WHEEL_INDEX_BINDING[tIndex];
		tFriendSpell = VUHDO_SPELLS_KEYBOARD["HOSTILE_WHEEL"][tAssignIdx][3];
		tHostSpell = VUHDO_SPELLS_KEYBOARD["WHEEL"][tAssignIdx][3];

		if #tFriendSpell > 0 or #tHostSpell > 0 then
			tString = format("%sself:SetBindingClick(0, \"%s\", self:GetName(), \"w%d\");", tString, tValue, tIndex);
		end
	end
	return tString;
end



--
local tString;
local function VUHDO_getInternalKeyString()
	tString = "";
	for tIndex, tEntries in pairs(VUHDO_SPELLS_KEYBOARD["INTERNAL"]) do
		tString = format("%sself:SetBindingClick(0, \"%s\", self:GetName(), \"ik%d\");", tString, tEntries[2] == "\\" and "\\\\" or tEntries[2] or "", tIndex);
	end
	return tString;
end



-- Parse and interpret action-type
local tPreAction;
local tTarget;
local tIsWheel;
local tHostSpell;
local tWheelDefString;
local tCnt;
local tFrame;
local tBinding;
function VUHDO_setupAllHealButtonAttributes(aButton, aUnit, anIsDisable, aForceTarget, anIsTgButton, anIsIcButton)

	if aUnit then
		aButton:SetAttribute("unit", aUnit);
		aButton["raidid"] = aUnit;
	end

	if not aButton:GetAttribute("vd_tt_hook") then
		if anIsIcButton then
			aButton:HookScript("OnEnter", function(self) VUHDO_showDebuffTooltip(self); VuhDoActionOnEnter(self:GetParent():GetParent():GetParent():GetParent()) end);
			aButton:HookScript("OnLeave", function(self) VUHDO_hideDebuffTooltip(); VuhDoActionOnLeave(self:GetParent():GetParent():GetParent():GetParent()) end);
		else
			aButton:HookScript("OnEnter",	function(self) VuhDoActionOnEnter(self); end);
			aButton:HookScript("OnLeave",	function(self) VuhDoActionOnLeave(self); end);
		end
		aButton:SetAttribute("vd_tt_hook", true);
	end

	if sIsCliqueCompat then
		aButton:EnableMouseWheel(1);
		return;
	end

	tPreAction = anIsDisable and "" or aForceTarget and "target" or nil;

	for tNoMinus, tWithMinus in pairs(VUHDO_MODIFIER_KEYS) do
		for tCnt = 1, 16 do -- VUHDO_NUM_MOUSE_BUTTONS
			tBinding = VUHDO_SPELL_ASSIGNMENTS[format("%s%d", tNoMinus, tCnt)];
			VUHDO_setupHealButtonAttributes(tWithMinus, tCnt,
				tPreAction or tBinding ~= nil and tBinding[3] or "",
				aButton, anIsTgButton);
		end
	end

	tIsWheel = false;
	for tIndex, tSpellDescr in pairs(VUHDO_SPELLS_KEYBOARD["WHEEL"]) do
		tHostSpell = VUHDO_SPELLS_KEYBOARD["HOSTILE_WHEEL"][tIndex][3];
		if #tSpellDescr[3] > 0 or #tHostSpell > 0 then
			tIsWheel = true;
			VUHDO_setupHealButtonAttributes("", tSpellDescr[2], tSpellDescr[3], aButton, anIsTgButton, tIndex);
		end
	end

	for tIndex, tEntries in pairs(VUHDO_SPELLS_KEYBOARD["INTERNAL"]) do
		if VUHDO_isActionValid(tEntries[1], false) then
			_VUHDO_setupHealButtonAttributes("",  "-ik" .. tIndex, tEntries[1], aButton, anIsTgButton, tIndex);
		else
			aButton:SetAttribute("type-ik" .. tIndex, "macro");
			aButton:SetAttribute("macrotext-ik" .. tIndex, VUHDO_replaceMacroTemplates(tEntries[3] or "", aUnit));
		end
	end

	if VUHDO_BUTTON_CACHE[aButton] or VUHDO_BUTTON_CACHE[aButton:GetParent():GetParent():GetParent():GetParent()] then
		tWheelDefString = "self:ClearBindings();";

		if tIsWheel then
			tWheelDefString = tWheelDefString .. VUHDO_getWheelDefString();
		end

		tWheelDefString = tWheelDefString .. VUHDO_getInternalKeyString();

		aButton:SetAttribute("_onenter", tWheelDefString);
		aButton:SetAttribute("_onleave", "self:ClearBindings();");
		aButton:SetAttribute("_onshow", "self:ClearBindings();");
		aButton:SetAttribute("_onhide", "self:ClearBindings();");
342 343 344 345
		aButton:SetAttribute(
			"_onmousedown", 
			"if not self:IsUnderMouse(false) then self:ClearBindings(); end"
		);
humfras's avatar
humfras committed
346 347 348 349 350 351 352 353 354 355 356 357 358 359
	end
end
local VUHDO_setupAllHealButtonAttributes = VUHDO_setupAllHealButtonAttributes;



--
local tProhibitSmartCastOn = {
	["target"] = true,
	["assist"] = true,
	["focus"] = true,
	["menu"] = true,
	["dropdown"] = true,
	["tell"] = true,
360 361 362 363 364
	["boss1"] = true,
	["boss2"] = true,
	["boss3"] = true,
	["boss4"] = true,
	["boss5"] = true,
humfras's avatar
humfras committed
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
};
-- Setup for smart cast
local tKey;
local function VUHDO_setupAllButtonsTo(aButton, aSpellName)
	for tNoMinus, tWithMinus in pairs(VUHDO_MODIFIER_KEYS) do
		for tCnt = 1, VUHDO_NUM_MOUSE_BUTTONS do
			tKey = tNoMinus .. tCnt;
			if not VUHDO_SPELL_ASSIGNMENTS[tKey] or not tProhibitSmartCastOn[VUHDO_SPELL_ASSIGNMENTS[tKey][3]] then
				VUHDO_setupHealButtonAttributes(tWithMinus, tCnt, aSpellName, aButton, false);
			end
		end
	end
end



--
function VUHDO_setupAllTargetButtonAttributes(aButton, aUnit)
	VUHDO_setupAllHealButtonAttributes(aButton, aUnit .. "target", false, false, true, false);
end



--
function VUHDO_setupAllTotButtonAttributes(aButton, aUnit)
	VUHDO_setupAllHealButtonAttributes(aButton, aUnit .. "targettarget", false, false, true, false);
end



--
local tCnt;
function VUHDO_disableActions(aButton)
	VUHDO_setupAllHealButtonAttributes(aButton, nil, true, false, false, false);

	aButton:Hide(); -- For clearing mouse-wheel click bindings
	aButton:Show();
end



--
local tCursorItemType;
local tAbilities;
local tUnit;
local tInfo;
local tBuff;
function VUHDO_setupSmartCast(aButton)
	if InCombatLockdown() or UnitIsDeadOrGhost("player")
414
		or (VUHDO_PLAYER_CLASS == "PRIEST" and GetShapeshiftForm() == 4) then -- Priest Spirit of Redemption?
humfras's avatar
humfras committed
415 416 417 418 419 420 421 422 423 424
		return false;
	end

	tUnit = aButton["raidid"];
	tInfo = VUHDO_RAID[tUnit];

	if not tInfo then	return false; end

	-- Resurrect?
	if VUHDO_CONFIG["SMARTCAST_RESURRECT"] and tInfo["dead"] then
425
		local tMainRes = VUHDO_getResurrectionSpells();
humfras's avatar
humfras committed
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
		if tMainRes then
			VUHDO_setupAllButtonsTo(aButton, tMainRes);
			return true;
		end
	end

	if not tInfo["baseRange"] then return false; end

	-- Trade?
	tCursorItemType = GetCursorInfo();
	if "item" == tCursorItemType or "money" == tCursorItemType then
		DropItemOnUnit(tUnit);
		VUHDO_disableActions(aButton);
		return true;
	end

	-- Cleanse?
	if VUHDO_CONFIG["SMARTCAST_CLEANSE"] and not tInfo["dead"] then
		if VUHDO_DEBUFF_TYPE_NONE ~= tInfo["debuff"] then
			tAbilities = VUHDO_getDebuffAbilities();
			if tAbilities[tInfo["debuff"]] then
				VUHDO_setupAllButtonsTo(aButton, tAbilities[tInfo["debuff"]]);
				return true;
			end
		end
	end

	-- Buff?
	if VUHDO_CONFIG["SMARTCAST_BUFF"] and tInfo["missbuff"] and not tInfo["dead"] then
		tBuff = tInfo["mibuvariants"];
456 457 458 459 460 461 462 463 464

		if VUHDO_isBuffOfTargetType(tBuff[1], VUHDO_BUFF_TARGET_HOSTILE) and not UnitIsEnemy("player", tUnit) then
			return false;
		else
			VUHDO_setupAllButtonsTo(aButton, tBuff[1]);
			VUHDO_setupHealButtonAttributes("", "2", tBuff[1], aButton, false);

			return true;
		end
humfras's avatar
humfras committed
465 466 467 468 469
	end

	return false;
end