BsGUISceneTreeView.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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)
  47. {
  48. HSceneObject currentSO = element->mSceneObject;
  49. // Check if SceneObject has changed in any way and update the tree element
  50. bool completeMatch = (UINT32)element->mChildren.size() == currentSO->getNumChildren();
  51. // Early exit case - Most commonly there will be no changes between active and cached data so
  52. // we first do a quick check in order to avoid expensive comparison later
  53. if(completeMatch)
  54. {
  55. for(UINT32 i = 0; i < currentSO->getNumChildren(); i++)
  56. {
  57. SceneTreeElement* currentChild = static_cast<SceneTreeElement*>(element->mChildren[i]);
  58. UINT32 curId = currentSO->getChild(i)->getId();
  59. if(curId != currentChild->mId)
  60. {
  61. completeMatch = false;
  62. break;
  63. }
  64. }
  65. }
  66. // Not a complete match, compare everything and insert/delete elements as needed
  67. bool needsUpdate = false;
  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. SceneTreeElement* currentChild = static_cast<SceneTreeElement*>(element->mChildren[j]);
  82. if(curId == currentChild->mId)
  83. {
  84. tempToDelete[j] = false;
  85. currentChild->mSortedIdx = (UINT32)newChildren.size();
  86. newChildren.push_back(currentChild);
  87. found = true;
  88. break;
  89. }
  90. }
  91. if(!found)
  92. {
  93. SceneTreeElement* newChild = cm_new<SceneTreeElement>();
  94. newChild->mParent = element;
  95. newChild->mSceneObject = currentSOChild;
  96. newChild->mId = currentSOChild->getId();
  97. newChild->mName = currentSOChild->getName();
  98. newChild->mSortedIdx = (UINT32)newChildren.size();
  99. newChild->mIsVisible = element->mIsVisible && element->mIsExpanded;
  100. newChildren.push_back(newChild);
  101. updateElementGUI(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. needsUpdate = 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. needsUpdate = true;
  120. }
  121. if(needsUpdate)
  122. updateElementGUI(element);
  123. // Calculate the sorted index of the element based on its name
  124. TreeElement* parent = element->mParent;
  125. if(parent != nullptr)
  126. {
  127. for(UINT32 i = 0; i < (UINT32)parent->mChildren.size(); i++)
  128. {
  129. INT32 stringCompare = element->mName.compare(parent->mChildren[i]->mName);
  130. if(stringCompare > 0)
  131. {
  132. if(element->mSortedIdx < parent->mChildren[i]->mSortedIdx)
  133. std::swap(element->mSortedIdx, parent->mChildren[i]->mSortedIdx);
  134. }
  135. else if(stringCompare < 0)
  136. {
  137. if(element->mSortedIdx > parent->mChildren[i]->mSortedIdx)
  138. std::swap(element->mSortedIdx, parent->mChildren[i]->mSortedIdx);
  139. }
  140. }
  141. }
  142. for(UINT32 i = 0; i < (UINT32)element->mChildren.size(); i++)
  143. {
  144. SceneTreeElement* sceneElement = static_cast<SceneTreeElement*>(element->mChildren[i]);
  145. updateTreeElement(sceneElement);
  146. }
  147. }
  148. void GUISceneTreeView::updateTreeElementHierarchy()
  149. {
  150. HSceneObject root = CM::gSceneManager().getRootNode();
  151. mRootElement.mSceneObject = root;
  152. mRootElement.mId = root->getId();
  153. mRootElement.mSortedIdx = 0;
  154. mRootElement.mIsExpanded = true;
  155. updateTreeElement(&mRootElement);
  156. }
  157. void GUISceneTreeView::renameTreeElement(GUITreeView::TreeElement* element, const CM::WString& name)
  158. {
  159. SceneTreeElement* sceneTreeElement = static_cast<SceneTreeElement*>(element);
  160. CmdEditPlainFieldGO<String>::execute(sceneTreeElement->mSceneObject, "mName", toString(name));
  161. }
  162. void GUISceneTreeView::deleteTreeElement(GUITreeView::TreeElement* element)
  163. {
  164. closeTemporarilyExpandedElements(); // In case this element is one of them
  165. if(element->mIsSelected)
  166. unselectElement(element);
  167. cm_delete(element);
  168. }
  169. bool GUISceneTreeView::acceptDragAndDrop() const
  170. {
  171. return DragAndDropManager::instance().isDragInProgress() && DragAndDropManager::instance().getDragTypeId() == (UINT32)DragAndDropType::SceneObject;
  172. }
  173. void GUISceneTreeView::dragAndDropStart()
  174. {
  175. DraggedSceneObjects* draggedSceneObjects = cm_new<DraggedSceneObjects>((UINT32)mSelectedElements.size());
  176. UINT32 cnt = 0;
  177. for(auto& selectedElement : mSelectedElements)
  178. {
  179. SceneTreeElement* sceneTreeElement = static_cast<SceneTreeElement*>(selectedElement.element);
  180. draggedSceneObjects->objects[cnt] = sceneTreeElement->mSceneObject;
  181. cnt++;
  182. }
  183. DragAndDropManager::instance().startDrag(HTexture(), (UINT32)DragAndDropType::SceneObject, (void*)draggedSceneObjects,
  184. boost::bind(&GUISceneTreeView::dragAndDropFinalize, this));
  185. }
  186. void GUISceneTreeView::dragAndDropEnded(TreeElement* overTreeElement)
  187. {
  188. if(overTreeElement != nullptr)
  189. {
  190. DraggedSceneObjects* draggedSceneObjects = reinterpret_cast<DraggedSceneObjects*>(DragAndDropManager::instance().getDragData());
  191. Vector<HSceneObject>::type sceneObjects;
  192. SceneTreeElement* sceneTreeElement = static_cast<SceneTreeElement*>(overTreeElement);
  193. HSceneObject newParent = sceneTreeElement->mSceneObject;
  194. for(UINT32 i = 0; i < draggedSceneObjects->numObjects; i++)
  195. {
  196. if(draggedSceneObjects->objects[i] != newParent)
  197. sceneObjects.push_back(draggedSceneObjects->objects[i]);
  198. }
  199. CmdReparentSO::execute(sceneObjects, newParent);
  200. }
  201. }
  202. void GUISceneTreeView::dragAndDropFinalize()
  203. {
  204. mDragInProgress = false;
  205. markContentAsDirty();
  206. DraggedSceneObjects* draggedSceneObjects = reinterpret_cast<DraggedSceneObjects*>(DragAndDropManager::instance().getDragData());
  207. cm_delete(draggedSceneObjects);
  208. }
  209. const String& GUISceneTreeView::getGUITypeName()
  210. {
  211. static String typeName = "SceneTreeView";
  212. return typeName;
  213. }
  214. }