WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   General Authoring Discussion (https://www.wowinterface.com/forums/forumdisplay.php?f=20)
-   -   reliably setting color of frames (https://www.wowinterface.com/forums/showthread.php?t=57809)

sotdumm 02-07-20 05:05 AM

reliably setting color of frames
 
Hey,

I am working on a study project. I am creating multiple colored frames and I am trying to make them change their color.
I have tried to methods, the first one setting the color via SetTexture and then using SetColorTexture to change the color of the texture on the frame. This is very unreliable, (maybe I am initalising the farmes incorrectly...?) but they sometimes dont show up, or show up in black without the color changing. Doing a couple of /reload fixes them but, its a very undeterministic behaviour. They show up as white, but doesn't change. Code for this one, they somehow get stuck with being white:
Code:

local frame = CreateFrame("Frame", k, UIParent)
    frame:SetPoint("TOPLEFT", x, heightFromCorner)
    frame:SetHeight(BOX_SIZE)
    frame:SetWidth(BOX_SIZE)

      frame.texture = frame:CreateTexture()
      frame.texture:SetAllPoints(frame)
      frame.texture:SetColorTexture(1,1,1,1)
    frame:Show()
frame:SetScript(
    "OnUpdate",
    function(self, event)
        SetPlayerHealth(GetPlayerHealth())
    end

The other method that I have tried was using SetBackdrop but that way the frames show up as green. I am trying to load a simple background texture, so it should be loadable, doing it like this:
Code:

frame:SetBackdrop({
      bgFile = [[Interface\AddOns\myaddon\data\backdrop.tga]],
      insets = {top = 0, left = 0, bottom = 0, right = 0},
      })

I suspect something is off with the way I am trying to update the color of the frames, but since I assign a callback to the OnUpdate event, the update should be handled correctly.

Can you give any insights to this?

Thank you

Fizzlemizz 02-07-20 07:40 AM

You don't tell us what the Set and Get functions are doing (setting the colours I'm guessing at here). Also, OnEvent and OnUpdate are two different scripts to do two different things. The game will trigger health update events which might be better in this situation than using OnUpdate which will cycle far more often than health gets updated (especially out of combat).

Something like:

Lua Code:
  1. local function GetPlayerHealth(unit)
  2.     local max = UnitHealthMax(unit)
  3.     local health = UnitHealth(unit)
  4.     --local r, g, b = ...
  5.     -- do the math, get the colours
  6.     return r, g, b
  7. end
  8.  
  9. local function SetPlayerHealth(self, r, g, b)
  10.     self.texture:SetVertexColor(r, g, b)
  11. end
  12.  
  13. local frame = CreateFrame("Frame", k, UIParent)
  14. frame:SetPoint("TOPLEFT", x, heightFromCorner)
  15. frame:SetHeight(BOX_SIZE)
  16. frame:SetWidth(BOX_SIZE)
  17. frame.texture = frame:CreateTexture()
  18. frame.texture:SetAllPoints(frame)
  19. frame.texture:SetTexture("Interface/BUTTONS/WHITE8X8")
  20. --frame.texture:SetColorTexture(1,1,1,1)
  21.  
  22. frame:SetScript("OnEvent", function(self, event, ...)
  23.     -- don't need to check for the event type because only one is registered.
  24.     local unit = ...
  25.     if unit == "player" then
  26.         SetPlayerHealth(self, GetPlayerHealth(unit))
  27.     end
  28.    
  29. end)
  30. frame:RegisterEvent("UNIT_HEALTH_FREQUENT")
  31. SetPlayerHealth(frame, GetPlayerHealth("player")) -- set initial colours


All times are GMT -6. The time now is 08:40 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI