| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425 |
- #include "BsGUISceneTreeView.h"
- #include "BsSceneObject.h"
- #include "BsSceneManager.h"
- #include "BsGUISkin.h"
- #include "BsCmdRecordSO.h"
- #include "BsCmdReparentSO.h"
- #include "BsCmdDeleteSO.h"
- #include "BsDragAndDropManager.h"
- #include "BsSelection.h"
- #include "BsGUIResourceTreeView.h"
- #include "BsProjectLibrary.h"
- #include "BsProjectResourceMeta.h"
- #include "BsPrefab.h"
- #include "BsResources.h"
- namespace BansheeEngine
- {
- const MessageId GUISceneTreeView::SELECTION_CHANGED_MSG = MessageId("SceneTreeView_SelectionChanged");
- DraggedSceneObjects::DraggedSceneObjects(UINT32 numObjects)
- :numObjects(numObjects)
- {
- objects = bs_newN<HSceneObject>(numObjects);
- }
- DraggedSceneObjects::~DraggedSceneObjects()
- {
- bs_deleteN(objects, numObjects);
- objects = nullptr;
- }
- GUISceneTreeView::GUISceneTreeView(const String& backgroundStyle, const String& elementBtnStyle,
- const String& foldoutBtnStyle, const String& highlightBackgroundStyle, const String& selectionBackgroundStyle,
- const String& editBoxStyle, const String& dragHighlightStyle, const String& dragSepHighlightStyle, const GUIDimensions& dimensions)
- :GUITreeView(backgroundStyle, elementBtnStyle, foldoutBtnStyle, highlightBackgroundStyle, selectionBackgroundStyle, editBoxStyle, dragHighlightStyle,
- dragSepHighlightStyle, dimensions)
- {
- SceneTreeViewLocator::_provide(this);
- }
- GUISceneTreeView::~GUISceneTreeView()
- {
- SceneTreeViewLocator::_provide(nullptr);
- }
- GUISceneTreeView* GUISceneTreeView::create(const String& backgroundStyle, const String& elementBtnStyle, const String& foldoutBtnStyle,
- const String& highlightBackgroundStyle, const String& selectionBackgroundStyle, const String& editBoxStyle, const String& dragHighlightStyle,
- const String& dragSepHighlightStyle)
- {
- return new (bs_alloc<GUISceneTreeView, PoolAlloc>()) GUISceneTreeView(backgroundStyle, elementBtnStyle, foldoutBtnStyle,
- highlightBackgroundStyle, selectionBackgroundStyle, editBoxStyle, dragHighlightStyle, dragSepHighlightStyle, GUIDimensions::create());
- }
- GUISceneTreeView* GUISceneTreeView::create(const GUIOptions& options, const String& backgroundStyle, const String& elementBtnStyle,
- const String& foldoutBtnStyle, const String& highlightBackgroundStyle, const String& selectionBackgroundStyle,
- const String& editBoxStyle, const String& dragHighlightStyle, const String& dragSepHighlightStyle)
- {
- return new (bs_alloc<GUISceneTreeView, PoolAlloc>()) GUISceneTreeView(backgroundStyle, elementBtnStyle,
- foldoutBtnStyle, highlightBackgroundStyle, selectionBackgroundStyle, editBoxStyle,
- dragHighlightStyle, dragSepHighlightStyle, GUIDimensions::create(options));
- }
- void GUISceneTreeView::updateTreeElement(SceneTreeElement* element)
- {
- HSceneObject currentSO = element->mSceneObject;
- // Check if SceneObject has changed in any way and update the tree element
- // Early exit case - Most commonly there will be no changes between active and cached data so
- // we first do a quick check in order to avoid expensive comparison later
- bool completeMatch = true;
- UINT32 visibleChildCount = 0;
- for (UINT32 i = 0; i < currentSO->getNumChildren(); i++)
- {
- if (i >= element->mChildren.size())
- {
- completeMatch = false;
- break;
- }
- HSceneObject currentSOChild = currentSO->getChild(i);
- #if BS_DEBUG_MODE == 0
- if (currentSOChild->hasFlag(SOF_Internal))
- continue;
- #endif
- SceneTreeElement* currentChild = static_cast<SceneTreeElement*>(element->mChildren[visibleChildCount]);
- visibleChildCount++;
- UINT64 curId = currentSOChild->getInstanceId();
- if (curId != currentChild->mId)
- {
- completeMatch = false;
- break;
- }
- }
- completeMatch &= visibleChildCount == element->mChildren.size();
- // Not a complete match, compare everything and insert/delete elements as needed
- bool needsUpdate = false;
- if(!completeMatch)
- {
- Vector<TreeElement*> newChildren;
- bool* tempToDelete = (bool*)bs_stack_alloc(sizeof(bool) * (UINT32)element->mChildren.size());
- for(UINT32 i = 0; i < (UINT32)element->mChildren.size(); i++)
- tempToDelete[i] = true;
- for(UINT32 i = 0; i < currentSO->getNumChildren(); i++)
- {
- HSceneObject currentSOChild = currentSO->getChild(i);
- #if BS_DEBUG_MODE == 0
- if (currentSOChild->hasFlag(SOF_Internal))
- continue;
- #endif
- UINT64 curId = currentSOChild->getInstanceId();
- bool found = false;
- for(UINT32 j = 0; j < element->mChildren.size(); j++)
- {
- SceneTreeElement* currentChild = static_cast<SceneTreeElement*>(element->mChildren[j]);
- if(curId == currentChild->mId)
- {
- tempToDelete[j] = false;
- currentChild->mSortedIdx = (UINT32)newChildren.size();
- newChildren.push_back(currentChild);
- found = true;
- break;
- }
- }
- if(!found)
- {
- SceneTreeElement* newChild = bs_new<SceneTreeElement>();
- newChild->mParent = element;
- newChild->mSceneObject = currentSOChild;
- newChild->mId = currentSOChild->getInstanceId();
- newChild->mName = currentSOChild->getName();
- newChild->mSortedIdx = (UINT32)newChildren.size();
- newChild->mIsVisible = element->mIsVisible && element->mIsExpanded;
- newChildren.push_back(newChild);
- updateElementGUI(newChild);
- }
- }
- for(UINT32 i = 0; i < element->mChildren.size(); i++)
- {
- if(!tempToDelete[i])
- continue;
- deleteTreeElementInternal(element->mChildren[i]);
- }
- bs_stack_free(tempToDelete);
- element->mChildren = newChildren;
- needsUpdate = true;
- }
- // Check if name needs updating
- const String& name = element->mSceneObject->getName();
- if(element->mName != name)
- {
- element->mName = name;
- needsUpdate = true;
- }
- if(needsUpdate)
- updateElementGUI(element);
- for(UINT32 i = 0; i < (UINT32)element->mChildren.size(); i++)
- {
- SceneTreeElement* sceneElement = static_cast<SceneTreeElement*>(element->mChildren[i]);
- updateTreeElement(sceneElement);
- }
- // Calculate the sorted index of the elements based on their name
- bs_frame_mark();
- FrameVector<SceneTreeElement*> sortVector;
- for (auto& child : element->mChildren)
- sortVector.push_back(static_cast<SceneTreeElement*>(child));
- std::sort(sortVector.begin(), sortVector.end(),
- [&](const SceneTreeElement* lhs, const SceneTreeElement* rhs)
- {
- return StringUtil::compare(lhs->mName, rhs->mName, false) < 0;
- });
- UINT32 idx = 0;
- for (auto& child : sortVector)
- {
- child->mSortedIdx = idx;
- idx++;
- }
- bs_frame_clear();
- }
- void GUISceneTreeView::updateTreeElementHierarchy()
- {
- HSceneObject root = gCoreSceneManager().getRootNode();
- mRootElement.mSceneObject = root;
- mRootElement.mId = root->getInstanceId();
- mRootElement.mSortedIdx = 0;
- mRootElement.mIsExpanded = true;
- updateTreeElement(&mRootElement);
- }
- void GUISceneTreeView::renameTreeElement(GUITreeView::TreeElement* element, const WString& name)
- {
- SceneTreeElement* sceneTreeElement = static_cast<SceneTreeElement*>(element);
- HSceneObject so = sceneTreeElement->mSceneObject;
- CmdRecordSO::execute(so, L"Renamed \"" + toWString(so->getName()) + L"\"");
- so->setName("mName");
- }
- void GUISceneTreeView::deleteTreeElement(TreeElement* element)
- {
- SceneTreeElement* sceneTreeElement = static_cast<SceneTreeElement*>(element);
- HSceneObject so = sceneTreeElement->mSceneObject;
- CmdDeleteSO::execute(so, L"Deleted \"" + toWString(so->getName()) + L"\"");
- }
- void GUISceneTreeView::deleteTreeElementInternal(GUITreeView::TreeElement* element)
- {
- closeTemporarilyExpandedElements(); // In case this element is one of them
- if (element->mIsHighlighted)
- clearPing();
- if(element->mIsSelected)
- unselectElement(element);
- bs_delete(element);
- }
- bool GUISceneTreeView::acceptDragAndDrop() const
- {
- return DragAndDropManager::instance().isDragInProgress() &&
- (DragAndDropManager::instance().getDragTypeId() == (UINT32)DragAndDropType::SceneObject ||
- DragAndDropManager::instance().getDragTypeId() == (UINT32)DragAndDropType::Resources);
- }
- void GUISceneTreeView::dragAndDropStart()
- {
- DraggedSceneObjects* draggedSceneObjects = bs_new<DraggedSceneObjects>((UINT32)mSelectedElements.size());
- UINT32 cnt = 0;
- for(auto& selectedElement : mSelectedElements)
- {
- SceneTreeElement* sceneTreeElement = static_cast<SceneTreeElement*>(selectedElement.element);
- draggedSceneObjects->objects[cnt] = sceneTreeElement->mSceneObject;
- cnt++;
- }
- DragAndDropManager::instance().startDrag((UINT32)DragAndDropType::SceneObject, (void*)draggedSceneObjects,
- std::bind(&GUISceneTreeView::dragAndDropFinalize, this), false);
- }
- void GUISceneTreeView::dragAndDropEnded(TreeElement* overTreeElement)
- {
- UINT32 dragTypeId = DragAndDropManager::instance().getDragTypeId();
- if (dragTypeId == (UINT32)DragAndDropType::SceneObject)
- {
- if (overTreeElement != nullptr)
- {
- DraggedSceneObjects* draggedSceneObjects = reinterpret_cast<DraggedSceneObjects*>(DragAndDropManager::instance().getDragData());
- Vector<HSceneObject> sceneObjects;
- SceneTreeElement* sceneTreeElement = static_cast<SceneTreeElement*>(overTreeElement);
- HSceneObject newParent = sceneTreeElement->mSceneObject;
- for (UINT32 i = 0; i < draggedSceneObjects->numObjects; i++)
- {
- if (draggedSceneObjects->objects[i] != newParent)
- sceneObjects.push_back(draggedSceneObjects->objects[i]);
- }
- CmdReparentSO::execute(sceneObjects, newParent);
- }
- }
- else if (dragTypeId == (UINT32)DragAndDropType::Resources)
- {
- DraggedResources* draggedResources = reinterpret_cast<DraggedResources*>(DragAndDropManager::instance().getDragData());
- HSceneObject newParent;
- if (overTreeElement != nullptr)
- {
- SceneTreeElement* sceneTreeElement = static_cast<SceneTreeElement*>(overTreeElement);
- newParent = sceneTreeElement->mSceneObject;
- }
- for (auto& path : draggedResources->resourcePaths)
- {
- ProjectLibrary::LibraryEntry* entry = ProjectLibrary::instance().findEntry(path);
- if (entry != nullptr && entry->type == ProjectLibrary::LibraryEntryType::File)
- {
- ProjectLibrary::ResourceEntry* resEntry = static_cast<ProjectLibrary::ResourceEntry*>(entry);
- if (resEntry->meta->getTypeID() == TID_Prefab)
- {
- HPrefab prefab = static_resource_cast<Prefab>(gResources().loadFromUUID(resEntry->meta->getUUID()));
- if (prefab != nullptr)
- {
- HSceneObject instance = prefab->instantiate();
- if (newParent != nullptr)
- instance->setParent(newParent);
- }
- }
- }
- }
- }
- }
- void GUISceneTreeView::dragAndDropFinalize()
- {
- mDragInProgress = false;
- _markLayoutAsDirty();
- if (DragAndDropManager::instance().getDragTypeId() == (UINT32)DragAndDropType::SceneObject)
- {
- DraggedSceneObjects* draggedSceneObjects = reinterpret_cast<DraggedSceneObjects*>(DragAndDropManager::instance().getDragData());
- bs_delete(draggedSceneObjects);
- }
- }
- bool GUISceneTreeView::_acceptDragAndDrop(const Vector2I position, UINT32 typeId) const
- {
- return typeId == (UINT32)DragAndDropType::SceneObject || typeId == (UINT32)DragAndDropType::Resources;
- }
- void GUISceneTreeView::selectionChanged()
- {
- onSelectionChanged();
- sendMessage(SELECTION_CHANGED_MSG);
- }
- Vector<HSceneObject> GUISceneTreeView::getSelection() const
- {
- Vector<HSceneObject> selectedSOs;
- for (auto& selectedElem : mSelectedElements)
- {
- SceneTreeElement* sceneTreeElement = static_cast<SceneTreeElement*>(selectedElem.element);
- selectedSOs.push_back(sceneTreeElement->mSceneObject);
- }
- return selectedSOs;
- }
- void GUISceneTreeView::setSelection(const Vector<HSceneObject>& objects)
- {
- unselectAll();
- SceneTreeElement& root = mRootElement;
- Stack<SceneTreeElement*> todo;
- todo.push(&mRootElement);
- while (!todo.empty())
- {
- SceneTreeElement* currentElem = todo.top();
- todo.pop();
- auto iterFind = std::find(objects.begin(), objects.end(), currentElem->mSceneObject);
- if (iterFind != objects.end())
- {
- expandToElement(currentElem);
- selectElement(currentElem);
- }
- for (auto& child : currentElem->mChildren)
- {
- SceneTreeElement* sceneChild = static_cast<SceneTreeElement*>(child);
- todo.push(sceneChild);
- }
- }
- }
- void GUISceneTreeView::ping(const HSceneObject& object)
- {
- SceneTreeElement& root = mRootElement;
- Stack<SceneTreeElement*> todo;
- todo.push(&mRootElement);
- while (!todo.empty())
- {
- SceneTreeElement* currentElem = todo.top();
- todo.pop();
- if (currentElem->mSceneObject == object)
- {
- GUITreeView::ping(currentElem);
- break;
- }
- for (auto& child : currentElem->mChildren)
- {
- SceneTreeElement* sceneChild = static_cast<SceneTreeElement*>(child);
- todo.push(sceneChild);
- }
- }
- }
- const String& GUISceneTreeView::getGUITypeName()
- {
- static String typeName = "SceneTreeView";
- return typeName;
- }
- }
|