Go to Page... |
Thread Tools | Display Modes |
07-16-13, 02:50 PM | #1 |
Removing an item from a UIDropDownMenu
[This is a cross-post from the wowace forum.]
I've just recently got into WoW AddOn programming and am designing my first interface and have came across what seems like a rather simple problem. I'm trying to make an options dialog (not in the the options menu, mostly because I wanted to test how to create frames by themselves) with a profiles drop-down menu that updates dynamically as the user creates and deletes profiles. There's no problem with creating profiles. As soon as the user enters a name it will show up in the drop-down menu the next time it's dropped. The problem comes when a user wishes to delete a profile. I've set it up so it will successfully delete the profile from the SavedVariables table, and delete it from the menu's tables, but the button's still in the drop-down until I /reload the interface. It seems to me that the problem is that once an item is added to them menu with UIDropDownMenu_AddButton() it's there permanently. I've done some digging and it looks like the AddButton function creates a button frame. Is there some way to retrieve the name of the button and hide it? Or is it something more complicated than that? CA_UserProfile is a SavedVariablesPerCharacter variable that holds the profile to default to when a character loads into the game. (Set to "Default" by default.) CA_ChatAnnouncements is the SavedVariables variable that contains the table with the various setups. mainFrame is the parent frame for the whole dialog. The ADDON_LOADED event script runs InitializeDropDown() to make sure the SavedVariables are all loaded with the profiles before trying to create the menu. Frame creation and initialization function: Lua Code:
Initialization function wrapper: Lua Code:
What I was trying to do with deleteButton: Lua Code:
The OnClick function for reference. I added the virtual variable so I can change button states without needing user input. The variable value is there so I can check the state of the drop-down menu across the AddOn: Lua Code:
Also, the code ran for when the "Create" button is hit: Lua Code:
Sorry if this is a bit too much code in one post, but I feel it's all relevant. |
|
07-16-13, 04:36 PM | #2 |
In InitiateDropDownMenu you are constantly adding to dropDownItems but never removing anything. I know you have "dropDownItems[value] = nil" in your OnClick function but value is a string and dropDownItems is an array so that line doesn't do anything. Here are the changes (untested) that I would make to remove some unneeded function calls, redundant variable assignments, and fix your issue:
Code:
local dropDownProfiles = CreateFrame("Button", "DropDownProfiles", mainFrame, "UIDropDownMenuTemplate") dropDownProfiles:SetPoint("TOPLEFT", -15, -21) dropDownProfiles:Show() dropDownProfiles:RegisterEvent("ADDON_LOADED") local dropDownItems = {} local function initialize(self, level) local info = UIDropDownMenu_CreateInfo() info.func = OnClick for index = 1, #dropDownItems do info.text = dropDownItems[index] info.value = dropDownItems[index] UIDropDownMenu_AddButton(info, level) end end Code:
local function InitiateDropDownMenu() wipe(dropDownItems) dropDownItems[1] = "Default" for item in pairs(CA_ChatAnnouncements) do if item ~= "Default" then dropDownItems[#dropDownItems + 1] = item end end UIDropDownMenu_Initialize(dropDownProfiles, initialize) UIDropDownMenu_SetWidth(dropDownProfiles, 100) UIDropDownMenu_SetButtonWidth(dropDownProfiles, 124) UIDropDownMenu_SetSelectedValue(dropDownProfiles, CA_UserProfile) UIDropDownMenu_JustifyText(dropDownProfiles, "LEFT") if CA_UserProfile == "Default" then deleteButton:Disable() end end Code:
deleteButton:SetScript("PostClick", function() CA_ChatAnnouncements[value] = nil InitiateDropDownMenu() OnClick(nil, "Default") end) |
|
07-16-13, 06:44 PM | #3 |
It's working good now, thanks! Except, there is one thing. When clicking on an item in the menu now, it also "selects" (Although it doesn't change value, so the rest of the program works fine) all the items under it. It's just a visual thing, but still a bit annoying.
Example: [ ] Default [ ] ToT [x] Battlegrounds [x] Trade [x] Guild Before it would just select one at a time, so I'm unsure what could be causing it now. |
|
07-16-13, 09:20 PM | #4 |
Here's a cleaned up (and hopefully fixed) version:
Code:
local dropDownItems = { "Default" } local dropDownProfiles = CreateFrame("Button", "CA_DropDownProfiles", mainFrame, "UIDropDownMenuTemplate") dropDownProfiles:SetPoint("TOPLEFT", -15, -21) dropDownProfiles:Show() UIDropDownMenu_SetWidth(dropDownProfiles, 100) UIDropDownMenu_SetButtonWidth(dropDownProfiles, 124) UIDropDownMenu_JustifyText(dropDownProfiles, "LEFT") local function SetUserProfile(_, profile) CA_UserProfile = profile UIDropDownMenu_SetSelectedName(dropDownProfiles, profile) local announcementGroup = CA_ChatAnnouncements[profile] announcementOneBox:SetText(announcementGroup["Announcement1"]) announcementTwoBox:SetText(announcementGroup["Announcement2"]) announcementThreeBox:SetText(announcementGroup["Announcement3"]) announcementFourBox:SetText(announcementGroup["Announcement4"]) announcementFiveBox:SetText(announcementGroup["Announcement5"]) if profile ~= "Default" then deleteButton:Enable() else deleteButton:Disable() end end dropDownProfiles:SetScript("OnEvent", function(self, event, name) if event == "ADDON_LOADED" then if name ~= "ThisAddonName" then return end if type(CA_ChatAnnouncements) ~= "table" then CA_ChatAnnouncements = { } end if type(CA_UserProfile) ~= "string" or type(CA_ChatAnnouncements[CA_UserProfile]) ~= "table" then CA_UserProfile = "Default" end for profile in pairs(CA_ChatAnnouncements) do if profile ~= "Default" then dropDownItems[#dropDownItems + 1] = profile end end SetUserProfile(nil, CA_UserProfile) end end) dropDownProfiles:RegisterEvent("ADDON_LOADED") dropDownProfiles.initialize = function(self, level) local info = UIDropDownMenu_CreateInfo() info.func = SetUserProfile for index = 1, #dropDownItems do info.text = dropDownItems[index] info.arg1 = dropDownItems[index] UIDropDownMenu_AddButton(info, level) end end Code:
deleteButton:SetScript("PostClick", function() CA_ChatAnnouncements[CA_UserProfile] = nil for index = 2, #dropDownItems do if dropDownItems[index] == CA_UserProfile then tremove(dropDownItems, index) break end end SetUserProfile(nil, "Default") end) Code:
OnAccept = function(self) local profile, data = self.editBox:GetText(), { } for key, value in pairs(CA_ChatAnnouncements[CA_UserProfile]) do data[key] = value end dropDownItems[#dropDownItems + 1] = profile CA_ChatAnnouncements[profile] = data SetUserProfile(nil, profile) end |
|
07-17-13, 02:28 AM | #5 |
For future reference, when you cross-post the same thing on two closely related forums, please state that prominently at the top of your post in both places, and provide a link from each to the other. It's beyond frustrating to spend 30 minutes typing up a lengthy reply with explanations and examples, only to find your cross-post on the other site afterwards where someone had already answered the question.
__________________
Retired author of too many addons. Message me if you're interested in taking over one of my addons. Don’t message me about addon bugs or programming questions. |
|
07-17-13, 06:27 PM | #6 |
I'm sorry about the cross-post thing. I wasn't entirely sure on Forum etiquette and did what I thought was right, but as it turns out is wrong. I haven't gotten the opportunity to try out any of the suggestions, but when I do I will be posting to this forum.
|
|
WoWInterface » Developer Discussions » General Authoring Discussion » Removing an item from a UIDropDownMenu |
«
Previous Thread
|
Next Thread
»
|
Display Modes |
Linear Mode |
Switch to Hybrid Mode |
Switch to Threaded Mode |
|
|