View Single Post
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