Skip to content

Commit

Permalink
Make debugger compatible with new world detection
Browse files Browse the repository at this point in the history
  • Loading branch information
jackTabsCode committed Sep 11, 2024
1 parent 4440052 commit 4ae00e0
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 45 deletions.
1 change: 1 addition & 0 deletions example/src/shared/start.luau
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ local function start(containers)
local cx = { world = world, state = state, widgets = debugger:getWidgets() }

local loop = Matter.Loop.new(cx)
loop:setWorlds({ world })

-- Set up hot reloading

Expand Down
10 changes: 0 additions & 10 deletions lib/debugger/debugger.luau
Original file line number Diff line number Diff line change
Expand Up @@ -271,16 +271,6 @@ function Debugger:_enable()
return
end

-- TODO: Find a better way for the user to specify the world.
if not self.debugWorld then
for _, object in self.loop._state do
if getmetatable(object) == World then
self.debugWorld = object
break
end
end
end

self.enabled = true
self.loop.profiling = self.loop.profiling or {}

Expand Down
63 changes: 35 additions & 28 deletions lib/debugger/ui.luau
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,13 @@ local function ui(debugger, loop)
})

local objectStack = plasma.useState({})
local worldViewOpen, setWorldViewOpen = plasma.useState(false)
local worldExists = debugger.debugWorld ~= nil
local worldViewOpen, setWorldViewOpen = plasma.useState()

if debugger.hoverEntity and worldExists then
if debugger.debugWorld:contains(debugger.hoverEntity) then
custom.hoverInspect(debugger.debugWorld, debugger.hoverEntity, custom)
if debugger.hoverEntity then
for _, world in loop._worlds do
if world:contains(debugger.hoverEntity) then
custom.hoverInspect(world, debugger.hoverEntity, custom)
end
end
end

Expand Down Expand Up @@ -79,38 +80,44 @@ local function ui(debugger, loop)

local items = {}

for index, object in loop._state do
if type(object) ~= "table" then
continue
end
for index, world in loop._worlds do
local selected = worldViewOpen == index

local isWorld = getmetatable(object) == World
table.insert(items, {
index = index,
text = "World " .. index,
icon = "🌐",
object = world,
selected = selected,
isWorld = true,
})
end

local selected = (#objectStack > 0 and object == objectStack[#objectStack].value)
or (debugger.debugWorld == object and worldViewOpen)
for index, value in loop._state do
if type(value) ~= "table" then
continue
end

local name = debugger.loopParameterNames[index]
local defaultName = (if isWorld then "World" else "table") .. " " .. index
local selected = (#objectStack > 0 and value == objectStack[#objectStack].value)

table.insert(items, {
text = if name then name else defaultName,
icon = if isWorld then "🌐" else "{}",
object = object,
index = index,
text = "table " .. index,
icon = "{}",
object = value,
selected = selected,
isWorld = isWorld,
isWorld = false,
})
end

local selectedState = custom.selectionList(items):selected()

if selectedState then
if selectedState.isWorld then
if worldViewOpen and debugger.debugWorld == selectedState.object then
debugger.debugWorld = nil
setWorldViewOpen(false)
if worldViewOpen == selectedState.index then
setWorldViewOpen(nil)
else
debugger.debugWorld = selectedState.object
setWorldViewOpen(true)
setWorldViewOpen(selectedState.index)
end
else
local previousFirstValue = if objectStack[1] then objectStack[1].value else nil
Expand Down Expand Up @@ -228,16 +235,16 @@ local function ui(debugger, loop)
end)

debugger.parent = custom.container(function()
if debugger.debugWorld and worldViewOpen then
local closed = custom.worldInspect(debugger, objectStack)
if worldViewOpen then
local closed = custom.worldInspect(debugger, loop._worlds[worldViewOpen], objectStack)

if closed then
setWorldViewOpen(false)
setWorldViewOpen(nil)
end
end

if debugger.debugWorld and debugger.debugEntity then
custom.entityInspect(debugger)
if worldViewOpen and debugger.debugEntity then
custom.entityInspect(debugger, loop._worlds[worldViewOpen])
end

if #objectStack > 0 then
Expand Down
8 changes: 4 additions & 4 deletions lib/debugger/widgets/entityInspect.luau
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ local formatTable = formatTableModule.formatTable
local FormatMode = formatTableModule.FormatMode

return function(plasma)
return plasma.widget(function(debugger)
return plasma.widget(function(debugger, world)
local style = plasma.useStyle()

local closed = plasma
.window({
title = string.format("Entity %d", debugger.debugEntity),
closable = true,
}, function()
if not debugger.debugWorld:contains(debugger.debugEntity) then
if not world:contains(debugger.debugEntity) then
debugger.debugEntity = nil
return
end
Expand All @@ -28,7 +28,7 @@ return function(plasma)

plasma.row(function()
if plasma.button("despawn"):clicked() then
debugger.debugWorld:despawn(debugger.debugEntity)
world:despawn(debugger.debugEntity)
debugger.debugEntity = nil
end
end)
Expand All @@ -39,7 +39,7 @@ return function(plasma)

local items = { { "Component", "Data" } }

for component, data in debugger.debugWorld:_getEntity(debugger.debugEntity) do
for component, data in world:_getEntity(debugger.debugEntity) do
table.insert(items, {
tostring(component),
formatTable(data, FormatMode.Long),
Expand Down
4 changes: 1 addition & 3 deletions lib/debugger/widgets/worldInspect.luau
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ local BY_COMPONENT_NAME = "ComponentName"
local BY_ENTITY_COUNT = "EntityCount"

return function(plasma)
return plasma.widget(function(debugger, objectStack)
return plasma.widget(function(debugger, world, objectStack)
local style = plasma.useStyle()

local world = debugger.debugWorld

local cache, setCache = plasma.useState()

local sortType, setSortType = plasma.useState(BY_COMPONENT_NAME)
Expand Down

0 comments on commit 4ae00e0

Please sign in to comment.