View Single Post
09-29-18, 09:44 AM   #2
Vrul
A Scalebane Royal Guard
 
Vrul's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2007
Posts: 404
Originally Posted by Lyak View Post
The question is, is it possible to re-use those General, Health and Power tabs, but only change the db entry based on the selected unit?
Yes, and the ease of which is determined by how your db table is set up. Here is a generic example of how it could look:
Code:
local ACR = LibStub("AceConfigRegistry-3.0")
local ACD = LibStub("AceConfigDialog-3.0")
local LSM = LibStub("LibSharedMedia-3.0")

local anchorList = {
	TOPLEFT = "TOPLEFT",
	TOPRIGHT = "TOPRIGHT",
	BOTTOMRIGHT = "BOTTOMRIGHT",
	BOTTOMLEFT = "BOTTOMLEFT",
	LEFT = "LEFT",
	TOP = "TOP",
	RIGHT = "RIGHT",
	BOTTOM = "BOTTOM",
	CENTER = "CENTER"
}

local unitFrame = {
	Player = PlayerFrame,
	Pet = PetFrame,
	Target = TargetFrame,
	TargetTarget = TargetFrameToT,
	Focus = FocusFrame
}

local unitList = { }
for unit in pairs(unitFrame) do
	unitList[unit] = unit
end

local db = { }
for unit in pairs(unitFrame) do
	db[unit] = { }
end

local selectedUnit = "Player"

local function GetOption(info)
	return db[selectedUnit][info[#info]]
end

local function SetOption(info, value)
	db[selectedUnit][info[#info]] = value
end

local function UpdatePosition(unit)
	local frame = unitFrame[unit]
	frame:ClearAllPoints()
	local settings = db[unit]
	frame:SetPoint(settings.point or "CENTER", settings.relativeTo or UIParent, settings.relativePoint or settings.point or "CENTER", tonumber(settings.offsetX) or 0, tonumber(settings.offsetY) or 0)
end

local function UpdateStatusBar(unit, barKey)
	local settings = db[unit]
	local statusBar = unitFrame[unit][barKey]
	statusBar:SetHeight(tonumber(settings[barKey .. "Height"]) or 12)
	statusBar:SetStatusBarTexture(LSM:Fetch("statusbar", settings[barKey .. "Texture"]))
end

local function SetStatusBarOption(info, value)
	SetOption(info, value)
	UpdateStatusBar(selectedUnit, info[#info - 1])
end

local testOption = {
    type = "group", 
    childGroups = "tab",
    name = "TestOption",
	get = GetOption,
    args = {
        selected = {
            order = 1,
            type = "select",
            name = "Selected Unit",
            values = unitList,
            get = function(info)
                return selectedUnit
            end,
            set = function(info, value)
                selectedUnit = value
            end,
        },
        general = {
            order = 2,
            type = "group",
            name = "General",
			set = function(info, value)
				SetOption(info, value)
				UpdatePosition(selectedUnit)
			end,
            args = {
                point = {
                    order = 1,
                    type = "select",
                    name = "Point",
                    values = anchorList,
                },
                relativeTo = {
                    order = 2,
                    type = "input",
                    name = "Relative To",
                },
                relativePoint = {
                    order = 3,
                    type = "select",
                    name = "Relative Point",
                    values = anchorList,
                },
                offsetX = {
                    order = 4,
                    type = "input",
                    name = "X-Offset",
                },
                offsetY = {
                    order = 5,
                    type = "input",
                    name = "Y-Offset",
                },
            },
        },
        healthbar = {
            order = 3,
            type = "group",
            name = "Health",
			set = SetStatusBarOption,
            args = {
                healthbarHeight = {
                    order = 1,
                    type = "range",
                    name = "Height",
                    min = 4,
                    max = 32,
                    step = 1,
                },
                healthbarTexture = {
                    order = 2,
                    type = "select",
                    name = "Texture",
                    dialogControl = "LSM30_Statusbar",
                    values = AceGUIWidgetLSMlists.statusbar,
                },
            },
        },
        manabar = {
            order = 4,
            type = "group",
            name = "Power",
			set = SetStatusBarOption,
            args = {
                manabarHeight = {
                    order = 1,
                    type = "range",
                    name = "Height",
                    min = 4,
                    max = 32,
                    step = 1,
                },
                manabarTexture = {
                    order = 2,
                    type = "select",
                    name = "Texture",
                    dialogControl = "LSM30_Statusbar",
                    values = AceGUIWidgetLSMlists.statusbar,
                },
            },
        },
    },
}

ACR:RegisterOptionsTable("TestOption", testOption)
ACD:AddToBlizOptions("TestOption", nil, nil)
  Reply With Quote