ToolCoreJS.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Atomic/Graphics/Animation.h>
  23. #include <AtomicJS/Javascript/JSVM.h>
  24. #include <ToolCore/ToolEnvironment.h>
  25. #include <ToolCore/ToolSystem.h>
  26. #include <ToolCore/Assets/AssetDatabase.h>
  27. #include <ToolCore/Project/Project.h>
  28. #include <ToolCore/License/LicenseSystem.h>
  29. #include <ToolCore/Build/BuildSystem.h>
  30. #include <ToolCore/NETTools/NETProjectSystem.h>
  31. #include <ToolCore/Assets/ModelImporter.h>
  32. using namespace Atomic;
  33. namespace Atomic
  34. {
  35. extern void jsb_package_toolcore_init(JSVM* vm);
  36. }
  37. namespace ToolCore
  38. {
  39. static int js_atomic_GetToolEnvironment(duk_context* ctx)
  40. {
  41. JSVM* vm = JSVM::GetJSVM(ctx);
  42. js_push_class_object_instance(ctx, vm->GetSubsystem<ToolEnvironment>());
  43. return 1;
  44. }
  45. static int js_atomic_GetToolSystem(duk_context* ctx)
  46. {
  47. JSVM* vm = JSVM::GetJSVM(ctx);
  48. js_push_class_object_instance(ctx, vm->GetSubsystem<ToolSystem>());
  49. return 1;
  50. }
  51. static int js_atomic_GetLicenseSystem(duk_context* ctx)
  52. {
  53. JSVM* vm = JSVM::GetJSVM(ctx);
  54. js_push_class_object_instance(ctx, vm->GetSubsystem<LicenseSystem>());
  55. return 1;
  56. }
  57. static int js_atomic_GetAssetDatabase(duk_context* ctx)
  58. {
  59. JSVM* vm = JSVM::GetJSVM(ctx);
  60. js_push_class_object_instance(ctx, vm->GetSubsystem<AssetDatabase>());
  61. return 1;
  62. }
  63. static int AssetDatabase_GetFolderAssets(duk_context* ctx)
  64. {
  65. JSVM* vm = JSVM::GetJSVM(ctx);
  66. ToolSystem* ts = vm->GetSubsystem<ToolSystem>();
  67. AssetDatabase* db = vm->GetSubsystem<AssetDatabase>();
  68. Project* project = ts->GetProject();
  69. String folder = duk_require_string(ctx, 0);
  70. duk_push_array(ctx);
  71. if (!project)
  72. return 1;
  73. PODVector<Asset*> assets;
  74. db->GetFolderAssets(folder, assets);
  75. for(unsigned i = 0; i < assets.Size(); i++)
  76. {
  77. js_push_class_object_instance(ctx, assets[i], 0);
  78. duk_put_prop_index(ctx, -2, i);
  79. }
  80. return 1;
  81. }
  82. static int AssetDatabase_GetAssetsByImporterType(duk_context* ctx)
  83. {
  84. JSVM* vm = JSVM::GetJSVM(ctx);
  85. ToolSystem* ts = vm->GetSubsystem<ToolSystem>();
  86. AssetDatabase* db = vm->GetSubsystem<AssetDatabase>();
  87. Project* project = ts->GetProject();
  88. StringHash type = duk_require_string(ctx, 0);
  89. String resourceType = duk_require_string(ctx, 1);
  90. duk_push_array(ctx);
  91. if (!project)
  92. return 1;
  93. PODVector<Asset*> assets;
  94. db->GetAssetsByImporterType(type, resourceType, assets);
  95. for(unsigned i = 0; i < assets.Size(); i++)
  96. {
  97. js_push_class_object_instance(ctx, assets[i], 0);
  98. duk_put_prop_index(ctx, -2, i);
  99. }
  100. return 1;
  101. }
  102. static int ModelImporter_GetAnimations(duk_context* ctx)
  103. {
  104. duk_push_this(ctx);
  105. ModelImporter* importer = js_to_class_instance<ModelImporter>(ctx, -1, 0);
  106. PODVector<Animation*> animations;
  107. importer->GetAnimations(animations);
  108. duk_push_array(ctx);
  109. for(unsigned i = 0; i < animations.Size(); i++)
  110. {
  111. js_push_class_object_instance(ctx, animations[i], 0);
  112. duk_put_prop_index(ctx, -2, i);
  113. }
  114. return 1;
  115. }
  116. void jsapi_init_toolcore(JSVM* vm)
  117. {
  118. duk_context* ctx = vm->GetJSContext();
  119. jsb_package_toolcore_init(vm);
  120. duk_get_global_string(ctx, "ToolCore");
  121. duk_push_c_function(ctx, js_atomic_GetToolEnvironment, 0);
  122. duk_put_prop_string(ctx, -2, "getToolEnvironment");
  123. js_push_class_object_instance(ctx, vm->GetSubsystem<ToolEnvironment>(), "ToolEnvironment");
  124. duk_put_prop_string(ctx, -2, "toolEnvironment");
  125. duk_push_c_function(ctx, js_atomic_GetToolSystem, 0);
  126. duk_put_prop_string(ctx, -2, "getToolSystem");
  127. js_push_class_object_instance(ctx, vm->GetSubsystem<ToolSystem>(), "ToolSystem");
  128. duk_put_prop_string(ctx, -2, "toolSystem");
  129. js_push_class_object_instance(ctx, vm->GetSubsystem<BuildSystem>(), "BuildSystem");
  130. duk_put_prop_string(ctx, -2, "buildSystem");
  131. js_push_class_object_instance(ctx, vm->GetSubsystem<NETProjectSystem>(), "NETProjectSystem");
  132. duk_put_prop_string(ctx, -2, "netProjectSystem");
  133. duk_push_c_function(ctx, js_atomic_GetLicenseSystem, 0);
  134. duk_put_prop_string(ctx, -2, "getLicenseSystem");
  135. js_push_class_object_instance(ctx, vm->GetSubsystem<LicenseSystem>(), "LicenseSystem");
  136. duk_put_prop_string(ctx, -2, "licenseSystem");
  137. duk_push_c_function(ctx, js_atomic_GetAssetDatabase, 0);
  138. duk_put_prop_string(ctx, -2, "getAssetDatabase");
  139. js_push_class_object_instance(ctx, vm->GetSubsystem<AssetDatabase>(), "AssetDatabase");
  140. duk_put_prop_string(ctx, -2, "assetDatabase");
  141. duk_pop(ctx);
  142. js_class_get_prototype(ctx, "ToolCore", "AssetDatabase");
  143. duk_push_c_function(ctx, AssetDatabase_GetFolderAssets, 1);
  144. duk_put_prop_string(ctx, -2, "getFolderAssets");
  145. duk_push_c_function(ctx, AssetDatabase_GetAssetsByImporterType, 2);
  146. duk_put_prop_string(ctx, -2, "getAssetsByImporterType");
  147. duk_pop(ctx);
  148. js_class_get_prototype(ctx, "ToolCore", "ModelImporter");
  149. duk_push_c_function(ctx, ModelImporter_GetAnimations, 0);
  150. duk_put_prop_string(ctx, -2, "getAnimations");
  151. duk_pop(ctx);
  152. }
  153. }