| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- #include "BsGUISceneTreeView.h"
- #include "CmSceneObject.h"
- #include "CmSceneManager.h"
- #include "BsGUISkin.h"
- #include "BsCmdEditPlainFieldGO.h"
- #include "BsDragAndDropManager.h"
- #include "BsCmdReparentSO.h"
- using namespace CamelotFramework;
- using namespace BansheeEngine;
- namespace BansheeEditor
- {
- GUISceneTreeView::DraggedSceneObjects::DraggedSceneObjects(UINT32 numObjects)
- :numObjects(numObjects)
- {
- objects = cm_newN<HSceneObject>(numObjects);
- }
- GUISceneTreeView::DraggedSceneObjects::~DraggedSceneObjects()
- {
- cm_deleteN(objects, numObjects);
- objects = nullptr;
- }
- GUISceneTreeView::GUISceneTreeView(GUIWidget& parent, GUIElementStyle* backgroundStyle, GUIElementStyle* elementBtnStyle,
- GUIElementStyle* foldoutBtnStyle, GUIElementStyle* selectionBackgroundStyle, GUIElementStyle* editBoxStyle,
- BS::GUIElementStyle* dragHighlightStyle, BS::GUIElementStyle* dragSepHighlightStyle, const GUILayoutOptions& layoutOptions)
- :GUITreeView(parent, backgroundStyle, elementBtnStyle, foldoutBtnStyle, selectionBackgroundStyle, editBoxStyle, dragHighlightStyle,
- dragSepHighlightStyle, layoutOptions)
- {
-
- }
- GUISceneTreeView::~GUISceneTreeView()
- {
-
- }
- GUISceneTreeView* GUISceneTreeView::create(GUIWidget& parent, GUIElementStyle* backgroundStyle, GUIElementStyle* elementBtnStyle,
- GUIElementStyle* foldoutBtnStyle, GUIElementStyle* selectionBackgroundStyle, GUIElementStyle* editBoxStyle, GUIElementStyle* dragHighlightStyle,
- GUIElementStyle* dragSepHighlightStyle)
- {
- return new (cm_alloc<GUISceneTreeView, PoolAlloc>()) GUISceneTreeView(parent, backgroundStyle, elementBtnStyle, foldoutBtnStyle,
- selectionBackgroundStyle, editBoxStyle, dragHighlightStyle, dragSepHighlightStyle, GUILayoutOptions::create(&GUISkin::DefaultStyle));
- }
- GUISceneTreeView* GUISceneTreeView::create(GUIWidget& parent, const GUIOptions& options, GUIElementStyle* backgroundStyle,
- GUIElementStyle* elementBtnStyle, GUIElementStyle* foldoutBtnStyle, GUIElementStyle* selectionBackgroundStyle,
- GUIElementStyle* editBoxStyle, GUIElementStyle* dragHighlightStyle, GUIElementStyle* dragSepHighlightStyle)
- {
- return new (cm_alloc<GUISceneTreeView, PoolAlloc>()) GUISceneTreeView(parent, backgroundStyle, elementBtnStyle,
- foldoutBtnStyle, selectionBackgroundStyle, editBoxStyle, dragHighlightStyle, dragSepHighlightStyle, GUILayoutOptions::create(options, &GUISkin::DefaultStyle));
- }
- void GUISceneTreeView::updateTreeElement(SceneTreeElement* element, bool visible)
- {
- HSceneObject currentSO = element->mSceneObject;
- // Check if SceneObject has changed in any way and update the tree element
- if(visible)
- {
- bool completeMatch = (UINT32)element->mChildren.size() == currentSO->getNumChildren();
- // 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
- if(completeMatch)
- {
- for(UINT32 i = 0; i < currentSO->getNumChildren(); i++)
- {
- UINT32 curId = currentSO->getChild(i)->getId();
- if(curId != element->mChildren[i]->mId)
- {
- completeMatch = false;
- break;
- }
- }
- }
- // Not a complete match, compare everything and insert/delete elements as needed
- if(!completeMatch)
- {
- Vector<TreeElement*>::type newChildren;
- bool* tempToDelete = (bool*)stackAlloc(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);
- UINT32 curId = currentSOChild->getId();
- bool found = false;
- for(UINT32 j = 0; j < element->mChildren.size(); j++)
- {
- TreeElement* currentChild = element->mChildren[j];
- if(curId == currentChild->mId)
- {
- tempToDelete[j] = false;
- currentChild->mIsDirty = true;
- currentChild->mSortedIdx = (UINT32)newChildren.size();
- newChildren.push_back(currentChild);
- found = true;
- break;
- }
- }
- if(!found)
- {
- SceneTreeElement* newChild = cm_new<SceneTreeElement>();
- newChild->mParent = element;
- newChild->mSceneObject = currentSOChild;
- newChild->mId = currentSOChild->getId();
- newChild->mName = currentSOChild->getName();
- newChild->mSortedIdx = (UINT32)newChildren.size();
- newChild->mIsDirty = true;
- newChildren.push_back(newChild);
- }
- }
- for(UINT32 i = 0; i < element->mChildren.size(); i++)
- {
- if(!tempToDelete[i])
- continue;
- deleteTreeElement(element->mChildren[i]);
- }
- stackDeallocLast(tempToDelete);
- element->mChildren = newChildren;
- element->mIsDirty = true;
- }
- // Check if name needs updating
- const String& name = element->mSceneObject->getName();
- if(element->mName != name)
- {
- element->mName = name;
- element->mIsDirty = true;
- }
- // Calculate the sorted index of the element based on its name
- TreeElement* parent = element->mParent;
- if(element->mIsDirty && parent != nullptr)
- {
- for(UINT32 i = 0; i < (UINT32)parent->mChildren.size(); i++)
- {
- INT32 stringCompare = element->mName.compare(parent->mChildren[i]->mName);
- if(stringCompare > 0)
- {
- if(element->mSortedIdx < parent->mChildren[i]->mSortedIdx)
- std::swap(element->mSortedIdx, parent->mChildren[i]->mSortedIdx);
- }
- else if(stringCompare < 0)
- {
- if(element->mSortedIdx > parent->mChildren[i]->mSortedIdx)
- std::swap(element->mSortedIdx, parent->mChildren[i]->mSortedIdx);
- }
- }
- }
- }
- bool visibilityChanged = false;
- if(element->mIsVisible != visible)
- {
- visibilityChanged = true;
- element->mIsVisible = visible;
- element->mIsDirty = true;
- }
- if(visibilityChanged || element->mIsVisible)
- {
- for(UINT32 i = 0; i < (UINT32)element->mChildren.size(); i++)
- {
- SceneTreeElement* sceneElement = static_cast<SceneTreeElement*>(element->mChildren[i]);
- updateTreeElement(sceneElement, element->mIsVisible && element->mIsExpanded);
- }
- }
- }
- void GUISceneTreeView::updateTreeElementHierarchy()
- {
- HSceneObject root = CM::gSceneManager().getRootNode();
- mRootElement.mSceneObject = root;
- mRootElement.mId = root->getId();
- mRootElement.mSortedIdx = 0;
- mRootElement.mIsExpanded = true;
- updateTreeElement(&mRootElement, true);
- }
- void GUISceneTreeView::renameTreeElement(GUITreeView::TreeElement* element, const CM::WString& name)
- {
- SceneTreeElement* sceneTreeElement = static_cast<SceneTreeElement*>(element);
- CmdEditPlainFieldGO<String>::execute(sceneTreeElement->mSceneObject, "mName", toString(name));
- }
- bool GUISceneTreeView::acceptDragAndDrop() const
- {
- return DragAndDropManager::instance().isDragInProgress() && DragAndDropManager::instance().getDragTypeId() == (UINT32)DragAndDropType::SceneObject;
- }
- void GUISceneTreeView::dragAndDropStart()
- {
- DraggedSceneObjects* draggedSceneObjects = cm_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(HTexture(), (UINT32)DragAndDropType::SceneObject, (void*)draggedSceneObjects,
- boost::bind(&GUISceneTreeView::dragAndDropFinalize, this));
- }
- void GUISceneTreeView::dragAndDropEnded(TreeElement* overTreeElement)
- {
- if(overTreeElement != nullptr)
- {
- DraggedSceneObjects* draggedSceneObjects = reinterpret_cast<DraggedSceneObjects*>(DragAndDropManager::instance().getDragData());
- Vector<HSceneObject>::type 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);
- }
- }
- void GUISceneTreeView::dragAndDropFinalize()
- {
- mDragInProgress = false;
- markContentAsDirty();
- DraggedSceneObjects* draggedSceneObjects = reinterpret_cast<DraggedSceneObjects*>(DragAndDropManager::instance().getDragData());
- cm_delete(draggedSceneObjects);
- }
- const String& GUISceneTreeView::getGUITypeName()
- {
- static String typeName = "SceneTreeView";
- return typeName;
- }
- }
|