engine_api.vala 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. * Copyright (c) 2012-2024 Daniele Bartolini et al.
  3. * SPDX-License-Identifier: GPL-3.0-or-later
  4. */
  5. namespace Crown
  6. {
  7. /// Functions to encode Vala types to Lua.
  8. namespace Lua
  9. {
  10. public string bool(bool b)
  11. {
  12. return b == true ? "true" : "false";
  13. }
  14. public string vector2(Vector2 v)
  15. {
  16. return "Vector2(%.17g, %.17g)".printf(v.x, v.y);
  17. }
  18. public string vector3(Vector3 v)
  19. {
  20. return "Vector3(%.17g, %.17g, %.17g)".printf(v.x, v.y, v.z);
  21. }
  22. public string quaternion(Quaternion q)
  23. {
  24. return "Quaternion.from_elements(%.17g, %.17g, %.17g, %.17g)".printf(q.x, q.y, q.z, q.w);
  25. }
  26. } /* namespace Lua */
  27. namespace RuntimeApi
  28. {
  29. public string quit()
  30. {
  31. return "{\"type\":\"quit\"}";
  32. }
  33. } /* namespace RuntimeApi */
  34. namespace DataCompilerApi
  35. {
  36. public string compile(Guid id, string data_dir, string platform)
  37. {
  38. return "{\"type\":\"compile\",\"id\":\"%s\",\"data_dir\":\"%s\",\"platform\":\"%s\"}".printf(id.to_string()
  39. , data_dir.replace("\\", "\\\\").replace("\"", "\\\"")
  40. , platform
  41. );
  42. }
  43. public string refresh_list(uint since_revision)
  44. {
  45. return "{\"type\":\"refresh_list\",\"revision\":%u}".printf(since_revision);
  46. }
  47. } /* namespace DataCompilerApi */
  48. namespace DeviceApi
  49. {
  50. public string command(string[] args)
  51. {
  52. StringBuilder sb = new StringBuilder();
  53. for (int i = 0; i < args.length; ++i) {
  54. string arg = args[i].replace("\\", "\\\\").replace("\"", "\\\"");
  55. sb.append("\"%s\",".printf(arg));
  56. }
  57. return "{\"type\":\"command\",\"args\":[%s]}".printf(sb.str);
  58. }
  59. public string reload(string type, string name)
  60. {
  61. return command({ "reload", type, name });
  62. }
  63. public string pause()
  64. {
  65. return command({ "pause" });
  66. }
  67. public string unpause()
  68. {
  69. return command({ "unpause" });
  70. }
  71. public string resize(int width, int height)
  72. {
  73. return "{\"type\":\"resize\",\"width\":%d,\"height\":%d}".printf(width, height);
  74. }
  75. public string frame()
  76. {
  77. return "{\"type\":\"frame\"}";
  78. }
  79. public string refresh(Gee.ArrayList<Value?> resources)
  80. {
  81. StringBuilder sb = new StringBuilder();
  82. foreach (var res in resources) {
  83. sb.append("\"%s\",".printf((string)res));
  84. }
  85. return "{\"type\":\"refresh\",\"list\":[%s]}".printf(sb.str);
  86. }
  87. } /* namespace DeviceApi */
  88. namespace LevelEditorApi
  89. {
  90. public string reset()
  91. {
  92. return "LevelEditor:reset()";
  93. }
  94. public string set_mouse_state(int x, int y, bool left, bool middle, bool right)
  95. {
  96. return "LevelEditor:set_mouse_state(%d,%d,%s,%s,%s)".printf(x
  97. , y
  98. , Lua.bool(left)
  99. , Lua.bool(middle)
  100. , Lua.bool(right)
  101. );
  102. }
  103. public string mouse_wheel(double delta)
  104. {
  105. return "LevelEditor:mouse_wheel(%.17g)".printf(delta);
  106. }
  107. public string mouse_down(int x, int y)
  108. {
  109. return "LevelEditor:mouse_down(%d,%d)".printf(x, y);
  110. }
  111. public string mouse_up(int x, int y)
  112. {
  113. return "LevelEditor:mouse_up(%d,%d)".printf(x, y);
  114. }
  115. public string key_down(string key)
  116. {
  117. return "LevelEditor:key_down(\"%s\")".printf(key);
  118. }
  119. public string key_up(string key)
  120. {
  121. return "LevelEditor:key_up(\"%s\")".printf(key);
  122. }
  123. public string set_tool_type(ToolType type)
  124. {
  125. const string _tools[] =
  126. {
  127. "place_tool",
  128. "move_tool",
  129. "rotate_tool",
  130. "scale_tool"
  131. };
  132. GLib.static_assert(_tools.length == ToolType.COUNT);
  133. return "LevelEditor:set_tool(LevelEditor.%s)".printf(_tools[(int)type]);
  134. }
  135. public string set_snap_mode(SnapMode sm)
  136. {
  137. return """LevelEditor:set_snap_mode("%s")""".printf(sm == SnapMode.RELATIVE ? "relative" : "absolute");
  138. }
  139. public string set_reference_system(ReferenceSystem rs)
  140. {
  141. return """LevelEditor:set_reference_system("%s")""".printf(rs == ReferenceSystem.LOCAL ? "local" : "world");
  142. }
  143. public string set_camera_view_type(CameraViewType type)
  144. {
  145. if (type == CameraViewType.PERSPECTIVE)
  146. return "LevelEditor:camera_view_perspective()";
  147. else if (type == CameraViewType.FRONT)
  148. return "LevelEditor:camera_view_front()";
  149. else if (type == CameraViewType.BACK)
  150. return "LevelEditor:camera_view_back()";
  151. else if (type == CameraViewType.RIGHT)
  152. return "LevelEditor:camera_view_right()";
  153. else if (type == CameraViewType.LEFT)
  154. return "LevelEditor:camera_view_left()";
  155. else if (type == CameraViewType.TOP)
  156. return "LevelEditor:camera_view_top()";
  157. else if (type == CameraViewType.BOTTOM)
  158. return "LevelEditor:camera_view_bottom()";
  159. else
  160. return "LevelEditor:camera_view_perspective()";
  161. }
  162. public string frame_objects(Guid?[] ids)
  163. {
  164. StringBuilder sb = new StringBuilder();
  165. sb.append("LevelEditor:frame_objects({");
  166. for (int i = 0; i < ids.length; ++i)
  167. sb.append("\"%s\",".printf(ids[i].to_string()));
  168. sb.append("})");
  169. return sb.str;
  170. }
  171. public string enable_show_grid(bool enabled)
  172. {
  173. return "LevelEditor:enable_show_grid(%s)".printf(Lua.bool(enabled));
  174. }
  175. public string enable_snap_to_grid(bool enabled)
  176. {
  177. return "LevelEditor:enable_snap_to_grid(%s)".printf(Lua.bool(enabled));
  178. }
  179. public string enable_debug_render_world(bool enabled)
  180. {
  181. return "LevelEditor:enable_debug_render_world(%s)".printf(Lua.bool(enabled));
  182. }
  183. public string enable_debug_physics_world(bool enabled)
  184. {
  185. return "LevelEditor:enable_debug_physics_world(%s)".printf(Lua.bool(enabled));
  186. }
  187. public string set_grid_size(double size)
  188. {
  189. return "LevelEditor:set_grid_size(%.17g)".printf(size);
  190. }
  191. public string set_rotation_snap(double deg)
  192. {
  193. return "LevelEditor:set_rotation_snap(%.17g)".printf(MathUtils.rad(deg));
  194. }
  195. public string spawn_unit(Guid id, string name, Vector3 pos, Quaternion rot, Vector3 scl)
  196. {
  197. return "LevelEditor:spawn_unit(\"%s\", \"%s\", %s, %s, %s)".printf(id.to_string()
  198. , name
  199. , Lua.vector3(pos)
  200. , Lua.quaternion(rot)
  201. , Lua.vector3(scl)
  202. );
  203. }
  204. public string spawn_empty_unit(Guid id)
  205. {
  206. return "LevelEditor:spawn_empty_unit(\"%s\")".printf(id.to_string());
  207. }
  208. public string spawn_sound(Guid id, string name, Vector3 pos, Quaternion rot, double range, double volume, bool loop)
  209. {
  210. return "LevelEditor:spawn_sound(\"%s\", \"%s\", %s, %s, %.17g, %.17g, %s)".printf(id.to_string()
  211. , name
  212. , Lua.vector3(pos)
  213. , Lua.quaternion(rot)
  214. , range
  215. , volume
  216. , Lua.bool(loop)
  217. );
  218. }
  219. public string add_tranform_component(Guid id, Guid component_id, Vector3 pos, Quaternion rot, Vector3 scl)
  220. {
  221. return "LevelEditor:add_transform_component(\"%s\", \"%s\", %s, %s, %s)".printf(id.to_string()
  222. , component_id.to_string()
  223. , Lua.vector3(pos)
  224. , Lua.quaternion(rot)
  225. , Lua.vector3(scl)
  226. );
  227. }
  228. public string add_camera_component(Guid id, Guid component_id, string projection, double fov, double far_range, double near_range)
  229. {
  230. return "LevelEditor:add_camera_component(\"%s\", \"%s\", \"%s\", %.17g, %.17g, %.17g)".printf(id.to_string()
  231. , component_id.to_string()
  232. , projection
  233. , fov
  234. , far_range
  235. , near_range
  236. );
  237. }
  238. public string add_mesh_renderer_component(Guid id, Guid component_id, string mesh_resource, string geometry_name, string material_resource, bool visible)
  239. {
  240. return "LevelEditor:add_mesh_component(\"%s\", \"%s\", \"%s\", \"%s\", \"%s\", %s)".printf(id.to_string()
  241. , component_id.to_string()
  242. , mesh_resource
  243. , geometry_name
  244. , material_resource
  245. , Lua.bool(visible)
  246. );
  247. }
  248. public string add_sprite_renderer_component(Guid id, Guid component_id, string sprite_resource, string material_resource, double layer, double depth, bool visible)
  249. {
  250. return "LevelEditor:add_sprite_component(\"%s\", \"%s\", \"%s\", \"%s\", %.17g, %.17g, %s)".printf(id.to_string()
  251. , component_id.to_string()
  252. , sprite_resource
  253. , material_resource
  254. , layer
  255. , depth
  256. , Lua.bool(visible)
  257. );
  258. }
  259. public string add_light_component(Guid id, Guid component_id, string type, double range, double intensity, double spot_angle, Vector3 color)
  260. {
  261. return "LevelEditor:add_light_component(\"%s\", \"%s\", \"%s\", %.17g, %.17g, %.17g, %s)".printf(id.to_string()
  262. , component_id.to_string()
  263. , type
  264. , range
  265. , intensity
  266. , spot_angle
  267. , Lua.vector3(color)
  268. );
  269. }
  270. public string move_object(Guid id, Vector3 pos, Quaternion rot, Vector3 scl)
  271. {
  272. return "LevelEditor:move_object(\"%s\", %s, %s, %s)".printf(id.to_string()
  273. , Lua.vector3(pos)
  274. , Lua.quaternion(rot)
  275. , Lua.vector3(scl)
  276. );
  277. }
  278. public string set_light(Guid id, string type, double range, double intensity, double spot_angle, Vector3 color)
  279. {
  280. return "LevelEditor._objects[\"%s\"]:set_light(\"%s\", %.17g, %.17g, %.17g, %s)".printf(id.to_string()
  281. , type
  282. , range
  283. , intensity
  284. , spot_angle
  285. , Lua.quaternion({color.x, color.y, color.z, 1.0})
  286. );
  287. }
  288. public string set_sound_range(Guid id, double range)
  289. {
  290. return "LevelEditor._objects[\"%s\"]:set_range(%.17g)".printf(id.to_string()
  291. , range
  292. );
  293. }
  294. public string set_mesh(Guid id, string material, bool visible)
  295. {
  296. return "LevelEditor._objects[\"%s\"]:set_mesh(\"%s\", %s)".printf(id.to_string()
  297. , material
  298. , Lua.bool(visible)
  299. );
  300. }
  301. public string set_sprite(Guid id, string sprite_resource_name, string material, double layer, double depth, bool visible)
  302. {
  303. return "LevelEditor._objects[\"%s\"]:set_sprite(\"%s\", \"%s\", %.17g, %.17g, %s)".printf(id.to_string()
  304. , sprite_resource_name
  305. , material
  306. , layer
  307. , depth
  308. , Lua.bool(visible)
  309. );
  310. }
  311. public string set_camera(Guid id, string projection, double fov, double near_range, double far_range)
  312. {
  313. return "LevelEditor._objects[\"%s\"]:set_camera(\"%s\", %.17g, %.17g, %.17g)".printf(id.to_string()
  314. , projection
  315. , fov
  316. , near_range
  317. , far_range
  318. );
  319. }
  320. public string set_placeable(string type, string name)
  321. {
  322. return "LevelEditor:set_placeable(\"%s\", \"%s\")".printf(type, name);
  323. }
  324. public string selection_set(Guid?[] ids)
  325. {
  326. StringBuilder sb = new StringBuilder();
  327. sb.append("LevelEditor._selection:set({");
  328. for (int i = 0; i < ids.length; ++i)
  329. sb.append("\"%s\",".printf(ids[i].to_string()));
  330. sb.append("})");
  331. return sb.str;
  332. }
  333. public string destroy(Guid id)
  334. {
  335. return "LevelEditor:destroy(\"%s\")".printf(id.to_string());
  336. }
  337. public string set_color(string name, Vector3 color)
  338. {
  339. Quaternion c = Quaternion(color.x, color.y, color.z, 1.0);
  340. return "Colors.%s = function() return %s end".printf(name, Lua.quaternion(c));
  341. }
  342. } /* namespace LevelEditorApi */
  343. namespace UnitPreviewApi
  344. {
  345. public string set_preview_resource(string placeable_type, string name)
  346. {
  347. return "UnitPreview:set_preview_resource(\"%s\", \"%s\")".printf(placeable_type, name);
  348. }
  349. } /* namespace UnitPreviewApi */
  350. namespace ThumbnailApi
  351. {
  352. public string add_request(string placeable_type, string name, string thumbnail_path)
  353. {
  354. return "Thumbnail:add_request(\"%s\", \"%s\", \"%s\")".printf(placeable_type
  355. , name
  356. , thumbnail_path.replace("\\", "\\\\").replace("\"", "\\\"")
  357. );
  358. }
  359. } /* namespace UnitPreviewApi */
  360. } /* namespace Crown */