BsGUISceneTreeView.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. #include "BsGUISceneTreeView.h"
  2. #include "BsGUIArea.h"
  3. #include "BsGUILayout.h"
  4. #include "BsGUITexture.h"
  5. #include "BsGUIButton.h"
  6. #include "BsGUILabel.h"
  7. #include "BsGUISpace.h"
  8. #include "BsGUIWidget.h"
  9. #include "BsGUIMouseEvent.h"
  10. #include "BsGUISkin.h"
  11. #include "CmSceneObject.h"
  12. #include "CmSceneManager.h"
  13. using namespace CamelotFramework;
  14. using namespace BansheeEngine;
  15. namespace BansheeEditor
  16. {
  17. const UINT32 GUISceneTreeView::ELEMENT_EXTRA_SPACING = 3;
  18. const UINT32 GUISceneTreeView::INDENT_SIZE = 10;
  19. GUISceneTreeView::TreeElement::TreeElement()
  20. :mParent(nullptr), mFoldoutBtn(nullptr), mElement(nullptr),
  21. mId(0), mIsExpanded(false), mSortedIdx(0), mIsDirty(false), mIsVisible(true)
  22. { }
  23. GUISceneTreeView::TreeElement::~TreeElement()
  24. {
  25. for(auto& child : mChildren)
  26. cm_delete(child);
  27. if(mFoldoutBtn != nullptr)
  28. GUIElement::destroy(mFoldoutBtn);
  29. if(mElement != nullptr)
  30. GUIElement::destroy(mElement);
  31. mChildren.clear();
  32. }
  33. GUISceneTreeView::GUISceneTreeView(GUIWidget& parent, GUIElementStyle* backgroundStyle, GUIElementStyle* elementBtnStyle,
  34. GUIElementStyle* foldoutBtnStyle, const BS::GUILayoutOptions& layoutOptions)
  35. :GUIElementContainer(parent, layoutOptions), mBackgroundStyle(backgroundStyle),
  36. mElementBtnStyle(elementBtnStyle), mFoldoutBtnStyle(foldoutBtnStyle)
  37. {
  38. if(mBackgroundStyle == nullptr)
  39. mBackgroundStyle = parent.getSkin().getStyle("TreeViewBackground");
  40. if(mElementBtnStyle == nullptr)
  41. mElementBtnStyle = parent.getSkin().getStyle("TreeViewElementBtn");
  42. if(mFoldoutBtnStyle == nullptr)
  43. mFoldoutBtnStyle = parent.getSkin().getStyle("TreeViewFoldoutBtn");
  44. mBackgroundImage = GUITexture::create(parent, mBackgroundStyle);
  45. _registerChildElement(mBackgroundImage);
  46. }
  47. GUISceneTreeView::~GUISceneTreeView()
  48. {
  49. }
  50. GUISceneTreeView* GUISceneTreeView::create(GUIWidget& parent, GUIElementStyle* backgroundStyle, GUIElementStyle* elementBtnStyle,
  51. GUIElementStyle* foldoutBtnStyle)
  52. {
  53. return new (cm_alloc<GUISceneTreeView, PoolAlloc>()) GUISceneTreeView(parent, backgroundStyle, elementBtnStyle, foldoutBtnStyle,
  54. GUILayoutOptions::create(&GUISkin::DefaultStyle));
  55. }
  56. GUISceneTreeView* GUISceneTreeView::create(GUIWidget& parent, const GUILayoutOptions& layoutOptions)
  57. {
  58. return new (cm_alloc<GUISceneTreeView, PoolAlloc>()) GUISceneTreeView(parent, nullptr, nullptr, nullptr, layoutOptions);
  59. }
  60. GUISceneTreeView* GUISceneTreeView::create(GUIWidget& parent, const GUILayoutOptions& layoutOptions, GUIElementStyle* backgroundStyle,
  61. GUIElementStyle* elementBtnStyle, GUIElementStyle* foldoutBtnStyle)
  62. {
  63. return new (cm_alloc<GUISceneTreeView, PoolAlloc>()) GUISceneTreeView(parent, backgroundStyle, elementBtnStyle,
  64. foldoutBtnStyle, layoutOptions);
  65. }
  66. void GUISceneTreeView::update()
  67. {
  68. // NOTE - Instead of iterating through every visible element and comparing it with internal values,
  69. // I might just want to add callbacks to SceneManager that notify me of any changes and then only perform
  70. // update if anything is actually dirty
  71. struct UpdateTreeElement
  72. {
  73. UpdateTreeElement(TreeElement* element, UINT32 seqIdx, bool visible)
  74. :element(element), seqIdx(seqIdx), visible(visible)
  75. { }
  76. TreeElement* element;
  77. UINT32 seqIdx;
  78. bool visible;
  79. };
  80. HSceneObject root = CM::gSceneManager().getRootNode();
  81. mRootElement.mSceneObject = root;
  82. mRootElement.mId = root->getId();
  83. mRootElement.mSortedIdx = 0;
  84. Stack<UpdateTreeElement>::type todo;
  85. todo.push(UpdateTreeElement(&mRootElement, 0, true));
  86. while(!todo.empty())
  87. {
  88. UpdateTreeElement updateElement = todo.top();
  89. TreeElement* current = updateElement.element;
  90. HSceneObject currentSO = current->mSceneObject;
  91. todo.pop();
  92. // Check if SceneObject has changed in any way and update the tree element
  93. if(updateElement.visible)
  94. {
  95. bool completeMatch = (UINT32)current->mChildren.size() == currentSO->getNumChildren();
  96. // Early exit case - Most commonly there will be no changes between active and cached data so
  97. // we first do a quick check in order to avoid expensive comparison later
  98. if(completeMatch)
  99. {
  100. for(UINT32 i = 0; i < currentSO->getNumChildren(); i++)
  101. {
  102. UINT32 curId = currentSO->getChild(i)->getId();
  103. if(curId != current->mChildren[i]->mId)
  104. {
  105. completeMatch = false;
  106. break;
  107. }
  108. }
  109. }
  110. // Not a complete match, compare everything and insert/delete elements as needed
  111. if(!completeMatch)
  112. {
  113. Vector<TreeElement*>::type newChildren;
  114. mTempToDelete.resize(current->mChildren.size(), true);
  115. for(UINT32 i = 0; i < currentSO->getNumChildren(); i++)
  116. {
  117. UINT32 curId = currentSO->getChild(i)->getId();
  118. bool found = false;
  119. for(UINT32 j = 0; j < current->mChildren.size(); j++)
  120. {
  121. TreeElement* currentChild = current->mChildren[i];
  122. if(curId == currentChild->mId)
  123. {
  124. mTempToDelete[j] = false;
  125. currentChild->mIsDirty = true;
  126. currentChild->mSortedIdx = (UINT32)newChildren.size();
  127. newChildren.push_back(currentChild);
  128. found = true;
  129. break;
  130. }
  131. }
  132. if(!found)
  133. {
  134. TreeElement* newChild = cm_new<TreeElement>();
  135. newChild->mParent = current;
  136. newChild->mSceneObject = currentSO;
  137. newChild->mId = currentSO->getId();
  138. newChild->mName = currentSO->getName();
  139. newChild->mSortedIdx = (UINT32)newChildren.size();
  140. newChild->mIsDirty = true;
  141. newChildren.push_back(newChild);
  142. }
  143. }
  144. for(UINT32 i = 0; i < current->mChildren.size(); i++)
  145. {
  146. if(!mTempToDelete[i])
  147. continue;
  148. cm_delete(current->mChildren[i]);
  149. }
  150. current->mChildren = newChildren;
  151. current->mIsDirty = true;
  152. }
  153. // Check if name needs updating
  154. const String& name = current->mSceneObject->getName();
  155. if(current->mName != name)
  156. {
  157. current->mName = name;
  158. current->mIsDirty = true;
  159. }
  160. // Calculate the sorted index of the element based on its name
  161. TreeElement* parent = current->mParent;
  162. if(current->mIsDirty && parent != nullptr)
  163. {
  164. for(UINT32 i = 0; i < (UINT32)parent->mChildren.size(); i++)
  165. {
  166. if(current->mSortedIdx <= parent->mChildren[i]->mSortedIdx)
  167. continue;
  168. UINT32 stringCompare = current->mName.compare(parent->mChildren[i]->mName);
  169. if(stringCompare > 0)
  170. {
  171. std::swap(current->mSortedIdx, parent->mChildren[i]->mSortedIdx);
  172. }
  173. }
  174. }
  175. }
  176. bool visibilityChanged = false;
  177. if(current->mIsVisible != updateElement.visible)
  178. {
  179. visibilityChanged = true;
  180. current->mIsVisible = updateElement.visible;
  181. current->mIsDirty = true;
  182. }
  183. if(current->mIsDirty)
  184. {
  185. if(updateElement.visible)
  186. {
  187. // TODO - If no label exists create it
  188. // TODO - If has children and no expand button exists create it
  189. // TODO - If it has no children but an expand button exists remove it
  190. }
  191. else
  192. {
  193. // TODO - If either label or expand button exist remove them
  194. }
  195. markContentAsDirty();
  196. current->mIsDirty = false;
  197. }
  198. // Queue children for next iteration
  199. if(visibilityChanged || current->mIsVisible)
  200. {
  201. for(UINT32 i = 0; i < (UINT32)current->mChildren.size(); i++)
  202. {
  203. todo.push(UpdateTreeElement(current->mChildren[i], i, current->mIsVisible && current->mIsExpanded));
  204. }
  205. }
  206. }
  207. }
  208. bool GUISceneTreeView::mouseEvent(const GUIMouseEvent& event)
  209. {
  210. return false;
  211. }
  212. void GUISceneTreeView::updateClippedBounds()
  213. {
  214. Vector2I offset = _getOffset();
  215. mClippedBounds = RectI(offset.x, offset.y, _getWidth(), _getHeight());
  216. }
  217. void GUISceneTreeView::_updateLayoutInternal(INT32 x, INT32 y, UINT32 width, UINT32 height,
  218. RectI clipRect, UINT8 widgetDepth, UINT16 areaDepth)
  219. {
  220. struct UpdateTreeElement
  221. {
  222. UpdateTreeElement(TreeElement* element, UINT32 indent)
  223. :element(element), indent(indent)
  224. { }
  225. TreeElement* element;
  226. UINT32 indent;
  227. };
  228. Stack<UpdateTreeElement>::type todo;
  229. todo.push(UpdateTreeElement(&mRootElement, 0));
  230. // NOTE - Instead of iterating through all elements, try to find those within the clip rect
  231. // and only iterate through those. Others should somehow be marked in-active (similar to GUIElement::isDisabled()?)
  232. Vector2I offset(x, y);
  233. while(!todo.empty())
  234. {
  235. UpdateTreeElement currentUpdateElement = todo.top();
  236. TreeElement* current = currentUpdateElement.element;
  237. UINT32 indent = currentUpdateElement.indent;
  238. todo.pop();
  239. for(auto& child : current->mChildren)
  240. {
  241. if(!child->mIsVisible)
  242. continue;
  243. if(child->mElement != nullptr)
  244. {
  245. Vector2I elementSize = child->mElement->_getOptimalSize();
  246. offset.x = indent * INDENT_SIZE;
  247. child->mElement->_setOffset(offset);
  248. child->mElement->_setWidth(elementSize.x);
  249. child->mElement->_setHeight(elementSize.y);
  250. child->mElement->_setAreaDepth(areaDepth);
  251. child->mElement->_setWidgetDepth(widgetDepth);
  252. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  253. child->mElement->_setClipRect(elemClipRect);
  254. offset.y += elementSize.y + ELEMENT_EXTRA_SPACING;
  255. }
  256. // TODO - Position expand buttons
  257. todo.push(UpdateTreeElement(child, indent + 1));
  258. }
  259. }
  260. }
  261. const String& GUISceneTreeView::getGUITypeName()
  262. {
  263. static String typeName = "SceneTreeView";
  264. return typeName;
  265. }
  266. }