BsScriptGizmoManager.cpp 5.8 KB

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