Thread Tools Display Modes
02-06-23, 03:14 AM   #1
briskman3000
A Flamescale Wyrmkin
 
briskman3000's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2009
Posts: 108
How to move my slash commands to the options panel?

So my brain hurts from trying to figure this out. I tried to look at some of the other addons that I have installed, but they all seem to use libraries for this, which I don't use or really understand.

So I currently use a set of slash commands to handle the only configurable portion of my addon, the color in which the text is displayed for the user.

Currently this is how I accomplish this:

Lua Code:
  1. --Set color variables default values to avoid first load errors
  2. cvred = 1
  3. cvgreen = .996
  4. cvblue = .545
  5. cvalpha = 1
  6.  
  7. --Hard coded color options table
  8. local colorTable = {
  9.     ["blue"] = {0, 0, 1},
  10.     ["green"] = {0, 1, 0},
  11.     ["red"] = {1, 0, 0},
  12.     ["black"] = {0, 0, 0},
  13.     ["white"] = {1, 1, 1},
  14.     ["lightblue"] = {0, 1, 1},
  15.     ["lightred"] = {1, .5, .5},
  16.     ["pink"] = {1, .5, 1},
  17.     ["purple"] = {.7, 0, 1},
  18.     ["orange"] = {1, 0.5, 1},
  19.     ["default"] = {1, .996, .545}
  20. }
  21.  
  22. --Color Picker
  23. function ShowColorPicker(cvred, cvgreen, cvblue, cvalpha, changedCallback)
  24.  ColorPickerFrame:SetColorRGB(cvred, cvgreen, cvblue);
  25.  ColorPickerFrame.hasOpacity, ColorPickerFrame.opacity = (cvalpha ~= nil), cvalpha;
  26.  ColorPickerFrame.previousValues = {cvred, cvgreen, cvblue, cvalpha};
  27.  ColorPickerFrame.func, ColorPickerFrame.opacityFunc, ColorPickerFrame.cancelFunc =  changedCallback, changedCallback, changedCallback;
  28.  ColorPickerFrame:Hide();
  29.  ColorPickerFrame:Show();
  30.  end
  31.  
  32. local function myColorCallback(restore)
  33.  local newR, newG, newB, newA;
  34.  if restore then
  35.   newR, newG, newB, newA = unpack(restore);  
  36.  else
  37.   newA, newR, newG, newB = OpacitySliderFrame:GetValue(), ColorPickerFrame:GetColorRGB();  
  38.  end
  39.   cvred, cvgreen, cvblue, cvalpha = newR, newG, newB, newA;
  40. end
  41.  
  42. --Slash Command to change the color of the output
  43. SLASH_CONVERTRATINGS1, SLASH_CONVERTRATINGS2 = '/convertratings', '/cvr';
  44. function SlashCmdList.CONVERTRATINGS(msg, editBox)
  45.     --Grab the first input word as the command and the rest of the input as a user variable
  46.     local command, rest = msg:match("^(%S*)%s*(.-)$");
  47.     --Hard coded color options parsing
  48.    
  49.     if (colorTable[string.lower(command)]) then
  50.         cvred, cvgreen, cvblue = unpack(colorTable[string.lower(command)]);
  51.         print("Convert Ratings output color set to "..string.lower(command));
  52.     elseif string.lower(command) == 'custom' and rest == "" then
  53.         ShowColorPicker(cvred, cvgreen, cvblue, nil, myColorCallback);
  54.     else
  55.         --when no valid args entered, output this stuff
  56.         print("Convert Ratings: Valid color options are red, green, blue, black, white, lightblue, lightred, pink, purple, orange or custom")
  57.         print("Convert Ratings: The custom option will bring up the Color Picker for you to choose a color.")
  58.         print("Convert Ratings: To reset to the default color, use /convertratings default")
  59.     end
  60. end

I figured the first thing I would try to get working is adding a button to the options panel, which when you click on it, it calls my already defined color picker functions.

Lua Code:
  1. --Create a Frame to add to the default options panel
  2. local cvrpanel = CreateFrame("Frame")
  3. cvrpanel.name = "Convert Ratings"
  4.  
  5. --Options Panel Title
  6. local cvropttitle = cvrpanel:CreateFontString("ARTWORK", nil, "GameFontNormalLarge")
  7. cvropttitle:SetPoint("TOP")
  8. cvropttitle:SetText("Convert Ratings")
  9.  
  10. --Add a button to open the custom color picker
  11. local cvrbutton = CreateFrame("Button", nil, cvrpanel, "UIPanelButtonTemplate")
  12. cvrbutton:SetPoint("TOPLEFT", cvrpanel, 0, -40)
  13. cvrbutton:SetText("Custom Color")
  14. cvrbutton:SetWidth(150)
  15. cvrbutton:HookScript("OnClick", ShowColorPicker)
  16.    
  17. --Add our panel to the options frame   
  18. InterfaceOptions_AddCategory(cvrpanel)

This successfully adds a new panel to the ingame options window with the desired title displayed and the clickable button. However, when I actually click on the button, it throws an error:

Code:
5x ConvertRatings/ConvertRatings.lua:27: bad argument #1 to 'SetColorRGB' (Usage: self:SetColorRGB(rgb))
[string "=[C]"]: in function `SetColorRGB'
[string "@ConvertRatings/ConvertRatings.lua"]:27: in function <ConvertRatings/ConvertRatings.lua:26>

Locals:
(*temporary) = ColorPickerFrame {
 0 = <userdata>
 Center = Texture {
 }
 PixelSnapDisabled = true
 TopLeftCorner = Texture {
 }
 OnBackdropLoaded = <function> defined @SharedXML/Backdrop.lua:152
 Border = Frame {
 }
 BottomEdge = Texture {
 }
 GetBackdropColor = <function> defined @SharedXML/Backdrop.lua:390
 SetupTextureCoordinates = <function> defined @SharedXML/Backdrop.lua:214
 OnBackdropSizeChanged = <function> defined @SharedXML/Backdrop.lua:182
 HasBackdropInfo = <function> defined @SharedXML/Backdrop.lua:278
 SetBackdropBorderColor = <function> defined @SharedXML/Backdrop.lua:422
 RightEdge = Texture {
 }
 BottomRightCorner = Texture {
 }
 BottomLeftCorner = Texture {
 }
 GetEdgeSize = <function> defined @SharedXML/Backdrop.lua:188
 template = "Transparent"
 TopRightCorner = Texture {
 }
 TopEdge = Texture {
 }
 ApplyBackdrop = <function> defined @SharedXML/Backdrop.lua:294
 SetBackdrop = <function> defined @SharedXML/Backdrop.lua:329
 ClearBackdrop = <function> defined @SharedXML/Backdrop.lua:282
 backdropInfo = <table> {
 }
 GetBackdropBorderColor = <function> defined @SharedXML/Backdrop.lua:409
 GetBackdrop = <function> defined @SharedXML/Backdrop.lua:347
 SetBorderBlendMode = <function> defined @SharedXML/Backdrop.lua:266
 SetupPieceVisuals = <function> defined @SharedXML/Backdrop.lua:246
 SetBackdropColor = <function> defined @SharedXML/Backdrop.lua:399
 LeftEdge = Texture {
 }
 Header = Frame {
 }
}
(*temporary) = Button {
 SetTextToFit = <function> defined @SharedXML/SecureUIPanelTemplates.lua:440
 Right = Texture {
 }
 Left = Texture {
 }
 fitTextWidthPadding = 40
 FitToText = <function> defined @SharedXML/SecureUIPanelTemplates.lua:445
 Text = TopText {
 }
 0 = <userdata>
 Middle = Texture {
 }
 fitTextCanWidthDecrease = true
}
(*temporary) = "LeftButton"
(*temporary) = false
So my next though would be that I needed to change the hookscript line to:

Lua Code:
  1. cvrbutton:HookScript("OnClick", ShowColorPicker(cvred, cvgreen, cvblue, nil, myColorCallback))

but that just throws a different error when you load in:

Code:
2x ConvertRatings/ConvertRatings.lua:79: Usage: Button:HookScript("frameScriptTypeName", function[, bindingType])
[string "=[C]"]: in function `HookScript'
[string "@ConvertRatings/ConvertRatings.lua"]:79: in main chunk

Locals:
(*temporary) = Button {
 SetTextToFit = <function> defined @SharedXML/SecureUIPanelTemplates.lua:440
 Right = Texture {
 }
 Left = Texture {
 }
 fitTextWidthPadding = 40
 FitToText = <function> defined @SharedXML/SecureUIPanelTemplates.lua:445
 Text = TopText {
 }
 0 = <userdata>
 Middle = Texture {
 }
 fitTextCanWidthDecrease = true
}
(*temporary) = "OnClick"
My next thought was maybe I should change it from HookScript, to SetScript but just gave me the same errors referencing SetScript instead of HookScript.

What probably glaring issue am I not seeing here? Why does changing it from a slash command to a button press fuck it all up?
__________________
My Addons: Convert Ratings Honor Track
  Reply With Quote
02-06-23, 07:39 AM   #2
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,871
Code:
cvrbutton:HookScript("OnClick", function() ShowColorPicker(cvred, cvgreen, cvblue, nil, myColorCallback) end)
The problem there is that you have to make sure that cvred, cvgreen, cvblue, and myColorCallback are all in scope or you'll just be passing nils
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 02-06-23 at 07:58 AM.
  Reply With Quote
02-06-23, 01:50 PM   #3
briskman3000
A Flamescale Wyrmkin
 
briskman3000's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2009
Posts: 108
Originally Posted by Fizzlemizz View Post
Code:
cvrbutton:HookScript("OnClick", function() ShowColorPicker(cvred, cvgreen, cvblue, nil, myColorCallback) end)
The problem there is that you have to make sure that cvred, cvgreen, cvblue, and myColorCallback are all in scope or you'll just be passing nils
Those are all declared in the filespace above this, so they all are available for the code to use.

Wrapping ShowColorPicker in function() and end seemed to do the trick as the color picker now shows and sets the color when you pick one.
__________________
My Addons: Convert Ratings Honor Track
  Reply With Quote
02-06-23, 03:29 PM   #4
briskman3000
A Flamescale Wyrmkin
 
briskman3000's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2009
Posts: 108
Ok so I have one final problem that I can't seem to figure out, and my googling has turned up no help.

In addition to the color wheel, I have some hard coded color options that I want the user to be able to choose from via a drop down menu.

I was able to successfully get the drop down to successfully open, set the colors in my addon and then display the current color chosen in the drop down frame when it is closed. My saved variables get set properly so the color is saved between sessions.

What I can't figure out is how to get the drop down menu to read from the saved variables so that it will correctly display the chosen value when the user opens up the options menu in a different session.

When I reload the ui, it sets the value of the displayed text to "Default". I tried to not declare a default value for cvcolor at the top of the file, but then i get a nil value concat error.

cvred, cvgreen, cvblue, cvalpha, cvcolor are all declared in my toc and I can watch the saved variables file get updated when i reload the ui.


Again I'm probably missing something stupid easy.

Lua Code:
  1. --Set color variables default values to avoid first load errors
  2. cvred = 1
  3. cvgreen = .996
  4. cvblue = .545
  5. cvalpha = 1
  6. cvcolor = "Default"
  7.  
  8. --Hard coded color options table
  9. local colorTable = {
  10.     ["blue"] = {0, 0, 1},
  11.     ["green"] = {0, 1, 0},
  12.     ["red"] = {1, 0, 0},
  13.     ["black"] = {0, 0, 0},
  14.     ["white"] = {1, 1, 1},
  15.     ["light blue"] = {0, 1, 1},
  16.     ["light red"] = {1, .5, .5},
  17.     ["pink"] = {1, .5, 1},
  18.     ["purple"] = {.7, 0, 1},
  19.     ["orange"] = {1, 0.5, 1},
  20.     ["default"] = {1, .996, .545}
  21. }
  22.  
  23. --Color Picker
  24. local function ShowColorPicker(cvred, cvgreen, cvblue, cvalpha, changedCallback)
  25.  ColorPickerFrame:SetColorRGB(cvred, cvgreen, cvblue);
  26.  ColorPickerFrame.hasOpacity, ColorPickerFrame.opacity = (cvalpha ~= nil), cvalpha;
  27.  ColorPickerFrame.previousValues = {cvred, cvgreen, cvblue, cvalpha};
  28.  ColorPickerFrame.func, ColorPickerFrame.opacityFunc, ColorPickerFrame.cancelFunc =  changedCallback, changedCallback, changedCallback;
  29.  ColorPickerFrame:Hide();
  30.  ColorPickerFrame:Show();
  31.  end
  32.  
  33. function myColorCallback(restore)
  34.  local newR, newG, newB, newA;
  35.  if restore then
  36.   newR, newG, newB, newA = unpack(restore);  
  37.  else
  38.   newA, newR, newG, newB = OpacitySliderFrame:GetValue(), ColorPickerFrame:GetColorRGB();  
  39.  end
  40.   cvred, cvgreen, cvblue, cvalpha = newR, newG, newB, newA;
  41. end
  42.  
  43. --Create a Frame to add to the default options panel
  44. local cvrpanel = CreateFrame("Frame")
  45. cvrpanel.name = "Convert Ratings"
  46.  
  47. --Options Panel Title
  48. local cvropttitle = cvrpanel:CreateFontString("ARTWORK", nil, "GameFontNormalLarge")
  49. cvropttitle:SetPoint("TOP")
  50. cvropttitle:SetText("Convert Ratings")
  51.  
  52. --Add a dropdown menu to select a pre defined color
  53. local cvrddtitle = cvrpanel:CreateFontString("ARTWORK", nil, "GameFontNormal")
  54. cvrddtitle:SetPoint("TOPLEFT", cvrpanel, 0, - 80)
  55. cvrddtitle:SetText("Pick a Color")
  56.  
  57. local cvrdropdown = CreateFrame("Frame", "CVRDropDown", cvrpanel, "UIDropDownMenuTemplate")
  58. cvrdropdown:SetPoint("TOPLEFT", cvrpanel, -20, -100)
  59. UIDropDownMenu_SetWidth(cvrdropdown, 150)
  60.  
  61. local function cvrdropdown_OnClick(self, arg1, arg2, checked)
  62.     cvred, cvgreen, cvblue = unpack(colorTable[string.lower(arg1)])
  63.     UIDropDownMenu_SetText(cvrdropdown, "Current Color: " .. arg1)
  64.     cvcolor = tostring(arg1)
  65. end
  66.  
  67. function cvrdropdown_Menu(frame, level, menuList)
  68.     UIDropDownMenu_SetText(cvrdropdown, "Current Color: " .. cvcolor)
  69.     local info = UIDropDownMenu_CreateInfo()   
  70.     info.func = cvrdropdown_OnClick
  71.     info.text, info.arg1 = "Default", "Default"
  72.     UIDropDownMenu_AddButton(info)
  73.     info.func = cvrdropdown_OnClick
  74.     info.text, info.arg1 = "Blue", "Blue"
  75.     UIDropDownMenu_AddButton(info)
  76.     info.func = cvrdropdown_OnClick
  77.     info.text, info.arg1 = "Green", "Green"
  78.     UIDropDownMenu_AddButton(info)
  79.     info.func = cvrdropdown_OnClick
  80.     info.text, info.arg1 = "Red", "Red"
  81.     UIDropDownMenu_AddButton(info)
  82.     info.func = cvrdropdown_OnClick
  83.     info.text, info.arg1 = "Black", "Black"
  84.     UIDropDownMenu_AddButton(info)
  85.     info.func = cvrdropdown_OnClick
  86.     info.text, info.arg1 = "White", "White"
  87.     UIDropDownMenu_AddButton(info)
  88.     info.func = cvrdropdown_OnClick
  89.     info.text, info.arg1 = "Light Blue", "Light Blue"
  90.     UIDropDownMenu_AddButton(info)
  91.     info.func = cvrdropdown_OnClick
  92.     info.text, info.arg1 = "Light Red", "Light Red"
  93.     UIDropDownMenu_AddButton(info)
  94.     info.func = cvrdropdown_OnClick
  95.     info.text, info.arg1 = "Pink", "Pink"
  96.     UIDropDownMenu_AddButton(info)
  97.     info.func = cvrdropdown_OnClick
  98.     info.text, info.arg1 = "Purple", "Purple"
  99.     UIDropDownMenu_AddButton(info)
  100.     info.func = cvrdropdown_OnClick
  101.     info.text, info.arg1 = "Orange", "Orange"
  102.     UIDropDownMenu_AddButton(info)
  103. end
  104.  
  105. UIDropDownMenu_Initialize(cvrdropdown, cvrdropdown_Menu)
  106.  
  107. --Add a button to open the custom color picker
  108. local cvrbutton = CreateFrame("Button", nil, cvrpanel, "UIPanelButtonTemplate")
  109. cvrbutton:SetPoint("TOPLEFT", cvrpanel, 0, -40)
  110. cvrbutton:SetText("Custom Color")
  111. cvrbutton:SetWidth(150)
  112. cvrbutton:HookScript("OnClick", function() ShowColorPicker(cvred, cvgreen, cvblue, nil, myColorCallback); cvcolor = "Custom"; UIDropDownMenu_SetText(cvrdropdown, "Current Color: " .. cvcolor)  end)
  113.    
  114. --Add our panel to the options frame   
  115. InterfaceOptions_AddCategory(cvrpanel)

Edit: I just noticed that when I click on the dropdown menu in the options frame, the displayed text changes to the value currently set in the saved variable. So it knows what it is between sessions, it just doesn't update until i click on the drop down menu frame.
__________________
My Addons: Convert Ratings Honor Track

Last edited by briskman3000 : 02-06-23 at 03:39 PM.
  Reply With Quote
02-06-23, 03:45 PM   #5
briskman3000
A Flamescale Wyrmkin
 
briskman3000's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2009
Posts: 108
I figured it out.

I had to add the UIDropDownMenu_SetText fucntion to my addon's hook to the PLAYER_ENTERING_WORLD event.

Now when I open up the options frame in a different session, it correctly displays the value set in the saved variable in the drop down menu.
__________________
My Addons: Convert Ratings Honor Track
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » How to move my slash commands to the options panel?

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