SLua: HUD Positioning

HUDs can detect where they are attached and make appropriate adjustments to ensure they are not hidden off the edge of the viewer's window.

There is a bit of magic involved in setting the actual offsets as it depends on the geometry of all of the links in the HUD but the basics are here.

-- HUD Positioing
-- Position HUD at attach-time to ensure it is visible

local VERBOSE = FALSE;

-- HUD Positioning offsets
-- These values are usually tweaked by trial-and-error to match the 
-- specific HUD being used.  If the root is centered starting with
-- half of the size from the root to each edge usually works well.

-- Offsets of 0.00 put the centerlines at the edge of the window
-- Add half the width or height to get the whole face on-screen
-- and 0.03 to leave a small border from the edge

local top_offset = -0.08;

-- Add about 0.02 to get the center line above the toolbutton bar (SL viewer)
local bot_offset = 0.07;

local left_offset = -0.13;

local right_offset = 0.13;

local center_offset = 0.00;

-- Adjust this higher to put toward the bottom of the 'stack' of HUDs on the screen,
-- and lower to bring it toward the front
local front_offset = 0.00;

last_attach = 0;

function attach(id: uuid)
    local attach_point = ll.GetAttached();
    if id ~= NULL_KEY and attach_point > 0 and attach_point ~= last_attach then
        -- HUD rotation needs to know if we are on top or bottom
        attach_bottom = FALSE;

        -- Nasty if else block
        if attach_point == ATTACH_HUD_TOP_LEFT then
            ll.SetPos(vector(front_offset, left_offset, top_offset));
        elseif attach_point == ATTACH_HUD_TOP_CENTER then
            ll.SetPos(vector(front_offset, center_offset, top_offset));
        elseif attach_point == ATTACH_HUD_TOP_RIGHT then
            ll.SetPos(vector(front_offset, right_offset, top_offset));
        elseif attach_point == ATTACH_HUD_CENTER_1 then
            ll.SetPos(vector(front_offset, left_offset, 0.0));
        elseif attach_point == ATTACH_HUD_CENTER_2 then
            ll.SetPos(vector(front_offset, right_offset, 0.0));
        elseif attach_point == ATTACH_HUD_BOTTOM_LEFT then
            ll.SetPos(vector(front_offset, left_offset, bot_offset));
        elseif attach_point == ATTACH_HUD_BOTTOM then
            ll.SetPos(vector(front_offset, center_offset, bot_offset));
        elseif attach_point == ATTACH_HUD_BOTTOM_RIGHT then
            ll.SetPos(vector(front_offset, right_offset, bot_offset));
        end
        last_attach = attach_point;
    end
end