BsGUITreeView.h 11 KB

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