engine_api.vala 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. /*
  2. * Copyright (c) 2012-2025 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 camera_restore(Vector3 position
  163. , Quaternion rotation
  164. , double ortho_size
  165. , double target_distance
  166. , CameraViewType view_type
  167. )
  168. {
  169. return """LevelEditor._camera:restore(%s, %s, %.17g, %.17g, '%s')""".printf(Lua.vector3(position)
  170. , Lua.quaternion(rotation)
  171. , ortho_size
  172. , target_distance
  173. , view_type == CameraViewType.PERSPECTIVE ? "perspective" : "orthographic"
  174. );
  175. }
  176. public string frame_objects(Guid?[] ids)
  177. {
  178. StringBuilder sb = new StringBuilder();
  179. sb.append("LevelEditor:frame_objects({");
  180. for (int i = 0; i < ids.length; ++i)
  181. sb.append("\"%s\",".printf(ids[i].to_string()));
  182. sb.append("})");
  183. return sb.str;
  184. }
  185. public string enable_show_grid(bool enabled)
  186. {
  187. return "LevelEditor:enable_show_grid(%s)".printf(Lua.bool(enabled));
  188. }
  189. public string enable_snap_to_grid(bool enabled)
  190. {
  191. return "LevelEditor:enable_snap_to_grid(%s)".printf(Lua.bool(enabled));
  192. }
  193. public string enable_debug_render_world(bool enabled)
  194. {
  195. return "LevelEditor:enable_debug_render_world(%s)".printf(Lua.bool(enabled));
  196. }
  197. public string enable_debug_physics_world(bool enabled)
  198. {
  199. return "LevelEditor:enable_debug_physics_world(%s)".printf(Lua.bool(enabled));
  200. }
  201. public string set_grid_size(double size)
  202. {
  203. return "LevelEditor:set_grid_size(%.17g)".printf(size);
  204. }
  205. public string set_rotation_snap(double deg)
  206. {
  207. return "LevelEditor:set_rotation_snap(%.17g)".printf(MathUtils.rad(deg));
  208. }
  209. public string spawn_unit(Guid id, string name, Vector3 pos, Quaternion rot, Vector3 scl)
  210. {
  211. return "LevelEditor:spawn_unit(\"%s\", \"%s\", %s, %s, %s)".printf(id.to_string()
  212. , name
  213. , Lua.vector3(pos)
  214. , Lua.quaternion(rot)
  215. , Lua.vector3(scl)
  216. );
  217. }
  218. public string spawn_empty_unit(Guid id)
  219. {
  220. return "LevelEditor:spawn_empty_unit(\"%s\")".printf(id.to_string());
  221. }
  222. public string spawn_skydome(string skydome_name)
  223. {
  224. return "LevelEditor:spawn_skydome(\"%s\")".printf(skydome_name);
  225. }
  226. public string spawn_sound(Guid id, string name, Vector3 pos, Quaternion rot, double range, double volume, bool loop)
  227. {
  228. return "LevelEditor:spawn_sound(\"%s\", \"%s\", %s, %s, %.17g, %.17g, %s)".printf(id.to_string()
  229. , name
  230. , Lua.vector3(pos)
  231. , Lua.quaternion(rot)
  232. , range
  233. , volume
  234. , Lua.bool(loop)
  235. );
  236. }
  237. public string add_tranform_component(Guid id, Guid component_id, Vector3 pos, Quaternion rot, Vector3 scl)
  238. {
  239. return "LevelEditor:add_transform_component(\"%s\", \"%s\", %s, %s, %s)".printf(id.to_string()
  240. , component_id.to_string()
  241. , Lua.vector3(pos)
  242. , Lua.quaternion(rot)
  243. , Lua.vector3(scl)
  244. );
  245. }
  246. public string add_camera_component(Guid id, Guid component_id, string projection, double fov, double far_range, double near_range)
  247. {
  248. return "LevelEditor:add_camera_component(\"%s\", \"%s\", \"%s\", %.17g, %.17g, %.17g)".printf(id.to_string()
  249. , component_id.to_string()
  250. , projection
  251. , fov
  252. , far_range
  253. , near_range
  254. );
  255. }
  256. public string add_mesh_renderer_component(Guid id
  257. , Guid component_id
  258. , string mesh_resource
  259. , string geometry_name
  260. , string material_resource
  261. , bool visible
  262. , bool cast_shadows
  263. )
  264. {
  265. return "LevelEditor:add_mesh_component(\"%s\", \"%s\", \"%s\", \"%s\", \"%s\", %s, %s)".printf(id.to_string()
  266. , component_id.to_string()
  267. , mesh_resource
  268. , geometry_name
  269. , material_resource
  270. , Lua.bool(visible)
  271. , Lua.bool(cast_shadows)
  272. );
  273. }
  274. public string add_sprite_renderer_component(Guid id
  275. , Guid component_id
  276. , string sprite_resource
  277. , string material_resource
  278. , double layer
  279. , double depth
  280. , bool visible
  281. , bool flip_x
  282. , bool flip_y
  283. )
  284. {
  285. return "LevelEditor:add_sprite_component(\"%s\", \"%s\", \"%s\", \"%s\", %.17g, %.17g, %s, %s, %s)".printf(id.to_string()
  286. , component_id.to_string()
  287. , sprite_resource
  288. , material_resource
  289. , layer
  290. , depth
  291. , Lua.bool(visible)
  292. , Lua.bool(flip_x)
  293. , Lua.bool(flip_y)
  294. );
  295. }
  296. public string add_light_component(Guid id
  297. , Guid component_id
  298. , string type
  299. , double range
  300. , double intensity
  301. , double spot_angle
  302. , Vector3 color
  303. , double shadow_bias
  304. , bool cast_shadows
  305. )
  306. {
  307. return "LevelEditor:add_light_component(\"%s\", \"%s\", \"%s\", %.17g, %.17g, %.17g, %s, %.17g, %s)".printf(id.to_string()
  308. , component_id.to_string()
  309. , type
  310. , range
  311. , intensity
  312. , spot_angle
  313. , Lua.vector3(color)
  314. , shadow_bias
  315. , Lua.bool(cast_shadows)
  316. );
  317. }
  318. public string add_animation_state_machine_component(Guid id
  319. , Guid component_id
  320. , string state_machine_resource
  321. )
  322. {
  323. return "LevelEditor:add_animation_state_machine_component(\"%s\", \"%s\", \"%s\")".printf(id.to_string()
  324. , component_id.to_string()
  325. , state_machine_resource
  326. );
  327. }
  328. public string add_mover_component(Guid id
  329. , Guid component_id
  330. , double height
  331. , double radius
  332. , double max_slope_angle
  333. , string collision_filter
  334. )
  335. {
  336. return "LevelEditor:add_mover_component(\"%s\", \"%s\", %.17g, %.17g, %.17g, \"%s\")".printf(id.to_string()
  337. , component_id.to_string()
  338. , height
  339. , radius
  340. , max_slope_angle
  341. , collision_filter
  342. );
  343. }
  344. public string add_fog_component(Guid id, Guid component_id)
  345. {
  346. return "LevelEditor:add_fog_component(\"%s\", \"%s\")".printf(id.to_string(), component_id.to_string());
  347. }
  348. public string add_global_lighting_component(Guid id, Guid component_id)
  349. {
  350. return "LevelEditor:add_global_lighting_component(\"%s\", \"%s\")".printf(id.to_string(), component_id.to_string());
  351. }
  352. public string add_bloom_component(Guid id, Guid component_id)
  353. {
  354. return "LevelEditor:add_bloom_component(\"%s\", \"%s\")".printf(id.to_string(), component_id.to_string());
  355. }
  356. public string add_tonemap_component(Guid id, Guid component_id)
  357. {
  358. return "LevelEditor:add_tonemap_component(\"%s\", \"%s\")".printf(id.to_string(), component_id.to_string());
  359. }
  360. public string unit_destroy_component_type(Guid id, string component_type)
  361. {
  362. return "LevelEditor:unit_destroy_component_type(\"%s\", \"%s\")".printf(id.to_string()
  363. , component_type
  364. );
  365. }
  366. public string move_object(Guid id, Vector3 pos, Quaternion rot, Vector3 scl)
  367. {
  368. return "LevelEditor:move_object(\"%s\", %s, %s, %s)".printf(id.to_string()
  369. , Lua.vector3(pos)
  370. , Lua.quaternion(rot)
  371. , Lua.vector3(scl)
  372. );
  373. }
  374. public string set_light(Guid id
  375. , string type
  376. , double range
  377. , double intensity
  378. , double spot_angle
  379. , Vector3 color
  380. , double shadow_bias
  381. , bool cast_shadows
  382. )
  383. {
  384. return "LevelEditor._objects[\"%s\"]:set_light(\"%s\", %.17g, %.17g, %.17g, %s, %.17g, %s)".printf(id.to_string()
  385. , type
  386. , range
  387. , intensity
  388. , spot_angle
  389. , Lua.quaternion({color.x, color.y, color.z, 1.0})
  390. , shadow_bias
  391. , Lua.bool(cast_shadows)
  392. );
  393. }
  394. public string set_animation_state_machine(Guid id
  395. , string state_machine_resource
  396. )
  397. {
  398. return "LevelEditor._objects[\"%s\"]:set_animation_state_machine(\"%s\")".printf(id.to_string()
  399. , state_machine_resource
  400. );
  401. }
  402. public string set_mover(Guid id
  403. , double height
  404. , double radius
  405. , double max_slope_angle
  406. , Vector3 center
  407. )
  408. {
  409. return "LevelEditor._objects[\"%s\"]:set_mover(%.17g, %.17g, %.17g, %s)".printf(id.to_string()
  410. , height
  411. , radius
  412. , max_slope_angle
  413. , Lua.vector3(center)
  414. );
  415. }
  416. public string set_fog(Guid id
  417. , Vector3 color
  418. , double density
  419. , double range_min
  420. , double range_max
  421. , double sun_blend
  422. , bool enabled
  423. )
  424. {
  425. return "LevelEditor._objects[\"%s\"]:set_fog(%s, %.17g, %.17g, %.17g, %.17g, %s)".printf(id.to_string()
  426. , Lua.vector3(color)
  427. , density
  428. , range_min
  429. , range_max
  430. , sun_blend
  431. , Lua.bool(enabled)
  432. );
  433. }
  434. public string set_global_lighting(Guid id
  435. , string skydome_map
  436. , double skydome_intensity
  437. , Vector3 ambient_color
  438. )
  439. {
  440. return "LevelEditor._objects[\"%s\"]:set_global_lighting(\"%s\", %.17g, %s)".printf(id.to_string()
  441. , skydome_map
  442. , skydome_intensity
  443. , Lua.quaternion({ambient_color.x, ambient_color.y, ambient_color.z, 1.0})
  444. );
  445. }
  446. public string set_bloom(Guid id
  447. , bool enabled
  448. , double threshold
  449. , double weight
  450. , double intensity
  451. )
  452. {
  453. return "LevelEditor._objects[\"%s\"]:set_bloom(%s, %.17g, %.17g, %.17g)".printf(id.to_string()
  454. , Lua.bool(enabled)
  455. , threshold
  456. , weight
  457. , intensity
  458. );
  459. }
  460. public string set_tonemap(Guid id, string type)
  461. {
  462. return "LevelEditor._objects[\"%s\"]:set_tonemap(\"%s\")".printf(id.to_string(), type);
  463. }
  464. public string set_sound_range(Guid id, double range)
  465. {
  466. return "LevelEditor._objects[\"%s\"]:set_range(%.17g)".printf(id.to_string()
  467. , range
  468. );
  469. }
  470. public string set_mesh(Guid id
  471. , string mesh_resource
  472. , string geometry
  473. , string material
  474. , bool visible
  475. , bool cast_shadows
  476. )
  477. {
  478. return "LevelEditor._objects[\"%s\"]:set_mesh(\"%s\", \"%s\", \"%s\", %s, %s)".printf(id.to_string()
  479. , mesh_resource
  480. , geometry
  481. , material
  482. , Lua.bool(visible)
  483. , Lua.bool(cast_shadows)
  484. );
  485. }
  486. public string set_sprite(Guid id
  487. , string sprite_resource_name
  488. , string material
  489. , double layer
  490. , double depth
  491. , bool visible
  492. , bool flip_x
  493. , bool flip_y
  494. )
  495. {
  496. return "LevelEditor._objects[\"%s\"]:set_sprite(\"%s\", \"%s\", %.17g, %.17g, %s, %s, %s)".printf(id.to_string()
  497. , sprite_resource_name
  498. , material
  499. , layer
  500. , depth
  501. , Lua.bool(visible)
  502. , Lua.bool(flip_x)
  503. , Lua.bool(flip_y)
  504. );
  505. }
  506. public string set_camera(Guid id, string projection, double fov, double near_range, double far_range)
  507. {
  508. return "LevelEditor._objects[\"%s\"]:set_camera(\"%s\", %.17g, %.17g, %.17g)".printf(id.to_string()
  509. , projection
  510. , fov
  511. , near_range
  512. , far_range
  513. );
  514. }
  515. public string set_placeable(string type, string name)
  516. {
  517. return "LevelEditor:set_placeable(\"%s\", \"%s\")".printf(type, name);
  518. }
  519. public string selection_set(Guid?[] ids)
  520. {
  521. StringBuilder sb = new StringBuilder();
  522. sb.append("LevelEditor._selection:set({");
  523. for (int i = 0; i < ids.length; ++i)
  524. sb.append("\"%s\",".printf(ids[i].to_string()));
  525. sb.append("})");
  526. return sb.str;
  527. }
  528. public string destroy(Guid id)
  529. {
  530. return "LevelEditor:destroy(\"%s\")".printf(id.to_string());
  531. }
  532. public string set_color(string name, Vector3 color)
  533. {
  534. Quaternion c = Quaternion(color.x, color.y, color.z, 1.0);
  535. return "Colors.%s = function() return %s end".printf(name, Lua.quaternion(c));
  536. }
  537. public string unit_freeze(Guid id)
  538. {
  539. return "LevelEditor._objects[\"%s\"]:freeze()".printf(id.to_string());
  540. }
  541. public string unit_set_parent(Guid parent_id, Guid child_id)
  542. {
  543. return "LevelEditor._objects[\"%s\"]:set_parent(\"%s\")".printf(child_id.to_string(), parent_id.to_string());
  544. }
  545. } /* namespace LevelEditorApi */
  546. namespace UnitPreviewApi
  547. {
  548. public string set_preview_resource(string placeable_type, string name)
  549. {
  550. return "UnitPreview:set_preview_resource(\"%s\", \"%s\")".printf(placeable_type, name);
  551. }
  552. } /* namespace UnitPreviewApi */
  553. namespace ThumbnailApi
  554. {
  555. public string add_request(string placeable_type, string name, string thumbnail_path)
  556. {
  557. return "Thumbnail:add_request(\"%s\", \"%s\", \"%s\")".printf(placeable_type
  558. , name
  559. , thumbnail_path.replace("\\", "\\\\").replace("\"", "\\\"")
  560. );
  561. }
  562. } /* namespace UnitPreviewApi */
  563. namespace StateMachineEditorApi
  564. {
  565. public string set_unit(string unit_name)
  566. {
  567. return "LevelEditor:set_unit(\"%s\")".printf(unit_name);
  568. }
  569. public string trigger_animation_event(string event_name)
  570. {
  571. return "LevelEditor:trigger_animation_event(\"%s\")".printf(event_name);
  572. }
  573. public string set_variable(string variable_name, double value)
  574. {
  575. return "LevelEditor:set_variable(\"%s\", %f)".printf(variable_name, value);
  576. }
  577. } /* namespace StateMachineEditorApi */
  578. } /* namespace Crown */