BsGUISceneTreeView.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #include "BsGUISceneTreeView.h"
  2. #include "CmSceneObject.h"
  3. #include "CmSceneManager.h"
  4. #include "BsGUISkin.h"
  5. #include "BsCmdEditPlainFieldGO.h"
  6. #include "BsDragAndDropManager.h"
  7. #include "BsCmdReparentSO.h"
  8. using namespace CamelotFramework;
  9. using namespace BansheeEngine;
  10. namespace BansheeEditor
  11. {
  12. GUISceneTreeView::DraggedSceneObjects::DraggedSceneObjects(UINT32 numObjects)
  13. :numObjects(numObjects)
  14. {
  15. objects = cm_newN<HSceneObject>(numObjects);
  16. }
  17. GUISceneTreeView::DraggedSceneObjects::~DraggedSceneObjects()
  18. {
  19. cm_deleteN(objects, numObjects);
  20. objects = nullptr;
  21. }
  22. GUISceneTreeView::GUISceneTreeView(GUIWidget& parent, GUIElementStyle* backgroundStyle, GUIElementStyle* elementBtnStyle,
  23. GUIElementStyle* foldoutBtnStyle, GUIElementStyle* selectionBackgroundStyle, GUIElementStyle* editBoxStyle,
  24. BS::GUIElementStyle* dragHighlightStyle, BS::GUIElementStyle* dragSepHighlightStyle, const GUILayoutOptions& layoutOptions)
  25. :GUITreeView(parent, backgroundStyle, elementBtnStyle, foldoutBtnStyle, selectionBackgroundStyle, editBoxStyle, dragHighlightStyle,
  26. dragSepHighlightStyle, layoutOptions)
  27. {
  28. }
  29. GUISceneTreeView::~GUISceneTreeView()
  30. {
  31. }
  32. GUISceneTreeView* GUISceneTreeView::create(GUIWidget& parent, GUIElementStyle* backgroundStyle, GUIElementStyle* elementBtnStyle,
  33. GUIElementStyle* foldoutBtnStyle, GUIElementStyle* selectionBackgroundStyle, GUIElementStyle* editBoxStyle, GUIElementStyle* dragHighlightStyle,
  34. GUIElementStyle* dragSepHighlightStyle)
  35. {
  36. return new (cm_alloc<GUISceneTreeView, PoolAlloc>()) GUISceneTreeView(parent, backgroundStyle, elementBtnStyle, foldoutBtnStyle,
  37. selectionBackgroundStyle, editBoxStyle, dragHighlightStyle, dragSepHighlightStyle, GUILayoutOptions::create(&GUISkin::DefaultStyle));
  38. }
  39. GUISceneTreeView* GUISceneTreeView::create(GUIWidget& parent, const GUIOptions& options, GUIElementStyle* backgroundStyle,
  40. GUIElementStyle* elementBtnStyle, GUIElementStyle* foldoutBtnStyle, GUIElementStyle* selectionBackgroundStyle,
  41. GUIElementStyle* editBoxStyle, GUIElementStyle* dragHighlightStyle, GUIElementStyle* dragSepHighlightStyle)
  42. {
  43. return new (cm_alloc<GUISceneTreeView, PoolAlloc>()) GUISceneTreeView(parent, backgroundStyle, elementBtnStyle,
  44. foldoutBtnStyle, selectionBackgroundStyle, editBoxStyle, dragHighlightStyle, dragSepHighlightStyle, GUILayoutOptions::create(options, &GUISkin::DefaultStyle));
  45. }
  46. void GUISceneTreeView::updateTreeElement(SceneTreeElement* element, bool visible)
  47. {
  48. HSceneObject currentSO = element->mSceneObject;
  49. // Check if SceneObject has changed in any way and update the tree element
  50. if(visible)
  51. {
  52. bool completeMatch = (UINT32)element->mChildren.size() == currentSO->getNumChildren();
  53. // Early exit case - Most commonly there will be no changes between active and cached data so
  54. // we first do a quick check in order to avoid expensive comparison later
  55. if(completeMatch)
  56. {
  57. for(UINT32 i = 0; i < currentSO->getNumChildren(); i++)
  58. {
  59. UINT32 curId = currentSO->getChild(i)->getId();
  60. if(curId != element->mChildren[i]->mId)
  61. {
  62. completeMatch = false;
  63. break;
  64. }
  65. }
  66. }
  67. // Not a complete match, compare everything and insert/delete elements as needed
  68. if(!completeMatch)
  69. {
  70. Vector<TreeElement*>::type newChildren;
  71. bool* tempToDelete = (bool*)stackAlloc(sizeof(bool) * (UINT32)element->mChildren.size());
  72. for(UINT32 i = 0; i < (UINT32)element->mChildren.size(); i++)
  73. tempToDelete[i] = true;
  74. for(UINT32 i = 0; i < currentSO->getNumChildren(); i++)
  75. {
  76. HSceneObject currentSOChild = currentSO->getChild(i);
  77. UINT32 curId = currentSOChild->getId();
  78. bool found = false;
  79. for(UINT32 j = 0; j < element->mChildren.size(); j++)
  80. {
  81. TreeElement* currentChild = element->mChildren[j];
  82. if(curId == currentChild->mId)
  83. {
  84. tempToDelete[j] = false;
  85. currentChild->mIsDirty = true;
  86. currentChild->mSortedIdx = (UINT32)newChildren.size();
  87. newChildren.push_back(currentChild);
  88. found = true;
  89. break;
  90. }
  91. }
  92. if(!found)
  93. {
  94. SceneTreeElement* newChild = cm_new<SceneTreeElement>();
  95. newChild->mParent = element;
  96. newChild->mSceneObject = currentSOChild;
  97. newChild->mId = currentSOChild->getId();
  98. newChild->mName = currentSOChild->getName();
  99. newChild->mSortedIdx = (UINT32)newChildren.size();
  100. newChild->mIsDirty = true;
  101. newChildren.push_back(newChild);
  102. }
  103. }
  104. for(UINT32 i = 0; i < element->mChildren.size(); i++)
  105. {
  106. if(!tempToDelete[i])
  107. continue;
  108. deleteTreeElement(element->mChildren[i]);
  109. }
  110. stackDeallocLast(tempToDelete);
  111. element->mChildren = newChildren;
  112. element->mIsDirty = true;
  113. }
  114. // Check if name needs updating
  115. const String& name = element->mSceneObject->getName();
  116. if(element->mName != name)
  117. {
  118. element->mName = name;
  119. element->mIsDirty = true;
  120. }
  121. // Calculate the sorted index of the element based on its name
  122. TreeElement* parent = element->mParent;
  123. if(element->mIsDirty && parent != nullptr)
  124. {
  125. for(UINT32 i = 0; i < (UINT32)parent->mChildren.size(); i++)
  126. {
  127. INT32 stringCompare = element->mName.compare(parent->mChildren[i]->mName);
  128. if(stringCompare > 0)
  129. {
  130. if(element->mSortedIdx < parent->mChildren[i]->mSortedIdx)
  131. std::swap(element->mSortedIdx, parent->mChildren[i]->mSortedIdx);
  132. }
  133. else if(stringCompare < 0)
  134. {
  135. if(element->mSortedIdx > parent->mChildren[i]->mSortedIdx)
  136. std::swap(element->mSortedIdx, parent->mChildren[i]->mSortedIdx);
  137. }
  138. }
  139. }
  140. }
  141. bool visibilityChanged = false;
  142. if(element->mIsVisible != visible)
  143. {
  144. visibilityChanged = true;
  145. element->mIsVisible = visible;
  146. element->mIsDirty = true;
  147. }
  148. if(visibilityChanged || element->mIsVisible)
  149. {
  150. for(UINT32 i = 0; i < (UINT32)element->mChildren.size(); i++)
  151. {
  152. SceneTreeElement* sceneElement = static_cast<SceneTreeElement*>(element->mChildren[i]);
  153. updateTreeElement(sceneElement, element->mIsVisible && element->mIsExpanded);
  154. }
  155. }
  156. }
  157. void GUISceneTreeView::updateTreeElementHierarchy()
  158. {
  159. HSceneObject root = CM::gSceneManager().getRootNode();
  160. mRootElement.mSceneObject = root;
  161. mRootElement.mId = root->getId();
  162. mRootElement.mSortedIdx = 0;
  163. mRootElement.mIsExpanded = true;
  164. updateTreeElement(&mRootElement, true);
  165. }
  166. void GUISceneTreeView::renameTreeElement(GUITreeView::TreeElement* element, const CM::WString& name)
  167. {
  168. SceneTreeElement* sceneTreeElement = static_cast<SceneTreeElement*>(element);
  169. CmdEditPlainFieldGO<String>::execute(sceneTreeElement->mSceneObject, "mName", toString(name));
  170. }
  171. bool GUISceneTreeView::acceptDragAndDrop() const
  172. {
  173. return DragAndDropManager::instance().isDragInProgress() && DragAndDropManager::instance().getDragTypeId() == (UINT32)DragAndDropType::SceneObject;
  174. }
  175. void GUISceneTreeView::dragAndDropStart()
  176. {
  177. DraggedSceneObjects* draggedSceneObjects = cm_new<DraggedSceneObjects>((UINT32)mSelectedElements.size());
  178. UINT32 cnt = 0;
  179. for(auto& selectedElement : mSelectedElements)
  180. {
  181. SceneTreeElement* sceneTreeElement = static_cast<SceneTreeElement*>(selectedElement.element);
  182. draggedSceneObjects->objects[cnt] = sceneTreeElement->mSceneObject;
  183. cnt++;
  184. }
  185. DragAndDropManager::instance().startDrag(HTexture(), (UINT32)DragAndDropType::SceneObject, (void*)draggedSceneObjects,
  186. boost::bind(&GUISceneTreeView::dragAndDropFinalize, this));
  187. }
  188. void GUISceneTreeView::dragAndDropEnded(TreeElement* overTreeElement)
  189. {
  190. if(overTreeElement != nullptr)
  191. {
  192. DraggedSceneObjects* draggedSceneObjects = reinterpret_cast<DraggedSceneObjects*>(DragAndDropManager::instance().getDragData());
  193. Vector<HSceneObject>::type sceneObjects;
  194. SceneTreeElement* sceneTreeElement = static_cast<SceneTreeElement*>(overTreeElement);
  195. HSceneObject newParent = sceneTreeElement->mSceneObject;
  196. for(UINT32 i = 0; i < draggedSceneObjects->numObjects; i++)
  197. {
  198. if(draggedSceneObjects->objects[i] != newParent)
  199. sceneObjects.push_back(draggedSceneObjects->objects[i]);
  200. }
  201. CmdReparentSO::execute(sceneObjects, newParent);
  202. }
  203. }
  204. void GUISceneTreeView::dragAndDropFinalize()
  205. {
  206. mDragInProgress = false;
  207. markContentAsDirty();
  208. DraggedSceneObjects* draggedSceneObjects = reinterpret_cast<DraggedSceneObjects*>(DragAndDropManager::instance().getDragData());
  209. cm_delete(draggedSceneObjects);
  210. }
  211. const String& GUISceneTreeView::getGUITypeName()
  212. {
  213. static String typeName = "SceneTreeView";
  214. return typeName;
  215. }
  216. }