lua_AnimationValue.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #include "Base.h"
  2. #include "ScriptController.h"
  3. #include "AnimationValue.h"
  4. #include "lua_AnimationValue.h"
  5. #include "lua_Global.h"
  6. namespace gameplay
  7. {
  8. void luaRegister_AnimationValue()
  9. {
  10. ScriptController* sc = ScriptController::getInstance();
  11. const luaL_Reg lua_members[] =
  12. {
  13. {"getFloat", lua_AnimationValue_getFloat},
  14. {"setFloat", lua_AnimationValue_setFloat},
  15. {NULL, NULL}
  16. };
  17. const luaL_Reg* lua_statics = NULL;
  18. std::vector<std::string> scopePath;
  19. sc->registerClass("AnimationValue", lua_members, NULL, NULL, lua_statics, scopePath);
  20. }
  21. static AnimationValue* getInstance(lua_State* state)
  22. {
  23. void* userdata = luaL_checkudata(state, 1, "AnimationValue");
  24. luaL_argcheck(state, userdata != NULL, 1, "'AnimationValue' expected.");
  25. return (AnimationValue*)((ScriptController::LuaObject*)userdata)->instance;
  26. }
  27. int lua_AnimationValue_getFloat(lua_State* state)
  28. {
  29. // Get the number of parameters.
  30. int paramCount = lua_gettop(state);
  31. // Attempt to match the parameters to a valid binding.
  32. switch (paramCount)
  33. {
  34. case 2:
  35. {
  36. if ((lua_type(state, 1) == LUA_TUSERDATA || lua_type(state, 1) == LUA_TNIL) &&
  37. lua_type(state, 2) == LUA_TNUMBER)
  38. {
  39. // Get parameter 1 off the stack.
  40. unsigned int param1 = (unsigned int)luaL_checkunsigned(state, 2);
  41. AnimationValue* instance = getInstance(state);
  42. float result = instance->getFloat(param1);
  43. // Push the return value onto the stack.
  44. lua_pushnumber(state, result);
  45. return 1;
  46. }
  47. else
  48. {
  49. lua_pushstring(state, "Failed to match the given parameters to a valid function signature.");
  50. lua_error(state);
  51. }
  52. break;
  53. }
  54. case 4:
  55. {
  56. if ((lua_type(state, 1) == LUA_TUSERDATA || lua_type(state, 1) == LUA_TNIL) &&
  57. (lua_type(state, 2) == LUA_TTABLE || lua_type(state, 2) == LUA_TLIGHTUSERDATA) &&
  58. lua_type(state, 3) == LUA_TNUMBER &&
  59. lua_type(state, 4) == LUA_TNUMBER)
  60. {
  61. // Get parameter 1 off the stack.
  62. float* param1 = ScriptController::getInstance()->getFloatPointer(2);
  63. // Get parameter 2 off the stack.
  64. unsigned int param2 = (unsigned int)luaL_checkunsigned(state, 3);
  65. // Get parameter 3 off the stack.
  66. unsigned int param3 = (unsigned int)luaL_checkunsigned(state, 4);
  67. AnimationValue* instance = getInstance(state);
  68. instance->getFloat(param1, param2, param3);
  69. return 0;
  70. }
  71. else
  72. {
  73. lua_pushstring(state, "Failed to match the given parameters to a valid function signature.");
  74. lua_error(state);
  75. }
  76. break;
  77. }
  78. default:
  79. {
  80. lua_pushstring(state, "Invalid number of parameters (expected 2 or 4).");
  81. lua_error(state);
  82. break;
  83. }
  84. }
  85. return 0;
  86. }
  87. int lua_AnimationValue_setFloat(lua_State* state)
  88. {
  89. // Get the number of parameters.
  90. int paramCount = lua_gettop(state);
  91. // Attempt to match the parameters to a valid binding.
  92. switch (paramCount)
  93. {
  94. case 3:
  95. {
  96. if ((lua_type(state, 1) == LUA_TUSERDATA || lua_type(state, 1) == LUA_TNIL) &&
  97. lua_type(state, 2) == LUA_TNUMBER &&
  98. lua_type(state, 3) == LUA_TNUMBER)
  99. {
  100. // Get parameter 1 off the stack.
  101. unsigned int param1 = (unsigned int)luaL_checkunsigned(state, 2);
  102. // Get parameter 2 off the stack.
  103. float param2 = (float)luaL_checknumber(state, 3);
  104. AnimationValue* instance = getInstance(state);
  105. instance->setFloat(param1, param2);
  106. return 0;
  107. }
  108. else
  109. {
  110. lua_pushstring(state, "Failed to match the given parameters to a valid function signature.");
  111. lua_error(state);
  112. }
  113. break;
  114. }
  115. case 4:
  116. {
  117. if ((lua_type(state, 1) == LUA_TUSERDATA || lua_type(state, 1) == LUA_TNIL) &&
  118. (lua_type(state, 2) == LUA_TTABLE || lua_type(state, 2) == LUA_TLIGHTUSERDATA) &&
  119. lua_type(state, 3) == LUA_TNUMBER &&
  120. lua_type(state, 4) == LUA_TNUMBER)
  121. {
  122. // Get parameter 1 off the stack.
  123. float* param1 = ScriptController::getInstance()->getFloatPointer(2);
  124. // Get parameter 2 off the stack.
  125. unsigned int param2 = (unsigned int)luaL_checkunsigned(state, 3);
  126. // Get parameter 3 off the stack.
  127. unsigned int param3 = (unsigned int)luaL_checkunsigned(state, 4);
  128. AnimationValue* instance = getInstance(state);
  129. instance->setFloat(param1, param2, param3);
  130. return 0;
  131. }
  132. else
  133. {
  134. lua_pushstring(state, "Failed to match the given parameters to a valid function signature.");
  135. lua_error(state);
  136. }
  137. break;
  138. }
  139. default:
  140. {
  141. lua_pushstring(state, "Invalid number of parameters (expected 3 or 4).");
  142. lua_error(state);
  143. break;
  144. }
  145. }
  146. return 0;
  147. }
  148. }