-- $LastChangedDate: 2010-04-16 23:43:07 -0400 (Fri, 16 Apr 2010) $
-- Macros written in Lua to expand C++ code for bindings between C++/Lua.
-- Avoid lua_/LUA_ which is reserved by Lua.

bindTable = {

class = "HudText",
typesig = "TYPESIG_HUD_TEXT",
luaCanInstantiate = true,

headers =
[[
#include "base/module.hh"
using namespace base;
#include "hud/module.hh"
using namespace hud;
]],

func_New = {
    comments = "HudText:New( { text='', orientation=, x=, y=, fontSize=, fontName= } )\n * text and orientation are required.",
    code = [[
        // BEGIN_OPTIONAL_TABLE_MEMBER macro isn't appropriate here because
        // of the need to catch an exception in the C++ core.

        // Restore stack in case a C++ exception is thrown during
        // during stack operations involved in accessing optional table members.
        SAVE_STACK( L );

        // Fill a HudText::Args struct with optional members of a Lua table.
        HudText::Args hudTextArgs;

        // .mText
        try
        {
            hudTextArgs.mText = GET_MEMBER_AS_STRING( L, "text" );
        }
        CATCH_LUA_EXCEPTION

        // .mOrientation
        try
        {
            const string orientation = GET_MEMBER_AS_STRING( L, "orientation" );
            if ( orientation == "TOP_LEFT" )
                hudTextArgs.mOrientation = HudText::eOrientation_TOP_LEFT;
            else if ( orientation == "TOP_RIGHT" )
                hudTextArgs.mOrientation = HudText::eOrientation_TOP_RIGHT;
            else if ( orientation == "BOTTOM_LEFT" )
                hudTextArgs.mOrientation = HudText::eOrientation_BOTTOM_LEFT;
            else if ( orientation == "BOTTOM_RIGHT" )
                hudTextArgs.mOrientation = HudText::eOrientation_BOTTOM_RIGHT;
            else if ( orientation == "CENTER" )
                hudTextArgs.mOrientation = HudText::eOrientation_CENTER;
        }
        CATCH_LUA_EXCEPTION

        // .mPos
        try
        {
            hudTextArgs.mPos[XX] = GET_MEMBER_AS_NUMBER( L, "x", OPTIONAL );
            hudTextArgs.mPos[YY] = GET_MEMBER_AS_NUMBER( L, "y", OPTIONAL );
        }
        catch( const LuaException& ) { } // ignore
        RESTORE_STACK( L );

        // .mFont (fontName)
        // -- ORDER-DEPEDENT Set font name before any other Font object settings. --
        // If exception occurs, hudTextArgs.mFont should be the default font.
        try
        {
            const string fontName = GET_MEMBER_AS_STRING( L, "fontName", OPTIONAL );
            hudTextArgs.mFont = GET_FONT_MAP().GetFont( fontName );
        }
        catch( const LuaException& ) { } // ignore Lua exception if fontName key missing (ok)
        catch( const Exception& ) { } // ignore C++ exception if font unavailable (ok)
        RESTORE_STACK( L );

        // .mFont (size)
        try
        {
            hudTextArgs.mFont.SetSize( GET_MEMBER_AS_NUMBER( L, "fontSize", OPTIONAL ) );
        }
        catch( const LuaException& ) { } // ignore
        RESTORE_STACK( L );

        // Construct HudText from HudText::Args.
        shptr<HudText> hudText = new HudText( hudTextArgs );
    
        // Attach to the Hud.
        GET_HUD().Attach( hudText->GetNode() );

        // Pop the passed table arg.
        lua_pop( L, 1 );

        return NEW_USERDATA_SHPTR<HudText>( L, hudText, BIND_METATABLE_NAME );
    ]]
},

func_Destroy = {
    comments = "Lua's garbage collector indirectly invokes this C++ destructor.",
    code = [[
        return DESTROY_USERDATA_SHPTR<HudText>( L, CheckTypesigHudText );
    ]]
},

func_SetText = {
    comments = "HudText:SetText( string )",
    code = [[
        const string text = POP_STRING( L );
        shptr<HudText> hudText = POP_USERDATA_AS_SHPTR<HudText>( L );
        CHECK_TYPESIG( hudText, TYPESIG_HUD_TEXT );
        hudText->SetText( text );
    ]]
},

func_SetColor = {
    comments = "HudText:SetColor( r, g, b, a )  (fp,fp,fp,fp)",
    code = [[
        const RGBA color = POP_RGBA( L );
        shptr<HudText> hudText = POP_USERDATA_AS_SHPTR<HudText>( L );
        CHECK_TYPESIG( hudText, TYPESIG_HUD_TEXT );
        hudText->SetColor( color );
    ]]
},

func_SetPosition3D = {
    comments = "HudText:SetPosition3D( x,y,z|Vec3 )\n * Set the position of a HudText by projecting a 3D coordinate into 2D.",
    code = [[
        const WorldVertex pos3 = POP_VECTOR3<WorldVertex>( L );
        shptr<HudText> hudText = POP_USERDATA_AS_SHPTR<HudText>( L );
        CHECK_TYPESIG( hudText, TYPESIG_HUD_TEXT );
        hudText->SetPosition3D( pos3 );
    ]]
},

func_SetFontSize = {
    comments = "HudText:SetFontSize( fp )",
    code = [[
        const fp fontSize = POP_FP( L );
        shptr<HudText> hudText = POP_USERDATA_AS_SHPTR<HudText>( L );
        CHECK_TYPESIG( hudText, TYPESIG_HUD_TEXT );
        hudText->SetFontSize( fontSize );
    ]]
},

}
