BsGUITreeView.h 11 KB

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