Thread Tools Display Modes
09-21-24, 05:55 AM   #1
liamnap
A Murloc Raider
Join Date: Aug 2024
Posts: 9
Saving Minimap Position

hi all,

I'm about to release my first addon but I have a little minimap icon issue. It appears and exists, however it never seems to save when I move it to a new position and I'm not sure why.

Thank you to everyone that takes the time to read this and help out.

1. I haven't been able to get LibStub installed as part of my addon, tried on PTR with no luck, so it's kinda relying on this existing from other addons I think - does anyone have a guide on how to integrate this correctly?
2. The minimap move not saving so it can be recalled on logon/reload etc, I cannot figure this out, it may be linked to point 1?

TOC
Code:
## Title: RatedStats
## Version: 1.0
## Author: liamp
## Interface: 110000
## DefaultState: enabled
## SavedVariables: RSTATS_Database

## OptionalDeps: LibDataBroker-1.1, LibDBIcon-1.0

config.lua
minimap.lua
init.lua
minimap
Code:
local _, RSTATS = ... -- Use the existing namespace
local playerName = UnitName("player") .. "-" .. GetRealmName()

-- Ensure that the character-specific database exists and is properly formatted for LibDBIcon
RSTATS.Database = RSTATS_Database or {} -- Global saved variable
RSTATS.Database[playerName] = RSTATS.Database[playerName] or {}

-- Ensure that the minimap table follows LibDBIcon's format
RSTATS.Database[playerName].minimap = RSTATS.Database[playerName].minimap or { hide = false, minimapPos = 220 }

local LDB = LibStub("LibDataBroker-1.1"):NewDataObject("RatedStats", {
    type = "data source",
    text = "RatedStats",
    icon = "Interface\\AddOns\\RatedStats\\RatedStats.tga", -- Path to your icon
    OnClick = function(_, button)
        if button == "LeftButton" then
            RSTATS.Config:Toggle() -- Toggle the config menu on left-click
        elseif button == "RightButton" then
            print("Right-click: Save position triggered.") -- Debug message for testing
        end
    end,
    OnTooltipShow = function(tooltip)
        tooltip:AddLine("RatedStats")
        tooltip:AddLine("Left-click to open the config.")
        tooltip:AddLine("Right-click to save minimap position.")
    end,
})

local icon = LibStub("LibDBIcon-1.0")

-- Function to debug and save the new minimap position
local function SaveMinimapPosition()
    -- Since LibDBIcon automatically saves the position, we're only using this for debug purposes
    local newpos = RSTATS.Database[playerName].minimap.minimapPos
    if newpos then
        print("New minimap position saved:", newpos)
    else
        print("Error: newpos is nil.")
    end
end

-- Initialize and register the minimap icon with LibDBIcon
function RSTATS:InitializeMinimapIcon()
    -- Register the icon using the character-specific minimap position from RSTATS.Database
    if not icon:IsRegistered("RatedStats") then
        print("Registering RatedStats minimap icon.")
        -- Register the minimap icon with LibDBIcon
        icon:Register("RatedStats", LDB, RSTATS.Database[playerName].minimap)
        print("Minimap icon registered with position:", RSTATS.Database[playerName].minimap.minimapPos)
    else
        print("Minimap icon is already registered.")
    end

    -- Hook into the drag stop event for debugging and position updating
    local minimapButton = icon:GetMinimapButton("RatedStats")
    if minimapButton then
        minimapButton:HookScript("OnDragStop", function(self)
            print("Minimap icon dragged to new position.")
            -- Since LibDBIcon should handle saving, just log the new position
            SaveMinimapPosition()
        end)
    end
end

RSTATS:InitializeMinimapIcon()
  Reply With Quote
09-21-24, 08:21 AM   #2
Xrystal
nUI Maintainer
 
Xrystal's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 6,007
This was part of a test project I set up for someone else who had a similar issue. You should be able to easily grab out what you need from it. This is just the minimap button code file.

Lua Code:
  1. local addonName, addon = ...
  2.  
  3. -- Main Addon SavedVariable Table
  4. TestAddonSettingsData = TestAddonSettingsData or {}
  5.  
  6. -- Minimap Icon SavedVariable Table
  7. TestAddonSettingsData_MinimapIcon = TestAddonSettingsData_MinimapIcon or {}
  8.  
  9. -- Initialise Minimap Icon values
  10. -- Use settings panel to allow player to change these
  11. -- When you move the minimap icon around the minimap the ldb library updates the respective MinimapIcon saved variable table
  12. -- The MinimapIcon Saved Variable Data has to be its own table, not a sub table
  13. TestAddonSettingsData_MinimapIcon.hide = false
  14. TestAddonSettingsData_MinimapIcon.lock = false            
  15. TestAddonSettingsData_MinimapIcon.minimapPos = 120
  16. TestAddonSettingsData_MinimapIcon.radius = 80
  17.  
  18. -- Make sure library exists
  19. local ldb = LibStub:GetLibrary("LibDataBroker-1.1", true)
  20. if not ldb then return end
  21.  
  22. -- Create Plugin
  23. local plugin = ldb:NewDataObject(addonName, {
  24.     type = "data source",
  25.     text = addonName,
  26.     icon = "Interface\\BUTTONS\\UI-GroupLoot-Dice-Up", -- Replace with path to your icon
  27. })
  28.  
  29. -- Handle OnClick functionality
  30. function plugin.OnClick(self, button)
  31.     if button == "LeftButton" then
  32.         Settings.OpenToCategory(addon.category:GetID()) -- Open or close the options panel
  33.     end
  34. end
  35.  
  36. -- Handle OnTooltipShow functionality
  37. function plugin.OnTooltipShow(tooltip)
  38.     tooltip:SetText(addonName)
  39.     tooltip:AddLine("Click to open options")
  40.     tooltip:Show()
  41. end
  42.    
  43. -- Initialise Minimap Icon on Player Login    
  44. local f = CreateFrame("Frame")
  45. f:SetScript("OnEvent", function()
  46.     local icon = LibStub("LibDBIcon-1.0", true)
  47.     if not icon then return end
  48.     icon:Register(addonName, plugin, TestAddonSettingsData_MinimapIcon)
  49. end)
  50. f:RegisterEvent("PLAYER_LOGIN")
__________________


All Level 70 Characters:
Demon Warlock
Resto Druid
Disc Priest
Resto Shaman
Survival Hunter
Augment Evoker
Frost Mage
Vengence Demon Hunter
Rogue ( was subtlety )

Brewmaster Monk (TR)
Prot Paladin (TR)
Blood Death Knight ( TR)

As you can see I am missing a warrior

And .. I don't have all the allied races covered. Time Runner time when it happens again

  Reply With Quote

WoWInterface » AddOns, Compilations, Macros » AddOn Help/Support » Saving Minimap Position


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