Thread Tools Display Modes
10-20-15, 11:14 AM   #1
saxitoxin
A Theradrim Guardian
 
saxitoxin's Avatar
AddOn Author - Click to view addons
Join Date: May 2009
Posts: 60
Need help with a addons I try to make

I try to make this bottombar for a person on Reddit

the main addon is a infobar but I have trouble with the proffession bar.
I try to fade the icons out if the daily cooldown has been used

I have created a code but it does not work properly, I have to hover the frame before the icon fades, and the tooltip is acting wierd
Lua Code:
  1. local addon, ns = ...
  2. local cfg = ns.cfg
  3. local unpack = unpack
  4. --------------------------------------------------------------
  5. if not cfg.show.tradeSkill then return end
  6.  
  7. local proffessions = {
  8.     ['ALCHEMY'] = {"Alchemical Catalyst", "Secrets of Draenor Alchemy", "Northrend Alchemy Research"},
  9.     ['BLACKSMITHING'] = {"Truesteel Ignot", "Secrets of Draenor Blacksmithing"},
  10.     ['ENCHANTING'] = {"Temporal Crystal", "Secrets of Draenor Enchanting"},
  11.     ['ENGINEERING'] = {"Gearsoring Parts", "Secrets of Draenor Engineering"},
  12.     ['INSCRIPTION'] = {"War Paints", "Secrets of Draenor Inscription","Draenor Merchant Order"},
  13.     ['JEWEL CRAFTING'] = {"Taladite Crystal", "Secrets of Draenor Jewelcrafting"},
  14.     ['LEATHERWORKING'] = {"Burnished Leather", "Secrets of Draenor Leatherworking"},
  15.     ['TAILORING'] = {"Hexweave Cloth", "Secrets of Draenor Tailoring"},
  16. }
  17.  
  18. local prof1OnCooldown = false
  19. local prof2OnCooldown = false
  20.  
  21. local tradeSkillFrame = CreateFrame("Frame",nil, cfg.SXUIframe)
  22. tradeSkillFrame:SetPoint("LEFT", cfg.SXUIframe, "CENTER", 120,0)
  23. tradeSkillFrame:SetSize(16, 16)
  24. ---------------------------------------------------------------------
  25. local primaryTradeSkillFrame = CreateFrame("BUTTON",nil, tradeSkillFrame)
  26. primaryTradeSkillFrame:SetSize(16, 16)
  27. primaryTradeSkillFrame:SetPoint("LEFT")
  28. primaryTradeSkillFrame:EnableMouse(true)
  29. primaryTradeSkillFrame:RegisterForClicks("AnyUp")
  30.  
  31. local primaryTradeSkillIcon = primaryTradeSkillFrame:CreateTexture(nil,"OVERLAY",nil,7)
  32. primaryTradeSkillIcon:SetSize(16, 16)
  33. primaryTradeSkillIcon:SetPoint("LEFT")
  34. primaryTradeSkillIcon:SetVertexColor(unpack(cfg.color.normal))
  35.  
  36. local primaryTradeSkillText = primaryTradeSkillFrame:CreateFontString(nil, "OVERLAY")
  37. primaryTradeSkillText:SetFont(cfg.font, cfg.core.normalFontSize)
  38. primaryTradeSkillText:SetPoint("RIGHT",primaryTradeSkillFrame,2,0 )
  39. primaryTradeSkillText:SetTextColor(unpack(cfg.color.normal))
  40.  
  41. local primaryTradeSkillStatusbar = CreateFrame("StatusBar", nil, primaryTradeSkillFrame)
  42. primaryTradeSkillStatusbar:SetStatusBarTexture(1,1,1)
  43. primaryTradeSkillStatusbar:SetStatusBarColor(unpack(cfg.color.normal))
  44. primaryTradeSkillStatusbar:SetPoint("TOPLEFT", primaryTradeSkillText, "BOTTOMLEFT",0,-2)
  45.  
  46. local primaryTradeSkillStatusbarBG = primaryTradeSkillStatusbar:CreateTexture(nil,"BACKGROUND",nil,7)
  47. primaryTradeSkillStatusbarBG:SetPoint("TOPLEFT", primaryTradeSkillText, "BOTTOMLEFT",0,-2)
  48. primaryTradeSkillStatusbarBG:SetTexture(unpack(cfg.color.inactive))
  49.  
  50. primaryTradeSkillFrame:SetScript("OnEnter", function()
  51.     if InCombatLockdown() then return end
  52.     GameTooltip:SetOwner(tradeSkillFrame, cfg.tooltipPos)
  53.     for i=1,GetNumTradeSkills() do
  54.         local cooldown = GetTradeSkillCooldown(i)
  55.         if cooldown then
  56.             local name = GetTradeSkillInfo(i)
  57.             GameTooltip:AddDoubleLine(name, SecondsToTime(cooldown), 1, 1, 0, 1, 1, 1)         
  58.         end
  59.     end
  60.    
  61.     primaryTradeSkillIcon:SetVertexColor(unpack(cfg.color.hover))
  62.     primaryTradeSkillStatusbar:SetStatusBarColor(unpack(cfg.color.hover))
  63.     GameTooltip:Show()
  64. end)
  65.  
  66. primaryTradeSkillFrame:SetScript("OnLeave", function()
  67.     if prof1OnCooldown then
  68.         primaryTradeSkillIcon:SetVertexColor(unpack(cfg.color.inactive))
  69.         primaryTradeSkillText:SetTextColor(unpack(cfg.color.inactive))
  70.     else
  71.         primaryTradeSkillIcon:SetVertexColor(unpack(cfg.color.normal))
  72.         primaryTradeSkillText:SetTextColor(unpack(cfg.color.normal))
  73.     end
  74.     primaryTradeSkillStatusbar:SetStatusBarColor(unpack(cfg.color.normal))
  75.     if ( GameTooltip:IsShown() ) then GameTooltip:Hide() end
  76. end)
  77.  
  78. primaryTradeSkillFrame:SetScript("OnClick", function(self, button, down)
  79.     if InCombatLockdown() then return end
  80.     if button == "LeftButton" then
  81.         local prof1, prof2 = GetProfessions()
  82.         if prof1 then
  83.             if (GetProfessionInfo(prof1) == ('Herbalism')) then
  84.                 ToggleSpellBook(BOOKTYPE_PROFESSION)   
  85.             elseif(GetProfessionInfo(prof1) == ('Skinning')) then
  86.                 ToggleSpellBook(BOOKTYPE_PROFESSION)   
  87.             elseif(GetProfessionInfo(prof1) == ('Mining')) then
  88.                 CastSpellByName("Smelting")
  89.             else   
  90.                 CastSpellByName((GetProfessionInfo(prof1)))
  91.             end
  92.         end
  93.     elseif button == "RightButton" then
  94.         ToggleSpellBook(BOOKTYPE_PROFESSION)
  95.     end
  96. end)
  97. ---------------------------------------------------------------------
  98. local secondaryTradeSkillFrame = CreateFrame("BUTTON",nil, tradeSkillFrame)
  99. secondaryTradeSkillFrame:SetPoint("RIGHT")
  100. secondaryTradeSkillFrame:SetSize(16, 16)
  101. secondaryTradeSkillFrame:EnableMouse(true)
  102. secondaryTradeSkillFrame:RegisterForClicks("AnyUp")
  103.  
  104. local secondaryTradeSkillIcon = secondaryTradeSkillFrame:CreateTexture(nil,"OVERLAY",nil,7)
  105. secondaryTradeSkillIcon:SetSize(16, 16)
  106. secondaryTradeSkillIcon:SetPoint("LEFT")
  107. secondaryTradeSkillIcon:SetVertexColor(unpack(cfg.color.normal))
  108.  
  109. local secondaryTradeSkillText = secondaryTradeSkillFrame:CreateFontString(nil, "OVERLAY")
  110. secondaryTradeSkillText:SetFont(cfg.font, cfg.core.normalFontSize)
  111. secondaryTradeSkillText:SetPoint("LEFT", secondaryTradeSkillIcon,"RIGHT",2,0)
  112. secondaryTradeSkillText:SetTextColor(unpack(cfg.color.normal))
  113.  
  114. local secondaryTradeSkillStatusbar = CreateFrame("StatusBar", nil, secondaryTradeSkillFrame)
  115. secondaryTradeSkillStatusbar:SetStatusBarTexture(1,1,1)
  116. secondaryTradeSkillStatusbar:SetStatusBarColor(unpack(cfg.color.normal))
  117. secondaryTradeSkillStatusbar:SetPoint("TOPLEFT", secondaryTradeSkillText, "BOTTOMLEFT",0,-2)
  118.  
  119. local secondaryTradeSkillStatusbarBG = secondaryTradeSkillStatusbar:CreateTexture(nil,"BACKGROUND",nil,7)
  120. secondaryTradeSkillStatusbarBG:SetPoint("TOPLEFT", secondaryTradeSkillText, "BOTTOMLEFT",0,-2)
  121. secondaryTradeSkillStatusbarBG:SetTexture(unpack(cfg.color.inactive))
  122.  
  123. secondaryTradeSkillFrame:SetScript("OnEnter", function()
  124.     if InCombatLockdown() then return end
  125.     GameTooltip:SetOwner(tradeSkillFrame, cfg.tooltipPos)
  126.     for i=1,GetNumTradeSkills() do
  127.         local cooldown = GetTradeSkillCooldown(i)
  128.         if cooldown then
  129.             local name = GetTradeSkillInfo(i)
  130.             GameTooltip:AddDoubleLine(name, SecondsToTime(cooldown), 1, 1, 0, 1, 1, 1)         
  131.         end
  132.     end
  133.     secondaryTradeSkillIcon:SetVertexColor(unpack(cfg.color.hover))
  134.     secondaryTradeSkillStatusbar:SetStatusBarColor(unpack(cfg.color.hover))
  135.     GameTooltip:Show()
  136. end)
  137.  
  138. secondaryTradeSkillFrame:SetScript("OnLeave", function()
  139.     if prof2OnCooldown then
  140.         secondaryTradeSkillIcon:SetVertexColor(unpack(cfg.color.inactive))
  141.         secondaryTradeSkillText:SetTextColor(unpack(cfg.color.inactive))
  142.     else
  143.         secondaryTradeSkillIcon:SetVertexColor(unpack(cfg.color.normal))
  144.         secondaryTradeSkillText:SetTextColor(unpack(cfg.color.normal))
  145.     end
  146.     secondaryTradeSkillStatusbar:SetStatusBarColor(unpack(cfg.color.normal))
  147.     if ( GameTooltip:IsShown() ) then GameTooltip:Hide() end
  148. end)
  149.  
  150. secondaryTradeSkillFrame:SetScript("OnClick", function(self, button, down)
  151.     if InCombatLockdown() then return end
  152.     if button == "LeftButton" then
  153.         local prof1, prof2 = GetProfessions()
  154.         if prof2 then
  155.             if (GetProfessionInfo(prof2) == ('Herbalism')) then
  156.                 ToggleSpellBook(BOOKTYPE_PROFESSION)   
  157.             elseif(GetProfessionInfo(prof2) == ('Skinning')) then
  158.                 ToggleSpellBook(BOOKTYPE_PROFESSION)   
  159.             elseif(GetProfessionInfo(prof2) == ('Mining')) then
  160.                 CastSpellByName("Smelting")
  161.             else   
  162.                 CastSpellByName((GetProfessionInfo(prof2)))
  163.             end
  164.         end
  165.     elseif button == "RightButton" then
  166.         ToggleSpellBook(BOOKTYPE_PROFESSION)
  167.     end
  168. end)
  169. ---------------------------------------------------------------------
  170.  
  171. local eventframe = CreateFrame("Frame")
  172. eventframe:RegisterEvent("PLAYER_ENTERING_WORLD")
  173. eventframe:RegisterEvent("TRADE_SKILL_UPDATE")
  174. eventframe:RegisterEvent("TRAINER_CLOSED")
  175. eventframe:RegisterEvent("SPELLS_CHANGED")
  176. eventframe:RegisterUnitEvent("UNIT_SPELLCAST_STOP", "player")
  177.  
  178. eventframe:SetScript("OnEvent", function(self,event, ...)
  179.     local prof1, prof2 = GetProfessions()
  180.     if prof1 then
  181.         local prof1Name, _, prof1Rank, prof1MaxRank = GetProfessionInfo(prof1)
  182.         prof1Name = string.upper(prof1Name)
  183.         primaryTradeSkillText:SetText(prof1Name)
  184.         primaryTradeSkillIcon:SetTexture(cfg.mediaFolder.."profession\\"..prof1Name)
  185.         if prof1Rank == prof1MaxRank then
  186.             primaryTradeSkillStatusbar:Hide()
  187.         else
  188.             primaryTradeSkillStatusbar:SetMinMaxValues(0, prof1MaxRank)
  189.             primaryTradeSkillStatusbar:SetValue(prof1Rank)
  190.         end
  191.         primaryTradeSkillFrame:SetSize(primaryTradeSkillText:GetStringWidth()+18, 16)
  192.         primaryTradeSkillStatusbar:SetSize(primaryTradeSkillText:GetStringWidth(),3)
  193.         primaryTradeSkillStatusbarBG:SetSize(primaryTradeSkillText:GetStringWidth(),3)
  194.         primaryTradeSkillFrame:Show()
  195.         primaryTradeSkillFrame:EnableMouse(true)
  196.        
  197.         for i=1,GetNumTradeSkills() do
  198.             local cooldown = GetTradeSkillCooldown(i)
  199.             if cooldown then
  200.                 local name = GetTradeSkillInfo(i)
  201.                 for k, v in pairs(proffessions) do
  202.                     for u = 1, #v do
  203.                         if k == prof1Name then
  204.                             if v[u] == name then
  205.                                 if not prof1OnCooldown then prof1OnCooldown = true end
  206.                                 primaryTradeSkillIcon:SetVertexColor(unpack(cfg.color.inactive))
  207.                                 primaryTradeSkillText:SetTextColor(unpack(cfg.color.inactive))
  208.                             end
  209.                         else
  210.                             primaryTradeSkillIcon:SetVertexColor(unpack(cfg.color.normal))
  211.                             primaryTradeSkillText:SetTextColor(unpack(cfg.color.normal))
  212.                         end
  213.                     end
  214.                 end
  215.             end
  216.         end
  217.     else
  218.         primaryTradeSkillFrame:Hide()
  219.         primaryTradeSkillFrame:EnableMouse(false)
  220.     end
  221.    
  222.     if prof2 then
  223.         local prof2Name, _, rank, maxRank = GetProfessionInfo(prof2)
  224.         prof2Name = string.upper(prof2Name)
  225.         secondaryTradeSkillText:SetText(prof2Name)
  226.         secondaryTradeSkillIcon:SetTexture(cfg.mediaFolder.."profession\\"..prof2Name)
  227.         if rank == maxRank then
  228.             secondaryTradeSkillStatusbar:Hide()
  229.         end
  230.         secondaryTradeSkillStatusbar:SetMinMaxValues(0, maxRank)
  231.         secondaryTradeSkillStatusbar:SetValue(rank)
  232.         secondaryTradeSkillFrame:SetSize(secondaryTradeSkillText:GetStringWidth()+18, 16)
  233.         secondaryTradeSkillStatusbar:SetSize(secondaryTradeSkillText:GetStringWidth(),3)
  234.         secondaryTradeSkillStatusbarBG:SetSize(secondaryTradeSkillText:GetStringWidth(),3)
  235.         secondaryTradeSkillFrame:Show()
  236.         secondaryTradeSkillFrame:EnableMouse(true)
  237.        
  238.         for i=1,GetNumTradeSkills() do
  239.             local cooldown = GetTradeSkillCooldown(i)
  240.             if cooldown then
  241.                 local name = GetTradeSkillInfo(i)
  242.                 for k, v in pairs(proffessions) do
  243.                     for u = 1, #v do
  244.                         if k == prof2Name then
  245.                             if v[u] == name then
  246.                                 if not prof2OnCooldown then prof2OnCooldown = true end
  247.                                 secondaryTradeSkillIcon:SetVertexColor(unpack(cfg.color.inactive))
  248.                                 secondaryTradeSkillText:SetTextColor(unpack(cfg.color.inactive))
  249.                             else
  250.                                 secondaryTradeSkillIcon:SetVertexColor(unpack(cfg.color.normal))
  251.                                 secondaryTradeSkillText:SetTextColor(unpack(cfg.color.normal))
  252.                             end
  253.                         end
  254.                     end
  255.                 end
  256.             end
  257.         end
  258.     end
  259.     tradeSkillFrame:SetSize((primaryTradeSkillFrame:GetWidth())+(secondaryTradeSkillFrame:GetWidth()+4), 16)
  260. end)

EDIT: Got the proffession frame to work, just needed to rearrange some things.
Lua Code:
  1. for i=1,GetNumTradeSkills() do
  2.             local cooldown = GetTradeSkillCooldown(i)
  3.             if cooldown then
  4.                 local name = GetTradeSkillInfo(i)
  5.                 for k, v in pairs(proffessions) do
  6.                     for u = 1, #v do
  7.                         if k == prof2Name then
  8.                             if v[u] == name then
  9.                                 if not prof2OnCooldown then prof2OnCooldown = true end
  10.                                 secondaryTradeSkillIcon:SetVertexColor(unpack(cfg.color.inactive))
  11.                                 secondaryTradeSkillText:SetTextColor(unpack(cfg.color.inactive))
  12.                                 if not prof2OnCooldown then
  13.                                     secondaryTradeSkillIcon:SetVertexColor(unpack(cfg.color.normal))
  14.                                     secondaryTradeSkillText:SetTextColor(unpack(cfg.color.normal))
  15.                                 end
  16.                             end
  17.                         end
  18.                     end
  19.                 end
  20.                
  21.             end
  22.         end



Also I would like to add to the gold frame that the hover shows gold earned this session, today and weekly, also shows gold across all characters on server with total at the bottom like the post say, but I have NO clue on how to make saved variables for that.
The code I use now

Lua Code:
  1. local addon, ns = ...
  2. local cfg = ns.cfg
  3. local unpack = unpack
  4. --------------------------------------------------------------
  5. if not cfg.show.gold then return end
  6.  
  7. local goldFrame = CreateFrame("BUTTON",nil, cfg.SXUIframe)
  8. goldFrame:SetPoint("RIGHT",-210,0)
  9. goldFrame:SetSize(16, 16)
  10. goldFrame:EnableMouse(true)
  11. goldFrame:RegisterForClicks("AnyUp")
  12.  
  13. local goldIcon = goldFrame:CreateTexture(nil,"OVERLAY",nil,7)
  14. goldIcon:SetPoint("LEFT")
  15. goldIcon:SetTexture(cfg.mediaFolder.."datatexts\\gold")
  16. goldIcon:SetVertexColor(unpack(cfg.color.normal))
  17.  
  18. local goldText = goldFrame:CreateFontString(nil, "OVERLAY")
  19. goldText:SetFont(cfg.font, cfg.core.normalFontSize)
  20. goldText:SetPoint("RIGHT", goldFrame,2,0)
  21. goldText:SetTextColor(unpack(cfg.color.normal))
  22.  
  23. goldFrame:SetScript("OnEnter", function()
  24.     if InCombatLockdown() then return end
  25.     goldIcon:SetVertexColor(unpack(cfg.color.hover))
  26.     GameTooltip:SetOwner(goldFrame, cfg.tooltipPos)
  27.     GameTooltip:AddLine("[|cff6699FFGold|r]")
  28.     GameTooltip:AddLine(" ")
  29.     ---------------------------------------------------
  30.     local money = GetMoney()
  31.     local moneyIcon = GetCoinIcon(money)
  32.     local g, s, c = abs(money/10000), abs(mod(money/100, 100)), abs(mod(money, 100))
  33.     if ( g < 1 ) then g = "" else g = string.format("|cffffffff%d|cffffd700g|r ", g) end
  34.     if ( s < 1 ) then s = "" else s = string.format("|cffffffff%d|cffc7c7cfs|r ", s) end
  35.     c = string.format("|cffffffff%d|cffeda55fc|r ", c)
  36.    
  37.     GameTooltip:AddDoubleLine("Current Gold",("%s%s%s"):format(g,s,c))
  38.    
  39.     GameTooltip:Show()
  40. end)
  41.  
  42. goldFrame:SetScript("OnLeave", function() if ( GameTooltip:IsShown() ) then GameTooltip:Hide() end goldIcon:SetVertexColor(unpack(cfg.color.normal)) end)
  43.  
  44. goldFrame:SetScript("OnClick", function(self, button, down)
  45.     if InCombatLockdown() then return end
  46.     if button == "LeftButton" then
  47.         OpenAllBags()
  48.     elseif button == "RightButton" then
  49.         CloseAllBags()
  50.     end
  51. end)
  52.  
  53. local eventframe = CreateFrame("Frame")
  54. eventframe:RegisterEvent("PLAYER_LOGIN")
  55. eventframe:RegisterEvent("PLAYER_ENTERING_WORLD")
  56. eventframe:RegisterEvent("PLAYER_MONEY")
  57. eventframe:RegisterEvent("SEND_MAIL_MONEY_CHANGED")
  58. eventframe:RegisterEvent("SEND_MAIL_COD_CHANGED")
  59. eventframe:RegisterEvent("PLAYER_TRADE_MONEY")
  60. eventframe:RegisterEvent("TRADE_MONEY_CHANGED")
  61. eventframe:RegisterEvent("TRADE_LOGOUT")
  62.  
  63. eventframe:SetScript("OnEvent", function(self,event, ...)
  64. if event == ("PLAYER_LOGIN") then
  65. local sessionGold = 0
  66. end
  67.  
  68.     local money = GetMoney()
  69.        
  70.     local g, s, c = abs(money/10000), abs(mod(money/100, 100)), abs(mod(money, 100))
  71.  
  72.     if g > 1 then
  73.         goldText:SetText(floor(g).."g")
  74.     elseif s > 1 then
  75.         goldText:SetText(floor(s).."s")
  76.     else
  77.         goldText:SetText(floor(c).."c")
  78.     end
  79.     goldFrame:SetSize(goldText:GetStringWidth()+18, 16)
  80. end)

Last edited by saxitoxin : 10-20-15 at 01:33 PM.
  Reply With Quote
10-20-15, 01:47 PM   #2
JDoubleU00
A Firelord
 
JDoubleU00's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2008
Posts: 463
Sorry, I cannot help you, but the bottom bar looks very nice. What font are you using?
__________________
Author of JWExpBar and JWRepBar.
  Reply With Quote
10-20-15, 02:20 PM   #3
lightspark
A Rage Talon Dragon Guard
 
lightspark's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2012
Posts: 341
Originally Posted by rocnroll View Post
Sorry, I cannot help you, but the bottom bar looks very nice. What font are you using?
It's written on reddit
Originally Posted by reddit
Font used is Homizio (bold) size 11 with all letters in caps
__________________
  Reply With Quote
10-20-15, 03:46 PM   #4
lightspark
A Rage Talon Dragon Guard
 
lightspark's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2012
Posts: 341
Yo!

Please read Phanx' post about saved variables here. You can also find some useful info on SVs on wowpedia, that article explains quite well what's going on there.

Then you can check this and this code by Phanx, those are two nice examples of how to read data from and write data into SV table.

It's 5am at my place, so I'm helluva tired, sorry, but can't help you now. If no one helps you by the time I wake up tomorrow, I'll try to explain things and provide some examples.

Good night.
__________________

Last edited by lightspark : 10-21-15 at 05:30 AM.
  Reply With Quote
10-21-15, 04:31 AM   #5
saxitoxin
A Theradrim Guardian
 
saxitoxin's Avatar
AddOn Author - Click to view addons
Join Date: May 2009
Posts: 60
thank you Lightspark :-)

I will check out and try to edit phanx code when I get home from work
  Reply With Quote
10-21-15, 06:04 AM   #6
karmamuscle
A Cobalt Mageweaver
 
karmamuscle's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2008
Posts: 205
That looks really cool.
__________________
55 89 144 233 377 610 987 1597 2584 4181 6765
  Reply With Quote
10-21-15, 08:49 AM   #7
lightspark
A Rage Talon Dragon Guard
 
lightspark's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2012
Posts: 341
Yo! That's me again... Here's a small addon I wrote to demonstrate how saved variables work. It stores info about character's gold on first day of the week, on first login this day and when session starts. Session begins, when player either logs in or reloads UI.

This data can be used for gold gain calculations.

test.toc:
Lua Code:
  1. ## Interface: 60200
  2. ## Author: lightspark
  3. ## Version: 0.1
  4. ## Title: test
  5. ## SavedVariables: TEST_CONFIG
  6.  
  7. test.lua

test.lua:
Lua Code:
  1. local _, ns = ... -- here we get addon table
  2. -------------
  3. -- HELPERS --
  4. -------------
  5.  
  6. -- copies missing fields from source table
  7. function CopyTable(src, dest)
  8.     if type(dest) ~= "table" then
  9.         dest = {}
  10.     end
  11.  
  12.     for k, v in pairs(src) do
  13.         if type(v) == "table" then
  14.             dest[k] = CopyTable(v, dest[k])
  15.         elseif type(v) ~= type(dest[k]) then
  16.             dest[k] = v
  17.         end
  18.     end
  19.  
  20.     return dest
  21. end
  22.  
  23. -- removes everything that is present in source table from another table
  24. function DiffTable(src, dest)
  25.     if type(dest) ~= "table" then
  26.         return {}
  27.     end
  28.  
  29.     if type(src) ~= "table" then
  30.         return dest
  31.     end
  32.  
  33.     for k, v in pairs(dest) do
  34.         if type(v) == "table" then
  35.             if not next(DiffTable(src[k], v)) then
  36.                 dest[k] = nil
  37.             end
  38.         elseif v == src[k] then
  39.             dest[k] = nil
  40.         end
  41.     end
  42.  
  43.     return dest
  44. end
  45.  
  46. local function ConvertDateToNumber(month, day, year)
  47.     month = gsub(month, "(%d)(%d?)", function(d1, d2) return d2 == "" and "0"..d1 or d1..d2 end) -- converts M to MM
  48.     day = gsub(day, "(%d)(%d?)", function(d1, d2) return d2 == "" and "0"..d1 or d1..d2 end) -- converts D to DD
  49.  
  50.     return tonumber(year..month..day)
  51. end
  52.  
  53. --------------
  54. -- DEFAULTS --
  55. --------------
  56.  
  57. local D = {
  58.     ["money_related_stuff"] = {}
  59. }
  60.  
  61. -----------
  62. -- STUFF --
  63. -----------
  64.  
  65. local function Controller_OnEvent(self, event, arg)
  66.     if event == "ADDON_LOADED" and arg == "test" then -- "test" is addon name
  67.         local CONFIG = CopyTable(D, TEST_CONFIG)
  68.         ns.CONFIG = CONFIG -- makes this table available throughout addon
  69.  
  70.         local playerName, playerRealm = UnitName("player"), GetRealmName()
  71.  
  72.         if not CONFIG["money_related_stuff"][playerRealm] then
  73.             CONFIG["money_related_stuff"][playerRealm] = {} -- creates a table, if it doesn't exist
  74.         end
  75.  
  76.         local realmData = CONFIG["money_related_stuff"][playerRealm] -- just an alias
  77.         ns.realmData = realmData
  78.  
  79.         if not realmData[playerName] then
  80.             realmData[playerName] = {} -- creates a table, if it doesn't exist
  81.         end
  82.  
  83.         ns.playerData = realmData[playerName]
  84.  
  85.         self:UnregisterEvent("ADDON_LOADED")
  86.     elseif event == "PLAYER_LOGIN" then
  87.         local playerData = ns.playerData
  88.  
  89.         local weekday, month, day, year = CalendarGetDate()
  90.         local today = ConvertDateToNumber(month, day, year)
  91.         local updateData
  92.  
  93.         if playerData.lastLoginDate then
  94.             if playerData.lastLoginDate < today then -- is true, if last time player logged in was the day before or even earlier
  95.                 playerData.lastLoginDate = today
  96.                 updateData = true
  97.             end
  98.         else
  99.             playerData.lastLoginDate = today
  100.             updateData = true
  101.         end
  102.  
  103.         if updateData then -- daily updates
  104.             if playerData["money_on_first_weekday"] then
  105.                 if weekday == 1 then -- 1 is Sunday, 2 is Monday, different countries have different first day of the week
  106.                     playerData["money_on_first_weekday"] = GetMoney()
  107.                 end
  108.             else
  109.                 playerData["money_on_first_weekday"] = GetMoney()
  110.             end
  111.  
  112.             playerData["money_on_first_login_today"] = GetMoney()
  113.         end
  114.  
  115.         playerData["money_on_session_start"] = GetMoney() -- this one resets on every single login or UI reload
  116.  
  117.         self:UnregisterEvent("PLAYER_LOGIN")
  118.     elseif event == "PLAYER_LOGOUT" then
  119.         TEST_CONFIG = DiffTable(D, ns.CONFIG) -- writes data into TEST_CONFIG table
  120.     end
  121. end
  122.  
  123. local Controller = CreateFrame("Frame")
  124. Controller:RegisterEvent("ADDON_LOADED")
  125. Controller:RegisterEvent("PLAYER_LOGIN")
  126. Controller:RegisterEvent("PLAYER_LOGOUT")
  127. Controller:SetScript("OnEvent", Controller_OnEvent)

and here are saved variables I got after logging in on few characters on different PTRs:
Lua Code:
  1. TEST_CONFIG = {
  2.     ["money_related_stuff"] = {
  3.         ["Brill (EU)"] = {
  4.             ["Galdricus"] = {
  5.                 ["lastLoginDate"] = 20151021,
  6.                 ["money_on_session_start"] = 371265253,
  7.                 ["money_on_first_weekday"] = 371265253,
  8.                 ["money_on_first_login_today"] = 371265253,
  9.             },
  10.             ["Livarra"] = {
  11.                 ["money_on_session_start"] = 496438400,
  12.                 ["money_on_first_login_today"] = 496438400,
  13.                 ["money_on_first_weekday"] = 496438400,
  14.                 ["lastLoginDate"] = 20151021,
  15.             },
  16.             ["Bjordius"] = {
  17.                 ["money_on_first_login_today"] = 500745400,
  18.                 ["money_on_session_start"] = 501745400,
  19.                 ["lastLoginDate"] = 20151021,
  20.                 ["money_on_first_weekday"] = 500745400,
  21.             },
  22.         },
  23.         ["Nobundo (KR)"] = {
  24.             ["Phia"] = {
  25.                 ["money_on_session_start"] = 188362133,
  26.                 ["money_on_first_login_today"] = 188362133,
  27.                 ["money_on_first_weekday"] = 188362133,
  28.                 ["lastLoginDate"] = 20151021,
  29.             },
  30.         },
  31.     },
  32. }

I hope table names are self-explanatory.
__________________

Last edited by lightspark : 10-21-15 at 11:03 AM. Reason: updated few comments
  Reply With Quote
10-21-15, 11:39 AM   #8
saxitoxin
A Theradrim Guardian
 
saxitoxin's Avatar
AddOn Author - Click to view addons
Join Date: May 2009
Posts: 60
Thanks for the example Lightspark, it will be easyer for me to both read and learn this now

only thing is that I get a error
Code:
11x SXUI_bottombar\modules\gold.lua:98: attempt to index local 'playerData' (a nil value)
SXUI_bottombar\modules\gold.lua:98: in function <SXUI_bottombar\modules\gold.lua:70>

Locals:
it say 98 here but in your code it is really 93 because I added some things before your code

from what I can tell it does not get any data from "playerData"

also, if I want to add faction into this do I then add bracets like this "realmData[playerFaction[playerName]]"
  Reply With Quote
10-21-15, 12:06 PM   #9
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,871
Code:
    if event == "ADDON_LOADED" and arg == "test" then -- "test" is addon name
Did you change the "test" argument to the actual name (folder/.toc) of your addon? If not this code will not be executed to initialse the playerData table.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.
  Reply With Quote
10-21-15, 12:59 PM   #10
saxitoxin
A Theradrim Guardian
 
saxitoxin's Avatar
AddOn Author - Click to view addons
Join Date: May 2009
Posts: 60
Yes, I even deleted the savedVariable for the addon

SXUI_bottombar.toc
Lua Code:
  1. ## Interface: 60200
  2. ## Title: SXUI_Bottombar
  3. ## Author: |cff00FF99Saxitoxin
  4. ## Notes: Adds a bottombar by the request of u/sammojo on Reddit
  5. ## SavedVariables: TEST_CONFIG
  6.  
  7. settings.lua
  8.  
  9. modules\social.lua
  10. modules\micromenu.lua
  11. modules\armor.lua
  12. modules\talent.lua
  13. modules\clock.lua
  14. modules\tradeskill.lua
  15. modules\currency.lua
  16. modules\performance.lua
  17. modules\system.lua
  18. modules\gold.lua
  19. modules\heartstone.lua

modules\gold.lua
Lua Code:
  1. if event == "ADDON_LOADED" and arg == "SXUI_Bottombar" then -- "test" is addon name
  Reply With Quote
10-21-15, 01:24 PM   #11
lightspark
A Rage Talon Dragon Guard
 
lightspark's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2012
Posts: 341
Originally Posted by saxitoxin View Post
Thanks for the example Lightspark, it will be easyer for me to both read and learn this now

only thing is that I get a error: -- snip --

it say 98 here but in your code it is really 93 because I added some things before your code

from what I can tell it does not get any data from "playerData"
Hmm... Could you post whole file content then? Cuz Fizzlemizz is correct. Spending time guessing is quite wasteful.

Originally Posted by saxitoxin View Post
also, if I want to add faction into this do I then add bracets like this "realmData[playerFaction[playerName]]"
Nope, it'll be realmData[playerFaction][playerName], because "playerFaction" and "playerName" are string values.

One more thing, I see that you have settings.lua file. Ideally, SV save/load process ("ADDON_LOADED" and "PLAYER_LOGOUT" handlers) should be there, and gold-related stuff from "PLAYER_LOGIN" handler should be left in gold.lua file. But that's what you should do after you figure out how SVs work, so you can painlessly rewrite my code the way you need.
__________________

Last edited by lightspark : 10-21-15 at 01:56 PM.
  Reply With Quote
10-21-15, 01:28 PM   #12
saxitoxin
A Theradrim Guardian
 
saxitoxin's Avatar
AddOn Author - Click to view addons
Join Date: May 2009
Posts: 60
whole modules\gold.lua
Lua Code:
  1. local addon, ns = ...
  2. local cfg = ns.cfg
  3. local unpack = unpack
  4. --------------------------------------------------------------
  5. --if not cfg.show.gold then return end
  6.  
  7. -------------
  8. -- HELPERS --
  9. -------------
  10.  
  11. -- copies missing parts of source table to another table
  12. function CopyTable(src, dest)
  13.     if type(dest) ~= "table" then
  14.         dest = {}
  15.     end
  16.  
  17.     for k, v in pairs(src) do
  18.         if type(v) == "table" then
  19.             dest[k] = CopyTable(v, dest[k])
  20.         elseif type(v) ~= type(dest[k]) then
  21.             dest[k] = v
  22.         end
  23.     end
  24.  
  25.     return dest
  26. end
  27.  
  28. -- removes everything that is present in source table from another table
  29. function DiffTable(src, dest)
  30.     if type(dest) ~= "table" then
  31.         return {}
  32.     end
  33.  
  34.     if type(src) ~= "table" then
  35.         return dest
  36.     end
  37.  
  38.     for k, v in pairs(dest) do
  39.         if type(v) == "table" then
  40.             if not next(DiffTable(src[k], v)) then
  41.                 dest[k] = nil
  42.             end
  43.         elseif v == src[k] then
  44.             dest[k] = nil
  45.         end
  46.     end
  47.  
  48.     return dest
  49. end
  50.  
  51. local function ConvertDateToNumber(month, day, year)
  52.     month = gsub(month, "(%d)(%d?)", function(d1, d2) return d2 == "" and "0"..d1 or d1..d2 end) -- converts M to MM
  53.     day = gsub(day, "(%d)(%d?)", function(d1, d2) return d2 == "" and "0"..d1 or d1..d2 end) -- converts D to DD
  54.  
  55.     return tonumber(year..month..day)
  56. end
  57.  
  58. --------------
  59. -- DEFAULTS --
  60. --------------
  61.  
  62. local D = {
  63.     ["money_related_stuff"] = {}
  64. }
  65.  
  66. -----------
  67. -- STUFF --
  68. -----------
  69.  
  70. local function Controller_OnEvent(self, event, arg)
  71.     if event == "ADDON_LOADED" and arg == "SXUI_Bottombar" then -- "test" is addon name
  72.         local CONFIG = CopyTable(D, TEST_CONFIG)
  73.         ns.CONFIG = CONFIG -- makes this table available throughout addon
  74.  
  75.         local playerRealm, playerFaction, playerName = GetRealmName(), UnitFactionGroup("player"), UnitName("player")
  76.  
  77.         if not CONFIG["money_related_stuff"][playerRealm] then
  78.             CONFIG["money_related_stuff"][playerRealm] = {} -- creates a table, if it doesn't exist
  79.         end
  80.  
  81.         local realmData = CONFIG["money_related_stuff"][playerRealm] -- just an alias
  82.         ns.realmData = realmData
  83.  
  84.         if not realmData[playerFaction[playerName]] then
  85.             realmData[playerFaction[playerName]] = {} -- creates a table, if it doesn't exist
  86.         end
  87.  
  88.         ns.playerData = realmData[playerFaction[playerName]]
  89.  
  90.         self:UnregisterEvent("ADDON_LOADED")
  91.     elseif event == "PLAYER_LOGIN" then
  92.         local playerData = ns.playerData
  93.  
  94.         local weekday, month, day, year = CalendarGetDate()
  95.         local today = ConvertDateToNumber(month, day, year)
  96.         local updateData
  97.  
  98.         if playerData.lastLoginDate then
  99.             if playerData.lastLoginDate < today then -- is true, if last time player logged in was the day before or even earlier
  100.                 playerData.lastLoginDate = today
  101.                 updateData = true
  102.             end
  103.         else
  104.             playerData.lastLoginDate = today
  105.             updateData = true
  106.         end
  107.  
  108.         if updateData then -- daily updates
  109.             if playerData["money_on_first_weekday"] then
  110.                 if weekday == cfg.core.firstWeekday then -- 1 is Sunday, 2 is Monday, different countries have different first day of the week
  111.                     playerData["money_on_first_weekday"] = GetMoney()
  112.                 end
  113.             else
  114.                 playerData["money_on_first_weekday"] = GetMoney()
  115.             end
  116.  
  117.             playerData["money_on_first_login_today"] = GetMoney()
  118.         end
  119.  
  120.         playerData["money_on_session_start"] = GetMoney() -- this one resets on every single login or UI reload
  121.  
  122.         self:UnregisterEvent("PLAYER_LOGIN")
  123.     elseif event == "PLAYER_LOGOUT" then
  124.         TEST_CONFIG = DiffTable(D, ns.CONFIG) -- writes data into TEST_CONFIG table
  125.     end
  126. end
  127.  
  128. local Controller = CreateFrame("Frame")
  129. Controller:RegisterEvent("ADDON_LOADED")
  130. Controller:RegisterEvent("PLAYER_LOGIN")
  131. Controller:RegisterEvent("PLAYER_LOGOUT")
  132. Controller:SetScript("OnEvent", Controller_OnEvent)
  133.  
  134. local goldConverter = function(money)
  135. if money == 1 then
  136. end
  137. local g, s, c = abs(money/10000), abs(mod(money/100, 100)), abs(mod(money, 100))
  138. end
  139.  
  140. local goldFrame = CreateFrame("BUTTON",nil, cfg.SXUIframe)
  141. goldFrame:SetPoint("RIGHT",-210,0)
  142. goldFrame:SetSize(16, 16)
  143. goldFrame:EnableMouse(true)
  144. goldFrame:RegisterForClicks("AnyUp")
  145.  
  146. local goldIcon = goldFrame:CreateTexture(nil,"OVERLAY",nil,7)
  147. goldIcon:SetPoint("LEFT")
  148. goldIcon:SetTexture(cfg.mediaFolder.."datatexts\\gold")
  149. goldIcon:SetVertexColor(unpack(cfg.color.normal))
  150.  
  151. local goldText = goldFrame:CreateFontString(nil, "OVERLAY")
  152. goldText:SetFont(cfg.font, cfg.core.normalFontSize)
  153. goldText:SetPoint("RIGHT", goldFrame,2,0)
  154. goldText:SetTextColor(unpack(cfg.color.normal))
  155.  
  156. goldFrame:SetScript("OnEnter", function()
  157.     if InCombatLockdown() then return end
  158.     goldIcon:SetVertexColor(unpack(cfg.color.hover))
  159.     GameTooltip:SetOwner(goldFrame, cfg.tooltipPos)
  160.     GameTooltip:AddLine("[|cff6699FFGold|r]")
  161.     GameTooltip:AddLine(" ")
  162.     ---------------------------------------------------
  163.  
  164.     local sessionGold = playerData["money_on_session_start"]
  165.     local dailyGold = playerData["money_on_first_login_today"]
  166.     local weekGold = playerData["money_on_first_weekday"]
  167.    
  168.     local money = GetMoney()
  169.     local moneyIcon = GetCoinIcon(money)
  170.     local g, s, c = abs(money/10000), abs(mod(money/100, 100)), abs(mod(money, 100))
  171.     if ( g < 1 ) then g = "" else g = string.format("|cffffffff%d|cffffd700g|r ", g) end
  172.     if ( s < 1 ) then s = "" else s = string.format("|cffffffff%d|cffc7c7cfs|r ", s) end
  173.     c = string.format("|cffffffff%d|cffeda55fc|r ", c)
  174.    
  175.     GameTooltip:AddDoubleLine("Current Gold",("%s%s%s"):format(g,s,c))
  176.     GameTooltip:AddDoubleLine("Session Gold",sessionGold)
  177.     GameTooltip:AddDoubleLine("Daily Gold",dailyGold)
  178.     GameTooltip:AddDoubleLine("Week Gold",weekGold)
  179.  
  180.    
  181.     GameTooltip:Show()
  182. end)
  183.  
  184. goldFrame:SetScript("OnLeave", function() if ( GameTooltip:IsShown() ) then GameTooltip:Hide() end goldIcon:SetVertexColor(unpack(cfg.color.normal)) end)
  185.  
  186. goldFrame:SetScript("OnClick", function(self, button, down)
  187.     if InCombatLockdown() then return end
  188.     if button == "LeftButton" then
  189.         OpenAllBags()
  190.     elseif button == "RightButton" then
  191.         CloseAllBags()
  192.     end
  193. end)
  194.  
  195. local eventframe = CreateFrame("Frame")
  196. eventframe:RegisterEvent("PLAYER_LOGIN")
  197. eventframe:RegisterEvent("PLAYER_ENTERING_WORLD")
  198. eventframe:RegisterEvent("PLAYER_MONEY")
  199. eventframe:RegisterEvent("SEND_MAIL_MONEY_CHANGED")
  200. eventframe:RegisterEvent("SEND_MAIL_COD_CHANGED")
  201. eventframe:RegisterEvent("PLAYER_TRADE_MONEY")
  202. eventframe:RegisterEvent("TRADE_MONEY_CHANGED")
  203. eventframe:RegisterEvent("TRADE_LOGOUT")
  204.  
  205. eventframe:SetScript("OnEvent", function(self,event, ...)
  206. if event == ("PLAYER_LOGIN") then
  207. local sessionGold = 0
  208. end
  209.  
  210.     local money = GetMoney()
  211.        
  212.     local g, s, c = abs(money/10000), abs(mod(money/100, 100)), abs(mod(money, 100))
  213.  
  214.     if g > 1 then
  215.         goldText:SetText(floor(g).."g")
  216.     elseif s > 1 then
  217.         goldText:SetText(floor(s).."s")
  218.     else
  219.         goldText:SetText(floor(c).."c")
  220.     end
  221.     goldFrame:SetSize(goldText:GetStringWidth()+18, 16)
  222. end)
  Reply With Quote
10-21-15, 01:43 PM   #13
lightspark
A Rage Talon Dragon Guard
 
lightspark's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2012
Posts: 341
Originally Posted by saxitoxin View Post
snip
Ok, you messed up with playerFaction thingy... I'll rewrite code the way it should be and post it here a bit later.

Thing is you are dealing with a table, you have to create it step by step, a table within a table within a table.

It normally initialize tables this way:
Lua Code:
  1. local table1 = {
  2.     table2 = {
  3.         table3 = {
  4.         -- other tables
  5.         }
  6.     }
  7. }

or this way:
Lua Code:
  1. local table1 = {}
  2. table1.table2 = {}
  3. table1.table2.table3 = {}

You can't skip a step and do it like so:
Lua Code:
  1. local table1 = {}
  2. table1.table2.table3 = {}

You'll get an error, cuz table2 doesn't exist, there's no such index. That's what you've done wrong.
__________________
  Reply With Quote
10-21-15, 01:53 PM   #14
lightspark
A Rage Talon Dragon Guard
 
lightspark's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2012
Posts: 341
Lua Code:
  1. local CONFIG = CopyTable(D, TEST_CONFIG)
  2. ns.CONFIG = CONFIG -- makes this table available throughout addon
  3.  
  4. local playerName, playerFaction, playerRealm = UnitName("player"), UnitFactionGroup("player"), GetRealmName()
  5.  
  6. if not CONFIG["money_related_stuff"][playerRealm] then
  7.     CONFIG["money_related_stuff"][playerRealm] = {} -- creates a table if it doesn't exist
  8. end
  9.  
  10. local realmData = CONFIG["money_related_stuff"][playerRealm] -- just an alias
  11. ns.realmData = realmData
  12.  
  13. if not realmData[playerFaction] then
  14.     realmData[playerFaction] = {} -- creates a table if it doesn't exist
  15. end
  16.  
  17. local factionData = realmData[playerFaction]
  18. ns.factionData = factionData
  19.  
  20. if not factionData[playerName] then
  21.     factionData[playerName] = {} -- creates a table if it doesn't exist
  22. end
  23.  
  24. ns.playerData = factionData[playerName]
  25.  
  26. self:UnregisterEvent("ADDON_LOADED")

Drycoded, haven't tested it, but if there are typos, you can easily fix them.

One more warning, Pandarens are created as characters of "Neutral" faction, you'll need to handle it later on your own.
__________________
  Reply With Quote
10-21-15, 02:03 PM   #15
lightspark
A Rage Talon Dragon Guard
 
lightspark's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2012
Posts: 341
Anyway it's quite good looking bottom bar, I myself enjoy working together with UI designers quite much. I think, it's a nice experience for you. Have fun :3

P.S. Mind publishing it on GitHub later, when it's done?
__________________
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Need help with a addons I try to make

Thread Tools
Display Modes

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