CConfigHandler.h 5.3 KB

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