Blizzard_GarrisonSharedTemplates.lua 96.8 KB
Newer Older
TOM_RUS's avatar
TOM_RUS committed
1 2
GARRISON_FOLLOWER_BUSY_COLOR = { 0, 0.06, 0.22, 0.44 };
GARRISON_FOLLOWER_INACTIVE_COLOR = { 0.22, 0.06, 0, 0.44 };
TOM_RUS's avatar
TOM_RUS committed
3

TOM_RUS's avatar
TOM_RUS committed
4 5 6 7 8
-- We default to item qualities if there is no description entry here.
GARRISON_FOLLOWER_QUALITY_DESC = {
	[6] = GARRISON_FOLLOWER_QUALITY6_DESC,
}

TOM_RUS's avatar
TOM_RUS committed
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
---------------------------------------------------------------------------------
--- Static Popup Dialogs                                                             ---
---------------------------------------------------------------------------------

StaticPopupDialogs["CONFIRM_FOLLOWER_UPGRADE"] = {
	text = "%s",
	button1 = YES,
	button2 = NO,
	OnAccept = function(self)
		C_Garrison.CastSpellOnFollower(self.data);
	end,
	showAlert = 1,
	timeout = 0,
	exclusive = 1,
	hideOnEscape = 1
};

StaticPopupDialogs["CONFIRM_FOLLOWER_ABILITY_UPGRADE"] = {
	text = "%s",
	button1 = YES,
	button2 = NO,
	OnAccept = function(self)
		C_Garrison.CastSpellOnFollowerAbility(self.data.followerID, self.data.abilityID);
	end,
	timeout = 0,
	exclusive = 1,
	hideOnEscape = 1
};

StaticPopupDialogs["CONFIRM_FOLLOWER_TEMPORARY_ABILITY"] = {
	text = CONFIRM_GARRISON_FOLLOWER_TEMPORARY_ABILITY,
	button1 = YES,
	button2 = NO,
	OnAccept = function(self)
		C_Garrison.CastSpellOnFollower(self.data);
	end,
	showAlert = 1,
	timeout = 0,
	exclusive = 1,
	hideOnEscape = 1
};

StaticPopupDialogs["CONFIRM_FOLLOWER_EQUIPMENT"] = {
	text = "%s",
	button1 = YES,
	button2 = NO,
	OnAccept = function(self)
		if (self.data.source == "spell") then
			C_Garrison.CastSpellOnFollowerAbility(self.data.followerID, self.data.abilityID);
		elseif (self.data.source == "item") then
			C_Garrison.CastItemSpellOnFollowerAbility(self.data.followerID, self.data.abilityID);
		end
	end,
	timeout = 0,
	exclusive = 1,
	hideOnEscape = 1
};

TOM_RUS's avatar
TOM_RUS committed
67 68 69
---------------------------------------------------------------------------------
--- Follower List                                                             ---
---------------------------------------------------------------------------------
TOM_RUS's avatar
TOM_RUS committed
70

TOM_RUS's avatar
TOM_RUS committed
71 72 73 74 75 76
local FOLLOWER_BUTTON_HEIGHT = 56;
local CATEGORY_BUTTON_HEIGHT = 20;
local FOLLOWER_LIST_BUTTON_OFFSET = -6;
local FOLLOWER_LIST_BUTTON_INITIAL_OFFSET = -7;
local GARRISON_FOLLOWER_LIST_BUTTON_FULL_XP_WIDTH = 205;

TOM_RUS's avatar
TOM_RUS committed
77 78
GarrisonFollowerList = {};

TOM_RUS's avatar
TOM_RUS committed
79
function GarrisonFollowerList:Initialize(followerType)
TOM_RUS's avatar
TOM_RUS committed
80 81 82 83
	self.followerTab = self:GetParent().FollowerTab;
	if (self.followerTab) then
		self.followerTab.followerList = self;
	end
TOM_RUS's avatar
TOM_RUS committed
84
	self:Setup(self:GetParent(), followerType, self.followerTemplate);
TOM_RUS's avatar
TOM_RUS committed
85 86
end

TOM_RUS's avatar
TOM_RUS committed
87 88 89
function GarrisonFollowerList:Setup(mainFrame, followerType, followerTemplate, initialOffsetX)
	self.followers = { };
	self.followersList = { };
TOM_RUS's avatar
TOM_RUS committed
90
	self:DirtyList();
TOM_RUS's avatar
TOM_RUS committed
91
	self.followerType = followerType;
TOM_RUS's avatar
TOM_RUS committed
92

TOM_RUS's avatar
TOM_RUS committed
93 94
	self.listScroll.update = function() self:UpdateData(); end;
	self.listScroll.dynamic = function(offset) return GarrisonFollowerList_GetTopButton(self, offset); end;
TOM_RUS's avatar
TOM_RUS committed
95

TOM_RUS's avatar
TOM_RUS committed
96
	if (not followerTemplate) then
TOM_RUS's avatar
TOM_RUS committed
97 98
		if followerType == Enum.GarrisonFollowerType.FollowerType_9_0 then
			followerTemplate = "CovenantMissionFollowerOrCategoryListButtonTemplate";
TOM_RUS's avatar
TOM_RUS committed
99
		else
TOM_RUS's avatar
TOM_RUS committed
100 101
			followerTemplate = "GarrisonMissionFollowerOrCategoryListButtonTemplate";
		end
TOM_RUS's avatar
TOM_RUS committed
102 103 104 105
	end
	if (not initialOffsetX) then
		initialOffsetX = 7;
	end
TOM_RUS's avatar
TOM_RUS committed
106
	HybridScrollFrame_CreateButtons(self.listScroll, followerTemplate, initialOffsetX, FOLLOWER_LIST_BUTTON_INITIAL_OFFSET, nil, nil, nil, FOLLOWER_LIST_BUTTON_OFFSET);
TOM_RUS's avatar
TOM_RUS committed
107
	self.listScroll.followerFrame = mainFrame;
TOM_RUS's avatar
TOM_RUS committed
108

TOM_RUS's avatar
TOM_RUS committed
109
	self:UpdateFollowers();
TOM_RUS's avatar
TOM_RUS committed
110 111
end

TOM_RUS's avatar
TOM_RUS committed
112
function GarrisonFollowerList:OnShow()
TOM_RUS's avatar
TOM_RUS committed
113 114
	if (self.followerTab) then
		self.followerTab.lastUpdate = GetTime();
TOM_RUS's avatar
TOM_RUS committed
115
	end
TOM_RUS's avatar
TOM_RUS committed
116
	self:StopAnimations();
TOM_RUS's avatar
TOM_RUS committed
117

TOM_RUS's avatar
TOM_RUS committed
118 119
	self:DirtyList();
	self:UpdateFollowers();
TOM_RUS's avatar
TOM_RUS committed
120
	-- if there's no follower displayed in the tab, select the first one
TOM_RUS's avatar
TOM_RUS committed
121 122 123 124 125 126 127 128
	if (self.followerTab) then
		if (not self.followerTab.followerID or not self:HasFollower(self.followerTab.followerID)) then
			local index = self:FindFirstFollower();
			if (index and index ~= 0) then
				self:ShowFollower(self.followers[index].followerID);
			else
				-- empty page
				self:ShowFollower(0);
TOM_RUS's avatar
TOM_RUS committed
129
			end
TOM_RUS's avatar
TOM_RUS committed
130 131 132
		else
			-- refresh the shown follower
			self:ShowFollower(self.followerTab.followerID);
TOM_RUS's avatar
TOM_RUS committed
133
		end
TOM_RUS's avatar
TOM_RUS committed
134
	end
TOM_RUS's avatar
TOM_RUS committed
135
	if (C_Garrison.GetNumFollowers(self.followerType) >= GarrisonFollowerOptions[self.followerType].minFollowersForThreatCountersFrame) then
TOM_RUS's avatar
TOM_RUS committed
136 137
		self:ShowThreatCountersFrame();
	end
TOM_RUS's avatar
TOM_RUS committed
138 139 140 141 142 143 144 145

	self:RegisterEvent("GARRISON_FOLLOWER_LIST_UPDATE");
	self:RegisterEvent("GARRISON_FOLLOWER_REMOVED");
	self:RegisterEvent("GARRISON_FOLLOWER_XP_CHANGED");
	self:RegisterEvent("GARRISON_FOLLOWER_UPGRADED");
	self:RegisterEvent("GARRISON_FOLLOWER_DURABILITY_CHANGED");
	self:RegisterEvent("CURRENT_SPELL_CAST_CHANGED");
	self:RegisterEvent("CURSOR_UPDATE");
TOM_RUS's avatar
TOM_RUS committed
146 147 148 149 150 151 152
end

function GarrisonFollowerList:ShowThreatCountersFrame()
	GarrisonThreatCountersFrame:Show();
end

function GarrisonFollowerList:StopAnimations()
TOM_RUS's avatar
TOM_RUS committed
153
	if (self.followerTab) then
TOM_RUS's avatar
TOM_RUS committed
154 155 156 157 158
		local abilities = self.followerTab.AbilitiesFrame.Abilities;
		if (abilities) then
		    for i, abilityFrame in ipairs(abilities) do
			    GarrisonFollowerPageAbility_StopAnimations(abilityFrame);
		    end
TOM_RUS's avatar
TOM_RUS committed
159
		end
TOM_RUS's avatar
TOM_RUS committed
160
	end
TOM_RUS's avatar
TOM_RUS committed
161 162
end

TOM_RUS's avatar
TOM_RUS committed
163
function GarrisonFollowerList:OnHide()
TOM_RUS's avatar
TOM_RUS committed
164 165
	self.followers = nil;

TOM_RUS's avatar
TOM_RUS committed
166 167 168 169 170 171 172 173
	self:UnregisterEvent("GARRISON_FOLLOWER_LIST_UPDATE");
	self:UnregisterEvent("GARRISON_FOLLOWER_REMOVED");
	self:UnregisterEvent("GARRISON_FOLLOWER_XP_CHANGED");
	self:UnregisterEvent("GARRISON_FOLLOWER_UPGRADED");
	self:UnregisterEvent("GARRISON_FOLLOWER_DURABILITY_CHANGED");
	self:UnregisterEvent("CURRENT_SPELL_CAST_CHANGED");
	self:UnregisterEvent("CURSOR_UPDATE");
end
TOM_RUS's avatar
TOM_RUS committed
174

TOM_RUS's avatar
TOM_RUS committed
175 176 177 178
function GarrisonFollowerList:FindFirstFollower(ignoreDBID)
	for i, index in ipairs(self.followersList) do
		if index ~= 0 and self.followers[self.followersList[i]].followerID ~= ignoreDBID then
			return index;
TOM_RUS's avatar
TOM_RUS committed
179 180
		end
	end
TOM_RUS's avatar
TOM_RUS committed
181
	return nil;
TOM_RUS's avatar
TOM_RUS committed
182 183
end

TOM_RUS's avatar
TOM_RUS committed
184 185 186 187 188 189 190
function GarrisonFollowerList:HasFollower(followerID)
	for i, index in ipairs(self.followersList) do
		if (index ~= 0 and (self.followers[index].followerID == followerID)) then
			return true;
		end
	end
	return false;
TOM_RUS's avatar
TOM_RUS committed
191 192
end

TOM_RUS's avatar
TOM_RUS committed
193
function GarrisonFollowerList:OnEvent(event, ...)
TOM_RUS's avatar
TOM_RUS committed
194
	if (event == "GARRISON_FOLLOWER_LIST_UPDATE" or event == "GARRISON_FOLLOWER_XP_CHANGED" or event == "GARRISON_FOLLOWER_DURABILITY_CHANGED") then
TOM_RUS's avatar
TOM_RUS committed
195 196 197 198 199
		local followerTypeID = ...;
		if (followerTypeID == self.followerType) then
			if (self.followerTab and self.followerTab.followerID and self.followerTab:IsVisible()) then
				self:ShowFollower(self.followerTab.followerID);
			end
TOM_RUS's avatar
TOM_RUS committed
200

TOM_RUS's avatar
TOM_RUS committed
201
			if (self:IsVisible() or event == "GARRISON_FOLLOWER_LIST_UPDATE") then
TOM_RUS's avatar
TOM_RUS committed
202 203
				self:DirtyList();
				self:UpdateFollowers();
TOM_RUS's avatar
TOM_RUS committed
204
			end
TOM_RUS's avatar
TOM_RUS committed
205

TOM_RUS's avatar
TOM_RUS committed
206
			if (self.followerTab and self.followerTab.followerID and self.followerTab:IsVisible()) then
TOM_RUS's avatar
TOM_RUS committed
207
				if (C_Garrison.GetNumFollowers(self.followerType) >= GarrisonFollowerOptions[self.followerType].minFollowersForThreatCountersFrame) then
TOM_RUS's avatar
TOM_RUS committed
208 209 210
					self:ShowThreatCountersFrame();
				end
			end
TOM_RUS's avatar
TOM_RUS committed
211
		end
TOM_RUS's avatar
TOM_RUS committed
212 213
		return true;
	elseif (event == "GARRISON_FOLLOWER_REMOVED") then
TOM_RUS's avatar
TOM_RUS committed
214
		local followerTypeID = ...;
TOM_RUS's avatar
TOM_RUS committed
215 216 217 218 219 220
		if (followerTypeID == self.followerType) then
			if (self.followerTab and self.followerTab.followerID and self.followerTab:IsVisible() and not C_Garrison.GetFollowerInfo(self.followerTab.followerID) and self.followers) then
				-- viewed follower got removed, pick someone else
				local ignoreFollowerDBID = self.followerTab.followerID;
				local index = self:FindFirstFollower(ignoreFollowerDBID);
				if (index and index ~= 0) then
TOM_RUS's avatar
TOM_RUS committed
221
					self:ShowFollower(self.followers[index].followerID);
TOM_RUS's avatar
TOM_RUS committed
222
				else
TOM_RUS's avatar
TOM_RUS committed
223
					self:ShowFollower(0);
TOM_RUS's avatar
TOM_RUS committed
224 225
				end
			end
TOM_RUS's avatar
TOM_RUS committed
226 227 228 229
			if (self:IsVisible()) then
				self:DirtyList();
				self:UpdateFollowers();
			end
TOM_RUS's avatar
TOM_RUS committed
230 231
		end
		return true;
TOM_RUS's avatar
TOM_RUS committed
232
	elseif (event == "GARRISON_FOLLOWER_UPGRADED") then
TOM_RUS's avatar
TOM_RUS committed
233
		if ( self.followerTab and self.followerTab.followerID and self.followerTab:IsVisible() ) then
TOM_RUS's avatar
TOM_RUS committed
234
			local followerID = ...;
TOM_RUS's avatar
TOM_RUS committed
235 236
			if ( followerID == self.followerTab.followerID ) then
				self.followerTab.ModelCluster.Child.Model[1]:SetSpellVisualKit(6375);	-- level up visual;
TOM_RUS's avatar
TOM_RUS committed
237
				PlaySound(SOUNDKIT.UI_GARRISON_COMMAND_TABLE_FOLLOWER_LEVEL_UP);
TOM_RUS's avatar
TOM_RUS committed
238 239
			end
		end
TOM_RUS's avatar
TOM_RUS committed
240
	elseif (event == "CURRENT_SPELL_CAST_CHANGED" or event == "CURSOR_UPDATE") then
TOM_RUS's avatar
TOM_RUS committed
241
		if (self.followerTab and self.followerTab.followerID and self:IsVisible()) then
TOM_RUS's avatar
TOM_RUS committed
242 243
			local followerID = self.followerTab.followerID;
			local followerInfo = C_Garrison.GetFollowerInfo(followerID);
TOM_RUS's avatar
TOM_RUS committed
244 245 246 247
			if (followerInfo) then
				followerInfo.abilities = C_Garrison.GetFollowerAbilities(followerID);
				self:UpdateValidSpellHighlight(followerID, followerInfo);
			end
TOM_RUS's avatar
TOM_RUS committed
248
		end
TOM_RUS's avatar
TOM_RUS committed
249 250 251 252 253
	end

	return false;
end

TOM_RUS's avatar
TOM_RUS committed
254 255 256 257

GarrisonMissionFollowerDurabilityMixin = { }

function GarrisonMissionFollowerDurabilityMixin:SetDurability(durability, maxDurability, durabilityLoss)
TOM_RUS's avatar
TOM_RUS committed
258
	local heartWidth = 13;
TOM_RUS's avatar
TOM_RUS committed
259 260
	local spacing = 2;

TOM_RUS's avatar
TOM_RUS committed
261 262 263 264
	self.durabilityVal = durability;
	self.maxDurabilityVal = maxDurability;
	self.durabilityLossVal = durabilityLoss;

TOM_RUS's avatar
TOM_RUS committed
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
	durability = Clamp(durability, 0, maxDurability);
	durabilityLoss = Clamp(durabilityLoss or 0, 0, durability);
	durability = durability - durabilityLoss;
	while ((self.durability and #self.durability or 0) < maxDurability) do
		local durabilityTexture = self:CreateTexture(nil, "ARTWORK", "GarrisonMissionFollowerButtonDurabilityTemplate");
		durabilityTexture:ClearAllPoints();
		if (#self.durability == 1) then
			durabilityTexture:SetPoint("TOPLEFT");
		else
			durabilityTexture:SetPoint("TOPLEFT", self.durability[#self.durability - 1], "TOPRIGHT", spacing, 0);
		end
	end

	for i = 1, durability do
		self.durability[i]:Show();
		self.durability[i]:SetAtlas("GarrisonTroops-Health");
		self.durability[i]:SetDesaturated(false);
	end
	for i = durability + 1, durability + durabilityLoss do
		self.durability[i]:Show();
		self.durability[i]:SetAtlas("GarrisonTroops-Health-Consume");
		self.durability[i]:SetDesaturated(false);
	end
	for i = durability + durabilityLoss + 1, maxDurability do
		self.durability[i]:Show();
		self.durability[i]:SetAtlas("GarrisonTroops-Health");
		self.durability[i]:SetDesaturated(true);
	end
	for i = maxDurability + 1, (self.durability and #self.durability or 0) do
		self.durability[i]:Hide();
	end

TOM_RUS's avatar
TOM_RUS committed
297
	local width = max(1, maxDurability * (heartWidth + spacing));
TOM_RUS's avatar
TOM_RUS committed
298 299 300
	self:SetWidth(width);
end

TOM_RUS's avatar
TOM_RUS committed
301 302 303 304
function GarrisonMissionFollowerDurabilityMixin:GetDurability()
	return self.durabilityVal, self.maxDurabilityVal, self.durabilityLossVal;
end

TOM_RUS's avatar
TOM_RUS committed
305 306 307 308 309 310
GarrisonFollowerListButton = { };

function GarrisonFollowerListButton:GetFollowerList()
	return self:GetParent():GetParent():GetParent();
end

TOM_RUS's avatar
TOM_RUS committed
311 312 313
function GarrisonFollowerListButton_OnDragStart(self, button)
	local mainFrame = self:GetFollowerList():GetParent();
	if (mainFrame.OnDragStartFollowerButton) then
TOM_RUS's avatar
TOM_RUS committed
314
		mainFrame:OnDragStartFollowerButton(mainFrame:GetPlacerFrame(), self, 24);
TOM_RUS's avatar
TOM_RUS committed
315 316 317 318 319 320
	end
end

function GarrisonFollowerListButton_OnDragStop(self)
	local mainFrame = self:GetFollowerList():GetParent();
	if (mainFrame.OnDragStopFollowerButton) then
TOM_RUS's avatar
TOM_RUS committed
321
		mainFrame:OnDragStopFollowerButton(mainFrame:GetPlacerFrame());
TOM_RUS's avatar
TOM_RUS committed
322 323 324
	end
end

TOM_RUS's avatar
TOM_RUS committed
325 326 327 328 329 330
GarrisonMissionFollowerOrCategoryListButtonMixin = { }

function GarrisonMissionFollowerOrCategoryListButtonMixin:GetFollowerList()
	return self:GetParent():GetParent():GetParent():GetParent();
end

TOM_RUS's avatar
TOM_RUS committed
331
function GarrisonFollowListEditBox_OnTextChanged(self)
TOM_RUS's avatar
TOM_RUS committed
332
	SearchBoxTemplate_OnTextChanged(self);
TOM_RUS's avatar
TOM_RUS committed
333
	self:GetParent():UpdateFollowers();
TOM_RUS's avatar
TOM_RUS committed
334 335
end

TOM_RUS's avatar
TOM_RUS committed
336 337 338 339 340 341 342 343 344 345 346 347 348 349
function GarrisonFollowerList:DoesFollowerMatchFilters(follower, searchString)
	if (not follower.isCollected and not self.showUncollected) then
		return false;
	end
	if (searchString ~= "" and not C_Garrison.SearchForFollower(follower.followerID, searchString)) then
		return false;
	end
	if (self.filter and not self.filter(follower)) then
		return false;
	end

	return true;
end

TOM_RUS's avatar
TOM_RUS committed
350
function GarrisonFollowerList:UpdateFollowers()
TOM_RUS's avatar
TOM_RUS committed
351 352

	if ( self.dirtyList ) then
TOM_RUS's avatar
TOM_RUS committed
353
		self.followers = C_Garrison.GetFollowers(self.followerType) or {};
TOM_RUS's avatar
TOM_RUS committed
354 355 356 357
		local autoCombatTroops = C_Garrison.GetAutoTroops(self.followerType);
		for i = 1, #autoCombatTroops do
			table.insert(self.followers, autoCombatTroops[i]);
		end
TOM_RUS's avatar
TOM_RUS committed
358
		self.dirtyList = nil;
TOM_RUS's avatar
TOM_RUS committed
359
	end
TOM_RUS's avatar
TOM_RUS committed
360

TOM_RUS's avatar
TOM_RUS committed
361
	if ( not self.followers ) then
TOM_RUS's avatar
TOM_RUS committed
362 363 364
		return;
	end

TOM_RUS's avatar
TOM_RUS committed
365 366
	--TODO: Tagify GetFollowers and condense this into it
	for i = 1, #self.followers do
TOM_RUS's avatar
TOM_RUS committed
367 368
		if self.followers[i].followerID then
			self.followers[i].autoCombatSpells = C_Garrison.GetFollowerAutoCombatSpells(self.followers[i].followerID, self.followers[i].level);
TOM_RUS's avatar
TOM_RUS committed
369
			self.followers[i].autoCombatantStats = C_Garrison.GetFollowerAutoCombatStats(self.followers[i].followerID);
TOM_RUS's avatar
TOM_RUS committed
370 371 372
		end
	end

TOM_RUS's avatar
TOM_RUS committed
373 374
	self.followersList = { };
	self.followersLabels = { };
TOM_RUS's avatar
TOM_RUS committed
375

TOM_RUS's avatar
TOM_RUS committed
376
	local searchString = "";
TOM_RUS's avatar
TOM_RUS committed
377 378
	if ( self.SearchBox ) then
		searchString = self.SearchBox:GetText();
TOM_RUS's avatar
TOM_RUS committed
379
	end
TOM_RUS's avatar
TOM_RUS committed
380

TOM_RUS's avatar
TOM_RUS committed
381 382 383 384 385
	local numActive = 0;
	local numTroops = 0;
	local numInactive = 0;
	local numUncollected = 0;
	for i = 1, #self.followers do
TOM_RUS's avatar
TOM_RUS committed
386
		if self:DoesFollowerMatchFilters(self.followers[i], searchString) then
TOM_RUS's avatar
TOM_RUS committed
387
			tinsert(self.followersList, i);
TOM_RUS's avatar
TOM_RUS committed
388 389
			if (not self.followers[i].isCollected) then
				numUncollected = numUncollected + 1;
TOM_RUS's avatar
TOM_RUS committed
390
			elseif (self.followers[i].isTroop or self.followers[i].isAutoTroop) then
TOM_RUS's avatar
TOM_RUS committed
391 392 393 394 395 396
				numTroops = numTroops + 1;
			elseif (self.followers[i].status == GARRISON_FOLLOWER_INACTIVE) then
				numInactive = numInactive + 1;
			else
				numActive = numActive + 1;
			end
TOM_RUS's avatar
TOM_RUS committed
397 398 399
		end
	end

TOM_RUS's avatar
TOM_RUS committed
400
	if ( self.followerTab and GarrisonFollowerOptions[self.followerType].showNumFollowers) then
TOM_RUS's avatar
TOM_RUS committed
401
		local maxFollowers = C_Garrison.GetFollowerSoftCap(self.followerType);
TOM_RUS's avatar
TOM_RUS committed
402
		local numActiveFollowers = C_Garrison.GetNumActiveFollowers(self.followerType) or 0;
TOM_RUS's avatar
TOM_RUS committed
403 404
		if ( self.isLandingPage ) then
			local countColor = HIGHLIGHT_FONT_COLOR_CODE;
TOM_RUS's avatar
TOM_RUS committed
405
			if ( numActiveFollowers > maxFollowers ) then
TOM_RUS's avatar
TOM_RUS committed
406 407
				countColor = RED_FONT_COLOR_CODE;
			end
TOM_RUS's avatar
TOM_RUS committed
408
			self.followerTab.NumFollowers:SetText(countColor..numActiveFollowers.."/"..maxFollowers..FONT_COLOR_CODE_CLOSE);
TOM_RUS's avatar
TOM_RUS committed
409
			self.followerTab.FollowerText:SetText(GarrisonFollowerOptions[self.followerType].strings.FOLLOWER_COUNT_LABEL);
TOM_RUS's avatar
TOM_RUS committed
410 411
		else
			local countColor = NORMAL_FONT_COLOR_CODE;
TOM_RUS's avatar
TOM_RUS committed
412
			if ( numActiveFollowers > maxFollowers ) then
TOM_RUS's avatar
TOM_RUS committed
413 414
				countColor = RED_FONT_COLOR_CODE;
			end
TOM_RUS's avatar
TOM_RUS committed
415
			self.followerTab.NumFollowers:SetText(format(GarrisonFollowerOptions[self.followerType].strings.FOLLOWER_COUNT_STRING, countColor, numActiveFollowers, maxFollowers, FONT_COLOR_CODE_CLOSE));
TOM_RUS's avatar
TOM_RUS committed
416 417 418 419
		end
	end

	GarrisonFollowerList_SortFollowers(self);
TOM_RUS's avatar
TOM_RUS committed
420

TOM_RUS's avatar
TOM_RUS committed
421 422 423 424
	if (GarrisonFollowerOptions[self.followerType].showCategoriesInFollowerList) then
		-- The sort above will yield the following sort order: Active followers, troops, inactive followers. Insert new entries for the appropriate
		-- category labels at the correct locations. Category labels will have "followerID" set to 0.

TOM_RUS's avatar
TOM_RUS committed
425
	    local additionalOffset = 0;
TOM_RUS's avatar
TOM_RUS committed
426 427 428

		additionalOffset = additionalOffset + 1;
		tinsert(self.followersList, 0 + additionalOffset, 0);
TOM_RUS's avatar
TOM_RUS committed
429
		self.followersLabels[0 + additionalOffset] = GarrisonFollowerOptions[self.followerType].strings.FOLLOWERLIST_LABEL_FOLLOWERS;
TOM_RUS's avatar
TOM_RUS committed
430 431 432

		additionalOffset = additionalOffset + 1;
		tinsert(self.followersList, numActive + additionalOffset, 0);
TOM_RUS's avatar
TOM_RUS committed
433
		self.followersLabels[numActive + additionalOffset] = GarrisonFollowerOptions[self.followerType].strings.FOLLOWERLIST_LABEL_TROOPS;
TOM_RUS's avatar
TOM_RUS committed
434

TOM_RUS's avatar
TOM_RUS committed
435 436 437 438 439 440 441 442 443 444 445
	    if ( numInactive > 0 ) then
		    additionalOffset = additionalOffset + 1;
		    tinsert(self.followersList, numActive + numTroops + additionalOffset, 0);
		    self.followersLabels[numActive + numTroops + additionalOffset] = FOLLOWERLIST_LABEL_INACTIVE;
	    end
		if ( numUncollected > 0 ) then
		    additionalOffset = additionalOffset + 1;
			tinsert(self.followersList, numActive + numTroops + numInactive + additionalOffset, 0)
		    self.followersLabels[numActive + numTroops + numInactive + additionalOffset] = FOLLOWERLIST_LABEL_UNCOLLECTED;
		end
	end
TOM_RUS's avatar
TOM_RUS committed
446
	self:UpdateData();
TOM_RUS's avatar
TOM_RUS committed
447 448
end

TOM_RUS's avatar
TOM_RUS committed
449
function GarrisonFollowerList_GetTopButton(self, offset)
TOM_RUS's avatar
TOM_RUS committed
450
	local followerFrame = self;
TOM_RUS's avatar
TOM_RUS committed
451 452 453 454 455 456 457
	local buttonHeight = followerFrame.listScroll.buttonHeight;
	local expandedFollower = followerFrame.expandedFollower;
	local followers = followerFrame.followers;
	local sortedList = followerFrame.followersList;
	local totalHeight = 0;
	for i = 1, #sortedList do
		local height;
TOM_RUS's avatar
TOM_RUS committed
458 459 460
		if ( sortedList[i] == 0 ) then
			height = CATEGORY_BUTTON_HEIGHT - FOLLOWER_LIST_BUTTON_OFFSET;
		elseif ( followers[sortedList[i]].followerID == expandedFollower ) then
TOM_RUS's avatar
TOM_RUS committed
461 462
			height = followerFrame.expandedFollowerHeight;
		else
TOM_RUS's avatar
TOM_RUS committed
463
			height = FOLLOWER_BUTTON_HEIGHT - FOLLOWER_LIST_BUTTON_OFFSET;
TOM_RUS's avatar
TOM_RUS committed
464 465 466 467 468 469 470 471 472 473 474
		end
		totalHeight = totalHeight + height;
		if ( totalHeight > offset ) then
			return i - 1, height + offset - totalHeight;
		end
	end

	--We're scrolled completely off the bottom
	return #followers, 0;
end

TOM_RUS's avatar
TOM_RUS committed
475 476
function GarrisonFollowerList_SetButtonMode(followerList, button, mode)
	if (mode == "CATEGORY") then
TOM_RUS's avatar
TOM_RUS committed
477
		followerList:CollapseButton(button.Follower);
TOM_RUS's avatar
TOM_RUS committed
478
		button:SetHeight(CATEGORY_BUTTON_HEIGHT);
TOM_RUS's avatar
TOM_RUS committed
479
	else
TOM_RUS's avatar
TOM_RUS committed
480 481 482 483
		button:SetHeight(FOLLOWER_BUTTON_HEIGHT);
	end

	button.mode = mode;
TOM_RUS's avatar
TOM_RUS committed
484 485
	button.Follower:SetShown(mode == "FOLLOWER");
	button.Category:SetShown(mode == "CATEGORY");
TOM_RUS's avatar
TOM_RUS committed
486 487
end

TOM_RUS's avatar
TOM_RUS committed
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
function GarrisonFollowerList:UpdateMissionRemainingTime(follower, fontString)
	if follower.status == GARRISON_FOLLOWER_ON_MISSION then
		local missionTimeLeft = C_Garrison.GetFollowerMissionTimeLeft(follower.followerID);
		if missionTimeLeft then
			if follower.isMaxLevel then
				fontString:SetText(missionTimeLeft);
			else
				fontString:SetFormattedText(GarrisonFollowerOptions[follower.followerTypeID].strings.OUT_WITH_DURATION, missionTimeLeft);
			end
		else
			fontString:SetText("");
		end
	end
end

TOM_RUS's avatar
TOM_RUS committed
503 504 505 506
function GarrisonFollowerList:UpdateData()
	local followerFrame = self:GetParent();
	local followers = self.followers;
	local followersList = self.followersList;
TOM_RUS's avatar
TOM_RUS committed
507
	local categoryLabels = self.followersLabels;
TOM_RUS's avatar
TOM_RUS committed
508
	local numFollowers = #followersList;
TOM_RUS's avatar
TOM_RUS committed
509
	local scrollFrame = self.listScroll;
TOM_RUS's avatar
TOM_RUS committed
510 511 512
	local offset = HybridScrollFrame_GetOffset(scrollFrame);
	local buttons = scrollFrame.buttons;
	local numButtons = #buttons;
TOM_RUS's avatar
TOM_RUS committed
513 514
	local showCounters = self.showCounters;
	local canExpand = self.canExpand;
TOM_RUS's avatar
TOM_RUS committed
515 516
	local totalHeight = -FOLLOWER_LIST_BUTTON_INITIAL_OFFSET;

TOM_RUS's avatar
TOM_RUS committed
517 518 519
	for i = 1, numButtons do
		local button = buttons[i];
		local index = offset + i; -- adjust index
TOM_RUS's avatar
TOM_RUS committed
520 521
		if ( index <= numFollowers and followersList[index] == 0 ) then
			GarrisonFollowerList_SetButtonMode(self, button, "CATEGORY");
TOM_RUS's avatar
TOM_RUS committed
522
			button.Category:SetText(categoryLabels[index]);
TOM_RUS's avatar
TOM_RUS committed
523 524
			button:Show();
		elseif ( index <= numFollowers ) then
TOM_RUS's avatar
TOM_RUS committed
525
			local follower = followers[followersList[index]];
TOM_RUS's avatar
TOM_RUS committed
526

TOM_RUS's avatar
TOM_RUS committed
527 528
			GarrisonFollowerList_SetButtonMode(self, button, "FOLLOWER");
			button.Follower.DurabilityFrame:SetShown(follower.isTroop);
TOM_RUS's avatar
TOM_RUS committed
529

TOM_RUS's avatar
TOM_RUS committed
530 531 532
			button.Follower.id = follower.followerID;
			button.Follower.info = follower;
			button.Follower.Name:SetText(follower.name);
TOM_RUS's avatar
TOM_RUS committed
533 534 535
			if ( button.Follower.Class) then
				button.Follower.Class:SetAtlas(follower.classAtlas);
			end
TOM_RUS's avatar
TOM_RUS committed
536
			button.Follower.Status:SetText(follower.status);
TOM_RUS's avatar
TOM_RUS committed
537
			if ( follower.status == GARRISON_FOLLOWER_INACTIVE ) then
TOM_RUS's avatar
TOM_RUS committed
538
				button.Follower.Status:SetTextColor(1, 0.1, 0.1);
TOM_RUS's avatar
TOM_RUS committed
539
			else
TOM_RUS's avatar
TOM_RUS committed
540
				button.Follower.Status:SetTextColor(0.698, 0.941, 1);
TOM_RUS's avatar
TOM_RUS committed
541
			end
TOM_RUS's avatar
TOM_RUS committed
542
			button.Follower.PortraitFrame:SetupPortrait(follower);
TOM_RUS's avatar
TOM_RUS committed
543

TOM_RUS's avatar
TOM_RUS committed
544
			local abilityGridAreaWidth = GarrisonFollowerButton_UpdateCounters(self:GetParent(), button.Follower, follower, showCounters, followerFrame.lastUpdate);
TOM_RUS's avatar
TOM_RUS committed
545
			if not showCounters then
TOM_RUS's avatar
TOM_RUS committed
546 547 548
				--This should be used to replace counter width, as they're currently exclusive sets.
				abilityGridAreaWidth = GarrisonFollowerButton_UpdateAutoSpells(self:GetParent(), button.Follower, follower);
			end
TOM_RUS's avatar
TOM_RUS committed
549

TOM_RUS's avatar
TOM_RUS committed
550 551
			if ( follower.isCollected ) then
				-- have this follower
TOM_RUS's avatar
TOM_RUS committed
552 553
				button.Follower.isCollected = true;
				button.Follower.Name:SetTextColor(NORMAL_FONT_COLOR.r, NORMAL_FONT_COLOR.g, NORMAL_FONT_COLOR.b);
TOM_RUS's avatar
TOM_RUS committed
554 555 556 557 558
				if( button.Follower.Class ) then
					button.Follower.Class:SetDesaturated(false);
					button.Follower.Class:SetAlpha(0.2);
				end
				if button.Follower.PortraitFrame.quality ~= Enum.GarrFollowerQuality.Title then
TOM_RUS's avatar
TOM_RUS committed
559 560
					button.Follower.PortraitFrame.PortraitRingQuality:Show();
				end
TOM_RUS's avatar
TOM_RUS committed
561
				button.Follower.PortraitFrame.Portrait:SetDesaturated(false);
TOM_RUS's avatar
TOM_RUS committed
562
				if ( follower.status == GARRISON_FOLLOWER_INACTIVE ) then
TOM_RUS's avatar
TOM_RUS committed
563 564 565 566
					button.Follower.PortraitFrame.PortraitRingCover:Show();
					button.Follower.PortraitFrame.PortraitRingCover:SetAlpha(0.5);
					button.Follower.BusyFrame:Show();
					button.Follower.BusyFrame.Texture:SetColorTexture(unpack(GARRISON_FOLLOWER_INACTIVE_COLOR));
TOM_RUS's avatar
TOM_RUS committed
567
				elseif ( follower.status ) then
TOM_RUS's avatar
TOM_RUS committed
568 569 570 571
					button.Follower.PortraitFrame.PortraitRingCover:Show();
					button.Follower.PortraitFrame.PortraitRingCover:SetAlpha(0.5);
					button.Follower.BusyFrame:Show();
					button.Follower.BusyFrame.Texture:SetColorTexture(unpack(GARRISON_FOLLOWER_BUSY_COLOR));
TOM_RUS's avatar
TOM_RUS committed
572
					-- get time remaining for follower
TOM_RUS's avatar
TOM_RUS committed
573
					self:UpdateMissionRemainingTime(follower, button.Follower.Status);
TOM_RUS's avatar
TOM_RUS committed
574
				else
TOM_RUS's avatar
TOM_RUS committed
575 576
					button.Follower.PortraitFrame.PortraitRingCover:Hide();
					button.Follower.BusyFrame:Hide();
TOM_RUS's avatar
TOM_RUS committed
577
				end
TOM_RUS's avatar
TOM_RUS committed
578 579 580 581 582 583
				if( button.Follower.DownArrow ) then
					if ( canExpand ) then
						button.Follower.DownArrow:SetAlpha(1);
					else
						button.Follower.DownArrow:SetAlpha(0);
					end
TOM_RUS's avatar
TOM_RUS committed
584
				end
TOM_RUS's avatar
TOM_RUS committed
585
				-- adjust text position if we have additional text to show below name
TOM_RUS's avatar
TOM_RUS committed
586 587 588
				local nameOffsetY = 0;
				if (follower.status) then
					nameOffsetY = nameOffsetY + 8;
TOM_RUS's avatar
TOM_RUS committed
589
				end
TOM_RUS's avatar
TOM_RUS committed
590
				-- show iLevel for max level followers
TOM_RUS's avatar
TOM_RUS committed
591
				if (ShouldShowILevelInFollowerList(follower)) then
TOM_RUS's avatar
TOM_RUS committed
592 593 594 595
					nameOffsetY = nameOffsetY + 9;
					if (COLLAPSE_ORDER_HALL_FOLLOWER_ITEM_LEVEL_DISPLAY) then
						button.Follower.ILevel:SetPoint("TOPLEFT", button.Follower.Name, "BOTTOMLEFT", 0, -1);
						button.Follower.Status:SetPoint("TOPLEFT", button.Follower.ILevel, "BOTTOMLEFT", -1, 1);
TOM_RUS's avatar
TOM_RUS committed
596
					else
TOM_RUS's avatar
TOM_RUS committed
597 598
						button.Follower.ILevel:SetPoint("TOPLEFT", button.Follower.Name, "BOTTOMLEFT", 0, -4);
						button.Follower.Status:SetPoint("TOPLEFT", button.Follower.ILevel, "BOTTOMLEFT", -1, -2);
TOM_RUS's avatar
TOM_RUS committed
599
					end
TOM_RUS's avatar
TOM_RUS committed
600
					button.Follower.ILevel:SetText(POWER_LEVEL_ABBR.." "..follower.iLevel);
TOM_RUS's avatar
TOM_RUS committed
601
					button.Follower.ILevel:Show();
TOM_RUS's avatar
TOM_RUS committed
602
				else
TOM_RUS's avatar
TOM_RUS committed
603
					button.Follower.ILevel:SetText(nil);
TOM_RUS's avatar
TOM_RUS committed
604
					button.Follower.ILevel:Hide();
TOM_RUS's avatar
TOM_RUS committed
605 606
					button.Follower.Status:SetPoint("TOPLEFT", button.Follower.Name, "BOTTOMLEFT", 0, -2);
				end
TOM_RUS's avatar
TOM_RUS committed
607

TOM_RUS's avatar
TOM_RUS committed
608 609
				if (button.Follower.DurabilityFrame:IsShown()) then
					nameOffsetY = nameOffsetY + 9;
TOM_RUS's avatar
TOM_RUS committed
610

TOM_RUS's avatar
TOM_RUS committed
611 612 613 614
					if (follower.status) then
						button.Follower.DurabilityFrame:SetPoint("TOPLEFT", button.Follower.Status, "BOTTOMLEFT", 0, -4);
					elseif (ShouldShowILevelInFollowerList(follower)) then
						button.Follower.DurabilityFrame:SetPoint("TOPLEFT", button.Follower.ILevel, "BOTTOMLEFT", 0, -6);
TOM_RUS's avatar
TOM_RUS committed
615
					else
TOM_RUS's avatar
TOM_RUS committed
616
						button.Follower.DurabilityFrame:SetPoint("TOPLEFT", button.Follower.Name, "BOTTOMLEFT", 0, -6);
TOM_RUS's avatar
TOM_RUS committed
617
					end
TOM_RUS's avatar
TOM_RUS committed
618
				end
TOM_RUS's avatar
TOM_RUS committed
619
				button.Follower.Name:SetPoint("LEFT", button.Follower.PortraitFrame, "LEFT", 66, nameOffsetY);
TOM_RUS's avatar
TOM_RUS committed
620
				button.Follower.Status:SetPoint("RIGHT", -abilityGridAreaWidth, 0);
TOM_RUS's avatar
TOM_RUS committed
621

TOM_RUS's avatar
TOM_RUS committed
622 623 624 625 626 627 628
				if ( button.Follower.XPBar ) then
					if (follower.xp == 0 or follower.levelXP == 0) then
						button.Follower.XPBar:Hide();
					else
						button.Follower.XPBar:Show();
						button.Follower.XPBar:SetWidth((follower.xp/follower.levelXP) * GARRISON_FOLLOWER_LIST_BUTTON_FULL_XP_WIDTH);
					end
TOM_RUS's avatar
TOM_RUS committed
629 630 631
				end
			else
				-- don't have this follower
TOM_RUS's avatar
TOM_RUS committed
632 633 634
				button.Follower.isCollected = nil;
				button.Follower.Name:SetTextColor(0.25, 0.25, 0.25);
				button.Follower.ILevel:SetText(nil);
TOM_RUS's avatar
TOM_RUS committed
635
				button.Follower.Status:SetPoint("TOPLEFT", button.Follower.ILevel, "TOPRIGHT", 0, 0);
TOM_RUS's avatar
TOM_RUS committed
636 637 638 639
				if( button.Follower.Class ) then
					button.Follower.Class:SetDesaturated(true);
					button.Follower.Class:SetAlpha(0.1);
				end
TOM_RUS's avatar
TOM_RUS committed
640 641 642 643 644
				button.Follower.PortraitFrame.PortraitRingQuality:Hide();
				button.Follower.PortraitFrame.Portrait:SetDesaturated(true);
				button.Follower.PortraitFrame.PortraitRingCover:Show();
				button.Follower.PortraitFrame.PortraitRingCover:SetAlpha(0.6);
				button.Follower.PortraitFrame:SetQuality(0);
TOM_RUS's avatar
TOM_RUS committed
645 646 647
				if ( button.Follower.XPBar ) then
					button.Follower.XPBar:Hide();
				end
TOM_RUS's avatar
TOM_RUS committed
648 649
				button.Follower.DownArrow:SetAlpha(0);
				button.Follower.BusyFrame:Hide();
TOM_RUS's avatar
TOM_RUS committed
650 651
			end

TOM_RUS's avatar
TOM_RUS committed
652 653
			if (canExpand and button.Follower.id == self.expandedFollower and button.Follower.id == followerFrame.selectedFollower) then
				self:ExpandButton(button.Follower, self);
TOM_RUS's avatar
TOM_RUS committed
654
			else
TOM_RUS's avatar
TOM_RUS committed
655
				self:CollapseButton(button.Follower);
TOM_RUS's avatar
TOM_RUS committed
656
			end
TOM_RUS's avatar
TOM_RUS committed
657

TOM_RUS's avatar
TOM_RUS committed
658 659 660
			button:SetHeight(button.Follower:GetHeight());
			if ( button.Follower.id == followerFrame.selectedFollower ) then
				button.Follower.Selection:Show();
TOM_RUS's avatar
TOM_RUS committed
661
			else
TOM_RUS's avatar
TOM_RUS committed
662
				button.Follower.Selection:Hide();
TOM_RUS's avatar
TOM_RUS committed
663 664 665
			end

			if (follower.isTroop) then
TOM_RUS's avatar
TOM_RUS committed
666
				button.Follower.DurabilityFrame:SetDurability(follower.durability, follower.maxDurability);
TOM_RUS's avatar
TOM_RUS committed
667
			end
TOM_RUS's avatar
TOM_RUS committed
668

TOM_RUS's avatar
TOM_RUS committed
669 670 671 672 673
			button:Show();
		else
			button:Hide();
		end
	end
TOM_RUS's avatar
TOM_RUS committed
674

TOM_RUS's avatar
TOM_RUS committed
675 676 677 678 679 680 681
	-- calculate the total height to pass to the HybridScrollFrame
	for i = 1, numFollowers do
		if (followersList[i] == 0) then
			totalHeight = totalHeight + CATEGORY_BUTTON_HEIGHT - FOLLOWER_LIST_BUTTON_OFFSET;
		else
			totalHeight = totalHeight + FOLLOWER_BUTTON_HEIGHT - FOLLOWER_LIST_BUTTON_OFFSET;
		end
TOM_RUS's avatar
TOM_RUS committed
682
	end
TOM_RUS's avatar
TOM_RUS committed
683 684 685 686
	if (self.expandedFollower) then
		totalHeight = totalHeight + self.expandedFollowerHeight - (FOLLOWER_BUTTON_HEIGHT - FOLLOWER_LIST_BUTTON_OFFSET);
	end

TOM_RUS's avatar
TOM_RUS committed
687 688
	local displayedHeight = numButtons * scrollFrame.buttonHeight;
	HybridScrollFrame_Update(scrollFrame, totalHeight, displayedHeight);
TOM_RUS's avatar
TOM_RUS committed
689 690

	followerFrame.lastUpdate = GetTime();
TOM_RUS's avatar
TOM_RUS committed
691 692
end

TOM_RUS's avatar
TOM_RUS committed
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
function GarrisonFollowerButton_AddCounterButtons(button, follower, numShown, counters, lastUpdate)
	if (not counters) then
		return numShown;
	end
	for i = 1, min(4 - numShown, #counters) do	-- max of 4 icons
		numShown = numShown + 1;
		GarrisonFollowerButton_SetCounterButton(button, follower.followerID, numShown, counters[i], lastUpdate, follower.followerTypeID);
	end

	return numShown;
end

local function GetFollowerButtonCounterSpacings(followerTypeID)
	local options = GarrisonFollowerOptions[followerTypeID];
	local numPerRow = options.followerListCounterNumPerRow;
	local innerSpacing = options.followerListCounterInnerSpacing;
	local outerSpacingX = options.followerListCounterOuterSpacingX;
	local outerSpacingY = options.followerListCounterOuterSpacingY;
	local scale = options.followerListCounterScale;

	return numPerRow, innerSpacing, outerSpacingX, outerSpacingY, scale;
end

TOM_RUS's avatar
TOM_RUS committed
716
function GarrisonFollowerButton_UpdateCounters(frame, button, follower, showCounters, lastUpdate)
TOM_RUS's avatar
TOM_RUS committed
717
	local numShown = 0;
TOM_RUS's avatar
TOM_RUS committed
718
	if ( showCounters and follower.isCollected and follower.status ~= GARRISON_FOLLOWER_INACTIVE ) then
TOM_RUS's avatar
TOM_RUS committed
719 720
		--if a mission is being viewed, show mechanics this follower can counter
		--for followers you have, show counters if they are or could be on the mission
TOM_RUS's avatar
TOM_RUS committed
721
		local counters = frame.followerCounters and frame.followerCounters[follower.followerID];
TOM_RUS's avatar
TOM_RUS committed
722
		if ( counters ) then
TOM_RUS's avatar
TOM_RUS committed
723
			if ( follower.followerTypeID == Enum.GarrisonFollowerType.FollowerType_6_2 ) then
TOM_RUS's avatar
TOM_RUS committed
724 725
				table.sort(counters, function(left, right) return left.factor > right.factor; end);
			end
TOM_RUS's avatar
TOM_RUS committed
726
		end
TOM_RUS's avatar
TOM_RUS committed
727 728 729

		numShown = GarrisonFollowerButton_AddCounterButtons(button, follower, numShown, counters, lastUpdate);

TOM_RUS's avatar
TOM_RUS committed
730
		local traits = frame.followerTraits and frame.followerTraits[follower.followerID];
TOM_RUS's avatar
TOM_RUS committed
731 732 733 734 735
		numShown = GarrisonFollowerButton_AddCounterButtons(button, follower, numShown, traits, lastUpdate);

		local spells = frame.followerSpells and frame.followerSpells[follower.followerID];
		numShown = GarrisonFollowerButton_AddCounterButtons(button, follower, numShown, spells, lastUpdate);

TOM_RUS's avatar
TOM_RUS committed
736
	end
TOM_RUS's avatar
TOM_RUS committed
737 738 739 740 741
	local numPerRow, innerSpacing, outerSpacingX, outerSpacingY, scale = GetFollowerButtonCounterSpacings(follower.followerTypeID);
	button.Counters[1]:ClearAllPoints();
	if ( numShown <= numPerRow ) then
		local collapsedButtonHeight = 46;
		button.Counters[1]:SetPoint("RIGHT", button.Counters[1]:GetParent(), "TOPRIGHT", -outerSpacingX, -collapsedButtonHeight/2);
TOM_RUS's avatar
TOM_RUS committed
742
	else
TOM_RUS's avatar
TOM_RUS committed
743
		button.Counters[1]:SetPoint("TOPRIGHT", -outerSpacingX, -outerSpacingY);
TOM_RUS's avatar
TOM_RUS committed
744 745 746 747 748
	end
	for i = numShown + 1, #button.Counters do
		button.Counters[i].info = nil;
		button.Counters[i]:Hide();
	end
TOM_RUS's avatar
TOM_RUS committed
749 750 751 752 753 754 755 756 757

	-- return the counters area width
	if (numShown == 0) then
		return 0;
	elseif (numShown == 1) then
		return 2 * outerSpacingX + button.Counters[1]:GetWidth() * scale;
	else
		return 2 * outerSpacingX + innerSpacing + (2 * button.Counters[1]:GetWidth() * scale);
	end
TOM_RUS's avatar
TOM_RUS committed
758 759
end

TOM_RUS's avatar
TOM_RUS committed
760
function GarrisonFollowerButton_SetCounterButton(button, followerID, index, info, lastUpdate, followerTypeID)
TOM_RUS's avatar
TOM_RUS committed
761 762 763 764 765
	local counter = button.Counters[index];
	if ( not counter ) then
		button.Counters[index] = CreateFrame("Frame", nil, button, "GarrisonMissionAbilityCounterTemplate");
		counter = button.Counters[index];
	end
TOM_RUS's avatar
TOM_RUS committed
766
	local numPerRow, innerSpacing, outerSpacingX, outerSpacingY, scale = GetFollowerButtonCounterSpacings(followerTypeID);
TOM_RUS's avatar
TOM_RUS committed
767 768 769 770 771 772
	if index > 1 then
		if ((index - 1) % numPerRow ~= 0) then
			counter:SetPoint("RIGHT", button.Counters[index-1], "LEFT", -innerSpacing, 0);
		else
			counter:SetPoint("TOP", button.Counters[index-numPerRow], "BOTTOM", 0, -innerSpacing);
		end
TOM_RUS's avatar
TOM_RUS committed
773 774
	end
	counter:SetScale(scale);
TOM_RUS's avatar
TOM_RUS committed
775
	counter.info = info;
TOM_RUS's avatar
TOM_RUS committed
776

TOM_RUS's avatar
TOM_RUS committed
777
	counter.followerTypeID = followerTypeID;
TOM_RUS's avatar
TOM_RUS committed
778 779 780
	if ( info.traitID ) then
		counter.tooltip = nil;
		counter.info.showCounters = false;
TOM_RUS's avatar
TOM_RUS committed
781
		counter.Icon:SetTexture(info.icon);
TOM_RUS's avatar
TOM_RUS committed
782
		counter.Border:Hide();
TOM_RUS's avatar
TOM_RUS committed
783 784 785 786 787 788 789 790

		if ( GarrisonFollowerAbilities_IsNew(lastUpdate, followerID, info.traitID, GARRISON_FOLLOWER_ABILITY_TYPE_TRAIT ) ) then
			counter.AbilityFeedbackGlowAnim.traitID = info.traitID;
			counter.AbilityFeedbackGlowAnim:Play();
		elseif ( counter.AbilityFeedbackGlowAnim.traitID ~= info.traitID ) then
			counter.AbilityFeedbackGlowAnim.traitID = nil;
			counter.AbilityFeedbackGlowAnim:Stop();
		end
TOM_RUS's avatar
TOM_RUS committed
791 792 793 794 795 796
	elseif (info.spellID) then
		counter.tooltip = nil;
		counter.info.showCounters = false;
		counter.Icon:SetTexture(select(3, GetSpellInfo(info.spellID)));
		counter.AbilityFeedbackGlowAnim:Stop();
		counter.Border:Hide();
TOM_RUS's avatar
TOM_RUS committed
797 798 799
	else
		counter.tooltip = info.name;
		counter.info.showCounters = true;
TOM_RUS's avatar
TOM_RUS committed
800 801 802 803
		if (GarrisonFollowerOptions[followerTypeID].displayCounterAbilityInPlaceOfMechanic and info.counterID) then
			local abilityInfo = C_Garrison.GetFollowerAbilityInfo(info.counterID);
			counter.Icon:SetTexture(abilityInfo.icon);
			counter.Border:SetShown(ShouldShowFollowerAbilityBorder(followerTypeID, abilityInfo));
TOM_RUS's avatar
TOM_RUS committed
804
		else
TOM_RUS's avatar
TOM_RUS committed
805 806 807
			counter.Icon:SetTexture(info.icon);
			counter.Border:Show();

TOM_RUS's avatar
TOM_RUS committed
808
			if ( counter.info.factor <= GARRISON_HIGH_THREAT_VALUE and followerTypeID == Enum.GarrisonFollowerType.FollowerType_6_2 ) then
TOM_RUS's avatar
TOM_RUS committed
809 810 811
				counter.Border:SetAtlas("GarrMission_WeakEncounterAbilityBorder");
			else
				counter.Border:SetAtlas("GarrMission_EncounterAbilityBorder");
TOM_RUS's avatar
TOM_RUS committed
812
			end
TOM_RUS's avatar
TOM_RUS committed
813
		end
TOM_RUS's avatar
TOM_RUS committed
814

TOM_RUS's avatar
TOM_RUS committed
815 816
		counter.AbilityFeedbackGlowAnim.traitID = nil;
		counter.AbilityFeedbackGlowAnim:Stop();
TOM_RUS's avatar
TOM_RUS committed
817 818 819 820
	end
	counter:Show();
end

TOM_RUS's avatar
TOM_RUS committed
821 822 823
function GarrisonFollowerButton_UpdateAutoSpells(frame, button, follower)
	local numShown = 0;

TOM_RUS's avatar
TOM_RUS committed
824
	if follower.autoCombatSpells then
TOM_RUS's avatar
TOM_RUS committed
825 826 827
		numShown = #follower.autoCombatSpells;
	end

TOM_RUS's avatar
TOM_RUS committed
828
	if numShown == 0 then
TOM_RUS's avatar
TOM_RUS committed
829 830 831 832 833 834 835 836
		return numShown;
	end

	if ( follower.isCollected and follower.status ~= GARRISON_FOLLOWER_INACTIVE ) then
		GarrisonFollowerButton_AddAutoSpellButtons(button, follower);
	end
	local numPerRow, innerSpacing, outerSpacingX, outerSpacingY, scale = GetFollowerButtonCounterSpacings(follower.followerTypeID);
	button.Counters[1]:ClearAllPoints();
TOM_RUS's avatar
TOM_RUS committed
837

TOM_RUS's avatar
TOM_RUS committed
838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
	if ( numShown <= numPerRow ) then
		local collapsedButtonHeight = 46;
		button.Counters[1]:SetPoint("RIGHT", button.Counters[1]:GetParent(), "TOPRIGHT", -outerSpacingX, -collapsedButtonHeight/2);
	else
		button.Counters[1]:SetPoint("TOPRIGHT", -outerSpacingX, -outerSpacingY);
	end
	for i = numShown + 1, #button.Counters do
		button.Counters[i].info = nil;
		button.Counters[i]:Hide();
	end

	if (numShown == 1) then
		return 2 * outerSpacingX + button.Counters[1]:GetWidth() * scale;
	else
		return 2 * outerSpacingX + innerSpacing + (2 * button.Counters[1]:GetWidth() * scale);
	end
end

function GarrisonFollowerButton_AddAutoSpellButtons(button, follower)
TOM_RUS's avatar
TOM_RUS committed
857
	for i = 1, math.min(3, #follower.autoCombatSpells) do	-- max of 3 abilities
TOM_RUS's avatar
TOM_RUS committed
858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
		GarrisonFollowerButton_SetAutoSpellButton(button, follower.followerID, i, follower.autoCombatSpells[i], follower.followerTypeID);
	end
end

function GarrisonFollowerButton_SetAutoSpellButton(button, followerID, index, info, followerTypeID)
	local abilityFrame = button.Counters[index];
	if ( not abilityFrame ) then
		button.Counters[index] = CreateFrame("Frame", nil, button, "GarrisonMissionAbilityCounterTemplate");
		abilityFrame = button.Counters[index];
	end
	local numPerRow, innerSpacing, outerSpacingX, outerSpacingY, scale = GetFollowerButtonCounterSpacings(followerTypeID);
	if index > 1 then
		if ((index - 1) % numPerRow ~= 0) then
			abilityFrame:SetPoint("RIGHT", button.Counters[index-1], "LEFT", -innerSpacing, 0);
		else
			abilityFrame:SetPoint("TOP", button.Counters[index-numPerRow], "BOTTOM", 0, -innerSpacing);
		end
	end
	abilityFrame:SetScale(scale);
	abilityFrame.info = info;
	abilityFrame.followerTypeID = followerTypeID;
	abilityFrame.tooltip = nil;
	abilityFrame.info.showCounters = false;
	abilityFrame.Icon:SetTexture(info.icon);
	abilityFrame.Border:Hide();
	abilityFrame:Show();
end

TOM_RUS's avatar
TOM_RUS committed
886 887 888
function GarrisonFollowerList:ExpandButton(button, followerListFrame)
	local abHeight = self:ExpandButtonAbilities(button, false);
	if (abHeight == -1) then
TOM_RUS's avatar
TOM_RUS committed
889 890
		return;
	end
TOM_RUS's avatar
TOM_RUS committed
891

TOM_RUS's avatar
TOM_RUS committed
892 893 894 895 896 897 898 899 900 901
	button.UpArrow:Show();
	button.DownArrow:Hide();
	button:SetHeight(51 + abHeight);
	followerListFrame.expandedFollowerHeight = 51 + abHeight + 6;
end

function GarrisonFollowerList:ExpandButtonAbilities(button, traitsFirst)
	if ( not button.isCollected ) then
		return -1;
	end
TOM_RUS's avatar
TOM_RUS committed
902 903

	local abHeight = 0;
TOM_RUS's avatar
TOM_RUS committed
904 905
	if (not button.info.abilities) then
		button.info.abilities = C_Garrison.GetFollowerAbilities(button.info.followerID);
TOM_RUS's avatar
TOM_RUS committed
906
	end
TOM_RUS's avatar
TOM_RUS committed
907 908

	local buttonCount = 0;
TOM_RUS's avatar
TOM_RUS committed
909 910
	for i=1, #button.info.abilities do
		if ( traitsFirst == button.info.abilities