lua_FontGlyph.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #include "Base.h"
  2. #include "ScriptController.h"
  3. #include "lua_FontGlyph.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_FontGlyph()
  15. {
  16. const luaL_Reg lua_members[] =
  17. {
  18. {"code", lua_FontGlyph_code},
  19. {"uvs", lua_FontGlyph_uvs},
  20. {"width", lua_FontGlyph_width},
  21. {NULL, NULL}
  22. };
  23. const luaL_Reg* lua_statics = NULL;
  24. std::vector<std::string> scopePath;
  25. scopePath.push_back("Font");
  26. ScriptUtil::registerClass("FontGlyph", lua_members, lua_FontGlyph__init, lua_FontGlyph__gc, lua_statics, scopePath);
  27. }
  28. static Font::Glyph* getInstance(lua_State* state)
  29. {
  30. void* userdata = luaL_checkudata(state, 1, "FontGlyph");
  31. luaL_argcheck(state, userdata != NULL, 1, "'FontGlyph' expected.");
  32. return (Font::Glyph*)((ScriptUtil::LuaObject*)userdata)->instance;
  33. }
  34. int lua_FontGlyph__gc(lua_State* state)
  35. {
  36. // Get the number of parameters.
  37. int paramCount = lua_gettop(state);
  38. // Attempt to match the parameters to a valid binding.
  39. switch (paramCount)
  40. {
  41. case 1:
  42. {
  43. if ((lua_type(state, 1) == LUA_TUSERDATA))
  44. {
  45. void* userdata = luaL_checkudata(state, 1, "FontGlyph");
  46. luaL_argcheck(state, userdata != NULL, 1, "'FontGlyph' expected.");
  47. ScriptUtil::LuaObject* object = (ScriptUtil::LuaObject*)userdata;
  48. if (object->owns)
  49. {
  50. Font::Glyph* instance = (Font::Glyph*)object->instance;
  51. SAFE_DELETE(instance);
  52. }
  53. return 0;
  54. }
  55. else
  56. {
  57. lua_pushstring(state, "lua_FontGlyph__gc - Failed to match the given parameters to a valid function signature.");
  58. lua_error(state);
  59. }
  60. break;
  61. }
  62. default:
  63. {
  64. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  65. lua_error(state);
  66. break;
  67. }
  68. }
  69. return 0;
  70. }
  71. int lua_FontGlyph__init(lua_State* state)
  72. {
  73. // Get the number of parameters.
  74. int paramCount = lua_gettop(state);
  75. // Attempt to match the parameters to a valid binding.
  76. switch (paramCount)
  77. {
  78. case 0:
  79. {
  80. void* returnPtr = (void*)new Font::Glyph();
  81. if (returnPtr)
  82. {
  83. ScriptUtil::LuaObject* object = (ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(ScriptUtil::LuaObject));
  84. object->instance = returnPtr;
  85. object->owns = true;
  86. luaL_getmetatable(state, "FontGlyph");
  87. lua_setmetatable(state, -2);
  88. }
  89. else
  90. {
  91. lua_pushnil(state);
  92. }
  93. return 1;
  94. break;
  95. }
  96. default:
  97. {
  98. lua_pushstring(state, "Invalid number of parameters (expected 0).");
  99. lua_error(state);
  100. break;
  101. }
  102. }
  103. return 0;
  104. }
  105. int lua_FontGlyph_code(lua_State* state)
  106. {
  107. // Validate the number of parameters.
  108. if (lua_gettop(state) > 2)
  109. {
  110. lua_pushstring(state, "Invalid number of parameters (expected 1 or 2).");
  111. lua_error(state);
  112. }
  113. Font::Glyph* instance = getInstance(state);
  114. if (lua_gettop(state) == 2)
  115. {
  116. // Get parameter 2 off the stack.
  117. unsigned int param2 = (unsigned int)luaL_checkunsigned(state, 2);
  118. instance->code = param2;
  119. return 0;
  120. }
  121. else
  122. {
  123. unsigned int result = instance->code;
  124. // Push the return value onto the stack.
  125. lua_pushunsigned(state, result);
  126. return 1;
  127. }
  128. }
  129. int lua_FontGlyph_uvs(lua_State* state)
  130. {
  131. // Validate the number of parameters.
  132. if (lua_gettop(state) > 2)
  133. {
  134. lua_pushstring(state, "Invalid number of parameters (expected 1 or 2).");
  135. lua_error(state);
  136. }
  137. Font::Glyph* instance = getInstance(state);
  138. if (lua_gettop(state) == 2)
  139. {
  140. // Get parameter 2 off the stack.
  141. float* param2 = ScriptUtil::getFloatPointer(2);
  142. memcpy(instance->uvs, param2, sizeof(float) * 4);
  143. return 0;
  144. }
  145. else
  146. {
  147. float* result = instance->uvs;
  148. // Push the return value onto the stack.
  149. lua_pushlightuserdata(state, result);
  150. return 1;
  151. }
  152. }
  153. int lua_FontGlyph_width(lua_State* state)
  154. {
  155. // Validate the number of parameters.
  156. if (lua_gettop(state) > 2)
  157. {
  158. lua_pushstring(state, "Invalid number of parameters (expected 1 or 2).");
  159. lua_error(state);
  160. }
  161. Font::Glyph* instance = getInstance(state);
  162. if (lua_gettop(state) == 2)
  163. {
  164. // Get parameter 2 off the stack.
  165. unsigned int param2 = (unsigned int)luaL_checkunsigned(state, 2);
  166. instance->width = param2;
  167. return 0;
  168. }
  169. else
  170. {
  171. unsigned int result = instance->width;
  172. // Push the return value onto the stack.
  173. lua_pushunsigned(state, result);
  174. return 1;
  175. }
  176. }
  177. }