engine_api.vala 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /*
  2. * Copyright (c) 2012-2022 Daniele Bartolini et al.
  3. * License: https://github.com/dbartolini/crown/blob/master/LICENSE
  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 DataCompilerApi
  28. {
  29. public string compile(Guid id, string data_dir, string platform)
  30. {
  31. return "{\"type\":\"compile\",\"id\":\"%s\",\"data_dir\":\"%s\",\"platform\":\"%s\"}".printf(id.to_string()
  32. , data_dir.replace("\\", "\\\\").replace("\"", "\\\"")
  33. , platform
  34. );
  35. }
  36. public string quit()
  37. {
  38. return "{\"type\":\"quit\"}";
  39. }
  40. } /* namespace DataCompilerApi */
  41. namespace DeviceApi
  42. {
  43. public string command(string[] args)
  44. {
  45. StringBuilder sb = new StringBuilder();
  46. for (int i = 0; i < args.length; ++i) {
  47. string arg = args[i].replace("\\", "\\\\").replace("\"", "\\\"");
  48. sb.append("\"%s\",".printf(arg));
  49. }
  50. return "{\"type\":\"command\",\"args\":[%s]}".printf(sb.str);
  51. }
  52. public string reload(string type, string name)
  53. {
  54. return command({ "reload", type, name });
  55. }
  56. public string refresh()
  57. {
  58. return command({ "refresh" });
  59. }
  60. public string pause()
  61. {
  62. return command({ "pause" });
  63. }
  64. public string unpause()
  65. {
  66. return command({ "unpause" });
  67. }
  68. public string resize(int width, int height)
  69. {
  70. return "{\"type\":\"resize\",\"width\":%d,\"height\":%d}".printf(width, height);
  71. }
  72. public string frame()
  73. {
  74. return "{\"type\":\"frame\"}";
  75. }
  76. } /* namespace DeviceApi */
  77. namespace LevelEditorApi
  78. {
  79. public string reset()
  80. {
  81. return "LevelEditor:reset()";
  82. }
  83. public string set_mouse_state(int x, int y, bool left, bool middle, bool right)
  84. {
  85. return "LevelEditor:set_mouse_state(%d,%d,%s,%s,%s)".printf(x
  86. , y
  87. , Lua.bool(left)
  88. , Lua.bool(middle)
  89. , Lua.bool(right)
  90. );
  91. }
  92. public string mouse_wheel(double delta)
  93. {
  94. return "LevelEditor:mouse_wheel(%.17g)".printf(delta);
  95. }
  96. public string mouse_down(int x, int y)
  97. {
  98. return "LevelEditor:mouse_down(%d,%d)".printf(x, y);
  99. }
  100. public string mouse_up(int x, int y)
  101. {
  102. return "LevelEditor:mouse_up(%d,%d)".printf(x, y);
  103. }
  104. public string key_down(string key)
  105. {
  106. return "LevelEditor:key_down(\"%s\")".printf(key);
  107. }
  108. public string key_up(string key)
  109. {
  110. return "LevelEditor:key_up(\"%s\")".printf(key);
  111. }
  112. public enum ToolType
  113. {
  114. PLACE,
  115. MOVE,
  116. ROTATE,
  117. SCALE,
  118. COUNT
  119. }
  120. public string set_tool_type(ToolType type)
  121. {
  122. const string _tools[] =
  123. {
  124. "place_tool",
  125. "move_tool",
  126. "rotate_tool",
  127. "scale_tool"
  128. };
  129. GLib.static_assert(_tools.length == LevelEditorApi.ToolType.COUNT);
  130. return "LevelEditor:set_tool(LevelEditor.%s)".printf(_tools[(int)type]);
  131. }
  132. public enum SnapMode
  133. {
  134. RELATIVE,
  135. ABSOLUTE
  136. }
  137. public string set_snap_mode(SnapMode sm)
  138. {
  139. return """LevelEditor:set_snap_mode("%s")""".printf(sm == SnapMode.RELATIVE ? "relative" : "absolute");
  140. }
  141. public enum ReferenceSystem
  142. {
  143. LOCAL,
  144. WORLD
  145. }
  146. public string set_reference_system(ReferenceSystem rs)
  147. {
  148. return """LevelEditor:set_reference_system("%s")""".printf(rs == ReferenceSystem.LOCAL ? "local" : "world");
  149. }
  150. public string enable_show_grid(bool enabled)
  151. {
  152. return "LevelEditor:enable_show_grid(%s)".printf(Lua.bool(enabled));
  153. }
  154. public string enable_snap_to_grid(bool enabled)
  155. {
  156. return "LevelEditor:enable_snap_to_grid(%s)".printf(Lua.bool(enabled));
  157. }
  158. public string enable_debug_render_world(bool enabled)
  159. {
  160. return "LevelEditor:enable_debug_render_world(%s)".printf(Lua.bool(enabled));
  161. }
  162. public string enable_debug_physics_world(bool enabled)
  163. {
  164. return "LevelEditor:enable_debug_physics_world(%s)".printf(Lua.bool(enabled));
  165. }
  166. public string set_grid_size(double size)
  167. {
  168. return "LevelEditor:set_grid_size(%.17g)".printf(size);
  169. }
  170. public string set_rotation_snap(double deg)
  171. {
  172. return "LevelEditor:set_rotation_snap(%.17g)".printf(MathUtils.rad(deg));
  173. }
  174. public string spawn_unit(Guid id, string name, Vector3 pos, Quaternion rot, Vector3 scl)
  175. {
  176. return "LevelEditor:spawn_unit(\"%s\", \"%s\", %s, %s, %s)".printf(id.to_string()
  177. , name
  178. , Lua.vector3(pos)
  179. , Lua.quaternion(rot)
  180. , Lua.vector3(scl)
  181. );
  182. }
  183. public string spawn_empty_unit(Guid id)
  184. {
  185. return "LevelEditor:spawn_empty_unit(\"%s\")".printf(id.to_string());
  186. }
  187. public string spawn_sound(Guid id, string name, Vector3 pos, Quaternion rot, double range, double volume, bool loop)
  188. {
  189. return "LevelEditor:spawn_sound(\"%s\", \"%s\", %s, %s, %.17g, %.17g, %s)".printf(id.to_string()
  190. , name
  191. , Lua.vector3(pos)
  192. , Lua.quaternion(rot)
  193. , range
  194. , volume
  195. , Lua.bool(loop)
  196. );
  197. }
  198. public string add_tranform_component(Guid id, Guid component_id, Vector3 pos, Quaternion rot, Vector3 scl)
  199. {
  200. return "LevelEditor:add_transform_component(\"%s\", \"%s\", %s, %s, %s)".printf(id.to_string()
  201. , component_id.to_string()
  202. , Lua.vector3(pos)
  203. , Lua.quaternion(rot)
  204. , Lua.vector3(scl)
  205. );
  206. }
  207. public string add_camera_component(Guid id, Guid component_id, string projection, double fov, double far_range, double near_range)
  208. {
  209. return "LevelEditor:add_camera_component(\"%s\", \"%s\", \"%s\", %.17g, %.17g, %.17g)".printf(id.to_string()
  210. , component_id.to_string()
  211. , projection
  212. , fov
  213. , far_range
  214. , near_range
  215. );
  216. }
  217. public string add_mesh_renderer_component(Guid id, Guid component_id, string mesh_resource, string geometry_name, string material_resource, bool visible)
  218. {
  219. return "LevelEditor:add_mesh_component(\"%s\", \"%s\", \"%s\", \"%s\", \"%s\", %s)".printf(id.to_string()
  220. , component_id.to_string()
  221. , mesh_resource
  222. , geometry_name
  223. , material_resource
  224. , Lua.bool(visible)
  225. );
  226. }
  227. public string add_sprite_renderer_component(Guid id, Guid component_id, string sprite_resource, string material_resource, double layer, double depth, bool visible)
  228. {
  229. return "LevelEditor:add_sprite_component(\"%s\", \"%s\", \"%s\", \"%s\", %.17g, %.17g, %s)".printf(id.to_string()
  230. , component_id.to_string()
  231. , sprite_resource
  232. , material_resource
  233. , layer
  234. , depth
  235. , Lua.bool(visible)
  236. );
  237. }
  238. public string add_light_component(Guid id, Guid component_id, string type, double range, double intensity, double spot_angle, Vector3 color)
  239. {
  240. return "LevelEditor:add_light_component(\"%s\", \"%s\", \"%s\", %.17g, %.17g, %.17g, %s)".printf(id.to_string()
  241. , component_id.to_string()
  242. , type
  243. , range
  244. , intensity
  245. , spot_angle
  246. , Lua.vector3(color)
  247. );
  248. }
  249. public string move_object(Guid id, Vector3 pos, Quaternion rot, Vector3 scl)
  250. {
  251. return "LevelEditor:move_object(\"%s\", %s, %s, %s)".printf(id.to_string()
  252. , Lua.vector3(pos)
  253. , Lua.quaternion(rot)
  254. , Lua.vector3(scl)
  255. );
  256. }
  257. public string set_light(Guid id, string type, double range, double intensity, double spot_angle, Vector3 color)
  258. {
  259. return "LevelEditor._objects[\"%s\"]:set_light(\"%s\", %.17g, %.17g, %.17g, %s)".printf(id.to_string()
  260. , type
  261. , range
  262. , intensity
  263. , spot_angle
  264. , Lua.quaternion({color.x, color.y, color.z, 1.0})
  265. );
  266. }
  267. public string set_sound_range(Guid id, double range)
  268. {
  269. return "LevelEditor._objects[\"%s\"]:set_range(%.17g)".printf(id.to_string()
  270. , range
  271. );
  272. }
  273. public string set_mesh(Guid id, string material, bool visible)
  274. {
  275. return "LevelEditor._objects[\"%s\"]:set_mesh(\"%s\", %s)".printf(id.to_string()
  276. , material
  277. , Lua.bool(visible)
  278. );
  279. }
  280. public string set_sprite(Guid id, string sprite_resource_name, string material, double layer, double depth, bool visible)
  281. {
  282. return "LevelEditor._objects[\"%s\"]:set_sprite(\"%s\", \"%s\", %.17g, %.17g, %s)".printf(id.to_string()
  283. , sprite_resource_name
  284. , material
  285. , layer
  286. , depth
  287. , Lua.bool(visible)
  288. );
  289. }
  290. public string set_camera(Guid id, string projection, double fov, double near_range, double far_range)
  291. {
  292. return "LevelEditor._objects[\"%s\"]:set_camera(\"%s\", %.17g, %.17g, %.17g)".printf(id.to_string()
  293. , projection
  294. , fov
  295. , near_range
  296. , far_range
  297. );
  298. }
  299. public string set_placeable(string type, string name)
  300. {
  301. return "LevelEditor:set_placeable(\"%s\", \"%s\")".printf(type, name);
  302. }
  303. public string selection_set(Guid?[] ids)
  304. {
  305. StringBuilder sb = new StringBuilder();
  306. sb.append("LevelEditor._selection:set({");
  307. for (int i = 0; i < ids.length; ++i)
  308. sb.append("\"%s\",".printf(ids[i].to_string()));
  309. sb.append("})");
  310. return sb.str;
  311. }
  312. public string destroy(Guid id)
  313. {
  314. return "LevelEditor:destroy(\"%s\")".printf(id.to_string());
  315. }
  316. public string set_color(string name, Vector3 color)
  317. {
  318. Quaternion c = Quaternion(color.x, color.y, color.z, 1.0);
  319. return "Colors.%s = function() return %s end".printf(name, Lua.quaternion(c));
  320. }
  321. } /* namespace LevelEditorApi */
  322. namespace UnitPreviewApi
  323. {
  324. public string set_preview_resource(string placeable_type, string name)
  325. {
  326. return "UnitPreview:set_preview_resource(\"%s\", \"%s\")".printf(placeable_type, name);
  327. }
  328. } /* namespace UnitPreviewApi */
  329. } /* namespace Crown */