BsScriptSelection.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "Wrappers/BsScriptSelection.h"
  4. #include "BsScriptMeta.h"
  5. #include "BsMonoClass.h"
  6. #include "BsMonoMethod.h"
  7. #include "Scene/BsSelection.h"
  8. #include "Wrappers/BsScriptSceneObject.h"
  9. #include "BsMonoUtil.h"
  10. #include "BsScriptGameObjectManager.h"
  11. #include "Wrappers/BsScriptResource.h"
  12. namespace bs
  13. {
  14. ScriptSelection::OnSelectionChangedThunkDef ScriptSelection::OnSelectionChangedThunk;
  15. ScriptSelection::OnPingResourceThunkDef ScriptSelection::OnPingResourceThunk;
  16. ScriptSelection::OnPingSceneObjectThunkDef ScriptSelection::OnPingSceneObjectThunk;
  17. HEvent ScriptSelection::OnSelectionChangedConn;
  18. HEvent ScriptSelection::OnPingResourceConn;
  19. HEvent ScriptSelection::OnPingSceneObjectConn;
  20. ScriptSelection::ScriptSelection(MonoObject* instance)
  21. :ScriptObject(instance)
  22. {
  23. }
  24. void ScriptSelection::initRuntimeData()
  25. {
  26. metaData.scriptClass->addInternalCall("Internal_GetSceneObjectSelection", (void*)&ScriptSelection::internal_GetSceneObjectSelection);
  27. metaData.scriptClass->addInternalCall("Internal_SetSceneObjectSelection", (void*)&ScriptSelection::internal_SetSceneObjectSelection);
  28. metaData.scriptClass->addInternalCall("Internal_GetResourceUUIDSelection", (void*)&ScriptSelection::internal_GetResourceUUIDSelection);
  29. metaData.scriptClass->addInternalCall("Internal_SetResourceUUIDSelection", (void*)&ScriptSelection::internal_SetResourceUUIDSelection);
  30. metaData.scriptClass->addInternalCall("Internal_GetResourcePathSelection", (void*)&ScriptSelection::internal_GetResourcePathSelection);
  31. metaData.scriptClass->addInternalCall("Internal_SetResourcePathSelection", (void*)&ScriptSelection::internal_SetResourcePathSelection);
  32. metaData.scriptClass->addInternalCall("Internal_PingResource", (void*)&ScriptSelection::internal_PingResource);
  33. metaData.scriptClass->addInternalCall("Internal_PingSceneObject", (void*)&ScriptSelection::internal_PingSceneObject);
  34. OnSelectionChangedThunk = (OnSelectionChangedThunkDef)metaData.scriptClass->getMethod("Internal_TriggerSelectionChanged", 2)->getThunk();
  35. OnPingResourceThunk = (OnPingResourceThunkDef)metaData.scriptClass->getMethod("Internal_TriggerResourcePing", 1)->getThunk();
  36. OnPingSceneObjectThunk = (OnPingSceneObjectThunkDef)metaData.scriptClass->getMethod("Internal_TriggerSceneObjectPing", 1)->getThunk();
  37. }
  38. void ScriptSelection::internal_GetSceneObjectSelection(MonoArray** selection)
  39. {
  40. Vector<HSceneObject> sceneObjects = Selection::instance().getSceneObjects();
  41. ::MonoClass* sceneObjectMonoClass = ScriptSceneObject::getMetaData()->scriptClass->_getInternalClass();
  42. ScriptArray sceneObjectArray(sceneObjectMonoClass, (UINT32)sceneObjects.size());
  43. for (UINT32 i = 0; i < (UINT32)sceneObjects.size(); i++)
  44. {
  45. ScriptSceneObject* scriptSceneObject = ScriptGameObjectManager::instance().getOrCreateScriptSceneObject(sceneObjects[i]);
  46. MonoObject* sceneMonoObject = scriptSceneObject->getManagedInstance();
  47. sceneObjectArray.set(i, sceneMonoObject);
  48. }
  49. MonoUtil::referenceCopy(selection, (MonoObject*)sceneObjectArray.getInternal());
  50. }
  51. void ScriptSelection::internal_SetSceneObjectSelection(MonoArray* selection)
  52. {
  53. Vector<HSceneObject> sceneObjects;
  54. if (selection != nullptr)
  55. {
  56. ScriptArray scriptArray(selection);
  57. UINT32 arrayLen = scriptArray.size();
  58. for (UINT32 i = 0; i < arrayLen; i++)
  59. {
  60. MonoObject* monoSO = scriptArray.get<MonoObject*>(i);
  61. ScriptSceneObject* scriptSO = ScriptSceneObject::toNative(monoSO);
  62. if (scriptSO == nullptr)
  63. continue;
  64. HSceneObject so = static_object_cast<SceneObject>(scriptSO->getNativeHandle());
  65. sceneObjects.push_back(so);
  66. }
  67. }
  68. Selection::instance().setSceneObjects(sceneObjects);
  69. }
  70. void ScriptSelection::internal_GetResourceUUIDSelection(MonoArray** selection)
  71. {
  72. Vector<UUID> uuids = Selection::instance().getResourceUUIDs();
  73. ScriptArray uuidArray(MonoUtil::getStringClass(), (UINT32)uuids.size());
  74. for (UINT32 i = 0; i < (UINT32)uuids.size(); i++)
  75. uuidArray.set(i, ScriptUUID::box(uuids[i]));
  76. MonoUtil::referenceCopy(selection, (MonoObject*)uuidArray.getInternal());
  77. }
  78. void ScriptSelection::internal_SetResourceUUIDSelection(MonoArray* selection)
  79. {
  80. Vector<UUID> uuids;
  81. ScriptArray uuidArray(selection);
  82. UINT32 arrayLen = uuidArray.size();
  83. for (UINT32 i = 0; i < arrayLen; i++)
  84. uuids.push_back(ScriptUUID::unbox(uuidArray.get<MonoObject*>(i)));
  85. Selection::instance().setResourceUUIDs(uuids);
  86. }
  87. void ScriptSelection::internal_GetResourcePathSelection(MonoArray** selection)
  88. {
  89. Vector<Path> paths = Selection::instance().getResourcePaths();
  90. ScriptArray pathArray(MonoUtil::getStringClass(), (UINT32)paths.size());
  91. for (UINT32 i = 0; i < (UINT32)paths.size(); i++)
  92. {
  93. MonoString* monoString = MonoUtil::stringToMono(paths[i].toString());
  94. pathArray.set(i, monoString);
  95. }
  96. MonoUtil::referenceCopy(selection, (MonoObject*)pathArray.getInternal());
  97. }
  98. void ScriptSelection::internal_SetResourcePathSelection(MonoArray* selection)
  99. {
  100. Vector<Path> paths;
  101. ScriptArray pathArray(selection);
  102. UINT32 arrayLen = pathArray.size();
  103. for (UINT32 i = 0; i < arrayLen; i++)
  104. {
  105. MonoString* monoString = pathArray.get<MonoString*>(i);
  106. Path path = MonoUtil::monoToString(monoString);
  107. paths.push_back(path);
  108. }
  109. Selection::instance().setResourcePaths(paths);
  110. }
  111. void ScriptSelection::internal_PingResource(MonoString* resourcePath)
  112. {
  113. Path path = MonoUtil::monoToString(resourcePath);
  114. Selection::instance().ping(path);
  115. }
  116. void ScriptSelection::internal_PingSceneObject(MonoObject* so)
  117. {
  118. ScriptSceneObject* scriptSO = ScriptSceneObject::toNative(so);
  119. HSceneObject soHandle = static_object_cast<SceneObject>(scriptSO->getNativeHandle());
  120. Selection::instance().ping(soHandle);
  121. }
  122. void ScriptSelection::startUp()
  123. {
  124. OnSelectionChangedConn = Selection::instance().onSelectionChanged.connect(&ScriptSelection::onSelectionChanged);
  125. OnPingResourceConn = Selection::instance().onResourcePing.connect(&ScriptSelection::onResourcePing);
  126. OnPingSceneObjectConn = Selection::instance().onSceneObjectPing.connect(&ScriptSelection::onSceneObjectPing);
  127. }
  128. void ScriptSelection::shutDown()
  129. {
  130. OnSelectionChangedConn.disconnect();
  131. OnPingResourceConn.disconnect();
  132. OnPingSceneObjectConn.disconnect();
  133. }
  134. void ScriptSelection::onSelectionChanged(const Vector<HSceneObject>& sceneObjects, const Vector<Path>& resPaths)
  135. {
  136. UINT32 numObjects = (UINT32)sceneObjects.size();
  137. ScriptArray scriptObjects = ScriptArray::create<ScriptSceneObject>(numObjects);
  138. for (UINT32 i = 0; i < numObjects; i++)
  139. {
  140. ScriptSceneObject* scriptSceneObject = ScriptGameObjectManager::instance().getOrCreateScriptSceneObject(sceneObjects[i]);
  141. scriptObjects.set(i, scriptSceneObject->getManagedInstance());
  142. }
  143. UINT32 numPaths = (UINT32)resPaths.size();
  144. ScriptArray scriptPaths = ScriptArray::create<String>(numPaths);
  145. for (UINT32 i = 0; i < numPaths; i++)
  146. scriptPaths.set(i, resPaths[i].toString());
  147. MonoArray* monoObjects = scriptObjects.getInternal();
  148. MonoArray* monoPaths = scriptPaths.getInternal();
  149. MonoUtil::invokeThunk(OnSelectionChangedThunk, monoObjects, monoPaths);
  150. }
  151. void ScriptSelection::onResourcePing(const Path& resPath)
  152. {
  153. MonoString* monoResPath = MonoUtil::stringToMono(resPath.toString());
  154. MonoUtil::invokeThunk(OnPingResourceThunk, monoResPath);
  155. }
  156. void ScriptSelection::onSceneObjectPing(const HSceneObject& sceneObject)
  157. {
  158. ScriptSceneObject* scriptSceneObject = ScriptGameObjectManager::instance().getOrCreateScriptSceneObject(sceneObject);
  159. MonoUtil::invokeThunk(OnPingSceneObjectThunk, scriptSceneObject->getManagedInstance());
  160. }
  161. }