View Single Post
08-10-23, 11:48 PM   #1
Sharpedge
A Theradrim Guardian
 
Sharpedge's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2022
Posts: 66
Having issues with AceDB, I think...

I have a addon called IncCallout. It is a complete rewrite of Incoming-BG. At curse the addon name is Incoming-BG, i took it over last year.

Here is the issues I have been facing. Just recently I have added the options to change the opacity, the button font color and the button color. Its all good except for the button color. I can change the button color, (sometimes it will not change) but on a reload it reverts back to default red. If I reload a few times it will actually change to the color i had picked. But, after another reload it will go back to red.

Before I added all the my addon worked just fine. MiniMap Icon position was saved as well as the GUI position was saved, as well as any messages players picked in options. I'm at my wits end on this and need some new eyes on it to see where I messed up. The code below is not in my Github yet as I want it correct before I update my addon. You guys are alot better than I with LUA. Can someone take a look and tell me what I did wrong, and how to correct it?



Code:
-- IncCallout (Rebuild of Incoming-BG)
-- Made by Sharpedge_Gaming
-- v2.0 - 10.1.5

-- Load embedded libraries
local LibStub = LibStub or _G.LibStub
local AceDB = LibStub("AceDB-3.0")
local AceAddon = LibStub("AceAddon-3.0")
local AceConfig = LibStub("AceConfig-3.0")
local AceConfigDialog = LibStub("AceConfigDialog-3.0")
local icon = LibStub("LibDBIcon-1.0")
local LDB = LibStub("LibDataBroker-1.1")

local addonName, addonNamespace = ...


-- Initialize your addon using AceAddon-3.0
local addon = AceAddon:NewAddon(addonName)


local defaults = {
    profile = {
        buttonColor = {1, 0, 0, 1},  -- Default to red
        fontColor = {1, 1, 1, 1},  -- Default to white
        opacity = 1,  -- Default to fully opaque
    },
}

local db = AceDB:New("IncDB", defaults, "Default")

local AceDB = LibStub("AceDB-3.0")
IncDB = AceDB:New(addonName.."DB", defaults, true)

local buttonTexts = {}
local buttons = {}

local playerFaction
playerFaction = UnitFactionGroup("player")

local buttonMessageIndices = {
    sendMore = 1,
    inc = 1,
    allClear = 1
}

-- Define the battleground locations
local battlegroundLocations = {
    "Stables", "Blacksmith", "Lumber Mill", "Gold Mine", "Mine", "Trollbane Hall", "Defiler's Den", "Farm",
    "Mage Tower", "Draenei Ruins", "Blood Elf Tower", "Fel Reaver Ruins", 
    "The Broken Temple", "Cauldron of Flames", "Central Bridge", "The Chilled Quagemire",
    "Eastern Bridge", "Flamewatch Tower", "The Forest of Shadows", "Glacial Falls", "The Steppe of Life",
    "The Sunken Ring", "Western Bridge", "Winter's Edge Tower", "Wintergrasp Fortress", "Eastpark Workshop", "Westpark Workshop ",
    "Lighthouse", "Waterworks", "Mines", "Docks", "Workshop", "Horde Keep", "Alliance Keep", "Market",
    "Hangar", "Refinery", "Quarry", "Wildhammer Stronghold", "Dragonmaw Stronghold",
    "Silverwing Hold", "Warsong Flag Room", "Baradin Base Camp", "Rustberg Village",
    "The Restless Front", "Wellson Shipyard", "Largo's Overlook", "Farson Hold",
    "Forgotten Hill", "Hellscream's Grasp","Stormpike Graveyard", "Irondeep Mine", "Dun Baldar",
    "Hall of the Stormpike", "Icewing Pass", "Stonehearth Outpost", "Iceblood Graveyard", 
    "Iceblood Garrison", "Tower Point", "Coldtooth Mine", "Dun Baldar Pass", "Icewing Bunker",
    "Field of Strife", "Stonehearth Graveyard", "Stonehearth Bunker", "Frost Dagger Pass", 
    "Snowfall Graveyard", "Winterax Hold", "Frostwolf Graveyard", "Frostwolf Village", 
    "Deepwind Gorge", "Frostwolf Keep", "Hall of the Frostwolf","Temple of Kotmogu",  "Silvershard Mines", "Southshore vs. Tauren Mill", "Alterac Valley",
    "Ashran", "StormShield",	
}

local buttonMessages = {
    sendMore = {
        "We need more peeps",
        "Need help",
        "We are outnumbered",
		"Need a few more",
		"Need more",
		"Backup required",
		"We could use some help",
		"Calling for backup",
		"Could use some backup",
		"Reinforcements needed",
		"In need of additional support",
        "Calling all hands on deck",
        "Require extra manpower",
        "Assistance urgently needed",
        "Requesting more participants",
        -- Add more custom messages if needed...
    },
    inc = {
        "Incoming",
        "INC INC INC",
        "INC",
		"Gotta INC",
		"BIG INC",
		"Incoming enemy forces",
		"Incoming threat",
		"Enemy push incoming",
		"Enemy blitz incoming",
		"Enemy strike team inbound",
		"Incoming attack alert",
		"Enemy wave inbound",
		"Enemy squad closing in",
		"Anticipate enemy push",
		"Enemy forces are closing in",
        -- Add more custom messages if needed...
    },
    allClear = {
        "We are all clear",
        "All clear",
        "Looks like a ghost town",
		"All good",
		"Looking good",
		"Area secure",
		"All quiet on the front",
		"Situation is under control",
		"All quiet here",
		"We are looking good",
		"Perimeter is secured",
        "Situation is calm",
        "No threats detected",
        "All quiet on this end",
        "Area is threat-free",
        -- Add more custom messages if needed...
    },
}

-- Create the main frame
local IncCallout = CreateFrame("Frame", "IncCalloutMainFrame", UIParent, "BackdropTemplate")
IncCallout:SetSize(160, 155)
IncCallout:SetPoint("CENTER")

-- Create a background texture for the main frame
local bgTexture = IncCallout:CreateTexture(nil, "BACKGROUND")
bgTexture:SetColorTexture(0, 0, 0)
bgTexture:SetAllPoints(IncCallout)

-- Create a border frame
local BorderFrame = CreateFrame("Frame", nil, IncCallout, "BackdropTemplate")
BorderFrame:SetFrameLevel(IncCallout:GetFrameLevel() - 1) -- Ensure it's behind the main frame
BorderFrame:SetSize(IncCallout:GetWidth() + 4, IncCallout:GetHeight() + 4) -- Adjust these values for border thickness
BorderFrame:SetPoint("CENTER", IncCallout, "CENTER")

-- Create a border texture for the border frame
local borderTexture = BorderFrame:CreateTexture(nil, "OVERLAY")
borderTexture:SetColorTexture(1, 1, 1)
borderTexture:SetAllPoints(BorderFrame)

IncCallout:SetMovable(true)
IncCallout:EnableMouse(true)
IncCallout:RegisterForDrag("LeftButton")
IncCallout:SetScript("OnDragStart", IncCallout.StartMoving)
IncCallout:SetScript("OnDragStop", IncCallout.StopMovingOrSizing)

local options = {
    name = "IncCallout",
    type = "group",
    args = {
        sendMore = {
            type = "select",
            name = "Send More message",
            desc = "Select the message for the 'Send More' button",
            values = buttonMessages.sendMore,
            get = function() return buttonMessageIndices.sendMore end,
            set = function(_, newValue) buttonMessageIndices.sendMore = newValue end,
            order = 1,
        },
        inc = {
            type = "select",
            name = "INC message",
            desc = "Select the message for the 'INC' button",
            values = buttonMessages.inc,
            get = function() return buttonMessageIndices.inc end,
            set = function(_, newValue) buttonMessageIndices.inc = newValue end,
            order = 2,
        },
        allClear = {
            type = "select",
            name = "All Clear message",
            desc = "Select the message for the 'All Clear' button",
            values = buttonMessages.allClear,
            get = function() return buttonMessageIndices.allClear end,
            set = function(_, newValue) buttonMessageIndices.allClear = newValue end,
            order = 3,
        },
        opacity = {
            type = "range",
            name = "Opacity",
            desc = "Adjust the transparency of the IncCallout frame.",
            min = 0, max = 1, step = 0.05,
            get = function() return IncDB.opacity or 1 end,
            set = function(_, newValue)
                bgTexture:SetAlpha(newValue)
                borderTexture:SetAlpha(newValue)
                IncDB.opacity = newValue
            end,
            order = 4,
        },
        fontColor = {
            type = "color",
            name = "Button Font Color",
            desc = "Set the color of the button text.",
            hasAlpha = true,
            get = function()
                return unpack(IncDB.fontColor or {1, 1, 1, 1}) -- Default to white
            end,
            set = function(_, r, g, b, a)
                for _, text in ipairs(buttonTexts) do text:SetTextColor(r, g, b, a) end
                IncDB.fontColor = {r, g, b, a}
            end,
            order = 5,
        },
        buttonColor = {
            type = "color",
            name = "Button Color",
            desc = "Set the color of the buttons.",
            hasAlpha = true,
            get = function()
                local color = IncDB.buttonColor or {1, 0, 0, 1} -- Default to red
                return unpack(color)
            end,
            set = function(_, r, g, b, a)
                for _, button in ipairs(buttons) do button:SetBackdropColor(r, g, b, a) end
                IncDB.buttonColor = {r, g, b, a}
            end,
            order = 6,
        },
    },
}

-- Register the options table
AceConfig:RegisterOptionsTable(addonName, options)

-- Create a config panel
local configPanel = AceConfigDialog:AddToBlizOptions(addonName, "IncCallout")
configPanel.default = function()
    -- Reset the options to default values
    buttonMessageIndices.sendMore = 1
    buttonMessageIndices.inc = 1
    buttonMessageIndices.allClear = 1
end

-- Create a table to map each location to itself
local locationTable = {}
for _, location in ipairs(battlegroundLocations) do
    locationTable[location] = location
end

local function isInBattleground()
    local inInstance, instanceType = IsInInstance()
    return inInstance and (instanceType == "pvp" or instanceType == "arena")
end

local function ButtonOnClick(self)
    if not isInBattleground() then
        print("You are not in a battleground.")
        return
    end

    local currentLocation = GetSubZoneText()
    local playerFaction = UnitFactionGroup("player") or ""
    local enemyFaction = playerFaction == "Alliance" and "Horde" or "Alliance"
    local message = self:GetText() .. " " .. enemyFaction .. " incoming at " .. currentLocation
    SendChatMessage(message, "INSTANCE_CHAT")
end

-- Register an event listener for when the player enters a new zone or subzone
local f = CreateFrame("Frame")
f:RegisterEvent("ZONE_CHANGED_NEW_AREA")

f:SetScript("OnEvent", function(self, event, ...)
    if event == "ZONE_CHANGED_NEW_AREA" then
        local currentLocation = GetRealZoneText() .. " - " .. GetSubZoneText()
        local location = locationTable[currentLocation]

        -- Check if location is in the defined battleground locations
        if location then
            IncCallout:Show()  -- Show the GUI
        else
            
        end
    end
end)

local fontSize = 15

-- Function to create a button
local function createButton(name, width, height, text, anchor, xOffset, yOffset, onClick)
    local button = CreateFrame("Button", nil, IncCallout, "UIPanelButtonTemplate, BackdropTemplate")
    button:SetSize(width, height)
    button:SetText(text)
    if type(anchor) == "table" then
        button:SetPoint(anchor[1], anchor[2], anchor[3], xOffset, yOffset)
    else
        button:SetPoint(anchor, xOffset, yOffset)
    end
    button:SetScript("OnClick", onClick)
    button:GetFontString():SetTextColor(1, 1, 1, 1)
    
    -- Create a backdrop for the button
    button:SetBackdrop({
        bgFile = "Interface/Tooltips/UI-Tooltip-Background",
        edgeFile = "Interface/Tooltips/UI-Tooltip-Border",
        tile = true,
        tileSize = 16,
        edgeSize = 16,
        insets = { left = 4, right = 4, top = 4, bottom = 4 }
    })
    
    -- Set the backdrop color
    button:SetBackdropColor(unpack(IncDB.buttonColor or {1, 0, 0, 1})) -- Set the button color
    
    table.insert(buttonTexts, button:GetFontString())
    table.insert(buttons, button) -- Add button to the list
    return button
end

-- Define a function to apply the button color
local function applyButtonColor()
    local color = IncDB.buttonColor or {1, 0, 0, 1}  -- Default to red
    for _, button in ipairs(buttons) do
        button:SetBackdropColor(unpack(color))
    end
end


applyButtonColor()


-- Create buttons
local button1 = createButton("button1", 20, 22, "1", {"TOPLEFT", IncCallout, "TOPLEFT"}, 15, -20, ButtonOnClick)

local button2 = createButton("button2", 20, 22, "2", {"LEFT", button1, "RIGHT"}, 3, 0, ButtonOnClick)

local button3 = createButton("button3", 20, 22, "3", {"LEFT", button2, "RIGHT"}, 3, 0, ButtonOnClick)

local button4 = createButton("button4", 20, 22, "4", {"LEFT", button3, "RIGHT"}, 3, 0, ButtonOnClick)

local buttonZerg = createButton("buttonZerg", 40, 22, "Zerg", {"LEFT", button4, "RIGHT"}, 3, 0, ButtonOnClick)

local incButton = createButton("incButton", 60, 22, "Inc", {"TOP", IncCallout, "TOP"}, 0, -45, ButtonOnClick)

local sendMoreButton = createButton("sendMoreButton", 80, 22, "Send More", {"TOP", incButton, "BOTTOM"}, 0, -5, SendMoreButtonOnClick)

local allClearButton = createButton("allClearButton", 60, 22, "All Clear", {"TOP", sendMoreButton, "BOTTOM"}, 0, -5, AllClearButtonOnClick)

local exitButton = createButton("exitButton", 60, 22, "Exit", {"TOP", allClearButton, "BOTTOM"}, 0, -5, function() IncCallout:Hide() end)

-- Initialize IncDB.minimap if it's not already initialized
if not IncDB.minimap then
    IncDB.minimap = {
        hide = false,
        minimapPos = 45, -- Default position angle (in degrees)
    }
end

-- Now that IncCallout is defined, you can create IncCalloutLDB
local IncCalloutLDB = LibStub("LibDataBroker-1.1"):NewDataObject("IncCallout", {
    type = "data source",
    text = "IncCallout",
    icon = "Interface\\AddOns\\IncCallout\\Icon\\INC.png",
    OnClick = function(_, button)
        if button == "LeftButton" then
            if IncCallout:IsShown() then
                IncCallout:Hide()
            else
                IncCallout:Show()
            end
        else
            InterfaceOptionsFrame_OpenToCategory("IncCallout")
            InterfaceOptionsFrame_OpenToCategory("IncCallout") -- Call it twice to ensure the correct category is selected
        end
    end,
    OnMouseDown = function(self, button)
        if button == "LeftButton" then
            IncCallout:StartMoving()
        end
    end,
    OnMouseUp = function(self, button)
        if button == "LeftButton" then
            IncCallout:StopMovingOrSizing()
            local point, _, _, x, y = IncCallout:GetPoint()
            local centerX, centerY = Minimap:GetCenter()
            local scale = Minimap:GetEffectiveScale()
            x, y = (x - centerX) / scale, (y - centerY) / scale
            IncDB.minimap.minimapPos = math.deg(math.atan2(y, x)) % 360
        end
    end,
    OnTooltipShow = function(tooltip)
        tooltip:AddLine("|cffff0000IncCallout|r")
        tooltip:AddLine("Left Click: GUI")
        tooltip:AddLine("Right Click: Options")
        tooltip:Show()
    end,
})

-- Function to handle the All Clear button click event
local function AllClearButtonOnClick()
    local location = GetSubZoneText()

    -- Check if location is in the defined battleground locations
    if not location then
    print("You are not in a BattleGround.")
    return
end

local message = buttonMessages.allClear[buttonMessageIndices.allClear] .. " at " .. location
SendChatMessage(message, "INSTANCE_CHAT")
end
allClearButton:SetScript("OnClick", AllClearButtonOnClick)

-- Function to handle the Send More button click event
local function SendMoreButtonOnClick()
    local location = GetSubZoneText()

    -- Check if location is in the defined battleground locations
    if not location then
    print("You are not in a BattleGround.")
    return
end

local message = buttonMessages.sendMore[buttonMessageIndices.sendMore] .. " at " .. location
SendChatMessage(message, "INSTANCE_CHAT")
end
sendMoreButton:SetScript("OnClick", SendMoreButtonOnClick)

-- Function to handle the INC button click event
local function IncButtonOnClick()
    local location = GetSubZoneText()

    -- Check if location is in the defined battleground locations
    if not location then
    print("You are not in a BattleGround.")
    return
end

local message = buttonMessages.inc[buttonMessageIndices.inc] .. " at " .. location
SendChatMessage(message, "INSTANCE_CHAT")
end
incButton:SetScript("OnClick", IncButtonOnClick)

-- Register the slash command
SLASH_INC1 = "/inc"
SlashCmdList["INC"] = function()
    if IncCallout:IsShown() then
        IncCallout:Hide()
    end
end

-- Function to handle player login, logout, and entering the world
local function OnEvent(self, event, ...)
    if event == "PLAYER_ENTERING_WORLD" then
        local inInstance, instanceType = IsInInstance()
        if inInstance and (instanceType == "pvp" or instanceType == "arena") then
            print("Don't forget to call INC's")
        else
            print("|cFFFF0000You need to queue up for PvP|r")
        end
        
        if inInstance and instanceType == "pvp" then
            IncCalloutMainFrame:Show() 
        end

    elseif event == "PLAYER_LOGIN" then
        -- Load saved button messages indices
        buttonMessageIndices.sendMore = IncDB.sendMoreIndex or 1
        buttonMessageIndices.inc = IncDB.incIndex or 1
        buttonMessageIndices.allClear = IncDB.allClearIndex or 1

        -- Load the opacity setting
        bgTexture:SetAlpha(IncDB.opacity or 1)
        borderTexture:SetAlpha(IncDB.opacity or 1)

        -- Load the font color
        local savedColor = IncDB.fontColor or {1, 1, 1, 1}  -- Default to white
        for _, text in ipairs(buttonTexts) do
            text:SetTextColor(unpack(savedColor))
            local color = IncDB.buttonColor or {1, 0, 0, 1}  -- Default to red
        end

        -- Load the font
        local defaultSize = 15  -- Default font size
        local font = IncDB.font or "Fonts\\ARIALN.ttf"  -- Default to Arial
        for _, text in ipairs(buttonTexts) do
            local _, size = text:GetFont()
            size = size or defaultSize
            text:SetFont(font, size)  -- Preserve the font size
        end

        -- Load the minimap icon settings
        icon:Register("IncCallout", IncCalloutLDB, IncDB.minimap)

        -- Apply the button color
        applyButtonColor() -- Call the function here
    elseif event == "PLAYER_LOGOUT" then
        -- Save button messages indices
        IncDB.sendMoreIndex = buttonMessageIndices.sendMore
        IncDB.incIndex = buttonMessageIndices.inc
        IncDB.allClearIndex = buttonMessageIndices.allClear

        -- Save the opacity setting
        IncDB.opacity = bgTexture:GetAlpha()

        -- Ensure all texts have the same color
        local firstTextColor = {buttonTexts[1]:GetTextColor()}
        for _, text in ipairs(buttonTexts) do
            text:SetTextColor(unpack(firstTextColor))
        end
        
        -- Save the font
        IncDB.font = buttonTexts[1]:GetFont()

        -- Save the button color
        IncDB.buttonColor = {buttons[1]:GetBackdropColor()}
    end
end

IncCallout:SetScript("OnEvent", OnEvent)

-- Register the events
IncCallout:RegisterEvent("PLAYER_ENTERING_WORLD")
IncCallout:RegisterEvent("PLAYER_LOGIN")
IncCallout:RegisterEvent("PLAYER_LOGOUT")
IncCallout:SetScript("OnEvent", OnEvent)
My addon at curse: Incoming-BG

I also works nights. So I will answer any questions once I get back to work tonight.
Attached Thumbnails
Click image for larger version

Name:	pic5.png
Views:	75
Size:	167.2 KB
ID:	9834  

Last edited by Sharpedge : 08-11-23 at 04:08 AM.
  Reply With Quote