ToolCoreJS.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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/Atomic3D/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/Assets/ModelImporter.h>
  31. using namespace Atomic;
  32. namespace Atomic
  33. {
  34. extern void jsb_package_toolcore_init(JSVM* vm);
  35. }
  36. namespace ToolCore
  37. {
  38. static int js_atomic_GetToolEnvironment(duk_context* ctx)
  39. {
  40. JSVM* vm = JSVM::GetJSVM(ctx);
  41. js_push_class_object_instance(ctx, vm->GetSubsystem<ToolEnvironment>());
  42. return 1;
  43. }
  44. static int js_atomic_GetToolSystem(duk_context* ctx)
  45. {
  46. JSVM* vm = JSVM::GetJSVM(ctx);
  47. js_push_class_object_instance(ctx, vm->GetSubsystem<ToolSystem>());
  48. return 1;
  49. }
  50. static int js_atomic_GetLicenseSystem(duk_context* ctx)
  51. {
  52. JSVM* vm = JSVM::GetJSVM(ctx);
  53. js_push_class_object_instance(ctx, vm->GetSubsystem<LicenseSystem>());
  54. return 1;
  55. }
  56. static int js_atomic_GetAssetDatabase(duk_context* ctx)
  57. {
  58. JSVM* vm = JSVM::GetJSVM(ctx);
  59. js_push_class_object_instance(ctx, vm->GetSubsystem<AssetDatabase>());
  60. return 1;
  61. }
  62. static int AssetDatabase_GetFolderAssets(duk_context* ctx)
  63. {
  64. JSVM* vm = JSVM::GetJSVM(ctx);
  65. ToolSystem* ts = vm->GetSubsystem<ToolSystem>();
  66. AssetDatabase* db = vm->GetSubsystem<AssetDatabase>();
  67. Project* project = ts->GetProject();
  68. String folder = duk_require_string(ctx, 0);
  69. duk_push_array(ctx);
  70. if (!project)
  71. return 1;
  72. PODVector<Asset*> assets;
  73. db->GetFolderAssets(folder, assets);
  74. for(unsigned i = 0; i < assets.Size(); i++)
  75. {
  76. js_push_class_object_instance(ctx, assets[i], 0);
  77. duk_put_prop_index(ctx, -2, i);
  78. }
  79. return 1;
  80. }
  81. static int AssetDatabase_GetAssetsByImporterType(duk_context* ctx)
  82. {
  83. JSVM* vm = JSVM::GetJSVM(ctx);
  84. ToolSystem* ts = vm->GetSubsystem<ToolSystem>();
  85. AssetDatabase* db = vm->GetSubsystem<AssetDatabase>();
  86. Project* project = ts->GetProject();
  87. StringHash type = duk_require_string(ctx, 0);
  88. String resourceType = duk_require_string(ctx, 1);
  89. duk_push_array(ctx);
  90. if (!project)
  91. return 1;
  92. PODVector<Asset*> assets;
  93. db->GetAssetsByImporterType(type, resourceType, assets);
  94. for(unsigned i = 0; i < assets.Size(); i++)
  95. {
  96. js_push_class_object_instance(ctx, assets[i], 0);
  97. duk_put_prop_index(ctx, -2, i);
  98. }
  99. return 1;
  100. }
  101. static int ModelImporter_GetAnimations(duk_context* ctx)
  102. {
  103. duk_push_this(ctx);
  104. ModelImporter* importer = js_to_class_instance<ModelImporter>(ctx, -1, 0);
  105. PODVector<Animation*> animations;
  106. importer->GetAnimations(animations);
  107. duk_push_array(ctx);
  108. for(unsigned i = 0; i < animations.Size(); i++)
  109. {
  110. js_push_class_object_instance(ctx, animations[i], 0);
  111. duk_put_prop_index(ctx, -2, i);
  112. }
  113. return 1;
  114. }
  115. void jsapi_init_toolcore(JSVM* vm)
  116. {
  117. duk_context* ctx = vm->GetJSContext();
  118. jsb_package_toolcore_init(vm);
  119. duk_get_global_string(ctx, "ToolCore");
  120. duk_push_c_function(ctx, js_atomic_GetToolEnvironment, 0);
  121. duk_put_prop_string(ctx, -2, "getToolEnvironment");
  122. js_push_class_object_instance(ctx, vm->GetSubsystem<ToolEnvironment>(), "ToolEnvironment");
  123. duk_put_prop_string(ctx, -2, "toolEnvironment");
  124. duk_push_c_function(ctx, js_atomic_GetToolSystem, 0);
  125. duk_put_prop_string(ctx, -2, "getToolSystem");
  126. js_push_class_object_instance(ctx, vm->GetSubsystem<ToolSystem>(), "ToolSystem");
  127. duk_put_prop_string(ctx, -2, "toolSystem");
  128. js_push_class_object_instance(ctx, vm->GetSubsystem<BuildSystem>(), "BuildSystem");
  129. duk_put_prop_string(ctx, -2, "buildSystem");
  130. duk_push_c_function(ctx, js_atomic_GetLicenseSystem, 0);
  131. duk_put_prop_string(ctx, -2, "getLicenseSystem");
  132. js_push_class_object_instance(ctx, vm->GetSubsystem<LicenseSystem>(), "LicenseSystem");
  133. duk_put_prop_string(ctx, -2, "licenseSystem");
  134. duk_push_c_function(ctx, js_atomic_GetAssetDatabase, 0);
  135. duk_put_prop_string(ctx, -2, "getAssetDatabase");
  136. js_push_class_object_instance(ctx, vm->GetSubsystem<AssetDatabase>(), "AssetDatabase");
  137. duk_put_prop_string(ctx, -2, "assetDatabase");
  138. duk_pop(ctx);
  139. js_class_get_prototype(ctx, "ToolCore", "AssetDatabase");
  140. duk_push_c_function(ctx, AssetDatabase_GetFolderAssets, 1);
  141. duk_put_prop_string(ctx, -2, "getFolderAssets");
  142. duk_push_c_function(ctx, AssetDatabase_GetAssetsByImporterType, 2);
  143. duk_put_prop_string(ctx, -2, "getAssetsByImporterType");
  144. duk_pop(ctx);
  145. js_class_get_prototype(ctx, "ToolCore", "ModelImporter");
  146. duk_push_c_function(ctx, ModelImporter_GetAnimations, 0);
  147. duk_put_prop_string(ctx, -2, "getAnimations");
  148. duk_pop(ctx);
  149. }
  150. }