--[[----------------------------------------------------------------------------- TreeGroup Container Container that uses a tree control to switch between groups. -------------------------------------------------------------------------------]] local Type, Version = "TreeGroup", 48 local AceGUI = LibStub and LibStub("AceGUI-3.0", true) if not AceGUI or (AceGUI:GetWidgetVersion(Type) or 0) >= Version then return end -- Lua APIs local next, pairs, ipairs, assert, type = next, pairs, ipairs, assert, type local math_min, math_max, floor = math.min, math.max, math.floor local select, tremove, unpack, tconcat = select, table.remove, unpack, table.concat -- WoW APIs local CreateFrame, UIParent = CreateFrame, UIParent -- Recycling functions local new, del do local pool = setmetatable({},{__mode='k'}) function new() local t = next(pool) if t then pool[t] = nil return t else return {} end end function del(t) for k in pairs(t) do t[k] = nil end pool[t] = true end end local DEFAULT_TREE_WIDTH = 175 local DEFAULT_TREE_SIZABLE = true --[[----------------------------------------------------------------------------- Support functions -------------------------------------------------------------------------------]] local function GetButtonUniqueValue(line) local parent = line.parent if parent and parent.value then return GetButtonUniqueValue(parent).."\001"..line.value else return line.value end end local function UpdateButton(button, treeline, selected, canExpand, isExpanded) local self = button.obj local toggle = button.toggle local text = treeline.text or "" local icon = treeline.icon local iconCoords = treeline.iconCoords local level = treeline.level local value = treeline.value local uniquevalue = treeline.uniquevalue local disabled = treeline.disabled button.treeline = treeline button.value = value button.uniquevalue = uniquevalue if selected then button:LockHighlight() button.selected = true else button:UnlockHighlight() button.selected = false end button.level = level if ( level == 1 ) then button:SetNormalFontObject("GameFontNormal") button:SetHighlightFontObject("GameFontHighlight") button.text:SetPoint("LEFT", (icon and 16 or 0) + 8, 2) else button:SetNormalFontObject("GameFontHighlightSmall") button:SetHighlightFontObject("GameFontHighlightSmall") button.text:SetPoint("LEFT", (icon and 16 or 0) + 8 * level, 2) end if disabled then button:EnableMouse(false) button.text:SetText("|cff808080"..text..FONT_COLOR_CODE_CLOSE) else button.text:SetText(text) button:EnableMouse(true) end if icon then button.icon:SetTexture(icon) button.icon:SetPoint("LEFT", 8 * level, (level == 1) and 0 or 1) else button.icon:SetTexture(nil) end if iconCoords then button.icon:SetTexCoord(unpack(iconCoords)) else button.icon:SetTexCoord(0, 1, 0, 1) end if canExpand then if not isExpanded then toggle:SetNormalTexture(130838) -- Interface\\Buttons\\UI-PlusButton-UP toggle:SetPushedTexture(130836) -- Interface\\Buttons\\UI-PlusButton-DOWN else toggle:SetNormalTexture(130821) -- Interface\\Buttons\\UI-MinusButton-UP toggle:SetPushedTexture(130820) -- Interface\\Buttons\\UI-MinusButton-DOWN end toggle:Show() else toggle:Hide() end end local function ShouldDisplayLevel(tree) local result = false for k, v in ipairs(tree) do if v.children == nil and v.visible ~= false then result = true elseif v.children then result = result or ShouldDisplayLevel(v.children) end if result then return result end end return false end local function addLine(self, v, tree, level, parent) local line = new() line.value = v.value line.text = v.text line.icon = v.icon line.iconCoords = v.iconCoords line.disabled = v.disabled line.tree = tree line.level = level line.parent = parent line.visible = v.visible line.uniquevalue = GetButtonUniqueValue(line) if v.children then line.hasChildren = true else line.hasChildren = nil end self.lines[#self.lines+1] = line return line end --fire an update after one frame to catch the treeframes height local function FirstFrameUpdate(frame) local self = frame.obj frame:SetScript("OnUpdate", nil) self:RefreshTree(nil, true) end local function BuildUniqueValue(...) local n = select('#', ...) if n == 1 then return ... else return (...).."\001"..BuildUniqueValue(select(2,...)) end end --[[----------------------------------------------------------------------------- Scripts -------------------------------------------------------------------------------]] local function Expand_OnClick(frame) local button = frame.button local self = button.obj local status = (self.status or self.localstatus).groups status[button.uniquevalue] = not status[button.uniquevalue] self:RefreshTree() end local function Button_OnClick(frame) local self = frame.obj self:Fire("OnClick", frame.uniquevalue, frame.selected) if not frame.selected then self:SetSelected(frame.uniquevalue) frame.selected = true frame:LockHighlight() self:RefreshTree() end AceGUI:ClearFocus() end local function Button_OnDoubleClick(button) local self = button.obj local status = (self.status or self.localstatus).groups status[button.uniquevalue] = not status[button.uniquevalue] self:RefreshTree() end local function Button_OnEnter(frame) local self = frame.obj self:Fire("OnButtonEnter", frame.uniquevalue, frame) if self.enabletooltips then local tooltip = AceGUI.tooltip tooltip:SetOwner(frame, "ANCHOR_NONE") tooltip:ClearAllPoints() tooltip:SetPoint("LEFT",frame,"RIGHT") tooltip:SetText(frame.text:GetText() or "", 1, .82, 0, true) tooltip:Show() end end local function Button_OnLeave(frame) local self = frame.obj self:Fire("OnButtonLeave", frame.uniquevalue, frame) if self.enabletooltips then AceGUI.tooltip:Hide() end end local function OnScrollValueChanged(frame, value) if frame.obj.noupdate then return end local self = frame.obj local status = self.status or self.localstatus status.scrollvalue = floor(value + 0.5) self:RefreshTree() AceGUI:ClearFocus() end local function Tree_OnSizeChanged(frame) frame.obj:RefreshTree() end local function Tree_OnMouseWheel(frame, delta) local self = frame.obj if self.showscroll then local scrollbar = self.scrollbar local min, max = scrollbar:GetMinMaxValues() local value = scrollbar:GetValue() local newvalue = math_min(max,math_max(min,value - delta)) if value ~= newvalue then scrollbar:SetValue(newvalue) end end end local function Dragger_OnLeave(frame) frame:SetBackdropColor(1, 1, 1, 0) end local function Dragger_OnEnter(frame) frame:SetBackdropColor(1, 1, 1, 0.8) end local function Dragger_OnMouseDown(frame) local treeframe = frame:GetParent() treeframe:StartSizing("RIGHT") end local function Dragger_OnMouseUp(frame) local treeframe = frame:GetParent() local self = treeframe.obj local treeframeParent = treeframe:GetParent() treeframe:StopMovingOrSizing() --treeframe:SetScript("OnUpdate", nil) treeframe:SetUserPlaced(false) --Without this :GetHeight will get stuck on the current height, causing the tree contents to not resize treeframe:SetHeight(0) treeframe:ClearAllPoints() treeframe:SetPoint("TOPLEFT", treeframeParent, "TOPLEFT",0,0) treeframe:SetPoint("BOTTOMLEFT", treeframeParent, "BOTTOMLEFT",0,0) local status = self.status or self.localstatus status.treewidth = treeframe:GetWidth() treeframe.obj:Fire("OnTreeResize",treeframe:GetWidth()) -- recalculate the content width treeframe.obj:OnWidthSet(status.fullwidth) -- update the layout of the content treeframe.obj:DoLayout() end --[[----------------------------------------------------------------------------- Methods -------------------------------------------------------------------------------]] local methods = { ["OnAcquire"] = function(self) self:SetTreeWidth(DEFAULT_TREE_WIDTH, DEFAULT_TREE_SIZABLE) self:EnableButtonTooltips(true) self.frame:SetScript("OnUpdate", FirstFrameUpdate) end, ["OnRelease"] = function(self) self.status = nil self.tree = nil self.frame:SetScript("OnUpdate", nil) for k, v in pairs(self.localstatus) do if k == "groups" then for k2 in pairs(v) do v[k2] = nil end else self.localstatus[k] = nil end end self.localstatus.scrollvalue = 0 self.localstatus.treewidth = DEFAULT_TREE_WIDTH self.localstatus.treesizable = DEFAULT_TREE_SIZABLE end, ["EnableButtonTooltips"] = function(self, enable) self.enabletooltips = enable end, ["CreateButton"] = function(self) local num = AceGUI:GetNextWidgetNum("TreeGroupButton") local button = CreateFrame("Button", ("AceGUI30TreeButton%d"):format(num), self.treeframe, "OptionsListButtonTemplate") button.obj = self local icon = button:CreateTexture(nil, "OVERLAY") icon:SetWidth(14) icon:SetHeight(14) button.icon = icon button:SetScript("OnClick",Button_OnClick) button:SetScript("OnDoubleClick", Button_OnDoubleClick) button:SetScript("OnEnter",Button_OnEnter) button:SetScript("OnLeave",Button_OnLeave) button.toggle.button = button button.toggle:SetScript("OnClick",Expand_OnClick) button.text:SetHeight(14) -- Prevents text wrapping return button end, ["SetStatusTable"] = function(self, status) assert(type(status) == "table") self.status = status if not status.groups then status.groups = {} end if not status.scrollvalue then status.scrollvalue = 0 end if not status.treewidth then status.treewidth = DEFAULT_TREE_WIDTH end if status.treesizable == nil then status.treesizable = DEFAULT_TREE_SIZABLE end self:SetTreeWidth(status.treewidth,status.treesizable) self:RefreshTree() end, --sets the tree to be displayed ["SetTree"] = function(self, tree, filter) self.filter = filter if tree then assert(type(tree) == "table") end self.tree = tree self:RefreshTree() end, ["BuildLevel"] = function(self, tree, level, parent) local groups = (self.status or self.localstatus).groups for i, v in ipairs(tree) do if v.children then if not self.filter or ShouldDisplayLevel(v.children) then local line = addLine(self, v, tree, level, parent) if groups[line.uniquevalue] then self:BuildLevel(v.children, level+1, line) end end elseif v.visible ~= false or not self.filter then addLine(self, v, tree, level, parent) end end end, ["RefreshTree"] = function(self,scrollToSelection,fromOnUpdate) local buttons = self.buttons local lines = self.lines while lines[1] do local t = tremove(lines) for k in pairs(t) do t[k] = nil end del(t) end if not self.tree then return end --Build the list of visible entries from the tree and status tables local status = self.status or self.localstatus local groupstatus = status.groups local tree = self.tree local treeframe = self.treeframe status.scrollToSelection = status.scrollToSelection or scrollToSelection -- needs to be cached in case the control hasn't been drawn yet (code bails out below) self:BuildLevel(tree, 1) local numlines = #lines local maxlines = (floor(((self.treeframe:GetHeight()or 0) - 20 ) / 18)) if maxlines <= 0 then return end if self.frame:GetParent() == UIParent and not fromOnUpdate then self.frame:SetScript("OnUpdate", FirstFrameUpdate) return end local first, last scrollToSelection = status.scrollToSelection status.scrollToSelection = nil if numlines <= maxlines then --the whole tree fits in the frame status.scrollvalue = 0 self:ShowScroll(false) first, last = 1, numlines else self:ShowScroll(true) --scrolling will be needed self.noupdate = true self.scrollbar:SetMinMaxValues(0, numlines - maxlines) --check if we are scrolled down too far if numlines - status.scrollvalue < maxlines then status.scrollvalue = numlines - maxlines end self.noupdate = nil first, last = status.scrollvalue+1, status.scrollvalue + maxlines --show selection? if scrollToSelection and status.selected then local show for i,line in ipairs(lines) do -- find the line number if line.uniquevalue==status.selected then show=i end end if not show then -- selection was deleted or something? elseif show>=first and show<=last then -- all good else -- scrolling needed! if show