📄 appletree.lua
字号:
--[[---- Earth Tree---- An easy way of rendering structured data.---- by Alexander Brazie----]] EARTHTREE_MAXTITLE_COUNT = 20;EARTHTREE_TITLE_HEIGHT = 16;EARTHTREE_EXPAND_INDENT = 16;EARTHTREE_COLOR_NUMBER = NORMAL_FONT_COLOR;EARTHTREE_COLOR_STRING = HIGHLIGHT_FONT_COLOR;EARTHTREE_COLOR_BOOLEAN = GRAY_FONT_COLOR;EARTHTREE_COLOR_NIL = RED_FONT_COLOR;EARTHTREE_COLOR_TABLE = RED_FONT_COLOR;EARTHTREE_COLOR_FUNCTION = GREEN_FONT_COLOR;EARTHTREE_COLOR_UNKNOWN = GRAY_FONT_COLOR;EARTHTREE_DEFAULT_FONT = ChatFontNormal;EARTHTREE_DEFAULT_HIGHLIGHT_COLOR = HIGHLIGHT_FONT_COLOR;-- Debug TogglesEARTHTREE_DEBUG = false;ET_DEBUG = "EARTHTREE_DEBUG";--[[---- Using EarthTree---- Create a Frame which uses EarthTreeTemplate. ---- Usage:-- Call either EarthTree_LoadTable or EarthTree_LoadEnhanced, -- Then call EarthTree_UpdateFrame to draw to the screen.------ Load a normal Lua table using-- -- EarthTree_LoadTable(-- getglobal("YourFrameName"), -- tableOfData,-- functionTable,-- keyOrder-- );---- functionTable may contain:-- onClick - function called when the text is clicked-- arguments to onClick are:-- arg1 - table showing the parent hierarchy of the clicked item-- onCheck - function called when the checkbox is checked or unchecked-- arguments to onCheck are:-- arg1 - check state (value or nil)-- arg2 - table showing the parent hierarchy of the clicked item---- returns:-- true - the checkbox will be set-- false - the checkbox will be unset-- nil - set to whatever state was sent to it.---- Load an enhanced (customized) table using:---- EarthTree_LoadEnhanced(-- getglobal("YourFrameName"),-- enhancedTable-- );---- enhancedTable is a table of numerically indexed elements. -- Each enhanced table entry may contain the following:---- {-- title - the title text-- titleColor - the color of the title -- right - the text bound to the right side-- rightColor - the color of the right text---- disabled - true if the text cannot be clicked.-- -- check - true if a checkbox exists, false if not-- checked - true if the checkbox is checked, false if not-- checkDisabled - true if the checkbox is disable, false if not---- radio - uses radio artwork instead of checkbox artwork-- radioSelected - true if the radio is active, false if not-- radioDisabled - true if the radio button is disabled---- onClick - function called when the text is clicked-- onCheck - function called when the checkbox is checked or unchecked-- onRadio - function called when the radio button is clicked---- tooltip - tooltip text when the text is moused-over-- checkTooltip - tooltip text when the checkbox is moused-over-- radioTooltip - tooltip text when the radiobox is moused-over-- expandTooltip - tooltip text when the expandbox is moused-over-- -- noTextIndent - the item will not indent children an extra amount ---- children - a table of enhanced entrys-- childrenOverride - true if the + should show up even if there are 0 children-- collapsed - true if the child elements are collapsed(hidden)-- }---- Each EarthTreeFrame has the following modifiable properties:-- titleCount - the number of title(rows) shown in the tree-- highlight - true if the frame will highlight the last clicked item-- highlightSize - "long" or "short" (short is buggy)-- -- tooltip - a string which represents the name of the tooltip used-- tooltipPlacement - "cursor","button" or "frame"-- tooltipAnchor - a string which is any valid tooltip anchor---- activeTable - the frame's activeTable-- -- collapseAllButton - true if the collapseAll button is shown---- keyOrder is a list of keys to use, in the order you want to use them.--]]--[[ Load a generic table for the Frame ]]--function EarthTree_LoadTable(frame, data, funcTable, keyOrder) local newEnhancedTable = {}; if ( not data ) then MFC.IO.print("Attempt to send nil data to ", frame:GetName(), " from ", this:GetName()); end if ( not funcTable ) then funcTable = {}; end -- Since we can only load 2 levels of depth -- We simply parse the first set of keys/values if ( type(keyOrder) ~= "table" ) then for k,v in data do local entry = EarthTree_CreateEntry(k,v,funcTable,nil); -- Set the tooltip entry.tooltip = nil; -- No tooltip! table.insert(newEnhancedTable, entry); end else for k,v in keyOrder do local entry = EarthTree_CreateEntry(v,data[v],funcTable,nil); -- Set the tooltip entry.tooltip = nil; -- No tooltip! table.insert(newEnhancedTable, entry); end end --[[ Load it ]]-- EarthTree_LoadEnhanced(frame, newEnhancedTable);end--[[ Loads an Enhanced (Complex) Table for the Frame ]]--function EarthTree_LoadEnhanced(frame, earthTable) if ( EarthTree_CheckFrame(frame) and EarthTree_CheckTable(earthTable) ) then frame.activeTable = earthTable; FauxScrollFrame_SetOffset(getglobal(frame:GetName().."ListScrollFrame"),0);-- getglobal(frame:GetName().."ListScrollFrameScrollBar"):SetValue(0);-- getglobal(frame:GetName().."ListScrollFrame"):UpdateScrollChildRect(); return true; else MFC.IO.print("Invalid data in LoadEnhanced"); return nil; end end--[[ Retrieves an Enhanced (Complex) Table from the Frame ]]--function EarthTree_GetEnhanced(frame) if (frame.activeTable) then return frame.activeTable; end end--[[ Updates the frame with values from the frame.activeTable object ]]--function EarthTree_UpdateFrame(frame) local flat = {}; if ( not frame ) then return; end -- Hide the highlight frame if ( getglobal(frame:GetName().."HighlightFrame") ) then getglobal(frame:GetName().."HighlightFrame"):Hide(); end -- Check if there's a table if ( frame.activeTable ) then flat = EarthTree_MakeFlatTable(frame.activeTable); local index = 0; if (frame.titleCount > EARTHTREE_MAXTITLE_COUNT) then --MFC.IO.print(ET_DEBUG,frame.titleCount, " exceeds EARTHTREE_MAXTITLE_COUNT (",EARTHTREE_MAXTITLE_COUNT,")"); frame.titleCount = EARTHTREE_MAXTITLE_COUNT; end for id=1, frame.titleCount do index = FauxScrollFrame_GetOffset(getglobal(frame:GetName().."ListScrollFrame")) + id; local value = flat[index]; if ( value ) then EarthTree_AddItem(frame,value,id); else EarthTree_ClearItem(frame,id); end end for id=frame.titleCount+1, EARTHTREE_MAXTITLE_COUNT do EarthTree_ClearItem(frame,id); end --MFC.IO.print(ET_DEBUG,index,",",frame.titleCount); end -- Toggles the collapseAllButton if ( frame.collapseAllButton ) then if ( getglobal(frame:GetName().."Expand") ) then getglobal(frame:GetName().."Expand"):Show(); end -- Shows or hides the artwork if ( frame.collapseAllArtwork ) then --getglobal(frame:GetName().."ExpandCollapseAllTabLeft"):SetTexture("Interface\\QuestFrame\\UI-QuestLogSortTab-Left"); --getglobal(frame:GetName().."ExpandCollapseAllTabMiddle"):SetTexture("Interface\\QuestFrame\\UI-QuestLogSortTab-Middle"); --getglobal(frame:GetName().."ExpandCollapseAllTabRight"):SetTexture("Interface\\QuestFrame\\UI-QuestLogSortTab-Right"); else --getglobal(frame:GetName().."ExpandCollapseAllTabLeft"):SetTexture(); --getglobal(frame:GetName().."ExpandCollapseAllTabMiddle"):SetTexture(); --getglobal(frame:GetName().."ExpandCollapseAllTabRight"):SetTexture(); end else if ( getglobal(frame:GetName().."Expand") ) then getglobal(frame:GetName().."Expand"):Hide(); end end -- ScrollFrame update FauxScrollFrame_Update(getglobal(frame:GetName().."ListScrollFrame"), table.getn(flat), frame.titleCount, EARTHTREE_TITLE_HEIGHT, nil, nil, nil, getglobal(frame:GetName().."HighlightFrame"), getglobal(frame:GetName().."HighlightFrame"):GetWidth(), getglobal(frame:GetName().."HighlightFrame"):GetHeight() );end--[[ Obtains an item from a GUI index ]]--function EarthTree_GetItemByID( frame, id ) local nth = id + FauxScrollFrame_GetOffset(getglobal(frame:GetName().."ListScrollFrame")); local tableD = EarthTree_MakeFlatTable(frame.activeTable); return tableD[nth];end--[[-- Obtains a tooltip for a specific frame/id----]]function EarthTree_GetTooltipText(frame, id, ttype) local item = EarthTree_GetItemByID(frame,id); if ( item ) then local tooltip = ""; if ( type(item.tooltip) == "function" ) then tooltip = item.tooltip(item.value, ttype); else tooltip = item.tooltip; end if ( ttype == "CHECK" ) then if ( type(item.checkTooltip) == "function" ) then tooltip = item.checkTooltip(item.value, ttype); elseif ( type (item.checkTooltip) ~= "nil" ) then tooltip = item.checkTooltip; end elseif ( ttype == "RADIO" ) then if ( type(item.radioTooltip) == "function" ) then tooltip = item.radioTooltip(item.value, ttype); elseif ( type (item.radioTooltip) ~= "nil" ) then tooltip = item.radioTooltip; end end return tooltip; else return nil; endend--[[---- Functions beyond this point are tool function and not meant for general use.---- -Alex----]]--[[---- EarthTree_AddItem(-- frame - the frame to add the item-- item - the table containing the item values-- i - the current index to be updated-- depth - the number of recursions performed-- )---- Returns: -- i, total-- i - the new i-- total - the new total of non-collapsed tree items----]]function EarthTree_AddItem(frame, item, i, depth) if ( not i ) then i = 1; end if ( not depth ) then depth = 0; end if ( i > frame.titleCount ) then -- Do nothing -- and EarthTree_CheckItem(item) elseif ( EarthTree_CheckFrame(frame) ) then -- If a item exits if ( item ) then local ldepth = item.depth-1; local title = item.title; local titleColor = nil; if ( type ( title ) == "function" ) then title, titleColor = title(); end if ( not titleColor ) then titleColor = item.titleColor; end --MFC.io.print(ET_DEBUG,frame:GetName(),": ",i," ",title); if ( item.disabled ) then getglobal(frame:GetName().."Title"..i.."Button"):Disable(); getglobal(frame:GetName().."Title"..i.."Button"):SetDisabledTextColor(titleColor.r, titleColor.g, titleColor.b); else getglobal(frame:GetName().."Title"..i.."Button"):Enable(); end -- Title if ( title ) then getglobal(frame:GetName().."Title"..i):Show(); if ( titleColor ) then getglobal(frame:GetName().."Title"..i.."Button"):SetText(title); -- Inset getglobal(frame:GetName().."Title"..i.."Button"):SetTextColor(titleColor.r, titleColor.g, titleColor.b); getglobal(frame:GetName().."Title"..i.."Button").r = titleColor.r; getglobal(frame:GetName().."Title"..i.."Button").g = titleColor.g; getglobal(frame:GetName().."Title"..i.."Button").b = titleColor.b; else getglobal(frame:GetName().."Title"..i.."Button"):SetText(title); getglobal(frame:GetName().."Title"..i.."Button"):SetTextColor(EARTHTREE_COLOR_NUMBER.r,EARTHTREE_COLOR_NUMBER.g,EARTHTREE_COLOR_NUMBER.b); getglobal(frame:GetName().."Title"..i.."Button").r = EARTHTREE_COLOR_NUMBER.r; getglobal(frame:GetName().."Title"..i.."Button").g = EARTHTREE_COLOR_NUMBER.g; getglobal(frame:GetName().."Title"..i.."Button").b = EARTHTREE_COLOR_NUMBER.b; end getglobal(frame:GetName().."Title"..i):SetWidth(getglobal(frame:GetName().."Title"..i):GetParent():GetWidth()-20); else getglobal(frame:GetName().."Title"..i.."Button"):Hide(); end -- Tag local rightText = item.right; local rightColor = nil; if ( type ( rightText ) == "function" ) then rightText, rightColor = rightText(); end if ( not rightColor ) then rightColor = item.rightColor; end --MFC.IO.print(ET_DEBUG,frame:GetName(),": ",i," ",rightText); if ( rightText ) then getglobal(frame:GetName().."Title"..i.."Tag"):Show(); if ( rightColor ) then getglobal(frame:GetName().."Title"..i.."Tag"):SetText(rightText); getglobal(frame:GetName().."Title"..i.."Tag"):SetTextColor(rightColor.r, rightColor.g, rightColor.b); getglobal(frame:GetName().."Title"..i.."Tag").r = rightColor.r; getglobal(frame:GetName().."Title"..i.."Tag").g = rightColor.g; getglobal(frame:GetName().."Title"..i.."Tag").b = rightColor.b; else getglobal(frame:GetName().."Title"..i.."Tag"):SetText(rightText); getglobal(frame:GetName().."Title"..i.."Tag"):SetTextColor(EARTHTREE_COLOR_STRING.r,EARTHTREE_COLOR_STRING.g,EARTHTREE_COLOR_STRING.b); getglobal(frame:GetName().."Title"..i.."Tag").r = EARTHTREE_COLOR_STRING.r; getglobal(frame:GetName().."Title"..i.."Tag").g = EARTHTREE_COLOR_STRING.g; getglobal(frame:GetName().."Title"..i.."Tag").b = EARTHTREE_COLOR_STRING.b; end else getglobal(frame:GetName().."Title"..i.."Tag"):Hide(); end -- Checkbox Text? if ( item.check ) then -- Indent the check symbol getglobal(frame:GetName().."Title"..i.."Check"):SetPoint("LEFT",frame:GetName().."Title"..i,"LEFT",EARTHTREE_EXPAND_INDENT*(ldepth)-2, 2 ); getglobal(frame:GetName().."Title"..i.."Check"):Show(); -- If might be checked... if ( item.checked ) then getglobal(frame:GetName().."Title"..i.."Check"):SetChecked(1); else getglobal(frame:GetName().."Title"..i.."Check"):SetChecked(nil); end -- If its disabled... if ( item.checkDisabled ) then getglobal(frame:GetName().."Title"..i.."Check"):Disable(); else getglobal(frame:GetName().."Title"..i.."Check"):Enable(); end -- Indent ldepth = ldepth + 1; else getglobal(frame:GetName().."Title"..i.."Check"):Hide(); end -- Radio Box? if ( item.radio ) then -- Indent the check symbol getglobal(frame:GetName().."Title"..i.."Radio"):SetPoint("LEFT",frame:GetName().."Title"..i,"LEFT",EARTHTREE_EXPAND_INDENT*(ldepth)-2, 2 ); getglobal(frame:GetName().."Title"..i.."Radio"):Show(); -- If might be checked... if ( item.radioSelected ) then getglobal(frame:GetName().."Title"..i.."Radio"):SetChecked(1); else getglobal(frame:GetName().."Title"..i.."Radio"):SetChecked(nil); end -- If its disabled... if ( item.radioDisabled ) then getglobal(frame:GetName().."Title"..i.."Radio"):Disable(); else getglobal(frame:GetName().."Title"..i.."Radio"):Enable(); end -- Indent ldepth = ldepth + 1; else getglobal(frame:GetName().."Title"..i.."Radio"):Hide(); end -- display the highlight if ( frame.highlight ) then -- Check if its the selected item if ( EarthTree_GetSelected(frame) == item ) then --- Highlight Sizes if ( frame.highlightSize == "short" ) then getglobal(frame:GetName().."HighlightFrame"):ClearAllPoints(); getglobal(frame:GetName().."HighlightFrame"):SetPoint("TOPLEFT", frame:GetName().."Title"..i, "TOPLEFT", EARTHTREE_EXPAND_INDENT*(ldepth), -1);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -