lua_vector3_box.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include "lua_environment.h"
  24. #include "lua_stack.h"
  25. #include "vector3.h"
  26. #include "string_utils.h"
  27. namespace crown
  28. {
  29. static int vector3box_new(lua_State* L)
  30. {
  31. LuaStack stack(L);
  32. Vector3 v;
  33. if (stack.num_args() == 1)
  34. {
  35. const Vector3 tv = stack.get_vector3(1);
  36. v = tv;
  37. }
  38. else if (stack.num_args() == 3)
  39. {
  40. v.x = stack.get_float(1);
  41. v.y = stack.get_float(2);
  42. v.z = stack.get_float(3);
  43. }
  44. stack.push_vector3box(v);
  45. return 1;
  46. }
  47. static int vector3box_ctor(lua_State* L)
  48. {
  49. LuaStack stack(L);
  50. stack.remove(1); // Remove table
  51. return vector3box_new(L);
  52. }
  53. static int vector3box_get_value(lua_State* L)
  54. {
  55. LuaStack stack(L);
  56. Vector3& v = stack.get_vector3box(1);
  57. const char* s = stack.get_string(2);
  58. if (strcmp(s, "x") == 0)
  59. {
  60. stack.push_float(v.x);
  61. return 1;
  62. }
  63. else if (strcmp(s, "y") == 0)
  64. {
  65. stack.push_float(v.y);
  66. return 1;
  67. }
  68. else if (strcmp(s, "z") == 0)
  69. {
  70. stack.push_float(v.z);
  71. return 1;
  72. }
  73. // Never happens
  74. return 0;
  75. }
  76. static int vector3box_set_value(lua_State* L)
  77. {
  78. LuaStack stack(L);
  79. Vector3& v = stack.get_vector3box(1);
  80. const char* s = stack.get_string(2);
  81. float value = stack.get_float(3);
  82. if (strcmp(s, "x") == 0)
  83. {
  84. v.x = value;
  85. }
  86. else if (strcmp(s, "y") == 0)
  87. {
  88. v.y = value;
  89. }
  90. else if (strcmp(s, "z") == 0)
  91. {
  92. v.z = value;
  93. }
  94. return 0;
  95. }
  96. static int vector3box_store(lua_State* L)
  97. {
  98. LuaStack stack(L);
  99. Vector3& v = stack.get_vector3box(1);
  100. if (stack.num_args() == 2)
  101. {
  102. Vector3 tv = stack.get_vector3(2);
  103. v = tv;
  104. }
  105. else if (stack.num_args() == 4)
  106. {
  107. v.x = stack.get_float(2);
  108. v.y = stack.get_float(3);
  109. v.z = stack.get_float(4);
  110. }
  111. return 0;
  112. }
  113. static int vector3box_unbox(lua_State* L)
  114. {
  115. LuaStack stack(L);
  116. Vector3& v = stack.get_vector3box(1);
  117. stack.push_vector3(v);
  118. return 1;
  119. }
  120. static int vector3box_tostring(lua_State* L)
  121. {
  122. LuaStack stack(L);
  123. Vector3& v = stack.get_vector3box(1);
  124. stack.push_fstring("Vector3Box (%p)", &v);
  125. return 1;
  126. }
  127. void load_vector3box(LuaEnvironment& env)
  128. {
  129. env.load_module_function("Vector3Box", "new", vector3box_new);
  130. env.load_module_function("Vector3Box", "store", vector3box_store);
  131. env.load_module_function("Vector3Box", "unbox", vector3box_unbox);
  132. env.load_module_function("Vector3Box", "__index", vector3box_get_value);
  133. env.load_module_function("Vector3Box", "__newindex", vector3box_set_value);
  134. env.load_module_function("Vector3Box", "__tostring", vector3box_tostring);
  135. env.load_module_constructor("Vector3Box", vector3box_ctor);
  136. }
  137. } // namespace crown