From 1f8dfab3edb723da5ca3064febba8256c9abbbb1 Mon Sep 17 00:00:00 2001 From: benbrimeyer Date: Sat, 2 Mar 2024 21:33:59 -0800 Subject: [PATCH] as-is, from video demo --- example/src/shared/start.lua | 8 + lib/debugger/widgets/worldInspect.lua | 214 +++++++++++++++----------- 2 files changed, 135 insertions(+), 87 deletions(-) diff --git a/example/src/shared/start.lua b/example/src/shared/start.lua index 5e89e2f2..d4dcb8a9 100644 --- a/example/src/shared/start.lua +++ b/example/src/shared/start.lua @@ -73,6 +73,14 @@ local function start(containers) loop:scheduleSystems(firstRunSystems) firstRunSystems = nil + debugger.queryForWatchedEntities = function(world) + return world:query(components.Model) + end + + debugger.labelForWatchedEntities = function(entityId) + return world:get(entityId, components.Model).model.Name + end + debugger:autoInitialize(loop) -- Begin running our systems diff --git a/lib/debugger/widgets/worldInspect.lua b/lib/debugger/widgets/worldInspect.lua index 1281081e..b7bb2b4b 100644 --- a/lib/debugger/widgets/worldInspect.lua +++ b/lib/debugger/widgets/worldInspect.lua @@ -11,6 +11,7 @@ return function(plasma) -- TODO #97 Implement sorting by descending as well. local ascendingOrder, _ = plasma.useState(false) local skipIntersections, setSkipIntersections = plasma.useState(true) + local filterForWatched, setFilteredForWatched = plasma.useState(false) local debugComponent, setDebugComponent = plasma.useState() local closed = plasma @@ -53,136 +54,175 @@ return function(plasma) key = "Raw World", } end - end) - plasma.row({ padding = 15 }, function() - if plasma.checkbox("Show intersections", { checked = not skipIntersections }):clicked() then - setSkipIntersections(not skipIntersections) + if plasma.checkbox("Filter watched", { checked = filterForWatched }):clicked() then + setFilteredForWatched(not filterForWatched) end end) - local items = {} - for component, count in cache.uniqueComponents do - table.insert(items, { - count, - tostring(component), - selected = debugComponent == component, - component = component, - }) + if not filterForWatched then + plasma.row({ padding = 15 }, function() + if plasma.checkbox("Show intersections", { checked = not skipIntersections }):clicked() then + setSkipIntersections(not skipIntersections) + end + end) end - table.sort(items, function(a, b) - if ascendingOrder then - return a[1] < b[1] + if filterForWatched then + local items = {} + local shouldShowLabels = debugger.labelForWatchedEntities ~= nil + + for entityId, _ in debugger.queryForWatchedEntities(debugger.debugWorld) do + table.insert(items, { + entityId, + if shouldShowLabels then debugger.labelForWatchedEntities(entityId) else nil, + selected = debugger.debugEntity == entityId, + }) end + table.sort(items, function(a, b) + return a[1] < b[1] + end) - -- Default to alphabetical - return a[2] < b[2] - end) + table.insert(items, 1, { "Entity ID", if shouldShowLabels then "Label" else nil }) - table.insert(items, 1, { "Count", "Component" }) + plasma.row({ padding = 30 }, function() + local selectedRow = plasma + .table(items, { + width = 200, + headings = true, + selectable = true, + font = Enum.Font.Code, + }) + :selected() - plasma.row({ padding = 30 }, function() - local selectedRow = plasma - .table(items, { - width = 200, - headings = true, - selectable = true, - font = Enum.Font.Code, + if selectedRow then + debugger.debugEntity = selectedRow[1] + end + end) + else + local items = {} + for component, count in cache.uniqueComponents do + table.insert(items, { + count, + tostring(component), + selected = debugComponent == component, + component = component, }) - :selected() - - if selectedRow then - setDebugComponent(selectedRow.component) end - if debugComponent then - local items = { { "Entity ID", tostring(debugComponent) } } - local intersectingComponents = {} + table.sort(items, function(a, b) + if ascendingOrder then + return a[1] < b[1] + end - local intersectingData = {} + -- Default to alphabetical + return a[2] < b[2] + end) - for entityId, data in world:query(debugComponent) do - table.insert(items, { - entityId, - formatTable(data), + table.insert(items, 1, { "Count", "Component" }) - selected = debugger.debugEntity == entityId, + plasma.row({ padding = 30 }, function() + local selectedRow = plasma + .table(items, { + width = 200, + headings = true, + selectable = true, + font = Enum.Font.Code, }) + :selected() - intersectingData[entityId] = {} + if selectedRow then + setDebugComponent(selectedRow.component) + end - if skipIntersections then - continue - end + if debugComponent then + local items = { { "Entity ID", tostring(debugComponent) } } + local intersectingComponents = {} - for component, value in world:_getEntity(entityId) do - if component == debugComponent then + local intersectingData = {} + + for entityId, data in world:query(debugComponent) do + table.insert(items, { + entityId, + formatTable(data), + + selected = debugger.debugEntity == entityId, + }) + + intersectingData[entityId] = {} + + if skipIntersections then continue end - local index = table.find(intersectingComponents, component) + for component, value in world:_getEntity(entityId) do + if component == debugComponent then + continue + end - if not index then - table.insert(intersectingComponents, component) + local index = table.find(intersectingComponents, component) - index = #intersectingComponents - end + if not index then + table.insert(intersectingComponents, component) - intersectingData[entityId][index] = value - end - end + index = #intersectingComponents + end - for i, item in items do - if i == 1 then - for _, component in intersectingComponents do - table.insert(item, tostring(component)) + intersectingData[entityId][index] = value end - - continue end - for i = 1, #intersectingComponents do - local data = intersectingData[item[1]][i] + for i, item in items do + if i == 1 then + for _, component in intersectingComponents do + table.insert(item, tostring(component)) + end - table.insert(item, if data then formatTable(data) else "") - end - end + continue + end - plasma.useKey(tostring(debugComponent)) + for i = 1, #intersectingComponents do + local data = intersectingData[item[1]][i] - local tableWidget = plasma.table(items, { - font = Enum.Font.Code, - selectable = true, - headings = true, - }) + table.insert(item, if data then formatTable(data) else "") + end + end - local selectedRow = tableWidget:selected() - local hovered = tableWidget:hovered() + plasma.useKey(tostring(debugComponent)) - if selectedRow then - debugger.debugEntity = selectedRow[1] - end + local tableWidget = plasma.table(items, { + font = Enum.Font.Code, + selectable = true, + headings = true, + }) - if hovered then - local entityId = hovered[1] + local selectedRow = tableWidget:selected() + local hovered = tableWidget:hovered() - if debugger.debugEntity == entityId or not world:contains(entityId) then - return + if selectedRow then + debugger.debugEntity = selectedRow[1] end - if debugger.findInstanceFromEntity then - local model = debugger.findInstanceFromEntity(entityId) + if hovered then + local entityId = hovered[1] + + if debugger.debugEntity == entityId or not world:contains(entityId) then + return + end + + if debugger.findInstanceFromEntity then + local model = debugger.findInstanceFromEntity(entityId) - if model then - plasma.highlight(model, { - fillColor = style.primaryColor, - }) + if model then + plasma.highlight(model, { + fillColor = style.primaryColor, + }) + end end end end - end - end) + end) + end end) :closed()