WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Refresh options screen dynamically (https://www.wowinterface.com/forums/showthread.php?t=58597)

myrroddin 02-20-21 03:38 PM

Refresh options screen dynamically
 
Below are chunks of code from two files in RepByZone. I am not getting any errors. However, when the user toggles useClassRep, the inline description does not update unless a UI reload occurs. As you can see, I have tried putting a .NotifyChange at line 136, but it isn't doing anything. Therefore, I must be missing something, but I don't know what it is or how to address the issue. According to the Ace3 docs, .NotifyChange fires a callback, which could be what I need, but I am not sure where to handle it, or how.

Examples for Classic and/or Retail: The description, with the toggle on, assign Druids == Cenarion Circle, and Darnassus if off, or Death Knights == Knights of the Ebon Blade when on, or Orgrimmar if off. Like I said above, this does happen, but it requires a UI reload to update the text. Is it possible to do this dynamically based on the status of the toggle?

Lua Code:
  1. ---------- Core.lua ----------
  2. function RepByZone:GetRacialRep()
  3.     local _, playerRace = UnitRace("player")
  4.     local H = UnitFactionGroup("player") == "Horde"
  5.     local A = UnitFactionGroup("player") == "Alliance"
  6.  
  7.     local racialRepID = playerRace == "Dwarf" and 47 -- Ironforge
  8.     or playerRace == "Gnome" and 54 -- Gnomeregan
  9.     or playerRace == "Human" and 72 -- Stormwind
  10.     or playerRace == "NightElf" and 69 -- Darnassus
  11.     or playerRace == "Orc" and 76 -- Orgrimmar
  12.     or playerRace == "Tauren" and 81 -- Thunder Bluff
  13.     or playerRace == "Troll" and 530 -- Darkspear Trolls
  14.     or playerRace == "Scourge" and 68 -- Undercity
  15.     or playerRace == "Goblin" and 1133 -- Bilgewater Cartel
  16.     or playerRace == "Draenei" and 930 -- Exodar
  17.     or playerRace == "Worgen" and 1134 -- Gilneas
  18.     or playerRace == "BloodElf" and 911 -- Sukvermoon City
  19.     or playerRace == "Pandaren" and (A and 1353 or H and 1352 or 1216) -- Tushui Pandaren or Huojin Pandaren or Shang Xi's Academy
  20.     or playerRace == "HighmountainTauren" and 1828 -- Highmountain Tribe
  21.     or playerRace == "VoidElf" and 2170 -- Argussian Reach
  22.     or playerRace == "Mechagnome" and 2391 -- Rustbolt Resistance
  23.     or playerRace == "Vulpera" and 2158 -- Voldunai
  24.     or playerRace == "KulTiran" and 2160 -- Proudmoore Admiralty
  25.     or playerRace == "ZandalariTroll" and 2103 -- Zandalari Empire
  26.     or playerRace == "Nightborne" and 1859 -- The Nightfallen
  27.     or playerRace == "MagharOrc" and 941 -- The Mag'har
  28.     or playerRace == "DarkIronDwarf" and 59 -- Thorium Brotherhood
  29.     or playerRace == "LightforgedDraenei" and 2165 -- Army of the Light
  30.  
  31.     -- classes have factions
  32.     local classRepID = nil
  33.     local _, classFileName = UnitClass("player")
  34.     if self.db.char.useClassRep then
  35.         classRepID = classFileName == "ROGUE" and 349 -- Ravenholdt
  36.         or classFileName == "DRUID" and 609 -- Cenarion Circle
  37.         [email protected]@
  38.         or classFileName == "SHAMAN" and 1135 -- The Earthen Ring
  39.         or classFileName == "DEATHKNIGHT" and 1098 -- Knights of the Ebon Blade
  40.         or classFileName == "MAGE" and 1090 -- Kirin Tor
  41.         [email protected]@
  42.     end
  43.  
  44.     racialRepID = classRepID or racialRepID
  45.     local racialRepName = GetFactionInfoByID(racialRepID)
  46.     return racialRepID, racialRepName
  47. end
  48.  
  49. ----------- Options.lua -----------
  50. function RepByZone:GetOptions()
  51.     local db = self.db.char
  52.     local racialRepID, racialRepName = self:GetRacialRep()
  53.     local options = {
  54.         name = "RepByZone",
  55.         handler = RepByZone,
  56.         type = "group",
  57.         childGroups = "tab",
  58.         args = {
  59.             enableDisable = {
  60.                 order = 10,
  61.                 name = ENABLE .. " " .. JUST_OR .. " " .. DISABLE,
  62.                 desc = L["Toggle RepByZone on or off."],
  63.                 descStyle = "inline",
  64.                 type = "toggle",
  65.                 width = "full",
  66.                 get = function() return db.enabled end,
  67.                 set = function(info, value)
  68.                     db.enabled = value
  69.                     if value then
  70.                         self:OnEnable()
  71.                     else
  72.                         self:OnDisable()
  73.                     end
  74.                 end
  75.             },
  76.             factionStuff = {
  77.                 order = 20,
  78.                 name = L["Reputation Settings"],
  79.                 type = "group",
  80.                 args = {
  81.                     watchSubZones = {
  82.                         order = 10,
  83.                         name = L["Watch Subzones"],
  84.                         desc = L["Switch watched faction based on subzones."],
  85.                         descStyle = "inline",
  86.                         type = "toggle",
  87.                         width = "double",
  88.                         get = function() return db.watchSubZones end,
  89.                         set = function(info, value)
  90.                             db.watchSubZones = value
  91.                             if value then
  92.                                 self:RegisterEvent("ZONE_CHANGED", "SwitchedSubZones")
  93.                                 self:RegisterEvent("ZONE_CHANGED_INDOORS", "SwitchedSubZones")
  94.                                 self:SwitchedSubZones()
  95.                             else
  96.                                 self:UnregisterEvent("ZONE_CHANGED")
  97.                                 self:UnregisterEvent("ZONE_CHANGED_INDOORS")
  98.                             end
  99.                         end
  100.                     },
  101.                     verbose = {
  102.                         order = 20,
  103.                         name = L["Verbose"],
  104.                         desc = L["Print to chat when you switch watched faction."],
  105.                         descStyle = "inline",
  106.                         type = "toggle",
  107.                         width = "double",
  108.                         get = function() return db.verbose end,
  109.                         set = function(info, value) db.verbose = value end
  110.                     },
  111.                     watchOnTaxi = {
  112.                         order = 30,
  113.                         name = L["Switch on taxi"],
  114.                         desc = L["Switch watched faction while you are on a taxi."],
  115.                         descStyle = "inline",
  116.                         type = "toggle",
  117.                         width = "double",
  118.                         get = function() return db.watchOnTaxi end,
  119.                         set = function(info, value) db.watchOnTaxi = value end
  120.                     },
  121.                     useClassRep = {
  122.                         order = 40,
  123.                         name = L["Override some default racial reps with class reps."],
  124.                         desc = (L["Your class reputation is %s"]):format(racialRepName),
  125.                         descStyle = "inline",
  126.                         type = "toggle",
  127.                         width = "double",
  128.                         get = function() return db.useClassRep end,
  129.                         set = function(info, value)
  130.                             db.useClassRep = value
  131.                             racialRepID, racialRepName = self:GetRacialRep()
  132.                             LibStub("AceConfigRegistry-3.0"):NotifyChange("RepByZone")
  133.                         end
  134.                     },
  135.                     defaultRep = {
  136.                         order = 100,
  137.                         name = L["Default watched faction"],
  138.                         desc = L["Defaults to your racial faction per character."],
  139.                         type = "select",
  140.                         values = function() return self:GetAllFactions() end,
  141.                         get = function() return db.watchedRepID end,
  142.                         set = function(info, value)
  143.                             db.watchedRepID = value
  144.                             db.watchedRepName = GetFactionInfoByID(value)
  145.                         end
  146.                     }
  147.                 }
  148.             }
  149.         }
  150.     }
  151.     return options
  152. end

Vrul 02-20-21 05:41 PM

NotifyChange is for when something outside of your AceConfig changes a value while the config is open and you want it updated. Changing a setting within AceConfig automatically updates your options. Your issue is that you set a hard value for your description when you should have used a function if you want it to be dynamic:
Code:

                    useClassRep = {
                        order = 40,
                        name = L["Override some default racial reps with class reps."],
                        desc = function() return (L["Your class reputation is %s"]):format(racialRepName) end,
                        descStyle = "inline",
                        type = "toggle",
                        width = "double",
                        get = function() return db.useClassRep end,
                        set = function(info, value)
                            db.useClassRep = value
                            racialRepID, racialRepName = self:GetRacialRep()
                        end
                    },


myrroddin 02-20-21 05:56 PM

oO Thank you! I'm going to make the change now and test. A function, huh? I should have thought about that myself... :banana: :o


All times are GMT -6. The time now is 08:43 AM.

vBulletin © 2021, Jelsoft Enterprises Ltd
© 2004 - 2020 MMOUI