BsGUITreeView.h 11 KB

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