And in the light of jeruku's post, I winged up an addon with your (slightly edited) code and my previous suggestion does work. I.e.:
Lua Code:
print("Detach called");
self:SetParent(nil);
self:SetParent(UIParent);
self:ClearAllPoints();
self:SetPoint("CENTER", UIParent, "CENTER", 0, -150);
print("attached to: " .. self:GetParent():GetName());
Output:
Code:
Detach called
attached to: UIParent
Furthermore,
:GetPoint should be added to the list jeruku quoted under "The following APIs are now blocked from being called on restricted frames:" If you add thiese two lines to the end of the snippet above:
Lua Code:
local point, anchor, anchorTo, x, y = self:GetPoint(1);
print("at point #1: " .. point.. ", ".. anchor:GetName().. ", ".. anchorTo.. ", ".. x.. ", ".. y);
The output is:
Code:
Detach called
attached to: UIParent
at point #1: CENTER, UIParent, CENTER, 0, -150
But, if you add that same code to your :Attach method, you get an error:
Code:
Message: Interface\AddOns\alkaTestLyabral\alkaTestLyabral.lua:22: Action[FrameMeasurement] failed because[Can't measure restricted regions]: attempted from: LybrialUIAnchor:GetPoint().
Time: Fri Jul 19 10:47:09 2019
Count: 1
Stack: Interface\AddOns\alkaTestLyabral\alkaTestLyabral.lua:22: Action[FrameMeasurement] failed because[Can't measure restricted regions]: attempted from: LybrialUIAnchor:GetPoint().
[C]: in function `GetPoint'
Interface\AddOns\alkaTestLyabral\alkaTestLyabral.lua:22: in function `Attach'
Interface\AddOns\alkaTestLyabral\alkaTestLyabral.lua:64: in function <Interface\AddOns\alkaTestLyabral\alkaTestLyabral.lua:55>
Locals: (*temporary) = LybrialUIAnchor {
0 = <userdata>
Attach = <function> defined @Interface\AddOns\alkaTestLyabral\alkaTestLyabral.lua:12
Detach = <function> defined @Interface\AddOns\alkaTestLyabral\alkaTestLyabral.lua:32
TestTexture = <unnamed> {
}
eventHandler = <function> defined @Interface\AddOns\alkaTestLyabral\alkaTestLyabral.lua:55
}
(*temporary) = 1
Finally, I also created a texture on the frame. And it's weird.
The texture reportedly attaches correctly to the frame, wich on Detach is reportedly positioned at "CENTER, UIParent, CENTER, 0, -150".
Code:
Lua Code:
print("Detach called");
print("attached to: " .. self:GetParent():GetName());
local point, anchor, anchorTo, x, y = self:GetPoint(1);
print("at point #1: " .. point.. ", ".. anchor:GetName().. ", ".. anchorTo.. ", ".. x.. ", ".. y);
local point, anchor, anchorTo, x, y = self.TestTexture:GetPoint(1);
print("Texture at point #1: " .. point.. ", ".. anchor:GetName().. ", ".. anchorTo.. ", ".. x.. ", ".. y);
Output:
Code:
Detach called
attached to: UIParent
at point #1: CENTER, UIParent, CENTER, 0, -150
Texture at point #1: TOPLEFT, LybrialUIAnchor, TOPLEFT, 0, 0
BUT the texture is always drawn at the position the player's nameplate was last drawn at (on :Attach), never resetting to "CENTER, UIParent, CENTER, 0, -150" (as set and reportedly done on : Dettach).
I'm actually rather unfamiliar with working with textures, so I may be missing something.
For reference, here's the full code I ran:
Lua Code:
local LybrialAnchors = {};
function LybrialAnchors:SetupFrames()
if (not self.anchorFrame) then
self.anchorFrame = CreateFrame("Frame", "LybrialUIAnchor");
self.anchorFrame.TestTexture = self.anchorFrame:CreateTexture(NIL, "ARTWORK");
self.anchorFrame.TestTexture:SetColorTexture(1, 1, 1, 1);
self.anchorFrame:RegisterEvent("NAME_PLATE_UNIT_ADDED");
self.anchorFrame:RegisterEvent("NAME_PLATE_UNIT_REMOVED");
self.anchorFrame.Attach = function(self, frame, frameTopLeft, frameBottomRight)
print("Attach called");
self:SetParent(frame);
self:ClearAllPoints();
self:SetPoint("TOPLEFT", frameTopLeft, "TOPLEFT", 0, 0);
self:SetPoint("BOTTOMRIGHT", frameBottomRight, "BOTTOMRIGHT", 0, 0);
print("attached to: " .. self:GetParent():GetName());
-- NB: The following line will error (cannot get measurements from restricted frame
-- local point, anchor, anchorTo, x, y = self:GetPoint(1);
-- print("at point #1: " .. point.. ", ".. anchor:GetName().. ", ".. anchorTo.. ", ".. x.. ", ".. y);
-- LybrialAnchors:OnUpdate();
self.TestTexture:ClearAllPoints();
self.TestTexture:SetAllPoints();
-- NB: The following line will error (cannot get measurements from restricted frame)
-- local point, anchor, anchorTo, x, y = self.TestTexture:GetPoint(1);
-- print("Texture at point #1: " .. point.. ", ".. anchor:GetName().. ", ".. anchorTo.. ", ".. x.. ", ".. y);
end
self.anchorFrame.Detach = function(self)
print("Detach called");
self:SetParent(nil);
self:SetParent(UIParent);
self:ClearAllPoints();
self:SetPoint("CENTER", UIParent, "CENTER", 0, -150);
print("attached to: " .. self:GetParent():GetName());
-- NB: The following line will NOT error
local point, anchor, anchorTo, x, y = self:GetPoint(1);
print("at point #1: " .. point.. ", ".. anchor:GetName().. ", ".. anchorTo.. ", ".. x.. ", ".. y);
-- LybrialAnchors:OnUpdate();
-- NB: The following line will error (cannot set a Texture's parent to nil)
-- self.TestTexture:SetParent(nil);
-- self.TestTexture:SetParent(self);
self.TestTexture:ClearAllPoints();
self.TestTexture:SetAllPoints();
-- NB: The following line will NOT error
local point, anchor, anchorTo, x, y = self.TestTexture:GetPoint(1);
print("Texture at point #1: " .. point.. ", ".. anchor:GetName().. ", ".. anchorTo.. ", ".. x.. ", ".. y);
end
self.anchorFrame.eventHandler = function(_, event, nameplate)
if (event == "NAME_PLATE_UNIT_ADDED") then
if (UnitIsUnit(nameplate, "player")) then
if (InCombatLockdown() or C_PetBattles.IsInBattle()) then
LybrialAnchors.anchorFrame:Detach();
else
local frame = C_NamePlate.GetNamePlateForUnit("player");
if (frame) then
LybrialAnchors.anchorFrame:Attach(frame, frame.UnitFrame.healthBar, NamePlateDriverFrame.classNamePlatePowerBar);
else
LybrialAnchors.anchorFrame:Detach();
end
end
end
elseif (event == "NAME_PLATE_UNIT_REMOVED") then
if (UnitIsUnit(nameplate, "player")) then
LybrialAnchors.anchorFrame:Detach();
end
end
end
self.anchorFrame:SetScript("OnEvent", self.anchorFrame.eventHandler);
end
end
LybrialAnchors:SetupFrames()
I'll be going back to my own nerve-wrecking projects now. But if anyone has any more insight on any of this, I'd be curious.
And... happy coding, Lybrial!