BsGUISceneTreeView.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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/BsGUITreeView.h"
  6. #include "Utility/BsEvent.h"
  7. #include "Utility/BsServiceLocator.h"
  8. namespace bs
  9. {
  10. /** @addtogroup GUI-Editor-Internal
  11. * @{
  12. */
  13. /** Contains SceneObject%s currently involved in a drag and drop operation. */
  14. struct BS_ED_EXPORT DraggedSceneObjects
  15. {
  16. DraggedSceneObjects(UINT32 numObjects);
  17. ~DraggedSceneObjects();
  18. UINT32 numObjects;
  19. HSceneObject* objects;
  20. };
  21. /** Information about a single element in GUISceneTreeView. */
  22. struct BS_SCRIPT_EXPORT(pl:true,api:bed) SceneTreeViewElement
  23. {
  24. HSceneObject sceneObject;
  25. bool isExpanded;
  26. };
  27. /** Information about all elements displayed in a GUISceneTreeView. */
  28. struct BS_SCRIPT_EXPORT(api:bed) SceneTreeViewState
  29. {
  30. BS_SCRIPT_EXPORT()
  31. Vector<SceneTreeViewElement> elements;
  32. };
  33. /** GUI element that displays all SceneObject%s in the current scene in the active project in a tree view. */
  34. class BS_ED_EXPORT GUISceneTreeView : public GUITreeView
  35. {
  36. /** Tree element with SceneObject%-specific data. */
  37. struct SceneTreeElement : public GUITreeView::TreeElement
  38. {
  39. SceneTreeElement()
  40. :mId(0), mIsPrefabInstance(false)
  41. { }
  42. HSceneObject mSceneObject;
  43. UINT64 mId;
  44. bool mIsPrefabInstance;
  45. };
  46. public:
  47. /** Returns type name of the GUI element used for finding GUI element styles. */
  48. static const String& getGUITypeName();
  49. /**
  50. * Creates a new resource tree view element.
  51. *
  52. * @param[in] backgroundStyle Name of the style for the tree view background.
  53. * @param[in] elementBtnStyle Name of the style for a normal tree view element.
  54. * @param[in] foldoutBtnStyle Name of the style for a foldout element (for example for a folder).
  55. * @param[in] selectionBackgroundStyle Name of the style for the background of selected elements.
  56. * @param[in] highlightBackgroundStyle Name of the style for the background of highlighted elements.
  57. * @param[in] editBoxStyle Name of the style for element that is being renamed.
  58. * @param[in] dragHighlightStyle Name of the style for the element being dragged over.
  59. * @param[in] dragSepHighlightStyle Name of the style for the separator displayed while dragging an element
  60. * between two other elements.
  61. */
  62. static GUISceneTreeView* create(
  63. const String& backgroundStyle = StringUtil::BLANK, const String& elementBtnStyle = StringUtil::BLANK,
  64. const String& foldoutBtnStyle = StringUtil::BLANK, const String& highlightBackgroundStyle = StringUtil::BLANK,
  65. const String& selectionBackgroundStyle = StringUtil::BLANK, const String& editBoxStyle = StringUtil::BLANK,
  66. const String& dragHighlightStyle = StringUtil::BLANK, const String& dragSepHighlightStyle = StringUtil::BLANK);
  67. /**
  68. * Creates a new resource tree view element.
  69. *
  70. * @param[in] options Options that allow you to control how is the element positioned and
  71. * sized. This will override any similar options set by style.
  72. * @param[in] backgroundStyle Name of the style for the tree view background.
  73. * @param[in] elementBtnStyle Name of the style for a normal tree view element.
  74. * @param[in] foldoutBtnStyle Name of the style for a foldout element (for example for a folder).
  75. * @param[in] highlightBackgroundStyle Name of the style for the background of highlighted elements.
  76. * @param[in] selectionBackgroundStyle Name of the style for the background of selected elements.
  77. * @param[in] editBoxStyle Name of the style for element that is being renamed.
  78. * @param[in] dragHighlightStyle Name of the style for the element being dragged over.
  79. * @param[in] dragSepHighlightStyle Name of the style for the separator displayed while dragging an element
  80. * between two other elements.
  81. */
  82. static GUISceneTreeView* create(const GUIOptions& options,
  83. const String& backgroundStyle = StringUtil::BLANK, const String& elementBtnStyle = StringUtil::BLANK,
  84. const String& foldoutBtnStyle = StringUtil::BLANK, const String& highlightBackgroundStyle = StringUtil::BLANK,
  85. const String& selectionBackgroundStyle = StringUtil::BLANK, const String& editBoxStyle = StringUtil::BLANK,
  86. const String& dragHighlightStyle = StringUtil::BLANK, const String& dragSepHighlightStyle = StringUtil::BLANK);
  87. /** Returns a list of SceneObject&s currently selected (if any). */
  88. Vector<HSceneObject> getSelection() const;
  89. /** Changes the active selection to the provided SceneObject%s. */
  90. void setSelection(const Vector<HSceneObject>& objects);
  91. /** Scrolls to and highlights the selected object (does not select it). */
  92. void ping(const HSceneObject& object);
  93. /** @copydoc GUITreeView::duplicateSelection */
  94. void duplicateSelection() override;
  95. /** @copydoc GUITreeView::copySelection */
  96. void copySelection() override;
  97. /** @copydoc GUITreeView::cutSelection */
  98. void cutSelection() override;
  99. /** @copydoc GUITreeView::paste */
  100. void paste() override;
  101. /** Returns the expand/collapse state of the elements in the tree view, allowing it to be restored later. */
  102. SPtr<SceneTreeViewState> getState() const;
  103. /** Sets the expand/collapse state of the elements in the tree view. */
  104. void setState(const SPtr<SceneTreeViewState>& state);
  105. /** Triggered whenever the selection changes. Call getSelection() to retrieve new selection. */
  106. Event<void()> onSelectionChanged;
  107. /**
  108. * Triggered whenever the scene is modified in any way from within the scene tree view (for example object is
  109. * deleted, added, etc.).
  110. */
  111. Event<void()> onModified;
  112. /**
  113. * Triggered when a resource drag and drop operation finishes over the scene tree view. Provided scene object
  114. * is the tree view element that the operation finished over (or null if none), and the list of paths is the list
  115. * of relative paths of the resources that were dropped.
  116. */
  117. Event<void(const HSceneObject&, const Vector<Path>&)> onResourceDropped;
  118. static const MessageId SELECTION_CHANGED_MSG;
  119. protected:
  120. virtual ~GUISceneTreeView();
  121. protected:
  122. GUISceneTreeView(const String& backgroundStyle, const String& elementBtnStyle,
  123. const String& foldoutBtnStyle, const String& highlightBackgroundStyle, const String& selectionBackgroundStyle,
  124. const String& editBoxStyle, const String& dragHighlightStyle, const String& dragSepHighlightStyle, const GUIDimensions& dimensions);
  125. /**
  126. * Checks it the SceneObject referenced by this tree element changed in any way and updates the tree element. This
  127. * can involve recursing all children and updating them as well.
  128. */
  129. void updateTreeElement(SceneTreeElement* element);
  130. /**
  131. * Triggered when a drag and drop operation that was started by the tree view ends, regardless if it was processed
  132. * or not.
  133. */
  134. void dragAndDropFinalize();
  135. /** @copydoc GUITreeView::getRootElement */
  136. TreeElement& getRootElement() override { return mRootElement; }
  137. /** @copydoc GUITreeView::getRootElementConst */
  138. const TreeElement& getRootElementConst() const override { return mRootElement; }
  139. /** @copydoc GUITreeView::updateTreeElementHierarchy */
  140. void updateTreeElementHierarchy() override;
  141. /** @copydoc GUITreeView::renameTreeElement */
  142. void renameTreeElement(TreeElement* element, const String& name) override;
  143. /** @copydoc GUITreeView::deleteTreeElement */
  144. void deleteTreeElement(TreeElement* element) override;
  145. /** @copydoc GUITreeView::acceptDragAndDrop */
  146. bool acceptDragAndDrop() const override;
  147. /** @copydoc GUITreeView::dragAndDropStart */
  148. void dragAndDropStart(const Vector<TreeElement*>& elements) override;
  149. /** @copydoc GUITreeView::dragAndDropEnded */
  150. void dragAndDropEnded(TreeElement* overTreeElement) override;
  151. /** @copydoc GUITreeView::_acceptDragAndDrop */
  152. bool _acceptDragAndDrop(Vector2I position, UINT32 typeId) const override;
  153. /** @copydoc GUITreeView::selectionChanged */
  154. void selectionChanged() override;
  155. /** Deletes the internal TreeElement representation without actually deleting the referenced SceneObject. */
  156. void deleteTreeElementInternal(TreeElement* element);
  157. /** Attempts to find a tree element referencing the specified scene object. */
  158. SceneTreeElement* findTreeElement(const HSceneObject& so);
  159. /** Creates a new scene object as a child of the currently selected object (if any). */
  160. void createNewSO();
  161. /** Removes all elements from the list used for copy/cut operations. */
  162. void clearCopyList();
  163. /**
  164. * Changes the active selection to the provided SceneObject%s and optionally triggers the necessary event
  165. * callbacks.
  166. */
  167. void setSelection(const Vector<HSceneObject>& objects, bool triggerEvents);
  168. /** Iterates over the tree element hierarchy, starting with the provided element and calls @p predicate. */
  169. template<class T>
  170. void recurse(SceneTreeElement* element, T predicate) const
  171. {
  172. predicate(element);
  173. for(UINT32 j = 0; j < element->mChildren.size(); j++)
  174. {
  175. SceneTreeElement* currentChild = static_cast<SceneTreeElement*>(element->mChildren[j]);
  176. recurse(currentChild, predicate);
  177. }
  178. }
  179. /**
  180. * Cleans duplicate objects from the provided scene object list. This involves removing child elements if their
  181. * parents are already part of the list.
  182. */
  183. static void cleanDuplicates(Vector<HSceneObject>& objects);
  184. SceneTreeElement mRootElement;
  185. Vector<HSceneObject> mCopyList;
  186. bool mCutFlag;
  187. static const Color PREFAB_TINT;
  188. };
  189. typedef ServiceLocator<GUISceneTreeView> SceneTreeViewLocator;
  190. /** @} */
  191. }