lua_FontText.cpp 4.7 KB

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