ElementStyle.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #include "precompiled.h"
  2. #include "LuaType.h"
  3. #include "lua.hpp"
  4. #include "ElementStyle.h"
  5. #include <ElementStyle.h>
  6. namespace Rocket {
  7. namespace Core {
  8. namespace Lua {
  9. template<> void LuaType<ElementStyle>::extra_init(lua_State* L, int metatable_index)
  10. {
  11. lua_pushcfunction(L,ElementStyle__index);
  12. lua_setfield(L,metatable_index,"__index");
  13. lua_pushcfunction(L,ElementStyle__newindex);
  14. lua_setfield(L,metatable_index,"__newindex");
  15. }
  16. int ElementStyle__index(lua_State* L)
  17. {
  18. /*the table obj and the missing key are currently on the stack(index 1 & 2) as defined by the Lua language*/
  19. int keytype = lua_type(L,2);
  20. if(keytype == LUA_TSTRING) //if we are trying to access a string, then we will assume that it is a property
  21. {
  22. ElementStyle* es = LuaType<ElementStyle>::check(L,1);
  23. if(es == NULL)
  24. {
  25. lua_pushnil(L);
  26. return 1;
  27. }
  28. const Property* prop = es->GetProperty(lua_tostring(L,2));
  29. if(prop == NULL)
  30. {
  31. lua_pushnil(L);
  32. return 1;
  33. }
  34. else
  35. {
  36. lua_pushstring(L,prop->ToString().CString());
  37. return 1;
  38. }
  39. }
  40. else //if it wasn't trying to get a string
  41. {
  42. lua_settop(L,2);
  43. return LuaType<ElementStyle>::index(L);
  44. }
  45. }
  46. int ElementStyle__newindex(lua_State* L)
  47. {
  48. //[1] = obj, [2] = key, [3] = value
  49. ElementStyle* es = LuaType<ElementStyle>::check(L,1);
  50. if(es == NULL)
  51. {
  52. lua_pushnil(L);
  53. return 1;
  54. }
  55. int keytype = lua_type(L,2);
  56. int valuetype = lua_type(L,3);
  57. if(keytype == LUA_TSTRING )
  58. {
  59. const char* key = lua_tostring(L,2);
  60. if(valuetype == LUA_TSTRING)
  61. {
  62. const char* value = lua_tostring(L,3);
  63. lua_pushboolean(L,es->SetProperty(key,value));
  64. return 1;
  65. }
  66. else if (valuetype == LUA_TNIL)
  67. {
  68. es->RemoveProperty(key);
  69. return 0;
  70. }
  71. }
  72. //everything else returns when it needs to, so we are safe to pass it
  73. //on if needed
  74. lua_settop(L,3);
  75. return LuaType<ElementStyle>::newindex(L);
  76. }
  77. int ElementStyleGetTable(lua_State* L, ElementStyle* obj)
  78. {
  79. LUACHECKOBJ(obj);
  80. int index = 0;
  81. String key,sval;
  82. const Property* value;
  83. PseudoClassList pseudo;
  84. lua_newtable(L);
  85. while(obj->IterateProperties(index,pseudo,key,value))
  86. {
  87. lua_pushstring(L,key.CString());
  88. value->definition->GetValue(sval,*value);
  89. lua_pushstring(L,sval.CString());
  90. lua_settable(L,-3);
  91. }
  92. return 1;
  93. }
  94. RegType<ElementStyle> ElementStyleMethods[] =
  95. {
  96. LUAMETHOD(ElementStyle,GetTable)
  97. { NULL, NULL },
  98. };
  99. luaL_reg ElementStyleGetters[] =
  100. {
  101. { NULL, NULL },
  102. };
  103. luaL_reg ElementStyleSetters[] =
  104. {
  105. { NULL, NULL },
  106. };
  107. template<> const char* GetTClassName<ElementStyle>() { return "ElementStyle"; }
  108. template<> RegType<ElementStyle>* GetMethodTable<ElementStyle>() { return ElementStyleMethods; }
  109. template<> luaL_reg* GetAttrTable<ElementStyle>() { return ElementStyleGetters; }
  110. template<> luaL_reg* SetAttrTable<ElementStyle>() { return ElementStyleSetters; }
  111. }
  112. }
  113. }