InterpreterWidget.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <QMessageBox>
  9. #include <AzToolsFramework/API/ToolsApplicationAPI.h>
  10. #include <UI/PropertyEditor/ReflectedPropertyEditor.hxx>
  11. #include <Editor/View/Windows/Tools/InterpreterWidget/InterpreterWidget.h>
  12. #include <Editor/View/Windows/Tools/InterpreterWidget/ui_InterpreterWidget.h>
  13. namespace InterpreterWidgetCpp
  14. {
  15. using namespace ScriptCanvasEditor;
  16. /// converts the interpreter status to a AZStd::pair<boo, bool> : first = Start button enabled, second = Stop button enabled
  17. AZStd::pair<bool, bool> ToStartStopButtonEnabled(InterpreterStatus status)
  18. {
  19. switch (status)
  20. {
  21. case InterpreterStatus::Ready:
  22. case InterpreterStatus::Stopped:
  23. return { true, false };
  24. case InterpreterStatus::Running:
  25. return { false, true };
  26. case InterpreterStatus::Waiting:
  27. case InterpreterStatus::Misconfigured:
  28. case InterpreterStatus::Incompatible:
  29. case InterpreterStatus::Configured:
  30. case InterpreterStatus::Pending:
  31. default:
  32. return { false, false };
  33. }
  34. }
  35. }
  36. namespace ScriptCanvasEditor
  37. {
  38. InterpreterWidget::InterpreterWidget(QWidget* parent)
  39. : AzQtComponents::StyledDialog(parent)
  40. , m_view(new Ui::InterpreterWidget())
  41. {
  42. m_view->setupUi(this);
  43. AZ::SerializeContext* serializeContext = nullptr;
  44. AZ::ComponentApplicationBus::BroadcastResult(serializeContext, &AZ::ComponentApplicationRequests::GetSerializeContext);
  45. AZ_Assert(serializeContext, "InterpreterWidget::InterpreterWidget Failed to retrieve serialize context.");
  46. auto propertyEditor = new AzToolsFramework::ReflectedPropertyEditor(this);
  47. propertyEditor->setObjectName("InterpreterWidget::ReflectedPropertyEditor");
  48. propertyEditor->Setup(serializeContext, this, true, 250);
  49. propertyEditor->show();
  50. propertyEditor->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  51. propertyEditor->AddInstance(&m_interpreter, azrtti_typeid<Interpreter>());
  52. propertyEditor->InvalidateAll();
  53. propertyEditor->ExpandAll();
  54. m_view->interpreterLayout->insertWidget(0, propertyEditor, 0);
  55. connect(m_view->buttonStart, &QPushButton::pressed, this, &InterpreterWidget::OnButtonStartPressed);
  56. connect(m_view->buttonStop, &QPushButton::pressed, this, &InterpreterWidget::OnButtonStopPressed);
  57. m_onIterpreterStatusChanged = m_interpreter.ConnectOnStatusChanged
  58. ([this](const Interpreter& interpreter)
  59. {
  60. OnInterpreterStatusChanged(interpreter);
  61. });
  62. m_handlerSourceCompiled = m_interpreter.GetConfiguration().ConnectToSourceCompiled
  63. ([propertyEditor](const Configuration&)
  64. {
  65. propertyEditor->QueueInvalidation(AzToolsFramework::Refresh_EntireTree);
  66. });
  67. // initialized status window and enabled setting for buttons
  68. OnInterpreterStatusChanged(m_interpreter);
  69. }
  70. InterpreterWidget::~InterpreterWidget()
  71. {
  72. delete m_view;
  73. }
  74. void InterpreterWidget::OnButtonStartPressed()
  75. {
  76. m_interpreter.Execute();
  77. }
  78. void InterpreterWidget::OnButtonStopPressed()
  79. {
  80. m_interpreter.Stop();
  81. }
  82. void InterpreterWidget::OnInterpreterStatusChanged(const Interpreter& interpreter)
  83. {
  84. using namespace InterpreterWidgetCpp;
  85. const auto status = interpreter.GetStatus();
  86. const auto startStopButtonEnabled = ToStartStopButtonEnabled(status);
  87. m_view->buttonStart->setEnabled(startStopButtonEnabled.first);
  88. m_view->buttonStop->setEnabled(startStopButtonEnabled.second);
  89. if (status == InterpreterStatus::Incompatible)
  90. {
  91. constexpr const char* message =
  92. "The selected script is written to be used with the ScriptCanvas Component attached to an Entity. "
  93. "It will not work in another context. Any script that refers to 'Self', that is the Entity that owns the component, "
  94. "will not not operate correctly here.";
  95. QMessageBox::critical(this, QObject::tr("Entity Script Not Allowed"), QObject::tr(message));
  96. }
  97. const auto statusString = interpreter.GetStatusString();
  98. m_view->interpreterStatus->setText(AZStd::string::format("%.*s", AZ_STRING_ARG(statusString)).c_str());
  99. }
  100. void InterpreterWidget::Reflect(AZ::ReflectContext* context)
  101. {
  102. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  103. {
  104. serializeContext->Class<Interpreter>()
  105. ->Field("interpreter", &InterpreterWidget::m_interpreter)
  106. ;
  107. if (auto editContext = serializeContext->GetEditContext())
  108. {
  109. editContext->Class<InterpreterWidget>("Script Canvas Interpreter Widget", "A Widget for a ScriptCanvas Interpreted")
  110. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  111. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  112. ->Attribute(AZ::Edit::Attributes::Visibility, AZ::Edit::PropertyVisibility::ShowChildrenOnly)
  113. ->DataElement(AZ::Edit::UIHandlers::Default, &InterpreterWidget::m_interpreter, "Interpreter", "Interpreter")
  114. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  115. ->Attribute(AZ::Edit::Attributes::Visibility, AZ::Edit::PropertyVisibility::ShowChildrenOnly)
  116. ;
  117. }
  118. }
  119. }
  120. }