CConfigHandler.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * CConfigHandler.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "../lib/JsonNode.h"
  12. class Settings;
  13. class SettingsListener;
  14. /// Main storage of game settings
  15. class DLL_LINKAGE SettingsStorage
  16. {
  17. //Helper struct to access specific node either via chain of operator[] or with one operator() (vector)
  18. template<typename Accessor>
  19. struct DLL_LINKAGE NodeAccessor
  20. {
  21. SettingsStorage & parent;
  22. std::vector<std::string> path;
  23. NodeAccessor(SettingsStorage & _parent, std::vector<std::string> _path);
  24. NodeAccessor<Accessor> operator [] (std::string nextNode) const;
  25. NodeAccessor<Accessor> operator () (std::vector<std::string> _path) const;
  26. operator Accessor() const;
  27. };
  28. std::set<SettingsListener*> listeners;
  29. JsonNode config;
  30. bool autoSaveConfig;
  31. JsonNode & getNode(std::vector<std::string> path);
  32. // Calls all required listeners
  33. void invalidateNode(const std::vector<std::string> &changedPath);
  34. Settings get(std::vector<std::string> path);
  35. public:
  36. // Initialize config structure
  37. SettingsStorage();
  38. void init(bool autoSave=false);
  39. // Get write access to config node at path
  40. const NodeAccessor<Settings> write;
  41. // Get access to listener at path
  42. const NodeAccessor<SettingsListener> listen;
  43. //Read access, see JsonNode::operator[]
  44. const JsonNode & operator [](std::string value) const;
  45. const JsonNode & toJsonNode() const;
  46. friend class SettingsListener;
  47. friend class Settings;
  48. };
  49. /// Class for listening changes in specific part of configuration (e.g. change of music volume)
  50. class DLL_LINKAGE SettingsListener
  51. {
  52. SettingsStorage &parent;
  53. // Path to this node
  54. std::vector<std::string> path;
  55. // Callback
  56. std::function<void(const JsonNode&)> callback;
  57. SettingsListener(SettingsStorage &_parent, const std::vector<std::string> &_path);
  58. // Executes callback if changedpath begins with path
  59. void nodeInvalidated(const std::vector<std::string> & changedPath);
  60. public:
  61. SettingsListener(const SettingsListener &sl);
  62. ~SettingsListener();
  63. // assign callback function
  64. void operator()(std::function<void(const JsonNode&)> _callback);
  65. friend class SettingsStorage;
  66. };
  67. /// System options, provides write access to config tree with auto-saving on change
  68. class DLL_LINKAGE Settings
  69. {
  70. SettingsStorage &parent;
  71. //path to this node
  72. std::vector<std::string> path;
  73. JsonNode &node;
  74. JsonNode copy;
  75. //Get access to node pointed by path
  76. Settings(SettingsStorage &_parent, const std::vector<std::string> &_path);
  77. public:
  78. //Saves config if it was modified
  79. ~Settings();
  80. //Returns node selected during construction
  81. JsonNode* operator ->();
  82. const JsonNode* operator ->() const;
  83. //Helper, replaces JsonNode::operator[]
  84. JsonNode& operator [](std::string value);
  85. const JsonNode& operator [](std::string value) const;
  86. friend class SettingsStorage;
  87. };
  88. namespace config
  89. {
  90. struct DLL_LINKAGE ButtonInfo
  91. {
  92. std::string defName;
  93. std::vector<std::string> additionalDefs;
  94. int x, y; //position on the screen
  95. bool playerColoured; //if true button will be colored to main player's color (works properly only for appropriate 8bpp graphics)
  96. };
  97. /// Struct which holds data about position of several GUI elements at the adventure map screen
  98. struct DLL_LINKAGE AdventureMapConfig
  99. {
  100. //minimap properties
  101. int minimapX, minimapY, minimapW, minimapH;
  102. //statusbar
  103. int statusbarX, statusbarY; //pos
  104. std::string statusbarG; //graphic name
  105. //resdatabar
  106. int resdatabarX, resdatabarY, resDist, resDateDist, resOffsetX, resOffsetY; //pos
  107. std::string resdatabarG; //graphic name
  108. //infobox
  109. int infoboxX, infoboxY;
  110. //advmap
  111. int advmapX, advmapY, advmapW, advmapH;
  112. bool smoothMove;
  113. bool puzzleSepia;
  114. bool screenFading;
  115. bool objectFading;
  116. //general properties
  117. std::string mainGraphic;
  118. std::string worldViewGraphic;
  119. //buttons
  120. ButtonInfo kingOverview, underground, questlog, sleepWake, moveHero, spellbook, advOptions,
  121. sysOptions, nextHero, endTurn;
  122. //hero list
  123. int hlistX, hlistY, hlistSize;
  124. std::string hlistMB, hlistMN, hlistAU, hlistAD;
  125. //town list
  126. int tlistX, tlistY, tlistSize;
  127. std::string tlistAU, tlistAD;
  128. //gems
  129. int gemX[4], gemY[4];
  130. std::vector<std::string> gemG;
  131. //in-game console
  132. int inputLineLength, outputLineLength;
  133. //kingdom overview
  134. int overviewPics, overviewSize; //pic count in def and count of visible slots
  135. std::string overviewBg; //background name
  136. };
  137. struct DLL_LINKAGE GUIOptions
  138. {
  139. AdventureMapConfig ac;
  140. };
  141. /// Handles adventure map screen settings
  142. class DLL_LINKAGE CConfigHandler
  143. {
  144. GUIOptions *current; // pointer to current gui options
  145. public:
  146. typedef std::map<std::pair<int,int>, GUIOptions > GuiOptionsMap;
  147. GuiOptionsMap guiOptions;
  148. void init();
  149. CConfigHandler();
  150. ~CConfigHandler();
  151. GUIOptions *go() { return current; };
  152. void SetResolution(int x, int y)
  153. {
  154. std::pair<int,int> index(x, y);
  155. if (guiOptions.count(index) == 0)
  156. current = nullptr;
  157. else
  158. current = &guiOptions.at(index);
  159. }
  160. };
  161. }
  162. extern DLL_LINKAGE SettingsStorage settings;
  163. extern DLL_LINKAGE config::CConfigHandler conf;