settings_common.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <forward_list>
  5. #include <functional>
  6. #include <map>
  7. #include <string>
  8. #include <typeindex>
  9. #include "common/common_types.h"
  10. namespace Settings {
  11. enum class Category : u32 {
  12. Audio,
  13. Core,
  14. Cpu,
  15. CpuDebug,
  16. CpuUnsafe,
  17. Renderer,
  18. RendererAdvanced,
  19. RendererDebug,
  20. System,
  21. SystemAudio,
  22. DataStorage,
  23. Debugging,
  24. DebuggingGraphics,
  25. Miscellaneous,
  26. Network,
  27. WebService,
  28. AddOns,
  29. Controls,
  30. Ui,
  31. UiGeneral,
  32. UiLayout,
  33. UiGameList,
  34. Screenshots,
  35. Shortcuts,
  36. Multiplayer,
  37. Services,
  38. Paths,
  39. MaxEnum,
  40. };
  41. enum class Specialization : u32 {
  42. Default,
  43. Time,
  44. Hex,
  45. List,
  46. RuntimeList,
  47. Scalar,
  48. Countable,
  49. Paired,
  50. };
  51. bool IsConfiguringGlobal();
  52. void SetConfiguringGlobal(bool is_global);
  53. class BasicSetting;
  54. class Linkage {
  55. public:
  56. explicit Linkage(u32 initial_count = 0);
  57. ~Linkage();
  58. std::map<Category, std::forward_list<BasicSetting*>> by_category{};
  59. std::vector<std::function<void()>> restore_functions{};
  60. u32 count;
  61. };
  62. /**
  63. * BasicSetting is an abstract class that only keeps track of metadata. The string methods are
  64. * available to get data values out.
  65. */
  66. class BasicSetting {
  67. protected:
  68. explicit BasicSetting(Linkage& linkage, const std::string& name, enum Category category_,
  69. bool save_, bool runtime_modifiable_, Specialization spec,
  70. BasicSetting* other_setting);
  71. public:
  72. virtual ~BasicSetting();
  73. /*
  74. * Data retrieval
  75. */
  76. /**
  77. * Returns a string representation of the internal data. If the Setting is Switchable, it
  78. * respects the internal global state: it is based on GetValue().
  79. *
  80. * @returns A string representation of the internal data.
  81. */
  82. [[nodiscard]] virtual std::string ToString() const = 0;
  83. /**
  84. * Returns a string representation of the global version of internal data. If the Setting is
  85. * not Switchable, it behaves like ToString.
  86. *
  87. * @returns A string representation of the global version of internal data.
  88. */
  89. [[nodiscard]] virtual std::string ToStringGlobal() const;
  90. /**
  91. * @returns A string representation of the Setting's default value.
  92. */
  93. [[nodiscard]] virtual std::string DefaultToString() const = 0;
  94. /**
  95. * Returns a string representation of the minimum value of the setting. If the Setting is not
  96. * ranged, the string represents the default initialization of the data type.
  97. *
  98. * @returns A string representation of the minimum value of the setting.
  99. */
  100. [[nodiscard]] virtual std::string MinVal() const = 0;
  101. /**
  102. * Returns a string representation of the maximum value of the setting. If the Setting is not
  103. * ranged, the string represents the default initialization of the data type.
  104. *
  105. * @returns A string representation of the maximum value of the setting.
  106. */
  107. [[nodiscard]] virtual std::string MaxVal() const = 0;
  108. /**
  109. * Takes a string input, converts it to the internal data type if necessary, and then runs
  110. * SetValue with it.
  111. *
  112. * @param load String of the input data.
  113. */
  114. virtual void LoadString(const std::string& load) = 0;
  115. /**
  116. * Returns a string representation of the data. If the data is an enum, it returns a string of
  117. * the enum value. If the internal data type is not an enum, this is equivalent to ToString.
  118. *
  119. * e.g. renderer_backend.Canonicalize() == "OpenGL"
  120. *
  121. * @returns Canonicalized string representation of the internal data
  122. */
  123. [[nodiscard]] virtual std::string Canonicalize() const = 0;
  124. /*
  125. * Metadata
  126. */
  127. /**
  128. * @returns A unique identifier for the Setting's internal data type.
  129. */
  130. [[nodiscard]] virtual std::type_index TypeId() const = 0;
  131. /**
  132. * Returns true if the Setting's internal data type is an enum.
  133. *
  134. * @returns True if the Setting's internal data type is an enum
  135. */
  136. [[nodiscard]] virtual constexpr bool IsEnum() const = 0;
  137. /**
  138. * Returns true if the current setting is Switchable.
  139. *
  140. * @returns If the setting is a SwitchableSetting
  141. */
  142. [[nodiscard]] virtual constexpr bool Switchable() const {
  143. return false;
  144. }
  145. /**
  146. * Returns true to suggest that a frontend can read or write the setting to a configuration
  147. * file.
  148. *
  149. * @returns The save preference
  150. */
  151. [[nodiscard]] bool Save() const;
  152. /**
  153. * @returns true if the current setting can be changed while the guest is running.
  154. */
  155. [[nodiscard]] bool RuntimeModfiable() const;
  156. /**
  157. * @returns A unique number corresponding to the setting.
  158. */
  159. [[nodiscard]] constexpr u32 Id() const {
  160. return id;
  161. }
  162. /**
  163. * Returns the setting's category AKA INI group.
  164. *
  165. * @returns The setting's category
  166. */
  167. [[nodiscard]] enum Category Category() const;
  168. /**
  169. * @returns Extra metadata for data representation in frontend implementations.
  170. */
  171. [[nodiscard]] enum Specialization Specialization() const;
  172. /**
  173. * @returns Another BasicSetting if one is paired, or nullptr otherwise.
  174. */
  175. [[nodiscard]] BasicSetting* PairedSetting() const;
  176. /**
  177. * Returns the label this setting was created with.
  178. *
  179. * @returns A reference to the label
  180. */
  181. [[nodiscard]] const std::string& GetLabel() const;
  182. /**
  183. * @returns If the Setting checks input values for valid ranges.
  184. */
  185. [[nodiscard]] virtual constexpr bool Ranged() const = 0;
  186. /*
  187. * Switchable settings
  188. */
  189. /**
  190. * Sets a setting's global state. True means use the normal setting, false to use a custom
  191. * value. Has no effect if the Setting is not Switchable.
  192. *
  193. * @param global The desired state
  194. */
  195. virtual void SetGlobal(bool global);
  196. /**
  197. * Returns true if the setting is using the normal setting value. Always true if the setting is
  198. * not Switchable.
  199. *
  200. * @returns The Setting's global state
  201. */
  202. [[nodiscard]] virtual bool UsingGlobal() const;
  203. private:
  204. const std::string label; ///< The setting's label
  205. const enum Category category; ///< The setting's category AKA INI group
  206. const u32 id; ///< Unique integer for the setting
  207. const bool save; ///< Suggests if the setting should be saved and read to a frontend config
  208. const bool
  209. runtime_modifiable; ///< Suggests if the setting can be modified while a guest is running
  210. const enum Specialization
  211. specialization; ///< Extra data to identify representation of a setting
  212. BasicSetting* const other_setting; ///< A paired setting
  213. };
  214. } // namespace Settings