/*****************************************************************************
 * $LastChangedDate: 2011-03-15 14:18:37 -0400 (Tue, 15 Mar 2011) $
 * @file
 * @author  Jim E. Brooks  http://www.palomino3d.org
 * @brief   Interface of C functions for Lua scripts.
 * @remarks @remarks module.txt documents how to export new C++ classes to Lua.
 *
 * ----------------------------
 * -- THIS IS AUTO-GENERATED --
 * ----------------------------
 *
 *//*
 * LEGAL:   COPYRIGHT (C) 2008 JIM E. BROOKS
 *          THIS SOURCE CODE IS RELEASED UNDER THE TERMS
 *          OF THE GNU GENERAL PUBLIC LICENSE VERSION 2 (GPL 2).
 *****************************************************************************/

#include "base/module.hh"
using namespace base;
#include "hud/module.hh"
using namespace hud;
#include "lua_bind/module.hh"
#include "lua_bind/bind.hh"

namespace lua {


namespace bind {

#define BIND_CLASS_NAME      "HudText"
#define BIND_METATABLE_NAME  LUABIND_METATABLE_STRING(BIND_CLASS_NAME)

//==============================================================================
// C functions called by Lua that access C++ objects.
//==============================================================================
// Need a function to check typesig because of macro/template limitations.
#if ! DOXYGEN
INTERN void
CheckTypesigHudText( shptr<HudText> obj )
{
    CHECK_TYPESIG(obj,TYPESIG_HUD_TEXT);
}
#endif // ! DOXYGEN

/*****************************************************************************
 * Lua's garbage collector indirectly invokes this C++ destructor.
 *****************************************************************************/
INTERN int 
HudText_Destroy( lua_State* L ) 
{ 
   try { 

        return DESTROY_USERDATA_SHPTR<HudText>( L, CheckTypesigHudText );
    
   } CATCH_LUA_EXCEPTION 
   return 0; 
} 

/*****************************************************************************
 * HudText:New( { text='', orientation=, x=, y=, fontSize=, fontName= } )
 * text and orientation are required.
 *****************************************************************************/
INTERN int 
HudText_New( lua_State* L ) 
{ 
   try { 

        // 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 );
    
   } CATCH_LUA_EXCEPTION 
   return 0; 
} 

/*****************************************************************************
 * HudText:SetColor( r, g, b, a )  (fp,fp,fp,fp)
 *****************************************************************************/
INTERN int 
HudText_SetColor( lua_State* L ) 
{ 
   try { 

        const RGBA color = POP_RGBA( L );
        shptr<HudText> hudText = POP_USERDATA_AS_SHPTR<HudText>( L );
        CHECK_TYPESIG( hudText, TYPESIG_HUD_TEXT );
        hudText->SetColor( color );
    
   } CATCH_LUA_EXCEPTION 
   return 0; 
} 

/*****************************************************************************
 * HudText:SetFontSize( fp )
 *****************************************************************************/
INTERN int 
HudText_SetFontSize( lua_State* L ) 
{ 
   try { 

        const fp fontSize = POP_FP( L );
        shptr<HudText> hudText = POP_USERDATA_AS_SHPTR<HudText>( L );
        CHECK_TYPESIG( hudText, TYPESIG_HUD_TEXT );
        hudText->SetFontSize( fontSize );
    
   } CATCH_LUA_EXCEPTION 
   return 0; 
} 

/*****************************************************************************
 * HudText:SetPosition3D( x,y,z|Vec3 )
 * Set the position of a HudText by projecting a 3D coordinate into 2D.
 *****************************************************************************/
INTERN int 
HudText_SetPosition3D( lua_State* L ) 
{ 
   try { 

        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 );
    
   } CATCH_LUA_EXCEPTION 
   return 0; 
} 

/*****************************************************************************
 * HudText:SetText( string )
 *****************************************************************************/
INTERN int 
HudText_SetText( lua_State* L ) 
{ 
   try { 

        const string text = POP_STRING( L );
        shptr<HudText> hudText = POP_USERDATA_AS_SHPTR<HudText>( L );
        CHECK_TYPESIG( hudText, TYPESIG_HUD_TEXT );
        hudText->SetText( text );
    
   } CATCH_LUA_EXCEPTION 
   return 0; 
} 

//==============================================================================
// Register binding with Lua. 
//==============================================================================

#if ! DOXYGEN 

INTERN const struct luaL_reg sMethodTable[] =
{
  { "New", HudText_New },
  { "__gc", HudText_Destroy },
  { "SetColor", HudText_SetColor },
  { "SetFontSize", HudText_SetFontSize },
  { "SetPosition3D", HudText_SetPosition3D },
  { "SetText", HudText_SetText },
  { NULL, NULL }
};

void
RegisterHudText( lua_State* L )
{
    REGISTER_BINDING( L,
                      BIND_METATABLE_NAME,
                      BIND_CLASS_NAME,
                      sMethodTable );
}

#endif // ! DOXYGEN 

} // namespace bind 

} // namespace lua 
