首页 > 其他分享 >看到一段代码,留个脚印_2(封装或者说是模板)

看到一段代码,留个脚印_2(封装或者说是模板)

时间:2022-10-07 17:33:16浏览次数:48  
标签:TOPLEFT 留个 封装 ETT frame MY SetPoint 模板 ClearAllPoints

2022年6月的时候玩wow3.35版本的时候,目标的目标以及目标的目标的目标是这样的,突发奇想,能不能把目标的目标以及目标的目标的目标头像翻转,就是头像在右,血条在左,和目标框体一样,参考其它代码发现加上这行代码

frame.Texture:SetTexCoord(1, 0, 1, 0) -- 血条靠近头像下方

 

 frame.Texture:SetTexCoord(1, 0, 0, 1) -- 血条靠近头像上方

 查看XML代码,发现代码是这样的

<Button name="ETT_TargetTargetFrameTemplate" parent="UIParent" toplevel="true" virtual="true" movable="true" inherits="SecureUnitButtonTemplate">  -- 建立目标的目标框体模板

...

<Button name="ETT_TargetTargetFrame" inherits="ETT_TargetTargetFrameTemplate">                               -- 建立目标的目标框体并继承上面的模板

...

<Button name="ETT_TargetTargetTargetFrame" inherits="ETT_TargetTargetFrameTemplate">                            -- 建立目标的目标的目标框体也继承集成上面的模板

...

不知为何,不太喜欢用xml来写代码,比较喜欢用lua,于是照葫芦将xml建立目标的目标和目标的目标的目标代码转为lua,代码如下

--ToT
local ETT_TargetTargetFrame = CreateFrame("Button", "ToTFrame", UIParent, "SecureUnitButtonTemplate");
ETT_TargetTargetFrame:SetFrameStrata("LOW");
ETT_TargetTargetFrame:SetFrameLevel(8);
ETT_TargetTargetFrame:SetWidth(96);
ETT_TargetTargetFrame:SetHeight(48);
ETT_TargetTargetFrame:ClearAllPoints();
ETT_TargetTargetFrame:SetPoint("LEFT", TargetFrame, "RIGHT", 15, 0);
ETT_TargetTargetFrame:SetAlpha(0);

-- 头像在左血条在右开始
ETT_TargetTargetFramePortrait = ETT_TargetTargetFrame:CreateTexture("ETT_TargetTargetFramePortrait", "BORDER");
ETT_TargetTargetFramePortrait:SetWidth(27);
ETT_TargetTargetFramePortrait:SetHeight(27);
ETT_TargetTargetFramePortrait:ClearAllPoints();
ETT_TargetTargetFramePortrait:SetPoint("TOPLEFT", ETT_TargetTargetFrame, "TOPLEFT", 6, -5);

ETT_TargetTargetFrameTexture = ETT_TargetTargetFrame:CreateTexture("ToTTexture", "ARTWORK");
ETT_TargetTargetFrameTexture:SetTexture("Interface\\TargetingFrame\\UI-PartyFrame");
ETT_TargetTargetFrameTexture:SetWidth(96);
ETT_TargetTargetFrameTexture:SetHeight(48);
ETT_TargetTargetFrameTexture:ClearAllPoints();
ETT_TargetTargetFrameTexture:SetPoint("TOPLEFT", ETT_TargetTargetFrame, "TOPLEFT", 0, -2);

ETT_TargetTargetFrameName = ETT_TargetTargetFrame:CreateFontString("ETT_TargetTargetFrameName", "ARTWORK", "GameFontNormalSmall");
ETT_TargetTargetFrameName:ClearAllPoints();
ETT_TargetTargetFrameName:SetPoint("BOTTOMLEFT", ETT_TargetTargetFrame, "BOTTOMLEFT", 34, 38);

ETT_TargetTargetFrameHealthBar = CreateFrame("StatusBar", "ToTHealthBar", UIParent);
ETT_TargetTargetFrameHealthBar:SetStatusBarTexture("Interface\\TargetingFrame\\UI-StatusBar");
ETT_TargetTargetFrameHealthBar:SetFrameStrata("LOW");
ETT_TargetTargetFrameHealthBar:SetFrameLevel(7);
ETT_TargetTargetFrameHealthBar:SetMinMaxValues(0, 100);
ETT_TargetTargetFrameHealthBar:SetValue(0);
ETT_TargetTargetFrameHealthBar:SetWidth(53);
ETT_TargetTargetFrameHealthBar:SetHeight(6);
ETT_TargetTargetFrameHealthBar:ClearAllPoints();
ETT_TargetTargetFrameHealthBar:SetPoint("TOPLEFT", ETT_TargetTargetFrame, "TOPLEFT", 35, -9);
ETT_TargetTargetFrameHealthBar:SetStatusBarColor(0, 1, 0);

ETT_TargetTargetFrameManaBar = CreateFrame("StatusBar", "ToTManaBar", UIParent);
ETT_TargetTargetFrameManaBar:SetStatusBarTexture("Interface\\TargetingFrame\\UI-StatusBar");
ETT_TargetTargetFrameManaBar:SetFrameStrata("LOW");
ETT_TargetTargetFrameManaBar:SetFrameLevel(7);
ETT_TargetTargetFrameManaBar:SetMinMaxValues(0, 100);
ETT_TargetTargetFrameManaBar:SetValue(0);
ETT_TargetTargetFrameManaBar:SetWidth(53);
ETT_TargetTargetFrameManaBar:SetHeight(6);
ETT_TargetTargetFrameManaBar:ClearAllPoints();
ETT_TargetTargetFrameManaBar:SetPoint("TOPLEFT", ETT_TargetTargetFrame, "TOPLEFT", 35, -16);
ETT_TargetTargetFrameManaBar:SetStatusBarColor(0, 0, 1);

--ToToT
local ETT_TargetTargetTargetFrame = CreateFrame("Button", "ETT_TargetTargetTargetFrame", UIParent, "SecureUnitButtonTemplate");
ETT_TargetTargetTargetFrame:SetFrameStrata("LOW");
ETT_TargetTargetTargetFrame:SetFrameLevel(8);
ETT_TargetTargetTargetFrame:SetWidth(96);
ETT_TargetTargetTargetFrame:SetHeight(48);
ETT_TargetTargetTargetFrame:ClearAllPoints();
ETT_TargetTargetTargetFrame:SetPoint("TOP", ETT_TargetTargetFrame, "BOTTOM", 26, 10);
ETT_TargetTargetTargetFrame:SetAlpha(0);

ETT_TargetTargetTargetFramePortrait = ETT_TargetTargetTargetFrame:CreateTexture("ToTPortrait", "BORDER");
ETT_TargetTargetTargetFramePortrait:SetWidth(27);
ETT_TargetTargetTargetFramePortrait:SetHeight(27);
ETT_TargetTargetTargetFramePortrait:ClearAllPoints();
ETT_TargetTargetTargetFramePortrait:SetPoint("TOPLEFT", ETT_TargetTargetTargetFrame, "TOPLEFT", 6, -5);

ETT_TargetTargetTargetFrameTexture = ETT_TargetTargetTargetFrame:CreateTexture("ToTTexture", "ARTWORK");
ETT_TargetTargetTargetFrameTexture:SetTexture("Interface\\TargetingFrame\\UI-PartyFrame");
ETT_TargetTargetTargetFrameTexture:SetWidth(96);
ETT_TargetTargetTargetFrameTexture:SetHeight(48);
ETT_TargetTargetTargetFrameTexture:ClearAllPoints();
ETT_TargetTargetTargetFrameTexture:SetPoint("TOPLEFT", ETT_TargetTargetTargetFrame, "TOPLEFT", 0, -2);

ETT_TargetTargetTargetFrameName = ETT_TargetTargetTargetFrame:CreateFontString("ToTName", "ARTWORK", "GameFontNormalSmall");
ETT_TargetTargetTargetFrameName:ClearAllPoints();
ETT_TargetTargetTargetFrameName:SetPoint("BOTTOMLEFT", ETT_TargetTargetTargetFrame, "BOTTOMLEFT", 34, 38);

ETT_TargetTargetTargetFrameHealthBar = CreateFrame("StatusBar", "ToTHealthBar", UIParent);
ETT_TargetTargetTargetFrameHealthBar:SetStatusBarTexture("Interface\\TargetingFrame\\UI-StatusBar");
ETT_TargetTargetTargetFrameHealthBar:SetFrameStrata("LOW");
ETT_TargetTargetTargetFrameHealthBar:SetFrameLevel(7);
ETT_TargetTargetTargetFrameHealthBar:SetMinMaxValues(0, 100);
ETT_TargetTargetTargetFrameHealthBar:SetValue(0);
ETT_TargetTargetTargetFrameHealthBar:SetWidth(53);
ETT_TargetTargetTargetFrameHealthBar:SetHeight(6);
ETT_TargetTargetTargetFrameHealthBar:ClearAllPoints();
ETT_TargetTargetTargetFrameHealthBar:SetPoint("TOPLEFT", ETT_TargetTargetTargetFrame, "TOPLEFT", 35, -9);
ETT_TargetTargetTargetFrameHealthBar:SetStatusBarColor(0, 1, 0);

ETT_TargetTargetTargetFrameManaBar = CreateFrame("StatusBar", "ToTManaBar", UIParent);
ETT_TargetTargetTargetFrameManaBar:SetStatusBarTexture("Interface\\TargetingFrame\\UI-StatusBar");
ETT_TargetTargetTargetFrameManaBar:SetFrameStrata("LOW");
ETT_TargetTargetTargetFrameManaBar:SetFrameLevel(7);
ETT_TargetTargetTargetFrameManaBar:SetMinMaxValues(0, 100);
ETT_TargetTargetTargetFrameManaBar:SetValue(0);
ETT_TargetTargetTargetFrameManaBar:SetWidth(53);
ETT_TargetTargetTargetFrameManaBar:SetHeight(6);
ETT_TargetTargetTargetFrameManaBar:ClearAllPoints();
ETT_TargetTargetTargetFrameManaBar:SetPoint("TOPLEFT", ETT_TargetTargetTargetFrame, "TOPLEFT", 35, -16);
ETT_TargetTargetTargetFrameManaBar:SetStatusBarColor(0, 0, 1);

去掉xml中的建立框体代码进入WOW,可行,于是又看到这样代码有些重复,有些臃肿,能不简化一些,能不能像xml,建立一个模板,然后传参数进行调用,这不简化了好多代码?在lua里照着xml建立框体模板写一段代码

local ETT_TargetTargetFrameTemplate = CreateFrame("Button","ETT_TargetTargetFrameTemplate",UIParent,"SecureUnitButtonTemplate")
   ETT_TargetTargetFrameTemplate:SetFrameStrata("LOW");
    ETT_TargetTargetFrameTemplate:SetFrameLevel(8);
    ETT_TargetTargetFrameTemplate:SetWidth(96);
    ETT_TargetTargetFrameTemplate:SetHeight(48);
    ETT_TargetTargetFrameTemplate:ClearAllPoints();
    ETT_TargetTargetFrameTemplate:SetPoint("LEFT", TargetFrame, "RIGHT", 15, 0);
    ETT_TargetTargetFrameTemplate:SetAlpha(0);

    ETT_TargetTargetFrameTemplatePortrait = ETT_TargetTargetFrameTemplate:CreateTexture("ETT_TargetTargetFrameTemplatePortrait", "BORDER");
    ETT_TargetTargetFrameTemplatePortrait:SetWidth(27);
    ETT_TargetTargetFrameTemplatePortrait:SetHeight(27);
    ETT_TargetTargetFrameTemplatePortrait:ClearAllPoints();
  ETT_TargetTargetFrameTemplatePortrait:SetPoint("TOPLEFT", ETT_TargetTargetFrameTemplate, "TOPLEFT", 6, -5);

  ETT_TargetTargetFrameTemplateTexture = ETT_TargetTargetFrameTemplate:CreateTexture("ToTTexture", "ARTWORK");
  ETT_TargetTargetFrameTemplateTexture:SetTexture("Interface\\TargetingFrame\\UI-PartyFrame");
  ETT_TargetTargetFrameTemplateTexture:SetWidth(96);
  ETT_TargetTargetFrameTemplateTexture:SetHeight(48);
  ETT_TargetTargetFrameTemplateTexture:ClearAllPoints();
  ETT_TargetTargetFrameTemplateTexture:SetPoint("TOPLEFT", ETT_TargetTargetFrameTemplate, "TOPLEFT", 0, -2);

  ETT_TargetTargetFrameTemplateName = ETT_TargetTargetFrameTemplate:CreateFontString("ETT_TargetTargetFrameTemplateName", "ARTWORK", "GameFontNormalSmall");
  ETT_TargetTargetFrameTemplateName:ClearAllPoints();
  ETT_TargetTargetFrameTemplateName:SetPoint("BOTTOMLEFT", ETT_TargetTargetFrameTemplate, "BOTTOMLEFT", 34, 38);

  ETT_TargetTargetFrameTemplateHealthBar = CreateFrame("StatusBar", "ToTHealthBar", UIParent);
  ETT_TargetTargetFrameTemplateHealthBar:SetStatusBarTexture("Interface\\TargetingFrame\\UI-StatusBar");
  ETT_TargetTargetFrameTemplateHealthBar:SetFrameStrata("LOW");
  ETT_TargetTargetFrameTemplateHealthBar:SetFrameLevel(7);
  ETT_TargetTargetFrameTemplateHealthBar:SetMinMaxValues(0, 100);
  ETT_TargetTargetFrameTemplateHealthBar:SetValue(0);
  ETT_TargetTargetFrameTemplateHealthBar:SetWidth(53);
  ETT_TargetTargetFrameTemplateHealthBar:SetHeight(6);
  ETT_TargetTargetFrameTemplateHealthBar:ClearAllPoints();
  ETT_TargetTargetFrameTemplateHealthBar:SetPoint("TOPLEFT", ETT_TargetTargetFrameTemplate, "TOPLEFT", 35, -9);
  ETT_TargetTargetFrameTemplateHealthBar:SetStatusBarColor(0, 1, 0);

  ETT_TargetTargetFrameTemplateManaBar = CreateFrame("StatusBar", "ToTManaBar", UIParent);
  ETT_TargetTargetFrameTemplateManaBar:SetStatusBarTexture("Interface\\TargetingFrame\\UI-StatusBar");
  ETT_TargetTargetFrameTemplateManaBar:SetFrameStrata("LOW");
  ETT_TargetTargetFrameTemplateManaBar:SetFrameLevel(7);
  ETT_TargetTargetFrameTemplateManaBar:SetMinMaxValues(0, 100);
  ETT_TargetTargetFrameTemplateManaBar:SetValue(0);
  ETT_TargetTargetFrameTemplateManaBar:SetWidth(53);
  ETT_TargetTargetFrameTemplateManaBar:SetHeight(6);
  ETT_TargetTargetFrameTemplateManaBar:ClearAllPoints();
  ETT_TargetTargetFrameTemplateManaBar:SetPoint("TOPLEFT", ETT_TargetTargetFrameTemplate, "TOPLEFT", 35, -16);
  ETT_TargetTargetFrameTemplateManaBar:SetStatusBarColor(0, 0, 1);

模板是建好了,然后也按照xml代码那样继承上面的框体

local ETT_TargetTargetFrame = CreateFrame("Button", "ToTFrame", UIParent, “ETT_TargetTargetFrameTemp”);

会有错误提示 CreateFrame(): Couldn't find inherited node "ETT_TargetTargetFrameTemp",问题出在 ETT_TargetTargetFrameTemp,这个代码上,换上系统自带的模板就可通过,自己建立的就不行,观察xml建立模板代码

<Button name="ETT_TargetTargetFrameTemplate" parent="UIParent" toplevel="true" virtual="true" movable="true" inherits="SecureUnitButtonTemplate">

分析下,Button 是建判刑是一个 Button,第二个是 Button 的名称,第三个是父窗体是谁,第三个字面的意思可能是层级,高上层,第五个字面意思可否移动,最后是继承的窗体是哪个,就第四个virtual 不是很清楚什么意思,自己猜测是不是这个参数影响的继承是否,查WOWAPI 的CreateFrame()有这样的提示,只提到 XML 写法,没有提到 lua 的写法。正好有个QQ群,去问问里面的大神,我把代码和情况问题在群里问了后,立马就有人解答了,意思就是要用函数封装一个模板,然后传入相应的参数进行调用

 

 

 

 

 

 然后我就依照他封装的一个函数写了一段代码

local CreatePanel = function(anchor, parent, relativeTo, x, y, w, h, a)
    local MY_TOTFRAME = CreateFrame("Button",  "MY_TOTFRAME", UIParent, "SecureUnitButtonTemplate");
    MY_TOTFRAME:SetFrameStrata("LOW");
    MY_TOTFRAME:SetFrameLevel(8);
    MY_TOTFRAME:SetWidth(w);
    MY_TOTFRAME:SetHeight(h);
    MY_TOTFRAME:ClearAllPoints();
    MY_TOTFRAME:SetPoint(anchor, relativeTo, x, y);
--    MY_TOTFRAME:SetAlpha(0);
    
    MY_TOTFRAMEPortrait = MY_TOTFRAME:CreateTexture("MY_TOTFRAMEPortrait", "BORDER");
    MY_TOTFRAMEPortrait:SetWidth(27);
    MY_TOTFRAMEPortrait:SetHeight(27);
    MY_TOTFRAMEPortrait:ClearAllPoints();
    MY_TOTFRAMEPortrait:SetPoint("TOPRIGHT", MY_TOTFRAME, "TOPRIGHT", -5, -6);

    MY_TOTFRAMETexture = MY_TOTFRAME:CreateTexture("ToTTexture", "ARTWORK");
    MY_TOTFRAMETexture:SetTexture("Interface\\TargetingFrame\\UI-PartyFrame");
    MY_TOTFRAMETexture:SetTexCoord(1, 0, 0, 1)    -- 血条靠近头像上方
    --MY_TOTFRAMETexture:SetTexCoord(1, 0, 1, 0)    -- 血条靠近头像下方
    MY_TOTFRAMETexture:SetWidth(96);
    MY_TOTFRAMETexture:SetHeight(48);
    MY_TOTFRAMETexture:ClearAllPoints();
    MY_TOTFRAMETexture:SetPoint("TOPLEFT", MY_TOTFRAME, "TOPLEFT", 0, -2);

    MY_TOTFRAMEName = MY_TOTFRAME:CreateFontString("MY_TOTFRAMEName", "ARTWORK", "GameFontNormalSmall");
    MY_TOTFRAMEName:ClearAllPoints();
    MY_TOTFRAMEName:SetPoint("BOTTOMLEFT", MY_TOTFRAME, "BOTTOMLEFT", 9, 40);

    MY_TOTFRAMEHealthBar = CreateFrame("StatusBar", "ToTHealthBar", UIParent);
    MY_TOTFRAMEHealthBar:SetStatusBarTexture("Interface\\TargetingFrame\\UI-StatusBar");
    MY_TOTFRAMEHealthBar:SetFrameStrata("LOW");
    MY_TOTFRAMEHealthBar:SetFrameLevel(7);
    MY_TOTFRAMEHealthBar:SetMinMaxValues(0, 100);
    MY_TOTFRAMEHealthBar:SetValue(0);
    MY_TOTFRAMEHealthBar:SetWidth(53);
    MY_TOTFRAMEHealthBar:SetHeight(6);
    MY_TOTFRAMEHealthBar:ClearAllPoints();
    MY_TOTFRAMEHealthBar:SetPoint("TOPLEFT", MY_TOTFRAME, "TOPLEFT", 9, -10);
    MY_TOTFRAMEHealthBar:SetStatusBarColor(0, 1, 0);

    MY_TOTFRAMEManaBar = CreateFrame("StatusBar", "ToTManaBar", UIParent);
    MY_TOTFRAMEManaBar:SetStatusBarTexture("Interface\\TargetingFrame\\UI-StatusBar");
    MY_TOTFRAMEManaBar:SetFrameStrata("LOW");
    MY_TOTFRAMEManaBar:SetFrameLevel(7);
    MY_TOTFRAMEManaBar:SetMinMaxValues(0, 100);
    MY_TOTFRAMEManaBar:SetValue(0);
    MY_TOTFRAMEManaBar:SetWidth(53);
    MY_TOTFRAMEManaBar:SetHeight(6);
    MY_TOTFRAMEManaBar:ClearAllPoints();
    MY_TOTFRAMEManaBar:SetPoint("TOPLEFT", MY_TOTFRAME, "TOPLEFT", 10, -16);
    MY_TOTFRAMEManaBar:SetStatusBarColor(0, 0, 1)

    MY_TOTFRAMEHPPct = MY_TOTFRAME:CreateFontString("ToTHPPct", "ARTWORK", "TextStatusBarText");
    MY_TOTFRAMEHPPct:SetFont("fonts\\1.ttf", 10, "OUTLINE");
    MY_TOTFRAMEHPPct:SetJustifyH("LEFT");
    MY_TOTFRAMEHPPct:ClearAllPoints();
    MY_TOTFRAMEHPPct:SetPoint("CENTER", MY_TOTFRAMEHealthBar, 0, 1);
    
    return MY_TOTFRAME
end

-- 调用创建的框架(锚点, 父级框架, 依赖于, x坐标, y坐标,宽, 高, 透明)
if not MY_TOTFRAME then return end

CreatePanel("LEFT", TargetFrame, "RIGHT", 20,  0, 96, 48, 1)

不过这段代码没有起作用,我便又请教大神,可能问的这个问题太没有之前那个有深度,大神对于这个问题没有给予任何回复。我猜目标的目标代码还得要在其它代码下才能实现,比如说触发条件等等。。。,我想以为现在的水平还不能写出触发的条件,以致此段代码就此搁置了。

时隔多日,期间也有断断续续的研究过封装代码,可没什么进展,特别是函数封装的变量作用域只在此函数内部,出了这个函数就不能用了。前几日,看到插件设置面里有这么多的CheckButton,看了看插件代码,代码也是建立了好多个CheckButton,于是又想这能 不能也建立一个模板,或者是封装一个函数里,把名称,大小,位置,等等都用变量传进去,那么就只建一个或者用一个函数进行调用就行了,也应该能简化好多的代码,于是翻找插件,找到一段代码,是TC2的代码

function option:CreateCheckButton(parent, text, typ ,val, func)
    local frame = CreateFrame("CheckButton", nil, parent);
    frame:SetNormalTexture("Interface/Buttons/UI-CheckBox-Up");
    frame:SetPushedTexture("Interface/Buttons/UI-CheckBox-Down");
    frame:SetHighlightTexture("Interface/Buttons/UI-CheckBox-Highlight");
    frame:SetCheckedTexture("Interface/Buttons/UI-CheckBox-Check");
    frame:SetHitRectInsets(0, -100, 0, 0);
    frame:SetWidth(26);
    frame:SetHeight(26);
    
    frame:SetScript("OnClick", function(self)
        db[self.typ][self.val] = self:GetChecked();
        print(self.typ..self.val)
        if self.func then self.func(self) end
    end)
    frame:SetScript("OnShow", function(self)
        self:SetChecked(db[self.typ][self.val]);
    end)
  return frame
end

紧跟着后面的调用创建 CheckButton

CreateCheckButton(panel, L["Enable"], "bar", "config", function(self)
    tCD:ToggleCenter(self:GetChecked() and true or nil);
    if not self:GetChecked() then
        tCDcoolingData = {};
        tCD:UpdateBar();
    end
end);

创建 CheckButton 倒不需要事件触发,只要位置对了就可以了,这个应该对于我来说应该可以实现,于是代码这样写

 

function CreateCheckButton(parent, text, typ ,val, f_text, func)
  local frame = CreateFrame("CheckButton", text, parent, "InterfaceOptionsCheckButtonTemplate");
  frame:SetNormalTexture("Interface/Buttons/UI-CheckBox-Up");
  frame:SetPushedTexture("Interface/Buttons/UI-CheckBox-Down");
  frame:SetHighlightTexture("Interface/Buttons/UI-CheckBox-Highlight");
  frame:SetCheckedTexture("Interface/Buttons/UI-CheckBox-Check");
  frame:SetHitRectInsets(0, -100, 0, 0);
  frame:SetWidth(26);
  frame:SetHeight(26);

 return frame
end

然后调用 CreateCheckButton 函数来创建一个 CheckButton,代码如下:

CreateCheckButton(UnitFramesPlus_Party_Options, "UnitFramesPlus_OptionsFrame_Test", nil, nil, nil, "test");
    UnitFramesPlus_OptionsFrame_Test:SetPoint("TOPLEFT", UnitFramesPlus_OptionsFrame_PartyClassIcon, "TOPLEFT", 0, -20);
    UnitFramesPlus_OptionsFrame_TestText:SetText("CheckButton模板测试");
    UnitFramesPlus_OptionsFrame_Test.tooltipText = ("2022年09月14日添加");

测试并添加成功,然后还可以把 CheckButton 显示文本也做成变量,上面 CreateCheckButton()加上一行代码

function CreateCheckButton(parent, text, typ ,val, f_text, func)
    local frame = CreateFrame("CheckButton", text, parent, "InterfaceOptionsCheckButtonTemplate");
    frame:SetNormalTexture("Interface/Buttons/UI-CheckBox-Up");
    frame:SetPushedTexture("Interface/Buttons/UI-CheckBox-Down");
    frame:SetHighlightTexture("Interface/Buttons/UI-CheckBox-Highlight");
    frame:SetCheckedTexture("Interface/Buttons/UI-CheckBox-Check");
    frame:SetHitRectInsets(0, -100, 0, 0);
    _G[text .. 'Text']:SetText(f_text);    加入这行代码
    frame:SetWidth(26);
    frame:SetHeight(26);
--[[    frame:SetPoint("TOPLEFT", parent, "TOPLEFT", 5, 22);
    
    for index,  val in pairs(DefaultDB) do
        DefaultDB["target"] = {};
        DefaultDB["target"]["index"] = val;
    end
    frame:SetScript("OnClick", function(self)
        UnitFramesPlusDB["target"]["test"] = self:GetChecked();
        print(index)
    end)
    frame:SetScript("OnShow", function(self)
        self:SetChecked(val);
    end)
    

    return frame
end

再将调用代码改成

    CreateCheckButton(UnitFramesPlus_Party_Options, "UnitFramesPlus_OptionsFrame_Test", nil, nil, "模板CheckButton测试", "test");
    UnitFramesPlus_OptionsFrame_Test:SetPoint("TOPLEFT", UnitFramesPlus_OptionsFrame_PartyClassIcon, "TOPLEFT", 0, -20);
--    UnitFramesPlus_OptionsFrame_TestText:SetText("CheckButton模板测试");
    UnitFramesPlus_OptionsFrame_Test.tooltipText = ("2022年09月14日添加");

代码已经实现,其它点击勾选功能待到以后再实现。突然又想到上次目标的目标以及目标的目标的目标实现代码,再试试,先来目标的目标

function TargetTarget_CreateButton(frame, anchor, parent, relativeTo, x, y, w, h, a, s)
   local frame = CreateFrame("Button", frame, UIParent, "SecureUnitButtonTemplate"); frame:SetFrameStrata("LOW"); frame:SetFrameLevel(8); frame:SetWidth(w); frame:SetHeight(h); frame:ClearAllPoints(); frame:SetPoint(anchor, parent, relativeTo, x, y); frame:SetAlpha(a); frame:SetScale(s); frame.Portrait = frame:CreateTexture("ToTPortrait", "BORDER"); frame.Portrait:SetWidth(27); frame.Portrait:SetHeight(27); frame.Portrait:ClearAllPoints(); frame.Portrait:SetPoint("TOPRIGHT", frame, "TOPRIGHT", -5, -6); frame.Texture = frame:CreateTexture("ToTTexture", "ARTWORK"); frame.Texture:SetTexture("Interface\\TargetingFrame\\UI-PartyFrame"); frame.Texture:SetTexCoord(1, 0, 0, 1) -- 血条靠近头像上方 --frame.Texture:SetTexCoord(1, 0, 1, 0) -- 血条靠近头像下方 frame.Texture:SetWidth(96); frame.Texture:SetHeight(48); frame.Texture:ClearAllPoints(); frame.Texture:SetPoint("TOPLEFT", frame, "TOPLEFT", 0, -2); frame.Name = frame:CreateFontString("ToTName", "ARTWORK", "GameFontNormalSmall"); frame.Name:ClearAllPoints(); frame.Name:SetPoint("BOTTOMLEFT", frame, "BOTTOMLEFT", 9, 40); frame.HealthBar = CreateFrame("StatusBar", "ToTHealthBar", UIParent); frame.HealthBar:SetStatusBarTexture("Interface\\TargetingFrame\\UI-StatusBar"); frame.HealthBar:SetFrameStrata("LOW"); frame.HealthBar:SetFrameLevel(7); frame.HealthBar:SetMinMaxValues(0, 100); frame.HealthBar:SetValue(0); frame.HealthBar:SetWidth(53); frame.HealthBar:SetHeight(6); frame.HealthBar:ClearAllPoints(); frame.HealthBar:SetPoint("TOPLEFT", frame, "TOPLEFT", 9, -10); frame.HealthBar:SetStatusBarColor(0, 1, 0); frame.HealthBar:SetScale(s); frame.ManaBar = CreateFrame("StatusBar", "ToTPowerBar", UIParent); frame.ManaBar:SetStatusBarTexture("Interface\\TargetingFrame\\UI-StatusBar"); frame.ManaBar:SetFrameStrata("LOW"); frame.ManaBar:SetFrameLevel(7); frame.ManaBar:SetMinMaxValues(0, 100); frame.ManaBar:SetValue(0); frame.ManaBar:SetWidth(53); frame.ManaBar:SetHeight(6); frame.ManaBar:ClearAllPoints(); frame.ManaBar:SetPoint("TOPLEFT",ToTFrame, "TOPLEFT", 10, -16); frame.ManaBar:SetStatusBarColor(0, 0, 1); frame.ManaBar:SetScale(s);

  -- 血条在左头像在右结束
  frame.HPPct = frame:CreateFontString("ToTHPPct", "ARTWORK", "TextStatusBarText");
  frame.HPPct:SetFont("fonts\\1.ttf", 10, "OUTLINE");
  frame.HPPct:SetJustifyH("LEFT");
  frame.HPPct:ClearAllPoints();
  frame.HPPct:SetPoint("CENTER", ToTFrame.HealthBar, 0, 1);

  return frame

end

再来调用的代码

-- 框体名称,锚点,父框体,依靠,x,y,宽,高,透明,缩放
TargetTarget_CreateButton("ToTFrame", "LEFT", TargetFrame, "RIGHT", -20, 0, 96, 48, 0, 1)

最后添加触发代码

local tot = CreateFrame("Frame");
function UnitFramesPlus_TargetTarget()
    if UnitFramesPlusDB["target"]["tot"] == 1 then
        tot:SetScript("OnUpdate", function(self, elapsed)
            self.timer = (self.timer or 0) + elapsed;
            if self.timer >= 0.1 then
                if UnitExists("targettarget") then
                    ToTFrame:SetAlpha(1);
                    ToTFrame.HealthBar:SetAlpha(1);
                    ToTFrame.ManaBar:SetAlpha(1);
                    ToTFrame.Name:SetText(UnitName("targettarget"));

                    local ToTNameColor = PowerBarColor[UnitPowerType("targettarget")] or PowerBarColor["MANA"];
                    ToTFrame.ManaBar:SetStatusBarColor(ToTNameColor.r, ToTNameColor.g, ToTNameColor.b);

                    SetPortraitTexture(ToTFrame.Portrait, "targettarget");
                    ToTLevel:SetText(UnitLevel("targettarget"))
                    if UnitHealthMax("targettarget") > 0 then
                        ToTFrame.HealthBar:SetValue(UnitHealth("targettarget") / UnitHealthMax("targettarget") * 100);
                        local HPPct = "";
                        if UnitFramesPlusDB["target"]["tothppct"] == 1 then
                            HPPct = math.floor(UnitHealth("targettarget") / UnitHealthMax("targettarget") * 100).."%";
                        end
                        ToTFrame.HPPct:SetText(HPPct);
                        ToTFrame.HPPct:SetTextColor(UnitFramesPlus_GetRGB(UnitHealth("targettarget"), UnitHealthMax("targettarget")));
--                        ToTFrame.HealthBar:SetStatusBarColor(UnitFramesPlus_GetRGB(UnitHealth("targettarget"), UnitHealthMax("targettarget")));
                    else
                        ToTFrame.HealthBar:SetValue(0);
                        ToTFrame.HPPct:SetText("");
                    end
                        ToTFrame.HealthBar:SetStatusBarColor(UnitFramesPlus_GetRGB(UnitHealth("targettarget"), UnitHealthMax("targettarget")));
                    if UnitPowerMax("targettarget") > 0 then
                        ToTFrame.ManaBar:SetValue(UnitPower("targettarget") / UnitPowerMax("targettarget") * 100);
                    else
                        ToTFrame.ManaBar:SetValue(0);
                    end
                else
                    ToTFrame:SetAlpha(0);
                    ToTFrame.HealthBar:SetValue(0);
                    ToTFrame.ManaBar:SetValue(0);
                    ToTFrame.HPPct:SetText("");
                end
                self.timer = 0;
            end
        end);
        
    else
        ToTFrame:SetAlpha(0);
        ToTFrame.HealthBar:SetValue(0);
        ToTFrame.ManaBar:SetValue(0);
        ToTFrame.HPPct:SetText("");
        tot:SetScript("OnUpdate", nil);
    end
end

进入WOW,无目标的目标时是酱紫的,有目标的目标时

 

效果是出来了一半,框体,头像,姓名都出来了,就生命条和魔法条没出来,再看下错误信息,一直跳这个,由于目标的目标级别的代码还没有写,

 

 

 将这行注释点

 

 

 再进WOW,找个目标的目标有魔法值的角色看下,没有问题,怎么之前目标的目标一直出不来,之前的代码也找不出来了,既然目标的目标可以显示,那目标的目标的目标应该也可以实现了吧,下面就是模板

function TargetTarget_CreateButton(frame, anchor, parent, relativeTo, x, y, w, h, a, s)
    local str = frame
    local frame = CreateFrame("Button", frame, UIParent, "SecureUnitButtonTemplate");
    frame:SetFrameStrata("LOW");
    frame:SetFrameLevel(8);
    frame:SetWidth(w);
    frame:SetHeight(h);
    frame:ClearAllPoints();
    frame:SetPoint(anchor, parent, relativeTo, x, y);
    frame:SetAlpha(a);
    frame:SetScale(s);

    frame.Portrait = frame:CreateTexture(str..".Portrait", "BORDER");
    frame.Portrait:SetWidth(27);
    frame.Portrait:SetHeight(27);
    frame.Portrait:ClearAllPoints();
    frame.Portrait:SetPoint("TOPRIGHT", frame, "TOPRIGHT", -5, -6);

    frame.Texture = frame:CreateTexture(str..".Texture", "ARTWORK");
    frame.Texture:SetTexture("Interface\\TargetingFrame\\UI-PartyFrame");
    frame.Texture:SetTexCoord(1, 0, 0, 1)    -- 血条靠近头像上方
    --frame.Texture:SetTexCoord(1, 0, 1, 0)    -- 血条靠近头像下方
    frame.Texture:SetWidth(96);
    frame.Texture:SetHeight(48);
    frame.Texture:ClearAllPoints();
    frame.Texture:SetPoint("TOPLEFT", frame, "TOPLEFT", 0, -2);

    frame.Name = frame:CreateFontString(str..".Name", "ARTWORK", "GameFontNormalSmall");
    frame.Name:ClearAllPoints();
    frame.Name:SetPoint("BOTTOMLEFT", frame, "BOTTOMLEFT", 9, 40);

    frame.HealthBar = CreateFrame("StatusBar", str..".HealthBar", UIParent);
    frame.HealthBar:SetStatusBarTexture("Interface\\TargetingFrame\\UI-StatusBar");
    frame.HealthBar:SetFrameStrata("LOW");
    frame.HealthBar:SetFrameLevel(7);
    frame.HealthBar:SetMinMaxValues(0, 100);
    frame.HealthBar:SetValue(0);
    frame.HealthBar:SetWidth(53);
    frame.HealthBar:SetHeight(6);
    frame.HealthBar:ClearAllPoints();
    frame.HealthBar:SetPoint("TOPLEFT", frame, "TOPLEFT", 9, -10);
    frame.HealthBar:SetStatusBarColor(0, 1, 0);
    frame.HealthBar:SetScale(s);

    frame.ManaBar = CreateFrame("StatusBar", str..".ManaBar", UIParent);
    frame.ManaBar:SetStatusBarTexture("Interface\\TargetingFrame\\UI-StatusBar");
    frame.ManaBar:SetFrameStrata("LOW");
    frame.ManaBar:SetFrameLevel(7);
    frame.ManaBar:SetMinMaxValues(0, 100);
    frame.ManaBar:SetValue(0);
    frame.ManaBar:SetWidth(53);
    frame.ManaBar:SetHeight(6);
    frame.ManaBar:ClearAllPoints();
    frame.ManaBar:SetPoint("TOPLEFT", frame, "TOPLEFT", 10, -16);
    frame.ManaBar:SetStatusBarColor(0, 0, 1);
    frame.ManaBar:SetScale(s);
    -- 血条在左头像在右结束

    frame.HPPct = frame:CreateFontString(str..".HPPct", "ARTWORK", "TextStatusBarText");
    frame.HPPct:SetFont("fonts\\1.ttf", 10, "OUTLINE");

    frame.HPPct:SetJustifyH("LEFT");
    frame.HPPct:ClearAllPoints();
    frame.HPPct:SetPoint("CENTER", frame.HealthBar, 0, 1);

    return frame
end

 

 

 

 这代码有一点搞不清楚,比如

frame.Portrait = frame:CreateTexture(str..".Portrait", "BORDER");

其实就是

frame.Portrait = frame:CreateTexture(frame..".Portrait", "BORDER");

为何用 frame..".Portrait" 会提示  这个错误,好像是提示 frame 是一个无效的表值,解决方法是先 将 frame 赋给一个字符串,local str = frame,然后用str 代替 frame 代码即可解决这个错误,上图,每个框体都有自己的名称 <<< 这是TOT 即目标的目标

 

<<< 这是TOTOT 即目标的目标的目标,基础的功能目前已经实现了,后面就是实现

 

等级显示及开关,翻转头像及开关,职业小图标显示及开关目标的目标显示DUBUFF及开关。

先用刚才建立CheckButton 封装函数来建立一个 翻转头像的CheckButton,怪事,昨天框体显示还好好的,今天就不显示,还出莫名其妙的错误,调用目标的目标的函数无效,明明初始化函数里已经有了同名的函数,后来改个名字就好了,同样的代码,不知哪里出问题了,下面新建一个翻转头像的 CheckButton 代码

CreateCheckButton(UnitFramesPlus_Player_Options, "UnitFramesPlus_OptionsFrame_reversal", nil, "reversal", Target_reversal, "reversal")
UnitFramesPlus_OptionsFrame_reversal:SetPoint("TOPLEFT", UnitFramesPlus_OptionsFrame_TargetPctMP, "TOPLEFT", 140, 0);

   接下来就是写一个翻转头像的代码

function frame_revered_display(frame)
    local str = frame
    if UnitExists("targettarget") and UnitFramesPlusDB["target"]["revered"] == 1 then 
        frame.Texture:ClearAllPoints();
        frame.Portrait:ClearAllPoints();
        frame.Name:ClearAllPoints();
        frame.HealthBar:ClearAllPoints();
        frame.ManaBar:ClearAllPoints();        
        frame.Texture:SetTexture("Interface\\TargetingFrame\\UI-PartyFrame");
        frame.Texture:SetTexCoord(1, 0, 0, 1)    -- 血条靠近头像上方
        frame.Portrait:SetPoint("TOPRIGHT", frame, "TOPRIGHT", -5, -6);        --1头像在左
        frame.Name:SetPoint("TOP", frame.HPPct, "TOP", 0, 13);        -- 1名字在右
        frame.HealthBar:SetPoint("TOPLEFT", frame, "TOPLEFT", 9, -10);        -- 1血条在左
        frame.ManaBar:SetPoint("TOPLEFT", frame, "TOPLEFT", 10, -16);            -- 1魔法条在左
    else
        frame.Texture:ClearAllPoints();
        frame.Portrait:ClearAllPoints();
        frame.Name:ClearAllPoints();
        frame.HealthBar:ClearAllPoints();
        frame.ManaBar:ClearAllPoints();
        frame.Texture:SetTexture("Interface\\TargetingFrame\\UI-PartyFrame");
        frame.Texture:SetTexCoord(0, 1.0, 0, 1.0);
        frame.Portrait:SetPoint("TOPLEFT", frame, "TOPLEFT", 5, -6);                --头像在右
        frame.Name:SetPoint("TOP", frame.HPPct, "TOP", 0, 13);                -- 名字在左
        frame.HealthBar:SetPoint("TOPRIGHT", frame, "TOPRIGHT", -9, -10);            -- 血条在右
        frame.ManaBar:SetPoint("TOPLEFT", frame.HealthBar, "BOTTOMLEFT", 0, 0);    -- 魔法条在右
    end
end

有一点也就是弄不明白就是

frame.ManaBar:SetPoint("TOPLEFT", frame.HealthBar, "BOTTOMLEFT", 0, 0);    -- 魔法条在右

这行代码中的 frame.HealthBar在这个函数里一定要用 frame.HealthBar  这样的格式,而在上面建立模板的时候却要用 str..".ManaBar" 这样的格式 ,用 frame.HealthBar 这样的格式就会报错,但今天却又可以用  frame.HealthBar ,现在想来是不是和其它某个插件的冲突,然后在翻转头像的 CheckButton 代码里写上点击触了代码

CreateCheckButton(UnitFramesPlus_Player_Options, "UnitFramesPlus_OptionsFrame_reversal", nil, "reversal", Target_reversal, "reversal")
    UnitFramesPlus_OptionsFrame_reversal:SetPoint("TOPLEFT", UnitFramesPlus_OptionsFrame_TargetPctMP, "TOPLEFT", 140, 0);
    UnitFramesPlus_OptionsFrame_reversal:SetScript("OnClick", function(self)
        if UnitFramesPlusDB["target"]["revered"] == 1 then
            UnitFramesPlusDB["target"]["revered"] = 0;
        else
            UnitFramesPlusDB["target"]["revered"] = 1;
        end
        frame_revered_display(ToTFrame)
        frame_revered_display(ToToTFrame)
        self:SetChecked(UnitFramesPlusDB["target"]["revered"])
    end)

 

两个可以即时切换

 

标签:TOPLEFT,留个,封装,ETT,frame,MY,SetPoint,模板,ClearAllPoints
From: https://www.cnblogs.com/hsoftk/p/16702192.html

相关文章

  • 压位高精度封装
    C++压位高精度封装模板普通的高精度算法都是一位只存放一个数字,但是我们这样显然浪费了大量的空间,一个int可以存放超过\(2\times10^9\)大小的数,考虑利用int的多位......
  • C++模板类-数组
    /*Container.h所有容器的基类/*MemoryObject内存申请基类我使用TBB申请内存*/template<typenameT> classContainer:publicMemoryObject { protected: T*C......
  • C++模板基础知识
    源码编译环境:win10x86反汇编软件:IDAPro(胖大妈)第一次接触到模板是在C#的泛型编程,对其表面的理解是可以对一些约束范围内参数类型的方法进行重用,可以少写一些方法。在后......
  • 【模板】筛法
    \(prime\)constintN=1e7+10;intprime[N];boolnotprime[N];intminprime[N];voidprime_sieve(constintmaxn){ registerinti,multi_helper; registerin......
  • 微信小程序排行榜页面模板
    1需求在开发一款背单词的微信小程序时,为了加强用户的体验感,刺激用户积极学习,小程序中需要有排行榜的模块。通过打开天数来排名,让用户有攀比学习的心里。具体的页面截图如......
  • 模板基类与正确的派生类函数调用--Effective C++ Item 43
    问题描述假设我们有这样一个业务场景,我们管理着许多公司,每个公司都有一个自己的许多日志信息需要处理,于是为了方便,我们写了一个模板类用来处理这些公司的信息,并且将这些公......
  • 运算符重载与过度封装的弊病——记一次python debug
    注:标题用了C++的说法,实际上python里应该叫运算符对应的魔法方法?不过大致意思一样,而且标题太长估计不好看。久仰pandas包的大名,今天要处理csv文件,便去翻了翻文档,看了下入门......
  • 设计模式系列1 - 模板模式&策略模式
    分别讲述模板模式和策略模式的使用姿势,以及两者的区别,基于java。往期精选(欢迎转发~~)Java全套学习资料(14W字),耗时半年整理消息队列:从选型到原理,一文带你全部掌握肝了......
  • python3+request 接口登录封装
    #-*-coding:utf-8-*-#@Desc:请求的封装importjsonimportreimportrequestsTOKEN='xxx-xxx-xxx-xxx'classMyRequests:def__init__(self,h......
  • #yyds干货盘点#【愚公系列】2022年10月 微信小程序-日期时间组件封装
    前言微信小程序默认是没日期和时间组件的,而是分开的,需要使用日期和时间组件需要自己封装。一、组件源码<pickermode="multiSelector"value="{{valueArray}}"range="{{......