lua_FontText.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #include "Base.h"
  2. #include "ScriptController.h"
  3. #include "lua_FontText.h"
  4. #include "Base.h"
  5. #include "Bundle.h"
  6. #include "FileSystem.h"
  7. #include "Font.h"
  8. #include "Game.h"
  9. #include "Ref.h"
  10. #include "lua_FontJustify.h"
  11. namespace gameplay
  12. {
  13. void luaRegister_FontText()
  14. {
  15. const luaL_Reg lua_members[] =
  16. {
  17. {"getText", lua_FontText_getText},
  18. {NULL, NULL}
  19. };
  20. const luaL_Reg* lua_statics = NULL;
  21. std::vector<std::string> scopePath;
  22. scopePath.push_back("Font");
  23. gameplay::ScriptUtil::registerClass("FontText", lua_members, lua_FontText__init, lua_FontText__gc, lua_statics, scopePath);
  24. }
  25. static Font::Text* getInstance(lua_State* state)
  26. {
  27. void* userdata = luaL_checkudata(state, 1, "FontText");
  28. luaL_argcheck(state, userdata != NULL, 1, "'FontText' expected.");
  29. return (Font::Text*)((gameplay::ScriptUtil::LuaObject*)userdata)->instance;
  30. }
  31. int lua_FontText__gc(lua_State* state)
  32. {
  33. // Get the number of parameters.
  34. int paramCount = lua_gettop(state);
  35. // Attempt to match the parameters to a valid binding.
  36. switch (paramCount)
  37. {
  38. case 1:
  39. {
  40. if ((lua_type(state, 1) == LUA_TUSERDATA))
  41. {
  42. void* userdata = luaL_checkudata(state, 1, "FontText");
  43. luaL_argcheck(state, userdata != NULL, 1, "'FontText' expected.");
  44. gameplay::ScriptUtil::LuaObject* object = (gameplay::ScriptUtil::LuaObject*)userdata;
  45. if (object->owns)
  46. {
  47. Font::Text* instance = (Font::Text*)object->instance;
  48. SAFE_DELETE(instance);
  49. }
  50. return 0;
  51. }
  52. lua_pushstring(state, "lua_FontText__gc - Failed to match the given parameters to a valid function signature.");
  53. lua_error(state);
  54. break;
  55. }
  56. default:
  57. {
  58. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  59. lua_error(state);
  60. break;
  61. }
  62. }
  63. return 0;
  64. }
  65. int lua_FontText__init(lua_State* state)
  66. {
  67. // Get the number of parameters.
  68. int paramCount = lua_gettop(state);
  69. // Attempt to match the parameters to a valid binding.
  70. switch (paramCount)
  71. {
  72. case 1:
  73. {
  74. if ((lua_type(state, 1) == LUA_TSTRING || lua_type(state, 1) == LUA_TNIL))
  75. {
  76. // Get parameter 1 off the stack.
  77. const char* param1 = gameplay::ScriptUtil::getString(1, false);
  78. void* returnPtr = (void*)new Font::Text(param1);
  79. if (returnPtr)
  80. {
  81. gameplay::ScriptUtil::LuaObject* object = (gameplay::ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(gameplay::ScriptUtil::LuaObject));
  82. object->instance = returnPtr;
  83. object->owns = true;
  84. luaL_getmetatable(state, "FontText");
  85. lua_setmetatable(state, -2);
  86. }
  87. else
  88. {
  89. lua_pushnil(state);
  90. }
  91. return 1;
  92. }
  93. lua_pushstring(state, "lua_FontText__init - Failed to match the given parameters to a valid function signature.");
  94. lua_error(state);
  95. break;
  96. }
  97. default:
  98. {
  99. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  100. lua_error(state);
  101. break;
  102. }
  103. }
  104. return 0;
  105. }
  106. int lua_FontText_getText(lua_State* state)
  107. {
  108. // Get the number of parameters.
  109. int paramCount = lua_gettop(state);
  110. // Attempt to match the parameters to a valid binding.
  111. switch (paramCount)
  112. {
  113. case 1:
  114. {
  115. if ((lua_type(state, 1) == LUA_TUSERDATA))
  116. {
  117. Font::Text* instance = getInstance(state);
  118. const char* result = instance->getText();
  119. // Push the return value onto the stack.
  120. lua_pushstring(state, result);
  121. return 1;
  122. }
  123. lua_pushstring(state, "lua_FontText_getText - Failed to match the given parameters to a valid function signature.");
  124. lua_error(state);
  125. break;
  126. }
  127. default:
  128. {
  129. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  130. lua_error(state);
  131. break;
  132. }
  133. }
  134. return 0;
  135. }
  136. }