BsGUITreeView.h 11 KB

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