WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Form for choosing settings (https://www.wowinterface.com/forums/showthread.php?t=52281)

Benalish 05-07-15 05:33 AM

Form for choosing settings
 
I've created a form to choose the settings. These settings are automatically saved from the client in this form:

Lua Code:
  1. ["Setting"] = {
  2.     ["track"] = "Water Shield",
  3.     ["duration"] = {
  4.         ["minimum"] = {
  5.             ["enabled"] = 1,
  6.             ["value"] = 50,
  7.         },
  8.         ["maximum"] = {
  9.         },
  10.     },
  11.     ["stack"] = {
  12.         ["minimum"] = {
  13.         },
  14.         ["maximum"] = {
  15.             ["enabled"] = 1,
  16.             ["value"] = 2,
  17.         },
  18.     },
  19. }

I created this function to save the data, when the form is closed or the apply button is clicked:

Lua Code:
  1. function loadSettings()
  2.     local options = { 'duration', 'stack' }
  3.     for i=1, #options do
  4.         local option = options[i]
  5.  
  6.         if not db[option] then
  7.             db[option] = { minimum = {}, maximum = {} }
  8.         end
  9.  
  10.             local enabled
  11.             local value
  12.  
  13.             enabled = db[option].minimum.enabled
  14.             if (enabled) then _G["min"..option].cbutton:SetChecked(1) else _G["min"..option].cbutton:SetChecked(0) end
  15.             value = db[option].minimum.value
  16.             if (value) then _G["min"..option].ebox:SetText(value) else _G["min"..option].ebox:SetText("\00") end
  17.  
  18.             enabled = db[option].maximum.enabled
  19.             if (enabled) then _G["max"..option].cbutton:SetChecked(1) else _G["max"..option].cbutton:SetChecked(0) end
  20.             value = db[option].maximum.value
  21.             if (value) then _G["max"..option].ebox:SetText(value) else _G["max"..option].ebox:SetText("\00") end
  22.  
  23.  
  24.     end
  25. end

Where

• db = The "Setting" table
• SetChecked(enable) = True to check the button; false to uncheck (boolean)
• SetText("text") = Text to be placed in the edit box (string)

Could you help me to make the code less repetitive and more clean and elegant? Expecially from 13 to 22 line, I wish to make this lines more DRY (Don't Repeat Yourself)

Rilgamon 05-07-15 07:13 AM

First thing: You should NEVER put such generic names into the global namespace (_G).
Prefix them with your AddOn-Name at least if its really required that you make them global.

Lua Code:
  1. _G["min"..option].cbutton:SetChecked(db[option].minimum.enabled and 1 or 0)
  2. _G["min"..option].ebox:SetText(db[option].minimum.value or "\00")
  3.  
  4. _G["max"..option].cbutton:SetChecked(db[option].maximum.enabled and 1 or 0)
  5. _G["max"..option].ebox:SetText(db[option].maximum.value or "\00")

If you add more of those lines I'd put it in a function

Lua Code:
  1. local function setOption(name)
  2. _G["max"..option].cbutton:SetChecked(db[option][name].enabled and 1 or 0)
  3. _G["max"..option].ebox:SetText(db[option][name].value or "\00")
  4. end
  5.  
  6. setOption("minimum")
  7. setOption("maximum")


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

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