lua_FontText.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. 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*)((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. ScriptUtil::LuaObject* object = (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. else
  53. {
  54. lua_pushstring(state, "lua_FontText__gc - Failed to match the given parameters to a valid function signature.");
  55. lua_error(state);
  56. }
  57. break;
  58. }
  59. default:
  60. {
  61. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  62. lua_error(state);
  63. break;
  64. }
  65. }
  66. return 0;
  67. }
  68. int lua_FontText__init(lua_State* state)
  69. {
  70. // Get the number of parameters.
  71. int paramCount = lua_gettop(state);
  72. // Attempt to match the parameters to a valid binding.
  73. switch (paramCount)
  74. {
  75. case 1:
  76. {
  77. if ((lua_type(state, 1) == LUA_TSTRING || lua_type(state, 1) == LUA_TNIL))
  78. {
  79. // Get parameter 1 off the stack.
  80. ScriptUtil::LuaArray<const char> param1 = ScriptUtil::getString(1, false);
  81. void* returnPtr = (void*)new Font::Text(param1);
  82. if (returnPtr)
  83. {
  84. ScriptUtil::LuaObject* object = (ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(ScriptUtil::LuaObject));
  85. object->instance = returnPtr;
  86. object->owns = true;
  87. luaL_getmetatable(state, "FontText");
  88. lua_setmetatable(state, -2);
  89. }
  90. else
  91. {
  92. lua_pushnil(state);
  93. }
  94. return 1;
  95. }
  96. else
  97. {
  98. lua_pushstring(state, "lua_FontText__init - Failed to match the given parameters to a valid function signature.");
  99. lua_error(state);
  100. }
  101. break;
  102. }
  103. default:
  104. {
  105. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  106. lua_error(state);
  107. break;
  108. }
  109. }
  110. return 0;
  111. }
  112. int lua_FontText_getText(lua_State* state)
  113. {
  114. // Get the number of parameters.
  115. int paramCount = lua_gettop(state);
  116. // Attempt to match the parameters to a valid binding.
  117. switch (paramCount)
  118. {
  119. case 1:
  120. {
  121. if ((lua_type(state, 1) == LUA_TUSERDATA))
  122. {
  123. Font::Text* instance = getInstance(state);
  124. const char* result = instance->getText();
  125. // Push the return value onto the stack.
  126. lua_pushstring(state, result);
  127. return 1;
  128. }
  129. else
  130. {
  131. lua_pushstring(state, "lua_FontText_getText - Failed to match the given parameters to a valid function signature.");
  132. lua_error(state);
  133. }
  134. break;
  135. }
  136. default:
  137. {
  138. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  139. lua_error(state);
  140. break;
  141. }
  142. }
  143. return 0;
  144. }
  145. }