ScriptableImGui.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  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 <Automation/ScriptableImGui.h>
  9. #include <Automation/ScriptRepeaterBus.h>
  10. #include <AzFramework/StringFunc/StringFunc.h>
  11. #include <Utils/Utils.h>
  12. #include <SampleComponentManagerBus.h>
  13. namespace AtomSampleViewer
  14. {
  15. ScriptableImGui* ScriptableImGui::GetInstance()
  16. {
  17. static ScriptableImGui* s_instance = nullptr;
  18. if (!s_instance)
  19. {
  20. AtomSampleViewer::SampleComponentSingletonRequestBus::BroadcastResult(s_instance, &AtomSampleViewer::SampleComponentSingletonRequestBus::Events::GetScriptableImGuiInstance);
  21. }
  22. return s_instance;
  23. }
  24. void ScriptableImGui::CheckAllActionsConsumed()
  25. {
  26. ScriptableImGui* s_instance = GetInstance();
  27. AZ_Error("Automation", s_instance->m_scriptedActions.empty(), "Not all scripted ImGui actions were consumed");
  28. for (auto iter : s_instance->m_scriptedActions)
  29. {
  30. AZ_Error("Automation", false, "Scripted action for '%s' not consumed", iter.first.c_str());
  31. }
  32. AZ_Error("Automation", s_instance->m_nameContextStack.empty(), "PushNameContext and PopNameContext calls didn't match");
  33. }
  34. void ScriptableImGui::ClearActions()
  35. {
  36. ScriptableImGui* s_instance = GetInstance();
  37. s_instance->m_scriptedActions.clear();
  38. s_instance->m_nameContextStack.clear();
  39. }
  40. void ScriptableImGui::PushNameContext(const AZStd::string& nameContext)
  41. {
  42. ScriptableImGui* s_instance = GetInstance();
  43. s_instance->m_nameContextStack.push_back(nameContext);
  44. }
  45. void ScriptableImGui::PopNameContext()
  46. {
  47. ScriptableImGui* s_instance = GetInstance();
  48. AZ_Assert(!s_instance->m_nameContextStack.empty(), "Called PopNameContext too many times");
  49. s_instance->m_nameContextStack.pop_back();
  50. }
  51. ScriptableImGui::ActionItem ScriptableImGui::FindAndRemoveAction(const AZStd::string& pathToImGuiItem)
  52. {
  53. ScriptableImGui* s_instance = GetInstance();
  54. auto iter = s_instance->m_scriptedActions.find(pathToImGuiItem);
  55. if (iter != s_instance->m_scriptedActions.end())
  56. {
  57. ScriptableImGui::ActionItem item = AZStd::move(iter->second);
  58. s_instance->m_scriptedActions.erase(iter);
  59. return AZStd::move(item);
  60. }
  61. return ScriptableImGui::ActionItem{};
  62. }
  63. AZStd::string ScriptableImGui::MakeFullPath(const AZStd::string& forLabel)
  64. {
  65. ScriptableImGui* s_instance = GetInstance();
  66. static constexpr char Delimiter[] = "/";
  67. AZStd::string fullPath;
  68. if (!s_instance->m_nameContextStack.empty())
  69. {
  70. AzFramework::StringFunc::Join(fullPath, s_instance->m_nameContextStack.begin(), s_instance->m_nameContextStack.end(), Delimiter);
  71. fullPath += Delimiter;
  72. }
  73. fullPath += forLabel;
  74. return fullPath;
  75. }
  76. void ScriptableImGui::ReportScriptError([[maybe_unused]] const char* message)
  77. {
  78. AZ_Error("Automation", false, "Script: %s", message);
  79. }
  80. void ScriptableImGui::SetBool(const AZStd::string& pathToImGuiItem, bool value)
  81. {
  82. ScriptableImGui* s_instance = GetInstance();
  83. s_instance->m_scriptedActions[pathToImGuiItem] = value;
  84. }
  85. void ScriptableImGui::SetNumber(const AZStd::string& pathToImGuiItem, float value)
  86. {
  87. ScriptableImGui* s_instance = GetInstance();
  88. s_instance->m_scriptedActions[pathToImGuiItem] = value;
  89. }
  90. void ScriptableImGui::SetVector(const AZStd::string& pathToImGuiItem, const AZ::Vector2& value)
  91. {
  92. ScriptableImGui* s_instance = GetInstance();
  93. s_instance->m_scriptedActions[pathToImGuiItem] = value;
  94. }
  95. void ScriptableImGui::SetVector(const AZStd::string& pathToImGuiItem, const AZ::Vector3& value)
  96. {
  97. ScriptableImGui* s_instance = GetInstance();
  98. s_instance->m_scriptedActions[pathToImGuiItem] = value;
  99. }
  100. void ScriptableImGui::SetString(const AZStd::string& pathToImGuiItem, const AZStd::string& value)
  101. {
  102. ScriptableImGui* s_instance = GetInstance();
  103. s_instance->m_scriptedActions[pathToImGuiItem] = value;
  104. }
  105. template<typename ActionDataT>
  106. bool ScriptableImGui::ActionHelper(
  107. const char* label,
  108. AZStd::function<bool()> imguiAction,
  109. AZStd::function<void(const AZStd::string& /*pathToImGuiItem*/)> reportScriptableAction,
  110. AZStd::function<bool(ActionDataT /*scriptArg*/)> handleScriptedAction,
  111. bool shouldReportScriptableActionAfterAnyChange)
  112. {
  113. bool imResult = imguiAction();
  114. const AZStd::string pathToImGuiItem = MakeFullPath(label);
  115. if (ImGui::IsItemDeactivatedAfterEdit() || (shouldReportScriptableActionAfterAnyChange && imResult))
  116. {
  117. reportScriptableAction(pathToImGuiItem);
  118. }
  119. bool scriptResult = false;
  120. ActionItem actionItem = FindAndRemoveAction(pathToImGuiItem);
  121. bool foundAction = !AZStd::holds_alternative<InvalidActionItem>(actionItem);
  122. if (foundAction)
  123. {
  124. if (AZStd::holds_alternative<ActionDataT>(actionItem))
  125. {
  126. scriptResult = handleScriptedAction(AZStd::get<ActionDataT>(actionItem));
  127. }
  128. else
  129. {
  130. ReportScriptError(AZStd::string::format("Wrong data type used to set '%s'", pathToImGuiItem.c_str()).c_str());
  131. }
  132. }
  133. return imResult || scriptResult;
  134. }
  135. bool ScriptableImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags)
  136. {
  137. PushNameContext(name);
  138. return ImGui::Begin(name, p_open, flags);
  139. }
  140. void ScriptableImGui::End()
  141. {
  142. ImGui::End();
  143. PopNameContext();
  144. }
  145. bool ScriptableImGui::Checkbox(const char* label, bool* v)
  146. {
  147. auto imguiAction = [&]()
  148. {
  149. return ImGui::Checkbox(label, v);
  150. };
  151. auto reportScriptableAction = [&](const AZStd::string& pathToImGuiItem)
  152. {
  153. Utils::ReportScriptableAction("SetImguiValue('%s', %s)", pathToImGuiItem.c_str(), (*v ? "true" : "false"));
  154. };
  155. auto handleScriptedAction = [&](bool scriptArg)
  156. {
  157. if (*v != scriptArg)
  158. {
  159. *v = scriptArg;
  160. return true;
  161. }
  162. return false;
  163. };
  164. return ActionHelper<bool>(label, imguiAction, reportScriptableAction, handleScriptedAction);
  165. }
  166. bool ScriptableImGui::Button(const char* label, const ImVec2& size_arg)
  167. {
  168. auto imguiAction = [&]()
  169. {
  170. return ImGui::Button(label, size_arg);
  171. };
  172. auto reportScriptableAction = [&](const AZStd::string& pathToImGuiItem)
  173. {
  174. Utils::ReportScriptableAction("SetImguiValue('%s', true)", pathToImGuiItem.c_str());
  175. };
  176. auto handleScriptedAction = [](bool scriptArg)
  177. {
  178. return scriptArg;
  179. };
  180. return ActionHelper<bool>(label, imguiAction, reportScriptableAction, handleScriptedAction);
  181. }
  182. bool ScriptableImGui::ListBox(const char* label, int* current_item, bool(*items_getter)(void* data, int idx, const char** out_text), void* data, int items_count, int height_in_items)
  183. {
  184. auto imguiAction = [&]()
  185. {
  186. return ImGui::ListBox(label, current_item, items_getter, data, items_count, height_in_items);
  187. };
  188. auto reportScriptableAction = [&](const AZStd::string& pathToImGuiItem)
  189. {
  190. const char* itemText = nullptr;
  191. if (items_getter(data, *current_item, &itemText) && itemText)
  192. {
  193. Utils::ReportScriptableAction("SetImguiValue('%s', '%s')", pathToImGuiItem.c_str(), itemText);
  194. }
  195. };
  196. auto handleScriptedAction = [&](AZStd::string scriptArg)
  197. {
  198. for (int i = 0; i < items_count; ++i)
  199. {
  200. const char* itemText = nullptr;
  201. if (items_getter(data, i, &itemText) && itemText && scriptArg == itemText)
  202. {
  203. *current_item = i;
  204. return true;
  205. }
  206. }
  207. ReportScriptError(AZStd::string::format("List '%s' does not contain item '%s'", label, scriptArg.c_str()).c_str());
  208. return false;
  209. };
  210. return ActionHelper<AZStd::string>(label, imguiAction, reportScriptableAction, handleScriptedAction);
  211. }
  212. bool ScriptableImGui::Combo(const char* label, int* current_item, const char* const items[], int items_count, int height_in_items)
  213. {
  214. auto imguiAction = [&]()
  215. {
  216. return ImGui::Combo(label, current_item, items, items_count, height_in_items);
  217. };
  218. auto reportScriptableAction = [&](const AZStd::string& pathToImGuiItem)
  219. {
  220. Utils::ReportScriptableAction("SetImguiValue('%s', '%s')", pathToImGuiItem.c_str(), items[*current_item]);
  221. };
  222. auto handleScriptedAction = [&](AZStd::string scriptArg)
  223. {
  224. for (int i = 0; i < items_count; ++i)
  225. {
  226. if (items[i] == scriptArg)
  227. {
  228. *current_item = i;
  229. return true;
  230. }
  231. }
  232. ReportScriptError(AZStd::string::format("Combo box '%s' does not contain item '%s'", label, scriptArg.c_str()).c_str());
  233. return false;
  234. };
  235. return ActionHelper<AZStd::string>(label, imguiAction, reportScriptableAction, handleScriptedAction,
  236. true /* It seems ImGui::Combo doesn't work with IsItemDeactivatedAfterChange() */);
  237. }
  238. bool ScriptableImGui::RadioButton(const char* label, int* v, int v_button)
  239. {
  240. auto imguiAction = [&]()
  241. {
  242. return ImGui::RadioButton(label, v, v_button);
  243. };
  244. auto reportScriptableAction = [&](const AZStd::string& pathToImGuiItem)
  245. {
  246. Utils::ReportScriptableAction("SetImguiValue('%s', true)", pathToImGuiItem.c_str());
  247. };
  248. auto handleScriptedAction = [&](bool scriptArg)
  249. {
  250. if (scriptArg)
  251. {
  252. *v = v_button;
  253. return true;
  254. }
  255. return false;
  256. };
  257. return ActionHelper<bool>(label, imguiAction, reportScriptableAction, handleScriptedAction);
  258. }
  259. bool ScriptableImGui::SliderInt(const char* label, int* v, int v_min, int v_max, const char* format)
  260. {
  261. auto imguiAction = [&]()
  262. {
  263. return ImGui::SliderInt(label, v, v_min, v_max, format);
  264. };
  265. auto reportScriptableAction = [&](const AZStd::string& pathToImGuiItem)
  266. {
  267. Utils::ReportScriptableAction("SetImguiValue('%s', %d)", pathToImGuiItem.c_str(), *v);
  268. };
  269. auto handleScriptedAction = [&](float scriptArg)
  270. {
  271. *v = aznumeric_cast<int>(scriptArg);
  272. return true;
  273. };
  274. return ActionHelper<float>(label, imguiAction, reportScriptableAction, handleScriptedAction);
  275. }
  276. bool ScriptableImGui::SliderFloat(const char* label, float* v, float v_min, float v_max, const char* format, ImGuiSliderFlags flags)
  277. {
  278. auto imguiAction = [&]()
  279. {
  280. return ImGui::SliderFloat(label, v, v_min, v_max, format, flags);
  281. };
  282. auto reportScriptableAction = [&](const AZStd::string& pathToImGuiItem)
  283. {
  284. Utils::ReportScriptableAction("SetImguiValue('%s', %f)", pathToImGuiItem.c_str(), *v);
  285. };
  286. auto handleScriptedAction = [&](float scriptArg)
  287. {
  288. *v = scriptArg;
  289. return true;
  290. };
  291. return ActionHelper<float>(label, imguiAction, reportScriptableAction, handleScriptedAction);
  292. }
  293. bool ScriptableImGui::SliderFloat2(const char* label, float v[2], float v_min, float v_max, const char* format, ImGuiSliderFlags flags)
  294. {
  295. auto imguiAction = [&]()
  296. {
  297. return ImGui::SliderFloat2(label, v, v_min, v_max, format, flags);
  298. };
  299. auto reportScriptableAction = [&](const AZStd::string& pathToImGuiItem)
  300. {
  301. Utils::ReportScriptableAction("SetImguiValue('%s', Vector2(%f, %f))", pathToImGuiItem.c_str(), v[0], v[1]);
  302. };
  303. auto handleScriptedAction = [&](AZ::Vector2 scriptArg)
  304. {
  305. v[0] = scriptArg.GetX();
  306. v[1] = scriptArg.GetY();
  307. return true;
  308. };
  309. return ActionHelper<AZ::Vector2>(label, imguiAction, reportScriptableAction, handleScriptedAction);
  310. }
  311. template <typename ImGuiActionType>
  312. bool ScriptableImGui::ThreeComponentHelper(const char* label, float v[3], ImGuiActionType& imguiAction)
  313. {
  314. auto reportScriptableAction = [&](const AZStd::string& pathToImGuiItem)
  315. {
  316. Utils::ReportScriptableAction("SetImguiValue('%s', Vector3(%f, %f, %f))", pathToImGuiItem.c_str(), v[0], v[1], v[2]);
  317. };
  318. auto handleScriptedAction = [&](AZ::Vector3 scriptArg)
  319. {
  320. v[0] = scriptArg.GetX();
  321. v[1] = scriptArg.GetY();
  322. v[2] = scriptArg.GetZ();
  323. return true;
  324. };
  325. return ActionHelper<AZ::Vector3>(label, imguiAction, reportScriptableAction, handleScriptedAction);
  326. }
  327. bool ScriptableImGui::SliderFloat3(const char* label, float v[3], float v_min, float v_max, const char* format, ImGuiSliderFlags flags)
  328. {
  329. auto imguiAction = [&]()
  330. {
  331. return ImGui::SliderFloat3(label, v, v_min, v_max, format, flags);
  332. };
  333. return ThreeComponentHelper(label, v, imguiAction);
  334. }
  335. bool ScriptableImGui::ColorEdit3(const char* label, float v[3], ImGuiColorEditFlags flags)
  336. {
  337. auto imguiAction = [&]()
  338. {
  339. return ImGui::ColorEdit3(label, v, flags);
  340. };
  341. return ThreeComponentHelper(label, v, imguiAction);
  342. }
  343. bool ScriptableImGui::ColorPicker3(const char* label, float v[3], ImGuiColorEditFlags flags)
  344. {
  345. auto imguiAction = [&]()
  346. {
  347. return ImGui::ColorPicker3(label, v, flags);
  348. };
  349. return ThreeComponentHelper(label, v, imguiAction);
  350. }
  351. bool ScriptableImGui::SliderAngle(const char* label, float* v, float v_min, float v_max, const char* format)
  352. {
  353. auto imguiAction = [&]()
  354. {
  355. return ImGui::SliderAngle(label, v, v_min, v_max, format);
  356. };
  357. auto reportScriptableAction = [&](const AZStd::string& pathToImGuiItem)
  358. {
  359. Utils::ReportScriptableAction("SetImguiValue('%s', %f)", pathToImGuiItem.c_str(), *v);
  360. };
  361. auto handleScriptedAction = [&](float scriptArg)
  362. {
  363. *v = scriptArg;
  364. return true;
  365. };
  366. return ActionHelper<float>(label, imguiAction, reportScriptableAction, handleScriptedAction);
  367. }
  368. bool ScriptableImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags flags, const ImVec2& size)
  369. {
  370. auto imguiAction = [&]()
  371. {
  372. return ImGui::Selectable(label, selected, flags, size);
  373. };
  374. auto reportScriptableAction = [&](const AZStd::string& pathToImGuiItem)
  375. {
  376. // The "selected" value that's passed determines if the selectable is *currently* selected, and clicking the selectable toggles its state.
  377. // So when someone clicks the selectable to change its state, we need to report the opposite of what the original state was.
  378. Utils::ReportScriptableAction("SetImguiValue('%s', %s)", pathToImGuiItem.c_str(), (selected ? "false" : "true"));
  379. };
  380. auto handleScriptedAction = [&](bool scriptArg)
  381. {
  382. return scriptArg != selected;
  383. };
  384. return ActionHelper<bool>(label, imguiAction, reportScriptableAction, handleScriptedAction);
  385. }
  386. bool ScriptableImGui::Selectable(const char* label, bool* p_selected, ImGuiSelectableFlags flags, const ImVec2& size)
  387. {
  388. auto imguiAction = [&]()
  389. {
  390. return ImGui::Selectable(label, p_selected, flags, size);
  391. };
  392. auto reportScriptableAction = [&](const AZStd::string& pathToImGuiItem)
  393. {
  394. // The "selected" value that's passed determines if the selectable is *currently* selected, and clicking the selectable toggles its state.
  395. // So when someone clicks the selectable to change its state, we need to report the opposite of what the original state was.
  396. Utils::ReportScriptableAction("SetImguiValue('%s', %s)", pathToImGuiItem.c_str(), (*p_selected ? "false" : "true"));
  397. };
  398. auto handleScriptedAction = [&](bool scriptArg)
  399. {
  400. if (scriptArg != *p_selected)
  401. {
  402. *p_selected = scriptArg;
  403. return true;
  404. }
  405. return false;
  406. };
  407. return ActionHelper<bool>(label, imguiAction, reportScriptableAction, handleScriptedAction);
  408. }
  409. bool ScriptableImGui::TreeNodeEx(const char* label, ImGuiSelectableFlags flags)
  410. {
  411. if (ImGui::TreeNodeEx(label, flags))
  412. {
  413. PushNameContext(label);
  414. return true;
  415. }
  416. return false;
  417. }
  418. void ScriptableImGui::TreePop()
  419. {
  420. ImGui::TreePop();
  421. PopNameContext();
  422. }
  423. bool ScriptableImGui::BeginCombo(const char* label, const char* preview_value, ImGuiComboFlags flags)
  424. {
  425. ScriptableImGui* s_instance = GetInstance();
  426. const AZStd::string pathToImGuiItem = MakeFullPath(label);
  427. if (ImGui::BeginCombo(label, preview_value, flags))
  428. {
  429. PushNameContext(label);
  430. return true;
  431. }
  432. else if (!s_instance->m_scriptedActions.empty())
  433. {
  434. // If a script is running, we return true so that imgui controls inside the combo box are checked.
  435. // We also have to set a flag to prevent ScriptableImGui::EndCombo() from calling ImGui::EndCombo()
  436. // because that is only allowed when ImGui::BeginCombo() returns true.
  437. s_instance->m_isInScriptedComboPopup = true;
  438. PushNameContext(label);
  439. return true;
  440. }
  441. return false;
  442. }
  443. void ScriptableImGui::EndCombo()
  444. {
  445. ScriptableImGui* s_instance = GetInstance();
  446. if (s_instance->m_isInScriptedComboPopup)
  447. {
  448. s_instance->m_isInScriptedComboPopup = false;
  449. // ScriptableImGui::BeginCombo() returned true even though ImGui::BeginCombo() didn't, so we aren't allowed
  450. // to call ImGui::EndCombo() here.
  451. }
  452. else
  453. {
  454. ImGui::EndCombo();
  455. }
  456. PopNameContext();
  457. }
  458. bool ScriptableImGui::BeginMenu(const char* label, bool enabled)
  459. {
  460. // We don't use ActionHelper here because BeginMenu has to do things a bit different since there is a persistent popup.
  461. // It has to run the script code before the ImGui code, and if there is a scripted action then force the menu to open.
  462. // Also, we don't include a "scriptResult", just the "imResult", because we need to ensure that ImGui is in the actual
  463. // state we are reporting back to the caller. Otherwise the internal state of ImGui could become invalid and crash.
  464. const AZStd::string pathToImGuiItem = MakeFullPath(label);
  465. ActionItem actionItem = FindAndRemoveAction(pathToImGuiItem);
  466. bool foundAction = !AZStd::holds_alternative<InvalidActionItem>(actionItem);
  467. if (foundAction)
  468. {
  469. if (AZStd::holds_alternative<bool>(actionItem))
  470. {
  471. if (AZStd::get<bool>(actionItem))
  472. {
  473. // Here we force the menu to open before arriving at ImGui::BeginMenu
  474. ImGui::OpenPopup(label);
  475. }
  476. }
  477. else
  478. {
  479. ReportScriptError(AZStd::string::format("Wrong data type used to set '%s'", label).c_str());
  480. }
  481. }
  482. bool wasPopupOpen = ImGui::IsPopupOpen(label);
  483. bool isPopupOpen = ImGui::BeginMenu(label, enabled);
  484. if (isPopupOpen)
  485. {
  486. PushNameContext(label);
  487. if (!wasPopupOpen && isPopupOpen)
  488. {
  489. Utils::ReportScriptableAction("SetImguiValue('%s', true)", pathToImGuiItem.c_str());
  490. }
  491. }
  492. return isPopupOpen;
  493. }
  494. void ScriptableImGui::EndMenu()
  495. {
  496. ImGui::EndMenu();
  497. PopNameContext();
  498. }
  499. bool ScriptableImGui::MenuItem(const char* label, const char* shortcut, bool selected, bool enabled)
  500. {
  501. auto imguiAction = [&]()
  502. {
  503. return ImGui::MenuItem(label, shortcut, selected, enabled);
  504. };
  505. auto reportScriptableAction = [&](const AZStd::string& pathToImGuiItem)
  506. {
  507. Utils::ReportScriptableAction("SetImguiValue('%s', true)", pathToImGuiItem.c_str());
  508. };
  509. auto handleScriptedAction = [](bool scriptArg)
  510. {
  511. return scriptArg;
  512. };
  513. return ActionHelper<bool>(label, imguiAction, reportScriptableAction, handleScriptedAction);
  514. }
  515. } // namespace AtomSampleViewer