Thread Tools Display Modes
04-15-24, 10:58 AM   #1
Xrystal
nUI Maintainer
 
Xrystal's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 5,935
StatusBar / Texture - Width/Coloring issues

I've been slowly working on the nUI replacement. But I have hit a snag with creating a simple health bar.

I tried StatusBar control which correctly resized the bar according to the min/max and value settings. But, it wouldn't let me change the bar color based on the percentage.

I noticed nUI used a regular frame with a texture, so thought I would try that route. So, I had a frame holding a black texture that was always max width to the frame width and a texture holding the bar that adjusts its colours according to the percentage of health.

However, I have not been able to get the SetWidth command to work with the resizable texture. If I clear all its points and then set the single TOPLEFT point it stays at minWidth (0), if I don't clear all the points it stays at max width (150).

The new width is correctly being calculated according to current heath and max health ratio and the parent frames width but the function to actually SetWidth appears to be ignoring it.

I keep going through the code but can't see anything I have missed or added extra. Below should be all the pertinent code blocks that affects the element in question. Hopefully someone will see something my eyes are clearly missing, or I am misunderstanding in general rofl.

Thanks in advance.

This is the template that I created for it
Lua Code:
  1. <Frame name = "XrystalUI_StatusBarTemplate" virtual = "true">        
  2.         <Size x="5" y="5"/>
  3.         <Layers>
  4.         <Layer level = "BACKGROUND" textureSubLevel="-2">
  5.             <Texture parentKey = "Background" />
  6.         </Layer>        
  7.         <Layer level = "BACKGROUND" textureSubLevel="2">
  8.             <Texture parentKey = "Bar" />              <<<<<<<<<<<<<<<<<
  9.         </Layer>        
  10.         <Layer level = "OVERLAY" textureSubLevel = "2">
  11.             <FontString parentKey="CenterText">
  12.                 <Anchors>
  13.                     <Anchor point="CENTER" x="0" y="0"/>
  14.                 </Anchors>
  15.             </FontString>
  16.             <FontString parentKey="LeftText">
  17.                 <Anchors>
  18.                     <Anchor point="LEFT" x="2" y="0"/>
  19.                 </Anchors>
  20.             </FontString>
  21.             <FontString parentKey="RightText">
  22.                 <Anchors>
  23.                     <Anchor point="RIGHT" x="-2" y="0"/>
  24.                 </Anchors>
  25.             </FontString>        
  26.         </Layer>
  27.         </Layers>
  28.     </Frame>

This is the layout settings being used ( will become the default settings when finished )
Lua Code:
  1. addonNS.Solo.Player.HealthBar = {
  2.     width = 150,
  3.     height = 20,
  4.     anchor = {                              
  5.         point = "TOPLEFT",
  6.         relativeTo = "Portrait",
  7.         relativePoint = "TOPRIGHT",
  8.         xOffset = "5",
  9.         yOffset = "0",
  10.     },
  11.     fontObject = "GameFontHighlightOutline",
  12.     --orientation = "HORIZONTAL", -- also "VERTICAL"
  13.     --fillStyle = "STANDARD",
  14.     color =
  15.     {
  16.         ["min"] = { r = 1, g = 0, b = 0, a = 1 },   -- empty bar color
  17.         ["mid"] = { r = 1, g = 1, b = 0, a = 1 },   -- bar color at 50%
  18.         ["max"] = { r = 0, g = 1, b = 0, a = 1 },   -- full bar color
  19.     },
  20.    
  21. }

The frame is being created with this code and is called when the unit frame in question has finished it's creation stage
Lua Code:
  1. addonNS.HealthBar.Create = function(unitFrame)
  2.     local newFrame = CreateFrame("StatusBar","XrystalUI_".. unitFrame.unitKey .. "_HealthBar",unitFrame,"XrystalUI_StatusBarTemplate")
  3.    
  4.     newFrame.unitFrame = unitFrame
  5.  
  6.     newFrame:RegisterEvent("PLAYER_ENTERING_WORLD")
  7.     newFrame:RegisterEvent("UNIT_HEALTH")
  8.     newFrame:RegisterEvent("UNIT_MAXHEALTH")
  9.     newFrame:SetScript("OnEvent",OnHealthEvent)
  10.    
  11.     return newFrame
  12. end

-- This is used to apply the layout information and is called by the unit frame when doing their layout updates. It is also aimed to be used when the layout is changed ( at present just a default layout used )
Lua Code:
  1. addonNS.HealthBar.ApplyLayout = function(newFrame, activeFrameLayout, defaultFrameLayout)
  2.     local activeLayout = activeFrameLayout.HealthBar
  3.     local defaultLayout = defaultFrameLayout.HealthBar
  4.     local relativeTo = addonNS.UnitFrames.IdentifyRelativeTo(activeLayout, defaultLayout, newFrame)
  5.    
  6.     newFrame:SetPoint(  activeLayout.anchor.point or defaultLayout.anchor.point,
  7.                         relativeTo,
  8.                         activeLayout.anchor.relativePoint or defaultLayout.anchor.relativePoint,
  9.                         activeLayout.anchor.xOffset or defaultLayout.anchor.xOffset,
  10.                         activeLayout.anchor.yOffset or defaultLayout.anchor.yOffset   )
  11.    
  12.     newFrame:SetSize(   activeLayout.width or defaultLayout.width,
  13.                         activeLayout.height  or defaultLayout.height   )
  14.    
  15.     -- Set up the BarTexture's Background
  16.     newFrame.Background:SetColorTexture(0,0,0,1)
  17.     newFrame.Background:SetAllPoints()
  18.    
  19.     -- Set up the BarTexture Position  <<<<<<<<<<<
  20.     newFrame.Bar:ClearAllPoints()
  21.     newFrame.Bar:SetPoint("LEFT",newFrame,"LEFT",0,0)
  22.     newFrame.Bar:SetWidth(5)
  23.    
  24.     -- Set up the texts font objects
  25.     local fontObject = activeLayout.fontObject or defaultLayout.fontObject
  26.     newFrame.CenterText:SetFontObject(fontObject)
  27.     newFrame.LeftText:SetFontObject(fontObject)
  28.     newFrame.RightText:SetFontObject(fontObject)
  29. end

And this is the code it executes whenever there is a health related event triggered ( currently UNIT_HEALTH and UNIT_MAXHEALTH and PLAYER_ENTERING_WORLD )
Lua Code:
  1. addonNS.HealthBar.UpdateBarTexture = function(unitFrame)
  2.     print("UpdateBarTexture")
  3.     local activeFrameLayout,defaultFrameLayout = addonNS.UnitFrames.IdentifyLayoutsForUnit(unitFrame.unitKey)
  4.    
  5.     local activeLayout = activeFrameLayout and activeFrameLayout.HealthBar
  6.     local defaultLayout = defaultFrameLayout and defaultFrameLayout.HealthBar
  7.     activeLayout = activeLayout or defaultLayout
  8.  
  9.     local health = unitFrame.health
  10.     local maxHealth = unitFrame.maxHealth
  11.    
  12.     local healthRatio = tonumber(health) / tonumber(maxHealth)
  13.     local minRange = math.min( healthRatio, 1.0 )
  14.     local maxRange = math.max( 0.0, minRange)
  15.    
  16.     local offset = maxRange
  17.     unitFrame.offset = maxRange
  18.    
  19.     print(offset * activeLayout.width)
  20.     unitFrame.HealthBar.Bar:SetWidth(offset * activeLayout.width)
  21.    
  22.     local range, color1, color2
  23.     if offset > 0.5 then               
  24.         range  = (offset - 0.5) * 2.0
  25.         color1 = activeLayout.color["mid"]
  26.         color2 = activeLayout.color["max"]        
  27.     else
  28.         range  = offset * 2.0
  29.         color1 = activeLayout.color["min"]
  30.         color2 = activeLayout.color["mid"]                
  31.     end
  32.    
  33.     local r = (color2.r - color1.r) * range + color1.r;
  34.     local g = (color2.g - color1.g) * range + color1.g;
  35.     local b = (color2.b - color1.b) * range + color1.b;
  36.     local a = (color2.a - color1.a) * range + color1.a;    
  37.    
  38.     unitFrame.HealthBar.Bar:SetColorTexture(r,g,b,1)    
  39.    
  40. end
__________________


Characters:
Gwynedda - 70 - Demon Warlock
Galaviel - 65 - Resto Druid
Gamaliel - 61 - Disc Priest
Gwynytha - 60 - Survival Hunter
Lienae - 60 - Resto Shaman
Plus several others below level 60

Info Panel IDs : http://www.wowinterface.com/forums/s...818#post136818
  Reply With Quote
04-15-24, 12:05 PM   #2
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,881
From a quick look, I don't see .Bar ever getting a SetHeight() setting, just a width so you won't ever actually see the bar using a single anchor point. Maybe use "TOPLEFT" and "BOTTOMLEFT" in the template if it's only ever going to size in one direction
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 04-15-24 at 12:10 PM.
  Reply With Quote
04-15-24, 12:16 PM   #3
Xrystal
nUI Maintainer
 
Xrystal's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 5,935
That is a good point. I totally forgot it didn't have a height either.

But, I somehow managed to get things to work again as a status bar.

Can't think what is different outside of using a different texture. Instead of setting BarTexture in the xml to an atlas ( example from one of the blizzard files) , I used SetStatusBarTexture with the texture used in the example here ( https://warcraft.wiki.gg/wiki/UIOBJECT_StatusBar ).

I changed the following in ApplyLayout after changing the Frame to StatusBar in the xml and removing the alternative bar texture section.
Lua Code:
  1. -- Set up the BarTexture Position
  2.     --newFrame.Bar:ClearAllPoints()
  3.     --newFrame.Bar:SetPoint("LEFT",newFrame,"LEFT",0,0)
  4.     --newFrame.Bar:SetWidth(50)
  5.    
  6.     newFrame:SetStatusBarTexture("Interface/TargetingFrame/UI-StatusBar")
  7.     newFrame:SetStatusBarColor(0, 1, 0)
  8.     newFrame:SetMinMaxValues(0, 1)
  9.     newFrame:SetValue(0)

And the following in UpdateBarTexture
Lua Code:
  1. --unitFrame.HealthBar.Bar:SetWidth(offset * activeLayout.width)
  2.     --unitFrame.HealthBar.Bar:SetColorTexture(r,g,b,1)    
  3.    
  4.     unitFrame.HealthBar:SetStatusBarColor(r,g,b)
  5.     unitFrame.HealthBar:SetMinMaxValues(0, maxHealth)
  6.     unitFrame.HealthBar:SetValue(health)

The bar is shrinking and the color is changing as expected. Thanks for the reminder though.
__________________


Characters:
Gwynedda - 70 - Demon Warlock
Galaviel - 65 - Resto Druid
Gamaliel - 61 - Disc Priest
Gwynytha - 60 - Survival Hunter
Lienae - 60 - Resto Shaman
Plus several others below level 60

Info Panel IDs : http://www.wowinterface.com/forums/s...818#post136818
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » StatusBar / Texture - Width/Coloring issues


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off