Created
August 14, 2018 20:31
-
-
Save botder/d8022a81b8613b6c863aae622dca15b2 to your computer and use it in GitHub Desktop.
Element tree visualization
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- | |
-- Element tree | |
-- | |
local screenWidth, screenHeight = guiGetScreenSize() | |
local function getCenteredPosition(width, height) | |
return (screenWidth - width) / 2, (screenHeight - height) / 2 | |
end | |
local function createChildrenStack(element) | |
local children = getElementChildren(element) | |
return { | |
index = 0, | |
length = #children, | |
children = children, | |
} | |
end | |
local function getGUIElementTreeList(resource) | |
local treeList = {} | |
local guiRoot = getResourceGUIElement(resource) | |
local stack = { createChildrenStack(guiRoot) } | |
local level = 1 | |
local indent = "" | |
while level > 0 do | |
local s = stack[level] | |
local lastChild = ((s.index + 1) == s.length) | |
if s.index < s.length then | |
s.index = s.index + 1 | |
local element = s.children[s.index] | |
local elementType = getElementType(element) | |
local text = guiGetText(element) | |
local prefix = lastChild and "└" or "├" | |
treeList[#treeList + 1] = ("%s%s [%s] %s"):format(indent, prefix, elementType, text) | |
if getElementChild(element, 0) then | |
level = level + 1 | |
stack[level] = createChildrenStack(element) | |
if lastChild then | |
indent = indent .." " | |
else | |
indent = indent .."│ " | |
end | |
end | |
else | |
level = level - 1 | |
indent = utf8.sub(indent, 1, -4) | |
end | |
end | |
return treeList | |
end | |
function createElementTreeWindow(resource) | |
local width, height = 400, 600 | |
local x, y = 10, 10 | |
-- local x, y = getCenteredPosition(width, height) | |
local resourceName = getResourceName(resource) | |
local window = guiCreateWindow(x, y, width, height, "GUI element tree of ".. resourceName, false) | |
guiWindowSetSizable(window, false) | |
local gl_width = width - 20 | |
local gl_height = height - 40 | |
local gridlist = guiCreateGridList(10, 30, gl_width, gl_height, false, window) | |
local column = guiGridListAddColumn(gridlist, "Element", 1.0) | |
guiGridListSetColumnWidth(gridlist, column, gl_width - 30, false) | |
local row = guiGridListAddRow(gridlist) | |
guiGridListSetItemText(gridlist, row, column, resourceName, true, false) | |
local treeList = getGUIElementTreeList(resource) | |
for i = 1, #treeList do | |
local rowText = treeList[i] | |
guiGridListAddRow(gridlist, rowText) | |
end | |
end | |
local admin = getResourceFromName("admin") | |
if admin then | |
createElementTreeWindow(admin) | |
else | |
createResourceSelectorWindow( | |
function (resource) | |
createElementTreeWindow(resource) | |
end | |
) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- | |
-- Resource selector | |
-- | |
local screenWidth, screenHeight = guiGetScreenSize() | |
local function getCenteredPosition(width, height) | |
return (screenWidth - width) / 2, (screenHeight - height) / 2 | |
end | |
local function getResourceNameList() | |
local nameList = {} | |
for index, resourceRoot in pairs(getElementsByType("resource")) do | |
local id = getElementID(resourceRoot) | |
if getResourceFromName(id) then | |
nameList[#nameList + 1] = id | |
end | |
end | |
return nameList | |
end | |
function createResourceSelectorWindow(callback) | |
local width, height = 200, 300 | |
local x, y = 10, 10 | |
-- local x, y = getCenteredPosition(width, height) | |
local window = guiCreateWindow(x, y, width, height, "Select a resource", false) | |
guiWindowSetSizable(window, false) | |
local gl_width = width - 20 | |
local gl_height = height - 40 | |
local gridlist = guiCreateGridList(10, 30, gl_width, gl_height, false, window) | |
local column = guiGridListAddColumn(gridlist, "Resource", 1.0) | |
guiGridListSetColumnWidth(gridlist, column, gl_width - 30, false) | |
local resourceNameList = getResourceNameList() | |
for i = 1, #resourceNameList do | |
local name = resourceNameList[i] | |
guiGridListAddRow(gridlist, name) | |
end | |
addEventHandler("onClientGUIDoubleClick", gridlist, | |
function () | |
local row = guiGridListGetSelectedItem(gridlist) | |
if not row or row == -1 then return end | |
local resourceName = guiGridListGetItemText(gridlist, row, column) | |
local resource = getResourceFromName(resourceName) | |
if not resource then return end | |
destroyElement(window) | |
callback(resource) | |
end, | |
false) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment