| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- #include "BsGUIGameObjectField.h"
- #include "BsGUILayoutX.h"
- #include "BsGUILabel.h"
- #include "BsGUIDropButton.h"
- #include "BsGUIButton.h"
- #include "BsBuiltinResources.h"
- #include "BsCGUIWidget.h"
- #include "BsGUIMouseEvent.h"
- #include "BsGUISceneTreeView.h"
- #include "BsCGUIWidget.h"
- #include "BsGameObjectManager.h"
- #include "BsScriptAssemblyManager.h"
- #include "BsMonoClass.h"
- #include "BsSceneObject.h"
- #include "BsManagedComponent.h"
- #include "BsMonoManager.h"
- #include "BsBuiltinEditorResources.h"
- #include "BsComponent.h"
- #include "BsSelection.h"
- using namespace std::placeholders;
- namespace BansheeEngine
- {
- const UINT32 GUIGameObjectField::DEFAULT_LABEL_WIDTH = 100;
- GUIGameObjectField::GUIGameObjectField(const PrivatelyConstruct& dummy, const String& typeNamespace, const String& type, const GUIContent& labelContent, UINT32 labelWidth,
- const String& style, const GUIDimensions& dimensions, bool withLabel)
- :GUIElementContainer(dimensions, style), mLabel(nullptr), mClearButton(nullptr), mDropButton(nullptr), mInstanceId(0), mType(type), mNamespace(typeNamespace)
- {
- mLayout = GUILayoutX::create();
- _registerChildElement(mLayout);
- if(withLabel)
- {
- mLabel = GUILabel::create(labelContent, GUIOptions(GUIOption::fixedWidth(labelWidth)), getSubStyleName(BuiltinEditorResources::ObjectFieldLabelStyleName));
- mLayout->addElement(mLabel);
- }
- mDropButton = GUIDropButton::create((UINT32)DragAndDropType::SceneObject, GUIOptions(GUIOption::flexibleWidth()), getSubStyleName(BuiltinEditorResources::ObjectFieldDropBtnStyleName));
- mClearButton = GUIButton::create(HString(L""), getSubStyleName(BuiltinEditorResources::ObjectFieldClearBtnStyleName));
- mClearButton->onClick.connect(std::bind(&GUIGameObjectField::onClearButtonClicked, this));
- mLayout->addElement(mDropButton);
- mLayout->addElement(mClearButton);
- mDropButton->onDataDropped.connect(std::bind(&GUIGameObjectField::dataDropped, this, _1));
- mDropButton->onClick.connect(std::bind(&GUIGameObjectField::onDropButtonClicked, this));
- }
- GUIGameObjectField::~GUIGameObjectField()
- {
- }
- GUIGameObjectField* GUIGameObjectField::create(const String& typeNamespace, const String& type, const GUIContent& labelContent, UINT32 labelWidth, const GUIOptions& options,
- const String& style)
- {
- const String* curStyle = &style;
- if (*curStyle == StringUtil::BLANK)
- curStyle = &BuiltinEditorResources::ObjectFieldStyleName;
- return bs_new<GUIGameObjectField>(PrivatelyConstruct(), typeNamespace, type, labelContent, labelWidth, *curStyle,
- GUIDimensions::create(options), true);
- }
- GUIGameObjectField* GUIGameObjectField::create(const String& typeNamespace, const String& type, const GUIContent& labelContent, const GUIOptions& options,
- const String& style)
- {
- const String* curStyle = &style;
- if (*curStyle == StringUtil::BLANK)
- curStyle = &BuiltinEditorResources::ObjectFieldStyleName;
- return bs_new<GUIGameObjectField>(PrivatelyConstruct(), typeNamespace, type, labelContent, DEFAULT_LABEL_WIDTH, *curStyle,
- GUIDimensions::create(options), true);
- }
- GUIGameObjectField* GUIGameObjectField::create(const String& typeNamespace, const String& type, const HString& labelText, UINT32 labelWidth, const GUIOptions& options,
- const String& style)
- {
- const String* curStyle = &style;
- if (*curStyle == StringUtil::BLANK)
- curStyle = &BuiltinEditorResources::ObjectFieldStyleName;
- return bs_new<GUIGameObjectField>(PrivatelyConstruct(), typeNamespace, type, GUIContent(labelText), labelWidth, *curStyle,
- GUIDimensions::create(options), true);
- }
- GUIGameObjectField* GUIGameObjectField::create(const String& typeNamespace, const String& type, const HString& labelText, const GUIOptions& options,
- const String& style)
- {
- const String* curStyle = &style;
- if (*curStyle == StringUtil::BLANK)
- curStyle = &BuiltinEditorResources::ObjectFieldStyleName;
- return bs_new<GUIGameObjectField>(PrivatelyConstruct(), typeNamespace, type, GUIContent(labelText), DEFAULT_LABEL_WIDTH, *curStyle,
- GUIDimensions::create(options), true);
- }
- GUIGameObjectField* GUIGameObjectField::create(const String& typeNamespace, const String& type, const GUIOptions& options, const String& style)
- {
- const String* curStyle = &style;
- if (*curStyle == StringUtil::BLANK)
- curStyle = &BuiltinEditorResources::ObjectFieldStyleName;
- return bs_new<GUIGameObjectField>(PrivatelyConstruct(), typeNamespace, type, GUIContent(), 0, *curStyle,
- GUIDimensions::create(options), false);
- }
- GUIGameObjectField* GUIGameObjectField::create(const String& typeNamespace, const String& type, const GUIContent& labelContent, UINT32 labelWidth,
- const String& style)
- {
- const String* curStyle = &style;
- if (*curStyle == StringUtil::BLANK)
- curStyle = &BuiltinEditorResources::ObjectFieldStyleName;
- return bs_new<GUIGameObjectField>(PrivatelyConstruct(), typeNamespace, type, labelContent, labelWidth, *curStyle,
- GUIDimensions::create(), true);
- }
- GUIGameObjectField* GUIGameObjectField::create(const String& typeNamespace, const String& type, const GUIContent& labelContent,
- const String& style)
- {
- const String* curStyle = &style;
- if (*curStyle == StringUtil::BLANK)
- curStyle = &BuiltinEditorResources::ObjectFieldStyleName;
- return bs_new<GUIGameObjectField>(PrivatelyConstruct(), typeNamespace, type, labelContent, DEFAULT_LABEL_WIDTH, *curStyle,
- GUIDimensions::create(), true);
- }
- GUIGameObjectField* GUIGameObjectField::create(const String& typeNamespace, const String& type, const HString& labelText, UINT32 labelWidth,
- const String& style)
- {
- const String* curStyle = &style;
- if (*curStyle == StringUtil::BLANK)
- curStyle = &BuiltinEditorResources::ObjectFieldStyleName;
- return bs_new<GUIGameObjectField>(PrivatelyConstruct(), typeNamespace, type, GUIContent(labelText), labelWidth, *curStyle,
- GUIDimensions::create(), true);
- }
- GUIGameObjectField* GUIGameObjectField::create(const String& typeNamespace, const String& type, const HString& labelText,
- const String& style)
- {
- const String* curStyle = &style;
- if (*curStyle == StringUtil::BLANK)
- curStyle = &BuiltinEditorResources::ObjectFieldStyleName;
- return bs_new<GUIGameObjectField>(PrivatelyConstruct(), typeNamespace, type, GUIContent(labelText), DEFAULT_LABEL_WIDTH, *curStyle,
- GUIDimensions::create(), true);
- }
- GUIGameObjectField* GUIGameObjectField::create(const String& typeNamespace, const String& type, const String& style)
- {
- const String* curStyle = &style;
- if (*curStyle == StringUtil::BLANK)
- curStyle = &BuiltinEditorResources::ObjectFieldStyleName;
- return bs_new<GUIGameObjectField>(PrivatelyConstruct(), typeNamespace, type, GUIContent(), 0, *curStyle,
- GUIDimensions::create(), false);
- }
- HGameObject GUIGameObjectField::getValue() const
- {
- HGameObject obj;
- if(mInstanceId != 0)
- GameObjectManager::instance().tryGetObject(mInstanceId, obj);
- return obj;
- }
- void GUIGameObjectField::setValue(const HGameObject& value)
- {
- setValue(value, false);
- }
- void GUIGameObjectField::setValue(const HGameObject& value, bool triggerEvent)
- {
- if (value)
- {
- if (mInstanceId == value.getInstanceId())
- return;
- mInstanceId = value->getInstanceId();
- mDropButton->setContent(GUIContent(HString(toWString(value->getName()) + L" (" + toWString(mType) + L")")));
- }
- else
- {
- if (mInstanceId == 0)
- return;
- mInstanceId = 0;
- mDropButton->setContent(GUIContent(HString(L"None (" + toWString(mType) + L")")));
- }
- if (triggerEvent)
- onValueChanged(value);
- }
- void GUIGameObjectField::setTint(const Color& color)
- {
- if (mLabel != nullptr)
- mLabel->setTint(color);
- mDropButton->setTint(color);
- mClearButton->setTint(color);
- }
- void GUIGameObjectField::_updateLayoutInternal(const GUILayoutData& data)
- {
- mLayout->_setLayoutData(data);
- mLayout->_updateLayoutInternal(data);
- }
- Vector2I GUIGameObjectField::_getOptimalSize() const
- {
- return mLayout->_getOptimalSize();
- }
- void GUIGameObjectField::onDropButtonClicked()
- {
- if (mInstanceId == 0)
- return;
- HGameObject go;
- if (GameObjectManager::instance().tryGetObject(mInstanceId, go))
- {
- HSceneObject so;
- if (rtti_is_of_type<SceneObject>(go.get()))
- {
- so = static_object_cast<SceneObject>(go);
- }
- else if(rtti_is_subclass<Component>(go.get()))
- {
- HComponent component = static_object_cast<Component>(go);
- so = component->SO();
- }
- Selection::instance().ping(so);
- }
- }
- void GUIGameObjectField::dataDropped(void* data)
- {
- DraggedSceneObjects* draggedSceneObjects = reinterpret_cast<DraggedSceneObjects*>(data);
- if (draggedSceneObjects->numObjects <= 0)
- return;
- MonoClass* sceneObjectClass = ScriptAssemblyManager::instance().getSceneObjectClass();
- if (mType == sceneObjectClass->getFullName()) // A scene object
- {
- setValue(draggedSceneObjects->objects[0], true);
- }
- else // A component
- {
- for (UINT32 i = 0; i < draggedSceneObjects->numObjects; i++)
- {
- HSceneObject so = draggedSceneObjects->objects[i];
- const Vector<HComponent>& components = so->getComponents();
- for (auto& component : components)
- {
- if (component->getTypeId() == TID_ManagedComponent) // We only care about managed components
- {
- HManagedComponent managedComponent = static_object_cast<ManagedComponent>(component);
- MonoClass* acceptedClass = MonoManager::instance().findClass(mNamespace, mType);
- MonoClass* providedClass = MonoManager::instance().findClass(managedComponent->getManagedNamespace(), managedComponent->getManagedTypeName());
- if (acceptedClass != nullptr && providedClass != nullptr)
- {
- if (providedClass->isSubClassOf(acceptedClass))
- {
- setValue(managedComponent, true);
- }
- }
- }
- }
- }
- }
- }
- void GUIGameObjectField::styleUpdated()
- {
- if (mLabel != nullptr)
- mLabel->setStyle(getSubStyleName(BuiltinEditorResources::ObjectFieldLabelStyleName));
- mDropButton->setStyle(getSubStyleName(BuiltinEditorResources::ObjectFieldDropBtnStyleName));
- mClearButton->setStyle(getSubStyleName(BuiltinEditorResources::ObjectFieldClearBtnStyleName));
- }
- void GUIGameObjectField::onClearButtonClicked()
- {
- setValue(HGameObject(), true);
- }
- const String& GUIGameObjectField::getGUITypeName()
- {
- static String typeName = "GUIGameObjectField";
- return typeName;
- }
- }
|