BsGUITreeView.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. #pragma once
  2. #include "BsEditorPrerequisites.h"
  3. #include "BsGUIElementContainer.h"
  4. #include "BsVirtualInput.h"
  5. #include "BsEvent.h"
  6. namespace BansheeEngine
  7. {
  8. /**
  9. * @brief GUI element that displays some contents in a tree-view where elements
  10. * are placed vertically above/beneath each other, and different elements
  11. * may be nested within other elements.
  12. *
  13. * Elements may be selected, renamed, dragged and re-parented.
  14. *
  15. * This class is abstract and meant to be extended by an implementation
  16. * specific to some content type (e.g. scene object hierarchy).
  17. */
  18. class BS_ED_EXPORT GUITreeView : public GUIElementContainer
  19. {
  20. protected:
  21. /**
  22. * @brief Types of possible states when the tree view is auto-scrolling.
  23. */
  24. enum class ScrollState
  25. {
  26. None,
  27. Up,
  28. Down,
  29. TransitioningUp,
  30. TransitioningDown
  31. };
  32. /**
  33. * @brief Contains data about a single piece of content
  34. * and all its children. This element may be visible and represented
  35. * by a GUI element, but might not (e.g. its parent is collapsed).
  36. */
  37. struct TreeElement
  38. {
  39. TreeElement();
  40. virtual ~TreeElement();
  41. TreeElement* mParent;
  42. Vector<TreeElement*> mChildren;
  43. GUIToggle* mFoldoutBtn;
  44. GUILabel* mElement;
  45. String mName;
  46. UINT32 mSortedIdx;
  47. bool mIsExpanded;
  48. bool mIsSelected;
  49. bool mIsVisible;
  50. bool isParentRec(TreeElement* element) const;
  51. };
  52. /**
  53. * @brief Contains data about all visible elements in the tree view.
  54. * This might be a TreeElement entry, or a separator (empty space)
  55. * between two TreeElement%s.
  56. */
  57. struct InteractableElement
  58. {
  59. InteractableElement(TreeElement* parent, UINT32 index, const Rect2I& bounds)
  60. :parent(parent), index(index), bounds(bounds)
  61. { }
  62. bool isTreeElement() const { return index % 2 == 1; }
  63. TreeElement* getTreeElement() const;
  64. TreeElement* parent;
  65. UINT32 index;
  66. Rect2I bounds;
  67. };
  68. /**
  69. * @brief Contains data about one of the currently selected tree elements.
  70. */
  71. struct SelectedElement
  72. {
  73. SelectedElement(TreeElement* elem, GUITexture* back)
  74. :element(elem), background(back)
  75. { }
  76. TreeElement* element;
  77. GUITexture* background;
  78. };
  79. public:
  80. /**
  81. * Returns type name of the GUI element used for finding GUI element styles.
  82. */
  83. static const String& getGUITypeName();
  84. /**
  85. * @brief Updates tree view if dirty, among other operations.
  86. * Must be called every frame.
  87. *
  88. * @note Internal method.
  89. */
  90. void _update();
  91. protected:
  92. GUITreeView(const String& backgroundStyle, const String& elementBtnStyle,
  93. const String& foldoutBtnStyle, const String& selectionBackgroundStyle, const String& editBoxStyle,
  94. const String& dragHighlightStyle, const String& dragSepHighlightStyle, const GUIDimensions& dimensions);
  95. virtual ~GUITreeView();
  96. /**
  97. * @copydoc GUIElement::_getOptimalSize
  98. */
  99. Vector2I _getOptimalSize() const override;
  100. /**
  101. * @copydoc GUIElement::updateClippedBounds
  102. */
  103. void updateClippedBounds() override;
  104. /**
  105. * @copydoc GUIElement::_updateLayoutInternal
  106. */
  107. virtual void _updateLayoutInternal(const GUILayoutData& data) override;
  108. /**
  109. * @copydoc GUIElement::_mouseEvent
  110. */
  111. virtual bool _mouseEvent(const GUIMouseEvent& ev) override;
  112. /**
  113. * @copydoc GUIElement::_commandEvent
  114. */
  115. virtual bool _commandEvent(const GUICommandEvent& ev) override;
  116. /**
  117. * @copydoc GUIElement::_virtualButtonEvent
  118. */
  119. virtual bool _virtualButtonEvent(const GUIVirtualButtonEvent& ev) override;
  120. /**
  121. * @brief Attempts to find an interactable element under the specified coordinates.
  122. * Returns null if one cannot be found.
  123. *
  124. * @param coord Coordinates relative to parent GUI widget.
  125. */
  126. const GUITreeView::InteractableElement* findElementUnderCoord(const Vector2I& coord) const;
  127. /**
  128. * @brief Returns the top-most selected tree element if selection is active,
  129. * null otherwise.
  130. */
  131. TreeElement* getTopMostSelectedElement() const;
  132. /**
  133. * @brief Returns the bottom-most selected tree element if selection is active,
  134. * null otherwise.
  135. */
  136. TreeElement* getBottomMostSelectedElement() const;
  137. /**
  138. * @brief Starts rename operation on the specified tree element, spawning an input box.
  139. */
  140. void enableEdit(TreeElement* element);
  141. /**
  142. * @brief Ends rename operation if one is currently active.
  143. *
  144. * @param acceptChanges Should the new name be accepted or discarded.
  145. */
  146. void disableEdit(bool acceptChanges);
  147. /**
  148. * @brief Triggered when the Foldout button for a tree element was been toggled.
  149. * (i.e. something was expanded or collapsed).
  150. */
  151. void elementToggled(TreeElement* element, bool toggled);
  152. /**
  153. * @brief Returns the top level TreeElement.
  154. */
  155. virtual TreeElement& getRootElement() = 0;
  156. /**
  157. * @brief Returns the top level TreeElement that cannot be modified.
  158. */
  159. virtual const TreeElement& getRootElementConst() const = 0;
  160. /**
  161. * @brief Checks if the hierarchy needs any updates and performs those
  162. * updates if needed.
  163. */
  164. virtual void updateTreeElementHierarchy() = 0;
  165. /**
  166. * @brief Changes the name of the content associated with the provided tree element.
  167. */
  168. virtual void renameTreeElement(TreeElement* element, const WString& name) = 0;
  169. /**
  170. * @brief Deletes the content associated with the provided tree element.
  171. */
  172. virtual void deleteTreeElement(TreeElement* element) = 0;
  173. /**
  174. * @brief Checks whether the tree view can accept the currently active drag and drop
  175. * operation.
  176. */
  177. virtual bool acceptDragAndDrop() const = 0;
  178. /**
  179. * @brief Triggered when the user drags a tree element and starts a drag and drop operation.
  180. */
  181. virtual void dragAndDropStart() = 0;
  182. /**
  183. * @brief Triggered when the user ends a drag and drop operation over the tree view.
  184. *
  185. * @param overTreeElement TreeElement the drag operation ended over, if any.
  186. */
  187. virtual void dragAndDropEnded(TreeElement* overTreeElement) = 0;
  188. /**
  189. * @brief Triggered whenever a TreeElement gets selected or deselected.
  190. */
  191. virtual void selectionChanged() { }
  192. /**
  193. * @brief Are any tree elements currently selected.
  194. */
  195. bool isSelectionActive() const;
  196. /**
  197. * @brief Expands the selection to the provided TreeElement. Doesn't clear
  198. * previous selection.
  199. */
  200. void selectElement(TreeElement* element);
  201. /**
  202. * @brief Unselects the provided TreeElement.
  203. */
  204. void unselectElement(TreeElement* element);
  205. /**
  206. * @brief Unselects all selected TreeElement%s.
  207. */
  208. void unselectAll();
  209. /**
  210. * @brief Expands all parents of the provided TreeElement making it interactable.
  211. */
  212. void expandToElement(TreeElement* element);
  213. /**
  214. * @brief Expands the provided TreeElement making its children interactable.
  215. */
  216. void expandElement(TreeElement* element);
  217. /**
  218. * @brief Collapses the provided TreeElement making its children hidden and not interactable.
  219. */
  220. void collapseElement(TreeElement* element);
  221. /**
  222. * @brief Rebuilds the needed GUI elements for the provided TreeElement.
  223. */
  224. void updateElementGUI(TreeElement* element);
  225. /**
  226. * @brief Close any elements that were temporarily expanded due to a drag operation
  227. * hovering over them.
  228. */
  229. void closeTemporarilyExpandedElements();
  230. /**
  231. * @brief Temporarily expand the provided element. Temporarily expanded elements can be
  232. * closed by calling ::closeTemporarilyExpandedElements.
  233. */
  234. void temporarilyExpandElement(const GUITreeView::InteractableElement* mouseOverElement);
  235. /**
  236. * @brief Scrolls the parent GUIScrollArea (if any) so that the specified TreeElement is visible.
  237. *
  238. * @param element Element to scroll to.
  239. * @param center If true the element will be centered in the scroll view,
  240. * otherwise it will be at the top.
  241. */
  242. void scrollToElement(TreeElement* element, bool center);
  243. /**
  244. * @brief Attempts to find the parent GUIScrollArea that the tree view is a child of.
  245. */
  246. GUIScrollArea* findParentScrollArea() const;
  247. /**
  248. * @brief Triggered when the user accepts the changes during a rename operation.
  249. */
  250. void onEditAccepted();
  251. /**
  252. * @brief Triggered when the user rejects the changes during a rename operation.
  253. */
  254. void onEditCanceled();
  255. String mBackgroundStyle;
  256. String mElementBtnStyle;
  257. String mFoldoutBtnStyle;
  258. String mSelectionBackgroundStyle;
  259. String mEditBoxStyle;
  260. String mDragHighlightStyle;
  261. String mDragSepHighlightStyle;
  262. GUITexture* mBackgroundImage;
  263. Vector<InteractableElement> mVisibleElements;
  264. bool mIsElementSelected;
  265. Vector<SelectedElement> mSelectedElements;
  266. TreeElement* mEditElement;
  267. GUITreeViewEditBox* mNameEditBox;
  268. Vector2I mDragStartPosition;
  269. Vector2I mDragPosition;
  270. bool mDragInProgress;
  271. GUITexture* mDragHighlight;
  272. GUITexture* mDragSepHighlight;
  273. Rect2I mTopScrollBounds;
  274. Rect2I mBottomScrollBounds;
  275. ScrollState mScrollState;
  276. float mLastScrollTime;
  277. Stack<TreeElement*> mAutoExpandedElements;
  278. TreeElement* mMouseOverDragElement;
  279. float mMouseOverDragElementTime;
  280. static VirtualButton mRenameVB;
  281. static VirtualButton mDeleteVB;
  282. static const UINT32 ELEMENT_EXTRA_SPACING;
  283. static const UINT32 INDENT_SIZE;
  284. static const UINT32 INITIAL_INDENT_OFFSET;
  285. static const UINT32 DRAG_MIN_DISTANCE;
  286. static const float AUTO_EXPAND_DELAY_SEC;
  287. static const float SCROLL_AREA_HEIGHT_PCT;
  288. static const UINT32 SCROLL_SPEED_PX_PER_SEC;
  289. };
  290. }