mapcolors: rewrite module implementation

This commit is contained in:
Artur Morozov
2024-06-07 09:16:02 +02:00
committed by GitHub
parent 4da109a705
commit f5dcfa6e0e
11 changed files with 151 additions and 82 deletions
+141 -82
View File
@@ -1,93 +1,152 @@
-- adds class colored circles on world and battlefield map
pfUI:RegisterModule("mapcolors", function ()
pfUI.mapcolors = CreateFrame("Frame", nil, UIParent)
pfUI.mapcolors:SetScript("OnUpdate", function()
-- throttle to to one item per .1 second
if ( this.tick or 1) > GetTime() then return else this.tick = GetTime() + .1 end
local frame, icon
-- initialize all button names
if not this.buttons then
this.buttons = {}
for i = 1, 4 do
icon = string.format("WorldMapParty%d", i)
this.buttons[icon] = string.format("party%d", i)
end
for i = 1, 4 do
icon = string.format("BattlefieldMinimapParty%d", i)
this.buttons[icon] = string.format("party%d", i)
end
for i = 1, 40 do
icon = string.format("BattlefieldMinimapRaid%d", i)
this.buttons[icon] = string.format("raid%d", i)
end
for i = 1, 40 do
icon = string.format("WorldMapRaid%d", i)
this.buttons[icon] = string.format("raid%d", i)
end
local button_cache = {}
local function Initialize(unit_button_name)
local texture_size = tonumber(C.appearance.worldmap.groupcircles)
for i=1, MAX_PARTY_MEMBERS do
local frame_name = unit_button_name.."Party"..i
local frame = _G[frame_name]
frame.icon = _G[frame_name.."Icon"]
frame.icon:SetTexture(pfUI.media["img:circleparty"])
frame.icon:SetVertexColor(.5, 1, .5)
SetAllPointsOffset(frame.icon, frame, texture_size, -texture_size)
-- populate cache to default values
button_cache[frame_name] = {
r = .5, g = 1, b = .5
}
end
-- update all available buttons
local ingroup
for name, unitstr in pairs(this.buttons) do
frame = _G[name]
if frame and UnitExists(unitstr) then
icon = _G[name.."Icon"]
icon:SetTexture()
-- create icon if not yet existing
if not frame.pfIcon then
frame.pfIcon = frame:CreateTexture(nil, "OVERLAY")
SetAllPointsOffset(frame.pfIcon, frame, this.size, -this.size)
end
-- check if unit is in same group
ingroup = nil
for i=1,5 do -- check if unit is in group
if UnitName(string.format("party%d", i)) == UnitName(unitstr) then
ingroup = true
for i=1, MAX_RAID_MEMBERS do
local frame_name = unit_button_name.."Raid"..i
local frame = _G[frame_name]
frame.icon = _G[frame_name.."Icon"]
frame.icon:SetTexture(pfUI.media["img:circleraid"])
frame.icon:SetVertexColor(.5, 1, .5)
SetAllPointsOffset(frame.icon, frame, texture_size, -texture_size)
-- populate cache to default values
button_cache[frame_name] = {
inParty = false,
r = .5, g = 1, b = .5
}
end
end
local function GetTextureColor(frame)
if UnitExists(frame.unit) then
local _, class = UnitClass(frame.unit)
local color = RAID_CLASS_COLORS[class]
return color.r, color.g, color.b
else
return .5, 1, .5
end
end
local function UpdateTexture(frame, inParty)
print('UpdateTexture', frame:GetName(), frame.unit, UnitName(frame.unit), UnitClass(frame.unit), inParty)
if inParty then
frame.icon:SetTexture(pfUI.media["img:circleparty"])
else
frame.icon:SetTexture(pfUI.media["img:circleraid"])
end
end
local function UpdateTextureColor(frame, r, g, b)
print('UpdateTextureColor', frame:GetName(), frame.unit, UnitName(frame.unit), UnitClass(frame.unit), r, g, b)
frame.icon:SetVertexColor(r, g, b)
end
local function UpdateUnitFrames(unit_button_name)
if GetNumRaidMembers() > 0 then
for i=1, MAX_RAID_MEMBERS do
local frame_name = unit_button_name.."Raid"..i
local frame = _G[frame_name]
local cache = button_cache[frame_name]
if frame.unit then
local inParty = UnitInParty(frame.unit)
if inParty ~= cache.inParty then
cache.inParty = inParty
UpdateTexture(frame, cache.inParty)
end
local r, g, b = GetTextureColor(frame)
if r ~= cache.r or g ~= cache.g or b ~= cache.b then
cache.r, cache.g, cache.b = r, g, b
UpdateTextureColor(frame, cache.r, cache.g, cache.b)
end
end
-- update texture according to raid/group state
if ingroup and frame.pfIcon.ingroup ~= "PARTY" then
frame.pfIcon:SetTexture(pfUI.media["img:circleparty"])
frame.pfIcon.ingroup = "PARTY"
elseif not ingroup and frame.pfIcon.ingroup ~= "RAID" then
frame.pfIcon:SetTexture(pfUI.media["img:circleraid"])
frame.pfIcon.ingroup = "RAID"
end
-- detect unit class and set color
local _, class = UnitClass(unitstr)
local color = RAID_CLASS_COLORS[class]
if color then
frame.pfIcon:SetVertexColor(color.r, color.g, color.b)
else
frame.pfIcon:SetVertexColor(.5,1,.5)
end
end
end
end)
pfUI.mapcolors.UpdateConfig = function()
pfUI.mapcolors.size = tonumber(C.appearance.worldmap.groupcircles)
if not pfUI.mapcolors.buttons then return end
for name, unitstr in pairs(pfUI.mapcolors.buttons) do
frame = _G[name]
if frame and frame.pfIcon then
SetAllPointsOffset(frame.pfIcon, frame, pfUI.mapcolors.size, -pfUI.mapcolors.size)
elseif GetNumPartyMembers() > 0 then
for i=1, MAX_PARTY_MEMBERS do
local frame_name = unit_button_name.."Party"..i
local frame = _G[frame_name]
local cache = button_cache[frame_name]
if frame.unit then
local r, g, b = GetTextureColor(frame)
if r ~= cache.r or g ~= cache.g or b ~= cache.b then
cache.r, cache.g, cache.b = r, g, b
UpdateTextureColor(frame, cache.r, cache.g, cache.b)
end
end
end
end
end
local function ColorizeName(frame)
local _, class = UnitClass(frame.unit)
local color = RAID_CLASS_COLORS[class]
frame.name = frame.name or UnitName(frame.unit)
frame.name = '|c'..color.colorStr..frame.name..'|r'
end
local function UpdateUnitColors(unit_button_name, tooltip)
local tooltipText = ""
if unit_button_name == 'WorldMap' then
-- Check player
if MouseIsOver(WorldMapPlayer) then
ColorizeName(WorldMapPlayer)
tooltipText = WorldMapPlayer.name
end
end
-- Check party
for i=1, MAX_PARTY_MEMBERS do
local frame = _G[unit_button_name.."Party"..i]
if frame:IsVisible() and MouseIsOver(frame) then
ColorizeName(frame)
tooltipText = tooltipText.."\n"..frame.name
end
end
--Check Raid
for i=1, MAX_RAID_MEMBERS do
local frame = _G[unit_button_name.."Raid"..i]
if frame:IsVisible() and MouseIsOver(frame) then
ColorizeName(frame)
tooltipText = tooltipText.."\n"..frame.name
end
end
tooltip:SetText(tooltipText)
tooltip:Show()
end
pfUI.mapcolors:UpdateConfig()
-- WorldMap
Initialize('WorldMap')
hooksecurefunc('WorldMapButton_OnUpdate', function()
-- throttle to to one item per 1 second
if ( this.tick or .5) > GetTime() then return else this.tick = GetTime() + .5 end
UpdateUnitFrames('WorldMap')
end)
if C.appearance.worldmap.colornames == "1" then
hooksecurefunc('WorldMapUnit_OnEnter', function()
-- throttle to to one item per 1 second
if ( this.tick or .5) > GetTime() then return else this.tick = GetTime() + .5 end
UpdateUnitColors('WorldMap', WorldMapTooltip)
end)
end
-- BattlefieldMinimap
HookAddonOrVariable("Blizzard_BattlefieldMinimap", function()
Initialize('BattlefieldMinimap')
hooksecurefunc('BattlefieldMinimap_OnUpdate', function()
-- throttle to to one item per 1 second
if ( this.tick or .5) > GetTime() then return else this.tick = GetTime() + .5 end
UpdateUnitFrames('BattlefieldMinimap')
end)
if C.appearance.worldmap.colornames == "1" then
hooksecurefunc('BattlefieldMinimapUnit_OnEnter', function()
-- throttle to to one item per 1 second
if ( this.tick or .5) > GetTime() then return else this.tick = GetTime() + .5 end
UpdateUnitColors('BattlefieldMinimap', GameTooltip)
end)
end
end)
end)