View Single Post
07-10-14, 10:48 AM   #6
Rainrider
A Firelord
AddOn Author - Click to view addons
Join Date: Nov 2008
Posts: 454
It seems to me you are a bit confused about the structure of your layout and that you are copying stuff from somewhere else without fully understanding it. Let's try it step by step.

Widgets in World of Warcraft are just tables to Lua. So you just attach oUF elements to the table you create for a certain unit.

The function you use to describe the look of your frames (the one you called Spawn at line 10 in your pastebin) is your style function and it is run by oUF for every unit you spawn through oUF:Spawn.

The style function receives 3 arguments:
  1. the unit button - that is what you called Frame at line 10. Technically this is a frame of type "Button" (so that mouse interaction is possible for it), that's why it is called a unit button. Remember, to Lua this is still just a table.
  2. unit - a string identifying the unit corresponding to the unit button. In your case it will be either "player", "target" or "pet".
  3. isSingle - a boolean telling you whether you get a header or a single unit. You are currently only creating single units, so just leave this be for the moment.

So let's start empty and try to style the player frame (the unit button for the player unit), add a health bar and a health value to it.

lua Code:
  1. local function Style(frame, unit, isSingle)
  2.     frame:SetSize(100, 25)
  3.  
  4.     -- create a background for the frame
  5.     local frameBG = frame:CreateTexture(nil, "BACKGROUND")
  6.     frameBG:SetAllPoints()
  7.     frameBG:SetTexture(0, 0, 0, 0.5)
  8.  
  9.     -- define the health element
  10.     local health = CreateFrame("StatusBar", nil, frame)
  11.     health:SetSize(100, 15)
  12.     health:SetPoint("TOPLEFT", frame, "TOPLEFT", 0, 0)
  13.     -- let oUF handle coloring
  14.     -- the key names matter!
  15.     health.colorReaction = true
  16.     health.colorClass = true
  17.  
  18.     -- create a background for the health bar
  19.     local healthBG = health:CreateTexture(nil, "BACKGROUND")
  20.     healthBG:SetAllPoints()
  21.     healthBG:SetTexture("Interface\\ChatFrame\\ChatFrameBackground")
  22.     -- make the background inherit the color of the status bar and be darker
  23.     healthBG.multiplier = 0.3
  24.     -- attach the background to the health bar table
  25.     -- the table key has to be named bg
  26.     health.bg = healthBG
  27.  
  28.     -- register it with oUF
  29.     -- this means you create a table key named Health
  30.     frame.Health = health
  31.  
  32.     -- create the health text
  33.     -- the health bar is the parent of the fontstring
  34.     -- because we want it to appear on the health bar
  35.     local healthText = health:CreateFontString(nil, "OVERLAY", GameFontNormal)
  36.     healthText:SetPoint("RIGHT", health, "RIGHT")
  37.     frame:Tag(healthText, "[dead][curhp]")
  38. end

Beware the order in that we define things. The unit button is passed as an argument to the Style function, that is the reason I can use it right away. You generally can't use stuff that hasn't been defined yet. So if you want to create a background for your health bar, you have to create the health bar first.

So what did I do and why:
I created a background for the unit button so that you can have a visual representation for it and can better see how it is used to hold the other stuff we apply to the frame.

Then I created the health bar and a background texture for it, so that you can see how the health bar sits on top of the frame and how it looks when it depletes.

Then I created a tag to hold the current health value and display "Dead" before it, if the unit is dead.

Beware that I didn't position the frame. In your code you used a single statement for positioning regardless of what frame your Style function is being called for. Thus all your spawned frames appear on top of each other and you have to move them with oUF_MovableFrames. Let's position them properly:

lua Code:
  1. oUF:RegisterStyle("Kygo", Style)
  2. oUF:SetActiveStyle("Kygo")
  3. local player = oUF:Spawn("player")
  4. player:SetPoint("CENTER", UIParent, "CENTER", 0, 0)
  5.  
  6. local target = oUF:Spawn("target")
  7. target:SetPoint("LEFT", player, "RIGHT", 20, 0)
  8.  
  9. local pet = oUF:Spawn("pet")
  10. pet:SetPoint("BOTTOMLEFT", player, "TOPLEFT", 0, 20)

The above goes at the end of you file. Beware that we now call our style function Style. I changed the name so that you differentiate it from Spawn, which is a function oUF provides. Now the target frame is positioned 20px right to the player frame, and the pet frame is 20px above the player. Those settings will be overridden by oUF_MovableFrames with the values you set previously by moving your frames around.

I hope you now see how oUF looks for certain table keys attached to your frames and does it's magic based on their existence. Please note that the code provided is dry coded and might not function
  Reply With Quote