BsScriptGizmoManager.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #include "BsScriptGizmoManager.h"
  2. #include "BsRuntimeScriptObjects.h"
  3. #include "BsMonoAssembly.h"
  4. #include "BsMonoClass.h"
  5. #include "BsMonoMethod.h"
  6. #include "BsMonoField.h"
  7. #include "BsMonoManager.h"
  8. #include "BsSceneManager.h"
  9. #include "BsSceneObject.h"
  10. #include "BsComponent.h"
  11. #include "BsManagedComponent.h"
  12. #include "BsGizmoManager.h"
  13. #include "BsSelection.h"
  14. using namespace std::placeholders;
  15. namespace BansheeEngine
  16. {
  17. ScriptGizmoManager::ScriptGizmoManager(RuntimeScriptObjects& scriptObjectManager)
  18. :mScriptObjectManager(scriptObjectManager), mDrawGizmoAttribute(nullptr), mNextAssemblyId(0), mFlagsField(nullptr)
  19. {
  20. mAssemblyRefreshedConn = mScriptObjectManager.onAssemblyRefreshed.connect(std::bind(&ScriptGizmoManager::reloadAssemblyMethods, this, _1));
  21. Vector<String> initializedAssemblyNames = mScriptObjectManager.getInitializedAssemblies();
  22. for (auto& assemblyName : initializedAssemblyNames)
  23. {
  24. MonoAssembly* assembly = MonoManager::instance().getAssembly(assemblyName);
  25. if (assembly != nullptr)
  26. reloadAssemblyMethods(assembly);
  27. }
  28. }
  29. ScriptGizmoManager::~ScriptGizmoManager()
  30. {
  31. mAssemblyRefreshedConn.disconnect();
  32. }
  33. bool dummyIsSelected(const HSceneObject& so)
  34. {
  35. return false; // TODO - Replace with a call to Selection class once I have it
  36. }
  37. void ScriptGizmoManager::update()
  38. {
  39. GizmoManager::instance().clearGizmos();
  40. HSceneObject rootSO = SceneManager::instance().getRootNode();
  41. Stack<HSceneObject> todo;
  42. todo.push(rootSO);
  43. bool isParentSelected = false;
  44. UINT32 parentSelectedPopIdx = 0;
  45. Vector<HSceneObject> selectedObjects = Selection::instance().getSceneObjects();
  46. while (!todo.empty())
  47. {
  48. if (isParentSelected && parentSelectedPopIdx == (UINT32)todo.size())
  49. {
  50. isParentSelected = false;
  51. }
  52. HSceneObject curSO = todo.top();
  53. todo.pop();
  54. bool isSelected = std::count(selectedObjects.begin(), selectedObjects.end(), curSO) > 0;
  55. if (isSelected && !isParentSelected)
  56. {
  57. isParentSelected = true;
  58. parentSelectedPopIdx = (UINT32)todo.size();
  59. }
  60. const Vector<HComponent>& components = curSO->getComponents();
  61. for (auto& component : components)
  62. {
  63. if (rtti_is_of_type<ManagedComponent>(component.get()))
  64. {
  65. ManagedComponent* managedComponent = static_cast<ManagedComponent*>(component.get());
  66. auto iterFind = mGizmoDrawers.find(managedComponent->getManagedFullTypeName());
  67. if (iterFind != mGizmoDrawers.end())
  68. {
  69. UINT32 flags = iterFind->second.flags;
  70. bool drawGizmo = false;
  71. if (((flags & (UINT32)DrawGizmoFlags::Selected) != 0) && isSelected)
  72. drawGizmo = true;
  73. if (((flags & (UINT32)DrawGizmoFlags::ParentSelected) != 0) && isParentSelected)
  74. drawGizmo = true;
  75. if (((flags & (UINT32)DrawGizmoFlags::NotSelected) != 0) && !isSelected && !isParentSelected)
  76. drawGizmo = true;
  77. if (drawGizmo)
  78. {
  79. bool pickable = (flags & (UINT32)DrawGizmoFlags::Pickable) != 0;
  80. GizmoManager::instance().startGizmo(curSO);
  81. GizmoManager::instance().setPickable(pickable);
  82. void* params[1] = { managedComponent->getManagedInstance() };
  83. iterFind->second.drawGizmosMethod->invoke(nullptr, params);
  84. GizmoManager::instance().endGizmo();
  85. }
  86. }
  87. }
  88. }
  89. for (UINT32 i = 0; i < curSO->getNumChildren(); i++)
  90. todo.push(curSO->getChild(i));
  91. }
  92. }
  93. void ScriptGizmoManager::reloadAssemblyMethods(MonoAssembly* assembly)
  94. {
  95. String assemblyName = assembly->getName();
  96. // If editor assembly, reload DrawGizmo attribute
  97. if (assemblyName == BansheeEditorAssemblyName)
  98. {
  99. mDrawGizmoAttribute = assembly->getClass("BansheeEditor", "DrawGizmo");
  100. if (mDrawGizmoAttribute == nullptr)
  101. BS_EXCEPT(InvalidStateException, "Cannot find DrawGizmo managed class.");
  102. mFlagsField = mDrawGizmoAttribute->getField("flags");
  103. // Load delayed assemblies first
  104. for (auto iter = mDelayedLoad.rbegin(); iter != mDelayedLoad.rend(); ++iter)
  105. reloadAssemblyMethods(*iter);
  106. mDelayedLoad.clear();
  107. }
  108. else
  109. {
  110. // If we haven't loaded editor assembly yet, wait until we do before continuing
  111. if (mDrawGizmoAttribute == nullptr)
  112. {
  113. mDelayedLoad.push_back(assembly);
  114. return;
  115. }
  116. }
  117. // If we had this assembly previously loaded, clear all of its methods
  118. UINT32 assemblyId = 0;
  119. auto iterFind = mAssemblyNameToId.find(assemblyName);
  120. if (iterFind != mAssemblyNameToId.end())
  121. {
  122. assemblyId = iterFind->second;
  123. Map<String, GizmoData> newGizmoDrawers;
  124. for (auto& gizmoMethod : mGizmoDrawers)
  125. {
  126. if (gizmoMethod.second.assemblyId != assemblyId)
  127. newGizmoDrawers[gizmoMethod.first] = gizmoMethod.second;
  128. }
  129. mGizmoDrawers.swap(newGizmoDrawers);
  130. }
  131. else
  132. {
  133. assemblyId = mNextAssemblyId++;
  134. mAssemblyNameToId[assemblyName] = assemblyId;
  135. }
  136. // Find new gizmo drawer methods
  137. const Vector<MonoClass*>& allClasses = assembly->getAllClasses();
  138. for (auto curClass : allClasses)
  139. {
  140. const Vector<MonoMethod*>& methods = curClass->getAllMethods();
  141. for (auto& curMethod : methods)
  142. {
  143. UINT32 drawGizmoFlags = 0;
  144. MonoClass* componentType = nullptr;
  145. if (isValidDrawGizmoMethod(curMethod, componentType, drawGizmoFlags))
  146. {
  147. String fullComponentName = componentType->getFullName();
  148. GizmoData& newGizmoData = mGizmoDrawers[fullComponentName];
  149. newGizmoData.assemblyId = assemblyId;
  150. newGizmoData.componentType = componentType;
  151. newGizmoData.drawGizmosMethod = curMethod;
  152. newGizmoData.flags = drawGizmoFlags;
  153. }
  154. }
  155. }
  156. }
  157. bool ScriptGizmoManager::isValidDrawGizmoMethod(MonoMethod* method, MonoClass*& componentType, UINT32& drawGizmoFlags)
  158. {
  159. componentType = nullptr;
  160. drawGizmoFlags = 0;
  161. if (!method->hasAttribute(mDrawGizmoAttribute))
  162. return false;
  163. if (method->getNumParameters() != 1)
  164. return false;
  165. if (!method->isStatic())
  166. return false;
  167. MonoClass* paramType = method->getParameterType(0);
  168. MonoClass* componentClass = mScriptObjectManager.getComponentClass();
  169. if (!paramType->isSubClassOf(componentClass))
  170. return false;
  171. componentType = paramType;
  172. MonoObject* drawGizmoAttrib = method->getAttribute(mDrawGizmoAttribute);
  173. mFlagsField->getValue(drawGizmoAttrib, &drawGizmoFlags);
  174. return true;
  175. }
  176. }