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

integer 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.

// Add about -0.05 to get below the address bar and shortcut buttons
float top_offset = -0.08;

// Add about 0.04 to get above the button bar
float bot_offset = 0.07;

float left_offset = -0.03;

float right_offset = 0.03;

float 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
float front_offset = 0.00;

integer last_attach = 0;

default {
    attach(key id) {
        integer attach_point = llGetAttached();
        if (id != NULL_KEY && attach_point > 0 && attach_point != last_attach) {

            // 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) {
                llSetPos(<front_offset, left_offset, top_offset>);
            }
            else if (attach_point == ATTACH_HUD_TOP_CENTER) {
                llSetPos(<front_offset, center_offset, top_offset>);
            }
            else if (attach_point == ATTACH_HUD_TOP_RIGHT) {
                llSetPos(<front_offset, right_offset, top_offset>);
            }
            else if (attach_point == ATTACH_HUD_CENTER_1) {
                llSetPos(<front_offset, left_offset, 0.0>);
            }
            else if (attach_point == ATTACH_HUD_CENTER_2) {
                llSetPos(<front_offset, right_offset, 0.0>);
            }
            else if (attach_point == ATTACH_HUD_BOTTOM_LEFT) {
                llSetPos(<front_offset, left_offset, bot_offset>);
            }
            else if (attach_point == ATTACH_HUD_BOTTOM) {
                llSetPos(<front_offset, center_offset, bot_offset>);
            }
            else if (attach_point == ATTACH_HUD_BOTTOM_RIGHT) {
                llSetPos(<front_offset, right_offset, bot_offset>);
            }
            last_attach = attach_point;
        }
    }
}