add Enum:ToString
This commit is contained in:
@@ -99,11 +99,24 @@ namespace
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int ToString( lua_State *L )
|
||||
{
|
||||
luaL_checktype( L, 1, LUA_TTABLE );
|
||||
|
||||
/* Look up the tostring table. If there is no metafield, then we were
|
||||
* called on the wrong type. */
|
||||
if( !luaL_getmetafield(L, 1, "tostring") )
|
||||
luaL_typerror( L, 1, "enum" );
|
||||
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
static const luaL_Reg EnumLib[] = {
|
||||
{ "GetName", GetName },
|
||||
{ "Reverse", Reverse },
|
||||
{ "ToString", ToString },
|
||||
{ NULL, NULL }
|
||||
};
|
||||
|
||||
@@ -113,14 +126,17 @@ static void PushEnumMethodTable( lua_State *L )
|
||||
}
|
||||
|
||||
/* Set up the enum table on the stack, and pop the table. */
|
||||
void Enum::SetMetatable( lua_State *L, LuaReference &EnumTable, LuaReference &EnumIndexTable, const char *szName )
|
||||
void Enum::SetMetatable( lua_State *L, LuaReference &IndexToEnumString, LuaReference &EnumStringToZeroIndex, LuaReference &EnumStringToString, const char *szName )
|
||||
{
|
||||
EnumTable.PushSelf( L );
|
||||
IndexToEnumString.PushSelf( L );
|
||||
lua_newtable( L );
|
||||
{
|
||||
lua_newtable( L );
|
||||
EnumIndexTable.PushSelf( L );
|
||||
EnumStringToZeroIndex.PushSelf( L );
|
||||
lua_setfield( L, -2, "reverse" );
|
||||
|
||||
EnumStringToString.PushSelf( L );
|
||||
lua_setfield( L, -2, "tostring" );
|
||||
|
||||
lua_pushstring( L, szName );
|
||||
lua_setfield( L, -2, "name" );
|
||||
|
||||
@@ -132,7 +148,7 @@ void Enum::SetMetatable( lua_State *L, LuaReference &EnumTable, LuaReference &En
|
||||
lua_setfield( L, -2, "__type" ); // for luaL_pushtype
|
||||
}
|
||||
lua_setmetatable( L, -2 );
|
||||
lua_pop( L, 2 );
|
||||
lua_pop( L, 1 );
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
+25
-14
@@ -18,20 +18,22 @@ int CheckEnum( lua_State *L, LuaReference &table, int iPos, int iInvalid, const
|
||||
template<typename T>
|
||||
struct EnumTraits
|
||||
{
|
||||
static LuaReference StringToEnum;
|
||||
static LuaReference EnumToString;
|
||||
static LuaReference IndexToEnumString;
|
||||
static LuaReference EnumStringToZeroIndex;
|
||||
static LuaReference EnumStringToString;
|
||||
static T Invalid;
|
||||
static const char *szName;
|
||||
};
|
||||
template<typename T> LuaReference EnumTraits<T>::StringToEnum;
|
||||
template<typename T> LuaReference EnumTraits<T>::EnumToString;
|
||||
template<typename T> LuaReference EnumTraits<T>::IndexToEnumString;
|
||||
template<typename T> LuaReference EnumTraits<T>::EnumStringToZeroIndex;
|
||||
template<typename T> LuaReference EnumTraits<T>::EnumStringToString; // same as XToString
|
||||
|
||||
namespace Enum
|
||||
{
|
||||
template<typename T>
|
||||
static T Check( lua_State *L, int iPos, bool bAllowInvalid = false )
|
||||
{
|
||||
return (T) CheckEnum( L, EnumTraits<T>::StringToEnum, iPos, EnumTraits<T>::Invalid, EnumTraits<T>::szName, bAllowInvalid );
|
||||
return (T) CheckEnum( L, EnumTraits<T>::EnumStringToZeroIndex, iPos, EnumTraits<T>::Invalid, EnumTraits<T>::szName, bAllowInvalid );
|
||||
}
|
||||
template<typename T>
|
||||
static void Push( lua_State *L, T iVal )
|
||||
@@ -44,12 +46,12 @@ namespace Enum
|
||||
}
|
||||
|
||||
/* Look up the string value. */
|
||||
EnumTraits<T>::EnumToString.PushSelf( L );
|
||||
EnumTraits<T>::IndexToEnumString.PushSelf( L );
|
||||
lua_rawgeti( L, -1, iVal + 1 );
|
||||
lua_remove( L, -2 );
|
||||
}
|
||||
|
||||
void SetMetatable( lua_State *L, LuaReference &EnumTable, LuaReference &EnumIndexTable, const char *szName );
|
||||
void SetMetatable( lua_State *L, LuaReference &EnumTable, LuaReference &EnumIndexTable, LuaReference &EnumStringToString, const char *szName );
|
||||
};
|
||||
|
||||
const RString &EnumToString( int iVal, int iMax, const char **szNameArray, auto_ptr<RString> *pNameCache ); // XToString helper
|
||||
@@ -94,7 +96,7 @@ const RString &EnumToString( int iVal, int iMax, const char **szNameArray, auto_
|
||||
template struct EnumTraits<X>; \
|
||||
static void Lua##X(lua_State* L) \
|
||||
{ \
|
||||
/* Create the EnumToString table: { "UnlockEntry_ArcadePoints", "UnlockEntry_DancePoints" } */ \
|
||||
/* Create the IndexToEnumString table: { 1 = "UnlockEntry_ArcadePoints", 2 = "UnlockEntry_DancePoints" } */ \
|
||||
lua_newtable( L ); \
|
||||
FOREACH_ENUM( X, i ) \
|
||||
{ \
|
||||
@@ -102,10 +104,10 @@ static void Lua##X(lua_State* L) \
|
||||
lua_pushstring( L, (#X "_")+s ); \
|
||||
lua_rawseti( L, -2, i+1 ); /* 1-based */ \
|
||||
} \
|
||||
EnumTraits<X>::EnumToString.SetFromStack( L ); \
|
||||
EnumTraits<X>::EnumToString.PushSelf( L ); \
|
||||
EnumTraits<X>::IndexToEnumString.SetFromStack( L ); \
|
||||
EnumTraits<X>::IndexToEnumString.PushSelf( L ); \
|
||||
lua_setglobal( L, #X ); \
|
||||
/* Create the StringToEnum table: { "UnlockEntry_ArcadePoints" = 0, "UnlockEntry_DancePoints" = 1 } */ \
|
||||
/* Create the EnumStringToZeroIndex table: { "UnlockEntry_ArcadePoints" = 0, "UnlockEntry_DancePoints" = 1 } */ \
|
||||
lua_newtable( L ); \
|
||||
FOREACH_ENUM( X, i ) \
|
||||
{ \
|
||||
@@ -114,9 +116,18 @@ static void Lua##X(lua_State* L) \
|
||||
lua_pushnumber( L, i ); /* 0-based */ \
|
||||
lua_rawset( L, -3 ); \
|
||||
} \
|
||||
EnumTraits<X>::StringToEnum.SetFromStack( L ); \
|
||||
EnumTraits<X>::StringToEnum.PushSelf( L ); \
|
||||
Enum::SetMetatable( L, EnumTraits<X>::EnumToString, EnumTraits<X>::StringToEnum, #X ); \
|
||||
EnumTraits<X>::EnumStringToZeroIndex.SetFromStack( L ); \
|
||||
/* Create the EnumStringToString table: { "UnlockEntry_ArcadePoints" = "ArcadePoints", "UnlockEntry_DancePoints" = "DancePoints" } */ \
|
||||
lua_newtable( L ); \
|
||||
FOREACH_ENUM( X, i ) \
|
||||
{ \
|
||||
RString s = X##ToString( i ); \
|
||||
lua_pushstring( L, (#X "_")+s ); \
|
||||
lua_pushstring( L, s ); \
|
||||
lua_rawset( L, -3 ); \
|
||||
} \
|
||||
EnumTraits<X>::EnumStringToString.SetFromStack( L ); \
|
||||
Enum::SetMetatable( L, EnumTraits<X>::IndexToEnumString, EnumTraits<X>::EnumStringToZeroIndex, EnumTraits<X>::EnumStringToString, #X ); \
|
||||
} \
|
||||
REGISTER_WITH_LUA_FUNCTION( Lua##X ); \
|
||||
template<> X EnumTraits<X>::Invalid = X##_Invalid; \
|
||||
|
||||
@@ -498,7 +498,9 @@ void ScreenNetSelectMusic::UpdateDifficulties( PlayerNumber pn )
|
||||
m_DifficultyIcon[pn].SetFromDifficulty( m_DC[pn] );
|
||||
}
|
||||
else
|
||||
{
|
||||
m_DifficultyIcon[pn].SetFromSteps( pn, NULL ); //It will blank it out
|
||||
}
|
||||
|
||||
StepsType st = GAMESTATE->GetCurrentStyle()->m_StepsType;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user