lua_FontText.cpp 4.4 KB

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