engine_api.vala 8.1 KB

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