engine_api.vala 8.9 KB

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