Quantcast
Form for choosing settings - WoWInterface
Thread Tools Display Modes
05-07-15, 05:33 AM   #1
Benalish
A Theradrim Guardian
 
Benalish's Avatar
Join Date: Dec 2012
Posts: 60
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)
  Reply With Quote
05-07-15, 07:13 AM   #2
Rilgamon
Premium Member
 
Rilgamon's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 779
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")
__________________
The cataclysm broke the world ... and the pandas could not fix it!
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Form for choosing settings

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