BsGUISceneTreeView.cpp 12 KB

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