View Single Post
08-09-12, 03:42 AM   #2
zork
A Pyroguard Emberseer
 
zork's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2008
Posts: 1,740
So to sum this up. When trying to create config menus the following UI elements come in handy.

Frame
A normal frame that can hold all your config elements. (buttons, checkboxes, etc.)
FrameType: Frame
Template: BasicFrameTemplate
Widget: http://wowprogramming.com/docs/widgets/Frame
ScrollFrame
A frame that is scrollable inside.. The best example for this is the keybind setup frame.
FrameType: ScrollFrame
Template: FauxScrollFrameTemplate
Widget: http://wowprogramming.com/docs/widgets/ScrollFrame
EditBox
A small input element that lets you input data into a field.
FrameType: EditBox
Template: InputBoxTemplate
Widget: http://wowprogramming.com/docs/widgets/EditBox
CheckButton / CheckBox
A checkbox like input element. Basically switch values between 0 and 1.
FrameType: CheckButton
Template: UICheckButtonTemplate
Widget: http://wowprogramming.com/docs/widgets/CheckButton

Lua Code:
  1. --Example:
  2. local name = "MyExampleCheckButton"
  3. local template = "UICheckButtonTemplate"
  4. local checkButton = CreateFrame("Button",name,UIParent,template) --frameType, frameName, frameParent, frameTemplate    
  5. checkButton:SetPoint("CENTER",0,0)
  6. checkButton.text = _G[name.."Text"]
  7. checkButton.text:SetText("Hello World")
  8. checkButton:SetScript("OnClick", function(self,event,arg1)
  9.   if self:GetChecked() then
  10.     print("Button is checked")
  11.   else
  12.     print("Button is unchecked")
  13.   end
  14. end)
Lua Code:
  1. --Example2:
  2. local name = "MyExampleCheckButton"
  3. local template = "OptionsBaseCheckButtonTemplate"
  4. local checkButton = CreateFrame("Button",name,UIParent,template) --frameType, frameName, frameParent, frameTemplate    
  5. checkButton:SetPoint("CENTER",0,0)
  6. checkButton.text = _G[name.."Text"]
  7. checkButton.text:SetText("Hello World")
  8. local callback = function(setting) print("callback") end
  9. checkButton.setFunc = callback
CheckButton / RadioButton
A list of radiobuttons. Lets you choose one of "n" options.
FrameType: CheckButton
Template: UIRadioButtonTemplate
Widget: http://wowprogramming.com/docs/widgets/CheckButton
Slider
Sliders are elements intended to allow the user to choose a value in a range.
FrameType: Slider
Template: UIPanelScrollBarTemplate
Widget: http://wowprogramming.com/docs/widgets/Slider

Lua Code:
  1. --Example:
  2. local name = "MyExampleSlider"
  3. local template = "OptionsSliderTemplate"
  4. local slider = CreateFrame("Slider",name,UIParent,template) --frameType, frameName, frameParent, frameTemplate  
  5. slider:SetPoint("CENTER",0,0)
  6. slider.textLow = _G[name.."Low"]
  7. slider.textHigh = _G[name.."High"]
  8. slider.text = _G[name.."Text"]
  9. slider:SetMinMaxValues(1, 100)
  10. slider.minValue, slider.maxValue = slider:GetMinMaxValues()
  11. slider.textLow:SetText(slider.minValue)
  12. slider.textHigh:SetText(slider.maxValue)
  13. slider.text:SetText(name)
  14. slider:SetValue(50)
  15. slider:SetValueStep(1)
  16. slider:SetScript("OnValueChanged", function(self,event,arg1) print(event) end)
Button
A clickable frame that calls an action OnClick. Blizzard provides tons of button templates.
FrameType: Button
Template: UIPanelButtonTemplate
Widget: http://wowprogramming.com/docs/widgets/Button

Lua Code:
  1. --Example:
  2. local name = "MyExampleButton"
  3. local template = "UIPanelButtonTemplate"
  4. local button = CreateFrame("Button",name,UIParent,template) --frameType, frameName, frameParent, frameTemplate    
  5. button:SetSize(150,80)
  6. button:SetPoint("CENTER",0,0)
  7. button.text = _G[name.."Text"]
  8. button.text:SetText("Hello World")
UIDropDownMenu
Depending on setup this can be both a selectbox or a context menu.
FrameType: Frame
Template: UIDropDownMenuTemplate
WriteUps: [1] [2] [3] [4]

Lua Code:
  1. --EasyMenu Example:
  2. local dropdown = CreateFrame("Frame", "MyMenuFrame", nil, "UIDropDownMenuTemplate")
  3. local menuTable = {}
  4. local line, data, submenu = nil, nil, nil
  5.  
  6. local function createMenuLine(data)
  7.   local line = {}
  8.   if not data then return line end
  9.   line.text = data.text or nil
  10.   line.isTitle = data.isTitle or nil
  11.   line.notCheckable = data.notCheckable or nil
  12.   line.notClickable = data.notClickable or nil
  13.   line.keepShownOnClick = data.keepShownOnClick or nil
  14.   line.hasArrow = data.hasArrow or nil
  15.   line.func = data.func or nil
  16.   line.menuList = data.menuList or nil
  17.   return line
  18. end
  19.  
  20. local function createDelimLine()
  21.   --split line
  22.   local delim = "~~~~~~~~~~~~~~~~~~~~~~~"
  23.   data = { text = delim, notCheckable = true, notClickable = true }
  24.   line = createMenuLine(data)
  25.   return line
  26. end
  27.  
  28. local function createMenu()
  29.  
  30.   --title
  31.   data = { text = "MyMenuTitle", isTitle = true, notCheckable = true, notClickable = true }
  32.   line = createMenuLine(data)
  33.   table.insert(menuTable,line)
  34.  
  35.   --add delimiter
  36.   line = createDelimLine()
  37.   table.insert(menuTable,line)
  38.  
  39.     --generate sub menu
  40.     submenu = {}      
  41.     data = { text = "General", isTitle = true, notCheckable = true, notClickable = true }
  42.     line = createMenuLine(data)
  43.     table.insert(submenu,line)
  44.     line = createDelimLine()
  45.     table.insert(submenu,line)
  46.     data = { text = "MyOption1", func = myGlobalFunction1, notCheckable = true, keepShownOnClick = true, }
  47.     line = createMenuLine(data)
  48.     table.insert(submenu,line)
  49.     data = { text = "MyOption2", func = myGlobalFunction2, notCheckable = true, keepShownOnClick = true, }
  50.     line = createMenuLine(data)
  51.     table.insert(submenu,line)
  52.     data = { text = "MyOption3", func = myGlobalFunction3, notCheckable = true, keepShownOnClick = true, }
  53.     line = createMenuLine(data)
  54.     table.insert(submenu,line)
  55.  
  56.   --add submenu to menuTable
  57.   data = { text = "General", notCheckable = true, hasArrow = true, menuList = submenu }
  58.   line = createMenuLine(data)
  59.   table.insert(menuTable,line)
  60.  
  61.   --add delimiter
  62.   line = createDelimLine()
  63.   table.insert(menuTable,line)
  64.  
  65.   --close menu
  66.   data = { text = "Close", func = function() CloseDropDownMenus() end, notCheckable = true }
  67.   line = createMenuLine(data)
  68.   table.insert(menuTable,line)
  69.  
  70. end
  71.  
  72. --INIT
  73. createMenu()
  74.  
  75. --CALL
  76. local function SlashCmd(cmd)
  77.   EasyMenu(menuTable, dropdown, "cursor", 10 , -15, "MENU")
  78. end
  79. SlashCmdList["menu"] = SlashCmd
  80. SLASH_menu1 = "/menu"
ColorSelect / ColorPicker
In many cases you want to recolor textures. All you have to do is to call the ColorPicker and apply a callback function that handles the colors returned by the ColorPicker. Normally you have a small frame with a texture and once you change the color in the ColorPicker the texture color updates via the callback function.
FrameType: Frame (with an applied texture)
WriteUps: [1] [2]

Lua Code:
  1. local function showColorPicker(r,g,b,a,callback)
  2.   ColorPickerFrame:SetColorRGB(r,g,b)
  3.   ColorPickerFrame.hasOpacity, ColorPickerFrame.opacity = (a ~= nil), a
  4.   ColorPickerFrame.previousValues = {r,g,b,a}
  5.   ColorPickerFrame.func, ColorPickerFrame.opacityFunc, ColorPickerFrame.cancelFunc = callback, callback, callback
  6.   ColorPickerFrame:Hide() -- Need to run the OnShow handler.
  7.   ColorPickerFrame:Show()
  8. end
  9.  
  10. local function createMyFrame()
  11.   --frame
  12.   local f = CreateFrame("FRAME",nil,UIParent)
  13.   f:SetSize(150,150)
  14.   f:SetPoint("CENTER",0,0)
  15.   --texture
  16.   f.tex = f:CreateTexture(nil,"BACKGROUND",nil,-7)
  17.   f.tex:SetAllPoints(f)
  18.   f.tex:SetTexture(1,0,0,1)
  19.   --recolor callback function
  20.   f.recolorTexture = function(color)
  21.     local r,g,b,a
  22.     if color then
  23.       r,g,b,a = unpack(color)
  24.     else
  25.       r,g,b = ColorPickerFrame:GetColorRGB()
  26.       a = OpacitySliderFrame:GetValue()
  27.     end
  28.     f.tex:SetVertexColor(r,g,b,a)
  29.   end
  30.   f:EnableMouse(true)
  31.   f:SetScript("OnMouseDown", function(self,button,...)
  32.     if button == "LeftButton" then
  33.       local r,g,b,a = self.tex:GetVertexColor()
  34.       showColorPicker(r,g,b,a,self.recolorTexture)
  35.     end
  36.   end)
  37. end
  38.  
  39. --init
  40. createMyFrame()
StatusBar
Sometimes it can be helpful to display data in a statusbar. They are similar to sliders, just without the option to edit. Like a progressbar or healthbar.
FrameType: StatusBar
Template: ???
Widget: http://wowprogramming.com/docs/widgets/StatusBar

Lua Code:
  1. --Example:
  2. local color = {0,1,0}
  3. local statusbar = CreateFrame("StatusBar",nil,UIParent) --frameType, frameName, frameParent  
  4. statusbar:SetPoint("CENTER",0,0)
  5. statusbar:SetSize(200,20)
  6. --statusbar background
  7. statusbar.bg = statusbar:CreateTexture(nil,"BACKGROUND",nil,-8)
  8. statusbar.bg:SetAllPoints(statusbar)
  9. statusbar.bg:SetTexture(unpack(color))
  10. statusbar.bg:SetAlpha(0.2)
  11. --statusbar texture
  12. local tex = statusbar:CreateTexture(nil,"BACKGROUND",nil,-6)
  13. tex:SetTexture(unpack(color))
  14. statusbar:SetStatusBarTexture(tex)
  15. statusbar:SetStatusBarColor(unpack(color))
  16. --values
  17. statusbar:SetMinMaxValues(1, 100)
  18. statusbar.minValue, statusbar.maxValue = statusbar:GetMinMaxValues()
  19. statusbar:SetValue(50)
__________________
| Simple is beautiful.
| WoWI AddOns | GitHub | Zork (WoW)

"I wonder what the non-pathetic people are doing tonight?" - Rajesh Koothrappali (The Big Bang Theory)

Last edited by zork : 08-09-12 at 06:51 AM.
  Reply With Quote