BsGUISceneTreeView.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. SceneTreeElement* currentChild = static_cast<SceneTreeElement*>(element->mChildren[i]);
  60. UINT32 curId = currentSO->getChild(i)->getId();
  61. if(curId != currentChild->mId)
  62. {
  63. completeMatch = false;
  64. break;
  65. }
  66. }
  67. }
  68. // Not a complete match, compare everything and insert/delete elements as needed
  69. if(!completeMatch)
  70. {
  71. Vector<TreeElement*>::type newChildren;
  72. bool* tempToDelete = (bool*)stackAlloc(sizeof(bool) * (UINT32)element->mChildren.size());
  73. for(UINT32 i = 0; i < (UINT32)element->mChildren.size(); i++)
  74. tempToDelete[i] = true;
  75. for(UINT32 i = 0; i < currentSO->getNumChildren(); i++)
  76. {
  77. HSceneObject currentSOChild = currentSO->getChild(i);
  78. UINT32 curId = currentSOChild->getId();
  79. bool found = false;
  80. for(UINT32 j = 0; j < element->mChildren.size(); j++)
  81. {
  82. SceneTreeElement* currentChild = static_cast<SceneTreeElement*>(element->mChildren[j]);
  83. if(curId == currentChild->mId)
  84. {
  85. tempToDelete[j] = false;
  86. currentChild->mIsDirty = true;
  87. currentChild->mSortedIdx = (UINT32)newChildren.size();
  88. newChildren.push_back(currentChild);
  89. found = true;
  90. break;
  91. }
  92. }
  93. if(!found)
  94. {
  95. SceneTreeElement* newChild = cm_new<SceneTreeElement>();
  96. newChild->mParent = element;
  97. newChild->mSceneObject = currentSOChild;
  98. newChild->mId = currentSOChild->getId();
  99. newChild->mName = currentSOChild->getName();
  100. newChild->mSortedIdx = (UINT32)newChildren.size();
  101. newChild->mIsDirty = true;
  102. newChildren.push_back(newChild);
  103. }
  104. }
  105. for(UINT32 i = 0; i < element->mChildren.size(); i++)
  106. {
  107. if(!tempToDelete[i])
  108. continue;
  109. deleteTreeElement(element->mChildren[i]);
  110. }
  111. stackDeallocLast(tempToDelete);
  112. element->mChildren = newChildren;
  113. element->mIsDirty = true;
  114. }
  115. // Check if name needs updating
  116. const String& name = element->mSceneObject->getName();
  117. if(element->mName != name)
  118. {
  119. element->mName = name;
  120. element->mIsDirty = true;
  121. }
  122. // Calculate the sorted index of the element based on its name
  123. TreeElement* parent = element->mParent;
  124. if(element->mIsDirty && parent != nullptr)
  125. {
  126. for(UINT32 i = 0; i < (UINT32)parent->mChildren.size(); i++)
  127. {
  128. INT32 stringCompare = element->mName.compare(parent->mChildren[i]->mName);
  129. if(stringCompare > 0)
  130. {
  131. if(element->mSortedIdx < parent->mChildren[i]->mSortedIdx)
  132. std::swap(element->mSortedIdx, parent->mChildren[i]->mSortedIdx);
  133. }
  134. else if(stringCompare < 0)
  135. {
  136. if(element->mSortedIdx > parent->mChildren[i]->mSortedIdx)
  137. std::swap(element->mSortedIdx, parent->mChildren[i]->mSortedIdx);
  138. }
  139. }
  140. }
  141. }
  142. bool visibilityChanged = false;
  143. if(element->mIsVisible != visible)
  144. {
  145. visibilityChanged = true;
  146. element->mIsVisible = visible;
  147. element->mIsDirty = true;
  148. }
  149. if(visibilityChanged || element->mIsVisible)
  150. {
  151. for(UINT32 i = 0; i < (UINT32)element->mChildren.size(); i++)
  152. {
  153. SceneTreeElement* sceneElement = static_cast<SceneTreeElement*>(element->mChildren[i]);
  154. updateTreeElement(sceneElement, element->mIsVisible && element->mIsExpanded);
  155. }
  156. }
  157. }
  158. void GUISceneTreeView::updateTreeElementHierarchy()
  159. {
  160. HSceneObject root = CM::gSceneManager().getRootNode();
  161. mRootElement.mSceneObject = root;
  162. mRootElement.mId = root->getId();
  163. mRootElement.mSortedIdx = 0;
  164. mRootElement.mIsExpanded = true;
  165. updateTreeElement(&mRootElement, true);
  166. }
  167. void GUISceneTreeView::renameTreeElement(GUITreeView::TreeElement* element, const CM::WString& name)
  168. {
  169. SceneTreeElement* sceneTreeElement = static_cast<SceneTreeElement*>(element);
  170. CmdEditPlainFieldGO<String>::execute(sceneTreeElement->mSceneObject, "mName", toString(name));
  171. }
  172. void GUISceneTreeView::deleteTreeElement(GUITreeView::TreeElement* element)
  173. {
  174. closeTemporarilyExpandedElements(); // In case this element is one of them
  175. if(element->mIsSelected)
  176. unselectElement(element);
  177. cm_delete(element);
  178. }
  179. bool GUISceneTreeView::acceptDragAndDrop() const
  180. {
  181. return DragAndDropManager::instance().isDragInProgress() && DragAndDropManager::instance().getDragTypeId() == (UINT32)DragAndDropType::SceneObject;
  182. }
  183. void GUISceneTreeView::dragAndDropStart()
  184. {
  185. DraggedSceneObjects* draggedSceneObjects = cm_new<DraggedSceneObjects>((UINT32)mSelectedElements.size());
  186. UINT32 cnt = 0;
  187. for(auto& selectedElement : mSelectedElements)
  188. {
  189. SceneTreeElement* sceneTreeElement = static_cast<SceneTreeElement*>(selectedElement.element);
  190. draggedSceneObjects->objects[cnt] = sceneTreeElement->mSceneObject;
  191. cnt++;
  192. }
  193. DragAndDropManager::instance().startDrag(HTexture(), (UINT32)DragAndDropType::SceneObject, (void*)draggedSceneObjects,
  194. boost::bind(&GUISceneTreeView::dragAndDropFinalize, this));
  195. }
  196. void GUISceneTreeView::dragAndDropEnded(TreeElement* overTreeElement)
  197. {
  198. if(overTreeElement != nullptr)
  199. {
  200. DraggedSceneObjects* draggedSceneObjects = reinterpret_cast<DraggedSceneObjects*>(DragAndDropManager::instance().getDragData());
  201. Vector<HSceneObject>::type sceneObjects;
  202. SceneTreeElement* sceneTreeElement = static_cast<SceneTreeElement*>(overTreeElement);
  203. HSceneObject newParent = sceneTreeElement->mSceneObject;
  204. for(UINT32 i = 0; i < draggedSceneObjects->numObjects; i++)
  205. {
  206. if(draggedSceneObjects->objects[i] != newParent)
  207. sceneObjects.push_back(draggedSceneObjects->objects[i]);
  208. }
  209. CmdReparentSO::execute(sceneObjects, newParent);
  210. }
  211. }
  212. void GUISceneTreeView::dragAndDropFinalize()
  213. {
  214. mDragInProgress = false;
  215. markContentAsDirty();
  216. DraggedSceneObjects* draggedSceneObjects = reinterpret_cast<DraggedSceneObjects*>(DragAndDropManager::instance().getDragData());
  217. cm_delete(draggedSceneObjects);
  218. }
  219. const String& GUISceneTreeView::getGUITypeName()
  220. {
  221. static String typeName = "SceneTreeView";
  222. return typeName;
  223. }
  224. }