WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   trying to make an XP bar the way i want it (https://www.wowinterface.com/forums/showthread.php?t=49337)

Uitat 05-28-14 07:49 PM

trying to make an XP bar the way i want it
 
ok i am having issues herethe functions all work fine and all
but what i am trying to do here is
make the XP bar Primary
inside of a frame named XPFrame

now i have a mathmatic formula that sizes that frame so i include it here, yay the formula works, however my darn xp bar dont switch to a Rep bar at max level

also i want a little mouse interaction of on shift right click it switches to Rep bar, also i want to have it auto switch at max level

here is my code, please for god sake some one help me im bout to loose my mind, basically i have a working XP bar and a working Rep Bar
sorry for bombarding you all... maybe i need a more experienced coder to work with me on my project, yes no?

Code:


local fontsize = 12
localfont = "Fonts\\FRIZQT__.TTF"

local resolHeight = GetScreenHeight()
local resolWidth = GetScreenWidth()
local aspectRatio = (resolWidth/resolHeight)
--XP Bar
if UnitLevel("player") < MAX_PLAYER_LEVEL then   
    local derXpBarbg = CreateFrame("Frame", derXpBarbg, XPFrame)
    derXpBarbg:SetFrameLevel(0)
    derXpBarbg:SetFrameStrata("low")
    derXpBarbg:SetHeight(14)   
    derXpBarbg:SetPoint("bottom",0,8)
    derXpBarbg:SetBackdrop( {
      bgFile = "Interface\\Buttons\\WHITE8X8",
      edgeFile = "Interface\\Buttons\\WHITE8X8",
      tile = false, tileSize = 0, edgeSize = 1,
      insets = { left = (-1), right = (-1), top = (-1), bottom = -(1)}
    })
    derXpBarbg:SetBackdropColor(0.1,0.1,0.1)
    derXpBarbg:SetBackdropBorderColor(0.6,0.6,0.6)
    local derXpBar = CreateFrame("Frame", derXpBar, XPFrame)
    derXpBar:SetFrameLevel(1)
    derXpBar:SetFrameStrata("low")
    derXpBar:SetHeight(14)
    derXpBar:SetPoint("LEFT", derXpBarbg, "LEFT", 1, 0)
    derXpBar:SetBackdrop( {
      bgFile = "Interface\\Buttons\\WHITE8X8",
      tile = false, tileSize = 0, edgeSize = 1,
      insets = { left = (-1), right = (-1), top = (-1), bottom = -(1)}
    })
    derXpBar:RegisterEvent("PLAYER_XP_UPDATE");
    derXpBar:RegisterEvent("PLAYER_LEVEL_UP");
    derXpBar:RegisterEvent("UPDATE_EXHAUSTION");
    derXpBar:RegisterEvent("PLAYER_ENTERING_WORLD")
    local derXpBarRested = CreateFrame("Frame", derXpBar, XPFrame)
    derXpBarRested:SetFrameLevel(1)
    derXpBarRested:SetFrameStrata("low")
    derXpBarRested:SetHeight(14)
    derXpBarRested:SetPoint("LEFT", derXpBar, "RIGHT", 1, 0)
    derXpBarRested:SetBackdrop( {
      bgFile = "Interface\\Buttons\\WHITE8X8",
      tile = false, tileSize = 0, edgeSize = 1,
      insets = { left = (-1), right = (-1), top = (-1), bottom = -(1)}
    })
    derXpBarRested:SetBackdropColor(0.2,0.2,0.8,0.5) --blue Rested
    local newNameRegion = derXpBar:CreateFontString()
    newNameRegion:SetPoint("CENTER", derXpBarbg, "CENTER", 0, 0)
    newNameRegion:SetFont(localfont, fontsize, "OVERLAY")
    local function eventHandler(self, event, ...)
        local percent_full = UnitXP("player") / UnitXPMax("player");
        derXpBarbg:SetWidth(resolWidth*.34-15)--/2)
        if event == "PLAYER_LEVEL_UP" then
            derXpBar:SetWidth(resolWidth*.34-15)
            if UnitLevel("player") == MAX_PLAYER_LEVEL then
                derXpBarbg:SetAlpha(0)
                derXpBar:SetAlpha(0)
                derXpBarRested:SetAlpha(0)
                derXpBar:UnregisterEvent("PLAYER_XP_UPDATE");
                derXpBar:UnregisterEvent("PLAYER_LEVEL_UP");
                derXpBar:UnregisterEvent("UPDATE_EXHAUSTION");
                derXpBar:UnregisterEvent("PLAYER_ENTERING_WORLD")
            end
        else
            derXpBar:SetWidth(percent_full*(resolWidth*.34-15))--/2))
        end
        --newNameRegion:SetText(string.sub((percent_full*100),0,5).."%") --Show Percentage
        newNameRegion:SetText(UnitXP("player").." / "..UnitXPMax("player")) --Show Full Numbers
        if GetXPExhaustion() then
            derXpBar:SetBackdropColor(0.6,0,0.8,0.5)
            derXpBarRested:SetAlpha(1)
            if GetXPExhaustion()/UnitXPMax("player") > 0.99 then
                derXpBarRested:SetWidth((resolWidth*.34-15) - derXpBar:GetWidth())
            else
                derXpBarRested:SetWidth((GetXPExhaustion()/UnitXPMax("player"))*(resolWidth*.34-15))--/2))
            end
        else
            derXpBar:SetBackdropColor(0.6,0,0.8,0.5)
            derXpBarRested:SetAlpha(0)
        end
    end
    derXpBar:SetScript("OnEvent", eventHandler);
end
--Rep Bar

local myrepbarbg = CreateFrame("Frame", myrepbarbg, RepFrame)
myrepbarbg:SetFrameLevel(1)
myrepbarbg:SetFrameStrata("low")
myrepbarbg:SetHeight(16)
if UnitLevel("player") == MAX_PLAYER_LEVEL then   
    myrepbarbg:SetPoint("center",0,0)
else
    myrepbarbg:SetPoint("center",0,0)
end
if UnitLevel("player") == MAX_PLAYER_LEVEL then   

    myrepbarbg:SetBackdrop( {
      bgFile = "Interface\\Buttons\\WHITE8X8",
      edgeFile = "Interface\\Buttons\\WHITE8X8",
      tile = false, tileSize = 0, edgeSize = 1,
      insets = { left = (-1), right = (-1), top = (-1), bottom = -(1)}
})
myrepbarbg:SetBackdropColor(0.1,0.1,0.1)
myrepbarbg:SetBackdropBorderColor(0.6,0.6,0.6)
    myrepbarbg:SetPoint("center",0,20)
    end

local derRepBar = CreateFrame("Frame", derRepBar, RepFrame)
derRepBar:SetFrameLevel(1)
derRepBar:SetFrameStrata("low")
derRepBar:SetHeight(18)
derRepBar:SetPoint("LEFT", XPFrame, "LEFT", 10, 20)
derRepBar:SetBackdrop( {
  bgFile = "Interface\\Buttons\\WHITE8X8",
  tile = false, tileSize = 0, edgeSize = 1,
      insets = { left = (-1), right = (-1), top = (-1), bottom = -(1)}
})

derRepBar:RegisterEvent("UPDATE_FACTION");

local newNameRegion = derRepBar:CreateFontString()
newNameRegion:SetPoint("CENTER", myrepbarbg, "CENTER", 0, 0)
newNameRegion:SetFont(localfont, fontsize, "OVERLAY")

local function eventHandler(self, event, ...)
    local name, standing, min, max, value = GetWatchedFactionInfo()
    if name then
        local percent_full = (value-min) / (max-min);
        myrepbarbg:SetWidth(resolWidth*.34-15)
        derRepBar:SetWidth(percent_full*resolWidth*.34-19)   
        newNameRegion:SetText(name.." "..(value-min).." / "..(max-min).." "..(getglobal("FACTION_STANDING_LABEL"..standing)))
        derRepBar:SetBackdropColor(FACTION_BAR_COLORS[standing].r, FACTION_BAR_COLORS[standing].g, FACTION_BAR_COLORS[standing].b,0.8)
        myrepbarbg:Show()
        derRepBar:Show()
    else
        myrepbarbg:Hide()
        derRepBar:Hide()
    end
end
derRepBar:SetScript("OnEvent", eventHandler);


Phanx 05-28-14 09:40 PM

You can save yourself a lot of work by using StatusBar objects instead of manually calculating and setting the width of plain Frame objects. Your code was really messy and I didn't feel like going over it line by line, so I just adapted the code from my already-working XP/rep bar to behave the way you described (auto switch to rep at max level or on shift-right-click) and use the same flat texture as your code.

Code:

local XP_COLOR  = { r = 0.6, g = 0,  b = 0.5 }
local REST_COLOR = { r = 0.2, g = 0.2, b = 0.8 }

------------------------------------------------------------------------
-- Object creation

local f = CreateFrame("Frame", "derXPBar", UIParent)
f:SetPoint("BOTTOM", 0, 8)
f:SetHeight(14)
f:SetWidth(UIParent:GetWidth() * 0.75)

f:SetBackdrop({
        bgFile = "Interface\\BUTTONS\\WHITE8X8",
        edgeFile = "Interface\\BUTTONS\\WHITE8X8", edgeSize = 1,
        insets = { left = 1, right = 1, top = 1, bottom = 1 }
})
f:SetBackdropColor(0.1, 0.1, 0.1)
f:SetBackdropBorderColor(0.6, 0.6, 0.6)

local restBar = CreateFrame("StatusBar", nil, f)
restBar:SetPoint("BOTTOMLEFT", 1, 1)
restBar:SetPoint("TOPRIGHT", -1, -1)
restBar:SetStatusBarTexture("Interface\\BUTTONS\\WHITE8X8")
restBar:SetStatusBarColor(REST_COLOR.r, REST_COLOR.g, REST_COLOR.b, 0.5)

local bar = CreateFrame("StatusBar", nil, restBar)
bar:SetAllPoints(true)
bar:SetStatusBarTexture("Interface\\BUTTONS\\WHITE8X8")

local text = bar:CreateFontString(nil, "OVERLAY", "GameFontNormal")
text:SetPoint("CENTER")

------------------------------------------------------------------------
-- Display logic

local mode = "XP"

function f:Update(event, arg1)
        if event == "PET_BATTLE_OPENING_START" or C_PetBattles.IsInBattle() then
                return f:Hide()
        end

        local level = event == "PLAYER_LEVEL_UP" and arg1 or UnitLevel("player")
        if level == MAX_PLAYER_LEVEL or mode == "REP" then
                local name, standing, min, max, rep = GetWatchedFactionInfo()
                if name then
                        text:SetFormattedText("%s: %d / %d %s", name, max - rep, max - min, UnitSex("player") == 3 and _G["FACTION_STANDING_LABEL"..standing.."_FEMALE"] or  _G["FACTION_STANDING_LABEL"..standing])

                        bar:SetMinMaxValues(min, max)
                        bar:SetValue(cur)

                        local color = FACTION_BAR_COLORS[standing]
                        bar:SetStatusBarColor(color.r, color.g, color.b)

                        restBar:SetMinMaxValues(0, 1)
                        restBar:SetValue(0)

                        return
                end
        end

        local xp, max, rest = UnitXP("player"), UnitXPMax("player"), GetXPExhaustion()
        text:SetFormattedText("Level %d XP: %d / %d", level, xp, max)

        bar:SetMinMaxValues(0, max)
        bar:SetValue(xp)

        if rest then
                bar:SetStatusBarColor(REST_COLOR.r, REST_COLOR.g, REST_COLOR.b)
                restBar:SetMinMaxValues(0, max)
                restBar:SetValue( (xp + rest > max) and max or (xp + rest) )
        else
                bar:SetStatusBarColor(XP_COLOR.r, XP_COLOR.g, XP_COLOR.b)
                restBar:SetMinMaxValues(0, 1)
                restBar:SetValue(0)
        end
end

------------------------------------------------------------------------
-- Update on events

f:RegisterEvent("PET_BATTLE_OPENING_START")
f:RegisterEvent("PET_BATTLE_CLOSE")
f:RegisterEvent("PLAYER_ENTERING_WORLD")
f:RegisterEvent("PLAYER_LEVEL_UP")
f:RegisterEvent("PLAYER_XP_UPDATE")
f:RegisterEvent("UPDATE_EXHAUSTION")
f:RegisterEvent("UPDATE_FACTION")

f:SetScript("OnEvent", f.Update)
hooksecurefunc("SetWatchedFactionIndex", f.Update)

------------------------------------------------------------------------
-- Toggle mode on click

f:EnableMouse(true)
f:SetScript("OnMouseUp", function(self, button)
        if button == "RightButton" and IsShiftKeyDown() then
                mode = mode == "XP" and "REP" or "XP"
        end
        self:Update()
end)


Uitat 05-29-14 03:14 AM

Quote:

Originally Posted by Phanx (Post 293055)
You can save yourself a lot of work by using StatusBar objects instead of manually calculating and setting the width of plain Frame objects. Your code was really messy and I didn't feel like going over it line by line, so I just adapted the code from my already-working XP/rep bar to behave the way you described (auto switch to rep at max level or on shift-right-click) and use the same flat texture as your code.

Quote:

Originally Posted by Phanx (Post 293055)

Code:

local XP_COLOR  = { r = 0.6, g = 0,  b = 0.5 }
local REST_COLOR = { r = 0.2, g = 0.2, b = 0.8 }

------------------------------------------------------------------------
-- Object creation

local f = CreateFrame("Frame", "derXPBar", UIParent)
f:SetPoint("BOTTOM", 0, 8)
f:SetHeight(14)
f:SetWidth(UIParent:GetWidth() * 0.75)





Phannx, your a God

i only had to tweek that just a little bit... i.e.
Code:

local f = CreateFrame("Frame", "derXPBar", XPFrame)
f:SetPoint("CENTER", 0, 0)
f:SetHeight(14)
f:SetWidth(UIParent:GetWidth() * 0.33)



i was wondering though, how could i add in a texture like all those cool bars out there,

i dont really like the flat to much.. i like the aluminum texture a lot and when i use my other addon UI-tat, thats my primary texture

Librarys convolute things a lot because i dont understand their workings

10leej 05-29-14 04:12 AM

I just use a backdrop myself, my XP bar overall is probably my favorite part of my UI of all things too. Likely because it's one of the first parts I made.


Uitat 05-29-14 04:25 AM

error
 
im getting a consistant error right now, i see where its happening but cant understand why,
in the code you altered for me

here is the error as provided by bug sack/grabber
Code:

7x Deranjata-0.0.2.1a\Modules\XPbar.lua:198: Usage: <unnamed>:SetValue(value)
<in C code>
Deranjata-0.0.2.1a\Modules\XPbar.lua:198: in function "Update"
Deranjata-0.0.2.1a\Modules\XPbar.lua:249: in function <Deranjata\Modules\XPbar.lua:245>

Locals:
self = derXPBar {
0 = <userdata>
Update = <function> defined @Interface\AddOns\Deranjata\Modules\XPbar.lua:186
}
event = nil
arg1 = nil
level = 6
name = "Silvermoon City"
standing = 5
min = 3000
max = 9000
rep = 7102
f = derXPBar {
0 = <userdata>
Update = <function> defined @Interface\AddOns\Deranjata\Modules\XPbar.lua:186
}
mode = "REP"
text = <unnamed> {
0 = <userdata>
}
bar = <unnamed> {
0 = <userdata>
}
restBar = <unnamed> {
0 = <userdata>
}
REST_COLOR = <table> {
b = 0.8
g = 0.2
r = 0.2
}
XP_COLOR = <table> {
b = 0.5
g = 0
r = 0.6
}


10leej 05-29-14 05:36 AM

Can you post your current XPbar.lua? preferrably on a site like pastebin using the lua syntax highlighting.

Uitat 05-29-14 05:47 AM

Quote:

Originally Posted by 10leej (Post 293063)
Can you post your current XPbar.lua? preferrably on a site like

Quote:

Originally Posted by 10leej (Post 293063)
pastebin using the lua syntax highlighting.



http://pastebin.com/mR712PP6


there is a link to it with Lua Highlighting
for some reason i cant use the code wrap or link wraps or any of the nifty forum tools so youll hae to copy past to browser
without all my little notes at the top and just putting in just like i pastebinned, the error now reads

1x Deranjata-0.0.2.1a\Modules\XPbar.lua:51: Usage: <unnamed>:SetValue(value)
<in C code>
Deranjata-0.0.2.1a\Modules\XPbar.lua:51: in function "Update"
Deranjata-0.0.2.1a\Modules\XPbar.lua:102: in function <Deranjata\Modules\XPbar.lua:98>


Locals:
self = derXPBar {
0 = <userdata>
Update = <function> defined @Interface\AddOns\Deranjata\Modules\XPbar.lua:39
}
event = nil
arg1 = nil
level = 6
name = "Thunder Bluff"
standing = 4
min = 0
max = 300

Lombra 05-29-14 05:49 AM

Code:

bar:SetValue(cur)
should probably be
Code:

bar:SetValue(rep)

Phanx 05-29-14 05:50 AM

Please stop encasing every line of your post in a color tag... makes reading through code and quoting really annoying. :(

Quote:

Originally Posted by Uitat (Post 293060)
i was wondering though, how could i add in a texture like all those cool bars out there, i dont really like the flat to much.. i like the aluminum texture a lot and when i use my other addon UI-tat, thats my primary texture

Just change the texture path to point to the file you want to use. For example, instead of:

Code:

restBar:SetStatusBarTexture("Interface\\BUTTONS\\WHITE8X8")
do:

Code:

restBar:SetStatusBarTexture("Interface\\AddOns\\MyXPAddon\\MyTextureFile")
Leave off the .tga or .blp extension.

Quote:

Originally Posted by Uitat (Post 293062)
im getting a consistant error right now, i see where its happening but cant understand why, in the code you altered for me

Code:

7x Deranjata-0.0.2.1a\Modules\XPbar.lua:198: Usage: <unnamed>:SetValue(value)
<in C code>
Deranjata-0.0.2.1a\Modules\XPbar.lua:198: in function "Update"
Deranjata-0.0.2.1a\Modules\XPbar.lua:249: in function <Deranjata\Modules\XPbar.lua:245>


Without seeing your current code, I can't say for sure, but it sounds like you changed a variable name and forgot to change all instances of it, or you moved chunks of code around so variables aren't in scope where they should be.

Uitat 05-29-14 06:20 AM

Quote:

Originally Posted by Phanx (Post 293066)
Please stop encasing every line of your post in a color tag... makes reading through code and quoting really annoying. :(

i cant access any color tags, when i do code i litteraly must manualy do the brakets then code then closed brakets


other then the quotes and code tags i dont use anything, hmmm maybe my browser is out of date

10leej 05-29-14 06:32 AM

Ah I was thinking there was more to it because of the "XPbar.lua:198" showing an error at line 198, anyways what lombra says should fix it, I don't get any errors with it.

Uitat 05-29-14 06:40 AM

Quote:

Originally Posted by 10leej (Post 293069)
Ah I was thinking there was more to it because of the "XPbar.lua:198" showing an error at line 198, anyways what lombra says should fix it, I don't get any errors with it.

maybe its because of my shift click thingie that cur has to change to rep, who knows, after the "very" small change, works perfect,

also thanks for the texture tip, worked like a dream,

next up i think i will do buff bars, little face smashing there but i think my keyboard can take the abuse, it has up until now

then i think ill be ready to update my addon to 0.0.3.0a hehe

10leej 05-29-14 06:58 AM

Quote:

Originally Posted by Uitat (Post 293070)
maybe its because of my shift click thingie that cur has to change to rep, who knows, after the "very" small change, works perfect,

also thanks for the texture tip, worked like a dream,

next up i think i will do buff bars, little face smashing there but i think my keyboard can take the abuse, it has up until now

then i think ill be ready to update my addon to 0.0.3.0a hehe

I cheat those and put a texture on top of the buffs and thats how I get my buffs to look the way they are

Uitat 05-29-14 07:05 AM

yeah im going to probly go for more of the status bar style like the old elkanos buff bars if anyone remembers that

Uitat 05-29-14 07:40 AM

to Leej and Sphanx and Lombra
 
i would like to thank you in t the credits of my addon, i know many are finicky of what addons their names are associated with, could i include my thanks to you all, it seems your names keep coming up on theRoster of most help to me

10leej 05-29-14 02:17 PM

Quote:

Originally Posted by Uitat (Post 293074)
i would like to thank you in t the credits of my addon, i know many are finicky of what addons their names are associated with, could i include my thanks to you all, it seems your names keep coming up on theRoster of most help to me

Phanx did most of the work, I only offerred a small suggestion.

Uitat 05-29-14 02:20 PM

Quote:

Originally Posted by 10leej (Post 293082)
Phanx did most of the work, I only offerred a small suggestion.

small when my knowlege is small = big

local big = small x small


All times are GMT -6. The time now is 10:24 AM.

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