tool_api.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * Copyright (c) 2012-2021 Daniele Bartolini et al.
  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.inl"
  9. #include "core/strings/string_stream.inl"
  10. #include "core/strings/dynamic_string.inl"
  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 key_down(StringStream& out, const char* key)
  72. {
  73. out << "LevelEditor:key_down(";
  74. out << "'" << key << "'" << ")";
  75. }
  76. void key_up(StringStream& out, const char* key)
  77. {
  78. out << "LevelEditor:key_up(";
  79. out << "'" << key << "'" << ")";
  80. }
  81. void set_grid_size(StringStream& out, f32 size)
  82. {
  83. out << "LevelEditor:set_grid_size(";
  84. out << size << ")";
  85. }
  86. void set_rotation_snap(StringStream& out, f32 degrees)
  87. {
  88. out << "LevelEditor:set_rotation_snap(";
  89. out << frad(degrees) << ")";
  90. }
  91. void enable_show_grid(StringStream& out, bool enable)
  92. {
  93. out << "LevelEditor:enable_show_grid(";
  94. out << lua_bool(enable) << ")";
  95. }
  96. void enable_snap_to_grid(StringStream& out, bool enable)
  97. {
  98. out << "LevelEditor:enable_snap_to_grid(";
  99. out << lua_bool(enable) << ")";
  100. }
  101. void enable_debug_render_world(StringStream& out, bool enable)
  102. {
  103. out << "LevelEditor:enable_debug_render_world(";
  104. out << lua_bool(enable) << ")";
  105. }
  106. void enable_debug_physics_world(StringStream& out, bool enable)
  107. {
  108. out << "LevelEditor:enable_debug_physics_world(";
  109. out << lua_bool(enable) << ")";
  110. }
  111. void set_tool_type(StringStream& out, const ToolType::Enum tt)
  112. {
  113. out << "LevelEditor:set_tool(LevelEditor.";
  114. switch (tt)
  115. {
  116. case ToolType::PLACE: out << "place_tool"; break;
  117. case ToolType::MOVE: out << "move_tool"; break;
  118. case ToolType::ROTATE: out << "rotate_tool"; break;
  119. case ToolType::SCALE: out << "scale_tool"; break;
  120. }
  121. out << ")";
  122. }
  123. void set_snap_mode(StringStream& out, const SnapMode::Enum sm)
  124. {
  125. out << "LevelEditor:set_snap_mode('";
  126. out << (sm == SnapMode::RELATIVE ? "relative" : "absolute");
  127. out << "')";
  128. }
  129. void set_reference_system(StringStream& out, const ReferenceSystem::Enum rs)
  130. {
  131. out << "LevelEditor:set_reference_system('";
  132. out << (rs == ReferenceSystem::LOCAL ? "local" : "world");
  133. out << "')";
  134. }
  135. void spawn_unit(StringStream& out
  136. , const Guid& id
  137. , const char* name
  138. , const Vector3& pos
  139. , const Quaternion& rot
  140. , const Vector3& scl)
  141. {
  142. TempAllocator128 ta;
  143. DynamicString ds(ta);
  144. ds.from_guid(id);
  145. out << "LevelEditor:spawn_unit(";
  146. out << "\"" << ds.c_str() << "\",";
  147. out << "\"" << name << "\",";
  148. lua_vector3(out, pos); out << ",";
  149. lua_quaternion(out, rot); out << ",";
  150. lua_vector3(out, scl); out << ")";
  151. }
  152. void spawn_empty_unit(StringStream& /*out*/, Guid& /*id*/)
  153. {
  154. }
  155. void spawn_sound(StringStream& /*out*/
  156. , const Guid& /*id*/
  157. , const char* /*name*/
  158. , const Vector3& /*pos*/
  159. , const Quaternion& /*rot*/
  160. , const f64 /*vol*/
  161. , const bool /*loop*/)
  162. {
  163. }
  164. void add_tranform_component(StringStream& /*out*/
  165. , const Guid& /*id*/
  166. , const Guid& /*component_id*/
  167. , const Vector3& /*pos*/
  168. , const Quaternion& /*rot*/
  169. , const Vector3& /*scl*/)
  170. {
  171. }
  172. void add_mesh_component(StringStream& /*out*/
  173. , const Guid& /*id*/
  174. , const Guid& /*component_id*/
  175. , const char* /*mesh_resource*/
  176. , const char* /*geometry_name*/
  177. , const char* /*material_resource*/
  178. , const bool /*visible*/)
  179. {
  180. }
  181. void add_material_component(StringStream& /*out*/
  182. , const Guid& /*id*/
  183. , const Guid& /*component_id*/
  184. , const char* /*sprite_resource*/
  185. , const char* /*material_resource*/
  186. , const bool /*visible*/)
  187. {
  188. }
  189. void add_camera_content(StringStream& /*out*/
  190. , const Guid& /*id*/
  191. , const Guid& /*component_id*/
  192. , const ProjectionType::Enum /*projection*/
  193. , const f64 /*fov*/
  194. , const f64 /*far_range*/
  195. , const f64 /*near_range*/)
  196. {
  197. }
  198. void add_light_component(StringStream& /*out*/
  199. , const Guid& /*id*/
  200. , const Guid& /*component_id*/
  201. , const LightType::Enum /*type*/
  202. , const f64 /*range*/
  203. , const f64 /*intensity*/
  204. , const f64 /*spot_angle*/
  205. , const Vector3& /*color*/)
  206. {
  207. }
  208. void move_object(StringStream& /*out*/
  209. , const Guid& /*id*/
  210. , const Vector3& /*pos*/
  211. , const Quaternion& /*rot*/
  212. , const Vector3& /*scl*/)
  213. {
  214. }
  215. void set_light(StringStream& /*out*/
  216. , const Guid& /*id*/
  217. , const LightType::Enum /*type*/
  218. , const f64 /*range*/
  219. , const f64 /*intensity*/
  220. , const f64 /*spot_angle*/
  221. , const Vector3& /*color*/)
  222. {
  223. }
  224. void set_sound_range(StringStream& /*out*/
  225. , const Guid& /*id*/
  226. , f64 /*range*/)
  227. {
  228. }
  229. void set_placeable(StringStream& out
  230. , const char* type
  231. , const char* name)
  232. {
  233. out << "LevelEditor:set_placeable(";
  234. out << "'" << type << "'" << ",";
  235. out << "'" << name << "'" << ")";
  236. }
  237. void selection_set(StringStream& /*out*/, const Array<Guid>& /*ids*/)
  238. {
  239. }
  240. void camera_view_perspective(StringStream& out)
  241. {
  242. out << "LevelEditor:camera_view_perspective()";
  243. }
  244. void camera_view_front(StringStream& out)
  245. {
  246. out << "LevelEditor:camera_view_front()";
  247. }
  248. void camera_view_back(StringStream& out)
  249. {
  250. out << "LevelEditor:camera_view_back()";
  251. }
  252. void camera_view_right(StringStream& out)
  253. {
  254. out << "LevelEditor:camera_view_right()";
  255. }
  256. void camera_view_left(StringStream& out)
  257. {
  258. out << "LevelEditor:camera_view_left()";
  259. }
  260. void camera_view_top(StringStream& out)
  261. {
  262. out << "LevelEditor:camera_view_top()";
  263. }
  264. void camera_view_bottom(StringStream& out)
  265. {
  266. out << "LevelEditor:camera_view_bottom()";
  267. }
  268. } // namespace tool
  269. } // namespace crown