BsGUITreeView.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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 "GUI/BsGUIElementContainer.h"
  6. #include "Input/BsVirtualInput.h"
  7. #include "Utility/BsEvent.h"
  8. namespace bs
  9. {
  10. /** @addtogroup GUI-Editor-Internal
  11. * @{
  12. */
  13. /**
  14. * GUI element that displays some contents in a tree-view where elements are placed vertically above/beneath each other,
  15. * and different elements may be nested within other elements.
  16. *
  17. * Elements may be selected, renamed, dragged and re-parented.
  18. *
  19. * This class is abstract and meant to be extended by an implementation specific to some content type (for example scene
  20. * object hierarchy).
  21. */
  22. class BS_ED_EXPORT GUITreeView : public GUIElementContainer
  23. {
  24. protected:
  25. /** Types of possible states when the tree view is auto-scrolling. */
  26. enum class ScrollState
  27. {
  28. None,
  29. Up,
  30. Down,
  31. TransitioningUp,
  32. TransitioningDown
  33. };
  34. /**
  35. * Contains data about a single piece of content and all its children. This element may be visible and represented
  36. * by a GUI element, but might not (for example its parent is collapsed).
  37. */
  38. struct TreeElement
  39. {
  40. TreeElement();
  41. virtual ~TreeElement();
  42. TreeElement* mParent;
  43. Vector<TreeElement*> mChildren;
  44. GUIToggle* mFoldoutBtn;
  45. GUILabel* mElement;
  46. String mName;
  47. UINT32 mSortedIdx;
  48. bool mIsExpanded;
  49. bool mIsSelected;
  50. bool mIsHighlighted;
  51. bool mIsVisible;
  52. bool mIsCut;
  53. bool mIsDisabled;
  54. Color mTint;
  55. bool isParentRec(TreeElement* element) const;
  56. };
  57. /**
  58. * Contains data about all visible elements in the tree view. This might be a TreeElement entry, or a separator
  59. * (empty space) 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. /** Contains data about one of the currently selected tree elements. */
  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. /** Returns type name of the GUI element used for finding GUI element styles. */
  86. static const String& getGUITypeName();
  87. /** Deletes all currently selected elements. */
  88. void deleteSelection();
  89. /** Duplicates the currently selected entries. */
  90. virtual void duplicateSelection() { }
  91. /** Marks the current selection for copying. */
  92. virtual void copySelection() { }
  93. /** Marks the current selection for cutting. */
  94. virtual void cutSelection() { }
  95. /** Pastes a set of entries previously marked for cut or copy. */
  96. virtual void paste() { }
  97. /** Starts rename operation on the currently selected element. */
  98. void renameSelected();
  99. /** @name Internal
  100. * @{
  101. */
  102. /** Updates tree view if dirty, among other operations. Must be called every frame. */
  103. void _update();
  104. /** @} */
  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. /** @copydoc GUIElement::_getOptimalSize */
  111. Vector2I _getOptimalSize() const override;
  112. /** @copydoc GUIElement::updateClippedBounds */
  113. void updateClippedBounds() override;
  114. /** @copydoc GUIElement::_updateLayoutInternal */
  115. void _updateLayoutInternal(const GUILayoutData& data) override;
  116. /** @copydoc GUIElement::_mouseEvent */
  117. bool _mouseEvent(const GUIMouseEvent& ev) override;
  118. /** @copydoc GUIElement::_commandEvent */
  119. bool _commandEvent(const GUICommandEvent& ev) override;
  120. /**
  121. * Attempts to find an interactable element under the specified coordinates. Returns null if one cannot be found.
  122. *
  123. * @param[in] coord Coordinates relative to parent GUI widget.
  124. */
  125. const InteractableElement* findElementUnderCoord(const Vector2I& coord) const;
  126. /** Returns the top-most selected tree element if selection is active, null otherwise. */
  127. TreeElement* getTopMostSelectedElement() const;
  128. /** Returns the bottom-most selected tree element if selection is active, null otherwise. */
  129. TreeElement* getBottomMostSelectedElement() const;
  130. /** Starts rename operation on the specified tree element, spawning an input box. */
  131. void enableEdit(TreeElement* element);
  132. /**
  133. * Ends rename operation if one is currently active.
  134. *
  135. * @param[in] acceptChanges Should the new name be accepted or discarded.
  136. */
  137. void disableEdit(bool acceptChanges);
  138. /**
  139. * Triggered when the Foldout button for a tree element was been toggled (something was expanded or collapsed).
  140. */
  141. void elementToggled(TreeElement* element, bool toggled);
  142. /** Returns the top level TreeElement. */
  143. virtual TreeElement& getRootElement() = 0;
  144. /** Returns the top level TreeElement that cannot be modified. */
  145. virtual const TreeElement& getRootElementConst() const = 0;
  146. /** Checks if the hierarchy needs any updates and performs those updates if needed. */
  147. virtual void updateTreeElementHierarchy() = 0;
  148. /** Changes the name of the content associated with the provided tree element. */
  149. virtual void renameTreeElement(TreeElement* element, const String& name) = 0;
  150. /** Deletes the content associated with the provided tree element. */
  151. virtual void deleteTreeElement(TreeElement* element) = 0;
  152. /** Checks whether the tree view can accept the currently active drag and drop operation. */
  153. virtual bool acceptDragAndDrop() const = 0;
  154. /** Triggered when the user drags a tree element and starts a drag and drop operation. */
  155. virtual void dragAndDropStart(const Vector<TreeElement*>& elements) = 0;
  156. /**
  157. * Triggered when the user ends a drag and drop operation over the tree view.
  158. *
  159. * @param[in] overTreeElement TreeElement the drag operation ended over, if any.
  160. */
  161. virtual void dragAndDropEnded(TreeElement* overTreeElement) = 0;
  162. /** Triggered whenever a TreeElement gets selected or deselected. */
  163. virtual void selectionChanged() { }
  164. /** Are any tree elements currently selected. */
  165. bool isSelectionActive() const;
  166. /** Expands the selection to the provided TreeElement. Doesn't clear previous selection. */
  167. void selectElement(TreeElement* element, bool triggerEvents = true);
  168. /** Unselects the provided TreeElement. */
  169. void unselectElement(TreeElement* element, bool triggerEvents = true);
  170. /**
  171. * Unselects all selected TreeElement%s.
  172. *
  173. * @param[in] sendEvent Determines should the external world be notified of this change.
  174. */
  175. void unselectAll(bool sendEvent = true);
  176. /** Expands all parents of the provided TreeElement making it interactable. */
  177. void expandToElement(TreeElement* element);
  178. /**
  179. * Expands the provided TreeElement making its children interactable. Set @p toggleButton to true to change
  180. * the toggle state on the toggle button itself. Set this to false if called from the toggle button callback.
  181. */
  182. void expandElement(TreeElement* element, bool toggleButton = true);
  183. /**
  184. * Collapses the provided TreeElement making its children hidden and not interactable. Set @p toggleButton to true
  185. * to change the toggle state on the toggle button itself. Set this to false if called from the toggle button
  186. * callback.
  187. */
  188. void collapseElement(TreeElement* element, bool toggleButton = true);
  189. /** Rebuilds the needed GUI elements for the provided TreeElement. */
  190. void updateElementGUI(TreeElement* element);
  191. /** Close any elements that were temporarily expanded due to a drag operation hovering over them. */
  192. void closeTemporarilyExpandedElements();
  193. /**
  194. * Temporarily expand the provided element. Temporarily expanded elements can be closed by calling
  195. * closeTemporarilyExpandedElements().
  196. */
  197. void temporarilyExpandElement(const GUITreeView::InteractableElement* mouseOverElement);
  198. /**
  199. * Scrolls the parent GUIScrollArea (if any) so that the specified TreeElement is visible.
  200. *
  201. * @param[in] element Element to scroll to.
  202. * @param[in] center If true the element will be centered in the scroll view, otherwise it will be at the top.
  203. */
  204. void scrollToElement(TreeElement* element, bool center);
  205. /** Attempts to find the parent GUIScrollArea that the tree view is a child of. */
  206. GUIScrollArea* findParentScrollArea() const;
  207. /** Scrolls the tree view to the specified element and highlights it. */
  208. void ping(TreeElement* element);
  209. /** Clears the ping highlight on the currently highlighted element. */
  210. void clearPing();
  211. /** Triggered when the user accepts the changes during a rename operation. */
  212. void onEditAccepted();
  213. /** Triggered when the user rejects the changes during a rename operation. */
  214. void onEditCanceled();
  215. /** Triggered when the user clicks outside of the edit box during a rename operation. */
  216. void onEditFocusLost();
  217. String mBackgroundStyle;
  218. String mElementBtnStyle;
  219. String mFoldoutBtnStyle;
  220. String mSelectionBackgroundStyle;
  221. String mHighlightBackgroundStyle;
  222. String mEditBoxStyle;
  223. String mDragHighlightStyle;
  224. String mDragSepHighlightStyle;
  225. GUITexture* mBackgroundImage;
  226. Vector<InteractableElement> mVisibleElements;
  227. bool mIsElementSelected;
  228. Vector<SelectedElement> mSelectedElements;
  229. bool mIsElementHighlighted;
  230. SelectedElement mHighlightedElement;
  231. TreeElement* mEditElement;
  232. GUITreeViewEditBox* mNameEditBox;
  233. Vector2I mDragStartPosition;
  234. Vector2I mDragPosition;
  235. bool mDragInProgress;
  236. GUITexture* mDragHighlight;
  237. GUITexture* mDragSepHighlight;
  238. Rect2I mTopScrollBounds;
  239. Rect2I mBottomScrollBounds;
  240. ScrollState mScrollState;
  241. float mLastScrollTime;
  242. Stack<TreeElement*> mAutoExpandedElements;
  243. TreeElement* mMouseOverDragElement;
  244. float mMouseOverDragElementTime;
  245. static const UINT32 ELEMENT_EXTRA_SPACING;
  246. static const UINT32 INDENT_SIZE;
  247. static const UINT32 INITIAL_INDENT_OFFSET;
  248. static const UINT32 DRAG_MIN_DISTANCE;
  249. static const float AUTO_EXPAND_DELAY_SEC;
  250. static const float SCROLL_AREA_HEIGHT_PCT;
  251. static const UINT32 SCROLL_SPEED_PX_PER_SEC;
  252. static const Color CUT_COLOR;
  253. static const Color DISABLED_COLOR;
  254. };
  255. /** @} */
  256. }