imgui_manager.h (4967B)
1 // SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <stenzek@gmail.com> 2 // SPDX-License-Identifier: (GPL-3.0 OR PolyForm-Strict-1.0.0) 3 4 #pragma once 5 6 #include "common/types.h" 7 #include <span> 8 #include <string> 9 #include <vector> 10 11 class Error; 12 13 struct ImFont; 14 15 union InputBindingKey; 16 enum class GenericInputBinding : u8; 17 18 namespace ImGuiManager { 19 20 using WCharType = u32; 21 22 /// Sets the path to the font to use. Empty string means to use the default. 23 void SetFontPathAndRange(std::string path, std::vector<WCharType> range); 24 25 /// Sets the emoji font range to use. Empty means no glyphs will be rasterized. 26 /// Should NOT be terminated with zeros, unlike the font range above. 27 void SetEmojiFontRange(std::vector<WCharType> range); 28 29 /// Returns a compacted font range, with adjacent glyphs merged into one pair. 30 std::vector<WCharType> CompactFontRange(std::span<const WCharType> range); 31 32 /// Changes the global scale. 33 void SetGlobalScale(float global_scale); 34 35 /// Changes whether OSD messages are silently dropped. 36 void SetShowOSDMessages(bool enable); 37 38 /// Initializes ImGui, creates fonts, etc. 39 bool Initialize(float global_scale, bool show_osd_messages, Error* error); 40 41 /// Frees all ImGui resources. 42 void Shutdown(); 43 44 /// Returns the size of the display window. Can be safely called from any thread. 45 float GetWindowWidth(); 46 float GetWindowHeight(); 47 48 /// Updates internal state when the window is size. 49 void WindowResized(float width, float height); 50 51 /// Updates scaling of the on-screen elements. 52 void RequestScaleUpdate(); 53 54 /// Call at the beginning of the frame to set up ImGui state. 55 void NewFrame(); 56 57 /// Renders any on-screen display elements. 58 void RenderOSDMessages(); 59 60 /// Returns the scale of all on-screen elements. 61 float GetGlobalScale(); 62 63 /// Returns true if fullscreen fonts are present. 64 bool HasFullscreenFonts(); 65 66 /// Allocates/adds fullscreen fonts if they're not loaded. 67 bool AddFullscreenFontsIfMissing(); 68 69 /// Returns the standard font for external drawing. 70 ImFont* GetStandardFont(); 71 72 /// Returns the fixed-width font for external drawing. 73 ImFont* GetFixedFont(); 74 75 /// Returns the medium font for external drawing, scaled by ImGuiFullscreen. 76 /// This font is allocated on demand. 77 ImFont* GetMediumFont(); 78 79 /// Returns the large font for external drawing, scaled by ImGuiFullscreen. 80 /// This font is allocated on demand. 81 ImFont* GetLargeFont(); 82 83 /// Returns true if imgui wants to intercept text input. 84 bool WantsTextInput(); 85 86 /// Returns true if imgui wants to intercept mouse input. 87 bool WantsMouseInput(); 88 89 /// Called on the UI or CPU thread in response to a key press. String is UTF-8. 90 void AddTextInput(std::string str); 91 92 /// Called on the UI or CPU thread in response to mouse movement. 93 void UpdateMousePosition(float x, float y); 94 95 /// Called on the CPU thread in response to a mouse button press. 96 /// Returns true if ImGui intercepted the event, and regular handlers should not execute. 97 bool ProcessPointerButtonEvent(InputBindingKey key, float value); 98 99 /// Called on the CPU thread in response to a mouse wheel movement. 100 /// Returns true if ImGui intercepted the event, and regular handlers should not execute. 101 bool ProcessPointerAxisEvent(InputBindingKey key, float value); 102 103 /// Called on the CPU thread in response to a key press. 104 /// Returns true if ImGui intercepted the event, and regular handlers should not execute. 105 bool ProcessHostKeyEvent(InputBindingKey key, float value); 106 107 /// Called on the CPU thread when any input event fires. Allows imgui to take over controller navigation. 108 bool ProcessGenericInputEvent(GenericInputBinding key, float value); 109 110 /// Sets an image and scale for a software cursor. Software cursors can be used for things like crosshairs. 111 void SetSoftwareCursor(u32 index, std::string image_path, float image_scale, u32 multiply_color = 0xFFFFFF); 112 bool HasSoftwareCursor(u32 index); 113 void ClearSoftwareCursor(u32 index); 114 115 /// Sets the position of a software cursor, used when we have relative coordinates such as controllers. 116 void SetSoftwareCursorPosition(u32 index, float pos_x, float pos_y); 117 118 /// Adds software cursors to ImGui render list. 119 void RenderSoftwareCursors(); 120 121 /// Strips icon characters from a string. 122 std::string StripIconCharacters(std::string_view str); 123 } // namespace ImGuiManager 124 125 namespace Host { 126 /// Typical durations for OSD messages. 127 static constexpr float OSD_CRITICAL_ERROR_DURATION = 20.0f; 128 static constexpr float OSD_ERROR_DURATION = 15.0f; 129 static constexpr float OSD_WARNING_DURATION = 10.0f; 130 static constexpr float OSD_INFO_DURATION = 5.0f; 131 static constexpr float OSD_QUICK_DURATION = 2.5f; 132 133 /// Adds OSD messages, duration is in seconds. 134 void AddOSDMessage(std::string message, float duration = 2.0f); 135 void AddKeyedOSDMessage(std::string key, std::string message, float duration = 2.0f); 136 void AddIconOSDMessage(std::string key, const char* icon, std::string message, float duration = 2.0f); 137 void RemoveKeyedOSDMessage(std::string key); 138 void ClearOSDMessages(); 139 } // namespace Host