ScriptableImGui.h 8.0 KB

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