Skip to content
Adrian L Lange edited this page Jul 7, 2024 · 6 revisions

Please browse the pages in the section to the right to see the available API, most of it is covered here

Embedding

Please see the embedding page on how to include this library (and its dependencies) with your addon.

Example with a button:

local button = CreateFrame('Button', 'MyButton', UIParent)
button:SetSize(50, 50)
local texture = button:CreateTexture()
texture:SetAllPoints()
texture:SetColorTexture(0, 0, 1)

local defaultPosition = {
	point = 'CENTER',
	x = 0,
	y = 0,
}

local function onPositionChanged(frame, layoutName, point, x, y)
	-- from here you can save the position into a savedvariable
	MyButtonDB[layoutName].point = point
	MyButtonDB[layoutName].x = x
	MyButtonDB[layoutName].y = y
end

local LEM = LibStub('LibEditMode')
LEM:AddFrame(button, onPositionChanged, defaultPosition)

-- additional (anonymous) callbacks
LEM:RegisterCallback('enter', function()
	-- from here you can show your button if it was hidden
end)
LEM:RegisterCallback('exit', function()
	-- from here you can hide your button if it's supposed to be hidden
end)
LEM:RegisterCallback('layout', function(layoutName)
	-- this will be called every time the Edit Mode layout is changed (which also happens at login),
	-- use it to load the saved button position from savedvariables and position it
	if not MyButtonDB then
		MyButtonDB = {}
	end
	if not MyButtonDB[layoutName] then
		MyButtonDB[layoutName] = CopyTable(defaultPosition)
	end

	button:ClearAllPoints()
	button:SetPoint(MyButtonDB[layoutName].point, MyButtonDB[layoutName].x, MyButtonDB[layoutName].y)
end)

Adding extra settings for a button:

LEM:AddFrameSettings(button, {
	{
		name = 'Button scale',
		kind = LEM.SettingType.Slider,
		default = 1,
		get = function(layoutName)
			return MyButtonDB[layoutName].scale
		end,
		set = function(layoutName, value)
			MyButtonDB[layoutName].scale = value
			button:SetScale(value)
		end,
		minValue = 0.1,
		maxValue = 5,
		valueStep = 0.1,
		formatter = function(value)
			return FormatPercentage(value, true)
		end,
	}
})
Clone this wiki locally