ToolBox.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. // Description : ToolBox Macro System
  9. #include "EditorDefs.h"
  10. #include "ToolBox.h"
  11. #include <AzCore/Utils/Utils.h>
  12. // AzToolsFramework
  13. #include <AzToolsFramework/API/EditorPythonRunnerRequestsBus.h>
  14. #include <AzToolsFramework/API/ToolsApplicationAPI.h>
  15. // Editor
  16. #include "Settings.h"
  17. //////////////////////////////////////////////////////////////////////////
  18. // CToolBoxCommand
  19. //////////////////////////////////////////////////////////////////////////
  20. void CToolBoxCommand::Save(XmlNodeRef commandNode) const
  21. {
  22. commandNode->setAttr("type", (int)m_type);
  23. commandNode->setAttr("text", m_text.toUtf8().data());
  24. commandNode->setAttr("bVariableToggle", m_bVariableToggle);
  25. }
  26. //////////////////////////////////////////////////////////////////////////
  27. void CToolBoxCommand::Load(XmlNodeRef commandNode)
  28. {
  29. int type = 0;
  30. commandNode->getAttr("type", type);
  31. m_type = CToolBoxCommand::EType(type);
  32. m_text = commandNode->getAttr("text");
  33. commandNode->getAttr("bVariableToggle", m_bVariableToggle);
  34. }
  35. //////////////////////////////////////////////////////////////////////////
  36. void CToolBoxCommand::Execute() const
  37. {
  38. if (m_type == CToolBoxCommand::eT_SCRIPT_COMMAND)
  39. {
  40. using namespace AzToolsFramework;
  41. EditorPythonRunnerRequestBus::Broadcast(&EditorPythonRunnerRequestBus::Events::ExecuteByString, m_text.toUtf8().data(), false);
  42. }
  43. else if (m_type == CToolBoxCommand::eT_CONSOLE_COMMAND)
  44. {
  45. if (m_bVariableToggle)
  46. {
  47. // Toggle the variable.
  48. float val = GetIEditor()->GetConsoleVar(m_text.toUtf8().data());
  49. bool bOn = val != 0;
  50. GetIEditor()->SetConsoleVar(m_text.toUtf8().data(), (bOn) ? 0.0f : 1.0f);
  51. }
  52. else
  53. {
  54. // does the command required an active Python interpreter?
  55. if (m_text.contains("pyRunFile") && !AzToolsFramework::EditorPythonRunnerRequestBus::HasHandlers())
  56. {
  57. AZ_Warning("toolbar", false, "The command '%s' requires an embedded Python interpreter. "
  58. "The gem named EditorPythonBindings offers this service. "
  59. "Please enable this gem for the project.", m_text.toUtf8().data());
  60. return;
  61. }
  62. GetIEditor()->GetSystem()->GetIConsole()->ExecuteString(m_text.toUtf8().data());
  63. }
  64. }
  65. }
  66. //////////////////////////////////////////////////////////////////////////
  67. // CToolBoxMacro
  68. //////////////////////////////////////////////////////////////////////////
  69. void CToolBoxMacro::Save(XmlNodeRef macroNode) const
  70. {
  71. for (size_t i = 0; i < m_commands.size(); ++i)
  72. {
  73. XmlNodeRef commandNode = macroNode->newChild("command");
  74. m_commands[i]->Save(commandNode);
  75. }
  76. }
  77. //////////////////////////////////////////////////////////////////////////
  78. void CToolBoxMacro::Load(XmlNodeRef macroNode)
  79. {
  80. for (int i = 0; i < macroNode->getChildCount(); ++i)
  81. {
  82. XmlNodeRef commandNode = macroNode->getChild(i);
  83. m_commands.push_back(new CToolBoxCommand);
  84. m_commands[i]->Load(commandNode);
  85. }
  86. }
  87. //////////////////////////////////////////////////////////////////////////
  88. void CToolBoxMacro::AddCommand(CToolBoxCommand::EType type, const QString& command, bool bVariableToggle)
  89. {
  90. CToolBoxCommand* pNewCommand = new CToolBoxCommand;
  91. pNewCommand->m_type = type;
  92. pNewCommand->m_text = command;
  93. pNewCommand->m_bVariableToggle = bVariableToggle;
  94. m_commands.push_back(pNewCommand);
  95. }
  96. //////////////////////////////////////////////////////////////////////////
  97. void CToolBoxMacro::Clear()
  98. {
  99. for (size_t i = 0; i < m_commands.size(); ++i)
  100. {
  101. delete m_commands[i];
  102. }
  103. m_commands.clear();
  104. }
  105. //////////////////////////////////////////////////////////////////////////
  106. const CToolBoxCommand* CToolBoxMacro::GetCommandAt(int index) const
  107. {
  108. assert(0 <= index && index < m_commands.size());
  109. return m_commands[index];
  110. }
  111. //////////////////////////////////////////////////////////////////////////
  112. CToolBoxCommand* CToolBoxMacro::GetCommandAt(int index)
  113. {
  114. assert(0 <= index && index < m_commands.size());
  115. return m_commands[index];
  116. }
  117. //////////////////////////////////////////////////////////////////////////
  118. void CToolBoxMacro::SwapCommand(int index1, int index2)
  119. {
  120. assert(0 <= index1 && index1 < m_commands.size());
  121. assert(0 <= index2 && index2 < m_commands.size());
  122. std::swap(m_commands[index1], m_commands[index2]);
  123. }
  124. void CToolBoxMacro::RemoveCommand(int index)
  125. {
  126. assert(0 <= index && index < m_commands.size());
  127. m_commands.erase(m_commands.begin() + index);
  128. }
  129. //////////////////////////////////////////////////////////////////////////
  130. void CToolBoxMacro::Execute() const
  131. {
  132. for (size_t i = 0; i < m_commands.size(); ++i)
  133. {
  134. m_commands[i]->Execute();
  135. }
  136. }
  137. //////////////////////////////////////////////////////////////////////////
  138. // CToolBoxManager
  139. //////////////////////////////////////////////////////////////////////////
  140. int CToolBoxManager::GetMacroCount(bool bToolbox) const
  141. {
  142. if (bToolbox)
  143. {
  144. return int(m_macros.size());
  145. }
  146. return int(m_shelveMacros.size());
  147. }
  148. //////////////////////////////////////////////////////////////////////////
  149. const CToolBoxMacro* CToolBoxManager::GetMacro(int iIndex, bool bToolbox) const
  150. {
  151. if (bToolbox)
  152. {
  153. assert(0 <= iIndex && iIndex < m_macros.size());
  154. return m_macros[iIndex];
  155. }
  156. else
  157. {
  158. assert(0 <= iIndex && iIndex < m_shelveMacros.size());
  159. return m_shelveMacros[iIndex];
  160. }
  161. }
  162. //////////////////////////////////////////////////////////////////////////
  163. CToolBoxMacro* CToolBoxManager::GetMacro(int iIndex, bool bToolbox)
  164. {
  165. if (bToolbox)
  166. {
  167. assert(0 <= iIndex && iIndex < m_macros.size());
  168. return m_macros[iIndex];
  169. }
  170. else
  171. {
  172. assert(0 <= iIndex && iIndex < m_shelveMacros.size());
  173. return m_shelveMacros[iIndex];
  174. }
  175. }
  176. //////////////////////////////////////////////////////////////////////////
  177. int CToolBoxManager::GetMacroIndex(const QString& title, bool bToolbox) const
  178. {
  179. if (bToolbox)
  180. {
  181. for (size_t i = 0; i < m_macros.size(); ++i)
  182. {
  183. if (QString::compare(m_macros[i]->GetTitle(), title, Qt::CaseInsensitive) == 0)
  184. {
  185. return int(i);
  186. }
  187. }
  188. }
  189. else
  190. {
  191. for (size_t i = 0; i < m_shelveMacros.size(); ++i)
  192. {
  193. if (QString::compare(m_shelveMacros[i]->GetTitle(), title, Qt::CaseInsensitive) == 0)
  194. {
  195. return int(i);
  196. }
  197. }
  198. }
  199. return -1;
  200. }
  201. //////////////////////////////////////////////////////////////////////////
  202. CToolBoxMacro* CToolBoxManager::NewMacro(const QString& title, bool bToolbox, int* newIdx)
  203. {
  204. if (bToolbox)
  205. {
  206. const int macroCount = static_cast<int>(m_macros.size());
  207. if (macroCount > ID_TOOL_LAST - ID_TOOL_FIRST + 1)
  208. {
  209. return nullptr;
  210. }
  211. for (size_t i = 0; i < macroCount; ++i)
  212. {
  213. if (QString::compare(m_macros[i]->GetTitle(), title, Qt::CaseInsensitive) == 0)
  214. {
  215. return nullptr;
  216. }
  217. }
  218. CToolBoxMacro* pNewTool = new CToolBoxMacro(title);
  219. if (newIdx)
  220. {
  221. *newIdx = macroCount;
  222. }
  223. m_macros.push_back(pNewTool);
  224. return pNewTool;
  225. }
  226. else
  227. {
  228. const int shelveMacroCount = static_cast<int>(m_shelveMacros.size());
  229. if (shelveMacroCount > ID_TOOL_SHELVE_LAST - ID_TOOL_SHELVE_FIRST + 1)
  230. {
  231. return nullptr;
  232. }
  233. CToolBoxMacro* pNewTool = new CToolBoxMacro(title);
  234. if (newIdx)
  235. {
  236. *newIdx = shelveMacroCount;
  237. }
  238. m_shelveMacros.push_back(pNewTool);
  239. return pNewTool;
  240. }
  241. }
  242. //////////////////////////////////////////////////////////////////////////
  243. bool CToolBoxManager::SetMacroTitle(int index, const QString& title, bool bToolbox)
  244. {
  245. if (bToolbox)
  246. {
  247. assert(0 <= index && index < m_macros.size());
  248. for (size_t i = 0; i < m_macros.size(); ++i)
  249. {
  250. if (i == index)
  251. {
  252. continue;
  253. }
  254. if (QString::compare(m_macros[i]->GetTitle(), title, Qt::CaseInsensitive) == 0)
  255. {
  256. return false;
  257. }
  258. }
  259. m_macros[index]->SetTitle(title);
  260. }
  261. else
  262. {
  263. assert(0 <= index && index < m_shelveMacros.size());
  264. for (size_t i = 0; i < m_shelveMacros.size(); ++i)
  265. {
  266. if (i == index)
  267. {
  268. continue;
  269. }
  270. if (QString::compare(m_shelveMacros[i]->GetTitle(), title, Qt::CaseInsensitive) == 0)
  271. {
  272. return false;
  273. }
  274. }
  275. m_shelveMacros[index]->SetTitle(title);
  276. }
  277. return true;
  278. }
  279. //////////////////////////////////////////////////////////////////////////
  280. void CToolBoxManager::Load()
  281. {
  282. Clear();
  283. QString path;
  284. GetSaveFilePath(path);
  285. Load(path, true);
  286. }
  287. void CToolBoxManager::Load(QString xmlpath, bool bToolbox)
  288. {
  289. XmlNodeRef toolBoxNode = XmlHelpers::LoadXmlFromFile(xmlpath.toUtf8().data());
  290. if (toolBoxNode == nullptr)
  291. {
  292. return;
  293. }
  294. GetIEditor()->GetSettingsManager()->AddSettingsNode(toolBoxNode);
  295. AZ::IO::FixedMaxPathString engineRoot = AZ::Utils::GetEnginePath();
  296. QDir engineDir = !engineRoot.empty() ? QDir(QString(engineRoot.c_str())) : QDir::current();
  297. AZStd::string enginePath = PathUtil::AddSlash(engineDir.absolutePath().toUtf8().data());
  298. for (int i = 0; i < toolBoxNode->getChildCount(); ++i)
  299. {
  300. XmlNodeRef macroNode = toolBoxNode->getChild(i);
  301. QString title = macroNode->getAttr("title");
  302. QString shortcutName = macroNode->getAttr("shortcut");
  303. QString iconPath = macroNode->getAttr("icon");
  304. int idx = -1;
  305. CToolBoxMacro* pMacro = NewMacro(title, bToolbox, &idx);
  306. if (!pMacro || idx == -1)
  307. {
  308. continue;
  309. }
  310. pMacro->Load(macroNode);
  311. pMacro->SetShortcutName(shortcutName);
  312. pMacro->SetIconPath(iconPath.toUtf8().data());
  313. pMacro->SetToolbarId(-1);
  314. }
  315. }
  316. //////////////////////////////////////////////////////////////////////////
  317. void CToolBoxManager::Save() const
  318. {
  319. XmlNodeRef toolBoxNode = XmlHelpers::CreateXmlNode(TOOLBOXMACROS_NODE);
  320. for (size_t i = 0; i < m_macros.size(); ++i)
  321. {
  322. if (m_macros[i]->GetToolbarId() != -1)
  323. {
  324. continue;
  325. }
  326. XmlNodeRef macroNode = toolBoxNode->newChild("macro");
  327. macroNode->setAttr("title", m_macros[i]->GetTitle().toUtf8().data());
  328. macroNode->setAttr("shortcut", m_macros[i]->GetShortcutName().toString().toUtf8().data());
  329. macroNode->setAttr("icon", m_macros[i]->GetIconPath().toUtf8().data());
  330. m_macros[i]->Save(macroNode);
  331. }
  332. QString path;
  333. GetSaveFilePath(path);
  334. XmlHelpers::SaveXmlNode(GetIEditor()->GetFileUtil(), toolBoxNode, path.toUtf8().data());
  335. }
  336. //////////////////////////////////////////////////////////////////////////
  337. void CToolBoxManager::Clear()
  338. {
  339. for (size_t i = 0; i < m_macros.size(); ++i)
  340. {
  341. delete m_macros[i];
  342. }
  343. m_macros.clear();
  344. }
  345. //////////////////////////////////////////////////////////////////////////
  346. void CToolBoxManager::ExecuteMacro(int iIndex, bool bToolbox) const
  347. {
  348. if (iIndex >= 0 && iIndex < GetMacroCount(bToolbox) && GetMacro(iIndex, bToolbox))
  349. {
  350. GetMacro(iIndex, bToolbox)->Execute();
  351. }
  352. }
  353. //////////////////////////////////////////////////////////////////////////
  354. void CToolBoxManager::ExecuteMacro(const QString& name, bool bToolbox) const
  355. {
  356. if (bToolbox)
  357. {
  358. // Find tool with this name.
  359. for (size_t i = 0; i < m_macros.size(); ++i)
  360. {
  361. if (QString::compare(m_macros[i]->GetTitle(), name, Qt::CaseInsensitive) == 0)
  362. {
  363. ExecuteMacro(int(i), bToolbox);
  364. break;
  365. }
  366. }
  367. }
  368. else
  369. {
  370. // Find tool with this name.
  371. for (size_t i = 0; i < m_shelveMacros.size(); ++i)
  372. {
  373. if (QString::compare(m_shelveMacros[i]->GetTitle(), name, Qt::CaseInsensitive) == 0)
  374. {
  375. ExecuteMacro(int(i), bToolbox);
  376. break;
  377. }
  378. }
  379. }
  380. }
  381. //////////////////////////////////////////////////////////////////////////
  382. void CToolBoxManager::SwapMacro(int index1, int index2, bool bToolbox)
  383. {
  384. assert(0 <= index1 && index1 < GetMacroCount(bToolbox));
  385. assert(0 <= index2 && index2 < GetMacroCount(bToolbox));
  386. if (bToolbox)
  387. {
  388. std::swap(m_macros[index1], m_macros[index2]);
  389. }
  390. else
  391. {
  392. std::swap(m_shelveMacros[index1], m_shelveMacros[index2]);
  393. }
  394. }
  395. //////////////////////////////////////////////////////////////////////////
  396. void CToolBoxManager::RemoveMacro(int index, bool bToolbox)
  397. {
  398. assert(0 <= index && index < GetMacroCount(bToolbox));
  399. if (bToolbox)
  400. {
  401. delete m_macros[index];
  402. m_macros[index] = nullptr;
  403. m_macros.erase(m_macros.begin() + index);
  404. }
  405. else
  406. {
  407. delete m_shelveMacros[index];
  408. m_shelveMacros[index] = nullptr;
  409. m_shelveMacros.erase(m_shelveMacros.begin() + index);
  410. }
  411. }
  412. //////////////////////////////////////////////////////////////////////////
  413. void CToolBoxManager::GetSaveFilePath(QString& outPath) const
  414. {
  415. outPath = Path::GetResolvedUserSandboxFolder();
  416. outPath += "Macros.xml";
  417. }