tool_api.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. * Copyright (c) 2012-2018 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/dbartolini/crown/blob/master/LICENSE
  4. */
  5. #include "tool_api.h"
  6. #include "core/guid.h"
  7. #include "core/math/math.h"
  8. #include "core/memory/temp_allocator.h"
  9. #include "core/strings/string_stream.h"
  10. #include "core/strings/dynamic_string.h"
  11. #include "world/types.h"
  12. namespace crown
  13. {
  14. namespace tool
  15. {
  16. const char* lua_bool(const bool b)
  17. {
  18. return b ? "true" : "false";
  19. }
  20. void lua_vector2(StringStream& out, const Vector2& v)
  21. {
  22. out << "Vector2(";
  23. out << v.x << ",";
  24. out << v.y << ")";
  25. }
  26. void lua_vector3(StringStream& out, const Vector3& v)
  27. {
  28. out << "Vector3(";
  29. out << v.x << ",";
  30. out << v.y << ",";
  31. out << v.z << ")";
  32. }
  33. void lua_quaternion(StringStream& out, const Quaternion& q)
  34. {
  35. out << "Quaternion.from_elements(";
  36. out << q.x << ",";
  37. out << q.y << ",";
  38. out << q.z << ",";
  39. out << q.w << ",";
  40. }
  41. void device_quit(StringStream& out)
  42. {
  43. out << "Device.quit()";
  44. }
  45. void set_mouse_state(StringStream& out, f32 x, f32 y, bool left, bool middle, bool right)
  46. {
  47. out << "LevelEditor:set_mouse_state(";
  48. out << x << ",";
  49. out << y << ",";
  50. out << lua_bool(left) << ",";
  51. out << lua_bool(middle) << ",";
  52. out << lua_bool(right) << ")";
  53. }
  54. void mouse_down(StringStream& out, f32 x, f32 y)
  55. {
  56. out << "LevelEditor:mouse_down(";
  57. out << x << ",";
  58. out << y << ")";
  59. }
  60. void mouse_up(StringStream& out, f32 x, f32 y)
  61. {
  62. out << "LevelEditor:mouse_up(";
  63. out << x << ",";
  64. out << y << ")";
  65. }
  66. void mouse_wheel(StringStream& out, f32 delta)
  67. {
  68. out << "LevelEditor:mouse_wheel(";
  69. out << delta << ")";
  70. }
  71. void mouse_move(StringStream& out, f32 x, f32 y)
  72. {
  73. out << "LevelEditor:mouse_move(";
  74. out << x << ",";
  75. out << y << ")";
  76. }
  77. void key_down(StringStream& out, const char* key)
  78. {
  79. out << "LevelEditor:key_down(";
  80. out << "'" << key << "'" << ")";
  81. }
  82. void key_up(StringStream& out, const char* key)
  83. {
  84. out << "LevelEditor:key_up(";
  85. out << "'" << key << "'" << ")";
  86. }
  87. void set_grid_size(StringStream& out, f32 size)
  88. {
  89. out << "LevelEditor:set_grid_size(";
  90. out << size << ")";
  91. }
  92. void set_rotation_snap(StringStream& out, f32 degrees)
  93. {
  94. out << "LevelEditor:set_rotation_snap(";
  95. out << frad(degrees) << ")";
  96. }
  97. void enable_show_grid(StringStream& out, bool enable)
  98. {
  99. out << "LevelEditor:enable_show_grid(";
  100. out << lua_bool(enable) << ")";
  101. }
  102. void enable_snap_to_grid(StringStream& out, bool enable)
  103. {
  104. out << "LevelEditor:enable_snap_to_grid(";
  105. out << lua_bool(enable) << ")";
  106. }
  107. void enable_debug_render_world(StringStream& out, bool enable)
  108. {
  109. out << "LevelEditor:enable_debug_render_world(";
  110. out << lua_bool(enable) << ")";
  111. }
  112. void enable_debug_physics_world(StringStream& out, bool enable)
  113. {
  114. out << "LevelEditor:enable_debug_physics_world(";
  115. out << lua_bool(enable) << ")";
  116. }
  117. void set_tool_type(StringStream& out, const ToolType::Enum tt)
  118. {
  119. out << "LevelEditor:set_tool(LevelEditor.";
  120. switch (tt)
  121. {
  122. case ToolType::PLACE: out << "place_tool"; break;
  123. case ToolType::MOVE: out << "move_tool"; break;
  124. case ToolType::ROTATE: out << "rotate_tool"; break;
  125. case ToolType::SCALE: out << "scale_tool"; break;
  126. }
  127. out << ")";
  128. }
  129. void set_snap_mode(StringStream& out, const SnapMode::Enum sm)
  130. {
  131. out << "LevelEditor:set_snap_mode('";
  132. out << (sm == SnapMode::RELATIVE ? "relative" : "absolute");
  133. out << "')";
  134. }
  135. void set_reference_system(StringStream& out, const ReferenceSystem::Enum rs)
  136. {
  137. out << "LevelEditor:set_reference_system('";
  138. out << (rs == ReferenceSystem::LOCAL ? "local" : "world");
  139. out << "')";
  140. }
  141. void spawn_unit(StringStream& out
  142. , const Guid& id
  143. , const char* name
  144. , const Vector3& pos
  145. , const Quaternion& rot
  146. , const Vector3& scl)
  147. {
  148. TempAllocator128 ta;
  149. DynamicString ds(ta);
  150. ds.from_guid(id);
  151. out << "LevelEditor:spawn_unit(";
  152. out << "\"" << ds.c_str() << "\",";
  153. out << "\"" << name << "\",";
  154. lua_vector3(out, pos); out << ",";
  155. lua_quaternion(out, rot); out << ",";
  156. lua_vector3(out, scl); out << ")";
  157. }
  158. void spawn_empty_unit(StringStream& /*out*/, Guid& /*id*/)
  159. {
  160. }
  161. void spawn_sound(StringStream& /*out*/
  162. , const Guid& /*id*/
  163. , const char* /*name*/
  164. , const Vector3& /*pos*/
  165. , const Quaternion& /*rot*/
  166. , const f64 /*vol*/
  167. , const bool /*loop*/)
  168. {
  169. }
  170. void add_tranform_component(StringStream& /*out*/
  171. , const Guid& /*id*/
  172. , const Guid& /*component_id*/
  173. , const Vector3& /*pos*/
  174. , const Quaternion& /*rot*/
  175. , const Vector3& /*scl*/)
  176. {
  177. }
  178. void add_mesh_component(StringStream& /*out*/
  179. , const Guid& /*id*/
  180. , const Guid& /*component_id*/
  181. , const char* /*mesh_resource*/
  182. , const char* /*geometry_name*/
  183. , const char* /*material_resource*/
  184. , const bool /*visible*/)
  185. {
  186. }
  187. void add_material_component(StringStream& /*out*/
  188. , const Guid& /*id*/
  189. , const Guid& /*component_id*/
  190. , const char* /*sprite_resource*/
  191. , const char* /*material_resource*/
  192. , const bool /*visible*/)
  193. {
  194. }
  195. void add_camera_content(StringStream& /*out*/
  196. , const Guid& /*id*/
  197. , const Guid& /*component_id*/
  198. , const ProjectionType::Enum /*projection*/
  199. , const f64 /*fov*/
  200. , const f64 /*far_range*/
  201. , const f64 /*near_range*/)
  202. {
  203. }
  204. void add_light_component(StringStream& /*out*/
  205. , const Guid& /*id*/
  206. , const Guid& /*component_id*/
  207. , const LightType::Enum /*type*/
  208. , const f64 /*range*/
  209. , const f64 /*intensity*/
  210. , const f64 /*spot_angle*/
  211. , const Vector3& /*color*/)
  212. {
  213. }
  214. void move_object(StringStream& /*out*/
  215. , const Guid& /*id*/
  216. , const Vector3& /*pos*/
  217. , const Quaternion& /*rot*/
  218. , const Vector3& /*scl*/)
  219. {
  220. }
  221. void set_light(StringStream& /*out*/
  222. , const Guid& /*id*/
  223. , const LightType::Enum /*type*/
  224. , const f64 /*range*/
  225. , const f64 /*intensity*/
  226. , const f64 /*spot_angle*/
  227. , const Vector3& /*color*/)
  228. {
  229. }
  230. void set_sound_range(StringStream& /*out*/
  231. , const Guid& /*id*/
  232. , f64 /*range*/)
  233. {
  234. }
  235. void set_placeable(StringStream& out
  236. , const char* type
  237. , const char* name)
  238. {
  239. out << "LevelEditor:set_placeable(";
  240. out << "'" << type << "'" << ",";
  241. out << "'" << name << "'" << ")";
  242. }
  243. void selection_set(StringStream& /*out*/, const Array<Guid>& /*ids*/)
  244. {
  245. }
  246. void camera_view_perspective(StringStream& out)
  247. {
  248. out << "LevelEditor:camera_view_perspective()";
  249. }
  250. void camera_view_front(StringStream& out)
  251. {
  252. out << "LevelEditor:camera_view_front()";
  253. }
  254. void camera_view_back(StringStream& out)
  255. {
  256. out << "LevelEditor:camera_view_back()";
  257. }
  258. void camera_view_right(StringStream& out)
  259. {
  260. out << "LevelEditor:camera_view_right()";
  261. }
  262. void camera_view_left(StringStream& out)
  263. {
  264. out << "LevelEditor:camera_view_left()";
  265. }
  266. void camera_view_top(StringStream& out)
  267. {
  268. out << "LevelEditor:camera_view_top()";
  269. }
  270. void camera_view_bottom(StringStream& out)
  271. {
  272. out << "LevelEditor:camera_view_bottom()";
  273. }
  274. } // namespace tool
  275. } // namespace crown