Thread Tools Display Modes
Prev Previous Post   Next Post Next
05-07-15, 05:33 AM   #1
Benalish
A Flamescale Wyrmkin
 
Benalish's Avatar
Join Date: Dec 2012
Posts: 123
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
 

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


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