/* * All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or * its licensors. * * For complete copyright and license terms please see the LICENSE at the root of this * distribution (the "License"). All use of this software is governed by the License, * or, if provided, by the license below or the license accompanying this file. Do not * remove or modify any license notices. This file is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * */ #include "stdafx.h" #include "EditorCommon.h" #include "PropertyHandlerEntityIdComboBox.h" #include #include void PropertyHandlerEntityIdComboBox::WriteGUIValuesIntoProperty(size_t index, PropertyEntityIdComboBoxCtrl* GUI, property_t& instance, AzToolsFramework::InstanceDataNode* node) { Q_UNUSED(index); Q_UNUSED(node); instance = GUI->value(); } bool PropertyHandlerEntityIdComboBox::ReadValuesIntoGUI(size_t index, PropertyEntityIdComboBoxCtrl* GUI, const property_t& instance, AzToolsFramework::InstanceDataNode* node) { Q_UNUSED(index); Q_UNUSED(node); GUI->setValue(instance); return false; } QWidget* PropertyHandlerEntityIdComboBox::CreateGUI(QWidget* pParent) { PropertyEntityIdComboBoxCtrl* newCtrl = aznew PropertyEntityIdComboBoxCtrl(pParent); QObject::connect(newCtrl, &PropertyEntityIdComboBoxCtrl::valueChanged, this, [newCtrl]() { EBUS_EVENT(AzToolsFramework::PropertyEditorGUIMessages::Bus, RequestWrite, newCtrl); }); return newCtrl; } void PropertyHandlerEntityIdComboBox::ConsumeAttribute(PropertyEntityIdComboBoxCtrl* GUI, AZ::u32 attrib, AzToolsFramework::PropertyAttributeReader* attrValue, const char* debugName) { (void)debugName; if (attrib == AZ_CRC("EnumValue", 0xe4f32eed)) { AZStd::pair guiEnumValue; AZStd::pair enumValue; AZ::Edit::EnumConstant enumConstant; if (attrValue->Read>(enumConstant)) { guiEnumValue.first = enumConstant.m_value; guiEnumValue.second = enumConstant.m_description; GUI->addEnumValue(guiEnumValue); } else { // Legacy path. Support temporarily for compatibility. if (attrValue->Read< AZStd::pair >(enumValue)) { guiEnumValue = static_cast>(enumValue); GUI->addEnumValue(guiEnumValue); } else { AZStd::pair enumValueConst; if (attrValue->Read< AZStd::pair >(enumValueConst)) { AZStd::string consted = enumValueConst.second; guiEnumValue = AZStd::make_pair(enumValueConst.first, consted); GUI->addEnumValue(guiEnumValue); } else { AZ_WarningOnce("AzToolsFramework", false, "Failed to read 'EnumValue' attribute from property '%s' into enum combo box. Expected pair or pair, where IntegerType is int or u32 ", debugName); } } } } else if (attrib == AZ::Edit::Attributes::EnumValues) { AZStd::vector> guiEnumValues; AZStd::vector > enumValues; AZStd::vector> enumConstantValues; if (attrValue->Read>>(enumConstantValues)) { for (const AZ::Edit::EnumConstant& constantValue : enumConstantValues) { guiEnumValues.push_back(); auto& enumValue = guiEnumValues.back(); enumValue.first = constantValue.m_value; enumValue.second = constantValue.m_description; } GUI->addEnumValues(guiEnumValues); } else { // Legacy path. Support temporarily for compatibility. if (attrValue->Read< AZStd::vector > >(enumValues)) { for (auto it = enumValues.begin(); it != enumValues.end(); ++it) guiEnumValues.push_back(static_cast>(*it)); GUI->addEnumValues(guiEnumValues); } else { AZStd::vector > attempt2; if (attrValue->Read > >(attempt2)) { for (auto it = attempt2.begin(); it != attempt2.end(); ++it) guiEnumValues.push_back(AZStd::make_pair(it->first, it->second)); GUI->addEnumValues(guiEnumValues); } else { // emit a warning! AZ_WarningOnce("AzToolsFramework", false, "Failed to read 'EnumValue' attribute from property '%s' into enum combo box", debugName); } } } } } void PropertyHandlerEntityIdComboBox::Register() { EBUS_EVENT(AzToolsFramework::PropertyTypeRegistrationMessages::Bus, RegisterPropertyType, aznew PropertyHandlerEntityIdComboBox()); } PropertyEntityIdComboBoxCtrl::PropertyEntityIdComboBoxCtrl(QWidget* pParent) : QWidget(pParent) { // create the gui, it consists of a layout, and in that layout, a text field for the value // and then a slider for the value. QHBoxLayout* pLayout = new QHBoxLayout(this); m_pComboBox = aznew AzToolsFramework::DHQComboBox(this); // many UI elements hide 1 pixel of their size in a border area that only shows up when they are selected. // The combo box used in this layout does not do this, so adding 1 to the left and right margins will make // sure that it has the same dimensions as the other UI elements when they are unselected. pLayout->setSpacing(4); pLayout->setContentsMargins(1, 0, 1, 0); pLayout->addWidget(m_pComboBox); m_pComboBox->setMinimumWidth(AzToolsFramework::PropertyQTConstant_MinimumWidth); m_pComboBox->setFixedHeight(AzToolsFramework::PropertyQTConstant_DefaultHeight); m_pComboBox->setFocusPolicy(Qt::StrongFocus); setLayout(pLayout); setFocusProxy(m_pComboBox); setFocusPolicy(m_pComboBox->focusPolicy()); connect(m_pComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(onChildComboBoxValueChange(int))); }; void PropertyEntityIdComboBoxCtrl::setValue(AZ::EntityId value) { m_pComboBox->blockSignals(true); bool indexWasFound = false; for (size_t enumValIndex = 0; enumValIndex < m_enumValues.size(); enumValIndex++) { if (m_enumValues[enumValIndex].first == value) { m_pComboBox->setCurrentIndex(static_cast(enumValIndex)); indexWasFound = true; break; } } AZ_Warning("AzToolsFramework", indexWasFound == true, "No index in property enum for value %d", value); m_pComboBox->blockSignals(false); } void PropertyEntityIdComboBoxCtrl::addEnumValue(AZStd::pair& val) { m_pComboBox->blockSignals(true); m_enumValues.push_back(val); m_pComboBox->addItem(val.second.c_str()); m_pComboBox->blockSignals(false); } void PropertyEntityIdComboBoxCtrl::addEnumValues(AZStd::vector< AZStd::pair >& vals) { m_pComboBox->blockSignals(true); for (size_t valIndex = 0; valIndex < vals.size(); valIndex++) { m_enumValues.push_back(vals[valIndex]); m_pComboBox->addItem(vals[valIndex].second.c_str()); } m_pComboBox->blockSignals(false); } AZ::EntityId PropertyEntityIdComboBoxCtrl::value() const { AZ_Assert(m_pComboBox->currentIndex() >= 0 && m_pComboBox->currentIndex() < static_cast(m_enumValues.size()), "Out of range combo box index %d", m_pComboBox->currentIndex()); return m_enumValues[m_pComboBox->currentIndex()].first; } void PropertyEntityIdComboBoxCtrl::onChildComboBoxValueChange(int comboBoxIndex) { AZ_Assert(comboBoxIndex >= 0 && comboBoxIndex < static_cast(m_enumValues.size()), "Out of range combo box index %d", comboBoxIndex); emit valueChanged(m_enumValues[comboBoxIndex].first); } QWidget* PropertyEntityIdComboBoxCtrl::GetFirstInTabOrder() { return m_pComboBox; } QWidget* PropertyEntityIdComboBoxCtrl::GetLastInTabOrder() { return m_pComboBox; } void PropertyEntityIdComboBoxCtrl::UpdateTabOrder() { // There's only one QT widget on this property. } #include