singe/testScripts/scene39.singe
2026-09-07 23:44:37 -05:00

93 lines
3.1 KiB
Text

-- GUI on a surface, pointed at: the scene37 monitor again, with sceneProbeGui asked where on the
-- GUI the ray through several overlay points lands. The four corners of the screen plane are
-- projected with sceneProject and probed back, which must give the GUI's corners; a point off the
-- monitor must give nil. The same probe is what the engine uses when the mouse or a light gun
-- points at a screen in the scene while that GUI takes input, so a click on the 3D monitor presses
-- the button under it. Results go to the console and the overlay.
local font = fontLoad("Singe/FreeSansBold.ttf", 22)
local frames = 0
local lines = {}
fontSelect(font)
discPlay()
sceneEnable(true)
sceneSetBackground(20, 20, 40, 255)
sceneSetAmbient(80, 80, 90)
local gui = guiNew(720, 480)
local doc = guiLoad(gui, "testScripts/scene38.rml")
guiSetInput(gui, true)
guiSetHandler(gui, doc, "go", "click", function() lines[#lines + 1] = "Start pressed on the monitor" end)
local screen = materialNew()
materialSetGui(screen, gui)
materialSetUnlit(screen, true)
local cabinet = materialNew()
materialSetColor(cabinet, 60, 60, 70)
local body = nodeNew()
nodeSetMesh(body, meshBox(3.2, 2.4, 0.4), cabinet)
nodeSetPosition(body, -0.6, 0.2, -0.3)
nodeSetRotation(body, 0, 25, 0)
local tv = nodeNew(body)
nodeSetMesh(tv, meshPlane(3.0, 2.0), screen)
nodeSetPosition(tv, 0, 0, 0.21)
nodeSetRotation(tv, 90, 0, 0)
local camera = nodeNew()
nodeSetPosition(camera, 0, 0.8, 5.5)
nodeLookAt(camera, 0, 0, 0)
cameraSet(camera)
-- Helper nodes at the plane's corners and centre (the plane is 3 by 2 in its node's XZ plane before
-- the 90 degree turn), made now so their world positions exist by the time they are read.
local w, h = guiGetWidth(gui), guiGetHeight(gui)
local corners = { { "top left", -1.5, 0, -1, 0, 0 }, { "top right", 1.5, 0, -1, w, 0 }, { "bottom left", -1.5, 0, 1, 0, h }, { "bottom right", 1.5, 0, 1, w, h }, { "centre", 0, 0, 0, w / 2, h / 2 } }
for _, c in ipairs(corners) do
c.node = nodeNew(tv)
nodeSetPosition(c.node, c[2], c[3], c[4])
end
local function probe(label, sx, sy, expectX, expectY)
local g, x, y = sceneProbeGui(sx, sy)
local text
if g == nil then
text = label .. ": off the monitor"
if expectX ~= nil then
text = text .. " FAIL"
end
else
text = string.format("%s: gui %d at %d,%d", label, g, x, y)
if expectX ~= nil and (math.abs(x - expectX) > 12 or math.abs(y - expectY) > 12) then
text = text .. string.format(" FAIL (expected %d,%d)", expectX, expectY)
end
end
lines[#lines + 1] = text
debugPrint("scene39 " .. text)
end
function onOverlayUpdate()
frames = frames + 1
overlayClear()
colorForeground(255, 255, 255, 255)
if frames == 20 then
for _, c in ipairs(corners) do
local hx, hy, hz = nodeGetWorldPosition(c.node)
local sx, sy = sceneProject(hx, hy, hz)
probe(c[1], sx, sy, c[5], c[6])
end
probe("far corner of the overlay", 5, 5, nil, nil)
end
for i, text in ipairs(lines) do
fontPrint(10, 10 + (i - 1) * 24, text)
end
if frames == 40 then
singeScreenshot()
end
if frames == 60 then
singeQuit()
end
return OVERLAY_UPDATED
end