I can get the world position of panels, and I have a push/pop method that is ready to have some code put into it.
But look, here is the problem with your method:
I can't have subpanels clip and their parents with a different clip. That's why I can't just use g.setWorldClip, because you can't "stack" the clips.
That's why you keep track of clips using a stack like Dajoh said.
Here's a really quick example I wrote up in lua:
local clip = {stack = {{x =0, y = 0, w = 3000, h = 3000}}, current = {x =0, y = 0, w = 3000, h = 3000}}
function clip.push(x, y, w, h)
local c = clip.current
-- If the new region is smaller in any way than the old region, compress the current region to fit.
if(x + w < c.x + c.w) then c.w = w end
if(y + h < c.y + c.h) then c.h = h end
if(x > c.x) then c.x = x end
if(y > c.y) then c.y = y end
table.insert(clip.stack, {x = x, y = y, w = w, h = h})
end
function clip.pop()
if(#clip.stack <= 0) then return end
-- Remove the "current" stack
table.remove(clip.stack, #clip.stack)
local c = clip.current
local o = clip.stack[#clip.stack]
-- If the old region on the stack is bigger in any way, expand the relevant component to fit.
if(o.x + o.w > c.x + c.w) then c.w = o.w end
if(o.y + o.h > c.y + c.h) then c.h = o.h end
if(o.x < c.x) then c.x = o.x end
if(o.y < c.y) then c.y = o.y end
end
function clip.getRegion()
return clip.current.x, clip.current.y, clip.current.w, clip.current.h
end
-- Tests
print("start: ", clip.getRegion())
clip.push(10, 10, 200, 200)
print("push: ", clip.getRegion())
clip.push(50, 10, 50, 200)
print("push: ", clip.getRegion())
clip.push(10, 70, 200, 130)
print("push: ", clip.getRegion())
clip.pop()
print("pop: ", clip.getRegion())
clip.pop()
print("pop: ", clip.getRegion())
clip.pop()
print("pop: ", clip.getRegion())
It outputs this, which is the expected output. It's basically the smallest possible region that doesn't expand beyond any of the given points.
Code:
lua clip.lua
start: 0 0 3000 3000
push: 10 10 200 200
push: 50 10 50 200
push: 50 70 50 130
pop: 50 10 50 200
pop: 10 10 200 200
pop: 0 0 3000 3000