G2A Many GEOs
SHARE
TWEET

HarvensAddonSettingsExample.lua

Harven Jun 14th, 2014 (edited) 573 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local function AddonLoaded(eventType, addonName)
  2.     if addonName ~= "HarvensAddonSettingsExample" then
  3.         return
  4.     end
  5.    
  6.     local LibHarvensAddonSettings = LibHarvensAddonSettings
  7.    
  8.     local options = {
  9.         allowDefaults = true, --will allow users to reset the settings to default values
  10.         allowRefresh = true, --if this is true, when one of settings is changed, all other settings will be checked for state change (disable/enable)
  11.         defaultsFunction = function() --this function is called when allowDefaults is true and user hit the reset button
  12.             d("Reset")
  13.         end,
  14.     }
  15.     --Create settings "container" for your addon
  16.     --First parameter is the name that will be displayed in the options,
  17.     --Second parameter is the options table (it is optional)
  18.     local settings = LibHarvensAddonSettings:AddAddon("Settings Examples", options)
  19.     if not settings then
  20.         return
  21.     end
  22.  
  23.     local areSettingsDisabled = false
  24.  
  25.     --[[
  26.         CHECKBOX
  27.     --]]
  28.     local checked = false
  29.     --Define checkbox table
  30.     local checkbox = {
  31.         type = LibHarvensAddonSettings.ST_CHECKBOX, --setting type
  32.         label = "Example Checkbox", --label text
  33.         tooltip = "This is an example checkbox", --tooltip text (optional)
  34.         default = false, --default value, only used when options.allowDefaults == true (optional)
  35.         setFunction = function(state) --this function is called when the setting is changed
  36.             checked = state
  37.         end,
  38.         getFunction = function() --this function is called to set initial state of the checkbox
  39.             return checked
  40.         end,
  41.         disable = function() return areSettingsDisabled end, --when options.allowRefresh == true, this function will be called when any other setting is changed (optional)
  42.     }
  43.     --Add the checkbox to your "container"
  44.     settings:AddSetting(checkbox)
  45.     --And that's is everything, below are other controls examples
  46.    
  47.    
  48.     --[[
  49.         SLIDER
  50.     --]]
  51.     local sliderVal = 5
  52.     local slider = {
  53.         type = LibHarvensAddonSettings.ST_SLIDER,
  54.         label = "Example Slider",
  55.         tooltip = "This is an example slider",
  56.         setFunction = function(value)
  57.             sliderVal = value
  58.         end,
  59.         getFunction = function()
  60.             return sliderVal
  61.         end,
  62.         default = 5,
  63.         min = 1,
  64.         max = 10,
  65.         step = 1,
  66.         unit = "s", --optional unit
  67.         format = "%d", --value format
  68.         disable = function() return areSettingsDisabled end,
  69.     }
  70.     settings:AddSetting(slider)
  71.    
  72.     --[[
  73.         BUTTON
  74.     --]]
  75.     local button = {
  76.         type = LibHarvensAddonSettings.ST_BUTTON,
  77.         label = "Example Button",
  78.         tooltip = "This is an example button",
  79.         buttonText = "Click me!",
  80.         clickHandler = function(control, button)
  81.             d("Click")
  82.         end,
  83.         disable = function() return areSettingsDisabled end,
  84.     }
  85.     settings:AddSetting(button)
  86.        
  87.     --[[
  88.         EDIT
  89.     --]]
  90.     local editValue = "Hello"
  91.     local edit = {
  92.         type = LibHarvensAddonSettings.ST_EDIT,
  93.         label = "Example Edit",
  94.         tooltip = "This is an example edit",
  95.         default = "Hello",
  96.         setFunction = function(value)
  97.             editValue = value
  98.         end,
  99.         getFunction = function()
  100.             return editValue
  101.         end,
  102.         disable = function() return areSettingsDisabled end,
  103.     }
  104.     settings:AddSetting(edit)
  105.  
  106.     --[[
  107.         DROPDOWN (COMBOBOX)
  108.     --]]
  109.     local selectedText = "Item 4"
  110.     local dropdown = {
  111.         type = LibHarvensAddonSettings.ST_DROPDOWN,
  112.         label = "Example Dropdown",
  113.         tooltip = "This is an example dropdown",
  114.         setFunction = function(combobox, name, item)
  115.             d("Selected item: "..name..", data: "..tostring(item.data))
  116.         end,
  117.         getFunction = function()
  118.             return selectedText
  119.         end,
  120.         default = "Item 4",
  121.         items = {
  122.             {
  123.                 name = "Item 1",
  124.                 data = 1
  125.             },
  126.             {
  127.                 name = "Item 2",
  128.                 data = "Some string"
  129.             },
  130.             {
  131.                 name = "Item 3",
  132.                 data = true
  133.             },
  134.             {
  135.                 name = "Item 4",
  136.                 data = {
  137.                     value = 10
  138.                 }
  139.             },
  140.         },
  141.         disable = function() return areSettingsDisabled end,
  142.     }
  143.     settings:AddSetting(dropdown)
  144.    
  145.     --[[
  146.         LABEL
  147.     --]]
  148.     local label = {
  149.         type = LibHarvensAddonSettings.ST_LABEL,
  150.         label = "Example Label\nMultiline! |cff0000and colors|r",
  151.     }
  152.     settings:AddSetting(label)
  153.  
  154.     --[[
  155.         SECTION TITLE
  156.     --]]
  157.     local section = {
  158.         type = LibHarvensAddonSettings.ST_SECTION,
  159.         label = "Example Section",
  160.     }
  161.     settings:AddSetting(section)
  162.    
  163.     --[[
  164.         COLOR PICKER
  165.     --]]
  166.     local colorR = 0.3
  167.     local colorG = 0.6
  168.     local colorB = 0.2
  169.     local colorA = 1.0
  170.     local color = {
  171.         type = LibHarvensAddonSettings.ST_COLOR,
  172.         label = "Example Color",
  173.         tooltip = "This is an example color",
  174.         setFunction = function(...) --newR, newG, newB, newA
  175.             colorR, colorG, colorB, colorA = ...
  176.         end,
  177.         default = {0.3, 0.6, 0.2, 1.0},
  178.         getFunction = function()
  179.             return colorR, colorG, colorB, colorA
  180.         end,
  181.         disable = function() return areSettingsDisabled end,
  182.     }
  183.     settings:AddSetting(color)
  184.    
  185.     local disableAll = {
  186.         type = LibHarvensAddonSettings.ST_CHECKBOX,
  187.         label = "Disable All",
  188.         tooltip = "This will disable all other settings",
  189.         default = false,
  190.         setFunction = function(state)
  191.             areSettingsDisabled = state
  192.         end,
  193.         getFunction = function()
  194.             return areSettingsDisabled
  195.         end,
  196.     }
  197.     settings:AddSetting(disableAll)
  198.  
  199.     --you can add all settings with one function call like this:
  200.     --settings:AddSettings({checkbox, slider, button, edit, dropdown, label, section, color})
  201. end
  202.  
  203. EVENT_MANAGER:RegisterForEvent("HarvensAddonSettignsExample", EVENT_ADD_ON_LOADED, AddonLoaded)
RAW Paste Data
Ledger Nano X - The secure hardware wallet
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top