Skip to content

Commit

Permalink
Suppport for method tooltips
Browse files Browse the repository at this point in the history
Signed-off-by: Artur Kamieniecki <[email protected]>
  • Loading branch information
arturkamieniecki committed Mar 8, 2024
1 parent 45693f9 commit c68ade6
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,18 @@ namespace EditorPythonBindings

//! Creates a string with class or global method definition and documentation.
AZStd::string MethodDefinition(
const AZStd::string methodName, const AZ::BehaviorMethod& behaviorMethod, const AZ::BehaviorClass* behaviorClass = nullptr);
const AZStd::string methodName,
const AZ::BehaviorMethod& behaviorMethod,
const AZ::BehaviorClass* behaviorClass = nullptr,
bool defineTooltip = false);

//! Creates a string with class definition and documentation.
AZStd::string ClassDefinition(
const AZ::BehaviorClass* behaviorClass,
const AZStd::string className,
bool defineProperties = true,
bool defineMethods = true);
bool defineMethods = true,
bool defineTooltip = false);

//! Creates a property definition
AZStd::string PropertyDefinition(
Expand Down
41 changes: 38 additions & 3 deletions Gems/EditorPythonBindings/Code/Source/PythonUtility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*
*/

#include "AzCore/Script/ScriptContextAttributes.h"
#include <EditorPythonBindings/PythonUtility.h>
#include <Source/PythonMarshalComponent.h>
#include <Source/PythonProxyObject.h>
Expand Down Expand Up @@ -779,6 +780,23 @@ namespace EditorPythonBindings
{
namespace Internal
{
AZStd::string ReadStringAttribute(const AZ::AttributeArray& attributes, const AZ::Crc32& attribute)
{
AZStd::string attributeValue = "";
if (auto attributeItem = azrtti_cast<AZ::AttributeData<AZStd::string>*>(AZ::FindAttribute(attribute, attributes)))
{
attributeValue = attributeItem->Get(nullptr);
return attributeValue;
}

if (auto attributeItem = azrtti_cast<AZ::AttributeData<const char*>*>(AZ::FindAttribute(attribute, attributes)))
{
attributeValue = attributeItem->Get(nullptr);
return attributeValue;
}
return {};
}

AZStd::string TypeNameFallback(const AZ::TypeId& typeId)
{
// fall back to class data m_name
Expand Down Expand Up @@ -1083,7 +1101,10 @@ namespace EditorPythonBindings

//! Creates a string with class or global method definition and documentation.
AZStd::string PythonBehaviorDescription::MethodDefinition(
const AZStd::string methodName, const AZ::BehaviorMethod& behaviorMethod, const AZ::BehaviorClass* behaviorClass)
const AZStd::string methodName,
const AZ::BehaviorMethod& behaviorMethod,
const AZ::BehaviorClass* behaviorClass,
bool defineTooltip)
{
AZStd::string buffer;
AZStd::vector<AZStd::string> pythonArgs;
Expand Down Expand Up @@ -1138,13 +1159,27 @@ namespace EditorPythonBindings
AZStd::string argsList;
AzFramework::StringFunc::Join(buffer, pythonArgs.begin(), pythonArgs.end(), ",");
AzFramework::StringFunc::Append(buffer, ") -> None:\n");

if (defineTooltip)
{
AZStd::string methodTooltip = Internal::ReadStringAttribute(behaviorMethod.m_attributes, AZ::Script::Attributes::ToolTip);
if (!methodTooltip.empty())
{
Internal::AddCommentBlock(indentLevel + 1, methodTooltip, buffer);
}
}

Internal::Indent(indentLevel + 1, buffer);
AzFramework::StringFunc::Append(buffer, "pass\n\n");
return buffer;
}

AZStd::string PythonBehaviorDescription::ClassDefinition(
const AZ::BehaviorClass* behaviorClass, const AZStd::string className, bool defineProperties, bool defineMethods)
const AZ::BehaviorClass* behaviorClass,
const AZStd::string className,
bool defineProperties,
bool defineMethods,
bool defineTooltip)
{
AZStd::string buffer;
AzFramework::StringFunc::Append(buffer, "class ");
Expand Down Expand Up @@ -1197,7 +1232,7 @@ namespace EditorPythonBindings
{
AZStd::string baseMethodName{ methodEntry.first };
Scope::FetchScriptName(method->m_attributes, baseMethodName);
AZStd::string methodDef = MethodDefinition(baseMethodName, *method, behaviorClass);
AZStd::string methodDef = MethodDefinition(baseMethodName, *method, behaviorClass, defineTooltip);
AzFramework::StringFunc::Append(buffer, methodDef.c_str());
}
}
Expand Down

0 comments on commit c68ade6

Please sign in to comment.