BsGUITreeView.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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 mIsHighlighted;
  50. bool mIsVisible;
  51. bool mIsGrayedOut;
  52. bool isParentRec(TreeElement* element) const;
  53. };
  54. /**
  55. * @brief Contains data about all visible elements in the tree view.
  56. * This might be a TreeElement entry, or a separator (empty space)
  57. * between two TreeElement%s.
  58. */
  59. struct InteractableElement
  60. {
  61. InteractableElement(TreeElement* parent, UINT32 index, const Rect2I& bounds)
  62. :parent(parent), index(index), bounds(bounds)
  63. { }
  64. bool isTreeElement() const { return index % 2 == 1; }
  65. TreeElement* getTreeElement() const;
  66. TreeElement* parent;
  67. UINT32 index;
  68. Rect2I bounds;
  69. };
  70. /**
  71. * @brief Contains data about one of the currently selected tree elements.
  72. */
  73. struct SelectedElement
  74. {
  75. SelectedElement()
  76. :element(nullptr), background(nullptr)
  77. { }
  78. SelectedElement(TreeElement* elem, GUITexture* back)
  79. :element(elem), background(back)
  80. { }
  81. TreeElement* element;
  82. GUITexture* background;
  83. };
  84. public:
  85. /**
  86. * Returns type name of the GUI element used for finding GUI element styles.
  87. */
  88. static const String& getGUITypeName();
  89. /**
  90. * @brief Updates tree view if dirty, among other operations.
  91. * Must be called every frame.
  92. *
  93. * @note Internal method.
  94. */
  95. void _update();
  96. protected:
  97. GUITreeView(const String& backgroundStyle, const String& elementBtnStyle,
  98. const String& foldoutBtnStyle, const String& highlightBackgroundStyle, const String& selectionBackgroundStyle, const String& editBoxStyle,
  99. const String& dragHighlightStyle, const String& dragSepHighlightStyle, const GUIDimensions& dimensions);
  100. virtual ~GUITreeView();
  101. /**
  102. * @copydoc GUIElement::_getOptimalSize
  103. */
  104. Vector2I _getOptimalSize() const override;
  105. /**
  106. * @copydoc GUIElement::updateClippedBounds
  107. */
  108. void updateClippedBounds() override;
  109. /**
  110. * @copydoc GUIElement::_updateLayoutInternal
  111. */
  112. virtual void _updateLayoutInternal(const GUILayoutData& data) override;
  113. /**
  114. * @copydoc GUIElement::_mouseEvent
  115. */
  116. virtual bool _mouseEvent(const GUIMouseEvent& ev) override;
  117. /**
  118. * @copydoc GUIElement::_commandEvent
  119. */
  120. virtual bool _commandEvent(const GUICommandEvent& ev) override;
  121. /**
  122. * @copydoc GUIElement::_virtualButtonEvent
  123. */
  124. virtual bool _virtualButtonEvent(const GUIVirtualButtonEvent& ev) override;
  125. /**
  126. * @brief Attempts to find an interactable element under the specified coordinates.
  127. * Returns null if one cannot be found.
  128. *
  129. * @param coord Coordinates relative to parent GUI widget.
  130. */
  131. const GUITreeView::InteractableElement* findElementUnderCoord(const Vector2I& coord) const;
  132. /**
  133. * @brief Returns the top-most selected tree element if selection is active,
  134. * null otherwise.
  135. */
  136. TreeElement* getTopMostSelectedElement() const;
  137. /**
  138. * @brief Returns the bottom-most selected tree element if selection is active,
  139. * null otherwise.
  140. */
  141. TreeElement* getBottomMostSelectedElement() const;
  142. /**
  143. * @brief Starts rename operation on the specified tree element, spawning an input box.
  144. */
  145. void enableEdit(TreeElement* element);
  146. /**
  147. * @brief Ends rename operation if one is currently active.
  148. *
  149. * @param acceptChanges Should the new name be accepted or discarded.
  150. */
  151. void disableEdit(bool acceptChanges);
  152. /**
  153. * @brief Triggered when the Foldout button for a tree element was been toggled.
  154. * (i.e. something was expanded or collapsed).
  155. */
  156. void elementToggled(TreeElement* element, bool toggled);
  157. /**
  158. * @brief Returns the top level TreeElement.
  159. */
  160. virtual TreeElement& getRootElement() = 0;
  161. /**
  162. * @brief Returns the top level TreeElement that cannot be modified.
  163. */
  164. virtual const TreeElement& getRootElementConst() const = 0;
  165. /**
  166. * @brief Checks if the hierarchy needs any updates and performs those
  167. * updates if needed.
  168. */
  169. virtual void updateTreeElementHierarchy() = 0;
  170. /**
  171. * @brief Changes the name of the content associated with the provided tree element.
  172. */
  173. virtual void renameTreeElement(TreeElement* element, const WString& name) = 0;
  174. /**
  175. * @brief Deletes the content associated with the provided tree element.
  176. */
  177. virtual void deleteTreeElement(TreeElement* element) = 0;
  178. /**
  179. * @brief Checks whether the tree view can accept the currently active drag and drop
  180. * operation.
  181. */
  182. virtual bool acceptDragAndDrop() const = 0;
  183. /**
  184. * @brief Triggered when the user drags a tree element and starts a drag and drop operation.
  185. */
  186. virtual void dragAndDropStart() = 0;
  187. /**
  188. * @brief Triggered when the user ends a drag and drop operation over the tree view.
  189. *
  190. * @param overTreeElement TreeElement the drag operation ended over, if any.
  191. */
  192. virtual void dragAndDropEnded(TreeElement* overTreeElement) = 0;
  193. /**
  194. * @brief Triggered whenever a TreeElement gets selected or deselected.
  195. */
  196. virtual void selectionChanged() { }
  197. /**
  198. * @brief Are any tree elements currently selected.
  199. */
  200. bool isSelectionActive() const;
  201. /**
  202. * @brief Expands the selection to the provided TreeElement. Doesn't clear
  203. * previous selection.
  204. */
  205. void selectElement(TreeElement* element);
  206. /**
  207. * @brief Unselects the provided TreeElement.
  208. */
  209. void unselectElement(TreeElement* element);
  210. /**
  211. * @brief Unselects all selected TreeElement%s.
  212. */
  213. void unselectAll();
  214. /**
  215. * @brief Starts rename operation on the currently selected element.
  216. */
  217. void renameSelected();
  218. /**
  219. * @brief Deletes all currently selected elements.
  220. */
  221. void deleteSelection();
  222. /**
  223. * @brief Expands all parents of the provided TreeElement making it interactable.
  224. */
  225. void expandToElement(TreeElement* element);
  226. /**
  227. * @brief Expands the provided TreeElement making its children interactable.
  228. */
  229. void expandElement(TreeElement* element);
  230. /**
  231. * @brief Collapses the provided TreeElement making its children hidden and not interactable.
  232. */
  233. void collapseElement(TreeElement* element);
  234. /**
  235. * @brief Duplicates the currently selected entries.
  236. */
  237. virtual void duplicateSelection() { }
  238. /**
  239. * @brief Marks the current selection for copying.
  240. */
  241. virtual void copySelection() { }
  242. /**
  243. * @brief Marks the current selection for cutting.
  244. */
  245. virtual void cutSelection() { }
  246. /**
  247. * @brief Pastes a set of entries previously marked for cut or copy.
  248. */
  249. virtual void paste() { }
  250. /**
  251. * @brief Rebuilds the needed GUI elements for the provided TreeElement.
  252. */
  253. void updateElementGUI(TreeElement* element);
  254. /**
  255. * @brief Close any elements that were temporarily expanded due to a drag operation
  256. * hovering over them.
  257. */
  258. void closeTemporarilyExpandedElements();
  259. /**
  260. * @brief Temporarily expand the provided element. Temporarily expanded elements can be
  261. * closed by calling ::closeTemporarilyExpandedElements.
  262. */
  263. void temporarilyExpandElement(const GUITreeView::InteractableElement* mouseOverElement);
  264. /**
  265. * @brief Scrolls the parent GUIScrollArea (if any) so that the specified TreeElement is visible.
  266. *
  267. * @param element Element to scroll to.
  268. * @param center If true the element will be centered in the scroll view,
  269. * otherwise it will be at the top.
  270. */
  271. void scrollToElement(TreeElement* element, bool center);
  272. /**
  273. * @brief Attempts to find the parent GUIScrollArea that the tree view is a child of.
  274. */
  275. GUIScrollArea* findParentScrollArea() const;
  276. /**
  277. * @brief Scrolls the tree view to the specified element and highlights it.
  278. */
  279. void ping(TreeElement* element);
  280. /**
  281. * @brief Clears the ping highlight on the currently highlighted element.
  282. */
  283. void clearPing();
  284. /**
  285. * @brief Triggered when the user accepts the changes during a rename operation.
  286. */
  287. void onEditAccepted();
  288. /**
  289. * @brief Triggered when the user rejects the changes during a rename operation.
  290. */
  291. void onEditCanceled();
  292. String mBackgroundStyle;
  293. String mElementBtnStyle;
  294. String mFoldoutBtnStyle;
  295. String mSelectionBackgroundStyle;
  296. String mHighlightBackgroundStyle;
  297. String mEditBoxStyle;
  298. String mDragHighlightStyle;
  299. String mDragSepHighlightStyle;
  300. GUITexture* mBackgroundImage;
  301. Vector<InteractableElement> mVisibleElements;
  302. bool mIsElementSelected;
  303. Vector<SelectedElement> mSelectedElements;
  304. bool mIsElementHighlighted;
  305. SelectedElement mHighlightedElement;
  306. TreeElement* mEditElement;
  307. GUITreeViewEditBox* mNameEditBox;
  308. Vector2I mDragStartPosition;
  309. Vector2I mDragPosition;
  310. bool mDragInProgress;
  311. GUITexture* mDragHighlight;
  312. GUITexture* mDragSepHighlight;
  313. Rect2I mTopScrollBounds;
  314. Rect2I mBottomScrollBounds;
  315. ScrollState mScrollState;
  316. float mLastScrollTime;
  317. Stack<TreeElement*> mAutoExpandedElements;
  318. TreeElement* mMouseOverDragElement;
  319. float mMouseOverDragElementTime;
  320. static VirtualButton mRenameVB;
  321. static VirtualButton mDeleteVB;
  322. static VirtualButton mDuplicateVB;
  323. static VirtualButton mCutVB;
  324. static VirtualButton mCopyVB;
  325. static VirtualButton mPasteVB;
  326. static const UINT32 ELEMENT_EXTRA_SPACING;
  327. static const UINT32 INDENT_SIZE;
  328. static const UINT32 INITIAL_INDENT_OFFSET;
  329. static const UINT32 DRAG_MIN_DISTANCE;
  330. static const float AUTO_EXPAND_DELAY_SEC;
  331. static const float SCROLL_AREA_HEIGHT_PCT;
  332. static const UINT32 SCROLL_SPEED_PX_PER_SEC;
  333. static const Color GRAYED_OUT_COLOR;
  334. };
  335. }