2
0

PythonActionManagerHandler.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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 <ActionManager/PythonActionManagerHandler.h>
  9. #include <AzToolsFramework/ActionManager/Action/ActionManagerInterface.h>
  10. #include <AzToolsFramework/ActionManager/ToolBar/ToolBarManagerInterface.h>
  11. #include <EditorPythonBindings/PythonCommon.h>
  12. #include <pybind11/pybind11.h>
  13. namespace EditorPythonBindings
  14. {
  15. PythonActionManagerHandler::PythonActionManagerHandler()
  16. {
  17. m_actionManagerInterface = AZ::Interface<AzToolsFramework::ActionManagerInterface>::Get();
  18. if (m_actionManagerInterface)
  19. {
  20. EditorPythonBindings::CustomTypeBindingNotificationBus::Handler::BusConnect(azrtti_typeid<PythonEditorAction>());
  21. ActionManagerRequestBus::Handler::BusConnect();
  22. }
  23. m_menuManagerInterface = AZ::Interface<AzToolsFramework::MenuManagerInterface>::Get();
  24. if (m_menuManagerInterface)
  25. {
  26. MenuManagerRequestBus::Handler::BusConnect();
  27. }
  28. m_toolBarManagerInterface = AZ::Interface<AzToolsFramework::ToolBarManagerInterface>::Get();
  29. if (m_toolBarManagerInterface)
  30. {
  31. ToolBarManagerRequestBus::Handler::BusConnect();
  32. }
  33. }
  34. PythonActionManagerHandler::~PythonActionManagerHandler()
  35. {
  36. if (m_actionManagerInterface)
  37. {
  38. ActionManagerRequestBus::Handler::BusDisconnect();
  39. EditorPythonBindings::CustomTypeBindingNotificationBus::Handler::BusDisconnect();
  40. }
  41. if (m_menuManagerInterface)
  42. {
  43. MenuManagerRequestBus::Handler::BusDisconnect();
  44. }
  45. if (m_toolBarManagerInterface)
  46. {
  47. ToolBarManagerRequestBus::Handler::BusDisconnect();
  48. }
  49. }
  50. void PythonActionManagerHandler::Reflect(AZ::ReflectContext* context)
  51. {
  52. if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  53. {
  54. behaviorContext
  55. ->Class<AzToolsFramework::ActionProperties>("ActionProperties")
  56. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation)
  57. ->Attribute(AZ::Script::Attributes::Category, "Action")
  58. ->Attribute(AZ::Script::Attributes::Module, "action")
  59. ->Property("name", BehaviorValueProperty(&AzToolsFramework::ActionProperties::m_name))
  60. ->Property("description", BehaviorValueProperty(&AzToolsFramework::ActionProperties::m_description))
  61. ->Property("category", BehaviorValueProperty(&AzToolsFramework::ActionProperties::m_category))
  62. ->Property("iconPath", BehaviorValueProperty(&AzToolsFramework::ActionProperties::m_iconPath))
  63. ;
  64. behaviorContext
  65. ->EBus<ActionManagerRequestBus>("ActionManagerPythonRequestBus")
  66. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation)
  67. ->Attribute(AZ::Script::Attributes::Category, "Action")
  68. ->Attribute(AZ::Script::Attributes::Module, "action")
  69. ->Event("RegisterAction", &ActionManagerRequestBus::Handler::RegisterAction)
  70. ->Event("RegisterCheckableAction", &ActionManagerRequestBus::Handler::RegisterCheckableAction)
  71. ->Event("TriggerAction", &ActionManagerRequestBus::Handler::TriggerAction)
  72. ->Event("UpdateAction", &ActionManagerRequestBus::Handler::UpdateAction)
  73. ;
  74. behaviorContext->Class<AzToolsFramework::MenuProperties>("MenuProperties")
  75. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation)
  76. ->Attribute(AZ::Script::Attributes::Category, "Action")
  77. ->Attribute(AZ::Script::Attributes::Module, "action")
  78. ->Property("name", BehaviorValueProperty(&AzToolsFramework::MenuProperties::m_name))
  79. ;
  80. behaviorContext
  81. ->EBus<MenuManagerRequestBus>("MenuManagerPythonRequestBus")
  82. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation)
  83. ->Attribute(AZ::Script::Attributes::Category, "Action")
  84. ->Attribute(AZ::Script::Attributes::Module, "action")
  85. ->Event("RegisterMenu", &MenuManagerRequestBus::Handler::RegisterMenu)
  86. ->Event("AddActionToMenu", &MenuManagerRequestBus::Handler::AddActionToMenu)
  87. ->Event("AddSeparatorToMenu", &MenuManagerRequestBus::Handler::AddSeparatorToMenu)
  88. ->Event("AddSubMenuToMenu", &MenuManagerRequestBus::Handler::AddSubMenuToMenu)
  89. ->Event("AddWidgetToMenu", &MenuManagerRequestBus::Handler::AddWidgetToMenu)
  90. ->Event("AddMenuToMenuBar", &MenuManagerRequestBus::Handler::AddMenuToMenuBar)
  91. ;
  92. behaviorContext->Class<AzToolsFramework::ToolBarProperties>("ToolBarProperties")
  93. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation)
  94. ->Attribute(AZ::Script::Attributes::Category, "Action")
  95. ->Attribute(AZ::Script::Attributes::Module, "action")
  96. ->Property("name", BehaviorValueProperty(&AzToolsFramework::ToolBarProperties::m_name))
  97. ;
  98. behaviorContext
  99. ->EBus<ToolBarManagerRequestBus>("ToolBarManagerPythonRequestBus")
  100. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation)
  101. ->Attribute(AZ::Script::Attributes::Category, "Action")
  102. ->Attribute(AZ::Script::Attributes::Module, "action")
  103. ->Event("RegisterToolBar", &ToolBarManagerRequestBus::Handler::RegisterToolBar)
  104. ->Event("AddActionToToolBar", &ToolBarManagerRequestBus::Handler::AddActionToToolBar)
  105. ->Event("AddActionWithSubMenuToToolBar", &ToolBarManagerRequestBus::Handler::AddActionWithSubMenuToToolBar)
  106. ->Event("AddActionsToToolBar", &ToolBarManagerRequestBus::Handler::AddActionsToToolBar)
  107. ->Event("RemoveActionFromToolBar", &ToolBarManagerRequestBus::Handler::RemoveActionFromToolBar)
  108. ->Event("RemoveActionsFromToolBar", &ToolBarManagerRequestBus::Handler::RemoveActionsFromToolBar)
  109. ->Event("AddSeparatorToToolBar", &ToolBarManagerRequestBus::Handler::AddSeparatorToToolBar)
  110. ->Event("AddWidgetToToolBar", &ToolBarManagerRequestBus::Handler::AddWidgetToToolBar)
  111. ->Event("GetSortKeyOfActionInToolBar", &ToolBarManagerRequestBus::Handler::GetSortKeyOfActionInToolBar)
  112. ->Event("GetSortKeyOfWidgetInToolBar", &ToolBarManagerRequestBus::Handler::GetSortKeyOfWidgetInToolBar)
  113. ;
  114. }
  115. }
  116. AzToolsFramework::ActionManagerOperationResult PythonActionManagerHandler::RegisterAction(
  117. const AZStd::string& contextIdentifier,
  118. const AZStd::string& actionIdentifier,
  119. const AzToolsFramework::ActionProperties& properties,
  120. PythonEditorAction handler)
  121. {
  122. auto outcome = m_actionManagerInterface->RegisterAction(
  123. contextIdentifier,
  124. actionIdentifier,
  125. properties,
  126. [h = AZStd::move(handler)]() mutable
  127. {
  128. PyObject_CallObject(h.GetPyObject(), nullptr);
  129. }
  130. );
  131. if (outcome.IsSuccess())
  132. {
  133. // Store the callable to handle reference counting correctly.
  134. m_actionHandlerMap.insert({ actionIdentifier, PythonFunctionObject(handler.GetPyObject()) });
  135. }
  136. return outcome;
  137. }
  138. AzToolsFramework::ActionManagerOperationResult PythonActionManagerHandler::RegisterCheckableAction(
  139. const AZStd::string& contextIdentifier,
  140. const AZStd::string& actionIdentifier,
  141. const AzToolsFramework::ActionProperties& properties,
  142. PythonEditorAction handler,
  143. PythonEditorAction updateCallback)
  144. {
  145. PyObject* handlerObject = handler.GetPyObject();
  146. PyObject* updateCallbackObject = updateCallback.GetPyObject();
  147. auto outcome = m_actionManagerInterface->RegisterCheckableAction(
  148. contextIdentifier,
  149. actionIdentifier,
  150. properties,
  151. [h = AZStd::move(handler)]() mutable
  152. {
  153. PyObject_CallObject(h.GetPyObject(), nullptr);
  154. },
  155. [u = AZStd::move(updateCallback)]() mutable -> bool
  156. {
  157. return PyObject_IsTrue(PyObject_CallObject(u.GetPyObject(), nullptr));
  158. }
  159. );
  160. if (outcome.IsSuccess())
  161. {
  162. // Store the callable to handle reference counting correctly.
  163. m_actionHandlerMap.insert({ actionIdentifier, PythonFunctionObject(handlerObject) });
  164. m_actionUpdateCallbackMap.insert({ actionIdentifier, PythonFunctionObject(updateCallbackObject) });
  165. }
  166. return outcome;
  167. }
  168. AzToolsFramework::ActionManagerOperationResult PythonActionManagerHandler::TriggerAction(const AZStd::string& actionIdentifier)
  169. {
  170. return m_actionManagerInterface->TriggerAction(actionIdentifier);
  171. }
  172. AzToolsFramework::ActionManagerOperationResult PythonActionManagerHandler::UpdateAction(const AZStd::string& actionIdentifier)
  173. {
  174. return m_actionManagerInterface->UpdateAction(actionIdentifier);
  175. }
  176. AzToolsFramework::MenuManagerOperationResult PythonActionManagerHandler::RegisterMenu(
  177. const AZStd::string& identifier, const AzToolsFramework::MenuProperties& properties)
  178. {
  179. return m_menuManagerInterface->RegisterMenu(identifier, properties);
  180. }
  181. AzToolsFramework::MenuManagerOperationResult PythonActionManagerHandler::AddActionToMenu(
  182. const AZStd::string& menuIdentifier, const AZStd::string& actionIdentifier, int sortIndex)
  183. {
  184. return m_menuManagerInterface->AddActionToMenu(menuIdentifier, actionIdentifier, sortIndex);
  185. }
  186. AzToolsFramework::MenuManagerOperationResult PythonActionManagerHandler::AddSeparatorToMenu(
  187. const AZStd::string& menuIdentifier, int sortIndex)
  188. {
  189. return m_menuManagerInterface->AddSeparatorToMenu(menuIdentifier, sortIndex);
  190. }
  191. AzToolsFramework::MenuManagerOperationResult PythonActionManagerHandler::AddSubMenuToMenu(
  192. const AZStd::string& menuIdentifier, const AZStd::string& subMenuIdentifier, int sortIndex)
  193. {
  194. return m_menuManagerInterface->AddSubMenuToMenu(menuIdentifier, subMenuIdentifier, sortIndex);
  195. }
  196. AzToolsFramework::MenuManagerOperationResult PythonActionManagerHandler::AddWidgetToMenu(
  197. const AZStd::string& menuIdentifier, const AZStd::string& widgetActionIdentifier, int sortIndex)
  198. {
  199. return m_menuManagerInterface->AddWidgetToMenu(menuIdentifier, widgetActionIdentifier, sortIndex);
  200. }
  201. AzToolsFramework::MenuManagerOperationResult PythonActionManagerHandler::AddMenuToMenuBar(
  202. const AZStd::string& menuBarIdentifier, const AZStd::string& menuIdentifier, int sortIndex)
  203. {
  204. return m_menuManagerInterface->AddMenuToMenuBar(menuBarIdentifier, menuIdentifier, sortIndex);
  205. }
  206. AzToolsFramework::ToolBarManagerOperationResult PythonActionManagerHandler::RegisterToolBar(
  207. const AZStd::string& toolBarIdentifier, const AzToolsFramework::ToolBarProperties& properties)
  208. {
  209. return m_toolBarManagerInterface->RegisterToolBar(toolBarIdentifier, properties);
  210. }
  211. AzToolsFramework::ToolBarManagerOperationResult PythonActionManagerHandler::RegisterToolBarArea(
  212. const AZStd::string& toolBarAreaIdentifier, QMainWindow* mainWindow, Qt::ToolBarArea toolBarArea)
  213. {
  214. return m_toolBarManagerInterface->RegisterToolBarArea(toolBarAreaIdentifier, mainWindow, toolBarArea);
  215. }
  216. AzToolsFramework::ToolBarManagerOperationResult PythonActionManagerHandler::AddActionToToolBar(
  217. const AZStd::string& toolBarIdentifier, const AZStd::string& actionIdentifier, int sortIndex)
  218. {
  219. return m_toolBarManagerInterface->AddActionToToolBar(toolBarIdentifier, actionIdentifier, sortIndex);
  220. }
  221. AzToolsFramework::ToolBarManagerOperationResult PythonActionManagerHandler::AddActionWithSubMenuToToolBar(
  222. const AZStd::string& toolBarIdentifier,
  223. const AZStd::string& actionIdentifier,
  224. const AZStd::string& subMenuIdentifier,
  225. int sortIndex)
  226. {
  227. return m_toolBarManagerInterface->AddActionWithSubMenuToToolBar(toolBarIdentifier, actionIdentifier, subMenuIdentifier, sortIndex);
  228. }
  229. AzToolsFramework::ToolBarManagerOperationResult PythonActionManagerHandler::AddActionsToToolBar(
  230. const AZStd::string& toolBarIdentifier, const AZStd::vector<AZStd::pair<AZStd::string, int>>& actions)
  231. {
  232. return m_toolBarManagerInterface->AddActionsToToolBar(toolBarIdentifier, actions);
  233. }
  234. AzToolsFramework::ToolBarManagerOperationResult PythonActionManagerHandler::RemoveActionFromToolBar(
  235. const AZStd::string& toolBarIdentifier, const AZStd::string& actionIdentifier)
  236. {
  237. return m_toolBarManagerInterface->RemoveActionFromToolBar(toolBarIdentifier, actionIdentifier);
  238. }
  239. AzToolsFramework::ToolBarManagerOperationResult PythonActionManagerHandler::RemoveActionsFromToolBar(
  240. const AZStd::string& toolBarIdentifier, const AZStd::vector<AZStd::string>& actionIdentifiers)
  241. {
  242. return m_toolBarManagerInterface->RemoveActionsFromToolBar(toolBarIdentifier, actionIdentifiers);
  243. }
  244. AzToolsFramework::ToolBarManagerOperationResult PythonActionManagerHandler::AddSeparatorToToolBar(
  245. const AZStd::string& toolBarIdentifier, int sortIndex)
  246. {
  247. return m_toolBarManagerInterface->AddSeparatorToToolBar(toolBarIdentifier, sortIndex);
  248. }
  249. AzToolsFramework::ToolBarManagerOperationResult PythonActionManagerHandler::AddWidgetToToolBar(
  250. const AZStd::string& toolBarIdentifier, const AZStd::string& widgetActionIdentifier, int sortIndex)
  251. {
  252. return m_toolBarManagerInterface->AddWidgetToToolBar(toolBarIdentifier, widgetActionIdentifier, sortIndex);
  253. }
  254. AzToolsFramework::ToolBarManagerOperationResult PythonActionManagerHandler::AddToolBarToToolBarArea(
  255. const AZStd::string& toolBarAreaIdentifier, const AZStd::string& toolBarIdentifier, int sortIndex)
  256. {
  257. return m_toolBarManagerInterface->AddToolBarToToolBarArea(toolBarAreaIdentifier, toolBarIdentifier, sortIndex);
  258. }
  259. QToolBar* PythonActionManagerHandler::GenerateToolBar(const AZStd::string& toolBarIdentifier)
  260. {
  261. return m_toolBarManagerInterface->GenerateToolBar(toolBarIdentifier);
  262. }
  263. AzToolsFramework::ToolBarManagerIntegerResult PythonActionManagerHandler::GetSortKeyOfActionInToolBar(
  264. const AZStd::string& toolBarIdentifier, const AZStd::string& actionIdentifier) const
  265. {
  266. return m_toolBarManagerInterface->GetSortKeyOfActionInToolBar(toolBarIdentifier, actionIdentifier);
  267. }
  268. AzToolsFramework::ToolBarManagerIntegerResult PythonActionManagerHandler::GetSortKeyOfWidgetInToolBar(
  269. const AZStd::string& toolBarIdentifier, const AZStd::string& widgetActionIdentifier) const
  270. {
  271. return m_toolBarManagerInterface->GetSortKeyOfWidgetInToolBar(toolBarIdentifier, widgetActionIdentifier);
  272. }
  273. EditorPythonBindings::CustomTypeBindingNotifications::AllocationHandle PythonActionManagerHandler::AllocateDefault()
  274. {
  275. AZ::BehaviorObject behaviorObject;
  276. behaviorObject.m_address = azmalloc(sizeof(PythonEditorAction));
  277. behaviorObject.m_typeId = azrtti_typeid<PythonEditorAction>();
  278. m_allocationMap[behaviorObject.m_address] = behaviorObject.m_typeId;
  279. return { { reinterpret_cast<Handle>(behaviorObject.m_address), AZStd::move(behaviorObject) } };
  280. }
  281. AZStd::optional<EditorPythonBindings::CustomTypeBindingNotifications::ValueHandle> PythonActionManagerHandler::PythonToBehavior(
  282. PyObject* pyObj, [[maybe_unused]] AZ::BehaviorParameter::Traits traits, AZ::BehaviorArgument& outValue)
  283. {
  284. outValue.ConvertTo<PythonEditorAction>();
  285. outValue.StoreInTempData<PythonEditorAction>({ PythonEditorAction(pyObj) });
  286. return { NoAllocation };
  287. }
  288. AZStd::optional<EditorPythonBindings::CustomTypeBindingNotifications::ValueHandle> PythonActionManagerHandler::BehaviorToPython(
  289. const AZ::BehaviorArgument& behaviorValue, PyObject*& outPyObj)
  290. {
  291. PythonEditorAction* value = behaviorValue.GetAsUnsafe<PythonEditorAction>();
  292. outPyObj = value->GetPyObject();
  293. return { NoAllocation };
  294. }
  295. bool PythonActionManagerHandler::CanConvertPythonToBehavior([[maybe_unused]] AZ::BehaviorParameter::Traits traits, PyObject* pyObj) const
  296. {
  297. return PyCallable_Check(pyObj);
  298. }
  299. void PythonActionManagerHandler::CleanUpValue(ValueHandle handle)
  300. {
  301. if (auto handleEntry = m_allocationMap.find(reinterpret_cast<void*>(handle)); handleEntry != m_allocationMap.end())
  302. {
  303. m_allocationMap.erase(handleEntry);
  304. azfree(reinterpret_cast<void*>(handle));
  305. }
  306. }
  307. PythonActionManagerHandler::PythonFunctionObject::PythonFunctionObject(PyObject* handler)
  308. : m_functionObject(handler)
  309. {
  310. // Increment the reference counter for the handler on the Python side to ensure the function isn't garbage collected.
  311. if (m_functionObject)
  312. {
  313. Py_INCREF(m_functionObject);
  314. }
  315. }
  316. PythonActionManagerHandler::PythonFunctionObject::PythonFunctionObject(const PythonFunctionObject& obj)
  317. : m_functionObject(obj.m_functionObject)
  318. {
  319. if (m_functionObject)
  320. {
  321. Py_INCREF(m_functionObject);
  322. }
  323. }
  324. PythonActionManagerHandler::PythonFunctionObject::PythonFunctionObject(PythonFunctionObject&& obj)
  325. : m_functionObject(obj.m_functionObject)
  326. {
  327. // Reference counter does not need to be touched since we're moving ownership.
  328. obj.m_functionObject = nullptr;
  329. }
  330. PythonActionManagerHandler::PythonFunctionObject& PythonActionManagerHandler::PythonFunctionObject::operator=(
  331. const PythonFunctionObject& obj)
  332. {
  333. if (m_functionObject)
  334. {
  335. Py_DECREF(m_functionObject);
  336. }
  337. m_functionObject = obj.m_functionObject;
  338. if (m_functionObject)
  339. {
  340. Py_INCREF(m_functionObject);
  341. }
  342. return *this;
  343. }
  344. PythonActionManagerHandler::PythonFunctionObject::~PythonFunctionObject()
  345. {
  346. if (m_functionObject)
  347. {
  348. Py_DECREF(m_functionObject);
  349. // Clear the pointer in case the destructor is called multiple times.
  350. m_functionObject = nullptr;
  351. }
  352. }
  353. } // namespace EditorPythonBindings