ScriptableImGui.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. #pragma once
  9. #include <AzCore/RTTI/RTTI.h>
  10. #include <AzCore/std/string/string.h>
  11. #include <AzCore/std/containers/unordered_map.h>
  12. #include <AzCore/Math/Vector3.h>
  13. #include <AzCore/Memory/SystemAllocator.h>
  14. #include <imgui/imgui.h>
  15. #define SCRIPTABLE_IMGUI
  16. #ifdef SCRIPTABLE_IMGUI
  17. #define Scriptable_ImGui AtomSampleViewer::ScriptableImGui
  18. #endif
  19. namespace AtomSampleViewer
  20. {
  21. class ScriptManager;
  22. //! Wraps the ImGui API in a reflection system that automatically exposes ImGui data elements
  23. //! to the scripting system. It enhances standard ImGui functions to check for scripted
  24. //! actions that can perform the same actions as a user.
  25. class ScriptableImGui final
  26. {
  27. friend class ScriptManager;
  28. public:
  29. AZ_CLASS_ALLOCATOR(ScriptableImGui, AZ::SystemAllocator, 0);
  30. //! Utility for calling ScriptableImGui::PushNameContext and ScriptableImGui::PopNameContext
  31. class ScopedNameContext final
  32. {
  33. public:
  34. ScopedNameContext(const AZStd::string& nameContext) { ScriptableImGui::PushNameContext(nameContext); }
  35. ~ScopedNameContext() { ScriptableImGui::PopNameContext(); }
  36. };
  37. static ScriptableImGui* GetInstance();
  38. //! This can be used to add some context around the ImGui labels that are exposed to the script system.
  39. //! Each call to PushNameContext() will add a prefix the ImGui labels to form the script field IDs.
  40. //! For example, the following will result in a script field ID of "A/B/MyButton" instead of just "MyButton".
  41. //! PushNameContext("A");
  42. //! PushNameContext("B");
  43. //! Button("MyButton",...);
  44. //! PopNameContext();
  45. //! PopNameContext();
  46. //! This is especially useful for disambiguating similar ImGui labels.
  47. //! There is also a ScopedNameContext utility class for managing the push/pop using the call stack.
  48. static void PushNameContext(const AZStd::string& nameContext);
  49. static void PopNameContext();
  50. //////////////////////////////////////////////////////////////////
  51. // ImGui bridge functions...
  52. // These follow the same API as the corresponding ImGui functions.
  53. // Add more bridge functions as needed.
  54. static bool Begin(const char* name, bool* p_open = NULL, ImGuiWindowFlags flags = 0);
  55. static void End();
  56. static bool Checkbox(const char* label, bool* v);
  57. static bool Button(const char* label, const ImVec2& size_arg = ImVec2(0, 0));
  58. static bool 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 = -1);
  59. static bool Combo(const char* label, int* current_item, const char* const items[], int items_count, int height_in_items = -1);
  60. static bool SliderInt(const char* label, int* v, int v_min, int v_max, const char* format = "%d");
  61. static bool SliderFloat(const char* label, float* v, float v_min, float v_max, const char* format = "%.3f", ImGuiSliderFlags flags = 0);
  62. static bool SliderFloat2(const char* label, float v[2], float v_min, float v_max, const char* format = "%.3f", ImGuiSliderFlags flags = 0);
  63. static bool SliderFloat3(const char* label, float v[3], float v_min, float v_max, const char* format = "%.3f", ImGuiSliderFlags flags = 0);
  64. static bool SliderAngle(const char* label, float* v, float v_min = -360.0f, float v_max = 360.0f, const char* format = "%.0f deg");
  65. static bool ColorEdit3(const char* label, float v[3], ImGuiColorEditFlags flags);
  66. static bool ColorPicker3(const char* label, float v[3], ImGuiColorEditFlags flags);
  67. static bool Selectable(const char* label, bool selected = false, ImGuiSelectableFlags flags = 0, const ImVec2& size = ImVec2(0, 0));
  68. static bool Selectable(const char* label, bool* p_selected, ImGuiSelectableFlags flags = 0, const ImVec2& size = ImVec2(0, 0));
  69. static bool TreeNodeEx(const char* label, ImGuiSelectableFlags flags = 0);
  70. static void TreePop();
  71. static bool BeginCombo(const char* label, const char* preview_value, ImGuiComboFlags flags = 0);
  72. static void EndCombo();
  73. static bool BeginMenu(const char* label, bool enabled = true);
  74. static void EndMenu();
  75. static bool MenuItem(const char* label, const char* shortcut = NULL, bool selected = false, bool enabled = true);
  76. static bool RadioButton(const char* label, int* v, int v_button);
  77. private:
  78. /////////////////////////////////////////////////////////////////////////////////////////////////
  79. // Private API for ScriptManager to call...
  80. //! Call this every frame to report errors when scripted actions aren't consumed through ImGui API function calls.
  81. //! This usually indicates that a script is trying to manipulate ImGui elements that don't exist.
  82. static void CheckAllActionsConsumed();
  83. //! Clears any scripted actions that were scheduled. This should be called every frame to make sure old actions
  84. //! don't hang around indefinitely and get consumed later and cause unexpected behavior.
  85. static void ClearActions();
  86. //! These functions are called through scripts to schedule a scripted action.
  87. //! This data will be consumed by subsequent calls to ImGui API functions.
  88. //! @param pathToImGuiItem - full path to an ImGui item, including the name context
  89. static void SetBool(const AZStd::string& name, bool value);
  90. static void SetNumber(const AZStd::string& name, float value);
  91. static void SetVector(const AZStd::string& name, const AZ::Vector2& value);
  92. static void SetVector(const AZStd::string& name, const AZ::Vector3& value);
  93. static void SetString(const AZStd::string& name, const AZStd::string& value);
  94. /////////////////////////////////////////////////////////////////////////////////////////////////
  95. using InvalidActionItem = AZStd::monostate;
  96. // Note we don't include an int type because Lua only supports floats
  97. using ActionItem = AZStd::variant<InvalidActionItem, bool, float, AZ::Vector2, AZ::Vector3, AZStd::string>;
  98. //! This utility function factors out common steps that most of the ImGui API bridge functions must perform.
  99. template<typename ActionDataT>
  100. static bool ActionHelper(
  101. const char* label,
  102. AZStd::function<bool()> imguiAction,
  103. AZStd::function<void(const AZStd::string& /*pathToImGuiItem*/)> reportScriptableAction,
  104. AZStd::function<bool(ActionDataT /*scriptArg*/)> handleScriptedAction,
  105. bool shouldReportScriptableActionAfterAnyChange = false);
  106. template<typename ImGuiActionType>
  107. static bool ThreeComponentHelper(const char* label, float v[3], ImGuiActionType& imguiAction);
  108. //! Finds a scheduled script action and removes it from the list of actions.
  109. //! @param pathToImGuiItem - full path to an ImGui item, including the name context
  110. static ActionItem FindAndRemoveAction(const AZStd::string& pathToImGuiItem);
  111. //! Makes a full script field ID path for the given ImGui label, by prefixing the current name context
  112. static AZStd::string MakeFullPath(const AZStd::string& forLabel);
  113. //! Utility function to ensure all script errors use a similar format
  114. static void ReportScriptError(const char* message);
  115. //! Provides a name context prefix to script field IDs for disambiguation.
  116. AZStd::vector<AZStd::string> m_nameContextStack;
  117. using ActionMap = AZStd::unordered_map<AZStd::string, ActionItem>;
  118. ActionMap m_scriptedActions;
  119. bool m_isInScriptedComboPopup = false;
  120. };
  121. } // namespace AtomSampleViewer