vrtc / chorus (public) (License: CC0) (since 2023-08-12) (hash sha1)
World of Warcraft add-on stub. The overall goal is to create a specialized raid frame.

/src/ChorusUnitBackdropTemplate.lua (e9a8ab838fdb5843a1582701609a8b51d7471a1d) (3841 bytes) (mode 100644) (type blob)

--[[--
`ChorusUnitBackdropTemplate` handles the visuals and dynamic changes to unit
frame borders.

@submodule chorus
]]

local UnitExists = Chorus.test.UnitExists or UnitExists

local SecureButton_GetUnit = SecureButton_GetUnit

local function getDefaultBackdropColorMap()

	--[[ player: gold; target: magenta; focus: cyan; none: grey; ]]--

	return {
		target = {
			1, 0, 1, 1,
		},
		focus = {
			0, 1, 1, 1,
		},
		player = {
			1, 225 / 255, 0, 1,
		},
	}
end

local function unitBackdropEventProcessor(unitBackdrop)
	assert(unitBackdrop ~= nil)

	local unitDesignation = unitBackdrop:GetAttribute('unit')
	if not unitDesignation then
		unitDesignation = SecureButton_GetUnit(unitBackdrop)
	end
	assert(unitDesignation ~= nil)

	if UnitExists(unitDesignation) then
		unitBackdrop:Show()
	else
		unitBackdrop:Hide()
		return
	end

	--[[ Load backdrop color configuration options. ]]--

	local colorMap = Chorus.backdropColorMap or getDefaultBackdropColorMap()
	assert(colorMap ~= nil)
	assert('table' == type(colorMap))

	local r = 1.0
	local g = 1.0
	local b = 1.0
	local a = 1.0

	--[[ Find valid setting. ]]--

	--[[ Order is important. The first matching unit category will yield
	corresponding color. ]]--

	local t = {'player', 'target', 'focus',}
	local i = 0
	while (i < #t) do
		i = i + 1
		local u = t[i]
		assert(u ~= nil)
		assert('string' == type(u))
		if UnitIsUnit(unitDesignation, u) then
			local e = colorMap[u] or {1.0, 1.0, 1.0, 1.0}
			if e ~= nil and 'table' == type(e) then
				r = e[1] or 1.0
				g = e[2] or 1.0
				b = e[3] or 1.0
				a = e[4] or 1.0
			end
			break
		end
	end

	--[[ Apply setting. ]]--

	unitBackdrop:SetBackdropBorderColor(r, g, b, a)
	unitBackdrop:SetBackdropColor(r / 6.0, g / 6.0, b / 6.0, a)
end

--[[
When initializing a backdrop frame, given no explicit anchors were set in XML,
or `setAllPoints` is `false, then fallback to these hardcoded default anchors.
@function applyDefaultDimensionsIfNecessary
]]
local function applyDefaultDimensionsIfNecessary(unitBackdrop)
	assert(unitBackdrop ~= nil)

	local backdropTable = unitBackdrop:GetBackdrop()
	assert(backdropTable ~= nil)
	assert('table' == type(backdropTable))

	local p = unitBackdrop:GetParent()
	local w = unitBackdrop:GetWidth()
	local h = unitBackdrop:GetHeight()
	local fallbackFlag = (math.abs(w) <= 0 or math.abs(h) <= 0) and p ~= nil
	if fallbackFlag then
		local offset = (backdropTable.edgeSize or 24) / 4
		unitBackdrop:SetPoint('TOPRIGHT', offset, offset)
		unitBackdrop:SetPoint('TOPLEFT', -offset, offset)
		unitBackdrop:SetPoint('BOTTOMLEFT', -offset, -offset)
		unitBackdrop:SetPoint('BOTTOMRIGHT', offset, -offset)
	end
end

--[[--
Initialize backdrop.

Unit backdrop frame is intended to be composed with unit frames and unit
buttons. It is intended to handle the render of tiled background and edge
textures.

@function unitBackdropMain
@tparam frame unitBackdrop this unit backdrop frame
]]
function Chorus.unitBackdropMain(unitBackdrop)
	assert(unitBackdrop ~= nil)

	local colorMap = Chorus.backdropColorMap
	if not colorMap then
		colorMap = getDefaultBackdropColorMap()
	end
	Chorus.backdropColorMap = colorMap

	applyDefaultDimensionsIfNecessary(unitBackdrop)

	unitBackdrop:SetBackdropBorderColor(1, 1, 1, 1)
	unitBackdrop:SetBackdropColor(0, 0, 0, 0.8)

	unitBackdrop:RegisterEvent('PARTY_CONVERTED_TO_RAID')
	unitBackdrop:RegisterEvent('PARTY_MEMBERS_CHANGED')
	unitBackdrop:RegisterEvent('PLAYER_ENTERING_WORLD')
	unitBackdrop:RegisterEvent('PLAYER_FOCUS_CHANGED')
	unitBackdrop:RegisterEvent('PLAYER_LOGIN')
	unitBackdrop:RegisterEvent('PLAYER_TARGET_CHANGED')
	unitBackdrop:RegisterEvent('RAID_ROSTER_UPDATE')
	unitBackdrop:RegisterEvent('ZONE_CHANGED')
	unitBackdrop:SetScript('OnEvent', unitBackdropEventProcessor)
	unitBackdrop:SetScript('OnAttributeChanged', unitBackdropEventProcessor)
end


Mode Type Size Ref File
100644 blob 35 5c40e6e2862d70b5c51c326a13073a4012ac05c7 .gitignore
100644 blob 3606 f73da473168d1897963fd2e32d89841ca0461ec0 README.adoc
040000 tree - 271296b4bafcaa151458a0192fd313641ca9b409 bin
100644 blob 228 c7dd24afa7d5c2375ff60a91c73623a304b808f9 chorus.toc
040000 tree - 99c99c3cbc641f8954a5be291e61681cb5e74629 conf
040000 tree - efa7258757edf7b888ea13c215e14b497fef8a16 doc
100644 blob 2391 1b0ca1bc25f74a34476360e5a8f14e28767b204e makefile
040000 tree - b9d3ea9d61b99ee71b5fcdbc6b18843df20c8c3c src
Hints:
Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://rocketgit.com/user/vrtc/chorus

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@ssh.rocketgit.com/user/vrtc/chorus

Clone this repository using git:
git clone git://git.rocketgit.com/user/vrtc/chorus

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a merge request:
... clone the repository ...
... make some changes and some commits ...
git push origin main