/* * 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. * */ #pragma once #include #include #include #include #include #include #include #include #include #include #include namespace EditorPythonBindings { //! An abstract to marshal between Behavior and Python type values class PythonMarshalTypeRequests : public AZ::EBusTraits { public: ////////////////////////////////////////////////////////////////////////// // EBusTraits overrides static const AZ::EBusHandlerPolicy HandlerPolicy = AZ::EBusHandlerPolicy::Multiple; static const AZ::EBusAddressPolicy AddressPolicy = AZ::EBusAddressPolicy::ById; using BusIdType = AZ::TypeId; ////////////////////////////////////////////////////////////////////////// using DeallocateFunction = Convert::VariableDeleter; using BehaviorTraits = AZ::BehaviorParameter::Traits; //! Marshals a Python value to a BehaviorValueParameter plus an optional function to deallocate it after usage //! @return returns a pair of a flag to indicate success and an function to deallocate using BehaviorValueResult = AZStd::pair; virtual AZStd::optional PythonToBehaviorValueParameter(BehaviorTraits traits, pybind11::object pyObj, AZ::BehaviorValueParameter& outValue) = 0; //! Marshals a BehaviorValueParameter to a Python value object //! @return returns a pair of a valid Python object and an optional function to deallocate after sent to Python using PythonValueResult = AZStd::pair; virtual AZStd::optional BehaviorValueParameterToPython(AZ::BehaviorValueParameter& behaviorValue) = 0; //! Validates that a particular Python object can convert into a Behavior value parameter type virtual bool CanConvertPythonToBehaviorValue(BehaviorTraits traits, pybind11::object pyObj) const = 0; }; using PythonMarshalTypeRequestBus = AZ::EBus; //! Handles marshaling of built-in Behavior types like numbers, strings, and lists class PythonMarshalComponent : public AZ::Component , protected PythonMarshalTypeRequestBus::MultiHandler { public: AZ_COMPONENT(PythonMarshalComponent, PythonMarshalComponentTypeId, AZ::Component); static void Reflect(AZ::ReflectContext* context); static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided); static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible); static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required); class TypeConverter { public: virtual AZStd::optional PythonToBehaviorValueParameter(PythonMarshalTypeRequests::BehaviorTraits traits, pybind11::object pyObj, AZ::BehaviorValueParameter& outValue) = 0; virtual AZStd::optional BehaviorValueParameterToPython(AZ::BehaviorValueParameter& behaviorValue) = 0; virtual bool CanConvertPythonToBehaviorValue(BehaviorTraits traits, pybind11::object pyObj) const = 0; }; using TypeConverterPointer = AZStd::shared_ptr; void RegisterTypeConverter(const AZ::TypeId& typeId, TypeConverterPointer typeConverterPointer); protected: //////////////////////////////////////////////////////////////////////// // AZ::Component interface implementation void Activate() override; void Deactivate() override; //////////////////////////////////////////////////////////////////////// // PythonMarshalTypeRequestBus interface implementation AZStd::optional PythonToBehaviorValueParameter(PythonMarshalTypeRequests::BehaviorTraits traits, pybind11::object pyObj, AZ::BehaviorValueParameter& outValue) override; AZStd::optional BehaviorValueParameterToPython(AZ::BehaviorValueParameter& behaviorValue) override; bool CanConvertPythonToBehaviorValue(BehaviorTraits traits, pybind11::object pyObj) const override; private: using TypeConverterMap = AZStd::unordered_map; TypeConverterMap m_typeConverterMap; }; }