level_editor_api.vala 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * Copyright (c) 2012-2017 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE-GPLv2
  4. */
  5. namespace Crown
  6. {
  7. namespace LevelEditorApi
  8. {
  9. public string reset()
  10. {
  11. return "LevelEditor:reset()";
  12. }
  13. public string set_mouse_state(int x, int y, bool left, bool middle, bool right)
  14. {
  15. return "LevelEditor:set_mouse_state(%d,%d,%s,%s,%s)".printf(x
  16. , y
  17. , Lua.bool(left)
  18. , Lua.bool(middle)
  19. , Lua.bool(right)
  20. );
  21. }
  22. public string mouse_move(int x, int y, int dx, int dy)
  23. {
  24. return "LevelEditor:mouse_move(%d,%d,%d,%d)".printf(x, y, dx, dy);
  25. }
  26. public string mouse_wheel(double delta)
  27. {
  28. return "LevelEditor:mouse_wheel(%f)".printf(delta);
  29. }
  30. public string mouse_down(int x, int y)
  31. {
  32. return "LevelEditor:mouse_down(%d,%d)".printf(x, y);
  33. }
  34. public string mouse_up(int x, int y)
  35. {
  36. return "LevelEditor:mouse_up(%d,%d)".printf(x, y);
  37. }
  38. public string key_down(string key)
  39. {
  40. return "LevelEditor:key_down(\"%s\")".printf(key);
  41. }
  42. public string key_up(string key)
  43. {
  44. return "LevelEditor:key_up(\"%s\")".printf(key);
  45. }
  46. private const string[] _tools =
  47. {
  48. "place_tool",
  49. "move_tool",
  50. "rotate_tool",
  51. "scale_tool"
  52. };
  53. public string set_tool_type(ToolType type)
  54. {
  55. return "LevelEditor:set_tool(LevelEditor.%s)".printf(_tools[(int)type]);
  56. }
  57. public string set_snap_mode(SnapMode sm)
  58. {
  59. return """LevelEditor:set_snap_mode("%s")""".printf(sm == SnapMode.RELATIVE ? "relative" : "absolute");
  60. }
  61. public string set_reference_system(ReferenceSystem rs)
  62. {
  63. return """LevelEditor:set_reference_system("%s")""".printf(rs == ReferenceSystem.LOCAL ? "local" : "world");
  64. }
  65. public string enable_show_grid(bool enabled)
  66. {
  67. return "LevelEditor:enable_show_grid(%s)".printf(Lua.bool(enabled));
  68. }
  69. public string enable_snap_to_grid(bool enabled)
  70. {
  71. return "LevelEditor:enable_snap_to_grid(%s)".printf(Lua.bool(enabled));
  72. }
  73. public string enable_debug_render_world(bool enabled)
  74. {
  75. return "LevelEditor:enable_debug_render_world(%s)".printf(Lua.bool(enabled));
  76. }
  77. public string enable_debug_physics_world(bool enabled)
  78. {
  79. return "LevelEditor:enable_debug_physics_world(%s)".printf(Lua.bool(enabled));
  80. }
  81. public string set_grid_size(double size)
  82. {
  83. return "LevelEditor:set_grid_size(%f)".printf(size);
  84. }
  85. public string set_rotation_snap(double deg)
  86. {
  87. return "LevelEditor:set_rotation_snap(%f)".printf(MathUtils.rad(deg));
  88. }
  89. public string spawn_unit(Guid id, string name, Vector3 pos, Quaternion rot, Vector3 scl)
  90. {
  91. return "LevelEditor:spawn_unit(\"%s\", \"%s\", %s, %s, %s)".printf(id.to_string()
  92. , name
  93. , Lua.vector3(pos)
  94. , Lua.quaternion(rot)
  95. , Lua.vector3(scl)
  96. );
  97. }
  98. public string spawn_empty_unit(Guid id)
  99. {
  100. return "LevelEditor:spawn_empty_unit(\"%s\")".printf(id.to_string());
  101. }
  102. public string spawn_sound(Guid id, string name, Vector3 pos, Quaternion rot, double range, double volume, bool loop)
  103. {
  104. return "LevelEditor:spawn_sound(\"%s\", \"%s\", %s, %s, %f, %f, %s)".printf(id.to_string()
  105. , name
  106. , Lua.vector3(pos)
  107. , Lua.quaternion(rot)
  108. , range
  109. , volume
  110. , Lua.bool(loop)
  111. );
  112. }
  113. public string add_tranform_component(Guid id, Guid component_id, Vector3 pos, Quaternion rot, Vector3 scl)
  114. {
  115. return "LevelEditor:add_transform_component(\"%s\", \"%s\", %s, %s, %s)".printf(id.to_string()
  116. , component_id.to_string()
  117. , Lua.vector3(pos)
  118. , Lua.quaternion(rot)
  119. , Lua.vector3(scl)
  120. );
  121. }
  122. public string add_mesh_component(Guid id, Guid component_id, string mesh_resource, string geometry_name, string material_resource, bool visible)
  123. {
  124. return "LevelEditor:add_mesh_component(\"%s\", \"%s\", \"%s\", \"%s\", \"%s\", %s)".printf(id.to_string()
  125. , component_id.to_string()
  126. , mesh_resource
  127. , geometry_name
  128. , material_resource
  129. , Lua.bool(visible)
  130. );
  131. }
  132. public string add_sprite_component(Guid id, Guid component_id, string sprite_resource, string material_resource, bool visible)
  133. {
  134. return "LevelEditor:add_sprite_component(\"%s\", \"%s\", \"%s\", \"%s\", %s)".printf(id.to_string()
  135. , component_id.to_string()
  136. , sprite_resource
  137. , material_resource
  138. , Lua.bool(visible)
  139. );
  140. }
  141. public string add_camera_component(Guid id, Guid component_id, string projection, double fov, double far_range, double near_range)
  142. {
  143. return "LevelEditor:add_camera_component(\"%s\", \"%s\", \"%s\", %f, %f, %f)".printf(id.to_string()
  144. , component_id.to_string()
  145. , projection
  146. , fov
  147. , far_range
  148. , near_range
  149. );
  150. }
  151. public string add_light_component(Guid id, Guid component_id, string type, double range, double intensity, double spot_angle, Vector3 color)
  152. {
  153. return "LevelEditor:add_light_component(\"%s\", \"%s\", \"%s\", %f, %f, %f, %s)".printf(id.to_string()
  154. , component_id.to_string()
  155. , type
  156. , range
  157. , intensity
  158. , spot_angle
  159. , Lua.vector3(color)
  160. );
  161. }
  162. public string move_object(Guid id, Vector3 pos, Quaternion rot, Vector3 scl)
  163. {
  164. return @"LevelEditor:move_object(\"%s\", %s, %s, %s)".printf(id.to_string()
  165. , Lua.vector3(pos)
  166. , Lua.quaternion(rot)
  167. , Lua.vector3(scl)
  168. );
  169. }
  170. public string set_light(Guid id, string type, double range, double intensity, double spot_angle, Vector3 color)
  171. {
  172. return @"LevelEditor._objects[\"%s\"]:set_light(\"%s\", %f, %f, %f, %s)".printf(id.to_string()
  173. , type
  174. , range
  175. , intensity
  176. , spot_angle
  177. , Lua.quaternion({color.x, color.y, color.z, 1.0})
  178. );
  179. }
  180. public string set_sound_range(Guid id, double range)
  181. {
  182. return @"LevelEditor._objects[\"%s\"]:set_range(%f)".printf(id.to_string()
  183. , range
  184. );
  185. }
  186. public string set_placeable(string type, string name)
  187. {
  188. return "LevelEditor:set_placeable(\"%s\", \"%s\")".printf(type, name);
  189. }
  190. public string selection_set(Guid[] ids)
  191. {
  192. StringBuilder sb = new StringBuilder();
  193. sb.append("LevelEditor._selection:set({");
  194. for (int i = 0; i < ids.length; ++i)
  195. sb.append("\"%s\",".printf(ids[i].to_string()));
  196. sb.append("})");
  197. return sb.str;
  198. }
  199. public string destroy(Guid id)
  200. {
  201. return @"LevelEditor:destroy(\"%s\")".printf(id.to_string());
  202. }
  203. public string set_color(string name, Vector3 color)
  204. {
  205. Quaternion c = Quaternion(color.x, color.y, color.z, 1.0);
  206. return @"Colors.%s = function() return %s end".printf(name, Lua.quaternion(c));
  207. }
  208. }
  209. }