Asa@0: --- AceConfigDialog-3.0 generates AceGUI-3.0 based windows based on option tables. Asa@0: -- @class file Asa@0: -- @name AceConfigDialog-3.0 Asa@0: -- @release $Id: AceConfigDialog-3.0.lua 902 2009-12-12 14:56:14Z nevcairiel $ Asa@0: Asa@0: local LibStub = LibStub Asa@0: local MAJOR, MINOR = "AceConfigDialog-3.0", 43 Asa@0: local AceConfigDialog, oldminor = LibStub:NewLibrary(MAJOR, MINOR) Asa@0: Asa@0: if not AceConfigDialog then return end Asa@0: Asa@0: AceConfigDialog.OpenFrames = AceConfigDialog.OpenFrames or {} Asa@0: AceConfigDialog.Status = AceConfigDialog.Status or {} Asa@0: AceConfigDialog.frame = AceConfigDialog.frame or CreateFrame("Frame") Asa@0: Asa@0: AceConfigDialog.frame.apps = AceConfigDialog.frame.apps or {} Asa@0: AceConfigDialog.frame.closing = AceConfigDialog.frame.closing or {} Asa@0: Asa@0: local gui = LibStub("AceGUI-3.0") Asa@0: local reg = LibStub("AceConfigRegistry-3.0") Asa@0: Asa@0: -- Lua APIs Asa@0: local tconcat, tinsert, tsort, tremove = table.concat, table.insert, table.sort, table.remove Asa@0: local strmatch, format = string.match, string.format Asa@0: local assert, loadstring, error = assert, loadstring, error Asa@0: local pairs, next, select, type, unpack = pairs, next, select, type, unpack Asa@0: local rawset, tostring = rawset, tostring Asa@0: local math_min, math_max, math_floor = math.min, math.max, math.floor Asa@0: Asa@0: -- Global vars/functions that we don't upvalue since they might get hooked, or upgraded Asa@0: -- List them here for Mikk's FindGlobals script Asa@0: -- GLOBALS: NORMAL_FONT_COLOR, GameTooltip, StaticPopupDialogs, ACCEPT, CANCEL, StaticPopup_Show Asa@0: -- GLOBALS: PlaySound, GameFontHighlight, GameFontHighlightSmall, GameFontHighlightLarge Asa@0: -- GLOBALS: CloseSpecialWindows, InterfaceOptions_AddCategory, geterrorhandler Asa@0: Asa@0: local emptyTbl = {} Asa@0: Asa@0: --[[ Asa@0: xpcall safecall implementation Asa@0: ]] Asa@0: local xpcall = xpcall Asa@0: Asa@0: local function errorhandler(err) Asa@0: return geterrorhandler()(err) Asa@0: end Asa@0: Asa@0: local function CreateDispatcher(argCount) Asa@0: local code = [[ Asa@0: local xpcall, eh = ... Asa@0: local method, ARGS Asa@0: local function call() return method(ARGS) end Asa@0: Asa@0: local function dispatch(func, ...) Asa@0: method = func Asa@0: if not method then return end Asa@0: ARGS = ... Asa@0: return xpcall(call, eh) Asa@0: end Asa@0: Asa@0: return dispatch Asa@0: ]] Asa@0: Asa@0: local ARGS = {} Asa@0: for i = 1, argCount do ARGS[i] = "arg"..i end Asa@0: code = code:gsub("ARGS", tconcat(ARGS, ", ")) Asa@0: return assert(loadstring(code, "safecall Dispatcher["..argCount.."]"))(xpcall, errorhandler) Asa@0: end Asa@0: Asa@0: local Dispatchers = setmetatable({}, {__index=function(self, argCount) Asa@0: local dispatcher = CreateDispatcher(argCount) Asa@0: rawset(self, argCount, dispatcher) Asa@0: return dispatcher Asa@0: end}) Asa@0: Dispatchers[0] = function(func) Asa@0: return xpcall(func, errorhandler) Asa@0: end Asa@0: Asa@0: local function safecall(func, ...) Asa@0: return Dispatchers[select('#', ...)](func, ...) Asa@0: end Asa@0: Asa@0: local width_multiplier = 170 Asa@0: Asa@0: --[[ Asa@0: Group Types Asa@0: Tree - All Descendant Groups will all become nodes on the tree, direct child options will appear above the tree Asa@0: - Descendant Groups with inline=true and thier children will not become nodes Asa@0: Asa@0: Tab - Direct Child Groups will become tabs, direct child options will appear above the tab control Asa@0: - Grandchild groups will default to inline unless specified otherwise Asa@0: Asa@0: Select- Same as Tab but with entries in a dropdown rather than tabs Asa@0: Asa@0: Asa@0: Inline Groups Asa@0: - Will not become nodes of a select group, they will be effectivly part of thier parent group seperated by a border Asa@0: - If declared on a direct child of a root node of a select group, they will appear above the group container control Asa@0: - When a group is displayed inline, all descendants will also be inline members of the group Asa@0: Asa@0: ]] Asa@0: Asa@0: -- Recycling functions Asa@0: local new, del, copy Asa@0: --newcount, delcount,createdcount,cached = 0,0,0 Asa@0: do Asa@0: local pool = setmetatable({},{__mode='k'}) Asa@0: function new() Asa@0: --newcount = newcount + 1 Asa@0: local t = next(pool) Asa@0: if t then Asa@0: pool[t] = nil Asa@0: return t Asa@0: else Asa@0: --createdcount = createdcount + 1 Asa@0: return {} Asa@0: end Asa@0: end Asa@0: function copy(t) Asa@0: local c = new() Asa@0: for k, v in pairs(t) do Asa@0: c[k] = v Asa@0: end Asa@0: return c Asa@0: end Asa@0: function del(t) Asa@0: --delcount = delcount + 1 Asa@0: for k in pairs(t) do Asa@0: t[k] = nil Asa@0: end Asa@0: pool[t] = true Asa@0: end Asa@0: -- function cached() Asa@0: -- local n = 0 Asa@0: -- for k in pairs(pool) do Asa@0: -- n = n + 1 Asa@0: -- end Asa@0: -- return n Asa@0: -- end Asa@0: end Asa@0: Asa@0: -- picks the first non-nil value and returns it Asa@0: local function pickfirstset(...) Asa@0: for i=1,select("#",...) do Asa@0: if select(i,...)~=nil then Asa@0: return select(i,...) Asa@0: end Asa@0: end Asa@0: end Asa@0: Asa@0: --gets an option from a given group, checking plugins Asa@0: local function GetSubOption(group, key) Asa@0: if group.plugins then Asa@0: for plugin, t in pairs(group.plugins) do Asa@0: if t[key] then Asa@0: return t[key] Asa@0: end Asa@0: end Asa@0: end Asa@0: Asa@0: return group.args[key] Asa@0: end Asa@0: Asa@0: --Option member type definitions, used to decide how to access it Asa@0: Asa@0: --Is the member Inherited from parent options Asa@0: local isInherited = { Asa@0: set = true, Asa@0: get = true, Asa@0: func = true, Asa@0: confirm = true, Asa@0: validate = true, Asa@0: disabled = true, Asa@0: hidden = true Asa@0: } Asa@0: Asa@0: --Does a string type mean a literal value, instead of the default of a method of the handler Asa@0: local stringIsLiteral = { Asa@0: name = true, Asa@0: desc = true, Asa@0: icon = true, Asa@0: usage = true, Asa@0: width = true, Asa@0: image = true, Asa@0: fontSize = true, Asa@0: } Asa@0: Asa@0: --Is Never a function or method Asa@0: local allIsLiteral = { Asa@0: type = true, Asa@0: descStyle = true, Asa@0: imageWidth = true, Asa@0: imageHeight = true, Asa@0: } Asa@0: Asa@0: --gets the value for a member that could be a function Asa@0: --function refs are called with an info arg Asa@0: --every other type is returned Asa@0: local function GetOptionsMemberValue(membername, option, options, path, appName, ...) Asa@0: --get definition for the member Asa@0: local inherits = isInherited[membername] Asa@0: Asa@0: Asa@0: --get the member of the option, traversing the tree if it can be inherited Asa@0: local member Asa@0: Asa@0: if inherits then Asa@0: local group = options Asa@0: if group[membername] ~= nil then Asa@0: member = group[membername] Asa@0: end Asa@0: for i = 1, #path do Asa@0: group = GetSubOption(group, path[i]) Asa@0: if group[membername] ~= nil then Asa@0: member = group[membername] Asa@0: end Asa@0: end Asa@0: else Asa@0: member = option[membername] Asa@0: end Asa@0: Asa@0: --check if we need to call a functon, or if we have a literal value Asa@0: if ( not allIsLiteral[membername] ) and ( type(member) == "function" or ((not stringIsLiteral[membername]) and type(member) == "string") ) then Asa@0: --We have a function to call Asa@0: local info = new() Asa@0: --traverse the options table, picking up the handler and filling the info with the path Asa@0: local handler Asa@0: local group = options Asa@0: handler = group.handler or handler Asa@0: Asa@0: for i = 1, #path do Asa@0: group = GetSubOption(group, path[i]) Asa@0: info[i] = path[i] Asa@0: handler = group.handler or handler Asa@0: end Asa@0: Asa@0: info.options = options Asa@0: info.appName = appName Asa@0: info[0] = appName Asa@0: info.arg = option.arg Asa@0: info.handler = handler Asa@0: info.option = option Asa@0: info.type = option.type Asa@0: info.uiType = 'dialog' Asa@0: info.uiName = MAJOR Asa@0: Asa@0: local a, b, c ,d Asa@0: --using 4 returns for the get of a color type, increase if a type needs more Asa@0: if type(member) == "function" then Asa@0: --Call the function Asa@0: a,b,c,d = member(info, ...) Asa@0: else Asa@0: --Call the method Asa@0: if handler and handler[member] then Asa@0: a,b,c,d = handler[member](handler, info, ...) Asa@0: else Asa@0: error(format("Method %s doesn't exist in handler for type %s", member, membername)) Asa@0: end Asa@0: end Asa@0: del(info) Asa@0: return a,b,c,d Asa@0: else Asa@0: --The value isnt a function to call, return it Asa@0: return member Asa@0: end Asa@0: end Asa@0: Asa@0: --[[calls an options function that could be inherited, method name or function ref Asa@0: local function CallOptionsFunction(funcname ,option, options, path, appName, ...) Asa@0: local info = new() Asa@0: Asa@0: local func Asa@0: local group = options Asa@0: local handler Asa@0: Asa@0: --build the info table containing the path Asa@0: -- pick up functions while traversing the tree Asa@0: if group[funcname] ~= nil then Asa@0: func = group[funcname] Asa@0: end Asa@0: handler = group.handler or handler Asa@0: Asa@0: for i, v in ipairs(path) do Asa@0: group = GetSubOption(group, v) Asa@0: info[i] = v Asa@0: if group[funcname] ~= nil then Asa@0: func = group[funcname] Asa@0: end Asa@0: handler = group.handler or handler Asa@0: end Asa@0: Asa@0: info.options = options Asa@0: info[0] = appName Asa@0: info.arg = option.arg Asa@0: Asa@0: local a, b, c ,d Asa@0: if type(func) == "string" then Asa@0: if handler and handler[func] then Asa@0: a,b,c,d = handler[func](handler, info, ...) Asa@0: else Asa@0: error(string.format("Method %s doesn't exist in handler for type func", func)) Asa@0: end Asa@0: elseif type(func) == "function" then Asa@0: a,b,c,d = func(info, ...) Asa@0: end Asa@0: del(info) Asa@0: return a,b,c,d Asa@0: end Asa@0: --]] Asa@0: Asa@0: --tables to hold orders and names for options being sorted, will be created with new() Asa@0: --prevents needing to call functions repeatedly while sorting Asa@0: local tempOrders Asa@0: local tempNames Asa@0: Asa@0: local function compareOptions(a,b) Asa@0: if not a then Asa@0: return true Asa@0: end Asa@0: if not b then Asa@0: return false Asa@0: end Asa@0: local OrderA, OrderB = tempOrders[a] or 100, tempOrders[b] or 100 Asa@0: if OrderA == OrderB then Asa@0: local NameA = (type(tempNames[a] == "string") and tempNames[a]) or "" Asa@0: local NameB = (type(tempNames[b] == "string") and tempNames[b]) or "" Asa@0: return NameA:upper() < NameB:upper() Asa@0: end Asa@0: if OrderA < 0 then Asa@0: if OrderB > 0 then Asa@0: return false Asa@0: end Asa@0: else Asa@0: if OrderB < 0 then Asa@0: return true Asa@0: end Asa@0: end Asa@0: return OrderA < OrderB Asa@0: end Asa@0: Asa@0: Asa@0: Asa@0: --builds 2 tables out of an options group Asa@0: -- keySort, sorted keys Asa@0: -- opts, combined options from .plugins and args Asa@0: local function BuildSortedOptionsTable(group, keySort, opts, options, path, appName) Asa@0: tempOrders = new() Asa@0: tempNames = new() Asa@0: Asa@0: if group.plugins then Asa@0: for plugin, t in pairs(group.plugins) do Asa@0: for k, v in pairs(t) do Asa@0: if not opts[k] then Asa@0: tinsert(keySort, k) Asa@0: opts[k] = v Asa@0: Asa@0: path[#path+1] = k Asa@0: tempOrders[k] = GetOptionsMemberValue("order", v, options, path, appName) Asa@0: tempNames[k] = GetOptionsMemberValue("name", v, options, path, appName) Asa@0: path[#path] = nil Asa@0: end Asa@0: end Asa@0: end Asa@0: end Asa@0: Asa@0: for k, v in pairs(group.args) do Asa@0: if not opts[k] then Asa@0: tinsert(keySort, k) Asa@0: opts[k] = v Asa@0: Asa@0: path[#path+1] = k Asa@0: tempOrders[k] = GetOptionsMemberValue("order", v, options, path, appName) Asa@0: tempNames[k] = GetOptionsMemberValue("name", v, options, path, appName) Asa@0: path[#path] = nil Asa@0: end Asa@0: end Asa@0: Asa@0: tsort(keySort, compareOptions) Asa@0: Asa@0: del(tempOrders) Asa@0: del(tempNames) Asa@0: end Asa@0: Asa@0: local function DelTree(tree) Asa@0: if tree.children then Asa@0: local childs = tree.children Asa@0: for i = 1, #childs do Asa@0: DelTree(childs[i]) Asa@0: del(childs[i]) Asa@0: end Asa@0: del(childs) Asa@0: end Asa@0: end Asa@0: Asa@0: local function CleanUserData(widget, event) Asa@0: Asa@0: local user = widget:GetUserDataTable() Asa@0: Asa@0: if user.path then Asa@0: del(user.path) Asa@0: end Asa@0: Asa@0: if widget.type == "TreeGroup" then Asa@0: local tree = user.tree Asa@0: widget:SetTree(nil) Asa@0: if tree then Asa@0: for i = 1, #tree do Asa@0: DelTree(tree[i]) Asa@0: del(tree[i]) Asa@0: end Asa@0: del(tree) Asa@0: end Asa@0: end Asa@0: Asa@0: if widget.type == "TabGroup" then Asa@0: widget:SetTabs(nil) Asa@0: if user.tablist then Asa@0: del(user.tablist) Asa@0: end Asa@0: end Asa@0: Asa@0: if widget.type == "DropdownGroup" then Asa@0: widget:SetGroupList(nil) Asa@0: if user.grouplist then Asa@0: del(user.grouplist) Asa@0: end Asa@0: end Asa@0: end Asa@0: Asa@0: -- - Gets a status table for the given appname and options path. Asa@0: -- @param appName The application name as given to `:RegisterOptionsTable()` Asa@0: -- @param path The path to the options (a table with all group keys) Asa@0: -- @return Asa@0: function AceConfigDialog:GetStatusTable(appName, path) Asa@0: local status = self.Status Asa@0: Asa@0: if not status[appName] then Asa@0: status[appName] = {} Asa@0: status[appName].status = {} Asa@0: status[appName].children = {} Asa@0: end Asa@0: Asa@0: status = status[appName] Asa@0: Asa@0: if path then Asa@0: for i = 1, #path do Asa@0: local v = path[i] Asa@0: if not status.children[v] then Asa@0: status.children[v] = {} Asa@0: status.children[v].status = {} Asa@0: status.children[v].children = {} Asa@0: end Asa@0: status = status.children[v] Asa@0: end Asa@0: end Asa@0: Asa@0: return status.status Asa@0: end Asa@0: Asa@0: --- Selects the specified path in the options window. Asa@0: -- The path specified has to match the keys of the groups in the table. Asa@0: -- @param appName The application name as given to `:RegisterOptionsTable()` Asa@0: -- @param ... The path to the key that should be selected Asa@0: function AceConfigDialog:SelectGroup(appName, ...) Asa@0: local path = new() Asa@0: Asa@0: Asa@0: local app = reg:GetOptionsTable(appName) Asa@0: if not app then Asa@0: error(("%s isn't registed with AceConfigRegistry, unable to open config"):format(appName), 2) Asa@0: end Asa@0: local options = app("dialog", MAJOR) Asa@0: local group = options Asa@0: local status = self:GetStatusTable(appName, path) Asa@0: if not status.groups then Asa@0: status.groups = {} Asa@0: end Asa@0: status = status.groups Asa@0: local treevalue Asa@0: local treestatus Asa@0: Asa@0: for n = 1, select('#',...) do Asa@0: local key = select(n, ...) Asa@0: Asa@0: if group.childGroups == "tab" or group.childGroups == "select" then Asa@0: --if this is a tab or select group, select the group Asa@0: status.selected = key Asa@0: --children of this group are no longer extra levels of a tree Asa@0: treevalue = nil Asa@0: else Asa@0: --tree group by default Asa@0: if treevalue then Asa@0: --this is an extra level of a tree group, build a uniquevalue for it Asa@0: treevalue = treevalue.."\001"..key Asa@0: else Asa@0: --this is the top level of a tree group, the uniquevalue is the same as the key Asa@0: treevalue = key Asa@0: if not status.groups then Asa@0: status.groups = {} Asa@0: end Asa@0: --save this trees status table for any extra levels or groups Asa@0: treestatus = status Asa@0: end Asa@0: --make sure that the tree entry is open, and select it. Asa@0: --the selected group will be overwritten if a child is the final target but still needs to be open Asa@0: treestatus.selected = treevalue Asa@0: treestatus.groups[treevalue] = true Asa@0: Asa@0: end Asa@0: Asa@0: --move to the next group in the path Asa@0: group = GetSubOption(group, key) Asa@0: if not group then Asa@0: break Asa@0: end Asa@0: tinsert(path, key) Asa@0: status = self:GetStatusTable(appName, path) Asa@0: if not status.groups then Asa@0: status.groups = {} Asa@0: end Asa@0: status = status.groups Asa@0: end Asa@0: Asa@0: del(path) Asa@0: reg:NotifyChange(appName) Asa@0: end Asa@0: Asa@0: local function OptionOnMouseOver(widget, event) Asa@0: --show a tooltip/set the status bar to the desc text Asa@0: local user = widget:GetUserDataTable() Asa@0: local opt = user.option Asa@0: local options = user.options Asa@0: local path = user.path Asa@0: local appName = user.appName Asa@0: Asa@0: GameTooltip:SetOwner(widget.frame, "ANCHOR_TOPRIGHT") Asa@0: local name = GetOptionsMemberValue("name", opt, options, path, appName) Asa@0: local desc = GetOptionsMemberValue("desc", opt, options, path, appName) Asa@0: local usage = GetOptionsMemberValue("usage", opt, options, path, appName) Asa@0: local descStyle = opt.descStyle Asa@0: Asa@0: if descStyle and descStyle ~= "tooltip" then return end Asa@0: Asa@0: GameTooltip:SetText(name, 1, .82, 0, 1) Asa@0: Asa@0: if opt.type == 'multiselect' then Asa@0: GameTooltip:AddLine(user.text,0.5, 0.5, 0.8, 1) Asa@0: end Asa@0: if type(desc) == "string" then Asa@0: GameTooltip:AddLine(desc, 1, 1, 1, 1) Asa@0: end Asa@0: if type(usage) == "string" then Asa@0: GameTooltip:AddLine("Usage: "..usage, NORMAL_FONT_COLOR.r, NORMAL_FONT_COLOR.g, NORMAL_FONT_COLOR.b, 1) Asa@0: end Asa@0: Asa@0: GameTooltip:Show() Asa@0: end Asa@0: Asa@0: local function OptionOnMouseLeave(widget, event) Asa@0: GameTooltip:Hide() Asa@0: end Asa@0: Asa@0: local function GetFuncName(option) Asa@0: local type = option.type Asa@0: if type == 'execute' then Asa@0: return 'func' Asa@0: else Asa@0: return 'set' Asa@0: end Asa@0: end Asa@0: local function confirmPopup(appName, rootframe, basepath, info, message, func, ...) Asa@0: if not StaticPopupDialogs["ACECONFIGDIALOG30_CONFIRM_DIALOG"] then Asa@0: StaticPopupDialogs["ACECONFIGDIALOG30_CONFIRM_DIALOG"] = {} Asa@0: end Asa@0: local t = StaticPopupDialogs["ACECONFIGDIALOG30_CONFIRM_DIALOG"] Asa@0: for k in pairs(t) do Asa@0: t[k] = nil Asa@0: end Asa@0: t.text = message Asa@0: t.button1 = ACCEPT Asa@0: t.button2 = CANCEL Asa@0: local dialog, oldstrata Asa@0: t.OnAccept = function() Asa@0: safecall(func, unpack(t)) Asa@0: if dialog and oldstrata then Asa@0: dialog:SetFrameStrata(oldstrata) Asa@0: end Asa@0: AceConfigDialog:Open(appName, rootframe, unpack(basepath or emptyTbl)) Asa@0: del(info) Asa@0: end Asa@0: t.OnCancel = function() Asa@0: if dialog and oldstrata then Asa@0: dialog:SetFrameStrata(oldstrata) Asa@0: end Asa@0: AceConfigDialog:Open(appName, rootframe, unpack(basepath or emptyTbl)) Asa@0: del(info) Asa@0: end Asa@0: for i = 1, select('#', ...) do Asa@0: t[i] = select(i, ...) or false Asa@0: end Asa@0: t.timeout = 0 Asa@0: t.whileDead = 1 Asa@0: t.hideOnEscape = 1 Asa@0: Asa@0: dialog = StaticPopup_Show("ACECONFIGDIALOG30_CONFIRM_DIALOG") Asa@0: if dialog then Asa@0: oldstrata = dialog:GetFrameStrata() Asa@0: dialog:SetFrameStrata("TOOLTIP") Asa@0: end Asa@0: end Asa@0: Asa@0: local function ActivateControl(widget, event, ...) Asa@0: --This function will call the set / execute handler for the widget Asa@0: --widget:GetUserDataTable() contains the needed info Asa@0: local user = widget:GetUserDataTable() Asa@0: local option = user.option Asa@0: local options = user.options Asa@0: local path = user.path Asa@0: local info = new() Asa@0: Asa@0: local func Asa@0: local group = options Asa@0: local funcname = GetFuncName(option) Asa@0: local handler Asa@0: local confirm Asa@0: local validate Asa@0: --build the info table containing the path Asa@0: -- pick up functions while traversing the tree Asa@0: if group[funcname] ~= nil then Asa@0: func = group[funcname] Asa@0: end Asa@0: handler = group.handler or handler Asa@0: confirm = group.confirm Asa@0: validate = group.validate Asa@0: for i = 1, #path do Asa@0: local v = path[i] Asa@0: group = GetSubOption(group, v) Asa@0: info[i] = v Asa@0: if group[funcname] ~= nil then Asa@0: func = group[funcname] Asa@0: end Asa@0: handler = group.handler or handler Asa@0: if group.confirm ~= nil then Asa@0: confirm = group.confirm Asa@0: end Asa@0: if group.validate ~= nil then Asa@0: validate = group.validate Asa@0: end Asa@0: end Asa@0: Asa@0: info.options = options Asa@0: info.appName = user.appName Asa@0: info.arg = option.arg Asa@0: info.handler = handler Asa@0: info.option = option Asa@0: info.type = option.type Asa@0: info.uiType = 'dialog' Asa@0: info.uiName = MAJOR Asa@0: Asa@0: local name Asa@0: if type(option.name) == "function" then Asa@0: name = option.name(info) Asa@0: elseif type(option.name) == "string" then Asa@0: name = option.name Asa@0: else Asa@0: name = "" Asa@0: end Asa@0: local usage = option.usage Asa@0: local pattern = option.pattern Asa@0: Asa@0: local validated = true Asa@0: Asa@0: if option.type == "input" then Asa@0: if type(pattern)=="string" then Asa@0: if not strmatch(..., pattern) then Asa@0: validated = false Asa@0: end Asa@0: end Asa@0: end Asa@0: Asa@0: local success Asa@0: if validated and option.type ~= "execute" then Asa@0: if type(validate) == "string" then Asa@0: if handler and handler[validate] then Asa@0: success, validated = safecall(handler[validate], handler, info, ...) Asa@0: if not success then validated = false end Asa@0: else Asa@0: error(format("Method %s doesn't exist in handler for type execute", validate)) Asa@0: end Asa@0: elseif type(validate) == "function" then Asa@0: success, validated = safecall(validate, info, ...) Asa@0: if not success then validated = false end Asa@0: end Asa@0: end Asa@0: Asa@0: local rootframe = user.rootframe Asa@0: if type(validated) == "string" then Asa@0: --validate function returned a message to display Asa@0: if rootframe.SetStatusText then Asa@0: rootframe:SetStatusText(validated) Asa@0: else Asa@0: -- TODO: do something else. Asa@0: end Asa@0: PlaySound("igPlayerInviteDecline") Asa@0: del(info) Asa@0: return true Asa@0: elseif not validated then Asa@0: --validate returned false Asa@0: if rootframe.SetStatusText then Asa@0: if usage then Asa@0: rootframe:SetStatusText(name..": "..usage) Asa@0: else Asa@0: if pattern then Asa@0: rootframe:SetStatusText(name..": Expected "..pattern) Asa@0: else Asa@0: rootframe:SetStatusText(name..": Invalid Value") Asa@0: end Asa@0: end Asa@0: else Asa@0: -- TODO: do something else Asa@0: end Asa@0: PlaySound("igPlayerInviteDecline") Asa@0: del(info) Asa@0: return true Asa@0: else Asa@0: Asa@0: local confirmText = option.confirmText Asa@0: --call confirm func/method Asa@0: if type(confirm) == "string" then Asa@0: if handler and handler[confirm] then Asa@0: success, confirm = safecall(handler[confirm], handler, info, ...) Asa@0: if success and type(confirm) == "string" then Asa@0: confirmText = confirm Asa@0: confirm = true Asa@0: elseif not success then Asa@0: confirm = false Asa@0: end Asa@0: else Asa@0: error(format("Method %s doesn't exist in handler for type confirm", confirm)) Asa@0: end Asa@0: elseif type(confirm) == "function" then Asa@0: success, confirm = safecall(confirm, info, ...) Asa@0: if success and type(confirm) == "string" then Asa@0: confirmText = confirm Asa@0: confirm = true Asa@0: elseif not success then Asa@0: confirm = false Asa@0: end Asa@0: end Asa@0: Asa@0: --confirm if needed Asa@0: if type(confirm) == "boolean" then Asa@0: if confirm then Asa@0: if not confirmText then Asa@0: local name, desc = option.name, option.desc Asa@0: if type(name) == "function" then Asa@0: name = name(info) Asa@0: end Asa@0: if type(desc) == "function" then Asa@0: desc = desc(info) Asa@0: end Asa@0: confirmText = name Asa@0: if desc then Asa@0: confirmText = confirmText.." - "..desc Asa@0: end Asa@0: end Asa@0: Asa@0: local iscustom = user.rootframe:GetUserData('iscustom') Asa@0: local rootframe Asa@0: Asa@0: if iscustom then Asa@0: rootframe = user.rootframe Asa@0: end Asa@0: local basepath = user.rootframe:GetUserData('basepath') Asa@0: if type(func) == "string" then Asa@0: if handler and handler[func] then Asa@0: confirmPopup(user.appName, rootframe, basepath, info, confirmText, handler[func], handler, info, ...) Asa@0: else Asa@0: error(format("Method %s doesn't exist in handler for type func", func)) Asa@0: end Asa@0: elseif type(func) == "function" then Asa@0: confirmPopup(user.appName, rootframe, basepath, info, confirmText, func, info, ...) Asa@0: end Asa@0: --func will be called and info deleted when the confirm dialog is responded to Asa@0: return Asa@0: end Asa@0: end Asa@0: Asa@0: --call the function Asa@0: if type(func) == "string" then Asa@0: if handler and handler[func] then Asa@0: safecall(handler[func],handler, info, ...) Asa@0: else Asa@0: error(format("Method %s doesn't exist in handler for type func", func)) Asa@0: end Asa@0: elseif type(func) == "function" then Asa@0: safecall(func,info, ...) Asa@0: end Asa@0: Asa@0: Asa@0: Asa@0: local iscustom = user.rootframe:GetUserData('iscustom') Asa@0: local basepath = user.rootframe:GetUserData('basepath') or emptyTbl Asa@0: --full refresh of the frame, some controls dont cause this on all events Asa@0: if option.type == "color" then Asa@0: if event == "OnValueConfirmed" then Asa@0: Asa@0: if iscustom then Asa@0: AceConfigDialog:Open(user.appName, user.rootframe, unpack(basepath)) Asa@0: else Asa@0: AceConfigDialog:Open(user.appName, unpack(basepath)) Asa@0: end Asa@0: end Asa@0: elseif option.type == "range" then Asa@0: if event == "OnMouseUp" then Asa@0: if iscustom then Asa@0: AceConfigDialog:Open(user.appName, user.rootframe, unpack(basepath)) Asa@0: else Asa@0: AceConfigDialog:Open(user.appName, unpack(basepath)) Asa@0: end Asa@0: end Asa@0: --multiselects don't cause a refresh on 'OnValueChanged' only 'OnClosed' Asa@0: elseif option.type == "multiselect" then Asa@0: user.valuechanged = true Asa@0: else Asa@0: if iscustom then Asa@0: AceConfigDialog:Open(user.appName, user.rootframe, unpack(basepath)) Asa@0: else Asa@0: AceConfigDialog:Open(user.appName, unpack(basepath)) Asa@0: end Asa@0: end Asa@0: Asa@0: end Asa@0: del(info) Asa@0: end Asa@0: Asa@0: local function ActivateSlider(widget, event, value) Asa@0: local option = widget:GetUserData('option') Asa@0: local min, max, step = option.min or 0, option.max or 100, option.step Asa@0: if step then Asa@0: value = math_floor((value - min) / step + 0.5) * step + min Asa@0: else Asa@0: value = math_max(math_min(value,max),min) Asa@0: end Asa@0: ActivateControl(widget,event,value) Asa@0: end Asa@0: Asa@0: --called from a checkbox that is part of an internally created multiselect group Asa@0: --this type is safe to refresh on activation of one control Asa@0: local function ActivateMultiControl(widget, event, ...) Asa@0: ActivateControl(widget, event, widget:GetUserData('value'), ...) Asa@0: local user = widget:GetUserDataTable() Asa@0: local iscustom = user.rootframe:GetUserData('iscustom') Asa@0: local basepath = user.rootframe:GetUserData('basepath') or emptyTbl Asa@0: if iscustom then Asa@0: AceConfigDialog:Open(user.appName, user.rootframe, unpack(basepath)) Asa@0: else Asa@0: AceConfigDialog:Open(user.appName, unpack(basepath)) Asa@0: end Asa@0: end Asa@0: Asa@0: local function MultiControlOnClosed(widget, event, ...) Asa@0: local user = widget:GetUserDataTable() Asa@0: if user.valuechanged then Asa@0: local iscustom = user.rootframe:GetUserData('iscustom') Asa@0: local basepath = user.rootframe:GetUserData('basepath') or emptyTbl Asa@0: if iscustom then Asa@0: AceConfigDialog:Open(user.appName, user.rootframe, unpack(basepath)) Asa@0: else Asa@0: AceConfigDialog:Open(user.appName, unpack(basepath)) Asa@0: end Asa@0: end Asa@0: end Asa@0: Asa@0: local function FrameOnClose(widget, event) Asa@0: local appName = widget:GetUserData('appName') Asa@0: AceConfigDialog.OpenFrames[appName] = nil Asa@0: gui:Release(widget) Asa@0: end Asa@0: Asa@0: local function CheckOptionHidden(option, options, path, appName) Asa@0: --check for a specific boolean option Asa@0: local hidden = pickfirstset(option.dialogHidden,option.guiHidden) Asa@0: if hidden ~= nil then Asa@0: return hidden Asa@0: end Asa@0: Asa@0: return GetOptionsMemberValue("hidden", option, options, path, appName) Asa@0: end Asa@0: Asa@0: local function CheckOptionDisabled(option, options, path, appName) Asa@0: --check for a specific boolean option Asa@0: local disabled = pickfirstset(option.dialogDisabled,option.guiDisabled) Asa@0: if disabled ~= nil then Asa@0: return disabled Asa@0: end Asa@0: Asa@0: return GetOptionsMemberValue("disabled", option, options, path, appName) Asa@0: end Asa@0: --[[ Asa@0: local function BuildTabs(group, options, path, appName) Asa@0: local tabs = new() Asa@0: local text = new() Asa@0: local keySort = new() Asa@0: local opts = new() Asa@0: Asa@0: BuildSortedOptionsTable(group, keySort, opts, options, path, appName) Asa@0: Asa@0: for i = 1, #keySort do Asa@0: local k = keySort[i] Asa@0: local v = opts[k] Asa@0: if v.type == "group" then Asa@0: path[#path+1] = k Asa@0: local inline = pickfirstset(v.dialogInline,v.guiInline,v.inline, false) Asa@0: local hidden = CheckOptionHidden(v, options, path, appName) Asa@0: if not inline and not hidden then Asa@0: tinsert(tabs, k) Asa@0: text[k] = GetOptionsMemberValue("name", v, options, path, appName) Asa@0: end Asa@0: path[#path] = nil Asa@0: end Asa@0: end Asa@0: Asa@0: del(keySort) Asa@0: del(opts) Asa@0: Asa@0: return tabs, text Asa@0: end Asa@0: ]] Asa@0: local function BuildSelect(group, options, path, appName) Asa@0: local groups = new() Asa@0: local keySort = new() Asa@0: local opts = new() Asa@0: Asa@0: BuildSortedOptionsTable(group, keySort, opts, options, path, appName) Asa@0: Asa@0: for i = 1, #keySort do Asa@0: local k = keySort[i] Asa@0: local v = opts[k] Asa@0: if v.type == "group" then Asa@0: path[#path+1] = k Asa@0: local inline = pickfirstset(v.dialogInline,v.guiInline,v.inline, false) Asa@0: local hidden = CheckOptionHidden(v, options, path, appName) Asa@0: if not inline and not hidden then Asa@0: groups[k] = GetOptionsMemberValue("name", v, options, path, appName) Asa@0: end Asa@0: path[#path] = nil Asa@0: end Asa@0: end Asa@0: Asa@0: del(keySort) Asa@0: del(opts) Asa@0: Asa@0: return groups Asa@0: end Asa@0: Asa@0: local function BuildSubGroups(group, tree, options, path, appName) Asa@0: local keySort = new() Asa@0: local opts = new() Asa@0: Asa@0: BuildSortedOptionsTable(group, keySort, opts, options, path, appName) Asa@0: Asa@0: for i = 1, #keySort do Asa@0: local k = keySort[i] Asa@0: local v = opts[k] Asa@0: if v.type == "group" then Asa@0: path[#path+1] = k Asa@0: local inline = pickfirstset(v.dialogInline,v.guiInline,v.inline, false) Asa@0: local hidden = CheckOptionHidden(v, options, path, appName) Asa@0: if not inline and not hidden then Asa@0: local entry = new() Asa@0: entry.value = k Asa@0: entry.text = GetOptionsMemberValue("name", v, options, path, appName) Asa@0: entry.icon = GetOptionsMemberValue("icon", v, options, path, appName) Asa@0: entry.disabled = CheckOptionDisabled(v, options, path, appName) Asa@0: if not tree.children then tree.children = new() end Asa@0: tinsert(tree.children,entry) Asa@0: if (v.childGroups or "tree") == "tree" then Asa@0: BuildSubGroups(v,entry, options, path, appName) Asa@0: end Asa@0: end Asa@0: path[#path] = nil Asa@0: end Asa@0: end Asa@0: Asa@0: del(keySort) Asa@0: del(opts) Asa@0: end Asa@0: Asa@0: local function BuildGroups(group, options, path, appName, recurse) Asa@0: local tree = new() Asa@0: local keySort = new() Asa@0: local opts = new() Asa@0: Asa@0: BuildSortedOptionsTable(group, keySort, opts, options, path, appName) Asa@0: Asa@0: for i = 1, #keySort do Asa@0: local k = keySort[i] Asa@0: local v = opts[k] Asa@0: if v.type == "group" then Asa@0: path[#path+1] = k Asa@0: local inline = pickfirstset(v.dialogInline,v.guiInline,v.inline, false) Asa@0: local hidden = CheckOptionHidden(v, options, path, appName) Asa@0: if not inline and not hidden then Asa@0: local entry = new() Asa@0: entry.value = k Asa@0: entry.text = GetOptionsMemberValue("name", v, options, path, appName) Asa@0: entry.icon = GetOptionsMemberValue("icon", v, options, path, appName) Asa@0: entry.disabled = CheckOptionDisabled(v, options, path, appName) Asa@0: tinsert(tree,entry) Asa@0: if recurse and (v.childGroups or "tree") == "tree" then Asa@0: BuildSubGroups(v,entry, options, path, appName) Asa@0: end Asa@0: end Asa@0: path[#path] = nil Asa@0: end Asa@0: end Asa@0: del(keySort) Asa@0: del(opts) Asa@0: return tree Asa@0: end Asa@0: Asa@0: local function InjectInfo(control, options, option, path, rootframe, appName) Asa@0: local user = control:GetUserDataTable() Asa@0: for i = 1, #path do Asa@0: user[i] = path[i] Asa@0: end Asa@0: user.rootframe = rootframe Asa@0: user.option = option Asa@0: user.options = options Asa@0: user.path = copy(path) Asa@0: user.appName = appName Asa@0: control:SetCallback("OnRelease", CleanUserData) Asa@0: control:SetCallback("OnLeave", OptionOnMouseLeave) Asa@0: control:SetCallback("OnEnter", OptionOnMouseOver) Asa@0: end Asa@0: Asa@0: Asa@0: --[[ Asa@0: options - root of the options table being fed Asa@0: container - widget that controls will be placed in Asa@0: rootframe - Frame object the options are in Asa@0: path - table with the keys to get to the group being fed Asa@0: --]] Asa@0: Asa@0: local function FeedOptions(appName, options,container,rootframe,path,group,inline) Asa@0: local keySort = new() Asa@0: local opts = new() Asa@0: Asa@0: BuildSortedOptionsTable(group, keySort, opts, options, path, appName) Asa@0: Asa@0: for i = 1, #keySort do Asa@0: local k = keySort[i] Asa@0: local v = opts[k] Asa@0: tinsert(path, k) Asa@0: local hidden = CheckOptionHidden(v, options, path, appName) Asa@0: local name = GetOptionsMemberValue("name", v, options, path, appName) Asa@0: if not hidden then Asa@0: if v.type == "group" then Asa@0: if inline or pickfirstset(v.dialogInline,v.guiInline,v.inline, false) then Asa@0: --Inline group Asa@0: local GroupContainer Asa@0: if name and name ~= "" then Asa@0: GroupContainer = gui:Create("InlineGroup") Asa@0: GroupContainer:SetTitle(name or "") Asa@0: else Asa@0: GroupContainer = gui:Create("SimpleGroup") Asa@0: end Asa@0: Asa@0: GroupContainer.width = "fill" Asa@0: GroupContainer:SetLayout("flow") Asa@0: container:AddChild(GroupContainer) Asa@0: FeedOptions(appName,options,GroupContainer,rootframe,path,v,true) Asa@0: end Asa@0: else Asa@0: --Control to feed Asa@0: local control Asa@0: Asa@0: local name = GetOptionsMemberValue("name", v, options, path, appName) Asa@0: Asa@0: if v.type == "execute" then Asa@0: Asa@0: local imageCoords = GetOptionsMemberValue("imageCoords",v, options, path, appName) Asa@0: local image, width, height = GetOptionsMemberValue("image",v, options, path, appName) Asa@0: Asa@0: if type(image) == 'string' then Asa@0: control = gui:Create("Icon") Asa@0: if not width then Asa@0: width = GetOptionsMemberValue("imageWidth",v, options, path, appName) Asa@0: end Asa@0: if not height then Asa@0: height = GetOptionsMemberValue("imageHeight",v, options, path, appName) Asa@0: end Asa@0: if type(imageCoords) == 'table' then Asa@0: control:SetImage(image, unpack(imageCoords)) Asa@0: else Asa@0: control:SetImage(image) Asa@0: end Asa@0: if type(width) ~= "number" then Asa@0: width = 32 Asa@0: end Asa@0: if type(height) ~= "number" then Asa@0: height = 32 Asa@0: end Asa@0: control:SetImageSize(width, height) Asa@0: control:SetLabel(name) Asa@0: else Asa@0: control = gui:Create("Button") Asa@0: control:SetText(name) Asa@0: end Asa@0: control:SetCallback("OnClick",ActivateControl) Asa@0: Asa@0: elseif v.type == "input" then Asa@0: local controlType = v.dialogControl or v.control or (v.multiline and "MultiLineEditBox") or "EditBox" Asa@0: control = gui:Create(controlType) Asa@0: if not control then Asa@0: geterrorhandler()(("Invalid Custom Control Type - %s"):format(tostring(controlType))) Asa@0: control = gui:Create(v.multiline and "MultiLineEditBox" or "EditBox") Asa@0: end Asa@0: Asa@0: if v.multiline then Asa@0: local lines = 4 Asa@0: if type(v.multiline) == "number" then Asa@0: lines = v.multiline Asa@0: end Asa@0: control:SetHeight(60 + (14*lines)) Asa@0: end Asa@0: control:SetLabel(name) Asa@0: control:SetCallback("OnEnterPressed",ActivateControl) Asa@0: local text = GetOptionsMemberValue("get",v, options, path, appName) Asa@0: if type(text) ~= "string" then Asa@0: text = "" Asa@0: end Asa@0: control:SetText(text) Asa@0: Asa@0: elseif v.type == "toggle" then Asa@0: control = gui:Create("CheckBox") Asa@0: control:SetLabel(name) Asa@0: control:SetTriState(v.tristate) Asa@0: local value = GetOptionsMemberValue("get",v, options, path, appName) Asa@0: control:SetValue(value) Asa@0: control:SetCallback("OnValueChanged",ActivateControl) Asa@0: Asa@0: if v.descStyle == "inline" then Asa@0: local desc = GetOptionsMemberValue("desc", v, options, path, appName) Asa@0: control:SetDescription(desc) Asa@0: end Asa@0: Asa@0: local image = GetOptionsMemberValue("image", v, options, path, appName) Asa@0: local imageCoords = GetOptionsMemberValue("imageCoords", v, options, path, appName) Asa@0: Asa@0: if type(image) == 'string' then Asa@0: if type(imageCoords) == 'table' then Asa@0: control:SetImage(image, unpack(imageCoords)) Asa@0: else Asa@0: control:SetImage(image) Asa@0: end Asa@0: end Asa@0: elseif v.type == "range" then Asa@0: control = gui:Create("Slider") Asa@0: control:SetLabel(name) Asa@0: control:SetSliderValues(v.min or 0,v.max or 100, v.bigStep or v.step or 0) Asa@0: control:SetIsPercent(v.isPercent) Asa@0: local value = GetOptionsMemberValue("get",v, options, path, appName) Asa@0: if type(value) ~= "number" then Asa@0: value = 0 Asa@0: end Asa@0: control:SetValue(value) Asa@0: control:SetCallback("OnValueChanged",ActivateSlider) Asa@0: control:SetCallback("OnMouseUp",ActivateSlider) Asa@0: Asa@0: elseif v.type == "select" then Asa@0: local values = GetOptionsMemberValue("values", v, options, path, appName) Asa@0: local controlType = v.dialogControl or v.control or "Dropdown" Asa@0: control = gui:Create(controlType) Asa@0: if not control then Asa@0: geterrorhandler()(("Invalid Custom Control Type - %s"):format(tostring(controlType))) Asa@0: control = gui:Create("Dropdown") Asa@0: end Asa@0: control:SetLabel(name) Asa@0: control:SetList(values) Asa@0: local value = GetOptionsMemberValue("get",v, options, path, appName) Asa@0: if not values[value] then Asa@0: value = nil Asa@0: end Asa@0: control:SetValue(value) Asa@0: control:SetCallback("OnValueChanged",ActivateControl) Asa@0: Asa@0: elseif v.type == "multiselect" then Asa@0: local values = GetOptionsMemberValue("values", v, options, path, appName) Asa@0: local disabled = CheckOptionDisabled(v, options, path, appName) Asa@0: Asa@0: local controlType = v.dialogControl or v.control Asa@0: Asa@0: local valuesort = new() Asa@0: if values then Asa@0: for value, text in pairs(values) do Asa@0: tinsert(valuesort, value) Asa@0: end Asa@0: end Asa@0: tsort(valuesort) Asa@0: Asa@0: if controlType then Asa@0: control = gui:Create(controlType) Asa@0: if not control then Asa@0: geterrorhandler()(("Invalid Custom Control Type - %s"):format(tostring(controlType))) Asa@0: end Asa@0: end Asa@0: if control then Asa@0: control:SetMultiselect(true) Asa@0: control:SetLabel(name) Asa@0: control:SetList(values) Asa@0: control:SetDisabled(disabled) Asa@0: control:SetCallback("OnValueChanged",ActivateControl) Asa@0: control:SetCallback("OnClosed", MultiControlOnClosed) Asa@0: local width = GetOptionsMemberValue("width",v,options,path,appName) Asa@0: if width == "double" then Asa@0: control:SetWidth(width_multiplier * 2) Asa@0: elseif width == "half" then Asa@0: control:SetWidth(width_multiplier / 2) Asa@0: elseif width == "full" then Asa@0: control.width = "fill" Asa@0: else Asa@0: control:SetWidth(width_multiplier) Asa@0: end Asa@0: --check:SetTriState(v.tristate) Asa@0: for i = 1, #valuesort do Asa@0: local key = valuesort[i] Asa@0: local value = GetOptionsMemberValue("get",v, options, path, appName, key) Asa@0: control:SetItemValue(key,value) Asa@0: end Asa@0: else Asa@0: control = gui:Create("InlineGroup") Asa@0: control:SetLayout("Flow") Asa@0: control:SetTitle(name) Asa@0: control.width = "fill" Asa@0: Asa@0: control:PauseLayout() Asa@0: local width = GetOptionsMemberValue("width",v,options,path,appName) Asa@0: for i = 1, #valuesort do Asa@0: local value = valuesort[i] Asa@0: local text = values[value] Asa@0: local check = gui:Create("CheckBox") Asa@0: check:SetLabel(text) Asa@0: check:SetUserData('value', value) Asa@0: check:SetUserData('text', text) Asa@0: check:SetDisabled(disabled) Asa@0: check:SetTriState(v.tristate) Asa@0: check:SetValue(GetOptionsMemberValue("get",v, options, path, appName, value)) Asa@0: check:SetCallback("OnValueChanged",ActivateMultiControl) Asa@0: InjectInfo(check, options, v, path, rootframe, appName) Asa@0: control:AddChild(check) Asa@0: if width == "double" then Asa@0: check:SetWidth(width_multiplier * 2) Asa@0: elseif width == "half" then Asa@0: check:SetWidth(width_multiplier / 2) Asa@0: elseif width == "full" then Asa@0: check.width = "fill" Asa@0: else Asa@0: check:SetWidth(width_multiplier) Asa@0: end Asa@0: end Asa@0: control:ResumeLayout() Asa@0: control:DoLayout() Asa@0: Asa@0: Asa@0: end Asa@0: Asa@0: del(valuesort) Asa@0: Asa@0: elseif v.type == "color" then Asa@0: control = gui:Create("ColorPicker") Asa@0: control:SetLabel(name) Asa@0: control:SetHasAlpha(v.hasAlpha) Asa@0: control:SetColor(GetOptionsMemberValue("get",v, options, path, appName)) Asa@0: control:SetCallback("OnValueChanged",ActivateControl) Asa@0: control:SetCallback("OnValueConfirmed",ActivateControl) Asa@0: Asa@0: elseif v.type == "keybinding" then Asa@0: control = gui:Create("Keybinding") Asa@0: control:SetLabel(name) Asa@0: control:SetKey(GetOptionsMemberValue("get",v, options, path, appName)) Asa@0: control:SetCallback("OnKeyChanged",ActivateControl) Asa@0: Asa@0: elseif v.type == "header" then Asa@0: control = gui:Create("Heading") Asa@0: control:SetText(name) Asa@0: control.width = "fill" Asa@0: Asa@0: elseif v.type == "description" then Asa@0: control = gui:Create("Label") Asa@0: control:SetText(name) Asa@0: Asa@0: local fontSize = GetOptionsMemberValue("fontSize",v, options, path, appName) Asa@0: if fontSize == "medium" then Asa@0: control:SetFontObject(GameFontHighlight) Asa@0: elseif fontSize == "large" then Asa@0: control:SetFontObject(GameFontHighlightLarge) Asa@0: else -- small or invalid Asa@0: control:SetFontObject(GameFontHighlightSmall) Asa@0: end Asa@0: Asa@0: local imageCoords = GetOptionsMemberValue("imageCoords",v, options, path, appName) Asa@0: local image, width, height = GetOptionsMemberValue("image",v, options, path, appName) Asa@0: Asa@0: if type(image) == 'string' then Asa@0: if not width then Asa@0: width = GetOptionsMemberValue("imageWidth",v, options, path, appName) Asa@0: end Asa@0: if not height then Asa@0: height = GetOptionsMemberValue("imageHeight",v, options, path, appName) Asa@0: end Asa@0: if type(imageCoords) == 'table' then Asa@0: control:SetImage(image, unpack(imageCoords)) Asa@0: else Asa@0: control:SetImage(image) Asa@0: end Asa@0: if type(width) ~= "number" then Asa@0: width = 32 Asa@0: end Asa@0: if type(height) ~= "number" then Asa@0: height = 32 Asa@0: end Asa@0: control:SetImageSize(width, height) Asa@0: end Asa@0: local width = GetOptionsMemberValue("width",v,options,path,appName) Asa@0: control.width = not width and "fill" Asa@0: end Asa@0: Asa@0: --Common Init Asa@0: if control then Asa@0: if control.width ~= "fill" then Asa@0: local width = GetOptionsMemberValue("width",v,options,path,appName) Asa@0: if width == "double" then Asa@0: control:SetWidth(width_multiplier * 2) Asa@0: elseif width == "half" then Asa@0: control:SetWidth(width_multiplier / 2) Asa@0: elseif width == "full" then Asa@0: control.width = "fill" Asa@0: else Asa@0: control:SetWidth(width_multiplier) Asa@0: end Asa@0: end Asa@0: if control.SetDisabled then Asa@0: local disabled = CheckOptionDisabled(v, options, path, appName) Asa@0: control:SetDisabled(disabled) Asa@0: end Asa@0: Asa@0: InjectInfo(control, options, v, path, rootframe, appName) Asa@0: container:AddChild(control) Asa@0: end Asa@0: Asa@0: end Asa@0: end Asa@0: tremove(path) Asa@0: end Asa@0: container:ResumeLayout() Asa@0: container:DoLayout() Asa@0: del(keySort) Asa@0: del(opts) Asa@0: end Asa@0: Asa@0: local function BuildPath(path, ...) Asa@0: for i = 1, select('#',...) do Asa@0: tinsert(path, (select(i,...))) Asa@0: end Asa@0: end Asa@0: Asa@0: Asa@0: local function TreeOnButtonEnter(widget, event, uniquevalue, button) Asa@0: local user = widget:GetUserDataTable() Asa@0: if not user then return end Asa@0: local options = user.options Asa@0: local option = user.option Asa@0: local path = user.path Asa@0: local appName = user.appName Asa@0: Asa@0: local feedpath = new() Asa@0: for i = 1, #path do Asa@0: feedpath[i] = path[i] Asa@0: end Asa@0: Asa@0: BuildPath(feedpath, ("\001"):split(uniquevalue)) Asa@0: local group = options Asa@0: for i = 1, #feedpath do Asa@0: if not group then return end Asa@0: group = GetSubOption(group, feedpath[i]) Asa@0: end Asa@0: Asa@0: local name = GetOptionsMemberValue("name", group, options, feedpath, appName) Asa@0: local desc = GetOptionsMemberValue("desc", group, options, feedpath, appName) Asa@0: Asa@0: GameTooltip:SetOwner(button, "ANCHOR_NONE") Asa@0: if widget.type == "TabGroup" then Asa@0: GameTooltip:SetPoint("BOTTOM",button,"TOP") Asa@0: else Asa@0: GameTooltip:SetPoint("LEFT",button,"RIGHT") Asa@0: end Asa@0: Asa@0: GameTooltip:SetText(name, 1, .82, 0, 1) Asa@0: Asa@0: if type(desc) == "string" then Asa@0: GameTooltip:AddLine(desc, 1, 1, 1, 1) Asa@0: end Asa@0: Asa@0: GameTooltip:Show() Asa@0: end Asa@0: Asa@0: local function TreeOnButtonLeave(widget, event, value, button) Asa@0: GameTooltip:Hide() Asa@0: end Asa@0: Asa@0: Asa@0: local function GroupExists(appName, options, path, uniquevalue) Asa@0: if not uniquevalue then return false end Asa@0: Asa@0: local feedpath = new() Asa@0: local temppath = new() Asa@0: for i = 1, #path do Asa@0: feedpath[i] = path[i] Asa@0: end Asa@0: Asa@0: BuildPath(feedpath, ("\001"):split(uniquevalue)) Asa@0: Asa@0: local group = options Asa@0: for i = 1, #feedpath do Asa@0: local v = feedpath[i] Asa@0: temppath[i] = v Asa@0: group = GetSubOption(group, v) Asa@0: Asa@0: if not group or group.type ~= "group" or CheckOptionHidden(group, options, temppath, appName) then Asa@0: del(feedpath) Asa@0: del(temppath) Asa@0: return false Asa@0: end Asa@0: end Asa@0: del(feedpath) Asa@0: del(temppath) Asa@0: return true Asa@0: end Asa@0: Asa@0: local function GroupSelected(widget, event, uniquevalue) Asa@0: Asa@0: local user = widget:GetUserDataTable() Asa@0: Asa@0: local options = user.options Asa@0: local option = user.option Asa@0: local path = user.path Asa@0: local rootframe = user.rootframe Asa@0: Asa@0: local feedpath = new() Asa@0: for i = 1, #path do Asa@0: feedpath[i] = path[i] Asa@0: end Asa@0: Asa@0: BuildPath(feedpath, ("\001"):split(uniquevalue)) Asa@0: local group = options Asa@0: for i = 1, #feedpath do Asa@0: group = GetSubOption(group, feedpath[i]) Asa@0: end Asa@0: widget:ReleaseChildren() Asa@0: AceConfigDialog:FeedGroup(user.appName,options,widget,rootframe,feedpath) Asa@0: Asa@0: del(feedpath) Asa@0: end Asa@0: Asa@0: Asa@0: Asa@0: --[[ Asa@0: -- INTERNAL -- Asa@0: This function will feed one group, and any inline child groups into the given container Asa@0: Select Groups will only have the selection control (tree, tabs, dropdown) fed in Asa@0: and have a group selected, this event will trigger the feeding of child groups Asa@0: Asa@0: Rules: Asa@0: If the group is Inline, FeedOptions Asa@0: If the group has no child groups, FeedOptions Asa@0: Asa@0: If the group is a tab or select group, FeedOptions then add the Group Control Asa@0: If the group is a tree group FeedOptions then Asa@0: its parent isnt a tree group: then add the tree control containing this and all child tree groups Asa@0: if its parent is a tree group, its already a node on a tree Asa@0: --]] Asa@0: Asa@0: function AceConfigDialog:FeedGroup(appName,options,container,rootframe,path, isRoot) Asa@0: local group = options Asa@0: --follow the path to get to the curent group Asa@0: local inline Asa@0: local grouptype, parenttype = options.childGroups, "none" Asa@0: Asa@0: Asa@0: --temp path table to pass to callbacks as we traverse the tree Asa@0: local temppath = new() Asa@0: for i = 1, #path do Asa@0: local v = path[i] Asa@0: temppath[i] = v Asa@0: group = GetSubOption(group, v) Asa@0: inline = inline or pickfirstset(v.dialogInline,v.guiInline,v.inline, false) Asa@0: parenttype = grouptype Asa@0: grouptype = group.childGroups Asa@0: end Asa@0: del(temppath) Asa@0: Asa@0: if not parenttype then Asa@0: parenttype = "tree" Asa@0: end Asa@0: Asa@0: --check if the group has child groups Asa@0: local hasChildGroups Asa@0: for k, v in pairs(group.args) do Asa@0: if v.type == "group" and not pickfirstset(v.dialogInline,v.guiInline,v.inline, false) and not CheckOptionHidden(v, options, path, appName) then Asa@0: hasChildGroups = true Asa@0: end Asa@0: end Asa@0: if group.plugins then Asa@0: for plugin, t in pairs(group.plugins) do Asa@0: for k, v in pairs(t) do Asa@0: if v.type == "group" and not pickfirstset(v.dialogInline,v.guiInline,v.inline, false) and not CheckOptionHidden(v, options, path, appName) then Asa@0: hasChildGroups = true Asa@0: end Asa@0: end Asa@0: end Asa@0: end Asa@0: Asa@0: container:SetLayout("flow") Asa@0: local scroll Asa@0: Asa@0: --Add a scrollframe if we are not going to add a group control, this is the inverse of the conditions for that later on Asa@0: if (not (hasChildGroups and not inline)) or (grouptype ~= "tab" and grouptype ~= "select" and (parenttype == "tree" and not isRoot)) then Asa@0: if container.type ~= "InlineGroup" and container.type ~= "SimpleGroup" then Asa@0: scroll = gui:Create("ScrollFrame") Asa@0: scroll:SetLayout("flow") Asa@0: scroll.width = "fill" Asa@0: scroll.height = "fill" Asa@0: container:SetLayout("fill") Asa@0: container:AddChild(scroll) Asa@0: container = scroll Asa@0: end Asa@0: end Asa@0: Asa@0: FeedOptions(appName,options,container,rootframe,path,group,nil) Asa@0: Asa@0: if scroll then Asa@0: container:PerformLayout() Asa@0: local status = self:GetStatusTable(appName, path) Asa@0: if not status.scroll then Asa@0: status.scroll = {} Asa@0: end Asa@0: scroll:SetStatusTable(status.scroll) Asa@0: end Asa@0: Asa@0: if hasChildGroups and not inline then Asa@0: local name = GetOptionsMemberValue("name", group, options, path, appName) Asa@0: if grouptype == "tab" then Asa@0: Asa@0: local tab = gui:Create("TabGroup") Asa@0: InjectInfo(tab, options, group, path, rootframe, appName) Asa@0: tab:SetCallback("OnGroupSelected", GroupSelected) Asa@0: tab:SetCallback("OnTabEnter", TreeOnButtonEnter) Asa@0: tab:SetCallback("OnTabLeave", TreeOnButtonLeave) Asa@0: Asa@0: local status = AceConfigDialog:GetStatusTable(appName, path) Asa@0: if not status.groups then Asa@0: status.groups = {} Asa@0: end Asa@0: tab:SetStatusTable(status.groups) Asa@0: tab.width = "fill" Asa@0: tab.height = "fill" Asa@0: Asa@0: local tabs = BuildGroups(group, options, path, appName) Asa@0: tab:SetTabs(tabs) Asa@0: tab:SetUserData("tablist", tabs) Asa@0: Asa@0: for i = 1, #tabs do Asa@0: local entry = tabs[i] Asa@0: if not entry.disabled then Asa@0: tab:SelectTab((GroupExists(appName, options, path,status.groups.selected) and status.groups.selected) or entry.value) Asa@0: break Asa@0: end Asa@0: end Asa@0: Asa@0: container:AddChild(tab) Asa@0: Asa@0: elseif grouptype == "select" then Asa@0: Asa@0: local select = gui:Create("DropdownGroup") Asa@0: select:SetTitle(name) Asa@0: InjectInfo(select, options, group, path, rootframe, appName) Asa@0: select:SetCallback("OnGroupSelected", GroupSelected) Asa@0: local status = AceConfigDialog:GetStatusTable(appName, path) Asa@0: if not status.groups then Asa@0: status.groups = {} Asa@0: end Asa@0: select:SetStatusTable(status.groups) Asa@0: local grouplist = BuildSelect(group, options, path, appName) Asa@0: select:SetGroupList(grouplist) Asa@0: select:SetUserData("grouplist", grouplist) Asa@0: local firstgroup Asa@0: for k, v in pairs(grouplist) do Asa@0: if not firstgroup or k < firstgroup then Asa@0: firstgroup = k Asa@0: end Asa@0: end Asa@0: Asa@0: if firstgroup then Asa@0: select:SetGroup((GroupExists(appName, options, path,status.groups.selected) and status.groups.selected) or firstgroup) Asa@0: end Asa@0: Asa@0: select.width = "fill" Asa@0: select.height = "fill" Asa@0: Asa@0: container:AddChild(select) Asa@0: Asa@0: --assume tree group by default Asa@0: --if parenttype is tree then this group is already a node on that tree Asa@0: elseif (parenttype ~= "tree") or isRoot then Asa@0: local tree = gui:Create("TreeGroup") Asa@0: InjectInfo(tree, options, group, path, rootframe, appName) Asa@0: tree:EnableButtonTooltips(false) Asa@0: Asa@0: tree.width = "fill" Asa@0: tree.height = "fill" Asa@0: Asa@0: tree:SetCallback("OnGroupSelected", GroupSelected) Asa@0: tree:SetCallback("OnButtonEnter", TreeOnButtonEnter) Asa@0: tree:SetCallback("OnButtonLeave", TreeOnButtonLeave) Asa@0: Asa@0: local status = AceConfigDialog:GetStatusTable(appName, path) Asa@0: if not status.groups then Asa@0: status.groups = {} Asa@0: end Asa@0: local treedefinition = BuildGroups(group, options, path, appName, true) Asa@0: tree:SetStatusTable(status.groups) Asa@0: Asa@0: tree:SetTree(treedefinition) Asa@0: tree:SetUserData("tree",treedefinition) Asa@0: Asa@0: for i = 1, #treedefinition do Asa@0: local entry = treedefinition[i] Asa@0: if not entry.disabled then Asa@0: tree:SelectByValue((GroupExists(appName, options, path,status.groups.selected) and status.groups.selected) or entry.value) Asa@0: break Asa@0: end Asa@0: end Asa@0: Asa@0: container:AddChild(tree) Asa@0: end Asa@0: end Asa@0: end Asa@0: Asa@0: local old_CloseSpecialWindows Asa@0: Asa@0: Asa@0: local function RefreshOnUpdate(this) Asa@0: for appName in pairs(this.closing) do Asa@0: if AceConfigDialog.OpenFrames[appName] then Asa@0: AceConfigDialog.OpenFrames[appName]:Hide() Asa@0: end Asa@0: if AceConfigDialog.BlizOptions and AceConfigDialog.BlizOptions[appName] then Asa@0: for key, widget in pairs(AceConfigDialog.BlizOptions[appName]) do Asa@0: if not widget:IsVisible() then Asa@0: widget:ReleaseChildren() Asa@0: end Asa@0: end Asa@0: end Asa@0: this.closing[appName] = nil Asa@0: end Asa@0: Asa@0: if this.closeAll then Asa@0: for k, v in pairs(AceConfigDialog.OpenFrames) do Asa@0: v:Hide() Asa@0: end Asa@0: this.closeAll = nil Asa@0: end Asa@0: Asa@0: for appName in pairs(this.apps) do Asa@0: if AceConfigDialog.OpenFrames[appName] then Asa@0: local user = AceConfigDialog.OpenFrames[appName]:GetUserDataTable() Asa@0: AceConfigDialog:Open(appName, unpack(user.basepath or emptyTbl)) Asa@0: end Asa@0: if AceConfigDialog.BlizOptions and AceConfigDialog.BlizOptions[appName] then Asa@0: for key, widget in pairs(AceConfigDialog.BlizOptions[appName]) do Asa@0: local user = widget:GetUserDataTable() Asa@0: if widget:IsVisible() then Asa@0: AceConfigDialog:Open(widget:GetUserData('appName'), widget, unpack(user.basepath or emptyTbl)) Asa@0: end Asa@0: end Asa@0: end Asa@0: this.apps[appName] = nil Asa@0: end Asa@0: this:SetScript("OnUpdate", nil) Asa@0: end Asa@0: Asa@0: -- Upgrade the OnUpdate script as well, if needed. Asa@0: if AceConfigDialog.frame:GetScript("OnUpdate") then Asa@0: AceConfigDialog.frame:SetScript("OnUpdate", RefreshOnUpdate) Asa@0: end Asa@0: Asa@0: --- Close all open options windows Asa@0: function AceConfigDialog:CloseAll() Asa@0: AceConfigDialog.frame.closeAll = true Asa@0: AceConfigDialog.frame:SetScript("OnUpdate", RefreshOnUpdate) Asa@0: if next(self.OpenFrames) then Asa@0: return true Asa@0: end Asa@0: end Asa@0: Asa@0: --- Close a specific options window. Asa@0: -- @param appName The application name as given to `:RegisterOptionsTable()` Asa@0: function AceConfigDialog:Close(appName) Asa@0: if self.OpenFrames[appName] then Asa@0: AceConfigDialog.frame.closing[appName] = true Asa@0: AceConfigDialog.frame:SetScript("OnUpdate", RefreshOnUpdate) Asa@0: return true Asa@0: end Asa@0: end Asa@0: Asa@0: -- Internal -- Called by AceConfigRegistry Asa@0: function AceConfigDialog:ConfigTableChanged(event, appName) Asa@0: AceConfigDialog.frame.apps[appName] = true Asa@0: AceConfigDialog.frame:SetScript("OnUpdate", RefreshOnUpdate) Asa@0: end Asa@0: Asa@0: reg.RegisterCallback(AceConfigDialog, "ConfigTableChange", "ConfigTableChanged") Asa@0: Asa@0: --- Sets the default size of the options window for a specific application. Asa@0: -- @param appName The application name as given to `:RegisterOptionsTable()` Asa@0: -- @param width The default width Asa@0: -- @param height The default height Asa@0: function AceConfigDialog:SetDefaultSize(appName, width, height) Asa@0: local status = AceConfigDialog:GetStatusTable(appName) Asa@0: if type(width) == "number" and type(height) == "number" then Asa@0: status.width = width Asa@0: status.height = height Asa@0: end Asa@0: end Asa@0: Asa@0: --- Open an option window at the specified path (if any). Asa@0: -- This function can optionally feed the group into a pre-created container Asa@0: -- instead of creating a new container frame. Asa@0: -- @paramsig appName [, container][, ...] Asa@0: -- @param appName The application name as given to `:RegisterOptionsTable()` Asa@0: -- @param container An optional container frame to feed the options into Asa@0: -- @param ... The path to open after creating the options window (see `:SelectGroup` for details) Asa@0: function AceConfigDialog:Open(appName, container, ...) Asa@0: if not old_CloseSpecialWindows then Asa@0: old_CloseSpecialWindows = CloseSpecialWindows Asa@0: CloseSpecialWindows = function() Asa@0: local found = old_CloseSpecialWindows() Asa@0: return self:CloseAll() or found Asa@0: end Asa@0: end Asa@0: local app = reg:GetOptionsTable(appName) Asa@0: if not app then Asa@0: error(("%s isn't registed with AceConfigRegistry, unable to open config"):format(appName), 2) Asa@0: end Asa@0: local options = app("dialog", MAJOR) Asa@0: Asa@0: local f Asa@0: Asa@0: local path = new() Asa@0: local name = GetOptionsMemberValue("name", options, options, path, appName) Asa@0: Asa@0: --If an optional path is specified add it to the path table before feeding the options Asa@0: --as container is optional as well it may contain the first element of the path Asa@0: if type(container) == "string" then Asa@0: tinsert(path, container) Asa@0: container = nil Asa@0: end Asa@0: for n = 1, select('#',...) do Asa@0: tinsert(path, (select(n, ...))) Asa@0: end Asa@0: Asa@0: --if a container is given feed into that Asa@0: if container then Asa@0: f = container Asa@0: f:ReleaseChildren() Asa@0: f:SetUserData('appName', appName) Asa@0: f:SetUserData('iscustom', true) Asa@0: if #path > 0 then Asa@0: f:SetUserData('basepath', copy(path)) Asa@0: end Asa@0: local status = AceConfigDialog:GetStatusTable(appName) Asa@0: if not status.width then Asa@0: status.width = 700 Asa@0: end Asa@0: if not status.height then Asa@0: status.height = 500 Asa@0: end Asa@0: if f.SetStatusTable then Asa@0: f:SetStatusTable(status) Asa@0: end Asa@0: if f.SetTitle then Asa@0: f:SetTitle(name or "") Asa@0: end Asa@0: else Asa@0: if not self.OpenFrames[appName] then Asa@0: f = gui:Create("Frame") Asa@0: self.OpenFrames[appName] = f Asa@0: else Asa@0: f = self.OpenFrames[appName] Asa@0: end Asa@0: f:ReleaseChildren() Asa@0: f:SetCallback("OnClose", FrameOnClose) Asa@0: f:SetUserData('appName', appName) Asa@0: if #path > 0 then Asa@0: f:SetUserData('basepath', copy(path)) Asa@0: end Asa@0: f:SetTitle(name or "") Asa@0: local status = AceConfigDialog:GetStatusTable(appName) Asa@0: f:SetStatusTable(status) Asa@0: end Asa@0: Asa@0: self:FeedGroup(appName,options,f,f,path,true) Asa@0: if f.Show then Asa@0: f:Show() Asa@0: end Asa@0: del(path) Asa@0: end Asa@0: Asa@0: -- convert pre-39 BlizOptions structure to the new format Asa@0: if oldminor and oldminor < 39 and AceConfigDialog.BlizOptions then Asa@0: local old = AceConfigDialog.BlizOptions Asa@0: local new = {} Asa@0: for key, widget in pairs(old) do Asa@0: local appName = widget:GetUserData('appName') Asa@0: if not new[appName] then new[appName] = {} end Asa@0: new[appName][key] = widget Asa@0: end Asa@0: AceConfigDialog.BlizOptions = new Asa@0: else Asa@0: AceConfigDialog.BlizOptions = AceConfigDialog.BlizOptions or {} Asa@0: end Asa@0: Asa@0: local function FeedToBlizPanel(widget, event) Asa@0: local path = widget:GetUserData('path') Asa@0: AceConfigDialog:Open(widget:GetUserData('appName'), widget, unpack(path or emptyTbl)) Asa@0: end Asa@0: Asa@0: local function ClearBlizPanel(widget, event) Asa@0: local appName = widget:GetUserData('appName') Asa@0: AceConfigDialog.frame.closing[appName] = true Asa@0: AceConfigDialog.frame:SetScript("OnUpdate", RefreshOnUpdate) Asa@0: end Asa@0: Asa@0: --- Add an option table into the Blizzard Interface Options panel. Asa@0: -- You can optionally supply a descriptive name to use and a parent frame to use, Asa@0: -- as well as a path in the options table.\\ Asa@0: -- If no name is specified, the appName will be used instead. Asa@0: -- Asa@0: -- If you specify a proper `parent` (by name), the interface options will generate a Asa@0: -- tree layout. Note that only one level of children is supported, so the parent always Asa@0: -- has to be a head-level note. Asa@0: -- Asa@0: -- This function returns a reference to the container frame registered with the Interface Asa@0: -- Options. You can use this reference to open the options with the API function Asa@0: -- `InterfaceOptionsFrame_OpenToCategory`. Asa@0: -- @param appName The application name as given to `:RegisterOptionsTable()` Asa@0: -- @param name A descriptive name to display in the options tree (defaults to appName) Asa@0: -- @param parent The parent to use in the interface options tree. Asa@0: -- @param ... The path in the options table to feed into the interface options panel. Asa@0: -- @return The reference to the frame registered into the Interface Options. Asa@0: function AceConfigDialog:AddToBlizOptions(appName, name, parent, ...) Asa@0: local BlizOptions = AceConfigDialog.BlizOptions Asa@0: Asa@0: local key = appName Asa@0: for n = 1, select('#', ...) do Asa@0: key = key..'\001'..select(n, ...) Asa@0: end Asa@0: Asa@0: if not BlizOptions[appName] then Asa@0: BlizOptions[appName] = {} Asa@0: end Asa@0: Asa@0: if not BlizOptions[appName][key] then Asa@0: local group = gui:Create("BlizOptionsGroup") Asa@0: BlizOptions[appName][key] = group Asa@0: group:SetName(name or appName, parent) Asa@0: Asa@0: group:SetTitle(name or appName) Asa@0: group:SetUserData('appName', appName) Asa@0: if select('#', ...) > 0 then Asa@0: local path = {} Asa@0: for n = 1, select('#',...) do Asa@0: tinsert(path, (select(n, ...))) Asa@0: end Asa@0: group:SetUserData('path', path) Asa@0: end Asa@0: group:SetCallback("OnShow", FeedToBlizPanel) Asa@0: group:SetCallback("OnHide", ClearBlizPanel) Asa@0: InterfaceOptions_AddCategory(group.frame) Asa@0: return group.frame Asa@0: else Asa@0: error(("%s has already been added to the Blizzard Options Window with the given path"):format(appName), 2) Asa@0: end Asa@0: end