level_editor_api.vala 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * Copyright (c) 2012-2016 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 set_mouse_move(int x, int y, int dx, int dy)
  23. {
  24. return "LevelEditor:set_mouse_move(%d,%d,%d,%d)".printf(x, y, dx, dy);
  25. }
  26. public string set_mouse_wheel(double delta)
  27. {
  28. return "LevelEditor:set_mouse_wheel(%f)".printf(delta);
  29. }
  30. public string set_mouse_down(int x, int y)
  31. {
  32. return "LevelEditor:mouse_down(%d,%d)".printf(x, y);
  33. }
  34. public string set_mouse_up(int x, int y)
  35. {
  36. return "LevelEditor:mouse_up(%d,%d)".printf(x, y);
  37. }
  38. public string set_key_down(string key)
  39. {
  40. return "LevelEditor:key_down(\"%s\")".printf(key);
  41. }
  42. public string set_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, Vector3 pos, Quaternion rot, double range, double volume, bool loop)
  103. {
  104. return "LevelEditor:spawn_sound(\"%s\", %s, %s, %f, %f, %s)".printf(id.to_string()
  105. , Lua.vector3(pos)
  106. , Lua.quaternion(rot)
  107. , range
  108. , volume
  109. , Lua.bool(loop)
  110. );
  111. }
  112. public string add_tranform_component(Guid id, Guid component_id, Vector3 pos, Quaternion rot, Vector3 scl)
  113. {
  114. return "LevelEditor:add_transform_component(\"%s\", \"%s\", %s, %s, %s)".printf(id.to_string()
  115. , component_id.to_string()
  116. , Lua.vector3(pos)
  117. , Lua.quaternion(rot)
  118. , Lua.vector3(scl)
  119. );
  120. }
  121. public string add_mesh_component(Guid id, Guid component_id, string mesh_resource, string geometry_name, string material_resource, bool visible)
  122. {
  123. return "LevelEditor:add_mesh_component(\"%s\", \"%s\", \"%s\", \"%s\", \"%s\", %s)".printf(id.to_string()
  124. , component_id.to_string()
  125. , mesh_resource
  126. , geometry_name
  127. , material_resource
  128. , Lua.bool(visible)
  129. );
  130. }
  131. public string add_sprite_component(Guid id, Guid component_id, string sprite_resource, string material_resource, bool visible)
  132. {
  133. return "LevelEditor:add_sprite_component(\"%s\", \"%s\", \"%s\", \"%s\", %s)".printf(id.to_string()
  134. , component_id.to_string()
  135. , sprite_resource
  136. , material_resource
  137. , Lua.bool(visible)
  138. );
  139. }
  140. public string add_camera_component(Guid id, Guid component_id, string projection, double fov, double far_range, double near_range)
  141. {
  142. return "LevelEditor:add_camera_component(\"%s\", \"%s\", \"%s\", %f, %f, %f)".printf(id.to_string()
  143. , component_id.to_string()
  144. , projection
  145. , fov
  146. , far_range
  147. , near_range
  148. );
  149. }
  150. public string add_light_component(Guid id, Guid component_id, string type, double range, double intensity, double spot_angle, Vector3 color)
  151. {
  152. return "LevelEditor:add_light_component(\"%s\", \"%s\", \"%s\", %f, %f, %f, %s)".printf(id.to_string()
  153. , component_id.to_string()
  154. , type
  155. , range
  156. , intensity
  157. , spot_angle
  158. , Lua.vector3(color)
  159. );
  160. }
  161. public string move_object(Guid id, Vector3 pos, Quaternion rot, Vector3 scl)
  162. {
  163. return @"LevelEditor:move_object(\"%s\", %s, %s, %s)".printf(id.to_string()
  164. , Lua.vector3(pos)
  165. , Lua.quaternion(rot)
  166. , Lua.vector3(scl)
  167. );
  168. }
  169. public string set_light(Guid id, string type, double range, double intensity, double spot_angle, Vector3 color)
  170. {
  171. return @"LevelEditor._objects[\"%s\"]:set_light(\"%s\", %f, %f, %f, %s)".printf(id.to_string()
  172. , type
  173. , range
  174. , intensity
  175. , spot_angle
  176. , Lua.quaternion({color.x, color.y, color.z, 1.0})
  177. );
  178. }
  179. public string set_placeable(PlaceableType type, string name)
  180. {
  181. return "LevelEditor:set_placeable(\"%s\", \"%s\")".printf((type == PlaceableType.UNIT ? "unit" : "sound"), name);
  182. }
  183. public string selection_set(Guid[] ids)
  184. {
  185. StringBuilder sb = new StringBuilder();
  186. sb.append("LevelEditor._selection:set({");
  187. for (int i = 0; i < ids.length; ++i)
  188. sb.append("\"%s\",".printf(ids[i].to_string()));
  189. sb.append("})");
  190. return sb.str;
  191. }
  192. public string destroy(Guid id)
  193. {
  194. return @"LevelEditor:destroy(\"%s\")".printf(id.to_string());
  195. }
  196. public string set_color(string name, Vector3 color)
  197. {
  198. Quaternion c = Quaternion(color.x, color.y, color.z, 1.0);
  199. return @"Colors.%s = function() return %s end".printf(name, Lua.quaternion(c));
  200. }
  201. }
  202. }