View Single Post
08-27-19, 01:22 PM   #7
Vrul
A Scalebane Royal Guard
 
Vrul's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2007
Posts: 404
I changed your buttons names from "CHACHING_BagButtonOne" to "CHACHING_BagButton1" and similarly changed isBagChecked[BAG1] to isBagChecked[1] (repeat for 2 through 5). This should look how you originally had it:
Code:
function cha:CHACHING_InitializeOptions()
 
    local ConfigurationPanel = CreateFrame("FRAME","CHACHING_MainFrame")
    ConfigurationPanel.name = L["ADDON_NAME"]
    InterfaceOptions_AddCategory(ConfigurationPanel)    -- Register the Configuration panel with LibUIDropDownMenu

    -- Print a header at the top of the panel
    local IntroMessageHeader = ConfigurationPanel:CreateFontString(nil, "ARTWORK","GameFontNormalLarge")
    IntroMessageHeader:SetPoint("TOPLEFT", 10, -10)
    IntroMessageHeader:SetText(L["ADDON_NAME_AND_VERSION"])
 
    local AuthorSubHeader = ConfigurationPanel:CreateFontString(nil, "ARTWORK","GameFontNormal")
    AuthorSubHeader:SetPoint("TOPLEFT", 20, -30)
    AuthorSubHeader:SetText("Author: Shadowraith@Feathermoon")
 
    local DescrSubHeader = ConfigurationPanel:CreateFontString(nil, "ARTWORK","GameFontNormalLarge")
    DescrSubHeader:SetPoint("TOPLEFT", 20, -50)
    DescrSubHeader:SetText("Enables the bulk selling of selected items in player's inventory.")
        
    local ReadmeMessageText = ConfigurationPanel:CreateFontString(nil, "ARTWORK","GameFontNormalLarge")
    ReadmeMessageText:SetPoint("TOPLEFT", 10, -280)
    ReadmeMessageText:SetText(strjoin("\n",
        "                    *** IMPORTANT ***                               ",
        "The merchant buyback window only has 12 slots. However, the merchant",
        "will buy as many items as Cha-Ching is configured to sell. So, if more",
        "than 12 items were sold, you will only be able to buyback the last 12"
    ))

    -- Create check button to sell grey items
    local GreyQualityButton = CreateFrame("CheckButton", "CHACHING_GreyQualityButton", ConfigurationPanel, "ChatConfigCheckButtonTemplate")
    GreyQualityButton:SetPoint("TOPLEFT", 20, -80)
    GreyQualityButton.tooltip = "Check to sell all poor (grey) items in your inventory."
    _G[GreyQualityButton:GetName().."Text"]:SetText("Sell Grey Items?")
    GreyQualityButton:SetScript("OnClick", function(self)
        sellGrey = self:GetChecked() and true or false
    end)
 
    -- Create check button to sell white items
    local WhiteQualityButton = CreateFrame("CheckButton", "CHACHING_WhiteQualityButton", ConfigurationPanel, "ChatConfigCheckButtonTemplate")
    WhiteQualityButton:SetPoint("TOPLEFT", 200, -80)
    WhiteQualityButton.tooltip = L["TOOLTIP_CHECK_WHITE_BTN"]
    _G[WhiteQualityButton:GetName().."Text"]:SetText("Sell White Items (Only Armor and Weapons?")
    WhiteQualityButton:SetScript("OnClick", function(self)
        sellWhite = self:GetChecked() and true or false
    end)
 
    -- Create bag select buttons
    local bagsText = ConfigurationPanel:CreateFontString(nil, "ARTWORK","GameFontNormal")
    bagsText:SetPoint("TOPLEFT", 20, -200)
    bagsText:SetText("Select a bag. All items in the selected bag will be sold!")

    local xpos = 20
    local ypos = -215
    local delta_y = -20

    local function BagSelectButtonOnClick(self)
        isBagChecked[self.bagIndex] = self:GetChecked() and true or false
    end

    local bagSelectButtons = { } 
    for id = 0, 4 do
        local bagIndex = id + 1
        local button = CreateFrame("CheckButton", "CHACHING_BagButton" .. bagIndex, ConfigurationPanel, "ChatConfigCheckButtonTemplate")
        button:SetPoint("TOPLEFT", xpos, ypos + (delta_y * id))
        button.label = _G[button:GetName() .. "Text"]
        button.bagIndex = bagIndex
        button:SetScript("OnClick", BagSelectButtonOnClick)
        bagSelectButtons[bagIndex] = button
    end

    local function UpdateBagSelectButtons()
        local index = 0
        for id = 0, 4 do
            if GetContainerNumSlots(id) > 0 then
                index = index + 1
                local button = bagSelectButtons[index]
                button.bagIndex = id + 1
                button.label:SetText(("Bag[%s] - %s"):format(button.bagIndex, GetBagName(id)))
                button:Show()
            end
        end
        for index = index + 1, #bagSelectButtons do
            bagSelectButtons[index]:Hide()
        end
    end

    ConfigurationPanel:SetScript("OnEvent", UpdateBagSelectButtons)

    ConfigurationPanel:SetScript("OnHide", function(self)
        self:UnregisterEvent("BAG_UPDATE")
    end)

    ConfigurationPanel:SetScript("OnShow", function(self)
        self:RegisterEvent("BAG_UPDATE")
        UpdateBagSelectButtons()
    end)

end
If you replace the UpdateBagSelectButtons function with this then all the buttons will remain showing but just enable/disable as needed (along with updating their labels):
Code:
    local function UpdateBagSelectButtons()
        for id = 0, 4 do
            local button = bagSelectButtons[id + 1]
            if GetContainerNumSlots(id) > 0 then
                button.label:SetText(("Bag[%s] - %s"):format(button.bagIndex, GetBagName(id)))
                button:SetEnabled(true)
            else
                button.label:SetText(("Bag[%s]"):format(button.bagIndex))
                button:SetEnabled(false)
            end
        end
    end
Caveat: Code untested and may contain typos
  Reply With Quote