Sample Functions

From ESOUI Wiki

Jump to: navigation, search

Contents

Sample Functions

Feel free to put any useful functions you've found or created here. Please make them readable, or comment them so users can understand what's happening.

UI Functions

-- This function takes in the control you wish to add a toggle to, two textures for ON and OFF states, and an optional initial value.
-- control:toggle() is a new function added to the control, that can be called to change the toggle.
-- GetToggle() and SetToggle could also be added, if required, using similar function construction
local function createToggle(control, onTexture, offTexture, initialToggleValue)
	control.toggleValue = initialToggleValue or true
	function control:toggle()
		if self.toggleValue then
			self.toggleValue = false
			self:SetNormalTexture(offTexture)
		else
			self.toggleValue = true
			self:SetNormalTexture(onTexture)
		end
	end
end

General Helper Functions

get item id from link

-- via pattern matching. By calling tostring(itemLink) we make sure that we don't get an error if the itemLink is nil - feel free to try it 
function GetItemIDFromLink(itemLink)    
    return tonumber((tostring(itemLink):match("|H%d:item:(%d+)") or -1))
end

check an addon version

-- check for an addon version. Since addonVersionOptions is stored as string, we need to pattern match.
-- addOnTable:    The name of the global AddOn object (for example: InventoryInsight)
-- addOnVersion:  numeric, the version you want to check against
-- groupPosition: numeric, 1, 2 or 3 - first, second or third group of digits in the addon's version number
function checkAddonVersion(addOnTable, addOnVersion, groupPosition)
    groupPosition = groupPosition or 1
    local regexTable = {[1] = "%d+", [2] = "%p%d+", [3] = "%d+$"}
    local addonVersionNumber = tonumber(string.match(addOnTable.addonVars.addonVersionOptions, regexTable[groupPosition]):match("%d+"))
    return (addonVersionNumber >= addOnVersion) 
end

Other Useful Functions

Personal tools
Namespaces
Variants
Actions
Menu
Wiki
Toolbox