lua_vector3_box.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright (c) 2012-2014 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #include "lua_environment.h"
  6. #include "lua_stack.h"
  7. #include "vector3.h"
  8. #include "string_utils.h"
  9. namespace crown
  10. {
  11. static int vector3box_new(lua_State* L)
  12. {
  13. LuaStack stack(L);
  14. Vector3 v;
  15. if (stack.num_args() == 1)
  16. {
  17. const Vector3 tv = stack.get_vector3(1);
  18. v = tv;
  19. }
  20. else if (stack.num_args() == 3)
  21. {
  22. v.x = stack.get_float(1);
  23. v.y = stack.get_float(2);
  24. v.z = stack.get_float(3);
  25. }
  26. stack.push_vector3box(v);
  27. return 1;
  28. }
  29. static int vector3box_ctor(lua_State* L)
  30. {
  31. LuaStack stack(L);
  32. stack.remove(1); // Remove table
  33. return vector3box_new(L);
  34. }
  35. static int vector3box_get_value(lua_State* L)
  36. {
  37. LuaStack stack(L);
  38. Vector3& v = stack.get_vector3box(1);
  39. const char* s = stack.get_string(2);
  40. if (strcmp(s, "x") == 0)
  41. {
  42. stack.push_float(v.x);
  43. return 1;
  44. }
  45. else if (strcmp(s, "y") == 0)
  46. {
  47. stack.push_float(v.y);
  48. return 1;
  49. }
  50. else if (strcmp(s, "z") == 0)
  51. {
  52. stack.push_float(v.z);
  53. return 1;
  54. }
  55. // Never happens
  56. return 0;
  57. }
  58. static int vector3box_set_value(lua_State* L)
  59. {
  60. LuaStack stack(L);
  61. Vector3& v = stack.get_vector3box(1);
  62. const char* s = stack.get_string(2);
  63. float value = stack.get_float(3);
  64. if (strcmp(s, "x") == 0)
  65. {
  66. v.x = value;
  67. }
  68. else if (strcmp(s, "y") == 0)
  69. {
  70. v.y = value;
  71. }
  72. else if (strcmp(s, "z") == 0)
  73. {
  74. v.z = value;
  75. }
  76. return 0;
  77. }
  78. static int vector3box_store(lua_State* L)
  79. {
  80. LuaStack stack(L);
  81. Vector3& v = stack.get_vector3box(1);
  82. if (stack.num_args() == 2)
  83. {
  84. Vector3 tv = stack.get_vector3(2);
  85. v = tv;
  86. }
  87. else if (stack.num_args() == 4)
  88. {
  89. v.x = stack.get_float(2);
  90. v.y = stack.get_float(3);
  91. v.z = stack.get_float(4);
  92. }
  93. return 0;
  94. }
  95. static int vector3box_unbox(lua_State* L)
  96. {
  97. LuaStack stack(L);
  98. Vector3& v = stack.get_vector3box(1);
  99. stack.push_vector3(v);
  100. return 1;
  101. }
  102. static int vector3box_tostring(lua_State* L)
  103. {
  104. LuaStack stack(L);
  105. Vector3& v = stack.get_vector3box(1);
  106. stack.push_fstring("Vector3Box (%p)", &v);
  107. return 1;
  108. }
  109. void load_vector3box(LuaEnvironment& env)
  110. {
  111. env.load_module_function("Vector3Box", "new", vector3box_new);
  112. env.load_module_function("Vector3Box", "store", vector3box_store);
  113. env.load_module_function("Vector3Box", "unbox", vector3box_unbox);
  114. env.load_module_function("Vector3Box", "__index", vector3box_get_value);
  115. env.load_module_function("Vector3Box", "__newindex", vector3box_set_value);
  116. env.load_module_function("Vector3Box", "__tostring", vector3box_tostring);
  117. env.load_module_constructor("Vector3Box", vector3box_ctor);
  118. }
  119. } // namespace crown