View Single Post
03-19-14, 06:59 PM   #6
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
To answer your original question, if you want your custom texture to follow the original bar's color (which oUF automatically sets) just grab the color from the bar in your update function:

Code:
local r, g, b = power:GetStatusBarColor()
power.texture:SetVertexColor(r, g, b)
As for the other stuff, rather than hooking bar updates, just tell oUF to call your function after running its own update:
Code:
hp.PostUpdate = updateHealth
instead of:
Code:
hp:SetScript("OnValueChanged", updateHealth)
The other advantage of doing this properly is that your function gets the current value, max value, and (for power) power tyep passed in as arguments, so you don't have to call the same functions oUF already called to get the same values.

The only place you need to hook a script is for the castbar, since oUF doesn't provide any OnUpdate callback. Since OnUpdate is fairly expensive, I'd suggest not hooking it at all if the "castScroll" setting isn't enabled, rather than checking for that setting in the function itself.

-----

I'm still not quite sure what you're intending to do by setting the height of the power bar to 0 when you create the rotating texture, though. If you want to hide the original bar, just do that, eg. "power:Hide()". However, it looks like you're just duplicating the texture so you can rotate it. If that's the case, you don't need to create a whole new texture; just the texture object that's already on the statusbar:

Code:
power.texture = power:GetStatusBarTexture()
You can manipulate it just like any other texture object, eg. with SetPoint, SetScale, SetRotation, SetTexture, etc. The statusbar-specific methods are just wrappers around these functions, eg. SetValue just sizes and positions the texture relative to the bar, appropriate for the provided value.

You may also need to "remove" the regular statusbar methods so oUF isn't messing with the texture when it updates the element:

Code:
local doNothing = function() end
power.SetMinMaxValues = doNothing
power.SetValue = doNothing
-----

I'd also recommend putting all of your config variables into a table, to avoid having tons and tons of local variables, and so that later you could move them to their own file, or add in-game options and save changed between sessions, if you wanted. This would also improve code readability, because when you saw a config variable in the code, instead of just "size" you'd see "config.size" and you'd immediately know that variable was set at the top of the file in the configuration section.

There's no need to explicitly check "if <boolean value> == true then" -- you can just do "if <boolean value> then". It's cleaner and (theoretically) more efficient.

Avoid calling functions unless you have to. For example, in once place you were doing "floor(x / 1000) * 1000" to "round" a value to the first 2 decimal points, but you were only using the value as a multiplier for other values, so there's no need to make it "pretty" -- just use it as-is.

Similarly, there's no need to declare a variable if you're only going to use the value once. Instead of "local x = y - 42" and then "SomeFunction(x)" just do "SomeFunction(y - 42)".

Finally, while a variable name like "sch" or "po" may be short and (depending on taste) pretty, are you really going to remember what it is if you haven't looked at your code for a week? a month? a year? When other programmers look at your code, will they be able to figure out what the variable is without having to go find where it's defined? Space is unlimited, so use descriptive variable names like "scrollChild" or "portraitHolder" are better choices.

Also, if you have two similar objects referred to as "hpbg" and "hpbgo" don't mix them together -- calling "hpbg:SetSize" followed by "hpbg:SetTexture" followed by "hpbgo:SetTexture" followed by "hbbg:SetVertedColor" is just confusing. Keep your code organized in logical blocks.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote