ScriptableImGui.h 7.9 KB

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