diff --git a/env/api.lua b/env/api.lua index 7d4e4a6b..5498a7a5 100644 --- a/env/api.lua +++ b/env/api.lua @@ -21,6 +21,19 @@ function pfUI.api.strsplit(delimiter, subject) end end +-- [ strvertical ] +-- Creates vertical text using linebreaks. Multibyte char friendly. +-- 'str' [string] String to columnize. +-- return: [string] the string tranformed to a column. +function pfUI.api.strvertical(str) + local _, len = string.gsub(str,"[^\128-\193]", "") + if (len == string.len(str)) then + return string.gsub(str, "(.)", "%1\n") + else + return string.gsub(str,"([%z\1-\127\194-\244][\128-\191]*)", "%1\n") + end +end + -- [ round ] -- Rounds a float number into specified places after comma. -- 'input' [float] the number that should be rounded. @@ -320,3 +333,69 @@ function pfUI.api:CreateQuestionDialog(text, yes, no, editbox) local buttonspace = question.no:GetHeight() + padding question:SetHeight(textspace + inputspace + buttonspace + border) end + +-- [ Bar Layout Options ] -- +-- 'barsize' size of bar in number of buttons +-- returns: array of options as strings for pfUI.gui.bar +function pfUI.api:BarLayoutOptions(barsize) + assert(barsize > 0 and barsize <= NUM_ACTIONBAR_BUTTONS,"BarLayoutOptions: barsize "..tostring(barsize).." is invalid") + local options = {} + for i,layout in ipairs(pfGridmath[barsize]) do + options[i] = string.format("%d x %d",layout[1],layout[2]) + end + return options +end + +-- [ Bar Layout Formfactor ] -- +-- 'option' string option as used in pfUI_config.bars[bar].option +-- returns: integer formfactor +local formfactors = {} -- we'll use memoization so we only compute once, then lookup. +setmetatable(formfactors, {__mode = "v"}) -- weak table so values not referenced are collected on next gc +function pfUI.api:BarLayoutFormfactor(option) + if formfactors[option] then + return formfactors[option] + else + for barsize,_ in ipairs(pfGridmath) do + local options = pfUI.api:BarLayoutOptions(barsize) + for i,opt in ipairs(options) do + if opt == option then + formfactors[option] = i + return formfactors[option] + end + end + end + end +end + +-- [ Bar Layout Size ] -- +-- 'bar' frame reference, +-- 'barsize' integer number of buttons, +-- 'formfactor' string formfactor in cols x rows +function pfUI.api:BarLayoutSize(bar,barsize,formfactor,iconsize,bordersize) + assert(barsize > 0 and barsize <= NUM_ACTIONBAR_BUTTONS,"BarLayoutSize: barsize "..tostring(barsize).." is invalid") + local formfactor = pfUI.api:BarLayoutFormfactor(formfactor) + local cols, rows = unpack(pfGridmath[barsize][formfactor]) + local width = (iconsize + bordersize*3) * cols - bordersize + local height = (iconsize + bordersize*3) * rows - bordersize + bar._size = {width,height} + return bar._size +end + +-- [ Bar Button Anchor ] -- +-- 'button' frame reference +-- 'basename' name of button frame without index +-- 'buttonindex' index number of button on bar +-- 'formfactor' string formfactor in cols x rows +function pfUI.api:BarButtonAnchor(button,basename,buttonindex,barsize,formfactor,iconsize,bordersize) + assert(barsize > 0 and barsize <= NUM_ACTIONBAR_BUTTONS,"BarButtonAnchor: barsize "..tostring(barsize).." is invalid") + local formfactor = pfUI.api:BarLayoutFormfactor(formfactor) + local parent = button:GetParent() + local cols, rows = unpack(pfGridmath[barsize][formfactor]) + if buttonindex == 1 then + button._anchor = {"TOPLEFT", parent, "TOPLEFT", bordersize, -bordersize} + else + local col = buttonindex-((math.ceil(buttonindex/cols)-1)*cols) + button._anchor = col==1 and {"TOP",getglobal(basename..(buttonindex-cols)),"BOTTOM",0,-(bordersize*3)} or {"LEFT",getglobal(basename..(buttonindex-1)),"RIGHT",(bordersize*3),0} + end + return button._anchor +end \ No newline at end of file